Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp21274084rwd; Thu, 29 Jun 2023 13:36:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7Lp25uU0jq8Q/7j7EXeJIGIdkqmlNVKSY787qFMEHL1YRxx4XmZHFbcHZFqnwFmKsE3rZH X-Received: by 2002:a05:6a20:7487:b0:11f:33da:56ec with SMTP id p7-20020a056a20748700b0011f33da56ecmr652921pzd.27.1688070982025; Thu, 29 Jun 2023 13:36:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688070982; cv=none; d=google.com; s=arc-20160816; b=Cw3NSWznFVGkHSqZpynrXHmpPq10gmyE7NGxlF0qIH8luAiMkrXh8jAj26tW0ct9PL WGv7RFgq/UIolFTs2b8Sc3XVi3UKQizaBsFCpWdiOmJ2Y/QJ5/lZAUPyDngOB9DK370G 6s1e4oJLe0LmHfbYKzyPnEf2YBrJl5dSBVU6LLEuaZ02Mgo9sDQPXzsCXPm9L+mENTzM AeIt1KgbqK3BVj2lqUWi471zIL2K7A8A3zV/e274fukki8tVP1rWUEoyPQNgh07HvQM1 Vk3O+w4TXgXOS5pWoQvllPGZ5X/XpDf14gb/vSwcAJsL77gBOZO1Zid38F3rz68IOnFe DlBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=79zP5YHFLGBu1+jfogz5W0vV+Z/KFoVgf5MKidFMDlI=; fh=oHG++I3DWFrdy1rFxyUnbhcyiO48BEpx66rAxDKpchs=; b=mwaDD9xvHaLLr27+pGumMldz2dGhOEeWlXKPANuCvAeLmXkno2/FH3vSaATetTsBel OkOAyPB6UgYQ2dxO7DaRBW5eBEJ0yI3yLDxp1tWgQTNJ0uGjulY2/DZ5JJ8W6l8ZN2E+ q48I56f666I//36C9zKgpvsL+aNk8Al+l6/pLXTJhJR+NviIumbXCyrddVcccfN7TZMe vjhQJoBdnSi5nVXSUx/BEoapJUk3ZimjiFmMw8XRpIc9AMnZ6uxj89DSaCtFtOvG7JwA IU04ouQpPNKgtV5tZtZMeLS7ywZ+gJAsl3+16Sm06wAt8rltwt3jc3BVUktWh/lYcleq RExw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="COCl/U/c"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w6-20020a63fb46000000b00553ebb05d14si10948445pgj.111.2023.06.29.13.36.07; Thu, 29 Jun 2023 13:36:21 -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=@chromium.org header.s=google header.b="COCl/U/c"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231963AbjF2T4N (ORCPT + 99 others); Thu, 29 Jun 2023 15:56:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232259AbjF2T4K (ORCPT ); Thu, 29 Jun 2023 15:56:10 -0400 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9D112D50 for ; Thu, 29 Jun 2023 12:56:02 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-666e6541c98so1014160b3a.2 for ; Thu, 29 Jun 2023 12:56:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1688068562; x=1690660562; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=79zP5YHFLGBu1+jfogz5W0vV+Z/KFoVgf5MKidFMDlI=; b=COCl/U/c9SsKrj00+ygesw3WObTx3Osmqaz88cXeEi0v16GehgR4AlXwfn6v4ktv5I C10xGRe6FXsRNz9GEZSDN0VUenIdW6qee5G9xjkEUo9iK7AYDUHEUJP+LhlE4uP5GoTc yWmHeuNNBPf3DUPsveyOZm//CrDB/mUonJ6zE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688068562; x=1690660562; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=79zP5YHFLGBu1+jfogz5W0vV+Z/KFoVgf5MKidFMDlI=; b=X8oenbZf/LVGmWqjoVAPjpRFUmyesIgRTXXENjTsRjbF9a7PYjsHfQaGlFSLevbREr LXzs3VqUtcZfYEumpKUj9dwFwI9S8oEI6EIiKC7Q2Fu56tf5s/e6woeiSyPjH9Fo7MG3 +6aV4lR3BtIstMxWE9a74+i70uXOtz0n2k+5x69J1JoO8j7dw8KjCJCTFig2wHe3/MlZ YgkJSjXJP0k0mH+walsC8QiOhL93Q1OSVGLelClL1gC3PjBb5gDDbZZ1DOQoFbjTNzpR 6VzdERu+pNKKeK3oGbyFj/bSJt2kzoq56/XlpGLJ1a2n3Ucv2LfQ/vI+HONGX10vLC10 o6+A== X-Gm-Message-State: ABy/qLY64MJqxbB3Asv1OMerKvdq6XVi+hjCoJGq5tgEbb2OjKS7DGKt y+g72RNDoQOWwFs6MzHAJ1jPug== X-Received: by 2002:a05:6a00:2d1d:b0:680:98c:c595 with SMTP id fa29-20020a056a002d1d00b00680098cc595mr1171263pfb.13.1688068562212; Thu, 29 Jun 2023 12:56:02 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id h18-20020a62b412000000b0067c9ff80b64sm4590241pfn.113.2023.06.29.12.56.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jun 2023 12:56:01 -0700 (PDT) Date: Thu, 29 Jun 2023 12:56:00 -0700 From: Kees Cook To: "GONG, Ruiqi" Cc: Vlastimil Babka , Andrew Morton , Joonsoo Kim , David Rientjes , Pekka Enberg , Christoph Lameter , Tejun Heo , Dennis Zhou , Alexander Potapenko , Marco Elver , Jann Horn , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Dmitry Vyukov , Alexander Lobakin , Pedro Falcato , Paul Moore , James Morris , "Serge E . Hallyn" , Markus Elfring , Wang Weiyang , Xiu Jianfeng , linux-mm@kvack.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, gongruiqi1@huawei.com Subject: Re: [PATCH v4] Randomized slab caches for kmalloc() Message-ID: <202306291252.F916884987@keescook> References: <20230626031835.2279738-1-gongruiqi@huaweicloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230626031835.2279738-1-gongruiqi@huaweicloud.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Mon, Jun 26, 2023 at 11:18:35AM +0800, 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 for the v4; this looks good. :) -- Kees Cook