lkdtm.c 23 KB

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