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