[PATCH v6 2/4] module: panic: Taint the kernel when selftest modules load

David Gow
2022-07-08T04:49:19Z

Taint the kernel with TAINT_TEST whenever a test module loads, by adding
a new "TEST" module property, and setting it for all modules in the
tools/testing directory. This property can also be set manually, for
tests which live outside the tools/testing directory with:
MODULE_INFO(test, "Y");

Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Reviewed-by: Aaron Tomlin <atomlin@redhat.com>
Acked-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: David Gow <davidgow@google.com>
---

Version 6 of this patch fixes the issue pointed out by Nathan here,
whereby the check for the module property was inverted:
https://lore.kernel.org/linux-kselftest/Ysd9FG1fOSnzKv8d@dev-arch.thelio-3990X/

Changes since v5:
https://lore.kernel.org/linux-kselftest/20220702040959.3232874-2-davidgow@google.com/
- Fix the test for the module property being inverted, making this patch
do exactly the opposite of what it should. (Thanks Nathan Chancellor)
- Revert to using pr_warn(), as we already don't warn if the kernel is
tainted, so won't spam the logs.
- Add Reviewed-, Acked-by tags.

Changes since v4:
https://lore.kernel.org/linux-kselftest/20220701084744.3002019-2-davidgow@google.com/
- Use pr_warn_once() to only log a warning the first time a module
taints the kernel with TAINT_TEST
- Loading lots of test modules is a common usecase, and this would
otherwise spam the logs too much.
- Thanks Luis.
- Remove a superfluous newline (Thanks Greg)
- Add Luis' Reviewed-by tag.

This patch was new in v4 of the series.

---
kernel/module/main.c | 7 +++++++
scripts/mod/modpost.c | 3 +++
2 files changed, 10 insertions(+)

diff --git a/kernel/module/main.c b/kernel/module/main.c
index fed58d30725d..4723f1316709 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -1988,6 +1988,13 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags)
/* Set up license info based on the info section */
set_license(mod, get_modinfo(info, "license"));

+ if (get_modinfo(info, "test")) {
+ if (!test_taint(TAINT_TEST))
+ pr_warn("%s: loading test module taints kernel.\n",
+ mod->name);
+ add_taint_module(mod, TAINT_TEST, LOCKDEP_STILL_OK);
+ }
+
return 0;
}

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 29d5a841e215..5937212b4433 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -2191,6 +2191,9 @@ static void add_header(struct buffer *b, struct module *mod)

if (strstarts(mod->name, "drivers/staging"))
buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
+
+ if (strstarts(mod->name, "tools/testing"))
+ buf_printf(b, "\nMODULE_INFO(test, \"Y\");\n");
}

static void add_exported_symbols(struct buffer *buf, struct module *mod)
--
2.37.0.rc0.161.g10f37bed90-goog