Skip to content

Commit 5e88c48

Browse files
committed
kbuild: Switch from -Wvla to -Wvla-larger-than=1
Variable Length Arrays (VLAs) on the stack must not be used in the kernel. Function parameter VLAs[1] should be usable, but -Wvla will warn for those. For example, this will produce a warning but it is not using a stack VLA: int something(size_t n, int array[n]) { ... Clang has no way yet to distinguish between the VLA types[2], so depend on GCC for now to keep stack VLAs out of the tree by using GCC's -Wvla-larger-than=N option (though GCC may split -Wvla similarly[3] to how Clang is planning to). While GCC 8+ supports -Wvla-larger-than, only 9+ supports ...=0[4], so use -Wvla-larger-than=1. Adjust mm/kasan/Makefile to remove it from CFLAGS (GCC <9 appears unable to disable the warning correctly[5]). The VLA usage in lib/test_ubsan.c was removed in commit 9d7ca61 ("lib/test_ubsan.c: VLA no longer used in kernel") so the lib/Makefile disabling of VLA checking can be entirely removed. Link: https://en.cppreference.com/w/c/language/array [1] Link: llvm/llvm-project#57098 [2] Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98217 [3] Link: https://lore.kernel.org/lkml/[email protected]/ [4] Link: https://lore.kernel.org/r/[email protected]/ [5] Reviewed-by: Nathan Chancellor <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Tested-by: Venkat Rao Bagalkote <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 5106c65 commit 5e88c48

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

lib/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ CFLAGS_test_bitops.o += -Werror
7171
obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o
7272
obj-$(CONFIG_TEST_IDA) += test_ida.o
7373
obj-$(CONFIG_TEST_UBSAN) += test_ubsan.o
74-
CFLAGS_test_ubsan.o += $(call cc-disable-warning, vla)
7574
CFLAGS_test_ubsan.o += $(call cc-disable-warning, unused-but-set-variable)
7675
UBSAN_SANITIZE_test_ubsan.o := y
7776
obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o

mm/kasan/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ CFLAGS_shadow.o := $(CC_FLAGS_KASAN_RUNTIME)
3535
CFLAGS_hw_tags.o := $(CC_FLAGS_KASAN_RUNTIME)
3636
CFLAGS_sw_tags.o := $(CC_FLAGS_KASAN_RUNTIME)
3737

38-
CFLAGS_KASAN_TEST := $(CFLAGS_KASAN) $(call cc-disable-warning, vla)
38+
CFLAGS_KASAN_TEST := $(CFLAGS_KASAN)
3939
ifndef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
4040
# If compiler instruments memintrinsics by prefixing them with __asan/__hwasan,
4141
# we need to treat them normally (as builtins), otherwise the compiler won't
@@ -44,6 +44,7 @@ ifndef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
4444
CFLAGS_KASAN_TEST += -fno-builtin
4545
endif
4646

47+
CFLAGS_REMOVE_kasan_test_c.o += $(call cc-option, -Wvla-larger-than=1)
4748
CFLAGS_kasan_test_c.o := $(CFLAGS_KASAN_TEST)
4849
RUSTFLAGS_kasan_test_rust.o := $(RUSTFLAGS_KASAN)
4950

scripts/Makefile.extrawarn

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ endif
4545
# These result in bogus false positives
4646
KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer)
4747

48-
# Variable Length Arrays (VLAs) should not be used anywhere in the kernel
49-
KBUILD_CFLAGS += -Wvla
48+
# Stack Variable Length Arrays (VLAs) must not be used in the kernel.
49+
# Function array parameters should, however, be usable, but -Wvla will
50+
# warn for those. Clang has no way yet to distinguish between the VLA
51+
# types, so depend on GCC for now to keep stack VLAs out of the tree.
52+
# https://github.com/llvm/llvm-project/issues/57098
53+
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98217
54+
KBUILD_CFLAGS += $(call cc-option,-Wvla-larger-than=1)
5055

5156
# disable pointer signed / unsigned warnings in gcc 4.0
5257
KBUILD_CFLAGS += -Wno-pointer-sign

0 commit comments

Comments
 (0)