mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Brendan Higgins <brendan.higgins@linux.dev>,
	David Gow <david@davidgow.net>
Cc: Rae Moar <raemoar63@gmail.com>,
	linux-kselftest@vger.kernel.org,  kunit-dev@googlegroups.com,
	linux-kernel@vger.kernel.org,  llvm@lists.linux.dev,
	Elkin Cruz <elkin@google.com>, Ian Rogers <irogers@google.com>
Subject: [PATCH] kunit: fix uninitialized err and static suite kfree() when disabled
Date: Thu, 19 Nov 2026 09:06:36 -0800	[thread overview]
Message-ID: <20261119170636.1520696-1-irogers@google.com> (raw)

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


                 reply	other threads:[~2026-11-19 17:06 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119170636.1520696-1-irogers@google.com \
    --to=irogers@google.com \
    --cc=brendan.higgins@linux.dev \
    --cc=david@davidgow.net \
    --cc=elkin@google.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=raemoar63@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®