]>
Commit | Line | Data |
---|---|---|
99bd9956 AT |
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> | |
beef988c | 13 | #include <linux/debugfs.h> |
99bd9956 AT |
14 | #include <linux/rculist.h> |
15 | #include "internal.h" | |
16 | ||
17 | static LIST_HEAD(unloaded_tainted_modules); | |
df3e764d | 18 | extern struct dentry *mod_debugfs_root; |
99bd9956 AT |
19 | |
20 | int try_add_tainted_module(struct module *mod) | |
21 | { | |
22 | struct mod_unload_taint *mod_taint; | |
23 | ||
24 | module_assert_mutex_or_preempt(); | |
25 | ||
47cc75aa AT |
26 | if (!mod->taints) |
27 | goto out; | |
28 | ||
99bd9956 AT |
29 | list_for_each_entry_rcu(mod_taint, &unloaded_tainted_modules, list, |
30 | lockdep_is_held(&module_mutex)) { | |
31 | if (!strcmp(mod_taint->name, mod->name) && | |
32 | mod_taint->taints & mod->taints) { | |
33 | mod_taint->count++; | |
34 | goto out; | |
35 | } | |
36 | } | |
37 | ||
38 | mod_taint = kmalloc(sizeof(*mod_taint), GFP_KERNEL); | |
39 | if (unlikely(!mod_taint)) | |
40 | return -ENOMEM; | |
41 | strscpy(mod_taint->name, mod->name, MODULE_NAME_LEN); | |
42 | mod_taint->taints = mod->taints; | |
43 | list_add_rcu(&mod_taint->list, &unloaded_tainted_modules); | |
44 | mod_taint->count = 1; | |
45 | out: | |
46 | return 0; | |
47 | } | |
48 | ||
49 | void print_unloaded_tainted_modules(void) | |
50 | { | |
51 | struct mod_unload_taint *mod_taint; | |
52 | char buf[MODULE_FLAGS_BUF_SIZE]; | |
53 | ||
54 | if (!list_empty(&unloaded_tainted_modules)) { | |
55 | printk(KERN_DEFAULT "Unloaded tainted modules:"); | |
56 | list_for_each_entry_rcu(mod_taint, &unloaded_tainted_modules, | |
57 | list) { | |
58 | size_t l; | |
59 | ||
60 | l = module_flags_taint(mod_taint->taints, buf); | |
61 | buf[l++] = '\0'; | |
62 | pr_cont(" %s(%s):%llu", mod_taint->name, buf, | |
63 | mod_taint->count); | |
64 | } | |
65 | } | |
66 | } | |
beef988c AT |
67 | |
68 | #ifdef CONFIG_DEBUG_FS | |
69 | static void *unloaded_tainted_modules_seq_start(struct seq_file *m, loff_t *pos) | |
70 | __acquires(rcu) | |
71 | { | |
72 | rcu_read_lock(); | |
73 | return seq_list_start_rcu(&unloaded_tainted_modules, *pos); | |
74 | } | |
75 | ||
76 | static void *unloaded_tainted_modules_seq_next(struct seq_file *m, void *p, loff_t *pos) | |
77 | { | |
78 | return seq_list_next_rcu(p, &unloaded_tainted_modules, pos); | |
79 | } | |
80 | ||
81 | static void unloaded_tainted_modules_seq_stop(struct seq_file *m, void *p) | |
82 | __releases(rcu) | |
83 | { | |
84 | rcu_read_unlock(); | |
85 | } | |
86 | ||
87 | static int unloaded_tainted_modules_seq_show(struct seq_file *m, void *p) | |
88 | { | |
89 | struct mod_unload_taint *mod_taint; | |
90 | char buf[MODULE_FLAGS_BUF_SIZE]; | |
91 | size_t l; | |
92 | ||
93 | mod_taint = list_entry(p, struct mod_unload_taint, list); | |
94 | l = module_flags_taint(mod_taint->taints, buf); | |
95 | buf[l++] = '\0'; | |
96 | ||
97 | seq_printf(m, "%s (%s) %llu", mod_taint->name, buf, mod_taint->count); | |
98 | seq_puts(m, "\n"); | |
99 | ||
100 | return 0; | |
101 | } | |
102 | ||
103 | static const struct seq_operations unloaded_tainted_modules_seq_ops = { | |
104 | .start = unloaded_tainted_modules_seq_start, | |
105 | .next = unloaded_tainted_modules_seq_next, | |
106 | .stop = unloaded_tainted_modules_seq_stop, | |
107 | .show = unloaded_tainted_modules_seq_show, | |
108 | }; | |
109 | ||
110 | static int unloaded_tainted_modules_open(struct inode *inode, struct file *file) | |
111 | { | |
112 | return seq_open(file, &unloaded_tainted_modules_seq_ops); | |
113 | } | |
114 | ||
115 | static const struct file_operations unloaded_tainted_modules_fops = { | |
116 | .open = unloaded_tainted_modules_open, | |
117 | .read = seq_read, | |
118 | .llseek = seq_lseek, | |
119 | .release = seq_release, | |
120 | }; | |
121 | ||
122 | static int __init unloaded_tainted_modules_init(void) | |
123 | { | |
df3e764d | 124 | debugfs_create_file("unloaded_tainted", 0444, mod_debugfs_root, NULL, |
beef988c | 125 | &unloaded_tainted_modules_fops); |
beef988c AT |
126 | return 0; |
127 | } | |
128 | module_init(unloaded_tainted_modules_init); | |
129 | #endif /* CONFIG_DEBUG_FS */ |