core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. #define DEFAULT_COUNT 10
  31. static int lkdtm_debugfs_open(struct inode *inode, struct file *file);
  32. static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
  33. size_t count, loff_t *off);
  34. static ssize_t direct_entry(struct file *f, const char __user *user_buf,
  35. size_t count, loff_t *off);
  36. #ifdef CONFIG_KPROBES
  37. static int lkdtm_kprobe_handler(struct kprobe *kp, struct pt_regs *regs);
  38. static ssize_t lkdtm_debugfs_entry(struct file *f,
  39. const char __user *user_buf,
  40. size_t count, loff_t *off);
  41. # define CRASHPOINT_KPROBE(_symbol) \
  42. .kprobe = { \
  43. .symbol_name = (_symbol), \
  44. .pre_handler = lkdtm_kprobe_handler, \
  45. },
  46. # define CRASHPOINT_WRITE(_symbol) \
  47. (_symbol) ? lkdtm_debugfs_entry : direct_entry
  48. #else
  49. # define CRASHPOINT_KPROBE(_symbol)
  50. # define CRASHPOINT_WRITE(_symbol) direct_entry
  51. #endif
  52. /* Crash points */
  53. struct crashpoint {
  54. const char *name;
  55. const struct file_operations fops;
  56. struct kprobe kprobe;
  57. };
  58. #define CRASHPOINT(_name, _symbol) \
  59. { \
  60. .name = _name, \
  61. .fops = { \
  62. .read = lkdtm_debugfs_read, \
  63. .llseek = generic_file_llseek, \
  64. .open = lkdtm_debugfs_open, \
  65. .write = CRASHPOINT_WRITE(_symbol) \
  66. }, \
  67. CRASHPOINT_KPROBE(_symbol) \
  68. }
  69. /* Define the possible places where we can trigger a crash point. */
  70. static struct crashpoint crashpoints[] = {
  71. CRASHPOINT("DIRECT", NULL),
  72. #ifdef CONFIG_KPROBES
  73. CRASHPOINT("INT_HARDWARE_ENTRY", "do_IRQ"),
  74. CRASHPOINT("INT_HW_IRQ_EN", "handle_irq_event"),
  75. CRASHPOINT("INT_TASKLET_ENTRY", "tasklet_action"),
  76. CRASHPOINT("FS_SUBMIT_BH", "submit_bh"),
  77. CRASHPOINT("MEM_SWAPOUT", "shrink_inactive_list"),
  78. CRASHPOINT("TIMERADD", "hrtimer_start"),
  79. CRASHPOINT("SCSI_QUEUE_RQ", "scsi_queue_rq"),
  80. #endif
  81. };
  82. /* List of possible types for crashes that can be triggered. */
  83. static const struct crashtype_category *crashtype_categories[] = {
  84. &bugs_crashtypes,
  85. &heap_crashtypes,
  86. &perms_crashtypes,
  87. &refcount_crashtypes,
  88. &usercopy_crashtypes,
  89. &stackleak_crashtypes,
  90. &cfi_crashtypes,
  91. &fortify_crashtypes,
  92. #ifdef CONFIG_PPC_64S_HASH_MMU
  93. &powerpc_crashtypes,
  94. #endif
  95. };
  96. /* Global kprobe entry and crashtype. */
  97. static struct kprobe *lkdtm_kprobe;
  98. static struct crashpoint *lkdtm_crashpoint;
  99. static const struct crashtype *lkdtm_crashtype;
  100. /* Module parameters */
  101. static int recur_count = -1;
  102. module_param(recur_count, int, 0644);
  103. MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
  104. static char* cpoint_name;
  105. module_param(cpoint_name, charp, 0444);
  106. MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
  107. static char* cpoint_type;
  108. module_param(cpoint_type, charp, 0444);
  109. MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
  110. "hitting the crash point");
  111. static int cpoint_count = DEFAULT_COUNT;
  112. module_param(cpoint_count, int, 0644);
  113. MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
  114. "crash point is to be hit to trigger action");
  115. /*
  116. * For test debug reporting when CI systems provide terse summaries.
  117. * TODO: Remove this once reasonable reporting exists in most CI systems:
  118. * https://lore.kernel.org/lkml/CAHk-=wiFvfkoFixTapvvyPMN9pq5G-+Dys2eSyBa1vzDGAO5+A@mail.gmail.com
  119. */
  120. char *lkdtm_kernel_info;
  121. /* Return the crashtype number or NULL if the name is invalid */
  122. static const struct crashtype *find_crashtype(const char *name)
  123. {
  124. int cat, idx;
  125. for (cat = 0; cat < ARRAY_SIZE(crashtype_categories); cat++) {
  126. for (idx = 0; idx < crashtype_categories[cat]->len; idx++) {
  127. struct crashtype *crashtype;
  128. crashtype = &crashtype_categories[cat]->crashtypes[idx];
  129. if (!strcmp(name, crashtype->name))
  130. return crashtype;
  131. }
  132. }
  133. return NULL;
  134. }
  135. /*
  136. * This is forced noinline just so it distinctly shows up in the stackdump
  137. * which makes validation of expected lkdtm crashes easier.
  138. *
  139. * NOTE: having a valid return value helps prevent the compiler from doing
  140. * tail call optimizations and taking this out of the stack trace.
  141. */
  142. static noinline int lkdtm_do_action(const struct crashtype *crashtype)
  143. {
  144. if (WARN_ON(!crashtype || !crashtype->func))
  145. return -EINVAL;
  146. crashtype->func();
  147. return 0;
  148. }
  149. static int lkdtm_register_cpoint(struct crashpoint *crashpoint,
  150. const struct crashtype *crashtype)
  151. {
  152. int ret;
  153. /* If this doesn't have a symbol, just call immediately. */
  154. if (!crashpoint->kprobe.symbol_name)
  155. return lkdtm_do_action(crashtype);
  156. if (lkdtm_kprobe != NULL)
  157. unregister_kprobe(lkdtm_kprobe);
  158. lkdtm_crashpoint = crashpoint;
  159. lkdtm_crashtype = crashtype;
  160. lkdtm_kprobe = &crashpoint->kprobe;
  161. ret = register_kprobe(lkdtm_kprobe);
  162. if (ret < 0) {
  163. pr_info("Couldn't register kprobe %s\n",
  164. crashpoint->kprobe.symbol_name);
  165. lkdtm_kprobe = NULL;
  166. lkdtm_crashpoint = NULL;
  167. lkdtm_crashtype = NULL;
  168. }
  169. return ret;
  170. }
  171. #ifdef CONFIG_KPROBES
  172. /* Global crash counter and spinlock. */
  173. static int crash_count = DEFAULT_COUNT;
  174. static DEFINE_SPINLOCK(crash_count_lock);
  175. /* Called by kprobe entry points. */
  176. static int lkdtm_kprobe_handler(struct kprobe *kp, struct pt_regs *regs)
  177. {
  178. unsigned long flags;
  179. bool do_it = false;
  180. if (WARN_ON(!lkdtm_crashpoint || !lkdtm_crashtype))
  181. return 0;
  182. spin_lock_irqsave(&crash_count_lock, flags);
  183. crash_count--;
  184. pr_info("Crash point %s of type %s hit, trigger in %d rounds\n",
  185. lkdtm_crashpoint->name, lkdtm_crashtype->name, crash_count);
  186. if (crash_count == 0) {
  187. do_it = true;
  188. crash_count = cpoint_count;
  189. }
  190. spin_unlock_irqrestore(&crash_count_lock, flags);
  191. if (do_it)
  192. return lkdtm_do_action(lkdtm_crashtype);
  193. return 0;
  194. }
  195. static ssize_t lkdtm_debugfs_entry(struct file *f,
  196. const char __user *user_buf,
  197. size_t count, loff_t *off)
  198. {
  199. struct crashpoint *crashpoint = file_inode(f)->i_private;
  200. const struct crashtype *crashtype = NULL;
  201. char *buf;
  202. int err;
  203. if (count >= PAGE_SIZE)
  204. return -EINVAL;
  205. buf = (char *)__get_free_page(GFP_KERNEL);
  206. if (!buf)
  207. return -ENOMEM;
  208. if (copy_from_user(buf, user_buf, count)) {
  209. free_page((unsigned long) buf);
  210. return -EFAULT;
  211. }
  212. /* NULL-terminate and remove enter */
  213. buf[count] = '\0';
  214. strim(buf);
  215. crashtype = find_crashtype(buf);
  216. free_page((unsigned long)buf);
  217. if (!crashtype)
  218. return -EINVAL;
  219. err = lkdtm_register_cpoint(crashpoint, crashtype);
  220. if (err < 0)
  221. return err;
  222. *off += count;
  223. return count;
  224. }
  225. #endif
  226. /* Generic read callback that just prints out the available crash types */
  227. static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
  228. size_t count, loff_t *off)
  229. {
  230. int n, cat, idx;
  231. ssize_t out;
  232. char *buf;
  233. buf = (char *)__get_free_page(GFP_KERNEL);
  234. if (buf == NULL)
  235. return -ENOMEM;
  236. n = scnprintf(buf, PAGE_SIZE, "Available crash types:\n");
  237. for (cat = 0; cat < ARRAY_SIZE(crashtype_categories); cat++) {
  238. for (idx = 0; idx < crashtype_categories[cat]->len; idx++) {
  239. struct crashtype *crashtype;
  240. crashtype = &crashtype_categories[cat]->crashtypes[idx];
  241. n += scnprintf(buf + n, PAGE_SIZE - n, "%s\n",
  242. crashtype->name);
  243. }
  244. }
  245. buf[n] = '\0';
  246. out = simple_read_from_buffer(user_buf, count, off,
  247. buf, n);
  248. free_page((unsigned long) buf);
  249. return out;
  250. }
  251. static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
  252. {
  253. return 0;
  254. }
  255. /* Special entry to just crash directly. Available without KPROBEs */
  256. static ssize_t direct_entry(struct file *f, const char __user *user_buf,
  257. size_t count, loff_t *off)
  258. {
  259. const struct crashtype *crashtype;
  260. char *buf;
  261. int err;
  262. if (count >= PAGE_SIZE)
  263. return -EINVAL;
  264. if (count < 1)
  265. return -EINVAL;
  266. buf = (char *)__get_free_page(GFP_KERNEL);
  267. if (!buf)
  268. return -ENOMEM;
  269. if (copy_from_user(buf, user_buf, count)) {
  270. free_page((unsigned long) buf);
  271. return -EFAULT;
  272. }
  273. /* NULL-terminate and remove enter */
  274. buf[count] = '\0';
  275. strim(buf);
  276. crashtype = find_crashtype(buf);
  277. free_page((unsigned long) buf);
  278. if (!crashtype)
  279. return -EINVAL;
  280. pr_info("Performing direct entry %s\n", crashtype->name);
  281. err = lkdtm_do_action(crashtype);
  282. *off += count;
  283. if (err)
  284. return err;
  285. return count;
  286. }
  287. #ifndef MODULE
  288. /*
  289. * To avoid needing to export parse_args(), just don't use this code
  290. * when LKDTM is built as a module.
  291. */
  292. struct check_cmdline_args {
  293. const char *param;
  294. int value;
  295. };
  296. static int lkdtm_parse_one(char *param, char *val,
  297. const char *unused, void *arg)
  298. {
  299. struct check_cmdline_args *args = arg;
  300. /* short circuit if we already found a value. */
  301. if (args->value != -ESRCH)
  302. return 0;
  303. if (strncmp(param, args->param, strlen(args->param)) == 0) {
  304. bool bool_result;
  305. int ret;
  306. ret = kstrtobool(val, &bool_result);
  307. if (ret == 0)
  308. args->value = bool_result;
  309. }
  310. return 0;
  311. }
  312. int lkdtm_check_bool_cmdline(const char *param)
  313. {
  314. char *command_line;
  315. struct check_cmdline_args args = {
  316. .param = param,
  317. .value = -ESRCH,
  318. };
  319. command_line = kstrdup(saved_command_line, GFP_KERNEL);
  320. if (!command_line)
  321. return -ENOMEM;
  322. parse_args("Setting sysctl args", command_line,
  323. NULL, 0, -1, -1, &args, lkdtm_parse_one);
  324. kfree(command_line);
  325. return args.value;
  326. }
  327. #endif
  328. static struct dentry *lkdtm_debugfs_root;
  329. static int __init lkdtm_module_init(void)
  330. {
  331. struct crashpoint *crashpoint = NULL;
  332. const struct crashtype *crashtype = NULL;
  333. int ret;
  334. int i;
  335. /* Neither or both of these need to be set */
  336. if ((cpoint_type || cpoint_name) && !(cpoint_type && cpoint_name)) {
  337. pr_err("Need both cpoint_type and cpoint_name or neither\n");
  338. return -EINVAL;
  339. }
  340. if (cpoint_type) {
  341. crashtype = find_crashtype(cpoint_type);
  342. if (!crashtype) {
  343. pr_err("Unknown crashtype '%s'\n", cpoint_type);
  344. return -EINVAL;
  345. }
  346. }
  347. if (cpoint_name) {
  348. for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
  349. if (!strcmp(cpoint_name, crashpoints[i].name))
  350. crashpoint = &crashpoints[i];
  351. }
  352. /* Refuse unknown crashpoints. */
  353. if (!crashpoint) {
  354. pr_err("Invalid crashpoint %s\n", cpoint_name);
  355. return -EINVAL;
  356. }
  357. }
  358. #ifdef CONFIG_KPROBES
  359. /* Set crash count. */
  360. crash_count = cpoint_count;
  361. #endif
  362. /* Common initialization. */
  363. lkdtm_kernel_info = kasprintf(GFP_KERNEL, "kernel (%s %s)",
  364. init_uts_ns.name.release,
  365. init_uts_ns.name.machine);
  366. /* Handle test-specific initialization. */
  367. lkdtm_bugs_init(&recur_count);
  368. lkdtm_perms_init();
  369. lkdtm_usercopy_init();
  370. lkdtm_heap_init();
  371. /* Register debugfs interface */
  372. lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
  373. /* Install debugfs trigger files. */
  374. for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
  375. struct crashpoint *cur = &crashpoints[i];
  376. debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root, cur,
  377. &cur->fops);
  378. }
  379. /* Install crashpoint if one was selected. */
  380. if (crashpoint) {
  381. ret = lkdtm_register_cpoint(crashpoint, crashtype);
  382. if (ret < 0) {
  383. pr_info("Invalid crashpoint %s\n", crashpoint->name);
  384. goto out_err;
  385. }
  386. pr_info("Crash point %s of type %s registered\n",
  387. crashpoint->name, cpoint_type);
  388. } else {
  389. pr_info("No crash points registered, enable through debugfs\n");
  390. }
  391. return 0;
  392. out_err:
  393. debugfs_remove_recursive(lkdtm_debugfs_root);
  394. return ret;
  395. }
  396. static void __exit lkdtm_module_exit(void)
  397. {
  398. debugfs_remove_recursive(lkdtm_debugfs_root);
  399. /* Handle test-specific clean-up. */
  400. lkdtm_heap_exit();
  401. lkdtm_usercopy_exit();
  402. if (lkdtm_kprobe != NULL)
  403. unregister_kprobe(lkdtm_kprobe);
  404. kfree(lkdtm_kernel_info);
  405. pr_info("Crash point unregistered\n");
  406. }
  407. module_init(lkdtm_module_init);
  408. module_exit(lkdtm_module_exit);
  409. MODULE_LICENSE("GPL");
  410. MODULE_DESCRIPTION("Kernel crash testing module");