From patchwork Mon Jul 1 19:12:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13718558 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 2170FC2BD09 for ; Mon, 1 Jul 2024 19:13:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A613D6B009C; Mon, 1 Jul 2024 15:13:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E8BF6B00A0; Mon, 1 Jul 2024 15:13:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A0F16B009F; Mon, 1 Jul 2024 15:13:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 50DE26B009A for ; Mon, 1 Jul 2024 15:13:08 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0444F161D77 for ; Mon, 1 Jul 2024 19:13:07 +0000 (UTC) X-FDA: 82292131656.28.D61DBF7 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf01.hostedemail.com (Postfix) with ESMTP id 2CC3340020 for ; Mon, 1 Jul 2024 19:13:05 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ZWa0R0t0; spf=pass (imf01.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719861174; 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=CxOEO+GaUnU9l2X1JMr3pflfon1oWNou5sDuU09joDk=; b=Hz0bvwf2u3IL9k6GeldhU41mUpIdjPBrTQ3YuMQU6yWj+eJI0aRvREj5SjuiCynJY99aax +IRhMeO0z/O7UG+wfK0PVnkhd9xkUSACMkY2Y0FiMriEuEoicwuyuGSKlCCkYtkfzbkqE3 ukqcb9+exUSM0bgUCNeSneP2QA7Nw+U= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ZWa0R0t0; spf=pass (imf01.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719861174; a=rsa-sha256; cv=none; b=b1rQIhhddYuqjDm12ALE4elgPlJNveH8xXORo18xBEldsTSPs++vXRtKTH1IbyQHoTkrjL 1aYDD5TaWWKnKfWKOffmxTrWW6Nsb3fcBDNCDBBon+b/ECiSU1kNzbTkbdQ4B/+5yugUtr o4M9bgxy5D2RpyzsUIHcG2plXCZ2FgE= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 0698161782; Mon, 1 Jul 2024 19:13:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A2F71C116B1; Mon, 1 Jul 2024 19:13:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719861184; bh=jbXDHZFgMTEqVuIbGpjVHki9R28/opuEqbu8dA+PhS0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZWa0R0t0CPMjfv3nmnvMRYyT+0Li3uUEKN+F63bIWHM5FgA0Zb/H43PrpIm3S1wN+ cSuMAUVGcDq0VPaulTBXEjeP2l0TB9xjG+2UEbX0qsjc7rkvAiBsK7+I89ex7LZgPn gSDpEciVyMFmJKwREVaeNILwLmK/hGuEJS/2ULgNgm5mGv1s6E5j7ocWwtsbwUki+K LpaAGUv8GppMD1wiOwENbbpsCWiN8oDvi+xBdQV6MjW9MpuGnMlORr8FgmERKVods9 T5l7dWXqZMYSVX7ZmrKdnfa13VayjeNzXw3WFOR4TiMnCppfx/DXKOHiSND+6cIfI8 fVtSgUXMY0gnA== From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , "GONG, Ruiqi" , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , jvoisin , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Xiu Jianfeng , Suren Baghdasaryan , Kent Overstreet , Jann Horn , Matteo Rizzo , Thomas Graf , Herbert Xu , "Gustavo A. R. Silva" , Jonathan Corbet , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v6 1/6] mm/slab: Introduce kmem_buckets typedef Date: Mon, 1 Jul 2024 12:12:58 -0700 Message-Id: <20240701191304.1283894-1-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240701190152.it.631-kees@kernel.org> References: <20240701190152.it.631-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1406; i=kees@kernel.org; h=from:subject; bh=jbXDHZFgMTEqVuIbGpjVHki9R28/opuEqbu8dA+PhS0=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmgv++6T8pqxaDs77JF9cx/6sa+j5FaKjCYQYlt 8Av1OrjnquJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZoL/vgAKCRCJcvTf3G3A JrDmD/41/LShofUlbbo+pT14hOQzRPFxn19omAUJjuXG/RFbAAjGUsA1LVdU477HcV5iEqq9HwZ yownqKuZBSfRj5jSdbb8lWk+EvNPMJcq/bjuHRBHRVzCIF1dak6lCsCilziR3nDfx0QcIBdh9uM ZfgpyACf83OKtGsnoVrd9PtHkCxzzK4gCsg0cYfwsiMY7lZKRpJkm5Ovl/QeoBlcThOt4kLCnPh VK6h7O4Gi07HVvy/hSMdFhZ/Yg3usgb54keafxYrGlWCSoeYcfA7mccU3Fizf91lu9+yuMRRI+Q B6mnj29oSUPJAd63diYXlmeQHMKkR54f1Ugms2s/O2MuxJ5NN2PeSn3IyL3FbkxJlfCWK0XQ13/ FJoWJFVTtq1PfnOsq/2FR2LpfnPbNPfS/mBp2KVYU1xxcNrOTQ6PmfLN8NZ1dJZnWcctqhlAPvx qAyGmwfuj3emanIzCY/cVU6cLqr6IHmmy9Ejo46YzdG7GTbl1t+0VhPhVwMw+dnIFqJ2tHF6ffU 9S0/hRjxjVGzgpaGI3Rrfzzq/1Rg3PTRcuiXzLrK0K0yBR9YXwRUNuyztoKNbfzA9jitdfiuMBO Ro5XeQMITAZry0tFYeSs/l21CrQNLwzrU/+aIm+fSGZ9J74J2UiWplyhtTCe8DH74SORzWApt9Y atfme+bo8XXfHfg= = X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 2CC3340020 X-Stat-Signature: fzexsoyg9mjru36ucgx9j3rwcsxz86h3 X-Rspam-User: X-HE-Tag: 1719861185-97668 X-HE-Meta: U2FsdGVkX19VXiGPhpkMvcylvH6G46chM66qByAcC5tHXvGzHxMkIewFBvRtR3qqOBabeoZB24n3ELm8Iip+8rWFoW8D49qWiOHcHJ2BgaL6/ftdigiIZ9qiSotZlxeLZ14L6IRatie4rHmQ7u6Qic5CBSW0330Ym8CPSHJ9g2iZitAYuT6nMqDvTRxpOd/O1qItK9cpIu3/xmQtap3vWdGrR3UnlbosgPahP5liwlQiz2LC/QZw1VvQbRsjKx/xN1Un+SF85K3RuML2vb5zA/SapQ5W3iN3D9cT1onju/m7tJgSgWxZHveuyC2LDdqLw5sEOMDfAXiFlhS0kxPcIKIFWOZN0dKanXzflTFNIZ8RBtnNaEwwzcwg00dUl1Xm1vSwicIezZxu4UjFlWddFb0t72pI3p4S3xf7ZkPbACIJfZN7r/BY550usdas/nz3IGXrVs/iY1EuDDVdw6EnqUBvdpuau3Sl0MKSappg2UCwiboMLH5zqMpAefWDNE18bPZ69lBKTq1wVuNuxN7p7eVpINgLT85jSLNi/Odtiw1VLQN/X+dC0Soejqg6HtB1q9i/DXljjvTiQodbCsxgsjKh0XxGnoO8mvUWlNK/aS+iAcXbZWPq/pfH1NnlL8pMPiV/eFpqNvoeWt/ORD0GxY/NUSGtlHIUUHdDpBMTm0UPUK4fptfbCew85hN8LykfNn7kMA9v32NNaSyae0a7prIMgCUu7vV3ZOek24EmxuIUyMGgalmF4z23xhKgdfdDRegdIho+hT2mbRBVo3LtCgto3PtZZqmDrzaW88G4v0yJYvvON1SISh9nBdMDjkSS/l+qtCNAips2NySdIATadNTvG3+diKvmg5e6QMaEXkletUjAKNidE3M5xwdiN5VX8XNledlqIU3hmiXHQAuSm2YWLXibJXzlwPqJqs65nVwmcE9tsIAKpWYtdzG3iI7RtPTv48sqGJWM5dapYn7 IPR3RQ3s poUYjZ0NA9ubaN/Wfam18xe6YYJcmxOZbnXDvAc26QhcMN0sH1OaM7xs4kz69xgO8gxNrTkcPLQY5MtfoefdHSEk+VP9m4Dm/12yWmQTa1fyaDplNJNphwaWPzhcCI4AayjNKKLYykyZkkGRQDCBDen45mXEbGgsADpkAduuizAK6w0Rv1+0xyxhbbAPM5HyomeyfuJgQIJ7Jj09zv6qegDQYtQ4NYLGt9esP0oXzo9e/0ej/n9f/98WmE9oab3gRQ5GMJPZOkMhEcZDVuVbLZIyySxd2Xu3lmxAJLvzntUtwqhei9kw8K3aXax/h/6mv2SwBYnMPUmrv5ZadEYngi6dCnHB0+bXIzm3CqjWXlHSAuggS+URr882y9INpIzzBdx3cqhKTFrFV94faficorRoVCzpfa4kMn4Cm 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: Encapsulate the concept of a single set of kmem_caches that are used for the kmalloc size buckets. Redefine kmalloc_caches as an array of these buckets (for the different global cache buckets). Signed-off-by: Kees Cook --- include/linux/slab.h | 5 +++-- mm/slab_common.c | 3 +-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index ed6bee5ec2b6..8a006fac57c6 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -426,8 +426,9 @@ enum kmalloc_cache_type { NR_KMALLOC_TYPES }; -extern struct kmem_cache * -kmalloc_caches[NR_KMALLOC_TYPES][KMALLOC_SHIFT_HIGH + 1]; +typedef struct kmem_cache * kmem_buckets[KMALLOC_SHIFT_HIGH + 1]; + +extern kmem_buckets kmalloc_caches[NR_KMALLOC_TYPES]; /* * Define gfp bits that should not be set for KMALLOC_NORMAL. diff --git a/mm/slab_common.c b/mm/slab_common.c index 1560a1546bb1..e0b1c109bed2 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -653,8 +653,7 @@ static struct kmem_cache *__init create_kmalloc_cache(const char *name, return s; } -struct kmem_cache * -kmalloc_caches[NR_KMALLOC_TYPES][KMALLOC_SHIFT_HIGH + 1] __ro_after_init = +kmem_buckets kmalloc_caches[NR_KMALLOC_TYPES] __ro_after_init = { /* initialization for https://llvm.org/pr42570 */ }; EXPORT_SYMBOL(kmalloc_caches); From patchwork Mon Jul 1 19:12:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13718560 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 D695CC3065A for ; Mon, 1 Jul 2024 19:13:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 099156B009B; Mon, 1 Jul 2024 15:13:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EF2F16B009F; Mon, 1 Jul 2024 15:13:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A5D8C6B009B; Mon, 1 Jul 2024 15:13:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 5CD846B009B for ; Mon, 1 Jul 2024 15:13:08 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id D8060161D70 for ; Mon, 1 Jul 2024 19:13:07 +0000 (UTC) X-FDA: 82292131614.13.DC7A443 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf07.hostedemail.com (Postfix) with ESMTP id 249674000E for ; Mon, 1 Jul 2024 19:13:05 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=WwTCjn01; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf07.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719861168; a=rsa-sha256; cv=none; b=KIc52jlJKxDOeXgDe4MCiAlRBEXYc/FnDsURGsUiFn8HGIulIqA0pIRsxWL/5X4Nn/ZO8B sakCaXCPD6As9O2zu5a2julNUdlJ29Y9MB1fM8vKfYr6jeB8hCQgWkHOP0W/w6CS/dMe9Z xYL8xUEi3u23WQI1AlLr1EtbEwYKl3g= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=WwTCjn01; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf07.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719861168; 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=M+jtoKEQG0/xiQnWtq8YzjETcQq+iGCoVepQmuHKY1M=; b=PiM6G8VkHPNaJxdVJ4wLxw2FzRrbKV//2dyo5vzojl6hDhl29CIPWXTr0N8Bb2RK3DlJDo N5IyQvW9JTrzEeHgMc2tUZOy8NbK0vYZZA9vJ+48rAtXHl0UWDhThiREZ7IrdKdGXhLpce FWZ6Ar8GNU+jwll43PwpVl/7qm8TugY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 1018661783; Mon, 1 Jul 2024 19:13:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A62BBC32786; Mon, 1 Jul 2024 19:13:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719861184; bh=Fh8hn82A6Gkidq5L2q9k9Jp9kuNH5GwUiFNzo6MraKY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WwTCjn01rIrhnVzozUC8nSU9ygzSVOAWK2NwHfiIHz88ZxMfH78DfyBNZqPbgJhjt 1En0azbb+FzKarKw46ht5i47ODAV8FDJFJbLHdekbKlVvgy5q2vCiB5ZeWkJHdln/1 X/PkEyTnjr7iDbFCSDom1lDpgFvDRDsuB5rKrjSiBxCflX6oYP8LjJDXDPiaqt+j7M RBONoQcMp3d6DSSp4xZJsxVObs45dqtw4EetmwIMSdOBCvf/DloxqKQ6RMiW96hV6N /cF9uMmOesCr4A/8eGfvuj0uaKnoLQtFvne4BkLbRHuhb57/TY8sS7eCV1LW5/3Wjz tp4ark47oZ4pw== From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , "GONG, Ruiqi" , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , jvoisin , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Xiu Jianfeng , Suren Baghdasaryan , Kent Overstreet , Jann Horn , Matteo Rizzo , Thomas Graf , Herbert Xu , "Gustavo A. R. Silva" , Jonathan Corbet , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v6 2/6] mm/slab: Plumb kmem_buckets into __do_kmalloc_node() Date: Mon, 1 Jul 2024 12:12:59 -0700 Message-Id: <20240701191304.1283894-2-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240701190152.it.631-kees@kernel.org> References: <20240701190152.it.631-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=10530; i=kees@kernel.org; h=from:subject; bh=Fh8hn82A6Gkidq5L2q9k9Jp9kuNH5GwUiFNzo6MraKY=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmgv++vHvQ6T0Eu3tRk8QttwaaXPyJTfZbfA7bS LQp+ltrjfuJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZoL/vgAKCRCJcvTf3G3A Jl9uD/0f7gAetPdbAgu8momSOZv6Cae+I/11HPjOsFzUo49cNUJlFFs/A+yXPo0sZUGd6BWGT8b 4z3V/y10bQjSk2Ce5NdLROSleTVt5Z0kKP4znE7LRvawxxIMcE6NRSE239WY8ClZprrMQFAFota z2qwPKdBLlvhsvt8prwxl4AtS3MSqFWNiQTOyFNXvqwzNzx7q0R3yGSHR11TyMPdDITDPCMfKFJ ql4xYz4/P/0hJ61sbBSCKo6HU3NmOnPYGDe/WkXIB3bTn++2ACQsbkcihNSU3jLY/Oc0Pfr53XA L3Rm69795J6HaMBcRy1JF5ff+JNuyhBe0fA2iH5NlrjNJpi1vh0qA3azfgQtcF3usL1kxLzwCy1 tkfMp8MyYUIzs/PPx+0AHGKFLqRk+brl+1t8vYB2nZ3eHv9BC/ow8/GbPG5A0+BJOHBFNzEQuha ACPwzCjwKOzUZTBeIjK+93446+7EpROWkRRNgNk1wsqQ8eMs0l5RVVAfMT1gH74tDWYlyIYro5z KgthZkHBkTsGnii0Twa5PwMkHLtwdf5khBhW6/geyBph5Lk2lbU9LozhwbbEQPenvXSj1hAEoaC LXc/OOvyAxp8ZKJIZZVEGbqvjTRyg32q6V36pjHIrB6OMsSgFlZMwaI63511/yR3zN3r8GxPbvU mWcGxNfTGQUNs+w == X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 249674000E X-Stat-Signature: f95up1d6io1sogsc9btsef53im37kksx X-Rspam-User: X-HE-Tag: 1719861185-764857 X-HE-Meta: U2FsdGVkX1/uCltmDl0Kc4KyeMn33N1wKFNNnn5yYi/ey6qsT9zQkklDyuES6HngxPvY+rUQ2HZ0bCvyetMUwOfVVhAunEx0xb0/YrvXhZ6LudnRbbbRwrX8SMOdRUCJ36h248iMgeBFL5emnZpOfOPvRWuNnonhfsyb22TIXOyS/K/xOVYRcNsMcp7t0FgaPRM29tiAFB6ZvbznhBno8vsJG5Vej3dZG3o1odh3gaL5hkQWfOmdMcVWlxTsYaPbCwxTMaukBq5DfV1Jbj8SHAfJ6kTA37kwqa3ojdsdbKtI4tiOuZfSez9ZpVYbH/wDf2FYVgMGXYj4S/LEQ1U3BcaiCIkJ5/fumgiXyuARlHzSqGjIrAtuOYHm4Ol3MGYgn1fc6J3fOwhY1AngHktlkl4Vb5xSobJ1pfeAbWPR6NO5sjHr1YHxX0IaBUM29oQ1TcCaU10rRgUbJSJ7cY12ba82mDN6mlByHv9pZpJtt8Z0co4mdu+tmIMyGgaP/MugcGL+uVb9chvOfzx3UrzoJ/0Y4MXp3fv+kBj8OfEO2YqWXZvemm4HhToBF9HVY9L9F+IkqPvxwsdXTZc1kU/T+TVIzoaZ5yrw6jp2/T0gk32H7DpspglIeUsGWiwtr2bT9anwB9JlsWq9UfIL4lgZe4pE5iga1ekHyRtIgJy265scG0hmRdwfzfswKuRgdRLasDnfmwlD9uoTKz1f2W/hD3Fv8FFVNvl6vrLJ/xxypla94qfO6yQPhJ+/v8va2ey+BZtuFfw/giUr+F5Y8aY633dKI7ucZ+YN/pmZOdyNWrCra7I75mxPC62Bt7NhmAfL+Z22D9TCbW5MMSAgyOIBzcxxW8Qi96kVy1UEsoMiRlQa6ZTkb/Lkc7FxpckgMVabhvwLtqFY9onVtTQfIaYJd6Q2MQOvd/1jwmB64ZcvhrX3ia0JVWYNeabOs8AsUIW488kglI83/Kx6AlwB3NZ Z7Jdisr7 vluGmVSxUG8/MYXIdzgoPjSfmvNn1xOTUqUVN/PGPfBCOmDJ9I1AArbOOc/CGxZmqBkutInYI+ZtUTEu3FFG5oCiAc5Yt1kbTK+YXAZaT2nA+pJWfNhzdtE1ylPsyXg9m52/5E5AOZayUtH1s1Hp5c2bT4YEOgiXfFFVJrL6YnmI1Ck7BDGu6PTZfZNBJVnfMdtoXQmEz3ZKjB4g7LWKUYixYVHTVzDOsPDMg7Nl4/5NzN28xm0Z4evNt3no9pS773+e9f4OwEhKUVtFMNjH5IIZY1Dr+hnrqE4l62gkn2D3gwXgpvpP2sIUUmry+OfhNZ/79NOGJbL7GJlf9ICO+uG5brrnph633yT7wyh64155weP8= 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: Introduce CONFIG_SLAB_BUCKETS which provides the infrastructure to support separated kmalloc buckets (in the following kmem_buckets_create() patches and future codetag-based separation). Since this will provide a mitigation for a very common case of exploits, it is recommended to enable this feature for general purpose distros. By default, the new Kconfig will be enabled if CONFIG_SLAB_FREELIST_HARDENED is enabled (and it is added to the hardening.config Kconfig fragment). To be able to choose which buckets to allocate from, make the buckets available to the internal kmalloc interfaces by adding them as the second argument, rather than depending on the buckets being chosen from the fixed set of global buckets. Where the bucket is not available, pass NULL, which means "use the default system kmalloc bucket set" (the prior existing behavior), as implemented in kmalloc_slab(). To avoid adding the extra argument when !CONFIG_SLAB_BUCKETS, only the top-level macros and static inlines use the buckets argument (where they are stripped out and compiled out respectively). The actual extern functions can then be built without the argument, and the internals fall back to the global kmalloc buckets unconditionally. Co-developed-by: Vlastimil Babka Signed-off-by: Vlastimil Babka Signed-off-by: Kees Cook --- include/linux/slab.h | 27 ++++++++++++++++++++++----- kernel/configs/hardening.config | 1 + mm/Kconfig | 17 +++++++++++++++++ mm/slab.h | 6 ++++-- mm/slab_common.c | 2 +- mm/slub.c | 20 ++++++++++---------- scripts/kernel-doc | 1 + 7 files changed, 56 insertions(+), 18 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 8a006fac57c6..708bde6039f0 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -570,6 +570,21 @@ void *kmem_cache_alloc_node_noprof(struct kmem_cache *s, gfp_t flags, int node) __assume_slab_alignment __malloc; #define kmem_cache_alloc_node(...) alloc_hooks(kmem_cache_alloc_node_noprof(__VA_ARGS__)) +/* + * These macros allow declaring a kmem_buckets * parameter alongside size, which + * can be compiled out with CONFIG_SLAB_BUCKETS=n so that a large number of call + * sites don't have to pass NULL. + */ +#ifdef CONFIG_SLAB_BUCKETS +#define DECL_BUCKET_PARAMS(_size, _b) size_t (_size), kmem_buckets *(_b) +#define PASS_BUCKET_PARAMS(_size, _b) (_size), (_b) +#define PASS_BUCKET_PARAM(_b) (_b) +#else +#define DECL_BUCKET_PARAMS(_size, _b) size_t (_size) +#define PASS_BUCKET_PARAMS(_size, _b) (_size) +#define PASS_BUCKET_PARAM(_b) NULL +#endif + /* * The following functions are not to be used directly and are intended only * for internal use from kmalloc() and kmalloc_node() @@ -579,7 +594,7 @@ void *kmem_cache_alloc_node_noprof(struct kmem_cache *s, gfp_t flags, void *__kmalloc_noprof(size_t size, gfp_t flags) __assume_kmalloc_alignment __alloc_size(1); -void *__kmalloc_node_noprof(size_t size, gfp_t flags, int node) +void *__kmalloc_node_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node) __assume_kmalloc_alignment __alloc_size(1); void *__kmalloc_cache_noprof(struct kmem_cache *s, gfp_t flags, size_t size) @@ -679,7 +694,7 @@ static __always_inline __alloc_size(1) void *kmalloc_node_noprof(size_t size, gf kmalloc_caches[kmalloc_type(flags, _RET_IP_)][index], flags, node, size); } - return __kmalloc_node_noprof(size, flags, node); + return __kmalloc_node_noprof(PASS_BUCKET_PARAMS(size, NULL), flags, node); } #define kmalloc_node(...) alloc_hooks(kmalloc_node_noprof(__VA_ARGS__)) @@ -730,8 +745,10 @@ static inline __realloc_size(2, 3) void * __must_check krealloc_array_noprof(voi */ #define kcalloc(n, size, flags) kmalloc_array(n, size, (flags) | __GFP_ZERO) -void *kmalloc_node_track_caller_noprof(size_t size, gfp_t flags, int node, - unsigned long caller) __alloc_size(1); +void *__kmalloc_node_track_caller_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node, + unsigned long caller) __alloc_size(1); +#define kmalloc_node_track_caller_noprof(size, flags, node, caller) \ + __kmalloc_node_track_caller_noprof(PASS_BUCKET_PARAMS(size, NULL), flags, node, caller) #define kmalloc_node_track_caller(...) \ alloc_hooks(kmalloc_node_track_caller_noprof(__VA_ARGS__, _RET_IP_)) @@ -757,7 +774,7 @@ static inline __alloc_size(1, 2) void *kmalloc_array_node_noprof(size_t n, size_ return NULL; if (__builtin_constant_p(n) && __builtin_constant_p(size)) return kmalloc_node_noprof(bytes, flags, node); - return __kmalloc_node_noprof(bytes, flags, node); + return __kmalloc_node_noprof(PASS_BUCKET_PARAMS(bytes, NULL), flags, node); } #define kmalloc_array_node(...) alloc_hooks(kmalloc_array_node_noprof(__VA_ARGS__)) diff --git a/kernel/configs/hardening.config b/kernel/configs/hardening.config index 8a7ce7a6b3ab..3fabb8f55ef6 100644 --- a/kernel/configs/hardening.config +++ b/kernel/configs/hardening.config @@ -20,6 +20,7 @@ CONFIG_RANDOMIZE_MEMORY=y # Randomize allocator freelists, harden metadata. CONFIG_SLAB_FREELIST_RANDOM=y CONFIG_SLAB_FREELIST_HARDENED=y +CONFIG_SLAB_BUCKETS=y CONFIG_SHUFFLE_PAGE_ALLOCATOR=y CONFIG_RANDOM_KMALLOC_CACHES=y diff --git a/mm/Kconfig b/mm/Kconfig index b4cb45255a54..e0dfb268717c 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -273,6 +273,23 @@ config SLAB_FREELIST_HARDENED sacrifices to harden the kernel slab allocator against common freelist exploit methods. +config SLAB_BUCKETS + bool "Support allocation from separate kmalloc buckets" + depends on !SLUB_TINY + default SLAB_FREELIST_HARDENED + help + Kernel heap attacks frequently depend on being able to create + specifically-sized allocations with user-controlled contents + that will be allocated into the same kmalloc bucket as a + target object. To avoid sharing these allocation buckets, + provide an explicitly separated set of buckets to be used for + user-controlled allocations. This may very slightly increase + memory fragmentation, though in practice it's only a handful + of extra pages since the bulk of user-controlled allocations + are relatively long-lived. + + If unsure, say Y. + config SLUB_STATS default n bool "Enable performance statistics" diff --git a/mm/slab.h b/mm/slab.h index b16e63191578..d5e8034af9d5 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -403,16 +403,18 @@ static inline unsigned int size_index_elem(unsigned int bytes) * KMALLOC_MAX_CACHE_SIZE and the caller must check that. */ static inline struct kmem_cache * -kmalloc_slab(size_t size, gfp_t flags, unsigned long caller) +kmalloc_slab(size_t size, kmem_buckets *b, gfp_t flags, unsigned long caller) { unsigned int index; + if (!b) + b = &kmalloc_caches[kmalloc_type(flags, caller)]; if (size <= 192) index = kmalloc_size_index[size_index_elem(size)]; else index = fls(size - 1); - return kmalloc_caches[kmalloc_type(flags, caller)][index]; + return (*b)[index]; } gfp_t kmalloc_fix_flags(gfp_t flags); diff --git a/mm/slab_common.c b/mm/slab_common.c index e0b1c109bed2..9b0f2ef951f1 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -702,7 +702,7 @@ size_t kmalloc_size_roundup(size_t size) * The flags don't matter since size_index is common to all. * Neither does the caller for just getting ->object_size. */ - return kmalloc_slab(size, GFP_KERNEL, 0)->object_size; + return kmalloc_slab(size, NULL, GFP_KERNEL, 0)->object_size; } /* Above the smaller buckets, size is a multiple of page size. */ diff --git a/mm/slub.c b/mm/slub.c index 3d19a0ee411f..80f0a51242d1 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -4117,7 +4117,7 @@ void *__kmalloc_large_node_noprof(size_t size, gfp_t flags, int node) EXPORT_SYMBOL(__kmalloc_large_node_noprof); static __always_inline -void *__do_kmalloc_node(size_t size, gfp_t flags, int node, +void *__do_kmalloc_node(size_t size, kmem_buckets *b, gfp_t flags, int node, unsigned long caller) { struct kmem_cache *s; @@ -4133,32 +4133,32 @@ void *__do_kmalloc_node(size_t size, gfp_t flags, int node, if (unlikely(!size)) return ZERO_SIZE_PTR; - s = kmalloc_slab(size, flags, caller); + s = kmalloc_slab(size, b, flags, caller); ret = slab_alloc_node(s, NULL, flags, node, caller, size); ret = kasan_kmalloc(s, ret, size, flags); trace_kmalloc(caller, ret, size, s->size, flags, node); return ret; } - -void *__kmalloc_node_noprof(size_t size, gfp_t flags, int node) +void *__kmalloc_node_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node) { - return __do_kmalloc_node(size, flags, node, _RET_IP_); + return __do_kmalloc_node(size, PASS_BUCKET_PARAM(b), flags, node, _RET_IP_); } EXPORT_SYMBOL(__kmalloc_node_noprof); void *__kmalloc_noprof(size_t size, gfp_t flags) { - return __do_kmalloc_node(size, flags, NUMA_NO_NODE, _RET_IP_); + return __do_kmalloc_node(size, NULL, flags, NUMA_NO_NODE, _RET_IP_); } EXPORT_SYMBOL(__kmalloc_noprof); -void *kmalloc_node_track_caller_noprof(size_t size, gfp_t flags, - int node, unsigned long caller) +void *__kmalloc_node_track_caller_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, + int node, unsigned long caller) { - return __do_kmalloc_node(size, flags, node, caller); + return __do_kmalloc_node(size, PASS_BUCKET_PARAM(b), flags, node, caller); + } -EXPORT_SYMBOL(kmalloc_node_track_caller_noprof); +EXPORT_SYMBOL(__kmalloc_node_track_caller_noprof); void *__kmalloc_cache_noprof(struct kmem_cache *s, gfp_t gfpflags, size_t size) { diff --git a/scripts/kernel-doc b/scripts/kernel-doc index 95a59ac78f82..2791f8195203 100755 --- a/scripts/kernel-doc +++ b/scripts/kernel-doc @@ -1729,6 +1729,7 @@ sub dump_function($$) { $prototype =~ s/__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +//; $prototype =~ s/__(?:re)?alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\) +//; $prototype =~ s/__diagnose_as\s*\(\s*\S+\s*(?:,\s*\d+\s*)*\) +//; + $prototype =~ s/DECL_BUCKET_PARAMS\s*\(\s*(\S+)\s*,\s*(\S+)\s*\)/$1, $2/; my $define = $prototype =~ s/^#\s*define\s+//; #ak added $prototype =~ s/__attribute_const__ +//; $prototype =~ s/__attribute__\s*\(\( From patchwork Mon Jul 1 19:13:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13718561 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 BF9FDC2BD09 for ; Mon, 1 Jul 2024 19:13:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3471F6B009E; Mon, 1 Jul 2024 15:13:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 297056B00A3; Mon, 1 Jul 2024 15:13:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D4E116B009A; Mon, 1 Jul 2024 15:13:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 79E266B009E for ; Mon, 1 Jul 2024 15:13:08 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 31EE481AE5 for ; Mon, 1 Jul 2024 19:13:08 +0000 (UTC) X-FDA: 82292131656.09.9C5590D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf26.hostedemail.com (Postfix) with ESMTP id 75297140020 for ; Mon, 1 Jul 2024 19:13:06 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=mMao9yoX; spf=pass (imf26.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719861175; 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=siYgYiAhCvUSPA8SJhrIVBJeroAt8eFvCxrlbbpf4yM=; b=oGk+pUwLuuBXWVGk+Fsptthx792ZCL2O9d2ZrrcDDh/AEHG8gLuz7CHqPDXiGdhQVf/Q+T rBtRNV6xowGEA82AuWMB75393Lztk/+1jzKSD1Og5dOB7KFlNT3lIsM4ggb4KBdzmZ6f6K /3SMvLYbhDULXtHpO/RSoBD8PPw6KHc= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=mMao9yoX; spf=pass (imf26.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719861175; a=rsa-sha256; cv=none; b=lW2OJhc971BzX/EKvS0cLKD06qlqScrnkEFjBz4h5Xzyuugjc9/0i4qO370P1isj6wb+h1 qeb8s4ovp5qpprqdt/3DQXPimvjnHCBiQ8vqm7gYgnZ++KQQr717vemBwIvBTLAGAGzRJG W96K7GJUCd6JeKOReVVsNL6JWePOH3E= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 6B1DA61789; Mon, 1 Jul 2024 19:13:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B4989C4AF0A; Mon, 1 Jul 2024 19:13:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719861184; bh=xxet30UEfeVq/dWdKRNFzCWZboWOxErsseLhti/TXwg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mMao9yoX18zMVhMQY5kIolTbGVQ1jNtDDjmsTCB1JdDobR3dwbrFWHV4S6VLmWFQ6 8klmPbsi/OtNrbFwfYS/rBPG3KZGTj2fhyS7xNyw5asCH7SzSgCPO5zq4qlbgmJVC4 NlzKgmInTFyAWCzXWeqI/pP1k9mxSB9+5iA/G25w4RRI/8F7Fn4l/TaWWuIZgjbV0O 4PFTz+aPo3ftiGxkUDdGoJVCN+BSH5sVJriXl+rpVGOiq3qanJo2M802ddSfZPGdfg rZo1KXvtSOciCb+zMGA/Wx9Mm8L8/fQQBF2P5oOH8z0fF+CUrt0RzcxI+SL5n/fjyN gvLwRdyObFs6A== From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , "GONG, Ruiqi" , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , jvoisin , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Xiu Jianfeng , Suren Baghdasaryan , Kent Overstreet , Jann Horn , Matteo Rizzo , Thomas Graf , Herbert Xu , "Gustavo A. R. Silva" , Jonathan Corbet , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v6 3/6] mm/slab: Introduce kvmalloc_buckets_node() that can take kmem_buckets argument Date: Mon, 1 Jul 2024 12:13:00 -0700 Message-Id: <20240701191304.1283894-3-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240701190152.it.631-kees@kernel.org> References: <20240701190152.it.631-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2960; i=kees@kernel.org; h=from:subject; bh=xxet30UEfeVq/dWdKRNFzCWZboWOxErsseLhti/TXwg=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmgv++rAHw0U+CEnyXA/4XClLK6QAyqHm8wswfo OqdafG1Bj+JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZoL/vgAKCRCJcvTf3G3A JnwwEACfJUwABmsGkmhqQnATf7Y0sAvd2R6x4c0rtnoHIuF4bVZHVNfrX+sm0soE98hTF6dJhSb 54g7YCknTpz7GmrfaJ5B+6Nt892AgOuCstHR6q3rMDkpbqRRk7a3VIOEqbNnPUNJpyf4Wj6oTB+ FpKGwepyFVPdvd7Prn6jPRJzZQmnRlJRqG73USLhSw5sSEQjd452tI2JG+gHL/qwx11dQeJHiEr 9+/VZ1zhVvgJ1rrfQ4zOcSEPDuuIKjF75SGADyD6H0y20WrMSuGAHFoJpbJYxDkAJE4Gv1Yq28b NHiH8cJzxNi85ddufgze9mVDh6j/09oxZUB6Q4kZYmqy9Xu+f7qrxqQbLbnKV1XYYP9ZGJN0v8H Ludofn6ojZhgH8KOYlpEMD77aG9nOAYKMFPg5wBqXoXlzfhUVhS1Q3UlodTk7WQR9dhez0JEBpW /jw6xO8QkzUt0yBRT3nry/LJsmZmaeY54ekml6BugsOLniwFjBaq/fs/7a6TIDRO8ESL24TtvrB 1GNHbIRz/GxaxHXegXWceNTTLerX3JSOVEnG3qv1zJsV9XPTMAhmlZAV2DrLasptG6tkwgPC9zo Mf0SCpzv9ElM+HZzsRuDAJL6CkBHzXvbgW0IH4traaTQNdAHVoBKHn0pxZfluvUuUt4eiE9VO3D 14HEVxu1s4wtbJg= = X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 75297140020 X-Stat-Signature: 8nohi8s9qbf9icdj6kb7fg1dy1m1sx3s X-Rspam-User: X-HE-Tag: 1719861186-817839 X-HE-Meta: U2FsdGVkX18pLhGOoADupfwIinHwCTJZaAoCXDUJ0RJIJoWjU3DKTmuOXjdZNUo96I8rh0eNmHgezywxpALIgt6rwUbN1VU3rNRS/C2jRpFi9ECdlN2ADM1Xx2EgQg4P2a/4emDjZ9Q+0lZOAm6tqKgAyONyz5eIOR+XpyDhAqLbRKfys2gE45DYMZlI6eAxAlMGueJyeY/MrSc/22ZGKB661dMovJWYERCRUjYbz+QH8Ks1CPyJsWsRTvTjBxwsgJfMgqg8SqVnNslmhEP5Y4nhNkIf3OMV27JwuwAKMUvw3MT6Wwln4N/rXmI6rU9sCGkOtTCieJt6ENzKSOxRFXLkqNUJgcciX+7FC6gkS2IeYz91sTUORUI3C32w75I0cYuUkuAF3Rxyaa/KqaG4IvYXAm5dsOxvPrnSvj1joWkgCFZ15v+8yRPI8m0oYoCtk2EFvIRP5gQ43IZi4RbDFLBtFmyr1phmUeoqYeJX0qSG15NIcKM1eOLdtAvaK1sjVAJgMB/J09BgiEA3Za1QYvdvc5Vq3Vf4bYsG4f2ho1lAayN5JwpZtedjkfMjd2nunYau1HBDKH+wLSZ2tmltvrc4bG6gTr7AvTYjEGcaVXzxquv0+7XcmCbQYAfEIBJNkVDZlTGNUtuja+2hfHZTizoUhITS6672tn5MXw3IWmOYUGsjTlEzHJUczP+orY7VtcegVpUGt838Lqmyj+Ems0soIJykC3bZDEuG8yG04EQ54Zx0lBxf8eHLXUSSijDJYOq+XupmJr/dPjGZOrFf8gUsxL9DfMewlkkCnyrMErrHpxMRRj/s//uXFo5f0Ci1nK/ophISBsZD5wUYbdkCJMDlpEUhFaqEaLhZgFe1fGi8ypd1RmIF+Z7mBc66XKe1zdrnj927izOH9qpm9/JGdxxd+cqhYnow7RBtgu+rHtCtweTYss0Nn43S3mJMHTn4uSGA3AztLgAEBfb9MXu o7vtkX84 RkdnieZhvWQ7JjugJA3gYMZsF2/6WbnsQj1NGIsPyvYIa8NSYtstrHLRvHc9eNLwaft+Od8pvY+pcVj6jQ5nbYvy6C4FP7+h5e+FACyvwtfA6La/GH5LYVUYzSAfY42CxbwAspY4XVcqXnEdQ2HuY9ygF6ZxpFH/I/TXYoLUm4it6u78J/Fblr9iHraf84bGRFAhu7RVZnGHrrPPTEYn9Y/lenDBgDz+R4mbXZnYOT1B8hmO1qY76ymrAlzvHFNmsavqbxhXCpLVISjvm77ell7EsrM7O+U3IY4kt6hdjhJTyqMKXrv3+Ia2zW05U6BhZiCaB3qxAnmYSjVjfMl98oW3K6byM29LYpS6gKmM863WbQYs= 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: Plumb kmem_buckets arguments through kvmalloc_node_noprof() so it is possible to provide an API to perform kvmalloc-style allocations with a particular set of buckets. Introduce kvmalloc_buckets_node() that takes a kmem_buckets argument. Signed-off-by: Kees Cook --- include/linux/slab.h | 4 +++- mm/util.c | 9 +++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 708bde6039f0..8d0800c7579a 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -798,7 +798,9 @@ static inline __alloc_size(1) void *kzalloc_noprof(size_t size, gfp_t flags) #define kzalloc(...) alloc_hooks(kzalloc_noprof(__VA_ARGS__)) #define kzalloc_node(_size, _flags, _node) kmalloc_node(_size, (_flags)|__GFP_ZERO, _node) -extern void *kvmalloc_node_noprof(size_t size, gfp_t flags, int node) __alloc_size(1); +void *__kvmalloc_node_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node) __alloc_size(1); +#define kvmalloc_node_noprof(size, flags, node) \ + __kvmalloc_node_noprof(PASS_BUCKET_PARAMS(size, NULL), flags, node) #define kvmalloc_node(...) alloc_hooks(kvmalloc_node_noprof(__VA_ARGS__)) #define kvmalloc(_size, _flags) kvmalloc_node(_size, _flags, NUMA_NO_NODE) diff --git a/mm/util.c b/mm/util.c index c9e519e6811f..28c5356b9f1c 100644 --- a/mm/util.c +++ b/mm/util.c @@ -594,9 +594,10 @@ unsigned long vm_mmap(struct file *file, unsigned long addr, EXPORT_SYMBOL(vm_mmap); /** - * kvmalloc_node - attempt to allocate physically contiguous memory, but upon + * __kvmalloc_node - attempt to allocate physically contiguous memory, but upon * failure, fall back to non-contiguous (vmalloc) allocation. * @size: size of the request. + * @b: which set of kmalloc buckets to allocate from. * @flags: gfp mask for the allocation - must be compatible (superset) with GFP_KERNEL. * @node: numa node to allocate from * @@ -609,7 +610,7 @@ EXPORT_SYMBOL(vm_mmap); * * Return: pointer to the allocated memory of %NULL in case of failure */ -void *kvmalloc_node_noprof(size_t size, gfp_t flags, int node) +void *__kvmalloc_node_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node) { gfp_t kmalloc_flags = flags; void *ret; @@ -631,7 +632,7 @@ void *kvmalloc_node_noprof(size_t size, gfp_t flags, int node) kmalloc_flags &= ~__GFP_NOFAIL; } - ret = kmalloc_node_noprof(size, kmalloc_flags, node); + ret = __kmalloc_node_noprof(PASS_BUCKET_PARAMS(size, b), kmalloc_flags, node); /* * It doesn't really make sense to fallback to vmalloc for sub page @@ -660,7 +661,7 @@ void *kvmalloc_node_noprof(size_t size, gfp_t flags, int node) flags, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP, node, __builtin_return_address(0)); } -EXPORT_SYMBOL(kvmalloc_node_noprof); +EXPORT_SYMBOL(__kvmalloc_node_noprof); /** * kvfree() - Free memory. From patchwork Mon Jul 1 19:13:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13718562 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 ADD76C2BD09 for ; Mon, 1 Jul 2024 19:13:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 88D186B009A; Mon, 1 Jul 2024 15:13:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 751796B00A0; Mon, 1 Jul 2024 15:13:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 334106B009A; Mon, 1 Jul 2024 15:13:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id E17E36B009E for ; Mon, 1 Jul 2024 15:13:08 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 99A351C3641 for ; Mon, 1 Jul 2024 19:13:08 +0000 (UTC) X-FDA: 82292131656.26.4636ED3 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf24.hostedemail.com (Postfix) with ESMTP id C1585180011 for ; Mon, 1 Jul 2024 19:13:06 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RpSfmTgx; spf=pass (imf24.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719861175; a=rsa-sha256; cv=none; b=ElCMlZh2EEbHVQNExGE17OF5Z28KKuCjxnugIkElZMiFtNiNKh9py/fW7JgekDfOcpOv62 V08f2skEBYAI2iOSp8uWOucVlZj7f7gcAYWfxiKl3UqFJUqPmGkAbrlo3mmEyf3vP/HKnw 6dxxSK9aRevtJEbxv8a5zPN5OM2Qc98= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=RpSfmTgx; spf=pass (imf24.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719861175; 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=IL8BUaVY4e889rtWHb5bVtbIQfIcAxl0t0VbfdZZ2UE=; b=6W3qmOPTqT7BmObXz94BQwYek8r+WSup7VlbmqpVs0LKKbESFJY2YN3AlBmwXJ0WuWuMqD 9/wnu5Z+X7KCMj8XzNS6m7Z9RF6xfH/sjqXk69pCSudSADaHb1dX1DFwwactbBg5PsNqCE AxN9AWziMdBtglOsJxbwD4DzJ65iass= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 8698F61784; Mon, 1 Jul 2024 19:13:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BEF06C4AF0C; Mon, 1 Jul 2024 19:13:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719861184; bh=iLr1JO1gVFtydBJDlW/Z6SdDIb8sQtIWfFXCMaocDcg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RpSfmTgxm2O1XtuIFl+3ZxeSoxYypsrEU0+nDkunMVjz9fqvzNZXImfKfOgbhxqvZ CWJEEidhNGTyifG0nR+rXVJIx/A2qesIuRGrcbMilA4qSyFRmRnfeGtpcB6RwcdXAO 8FlpEO+3qZiqtJZNOnTdQuAwR7KU7OgZ5Fyv++2LC0iKVbRQ6H3cWeLFzRXFvoqnM0 eSrGAf0KiZ/UCgZ8S2+lv2V61tOvs+nM9c+XIVf2D9KThxnpza0obf9zEimu91+0nu 5mMnT0ywl5hdZUH+bX9G3igqs+pfRNBBTHgMRtShjduDuQlJt4j1RjRb3SuLNeQ4xo mLYYJCU2yV6Ew== From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , "GONG, Ruiqi" , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , jvoisin , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Xiu Jianfeng , Suren Baghdasaryan , Kent Overstreet , Jann Horn , Matteo Rizzo , Thomas Graf , Herbert Xu , "Gustavo A. R. Silva" , Jonathan Corbet , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v6 4/6] mm/slab: Introduce kmem_buckets_create() and family Date: Mon, 1 Jul 2024 12:13:01 -0700 Message-Id: <20240701191304.1283894-4-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240701190152.it.631-kees@kernel.org> References: <20240701190152.it.631-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=8116; i=kees@kernel.org; h=from:subject; bh=iLr1JO1gVFtydBJDlW/Z6SdDIb8sQtIWfFXCMaocDcg=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmgv++kshPnyXBQpYmXkhyaUZvkPmyCCbIBrXnN 5/UgqpveV2JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZoL/vgAKCRCJcvTf3G3A Ji6UEACxYLIBboDvKj5qoLIIzqp3hMa09xT5pXc8GbKmY2/GyXBftV83vive0p+5epvrpOMcCJj UdH1IPLxIdnSKJP/L/8+Rv9jj1BGjR6WTWWOv7GcNtMFdeXzAuh6c/mxpLYV8FRSYvFkXuhbab4 ERTeCuecezcFkNxqUYpcRO75k/eyY5twXVS4i42r/VGX1MOQOC00xBBZI4QqwAirHWU7TzmshBX t9dPiM/LqzE54MRSghuxmUFpVavRcaIGqE9gjNIEeV7ELNEBvT3UpdRuImMkWoxNKQE7he5T6h/ NtO+zOa14B1oBkp4YFjZHLuorD9gkhEsxIVkaPxe7eniA1a4r8s8/IY2/MbMeznYldyYmrNsxwY jbAGyovGKjvR/UWHJSNqT7X2ImJ2UdEt8dkJ8Zck+crZ4wBGPUnPgbANbT/OaiY6MSV5H5pwvG0 K7JQntezgoxoQ29Y1GTD0QpiESxigH3wAERxkJaXnMEL59PyEBWblaDhJz76ttKLqon7S79FZpp jAHE5SZWTFjOBBAwCDhsCp0NlD5jAQ70cJkGWgLYmfK8KWb0geMS3TKZs8yGCytCBA8YpTkM1Xc V2jkNV/A54x/ujur4vKgrirvvVJ+uK2WPCIB3AOYVdfxHAL6EknMufnamvya3upXuHwTcD3TU8u RuEkWXQJaZEN9eQ= = X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Stat-Signature: my9kcg69tuhhaxgaywbb8undkrntok7c X-Rspamd-Queue-Id: C1585180011 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1719861186-398408 X-HE-Meta: U2FsdGVkX18e2Ea87fw4QUPJ0sFjOXfEYiVm6hB1ha3SDrvdi7IHqh0yyCTHszLB+CfybwRGX8GADpIGf11I7eM0YpvyK4zS0rRS8iEZoYEUztu4Ygpur4DeGUUS3Y6YztZzjVI0ePcZ3PCo9d2W50GImQMdJ9kEgOEpU4cQZQn1vKSYZsN5lRhL00w8/VIomgbWq64v6TGhDe1SI+zYm+3k4nCTvJHA6Y6Qd+ZJjTh5HtkgwORw0k/ELbTBn0tSPQE8u4HR4PCjVXyY1sCp/x7R5WGEGQNQWKZ2j/3UmOm6kBixjYlB32Ucvmyz8uoHFn50/BukwbANjgXFt3AhE1cSnG0I9xOIAWaoqzYW4U8SRlY10FkXWxPAsFVWedH6WnMBnQglpMs4u+98PSfSguGWOX47Ct63RZkiQFGGudxDsbLKipza+M8aE7YT+m7v+JwoGhCRa/ncLR++q3kpL9k3vtm5gqoD2sE251qpM4NUz9BGo4TtadqkgssW//euL5Y0Hilm1k9Cj1CuROT+KDCJFmaeDAYuqfIFTmfxtsZvIP5OmtHv1nk0Rn4HhyHRxHxNpk5bZJTi76b30odGZUXxxvgQUY4O4MGpaBdbXZMUXboQbY4TuAZpj71OdWHpDw/Z+i65tQ0dJ/4H3VpwBdwl4gosJR09A3Go3C8qAYKwWbGqpwL5zdiKivr/D4wqIwS9djmx0xvJCxESlwyQ0RAiRkJ/ABvutg+hjdjMm/aSdwKYDHZWNnIs0F9dtApd7dNuMTvBoHeQWJlGPAvJforF7SQ1eHSCx4F82Z26ehon6eMpte+wJlxcHLDGTNdP6CT5wqFobAVimlaIXrPluEBgqU8E/wgrFbmRO+8lLDBOcXZx3gRpwTAXpE9S8JYA3f/6k1ZoIVCPaGjIqfp+KIehyBmthkzDouHgWvwb76PpFd+PVLe6AvAn9enEq2Bos2kFMy/mwiRY4yvvJHi nZhdrC6Z 17Aj61mBVAxyJWQ1cuOdTx9Tc8gxALwaC/+c/G9J1nfzWROB0TBqEVPP7C3SJQ6MSf5E3cSMvY52e1hZ4XfBv2sYEoGEVMZZn7qkIAzP+psVxQvcfPVyNzXdlztrWppzMFx4K56P8C/43BmJ1+YHj7lqy440/a8j/0STwwWzGVSSqL7UhGybgSq83vGsqb8qunjFvx4n82aRTg0VZ2IbvCar9gq6D/DMyGuU7o7tKqTUpqQ39aocjce1pyCvQo/CxrYIVo6QQOCPQnzc9964vvHLIshi6w9ZYC1zFPDBXg9MfXoFB077mOQ+ffg96QkBEWay/jWoJ02mOrn0lXXj+XZHNpN2UGeYOqRbg9WmmRo/2C7Gt9QO1pVAwdcyr3jLRGP1Y9vaGdtK/c92aHoobDQf+236xw1ReCsIKQ8o/nYoIXTcashY+vEWf0uE9PzLenJqQJV+TIH4wslul0Do8cGOmNw== 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: 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 isolating these kinds of interfaces will not become an unbounded game of whack-a-mole. For example, many pass through memdup_user(), making isolation there very effective. In order to isolate user-controllable dynamically-sized allocations from the common system kmalloc 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 fallback is needed. Note that these caches are specifically flagged with SLAB_NO_MERGE, since merging would defeat the entire purpose of the mitigation. This can also be used in the future to extend allocation profiling's use of code tagging to implement per-caller allocation cache isolation[1] even for dynamic allocations. Memory allocation pinning[2] is still needed to plug the Use-After-Free cross-allocator weakness (where attackers can arrange to free an entire slab page and have it reallocated to a different cache), 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] Signed-off-by: Kees Cook --- include/linux/slab.h | 12 ++++++ mm/slab_common.c | 96 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/include/linux/slab.h b/include/linux/slab.h index 8d0800c7579a..4c083f3196f4 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -549,6 +549,10 @@ void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru, void kmem_cache_free(struct kmem_cache *s, void *objp); +kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags, + unsigned int useroffset, unsigned int usersize, + void (*ctor)(void *)); + /* * Bulk allocation and freeing operations. These are accelerated in an * allocator specific way to avoid taking locks repeatedly or building @@ -681,6 +685,12 @@ static __always_inline __alloc_size(1) void *kmalloc_noprof(size_t size, gfp_t f } #define kmalloc(...) alloc_hooks(kmalloc_noprof(__VA_ARGS__)) +#define kmem_buckets_alloc(_b, _size, _flags) \ + alloc_hooks(__kmalloc_node_noprof(PASS_BUCKET_PARAMS(_size, _b), _flags, NUMA_NO_NODE)) + +#define kmem_buckets_alloc_track_caller(_b, _size, _flags) \ + alloc_hooks(__kmalloc_node_track_caller_noprof(PASS_BUCKET_PARAMS(_size, _b), _flags, NUMA_NO_NODE, _RET_IP_)) + static __always_inline __alloc_size(1) void *kmalloc_node_noprof(size_t size, gfp_t flags, int node) { if (__builtin_constant_p(size) && size) { @@ -808,6 +818,8 @@ void *__kvmalloc_node_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node) #define kvzalloc(_size, _flags) kvmalloc(_size, (_flags)|__GFP_ZERO) #define kvzalloc_node(_size, _flags, _node) kvmalloc_node(_size, (_flags)|__GFP_ZERO, _node) +#define kmem_buckets_valloc(_b, _size, _flags) \ + alloc_hooks(__kvmalloc_node_noprof(PASS_BUCKET_PARAMS(_size, _b), _flags, NUMA_NO_NODE)) static inline __alloc_size(1, 2) void * kvmalloc_array_node_noprof(size_t n, size_t size, gfp_t flags, int node) diff --git a/mm/slab_common.c b/mm/slab_common.c index 9b0f2ef951f1..641cf513d2d1 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -392,6 +392,98 @@ kmem_cache_create(const char *name, unsigned int size, unsigned int align, } EXPORT_SYMBOL(kmem_cache_create); +static struct kmem_cache *kmem_buckets_cache __ro_after_init; + +/** + * kmem_buckets_create - Create a set of caches that handle dynamic sized + * allocations via kmem_buckets_alloc() + * @name: A prefix string which is used in /proc/slabinfo to identify this + * cache. The individual caches with have their sizes as the suffix. + * @flags: SLAB flags (see kmem_cache_create() for details). + * @useroffset: Starting offset within an allocation that may be copied + * to/from userspace. + * @usersize: How many bytes, starting at @useroffset, may be copied + * to/from userspace. + * @ctor: A constructor for the objects, run when new allocations are made. + * + * Cannot be called within an interrupt, but can be interrupted. + * + * Return: a pointer to the cache on success, NULL on failure. When + * CONFIG_SLAB_BUCKETS is not enabled, ZERO_SIZE_PTR is returned, and + * subsequent calls to kmem_buckets_alloc() will fall back to kmalloc(). + * (i.e. callers only need to check for NULL on failure.) + */ +kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags, + unsigned int useroffset, + unsigned int usersize, + void (*ctor)(void *)) +{ + kmem_buckets *b; + int idx; + + /* + * When the separate buckets API is not built in, just return + * a non-NULL value for the kmem_buckets pointer, which will be + * unused when performing allocations. + */ + if (!IS_ENABLED(CONFIG_SLAB_BUCKETS)) + return ZERO_SIZE_PTR; + + if (WARN_ON(!kmem_buckets_cache)) + return NULL; + + b = kmem_cache_alloc(kmem_buckets_cache, GFP_KERNEL|__GFP_ZERO); + if (WARN_ON(!b)) + return NULL; + + flags |= SLAB_NO_MERGE; + + for (idx = 0; idx < ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL]); idx++) { + char *short_size, *cache_name; + unsigned int cache_useroffset, cache_usersize; + unsigned int size; + + if (!kmalloc_caches[KMALLOC_NORMAL][idx]) + continue; + + size = kmalloc_caches[KMALLOC_NORMAL][idx]->object_size; + if (!size) + continue; + + short_size = strchr(kmalloc_caches[KMALLOC_NORMAL][idx]->name, '-'); + if (WARN_ON(!short_size)) + goto fail; + + cache_name = kasprintf(GFP_KERNEL, "%s-%s", name, short_size + 1); + if (WARN_ON(!cache_name)) + goto fail; + + if (useroffset >= size) { + cache_useroffset = 0; + cache_usersize = 0; + } else { + cache_useroffset = useroffset; + cache_usersize = min(size - cache_useroffset, usersize); + } + (*b)[idx] = kmem_cache_create_usercopy(cache_name, size, + 0, flags, cache_useroffset, + cache_usersize, ctor); + kfree(cache_name); + if (WARN_ON(!(*b)[idx])) + goto fail; + } + + return b; + +fail: + for (idx = 0; idx < ARRAY_SIZE(kmalloc_caches[KMALLOC_NORMAL]); idx++) + kmem_cache_destroy((*b)[idx]); + kfree(b); + + return NULL; +} +EXPORT_SYMBOL(kmem_buckets_create); + #ifdef SLAB_SUPPORTS_SYSFS /* * For a given kmem_cache, kmem_cache_destroy() should only be called @@ -931,6 +1023,10 @@ void __init create_kmalloc_caches(void) /* Kmalloc array is now usable */ slab_state = UP; + + kmem_buckets_cache = kmem_cache_create("kmalloc_buckets", + sizeof(kmem_buckets), + 0, SLAB_NO_MERGE, NULL); } /** From patchwork Mon Jul 1 19:13:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13718563 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 C73DAC3065A for ; Mon, 1 Jul 2024 19:13:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D450D6B009F; Mon, 1 Jul 2024 15:13:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CAB706B00A0; Mon, 1 Jul 2024 15:13:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AAE386B00A3; Mon, 1 Jul 2024 15:13:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 80DBD6B009F for ; Mon, 1 Jul 2024 15:13:10 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3EF96121D73 for ; Mon, 1 Jul 2024 19:13:10 +0000 (UTC) X-FDA: 82292131740.20.F53BC20 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf12.hostedemail.com (Postfix) with ESMTP id 7401A40012 for ; Mon, 1 Jul 2024 19:13:08 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=gghdBhp7; spf=pass (imf12.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719861171; 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=PHFH1hF0XEKuDuy0/XJtFXBGJjxBd+MCfaHf6SVwRo8=; b=ZGrvSBtOV4Q4Q4Y/RXlDcYFOV02Kzda0MDNd7erZ+lwLOnpJe2ohsJGcGg76U3JVzSxtHK jC32h7jBqt3jCJvhNpABBQjOPIL4rZdRKxQ8JylnF0b9yli7Umt2Rvfw6CM30rhLkJ5P53 YXLmOHKF65mcyCscSEHAo0L+xagxyWU= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=gghdBhp7; spf=pass (imf12.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719861171; a=rsa-sha256; cv=none; b=gLkzYee0I9DLwQqlonf4LQLiBps+uVmhsTg5ZYfi+Xi052jCpv+m/rEzye+ZRmYDWB/5EU pPoPPbY4PZAV/R2LZ2ffEkJ2UwaMFUqAbd+BSJSuky3NYcsrA0xwT0OfvRalIzM216GDs4 IZXG+DXTCRgxDGn4J5woHLPjrS4hH5g= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 940E861791; Mon, 1 Jul 2024 19:13:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2019EC4AF0F; Mon, 1 Jul 2024 19:13:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719861185; bh=ZC+4xLsqMS25+wle+eOPiqDyMZP//OUcAmXX2J5RAVo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gghdBhp7ZB7XhRBrUQVzahbv03mmZLCVplJYE8pWnFaiQBh2snWO0KElN7hj5L0Ny TCJLGI1T2/AYbR2OOeDbeBWZfI7c7oL4M/JJ+RlL0S6mrIsnfyUr2asG/Co/Y4NEib 2S8SMe6fCT9MJr1eFjg0+faPtFSQNhPRNUFqlzP/oVpb1NFgNCMw9FHbeSyp8aQwd8 Rpp3mces1RFAQJXcr+XIjwJD+CSoa+o3l+nguqgsSPs0GKDCrrDHUS+spXwD2uqiPF isAeixm5YYh/xbBj+DX8ZhRg76tY6LlFeuQrEUT5H9ThGb16jkfh9m3ifBk/g38bmt dF3WKO2zZc9Gg== From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , "GONG, Ruiqi" , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , jvoisin , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Xiu Jianfeng , Suren Baghdasaryan , Kent Overstreet , Jann Horn , Matteo Rizzo , Thomas Graf , Herbert Xu , "Gustavo A. R. Silva" , Jonathan Corbet , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v6 5/6] ipc, msg: Use dedicated slab buckets for alloc_msg() Date: Mon, 1 Jul 2024 12:13:02 -0700 Message-Id: <20240701191304.1283894-5-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240701190152.it.631-kees@kernel.org> References: <20240701190152.it.631-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1954; i=kees@kernel.org; h=from:subject; bh=ZC+4xLsqMS25+wle+eOPiqDyMZP//OUcAmXX2J5RAVo=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmgv++179k0fHAvGjqEU5ltN8MHRwh6Dca9ahz8 0TL+TDEUBSJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZoL/vgAKCRCJcvTf3G3A JmaMEAChdkbMO+SdicBA6J36xZMom97yPCUJe+AQcsVFpIdELNyttcTScgDv/IxVDdTTaDyBHTr YayvHGh0Bi1wtq7wdAnfUZIju5ZUa/WCgNCX2fLmkOeyKifYhjk2i9+VtTvLMF6K9caBNiPSieA zYCUvURzBk7S44/7T4ThX23ycIiwE+oU9Ud4uP15daHyrsTeeX+aLrWunFHWf4kTgAr71yIitjr 3mqyNbU+g8ZieFgH4PDLYynu+TtPt4iO2wZWiZec5JKUOCxFXM8gah47jLrZhvfdS4y8JwaEt9m Vjla4RVs4n8rqtja/7dPtTBSB1GkhywqySFJoH1BYAt2Vn2vFpWf5I8u5bLmvJM87SdhhRDZyf6 WLCvokYur6sn8yBFlXEw4LO99JBoilqOqAWn+ErUmZLEJeY1HXMJhqTpqU7H2HHlBpleEymLIiN nkdQyxDF92oetfAMKw6rcHGic8gH4aqBBSAELkoPYc3RQg7CKUb8oQDwtNSCtdEgmYI0+rJ2J13 XH821MfWZoLrYcu86MJQ8X3ue/s6Qz+Fj3yp/11eMIW48V1qmlShzTv+ZoL365+1pAl5J0Wbilv XRniyDfgf6MQmT5FSDohh8ztx9NNEwlKNAGEaogx5tGQ8x9sQVSpCXj3bPqAzpYTbjOlm/kTmqh rmeH/xNCP1FCX6Q= = X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Stat-Signature: zpwzfm5nc7o1pp3fhmnf6chzrr6afk1t X-Rspam-User: X-Rspamd-Queue-Id: 7401A40012 X-Rspamd-Server: rspam02 X-HE-Tag: 1719861188-373600 X-HE-Meta: U2FsdGVkX1+KAZxXpV1bAD4vxneEodXCH3fGKXH31lGwVf/hW46NSvcyfF6crAPYCmv1Rpa+mb2wgq+25ywOJbmaVqT5QXSVh4WNb5itpzg63ofElcnJFX7mQ+tpZYlWR9fmfQaUJRPDpze/G9xPmV8YmVGyXASnR+PDDikX8pm4BCskxe7cs8zstlfftzpqFi7o7a0hd6mgCpDelu5WjH+gZDl0cmire2tG97LMCa0NsItPofO7Tn8/40ivpu8PfzrKJ6xaAkAjj+H0X6KIuPbCRFvmdSv9AiQa56qRPca3L7MxJsZmUUGYDvFdV+w/3kglPuNW3Or06GR+SdOAfecORvrzLHqC4jj3aWHOVuqckA+6Ks7Un1apsZEqJoCV/DQOMck76i6r6+k0wyFxtcINlX785HnvGXwelTP5o3Qh/X5nkBZPgfMMSYuTOuk+8/PLOY2f5oIixaj5dU88R+yei9hFDhasUjpTfqwX0cVgc6FiSKAB+v9F/vOI0nfEPvCGFNUHVx6LbMy4d/EU6LfgjzrIPTjX8J9DNTW/7X5iX9ZSDNT1lX7fJqDITiR9D09jC4gY6JHStfY1wc7/CUhe0bQif2RKV3sULq+np8XjV3wJ7TBZdLykIU5svaI5nPs2ZyUCa2vLfTl2xec4zA6D1CKf6lrjBfJ2rpCoRI4yhAjblnE+jSzBdJZDqr35g2dum6i0djHKoEeBjutXzLPWdGYYPOJ7vV/e+X4c5jcM8q4XRUw6yO/3WCmnLZv9Tpi/w8Kei403PYK8Gj3NbeNgHEAgMQqBzuEFqkRoo9WDpTkG3IASCmgZoUXL1AfJQdFVGkeT4fWkflkMV6uXBXwgwBiHarKkOiaJPyoqlmgHSeyZknjar0+rD5IN2hO1TKKhY0rLo5MugXu8KkFyx0lzV48X975+twRe+hyvBKOVX0lWp86URu/bsazoY60RN9UgGpSH8toNqFzyQ17 ovq8FYlb +O69ueKJV+nVkAafDHDQpomZ+ECKjygkbD1oa1mYvsh/B0t7KwnP2SI0/VTmOryFbvFYaA4hlRNig/I5ghFYYuRDXnTlE92UihiX2LhhcrZY5CcgAefPmEJnuupsEYFbEH+BPVKewAihIQHz4MyRaqrCX3dBWz3dukGAHCJBRxSWPN4qN9k7i8k0JOOQ2iUnUP+VuVeXQ9KGw7fXzF8pMgvGc5goVQq/rPP88yp1obxqGKlb4q3xv4v/u0USirFj4apk/4G7ny4gSOEeal8y6vmU1KgOf6YrAw54F5P0qUNlMGsahboWUPGUmu+lW04jzdNcy1qzJILGa02mWTF5zvAL+AQIZQFq98j28THRrhnzOuQijRCc8IhWGguUK9kzmiYE1rcrbREnaytNDcXKuMeJ+Ni0yuCYB7+fPp6C3i/u5kFwLRfbAbjA0jZKEd5FAJyC1MO8yizMe46klYTMNLTiBOk8KI1IyjH9O2H/BlXc/E4QckdMy5VvLELOFGV+uwF80CzbkuZeiakaYnVgr5K9lsgwZTHxAPux9iUar6ArMGBnusWcdk7E6skSX7roB1vk0QgXoEo3pn4ui2QZzwpcW/RJbxrr0Wl6r3fUc25F71LKHXqiIDfUHkjfft2Z1gH4JyYlq1rHbOf/hcLXFeMInk5xMHUtWRHLN1rlyQUsZPEDIwoehihrIr+WScCILuMWs04DOTz/81AYSZnGOZ9gak4+PkqZKWdxBD5lH4C0mJgcOqwmIhK9Fbb8RFFMVjBYpRlWnuqNW4nroCVp9yT/nmB+9qdvdX1nHfymkurVwMUXX109VNoEJPrnQLYej3VP3x5B3tE6FFHKie/SIj75jfCId2+22cHE0Rd+t4sTi5aOcHHAY6pfIsagP8JnImPSXzayjzQhoYG/H8wNHeKK3khoxc51J/mm+geedJ0wi1l57++UB32R19PKrTyK8qWml 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: The msg subsystem is a common target for exploiting[1][2][3][4][5][6][7] use-after-free type confusion flaws in the kernel for both read and write primitives. Avoid having a user-controlled dynamically-size allocation share the global kmalloc cache by using a separate set of kmalloc buckets via the kmem_buckets API. 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] Signed-off-by: Kees Cook --- ipc/msgutil.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ipc/msgutil.c b/ipc/msgutil.c index d0a0e877cadd..c7be0c792647 100644 --- a/ipc/msgutil.c +++ b/ipc/msgutil.c @@ -42,6 +42,17 @@ struct msg_msgseg { #define DATALEN_MSG ((size_t)PAGE_SIZE-sizeof(struct msg_msg)) #define DATALEN_SEG ((size_t)PAGE_SIZE-sizeof(struct msg_msgseg)) +static kmem_buckets *msg_buckets __ro_after_init; + +static int __init init_msg_buckets(void) +{ + msg_buckets = kmem_buckets_create("msg_msg", SLAB_ACCOUNT, + sizeof(struct msg_msg), + DATALEN_MSG, NULL); + + return 0; +} +subsys_initcall(init_msg_buckets); static struct msg_msg *alloc_msg(size_t len) { @@ -50,7 +61,7 @@ static struct msg_msg *alloc_msg(size_t len) size_t alen; alen = min(len, DATALEN_MSG); - msg = kmalloc(sizeof(*msg) + alen, GFP_KERNEL_ACCOUNT); + msg = kmem_buckets_alloc(msg_buckets, sizeof(*msg) + alen, GFP_KERNEL); if (msg == NULL) return NULL; From patchwork Mon Jul 1 19:13: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: 13718564 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 17F2BC2BD09 for ; Mon, 1 Jul 2024 19:13:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 201606B00A1; Mon, 1 Jul 2024 15:13:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 099256B00A4; Mon, 1 Jul 2024 15:13:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B9C2C6B00A1; Mon, 1 Jul 2024 15:13:10 -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 935486B00A0 for ; Mon, 1 Jul 2024 15:13:10 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4B2FF141D27 for ; Mon, 1 Jul 2024 19:13:10 +0000 (UTC) X-FDA: 82292131740.13.985488D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf07.hostedemail.com (Postfix) with ESMTP id 7577E40017 for ; Mon, 1 Jul 2024 19:13:08 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=bFl59nSl; spf=pass (imf07.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719861178; 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=UCPqAS1wQfpkxBSq+RkvtyhAWYw0GfCz0eapB+gICZU=; b=mGpxwuhRJeg+h+rIBCSEh45aaklsv+X+g1vW9VSxH5lh0zMZ6BVBb1X935Pn5/v2fzqLa5 2teHfr21KOX5fHqBTyfZCFiiqXgnxoLg8d4Xh6GHFeYJPLZLOZ+/FRVuu1FGpZIND//e9h SEcKrI8Yo+RppqkOg5zls7exCeQ4KRQ= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=bFl59nSl; spf=pass (imf07.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719861178; a=rsa-sha256; cv=none; b=npWmHilwHaiUS1aCUQ54e+n04UAS9m7hNmeblCcoHdaSmocQqCMC+V6q65QcTYHwMax43V btaArD83X7PweeklYLRPg85KztDzwEmKKrm1oQkI8pArNbr3Eq8n1FVUfxPE4fIexNtxt+ PnRx4541uLabcQkHfc7+CoacIGWjKgQ= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 940206178F; Mon, 1 Jul 2024 19:13:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1EA58C4AF0D; Mon, 1 Jul 2024 19:13:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719861185; bh=BZ80Ejo56zseoiz3weNc0AtXfj9+HOXw5S5R0du1E70=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bFl59nSlnVaSRU4+0HTFQ/YeTaJahbBTt5u256YqqA/IZQs7WUOzBvYKYRUR7Jmb+ wChXJYbVxmTLzNRLpAAdNpOUmpNsdgtdPYaJohsJXrbZU6BCCf7ByLGnDH/BQ216L3 560Mu5RiCeTyUZ/1umQVt9bDpooP1+0DEUxtHU2rZe8fjdKsqR/lSu/h5TyYmL36gl B+EZzQJtEw/HTSqmuDqIJgLCnr4S7c9/dCzAmRxEeHSGjY5ypxgKwo/Qu6ELpXbvlh V1yE6QbOqM4goWOm1jibPASL3Mm/dft5RrFbUr9E7X9HNaKn/bgWcHd4IL6jWnK2Nr qPcQDrAj7jZcw== From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , "GONG, Ruiqi" , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , jvoisin , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Xiu Jianfeng , Suren Baghdasaryan , Kent Overstreet , Jann Horn , Matteo Rizzo , Thomas Graf , Herbert Xu , "Gustavo A. R. Silva" , Jonathan Corbet , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v6 6/6] mm/util: Use dedicated slab buckets for memdup_user() Date: Mon, 1 Jul 2024 12:13:03 -0700 Message-Id: <20240701191304.1283894-6-kees@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240701190152.it.631-kees@kernel.org> References: <20240701190152.it.631-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2796; i=kees@kernel.org; h=from:subject; bh=BZ80Ejo56zseoiz3weNc0AtXfj9+HOXw5S5R0du1E70=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmgv++lHHVc7IaWVXSWcs6s/+88cmgFNyMQ+/tk NfYGR7ScZKJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZoL/vgAKCRCJcvTf3G3A JqpJD/9rs0Nsc8oYdGcPhljjR3Oz9ava6B9ExnvhV2nq0WD+FJVM5EdnZQAwoNyWeBpcuY2PjdX lGeJh61ZCUx/6o593wAWAgLwf2e/Mhg8nSPBCedp2wVFCHCVtzZhrNBAaQlXjO3gqzsR3DB03kj bchxInUC1Jscc86bOfbjVaIQ5IEy5V7YzHU6nQndBY2UIg+uZZmOkFzlZJpnMapIejOc6SpanQA hV6NVDpX2xdgr7MF4aOqxOwTrZz75rZo/5GXBqomoCrJB/fD4y99gSAraNxMJGvzAIKI8sJVGKr nsL6Q/dH6LCFuZlplSg3SIYt9Gos+RIxGQkllf9g7DUjeUG95w+fsoV1avbkZXjgQnI2mfAyjMF LgFIqrfiAFybNMFQqTOyMz5qVVeFFFi/KS+LDgZLc8yQlhZP519x+tJ6F4oVUtqM4dW5u4yP6Vp DG21pqkK6pZGhWARTM64DaxCBk/pN0rgw4Kuu/yLd29xhS7+fvJkksPam8vLip8OXe83i4Euu0m JTpZvCyJaLkwRmeqjgLa8fTykUMXm3m1HS9xyfPOrqmY9zOiJg6hGNR8aS7wdw+z9wjeph7E6pc z2OqisDHlJ0Pad2aMEfBofKXW2BKrBAxRPKjOnkC7biQAMODdjv2mPwU4BWxkHlSZwtHdrCs+GE Lj0ThhUc9z01RFQ= = X-Developer-Key: i=kees@kernel.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 7577E40017 X-Stat-Signature: 47n149sqjejqze6sfhrip9zbe1ra895s X-HE-Tag: 1719861188-782470 X-HE-Meta: U2FsdGVkX1/jcZo6qsK/t1ZxdPLR46u0sCWtZ4gww5Alg55tjYirPvneycSWqzUGBHCDUQ+9f8vbXWax+xUYaDUcC8S1fddCSta8ztPQP5sd7bXCOUGqPmlIFt5V8bUWdE7M0hO6CtRkP2SdonSoknIE5d+SVtWXCi+Qux5noyGrZYhn6Ytg38JjWmVeHMbsf8AwzL9UpYXhcF4pNng9efWa4VcVH/mMSti6499wBvw+GizOiKqPcWLWLVkk5imh9gBjX6WTF9Hf8ZLvComzwnnnY59sKf3IAdqmhgaTBSGbfDuYg3vs6nwmwte77ra3QSFwo3o/3KKgiA/Fo+5ezydhplo8a14lT7IIH9N8+/G3wxquTmfNkXUZUXpKzkRTsVyHhhmFIVRU05UglVEa7CkunnDtCH88urQplH3SUPHBnEqY0yjRrk1VxuqqcvMWOp3fQumuzQKEDs94VZSfyU7rimsbqVV8VB4FsKRgN6sccPviMtmWElEltz20u1pghbsJgC91JTAff98P8KTmxdz9rVimcO6q1SXQgt8hrEYqG24I+ECTvNovdVe7na4wm8Sqz5nIDKaAHuAV5bfnYWaAWN4B94x2VY4ekHR735pr9PqFNDyJVbqz3MYZk2xDo7q45YRaor+fLdGcvURoZhI0l9d3XoAeD59o/JlVjOsZEs6PykZ5QyyGNPVFwJBCnOnucWAhjg0O/E3I6UFl6NZhotg+8qjXi9i8OKgLCXc/uZxyCWqZWzrFoPdpWE49wGw20d8AHUDZOBpNjVUc61aNJ/NAEWYeCReyIQLCLq1yfSkqdCsIxb1Jq2d5KbfhqFRYO4GrgUpYIlTZdFr9pzJ673p4Hrw96R9jj3NpRYPR+pCeQO+lqzoQAVuIRZKjbJ4c3663M71w3BuFOUwKFvqH14VMvtr0JzGZj5gk1FYGB5fslO57o50Xz00vRbYacfbekmpXXqH2y55QOEZ 0s9tlYUf qZy1y68aNR7fzVLxJy4vXtFyZGNG/mGR685XZ+U7CIH1/EtT8XT9us5WLT7FM+VQ4o9XKBSfIsm81JMuiHl6QUxJ7JcahxaywQDh9b2i9mB33CVmkI+TkHoyGvXBFLQUwza9GQCiFC/klclyoLw/tKE1DHrtlZKFgSQxv+LNlMDhgaYHRZZklErqwjQcpsVYv7M/FhxEOQtSstWa4w1aV/8EjqG9f/q47v9NWCZnEMQO2XUM5jIVraUyHBGW7XHBbNZBv1NtqnMe04eAjFc075SSekaBrSi9AnSYf575SNIwICyD3VBoNgNaJ88pJfLPMQimXkYf9BMCRLPsUnGnvhYBwW99W5SZtilnPRPSzOU/U4xKcqdNCeeG4F6FV5n4KjxCUwZDGL1gTWZRlMcxCEhovKHgIPF9n2boEb/kvvEtI8pYinxkhGb4JTautQc8El28Is3ws+gbFHs+0OnqAHQfAo2Q7ePiUmnhKhgadr4IND4jNRfp9HRgbQi7OyYceLtYj7usHBbmXqX5Pf9/l6sTgJFx5Q3MkOHc2D6LKOvtS5AGlDavMNPTcQxvcUUkvyoQ05NO5toOkKypGLzNfJ/SXAvCOzLYGSuyz 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 --- mm/util.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mm/util.c b/mm/util.c index 28c5356b9f1c..29189f48ee04 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, 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);