lkdtm_core.c 22 KB

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