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_BOOK3S_64 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 /* For test debug reporting. */ 216 char *lkdtm_kernel_info; 217 218 /* Return the crashtype number or NULL if the name is invalid */ 219 static const struct crashtype *find_crashtype(const char *name) 220 { 221 int i; 222 223 for (i = 0; i < ARRAY_SIZE(crashtypes); i++) { 224 if (!strcmp(name, crashtypes[i].name)) 225 return &crashtypes[i]; 226 } 227 228 return NULL; 229 } 230 231 /* 232 * This is forced noinline just so it distinctly shows up in the stackdump 233 * which makes validation of expected lkdtm crashes easier. 234 */ 235 static noinline void lkdtm_do_action(const struct crashtype *crashtype) 236 { 237 if (WARN_ON(!crashtype || !crashtype->func)) 238 return; 239 crashtype->func(); 240 } 241 242 static int lkdtm_register_cpoint(struct crashpoint *crashpoint, 243 const struct crashtype *crashtype) 244 { 245 int ret; 246 247 /* If this doesn't have a symbol, just call immediately. */ 248 if (!crashpoint->kprobe.symbol_name) { 249 lkdtm_do_action(crashtype); 250 return 0; 251 } 252 253 if (lkdtm_kprobe != NULL) 254 unregister_kprobe(lkdtm_kprobe); 255 256 lkdtm_crashpoint = crashpoint; 257 lkdtm_crashtype = crashtype; 258 lkdtm_kprobe = &crashpoint->kprobe; 259 ret = register_kprobe(lkdtm_kprobe); 260 if (ret < 0) { 261 pr_info("Couldn't register kprobe %s\n", 262 crashpoint->kprobe.symbol_name); 263 lkdtm_kprobe = NULL; 264 lkdtm_crashpoint = NULL; 265 lkdtm_crashtype = NULL; 266 } 267 268 return ret; 269 } 270 271 #ifdef CONFIG_KPROBES 272 /* Global crash counter and spinlock. */ 273 static int crash_count = DEFAULT_COUNT; 274 static DEFINE_SPINLOCK(crash_count_lock); 275 276 /* Called by kprobe entry points. */ 277 static int lkdtm_kprobe_handler(struct kprobe *kp, struct pt_regs *regs) 278 { 279 unsigned long flags; 280 bool do_it = false; 281 282 if (WARN_ON(!lkdtm_crashpoint || !lkdtm_crashtype)) 283 return 0; 284 285 spin_lock_irqsave(&crash_count_lock, flags); 286 crash_count--; 287 pr_info("Crash point %s of type %s hit, trigger in %d rounds\n", 288 lkdtm_crashpoint->name, lkdtm_crashtype->name, crash_count); 289 290 if (crash_count == 0) { 291 do_it = true; 292 crash_count = cpoint_count; 293 } 294 spin_unlock_irqrestore(&crash_count_lock, flags); 295 296 if (do_it) 297 lkdtm_do_action(lkdtm_crashtype); 298 299 return 0; 300 } 301 302 static ssize_t lkdtm_debugfs_entry(struct file *f, 303 const char __user *user_buf, 304 size_t count, loff_t *off) 305 { 306 struct crashpoint *crashpoint = file_inode(f)->i_private; 307 const struct crashtype *crashtype = NULL; 308 char *buf; 309 int err; 310 311 if (count >= PAGE_SIZE) 312 return -EINVAL; 313 314 buf = (char *)__get_free_page(GFP_KERNEL); 315 if (!buf) 316 return -ENOMEM; 317 if (copy_from_user(buf, user_buf, count)) { 318 free_page((unsigned long) buf); 319 return -EFAULT; 320 } 321 /* NULL-terminate and remove enter */ 322 buf[count] = '\0'; 323 strim(buf); 324 325 crashtype = find_crashtype(buf); 326 free_page((unsigned long)buf); 327 328 if (!crashtype) 329 return -EINVAL; 330 331 err = lkdtm_register_cpoint(crashpoint, crashtype); 332 if (err < 0) 333 return err; 334 335 *off += count; 336 337 return count; 338 } 339 #endif 340 341 /* Generic read callback that just prints out the available crash types */ 342 static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf, 343 size_t count, loff_t *off) 344 { 345 char *buf; 346 int i, n, out; 347 348 buf = (char *)__get_free_page(GFP_KERNEL); 349 if (buf == NULL) 350 return -ENOMEM; 351 352 n = scnprintf(buf, PAGE_SIZE, "Available crash types:\n"); 353 for (i = 0; i < ARRAY_SIZE(crashtypes); i++) { 354 n += scnprintf(buf + n, PAGE_SIZE - n, "%s\n", 355 crashtypes[i].name); 356 } 357 buf[n] = '\0'; 358 359 out = simple_read_from_buffer(user_buf, count, off, 360 buf, n); 361 free_page((unsigned long) buf); 362 363 return out; 364 } 365 366 static int lkdtm_debugfs_open(struct inode *inode, struct file *file) 367 { 368 return 0; 369 } 370 371 /* Special entry to just crash directly. Available without KPROBEs */ 372 static ssize_t direct_entry(struct file *f, const char __user *user_buf, 373 size_t count, loff_t *off) 374 { 375 const struct crashtype *crashtype; 376 char *buf; 377 378 if (count >= PAGE_SIZE) 379 return -EINVAL; 380 if (count < 1) 381 return -EINVAL; 382 383 buf = (char *)__get_free_page(GFP_KERNEL); 384 if (!buf) 385 return -ENOMEM; 386 if (copy_from_user(buf, user_buf, count)) { 387 free_page((unsigned long) buf); 388 return -EFAULT; 389 } 390 /* NULL-terminate and remove enter */ 391 buf[count] = '\0'; 392 strim(buf); 393 394 crashtype = find_crashtype(buf); 395 free_page((unsigned long) buf); 396 if (!crashtype) 397 return -EINVAL; 398 399 pr_info("Performing direct entry %s\n", crashtype->name); 400 lkdtm_do_action(crashtype); 401 *off += count; 402 403 return count; 404 } 405 406 #ifndef MODULE 407 /* 408 * To avoid needing to export parse_args(), just don't use this code 409 * when LKDTM is built as a module. 410 */ 411 struct check_cmdline_args { 412 const char *param; 413 int value; 414 }; 415 416 static int lkdtm_parse_one(char *param, char *val, 417 const char *unused, void *arg) 418 { 419 struct check_cmdline_args *args = arg; 420 421 /* short circuit if we already found a value. */ 422 if (args->value != -ESRCH) 423 return 0; 424 if (strncmp(param, args->param, strlen(args->param)) == 0) { 425 bool bool_result; 426 int ret; 427 428 ret = kstrtobool(val, &bool_result); 429 if (ret == 0) 430 args->value = bool_result; 431 } 432 return 0; 433 } 434 435 int lkdtm_check_bool_cmdline(const char *param) 436 { 437 char *command_line; 438 struct check_cmdline_args args = { 439 .param = param, 440 .value = -ESRCH, 441 }; 442 443 command_line = kstrdup(saved_command_line, GFP_KERNEL); 444 if (!command_line) 445 return -ENOMEM; 446 447 parse_args("Setting sysctl args", command_line, 448 NULL, 0, -1, -1, &args, lkdtm_parse_one); 449 450 kfree(command_line); 451 452 return args.value; 453 } 454 #endif 455 456 static struct dentry *lkdtm_debugfs_root; 457 458 static int __init lkdtm_module_init(void) 459 { 460 struct crashpoint *crashpoint = NULL; 461 const struct crashtype *crashtype = NULL; 462 int ret; 463 int i; 464 465 /* Neither or both of these need to be set */ 466 if ((cpoint_type || cpoint_name) && !(cpoint_type && cpoint_name)) { 467 pr_err("Need both cpoint_type and cpoint_name or neither\n"); 468 return -EINVAL; 469 } 470 471 if (cpoint_type) { 472 crashtype = find_crashtype(cpoint_type); 473 if (!crashtype) { 474 pr_err("Unknown crashtype '%s'\n", cpoint_type); 475 return -EINVAL; 476 } 477 } 478 479 if (cpoint_name) { 480 for (i = 0; i < ARRAY_SIZE(crashpoints); i++) { 481 if (!strcmp(cpoint_name, crashpoints[i].name)) 482 crashpoint = &crashpoints[i]; 483 } 484 485 /* Refuse unknown crashpoints. */ 486 if (!crashpoint) { 487 pr_err("Invalid crashpoint %s\n", cpoint_name); 488 return -EINVAL; 489 } 490 } 491 492 #ifdef CONFIG_KPROBES 493 /* Set crash count. */ 494 crash_count = cpoint_count; 495 #endif 496 497 /* Common initialization. */ 498 lkdtm_kernel_info = kasprintf(GFP_KERNEL, "kernel (%s %s)", 499 init_uts_ns.name.release, 500 init_uts_ns.name.machine); 501 502 /* Handle test-specific initialization. */ 503 lkdtm_bugs_init(&recur_count); 504 lkdtm_perms_init(); 505 lkdtm_usercopy_init(); 506 lkdtm_heap_init(); 507 508 /* Register debugfs interface */ 509 lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL); 510 511 /* Install debugfs trigger files. */ 512 for (i = 0; i < ARRAY_SIZE(crashpoints); i++) { 513 struct crashpoint *cur = &crashpoints[i]; 514 515 debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root, cur, 516 &cur->fops); 517 } 518 519 /* Install crashpoint if one was selected. */ 520 if (crashpoint) { 521 ret = lkdtm_register_cpoint(crashpoint, crashtype); 522 if (ret < 0) { 523 pr_info("Invalid crashpoint %s\n", crashpoint->name); 524 goto out_err; 525 } 526 pr_info("Crash point %s of type %s registered\n", 527 crashpoint->name, cpoint_type); 528 } else { 529 pr_info("No crash points registered, enable through debugfs\n"); 530 } 531 532 return 0; 533 534 out_err: 535 debugfs_remove_recursive(lkdtm_debugfs_root); 536 return ret; 537 } 538 539 static void __exit lkdtm_module_exit(void) 540 { 541 debugfs_remove_recursive(lkdtm_debugfs_root); 542 543 /* Handle test-specific clean-up. */ 544 lkdtm_heap_exit(); 545 lkdtm_usercopy_exit(); 546 547 if (lkdtm_kprobe != NULL) 548 unregister_kprobe(lkdtm_kprobe); 549 550 kfree(lkdtm_kernel_info); 551 552 pr_info("Crash point unregistered\n"); 553 } 554 555 module_init(lkdtm_module_init); 556 module_exit(lkdtm_module_exit); 557 558 MODULE_LICENSE("GPL"); 559 MODULE_DESCRIPTION("Kernel crash testing module"); 560