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