Received: by 2002:a05:6358:16cd:b0:dc:6189:e246 with SMTP id r13csp1308286rwl; Fri, 4 Nov 2022 12:17:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6rqtsdTew/To4L9IHqNDqAeYgVxqOk0eiQ+M8F+exQ0CmEeLNUNlOm1ZPnqGLed2HWAe15 X-Received: by 2002:a17:907:1b0e:b0:72f:9b43:b98c with SMTP id mp14-20020a1709071b0e00b0072f9b43b98cmr36311229ejc.710.1667589471395; Fri, 04 Nov 2022 12:17:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667589471; cv=none; d=google.com; s=arc-20160816; b=oDvV3hDRDHbSvFcB+XrVM02Y7jMXYcZEfnkA5WiLuhdjEQYSFJPqRyFA1WKHHmpow8 wOwIYAhbxAPUc3Lp0eRhjxyi6TkbWR9ioMo2owgFrkShwl/rezdTslj76yfSwm7GFLzt +myLcqoJseOND33rQX+G1zysR2lJhLYswQx0dnedift43lFRjWevLCglwXFe5nt3quqM 9RMjnlW0c2LKVATmf1xoLzzjeJLKULMoXLc4FcvuymdosEo5Qp9WjufebbF7hJhLFMVb hSZA4q90TP3KN3Lc+3rOJNOuk08R+ol82ovUtokXwGn5R9kcWDBdjpbevaKxF2YBub6J 4hSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=5kwrddbh0AjpQHJDtv4p1kwlSWL7OpMjxgVy9tJ3ZdE=; b=dB1DQlkFW2Fc3egZPLC14m4rvkYRY/X/OSqjRtKrWrznzLmfLuQbVWYN1wjuIdzifI Hm7jscTfPkZJl67awylQus69iYTWT2PJM8Vp7vAu4mDR9+8G4vYB3tMN6GtrkznLOMxX GtKRzBywlzoyL5FHhpxvA7EZHAaF/++EXCFeTqp0yx+lEyh/uATTyWmS4zIlvZo6Kv2w /UOtBVZhZLjocGgzokTte6QkdSorFF2Hk4IWkAmKktwuayhkkn5+3Cw8bgOhfpiUydWC FMJBPF1tcxsU8crV22yWMrPsOVVTVp/glIOqVVT+KwwNlWZpelzXF+Pm+H9fQm+TuWTX WfhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=mediYFwU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m13-20020a056402430d00b0044ecd80f257si270953edc.603.2022.11.04.12.17.27; Fri, 04 Nov 2022 12:17:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=mediYFwU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232027AbiKDSXo (ORCPT + 96 others); Fri, 4 Nov 2022 14:23:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232072AbiKDSXP (ORCPT ); Fri, 4 Nov 2022 14:23:15 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25DD959FE6 for ; Fri, 4 Nov 2022 11:22:44 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id y203so5214821pfb.4 for ; Fri, 04 Nov 2022 11:22:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=5kwrddbh0AjpQHJDtv4p1kwlSWL7OpMjxgVy9tJ3ZdE=; b=mediYFwUYG/2yUbilqGlldO/gRsGC6/mgEYuIhDe1Si39QjNc/trPYKiP1p9XhU8FS YC6GtGaQ7BTmWdx71ehDpLJFh9gH4TAS6wjDRpintwM7thIXMmpHNBa+YpPp7Sk910xG LjmbkmsEmGX5jCFZeb0bEhpFMYS1j5fKV131E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=5kwrddbh0AjpQHJDtv4p1kwlSWL7OpMjxgVy9tJ3ZdE=; b=K+YocEblOfP2b2BBQsmzLHE+PHtP79sArp1KU8U2g2InTB3LT/ACiCdywgq4USI4Vx FHN+rreRO1A9ndNa76sanlTFwdfjOyzBwAIE93u4PyMze3s+5XZD4AIfIX+46mtlMs8w ehDv2nvpjkP9hwJU8K+W+8FsSJTztIDsm1ISDruXGHrF/oEGKFhnOqprq2yfr7OTGYHM rU2JWf2K/MhD7cDw0EmcExDDZ8CcFwN5ie4CbhW5wWZL9RYlROFiWAtzbtyRiLi0wYGR SYUsJUFH/ilZANHJDUpvdzxpmEFdbOAWhB/f3tHqrEBbIp3jhnkh9gHZWVMXeF6EmuPQ i5Sg== X-Gm-Message-State: ACrzQf1Lk8oMCkgJSU9f3yl8jGSxoOVx1YJpj4OFiBkOL7cnvh3vUpzG s23cUtI3br9LLFDPxbA1nMJ/nA== X-Received: by 2002:aa7:81cc:0:b0:563:4e53:c08b with SMTP id c12-20020aa781cc000000b005634e53c08bmr36706082pfn.19.1667586163647; Fri, 04 Nov 2022 11:22:43 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id z7-20020aa79e47000000b0053e38ac0ff4sm3013789pfq.115.2022.11.04.11.22.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Nov 2022 11:22:43 -0700 (PDT) Date: Fri, 4 Nov 2022 11:22:42 -0700 From: Kees Cook To: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Vlastimil Babka , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Roman Gushchin , 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: Re: [PATCH 3/6] slab: Provide functional __alloc_size() hints to kmalloc_trace*() Message-ID: <202211041121.2F1639D2@keescook> References: <20221101222520.never.109-kees@kernel.org> <20221101223321.1326815-3-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 03, 2022 at 11:16:53PM +0900, Hyeonggon Yoo wrote: > On Tue, Nov 01, 2022 at 03:33:11PM -0700, Kees Cook wrote: > > 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); > > The reason that we have two implementations of kmalloc_trace* > depending on CONFIG_TRACING is to save additional function call when > CONFIG_TRACING is not set. > > With this patch there is no reason to keep both. > So let's drop #ifdefs and use single implementation in mm/slab_common.c. Okay, I'll respin... -- Kees Cook