kunit: Taint the kernel when KUnit tests are run
authorDavid Gow <davidgow@google.com>
Fri, 1 Jul 2022 08:47:43 +0000 (16:47 +0800)
committerShuah Khan <skhan@linuxfoundation.org>
Fri, 1 Jul 2022 22:38:43 +0000 (16:38 -0600)
Make KUnit trigger the new TAINT_TEST taint when any KUnit test is run.
Due to KUnit tests not being intended to run on production systems, and
potentially causing problems (or security issues like leaking kernel
addresses), the kernel's state should not be considered safe for
production use after KUnit tests are run.

This both marks KUnit modules as test modules using MODULE_INFO() and
manually taints the kernel when tests are run (which catches builtin
tests).

Acked-by: Luis Chamberlain <mcgrof@kernel.org>
Tested-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: David Gow <davidgow@google.com>
Tested-by: MaĆ­ra Canal <mairacanal@riseup.net>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
include/kunit/test.h
lib/kunit/test.c

index 8ffcd7de96070d303d62074e3cf025a4025bbbcb..ccae848720dc2e3c3094cf84b1e330df5eb77ed9 100644 (file)
@@ -277,7 +277,8 @@ static inline int kunit_run_all_tests(void)
        {                                                               \
                return __kunit_test_suites_exit(__suites);              \
        }                                                               \
-       module_exit(kunit_test_suites_exit)
+       module_exit(kunit_test_suites_exit)                             \
+       MODULE_INFO(test, "Y");
 #else
 #define kunit_test_suites_for_module(__suites)
 #endif /* MODULE */
index a5053a07409ff9c855fe49f10c60d118121dd41e..8b11552dc215eea190a708e1f6aa81bc511937c1 100644 (file)
@@ -11,6 +11,7 @@
 #include <kunit/test-bug.h>
 #include <linux/kernel.h>
 #include <linux/moduleparam.h>
+#include <linux/panic.h>
 #include <linux/sched/debug.h>
 #include <linux/sched.h>
 
@@ -501,6 +502,9 @@ int kunit_run_tests(struct kunit_suite *suite)
        struct kunit_result_stats suite_stats = { 0 };
        struct kunit_result_stats total_stats = { 0 };
 
+       /* Taint the kernel so we know we've run tests. */
+       add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
+
        if (suite->suite_init) {
                suite->suite_init_err = suite->suite_init(suite);
                if (suite->suite_init_err) {