From 34af26c4252ca2918790a75caafecacdd51b555c Mon Sep 17 00:00:00 2001 From: Kees Cook Date: Tue, 31 Aug 2021 15:23:16 -0700 Subject: [PATCH] Compiler Attributes: Add __alloc_size for better bounds checking fix Adjust the warning logic to deal with pre-9.1 gcc behaviors. Link: https://lkml.kernel.org/r/20210827151327.2729736-1-keescook@chromium.org Signed-off-by: Kees Cook Reported-by: Randy Dunlap Tested-by: Randy Dunlap Cc: Stephen Rothwell Signed-off-by: Andrew Morton Signed-off-by: Stephen Rothwell --- Makefile | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 56151c396958..730d42641857 100644 --- a/Makefile +++ b/Makefile @@ -1099,8 +1099,17 @@ endif ifdef CONFIG_CC_IS_GCC # The allocators already balk at large sizes, so silence the compiler -# warnings for bounds checks involving those possible values. -KBUILD_CFLAGS += -Wno-alloc-size-larger-than +# warnings for bounds checks involving those possible values. While +# -Wno-alloc-size-larger-than would normally be used here, earlier versions +# of gcc (<9.1) weirdly don't handle the option correctly when _other_ +# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX +# doesn't work (as it is documented to), silently resolving to "0" prior to +# version 9.1 (and producing an error more recently). Numeric values larger +# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently +# ignored, continuing to default to PTRDIFF_MAX. So, left with no other +# choice, we must perform a versioned check to disable this warning. +# https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au +KBUILD_CFLAGS += $(call cc-ifversion, -ge, 0901, -Wno-alloc-size-larger-than) endif # disable invalid "can't wrap" optimizations for signed / pointers -- 2.50.1