lkdtm_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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_STACK),
  180. CRASHTYPE(UNALIGNED_LOAD_STORE_WRITE),
  181. CRASHTYPE(OVERWRITE_ALLOCATION),
  182. CRASHTYPE(WRITE_AFTER_FREE),
  183. CRASHTYPE(READ_AFTER_FREE),
  184. CRASHTYPE(WRITE_BUDDY_AFTER_FREE),
  185. CRASHTYPE(READ_BUDDY_AFTER_FREE),
  186. CRASHTYPE(SOFTLOCKUP),
  187. CRASHTYPE(HARDLOCKUP),
  188. CRASHTYPE(SPINLOCKUP),
  189. CRASHTYPE(HUNG_TASK),
  190. CRASHTYPE(EXEC_DATA),
  191. CRASHTYPE(EXEC_STACK),
  192. CRASHTYPE(EXEC_KMALLOC),
  193. CRASHTYPE(EXEC_VMALLOC),
  194. CRASHTYPE(EXEC_RODATA),
  195. CRASHTYPE(EXEC_USERSPACE),
  196. CRASHTYPE(ACCESS_USERSPACE),
  197. CRASHTYPE(WRITE_RO),
  198. CRASHTYPE(WRITE_RO_AFTER_INIT),
  199. CRASHTYPE(WRITE_KERN),
  200. CRASHTYPE(ATOMIC_UNDERFLOW),
  201. CRASHTYPE(ATOMIC_OVERFLOW),
  202. CRASHTYPE(USERCOPY_HEAP_SIZE_TO),
  203. CRASHTYPE(USERCOPY_HEAP_SIZE_FROM),
  204. CRASHTYPE(USERCOPY_HEAP_FLAG_TO),
  205. CRASHTYPE(USERCOPY_HEAP_FLAG_FROM),
  206. CRASHTYPE(USERCOPY_STACK_FRAME_TO),
  207. CRASHTYPE(USERCOPY_STACK_FRAME_FROM),
  208. CRASHTYPE(USERCOPY_STACK_BEYOND),
  209. CRASHTYPE(USERCOPY_KERNEL),
  210. };
  211. /* Global jprobe entry and crashtype. */
  212. static struct jprobe *lkdtm_jprobe;
  213. struct crashpoint *lkdtm_crashpoint;
  214. struct crashtype *lkdtm_crashtype;
  215. /* Module parameters */
  216. static int recur_count = -1;
  217. module_param(recur_count, int, 0644);
  218. MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
  219. static char* cpoint_name;
  220. module_param(cpoint_name, charp, 0444);
  221. MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
  222. static char* cpoint_type;
  223. module_param(cpoint_type, charp, 0444);
  224. MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
  225. "hitting the crash point");
  226. static int cpoint_count = DEFAULT_COUNT;
  227. module_param(cpoint_count, int, 0644);
  228. MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
  229. "crash point is to be hit to trigger action");
  230. /* Return the crashtype number or NULL if the name is invalid */
  231. static struct crashtype *find_crashtype(const char *name)
  232. {
  233. int i;
  234. for (i = 0; i < ARRAY_SIZE(crashtypes); i++) {
  235. if (!strcmp(name, crashtypes[i].name))
  236. return &crashtypes[i];
  237. }
  238. return NULL;
  239. }
  240. /*
  241. * This is forced noinline just so it distinctly shows up in the stackdump
  242. * which makes validation of expected lkdtm crashes easier.
  243. */
  244. static noinline void lkdtm_do_action(struct crashtype *crashtype)
  245. {
  246. BUG_ON(!crashtype || !crashtype->func);
  247. crashtype->func();
  248. }
  249. static int lkdtm_register_cpoint(struct crashpoint *crashpoint,
  250. struct crashtype *crashtype)
  251. {
  252. int ret;
  253. /* If this doesn't have a symbol, just call immediately. */
  254. if (!crashpoint->jprobe.kp.symbol_name) {
  255. lkdtm_do_action(crashtype);
  256. return 0;
  257. }
  258. if (lkdtm_jprobe != NULL)
  259. unregister_jprobe(lkdtm_jprobe);
  260. lkdtm_crashpoint = crashpoint;
  261. lkdtm_crashtype = crashtype;
  262. lkdtm_jprobe = &crashpoint->jprobe;
  263. ret = register_jprobe(lkdtm_jprobe);
  264. if (ret < 0) {
  265. pr_info("Couldn't register jprobe %s\n",
  266. crashpoint->jprobe.kp.symbol_name);
  267. lkdtm_jprobe = NULL;
  268. lkdtm_crashpoint = NULL;
  269. lkdtm_crashtype = NULL;
  270. }
  271. return ret;
  272. }
  273. #ifdef CONFIG_KPROBES
  274. /* Global crash counter and spinlock. */
  275. static int crash_count = DEFAULT_COUNT;
  276. static DEFINE_SPINLOCK(crash_count_lock);
  277. /* Called by jprobe entry points. */
  278. static void lkdtm_handler(void)
  279. {
  280. unsigned long flags;
  281. bool do_it = false;
  282. BUG_ON(!lkdtm_crashpoint || !lkdtm_crashtype);
  283. spin_lock_irqsave(&crash_count_lock, flags);
  284. crash_count--;
  285. pr_info("Crash point %s of type %s hit, trigger in %d rounds\n",
  286. lkdtm_crashpoint->name, lkdtm_crashtype->name, crash_count);
  287. if (crash_count == 0) {
  288. do_it = true;
  289. crash_count = cpoint_count;
  290. }
  291. spin_unlock_irqrestore(&crash_count_lock, flags);
  292. if (do_it)
  293. lkdtm_do_action(lkdtm_crashtype);
  294. }
  295. static ssize_t lkdtm_debugfs_entry(struct file *f,
  296. const char __user *user_buf,
  297. size_t count, loff_t *off)
  298. {
  299. struct crashpoint *crashpoint = file_inode(f)->i_private;
  300. struct crashtype *crashtype = NULL;
  301. char *buf;
  302. int err;
  303. if (count >= PAGE_SIZE)
  304. return -EINVAL;
  305. buf = (char *)__get_free_page(GFP_KERNEL);
  306. if (!buf)
  307. return -ENOMEM;
  308. if (copy_from_user(buf, user_buf, count)) {
  309. free_page((unsigned long) buf);
  310. return -EFAULT;
  311. }
  312. /* NULL-terminate and remove enter */
  313. buf[count] = '\0';
  314. strim(buf);
  315. crashtype = find_crashtype(buf);
  316. free_page((unsigned long)buf);
  317. if (!crashtype)
  318. return -EINVAL;
  319. err = lkdtm_register_cpoint(crashpoint, crashtype);
  320. if (err < 0)
  321. return err;
  322. *off += count;
  323. return count;
  324. }
  325. #endif
  326. /* Generic read callback that just prints out the available crash types */
  327. static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
  328. size_t count, loff_t *off)
  329. {
  330. char *buf;
  331. int i, n, out;
  332. buf = (char *)__get_free_page(GFP_KERNEL);
  333. if (buf == NULL)
  334. return -ENOMEM;
  335. n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
  336. for (i = 0; i < ARRAY_SIZE(crashtypes); i++) {
  337. n += snprintf(buf + n, PAGE_SIZE - n, "%s\n",
  338. crashtypes[i].name);
  339. }
  340. buf[n] = '\0';
  341. out = simple_read_from_buffer(user_buf, count, off,
  342. buf, n);
  343. free_page((unsigned long) buf);
  344. return out;
  345. }
  346. static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
  347. {
  348. return 0;
  349. }
  350. /* Special entry to just crash directly. Available without KPROBEs */
  351. static ssize_t direct_entry(struct file *f, const char __user *user_buf,
  352. size_t count, loff_t *off)
  353. {
  354. struct crashtype *crashtype;
  355. char *buf;
  356. if (count >= PAGE_SIZE)
  357. return -EINVAL;
  358. if (count < 1)
  359. return -EINVAL;
  360. buf = (char *)__get_free_page(GFP_KERNEL);
  361. if (!buf)
  362. return -ENOMEM;
  363. if (copy_from_user(buf, user_buf, count)) {
  364. free_page((unsigned long) buf);
  365. return -EFAULT;
  366. }
  367. /* NULL-terminate and remove enter */
  368. buf[count] = '\0';
  369. strim(buf);
  370. crashtype = find_crashtype(buf);
  371. free_page((unsigned long) buf);
  372. if (!crashtype)
  373. return -EINVAL;
  374. pr_info("Performing direct entry %s\n", crashtype->name);
  375. lkdtm_do_action(crashtype);
  376. *off += count;
  377. return count;
  378. }
  379. static struct dentry *lkdtm_debugfs_root;
  380. static int __init lkdtm_module_init(void)
  381. {
  382. struct crashpoint *crashpoint = NULL;
  383. struct crashtype *crashtype = NULL;
  384. int ret = -EINVAL;
  385. int i;
  386. /* Neither or both of these need to be set */
  387. if ((cpoint_type || cpoint_name) && !(cpoint_type && cpoint_name)) {
  388. pr_err("Need both cpoint_type and cpoint_name or neither\n");
  389. return -EINVAL;
  390. }
  391. if (cpoint_type) {
  392. crashtype = find_crashtype(cpoint_type);
  393. if (!crashtype) {
  394. pr_err("Unknown crashtype '%s'\n", cpoint_type);
  395. return -EINVAL;
  396. }
  397. }
  398. if (cpoint_name) {
  399. for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
  400. if (!strcmp(cpoint_name, crashpoints[i].name))
  401. crashpoint = &crashpoints[i];
  402. }
  403. /* Refuse unknown crashpoints. */
  404. if (!crashpoint) {
  405. pr_err("Invalid crashpoint %s\n", cpoint_name);
  406. return -EINVAL;
  407. }
  408. }
  409. #ifdef CONFIG_KPROBES
  410. /* Set crash count. */
  411. crash_count = cpoint_count;
  412. #endif
  413. /* Handle test-specific initialization. */
  414. lkdtm_bugs_init(&recur_count);
  415. lkdtm_perms_init();
  416. lkdtm_usercopy_init();
  417. /* Register debugfs interface */
  418. lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
  419. if (!lkdtm_debugfs_root) {
  420. pr_err("creating root dir failed\n");
  421. return -ENODEV;
  422. }
  423. /* Install debugfs trigger files. */
  424. for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
  425. struct crashpoint *cur = &crashpoints[i];
  426. struct dentry *de;
  427. de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
  428. cur, &cur->fops);
  429. if (de == NULL) {
  430. pr_err("could not create crashpoint %s\n", cur->name);
  431. goto out_err;
  432. }
  433. }
  434. /* Install crashpoint if one was selected. */
  435. if (crashpoint) {
  436. ret = lkdtm_register_cpoint(crashpoint, crashtype);
  437. if (ret < 0) {
  438. pr_info("Invalid crashpoint %s\n", crashpoint->name);
  439. goto out_err;
  440. }
  441. pr_info("Crash point %s of type %s registered\n",
  442. crashpoint->name, cpoint_type);
  443. } else {
  444. pr_info("No crash points registered, enable through debugfs\n");
  445. }
  446. return 0;
  447. out_err:
  448. debugfs_remove_recursive(lkdtm_debugfs_root);
  449. return ret;
  450. }
  451. static void __exit lkdtm_module_exit(void)
  452. {
  453. debugfs_remove_recursive(lkdtm_debugfs_root);
  454. /* Handle test-specific clean-up. */
  455. lkdtm_usercopy_exit();
  456. if (lkdtm_jprobe != NULL)
  457. unregister_jprobe(lkdtm_jprobe);
  458. pr_info("Crash point unregistered\n");
  459. }
  460. module_init(lkdtm_module_init);
  461. module_exit(lkdtm_module_exit);
  462. MODULE_LICENSE("GPL");
  463. MODULE_DESCRIPTION("Kernel crash testing module");