kunit: fix uninitialized err and static suite kfree() when disabled
PATCH 1/1 kunit: fix uninitialized err and static suite kfree() when disabled
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.
--- lib/kunit/executor.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
lib/kunit/executor.c +9 -6
@@ -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);
Signature · 1 line
2.56.1.708.g77570a6a1d-goog