Message ID | 20220923202822.2667581-2-keescook@chromium.org (mailing list archive) |
---|---|
State | Not Applicable |
Headers |
Received: from vger.kernel.org ([23.128.96.18]) by www.linuxtv.org with esmtp (Exim 4.92) (envelope-from <linux-media-owner@vger.kernel.org>) id 1obpM1-008Wia-VK; Fri, 23 Sep 2022 20:32:58 +0000 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233104AbiIWUct (ORCPT <rfc822;mkrufky@linuxtv.org> + 1 other); Fri, 23 Sep 2022 16:32:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232163AbiIWUc1 (ORCPT <rfc822;linux-media@vger.kernel.org>); Fri, 23 Sep 2022 16:32:27 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 55B11146602 for <linux-media@vger.kernel.org>; Fri, 23 Sep 2022 13:28:26 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id x1so1175603plv.5 for <linux-media@vger.kernel.org>; Fri, 23 Sep 2022 13:28:26 -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; bh=YGvtsu71vtHcP1IwH8KHiY99VHKmYF+/gM9Vu/q9kHU=; b=IFb3o6XSbaR/uQunXfgk/OAgUcbyiUG/neLZN7u63Mb8Dqi7z6PDoN9BNOVaTqa26y SRlzwgeKQKd7R1FctE6Aq8Of9Rx/sgZevcaBDxM7zwYZdr/ifKEynY9SIWqMo8+dRdxg Y1YODqFn9CHpITGWx8VNcWimDUOWaa7761v9Y= 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; bh=YGvtsu71vtHcP1IwH8KHiY99VHKmYF+/gM9Vu/q9kHU=; b=vfSf7t+oJjHs8fuBoiKJCWlClWvxoBaLegejZeAzKQvTS3qX3aY7Ay95fdu+NXO0uX 8ytfMzwanvYrDwgit4vmzHlwoc1ITTeqtKlh4uzqJDwmRsgYHHh6qMwqLppUj442CErs Gv4xHKKVOQ6W1mfqRCAGWtEGZbeTWWyvJPWPRcbXrjhytpvyHOwqlaxKHZ8EcmnVSLjf dkW4zaKPpqRZELCYJZ7FjsquIVZXo/z9CAkOm7KTWs7Xmf1otNR3B5YhU3cDJ1AUkJsB Bw/pB1f8LHg2CSa7+OeAz6m4CG2UKBKqejg4nQPhApvSCbHJOUQJ9x9pQwyDh6zs3heO tEug== X-Gm-Message-State: ACrzQf1Fihp3QPiED6nHtZ7dzjY3dwNtijStg1oNEscOsgsI1h+7yTGC +QtNkH20nuEStpCrlY93HWYESg== X-Google-Smtp-Source: AMsMyM4GlI4eTJyA9jGLi9lKSnvEwSBc6wqIcYDsgYSV7Hnp4hgp4C2mRxXo8b79UHawMzdaoq59kQ== X-Received: by 2002:a17:90a:e7c5:b0:200:ab1a:f32 with SMTP id kb5-20020a17090ae7c500b00200ab1a0f32mr22617314pjb.100.1663964905845; Fri, 23 Sep 2022 13:28:25 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id q9-20020a17090311c900b0017829f986a5sm6477104plh.133.2022.09.23.13.28.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Sep 2022 13:28:25 -0700 (PDT) From: Kees Cook <keescook@chromium.org> To: Vlastimil Babka <vbabka@suse.cz> Cc: Kees Cook <keescook@chromium.org>, Christoph Lameter <cl@linux.com>, Pekka Enberg <penberg@kernel.org>, David Rientjes <rientjes@google.com>, Joonsoo Kim <iamjoonsoo.kim@lge.com>, Andrew Morton <akpm@linux-foundation.org>, Roman Gushchin <roman.gushchin@linux.dev>, Hyeonggon Yoo <42.hyeyoo@gmail.com>, Marco Elver <elver@google.com>, linux-mm@kvack.org, "Ruhl, Michael J" <michael.j.ruhl@intel.com>, "David S. Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@google.com>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Nick Desaulniers <ndesaulniers@google.com>, Alex Elder <elder@kernel.org>, Josef Bacik <josef@toxicpanda.com>, David Sterba <dsterba@suse.com>, Sumit Semwal <sumit.semwal@linaro.org>, =?utf-8?q?Christian_K=C3=B6nig?= <christian.koenig@amd.com>, Jesse Brandeburg <jesse.brandeburg@intel.com>, Daniel Micay <danielmicay@gmail.com>, Yonghong Song <yhs@fb.com>, Miguel Ojeda <ojeda@kernel.org>, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-fsdevel@vger.kernel.org, intel-wired-lan@lists.osuosl.org, dev@openvswitch.org, x86@kernel.org, llvm@lists.linux.dev, linux-hardening@vger.kernel.org Subject: [PATCH v2 01/16] slab: Remove __malloc attribute from realloc functions Date: Fri, 23 Sep 2022 13:28:07 -0700 Message-Id: <20220923202822.2667581-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220923202822.2667581-1-keescook@chromium.org> References: <20220923202822.2667581-1-keescook@chromium.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4158; h=from:subject; bh=MMNKX5wNJYq+FFEmxyLeG88Y0pnaYb/m5JRZI7mnarg=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBjLhbj8zMWKpgfJIfLMPzhNCTicodYe34AHw5xM3H6 j5Q5e5iJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCYy4W4wAKCRCJcvTf3G3AJkqXEA C0CZsQPuFBR2/YBQC6kBOKXs3AnUbDd1vBqtB/SiXvO/znjATFPdq/AAaSBC59HBiMKMePM5lvoiuf aYrT/n/J7UMzadTeGJDsCFyc95mxXFpunUjeXGaTgZojjSgAVg4DABiW54DaumW0lAWFAYegD26yeW JBuJYikNr/QTKW2lM4rlUyCE6YeklCEU+t6NysjAG5alyIx4W0dgH5pUSaAsO6hX65jz8XnK6X4gIg /ftbvkJQdCA8yp6HbKj3bRnJNwHhei3Ew8kR8tRkxr/QCstK0qco60yvO2mmfdDhJOTt8ljlhX+99P ki8iAJ2IUxqLrohWV0+vBNh/UwJAT/E2xl9J7vSz0a5lE1JDaJni0l7B9aWJ6wdTRIlb2RIw/25CU3 2jH0hGLqeXUGTIW8ZuajWL/FWv6nUhF3N+Zql34CZfjXaiVB/8urV6hGJYu0PjR4RhPJtooFtvcGnc NN4P/rvByw1L7/4EGJFqF8KQolNWMu558pdQ29AecI/zn4E9FW41pu2k0zXuekf9ZHdzPr//MRq0VK jji3tNn5ZbZXQWzbxXbbkECT6CK1qfq6Re4GlmJrjPbAnkWCC4gXdw7JW6JIg/Km6CtttHx519iZge eYs/03ai/ytBwKoKeQmOX9IHVAAm1pTNUYNBd0RtV+OAb1aVL8B7TsN5Okig== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.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=unavailable 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: <linux-media.vger.kernel.org> X-Mailing-List: linux-media@vger.kernel.org X-LSpam-Score: -2.5 (--) X-LSpam-Report: No, score=-2.5 required=5.0 tests=BAYES_00=-1.9,DKIMWL_WL_HIGH=0.001,DKIM_SIGNED=0.1,DKIM_VALID=-0.1,DKIM_VALID_AU=-0.1,HEADER_FROM_DIFFERENT_DOMAINS=0.5,MAILING_LIST_MULTI=-1 autolearn=ham autolearn_force=no |
Series |
slab: Introduce kmalloc_size_roundup()
|
|
Commit Message
Kees Cook
Sept. 23, 2022, 8:28 p.m. UTC
The __malloc attribute should not be applied to "realloc" functions, as
the returned pointer may alias the storage of the prior pointer. Instead
of splitting __malloc from __alloc_size, which would be a huge amount of
churn, just create __realloc_size for the few cases where it is needed.
Additionally removes the conditional test for __alloc_size__, which is
always defined now.
Cc: Christoph Lameter <cl@linux.com>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: David Rientjes <rientjes@google.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: Marco Elver <elver@google.com>
Cc: linux-mm@kvack.org
Signed-off-by: Kees Cook <keescook@chromium.org>
---
include/linux/compiler_types.h | 13 +++++--------
include/linux/slab.h | 12 ++++++------
mm/slab_common.c | 4 ++--
3 files changed, 13 insertions(+), 16 deletions(-)
Comments
Hi Kees, On Fri, Sep 23, 2022 at 10:35 PM Kees Cook <keescook@chromium.org> wrote: > The __malloc attribute should not be applied to "realloc" functions, as > the returned pointer may alias the storage of the prior pointer. Instead > of splitting __malloc from __alloc_size, which would be a huge amount of > churn, just create __realloc_size for the few cases where it is needed. > > Additionally removes the conditional test for __alloc_size__, which is > always defined now. > > Cc: Christoph Lameter <cl@linux.com> > Cc: Pekka Enberg <penberg@kernel.org> > Cc: David Rientjes <rientjes@google.com> > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> > Cc: Andrew Morton <akpm@linux-foundation.org> > Cc: Vlastimil Babka <vbabka@suse.cz> > Cc: Roman Gushchin <roman.gushchin@linux.dev> > Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> > Cc: Marco Elver <elver@google.com> > Cc: linux-mm@kvack.org > Signed-off-by: Kees Cook <keescook@chromium.org> Thanks for your patch, which is now commit 63caa04ec60583b1 ("slab: Remove __malloc attribute from realloc functions") in next-20220927. Noreply@ellerman.id.au reported all gcc8-based builds to fail (e.g. [1], more at [2]): In file included from <command-line>: ./include/linux/percpu.h: In function ‘__alloc_reserved_percpu’: ././include/linux/compiler_types.h:279:30: error: expected declaration specifiers before ‘__alloc_size__’ #define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc ^~~~~~~~~~~~~~ ./include/linux/percpu.h:120:74: note: in expansion of macro ‘__alloc_size’ [...] It's building fine with e.g. gcc-9 (which is my usual m68k cross-compiler). Reverting this commit on next-20220927 fixes the issue. [1] http://kisskb.ellerman.id.au/kisskb/buildresult/14803908/ [2] http://kisskb.ellerman.id.au/kisskb/head/1bd8b75fe6adeaa89d02968bdd811ffe708cf839/ > --- > include/linux/compiler_types.h | 13 +++++-------- > include/linux/slab.h | 12 ++++++------ > mm/slab_common.c | 4 ++-- > 3 files changed, 13 insertions(+), 16 deletions(-) > > diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h > index 4f2a819fd60a..f141a6f6b9f6 100644 > --- a/include/linux/compiler_types.h > +++ b/include/linux/compiler_types.h > @@ -271,15 +271,12 @@ struct ftrace_likely_data { > > /* > * Any place that could be marked with the "alloc_size" attribute is also > - * a place to be marked with the "malloc" attribute. Do this as part of the > - * __alloc_size macro to avoid redundant attributes and to avoid missing a > - * __malloc marking. > + * a place to be marked with the "malloc" attribute, except those that may > + * be performing a _reallocation_, as that may alias the existing pointer. > + * For these, use __realloc_size(). > */ > -#ifdef __alloc_size__ > -# define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc > -#else > -# define __alloc_size(x, ...) __malloc > -#endif > +#define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc > +#define __realloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) > > #ifndef asm_volatile_goto > #define asm_volatile_goto(x...) asm goto(x) > diff --git a/include/linux/slab.h b/include/linux/slab.h > index 0fefdf528e0d..41bd036e7551 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -184,7 +184,7 @@ int kmem_cache_shrink(struct kmem_cache *s); > /* > * Common kmalloc functions provided by all allocators > */ > -void * __must_check krealloc(const void *objp, size_t new_size, gfp_t flags) __alloc_size(2); > +void * __must_check krealloc(const void *objp, size_t new_size, gfp_t flags) __realloc_size(2); > void kfree(const void *objp); > void kfree_sensitive(const void *objp); > size_t __ksize(const void *objp); > @@ -647,10 +647,10 @@ static inline __alloc_size(1, 2) void *kmalloc_array(size_t n, size_t size, gfp_ > * @new_size: new size of a single member of the array > * @flags: the type of memory to allocate (see kmalloc) > */ > -static inline __alloc_size(2, 3) void * __must_check krealloc_array(void *p, > - size_t new_n, > - size_t new_size, > - gfp_t flags) > +static inline __realloc_size(2, 3) void * __must_check krealloc_array(void *p, > + size_t new_n, > + size_t new_size, > + gfp_t flags) > { > size_t bytes; > > @@ -774,7 +774,7 @@ static inline __alloc_size(1, 2) void *kvcalloc(size_t n, size_t size, gfp_t fla > } > > extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags) > - __alloc_size(3); > + __realloc_size(3); > extern void kvfree(const void *addr); > extern void kvfree_sensitive(const void *addr, size_t len); > > diff --git a/mm/slab_common.c b/mm/slab_common.c > index 17996649cfe3..457671ace7eb 100644 > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -1134,8 +1134,8 @@ module_init(slab_proc_init); > > #endif /* CONFIG_SLAB || CONFIG_SLUB_DEBUG */ > > -static __always_inline void *__do_krealloc(const void *p, size_t new_size, > - gfp_t flags) > +static __always_inline __realloc_size(2) void * > +__do_krealloc(const void *p, size_t new_size, gfp_t flags) > { > void *ret; > size_t ks; > -- > 2.34.1 > -- Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
On 9/28/22 09:26, Geert Uytterhoeven wrote: > Hi Kees, > > On Fri, Sep 23, 2022 at 10:35 PM Kees Cook <keescook@chromium.org> wrote: >> The __malloc attribute should not be applied to "realloc" functions, as >> the returned pointer may alias the storage of the prior pointer. Instead >> of splitting __malloc from __alloc_size, which would be a huge amount of >> churn, just create __realloc_size for the few cases where it is needed. >> >> Additionally removes the conditional test for __alloc_size__, which is >> always defined now. >> >> Cc: Christoph Lameter <cl@linux.com> >> Cc: Pekka Enberg <penberg@kernel.org> >> Cc: David Rientjes <rientjes@google.com> >> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> >> Cc: Andrew Morton <akpm@linux-foundation.org> >> Cc: Vlastimil Babka <vbabka@suse.cz> >> Cc: Roman Gushchin <roman.gushchin@linux.dev> >> Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> >> Cc: Marco Elver <elver@google.com> >> Cc: linux-mm@kvack.org >> Signed-off-by: Kees Cook <keescook@chromium.org> > > Thanks for your patch, which is now commit 63caa04ec60583b1 ("slab: > Remove __malloc attribute from realloc functions") in next-20220927. > > Noreply@ellerman.id.au reported all gcc8-based builds to fail > (e.g. [1], more at [2]): > > In file included from <command-line>: > ./include/linux/percpu.h: In function ‘__alloc_reserved_percpu’: > ././include/linux/compiler_types.h:279:30: error: expected > declaration specifiers before ‘__alloc_size__’ > #define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc > ^~~~~~~~~~~~~~ > ./include/linux/percpu.h:120:74: note: in expansion of macro ‘__alloc_size’ > [...] > > It's building fine with e.g. gcc-9 (which is my usual m68k cross-compiler). > Reverting this commit on next-20220927 fixes the issue. So IIUC it was wrong to remove the #ifdefs? > [1] http://kisskb.ellerman.id.au/kisskb/buildresult/14803908/ > [2] http://kisskb.ellerman.id.au/kisskb/head/1bd8b75fe6adeaa89d02968bdd811ffe708cf839/ > > > >> --- >> include/linux/compiler_types.h | 13 +++++-------- >> include/linux/slab.h | 12 ++++++------ >> mm/slab_common.c | 4 ++-- >> 3 files changed, 13 insertions(+), 16 deletions(-) >> >> diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h >> index 4f2a819fd60a..f141a6f6b9f6 100644 >> --- a/include/linux/compiler_types.h >> +++ b/include/linux/compiler_types.h >> @@ -271,15 +271,12 @@ struct ftrace_likely_data { >> >> /* >> * Any place that could be marked with the "alloc_size" attribute is also >> - * a place to be marked with the "malloc" attribute. Do this as part of the >> - * __alloc_size macro to avoid redundant attributes and to avoid missing a >> - * __malloc marking. >> + * a place to be marked with the "malloc" attribute, except those that may >> + * be performing a _reallocation_, as that may alias the existing pointer. >> + * For these, use __realloc_size(). >> */ >> -#ifdef __alloc_size__ >> -# define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc >> -#else >> -# define __alloc_size(x, ...) __malloc >> -#endif >> +#define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc >> +#define __realloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) >> >> #ifndef asm_volatile_goto >> #define asm_volatile_goto(x...) asm goto(x) >> diff --git a/include/linux/slab.h b/include/linux/slab.h >> index 0fefdf528e0d..41bd036e7551 100644 >> --- a/include/linux/slab.h >> +++ b/include/linux/slab.h >> @@ -184,7 +184,7 @@ int kmem_cache_shrink(struct kmem_cache *s); >> /* >> * Common kmalloc functions provided by all allocators >> */ >> -void * __must_check krealloc(const void *objp, size_t new_size, gfp_t flags) __alloc_size(2); >> +void * __must_check krealloc(const void *objp, size_t new_size, gfp_t flags) __realloc_size(2); >> void kfree(const void *objp); >> void kfree_sensitive(const void *objp); >> size_t __ksize(const void *objp); >> @@ -647,10 +647,10 @@ static inline __alloc_size(1, 2) void *kmalloc_array(size_t n, size_t size, gfp_ >> * @new_size: new size of a single member of the array >> * @flags: the type of memory to allocate (see kmalloc) >> */ >> -static inline __alloc_size(2, 3) void * __must_check krealloc_array(void *p, >> - size_t new_n, >> - size_t new_size, >> - gfp_t flags) >> +static inline __realloc_size(2, 3) void * __must_check krealloc_array(void *p, >> + size_t new_n, >> + size_t new_size, >> + gfp_t flags) >> { >> size_t bytes; >> >> @@ -774,7 +774,7 @@ static inline __alloc_size(1, 2) void *kvcalloc(size_t n, size_t size, gfp_t fla >> } >> >> extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags) >> - __alloc_size(3); >> + __realloc_size(3); >> extern void kvfree(const void *addr); >> extern void kvfree_sensitive(const void *addr, size_t len); >> >> diff --git a/mm/slab_common.c b/mm/slab_common.c >> index 17996649cfe3..457671ace7eb 100644 >> --- a/mm/slab_common.c >> +++ b/mm/slab_common.c >> @@ -1134,8 +1134,8 @@ module_init(slab_proc_init); >> >> #endif /* CONFIG_SLAB || CONFIG_SLUB_DEBUG */ >> >> -static __always_inline void *__do_krealloc(const void *p, size_t new_size, >> - gfp_t flags) >> +static __always_inline __realloc_size(2) void * >> +__do_krealloc(const void *p, size_t new_size, gfp_t flags) >> { >> void *ret; >> size_t ks; >> -- >> 2.34.1 >> > > > -- > Gr{oetje,eeting}s, > > Geert > > -- > Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org > > In personal conversations with technical people, I call myself a hacker. But > when I'm talking to journalists I just say "programmer" or something like that. > -- Linus Torvalds
On Wed, Sep 28, 2022 at 09:26:15AM +0200, Geert Uytterhoeven wrote: > Hi Kees, > > On Fri, Sep 23, 2022 at 10:35 PM Kees Cook <keescook@chromium.org> wrote: > > The __malloc attribute should not be applied to "realloc" functions, as > > the returned pointer may alias the storage of the prior pointer. Instead > > of splitting __malloc from __alloc_size, which would be a huge amount of > > churn, just create __realloc_size for the few cases where it is needed. > > > > Additionally removes the conditional test for __alloc_size__, which is > > always defined now. > > > > Cc: Christoph Lameter <cl@linux.com> > > Cc: Pekka Enberg <penberg@kernel.org> > > Cc: David Rientjes <rientjes@google.com> > > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> > > Cc: Andrew Morton <akpm@linux-foundation.org> > > Cc: Vlastimil Babka <vbabka@suse.cz> > > Cc: Roman Gushchin <roman.gushchin@linux.dev> > > Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> > > Cc: Marco Elver <elver@google.com> > > Cc: linux-mm@kvack.org > > Signed-off-by: Kees Cook <keescook@chromium.org> > > Thanks for your patch, which is now commit 63caa04ec60583b1 ("slab: > Remove __malloc attribute from realloc functions") in next-20220927. > > Noreply@ellerman.id.au reported all gcc8-based builds to fail > (e.g. [1], more at [2]): > > In file included from <command-line>: > ./include/linux/percpu.h: In function ‘__alloc_reserved_percpu’: > ././include/linux/compiler_types.h:279:30: error: expected > declaration specifiers before ‘__alloc_size__’ > #define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc > ^~~~~~~~~~~~~~ > ./include/linux/percpu.h:120:74: note: in expansion of macro ‘__alloc_size’ > [...] > > It's building fine with e.g. gcc-9 (which is my usual m68k cross-compiler). > Reverting this commit on next-20220927 fixes the issue. > > [1] http://kisskb.ellerman.id.au/kisskb/buildresult/14803908/ > [2] http://kisskb.ellerman.id.au/kisskb/head/1bd8b75fe6adeaa89d02968bdd811ffe708cf839/ Eek! Thanks for letting me know. I'm confused about this -- __alloc_size__ wasn't optional in compiler_attributes.h -- but obviously I broke something! I'll go figure this out. -Kees
On 9/28/22 19:13, Kees Cook wrote: > On Wed, Sep 28, 2022 at 09:26:15AM +0200, Geert Uytterhoeven wrote: >> Hi Kees, >> >> On Fri, Sep 23, 2022 at 10:35 PM Kees Cook <keescook@chromium.org> wrote: >>> The __malloc attribute should not be applied to "realloc" functions, as >>> the returned pointer may alias the storage of the prior pointer. Instead >>> of splitting __malloc from __alloc_size, which would be a huge amount of >>> churn, just create __realloc_size for the few cases where it is needed. >>> >>> Additionally removes the conditional test for __alloc_size__, which is >>> always defined now. >>> >>> Cc: Christoph Lameter <cl@linux.com> >>> Cc: Pekka Enberg <penberg@kernel.org> >>> Cc: David Rientjes <rientjes@google.com> >>> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> >>> Cc: Andrew Morton <akpm@linux-foundation.org> >>> Cc: Vlastimil Babka <vbabka@suse.cz> >>> Cc: Roman Gushchin <roman.gushchin@linux.dev> >>> Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> >>> Cc: Marco Elver <elver@google.com> >>> Cc: linux-mm@kvack.org >>> Signed-off-by: Kees Cook <keescook@chromium.org> >> >> Thanks for your patch, which is now commit 63caa04ec60583b1 ("slab: >> Remove __malloc attribute from realloc functions") in next-20220927. >> >> Noreply@ellerman.id.au reported all gcc8-based builds to fail >> (e.g. [1], more at [2]): >> >> In file included from <command-line>: >> ./include/linux/percpu.h: In function ‘__alloc_reserved_percpu’: >> ././include/linux/compiler_types.h:279:30: error: expected >> declaration specifiers before ‘__alloc_size__’ >> #define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc >> ^~~~~~~~~~~~~~ >> ./include/linux/percpu.h:120:74: note: in expansion of macro ‘__alloc_size’ >> [...] >> >> It's building fine with e.g. gcc-9 (which is my usual m68k cross-compiler). >> Reverting this commit on next-20220927 fixes the issue. >> >> [1] http://kisskb.ellerman.id.au/kisskb/buildresult/14803908/ >> [2] http://kisskb.ellerman.id.au/kisskb/head/1bd8b75fe6adeaa89d02968bdd811ffe708cf839/ > > Eek! Thanks for letting me know. I'm confused about this -- > __alloc_size__ wasn't optional in compiler_attributes.h -- but obviously > I broke something! I'll go figure this out. Even in latest next I can see at the end of include/linux/compiler-gcc.h /* * Prior to 9.1, -Wno-alloc-size-larger-than (and therefore the "alloc_size" * attribute) do not work, and must be disabled. */ #if GCC_VERSION < 90100 #undef __alloc_size__ #endif > -Kees >
Kees Cook <keescook@chromium.org> writes: > On Wed, Sep 28, 2022 at 09:26:15AM +0200, Geert Uytterhoeven wrote: >> On Fri, Sep 23, 2022 at 10:35 PM Kees Cook <keescook@chromium.org> wrote: >> > The __malloc attribute should not be applied to "realloc" functions, as >> > the returned pointer may alias the storage of the prior pointer. Instead >> > of splitting __malloc from __alloc_size, which would be a huge amount of >> > churn, just create __realloc_size for the few cases where it is needed. >> > >> > Additionally removes the conditional test for __alloc_size__, which is >> > always defined now. >> > >> > Cc: Christoph Lameter <cl@linux.com> >> > Cc: Pekka Enberg <penberg@kernel.org> >> > Cc: David Rientjes <rientjes@google.com> >> > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> >> > Cc: Andrew Morton <akpm@linux-foundation.org> >> > Cc: Vlastimil Babka <vbabka@suse.cz> >> > Cc: Roman Gushchin <roman.gushchin@linux.dev> >> > Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> >> > Cc: Marco Elver <elver@google.com> >> > Cc: linux-mm@kvack.org >> > Signed-off-by: Kees Cook <keescook@chromium.org> >> >> Thanks for your patch, which is now commit 63caa04ec60583b1 ("slab: >> Remove __malloc attribute from realloc functions") in next-20220927. >> >> Noreply@ellerman.id.au reported all gcc8-based builds to fail >> (e.g. [1], more at [2]): >> >> In file included from <command-line>: >> ./include/linux/percpu.h: In function ‘__alloc_reserved_percpu’: >> ././include/linux/compiler_types.h:279:30: error: expected >> declaration specifiers before ‘__alloc_size__’ >> #define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc >> ^~~~~~~~~~~~~~ >> ./include/linux/percpu.h:120:74: note: in expansion of macro ‘__alloc_size’ >> [...] >> >> It's building fine with e.g. gcc-9 (which is my usual m68k cross-compiler). >> Reverting this commit on next-20220927 fixes the issue. >> >> [1] http://kisskb.ellerman.id.au/kisskb/buildresult/14803908/ >> [2] http://kisskb.ellerman.id.au/kisskb/head/1bd8b75fe6adeaa89d02968bdd811ffe708cf839/ > > Eek! Thanks for letting me know. I'm confused about this -- > __alloc_size__ wasn't optional in compiler_attributes.h -- but obviously > I broke something! I'll go figure this out. This fixes it for me. cheers diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h index f141a6f6b9f6..0717534f8364 100644 --- a/include/linux/compiler_types.h +++ b/include/linux/compiler_types.h @@ -275,8 +275,13 @@ struct ftrace_likely_data { * be performing a _reallocation_, as that may alias the existing pointer. * For these, use __realloc_size(). */ -#define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc -#define __realloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) +#ifdef __alloc_size__ +# define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc +# define __realloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) +#else +# define __alloc_size(x, ...) __malloc +# define __realloc_size(x, ...) +#endif #ifndef asm_volatile_goto #define asm_volatile_goto(x...) asm goto(x)
Hi Michael, On Thu, Sep 29, 2022 at 10:36 AM Michael Ellerman <mpe@ellerman.id.au> wrote: > Kees Cook <keescook@chromium.org> writes: > > On Wed, Sep 28, 2022 at 09:26:15AM +0200, Geert Uytterhoeven wrote: > >> On Fri, Sep 23, 2022 at 10:35 PM Kees Cook <keescook@chromium.org> wrote: > >> > The __malloc attribute should not be applied to "realloc" functions, as > >> > the returned pointer may alias the storage of the prior pointer. Instead > >> > of splitting __malloc from __alloc_size, which would be a huge amount of > >> > churn, just create __realloc_size for the few cases where it is needed. > >> > > >> > Additionally removes the conditional test for __alloc_size__, which is > >> > always defined now. > >> > > >> > Cc: Christoph Lameter <cl@linux.com> > >> > Cc: Pekka Enberg <penberg@kernel.org> > >> > Cc: David Rientjes <rientjes@google.com> > >> > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> > >> > Cc: Andrew Morton <akpm@linux-foundation.org> > >> > Cc: Vlastimil Babka <vbabka@suse.cz> > >> > Cc: Roman Gushchin <roman.gushchin@linux.dev> > >> > Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> > >> > Cc: Marco Elver <elver@google.com> > >> > Cc: linux-mm@kvack.org > >> > Signed-off-by: Kees Cook <keescook@chromium.org> > >> > >> Thanks for your patch, which is now commit 63caa04ec60583b1 ("slab: > >> Remove __malloc attribute from realloc functions") in next-20220927. > >> > >> Noreply@ellerman.id.au reported all gcc8-based builds to fail > >> (e.g. [1], more at [2]): > >> > >> In file included from <command-line>: > >> ./include/linux/percpu.h: In function ‘__alloc_reserved_percpu’: > >> ././include/linux/compiler_types.h:279:30: error: expected > >> declaration specifiers before ‘__alloc_size__’ > >> #define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc > >> ^~~~~~~~~~~~~~ > >> ./include/linux/percpu.h:120:74: note: in expansion of macro ‘__alloc_size’ > >> [...] > >> > >> It's building fine with e.g. gcc-9 (which is my usual m68k cross-compiler). > >> Reverting this commit on next-20220927 fixes the issue. > >> > >> [1] http://kisskb.ellerman.id.au/kisskb/buildresult/14803908/ > >> [2] http://kisskb.ellerman.id.au/kisskb/head/1bd8b75fe6adeaa89d02968bdd811ffe708cf839/ > > > > Eek! Thanks for letting me know. I'm confused about this -- > > __alloc_size__ wasn't optional in compiler_attributes.h -- but obviously > > I broke something! I'll go figure this out. > > This fixes it for me. Kees submitted a similar patch 20 minutes before: https://lore.kernel.org/all/20220929081642.1932200-1-keescook@chromium.org > --- a/include/linux/compiler_types.h > +++ b/include/linux/compiler_types.h > @@ -275,8 +275,13 @@ struct ftrace_likely_data { > * be performing a _reallocation_, as that may alias the existing pointer. > * For these, use __realloc_size(). > */ > -#define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc > -#define __realloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) > +#ifdef __alloc_size__ > +# define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc > +# define __realloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) > +#else > +# define __alloc_size(x, ...) __malloc > +# define __realloc_size(x, ...) > +#endif > > #ifndef asm_volatile_goto > #define asm_volatile_goto(x...) asm goto(x) Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
On Fri, Sep 23, 2022 at 01:28:07PM -0700, Kees Cook wrote: > The __malloc attribute should not be applied to "realloc" functions, as > the returned pointer may alias the storage of the prior pointer. Instead > of splitting __malloc from __alloc_size, which would be a huge amount of > churn, just create __realloc_size for the few cases where it is needed. > > Additionally removes the conditional test for __alloc_size__, which is > always defined now. > > Cc: Christoph Lameter <cl@linux.com> > Cc: Pekka Enberg <penberg@kernel.org> > Cc: David Rientjes <rientjes@google.com> > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> > Cc: Andrew Morton <akpm@linux-foundation.org> > Cc: Vlastimil Babka <vbabka@suse.cz> > Cc: Roman Gushchin <roman.gushchin@linux.dev> > Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> > Cc: Marco Elver <elver@google.com> > Cc: linux-mm@kvack.org > Signed-off-by: Kees Cook <keescook@chromium.org> > --- > include/linux/compiler_types.h | 13 +++++-------- > include/linux/slab.h | 12 ++++++------ > mm/slab_common.c | 4 ++-- > 3 files changed, 13 insertions(+), 16 deletions(-) > > diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h > index 4f2a819fd60a..f141a6f6b9f6 100644 > --- a/include/linux/compiler_types.h > +++ b/include/linux/compiler_types.h > @@ -271,15 +271,12 @@ struct ftrace_likely_data { > > /* > * Any place that could be marked with the "alloc_size" attribute is also > - * a place to be marked with the "malloc" attribute. Do this as part of the > - * __alloc_size macro to avoid redundant attributes and to avoid missing a > - * __malloc marking. > + * a place to be marked with the "malloc" attribute, except those that may > + * be performing a _reallocation_, as that may alias the existing pointer. > + * For these, use __realloc_size(). > */ > -#ifdef __alloc_size__ > -# define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc > -#else > -# define __alloc_size(x, ...) __malloc > -#endif > +#define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc > +#define __realloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) > > #ifndef asm_volatile_goto > #define asm_volatile_goto(x...) asm goto(x) > diff --git a/include/linux/slab.h b/include/linux/slab.h > index 0fefdf528e0d..41bd036e7551 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -184,7 +184,7 @@ int kmem_cache_shrink(struct kmem_cache *s); > /* > * Common kmalloc functions provided by all allocators > */ > -void * __must_check krealloc(const void *objp, size_t new_size, gfp_t flags) __alloc_size(2); > +void * __must_check krealloc(const void *objp, size_t new_size, gfp_t flags) __realloc_size(2); > void kfree(const void *objp); > void kfree_sensitive(const void *objp); > size_t __ksize(const void *objp); > @@ -647,10 +647,10 @@ static inline __alloc_size(1, 2) void *kmalloc_array(size_t n, size_t size, gfp_ > * @new_size: new size of a single member of the array > * @flags: the type of memory to allocate (see kmalloc) > */ > -static inline __alloc_size(2, 3) void * __must_check krealloc_array(void *p, > - size_t new_n, > - size_t new_size, > - gfp_t flags) > +static inline __realloc_size(2, 3) void * __must_check krealloc_array(void *p, > + size_t new_n, > + size_t new_size, > + gfp_t flags) > { > size_t bytes; > > @@ -774,7 +774,7 @@ static inline __alloc_size(1, 2) void *kvcalloc(size_t n, size_t size, gfp_t fla > } > > extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags) > - __alloc_size(3); > + __realloc_size(3); > extern void kvfree(const void *addr); > extern void kvfree_sensitive(const void *addr, size_t len); > > diff --git a/mm/slab_common.c b/mm/slab_common.c > index 17996649cfe3..457671ace7eb 100644 > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -1134,8 +1134,8 @@ module_init(slab_proc_init); > > #endif /* CONFIG_SLAB || CONFIG_SLUB_DEBUG */ > > -static __always_inline void *__do_krealloc(const void *p, size_t new_size, > - gfp_t flags) > +static __always_inline __realloc_size(2) void * > +__do_krealloc(const void *p, size_t new_size, gfp_t flags) > { > void *ret; > size_t ks; > -- > 2.34.1 > This is now squashed with later one. (so undefined __alloc_size__ issues are fixed) for the latest version of this patch: Looks good to me, Acked-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h index 4f2a819fd60a..f141a6f6b9f6 100644 --- a/include/linux/compiler_types.h +++ b/include/linux/compiler_types.h @@ -271,15 +271,12 @@ struct ftrace_likely_data { /* * Any place that could be marked with the "alloc_size" attribute is also - * a place to be marked with the "malloc" attribute. Do this as part of the - * __alloc_size macro to avoid redundant attributes and to avoid missing a - * __malloc marking. + * a place to be marked with the "malloc" attribute, except those that may + * be performing a _reallocation_, as that may alias the existing pointer. + * For these, use __realloc_size(). */ -#ifdef __alloc_size__ -# define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc -#else -# define __alloc_size(x, ...) __malloc -#endif +#define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc +#define __realloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) #ifndef asm_volatile_goto #define asm_volatile_goto(x...) asm goto(x) diff --git a/include/linux/slab.h b/include/linux/slab.h index 0fefdf528e0d..41bd036e7551 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -184,7 +184,7 @@ int kmem_cache_shrink(struct kmem_cache *s); /* * Common kmalloc functions provided by all allocators */ -void * __must_check krealloc(const void *objp, size_t new_size, gfp_t flags) __alloc_size(2); +void * __must_check krealloc(const void *objp, size_t new_size, gfp_t flags) __realloc_size(2); void kfree(const void *objp); void kfree_sensitive(const void *objp); size_t __ksize(const void *objp); @@ -647,10 +647,10 @@ static inline __alloc_size(1, 2) void *kmalloc_array(size_t n, size_t size, gfp_ * @new_size: new size of a single member of the array * @flags: the type of memory to allocate (see kmalloc) */ -static inline __alloc_size(2, 3) void * __must_check krealloc_array(void *p, - size_t new_n, - size_t new_size, - gfp_t flags) +static inline __realloc_size(2, 3) void * __must_check krealloc_array(void *p, + size_t new_n, + size_t new_size, + gfp_t flags) { size_t bytes; @@ -774,7 +774,7 @@ static inline __alloc_size(1, 2) void *kvcalloc(size_t n, size_t size, gfp_t fla } extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags) - __alloc_size(3); + __realloc_size(3); extern void kvfree(const void *addr); extern void kvfree_sensitive(const void *addr, size_t len); diff --git a/mm/slab_common.c b/mm/slab_common.c index 17996649cfe3..457671ace7eb 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -1134,8 +1134,8 @@ module_init(slab_proc_init); #endif /* CONFIG_SLAB || CONFIG_SLUB_DEBUG */ -static __always_inline void *__do_krealloc(const void *p, size_t new_size, - gfp_t flags) +static __always_inline __realloc_size(2) void * +__do_krealloc(const void *p, size_t new_size, gfp_t flags) { void *ret; size_t ks;