lkdtm_core.c 14 KB

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