Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp2180309rwp; Fri, 14 Jul 2023 01:58:26 -0700 (PDT) X-Google-Smtp-Source: APBJJlGVoba3jkJtmRcOxOICeJ98ZzPRklQniumjCPhnMlA0491XFlhboAG1PRfBer9DwZ0489/9 X-Received: by 2002:a05:6808:e8a:b0:3a1:f1b7:75b0 with SMTP id k10-20020a0568080e8a00b003a1f1b775b0mr4649059oil.19.1689325105806; Fri, 14 Jul 2023 01:58:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689325105; cv=none; d=google.com; s=arc-20160816; b=DOdNhOpt1LktbnJkkTuVmAwMbf5vlfoIKv8aM7rVVavEmf0BpBn9Fykw1tYkGPT7q6 ivDao0ciJP2eQpx+Ejyy38Zx/6rB1MV94ebUk5h1zmB2dNmXCVKGtBtJi4EfLnZM6bWv xL9+lfstYh5N9YKBmCjIUT58Zip7ZODE7vZ1QFB65rV6izxjY0y23Ef2vxcoD2RHvySO FhGredX6FzdKr8ybsciaEn34ZgJk2D8VAy++YMHaxwQSc1V0LHjy7e8ZrErQZ7+WKMru F5+KlXz0wVkRCeG3ox0S+GDfAXL/+vAarykTnjGhYSx49F/AlRFHN34ixeE+7dA3mVVb iC2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature:dkim-signature; bh=fks1WScsSrOgkleCsjnUGYtvL8hJjpfw+GXcKVrd4Cc=; fh=WEipalAs3D/v4mNLC0prgmXMdl9nT8TE47ew0USWZ/k=; b=GJ6PUPRWSjDsA+4kBrNcozRTYrK2MBOtkFZpmZsNm5sfnDufabVPd4xTWCAdLFE8lc pqNGSFa9FW6lN1aIbGrQrjbq/dVtpU3fgFOhxhuJB0nQHzAjiaa+ne5Kkdp/B30vyGNL rzwfskV6mSTH5OyVUFi3apYA4lgNQYFfdVEpZJ8OKjJbPSUfj88nOD29FHyVJFYahGbW DWdnisH7KZiOB8pSErnke5WQf2RohcMmMpVqfZ4joGt0RaEHdHyB1n6Rj1ktCjaMz9KS CNayUIutFVxu1woJ8GxeygepJ6aBfj1zYVAkFzMFQf+f1TNXTI4mpiGVnDLUe7lzuksG cmSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=WulcpL0q; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h132-20020a636c8a000000b0055bc30dbcd0si6415035pgc.742.2023.07.14.01.58.12; Fri, 14 Jul 2023 01:58:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=WulcpL0q; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235297AbjGNI0M (ORCPT + 99 others); Fri, 14 Jul 2023 04:26:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234889AbjGNI0K (ORCPT ); Fri, 14 Jul 2023 04:26:10 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E0869B; Fri, 14 Jul 2023 01:26:09 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 43CA91FD8E; Fri, 14 Jul 2023 08:26:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1689323168; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fks1WScsSrOgkleCsjnUGYtvL8hJjpfw+GXcKVrd4Cc=; b=WulcpL0qvQltYlR9ph9oGtrmlDGzPveFHunO2MGoiLrwrMpp9JHS4u16B4gHtodoBuGZCS CKCo2FxPqOueJOeaZlsjfrGRIba6MSZDlRvbCet4KO6g/2t5skOpN8DRFsq4RWQAXdOgL9 JKLB4M/ftVP5RQh1S2j8Z8MuhsRNbPc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1689323168; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fks1WScsSrOgkleCsjnUGYtvL8hJjpfw+GXcKVrd4Cc=; b=DERqA/i5gSqcbt3I3iT9HLa67XOi7yzmv5oKPmqERX0k+bLFm6qlso+QHecdA3yu1psp6D FAzxXJJz2h1/2HCQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id DB98413A15; Fri, 14 Jul 2023 08:26:07 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id G1jjNJ8GsWQOVAAAMHmgww (envelope-from ); Fri, 14 Jul 2023 08:26:07 +0000 Message-ID: Date: Fri, 14 Jul 2023 10:26:07 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH v5] Randomized slab caches for kmalloc() Content-Language: en-US To: "GONG, Ruiqi" , Andrew Morton , Joonsoo Kim , David Rientjes , Pekka Enberg , Christoph Lameter , Tejun Heo , Dennis Zhou , Alexander Potapenko , Marco Elver , Kees Cook , Jann Horn Cc: Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Dmitry Vyukov , Alexander Lobakin , Pedro Falcato , Paul Moore , James Morris , "Serge E . Hallyn" , Wang Weiyang , Xiu Jianfeng , linux-mm@kvack.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, gongruiqi1@huawei.com References: <20230714064422.3305234-1-gongruiqi@huaweicloud.com> From: Vlastimil Babka In-Reply-To: <20230714064422.3305234-1-gongruiqi@huaweicloud.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_SOFTFAIL,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 7/14/23 08:44, GONG, Ruiqi wrote: > When exploiting memory vulnerabilities, "heap spraying" is a common > technique targeting those related to dynamic memory allocation (i.e. the > "heap"), and it plays an important role in a successful exploitation. > Basically, it is to overwrite the memory area of vulnerable object by > triggering allocation in other subsystems or modules and therefore > getting a reference to the targeted memory location. It's usable on > various types of vulnerablity including use after free (UAF), heap out- > of-bound write and etc. > > There are (at least) two reasons why the heap can be sprayed: 1) generic > slab caches are shared among different subsystems and modules, and > 2) dedicated slab caches could be merged with the generic ones. > Currently these two factors cannot be prevented at a low cost: the first > one is a widely used memory allocation mechanism, and shutting down slab > merging completely via `slub_nomerge` would be overkill. > > To efficiently prevent heap spraying, we propose the following approach: > to create multiple copies of generic slab caches that will never be > merged, and random one of them will be used at allocation. The random > selection is based on the address of code that calls `kmalloc()`, which > means it is static at runtime (rather than dynamically determined at > each time of allocation, which could be bypassed by repeatedly spraying > in brute force). In other words, the randomness of cache selection will > be with respect to the code address rather than time, i.e. allocations > in different code paths would most likely pick different caches, > although kmalloc() at each place would use the same cache copy whenever > it is executed. In this way, the vulnerable object and memory allocated > in other subsystems and modules will (most probably) be on different > slab caches, which prevents the object from being sprayed. > > Meanwhile, the static random selection is further enhanced with a > per-boot random seed, which prevents the attacker from finding a usable > kmalloc that happens to pick the same cache with the vulnerable > subsystem/module by analyzing the open source code. In other words, with > the per-boot seed, the random selection is static during each time the > system starts and runs, but not across different system startups. > > The overhead of performance has been tested on a 40-core x86 server by > comparing the results of `perf bench all` between the kernels with and > without this patch based on the latest linux-next kernel, which shows > minor difference. A subset of benchmarks are listed below: > > sched/ sched/ syscall/ mem/ mem/ > messaging pipe basic memcpy memset > (sec) (sec) (sec) (GB/sec) (GB/sec) > > control1 0.019 5.459 0.733 15.258789 51.398026 > control2 0.019 5.439 0.730 16.009221 48.828125 > control3 0.019 5.282 0.735 16.009221 48.828125 > control_avg 0.019 5.393 0.733 15.759077 49.684759 > > experiment1 0.019 5.374 0.741 15.500992 46.502976 > experiment2 0.019 5.440 0.746 16.276042 51.398026 > experiment3 0.019 5.242 0.752 15.258789 51.398026 > experiment_avg 0.019 5.352 0.746 15.678608 49.766343 > > The overhead of memory usage was measured by executing `free` after boot > on a QEMU VM with 1GB total memory, and as expected, it's positively > correlated with # of cache copies: > > control 4 copies 8 copies 16 copies > > total 969.8M 968.2M 968.2M 968.2M > used 20.0M 21.9M 24.1M 26.7M > free 936.9M 933.6M 931.4M 928.6M > available 932.2M 928.8M 926.6M 923.9M > > Co-developed-by: Xiu Jianfeng > Signed-off-by: Xiu Jianfeng > Signed-off-by: GONG, Ruiqi > Reviewed-by: Kees Cook Thanks! Pushed to slab/for-6.6/random_kmalloc and for-next.