~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/fail_function.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /kernel/fail_function.c (Version linux-6.11.5) and /kernel/fail_function.c (Version linux-4.19.322)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 /*                                                  2 /*
  3  * fail_function.c: Function-based error injec      3  * fail_function.c: Function-based error injection
  4  */                                                 4  */
  5 #include <linux/error-injection.h>                  5 #include <linux/error-injection.h>
  6 #include <linux/debugfs.h>                          6 #include <linux/debugfs.h>
  7 #include <linux/fault-inject.h>                     7 #include <linux/fault-inject.h>
  8 #include <linux/kallsyms.h>                         8 #include <linux/kallsyms.h>
  9 #include <linux/kprobes.h>                          9 #include <linux/kprobes.h>
 10 #include <linux/module.h>                          10 #include <linux/module.h>
 11 #include <linux/mutex.h>                           11 #include <linux/mutex.h>
 12 #include <linux/slab.h>                            12 #include <linux/slab.h>
 13 #include <linux/uaccess.h>                         13 #include <linux/uaccess.h>
 14                                                    14 
 15 static int fei_kprobe_handler(struct kprobe *k     15 static int fei_kprobe_handler(struct kprobe *kp, struct pt_regs *regs);
 16                                                    16 
 17 static void fei_post_handler(struct kprobe *kp     17 static void fei_post_handler(struct kprobe *kp, struct pt_regs *regs,
 18                              unsigned long fla     18                              unsigned long flags)
 19 {                                                  19 {
 20         /*                                         20         /*
 21          * A dummy post handler is required to     21          * A dummy post handler is required to prohibit optimizing, because
 22          * jump optimization does not support      22          * jump optimization does not support execution path overriding.
 23          */                                        23          */
 24 }                                                  24 }
 25                                                    25 
 26 struct fei_attr {                                  26 struct fei_attr {
 27         struct list_head list;                     27         struct list_head list;
 28         struct kprobe kp;                          28         struct kprobe kp;
 29         unsigned long retval;                      29         unsigned long retval;
 30 };                                                 30 };
 31 static DEFINE_MUTEX(fei_lock);                     31 static DEFINE_MUTEX(fei_lock);
 32 static LIST_HEAD(fei_attr_list);                   32 static LIST_HEAD(fei_attr_list);
 33 static DECLARE_FAULT_ATTR(fei_fault_attr);         33 static DECLARE_FAULT_ATTR(fei_fault_attr);
 34 static struct dentry *fei_debugfs_dir;             34 static struct dentry *fei_debugfs_dir;
 35                                                    35 
 36 static unsigned long adjust_error_retval(unsig     36 static unsigned long adjust_error_retval(unsigned long addr, unsigned long retv)
 37 {                                                  37 {
 38         switch (get_injectable_error_type(addr     38         switch (get_injectable_error_type(addr)) {
 39         case EI_ETYPE_NULL:                        39         case EI_ETYPE_NULL:
 40                 return 0;                      !!  40                 if (retv != 0)
                                                   >>  41                         return 0;
                                                   >>  42                 break;
 41         case EI_ETYPE_ERRNO:                       43         case EI_ETYPE_ERRNO:
 42                 if (retv < (unsigned long)-MAX     44                 if (retv < (unsigned long)-MAX_ERRNO)
 43                         return (unsigned long)     45                         return (unsigned long)-EINVAL;
 44                 break;                             46                 break;
 45         case EI_ETYPE_ERRNO_NULL:                  47         case EI_ETYPE_ERRNO_NULL:
 46                 if (retv != 0 && retv < (unsig     48                 if (retv != 0 && retv < (unsigned long)-MAX_ERRNO)
 47                         return (unsigned long)     49                         return (unsigned long)-EINVAL;
 48                 break;                             50                 break;
 49         case EI_ETYPE_TRUE:                    << 
 50                 return 1;                      << 
 51         }                                          51         }
 52                                                    52 
 53         return retv;                               53         return retv;
 54 }                                                  54 }
 55                                                    55 
 56 static struct fei_attr *fei_attr_new(const cha     56 static struct fei_attr *fei_attr_new(const char *sym, unsigned long addr)
 57 {                                                  57 {
 58         struct fei_attr *attr;                     58         struct fei_attr *attr;
 59                                                    59 
 60         attr = kzalloc(sizeof(*attr), GFP_KERN     60         attr = kzalloc(sizeof(*attr), GFP_KERNEL);
 61         if (attr) {                                61         if (attr) {
 62                 attr->kp.symbol_name = kstrdup     62                 attr->kp.symbol_name = kstrdup(sym, GFP_KERNEL);
 63                 if (!attr->kp.symbol_name) {       63                 if (!attr->kp.symbol_name) {
 64                         kfree(attr);               64                         kfree(attr);
 65                         return NULL;               65                         return NULL;
 66                 }                                  66                 }
 67                 attr->kp.pre_handler = fei_kpr     67                 attr->kp.pre_handler = fei_kprobe_handler;
 68                 attr->kp.post_handler = fei_po     68                 attr->kp.post_handler = fei_post_handler;
 69                 attr->retval = adjust_error_re     69                 attr->retval = adjust_error_retval(addr, 0);
 70                 INIT_LIST_HEAD(&attr->list);       70                 INIT_LIST_HEAD(&attr->list);
 71         }                                          71         }
 72         return attr;                               72         return attr;
 73 }                                                  73 }
 74                                                    74 
 75 static void fei_attr_free(struct fei_attr *att     75 static void fei_attr_free(struct fei_attr *attr)
 76 {                                                  76 {
 77         if (attr) {                                77         if (attr) {
 78                 kfree(attr->kp.symbol_name);       78                 kfree(attr->kp.symbol_name);
 79                 kfree(attr);                       79                 kfree(attr);
 80         }                                          80         }
 81 }                                                  81 }
 82                                                    82 
 83 static struct fei_attr *fei_attr_lookup(const      83 static struct fei_attr *fei_attr_lookup(const char *sym)
 84 {                                                  84 {
 85         struct fei_attr *attr;                     85         struct fei_attr *attr;
 86                                                    86 
 87         list_for_each_entry(attr, &fei_attr_li     87         list_for_each_entry(attr, &fei_attr_list, list) {
 88                 if (!strcmp(attr->kp.symbol_na     88                 if (!strcmp(attr->kp.symbol_name, sym))
 89                         return attr;               89                         return attr;
 90         }                                          90         }
 91                                                    91 
 92         return NULL;                               92         return NULL;
 93 }                                                  93 }
 94                                                    94 
 95 static bool fei_attr_is_valid(struct fei_attr      95 static bool fei_attr_is_valid(struct fei_attr *_attr)
 96 {                                                  96 {
 97         struct fei_attr *attr;                     97         struct fei_attr *attr;
 98                                                    98 
 99         list_for_each_entry(attr, &fei_attr_li     99         list_for_each_entry(attr, &fei_attr_list, list) {
100                 if (attr == _attr)                100                 if (attr == _attr)
101                         return true;              101                         return true;
102         }                                         102         }
103                                                   103 
104         return false;                             104         return false;
105 }                                                 105 }
106                                                   106 
107 static int fei_retval_set(void *data, u64 val)    107 static int fei_retval_set(void *data, u64 val)
108 {                                                 108 {
109         struct fei_attr *attr = data;             109         struct fei_attr *attr = data;
110         unsigned long retv = (unsigned long)va    110         unsigned long retv = (unsigned long)val;
111         int err = 0;                              111         int err = 0;
112                                                   112 
113         mutex_lock(&fei_lock);                    113         mutex_lock(&fei_lock);
114         /*                                        114         /*
115          * Since this operation can be done af    115          * Since this operation can be done after retval file is removed,
116          * It is safer to check the attr is st    116          * It is safer to check the attr is still valid before accessing
117          * its member.                            117          * its member.
118          */                                       118          */
119         if (!fei_attr_is_valid(attr)) {           119         if (!fei_attr_is_valid(attr)) {
120                 err = -ENOENT;                    120                 err = -ENOENT;
121                 goto out;                         121                 goto out;
122         }                                         122         }
123                                                   123 
124         if (attr->kp.addr) {                      124         if (attr->kp.addr) {
125                 if (adjust_error_retval((unsig    125                 if (adjust_error_retval((unsigned long)attr->kp.addr,
126                                         val) !    126                                         val) != retv)
127                         err = -EINVAL;            127                         err = -EINVAL;
128         }                                         128         }
129         if (!err)                                 129         if (!err)
130                 attr->retval = val;               130                 attr->retval = val;
131 out:                                              131 out:
132         mutex_unlock(&fei_lock);                  132         mutex_unlock(&fei_lock);
133                                                   133 
134         return err;                               134         return err;
135 }                                                 135 }
136                                                   136 
137 static int fei_retval_get(void *data, u64 *val    137 static int fei_retval_get(void *data, u64 *val)
138 {                                                 138 {
139         struct fei_attr *attr = data;             139         struct fei_attr *attr = data;
140         int err = 0;                              140         int err = 0;
141                                                   141 
142         mutex_lock(&fei_lock);                    142         mutex_lock(&fei_lock);
143         /* Here we also validate @attr to ensu    143         /* Here we also validate @attr to ensure it still exists. */
144         if (!fei_attr_is_valid(attr))             144         if (!fei_attr_is_valid(attr))
145                 err = -ENOENT;                    145                 err = -ENOENT;
146         else                                      146         else
147                 *val = attr->retval;              147                 *val = attr->retval;
148         mutex_unlock(&fei_lock);                  148         mutex_unlock(&fei_lock);
149                                                   149 
150         return err;                               150         return err;
151 }                                                 151 }
152 DEFINE_DEBUGFS_ATTRIBUTE(fei_retval_ops, fei_r    152 DEFINE_DEBUGFS_ATTRIBUTE(fei_retval_ops, fei_retval_get, fei_retval_set,
153                          "%llx\n");               153                          "%llx\n");
154                                                   154 
155 static void fei_debugfs_add_attr(struct fei_at !! 155 static int fei_debugfs_add_attr(struct fei_attr *attr)
156 {                                                 156 {
157         struct dentry *dir;                       157         struct dentry *dir;
158                                                   158 
159         dir = debugfs_create_dir(attr->kp.symb    159         dir = debugfs_create_dir(attr->kp.symbol_name, fei_debugfs_dir);
                                                   >> 160         if (!dir)
                                                   >> 161                 return -ENOMEM;
                                                   >> 162 
                                                   >> 163         if (!debugfs_create_file("retval", 0600, dir, attr, &fei_retval_ops)) {
                                                   >> 164                 debugfs_remove_recursive(dir);
                                                   >> 165                 return -ENOMEM;
                                                   >> 166         }
160                                                   167 
161         debugfs_create_file("retval", 0600, di !! 168         return 0;
162 }                                                 169 }
163                                                   170 
164 static void fei_debugfs_remove_attr(struct fei    171 static void fei_debugfs_remove_attr(struct fei_attr *attr)
165 {                                                 172 {
166         debugfs_lookup_and_remove(attr->kp.sym !! 173         struct dentry *dir;
                                                   >> 174 
                                                   >> 175         dir = debugfs_lookup(attr->kp.symbol_name, fei_debugfs_dir);
                                                   >> 176         if (dir)
                                                   >> 177                 debugfs_remove_recursive(dir);
167 }                                                 178 }
168                                                   179 
169 static int fei_kprobe_handler(struct kprobe *k    180 static int fei_kprobe_handler(struct kprobe *kp, struct pt_regs *regs)
170 {                                                 181 {
171         struct fei_attr *attr = container_of(k    182         struct fei_attr *attr = container_of(kp, struct fei_attr, kp);
172                                                   183 
173         if (should_fail(&fei_fault_attr, 1)) {    184         if (should_fail(&fei_fault_attr, 1)) {
174                 regs_set_return_value(regs, at    185                 regs_set_return_value(regs, attr->retval);
175                 override_function_with_return(    186                 override_function_with_return(regs);
176                 return 1;                         187                 return 1;
177         }                                         188         }
178                                                   189 
179         return 0;                                 190         return 0;
180 }                                                 191 }
181 NOKPROBE_SYMBOL(fei_kprobe_handler)               192 NOKPROBE_SYMBOL(fei_kprobe_handler)
182                                                   193 
183 static void *fei_seq_start(struct seq_file *m,    194 static void *fei_seq_start(struct seq_file *m, loff_t *pos)
184 {                                                 195 {
185         mutex_lock(&fei_lock);                    196         mutex_lock(&fei_lock);
186         return seq_list_start(&fei_attr_list,     197         return seq_list_start(&fei_attr_list, *pos);
187 }                                                 198 }
188                                                   199 
189 static void fei_seq_stop(struct seq_file *m, v    200 static void fei_seq_stop(struct seq_file *m, void *v)
190 {                                                 201 {
191         mutex_unlock(&fei_lock);                  202         mutex_unlock(&fei_lock);
192 }                                                 203 }
193                                                   204 
194 static void *fei_seq_next(struct seq_file *m,     205 static void *fei_seq_next(struct seq_file *m, void *v, loff_t *pos)
195 {                                                 206 {
196         return seq_list_next(v, &fei_attr_list    207         return seq_list_next(v, &fei_attr_list, pos);
197 }                                                 208 }
198                                                   209 
199 static int fei_seq_show(struct seq_file *m, vo    210 static int fei_seq_show(struct seq_file *m, void *v)
200 {                                                 211 {
201         struct fei_attr *attr = list_entry(v,     212         struct fei_attr *attr = list_entry(v, struct fei_attr, list);
202                                                   213 
203         seq_printf(m, "%ps\n", attr->kp.addr); !! 214         seq_printf(m, "%pf\n", attr->kp.addr);
204         return 0;                                 215         return 0;
205 }                                                 216 }
206                                                   217 
207 static const struct seq_operations fei_seq_ops    218 static const struct seq_operations fei_seq_ops = {
208         .start  = fei_seq_start,                  219         .start  = fei_seq_start,
209         .next   = fei_seq_next,                   220         .next   = fei_seq_next,
210         .stop   = fei_seq_stop,                   221         .stop   = fei_seq_stop,
211         .show   = fei_seq_show,                   222         .show   = fei_seq_show,
212 };                                                223 };
213                                                   224 
214 static int fei_open(struct inode *inode, struc    225 static int fei_open(struct inode *inode, struct file *file)
215 {                                                 226 {
216         return seq_open(file, &fei_seq_ops);      227         return seq_open(file, &fei_seq_ops);
217 }                                                 228 }
218                                                   229 
219 static void fei_attr_remove(struct fei_attr *a    230 static void fei_attr_remove(struct fei_attr *attr)
220 {                                                 231 {
221         fei_debugfs_remove_attr(attr);            232         fei_debugfs_remove_attr(attr);
222         unregister_kprobe(&attr->kp);             233         unregister_kprobe(&attr->kp);
223         list_del(&attr->list);                    234         list_del(&attr->list);
224         fei_attr_free(attr);                      235         fei_attr_free(attr);
225 }                                                 236 }
226                                                   237 
227 static void fei_attr_remove_all(void)             238 static void fei_attr_remove_all(void)
228 {                                                 239 {
229         struct fei_attr *attr, *n;                240         struct fei_attr *attr, *n;
230                                                   241 
231         list_for_each_entry_safe(attr, n, &fei    242         list_for_each_entry_safe(attr, n, &fei_attr_list, list) {
232                 fei_attr_remove(attr);            243                 fei_attr_remove(attr);
233         }                                         244         }
234 }                                                 245 }
235                                                   246 
236 static ssize_t fei_write(struct file *file, co    247 static ssize_t fei_write(struct file *file, const char __user *buffer,
237                          size_t count, loff_t     248                          size_t count, loff_t *ppos)
238 {                                                 249 {
239         struct fei_attr *attr;                    250         struct fei_attr *attr;
240         unsigned long addr;                       251         unsigned long addr;
241         char *buf, *sym;                          252         char *buf, *sym;
242         int ret;                                  253         int ret;
243                                                   254 
244         /* cut off if it is too long */           255         /* cut off if it is too long */
245         if (count > KSYM_NAME_LEN)                256         if (count > KSYM_NAME_LEN)
246                 count = KSYM_NAME_LEN;            257                 count = KSYM_NAME_LEN;
247                                                !! 258         buf = kmalloc(count + 1, GFP_KERNEL);
248         buf = memdup_user_nul(buffer, count);  !! 259         if (!buf)
249         if (IS_ERR(buf))                       !! 260                 return -ENOMEM;
250                 return PTR_ERR(buf);           !! 261 
251                                                !! 262         if (copy_from_user(buf, buffer, count)) {
                                                   >> 263                 ret = -EFAULT;
                                                   >> 264                 goto out_free;
                                                   >> 265         }
                                                   >> 266         buf[count] = '\0';
252         sym = strstrip(buf);                      267         sym = strstrip(buf);
253                                                   268 
254         mutex_lock(&fei_lock);                    269         mutex_lock(&fei_lock);
255                                                   270 
256         /* Writing just spaces will remove all    271         /* Writing just spaces will remove all injection points */
257         if (sym[0] == '\0') {                     272         if (sym[0] == '\0') {
258                 fei_attr_remove_all();            273                 fei_attr_remove_all();
259                 ret = count;                      274                 ret = count;
260                 goto out;                         275                 goto out;
261         }                                         276         }
262         /* Writing !function will remove one i    277         /* Writing !function will remove one injection point */
263         if (sym[0] == '!') {                      278         if (sym[0] == '!') {
264                 attr = fei_attr_lookup(sym + 1    279                 attr = fei_attr_lookup(sym + 1);
265                 if (!attr) {                      280                 if (!attr) {
266                         ret = -ENOENT;            281                         ret = -ENOENT;
267                         goto out;                 282                         goto out;
268                 }                                 283                 }
269                 fei_attr_remove(attr);            284                 fei_attr_remove(attr);
270                 ret = count;                      285                 ret = count;
271                 goto out;                         286                 goto out;
272         }                                         287         }
273                                                   288 
274         addr = kallsyms_lookup_name(sym);         289         addr = kallsyms_lookup_name(sym);
275         if (!addr) {                              290         if (!addr) {
276                 ret = -EINVAL;                    291                 ret = -EINVAL;
277                 goto out;                         292                 goto out;
278         }                                         293         }
279         if (!within_error_injection_list(addr)    294         if (!within_error_injection_list(addr)) {
280                 ret = -ERANGE;                    295                 ret = -ERANGE;
281                 goto out;                         296                 goto out;
282         }                                         297         }
283         if (fei_attr_lookup(sym)) {               298         if (fei_attr_lookup(sym)) {
284                 ret = -EBUSY;                     299                 ret = -EBUSY;
285                 goto out;                         300                 goto out;
286         }                                         301         }
287         attr = fei_attr_new(sym, addr);           302         attr = fei_attr_new(sym, addr);
288         if (!attr) {                              303         if (!attr) {
289                 ret = -ENOMEM;                    304                 ret = -ENOMEM;
290                 goto out;                         305                 goto out;
291         }                                         306         }
292                                                   307 
293         ret = register_kprobe(&attr->kp);         308         ret = register_kprobe(&attr->kp);
294         if (ret) {                             !! 309         if (!ret)
295                 fei_attr_free(attr);           !! 310                 ret = fei_debugfs_add_attr(attr);
296                 goto out;                      !! 311         if (ret < 0)
                                                   >> 312                 fei_attr_remove(attr);
                                                   >> 313         else {
                                                   >> 314                 list_add_tail(&attr->list, &fei_attr_list);
                                                   >> 315                 ret = count;
297         }                                         316         }
298         fei_debugfs_add_attr(attr);            << 
299         list_add_tail(&attr->list, &fei_attr_l << 
300         ret = count;                           << 
301 out:                                              317 out:
302         mutex_unlock(&fei_lock);                  318         mutex_unlock(&fei_lock);
                                                   >> 319 out_free:
303         kfree(buf);                               320         kfree(buf);
304         return ret;                               321         return ret;
305 }                                                 322 }
306                                                   323 
307 static const struct file_operations fei_ops =     324 static const struct file_operations fei_ops = {
308         .open =         fei_open,                 325         .open =         fei_open,
309         .read =         seq_read,                 326         .read =         seq_read,
310         .write =        fei_write,                327         .write =        fei_write,
311         .llseek =       seq_lseek,                328         .llseek =       seq_lseek,
312         .release =      seq_release,              329         .release =      seq_release,
313 };                                                330 };
314                                                   331 
315 static int __init fei_debugfs_init(void)          332 static int __init fei_debugfs_init(void)
316 {                                                 333 {
317         struct dentry *dir;                       334         struct dentry *dir;
318                                                   335 
319         dir = fault_create_debugfs_attr("fail_    336         dir = fault_create_debugfs_attr("fail_function", NULL,
320                                         &fei_f    337                                         &fei_fault_attr);
321         if (IS_ERR(dir))                          338         if (IS_ERR(dir))
322                 return PTR_ERR(dir);              339                 return PTR_ERR(dir);
323                                                   340 
324         /* injectable attribute is just a syml    341         /* injectable attribute is just a symlink of error_inject/list */
325         debugfs_create_symlink("injectable", d !! 342         if (!debugfs_create_symlink("injectable", dir,
                                                   >> 343                                     "../error_injection/list"))
                                                   >> 344                 goto error;
326                                                   345 
327         debugfs_create_file("inject", 0600, di !! 346         if (!debugfs_create_file("inject", 0600, dir, NULL, &fei_ops))
                                                   >> 347                 goto error;
328                                                   348 
329         fei_debugfs_dir = dir;                    349         fei_debugfs_dir = dir;
330                                                   350 
331         return 0;                                 351         return 0;
                                                   >> 352 error:
                                                   >> 353         debugfs_remove_recursive(dir);
                                                   >> 354         return -ENOMEM;
332 }                                                 355 }
333                                                   356 
334 late_initcall(fei_debugfs_init);                  357 late_initcall(fei_debugfs_init);
335                                                   358 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php