xref: /openbmc/linux/drivers/misc/lkdtm/core.c (revision dd21bfa4)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Linux Kernel Dump Test Module for testing kernel crashes conditions:
4  * induces system failures at predefined crashpoints and under predefined
5  * operational conditions in order to evaluate the reliability of kernel
6  * sanity checking and crash dumps obtained using different dumping
7  * solutions.
8  *
9  * Copyright (C) IBM Corporation, 2006
10  *
11  * Author: Ankita Garg <ankita@in.ibm.com>
12  *
13  * It is adapted from the Linux Kernel Dump Test Tool by
14  * Fernando Luis Vazquez Cao <http://lkdtt.sourceforge.net>
15  *
16  * Debugfs support added by Simon Kagstrom <simon.kagstrom@netinsight.net>
17  *
18  * See Documentation/fault-injection/provoke-crashes.rst for instructions
19  */
20 #include "lkdtm.h"
21 #include <linux/fs.h>
22 #include <linux/module.h>
23 #include <linux/buffer_head.h>
24 #include <linux/kprobes.h>
25 #include <linux/list.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/debugfs.h>
29 #include <linux/utsname.h>
30 
31 #define DEFAULT_COUNT 10
32 
33 static int lkdtm_debugfs_open(struct inode *inode, struct file *file);
34 static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
35 		size_t count, loff_t *off);
36 static ssize_t direct_entry(struct file *f, const char __user *user_buf,
37 			    size_t count, loff_t *off);
38 
39 #ifdef CONFIG_KPROBES
40 static int lkdtm_kprobe_handler(struct kprobe *kp, struct pt_regs *regs);
41 static ssize_t lkdtm_debugfs_entry(struct file *f,
42 				   const char __user *user_buf,
43 				   size_t count, loff_t *off);
44 # define CRASHPOINT_KPROBE(_symbol)				\
45 		.kprobe = {					\
46 			.symbol_name = (_symbol),		\
47 			.pre_handler = lkdtm_kprobe_handler,	\
48 		},
49 # define CRASHPOINT_WRITE(_symbol)				\
50 		(_symbol) ? lkdtm_debugfs_entry : direct_entry
51 #else
52 # define CRASHPOINT_KPROBE(_symbol)
53 # define CRASHPOINT_WRITE(_symbol)		direct_entry
54 #endif
55 
56 /* Crash points */
57 struct crashpoint {
58 	const char *name;
59 	const struct file_operations fops;
60 	struct kprobe kprobe;
61 };
62 
63 #define CRASHPOINT(_name, _symbol)				\
64 	{							\
65 		.name = _name,					\
66 		.fops = {					\
67 			.read	= lkdtm_debugfs_read,		\
68 			.llseek	= generic_file_llseek,		\
69 			.open	= lkdtm_debugfs_open,		\
70 			.write	= CRASHPOINT_WRITE(_symbol)	\
71 		},						\
72 		CRASHPOINT_KPROBE(_symbol)			\
73 	}
74 
75 /* Define the possible places where we can trigger a crash point. */
76 static struct crashpoint crashpoints[] = {
77 	CRASHPOINT("DIRECT",		 NULL),
78 #ifdef CONFIG_KPROBES
79 	CRASHPOINT("INT_HARDWARE_ENTRY", "do_IRQ"),
80 	CRASHPOINT("INT_HW_IRQ_EN",	 "handle_irq_event"),
81 	CRASHPOINT("INT_TASKLET_ENTRY",	 "tasklet_action"),
82 	CRASHPOINT("FS_DEVRW",		 "ll_rw_block"),
83 	CRASHPOINT("MEM_SWAPOUT",	 "shrink_inactive_list"),
84 	CRASHPOINT("TIMERADD",		 "hrtimer_start"),
85 	CRASHPOINT("SCSI_QUEUE_RQ",	 "scsi_queue_rq"),
86 #endif
87 };
88 
89 
90 /* Crash types. */
91 struct crashtype {
92 	const char *name;
93 	void (*func)(void);
94 };
95 
96 #define CRASHTYPE(_name)			\
97 	{					\
98 		.name = __stringify(_name),	\
99 		.func = lkdtm_ ## _name,	\
100 	}
101 
102 /* Define the possible types of crashes that can be triggered. */
103 static const struct crashtype crashtypes[] = {
104 	CRASHTYPE(PANIC),
105 	CRASHTYPE(BUG),
106 	CRASHTYPE(WARNING),
107 	CRASHTYPE(WARNING_MESSAGE),
108 	CRASHTYPE(EXCEPTION),
109 	CRASHTYPE(LOOP),
110 	CRASHTYPE(EXHAUST_STACK),
111 	CRASHTYPE(CORRUPT_STACK),
112 	CRASHTYPE(CORRUPT_STACK_STRONG),
113 	CRASHTYPE(REPORT_STACK),
114 	CRASHTYPE(REPORT_STACK_CANARY),
115 	CRASHTYPE(CORRUPT_LIST_ADD),
116 	CRASHTYPE(CORRUPT_LIST_DEL),
117 	CRASHTYPE(STACK_GUARD_PAGE_LEADING),
118 	CRASHTYPE(STACK_GUARD_PAGE_TRAILING),
119 	CRASHTYPE(UNSET_SMEP),
120 	CRASHTYPE(CORRUPT_PAC),
121 	CRASHTYPE(UNALIGNED_LOAD_STORE_WRITE),
122 	CRASHTYPE(SLAB_LINEAR_OVERFLOW),
123 	CRASHTYPE(VMALLOC_LINEAR_OVERFLOW),
124 	CRASHTYPE(WRITE_AFTER_FREE),
125 	CRASHTYPE(READ_AFTER_FREE),
126 	CRASHTYPE(WRITE_BUDDY_AFTER_FREE),
127 	CRASHTYPE(READ_BUDDY_AFTER_FREE),
128 	CRASHTYPE(SLAB_INIT_ON_ALLOC),
129 	CRASHTYPE(BUDDY_INIT_ON_ALLOC),
130 	CRASHTYPE(SLAB_FREE_DOUBLE),
131 	CRASHTYPE(SLAB_FREE_CROSS),
132 	CRASHTYPE(SLAB_FREE_PAGE),
133 	CRASHTYPE(SOFTLOCKUP),
134 	CRASHTYPE(HARDLOCKUP),
135 	CRASHTYPE(SPINLOCKUP),
136 	CRASHTYPE(HUNG_TASK),
137 	CRASHTYPE(OVERFLOW_SIGNED),
138 	CRASHTYPE(OVERFLOW_UNSIGNED),
139 	CRASHTYPE(ARRAY_BOUNDS),
140 	CRASHTYPE(EXEC_DATA),
141 	CRASHTYPE(EXEC_STACK),
142 	CRASHTYPE(EXEC_KMALLOC),
143 	CRASHTYPE(EXEC_VMALLOC),
144 	CRASHTYPE(EXEC_RODATA),
145 	CRASHTYPE(EXEC_USERSPACE),
146 	CRASHTYPE(EXEC_NULL),
147 	CRASHTYPE(ACCESS_USERSPACE),
148 	CRASHTYPE(ACCESS_NULL),
149 	CRASHTYPE(WRITE_RO),
150 	CRASHTYPE(WRITE_RO_AFTER_INIT),
151 	CRASHTYPE(WRITE_KERN),
152 	CRASHTYPE(REFCOUNT_INC_OVERFLOW),
153 	CRASHTYPE(REFCOUNT_ADD_OVERFLOW),
154 	CRASHTYPE(REFCOUNT_INC_NOT_ZERO_OVERFLOW),
155 	CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_OVERFLOW),
156 	CRASHTYPE(REFCOUNT_DEC_ZERO),
157 	CRASHTYPE(REFCOUNT_DEC_NEGATIVE),
158 	CRASHTYPE(REFCOUNT_DEC_AND_TEST_NEGATIVE),
159 	CRASHTYPE(REFCOUNT_SUB_AND_TEST_NEGATIVE),
160 	CRASHTYPE(REFCOUNT_INC_ZERO),
161 	CRASHTYPE(REFCOUNT_ADD_ZERO),
162 	CRASHTYPE(REFCOUNT_INC_SATURATED),
163 	CRASHTYPE(REFCOUNT_DEC_SATURATED),
164 	CRASHTYPE(REFCOUNT_ADD_SATURATED),
165 	CRASHTYPE(REFCOUNT_INC_NOT_ZERO_SATURATED),
166 	CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_SATURATED),
167 	CRASHTYPE(REFCOUNT_DEC_AND_TEST_SATURATED),
168 	CRASHTYPE(REFCOUNT_SUB_AND_TEST_SATURATED),
169 	CRASHTYPE(REFCOUNT_TIMING),
170 	CRASHTYPE(ATOMIC_TIMING),
171 	CRASHTYPE(USERCOPY_HEAP_SIZE_TO),
172 	CRASHTYPE(USERCOPY_HEAP_SIZE_FROM),
173 	CRASHTYPE(USERCOPY_HEAP_WHITELIST_TO),
174 	CRASHTYPE(USERCOPY_HEAP_WHITELIST_FROM),
175 	CRASHTYPE(USERCOPY_STACK_FRAME_TO),
176 	CRASHTYPE(USERCOPY_STACK_FRAME_FROM),
177 	CRASHTYPE(USERCOPY_STACK_BEYOND),
178 	CRASHTYPE(USERCOPY_KERNEL),
179 	CRASHTYPE(STACKLEAK_ERASING),
180 	CRASHTYPE(CFI_FORWARD_PROTO),
181 	CRASHTYPE(FORTIFIED_OBJECT),
182 	CRASHTYPE(FORTIFIED_SUBOBJECT),
183 	CRASHTYPE(FORTIFIED_STRSCPY),
184 	CRASHTYPE(DOUBLE_FAULT),
185 #ifdef CONFIG_PPC_64S_HASH_MMU
186 	CRASHTYPE(PPC_SLB_MULTIHIT),
187 #endif
188 };
189 
190 
191 /* Global kprobe entry and crashtype. */
192 static struct kprobe *lkdtm_kprobe;
193 static struct crashpoint *lkdtm_crashpoint;
194 static const struct crashtype *lkdtm_crashtype;
195 
196 /* Module parameters */
197 static int recur_count = -1;
198 module_param(recur_count, int, 0644);
199 MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
200 
201 static char* cpoint_name;
202 module_param(cpoint_name, charp, 0444);
203 MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
204 
205 static char* cpoint_type;
206 module_param(cpoint_type, charp, 0444);
207 MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
208 				"hitting the crash point");
209 
210 static int cpoint_count = DEFAULT_COUNT;
211 module_param(cpoint_count, int, 0644);
212 MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
213 				"crash point is to be hit to trigger action");
214 
215 /*
216  * For test debug reporting when CI systems provide terse summaries.
217  * TODO: Remove this once reasonable reporting exists in most CI systems:
218  * https://lore.kernel.org/lkml/CAHk-=wiFvfkoFixTapvvyPMN9pq5G-+Dys2eSyBa1vzDGAO5+A@mail.gmail.com
219  */
220 char *lkdtm_kernel_info;
221 
222 /* Return the crashtype number or NULL if the name is invalid */
223 static const struct crashtype *find_crashtype(const char *name)
224 {
225 	int i;
226 
227 	for (i = 0; i < ARRAY_SIZE(crashtypes); i++) {
228 		if (!strcmp(name, crashtypes[i].name))
229 			return &crashtypes[i];
230 	}
231 
232 	return NULL;
233 }
234 
235 /*
236  * This is forced noinline just so it distinctly shows up in the stackdump
237  * which makes validation of expected lkdtm crashes easier.
238  */
239 static noinline void lkdtm_do_action(const struct crashtype *crashtype)
240 {
241 	if (WARN_ON(!crashtype || !crashtype->func))
242 		return;
243 	crashtype->func();
244 }
245 
246 static int lkdtm_register_cpoint(struct crashpoint *crashpoint,
247 				 const struct crashtype *crashtype)
248 {
249 	int ret;
250 
251 	/* If this doesn't have a symbol, just call immediately. */
252 	if (!crashpoint->kprobe.symbol_name) {
253 		lkdtm_do_action(crashtype);
254 		return 0;
255 	}
256 
257 	if (lkdtm_kprobe != NULL)
258 		unregister_kprobe(lkdtm_kprobe);
259 
260 	lkdtm_crashpoint = crashpoint;
261 	lkdtm_crashtype = crashtype;
262 	lkdtm_kprobe = &crashpoint->kprobe;
263 	ret = register_kprobe(lkdtm_kprobe);
264 	if (ret < 0) {
265 		pr_info("Couldn't register kprobe %s\n",
266 			crashpoint->kprobe.symbol_name);
267 		lkdtm_kprobe = NULL;
268 		lkdtm_crashpoint = NULL;
269 		lkdtm_crashtype = NULL;
270 	}
271 
272 	return ret;
273 }
274 
275 #ifdef CONFIG_KPROBES
276 /* Global crash counter and spinlock. */
277 static int crash_count = DEFAULT_COUNT;
278 static DEFINE_SPINLOCK(crash_count_lock);
279 
280 /* Called by kprobe entry points. */
281 static int lkdtm_kprobe_handler(struct kprobe *kp, struct pt_regs *regs)
282 {
283 	unsigned long flags;
284 	bool do_it = false;
285 
286 	if (WARN_ON(!lkdtm_crashpoint || !lkdtm_crashtype))
287 		return 0;
288 
289 	spin_lock_irqsave(&crash_count_lock, flags);
290 	crash_count--;
291 	pr_info("Crash point %s of type %s hit, trigger in %d rounds\n",
292 		lkdtm_crashpoint->name, lkdtm_crashtype->name, crash_count);
293 
294 	if (crash_count == 0) {
295 		do_it = true;
296 		crash_count = cpoint_count;
297 	}
298 	spin_unlock_irqrestore(&crash_count_lock, flags);
299 
300 	if (do_it)
301 		lkdtm_do_action(lkdtm_crashtype);
302 
303 	return 0;
304 }
305 
306 static ssize_t lkdtm_debugfs_entry(struct file *f,
307 				   const char __user *user_buf,
308 				   size_t count, loff_t *off)
309 {
310 	struct crashpoint *crashpoint = file_inode(f)->i_private;
311 	const struct crashtype *crashtype = NULL;
312 	char *buf;
313 	int err;
314 
315 	if (count >= PAGE_SIZE)
316 		return -EINVAL;
317 
318 	buf = (char *)__get_free_page(GFP_KERNEL);
319 	if (!buf)
320 		return -ENOMEM;
321 	if (copy_from_user(buf, user_buf, count)) {
322 		free_page((unsigned long) buf);
323 		return -EFAULT;
324 	}
325 	/* NULL-terminate and remove enter */
326 	buf[count] = '\0';
327 	strim(buf);
328 
329 	crashtype = find_crashtype(buf);
330 	free_page((unsigned long)buf);
331 
332 	if (!crashtype)
333 		return -EINVAL;
334 
335 	err = lkdtm_register_cpoint(crashpoint, crashtype);
336 	if (err < 0)
337 		return err;
338 
339 	*off += count;
340 
341 	return count;
342 }
343 #endif
344 
345 /* Generic read callback that just prints out the available crash types */
346 static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
347 		size_t count, loff_t *off)
348 {
349 	char *buf;
350 	int i, n, out;
351 
352 	buf = (char *)__get_free_page(GFP_KERNEL);
353 	if (buf == NULL)
354 		return -ENOMEM;
355 
356 	n = scnprintf(buf, PAGE_SIZE, "Available crash types:\n");
357 	for (i = 0; i < ARRAY_SIZE(crashtypes); i++) {
358 		n += scnprintf(buf + n, PAGE_SIZE - n, "%s\n",
359 			      crashtypes[i].name);
360 	}
361 	buf[n] = '\0';
362 
363 	out = simple_read_from_buffer(user_buf, count, off,
364 				      buf, n);
365 	free_page((unsigned long) buf);
366 
367 	return out;
368 }
369 
370 static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
371 {
372 	return 0;
373 }
374 
375 /* Special entry to just crash directly. Available without KPROBEs */
376 static ssize_t direct_entry(struct file *f, const char __user *user_buf,
377 		size_t count, loff_t *off)
378 {
379 	const struct crashtype *crashtype;
380 	char *buf;
381 
382 	if (count >= PAGE_SIZE)
383 		return -EINVAL;
384 	if (count < 1)
385 		return -EINVAL;
386 
387 	buf = (char *)__get_free_page(GFP_KERNEL);
388 	if (!buf)
389 		return -ENOMEM;
390 	if (copy_from_user(buf, user_buf, count)) {
391 		free_page((unsigned long) buf);
392 		return -EFAULT;
393 	}
394 	/* NULL-terminate and remove enter */
395 	buf[count] = '\0';
396 	strim(buf);
397 
398 	crashtype = find_crashtype(buf);
399 	free_page((unsigned long) buf);
400 	if (!crashtype)
401 		return -EINVAL;
402 
403 	pr_info("Performing direct entry %s\n", crashtype->name);
404 	lkdtm_do_action(crashtype);
405 	*off += count;
406 
407 	return count;
408 }
409 
410 #ifndef MODULE
411 /*
412  * To avoid needing to export parse_args(), just don't use this code
413  * when LKDTM is built as a module.
414  */
415 struct check_cmdline_args {
416 	const char *param;
417 	int value;
418 };
419 
420 static int lkdtm_parse_one(char *param, char *val,
421 			   const char *unused, void *arg)
422 {
423 	struct check_cmdline_args *args = arg;
424 
425 	/* short circuit if we already found a value. */
426 	if (args->value != -ESRCH)
427 		return 0;
428 	if (strncmp(param, args->param, strlen(args->param)) == 0) {
429 		bool bool_result;
430 		int ret;
431 
432 		ret = kstrtobool(val, &bool_result);
433 		if (ret == 0)
434 			args->value = bool_result;
435 	}
436 	return 0;
437 }
438 
439 int lkdtm_check_bool_cmdline(const char *param)
440 {
441 	char *command_line;
442 	struct check_cmdline_args args = {
443 		.param = param,
444 		.value = -ESRCH,
445 	};
446 
447 	command_line = kstrdup(saved_command_line, GFP_KERNEL);
448 	if (!command_line)
449 		return -ENOMEM;
450 
451 	parse_args("Setting sysctl args", command_line,
452 		   NULL, 0, -1, -1, &args, lkdtm_parse_one);
453 
454 	kfree(command_line);
455 
456 	return args.value;
457 }
458 #endif
459 
460 static struct dentry *lkdtm_debugfs_root;
461 
462 static int __init lkdtm_module_init(void)
463 {
464 	struct crashpoint *crashpoint = NULL;
465 	const struct crashtype *crashtype = NULL;
466 	int ret;
467 	int i;
468 
469 	/* Neither or both of these need to be set */
470 	if ((cpoint_type || cpoint_name) && !(cpoint_type && cpoint_name)) {
471 		pr_err("Need both cpoint_type and cpoint_name or neither\n");
472 		return -EINVAL;
473 	}
474 
475 	if (cpoint_type) {
476 		crashtype = find_crashtype(cpoint_type);
477 		if (!crashtype) {
478 			pr_err("Unknown crashtype '%s'\n", cpoint_type);
479 			return -EINVAL;
480 		}
481 	}
482 
483 	if (cpoint_name) {
484 		for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
485 			if (!strcmp(cpoint_name, crashpoints[i].name))
486 				crashpoint = &crashpoints[i];
487 		}
488 
489 		/* Refuse unknown crashpoints. */
490 		if (!crashpoint) {
491 			pr_err("Invalid crashpoint %s\n", cpoint_name);
492 			return -EINVAL;
493 		}
494 	}
495 
496 #ifdef CONFIG_KPROBES
497 	/* Set crash count. */
498 	crash_count = cpoint_count;
499 #endif
500 
501 	/* Common initialization. */
502 	lkdtm_kernel_info = kasprintf(GFP_KERNEL, "kernel (%s %s)",
503 				      init_uts_ns.name.release,
504 				      init_uts_ns.name.machine);
505 
506 	/* Handle test-specific initialization. */
507 	lkdtm_bugs_init(&recur_count);
508 	lkdtm_perms_init();
509 	lkdtm_usercopy_init();
510 	lkdtm_heap_init();
511 
512 	/* Register debugfs interface */
513 	lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
514 
515 	/* Install debugfs trigger files. */
516 	for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
517 		struct crashpoint *cur = &crashpoints[i];
518 
519 		debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root, cur,
520 				    &cur->fops);
521 	}
522 
523 	/* Install crashpoint if one was selected. */
524 	if (crashpoint) {
525 		ret = lkdtm_register_cpoint(crashpoint, crashtype);
526 		if (ret < 0) {
527 			pr_info("Invalid crashpoint %s\n", crashpoint->name);
528 			goto out_err;
529 		}
530 		pr_info("Crash point %s of type %s registered\n",
531 			crashpoint->name, cpoint_type);
532 	} else {
533 		pr_info("No crash points registered, enable through debugfs\n");
534 	}
535 
536 	return 0;
537 
538 out_err:
539 	debugfs_remove_recursive(lkdtm_debugfs_root);
540 	return ret;
541 }
542 
543 static void __exit lkdtm_module_exit(void)
544 {
545 	debugfs_remove_recursive(lkdtm_debugfs_root);
546 
547 	/* Handle test-specific clean-up. */
548 	lkdtm_heap_exit();
549 	lkdtm_usercopy_exit();
550 
551 	if (lkdtm_kprobe != NULL)
552 		unregister_kprobe(lkdtm_kprobe);
553 
554 	kfree(lkdtm_kernel_info);
555 
556 	pr_info("Crash point unregistered\n");
557 }
558 
559 module_init(lkdtm_module_init);
560 module_exit(lkdtm_module_exit);
561 
562 MODULE_LICENSE("GPL");
563 MODULE_DESCRIPTION("Kernel crash testing module");
564