From patchwork Wed Apr 24 21:41:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13642531 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5115AC10F15 for ; Wed, 24 Apr 2024 21:41:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 14A446B031D; Wed, 24 Apr 2024 17:41:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0FB1B6B031E; Wed, 24 Apr 2024 17:41:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E8C176B031F; Wed, 24 Apr 2024 17:41:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id CAD706B031E for ; Wed, 24 Apr 2024 17:41:13 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8288F121197 for ; Wed, 24 Apr 2024 21:41:13 +0000 (UTC) X-FDA: 82045746426.15.CC7265A Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf05.hostedemail.com (Postfix) with ESMTP id 9A762100017 for ; Wed, 24 Apr 2024 21:41:11 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Svow4mpd; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf05.hostedemail.com: domain of keescook@chromium.org designates 209.85.210.169 as permitted sender) smtp.mailfrom=keescook@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713994871; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AHwyZxYzmrEMajI8SxktaczkjAAhwGi1bdDM+8q+7+o=; b=wp+ZBY4x3cJA1yiSk1VIcnVf+OTuQNjC985mmnFJQB8uQwt9pjWK0bT/Zy3cQMIvkf2XKN ZvFSgqL9nrH25dfYqS9ZAq0RdsvbtbmMBfjt9zLcxUMtkg6tyGqZm7uk14rSDd6zt5NRbL IsqTP+GaufCoCsn1az9OOR9CkkmRs5E= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Svow4mpd; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf05.hostedemail.com: domain of keescook@chromium.org designates 209.85.210.169 as permitted sender) smtp.mailfrom=keescook@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713994871; a=rsa-sha256; cv=none; b=3jDkSj2l4Iye2igsxOg45l10R0ei5cmmq7d9cw9RJeXCAwFOUeymTJjPk4YSft5mYAVzmR mCWjFjRMqw12hxEJiTyiGTapeUn0XZT0yuPkGb4eTWQJCWjGP5kp5rN5B0Ig8Pa2ixrjZh 7O7FG38B6Kd8pYpDDYt7CfqgRtAsgV0= Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-6ed627829e6so440115b3a.1 for ; Wed, 24 Apr 2024 14:41:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1713994870; x=1714599670; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=AHwyZxYzmrEMajI8SxktaczkjAAhwGi1bdDM+8q+7+o=; b=Svow4mpd1p32K3JnWurAYYlODdC2OERZ5jkUoqIx1D/iBbtGvX1SBPwpu2tiEdlyJL IVwxC7dHE9hE/LVu64wUWbVM4BPv0cFqa2Om/IJpj8dG8LbPD5F+6+F5WOUvErAB3E6M JCc7C0jG18+j4zv0S7uuQPQptVr0nO5VA+0gI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713994870; x=1714599670; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AHwyZxYzmrEMajI8SxktaczkjAAhwGi1bdDM+8q+7+o=; b=DtZL3vcfNHCR6glQ/+eXWu2EhYYkzo6ikpYHJdQmqSCNLe5kQxmERvzjY71WtMAFP9 KmO91voZjJ0meTnsauKCjVdNgZRvSQa5m8A9Ub7H9A9O9jh9Z4E9v1MqEkVyxVw0zzsR IUDopVe8XfB1nTvNN7PhWcbCVYMh6uKn7mDIttXrwFSRt0Ig7A0XOXm3ve1JFdrZ++Dp D71cJEnTulHi3umLUZfxtVqFaDbT/0iMtlLyol5AE2Tu0j+C6soqzFZ1i5lRNeowIcNU a1NBna42Qc40uMaGdbudPxseks9OCz3zubGRRIDjqH8iPr6hUrtGiMtHcbw2SZQIFy+c 4aaw== X-Forwarded-Encrypted: i=1; AJvYcCWPtO1bjhFv+1sM9Z/YzNT1fqR7IRe2gvbUXXuaXh3QgRUD9Kp9W3U6Xh03ZXqbLDuq0xoYucCgYbMCd9+Y440Xl/k= X-Gm-Message-State: AOJu0YzBhy+vsWDfZh/l4Mww670l4dY184QQ5RJEIOGVGymPF/Rcxlea cRL05yC9zCGw2B9AvTxlOnDH7g3ldR4cK1tfdn4wOIJk8B0LT5/qcXHH2lZ6Lw== X-Google-Smtp-Source: AGHT+IF+6ZBq5uRyKg/lyeaV7Zfjb1C4ubPI1uf/1ezK3wBx+DQpcIBEs1vcTlhgEw/fCjnkutcopg== X-Received: by 2002:a05:6a00:3909:b0:6ea:c2c7:5d6a with SMTP id fh9-20020a056a00390900b006eac2c75d6amr4392756pfb.2.1713994870491; Wed, 24 Apr 2024 14:41:10 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id d11-20020a056a0010cb00b006e685994cdesm11863906pfu.63.2024.04.24.14.41.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 14:41:09 -0700 (PDT) From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , Andrew Morton , "GONG, Ruiqi" , Xiu Jianfeng , Suren Baghdasaryan , Kent Overstreet , Jann Horn , Matteo Rizzo , linux-mm@kvack.org, Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Thomas Graf , Herbert Xu , julien.voisin@dustri.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH v3 6/6] mm/util: Use dedicated slab buckets for memdup_user() Date: Wed, 24 Apr 2024 14:41:03 -0700 Message-Id: <20240424214104.3248214-6-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240424213019.make.366-kees@kernel.org> References: <20240424213019.make.366-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3141; i=keescook@chromium.org; h=from:subject; bh=b6S8APzjeiOvxh7QXxGMfpkIi0N6Ek4SYDm0EuC0dW4=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmKXxvnemB4TZWw6/SnmxaAlLoQIy43uGWllQev OnPiAH/9CKJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZil8bwAKCRCJcvTf3G3A JgDRD/9furWdb2wMJZeJdepRap6uPYO2c5ds4IQAE7tziQMuuSmONplOC3rkp+YC2fB+nkaxPWE d5SxYD9e0M/84q90AW2Ti0nh0jvfTtNMZ0w2uIaVV00CyQNhiPlE3NnR373AMGa7YCotWcpcwyX NCGb/Um52ghKJrB7GUeqADeH51g6QIiaQ5A+61wh2r58IOUBXzWTViSlBUz7tDW/wxytYUO0xLx MBsozAddCGBLWZde0yKTX/DhJtVJLK9QN57x8/gGzPrEbm+SwyhxWIlVSOFZCXIb1GmE2dCKP04 WKTMHUL9wBBDdVJHEAezmq1VUNjqrMAltoUhsU68zHrBMOZ0xALfArySjrQgYOZ/s/LhSN1eixy YB2HnwwW1Ob7iRyos9jHaqZuUfPhr5HkR1Xt7W48XR/orPhpxKNcC56bEVdjiNDqF2nH+7T6Dw1 wdT983ZF/WKKXFcuG9HEuWc/berdLOmFwtWLZ2o9u/HgPXUPUYUZ3y6nO89h5f63BoqgV9Pezyf VBHjGDS635QY8qwXZDBkeh/KqpQ6PKclipOmkFCvAKAWYzxi7Cae9uKfTTfmYrBkCvyAQ6TmATh nsSnp2IGyAwwXBGuXHvO3DSd4XidX2nYJrNw/qYXoDpZvebUgj46ue7R7B/WpnH8rgDo7mlnUJJ T33evI365HcKYwg== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 9A762100017 X-Stat-Signature: 5uas3wjofhdmmib3ifwkm4u8wktejgnu X-HE-Tag: 1713994871-698226 X-HE-Meta: U2FsdGVkX18Yil2v7hvSdHXZD7/X40OIPJg/baKwtZwmviYWnCbxvJr5t4aDr+Roo5pcjz/98UZel6bTPzZm3qWjSLb1DMCStSD+g/j3Yj3UvA6jknFvdahuXqgQIpSRR16L3nwDs/USv/Fg3eYnh3M93N7JmtInYGeD29G97rrM6sOcOJYASVDi/nW7gnByNINoWjfyN8hBatYQWrcUYax5Zv/RlLrvVqxdMdtikD6NM8LSE6irez5ECJg6ftseiIs01wCicrCpPcd5mBC/HIypa+P/+EEG2XmJlTUjMUCdRLGTg3TkFgNC6RH4jY0ATA4d2U9GLeFach/pk4E/J2W9n9GJANWlYU3fN+MyvOSr44uJS6X1hQZGJSuOwctUJPZlanK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prRLCeHUVVFON8ZL6KkF43au7JhHPfQjGuEaNUK3iQmHMoc5zYUNrf5+YplfvaTs6kuY36UGy7fmPLnExyG4rmEy+vdH7iidzhAw2uirKsyM8hrmijgFi66HvasRzoqempnorwYwBxV6blFijcu3uG2NLUW4PO7Tc+f2LKgdby1KyaIQx+RGa7MjeknneKurADzi6g8dEBjouCzUnqeswYWOYTODIEUCt0+KFLqouoQz22+kic5ReyiOmm0CgI0tWYQ4nBBQ2nwgjLQEb/8kW4myOdLecFP0uqPznHlDoNkGeZNY/sQzH55npXjHOXR+pmO7x2WyN/N4xQUHVkRW3BSOWrPPnlr2DdQlRvcbSXRzFSxrqKKyCLGIkdKLPXfwlRPXnen3NPenLLzL0fRdqtGDRmMhOvmghlkfhgXNA601jeugfjg8a4C0tj5LlJCXbLW6rPNsCCkeMjlqiu2rlPP0MzxZ9CuyVTcaDngn0fElRcPPd/PFBzLGBngsH EBsAxfPR tawgNoJZnP4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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. After a fresh boot under Ubuntu 23.10, we can see the caches are already in active use: # grep ^memdup /proc/slabinfo memdup_user-8k 4 4 8192 4 8 : ... memdup_user-4k 8 8 4096 8 8 : ... memdup_user-2k 16 16 2048 16 8 : ... memdup_user-1k 0 0 1024 16 4 : ... memdup_user-512 0 0 512 16 2 : ... memdup_user-256 0 0 256 16 1 : ... memdup_user-128 0 0 128 32 1 : ... memdup_user-64 256 256 64 64 1 : ... memdup_user-32 512 512 32 128 1 : ... memdup_user-16 1024 1024 16 256 1 : ... memdup_user-8 2048 2048 8 512 1 : ... memdup_user-192 0 0 192 21 1 : ... memdup_user-96 168 168 96 42 1 : ... 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] Signed-off-by: Kees Cook --- Cc: Andrew Morton Cc: "GONG, Ruiqi" Cc: Xiu Jianfeng Cc: Suren Baghdasaryan Cc: Kent Overstreet Cc: Jann Horn Cc: Matteo Rizzo Cc: linux-mm@kvack.org --- mm/util.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mm/util.c b/mm/util.c index bdec4954680a..c448f80ed441 100644 --- a/mm/util.c +++ b/mm/util.c @@ -198,6 +198,16 @@ char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) } EXPORT_SYMBOL(kmemdup_nul); +static kmem_buckets *user_buckets __ro_after_init; + +static int __init init_user_buckets(void) +{ + user_buckets = kmem_buckets_create("memdup_user", 0, 0, 0, INT_MAX, NULL); + + return 0; +} +subsys_initcall(init_user_buckets); + /** * memdup_user - duplicate memory region from user space * @@ -211,7 +221,7 @@ void *memdup_user(const void __user *src, size_t len) { void *p; - p = kmalloc_track_caller(len, GFP_USER | __GFP_NOWARN); + p = kmem_buckets_alloc_track_caller(user_buckets, len, GFP_USER | __GFP_NOWARN); if (!p) return ERR_PTR(-ENOMEM); @@ -237,7 +247,7 @@ void *vmemdup_user(const void __user *src, size_t len) { void *p; - p = kvmalloc(len, GFP_USER); + p = kmem_buckets_valloc(user_buckets, len, GFP_USER); if (!p) return ERR_PTR(-ENOMEM);