1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Module taint unload tracking support
4 *
5 * Copyright (C) 2022 Aaron Tomlin
6 */
7
8 #include <linux/module.h>
9 #include <linux/string.h>
10 #include <linux/printk.h>
11 #include <linux/slab.h>
12 #include <linux/list.h>
13 #include <linux/rculist.h>
14 #include "internal.h"
15
16 static LIST_HEAD(unloaded_tainted_modules);
17
try_add_tainted_module(struct module * mod)18 int try_add_tainted_module(struct module *mod)
19 {
20 struct mod_unload_taint *mod_taint;
21
22 module_assert_mutex_or_preempt();
23
24 list_for_each_entry_rcu(mod_taint, &unloaded_tainted_modules, list,
25 lockdep_is_held(&module_mutex)) {
26 if (!strcmp(mod_taint->name, mod->name) &&
27 mod_taint->taints & mod->taints) {
28 mod_taint->count++;
29 goto out;
30 }
31 }
32
33 mod_taint = kmalloc(sizeof(*mod_taint), GFP_KERNEL);
34 if (unlikely(!mod_taint))
35 return -ENOMEM;
36 strscpy(mod_taint->name, mod->name, MODULE_NAME_LEN);
37 mod_taint->taints = mod->taints;
38 list_add_rcu(&mod_taint->list, &unloaded_tainted_modules);
39 mod_taint->count = 1;
40 out:
41 return 0;
42 }
43
print_unloaded_tainted_modules(void)44 void print_unloaded_tainted_modules(void)
45 {
46 struct mod_unload_taint *mod_taint;
47 char buf[MODULE_FLAGS_BUF_SIZE];
48
49 if (!list_empty(&unloaded_tainted_modules)) {
50 printk(KERN_DEFAULT "Unloaded tainted modules:");
51 list_for_each_entry_rcu(mod_taint, &unloaded_tainted_modules,
52 list) {
53 size_t l;
54
55 l = module_flags_taint(mod_taint->taints, buf);
56 buf[l++] = '\0';
57 pr_cont(" %s(%s):%llu", mod_taint->name, buf,
58 mod_taint->count);
59 }
60 }
61 }
62