Add __counted_by_ptr attribute to struct stack_trace
Subsystems (3)
cover Add __counted_by_ptr attribute to struct stack_trace
Bill Wendling (2):
stacktrace: Add __counted_by_ptr attribute to struct stack_trace lib/tests: Add KUnit test for struct stack_trace __counted_by_ptr attribute
include/linux/stacktrace.h | 2 +- lib/Kconfig.debug | 10 +++++++ lib/kunit/.kunitconfig | 1 + lib/tests/Makefile | 1 + lib/tests/stacktrace_kunit.c | 51 ++++++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 lib/tests/stacktrace_kunit.c
Signature · 1 line
2.55.0.860.g4b6b3295ed-goog
PATCH 1/2 stacktrace: Add __counted_by_ptr attribute to struct stack_trace
For hardening and catching out-of-bounds accesses to the 'entries' pointer field in 'struct stack_trace', associate it with its count field 'max_entries' using the __counted_by_ptr attribute.
An analysis of the codebase reveals that 'struct stack_trace' is instantiated and initialized across several entry points in 'kernel/stacktrace.c'. In each execution path, 'trace.entries' is assigned a buffer of size 'size', and 'trace.max_entries' is assigned 'size' concurrently within the structure's initializer block. The pointer is not accessed before the count is set.
Because 'trace.entries' is always assigned at the same time as 'trace.max_entries' during initialization and is never reallocated or accessed beforehand, there are no uninitialized access windows. The 'max_entries' field accurately holds the exact element count of the buffer allocated for the 'entries' pointer, ensuring that compiler fortification and KASAN bounds checks using __counted_by_ptr do not trigger false-positive bounds checks or runtime panics.
---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Brendan Higgins <[email protected]>
Cc: David Gow <[email protected]>
Cc: Rae Moar <[email protected]>
Cc: Ryota Sakamoto <[email protected]>
Cc: Kuan-Wei Chiu <[email protected]>
Cc: Pasha Tatashin <[email protected]>
Cc: Dmitry Antipov <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Kir Chou <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
--- include/linux/stacktrace.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/stacktrace.h +1 -1
@@ -81,7 +81,7 @@ unsigned int filter_irq_stacks(unsigned long *entries, unsigned int nr_entries);/* Internal interfaces. Do not use in generic code */ struct stack_trace {unsigned int nr_entries, max_entries; -unsigned long *entries; + unsigned long *entries __counted_by_ptr(max_entries);unsigned int skip;/* input argument: How many entries to skip */ };
Signature · 1 line
2.55.0.860.g4b6b3295ed-goog
Re: [PATCH 1/2] stacktrace: Add __counted_by_ptr attribute to struct stack_trace
Quoted 19 lines · Bill Wendling
For hardening and catching out-of-bounds accesses to the 'entries' pointer field in 'struct
stack_trace', associate it with its count field 'max_entries' using the__counted_by_ptrattribute.An analysis of the codebase reveals that 'struct
stack_trace' is instantiated and initialized across several entry points in 'kernel/stacktrace.c'. In each execution path, 'trace.entries' is assigned a buffer of size 'size', and 'trace.max_entries' is assigned 'size' concurrently within the structure's initializer block. The pointer is not accessed before the count is set.Because 'trace.entries' is always assigned at the same time as 'trace.max_entries' during initialization and is never reallocated or accessed beforehand, there are no uninitialized access windows. The '
max_entries' field accurately holds the exact element count of the buffer allocated for the 'entries' pointer, ensuring that compiler fortification and KASAN bounds checks using__counted_by_ptrdo not trigger false-positive bounds checks or runtime panics.
Quoted 2 lines
Assisted-by Gemini NextSigned-off-by Bill Wendling <[email protected]>
Thanks
-Gustavo
Quoted 36 lines
---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Brendan Higgins <[email protected]>
Cc: David Gow <[email protected]>
Cc: Rae Moar <[email protected]>
Cc: Ryota Sakamoto <[email protected]>
Cc: Kuan-Wei Chiu <[email protected]>
Cc: Pasha Tatashin <[email protected]>
Cc: Dmitry Antipov <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Kir Chou <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]--- include/linux/stacktrace.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/stacktrace.h +1
-1@@ -81,7 +81,7 @@ unsigned int filter_irq_stacks(unsigned long *entries, unsigned int nr_entries);/* Internal interfaces. Do not use in generic code */ struct stack_trace {unsigned int nr_entries, max_entries; -unsigned long *entries; + unsigned long *entries __counted_by_ptr(max_entries);unsigned int skip;/* input argument: How many entries to skip */ };
PATCH 2/2 lib/tests: Add KUnit test for struct stack_trace __counted_by_ptr attribute
In reply to Bill WendlingAdd a custom KUnit test suite 'stacktrace_counted_by' to verify that the __counted_by_ptr annotation on the 'entries' field of 'struct stack_trace' behaves correctly.
The test verifies that 'max_entries' correctly limits and validates access to 'entries' when CONFIG_ARCH_STACKWALK is not defined. If it is defined, the test is cleanly skipped at runtime to prevent compile-time or runtime failures due to 'struct stack_trace' being undefined on modern architectures.
---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Brendan Higgins <[email protected]>
Cc: David Gow <[email protected]>
Cc: Rae Moar <[email protected]>
Cc: Ryota Sakamoto <[email protected]>
Cc: Kuan-Wei Chiu <[email protected]>
Cc: Pasha Tatashin <[email protected]>
Cc: Dmitry Antipov <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Kir Chou <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
--- lib/Kconfig.debug | 10 +++++++ lib/kunit/.kunitconfig | 1 + lib/tests/Makefile | 1 + lib/tests/stacktrace_kunit.c | 51 ++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 lib/tests/stacktrace_kunit.c
lib/Kconfig.debug +10 -0
@@ -2716,6 +2716,16 @@ config BITOPS_KUNIT If unsure, say N. +config STACKTRACE_KUNIT_TEST + tristate "KUnit test for stacktrace counted_by attribute" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + This option enables the KUnit test for verifying the __counted_by_ptr + attribute on struct stack_trace. + + If unsure, say N. + config BITFIELD_KUNIT tristate "KUnit test bitfield functions at runtime" if !KUNIT_ALL_TESTS depends on KUNIT
lib/kunit/.kunitconfig +1 -0
@@ -1,3 +1,4 @@ CONFIG_KUNIT=y CONFIG_KUNIT_TEST=y CONFIG_KUNIT_EXAMPLE_TEST=y +CONFIG_STACKTRACE_KUNIT_TEST=y
lib/tests/Makefile +1 -0
@@ -6,6 +6,7 @@ CFLAGS_bitfield_kunit.o := $(DISABLE_STRUCTLEAK_PLUGIN) obj-$(CONFIG_BASE64_KUNIT) += base64_kunit.o obj-$(CONFIG_BITOPS_KUNIT) += bitops_kunit.o +obj-$(CONFIG_STACKTRACE_KUNIT_TEST) += stacktrace_kunit.o obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o obj-$(CONFIG_BITS_TEST) += test_bits.o obj-$(CONFIG_SHDI3_KUNIT_TEST) += shdi3_kunit.o
lib/tests/stacktrace_kunit.c new file +51 -0
51 lines not on this page · raw message
Signature · 1 line
2.55.0.860.g4b6b3295ed-goog
Re: [PATCH 2/2] lib/tests: Add KUnit test for struct stack_trace __counted_by_ptr attribute
Hi Bill,
Quoted 102 lines · Bill Wendling
Add a custom KUnit test suite '
stacktrace_counted_by' to verify that the__counted_by_ptrannotation on the 'entries' field of 'structstack_trace' behaves correctly.The test verifies that '
max_entries' correctly limits and validates access to 'entries' whenCONFIG_ARCH_STACKWALKis not defined. If it is defined, the test is cleanly skipped at runtime to prevent compile-time or runtime failures due to 'structstack_trace' being undefined on modern architectures.Assisted-by Gemini NextSigned-off-by Bill Wendling <[email protected]>---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Brendan Higgins <[email protected]>
Cc: David Gow <[email protected]>
Cc: Rae Moar <[email protected]>
Cc: Ryota Sakamoto <[email protected]>
Cc: Kuan-Wei Chiu <[email protected]>
Cc: Pasha Tatashin <[email protected]>
Cc: Dmitry Antipov <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Kir Chou <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]--- lib/Kconfig.debug | 10 +++++++ lib/kunit/.kunitconfig | 1 + lib/tests/Makefile | 1 + lib/tests/stacktrace_kunit.c | 51 ++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 lib/tests/stacktrace_kunit.clib/Kconfig.debug +10
-0@@ -2716,6 +2716,16 @@ config BITOPS_KUNIT If unsure, say N. +config STACKTRACE_KUNIT_TEST + tristate "KUnit test for stacktrace counted_by attribute" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + This option enables the KUnit test for verifying the __counted_by_ptr + attribute on struct stack_trace. + + If unsure, say N. + config BITFIELD_KUNIT tristate "KUnit test bitfield functions at runtime" if !KUNIT_ALL_TESTS depends on KUNITlib/kunit/.kunitconfig +1
-0@@ -1,3 +1,4 @@ CONFIG_KUNIT=y CONFIG_KUNIT_TEST=y CONFIG_KUNIT_EXAMPLE_TEST=y +CONFIG_STACKTRACE_KUNIT_TEST=ylib/tests/Makefile +1
-0@@ -6,6 +6,7 @@ CFLAGS_bitfield_kunit.o := $(DISABLE_STRUCTLEAK_PLUGIN) obj-$(CONFIG_BASE64_KUNIT) += base64_kunit.o obj-$(CONFIG_BITOPS_KUNIT) += bitops_kunit.o +obj-$(CONFIG_STACKTRACE_KUNIT_TEST) += stacktrace_kunit.o obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o obj-$(CONFIG_BITS_TEST) += test_bits.o obj-$(CONFIG_SHDI3_KUNIT_TEST) += shdi3_kunit.olib/tests/stacktrace_kunit.c new file +15
-0@@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KUnit test for struct stack_trace counted_by attribute. +*/ + +#include <kunit/test.h> +#include <linux/stacktrace.h> + +#ifndef CONFIG_ARCH_STACKWALK +static void test_stack_trace_counted_by(struct kunit *test) +{ + unsigned long entries_buf[4]; + struct stack_trace trace = { + .entries = entries_buf, + .max_entries = 4,
lib/tests/stacktrace_kunit.c line 25
Quoted 10 lines
lib/tests/stacktrace_kunit.c new file +10
-0+ }; + +KUNIT_EXPECT_EQ (test, trace.max_entries,4U ); +KUNIT_EXPECT_PTR_EQ (test, trace.entries, (unsigned long *)entries_buf); + +/* Write to the allocated elements to verify access */ + trace.entries[0 ] =0xdeadbeef ; + trace.entries[1 ] =0xbeefcafe ; + trace.entries[2 ] =0xcafebabe ; + trace.entries[3 ] =0x12345678 ;
This only does in bounds array writes.
To test __counted_by_ptr, I thought we were supposed to intentionally trigger an out of bounds access and see if it actually catches the error?
Regards,
Kuan-Wei
Quoted 29 lines
lib/tests/stacktrace_kunit.c new file +26
-0+ +KUNIT_EXPECT_EQ (test, trace.entries[0 ],0xdeadbeefUL ); +KUNIT_EXPECT_EQ (test, trace.entries[1 ],0xbeefcafeUL ); +KUNIT_EXPECT_EQ (test, trace.entries[2 ],0xcafebabeUL ); + KUNIT_EXPECT_EQ(test, trace.entries[3], 0x12345678UL); +} +#else +static void test_stack_trace_counted_by (struct kunit *test) +{ +kunit_skip (test,"CONFIG_ARCH_STACKWALK is enabled, struct stack_trace is not defined" ); +} +#endif + +static struct kunit_case stacktrace_test_cases[] = { +KUNIT_CASE (test_stack_trace_counted_by), + {} +}; + +static struct kunit_suite stacktrace_test_suite = { + .name ="stacktrace_counted_by" , + .test_cases = stacktrace_test_cases, +}; + +kunit_test_suite (stacktrace_test_suite); + +MODULE_LICENSE ("GPL" );Signature · 1 line
2.55.0.860.g4b6b3295ed-goog
Re: [PATCH 2/2] lib/tests: Add KUnit test for struct stack_trace __counted_by_ptr attribute
Hi Kuan-Wei,
Quoted 113 lines · Kuan-Wei Chiu
Hi Bill,
Quoted 109 lines · Bill Wendling
Add a custom KUnit test suite '
stacktrace_counted_by' to verify that the__counted_by_ptrannotation on the 'entries' field of 'structstack_trace' behaves correctly.The test verifies that '
max_entries' correctly limits and validates access to 'entries' whenCONFIG_ARCH_STACKWALKis not defined. If it is defined, the test is cleanly skipped at runtime to prevent compile-time or runtime failures due to 'structstack_trace' being undefined on modern architectures.Assisted-by Gemini NextSigned-off-by Bill Wendling <[email protected]>---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Brendan Higgins <[email protected]>
Cc: David Gow <[email protected]>
Cc: Rae Moar <[email protected]>
Cc: Ryota Sakamoto <[email protected]>
Cc: Kuan-Wei Chiu <[email protected]>
Cc: Pasha Tatashin <[email protected]>
Cc: Dmitry Antipov <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Kir Chou <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]--- lib/Kconfig.debug | 10 +++++++ lib/kunit/.kunitconfig | 1 + lib/tests/Makefile | 1 + lib/tests/stacktrace_kunit.c | 51 ++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 lib/tests/stacktrace_kunit.clib/Kconfig.debug +10
-0@@ -2716,6 +2716,16 @@ config BITOPS_KUNIT If unsure, say N. +config STACKTRACE_KUNIT_TEST + tristate "KUnit test for stacktrace counted_by attribute" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + This option enables the KUnit test for verifying the __counted_by_ptr + attribute on struct stack_trace. + + If unsure, say N. + config BITFIELD_KUNIT tristate "KUnit test bitfield functions at runtime" if !KUNIT_ALL_TESTS depends on KUNITlib/kunit/.kunitconfig +1
-0@@ -1,3 +1,4 @@ CONFIG_KUNIT=y CONFIG_KUNIT_TEST=y CONFIG_KUNIT_EXAMPLE_TEST=y +CONFIG_STACKTRACE_KUNIT_TEST=ylib/tests/Makefile +1
-0@@ -6,6 +6,7 @@ CFLAGS_bitfield_kunit.o := $(DISABLE_STRUCTLEAK_PLUGIN) obj-$(CONFIG_BASE64_KUNIT) += base64_kunit.o obj-$(CONFIG_BITOPS_KUNIT) += bitops_kunit.o +obj-$(CONFIG_STACKTRACE_KUNIT_TEST) += stacktrace_kunit.o obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o obj-$(CONFIG_BITS_TEST) += test_bits.o obj-$(CONFIG_SHDI3_KUNIT_TEST) += shdi3_kunit.olib/tests/stacktrace_kunit.c new file +22
-0@@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KUnit test for struct stack_trace counted_by attribute. +*/ + +#include <kunit/test.h> +#include <linux/stacktrace.h> + +#ifndef CONFIG_ARCH_STACKWALK +static void test_stack_trace_counted_by (struct kunit *test) +{ +unsigned long entries_buf[4 ]; +struct stack_trace trace = { + .entries = entries_buf, + .max_entries =4 , + }; + +KUNIT_EXPECT_EQ (test, trace.max_entries,4U ); +KUNIT_EXPECT_PTR_EQ (test, trace.entries, (unsigned long *)entries_buf); + +/* Write to the allocated elements to verify access */ + trace.entries[0 ] =0xdeadbeef ;
Quoted 9 lines
lib/tests/stacktrace_kunit.c line 25
Quoted 3 lines
lib/tests/stacktrace_kunit.c new file +3
-0+ trace.entries[1 ] =0xbeefcafe ; + trace.entries[2 ] =0xcafebabe ; + trace.entries[3 ] =0x12345678 ;This only does in bounds array writes.
To test__counted_by_ptr, I thought we were supposed to intentionally trigger an out of bounds access and see if it actually catches the error?
True. This is testing only that the existing behavior doesn't fail with the new attribute. It looks like you're not the only one to comment on this, though...
-bw
Quoted 29 lines
Quoted 29 lines
lib/tests/stacktrace_kunit.c new file +26
-1+ +KUNIT_EXPECT_EQ (test, trace.entries[0 ],0xdeadbeefUL ); +KUNIT_EXPECT_EQ (test, trace.entries[1 ],0xbeefcafeUL ); +KUNIT_EXPECT_EQ (test, trace.entries[2 ],0xcafebabeUL ); + KUNIT_EXPECT_EQ(test, trace.entries[3], 0x12345678UL); +} +#else +static void test_stack_trace_counted_by (struct kunit *test) +{ +kunit_skip (test,"CONFIG_ARCH_STACKWALK is enabled, struct stack_trace is not defined" ); +} +#endif + +static struct kunit_case stacktrace_test_cases[] = { +KUNIT_CASE (test_stack_trace_counted_by), + {} +}; + +static struct kunit_suite stacktrace_test_suite = { + .name ="stacktrace_counted_by" , + .test_cases = stacktrace_test_cases, +}; + +kunit_test_suite (stacktrace_test_suite); + +MODULE_LICENSE ("GPL" ); --2.55.0.860.g4b6b3295ed-goog
Re: [PATCH 2/2] lib/tests: Add KUnit test for struct stack_trace __counted_by_ptr attribute
In reply to Bill WendlingQuoted 3 lines · Bill Wendling
Add a custom KUnit test suite '
stacktrace_counted_by' to verify that the__counted_by_ptrannotation on the 'entries' field of 'structstack_trace' behaves correctly.
The implementation of __counted_by_ptr should be tested by a dedicated unittest for that feature. And I am fairly sure that already exists. It should not be tested in random subsystems.
Quoted 34 lines
The test verifies that '
max_entries' correctly limits and validates access to 'entries' whenCONFIG_ARCH_STACKWALKis not defined. If it is defined, the test is cleanly skipped at runtime to prevent compile-time or runtime failures due to 'structstack_trace' being undefined on modern architectures.Assisted-by Gemini NextSigned-off-by Bill Wendling <[email protected]>---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Brendan Higgins <[email protected]>
Cc: David Gow <[email protected]>
Cc: Rae Moar <[email protected]>
Cc: Ryota Sakamoto <[email protected]>
Cc: Kuan-Wei Chiu <[email protected]>
Cc: Pasha Tatashin <[email protected]>
Cc: Dmitry Antipov <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Kir Chou <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]--- lib/Kconfig.debug | 10 +++++++ lib/kunit/.kunitconfig | 1 + lib/tests/Makefile | 1 + lib/tests/stacktrace_kunit.c | 51 ++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 lib/tests/stacktrace_kunit.c
(...)
Quoted 6 lines
lib/tests/stacktrace_kunit.c new file +0
-0@@ -0,0 +1,51 @@
(...)
lib/tests/stacktrace_kunit.c line 35
Quoted 5 lines
lib/tests/stacktrace_kunit.c new file +5
-0+} +#else +static void test_stack_trace_counted_by (struct kunit *test) +{ +kunit_skip (test,"CONFIG_ARCH_STACKWALK is enabled, struct stack_trace is not defined" );
If the test requires a specific kconfig symbol, then depend on it in kconfig. Putting all of the code behind a single, giant ifdef is pointless.
Quoted 2 lines
lib/tests/stacktrace_kunit.c new file +2
-0+} +#endif
(...)
Re: [PATCH 2/2] lib/tests: Add KUnit test for struct stack_trace __counted_by_ptr attribute
Quoted 10 lines · Thomas Weißschuh
Quoted 3 lines · Bill Wendling
Add a custom KUnit test suite '
stacktrace_counted_by' to verify that the__counted_by_ptrannotation on the 'entries' field of 'structstack_trace' behaves correctly.The implementation of
__counted_by_ptrshould be tested by a dedicated unittest for that feature. And I am fairly sure that already exists. It should not be tested in random subsystems.
I'm looking at tools/unittests and...what in the world is it supposed to be testing? It looks like it's testing absolutely nothing of substance. I assume you want there to be a test that runs and produces a stack trace triggered by an invalid access, right? I'm unable to find any tests in tools/unittests that do anything like that...maybe I'm missing something?
-bw
Quoted 59 lines
Quoted 34 lines
The test verifies that '
max_entries' correctly limits and validates access to 'entries' whenCONFIG_ARCH_STACKWALKis not defined. If it is defined, the test is cleanly skipped at runtime to prevent compile-time or runtime failures due to 'structstack_trace' being undefined on modern architectures.Assisted-by Gemini NextSigned-off-by Bill Wendling <[email protected]>---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Brendan Higgins <[email protected]>
Cc: David Gow <[email protected]>
Cc: Rae Moar <[email protected]>
Cc: Ryota Sakamoto <[email protected]>
Cc: Kuan-Wei Chiu <[email protected]>
Cc: Pasha Tatashin <[email protected]>
Cc: Dmitry Antipov <[email protected]>
Cc: Petr Mladek <[email protected]>
Cc: Kir Chou <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]--- lib/Kconfig.debug | 10 +++++++ lib/kunit/.kunitconfig | 1 + lib/tests/Makefile | 1 + lib/tests/stacktrace_kunit.c | 51 ++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 lib/tests/stacktrace_kunit.c(...)
Quoted 6 lines
lib/tests/stacktrace_kunit.c new file +0
-0@@ -0,0 +1,51 @@(...)
lib/tests/stacktrace_kunit.c line 35
Quoted 5 lines
lib/tests/stacktrace_kunit.c new file +5
-0+} +#else +static void test_stack_trace_counted_by (struct kunit *test) +{ +kunit_skip (test,"CONFIG_ARCH_STACKWALK is enabled, struct stack_trace is not defined" );If the test requires a specific kconfig symbol, then depend on it in kconfig. Putting all of the code behind a single, giant ifdef is pointless.
Quoted 2 lines
lib/tests/stacktrace_kunit.c new file +2
-0+} +#endif (...)
Re: [PATCH 2/2] lib/tests: Add KUnit test for struct stack_trace __counted_by_ptr attribute
Hi Bill,
sorry for the long delay.
Quoted 8 lines · Bill Wendling
Quoted 6 lines · Thomas Weißschuh
Quoted 3 lines · Bill Wendling
Add a custom KUnit test suite '
stacktrace_counted_by' to verify that the__counted_by_ptrannotation on the 'entries' field of 'structstack_trace' behaves correctly.
Quoted 6 lines
Quoted 4 lines
The implementation of
__counted_by_ptrshould be tested by a dedicated unittest for that feature. And I am fairly sure that already exists. It should not be tested in random subsystems.I'm looking at
tools/unittestsand...what in the world is it supposed to be testing?
This seems to be testing the documentation generator. An assumption which is corroborated by 'git log'.
Quoted 1 line
It looks like it's testing absolutely nothing of substance.
Debatable...
Quoted 2 lines
I assume you want there to be a test that runs and produces a stack trace triggered by an invalid access, right?
Correct.
Quoted 2 lines
I'm unable to find any tests in
tools/unitteststhat do anything like that...maybe I'm missing something?
Look for KUnit tests instead. For example lib/tests/fortify_kunit.c. Or non-KUnit tests in lib/test_fortify/.
Or LKDTM tests in drivers/misc/lkdtm/.
Also see Kees' recent message about testing traps with KUnit [0]. Apparently it is not so easy to test this from KUnit. But your proposed stack_trace-specific test would have run into the same issues.
[0] https://lore.kernel.org/lkml/202609041620.81446C2A@keescook/
(...)
Thomas
David Gow
maintainerRe: [PATCH 2/2] lib/tests: Add KUnit test for struct stack_trace __counted_by_ptr attribute
In reply to Bill WendlingLe 23/08/2026 à 20:35, Bill Wendling a écrit :
Quoted 24 lines
Add a custom KUnit test suite '
stacktrace_counted_by' to verify that the__counted_by_ptrannotation on the 'entries' field of 'structstack_trace' behaves correctly.The test verifies that '
max_entries' correctly limits and validates access to 'entries' whenCONFIG_ARCH_STACKWALKis not defined. If it is defined, the test is cleanly skipped at runtime to prevent compile-time or runtime failures due to 'structstack_trace' being undefined on modern architectures.Assisted-by Gemini NextSigned-off-by Bill Wendling <[email protected]>---
Cc: Kees Cook <[email protected]>
Cc: "Gustavo A. R. Silva" <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Brendan Higgins <[email protected]>
Cc: David Gow <[email protected]>
Cc: Rae Moar <[email protected]>
Cc: Ryota Sakamoto <[email protected]>
Cc: Kuan-Wei Chiu <[email protected]>
Cc: Pasha Tatashin <[email protected]>
Cc: Dmitry Antipov <[email protected]>
Cc: Petr Mladek <[email protected]>
Quoted 8 lines
Cc: Kir Chou <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
I'm a bit confused: what is this actually testing? You're creating an array of size 4 and writing 4 entries to it, after setting max_entries correctly? Removing the __counted_by_ptr attribute doesn't change anything here. Is there any circumstance where this should fail? The only case I can think of is if __counted_by_ptr is either totally broken, or someone randomly points it to a different value. Neither of which seem likely.
Even if this did something more exciting (like try to access elements beyond the end of the array), I'm not sure a KUnit test is the optimal place for it: ultimately we're looking for a compiler warning / error, not a runtime one, and even then, one which probably isn't of much value given it's unlikely __counted_by_ptr will spontaneously break.
Added to that, there are a bunch of stylistic issues with the test below anyway. These mostly seem like LLM-isms, but probably should've been caught before this went to the list.
Unless there's something I'm missing (and, if so, it's probably worth writing a description which better explains the "why" here), I think we can probably drop this test entirely and stick to just patch 1.
-- David
Quoted 12 lines
lib/Kconfig.debug | 10 +++++++ lib/kunit/.kunitconfig | 1 + lib/tests/Makefile | 1 + lib/tests/stacktrace_kunit.c | 51 ++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+)create mode 100644
lib/tests/stacktrace_kunit.clib/Kconfig.debug +0
-0
lib/Kconfig.debug config BITOPS_KUNIT · line 2721
Quoted 6 lines
lib/Kconfig.debug +3
-0@@ -2716,6 +2716,16 @@ config BITOPS_KUNIT If unsure, say N. +config STACKTRACE_KUNIT_TEST + tristate "KUnit test for stacktrace counted_by attribute" if !KUNIT_ALL_TESTS + depends on KUNIT
This should also just depend on CONFIG_ARCH_STACKWALK, rather than doing the whole skip test thing.
Quoted 15 lines
lib/Kconfig.debug +7
-0+ default KUNIT_ALL_TESTS + help + This option enables the KUnit test for verifying the __counted_by_ptr + attribute on struct stack_trace. + + If unsure, say N. + config BITFIELD_KUNIT tristate "KUnit test bitfield functions at runtime" if !KUNIT_ALL_TESTS depends on KUNITlib/kunit/.kunitconfig +0
-0
lib/kunit/.kunitconfig line 4
Quoted 4 lines
lib/kunit/.kunitconfig +1
-0@@ -1,3 +1,4 @@ CONFIG_KUNIT=y CONFIG_KUNIT_TEST=y CONFIG_KUNIT_EXAMPLE_TEST=y +CONFIG_STACKTRACE_KUNIT_TEST=y
Please don't add this here. This is not a test of KUnit itself, which is what the lib/kunit/.kunitconfig file is meant to configure.
For general KUnit test build, CONFIG_KUNIT_ALL_TESTS will enable it.
Quoted 55 lines
lib/tests/Makefile +1
-0@@ -6,6 +6,7 @@ CFLAGS_bitfield_kunit.o := $(DISABLE_STRUCTLEAK_PLUGIN) obj-$(CONFIG_BASE64_KUNIT) += base64_kunit.o obj-$(CONFIG_BITOPS_KUNIT) += bitops_kunit.o +obj-$(CONFIG_STACKTRACE_KUNIT_TEST) += stacktrace_kunit.o obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o obj-$(CONFIG_BITS_TEST) += test_bits.o obj-$(CONFIG_SHDI3_KUNIT_TEST) += shdi3_kunit.olib/tests/stacktrace_kunit.c new file +37
-0@@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KUnit test for struct stack_trace counted_by attribute. +*/ + +#include <kunit/test.h> +#include <linux/stacktrace.h> + +#ifndef CONFIG_ARCH_STACKWALK +static void test_stack_trace_counted_by (struct kunit *test) +{ +unsigned long entries_buf[4 ]; +struct stack_trace trace = { + .entries = entries_buf, + .max_entries =4 , + }; + +KUNIT_EXPECT_EQ (test, trace.max_entries,4U ); +KUNIT_EXPECT_PTR_EQ (test, trace.entries, (unsigned long *)entries_buf); + +/* Write to the allocated elements to verify access */ + trace.entries[0 ] =0xdeadbeef ; + trace.entries[1 ] =0xbeefcafe ; + trace.entries[2 ] =0xcafebabe ; + trace.entries[3 ] =0x12345678 ; + +KUNIT_EXPECT_EQ (test, trace.entries[0 ],0xdeadbeefUL ); +KUNIT_EXPECT_EQ (test, trace.entries[1 ],0xbeefcafeUL ); +KUNIT_EXPECT_EQ (test, trace.entries[2 ],0xcafebabeUL ); +KUNIT_EXPECT_EQ (test, trace.entries[3 ],0x12345678UL ); +} +#else +static void test_stack_trace_counted_by (struct kunit *test) +{ +kunit_skip (test,"CONFIG_ARCH_STACKWALK is enabled, struct stack_trace is not defined" ); +} +#endif
lib/tests/stacktrace_kunit.c line 47
Quoted 10 lines
lib/tests/stacktrace_kunit.c new file +10
-0+ +static struct kunit_case stacktrace_test_cases[] = { +KUNIT_CASE (test_stack_trace_counted_by), + {} +}; + +static struct kunit_suite stacktrace_test_suite = { + .name ="stacktrace_counted_by" , + .test_cases = stacktrace_test_cases, +};
Is this suite intended to cover more stacktrace tests than just the counted_by one at some point? If so, it'd be better to name it "stacktrace". If not, these structs should be named "stacktrace_counted_by_test_suite", etc.
Quoted 4 lines
lib/tests/stacktrace_kunit.c new file +4
-0+ +kunit_test_suite (stacktrace_test_suite); + +MODULE_LICENSE ("GPL" );