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