1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Module taint unload tracking support
5 * Copyright (C) 2022 Aaron Tomlin
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/debugfs.h>
14 #include <linux/rculist.h>
17 static LIST_HEAD(unloaded_tainted_modules
);
18 extern struct dentry
*mod_debugfs_root
;
20 int try_add_tainted_module(struct module
*mod
)
22 struct mod_unload_taint
*mod_taint
;
24 module_assert_mutex_or_preempt();
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
) {
38 mod_taint
= kmalloc(sizeof(*mod_taint
), GFP_KERNEL
);
39 if (unlikely(!mod_taint
))
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
);
49 void print_unloaded_tainted_modules(void)
51 struct mod_unload_taint
*mod_taint
;
52 char buf
[MODULE_FLAGS_BUF_SIZE
];
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
,
60 l
= module_flags_taint(mod_taint
->taints
, buf
);
62 pr_cont(" %s(%s):%llu", mod_taint
->name
, buf
,
68 #ifdef CONFIG_DEBUG_FS
69 static void *unloaded_tainted_modules_seq_start(struct seq_file
*m
, loff_t
*pos
)
73 return seq_list_start_rcu(&unloaded_tainted_modules
, *pos
);
76 static void *unloaded_tainted_modules_seq_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
78 return seq_list_next_rcu(p
, &unloaded_tainted_modules
, pos
);
81 static void unloaded_tainted_modules_seq_stop(struct seq_file
*m
, void *p
)
87 static int unloaded_tainted_modules_seq_show(struct seq_file
*m
, void *p
)
89 struct mod_unload_taint
*mod_taint
;
90 char buf
[MODULE_FLAGS_BUF_SIZE
];
93 mod_taint
= list_entry(p
, struct mod_unload_taint
, list
);
94 l
= module_flags_taint(mod_taint
->taints
, buf
);
97 seq_printf(m
, "%s (%s) %llu", mod_taint
->name
, buf
, mod_taint
->count
);
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
,
110 static int unloaded_tainted_modules_open(struct inode
*inode
, struct file
*file
)
112 return seq_open(file
, &unloaded_tainted_modules_seq_ops
);
115 static const struct file_operations unloaded_tainted_modules_fops
= {
116 .open
= unloaded_tainted_modules_open
,
119 .release
= seq_release
,
122 static int __init
unloaded_tainted_modules_init(void)
124 debugfs_create_file("unloaded_tainted", 0444, mod_debugfs_root
, NULL
,
125 &unloaded_tainted_modules_fops
);
128 module_init(unloaded_tainted_modules_init
);
129 #endif /* CONFIG_DEBUG_FS */