lkdtm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. * Kprobe module for testing crash dumps
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2006
  19. *
  20. * Author: Ankita Garg <ankita@in.ibm.com>
  21. *
  22. * This module induces system failures at predefined crashpoints to
  23. * evaluate the reliability of crash dumps obtained using different dumping
  24. * solutions.
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  34. #include <linux/kernel.h>
  35. #include <linux/fs.h>
  36. #include <linux/module.h>
  37. #include <linux/buffer_head.h>
  38. #include <linux/kprobes.h>
  39. #include <linux/list.h>
  40. #include <linux/init.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/hrtimer.h>
  43. #include <linux/slab.h>
  44. #include <scsi/scsi_cmnd.h>
  45. #include <linux/debugfs.h>
  46. #include <linux/vmalloc.h>
  47. #include <linux/mman.h>
  48. #ifdef CONFIG_IDE
  49. #include <linux/ide.h>
  50. #endif
  51. /*
  52. * Make sure our attempts to over run the kernel stack doesn't trigger
  53. * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
  54. * recurse past the end of THREAD_SIZE by default.
  55. */
  56. #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
  57. #define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
  58. #else
  59. #define REC_STACK_SIZE (THREAD_SIZE / 8)
  60. #endif
  61. #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
  62. #define DEFAULT_COUNT 10
  63. #define EXEC_SIZE 64
  64. enum cname {
  65. CN_INVALID,
  66. CN_INT_HARDWARE_ENTRY,
  67. CN_INT_HW_IRQ_EN,
  68. CN_INT_TASKLET_ENTRY,
  69. CN_FS_DEVRW,
  70. CN_MEM_SWAPOUT,
  71. CN_TIMERADD,
  72. CN_SCSI_DISPATCH_CMD,
  73. CN_IDE_CORE_CP,
  74. CN_DIRECT,
  75. };
  76. enum ctype {
  77. CT_NONE,
  78. CT_PANIC,
  79. CT_BUG,
  80. CT_WARNING,
  81. CT_EXCEPTION,
  82. CT_LOOP,
  83. CT_OVERFLOW,
  84. CT_CORRUPT_STACK,
  85. CT_UNALIGNED_LOAD_STORE_WRITE,
  86. CT_OVERWRITE_ALLOCATION,
  87. CT_WRITE_AFTER_FREE,
  88. CT_SOFTLOCKUP,
  89. CT_HARDLOCKUP,
  90. CT_SPINLOCKUP,
  91. CT_HUNG_TASK,
  92. CT_EXEC_DATA,
  93. CT_EXEC_STACK,
  94. CT_EXEC_KMALLOC,
  95. CT_EXEC_VMALLOC,
  96. CT_EXEC_USERSPACE,
  97. CT_ACCESS_USERSPACE,
  98. CT_WRITE_RO,
  99. };
  100. static char* cp_name[] = {
  101. "INT_HARDWARE_ENTRY",
  102. "INT_HW_IRQ_EN",
  103. "INT_TASKLET_ENTRY",
  104. "FS_DEVRW",
  105. "MEM_SWAPOUT",
  106. "TIMERADD",
  107. "SCSI_DISPATCH_CMD",
  108. "IDE_CORE_CP",
  109. "DIRECT",
  110. };
  111. static char* cp_type[] = {
  112. "PANIC",
  113. "BUG",
  114. "WARNING",
  115. "EXCEPTION",
  116. "LOOP",
  117. "OVERFLOW",
  118. "CORRUPT_STACK",
  119. "UNALIGNED_LOAD_STORE_WRITE",
  120. "OVERWRITE_ALLOCATION",
  121. "WRITE_AFTER_FREE",
  122. "SOFTLOCKUP",
  123. "HARDLOCKUP",
  124. "SPINLOCKUP",
  125. "HUNG_TASK",
  126. "EXEC_DATA",
  127. "EXEC_STACK",
  128. "EXEC_KMALLOC",
  129. "EXEC_VMALLOC",
  130. "EXEC_USERSPACE",
  131. "ACCESS_USERSPACE",
  132. "WRITE_RO",
  133. };
  134. static struct jprobe lkdtm;
  135. static int lkdtm_parse_commandline(void);
  136. static void lkdtm_handler(void);
  137. static char* cpoint_name;
  138. static char* cpoint_type;
  139. static int cpoint_count = DEFAULT_COUNT;
  140. static int recur_count = REC_NUM_DEFAULT;
  141. static enum cname cpoint = CN_INVALID;
  142. static enum ctype cptype = CT_NONE;
  143. static int count = DEFAULT_COUNT;
  144. static DEFINE_SPINLOCK(count_lock);
  145. static DEFINE_SPINLOCK(lock_me_up);
  146. static u8 data_area[EXEC_SIZE];
  147. static const unsigned long rodata = 0xAA55AA55;
  148. module_param(recur_count, int, 0644);
  149. MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
  150. module_param(cpoint_name, charp, 0444);
  151. MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
  152. module_param(cpoint_type, charp, 0444);
  153. MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
  154. "hitting the crash point");
  155. module_param(cpoint_count, int, 0644);
  156. MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
  157. "crash point is to be hit to trigger action");
  158. static unsigned int jp_do_irq(unsigned int irq)
  159. {
  160. lkdtm_handler();
  161. jprobe_return();
  162. return 0;
  163. }
  164. static irqreturn_t jp_handle_irq_event(unsigned int irq,
  165. struct irqaction *action)
  166. {
  167. lkdtm_handler();
  168. jprobe_return();
  169. return 0;
  170. }
  171. static void jp_tasklet_action(struct softirq_action *a)
  172. {
  173. lkdtm_handler();
  174. jprobe_return();
  175. }
  176. static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
  177. {
  178. lkdtm_handler();
  179. jprobe_return();
  180. }
  181. struct scan_control;
  182. static unsigned long jp_shrink_inactive_list(unsigned long max_scan,
  183. struct zone *zone,
  184. struct scan_control *sc)
  185. {
  186. lkdtm_handler();
  187. jprobe_return();
  188. return 0;
  189. }
  190. static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim,
  191. const enum hrtimer_mode mode)
  192. {
  193. lkdtm_handler();
  194. jprobe_return();
  195. return 0;
  196. }
  197. static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd)
  198. {
  199. lkdtm_handler();
  200. jprobe_return();
  201. return 0;
  202. }
  203. #ifdef CONFIG_IDE
  204. static int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file,
  205. struct block_device *bdev, unsigned int cmd,
  206. unsigned long arg)
  207. {
  208. lkdtm_handler();
  209. jprobe_return();
  210. return 0;
  211. }
  212. #endif
  213. /* Return the crashpoint number or NONE if the name is invalid */
  214. static enum ctype parse_cp_type(const char *what, size_t count)
  215. {
  216. int i;
  217. for (i = 0; i < ARRAY_SIZE(cp_type); i++) {
  218. if (!strcmp(what, cp_type[i]))
  219. return i + 1;
  220. }
  221. return CT_NONE;
  222. }
  223. static const char *cp_type_to_str(enum ctype type)
  224. {
  225. if (type == CT_NONE || type < 0 || type > ARRAY_SIZE(cp_type))
  226. return "None";
  227. return cp_type[type - 1];
  228. }
  229. static const char *cp_name_to_str(enum cname name)
  230. {
  231. if (name == CN_INVALID || name < 0 || name > ARRAY_SIZE(cp_name))
  232. return "INVALID";
  233. return cp_name[name - 1];
  234. }
  235. static int lkdtm_parse_commandline(void)
  236. {
  237. int i;
  238. unsigned long flags;
  239. if (cpoint_count < 1 || recur_count < 1)
  240. return -EINVAL;
  241. spin_lock_irqsave(&count_lock, flags);
  242. count = cpoint_count;
  243. spin_unlock_irqrestore(&count_lock, flags);
  244. /* No special parameters */
  245. if (!cpoint_type && !cpoint_name)
  246. return 0;
  247. /* Neither or both of these need to be set */
  248. if (!cpoint_type || !cpoint_name)
  249. return -EINVAL;
  250. cptype = parse_cp_type(cpoint_type, strlen(cpoint_type));
  251. if (cptype == CT_NONE)
  252. return -EINVAL;
  253. for (i = 0; i < ARRAY_SIZE(cp_name); i++) {
  254. if (!strcmp(cpoint_name, cp_name[i])) {
  255. cpoint = i + 1;
  256. return 0;
  257. }
  258. }
  259. /* Could not find a valid crash point */
  260. return -EINVAL;
  261. }
  262. static int recursive_loop(int remaining)
  263. {
  264. char buf[REC_STACK_SIZE];
  265. /* Make sure compiler does not optimize this away. */
  266. memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE);
  267. if (!remaining)
  268. return 0;
  269. else
  270. return recursive_loop(remaining - 1);
  271. }
  272. static void do_nothing(void)
  273. {
  274. return;
  275. }
  276. static noinline void corrupt_stack(void)
  277. {
  278. /* Use default char array length that triggers stack protection. */
  279. char data[8];
  280. memset((void *)data, 0, 64);
  281. }
  282. static void execute_location(void *dst)
  283. {
  284. void (*func)(void) = dst;
  285. pr_info("attempting ok execution at %p\n", do_nothing);
  286. do_nothing();
  287. memcpy(dst, do_nothing, EXEC_SIZE);
  288. flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE);
  289. pr_info("attempting bad execution at %p\n", func);
  290. func();
  291. }
  292. static void execute_user_location(void *dst)
  293. {
  294. /* Intentionally crossing kernel/user memory boundary. */
  295. void (*func)(void) = dst;
  296. pr_info("attempting ok execution at %p\n", do_nothing);
  297. do_nothing();
  298. if (copy_to_user((void __user *)dst, do_nothing, EXEC_SIZE))
  299. return;
  300. flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE);
  301. pr_info("attempting bad execution at %p\n", func);
  302. func();
  303. }
  304. static void lkdtm_do_action(enum ctype which)
  305. {
  306. switch (which) {
  307. case CT_PANIC:
  308. panic("dumptest");
  309. break;
  310. case CT_BUG:
  311. BUG();
  312. break;
  313. case CT_WARNING:
  314. WARN_ON(1);
  315. break;
  316. case CT_EXCEPTION:
  317. *((int *) 0) = 0;
  318. break;
  319. case CT_LOOP:
  320. for (;;)
  321. ;
  322. break;
  323. case CT_OVERFLOW:
  324. (void) recursive_loop(recur_count);
  325. break;
  326. case CT_CORRUPT_STACK:
  327. corrupt_stack();
  328. break;
  329. case CT_UNALIGNED_LOAD_STORE_WRITE: {
  330. static u8 data[5] __attribute__((aligned(4))) = {1, 2,
  331. 3, 4, 5};
  332. u32 *p;
  333. u32 val = 0x12345678;
  334. p = (u32 *)(data + 1);
  335. if (*p == 0)
  336. val = 0x87654321;
  337. *p = val;
  338. break;
  339. }
  340. case CT_OVERWRITE_ALLOCATION: {
  341. size_t len = 1020;
  342. u32 *data = kmalloc(len, GFP_KERNEL);
  343. data[1024 / sizeof(u32)] = 0x12345678;
  344. kfree(data);
  345. break;
  346. }
  347. case CT_WRITE_AFTER_FREE: {
  348. size_t len = 1024;
  349. u32 *data = kmalloc(len, GFP_KERNEL);
  350. kfree(data);
  351. schedule();
  352. memset(data, 0x78, len);
  353. break;
  354. }
  355. case CT_SOFTLOCKUP:
  356. preempt_disable();
  357. for (;;)
  358. cpu_relax();
  359. break;
  360. case CT_HARDLOCKUP:
  361. local_irq_disable();
  362. for (;;)
  363. cpu_relax();
  364. break;
  365. case CT_SPINLOCKUP:
  366. /* Must be called twice to trigger. */
  367. spin_lock(&lock_me_up);
  368. /* Let sparse know we intended to exit holding the lock. */
  369. __release(&lock_me_up);
  370. break;
  371. case CT_HUNG_TASK:
  372. set_current_state(TASK_UNINTERRUPTIBLE);
  373. schedule();
  374. break;
  375. case CT_EXEC_DATA:
  376. execute_location(data_area);
  377. break;
  378. case CT_EXEC_STACK: {
  379. u8 stack_area[EXEC_SIZE];
  380. execute_location(stack_area);
  381. break;
  382. }
  383. case CT_EXEC_KMALLOC: {
  384. u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
  385. execute_location(kmalloc_area);
  386. kfree(kmalloc_area);
  387. break;
  388. }
  389. case CT_EXEC_VMALLOC: {
  390. u32 *vmalloc_area = vmalloc(EXEC_SIZE);
  391. execute_location(vmalloc_area);
  392. vfree(vmalloc_area);
  393. break;
  394. }
  395. case CT_EXEC_USERSPACE: {
  396. unsigned long user_addr;
  397. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  398. PROT_READ | PROT_WRITE | PROT_EXEC,
  399. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  400. if (user_addr >= TASK_SIZE) {
  401. pr_warn("Failed to allocate user memory\n");
  402. return;
  403. }
  404. execute_user_location((void *)user_addr);
  405. vm_munmap(user_addr, PAGE_SIZE);
  406. break;
  407. }
  408. case CT_ACCESS_USERSPACE: {
  409. unsigned long user_addr, tmp;
  410. unsigned long *ptr;
  411. user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
  412. PROT_READ | PROT_WRITE | PROT_EXEC,
  413. MAP_ANONYMOUS | MAP_PRIVATE, 0);
  414. if (user_addr >= TASK_SIZE) {
  415. pr_warn("Failed to allocate user memory\n");
  416. return;
  417. }
  418. ptr = (unsigned long *)user_addr;
  419. pr_info("attempting bad read at %p\n", ptr);
  420. tmp = *ptr;
  421. tmp += 0xc0dec0de;
  422. pr_info("attempting bad write at %p\n", ptr);
  423. *ptr = tmp;
  424. vm_munmap(user_addr, PAGE_SIZE);
  425. break;
  426. }
  427. case CT_WRITE_RO: {
  428. unsigned long *ptr;
  429. ptr = (unsigned long *)&rodata;
  430. pr_info("attempting bad write at %p\n", ptr);
  431. *ptr ^= 0xabcd1234;
  432. break;
  433. }
  434. case CT_NONE:
  435. default:
  436. break;
  437. }
  438. }
  439. static void lkdtm_handler(void)
  440. {
  441. unsigned long flags;
  442. bool do_it = false;
  443. spin_lock_irqsave(&count_lock, flags);
  444. count--;
  445. pr_info("Crash point %s of type %s hit, trigger in %d rounds\n",
  446. cp_name_to_str(cpoint), cp_type_to_str(cptype), count);
  447. if (count == 0) {
  448. do_it = true;
  449. count = cpoint_count;
  450. }
  451. spin_unlock_irqrestore(&count_lock, flags);
  452. if (do_it)
  453. lkdtm_do_action(cptype);
  454. }
  455. static int lkdtm_register_cpoint(enum cname which)
  456. {
  457. int ret;
  458. cpoint = CN_INVALID;
  459. if (lkdtm.entry != NULL)
  460. unregister_jprobe(&lkdtm);
  461. switch (which) {
  462. case CN_DIRECT:
  463. lkdtm_do_action(cptype);
  464. return 0;
  465. case CN_INT_HARDWARE_ENTRY:
  466. lkdtm.kp.symbol_name = "do_IRQ";
  467. lkdtm.entry = (kprobe_opcode_t*) jp_do_irq;
  468. break;
  469. case CN_INT_HW_IRQ_EN:
  470. lkdtm.kp.symbol_name = "handle_IRQ_event";
  471. lkdtm.entry = (kprobe_opcode_t*) jp_handle_irq_event;
  472. break;
  473. case CN_INT_TASKLET_ENTRY:
  474. lkdtm.kp.symbol_name = "tasklet_action";
  475. lkdtm.entry = (kprobe_opcode_t*) jp_tasklet_action;
  476. break;
  477. case CN_FS_DEVRW:
  478. lkdtm.kp.symbol_name = "ll_rw_block";
  479. lkdtm.entry = (kprobe_opcode_t*) jp_ll_rw_block;
  480. break;
  481. case CN_MEM_SWAPOUT:
  482. lkdtm.kp.symbol_name = "shrink_inactive_list";
  483. lkdtm.entry = (kprobe_opcode_t*) jp_shrink_inactive_list;
  484. break;
  485. case CN_TIMERADD:
  486. lkdtm.kp.symbol_name = "hrtimer_start";
  487. lkdtm.entry = (kprobe_opcode_t*) jp_hrtimer_start;
  488. break;
  489. case CN_SCSI_DISPATCH_CMD:
  490. lkdtm.kp.symbol_name = "scsi_dispatch_cmd";
  491. lkdtm.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd;
  492. break;
  493. case CN_IDE_CORE_CP:
  494. #ifdef CONFIG_IDE
  495. lkdtm.kp.symbol_name = "generic_ide_ioctl";
  496. lkdtm.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl;
  497. #else
  498. pr_info("Crash point not available\n");
  499. return -EINVAL;
  500. #endif
  501. break;
  502. default:
  503. pr_info("Invalid Crash Point\n");
  504. return -EINVAL;
  505. }
  506. cpoint = which;
  507. if ((ret = register_jprobe(&lkdtm)) < 0) {
  508. pr_info("Couldn't register jprobe\n");
  509. cpoint = CN_INVALID;
  510. }
  511. return ret;
  512. }
  513. static ssize_t do_register_entry(enum cname which, struct file *f,
  514. const char __user *user_buf, size_t count, loff_t *off)
  515. {
  516. char *buf;
  517. int err;
  518. if (count >= PAGE_SIZE)
  519. return -EINVAL;
  520. buf = (char *)__get_free_page(GFP_KERNEL);
  521. if (!buf)
  522. return -ENOMEM;
  523. if (copy_from_user(buf, user_buf, count)) {
  524. free_page((unsigned long) buf);
  525. return -EFAULT;
  526. }
  527. /* NULL-terminate and remove enter */
  528. buf[count] = '\0';
  529. strim(buf);
  530. cptype = parse_cp_type(buf, count);
  531. free_page((unsigned long) buf);
  532. if (cptype == CT_NONE)
  533. return -EINVAL;
  534. err = lkdtm_register_cpoint(which);
  535. if (err < 0)
  536. return err;
  537. *off += count;
  538. return count;
  539. }
  540. /* Generic read callback that just prints out the available crash types */
  541. static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
  542. size_t count, loff_t *off)
  543. {
  544. char *buf;
  545. int i, n, out;
  546. buf = (char *)__get_free_page(GFP_KERNEL);
  547. if (buf == NULL)
  548. return -ENOMEM;
  549. n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
  550. for (i = 0; i < ARRAY_SIZE(cp_type); i++)
  551. n += snprintf(buf + n, PAGE_SIZE - n, "%s\n", cp_type[i]);
  552. buf[n] = '\0';
  553. out = simple_read_from_buffer(user_buf, count, off,
  554. buf, n);
  555. free_page((unsigned long) buf);
  556. return out;
  557. }
  558. static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
  559. {
  560. return 0;
  561. }
  562. static ssize_t int_hardware_entry(struct file *f, const char __user *buf,
  563. size_t count, loff_t *off)
  564. {
  565. return do_register_entry(CN_INT_HARDWARE_ENTRY, f, buf, count, off);
  566. }
  567. static ssize_t int_hw_irq_en(struct file *f, const char __user *buf,
  568. size_t count, loff_t *off)
  569. {
  570. return do_register_entry(CN_INT_HW_IRQ_EN, f, buf, count, off);
  571. }
  572. static ssize_t int_tasklet_entry(struct file *f, const char __user *buf,
  573. size_t count, loff_t *off)
  574. {
  575. return do_register_entry(CN_INT_TASKLET_ENTRY, f, buf, count, off);
  576. }
  577. static ssize_t fs_devrw_entry(struct file *f, const char __user *buf,
  578. size_t count, loff_t *off)
  579. {
  580. return do_register_entry(CN_FS_DEVRW, f, buf, count, off);
  581. }
  582. static ssize_t mem_swapout_entry(struct file *f, const char __user *buf,
  583. size_t count, loff_t *off)
  584. {
  585. return do_register_entry(CN_MEM_SWAPOUT, f, buf, count, off);
  586. }
  587. static ssize_t timeradd_entry(struct file *f, const char __user *buf,
  588. size_t count, loff_t *off)
  589. {
  590. return do_register_entry(CN_TIMERADD, f, buf, count, off);
  591. }
  592. static ssize_t scsi_dispatch_cmd_entry(struct file *f,
  593. const char __user *buf, size_t count, loff_t *off)
  594. {
  595. return do_register_entry(CN_SCSI_DISPATCH_CMD, f, buf, count, off);
  596. }
  597. static ssize_t ide_core_cp_entry(struct file *f, const char __user *buf,
  598. size_t count, loff_t *off)
  599. {
  600. return do_register_entry(CN_IDE_CORE_CP, f, buf, count, off);
  601. }
  602. /* Special entry to just crash directly. Available without KPROBEs */
  603. static ssize_t direct_entry(struct file *f, const char __user *user_buf,
  604. size_t count, loff_t *off)
  605. {
  606. enum ctype type;
  607. char *buf;
  608. if (count >= PAGE_SIZE)
  609. return -EINVAL;
  610. if (count < 1)
  611. return -EINVAL;
  612. buf = (char *)__get_free_page(GFP_KERNEL);
  613. if (!buf)
  614. return -ENOMEM;
  615. if (copy_from_user(buf, user_buf, count)) {
  616. free_page((unsigned long) buf);
  617. return -EFAULT;
  618. }
  619. /* NULL-terminate and remove enter */
  620. buf[count] = '\0';
  621. strim(buf);
  622. type = parse_cp_type(buf, count);
  623. free_page((unsigned long) buf);
  624. if (type == CT_NONE)
  625. return -EINVAL;
  626. pr_info("Performing direct entry %s\n", cp_type_to_str(type));
  627. lkdtm_do_action(type);
  628. *off += count;
  629. return count;
  630. }
  631. struct crash_entry {
  632. const char *name;
  633. const struct file_operations fops;
  634. };
  635. static const struct crash_entry crash_entries[] = {
  636. {"DIRECT", {.read = lkdtm_debugfs_read,
  637. .llseek = generic_file_llseek,
  638. .open = lkdtm_debugfs_open,
  639. .write = direct_entry} },
  640. {"INT_HARDWARE_ENTRY", {.read = lkdtm_debugfs_read,
  641. .llseek = generic_file_llseek,
  642. .open = lkdtm_debugfs_open,
  643. .write = int_hardware_entry} },
  644. {"INT_HW_IRQ_EN", {.read = lkdtm_debugfs_read,
  645. .llseek = generic_file_llseek,
  646. .open = lkdtm_debugfs_open,
  647. .write = int_hw_irq_en} },
  648. {"INT_TASKLET_ENTRY", {.read = lkdtm_debugfs_read,
  649. .llseek = generic_file_llseek,
  650. .open = lkdtm_debugfs_open,
  651. .write = int_tasklet_entry} },
  652. {"FS_DEVRW", {.read = lkdtm_debugfs_read,
  653. .llseek = generic_file_llseek,
  654. .open = lkdtm_debugfs_open,
  655. .write = fs_devrw_entry} },
  656. {"MEM_SWAPOUT", {.read = lkdtm_debugfs_read,
  657. .llseek = generic_file_llseek,
  658. .open = lkdtm_debugfs_open,
  659. .write = mem_swapout_entry} },
  660. {"TIMERADD", {.read = lkdtm_debugfs_read,
  661. .llseek = generic_file_llseek,
  662. .open = lkdtm_debugfs_open,
  663. .write = timeradd_entry} },
  664. {"SCSI_DISPATCH_CMD", {.read = lkdtm_debugfs_read,
  665. .llseek = generic_file_llseek,
  666. .open = lkdtm_debugfs_open,
  667. .write = scsi_dispatch_cmd_entry} },
  668. {"IDE_CORE_CP", {.read = lkdtm_debugfs_read,
  669. .llseek = generic_file_llseek,
  670. .open = lkdtm_debugfs_open,
  671. .write = ide_core_cp_entry} },
  672. };
  673. static struct dentry *lkdtm_debugfs_root;
  674. static int __init lkdtm_module_init(void)
  675. {
  676. int ret = -EINVAL;
  677. int n_debugfs_entries = 1; /* Assume only the direct entry */
  678. int i;
  679. /* Register debugfs interface */
  680. lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
  681. if (!lkdtm_debugfs_root) {
  682. pr_err("creating root dir failed\n");
  683. return -ENODEV;
  684. }
  685. #ifdef CONFIG_KPROBES
  686. n_debugfs_entries = ARRAY_SIZE(crash_entries);
  687. #endif
  688. for (i = 0; i < n_debugfs_entries; i++) {
  689. const struct crash_entry *cur = &crash_entries[i];
  690. struct dentry *de;
  691. de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
  692. NULL, &cur->fops);
  693. if (de == NULL) {
  694. pr_err("could not create %s\n", cur->name);
  695. goto out_err;
  696. }
  697. }
  698. if (lkdtm_parse_commandline() == -EINVAL) {
  699. pr_info("Invalid command\n");
  700. goto out_err;
  701. }
  702. if (cpoint != CN_INVALID && cptype != CT_NONE) {
  703. ret = lkdtm_register_cpoint(cpoint);
  704. if (ret < 0) {
  705. pr_info("Invalid crash point %d\n", cpoint);
  706. goto out_err;
  707. }
  708. pr_info("Crash point %s of type %s registered\n",
  709. cpoint_name, cpoint_type);
  710. } else {
  711. pr_info("No crash points registered, enable through debugfs\n");
  712. }
  713. return 0;
  714. out_err:
  715. debugfs_remove_recursive(lkdtm_debugfs_root);
  716. return ret;
  717. }
  718. static void __exit lkdtm_module_exit(void)
  719. {
  720. debugfs_remove_recursive(lkdtm_debugfs_root);
  721. unregister_jprobe(&lkdtm);
  722. pr_info("Crash point unregistered\n");
  723. }
  724. module_init(lkdtm_module_init);
  725. module_exit(lkdtm_module_exit);
  726. MODULE_LICENSE("GPL");