linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kbuild: Switch from -Wvla to -Wvla-larger-than=0
@ 2025-04-18 21:32 Kees Cook
  2025-04-21  9:12 ` Christoph Hellwig
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Kees Cook @ 2025-04-18 21:32 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Kees Cook, Masahiro Yamada, Andrew Morton, Andrey Ryabinin,
	Alexander Potapenko, Andrey Konovalov, Dmitry Vyukov,
	Vincenzo Frascino, Nathan Chancellor, Nicolas Schier,
	Nick Desaulniers, Bill Wendling, Justin Stitt, kasan-dev,
	linux-mm, linux-kbuild, llvm, linux-kernel, linux-hardening

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.

Link: https://en.cppreference.com/w/c/language/array [1]
Link: https://github.com/llvm/llvm-project/issues/57098 [2]
Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98217 [3]
Signed-off-by: Kees Cook <kees@kernel.org>
---
Cc: Christoph Hellwig <hch@lst.de>
Cc: Masahiro Yamada <masahiroy@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nicolas Schier <nicolas.schier@linux.dev>
Cc: Nick Desaulniers <nick.desaulniers+lkml@gmail.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Justin Stitt <justinstitt@google.com>
Cc: <kasan-dev@googlegroups.com>
Cc: <linux-mm@kvack.org>
Cc: <linux-kbuild@vger.kernel.org>
Cc: <llvm@lists.linux.dev>
---
 lib/Makefile               | 2 +-
 mm/kasan/Makefile          | 2 +-
 scripts/Makefile.extrawarn | 9 +++++++--
 3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/lib/Makefile b/lib/Makefile
index f07b24ce1b3f..37b6e5782ecb 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -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
diff --git a/mm/kasan/Makefile b/mm/kasan/Makefile
index 1a958e7c8a46..0e326116a70b 100644
--- a/mm/kasan/Makefile
+++ b/mm/kasan/Makefile
@@ -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
diff --git a/scripts/Makefile.extrawarn b/scripts/Makefile.extrawarn
index d75897559d18..0229b10c5d81 100644
--- a/scripts/Makefile.extrawarn
+++ b/scripts/Makefile.extrawarn
@@ -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
-- 
2.34.1



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] kbuild: Switch from -Wvla to -Wvla-larger-than=0
  2025-04-18 21:32 [PATCH] kbuild: Switch from -Wvla to -Wvla-larger-than=0 Kees Cook
@ 2025-04-21  9:12 ` Christoph Hellwig
  2025-04-21 16:43   ` Kees Cook
  2025-04-21 16:17 ` Nathan Chancellor
  2025-04-21 21:30 ` Kees Cook
  2 siblings, 1 reply; 5+ messages in thread
From: Christoph Hellwig @ 2025-04-21  9:12 UTC (permalink / raw)
  To: Kees Cook
  Cc: Christoph Hellwig, Masahiro Yamada, Andrew Morton,
	Andrey Ryabinin, Alexander Potapenko, Andrey Konovalov,
	Dmitry Vyukov, Vincenzo Frascino, Nathan Chancellor,
	Nicolas Schier, Nick Desaulniers, Bill Wendling, Justin Stitt,
	kasan-dev, linux-mm, linux-kbuild, llvm, linux-kernel,
	linux-hardening, linux-sparse, luc.vanoostenryck

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

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



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] kbuild: Switch from -Wvla to -Wvla-larger-than=0
  2025-04-18 21:32 [PATCH] kbuild: Switch from -Wvla to -Wvla-larger-than=0 Kees Cook
  2025-04-21  9:12 ` Christoph Hellwig
@ 2025-04-21 16:17 ` Nathan Chancellor
  2025-04-21 21:30 ` Kees Cook
  2 siblings, 0 replies; 5+ messages in thread
From: Nathan Chancellor @ 2025-04-21 16:17 UTC (permalink / raw)
  To: Kees Cook
  Cc: Christoph Hellwig, Masahiro Yamada, Andrew Morton,
	Andrey Ryabinin, Alexander Potapenko, Andrey Konovalov,
	Dmitry Vyukov, Vincenzo Frascino, Nicolas Schier,
	Nick Desaulniers, Bill Wendling, Justin Stitt, kasan-dev,
	linux-mm, linux-kbuild, llvm, linux-kernel, linux-hardening

On Fri, Apr 18, 2025 at 02:32:39PM -0700, Kees Cook wrote:
> 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.
> 
> Link: https://en.cppreference.com/w/c/language/array [1]
> Link: https://github.com/llvm/llvm-project/issues/57098 [2]
> Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98217 [3]
> Signed-off-by: Kees Cook <kees@kernel.org>

Reviewed-by: Nathan Chancellor <nathan@kernel.org>

>  lib/Makefile               | 2 +-
>  mm/kasan/Makefile          | 2 +-
>  scripts/Makefile.extrawarn | 9 +++++++--
>  3 files changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/lib/Makefile b/lib/Makefile
> index f07b24ce1b3f..37b6e5782ecb 100644
> --- a/lib/Makefile
> +++ b/lib/Makefile
> @@ -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
> diff --git a/mm/kasan/Makefile b/mm/kasan/Makefile
> index 1a958e7c8a46..0e326116a70b 100644
> --- a/mm/kasan/Makefile
> +++ b/mm/kasan/Makefile
> @@ -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
> diff --git a/scripts/Makefile.extrawarn b/scripts/Makefile.extrawarn
> index d75897559d18..0229b10c5d81 100644
> --- a/scripts/Makefile.extrawarn
> +++ b/scripts/Makefile.extrawarn
> @@ -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
> -- 
> 2.34.1
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] kbuild: Switch from -Wvla to -Wvla-larger-than=0
  2025-04-21  9:12 ` Christoph Hellwig
@ 2025-04-21 16:43   ` Kees Cook
  0 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2025-04-21 16:43 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Masahiro Yamada, Andrew Morton, Andrey Ryabinin,
	Alexander Potapenko, Andrey Konovalov, Dmitry Vyukov,
	Vincenzo Frascino, Nathan Chancellor, Nicolas Schier,
	Nick Desaulniers, Bill Wendling, Justin Stitt, kasan-dev,
	linux-mm, linux-kbuild, llvm, linux-kernel, linux-hardening,
	linux-sparse, luc.vanoostenryck

On Mon, Apr 21, 2025 at 11:12:33AM +0200, Christoph Hellwig wrote:
> Looks good:
> 
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> 
> 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?

-- 
Kees Cook


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] kbuild: Switch from -Wvla to -Wvla-larger-than=0
  2025-04-18 21:32 [PATCH] kbuild: Switch from -Wvla to -Wvla-larger-than=0 Kees Cook
  2025-04-21  9:12 ` Christoph Hellwig
  2025-04-21 16:17 ` Nathan Chancellor
@ 2025-04-21 21:30 ` Kees Cook
  2 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2025-04-21 21:30 UTC (permalink / raw)
  To: Christoph Hellwig, Kees Cook
  Cc: Masahiro Yamada, Andrew Morton, Andrey Ryabinin,
	Alexander Potapenko, Andrey Konovalov, Dmitry Vyukov,
	Vincenzo Frascino, Nathan Chancellor, Nicolas Schier,
	Nick Desaulniers, Bill Wendling, Justin Stitt, kasan-dev,
	linux-mm, linux-kbuild, llvm, linux-kernel, linux-hardening

On Fri, 18 Apr 2025 14:32:39 -0700, Kees Cook wrote:
> 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,

-- 
Kees Cook



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-04-21 21:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-18 21:32 [PATCH] kbuild: Switch from -Wvla to -Wvla-larger-than=0 Kees Cook
2025-04-21  9:12 ` Christoph Hellwig
2025-04-21 16:43   ` Kees Cook
2025-04-21 16:17 ` Nathan Chancellor
2025-04-21 21:30 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox