Re: [patch 9/9] mm/vmalloc: add __alloc_size attributes for better bounds checking
From: Joe Perches
Date: Tue Sep 21 2021 - 19:45:55 EST
On Tue, 2021-09-21 at 16:37 -0700, Kees Cook wrote:
> On Fri, Sep 10, 2021 at 10:23:48AM -0700, Linus Torvalds wrote:
> > On Thu, Sep 9, 2021 at 8:10 PM Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> wrote:
> > >
> > > +__alloc_size(1)
> > > extern void *vmalloc(unsigned long size);
> > [...]
> >
> > All of these are added in the wrong place - inconsistent with the very
> > compiler documentation the patches add.
> >
> > The function attributes are generally added _after_ the function,
> > although admittedly we've been quite confused here before.
> >
> > But the very compiler documentation you point to in the patch that
> > adds these macros gives that as the examples both for gcc and clang:
> >
> > + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alloc_005fsize-function-attribute
> > + * clang: https://clang.llvm.org/docs/AttributeReference.html#alloc-size
> >
> > and honestly I think that is the preferred format because this is
> > about the *function*, not about the return type.
> >
> > Do both placements work? Yes.
>
> I'm cleaning this up now, and have discovered that the reason for the
> before-function placement is consistency with static inlines. If I do this:
>
> static __always_inline void * kmalloc(size_t size, gfp_t flags) __alloc_size(1)
> {
> ...
> }
>
> GCC is very angry:
>
> ./include/linux/slab.h:519:1: error: attributes should be specified before the declarator in a function definition
> 519 | static __always_inline void *kmalloc_large(size_t size, gfp_t flags) __alloc_size(1)
> | ^~~~~~
>
> It's happy if I treat it as a "return type attribute" in the ordering,
> though:
>
> static __always_inline void * __alloc_size(1) kmalloc(size_t size, gfp_t flags)
>
> I'll do that unless you have a preference for somewhere else...
_please_ put it before the return type on a separate line.
[__attributes]
[static inline const] <return type> function(<args...>)