Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp775616lqa; Sun, 28 Apr 2024 04:03:05 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWvb2I9Izjqw3TYd9TmKfjiJGk2IckWP3JY7btJ+PA5b+w7CpE3C4SlyrCwucPYnXg00UHNXVS9xhFOWMl15CVWSHfWdQUt6hMI1C+bcw== X-Google-Smtp-Source: AGHT+IE17vrIhqfpuvosUf7vB/bpgatFxRBbK1JDZVGsD7N96a0Wr2ZJTDDJ0hckYdkMq2NzH3qH X-Received: by 2002:aca:1a01:0:b0:3c5:f333:3819 with SMTP id a1-20020aca1a01000000b003c5f3333819mr4673766oia.21.1714302185148; Sun, 28 Apr 2024 04:03:05 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714302185; cv=pass; d=google.com; s=arc-20160816; b=E1I2JItRT39HPtPvVUy4VJAZDDTsPH+be/vFUqWbXL+g7fLsy1xmbz3mFSSySPJLui pDDgasp1IZXzZ2AGQbCvhrdyD0pih4cQvt3qePZBSXdA+JY3qb4HkiAbIzip88d1s1sF 1ikzrbDJLKvx0B8vSMe8nGZtLR/5GD5D5IgQyDCBKaRMemVhLvrVa4WOzLuBxM7I4+ZH wCrfHJrOXvClSxkphp7nXsssLjJYvhw7R8w5MF/1Zh9MBq5SKQg7cpkQD++e2jJ6QPV0 8gbKhUARNRvscrIWRIvLZk08fqLiDNwHZVBiebxlzPISUNtKwxZRFnn08WCySLM2L5MM 8how== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:date :dkim-signature:message-id; bh=pVVFme9IGZxZcwcArhmhbXXpkSUt5435Tn6KlathRfY=; fh=+3nQf03clEaOAa6UN2KyQidcmyGcEN9K9oGbeVZ2AoE=; b=uE3RNXTxduWyowOl+aaZhIHafWQL+n8bgWTjAFO9KQaLYzJ7T9N3UD/v4UTS+IWhy3 fmcemfKywiTGgyFe+eMFl5IxEz+ol+yowkgCEo4N5BIn86ki+zq2iYAxWMu9wLT5OUjh IzWKYkIJrJlAP/nKhUWPDI4nrMh2yK6V/ENvowCX44B4fMw0rSUlr4bsXZJW9RyLaaYB p8y+vcI1xNmPCdyJrz0Q7jf6LjmaFPkdtnHCtVu0EHWhbi2rE+Lct+9J+in5m177rcxa ZMLV+w3kVplTjrtmy8dFjcbvpp10HkvIPeUf03QE0xVfWos+o/hCE3eS8Lt2AOHs9IbU TElg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@dustri.org header.s=key1 header.b=LJ0ZG0CV; arc=pass (i=1 spf=pass spfdomain=dustri.org dkim=pass dkdomain=dustri.org dmarc=pass fromdomain=dustri.org); spf=pass (google.com: domain of linux-kernel+bounces-161391-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161391-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=dustri.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id n3-20020a05622a11c300b0043aa9637d21si2808815qtk.76.2024.04.28.04.03.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Apr 2024 04:03:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-161391-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@dustri.org header.s=key1 header.b=LJ0ZG0CV; arc=pass (i=1 spf=pass spfdomain=dustri.org dkim=pass dkdomain=dustri.org dmarc=pass fromdomain=dustri.org); spf=pass (google.com: domain of linux-kernel+bounces-161391-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161391-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=dustri.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id CA1141C210DC for ; Sun, 28 Apr 2024 11:03:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 05A805675F; Sun, 28 Apr 2024 11:02:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=dustri.org header.i=@dustri.org header.b="LJ0ZG0CV" Received: from out-177.mta1.migadu.com (out-177.mta1.migadu.com [95.215.58.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD59D524A2 for ; Sun, 28 Apr 2024 11:02:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714302177; cv=none; b=aap0oB/KPGf/MkVg6ElX0q38ncC3mAxLnRjkqIyu5+ZKz1EoenggZ52Tros7+JVXl37dDwgoZDfC7SObQ41h2kPYI7lQHA4od0vVw0rxrlDRfieosttgCr+jvhip//cJav+yi7+7JEwSbTVyUCDq/QTgoTJcmmHEENJbXV69EYY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714302177; c=relaxed/simple; bh=2b6gYCoJF9gtM5xiJ8Jd3+6VlpnCIEJEXN4AknJPzHI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=efi8UxD+9kXpAchmPbjmTDVh72Xbqqn8lyC/c2pOj5BOxdJRyjkw8nSueenLPeRwK+5bPIlFzHswfATB9syQhrUDpAQLFB02k704qX6mCA1Ga3PWf7g/fO5Bc8C0NbBmgHLGi4cTD655zb2CMmc1UbZJzrmWQ9I/iCm0SPG+M98= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=dustri.org; spf=pass smtp.mailfrom=dustri.org; dkim=pass (2048-bit key) header.d=dustri.org header.i=@dustri.org header.b=LJ0ZG0CV; arc=none smtp.client-ip=95.215.58.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=dustri.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=dustri.org Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dustri.org; s=key1; t=1714302171; h=from:from:reply-to:subject:subject: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:autocrypt:autocrypt; bh=pVVFme9IGZxZcwcArhmhbXXpkSUt5435Tn6KlathRfY=; b=LJ0ZG0CVcsv9NBeC7oXAVzCp8DNIyDjzDzpmJwgI+ywplahcnxaBpn6DOV4eAcdYC803TS 6ctEcHggsNJkFsxrmE5eu64AmZ3nLKIJw+j0pv1hHuEKlLiDww4vPfXxFcUiSDv75SXONE 6vLeQEjumyb6fpYAZ6b96TTa1c04BazYBfIcUkKEUoJiYKSAd5NJWLBHvT2HSaAfwEQaSB +lfODkoOzH0jfX/Vu8glrcTLMn1mT5upZel7u3B7iqMd6PJGU1WxlRn1SkXfMKrp/YdXsD 7cSjyKQcAd338B5vRoSE8fJhRocVcVH7CE2+fPrpiZA4uAkYhWeC79hXJcOhmQ== Date: Sun, 28 Apr 2024 13:02:36 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH v3 0/6] slab: Introduce dedicated bucket allocator To: Kees Cook , Vlastimil Babka Cc: Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, "GONG, Ruiqi" , Xiu Jianfeng , Suren Baghdasaryan , Kent Overstreet , Jann Horn , Matteo Rizzo , Thomas Graf , Herbert Xu , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org References: <20240424213019.make.366-kees@kernel.org> Content-Language: en-US X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: jvoisin Autocrypt: addr=julien.voisin@dustri.org; keydata= xsFNBFWzxaABEACu3G1fwzHtrhHuotgvZ69zA4YqF9vYfx7hoYrjnKzP5pTiOZ2US6AJj1qE W1WlN6cHnqzzqoXotVu/MPuPrbadL21jRnJWurrkktpcqK4BaCZ5S0lOQ3ck40LysidexhI6 ZZi6jhBZzuzxs2Mi9aIPIxDekXAWQBybs4m27E4MNmJkIshVnDTMQ4ToGQxzwPj+VurpQVPh WGMCPwlUbVkN/w6N/lLC088ePpESh5E0vFE+BQc66ZpRn+cXTlaqjQnwRtWuEBoqJSn2MXAn wODEj4H5HvQjgFyRfmOHHMTEHOg4yyc84SmIv8YJlTbVX7VnMGUJF43SA4PFtXFypBkQ481u w10XdBPYwD/i0q3QnzzRiIsrlQJUCkGFxyIpcDNRnf3ApjT4+QuEaw98tKvgRzCozFx2D94w sSFz858vZrdYj4pt/VYw8JeoPDiWwuzPVvgpJmQlL8aCRnAhLIv9O+fySvXcGh1WEvtUgkNn 1WjU2M00BYnPNFBEeGMRWkxuVwV1o+WKNJfwg2UcDghSkJGBCPCAiC2fDlfyk3njjLjxZHP/ mYNwUkxTlQolzknJZ5wg7vbE6r4rfQX4gTi3mNzYtqUAb17GIczOARZK7qdSapObrXPFGgX3 Bd4FZJEaIq3p5xWcWS8fcMveoYO7m9cyaSkSQxAPrP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NzoVs/PtIuWb/y5BgOBGD9zUn4Z2FjB5eby230qkP1uQ +vyunBj6QnANa7qBxycL+xXbW8HBksArQ/HX+OZs7hagrP0qGMnjmCzsblv0wixghgvQTkpg 61RTH34ieLUkzE0oFkrqJyNZcoH0wStdP/9zwK1Av0cZcFcvlLdIL956v4IpZozW1ScG7OJw 766VTOg4l2PTPCnIdNFy1Os= In-Reply-To: <20240424213019.make.366-kees@kernel.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 4/24/24 23:40, Kees Cook wrote: > Hi, > > Series change history: > > v3: > - clarify rationale and purpose in commit log > - rebase to -next (CONFIG_CODE_TAGGING) > - simplify calling styles and split out bucket plumbing more cleanly > - consolidate kmem_buckets_*() family introduction patches > v2: https://lore.kernel.org/lkml/20240305100933.it.923-kees@kernel.org/ > v1: https://lore.kernel.org/lkml/20240304184252.work.496-kees@kernel.org/ > > For the cover letter, I'm repeating commit log for patch 4 here, which has > additional clarifications and rationale since v2: > > Dedicated caches are available for fixed size allocations via > kmem_cache_alloc(), but for dynamically sized allocations there is only > the global kmalloc API's set of buckets available. This means it isn't > possible to separate specific sets of dynamically sized allocations into > a separate collection of caches. > > This leads to a use-after-free exploitation weakness in the Linux > kernel since many heap memory spraying/grooming attacks depend on using > userspace-controllable dynamically sized allocations to collide with > fixed size allocations that end up in same cache. > > While CONFIG_RANDOM_KMALLOC_CACHES provides a probabilistic defense > against these kinds of "type confusion" attacks, including for fixed > same-size heap objects, we can create a complementary deterministic > defense for dynamically sized allocations that are directly user > controlled. Addressing these cases is limited in scope, so isolation these > kinds of interfaces will not become an unbounded game of whack-a-mole. For > example, pass through memdup_user(), making isolation there very > effective. What does "Addressing these cases is limited in scope, so isolation these kinds of interfaces will not become an unbounded game of whack-a-mole." mean exactly? > > In order to isolate user-controllable sized allocations from system > allocations, introduce kmem_buckets_create(), which behaves like > kmem_cache_create(). Introduce kmem_buckets_alloc(), which behaves like > kmem_cache_alloc(). Introduce kmem_buckets_alloc_track_caller() for > where caller tracking is needed. Introduce kmem_buckets_valloc() for > cases where vmalloc callback is needed. > > Allows for confining allocations to a dedicated set of sized caches > (which have the same layout as the kmalloc caches). > > This can also be used in the future to extend codetag allocation > annotations to implement per-caller allocation cache isolation[1] even > for dynamic allocations. Having per-caller allocation cache isolation looks like something that has already been done in https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=3c6152940584290668b35fa0800026f6a1ae05fe albeit in a randomized way. Why not piggy-back on the infra added by this patch, instead of adding a new API? > Memory allocation pinning[2] is still needed to plug the Use-After-Free > cross-allocator weakness, but that is an existing and separate issue > which is complementary to this improvement. Development continues for > that feature via the SLAB_VIRTUAL[3] series (which could also provide > guard pages -- another complementary improvement). > > Link: https://lore.kernel.org/lkml/202402211449.401382D2AF@keescook [1] > Link: https://googleprojectzero.blogspot.com/2021/10/how-simple-linux-kernel-memory.html [2] > Link: https://lore.kernel.org/lkml/20230915105933.495735-1-matteorizzo@google.com/ [3] To be honest, I think this series is close to useless without allocation pinning. And even with pinning, it's still routinely bypassed in the KernelCTF (https://github.com/google/security-research/tree/master/pocs/linux/kernelctf). Do you have some particular exploits in mind that would be completely mitigated by your series? Moreover, I'm not aware of any ongoing development of the SLAB_VIRTUAL series: the last sign of life on its thread is from 7 months ago. > > After the core implementation are 2 patches that cover the most heavily > abused "repeat offenders" used in exploits. Repeating those details here: > > The msg subsystem is a common target for exploiting[1][2][3][4][5][6] > use-after-free type confusion flaws in the kernel for both read and > write primitives. Avoid having a user-controlled size cache share the > global kmalloc allocator by using a separate set of kmalloc buckets. > > Link: https://blog.hacktivesecurity.com/index.php/2022/06/13/linux-kernel-exploit-development-1day-case-study/ [1] > Link: https://hardenedvault.net/blog/2022-11-13-msg_msg-recon-mitigation-ved/ [2] > Link: https://www.willsroot.io/2021/08/corctf-2021-fire-of-salvation-writeup.html [3] > Link: https://a13xp0p0v.github.io/2021/02/09/CVE-2021-26708.html [4] > Link: https://google.github.io/security-research/pocs/linux/cve-2021-22555/writeup.html [5] > Link: https://zplin.me/papers/ELOISE.pdf [6] > Link: https://syst3mfailure.io/wall-of-perdition/ [7] > > Both memdup_user() and vmemdup_user() handle allocations that are > regularly used for exploiting use-after-free type confusion flaws in > the kernel (e.g. prctl() PR_SET_VMA_ANON_NAME[1] and setxattr[2][3][4] > respectively). > > Since both are designed for contents coming from userspace, it allows > for userspace-controlled allocation sizes. Use a dedicated set of kmalloc > buckets so these allocations do not share caches with the global kmalloc > buckets. > > Link: https://starlabs.sg/blog/2023/07-prctl-anon_vma_name-an-amusing-heap-spray/ [1] > Link: https://duasynt.com/blog/linux-kernel-heap-spray [2] > Link: https://etenal.me/archives/1336 [3] > Link: https://github.com/a13xp0p0v/kernel-hack-drill/blob/master/drill_exploit_uaf.c [4] What's the performance impact of this series? Did you run some benchmarks? > > Thanks! > > -Kees > > > Kees Cook (6): > mm/slab: Introduce kmem_buckets typedef > mm/slab: Plumb kmem_buckets into __do_kmalloc_node() > mm/slab: Introduce __kvmalloc_node() that can take kmem_buckets > argument > mm/slab: Introduce kmem_buckets_create() and family > ipc, msg: Use dedicated slab buckets for alloc_msg() > mm/util: Use dedicated slab buckets for memdup_user() > > include/linux/slab.h | 44 ++++++++++++++++-------- > ipc/msgutil.c | 13 +++++++- > lib/fortify_kunit.c | 2 +- > lib/rhashtable.c | 2 +- > mm/slab.h | 6 ++-- > mm/slab_common.c | 79 +++++++++++++++++++++++++++++++++++++++++--- > mm/slub.c | 14 ++++---- > mm/util.c | 21 +++++++++--- > 8 files changed, 146 insertions(+), 35 deletions(-) >