From patchwork Wed Jan 22 07:48:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: GONG Ruiqi X-Patchwork-Id: 13946970 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 2CFC7C02181 for ; Wed, 22 Jan 2025 07:37:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 953426B0082; Wed, 22 Jan 2025 02:37:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9024A6B0083; Wed, 22 Jan 2025 02:37:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7F09B6B0085; Wed, 22 Jan 2025 02:37:52 -0500 (EST) 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 6444B6B0082 for ; Wed, 22 Jan 2025 02:37:52 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id EE1D6B136F for ; Wed, 22 Jan 2025 07:37:51 +0000 (UTC) X-FDA: 83034283542.12.952F156 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf05.hostedemail.com (Postfix) with ESMTP id DC462100002 for ; Wed, 22 Jan 2025 07:37:48 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf05.hostedemail.com: domain of gongruiqi1@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=gongruiqi1@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737531470; 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-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references; bh=Ol9n/Bwo/Tkz/AfpPnuL5efnHbUiUrL6tp7HtfpmhhU=; b=jKggGpkl4RZSCQXHGjiLaNSL9snjv8tRt8K36JO8hURvo7XFO+reAIcpyrY9VXRGkzlCp6 LuIhyeIZg2CsrVABRvv6RN7yccORFYLdqs1/un2uFTCqV/afPVomLmUEjMi0X5NxXMWDcP 9/d1ecp1t7ZyW6aAyTPsPyul2xQKD2U= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf05.hostedemail.com: domain of gongruiqi1@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=gongruiqi1@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737531470; a=rsa-sha256; cv=none; b=YQ4ab2xHR2PBVAegCweLveD0c8ly7xwF08kcdLqa9UPq99lurRNIyYk7obAVHz7PYLHnXx ++NWRbeM/fwrVCjeWvi5jnGzwGVzo52Wa5PUNd30yKksdS7XbbXspN4jUl/uTxRbDC1paE DMedsySkmzYV65Fvu1UgSFn8jrUjY+E= Received: from mail.maildlp.com (unknown [172.19.88.234]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4YdG9S0Z7nz1JHq6; Wed, 22 Jan 2025 15:36:44 +0800 (CST) Received: from kwepemg100016.china.huawei.com (unknown [7.202.181.57]) by mail.maildlp.com (Postfix) with ESMTPS id 583681400CF; Wed, 22 Jan 2025 15:37:44 +0800 (CST) Received: from huawei.com (10.67.174.33) by kwepemg100016.china.huawei.com (7.202.181.57) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Wed, 22 Jan 2025 15:37:43 +0800 From: GONG Ruiqi To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Kees Cook CC: Tamas Koczka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Xiu Jianfeng , , , Subject: [PATCH] mm/slab: Achieve better kmalloc caches randomization in kvmalloc Date: Wed, 22 Jan 2025 15:48:17 +0800 Message-ID: <20250122074817.991060-1-gongruiqi1@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.67.174.33] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemg100016.china.huawei.com (7.202.181.57) X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: DC462100002 X-Stat-Signature: 9fpth879kg8xkntxecfqm1zpy7pg9m51 X-Rspam-User: X-HE-Tag: 1737531468-872433 X-HE-Meta: U2FsdGVkX19WYFpJoABjExj812ieUEIdCpuz3t9f/mcWdFWQ/zjxQ56sKMzuHuzyEjNgJ0kN0ei3QXtWsqwSZQWaLZqRuVuMPrEbw7yWmF62iL8/N319yBvJdLmggrZD5JR9zJOrmWltCAzmNGFMOXje93D3dPT+lmLJT7Vo4viZUYXnB7A5qaRHRXDIEA+BZcnvGmMNwEIQ7xsWeviVpGWC9dkcboIVexrfFoUZDXDweyjyP6M+8zAeaAqH9mmEx6Lb4ZkjWw0jKSEv8FxoUoEyMkR7koGkp5BN0Wg7n5R+J/9IlNTUQKxsKpHJHFHZk7ulf1uRFI8yYh/CDbeUK/JsRca923EXMhP3UBse2+W3EIZhwUSeJyGXj1BFHkFUDD8RLANsss+pYW22fJguvWRTmcO/wv6UiEKPxbD0OyLDz84azPpBzpezh1KH337bvC6X1mwQs6+6XYBvI/DFlYcNKSz95+nq/FgR4GeCeaHR2nw6MnBBBliUOdkEdVj1z0oCRtcqECQ+zjGaV+sQxqGa2ihgqfXjBIIji3GuIjSINQqrsLY9iS4mcdjfV7bq/aVNRO9B2Qk3LIe877Nn3LeILt2Vkr5XnrSmvfvlkExDeXY6u3IzRkjIs2biUoZv/I5WK42RkH5VoYfrC1kwFgPCqG+UtC0obIfgy5wrQduIGFWkHKfYkGvmN+5I947jSAjYk1VRL7B+SAWqCLsW57nuyyEMwo/YBkfU1nsgKKuy2C016IilYXlnI5WPFyOYPBIyKgjbmcrg2iO8ZPD8YkTwQgNy749E/k4npYN1LFzb+ngnKuNkxWI6MytkUBouksvCFlIo+Z4Hsboj0eONFZ9+i78UQ3Z8SawiqSlCSPZS7+E/ioJgHmPO10X/k6BgVqkSojcm6d5JKjIdjCEXoAxmDQS3UoWl+Q5yprT3UVZUwoabg7rHU/SMPE1QvCaMnSpilDItWxnQFFHTmi/ 47Us98bL ylR0TU56Zh/10PoiL2D8owdpatM3tQfIbPZtQ2HAoDmEZ+BmgQKwVWjBxZ8Zq+oJGZZLiZWhmZo8heahTvo1UNat3wii048BJuDeIAJapFb5IjB6cakzqN2gppr6KEpGXt0nWhpr9WKvolDjeYVQzvwePTZ3rc9/ChW3VaVk9Zgfy4vpea8UZPKKyQrySWDTq0W+ytdQq8wmCm3dNdODdQCTq5QTINVuAhFVzRryUumLSBaLoZdwox+n//S/JeT6iQ9INEK55D8qR2ZA9v33Af6Np3A== 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: As revealed by this writeup[1], due to the fact that __kmalloc_node (now renamed to __kmalloc_node_noprof) is an exported symbol and will never get inlined, using it in kvmalloc_node (now is __kvmalloc_node_noprof) would make the RET_IP inside always point to the same address: upper_caller kvmalloc kvmalloc_node kvmalloc_node_noprof __kvmalloc_node_noprof <-- all macros all the way down here __kmalloc_node_noprof __do_kmalloc_node(.., _RET_IP_) ... <-- _RET_IP_ points to That literally means all kmalloc invoked via kvmalloc would use the same seed for cache randomization (CONFIG_RANDOM_KMALLOC_CACHES), which makes this hardening unfunctional. The root cause of this problem, IMHO, is that using RET_IP only cannot identify the actual allocation site in case of kmalloc being called inside wrappers or helper functions. And I believe there could be similar cases in other functions. Nevertheless, I haven't thought of any good solution for this. So for now let's solve this specific case first. For __kvmalloc_node_noprof, replace __kmalloc_node_noprof with an inline version, so that RET_IP can take the return address of kvmalloc and differentiate each kvmalloc invocation: upper_caller kvmalloc kvmalloc_node kvmalloc_node_noprof __kvmalloc_node_noprof <-- all macros all the way down here __kmalloc_node_inline(.., _RET_IP_) ... <-- _RET_IP_ points to Thanks to Tamás Koczka for the report and discussion! Links: [1] https://github.com/google/security-research/pull/83/files#diff-1604319b55a48c39a210ee52034ed7ff5b9cdc3d704d2d9e34eb230d19fae235R200 Signed-off-by: GONG Ruiqi --- include/linux/slab.h | 3 +++ mm/slub.c | 7 +++++++ mm/util.c | 4 ++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 10a971c2bde3..e03ca4a95511 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -834,6 +834,9 @@ void *__kmalloc_large_noprof(size_t size, gfp_t flags) void *__kmalloc_large_node_noprof(size_t size, gfp_t flags, int node) __assume_page_alignment __alloc_size(1); +void *__kmalloc_node_inline(size_t size, kmem_buckets *b, gfp_t flags, + int node, unsigned long caller); + /** * kmalloc - allocate kernel memory * @size: how many bytes of memory are required. diff --git a/mm/slub.c b/mm/slub.c index c2151c9fee22..ec75070345c6 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -4319,6 +4319,13 @@ void *__kmalloc_node_track_caller_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flag } EXPORT_SYMBOL(__kmalloc_node_track_caller_noprof); +__always_inline void *__kmalloc_node_inline(size_t size, kmem_buckets *b, + gfp_t flags, int node, + unsigned long caller) +{ + return __do_kmalloc_node(size, b, flags, node, caller); +} + void *__kmalloc_cache_noprof(struct kmem_cache *s, gfp_t gfpflags, size_t size) { void *ret = slab_alloc_node(s, NULL, gfpflags, NUMA_NO_NODE, diff --git a/mm/util.c b/mm/util.c index 60aa40f612b8..3910d1d1f595 100644 --- a/mm/util.c +++ b/mm/util.c @@ -642,9 +642,9 @@ void *__kvmalloc_node_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node) * It doesn't really make sense to fallback to vmalloc for sub page * requests */ - ret = __kmalloc_node_noprof(PASS_BUCKET_PARAMS(size, b), + ret = __kmalloc_node_inline(size, PASS_BUCKET_PARAM(b), kmalloc_gfp_adjust(flags, size), - node); + node, _RET_IP_); if (ret || size <= PAGE_SIZE) return ret;