[PATCH v3 2/2] lib: stackdepot: Avoid null pointer if stack_depot_save is called too early

From: John Stultz
Date: Thu Dec 19 2024 - 01:07:19 EST


If stack_depot_save is called before stackdepot has initialized
a null reference to the stack_table might be used, crashing the
system.

Instead, check stack_table has been initialized before going
further.

Cc: Steven Rostedt <rostedt@xxxxxxxxxxx>
Cc: Alexander Potapenko <glider@xxxxxxxxxx>
Cc: Andrey Konovalov <andreyknvl@xxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: kernel-team@xxxxxxxxxxx
Signed-off-by: John Stultz <jstultz@xxxxxxxxxx>
---
lib/stackdepot.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/lib/stackdepot.c b/lib/stackdepot.c
index 245d5b4166999..1fe627584535d 100644
--- a/lib/stackdepot.c
+++ b/lib/stackdepot.c
@@ -611,6 +611,9 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries,
if (unlikely(nr_entries == 0) || stack_depot_disabled)
return 0;

+ if (!stack_table)
+ return 0;
+
hash = hash_stack(entries, nr_entries);
bucket = &stack_table[hash & stack_hash_mask];

--
2.47.1.613.gc27f4b7a9f-goog