1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 // error-inject.c: Function-level error inject 2 // error-inject.c: Function-level error injection table 3 #include <linux/error-injection.h> 3 #include <linux/error-injection.h> 4 #include <linux/debugfs.h> 4 #include <linux/debugfs.h> 5 #include <linux/kallsyms.h> 5 #include <linux/kallsyms.h> 6 #include <linux/kprobes.h> 6 #include <linux/kprobes.h> 7 #include <linux/module.h> 7 #include <linux/module.h> 8 #include <linux/mutex.h> 8 #include <linux/mutex.h> 9 #include <linux/list.h> 9 #include <linux/list.h> 10 #include <linux/slab.h> 10 #include <linux/slab.h> 11 #include <asm/sections.h> << 12 11 13 /* Whitelist of symbols that can be overridden 12 /* Whitelist of symbols that can be overridden for error injection. */ 14 static LIST_HEAD(error_injection_list); 13 static LIST_HEAD(error_injection_list); 15 static DEFINE_MUTEX(ei_mutex); 14 static DEFINE_MUTEX(ei_mutex); 16 struct ei_entry { 15 struct ei_entry { 17 struct list_head list; 16 struct list_head list; 18 unsigned long start_addr; 17 unsigned long start_addr; 19 unsigned long end_addr; 18 unsigned long end_addr; 20 int etype; 19 int etype; 21 void *priv; 20 void *priv; 22 }; 21 }; 23 22 24 bool within_error_injection_list(unsigned long 23 bool within_error_injection_list(unsigned long addr) 25 { 24 { 26 struct ei_entry *ent; 25 struct ei_entry *ent; 27 bool ret = false; 26 bool ret = false; 28 27 29 mutex_lock(&ei_mutex); 28 mutex_lock(&ei_mutex); 30 list_for_each_entry(ent, &error_inject 29 list_for_each_entry(ent, &error_injection_list, list) { 31 if (addr >= ent->start_addr && 30 if (addr >= ent->start_addr && addr < ent->end_addr) { 32 ret = true; 31 ret = true; 33 break; 32 break; 34 } 33 } 35 } 34 } 36 mutex_unlock(&ei_mutex); 35 mutex_unlock(&ei_mutex); 37 return ret; 36 return ret; 38 } 37 } 39 38 40 int get_injectable_error_type(unsigned long ad 39 int get_injectable_error_type(unsigned long addr) 41 { 40 { 42 struct ei_entry *ent; 41 struct ei_entry *ent; 43 int ei_type = -EINVAL; << 44 42 45 mutex_lock(&ei_mutex); << 46 list_for_each_entry(ent, &error_inject 43 list_for_each_entry(ent, &error_injection_list, list) { 47 if (addr >= ent->start_addr && !! 44 if (addr >= ent->start_addr && addr < ent->end_addr) 48 ei_type = ent->etype; !! 45 return ent->etype; 49 break; << 50 } << 51 } 46 } 52 mutex_unlock(&ei_mutex); !! 47 return EI_ETYPE_NONE; 53 << 54 return ei_type; << 55 } 48 } 56 49 57 /* 50 /* 58 * Lookup and populate the error_injection_lis 51 * Lookup and populate the error_injection_list. 59 * 52 * 60 * For safety reasons we only allow certain fu 53 * For safety reasons we only allow certain functions to be overridden with 61 * bpf_error_injection, so we need to populate 54 * bpf_error_injection, so we need to populate the list of the symbols that have 62 * been marked as safe for overriding. 55 * been marked as safe for overriding. 63 */ 56 */ 64 static void populate_error_injection_list(stru 57 static void populate_error_injection_list(struct error_injection_entry *start, 65 stru 58 struct error_injection_entry *end, 66 void 59 void *priv) 67 { 60 { 68 struct error_injection_entry *iter; 61 struct error_injection_entry *iter; 69 struct ei_entry *ent; 62 struct ei_entry *ent; 70 unsigned long entry, offset = 0, size 63 unsigned long entry, offset = 0, size = 0; 71 64 72 mutex_lock(&ei_mutex); 65 mutex_lock(&ei_mutex); 73 for (iter = start; iter < end; iter++) 66 for (iter = start; iter < end; iter++) { 74 entry = (unsigned long)derefer !! 67 entry = arch_deref_entry_point((void *)iter->addr); 75 68 76 if (!kernel_text_address(entry 69 if (!kernel_text_address(entry) || 77 !kallsyms_lookup_size_offs 70 !kallsyms_lookup_size_offset(entry, &size, &offset)) { 78 pr_err("Failed to find 71 pr_err("Failed to find error inject entry at %p\n", 79 (void *)entry) 72 (void *)entry); 80 continue; 73 continue; 81 } 74 } 82 75 83 ent = kmalloc(sizeof(*ent), GF 76 ent = kmalloc(sizeof(*ent), GFP_KERNEL); 84 if (!ent) 77 if (!ent) 85 break; 78 break; 86 ent->start_addr = entry; 79 ent->start_addr = entry; 87 ent->end_addr = entry + size; 80 ent->end_addr = entry + size; 88 ent->etype = iter->etype; 81 ent->etype = iter->etype; 89 ent->priv = priv; 82 ent->priv = priv; 90 INIT_LIST_HEAD(&ent->list); 83 INIT_LIST_HEAD(&ent->list); 91 list_add_tail(&ent->list, &err 84 list_add_tail(&ent->list, &error_injection_list); 92 } 85 } 93 mutex_unlock(&ei_mutex); 86 mutex_unlock(&ei_mutex); 94 } 87 } 95 88 96 /* Markers of the _error_inject_whitelist sect 89 /* Markers of the _error_inject_whitelist section */ 97 extern struct error_injection_entry __start_er 90 extern struct error_injection_entry __start_error_injection_whitelist[]; 98 extern struct error_injection_entry __stop_err 91 extern struct error_injection_entry __stop_error_injection_whitelist[]; 99 92 100 static void __init populate_kernel_ei_list(voi 93 static void __init populate_kernel_ei_list(void) 101 { 94 { 102 populate_error_injection_list(__start_ 95 populate_error_injection_list(__start_error_injection_whitelist, 103 __stop_e 96 __stop_error_injection_whitelist, 104 NULL); 97 NULL); 105 } 98 } 106 99 107 #ifdef CONFIG_MODULES 100 #ifdef CONFIG_MODULES 108 static void module_load_ei_list(struct module 101 static void module_load_ei_list(struct module *mod) 109 { 102 { 110 if (!mod->num_ei_funcs) 103 if (!mod->num_ei_funcs) 111 return; 104 return; 112 105 113 populate_error_injection_list(mod->ei_ 106 populate_error_injection_list(mod->ei_funcs, 114 mod->ei_ 107 mod->ei_funcs + mod->num_ei_funcs, mod); 115 } 108 } 116 109 117 static void module_unload_ei_list(struct modul 110 static void module_unload_ei_list(struct module *mod) 118 { 111 { 119 struct ei_entry *ent, *n; 112 struct ei_entry *ent, *n; 120 113 121 if (!mod->num_ei_funcs) 114 if (!mod->num_ei_funcs) 122 return; 115 return; 123 116 124 mutex_lock(&ei_mutex); 117 mutex_lock(&ei_mutex); 125 list_for_each_entry_safe(ent, n, &erro 118 list_for_each_entry_safe(ent, n, &error_injection_list, list) { 126 if (ent->priv == mod) { 119 if (ent->priv == mod) { 127 list_del_init(&ent->li 120 list_del_init(&ent->list); 128 kfree(ent); 121 kfree(ent); 129 } 122 } 130 } 123 } 131 mutex_unlock(&ei_mutex); 124 mutex_unlock(&ei_mutex); 132 } 125 } 133 126 134 /* Module notifier call back, checking error i 127 /* Module notifier call back, checking error injection table on the module */ 135 static int ei_module_callback(struct notifier_ 128 static int ei_module_callback(struct notifier_block *nb, 136 unsigned long va 129 unsigned long val, void *data) 137 { 130 { 138 struct module *mod = data; 131 struct module *mod = data; 139 132 140 if (val == MODULE_STATE_COMING) 133 if (val == MODULE_STATE_COMING) 141 module_load_ei_list(mod); 134 module_load_ei_list(mod); 142 else if (val == MODULE_STATE_GOING) 135 else if (val == MODULE_STATE_GOING) 143 module_unload_ei_list(mod); 136 module_unload_ei_list(mod); 144 137 145 return NOTIFY_DONE; 138 return NOTIFY_DONE; 146 } 139 } 147 140 148 static struct notifier_block ei_module_nb = { 141 static struct notifier_block ei_module_nb = { 149 .notifier_call = ei_module_callback, 142 .notifier_call = ei_module_callback, 150 .priority = 0 143 .priority = 0 151 }; 144 }; 152 145 153 static __init int module_ei_init(void) 146 static __init int module_ei_init(void) 154 { 147 { 155 return register_module_notifier(&ei_mo 148 return register_module_notifier(&ei_module_nb); 156 } 149 } 157 #else /* !CONFIG_MODULES */ 150 #else /* !CONFIG_MODULES */ 158 #define module_ei_init() (0) 151 #define module_ei_init() (0) 159 #endif 152 #endif 160 153 161 /* 154 /* 162 * error_injection/whitelist -- shows which fu 155 * error_injection/whitelist -- shows which functions can be overridden for 163 * error injection. 156 * error injection. 164 */ 157 */ 165 static void *ei_seq_start(struct seq_file *m, 158 static void *ei_seq_start(struct seq_file *m, loff_t *pos) 166 { 159 { 167 mutex_lock(&ei_mutex); 160 mutex_lock(&ei_mutex); 168 return seq_list_start(&error_injection 161 return seq_list_start(&error_injection_list, *pos); 169 } 162 } 170 163 171 static void ei_seq_stop(struct seq_file *m, vo 164 static void ei_seq_stop(struct seq_file *m, void *v) 172 { 165 { 173 mutex_unlock(&ei_mutex); 166 mutex_unlock(&ei_mutex); 174 } 167 } 175 168 176 static void *ei_seq_next(struct seq_file *m, v 169 static void *ei_seq_next(struct seq_file *m, void *v, loff_t *pos) 177 { 170 { 178 return seq_list_next(v, &error_injecti 171 return seq_list_next(v, &error_injection_list, pos); 179 } 172 } 180 173 181 static const char *error_type_string(int etype 174 static const char *error_type_string(int etype) 182 { 175 { 183 switch (etype) { 176 switch (etype) { 184 case EI_ETYPE_NULL: 177 case EI_ETYPE_NULL: 185 return "NULL"; 178 return "NULL"; 186 case EI_ETYPE_ERRNO: 179 case EI_ETYPE_ERRNO: 187 return "ERRNO"; 180 return "ERRNO"; 188 case EI_ETYPE_ERRNO_NULL: 181 case EI_ETYPE_ERRNO_NULL: 189 return "ERRNO_NULL"; 182 return "ERRNO_NULL"; 190 case EI_ETYPE_TRUE: << 191 return "TRUE"; << 192 default: 183 default: 193 return "(unknown)"; 184 return "(unknown)"; 194 } 185 } 195 } 186 } 196 187 197 static int ei_seq_show(struct seq_file *m, voi 188 static int ei_seq_show(struct seq_file *m, void *v) 198 { 189 { 199 struct ei_entry *ent = list_entry(v, s 190 struct ei_entry *ent = list_entry(v, struct ei_entry, list); 200 191 201 seq_printf(m, "%ps\t%s\n", (void *)ent !! 192 seq_printf(m, "%pf\t%s\n", (void *)ent->start_addr, 202 error_type_string(ent->etyp 193 error_type_string(ent->etype)); 203 return 0; 194 return 0; 204 } 195 } 205 196 206 static const struct seq_operations ei_sops = { !! 197 static const struct seq_operations ei_seq_ops = { 207 .start = ei_seq_start, 198 .start = ei_seq_start, 208 .next = ei_seq_next, 199 .next = ei_seq_next, 209 .stop = ei_seq_stop, 200 .stop = ei_seq_stop, 210 .show = ei_seq_show, 201 .show = ei_seq_show, 211 }; 202 }; 212 203 213 DEFINE_SEQ_ATTRIBUTE(ei); !! 204 static int ei_open(struct inode *inode, struct file *filp) >> 205 { >> 206 return seq_open(filp, &ei_seq_ops); >> 207 } >> 208 >> 209 static const struct file_operations debugfs_ei_ops = { >> 210 .open = ei_open, >> 211 .read = seq_read, >> 212 .llseek = seq_lseek, >> 213 .release = seq_release, >> 214 }; 214 215 215 static int __init ei_debugfs_init(void) 216 static int __init ei_debugfs_init(void) 216 { 217 { 217 struct dentry *dir, *file; 218 struct dentry *dir, *file; 218 219 219 dir = debugfs_create_dir("error_inject 220 dir = debugfs_create_dir("error_injection", NULL); >> 221 if (!dir) >> 222 return -ENOMEM; 220 223 221 file = debugfs_create_file("list", 044 !! 224 file = debugfs_create_file("list", 0444, dir, NULL, &debugfs_ei_ops); 222 if (!file) { 225 if (!file) { 223 debugfs_remove(dir); 226 debugfs_remove(dir); 224 return -ENOMEM; 227 return -ENOMEM; 225 } 228 } 226 229 227 return 0; 230 return 0; 228 } 231 } 229 232 230 static int __init init_error_injection(void) 233 static int __init init_error_injection(void) 231 { 234 { 232 populate_kernel_ei_list(); 235 populate_kernel_ei_list(); 233 236 234 if (!module_ei_init()) 237 if (!module_ei_init()) 235 ei_debugfs_init(); 238 ei_debugfs_init(); 236 239 237 return 0; 240 return 0; 238 } 241 } 239 late_initcall(init_error_injection); 242 late_initcall(init_error_injection); 240 243
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.