mbox series

[0/6] slab: Provide full coverage for __alloc_size attribute

Message ID 20221101222520.never.109-kees@kernel.org (mailing list archive)
Headers show
Series slab: Provide full coverage for __alloc_size attribute | expand

Message

Kees Cook Nov. 1, 2022, 10:33 p.m. UTC
Hi,

This is a series to work around a deficiency in GCC (>=11) and Clang
(<16) where the __alloc_size attribute does not apply to inlines. :(
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503

This manifests as reduced overflow detection coverage for many allocation
sites under CONFIG_FORTIFY_SOURCE=y, where the allocation size was
not actually being propagated to __builtin_dynamic_object_size(). In
addition to working around the issue, expand use of __alloc_size (and
__realloc_size) to more places and provide KUnit tests to validate all
the covered allocator APIs.

-Kees

Kees Cook (6):
  slab: Clean up SLOB vs kmalloc() definition
  slab: Remove special-casing of const 0 size allocations
  slab: Provide functional __alloc_size() hints to kmalloc_trace*()
  string: Add __realloc_size hint to kmemdup()
  driver core: Add __alloc_size hint to devm allocators
  kunit/fortify: Validate __alloc_size attribute results

 include/linux/device.h         |   7 +-
 include/linux/fortify-string.h |   2 +-
 include/linux/slab.h           |  36 ++---
 include/linux/string.h         |   2 +-
 lib/Makefile                   |   1 +
 lib/fortify_kunit.c            | 255 +++++++++++++++++++++++++++++++++
 mm/slab_common.c               |  14 ++
 7 files changed, 296 insertions(+), 21 deletions(-)

Comments

Conor Dooley Nov. 29, 2022, 12:24 p.m. UTC | #1
On Tue, Nov 01, 2022 at 03:33:08PM -0700, Kees Cook wrote:
> Hi,
> 
> This is a series to work around a deficiency in GCC (>=11) and Clang
> (<16) where the __alloc_size attribute does not apply to inlines. :(
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503
> 
> This manifests as reduced overflow detection coverage for many allocation
> sites under CONFIG_FORTIFY_SOURCE=y, where the allocation size was
> not actually being propagated to __builtin_dynamic_object_size(). In
> addition to working around the issue, expand use of __alloc_size (and
> __realloc_size) to more places and provide KUnit tests to validate all
> the covered allocator APIs.

Hello Kees!

It would appear that one of the macros you've added here is doing Bad
Things^TM to allmodconfig on RISC-V since the 22nd:

../lib/fortify_kunit.c: In function 'alloc_size_kmalloc_const_test':
../lib/fortify_kunit.c:140:1: error: the frame size of 2384 bytes is larger than 2048 bytes [-Werror=frame-larger-than=]
  140 | }                                                                       \
      | ^
../lib/fortify_kunit.c:209:1: note: in expansion of macro 'DEFINE_ALLOC_SIZE_TEST_PAIR'
  209 | DEFINE_ALLOC_SIZE_TEST_PAIR(kmalloc)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

CONFIG_GCC_VERSION=110100
CONFIG_AS_VERSION=23700
CONFIG_LD_VERSION=23700

The report came out of my CI (which I should have passed on sooner) so
I do not have anything other than stderr - I can get you anything else
you'd like/need though if you LMK.

Thanks,
Conor.

> Kees Cook (6):
>   slab: Clean up SLOB vs kmalloc() definition
>   slab: Remove special-casing of const 0 size allocations
>   slab: Provide functional __alloc_size() hints to kmalloc_trace*()
>   string: Add __realloc_size hint to kmemdup()
>   driver core: Add __alloc_size hint to devm allocators
>   kunit/fortify: Validate __alloc_size attribute results
> 
>  include/linux/device.h         |   7 +-
>  include/linux/fortify-string.h |   2 +-
>  include/linux/slab.h           |  36 ++---
>  include/linux/string.h         |   2 +-
>  lib/Makefile                   |   1 +
>  lib/fortify_kunit.c            | 255 +++++++++++++++++++++++++++++++++
>  mm/slab_common.c               |  14 ++
>  7 files changed, 296 insertions(+), 21 deletions(-)
> 
> -- 
> 2.34.1
> 
>
Arnd Bergmann Nov. 29, 2022, 12:33 p.m. UTC | #2
On Tue, Nov 29, 2022, at 13:24, Conor Dooley wrote:
> On Tue, Nov 01, 2022 at 03:33:08PM -0700, Kees Cook wrote:
>> Hi,
>> 
>> This is a series to work around a deficiency in GCC (>=11) and Clang
>> (<16) where the __alloc_size attribute does not apply to inlines. :(
>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503
>> 
>> This manifests as reduced overflow detection coverage for many allocation
>> sites under CONFIG_FORTIFY_SOURCE=y, where the allocation size was
>> not actually being propagated to __builtin_dynamic_object_size(). In
>> addition to working around the issue, expand use of __alloc_size (and
>> __realloc_size) to more places and provide KUnit tests to validate all
>> the covered allocator APIs.
>
> Hello Kees!
>
> It would appear that one of the macros you've added here is doing Bad
> Things^TM to allmodconfig on RISC-V since the 22nd:
>
> ../lib/fortify_kunit.c: In function 'alloc_size_kmalloc_const_test':
> ../lib/fortify_kunit.c:140:1: error: the frame size of 2384 bytes is 
> larger than 2048 bytes [-Werror=frame-larger-than=]
>   140 | }                                                               
>         \
>       | ^
> ../lib/fortify_kunit.c:209:1: note: in expansion of macro 
> 'DEFINE_ALLOC_SIZE_TEST_PAIR'
>   209 | DEFINE_ALLOC_SIZE_TEST_PAIR(kmalloc)
>       | ^~~~~~~~~~~~~~~~~~~~~~~~~~~
> cc1: all warnings being treated as errors
>
> CONFIG_GCC_VERSION=110100
> CONFIG_AS_VERSION=23700
> CONFIG_LD_VERSION=23700
>
> The report came out of my CI (which I should have passed on sooner) so
> I do not have anything other than stderr - I can get you anything else
> you'd like/need though if you LMK.

There is generally a conflict between kunit and the structleak
gcc plugin, I think the Makefile needs a line like

CFLAGS_fortify_kunit.o += $(DISABLE_STRUCTLEAK_PLUGIN)

      Arnd
Kees Cook Dec. 1, 2022, 5:15 p.m. UTC | #3
On Tue, Nov 29, 2022 at 01:33:03PM +0100, Arnd Bergmann wrote:
> On Tue, Nov 29, 2022, at 13:24, Conor Dooley wrote:
> > On Tue, Nov 01, 2022 at 03:33:08PM -0700, Kees Cook wrote:
> >> Hi,
> >> 
> >> This is a series to work around a deficiency in GCC (>=11) and Clang
> >> (<16) where the __alloc_size attribute does not apply to inlines. :(
> >> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503
> >> 
> >> This manifests as reduced overflow detection coverage for many allocation
> >> sites under CONFIG_FORTIFY_SOURCE=y, where the allocation size was
> >> not actually being propagated to __builtin_dynamic_object_size(). In
> >> addition to working around the issue, expand use of __alloc_size (and
> >> __realloc_size) to more places and provide KUnit tests to validate all
> >> the covered allocator APIs.
> >
> > Hello Kees!
> >
> > It would appear that one of the macros you've added here is doing Bad
> > Things^TM to allmodconfig on RISC-V since the 22nd:
> >
> > ../lib/fortify_kunit.c: In function 'alloc_size_kmalloc_const_test':
> > ../lib/fortify_kunit.c:140:1: error: the frame size of 2384 bytes is 
> > larger than 2048 bytes [-Werror=frame-larger-than=]
> >   140 | }                                                               
> >         \
> >       | ^
> > ../lib/fortify_kunit.c:209:1: note: in expansion of macro 
> > 'DEFINE_ALLOC_SIZE_TEST_PAIR'
> >   209 | DEFINE_ALLOC_SIZE_TEST_PAIR(kmalloc)
> >       | ^~~~~~~~~~~~~~~~~~~~~~~~~~~
> > cc1: all warnings being treated as errors
> >
> > CONFIG_GCC_VERSION=110100
> > CONFIG_AS_VERSION=23700
> > CONFIG_LD_VERSION=23700
> >
> > The report came out of my CI (which I should have passed on sooner) so
> > I do not have anything other than stderr - I can get you anything else
> > you'd like/need though if you LMK.
> 
> There is generally a conflict between kunit and the structleak
> gcc plugin, I think the Makefile needs a line like
> 
> CFLAGS_fortify_kunit.o += $(DISABLE_STRUCTLEAK_PLUGIN)

Thanks for the report! I've taken Anders's patch for this now.