1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Linux Kernel Dump Test Module for testing kernel crashes conditions:
4*4882a593Smuzhiyun * induces system failures at predefined crashpoints and under predefined
5*4882a593Smuzhiyun * operational conditions in order to evaluate the reliability of kernel
6*4882a593Smuzhiyun * sanity checking and crash dumps obtained using different dumping
7*4882a593Smuzhiyun * solutions.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Copyright (C) IBM Corporation, 2006
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Author: Ankita Garg <ankita@in.ibm.com>
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * It is adapted from the Linux Kernel Dump Test Tool by
14*4882a593Smuzhiyun * Fernando Luis Vazquez Cao <http://lkdtt.sourceforge.net>
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * Debugfs support added by Simon Kagstrom <simon.kagstrom@netinsight.net>
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * See Documentation/fault-injection/provoke-crashes.rst for instructions
19*4882a593Smuzhiyun */
20*4882a593Smuzhiyun #include "lkdtm.h"
21*4882a593Smuzhiyun #include <linux/fs.h>
22*4882a593Smuzhiyun #include <linux/module.h>
23*4882a593Smuzhiyun #include <linux/buffer_head.h>
24*4882a593Smuzhiyun #include <linux/kprobes.h>
25*4882a593Smuzhiyun #include <linux/list.h>
26*4882a593Smuzhiyun #include <linux/init.h>
27*4882a593Smuzhiyun #include <linux/slab.h>
28*4882a593Smuzhiyun #include <linux/debugfs.h>
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #define DEFAULT_COUNT 10
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun static int lkdtm_debugfs_open(struct inode *inode, struct file *file);
33*4882a593Smuzhiyun static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
34*4882a593Smuzhiyun size_t count, loff_t *off);
35*4882a593Smuzhiyun static ssize_t direct_entry(struct file *f, const char __user *user_buf,
36*4882a593Smuzhiyun size_t count, loff_t *off);
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #ifdef CONFIG_KPROBES
39*4882a593Smuzhiyun static int lkdtm_kprobe_handler(struct kprobe *kp, struct pt_regs *regs);
40*4882a593Smuzhiyun static ssize_t lkdtm_debugfs_entry(struct file *f,
41*4882a593Smuzhiyun const char __user *user_buf,
42*4882a593Smuzhiyun size_t count, loff_t *off);
43*4882a593Smuzhiyun # define CRASHPOINT_KPROBE(_symbol) \
44*4882a593Smuzhiyun .kprobe = { \
45*4882a593Smuzhiyun .symbol_name = (_symbol), \
46*4882a593Smuzhiyun .pre_handler = lkdtm_kprobe_handler, \
47*4882a593Smuzhiyun },
48*4882a593Smuzhiyun # define CRASHPOINT_WRITE(_symbol) \
49*4882a593Smuzhiyun (_symbol) ? lkdtm_debugfs_entry : direct_entry
50*4882a593Smuzhiyun #else
51*4882a593Smuzhiyun # define CRASHPOINT_KPROBE(_symbol)
52*4882a593Smuzhiyun # define CRASHPOINT_WRITE(_symbol) direct_entry
53*4882a593Smuzhiyun #endif
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /* Crash points */
56*4882a593Smuzhiyun struct crashpoint {
57*4882a593Smuzhiyun const char *name;
58*4882a593Smuzhiyun const struct file_operations fops;
59*4882a593Smuzhiyun struct kprobe kprobe;
60*4882a593Smuzhiyun };
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun #define CRASHPOINT(_name, _symbol) \
63*4882a593Smuzhiyun { \
64*4882a593Smuzhiyun .name = _name, \
65*4882a593Smuzhiyun .fops = { \
66*4882a593Smuzhiyun .read = lkdtm_debugfs_read, \
67*4882a593Smuzhiyun .llseek = generic_file_llseek, \
68*4882a593Smuzhiyun .open = lkdtm_debugfs_open, \
69*4882a593Smuzhiyun .write = CRASHPOINT_WRITE(_symbol) \
70*4882a593Smuzhiyun }, \
71*4882a593Smuzhiyun CRASHPOINT_KPROBE(_symbol) \
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* Define the possible places where we can trigger a crash point. */
75*4882a593Smuzhiyun static struct crashpoint crashpoints[] = {
76*4882a593Smuzhiyun CRASHPOINT("DIRECT", NULL),
77*4882a593Smuzhiyun #ifdef CONFIG_KPROBES
78*4882a593Smuzhiyun CRASHPOINT("INT_HARDWARE_ENTRY", "do_IRQ"),
79*4882a593Smuzhiyun CRASHPOINT("INT_HW_IRQ_EN", "handle_irq_event"),
80*4882a593Smuzhiyun CRASHPOINT("INT_TASKLET_ENTRY", "tasklet_action"),
81*4882a593Smuzhiyun CRASHPOINT("FS_DEVRW", "ll_rw_block"),
82*4882a593Smuzhiyun CRASHPOINT("MEM_SWAPOUT", "shrink_inactive_list"),
83*4882a593Smuzhiyun CRASHPOINT("TIMERADD", "hrtimer_start"),
84*4882a593Smuzhiyun CRASHPOINT("SCSI_QUEUE_RQ", "scsi_queue_rq"),
85*4882a593Smuzhiyun CRASHPOINT("IDE_CORE_CP", "generic_ide_ioctl"),
86*4882a593Smuzhiyun #endif
87*4882a593Smuzhiyun };
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* Crash types. */
91*4882a593Smuzhiyun struct crashtype {
92*4882a593Smuzhiyun const char *name;
93*4882a593Smuzhiyun void (*func)(void);
94*4882a593Smuzhiyun };
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun #define CRASHTYPE(_name) \
97*4882a593Smuzhiyun { \
98*4882a593Smuzhiyun .name = __stringify(_name), \
99*4882a593Smuzhiyun .func = lkdtm_ ## _name, \
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /* Define the possible types of crashes that can be triggered. */
103*4882a593Smuzhiyun static const struct crashtype crashtypes[] = {
104*4882a593Smuzhiyun CRASHTYPE(PANIC),
105*4882a593Smuzhiyun CRASHTYPE(BUG),
106*4882a593Smuzhiyun CRASHTYPE(WARNING),
107*4882a593Smuzhiyun CRASHTYPE(WARNING_MESSAGE),
108*4882a593Smuzhiyun CRASHTYPE(EXCEPTION),
109*4882a593Smuzhiyun CRASHTYPE(LOOP),
110*4882a593Smuzhiyun CRASHTYPE(EXHAUST_STACK),
111*4882a593Smuzhiyun CRASHTYPE(CORRUPT_STACK),
112*4882a593Smuzhiyun CRASHTYPE(CORRUPT_STACK_STRONG),
113*4882a593Smuzhiyun CRASHTYPE(CORRUPT_LIST_ADD),
114*4882a593Smuzhiyun CRASHTYPE(CORRUPT_LIST_DEL),
115*4882a593Smuzhiyun CRASHTYPE(STACK_GUARD_PAGE_LEADING),
116*4882a593Smuzhiyun CRASHTYPE(STACK_GUARD_PAGE_TRAILING),
117*4882a593Smuzhiyun CRASHTYPE(UNSET_SMEP),
118*4882a593Smuzhiyun CRASHTYPE(CORRUPT_PAC),
119*4882a593Smuzhiyun CRASHTYPE(UNALIGNED_LOAD_STORE_WRITE),
120*4882a593Smuzhiyun CRASHTYPE(OVERWRITE_ALLOCATION),
121*4882a593Smuzhiyun CRASHTYPE(WRITE_AFTER_FREE),
122*4882a593Smuzhiyun CRASHTYPE(READ_AFTER_FREE),
123*4882a593Smuzhiyun CRASHTYPE(WRITE_BUDDY_AFTER_FREE),
124*4882a593Smuzhiyun CRASHTYPE(READ_BUDDY_AFTER_FREE),
125*4882a593Smuzhiyun CRASHTYPE(SLAB_FREE_DOUBLE),
126*4882a593Smuzhiyun CRASHTYPE(SLAB_FREE_CROSS),
127*4882a593Smuzhiyun CRASHTYPE(SLAB_FREE_PAGE),
128*4882a593Smuzhiyun CRASHTYPE(SOFTLOCKUP),
129*4882a593Smuzhiyun CRASHTYPE(HARDLOCKUP),
130*4882a593Smuzhiyun CRASHTYPE(SPINLOCKUP),
131*4882a593Smuzhiyun CRASHTYPE(HUNG_TASK),
132*4882a593Smuzhiyun CRASHTYPE(OVERFLOW_SIGNED),
133*4882a593Smuzhiyun CRASHTYPE(OVERFLOW_UNSIGNED),
134*4882a593Smuzhiyun CRASHTYPE(ARRAY_BOUNDS),
135*4882a593Smuzhiyun CRASHTYPE(EXEC_DATA),
136*4882a593Smuzhiyun CRASHTYPE(EXEC_STACK),
137*4882a593Smuzhiyun CRASHTYPE(EXEC_KMALLOC),
138*4882a593Smuzhiyun CRASHTYPE(EXEC_VMALLOC),
139*4882a593Smuzhiyun CRASHTYPE(EXEC_RODATA),
140*4882a593Smuzhiyun CRASHTYPE(EXEC_USERSPACE),
141*4882a593Smuzhiyun CRASHTYPE(EXEC_NULL),
142*4882a593Smuzhiyun CRASHTYPE(ACCESS_USERSPACE),
143*4882a593Smuzhiyun CRASHTYPE(ACCESS_NULL),
144*4882a593Smuzhiyun CRASHTYPE(WRITE_RO),
145*4882a593Smuzhiyun CRASHTYPE(WRITE_RO_AFTER_INIT),
146*4882a593Smuzhiyun CRASHTYPE(WRITE_KERN),
147*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_INC_OVERFLOW),
148*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_ADD_OVERFLOW),
149*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_INC_NOT_ZERO_OVERFLOW),
150*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_OVERFLOW),
151*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_DEC_ZERO),
152*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_DEC_NEGATIVE),
153*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_DEC_AND_TEST_NEGATIVE),
154*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_SUB_AND_TEST_NEGATIVE),
155*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_INC_ZERO),
156*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_ADD_ZERO),
157*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_INC_SATURATED),
158*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_DEC_SATURATED),
159*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_ADD_SATURATED),
160*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_INC_NOT_ZERO_SATURATED),
161*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_SATURATED),
162*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_DEC_AND_TEST_SATURATED),
163*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_SUB_AND_TEST_SATURATED),
164*4882a593Smuzhiyun CRASHTYPE(REFCOUNT_TIMING),
165*4882a593Smuzhiyun CRASHTYPE(ATOMIC_TIMING),
166*4882a593Smuzhiyun CRASHTYPE(USERCOPY_HEAP_SIZE_TO),
167*4882a593Smuzhiyun CRASHTYPE(USERCOPY_HEAP_SIZE_FROM),
168*4882a593Smuzhiyun CRASHTYPE(USERCOPY_HEAP_WHITELIST_TO),
169*4882a593Smuzhiyun CRASHTYPE(USERCOPY_HEAP_WHITELIST_FROM),
170*4882a593Smuzhiyun CRASHTYPE(USERCOPY_STACK_FRAME_TO),
171*4882a593Smuzhiyun CRASHTYPE(USERCOPY_STACK_FRAME_FROM),
172*4882a593Smuzhiyun CRASHTYPE(USERCOPY_STACK_BEYOND),
173*4882a593Smuzhiyun CRASHTYPE(USERCOPY_KERNEL),
174*4882a593Smuzhiyun CRASHTYPE(STACKLEAK_ERASING),
175*4882a593Smuzhiyun CRASHTYPE(CFI_FORWARD_PROTO),
176*4882a593Smuzhiyun CRASHTYPE(DOUBLE_FAULT),
177*4882a593Smuzhiyun };
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /* Global kprobe entry and crashtype. */
181*4882a593Smuzhiyun static struct kprobe *lkdtm_kprobe;
182*4882a593Smuzhiyun static struct crashpoint *lkdtm_crashpoint;
183*4882a593Smuzhiyun static const struct crashtype *lkdtm_crashtype;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /* Module parameters */
186*4882a593Smuzhiyun static int recur_count = -1;
187*4882a593Smuzhiyun module_param(recur_count, int, 0644);
188*4882a593Smuzhiyun MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun static char* cpoint_name;
191*4882a593Smuzhiyun module_param(cpoint_name, charp, 0444);
192*4882a593Smuzhiyun MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun static char* cpoint_type;
195*4882a593Smuzhiyun module_param(cpoint_type, charp, 0444);
196*4882a593Smuzhiyun MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
197*4882a593Smuzhiyun "hitting the crash point");
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun static int cpoint_count = DEFAULT_COUNT;
200*4882a593Smuzhiyun module_param(cpoint_count, int, 0644);
201*4882a593Smuzhiyun MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
202*4882a593Smuzhiyun "crash point is to be hit to trigger action");
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun /* Return the crashtype number or NULL if the name is invalid */
find_crashtype(const char * name)206*4882a593Smuzhiyun static const struct crashtype *find_crashtype(const char *name)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun int i;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(crashtypes); i++) {
211*4882a593Smuzhiyun if (!strcmp(name, crashtypes[i].name))
212*4882a593Smuzhiyun return &crashtypes[i];
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun return NULL;
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /*
219*4882a593Smuzhiyun * This is forced noinline just so it distinctly shows up in the stackdump
220*4882a593Smuzhiyun * which makes validation of expected lkdtm crashes easier.
221*4882a593Smuzhiyun */
lkdtm_do_action(const struct crashtype * crashtype)222*4882a593Smuzhiyun static noinline void lkdtm_do_action(const struct crashtype *crashtype)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun if (WARN_ON(!crashtype || !crashtype->func))
225*4882a593Smuzhiyun return;
226*4882a593Smuzhiyun crashtype->func();
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
lkdtm_register_cpoint(struct crashpoint * crashpoint,const struct crashtype * crashtype)229*4882a593Smuzhiyun static int lkdtm_register_cpoint(struct crashpoint *crashpoint,
230*4882a593Smuzhiyun const struct crashtype *crashtype)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun int ret;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /* If this doesn't have a symbol, just call immediately. */
235*4882a593Smuzhiyun if (!crashpoint->kprobe.symbol_name) {
236*4882a593Smuzhiyun lkdtm_do_action(crashtype);
237*4882a593Smuzhiyun return 0;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun if (lkdtm_kprobe != NULL)
241*4882a593Smuzhiyun unregister_kprobe(lkdtm_kprobe);
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun lkdtm_crashpoint = crashpoint;
244*4882a593Smuzhiyun lkdtm_crashtype = crashtype;
245*4882a593Smuzhiyun lkdtm_kprobe = &crashpoint->kprobe;
246*4882a593Smuzhiyun ret = register_kprobe(lkdtm_kprobe);
247*4882a593Smuzhiyun if (ret < 0) {
248*4882a593Smuzhiyun pr_info("Couldn't register kprobe %s\n",
249*4882a593Smuzhiyun crashpoint->kprobe.symbol_name);
250*4882a593Smuzhiyun lkdtm_kprobe = NULL;
251*4882a593Smuzhiyun lkdtm_crashpoint = NULL;
252*4882a593Smuzhiyun lkdtm_crashtype = NULL;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun return ret;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun #ifdef CONFIG_KPROBES
259*4882a593Smuzhiyun /* Global crash counter and spinlock. */
260*4882a593Smuzhiyun static int crash_count = DEFAULT_COUNT;
261*4882a593Smuzhiyun static DEFINE_SPINLOCK(crash_count_lock);
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /* Called by kprobe entry points. */
lkdtm_kprobe_handler(struct kprobe * kp,struct pt_regs * regs)264*4882a593Smuzhiyun static int lkdtm_kprobe_handler(struct kprobe *kp, struct pt_regs *regs)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun unsigned long flags;
267*4882a593Smuzhiyun bool do_it = false;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun if (WARN_ON(!lkdtm_crashpoint || !lkdtm_crashtype))
270*4882a593Smuzhiyun return 0;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun spin_lock_irqsave(&crash_count_lock, flags);
273*4882a593Smuzhiyun crash_count--;
274*4882a593Smuzhiyun pr_info("Crash point %s of type %s hit, trigger in %d rounds\n",
275*4882a593Smuzhiyun lkdtm_crashpoint->name, lkdtm_crashtype->name, crash_count);
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun if (crash_count == 0) {
278*4882a593Smuzhiyun do_it = true;
279*4882a593Smuzhiyun crash_count = cpoint_count;
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun spin_unlock_irqrestore(&crash_count_lock, flags);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun if (do_it)
284*4882a593Smuzhiyun lkdtm_do_action(lkdtm_crashtype);
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun return 0;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun
lkdtm_debugfs_entry(struct file * f,const char __user * user_buf,size_t count,loff_t * off)289*4882a593Smuzhiyun static ssize_t lkdtm_debugfs_entry(struct file *f,
290*4882a593Smuzhiyun const char __user *user_buf,
291*4882a593Smuzhiyun size_t count, loff_t *off)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun struct crashpoint *crashpoint = file_inode(f)->i_private;
294*4882a593Smuzhiyun const struct crashtype *crashtype = NULL;
295*4882a593Smuzhiyun char *buf;
296*4882a593Smuzhiyun int err;
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun if (count >= PAGE_SIZE)
299*4882a593Smuzhiyun return -EINVAL;
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun buf = (char *)__get_free_page(GFP_KERNEL);
302*4882a593Smuzhiyun if (!buf)
303*4882a593Smuzhiyun return -ENOMEM;
304*4882a593Smuzhiyun if (copy_from_user(buf, user_buf, count)) {
305*4882a593Smuzhiyun free_page((unsigned long) buf);
306*4882a593Smuzhiyun return -EFAULT;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun /* NULL-terminate and remove enter */
309*4882a593Smuzhiyun buf[count] = '\0';
310*4882a593Smuzhiyun strim(buf);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun crashtype = find_crashtype(buf);
313*4882a593Smuzhiyun free_page((unsigned long)buf);
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun if (!crashtype)
316*4882a593Smuzhiyun return -EINVAL;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun err = lkdtm_register_cpoint(crashpoint, crashtype);
319*4882a593Smuzhiyun if (err < 0)
320*4882a593Smuzhiyun return err;
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun *off += count;
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun return count;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun #endif
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun /* Generic read callback that just prints out the available crash types */
lkdtm_debugfs_read(struct file * f,char __user * user_buf,size_t count,loff_t * off)329*4882a593Smuzhiyun static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
330*4882a593Smuzhiyun size_t count, loff_t *off)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun char *buf;
333*4882a593Smuzhiyun int i, n, out;
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun buf = (char *)__get_free_page(GFP_KERNEL);
336*4882a593Smuzhiyun if (buf == NULL)
337*4882a593Smuzhiyun return -ENOMEM;
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun n = scnprintf(buf, PAGE_SIZE, "Available crash types:\n");
340*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(crashtypes); i++) {
341*4882a593Smuzhiyun n += scnprintf(buf + n, PAGE_SIZE - n, "%s\n",
342*4882a593Smuzhiyun crashtypes[i].name);
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun buf[n] = '\0';
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun out = simple_read_from_buffer(user_buf, count, off,
347*4882a593Smuzhiyun buf, n);
348*4882a593Smuzhiyun free_page((unsigned long) buf);
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun return out;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
lkdtm_debugfs_open(struct inode * inode,struct file * file)353*4882a593Smuzhiyun static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun return 0;
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun /* Special entry to just crash directly. Available without KPROBEs */
direct_entry(struct file * f,const char __user * user_buf,size_t count,loff_t * off)359*4882a593Smuzhiyun static ssize_t direct_entry(struct file *f, const char __user *user_buf,
360*4882a593Smuzhiyun size_t count, loff_t *off)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun const struct crashtype *crashtype;
363*4882a593Smuzhiyun char *buf;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun if (count >= PAGE_SIZE)
366*4882a593Smuzhiyun return -EINVAL;
367*4882a593Smuzhiyun if (count < 1)
368*4882a593Smuzhiyun return -EINVAL;
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun buf = (char *)__get_free_page(GFP_KERNEL);
371*4882a593Smuzhiyun if (!buf)
372*4882a593Smuzhiyun return -ENOMEM;
373*4882a593Smuzhiyun if (copy_from_user(buf, user_buf, count)) {
374*4882a593Smuzhiyun free_page((unsigned long) buf);
375*4882a593Smuzhiyun return -EFAULT;
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun /* NULL-terminate and remove enter */
378*4882a593Smuzhiyun buf[count] = '\0';
379*4882a593Smuzhiyun strim(buf);
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun crashtype = find_crashtype(buf);
382*4882a593Smuzhiyun free_page((unsigned long) buf);
383*4882a593Smuzhiyun if (!crashtype)
384*4882a593Smuzhiyun return -EINVAL;
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun pr_info("Performing direct entry %s\n", crashtype->name);
387*4882a593Smuzhiyun lkdtm_do_action(crashtype);
388*4882a593Smuzhiyun *off += count;
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun return count;
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun static struct dentry *lkdtm_debugfs_root;
394*4882a593Smuzhiyun
lkdtm_module_init(void)395*4882a593Smuzhiyun static int __init lkdtm_module_init(void)
396*4882a593Smuzhiyun {
397*4882a593Smuzhiyun struct crashpoint *crashpoint = NULL;
398*4882a593Smuzhiyun const struct crashtype *crashtype = NULL;
399*4882a593Smuzhiyun int ret;
400*4882a593Smuzhiyun int i;
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun /* Neither or both of these need to be set */
403*4882a593Smuzhiyun if ((cpoint_type || cpoint_name) && !(cpoint_type && cpoint_name)) {
404*4882a593Smuzhiyun pr_err("Need both cpoint_type and cpoint_name or neither\n");
405*4882a593Smuzhiyun return -EINVAL;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun if (cpoint_type) {
409*4882a593Smuzhiyun crashtype = find_crashtype(cpoint_type);
410*4882a593Smuzhiyun if (!crashtype) {
411*4882a593Smuzhiyun pr_err("Unknown crashtype '%s'\n", cpoint_type);
412*4882a593Smuzhiyun return -EINVAL;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun if (cpoint_name) {
417*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
418*4882a593Smuzhiyun if (!strcmp(cpoint_name, crashpoints[i].name))
419*4882a593Smuzhiyun crashpoint = &crashpoints[i];
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun /* Refuse unknown crashpoints. */
423*4882a593Smuzhiyun if (!crashpoint) {
424*4882a593Smuzhiyun pr_err("Invalid crashpoint %s\n", cpoint_name);
425*4882a593Smuzhiyun return -EINVAL;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun #ifdef CONFIG_KPROBES
430*4882a593Smuzhiyun /* Set crash count. */
431*4882a593Smuzhiyun crash_count = cpoint_count;
432*4882a593Smuzhiyun #endif
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun /* Handle test-specific initialization. */
435*4882a593Smuzhiyun lkdtm_bugs_init(&recur_count);
436*4882a593Smuzhiyun lkdtm_perms_init();
437*4882a593Smuzhiyun lkdtm_usercopy_init();
438*4882a593Smuzhiyun lkdtm_heap_init();
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun /* Register debugfs interface */
441*4882a593Smuzhiyun lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun /* Install debugfs trigger files. */
444*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
445*4882a593Smuzhiyun struct crashpoint *cur = &crashpoints[i];
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root, cur,
448*4882a593Smuzhiyun &cur->fops);
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun /* Install crashpoint if one was selected. */
452*4882a593Smuzhiyun if (crashpoint) {
453*4882a593Smuzhiyun ret = lkdtm_register_cpoint(crashpoint, crashtype);
454*4882a593Smuzhiyun if (ret < 0) {
455*4882a593Smuzhiyun pr_info("Invalid crashpoint %s\n", crashpoint->name);
456*4882a593Smuzhiyun goto out_err;
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun pr_info("Crash point %s of type %s registered\n",
459*4882a593Smuzhiyun crashpoint->name, cpoint_type);
460*4882a593Smuzhiyun } else {
461*4882a593Smuzhiyun pr_info("No crash points registered, enable through debugfs\n");
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun return 0;
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun out_err:
467*4882a593Smuzhiyun debugfs_remove_recursive(lkdtm_debugfs_root);
468*4882a593Smuzhiyun return ret;
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun
lkdtm_module_exit(void)471*4882a593Smuzhiyun static void __exit lkdtm_module_exit(void)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun debugfs_remove_recursive(lkdtm_debugfs_root);
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun /* Handle test-specific clean-up. */
476*4882a593Smuzhiyun lkdtm_heap_exit();
477*4882a593Smuzhiyun lkdtm_usercopy_exit();
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun if (lkdtm_kprobe != NULL)
480*4882a593Smuzhiyun unregister_kprobe(lkdtm_kprobe);
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun pr_info("Crash point unregistered\n");
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun module_init(lkdtm_module_init);
486*4882a593Smuzhiyun module_exit(lkdtm_module_exit);
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun MODULE_LICENSE("GPL");
489*4882a593Smuzhiyun MODULE_DESCRIPTION("Kernel crash testing module");
490