* [PATCH] kunit: fix uninitialized err and static suite kfree() when disabled
@ 2026-11-19 17:06 Ian Rogers
0 siblings, 0 replies; only message in thread
From: Ian Rogers @ 2026-11-19 17:06 UTC (permalink / raw)
To: Brendan Higgins, David Gow
Cc: Rae Moar, linux-kselftest, kunit-dev, linux-kernel, llvm,
Elkin Cruz, Ian Rogers
Commit e38f53f04824 ("kunit: Return void from kunit_run_all_tests()")
removed the zero-initialization of 'err' in kunit_run_all_tests(),
overlooking two live consumers of 'err':
1. When KUnit is disabled at runtime (kunit.enable=0) while any suite or
attribute filter is configured (via kunit.filter_glob, kunit.filter, or
CONFIG_KUNIT_DEFAULT_FILTER*), kunit_run_all_tests() sets suite_set to
normal_suite_set (pointing directly into the static .kunit_test_suites
linker section when init_num_suites == 0) and jumps to free_out before
kunit_filter_suites() runs. At free_out, 'if (err)' reads uninitialized
stack memory and either calls kunit_free_suite_set() on static linker
symbols (panicking at boot) or stores them in kunit_boot_suites.
2. When kunit.filter is set without kunit.filter_glob,
kunit_filter_suites() never initializes *err on the success path (or
on !copy OOM), so 'if (*err)' reads uninitialized stack memory and
aborts KUnit execution.
Check kunit_enabled() before merging or filtering suites and jump directly
to 'out', initialize 'err' to 0 in both kunit_run_all_tests() and
kunit_filter_suites(), and report -ENOMEM when suite pointer copy
allocation fails.
Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with
-Wconditional-uninitialized enabled; fix and changelog drafted with an AI
coding assistant. Build-tested under LLVM=1 W=1 (-Werror) and
-Wconditional-uninitialized; not boot-tested with KUnit kernel parameters.
Fixes: fb6988b83b4c ("kunit: fix use-after-free in debugfs when using kunit.filter")
Fixes: e38f53f04824 ("kunit: Return void from kunit_run_all_tests()")
Signed-off-by: Ian Rogers <irogers@google.com>
---
lib/kunit/executor.c | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
index c798a02470d8..5525f4da0bc9 100644
--- a/lib/kunit/executor.c
+++ b/lib/kunit/executor.c
@@ -189,8 +189,11 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
const size_t max = suite_set->end - suite_set->start;
+ *err = 0;
+
copy = kzalloc_objs(*copy, max);
if (!copy) { /* won't be able to run anything, return an empty set */
+ *err = -ENOMEM;
return filtered;
}
copy_start = copy;
@@ -382,7 +385,12 @@ void kunit_run_all_tests(void)
__kunit_suites_start, __kunit_suites_end,
};
size_t init_num_suites = init_suite_set.end - init_suite_set.start;
- int err;
+ int err = 0;
+
+ if (!kunit_enabled()) {
+ pr_info("kunit: disabled\n");
+ goto out;
+ }
if (init_num_suites > 0) {
suite_set = kunit_merge_suite_sets(init_suite_set, normal_suite_set);
@@ -391,11 +399,6 @@ void kunit_run_all_tests(void)
} else
suite_set = normal_suite_set;
- if (!kunit_enabled()) {
- pr_info("kunit: disabled\n");
- goto free_out;
- }
-
if (filter_glob_param || filter_param) {
filtered_suite_set = kunit_filter_suites(&suite_set, filter_glob_param,
filter_param, filter_action_param, &err);
--
2.56.1.708.g77570a6a1d-goog
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-19 17:06 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 17:06 [PATCH] kunit: fix uninitialized err and static suite kfree() when disabled Ian Rogers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®