Re: [PATCH v2 6/7] percpu: Add __alloc_size attributes for better bounds checking

Dennis Zhou
2021-08-19T14:12:48Z

On Wed, Aug 18, 2021 at 08:36:50PM -0700, Kees Cook wrote:
> On Wed, Aug 18, 2021 at 08:42:59PM -0400, Dennis Zhou wrote:
> > On Wed, Aug 18, 2021 at 02:40:20PM -0700, Kees Cook wrote:
> > > As already done in GrapheneOS, add the __alloc_size attribute for
> > > appropriate percpu allocator interfaces, to provide additional hinting
> > > for better bounds checking, assisting CONFIG_FORTIFY_SOURCE and other
> > > compiler optimizations.
> >
> > Can you elaborate a little bit for me how this works for percpu? In any
> > case that's not uniprocessor, any modification is done through address
> > accessors and not on the returned percpu pointer. Is the metadata kept
> > by gcc/clang able to transpire the percpu pointer accessors?
>
> That's an excellent point. :P I haven't tested it through the accessors,
> but I guess it's possible that this is only useful for UP, and even
> then, only where the access is very close to the "allocation", maybe
> like:
>

I see that this is already pulled by Andrew, but I think it would be
good to modify the commit log to add a short bit about this limitation.
Otherwise, the commit reads as if it's doing way more than it is.

Thanks,
Dennis

> char __percpu *test_buf;
>
> char *buf;
> test_var = __alloc_percpu(16, __alignof__(char));
> buf = per_cpu_ptr(test_buf, get_cpu());
> ...
> buf[20] = '!';
>
> -Kees
>
> >
> > Thanks,
> > Dennis
> >
> > >
> > > Co-developed-by: Daniel Micay <danielmicay@gmail.com>
> > > Signed-off-by: Daniel Micay <danielmicay@gmail.com>
> > > Cc: Dennis Zhou <dennis@kernel.org>
> > > Cc: Tejun Heo <tj@kernel.org>
> > > Cc: Christoph Lameter <cl@linux.com>
> > > Cc: linux-mm@kvack.org
> > > Signed-off-by: Kees Cook <keescook@chromium.org>
> > > ---
> > > include/linux/percpu.h | 3 +++
> > > 1 file changed, 3 insertions(+)
> > >
> > > diff --git a/include/linux/percpu.h b/include/linux/percpu.h
> > > index 5e76af742c80..119f41815b32 100644
> > > --- a/include/linux/percpu.h
> > > +++ b/include/linux/percpu.h
> > > @@ -123,6 +123,7 @@ extern int __init pcpu_page_first_chunk(size_t reserved_size,
> > > pcpu_fc_populate_pte_fn_t populate_pte_fn);
> > > #endif
> > >
> > > +__alloc_size(1)
> > > extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
> > > extern bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr);
> > > extern bool is_kernel_percpu_address(unsigned long addr);
> > > @@ -131,7 +132,9 @@ extern bool is_kernel_percpu_address(unsigned long addr);
> > > extern void __init setup_per_cpu_areas(void);
> > > #endif
> > >
> > > +__alloc_size(1)
> > > extern void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp);
> > > +__alloc_size(1)
> > > extern void __percpu *__alloc_percpu(size_t size, size_t align);
> > > extern void free_percpu(void __percpu *__pdata);
> > > extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
> > > --
> > > 2.30.2
> > >
>
> --
> Kees Cook