From patchwork Tue Nov 1 22:33:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13027558 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 0B081C433FE for ; Tue, 1 Nov 2022 22:33:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9EBD06B0088; Tue, 1 Nov 2022 18:33:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9936A8E0001; Tue, 1 Nov 2022 18:33:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 797CF6B008C; Tue, 1 Nov 2022 18:33:27 -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 5CD946B0089 for ; Tue, 1 Nov 2022 18:33:27 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 34E46160448 for ; Tue, 1 Nov 2022 22:33:27 +0000 (UTC) X-FDA: 80086326054.19.B6D651E Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf01.hostedemail.com (Postfix) with ESMTP id 0A71140006 for ; Tue, 1 Nov 2022 22:33:24 +0000 (UTC) Received: by mail-pj1-f42.google.com with SMTP id q1-20020a17090a750100b002139ec1e999so287101pjk.1 for ; Tue, 01 Nov 2022 15:33:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=87RNyMS5uX8ym9d4i08lSZonwWmXQDb7t+1Mtox/7yY=; b=KPuYVSv0nSM3xzmtsA2iZPi4FbNUt2u+J40XlA1k1oQQpVJTS9b6QN9F44Up65E+Dy ZEZDhFfCcAcXwoR6dC2i7yVaDmDWk5ahJKFXf7F78lPa3a9PuAhXHM1Bu7aJ4wvSpABe tdFQi2tLp11aSMFI3sSj0s/g/VSKZK5PRes3I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=87RNyMS5uX8ym9d4i08lSZonwWmXQDb7t+1Mtox/7yY=; b=gQgLsYI/UfzKF6f8cq4udGZOM8w5ir9rhnWzWjcM0ZT2/WogTNiiIH/4MMhlJAwA2I EukkBOs8PpN6xMoMXwwiCDHec2ukqTvykF7kw4DOUteADLNMa95t3Io0xFJb8XpgVvWp xRUexBBV27hfcfa9f7elD+GkugDcMw314GaF2l7mBJMF9LI0/T3B3UAfBusBAYHz6aK/ XGii9Th2C5ZBqC+zYUTZwtwlYvfPtNKtnulcweG6oqY7HIrc73Ki/7aBWDRayNhuMoh7 npfBmm+bKTy6ECA8EfUFLXwHteLrC/7U2chA65XVM9b0BYotIy9r9tZ62CJpFb8yu/QM ZZ2g== X-Gm-Message-State: ACrzQf0sU2ySQJjfF8DiX21KPpoXAG9Mf6rV77qpiTJ9WvXr5sT7jzKF c1s3B0IzAnq6udsR42bPWEijaA== X-Google-Smtp-Source: AMsMyM5nrJHbUGPB6olYPQsGSIKe4o16+BOFvZoWcvoBMUjk7mAVQmawGuJrbP4nRhkPmubegbnSrQ== X-Received: by 2002:a17:903:1205:b0:178:ac4e:70b8 with SMTP id l5-20020a170903120500b00178ac4e70b8mr21525995plh.52.1667342003962; Tue, 01 Nov 2022 15:33:23 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id 11-20020a63134b000000b0043ae1797e2bsm6253514pgt.63.2022.11.01.15.33.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Nov 2022 15:33:22 -0700 (PDT) From: Kees Cook To: Vlastimil Babka Cc: Kees Cook , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, David Gow , Rasmus Villemoes , Guenter Roeck , Andy Shevchenko , Paolo Abeni , Geert Uytterhoeven , Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH 3/6] slab: Provide functional __alloc_size() hints to kmalloc_trace*() Date: Tue, 1 Nov 2022 15:33:11 -0700 Message-Id: <20221101223321.1326815-3-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221101222520.never.109-kees@kernel.org> References: <20221101222520.never.109-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3777; h=from:subject; bh=7/m3dEaCfyTXz7DjZgXoExKzu8Xq2Kk2B9qgNF6RcGY=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjYZ6qEP2c2SjseVkvdeN6bkMUPKJVGJtbp1au+Zyw YWPCnLGJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCY2GeqgAKCRCJcvTf3G3AJrNyD/ 9AyySkd0dVZRcUAxeWk0ceyiENXBqnciPCjbF1LZnBp23ob+33YYuxYUgP9m4No9Ikisn0wDrMq0rJ c3TyrMW705BCcJ+9lbBCofVn9TIGFD7JhKd++KHwtef8oUTAG58EXkQbcVgVGvq1Jv4jNH+bpQlXOl uhQqH4GyghPppdIGRLTh1XjesN55zxz/jurH9h15oDDr+zxCyO6mVUIBQSriDgMRoDv1kCZL3G+URj gG83q7qxiVIu+QrRxph18SMAuz602+/uLDZZr8TnMuK89nUA4/MFj4l+ZbgRbK7ga+R9WVW8sVB3Wb JVOqkhmQtmMSEJLIoQGtkbobICmQcGtzzqUBlQ9VZuRc69Kw2bHAXxHsveb2DqRFUKWNYmfX13BVGn sJNfVCUb1x+Rv0TBIhjBTPgjNaQ7giL7KrbW5RgEfRIKNhi355QFPy6JI+EKjjucq/lT2r3cBQJZXE +7xvDIp6xphk+A+FKr9nyUA2O7eS5Ub0fd1g52rt2noSmGeymkatG8uhCVgENOxps0Gv42yGAByoKX 1WQu19FknjYAvQTr7LZdPpNkfdUfDNRGZNlMEjknJITtCJ4ztqyuKCnD6Lkm6RvZRLxVyacyW5cfRY F4Ls0x39ONT774Syx5nmWvPH8iYaMN8Ri5QDo8evxuWgJv8khtAVCf5MbUqA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667342005; a=rsa-sha256; cv=none; b=4tiUWwWHQyYJtlfgap5jofn3l5Fts3O49ECP2GDnryno52KjneheIXfUCSaKX5XqntqlCq p59rJHQ2asQh7IRXIeH70dsoxps5AGQef91x9FqhyocUlO8S4a5UNLd8KzgY7xrQh1A7K4 pGwmKOLirlvlaSdVBc1kFwZddW03m9A= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=KPuYVSv0; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf01.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.42 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=1667342005; 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=87RNyMS5uX8ym9d4i08lSZonwWmXQDb7t+1Mtox/7yY=; b=33YP063sGiHOb87Y8Q54FQ+wDLHJbVlgMShn4kh81djFwGiNbo+39ZiUSTOmAaiEioq/ej eNHG5a3ccfr8IZkK9NyVX/8flPXEJ1exte/zdgiSpo56iRFLEuEMf2b6l0/wYjT1En/Phb FUAuoLFkzVwQckJCdw0z2yph+ZVdKnc= X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 0A71140006 X-Rspam-User: Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=KPuYVSv0; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf01.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.42 as permitted sender) smtp.mailfrom=keescook@chromium.org X-Stat-Signature: ey9px16eaamtmzs4gx83mori6ykdyrf1 X-HE-Tag: 1667342004-703129 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: Since GCC cannot apply the __alloc_size attributes to inlines[1], all allocator inlines need to explicitly call into extern functions that contain a size argument. Provide these wrappers that end up just ignoring the size argument for the actual allocation. This allows CONFIG_FORTIFY_SOURCE=y to see all various dynamic allocation sizes under GCC 12+ and all supported Clang versions. [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503 Cc: Vlastimil Babka Cc: Christoph Lameter Cc: Pekka Enberg Cc: David Rientjes Cc: Joonsoo Kim Cc: Andrew Morton Cc: Roman Gushchin Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: linux-mm@kvack.org Signed-off-by: Kees Cook --- include/linux/slab.h | 8 ++++++-- mm/slab_common.c | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 970e9504949e..051d86ca31a8 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -442,6 +442,8 @@ static_assert(PAGE_SHIFT <= 20); void *__kmalloc(size_t size, gfp_t flags) __assume_kmalloc_alignment __alloc_size(1); void *kmem_cache_alloc(struct kmem_cache *s, gfp_t flags) __assume_slab_alignment __malloc; +void *kmem_cache_alloc_sized(struct kmem_cache *s, gfp_t flags, size_t size) + __assume_slab_alignment __alloc_size(3); void *kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru, gfp_t gfpflags) __assume_slab_alignment __malloc; void kmem_cache_free(struct kmem_cache *s, void *objp); @@ -469,6 +471,8 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node) __assume_kmalloc_alignm __alloc_size(1); void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t flags, int node) __assume_slab_alignment __malloc; +void *kmem_cache_alloc_node_sized(struct kmem_cache *s, gfp_t flags, int node, size_t size) + __assume_slab_alignment __alloc_size(4); #ifdef CONFIG_TRACING void *kmalloc_trace(struct kmem_cache *s, gfp_t flags, size_t size) @@ -482,7 +486,7 @@ void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags, static __always_inline __alloc_size(3) void *kmalloc_trace(struct kmem_cache *s, gfp_t flags, size_t size) { - void *ret = kmem_cache_alloc(s, flags); + void *ret = kmem_cache_alloc_sized(s, flags, size); ret = kasan_kmalloc(s, ret, size, flags); return ret; @@ -492,7 +496,7 @@ static __always_inline __alloc_size(4) void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags, int node, size_t size) { - void *ret = kmem_cache_alloc_node(s, gfpflags, node); + void *ret = kmem_cache_alloc_node_sized(s, gfpflags, node, size); ret = kasan_kmalloc(s, ret, size, gfpflags); return ret; diff --git a/mm/slab_common.c b/mm/slab_common.c index 33b1886b06eb..5fa547539a6a 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1457,6 +1457,20 @@ size_t ksize(const void *objp) } EXPORT_SYMBOL(ksize); +/* Wrapper so __alloc_size() can see the actual allocation size. */ +void *kmem_cache_alloc_sized(struct kmem_cache *s, gfp_t flags, size_t size) +{ + return kmem_cache_alloc(s, flags); +} +EXPORT_SYMBOL(kmem_cache_alloc_sized); + +/* Wrapper so __alloc_size() can see the actual allocation size. */ +void *kmem_cache_alloc_node_sized(struct kmem_cache *s, gfp_t flags, int node, size_t size) +{ + return kmem_cache_alloc_node(s, flags, node); +} +EXPORT_SYMBOL(kmem_cache_alloc_node_sized); + /* Tracepoints definitions. */ EXPORT_TRACEPOINT_SYMBOL(kmalloc); EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);