Skip to contentSkip to conversation

kbuild: Switch from -Wvla to -Wvla-larger-than=0

PATCH Applied Kees Cook 3 participants 5 messages Apr 18, 2025 – Apr 21, 2025

Subsystems (5)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 kbuild: Switch from -Wvla to -Wvla-larger-than=0

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=0 option (though GCC may split -Wvla[3] similarly to how Clang is planning to).

Switch to -Wvla-larger-than=0 and adjust the two VLA-checking selftests to disable the updated option name.

---
Cc: Christoph Hellwig <[email protected]>
Cc: Masahiro Yamada <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Cc: Alexander Potapenko <[email protected]>
Cc: Andrey Konovalov <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Vincenzo Frascino <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Nick Desaulniers <[email protected]>
Cc: Bill Wendling <[email protected]>
Cc: Justin Stitt <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>

---
 lib/Makefile               | 2 +-
 mm/kasan/Makefile          | 2 +-
 scripts/Makefile.extrawarn | 9 +++++++--
 3 files changed, 9 insertions(+), 4 deletions(-)
lib/Makefile +1 -1
index f07b24ce1b3f..37b6e5782ecb 100644
@@ -71,7 +71,7 @@ CFLAGS_test_bitops.o += -Werror
 obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o
 obj-$(CONFIG_TEST_IDA) += test_ida.o
 obj-$(CONFIG_TEST_UBSAN) += test_ubsan.o
-CFLAGS_test_ubsan.o += $(call cc-disable-warning, vla)
+CFLAGS_test_ubsan.o += $(call cc-option, -Wno-vla-larger-than)
 CFLAGS_test_ubsan.o += $(call cc-disable-warning, unused-but-set-variable)
 UBSAN_SANITIZE_test_ubsan.o := y
 obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o
mm/kasan/Makefile +1 -1
index 1a958e7c8a46..0e326116a70b 100644
@@ -35,7 +35,7 @@ CFLAGS_shadow.o := $(CC_FLAGS_KASAN_RUNTIME)
 CFLAGS_hw_tags.o := $(CC_FLAGS_KASAN_RUNTIME)
 CFLAGS_sw_tags.o := $(CC_FLAGS_KASAN_RUNTIME)
 
-CFLAGS_KASAN_TEST := $(CFLAGS_KASAN) $(call cc-disable-warning, vla)
+CFLAGS_KASAN_TEST := $(CFLAGS_KASAN) $(call cc-option, -Wno-vla-larger-than)
 ifndef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
 # If compiler instruments memintrinsics by prefixing them with __asan/__hwasan,
 # we need to treat them normally (as builtins), otherwise the compiler won't
scripts/Makefile.extrawarn +7 -2
index d75897559d18..0229b10c5d81 100644
@@ -45,8 +45,13 @@ endif
 # These result in bogus false positives
 KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer)
 
-# Variable Length Arrays (VLAs) should not be used anywhere in the kernel
-KBUILD_CFLAGS += -Wvla
+# Stack Variable Length Arrays (VLAs) must not be used in the kernel.
+# Function array parameters should, however, be usable, but -Wvla will
+# warn for those. Clang has no way yet to distinguish between the VLA
+# types, so depend on GCC for now to keep stack VLAs out of the tree.
+# https://github.com/llvm/llvm-project/issues/57098
+# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98217
+KBUILD_CFLAGS += $(call cc-option,-Wvla-larger-than=0)
 
 # disable pointer signed / unsigned warnings in gcc 4.0
 KBUILD_CFLAGS += -Wno-pointer-sign
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Kees Cook
Quoted 12 lines · Kees Cook

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=0 option (though GCC may split -Wvla[3] similarly to how Clang is planning to).

Quoted 7 lines

Switch to -Wvla-larger-than=0 and adjust the two VLA-checking selftests to disable the updated option name.

Quoted 54 lines
 lib/Makefile               | 2 +-
 mm/kasan/Makefile          | 2 +-
 scripts/Makefile.extrawarn | 9 +++++++--
 3 files changed, 9 insertions(+), 4 deletions(-)
lib/Makefile +1 -1
index f07b24ce1b3f..37b6e5782ecb 100644
@@ -71,7 +71,7 @@ CFLAGS_test_bitops.o += -Werror
 obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o
 obj-$(CONFIG_TEST_IDA) += test_ida.o
 obj-$(CONFIG_TEST_UBSAN) += test_ubsan.o
-CFLAGS_test_ubsan.o += $(call cc-disable-warning, vla)
+CFLAGS_test_ubsan.o += $(call cc-option, -Wno-vla-larger-than)
 CFLAGS_test_ubsan.o += $(call cc-disable-warning, unused-but-set-variable)
 UBSAN_SANITIZE_test_ubsan.o := y
 obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o
mm/kasan/Makefile +1 -1
index 1a958e7c8a46..0e326116a70b 100644
@@ -35,7 +35,7 @@ CFLAGS_shadow.o := $(CC_FLAGS_KASAN_RUNTIME)
 CFLAGS_hw_tags.o := $(CC_FLAGS_KASAN_RUNTIME)
 CFLAGS_sw_tags.o := $(CC_FLAGS_KASAN_RUNTIME)
 
-CFLAGS_KASAN_TEST := $(CFLAGS_KASAN) $(call cc-disable-warning, vla)
+CFLAGS_KASAN_TEST := $(CFLAGS_KASAN) $(call cc-option, -Wno-vla-larger-than)
 ifndef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
 # If compiler instruments memintrinsics by prefixing them with __asan/__hwasan,
 # we need to treat them normally (as builtins), otherwise the compiler won't
scripts/Makefile.extrawarn +7 -2
index d75897559d18..0229b10c5d81 100644
@@ -45,8 +45,13 @@ endif
 # These result in bogus false positives
 KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer)
 
-# Variable Length Arrays (VLAs) should not be used anywhere in the kernel
-KBUILD_CFLAGS += -Wvla
+# Stack Variable Length Arrays (VLAs) must not be used in the kernel.
+# Function array parameters should, however, be usable, but -Wvla will
+# warn for those. Clang has no way yet to distinguish between the VLA
+# types, so depend on GCC for now to keep stack VLAs out of the tree.
+# https://github.com/llvm/llvm-project/issues/57098
+# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98217
+KBUILD_CFLAGS += $(call cc-option,-Wvla-larger-than=0)
 
 # disable pointer signed / unsigned warnings in gcc 4.0
 KBUILD_CFLAGS += -Wno-pointer-sign
Signature · 1 line
2.34.1
author
#2 lore raw
In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig

Looks good:

Note that sparse currently also can't cope with VLAs including the prototype syntax, which also needs addressing.

Hm, it looks like it's been over a year since a commit to the sparse git.

Luc, are function prototypes with VLAs likely to be supported by sparse soon?

Signature · 1 line
Kees Cook
author Applied
#4 lore raw
In reply to Kees Cook
Quoted 8 lines · Kees Cook

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]) { ...

[...]

Applied to for-next/hardening, thanks!

[1/1] kbuild: Switch from -Wvla to -Wvla-larger-than=0

      https://git.kernel.org/kees/c/9c2cfa10444c

Take care,

Signature · 1 line
Kees Cook