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