From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 373FEC77B61 for ; Tue, 25 Apr 2023 03:55:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232693AbjDYDzY (ORCPT ); Mon, 24 Apr 2023 23:55:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229637AbjDYDzX (ORCPT ); Mon, 24 Apr 2023 23:55:23 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83575AD0F; Mon, 24 Apr 2023 20:55:21 -0700 (PDT) Received: from dggpemm500016.china.huawei.com (unknown [172.30.72.53]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4Q57RK54VWzKvMB; Tue, 25 Apr 2023 11:54:21 +0800 (CST) Received: from [10.67.110.48] (10.67.110.48) by dggpemm500016.china.huawei.com (7.185.36.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Tue, 25 Apr 2023 11:55:18 +0800 Message-ID: <0f3abe0f-216b-dda6-38c4-26ffa79d966f@huawei.com> Date: Tue, 25 Apr 2023 11:55:18 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH RFC] Randomized slab caches for kmalloc() Content-Language: en-US To: Alexander Lobakin CC: Hyeonggon Yoo <42.hyeyoo@gmail.com>, Dennis Zhou , Tejun Heo , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Alexander Potapenko , Marco Elver , Dmitry Vyukov , , , , Kees Cook , , Paul Moore , , James Morris , Wang Weiyang , Xiu Jianfeng References: <20230315095459.186113-1-gongruiqi1@huawei.com> <36019eb3-4b71-26c4-21ad-b0e0eabd0ca5@intel.com> From: Gong Ruiqi In-Reply-To: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.110.48] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemm500016.china.huawei.com (7.185.36.25) X-CFilter-Loop: Reflected Precedence: bulk List-ID: On 2023/04/24 21:46, Alexander Lobakin wrote: > From: Gong, Ruiqi > Date: Mon, 24 Apr 2023 10:54:33 +0800 > > ... > >> >>> It's fast enough according to Jason... `_RET_IP_ % nr` doesn't sound >>> "secure" to me. It really is a compile-time constant, which can be >>> calculated (or not?) manually. Even if it wasn't, `% nr` doesn't sound >>> good, there should be at least hash_32(). >> >> Yes, `_RET_IP_ % nr` is a bit naive. Currently the patch is more like a >> PoC so I wrote this. Indeed a proper hash function should be used here. >> >> And yes _RET_IP_ could somehow be manually determined especially for >> kernels without KASLR, and I think adding a per-boot random seed into >> the selection could solve this. > > I recall how it is done for kCFI/FineIBT in the x86 code -- it also uses > per-boot random seed (although it gets patched into the code itself each > time, when applying alternatives). So probably should be optimal enough. > The only thing I'm wondering is where to store this per-boot seed :D > It's generic code, so you can't patch it directly. OTOH storing it in > .data/.bss can make it vulnerable to attacks... Can't it? I think marking the seed with __ro_after_init is enough, since we don't mind it could be read by the attacker. Given that the code paths the attacker can utilize to spray the heap is limited, our address-related randomness in most cases prevents kmalloc()s on these paths from picking the same cache the vulnerable subsystem/module would pick. Although _RET_IP_ of kmalloc()s could be known, without tampering the source code and rebuilding the image, the attacker can't do anything to make those caches collide if the cache selection algorithm says they don't. So in my perspective the per-boot random seed is more like an enhancement: if one day, by analyzing the open source code, the attacker does find a usable kmalloc that happens to pick the same cache with the vulnerable subsystem/module, the seed could make his/her effort wasted ;) > >> >> I will implement these in v2. Thanks! >> >>> >>> Thanks, >>> Olek >>> > > Thanks, > Olek