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