lkdtm_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Linux Kernel Dump Test Module for testing kernel crashes conditions:
  3. * induces system failures at predefined crashpoints and under predefined
  4. * operational conditions in order to evaluate the reliability of kernel
  5. * sanity checking and crash dumps obtained using different dumping
  6. * solutions.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. *
  22. * Copyright (C) IBM Corporation, 2006
  23. *
  24. * Author: Ankita Garg <ankita@in.ibm.com>
  25. *
  26. * It is adapted from the Linux Kernel Dump Test Tool by
  27. * Fernando Luis Vazquez Cao <http://lkdtt.sourceforge.net>
  28. *
  29. * Debugfs support added by Simon Kagstrom <simon.kagstrom@netinsight.net>
  30. *
  31. * See Documentation/fault-injection/provoke-crashes.txt for instructions
  32. */
  33. #include "lkdtm.h"
  34. #include <linux/fs.h>
  35. #include <linux/module.h>
  36. #include <linux/buffer_head.h>
  37. #include <linux/kprobes.h>
  38. #include <linux/list.h>
  39. #include <linux/init.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/hrtimer.h>
  42. #include <linux/slab.h>
  43. #include <scsi/scsi_cmnd.h>
  44. #include <linux/debugfs.h>
  45. #ifdef CONFIG_IDE
  46. #include <linux/ide.h>
  47. #endif
  48. #define DEFAULT_COUNT 10
  49. static int lkdtm_debugfs_open(struct inode *inode, struct file *file);
  50. static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
  51. size_t count, loff_t *off);
  52. static ssize_t direct_entry(struct file *f, const char __user *user_buf,
  53. size_t count, loff_t *off);
  54. #ifdef CONFIG_KPROBES
  55. static int lkdtm_kprobe_handler(struct kprobe *kp, struct pt_regs *regs);
  56. static ssize_t lkdtm_debugfs_entry(struct file *f,
  57. const char __user *user_buf,
  58. size_t count, loff_t *off);
  59. # define CRASHPOINT_KPROBE(_symbol) \
  60. .kprobe = { \
  61. .symbol_name = (_symbol), \
  62. .pre_handler = lkdtm_kprobe_handler, \
  63. },
  64. # define CRASHPOINT_WRITE(_symbol) \
  65. (_symbol) ? lkdtm_debugfs_entry : direct_entry
  66. #else
  67. # define CRASHPOINT_KPROBE(_symbol)
  68. # define CRASHPOINT_WRITE(_symbol) direct_entry
  69. #endif
  70. /* Crash points */
  71. struct crashpoint {
  72. const char *name;
  73. const struct file_operations fops;
  74. struct kprobe kprobe;
  75. };
  76. #define CRASHPOINT(_name, _symbol) \
  77. { \
  78. .name = _name, \
  79. .fops = { \
  80. .read = lkdtm_debugfs_read, \
  81. .llseek = generic_file_llseek, \
  82. .open = lkdtm_debugfs_open, \
  83. .write = CRASHPOINT_WRITE(_symbol) \
  84. }, \
  85. CRASHPOINT_KPROBE(_symbol) \
  86. }
  87. /* Define the possible places where we can trigger a crash point. */
  88. static struct crashpoint crashpoints[] = {
  89. CRASHPOINT("DIRECT", NULL),
  90. #ifdef CONFIG_KPROBES
  91. CRASHPOINT("INT_HARDWARE_ENTRY", "do_IRQ"),
  92. CRASHPOINT("INT_HW_IRQ_EN", "handle_IRQ_event"),
  93. CRASHPOINT("INT_TASKLET_ENTRY", "tasklet_action"),
  94. CRASHPOINT("FS_DEVRW", "ll_rw_block"),
  95. CRASHPOINT("MEM_SWAPOUT", "shrink_inactive_list"),
  96. CRASHPOINT("TIMERADD", "hrtimer_start"),
  97. CRASHPOINT("SCSI_DISPATCH_CMD", "scsi_dispatch_cmd"),
  98. # ifdef CONFIG_IDE
  99. CRASHPOINT("IDE_CORE_CP", "generic_ide_ioctl"),
  100. # endif
  101. #endif
  102. };
  103. /* Crash types. */
  104. struct crashtype {
  105. const char *name;
  106. void (*func)(void);
  107. };
  108. #define CRASHTYPE(_name) \
  109. { \
  110. .name = __stringify(_name), \
  111. .func = lkdtm_ ## _name, \
  112. }
  113. /* Define the possible types of crashes that can be triggered. */
  114. static const struct crashtype crashtypes[] = {
  115. CRASHTYPE(PANIC),
  116. CRASHTYPE(BUG),
  117. CRASHTYPE(WARNING),
  118. CRASHTYPE(EXCEPTION),
  119. CRASHTYPE(LOOP),
  120. CRASHTYPE(OVERFLOW),
  121. CRASHTYPE(CORRUPT_LIST_ADD),
  122. CRASHTYPE(CORRUPT_LIST_DEL),
  123. CRASHTYPE(CORRUPT_USER_DS),
  124. CRASHTYPE(CORRUPT_STACK),
  125. CRASHTYPE(CORRUPT_STACK_STRONG),
  126. CRASHTYPE(STACK_GUARD_PAGE_LEADING),
  127. CRASHTYPE(STACK_GUARD_PAGE_TRAILING),
  128. CRASHTYPE(UNALIGNED_LOAD_STORE_WRITE),
  129. CRASHTYPE(OVERWRITE_ALLOCATION),
  130. CRASHTYPE(WRITE_AFTER_FREE),
  131. CRASHTYPE(READ_AFTER_FREE),
  132. CRASHTYPE(WRITE_BUDDY_AFTER_FREE),
  133. CRASHTYPE(READ_BUDDY_AFTER_FREE),
  134. CRASHTYPE(SOFTLOCKUP),
  135. CRASHTYPE(HARDLOCKUP),
  136. CRASHTYPE(SPINLOCKUP),
  137. CRASHTYPE(HUNG_TASK),
  138. CRASHTYPE(EXEC_DATA),
  139. CRASHTYPE(EXEC_STACK),
  140. CRASHTYPE(EXEC_KMALLOC),
  141. CRASHTYPE(EXEC_VMALLOC),
  142. CRASHTYPE(EXEC_RODATA),
  143. CRASHTYPE(EXEC_USERSPACE),
  144. CRASHTYPE(ACCESS_USERSPACE),
  145. CRASHTYPE(WRITE_RO),
  146. CRASHTYPE(WRITE_RO_AFTER_INIT),
  147. CRASHTYPE(WRITE_KERN),
  148. CRASHTYPE(REFCOUNT_INC_OVERFLOW),
  149. CRASHTYPE(REFCOUNT_ADD_OVERFLOW),
  150. CRASHTYPE(REFCOUNT_INC_NOT_ZERO_OVERFLOW),
  151. CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_OVERFLOW),
  152. CRASHTYPE(REFCOUNT_DEC_ZERO),
  153. CRASHTYPE(REFCOUNT_DEC_NEGATIVE),
  154. CRASHTYPE(REFCOUNT_DEC_AND_TEST_NEGATIVE),
  155. CRASHTYPE(REFCOUNT_SUB_AND_TEST_NEGATIVE),
  156. CRASHTYPE(REFCOUNT_INC_ZERO),
  157. CRASHTYPE(REFCOUNT_ADD_ZERO),
  158. CRASHTYPE(REFCOUNT_INC_SATURATED),
  159. CRASHTYPE(REFCOUNT_DEC_SATURATED),
  160. CRASHTYPE(REFCOUNT_ADD_SATURATED),
  161. CRASHTYPE(REFCOUNT_INC_NOT_ZERO_SATURATED),
  162. CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_SATURATED),
  163. CRASHTYPE(REFCOUNT_DEC_AND_TEST_SATURATED),
  164. CRASHTYPE(REFCOUNT_SUB_AND_TEST_SATURATED),
  165. CRASHTYPE(REFCOUNT_TIMING),
  166. CRASHTYPE(ATOMIC_TIMING),
  167. CRASHTYPE(USERCOPY_HEAP_SIZE_TO),
  168. CRASHTYPE(USERCOPY_HEAP_SIZE_FROM),
  169. CRASHTYPE(USERCOPY_HEAP_FLAG_TO),
  170. CRASHTYPE(USERCOPY_HEAP_FLAG_FROM),
  171. CRASHTYPE(USERCOPY_STACK_FRAME_TO),
  172. CRASHTYPE(USERCOPY_STACK_FRAME_FROM),
  173. CRASHTYPE(USERCOPY_STACK_BEYOND),
  174. CRASHTYPE(USERCOPY_KERNEL),
  175. };
  176. /* Global kprobe entry and crashtype. */
  177. static struct kprobe *lkdtm_kprobe;
  178. static struct crashpoint *lkdtm_crashpoint;
  179. static const struct crashtype *lkdtm_crashtype;
  180. /* Module parameters */
  181. static int recur_count = -1;
  182. module_param(recur_count, int, 0644);
  183. MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
  184. static char* cpoint_name;
  185. module_param(cpoint_name, charp, 0444);
  186. MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
  187. static char* cpoint_type;
  188. module_param(cpoint_type, charp, 0444);
  189. MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
  190. "hitting the crash point");
  191. static int cpoint_count = DEFAULT_COUNT;
  192. module_param(cpoint_count, int, 0644);
  193. MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
  194. "crash point is to be hit to trigger action");
  195. /* Return the crashtype number or NULL if the name is invalid */
  196. static const struct crashtype *find_crashtype(const char *name)
  197. {
  198. int i;
  199. for (i = 0; i < ARRAY_SIZE(crashtypes); i++) {
  200. if (!strcmp(name, crashtypes[i].name))
  201. return &crashtypes[i];
  202. }
  203. return NULL;
  204. }
  205. /*
  206. * This is forced noinline just so it distinctly shows up in the stackdump
  207. * which makes validation of expected lkdtm crashes easier.
  208. */
  209. static noinline void lkdtm_do_action(const struct crashtype *crashtype)
  210. {
  211. if (WARN_ON(!crashtype || !crashtype->func))
  212. return;
  213. crashtype->func();
  214. }
  215. static int lkdtm_register_cpoint(struct crashpoint *crashpoint,
  216. const struct crashtype *crashtype)
  217. {
  218. int ret;
  219. /* If this doesn't have a symbol, just call immediately. */
  220. if (!crashpoint->kprobe.symbol_name) {
  221. lkdtm_do_action(crashtype);
  222. return 0;
  223. }
  224. if (lkdtm_kprobe != NULL)
  225. unregister_kprobe(lkdtm_kprobe);
  226. lkdtm_crashpoint = crashpoint;
  227. lkdtm_crashtype = crashtype;
  228. lkdtm_kprobe = &crashpoint->kprobe;
  229. ret = register_kprobe(lkdtm_kprobe);
  230. if (ret < 0) {
  231. pr_info("Couldn't register kprobe %s\n",
  232. crashpoint->kprobe.symbol_name);
  233. lkdtm_kprobe = NULL;
  234. lkdtm_crashpoint = NULL;
  235. lkdtm_crashtype = NULL;
  236. }
  237. return ret;
  238. }
  239. #ifdef CONFIG_KPROBES
  240. /* Global crash counter and spinlock. */
  241. static int crash_count = DEFAULT_COUNT;
  242. static DEFINE_SPINLOCK(crash_count_lock);
  243. /* Called by kprobe entry points. */
  244. static int lkdtm_kprobe_handler(struct kprobe *kp, struct pt_regs *regs)
  245. {
  246. unsigned long flags;
  247. bool do_it = false;
  248. if (WARN_ON(!lkdtm_crashpoint || !lkdtm_crashtype))
  249. return 0;
  250. spin_lock_irqsave(&crash_count_lock, flags);
  251. crash_count--;
  252. pr_info("Crash point %s of type %s hit, trigger in %d rounds\n",
  253. lkdtm_crashpoint->name, lkdtm_crashtype->name, crash_count);
  254. if (crash_count == 0) {
  255. do_it = true;
  256. crash_count = cpoint_count;
  257. }
  258. spin_unlock_irqrestore(&crash_count_lock, flags);
  259. if (do_it)
  260. lkdtm_do_action(lkdtm_crashtype);
  261. return 0;
  262. }
  263. static ssize_t lkdtm_debugfs_entry(struct file *f,
  264. const char __user *user_buf,
  265. size_t count, loff_t *off)
  266. {
  267. struct crashpoint *crashpoint = file_inode(f)->i_private;
  268. const struct crashtype *crashtype = NULL;
  269. char *buf;
  270. int err;
  271. if (count >= PAGE_SIZE)
  272. return -EINVAL;
  273. buf = (char *)__get_free_page(GFP_KERNEL);
  274. if (!buf)
  275. return -ENOMEM;
  276. if (copy_from_user(buf, user_buf, count)) {
  277. free_page((unsigned long) buf);
  278. return -EFAULT;
  279. }
  280. /* NULL-terminate and remove enter */
  281. buf[count] = '\0';
  282. strim(buf);
  283. crashtype = find_crashtype(buf);
  284. free_page((unsigned long)buf);
  285. if (!crashtype)
  286. return -EINVAL;
  287. err = lkdtm_register_cpoint(crashpoint, crashtype);
  288. if (err < 0)
  289. return err;
  290. *off += count;
  291. return count;
  292. }
  293. #endif
  294. /* Generic read callback that just prints out the available crash types */
  295. static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
  296. size_t count, loff_t *off)
  297. {
  298. char *buf;
  299. int i, n, out;
  300. buf = (char *)__get_free_page(GFP_KERNEL);
  301. if (buf == NULL)
  302. return -ENOMEM;
  303. n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
  304. for (i = 0; i < ARRAY_SIZE(crashtypes); i++) {
  305. n += snprintf(buf + n, PAGE_SIZE - n, "%s\n",
  306. crashtypes[i].name);
  307. }
  308. buf[n] = '\0';
  309. out = simple_read_from_buffer(user_buf, count, off,
  310. buf, n);
  311. free_page((unsigned long) buf);
  312. return out;
  313. }
  314. static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
  315. {
  316. return 0;
  317. }
  318. /* Special entry to just crash directly. Available without KPROBEs */
  319. static ssize_t direct_entry(struct file *f, const char __user *user_buf,
  320. size_t count, loff_t *off)
  321. {
  322. const struct crashtype *crashtype;
  323. char *buf;
  324. if (count >= PAGE_SIZE)
  325. return -EINVAL;
  326. if (count < 1)
  327. return -EINVAL;
  328. buf = (char *)__get_free_page(GFP_KERNEL);
  329. if (!buf)
  330. return -ENOMEM;
  331. if (copy_from_user(buf, user_buf, count)) {
  332. free_page((unsigned long) buf);
  333. return -EFAULT;
  334. }
  335. /* NULL-terminate and remove enter */
  336. buf[count] = '\0';
  337. strim(buf);
  338. crashtype = find_crashtype(buf);
  339. free_page((unsigned long) buf);
  340. if (!crashtype)
  341. return -EINVAL;
  342. pr_info("Performing direct entry %s\n", crashtype->name);
  343. lkdtm_do_action(crashtype);
  344. *off += count;
  345. return count;
  346. }
  347. static struct dentry *lkdtm_debugfs_root;
  348. static int __init lkdtm_module_init(void)
  349. {
  350. struct crashpoint *crashpoint = NULL;
  351. const struct crashtype *crashtype = NULL;
  352. int ret = -EINVAL;
  353. int i;
  354. /* Neither or both of these need to be set */
  355. if ((cpoint_type || cpoint_name) && !(cpoint_type && cpoint_name)) {
  356. pr_err("Need both cpoint_type and cpoint_name or neither\n");
  357. return -EINVAL;
  358. }
  359. if (cpoint_type) {
  360. crashtype = find_crashtype(cpoint_type);
  361. if (!crashtype) {
  362. pr_err("Unknown crashtype '%s'\n", cpoint_type);
  363. return -EINVAL;
  364. }
  365. }
  366. if (cpoint_name) {
  367. for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
  368. if (!strcmp(cpoint_name, crashpoints[i].name))
  369. crashpoint = &crashpoints[i];
  370. }
  371. /* Refuse unknown crashpoints. */
  372. if (!crashpoint) {
  373. pr_err("Invalid crashpoint %s\n", cpoint_name);
  374. return -EINVAL;
  375. }
  376. }
  377. #ifdef CONFIG_KPROBES
  378. /* Set crash count. */
  379. crash_count = cpoint_count;
  380. #endif
  381. /* Handle test-specific initialization. */
  382. lkdtm_bugs_init(&recur_count);
  383. lkdtm_perms_init();
  384. lkdtm_usercopy_init();
  385. /* Register debugfs interface */
  386. lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
  387. if (!lkdtm_debugfs_root) {
  388. pr_err("creating root dir failed\n");
  389. return -ENODEV;
  390. }
  391. /* Install debugfs trigger files. */
  392. for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
  393. struct crashpoint *cur = &crashpoints[i];
  394. struct dentry *de;
  395. de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
  396. cur, &cur->fops);
  397. if (de == NULL) {
  398. pr_err("could not create crashpoint %s\n", cur->name);
  399. goto out_err;
  400. }
  401. }
  402. /* Install crashpoint if one was selected. */
  403. if (crashpoint) {
  404. ret = lkdtm_register_cpoint(crashpoint, crashtype);
  405. if (ret < 0) {
  406. pr_info("Invalid crashpoint %s\n", crashpoint->name);
  407. goto out_err;
  408. }
  409. pr_info("Crash point %s of type %s registered\n",
  410. crashpoint->name, cpoint_type);
  411. } else {
  412. pr_info("No crash points registered, enable through debugfs\n");
  413. }
  414. return 0;
  415. out_err:
  416. debugfs_remove_recursive(lkdtm_debugfs_root);
  417. return ret;
  418. }
  419. static void __exit lkdtm_module_exit(void)
  420. {
  421. debugfs_remove_recursive(lkdtm_debugfs_root);
  422. /* Handle test-specific clean-up. */
  423. lkdtm_usercopy_exit();
  424. if (lkdtm_kprobe != NULL)
  425. unregister_kprobe(lkdtm_kprobe);
  426. pr_info("Crash point unregistered\n");
  427. }
  428. module_init(lkdtm_module_init);
  429. module_exit(lkdtm_module_exit);
  430. MODULE_LICENSE("GPL");
  431. MODULE_DESCRIPTION("Kernel crash testing module");