[PATCH 1/2] stacktrace: Add __counted_by_ptr attribute to struct stack_trace
From: Bill Wendling
Date: Sun Aug 23 2026 - 08:36:13 EST
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.
Assisted-by: Gemini Next
Signed-off-by: Bill Wendling <morbo@xxxxxxxxxx>
---
Cc: Kees Cook <kees@xxxxxxxxxx>
Cc: "Gustavo A. R. Silva" <gustavoars@xxxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Brendan Higgins <brendan.higgins@xxxxxxxxx>
Cc: David Gow <david@xxxxxxxxxxxx>
Cc: Rae Moar <raemoar63@xxxxxxxxx>
Cc: Ryota Sakamoto <sakamo.ryota@xxxxxxxxx>
Cc: Kuan-Wei Chiu <visitorckw@xxxxxxxxx>
Cc: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>
Cc: Dmitry Antipov <dmantipov@xxxxxxxxx>
Cc: Petr Mladek <pmladek@xxxxxxxx>
Cc: Kir Chou <note351@xxxxxxxxxxx>
Cc: codemender-patching+linux@xxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Cc: linux-hardening@xxxxxxxxxxxxxxx
Cc: linux-kselftest@xxxxxxxxxxxxxxx
Cc: kunit-dev@xxxxxxxxxxxxxxxx
Cc: linux-hardening@xxxxxxxxxxxxxxx
---
include/linux/stacktrace.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/linux/stacktrace.h b/include/linux/stacktrace.h
index 97455880ac41..fbb0925d8864 100644
--- a/include/linux/stacktrace.h
+++ b/include/linux/stacktrace.h
@@ -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 */
};
--
2.55.0.860.g4b6b3295ed-goog