lkdtm_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 void lkdtm_handler(void);
  56. static ssize_t lkdtm_debugfs_entry(struct file *f,
  57. const char __user *user_buf,
  58. size_t count, loff_t *off);
  59. /* jprobe entry point handlers. */
  60. static unsigned int jp_do_irq(unsigned int irq)
  61. {
  62. lkdtm_handler();
  63. jprobe_return();
  64. return 0;
  65. }
  66. static irqreturn_t jp_handle_irq_event(unsigned int irq,
  67. struct irqaction *action)
  68. {
  69. lkdtm_handler();
  70. jprobe_return();
  71. return 0;
  72. }
  73. static void jp_tasklet_action(struct softirq_action *a)
  74. {
  75. lkdtm_handler();
  76. jprobe_return();
  77. }
  78. static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
  79. {
  80. lkdtm_handler();
  81. jprobe_return();
  82. }
  83. struct scan_control;
  84. static unsigned long jp_shrink_inactive_list(unsigned long max_scan,
  85. struct zone *zone,
  86. struct scan_control *sc)
  87. {
  88. lkdtm_handler();
  89. jprobe_return();
  90. return 0;
  91. }
  92. static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim,
  93. const enum hrtimer_mode mode)
  94. {
  95. lkdtm_handler();
  96. jprobe_return();
  97. return 0;
  98. }
  99. static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd)
  100. {
  101. lkdtm_handler();
  102. jprobe_return();
  103. return 0;
  104. }
  105. # ifdef CONFIG_IDE
  106. static int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file,
  107. struct block_device *bdev, unsigned int cmd,
  108. unsigned long arg)
  109. {
  110. lkdtm_handler();
  111. jprobe_return();
  112. return 0;
  113. }
  114. # endif
  115. #endif
  116. /* Crash points */
  117. struct crashpoint {
  118. const char *name;
  119. const struct file_operations fops;
  120. struct jprobe jprobe;
  121. };
  122. #define CRASHPOINT(_name, _write, _symbol, _entry) \
  123. { \
  124. .name = _name, \
  125. .fops = { \
  126. .read = lkdtm_debugfs_read, \
  127. .llseek = generic_file_llseek, \
  128. .open = lkdtm_debugfs_open, \
  129. .write = _write, \
  130. }, \
  131. .jprobe = { \
  132. .kp.symbol_name = _symbol, \
  133. .entry = (kprobe_opcode_t *)_entry, \
  134. }, \
  135. }
  136. /* Define the possible places where we can trigger a crash point. */
  137. struct crashpoint crashpoints[] = {
  138. CRASHPOINT("DIRECT", direct_entry,
  139. NULL, NULL),
  140. #ifdef CONFIG_KPROBES
  141. CRASHPOINT("INT_HARDWARE_ENTRY", lkdtm_debugfs_entry,
  142. "do_IRQ", jp_do_irq),
  143. CRASHPOINT("INT_HW_IRQ_EN", lkdtm_debugfs_entry,
  144. "handle_IRQ_event", jp_handle_irq_event),
  145. CRASHPOINT("INT_TASKLET_ENTRY", lkdtm_debugfs_entry,
  146. "tasklet_action", jp_tasklet_action),
  147. CRASHPOINT("FS_DEVRW", lkdtm_debugfs_entry,
  148. "ll_rw_block", jp_ll_rw_block),
  149. CRASHPOINT("MEM_SWAPOUT", lkdtm_debugfs_entry,
  150. "shrink_inactive_list", jp_shrink_inactive_list),
  151. CRASHPOINT("TIMERADD", lkdtm_debugfs_entry,
  152. "hrtimer_start", jp_hrtimer_start),
  153. CRASHPOINT("SCSI_DISPATCH_CMD", lkdtm_debugfs_entry,
  154. "scsi_dispatch_cmd", jp_scsi_dispatch_cmd),
  155. # ifdef CONFIG_IDE
  156. CRASHPOINT("IDE_CORE_CP", lkdtm_debugfs_entry,
  157. "generic_ide_ioctl", jp_generic_ide_ioctl),
  158. # endif
  159. #endif
  160. };
  161. /* Crash types. */
  162. struct crashtype {
  163. const char *name;
  164. void (*func)(void);
  165. };
  166. #define CRASHTYPE(_name) \
  167. { \
  168. .name = __stringify(_name), \
  169. .func = lkdtm_ ## _name, \
  170. }
  171. /* Define the possible types of crashes that can be triggered. */
  172. struct crashtype crashtypes[] = {
  173. CRASHTYPE(PANIC),
  174. CRASHTYPE(BUG),
  175. CRASHTYPE(WARNING),
  176. CRASHTYPE(EXCEPTION),
  177. CRASHTYPE(LOOP),
  178. CRASHTYPE(OVERFLOW),
  179. CRASHTYPE(CORRUPT_LIST_ADD),
  180. CRASHTYPE(CORRUPT_LIST_DEL),
  181. CRASHTYPE(CORRUPT_USER_DS),
  182. CRASHTYPE(CORRUPT_STACK),
  183. CRASHTYPE(CORRUPT_STACK_STRONG),
  184. CRASHTYPE(STACK_GUARD_PAGE_LEADING),
  185. CRASHTYPE(STACK_GUARD_PAGE_TRAILING),
  186. CRASHTYPE(UNALIGNED_LOAD_STORE_WRITE),
  187. CRASHTYPE(OVERWRITE_ALLOCATION),
  188. CRASHTYPE(WRITE_AFTER_FREE),
  189. CRASHTYPE(READ_AFTER_FREE),
  190. CRASHTYPE(WRITE_BUDDY_AFTER_FREE),
  191. CRASHTYPE(READ_BUDDY_AFTER_FREE),
  192. CRASHTYPE(SOFTLOCKUP),
  193. CRASHTYPE(HARDLOCKUP),
  194. CRASHTYPE(SPINLOCKUP),
  195. CRASHTYPE(HUNG_TASK),
  196. CRASHTYPE(EXEC_DATA),
  197. CRASHTYPE(EXEC_STACK),
  198. CRASHTYPE(EXEC_KMALLOC),
  199. CRASHTYPE(EXEC_VMALLOC),
  200. CRASHTYPE(EXEC_RODATA),
  201. CRASHTYPE(EXEC_USERSPACE),
  202. CRASHTYPE(EXEC_NULL),
  203. CRASHTYPE(ACCESS_USERSPACE),
  204. CRASHTYPE(ACCESS_NULL),
  205. CRASHTYPE(WRITE_RO),
  206. CRASHTYPE(WRITE_RO_AFTER_INIT),
  207. CRASHTYPE(WRITE_KERN),
  208. CRASHTYPE(REFCOUNT_INC_OVERFLOW),
  209. CRASHTYPE(REFCOUNT_ADD_OVERFLOW),
  210. CRASHTYPE(REFCOUNT_INC_NOT_ZERO_OVERFLOW),
  211. CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_OVERFLOW),
  212. CRASHTYPE(REFCOUNT_DEC_ZERO),
  213. CRASHTYPE(REFCOUNT_DEC_NEGATIVE),
  214. CRASHTYPE(REFCOUNT_DEC_AND_TEST_NEGATIVE),
  215. CRASHTYPE(REFCOUNT_SUB_AND_TEST_NEGATIVE),
  216. CRASHTYPE(REFCOUNT_INC_ZERO),
  217. CRASHTYPE(REFCOUNT_ADD_ZERO),
  218. CRASHTYPE(REFCOUNT_INC_SATURATED),
  219. CRASHTYPE(REFCOUNT_DEC_SATURATED),
  220. CRASHTYPE(REFCOUNT_ADD_SATURATED),
  221. CRASHTYPE(REFCOUNT_INC_NOT_ZERO_SATURATED),
  222. CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_SATURATED),
  223. CRASHTYPE(REFCOUNT_DEC_AND_TEST_SATURATED),
  224. CRASHTYPE(REFCOUNT_SUB_AND_TEST_SATURATED),
  225. CRASHTYPE(REFCOUNT_TIMING),
  226. CRASHTYPE(ATOMIC_TIMING),
  227. CRASHTYPE(USERCOPY_HEAP_SIZE_TO),
  228. CRASHTYPE(USERCOPY_HEAP_SIZE_FROM),
  229. CRASHTYPE(USERCOPY_HEAP_FLAG_TO),
  230. CRASHTYPE(USERCOPY_HEAP_FLAG_FROM),
  231. CRASHTYPE(USERCOPY_STACK_FRAME_TO),
  232. CRASHTYPE(USERCOPY_STACK_FRAME_FROM),
  233. CRASHTYPE(USERCOPY_STACK_BEYOND),
  234. CRASHTYPE(USERCOPY_KERNEL),
  235. };
  236. /* Global jprobe entry and crashtype. */
  237. static struct jprobe *lkdtm_jprobe;
  238. struct crashpoint *lkdtm_crashpoint;
  239. struct crashtype *lkdtm_crashtype;
  240. /* Module parameters */
  241. static int recur_count = -1;
  242. module_param(recur_count, int, 0644);
  243. MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
  244. static char* cpoint_name;
  245. module_param(cpoint_name, charp, 0444);
  246. MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
  247. static char* cpoint_type;
  248. module_param(cpoint_type, charp, 0444);
  249. MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
  250. "hitting the crash point");
  251. static int cpoint_count = DEFAULT_COUNT;
  252. module_param(cpoint_count, int, 0644);
  253. MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
  254. "crash point is to be hit to trigger action");
  255. /* Return the crashtype number or NULL if the name is invalid */
  256. static struct crashtype *find_crashtype(const char *name)
  257. {
  258. int i;
  259. for (i = 0; i < ARRAY_SIZE(crashtypes); i++) {
  260. if (!strcmp(name, crashtypes[i].name))
  261. return &crashtypes[i];
  262. }
  263. return NULL;
  264. }
  265. /*
  266. * This is forced noinline just so it distinctly shows up in the stackdump
  267. * which makes validation of expected lkdtm crashes easier.
  268. */
  269. static noinline void lkdtm_do_action(struct crashtype *crashtype)
  270. {
  271. BUG_ON(!crashtype || !crashtype->func);
  272. crashtype->func();
  273. }
  274. static int lkdtm_register_cpoint(struct crashpoint *crashpoint,
  275. struct crashtype *crashtype)
  276. {
  277. int ret;
  278. /* If this doesn't have a symbol, just call immediately. */
  279. if (!crashpoint->jprobe.kp.symbol_name) {
  280. lkdtm_do_action(crashtype);
  281. return 0;
  282. }
  283. if (lkdtm_jprobe != NULL)
  284. unregister_jprobe(lkdtm_jprobe);
  285. lkdtm_crashpoint = crashpoint;
  286. lkdtm_crashtype = crashtype;
  287. lkdtm_jprobe = &crashpoint->jprobe;
  288. ret = register_jprobe(lkdtm_jprobe);
  289. if (ret < 0) {
  290. pr_info("Couldn't register jprobe %s\n",
  291. crashpoint->jprobe.kp.symbol_name);
  292. lkdtm_jprobe = NULL;
  293. lkdtm_crashpoint = NULL;
  294. lkdtm_crashtype = NULL;
  295. }
  296. return ret;
  297. }
  298. #ifdef CONFIG_KPROBES
  299. /* Global crash counter and spinlock. */
  300. static int crash_count = DEFAULT_COUNT;
  301. static DEFINE_SPINLOCK(crash_count_lock);
  302. /* Called by jprobe entry points. */
  303. static void lkdtm_handler(void)
  304. {
  305. unsigned long flags;
  306. bool do_it = false;
  307. BUG_ON(!lkdtm_crashpoint || !lkdtm_crashtype);
  308. spin_lock_irqsave(&crash_count_lock, flags);
  309. crash_count--;
  310. pr_info("Crash point %s of type %s hit, trigger in %d rounds\n",
  311. lkdtm_crashpoint->name, lkdtm_crashtype->name, crash_count);
  312. if (crash_count == 0) {
  313. do_it = true;
  314. crash_count = cpoint_count;
  315. }
  316. spin_unlock_irqrestore(&crash_count_lock, flags);
  317. if (do_it)
  318. lkdtm_do_action(lkdtm_crashtype);
  319. }
  320. static ssize_t lkdtm_debugfs_entry(struct file *f,
  321. const char __user *user_buf,
  322. size_t count, loff_t *off)
  323. {
  324. struct crashpoint *crashpoint = file_inode(f)->i_private;
  325. struct crashtype *crashtype = NULL;
  326. char *buf;
  327. int err;
  328. if (count >= PAGE_SIZE)
  329. return -EINVAL;
  330. buf = (char *)__get_free_page(GFP_KERNEL);
  331. if (!buf)
  332. return -ENOMEM;
  333. if (copy_from_user(buf, user_buf, count)) {
  334. free_page((unsigned long) buf);
  335. return -EFAULT;
  336. }
  337. /* NULL-terminate and remove enter */
  338. buf[count] = '\0';
  339. strim(buf);
  340. crashtype = find_crashtype(buf);
  341. free_page((unsigned long)buf);
  342. if (!crashtype)
  343. return -EINVAL;
  344. err = lkdtm_register_cpoint(crashpoint, crashtype);
  345. if (err < 0)
  346. return err;
  347. *off += count;
  348. return count;
  349. }
  350. #endif
  351. /* Generic read callback that just prints out the available crash types */
  352. static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
  353. size_t count, loff_t *off)
  354. {
  355. char *buf;
  356. int i, n, out;
  357. buf = (char *)__get_free_page(GFP_KERNEL);
  358. if (buf == NULL)
  359. return -ENOMEM;
  360. n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
  361. for (i = 0; i < ARRAY_SIZE(crashtypes); i++) {
  362. n += snprintf(buf + n, PAGE_SIZE - n, "%s\n",
  363. crashtypes[i].name);
  364. }
  365. buf[n] = '\0';
  366. out = simple_read_from_buffer(user_buf, count, off,
  367. buf, n);
  368. free_page((unsigned long) buf);
  369. return out;
  370. }
  371. static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
  372. {
  373. return 0;
  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. struct crashtype *crashtype;
  380. char *buf;
  381. if (count >= PAGE_SIZE)
  382. return -EINVAL;
  383. if (count < 1)
  384. return -EINVAL;
  385. buf = (char *)__get_free_page(GFP_KERNEL);
  386. if (!buf)
  387. return -ENOMEM;
  388. if (copy_from_user(buf, user_buf, count)) {
  389. free_page((unsigned long) buf);
  390. return -EFAULT;
  391. }
  392. /* NULL-terminate and remove enter */
  393. buf[count] = '\0';
  394. strim(buf);
  395. crashtype = find_crashtype(buf);
  396. free_page((unsigned long) buf);
  397. if (!crashtype)
  398. return -EINVAL;
  399. pr_info("Performing direct entry %s\n", crashtype->name);
  400. lkdtm_do_action(crashtype);
  401. *off += count;
  402. return count;
  403. }
  404. static struct dentry *lkdtm_debugfs_root;
  405. static int __init lkdtm_module_init(void)
  406. {
  407. struct crashpoint *crashpoint = NULL;
  408. struct crashtype *crashtype = NULL;
  409. int ret = -EINVAL;
  410. int i;
  411. /* Neither or both of these need to be set */
  412. if ((cpoint_type || cpoint_name) && !(cpoint_type && cpoint_name)) {
  413. pr_err("Need both cpoint_type and cpoint_name or neither\n");
  414. return -EINVAL;
  415. }
  416. if (cpoint_type) {
  417. crashtype = find_crashtype(cpoint_type);
  418. if (!crashtype) {
  419. pr_err("Unknown crashtype '%s'\n", cpoint_type);
  420. return -EINVAL;
  421. }
  422. }
  423. if (cpoint_name) {
  424. for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
  425. if (!strcmp(cpoint_name, crashpoints[i].name))
  426. crashpoint = &crashpoints[i];
  427. }
  428. /* Refuse unknown crashpoints. */
  429. if (!crashpoint) {
  430. pr_err("Invalid crashpoint %s\n", cpoint_name);
  431. return -EINVAL;
  432. }
  433. }
  434. #ifdef CONFIG_KPROBES
  435. /* Set crash count. */
  436. crash_count = cpoint_count;
  437. #endif
  438. /* Handle test-specific initialization. */
  439. lkdtm_bugs_init(&recur_count);
  440. lkdtm_perms_init();
  441. lkdtm_usercopy_init();
  442. /* Register debugfs interface */
  443. lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
  444. if (!lkdtm_debugfs_root) {
  445. pr_err("creating root dir failed\n");
  446. return -ENODEV;
  447. }
  448. /* Install debugfs trigger files. */
  449. for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
  450. struct crashpoint *cur = &crashpoints[i];
  451. struct dentry *de;
  452. de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
  453. cur, &cur->fops);
  454. if (de == NULL) {
  455. pr_err("could not create crashpoint %s\n", cur->name);
  456. goto out_err;
  457. }
  458. }
  459. /* Install crashpoint if one was selected. */
  460. if (crashpoint) {
  461. ret = lkdtm_register_cpoint(crashpoint, crashtype);
  462. if (ret < 0) {
  463. pr_info("Invalid crashpoint %s\n", crashpoint->name);
  464. goto out_err;
  465. }
  466. pr_info("Crash point %s of type %s registered\n",
  467. crashpoint->name, cpoint_type);
  468. } else {
  469. pr_info("No crash points registered, enable through debugfs\n");
  470. }
  471. return 0;
  472. out_err:
  473. debugfs_remove_recursive(lkdtm_debugfs_root);
  474. return ret;
  475. }
  476. static void __exit lkdtm_module_exit(void)
  477. {
  478. debugfs_remove_recursive(lkdtm_debugfs_root);
  479. /* Handle test-specific clean-up. */
  480. lkdtm_usercopy_exit();
  481. if (lkdtm_jprobe != NULL)
  482. unregister_jprobe(lkdtm_jprobe);
  483. pr_info("Crash point unregistered\n");
  484. }
  485. module_init(lkdtm_module_init);
  486. module_exit(lkdtm_module_exit);
  487. MODULE_LICENSE("GPL");
  488. MODULE_DESCRIPTION("Kernel crash testing module");