lkdtm.c 20 KB

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