blob: d30301149242c92cefdf5532e3cce52be397407e [file] [log] [blame]
Ankita Garg8bb31b92006-10-02 02:17:36 -07001/*
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 *
Simon Kagstrom0347af42010-03-05 13:42:49 -080029 * Debugfs support added by Simon Kagstrom <simon.kagstrom@netinsight.net>
Ankita Garg8bb31b92006-10-02 02:17:36 -070030 *
Simon Kagstrom0347af42010-03-05 13:42:49 -080031 * See Documentation/fault-injection/provoke-crashes.txt for instructions
Ankita Garg8bb31b92006-10-02 02:17:36 -070032 */
Kees Cookfeac6e22014-02-09 13:48:46 -080033#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Ankita Garg8bb31b92006-10-02 02:17:36 -070034
35#include <linux/kernel.h>
Randy Dunlap5d861d92006-11-02 22:07:06 -080036#include <linux/fs.h>
Ankita Garg8bb31b92006-10-02 02:17:36 -070037#include <linux/module.h>
Randy Dunlap5d861d92006-11-02 22:07:06 -080038#include <linux/buffer_head.h>
Ankita Garg8bb31b92006-10-02 02:17:36 -070039#include <linux/kprobes.h>
Randy Dunlap5d861d92006-11-02 22:07:06 -080040#include <linux/list.h>
Ankita Garg8bb31b92006-10-02 02:17:36 -070041#include <linux/init.h>
Ankita Garg8bb31b92006-10-02 02:17:36 -070042#include <linux/interrupt.h>
Randy Dunlap5d861d92006-11-02 22:07:06 -080043#include <linux/hrtimer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Ankita Garg8bb31b92006-10-02 02:17:36 -070045#include <scsi/scsi_cmnd.h>
Simon Kagstrom0347af42010-03-05 13:42:49 -080046#include <linux/debugfs.h>
Kees Cookcc33c5372013-07-08 10:01:33 -070047#include <linux/vmalloc.h>
Kees Cook9ae113c2013-10-24 09:25:57 -070048#include <linux/mman.h>
Ankita Garg8bb31b92006-10-02 02:17:36 -070049
50#ifdef CONFIG_IDE
51#include <linux/ide.h>
52#endif
53
Kees Cook7d196ac2013-10-24 09:25:39 -070054/*
55 * Make sure our attempts to over run the kernel stack doesn't trigger
56 * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
57 * recurse past the end of THREAD_SIZE by default.
58 */
59#if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
60#define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
61#else
62#define REC_STACK_SIZE (THREAD_SIZE / 8)
63#endif
64#define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
65
Ankita Garg8bb31b92006-10-02 02:17:36 -070066#define DEFAULT_COUNT 10
Kees Cookcc33c5372013-07-08 10:01:33 -070067#define EXEC_SIZE 64
Ankita Garg8bb31b92006-10-02 02:17:36 -070068
69enum cname {
Namhyung Kim93e2f582010-10-26 14:22:40 -070070 CN_INVALID,
71 CN_INT_HARDWARE_ENTRY,
72 CN_INT_HW_IRQ_EN,
73 CN_INT_TASKLET_ENTRY,
74 CN_FS_DEVRW,
75 CN_MEM_SWAPOUT,
76 CN_TIMERADD,
77 CN_SCSI_DISPATCH_CMD,
78 CN_IDE_CORE_CP,
79 CN_DIRECT,
Ankita Garg8bb31b92006-10-02 02:17:36 -070080};
81
82enum ctype {
Namhyung Kim93e2f582010-10-26 14:22:40 -070083 CT_NONE,
84 CT_PANIC,
85 CT_BUG,
Kees Cook65892722013-07-08 10:01:31 -070086 CT_WARNING,
Namhyung Kim93e2f582010-10-26 14:22:40 -070087 CT_EXCEPTION,
88 CT_LOOP,
89 CT_OVERFLOW,
90 CT_CORRUPT_STACK,
91 CT_UNALIGNED_LOAD_STORE_WRITE,
92 CT_OVERWRITE_ALLOCATION,
93 CT_WRITE_AFTER_FREE,
94 CT_SOFTLOCKUP,
95 CT_HARDLOCKUP,
Kees Cook274a5852013-07-08 10:01:32 -070096 CT_SPINLOCKUP,
Namhyung Kim93e2f582010-10-26 14:22:40 -070097 CT_HUNG_TASK,
Kees Cookcc33c5372013-07-08 10:01:33 -070098 CT_EXEC_DATA,
99 CT_EXEC_STACK,
100 CT_EXEC_KMALLOC,
101 CT_EXEC_VMALLOC,
Kees Cook9ae113c2013-10-24 09:25:57 -0700102 CT_EXEC_USERSPACE,
103 CT_ACCESS_USERSPACE,
104 CT_WRITE_RO,
Kees Cookdc2b9e92014-02-09 13:48:48 -0800105 CT_WRITE_KERN,
Ankita Garg8bb31b92006-10-02 02:17:36 -0700106};
107
108static char* cp_name[] = {
109 "INT_HARDWARE_ENTRY",
110 "INT_HW_IRQ_EN",
111 "INT_TASKLET_ENTRY",
112 "FS_DEVRW",
113 "MEM_SWAPOUT",
114 "TIMERADD",
115 "SCSI_DISPATCH_CMD",
Simon Kagstrom0347af42010-03-05 13:42:49 -0800116 "IDE_CORE_CP",
117 "DIRECT",
Ankita Garg8bb31b92006-10-02 02:17:36 -0700118};
119
120static char* cp_type[] = {
121 "PANIC",
122 "BUG",
Kees Cook65892722013-07-08 10:01:31 -0700123 "WARNING",
Ankita Garg8bb31b92006-10-02 02:17:36 -0700124 "EXCEPTION",
125 "LOOP",
Simon Kagstrom0347af42010-03-05 13:42:49 -0800126 "OVERFLOW",
127 "CORRUPT_STACK",
128 "UNALIGNED_LOAD_STORE_WRITE",
129 "OVERWRITE_ALLOCATION",
130 "WRITE_AFTER_FREE",
Frederic Weisbeckera48223f2010-05-26 14:44:29 -0700131 "SOFTLOCKUP",
132 "HARDLOCKUP",
Kees Cook274a5852013-07-08 10:01:32 -0700133 "SPINLOCKUP",
Frederic Weisbeckera48223f2010-05-26 14:44:29 -0700134 "HUNG_TASK",
Kees Cookcc33c5372013-07-08 10:01:33 -0700135 "EXEC_DATA",
136 "EXEC_STACK",
137 "EXEC_KMALLOC",
138 "EXEC_VMALLOC",
Kees Cook9ae113c2013-10-24 09:25:57 -0700139 "EXEC_USERSPACE",
140 "ACCESS_USERSPACE",
141 "WRITE_RO",
Kees Cookdc2b9e92014-02-09 13:48:48 -0800142 "WRITE_KERN",
Ankita Garg8bb31b92006-10-02 02:17:36 -0700143};
144
145static struct jprobe lkdtm;
146
147static int lkdtm_parse_commandline(void);
148static void lkdtm_handler(void);
149
Al Viroec1c6202007-02-09 16:05:17 +0000150static char* cpoint_name;
151static char* cpoint_type;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700152static int cpoint_count = DEFAULT_COUNT;
153static int recur_count = REC_NUM_DEFAULT;
154
Namhyung Kim93e2f582010-10-26 14:22:40 -0700155static enum cname cpoint = CN_INVALID;
156static enum ctype cptype = CT_NONE;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700157static int count = DEFAULT_COUNT;
Josh Huntaa2c96d2011-06-27 16:18:08 -0700158static DEFINE_SPINLOCK(count_lock);
Kees Cook274a5852013-07-08 10:01:32 -0700159static DEFINE_SPINLOCK(lock_me_up);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700160
Kees Cookcc33c5372013-07-08 10:01:33 -0700161static u8 data_area[EXEC_SIZE];
162
Kees Cook9ae113c2013-10-24 09:25:57 -0700163static const unsigned long rodata = 0xAA55AA55;
164
Ankita Garg8bb31b92006-10-02 02:17:36 -0700165module_param(recur_count, int, 0644);
Kees Cook7d196ac2013-10-24 09:25:39 -0700166MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
Rusty Russelldca41302010-08-11 23:04:21 -0600167module_param(cpoint_name, charp, 0444);
Randy Dunlap5d861d92006-11-02 22:07:06 -0800168MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
Rusty Russelldca41302010-08-11 23:04:21 -0600169module_param(cpoint_type, charp, 0444);
Randy Dunlap5d861d92006-11-02 22:07:06 -0800170MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
171 "hitting the crash point");
172module_param(cpoint_count, int, 0644);
173MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
174 "crash point is to be hit to trigger action");
Ankita Garg8bb31b92006-10-02 02:17:36 -0700175
Adrian Bunk21181162008-02-06 01:36:50 -0800176static unsigned int jp_do_irq(unsigned int irq)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700177{
178 lkdtm_handler();
179 jprobe_return();
180 return 0;
181}
182
Adrian Bunk21181162008-02-06 01:36:50 -0800183static irqreturn_t jp_handle_irq_event(unsigned int irq,
184 struct irqaction *action)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700185{
186 lkdtm_handler();
187 jprobe_return();
188 return 0;
189}
190
Adrian Bunk21181162008-02-06 01:36:50 -0800191static void jp_tasklet_action(struct softirq_action *a)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700192{
193 lkdtm_handler();
194 jprobe_return();
195}
196
Adrian Bunk21181162008-02-06 01:36:50 -0800197static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
Ankita Garg8bb31b92006-10-02 02:17:36 -0700198{
199 lkdtm_handler();
200 jprobe_return();
201}
202
203struct scan_control;
204
Adrian Bunk21181162008-02-06 01:36:50 -0800205static unsigned long jp_shrink_inactive_list(unsigned long max_scan,
206 struct zone *zone,
207 struct scan_control *sc)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700208{
209 lkdtm_handler();
210 jprobe_return();
211 return 0;
212}
213
Adrian Bunk21181162008-02-06 01:36:50 -0800214static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim,
215 const enum hrtimer_mode mode)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700216{
217 lkdtm_handler();
218 jprobe_return();
219 return 0;
220}
221
Adrian Bunk21181162008-02-06 01:36:50 -0800222static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700223{
224 lkdtm_handler();
225 jprobe_return();
226 return 0;
227}
228
229#ifdef CONFIG_IDE
Rashika Kheria44629432013-12-13 12:29:42 +0530230static int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file,
Ankita Garg8bb31b92006-10-02 02:17:36 -0700231 struct block_device *bdev, unsigned int cmd,
232 unsigned long arg)
233{
234 lkdtm_handler();
235 jprobe_return();
236 return 0;
237}
238#endif
239
Simon Kagstrom0347af42010-03-05 13:42:49 -0800240/* Return the crashpoint number or NONE if the name is invalid */
241static enum ctype parse_cp_type(const char *what, size_t count)
242{
243 int i;
244
245 for (i = 0; i < ARRAY_SIZE(cp_type); i++) {
246 if (!strcmp(what, cp_type[i]))
247 return i + 1;
248 }
249
Namhyung Kim93e2f582010-10-26 14:22:40 -0700250 return CT_NONE;
Simon Kagstrom0347af42010-03-05 13:42:49 -0800251}
252
253static const char *cp_type_to_str(enum ctype type)
254{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700255 if (type == CT_NONE || type < 0 || type > ARRAY_SIZE(cp_type))
Simon Kagstrom0347af42010-03-05 13:42:49 -0800256 return "None";
257
258 return cp_type[type - 1];
259}
260
261static const char *cp_name_to_str(enum cname name)
262{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700263 if (name == CN_INVALID || name < 0 || name > ARRAY_SIZE(cp_name))
Simon Kagstrom0347af42010-03-05 13:42:49 -0800264 return "INVALID";
265
266 return cp_name[name - 1];
267}
268
269
Ankita Garg8bb31b92006-10-02 02:17:36 -0700270static int lkdtm_parse_commandline(void)
271{
272 int i;
Josh Huntaa2c96d2011-06-27 16:18:08 -0700273 unsigned long flags;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700274
Simon Kagstrom0347af42010-03-05 13:42:49 -0800275 if (cpoint_count < 1 || recur_count < 1)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700276 return -EINVAL;
277
Josh Huntaa2c96d2011-06-27 16:18:08 -0700278 spin_lock_irqsave(&count_lock, flags);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700279 count = cpoint_count;
Josh Huntaa2c96d2011-06-27 16:18:08 -0700280 spin_unlock_irqrestore(&count_lock, flags);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700281
Simon Kagstrom0347af42010-03-05 13:42:49 -0800282 /* No special parameters */
283 if (!cpoint_type && !cpoint_name)
284 return 0;
285
286 /* Neither or both of these need to be set */
287 if (!cpoint_type || !cpoint_name)
288 return -EINVAL;
289
290 cptype = parse_cp_type(cpoint_type, strlen(cpoint_type));
Namhyung Kim93e2f582010-10-26 14:22:40 -0700291 if (cptype == CT_NONE)
Simon Kagstrom0347af42010-03-05 13:42:49 -0800292 return -EINVAL;
293
294 for (i = 0; i < ARRAY_SIZE(cp_name); i++) {
295 if (!strcmp(cpoint_name, cp_name[i])) {
296 cpoint = i + 1;
297 return 0;
298 }
299 }
300
301 /* Could not find a valid crash point */
302 return -EINVAL;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700303}
304
Kees Cook7d196ac2013-10-24 09:25:39 -0700305static int recursive_loop(int remaining)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700306{
Kees Cook7d196ac2013-10-24 09:25:39 -0700307 char buf[REC_STACK_SIZE];
Ankita Garg8bb31b92006-10-02 02:17:36 -0700308
Kees Cook7d196ac2013-10-24 09:25:39 -0700309 /* Make sure compiler does not optimize this away. */
310 memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE);
311 if (!remaining)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700312 return 0;
313 else
Kees Cook7d196ac2013-10-24 09:25:39 -0700314 return recursive_loop(remaining - 1);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700315}
316
Kees Cookcc33c5372013-07-08 10:01:33 -0700317static void do_nothing(void)
318{
319 return;
320}
321
Kees Cookdc2b9e92014-02-09 13:48:48 -0800322/* Must immediately follow do_nothing for size calculuations to work out. */
323static void do_overwritten(void)
324{
325 pr_info("do_overwritten wasn't overwritten!\n");
326 return;
327}
328
Kees Cook629c66a2013-10-24 18:05:42 -0700329static noinline void corrupt_stack(void)
330{
331 /* Use default char array length that triggers stack protection. */
332 char data[8];
333
334 memset((void *)data, 0, 64);
335}
336
Kees Cookcc33c5372013-07-08 10:01:33 -0700337static void execute_location(void *dst)
338{
339 void (*func)(void) = dst;
340
Kees Cookaac416f2014-02-09 13:48:47 -0800341 pr_info("attempting ok execution at %p\n", do_nothing);
342 do_nothing();
343
Kees Cookcc33c5372013-07-08 10:01:33 -0700344 memcpy(dst, do_nothing, EXEC_SIZE);
Kees Cookaac416f2014-02-09 13:48:47 -0800345 flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE);
346 pr_info("attempting bad execution at %p\n", func);
Kees Cookcc33c5372013-07-08 10:01:33 -0700347 func();
348}
349
Kees Cook9ae113c2013-10-24 09:25:57 -0700350static void execute_user_location(void *dst)
351{
Kees Cook51236622013-11-11 11:23:49 -0800352 /* Intentionally crossing kernel/user memory boundary. */
Kees Cook9ae113c2013-10-24 09:25:57 -0700353 void (*func)(void) = dst;
354
Kees Cookaac416f2014-02-09 13:48:47 -0800355 pr_info("attempting ok execution at %p\n", do_nothing);
356 do_nothing();
357
Kees Cook51236622013-11-11 11:23:49 -0800358 if (copy_to_user((void __user *)dst, do_nothing, EXEC_SIZE))
Kees Cook9ae113c2013-10-24 09:25:57 -0700359 return;
Kees Cookaac416f2014-02-09 13:48:47 -0800360 flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE);
361 pr_info("attempting bad execution at %p\n", func);
Kees Cook9ae113c2013-10-24 09:25:57 -0700362 func();
363}
364
Simon Kagstrom0347af42010-03-05 13:42:49 -0800365static void lkdtm_do_action(enum ctype which)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700366{
Simon Kagstrom0347af42010-03-05 13:42:49 -0800367 switch (which) {
Namhyung Kim93e2f582010-10-26 14:22:40 -0700368 case CT_PANIC:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800369 panic("dumptest");
370 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700371 case CT_BUG:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800372 BUG();
373 break;
Kees Cook65892722013-07-08 10:01:31 -0700374 case CT_WARNING:
375 WARN_ON(1);
376 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700377 case CT_EXCEPTION:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800378 *((int *) 0) = 0;
379 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700380 case CT_LOOP:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800381 for (;;)
382 ;
383 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700384 case CT_OVERFLOW:
Kees Cook7d196ac2013-10-24 09:25:39 -0700385 (void) recursive_loop(recur_count);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800386 break;
Kees Cook629c66a2013-10-24 18:05:42 -0700387 case CT_CORRUPT_STACK:
388 corrupt_stack();
Simon Kagstrom0347af42010-03-05 13:42:49 -0800389 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700390 case CT_UNALIGNED_LOAD_STORE_WRITE: {
Simon Kagstrom0347af42010-03-05 13:42:49 -0800391 static u8 data[5] __attribute__((aligned(4))) = {1, 2,
392 3, 4, 5};
393 u32 *p;
394 u32 val = 0x12345678;
395
396 p = (u32 *)(data + 1);
397 if (*p == 0)
398 val = 0x87654321;
399 *p = val;
400 break;
401 }
Namhyung Kim93e2f582010-10-26 14:22:40 -0700402 case CT_OVERWRITE_ALLOCATION: {
Simon Kagstrom0347af42010-03-05 13:42:49 -0800403 size_t len = 1020;
404 u32 *data = kmalloc(len, GFP_KERNEL);
405
406 data[1024 / sizeof(u32)] = 0x12345678;
407 kfree(data);
408 break;
409 }
Namhyung Kim93e2f582010-10-26 14:22:40 -0700410 case CT_WRITE_AFTER_FREE: {
Simon Kagstrom0347af42010-03-05 13:42:49 -0800411 size_t len = 1024;
412 u32 *data = kmalloc(len, GFP_KERNEL);
413
414 kfree(data);
415 schedule();
416 memset(data, 0x78, len);
417 break;
418 }
Namhyung Kim93e2f582010-10-26 14:22:40 -0700419 case CT_SOFTLOCKUP:
Frederic Weisbeckera48223f2010-05-26 14:44:29 -0700420 preempt_disable();
421 for (;;)
422 cpu_relax();
423 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700424 case CT_HARDLOCKUP:
Frederic Weisbeckera48223f2010-05-26 14:44:29 -0700425 local_irq_disable();
426 for (;;)
427 cpu_relax();
428 break;
Kees Cook274a5852013-07-08 10:01:32 -0700429 case CT_SPINLOCKUP:
430 /* Must be called twice to trigger. */
431 spin_lock(&lock_me_up);
Kees Cook51236622013-11-11 11:23:49 -0800432 /* Let sparse know we intended to exit holding the lock. */
433 __release(&lock_me_up);
Kees Cook274a5852013-07-08 10:01:32 -0700434 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700435 case CT_HUNG_TASK:
Frederic Weisbeckera48223f2010-05-26 14:44:29 -0700436 set_current_state(TASK_UNINTERRUPTIBLE);
437 schedule();
438 break;
Kees Cookcc33c5372013-07-08 10:01:33 -0700439 case CT_EXEC_DATA:
440 execute_location(data_area);
441 break;
442 case CT_EXEC_STACK: {
443 u8 stack_area[EXEC_SIZE];
444 execute_location(stack_area);
445 break;
446 }
447 case CT_EXEC_KMALLOC: {
448 u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
449 execute_location(kmalloc_area);
450 kfree(kmalloc_area);
451 break;
452 }
453 case CT_EXEC_VMALLOC: {
454 u32 *vmalloc_area = vmalloc(EXEC_SIZE);
455 execute_location(vmalloc_area);
456 vfree(vmalloc_area);
457 break;
458 }
Kees Cook9ae113c2013-10-24 09:25:57 -0700459 case CT_EXEC_USERSPACE: {
460 unsigned long user_addr;
461
462 user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
463 PROT_READ | PROT_WRITE | PROT_EXEC,
464 MAP_ANONYMOUS | MAP_PRIVATE, 0);
465 if (user_addr >= TASK_SIZE) {
466 pr_warn("Failed to allocate user memory\n");
467 return;
468 }
469 execute_user_location((void *)user_addr);
470 vm_munmap(user_addr, PAGE_SIZE);
471 break;
472 }
473 case CT_ACCESS_USERSPACE: {
474 unsigned long user_addr, tmp;
475 unsigned long *ptr;
476
477 user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
478 PROT_READ | PROT_WRITE | PROT_EXEC,
479 MAP_ANONYMOUS | MAP_PRIVATE, 0);
480 if (user_addr >= TASK_SIZE) {
481 pr_warn("Failed to allocate user memory\n");
482 return;
483 }
484
485 ptr = (unsigned long *)user_addr;
Kees Cookaac416f2014-02-09 13:48:47 -0800486
487 pr_info("attempting bad read at %p\n", ptr);
Kees Cook9ae113c2013-10-24 09:25:57 -0700488 tmp = *ptr;
489 tmp += 0xc0dec0de;
Kees Cookaac416f2014-02-09 13:48:47 -0800490
491 pr_info("attempting bad write at %p\n", ptr);
Kees Cook9ae113c2013-10-24 09:25:57 -0700492 *ptr = tmp;
493
494 vm_munmap(user_addr, PAGE_SIZE);
495
496 break;
497 }
498 case CT_WRITE_RO: {
499 unsigned long *ptr;
500
501 ptr = (unsigned long *)&rodata;
Kees Cookaac416f2014-02-09 13:48:47 -0800502
503 pr_info("attempting bad write at %p\n", ptr);
Kees Cook9ae113c2013-10-24 09:25:57 -0700504 *ptr ^= 0xabcd1234;
505
506 break;
507 }
Kees Cookdc2b9e92014-02-09 13:48:48 -0800508 case CT_WRITE_KERN: {
509 size_t size;
510 unsigned char *ptr;
511
512 size = (unsigned long)do_overwritten -
513 (unsigned long)do_nothing;
514 ptr = (unsigned char *)do_overwritten;
515
516 pr_info("attempting bad %zu byte write at %p\n", size, ptr);
517 memcpy(ptr, (unsigned char *)do_nothing, size);
518 flush_icache_range((unsigned long)ptr,
519 (unsigned long)(ptr + size));
520
521 do_overwritten();
522 break;
523 }
Namhyung Kim93e2f582010-10-26 14:22:40 -0700524 case CT_NONE:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800525 default:
526 break;
527 }
528
529}
530
531static void lkdtm_handler(void)
532{
Josh Huntaa2c96d2011-06-27 16:18:08 -0700533 unsigned long flags;
Cong Wang92618182012-02-03 15:37:15 -0800534 bool do_it = false;
Josh Huntaa2c96d2011-06-27 16:18:08 -0700535
536 spin_lock_irqsave(&count_lock, flags);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800537 count--;
Kees Cookfeac6e22014-02-09 13:48:46 -0800538 pr_info("Crash point %s of type %s hit, trigger in %d rounds\n",
539 cp_name_to_str(cpoint), cp_type_to_str(cptype), count);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700540
541 if (count == 0) {
Cong Wang92618182012-02-03 15:37:15 -0800542 do_it = true;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700543 count = cpoint_count;
544 }
Josh Huntaa2c96d2011-06-27 16:18:08 -0700545 spin_unlock_irqrestore(&count_lock, flags);
Cong Wang92618182012-02-03 15:37:15 -0800546
547 if (do_it)
548 lkdtm_do_action(cptype);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700549}
550
Simon Kagstrom0347af42010-03-05 13:42:49 -0800551static int lkdtm_register_cpoint(enum cname which)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700552{
553 int ret;
554
Namhyung Kim93e2f582010-10-26 14:22:40 -0700555 cpoint = CN_INVALID;
Simon Kagstrom0347af42010-03-05 13:42:49 -0800556 if (lkdtm.entry != NULL)
557 unregister_jprobe(&lkdtm);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700558
Simon Kagstrom0347af42010-03-05 13:42:49 -0800559 switch (which) {
Namhyung Kim93e2f582010-10-26 14:22:40 -0700560 case CN_DIRECT:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800561 lkdtm_do_action(cptype);
562 return 0;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700563 case CN_INT_HARDWARE_ENTRY:
M. Mohan Kumarf58f2fa2009-09-22 16:43:29 -0700564 lkdtm.kp.symbol_name = "do_IRQ";
Ankita Garg8bb31b92006-10-02 02:17:36 -0700565 lkdtm.entry = (kprobe_opcode_t*) jp_do_irq;
566 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700567 case CN_INT_HW_IRQ_EN:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700568 lkdtm.kp.symbol_name = "handle_IRQ_event";
569 lkdtm.entry = (kprobe_opcode_t*) jp_handle_irq_event;
570 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700571 case CN_INT_TASKLET_ENTRY:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700572 lkdtm.kp.symbol_name = "tasklet_action";
573 lkdtm.entry = (kprobe_opcode_t*) jp_tasklet_action;
574 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700575 case CN_FS_DEVRW:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700576 lkdtm.kp.symbol_name = "ll_rw_block";
577 lkdtm.entry = (kprobe_opcode_t*) jp_ll_rw_block;
578 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700579 case CN_MEM_SWAPOUT:
Ankita Garg18a61e42006-11-05 23:52:07 -0800580 lkdtm.kp.symbol_name = "shrink_inactive_list";
581 lkdtm.entry = (kprobe_opcode_t*) jp_shrink_inactive_list;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700582 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700583 case CN_TIMERADD:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700584 lkdtm.kp.symbol_name = "hrtimer_start";
585 lkdtm.entry = (kprobe_opcode_t*) jp_hrtimer_start;
586 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700587 case CN_SCSI_DISPATCH_CMD:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700588 lkdtm.kp.symbol_name = "scsi_dispatch_cmd";
589 lkdtm.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd;
590 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700591 case CN_IDE_CORE_CP:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700592#ifdef CONFIG_IDE
593 lkdtm.kp.symbol_name = "generic_ide_ioctl";
594 lkdtm.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl;
595#else
Kees Cookfeac6e22014-02-09 13:48:46 -0800596 pr_info("Crash point not available\n");
Simon Kagstrom0347af42010-03-05 13:42:49 -0800597 return -EINVAL;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700598#endif
599 break;
600 default:
Kees Cookfeac6e22014-02-09 13:48:46 -0800601 pr_info("Invalid Crash Point\n");
Simon Kagstrom0347af42010-03-05 13:42:49 -0800602 return -EINVAL;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700603 }
604
Simon Kagstrom0347af42010-03-05 13:42:49 -0800605 cpoint = which;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700606 if ((ret = register_jprobe(&lkdtm)) < 0) {
Kees Cookfeac6e22014-02-09 13:48:46 -0800607 pr_info("Couldn't register jprobe\n");
Namhyung Kim93e2f582010-10-26 14:22:40 -0700608 cpoint = CN_INVALID;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700609 }
610
Simon Kagstrom0347af42010-03-05 13:42:49 -0800611 return ret;
612}
613
614static ssize_t do_register_entry(enum cname which, struct file *f,
615 const char __user *user_buf, size_t count, loff_t *off)
616{
617 char *buf;
618 int err;
619
620 if (count >= PAGE_SIZE)
621 return -EINVAL;
622
623 buf = (char *)__get_free_page(GFP_KERNEL);
624 if (!buf)
625 return -ENOMEM;
626 if (copy_from_user(buf, user_buf, count)) {
627 free_page((unsigned long) buf);
628 return -EFAULT;
629 }
630 /* NULL-terminate and remove enter */
631 buf[count] = '\0';
632 strim(buf);
633
634 cptype = parse_cp_type(buf, count);
635 free_page((unsigned long) buf);
636
Namhyung Kim93e2f582010-10-26 14:22:40 -0700637 if (cptype == CT_NONE)
Simon Kagstrom0347af42010-03-05 13:42:49 -0800638 return -EINVAL;
639
640 err = lkdtm_register_cpoint(which);
641 if (err < 0)
642 return err;
643
644 *off += count;
645
646 return count;
647}
648
649/* Generic read callback that just prints out the available crash types */
650static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
651 size_t count, loff_t *off)
652{
653 char *buf;
654 int i, n, out;
655
656 buf = (char *)__get_free_page(GFP_KERNEL);
Alan Cox086ff4b2012-07-30 14:43:24 -0700657 if (buf == NULL)
658 return -ENOMEM;
Simon Kagstrom0347af42010-03-05 13:42:49 -0800659
660 n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
661 for (i = 0; i < ARRAY_SIZE(cp_type); i++)
662 n += snprintf(buf + n, PAGE_SIZE - n, "%s\n", cp_type[i]);
663 buf[n] = '\0';
664
665 out = simple_read_from_buffer(user_buf, count, off,
666 buf, n);
667 free_page((unsigned long) buf);
668
669 return out;
670}
671
672static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
673{
Ankita Garg8bb31b92006-10-02 02:17:36 -0700674 return 0;
675}
676
Simon Kagstrom0347af42010-03-05 13:42:49 -0800677
678static ssize_t int_hardware_entry(struct file *f, const char __user *buf,
679 size_t count, loff_t *off)
680{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700681 return do_register_entry(CN_INT_HARDWARE_ENTRY, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800682}
683
684static ssize_t int_hw_irq_en(struct file *f, const char __user *buf,
685 size_t count, loff_t *off)
686{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700687 return do_register_entry(CN_INT_HW_IRQ_EN, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800688}
689
690static ssize_t int_tasklet_entry(struct file *f, const char __user *buf,
691 size_t count, loff_t *off)
692{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700693 return do_register_entry(CN_INT_TASKLET_ENTRY, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800694}
695
696static ssize_t fs_devrw_entry(struct file *f, const char __user *buf,
697 size_t count, loff_t *off)
698{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700699 return do_register_entry(CN_FS_DEVRW, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800700}
701
702static ssize_t mem_swapout_entry(struct file *f, const char __user *buf,
703 size_t count, loff_t *off)
704{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700705 return do_register_entry(CN_MEM_SWAPOUT, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800706}
707
708static ssize_t timeradd_entry(struct file *f, const char __user *buf,
709 size_t count, loff_t *off)
710{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700711 return do_register_entry(CN_TIMERADD, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800712}
713
714static ssize_t scsi_dispatch_cmd_entry(struct file *f,
715 const char __user *buf, size_t count, loff_t *off)
716{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700717 return do_register_entry(CN_SCSI_DISPATCH_CMD, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800718}
719
720static ssize_t ide_core_cp_entry(struct file *f, const char __user *buf,
721 size_t count, loff_t *off)
722{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700723 return do_register_entry(CN_IDE_CORE_CP, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800724}
725
726/* Special entry to just crash directly. Available without KPROBEs */
727static ssize_t direct_entry(struct file *f, const char __user *user_buf,
728 size_t count, loff_t *off)
729{
730 enum ctype type;
731 char *buf;
732
733 if (count >= PAGE_SIZE)
734 return -EINVAL;
735 if (count < 1)
736 return -EINVAL;
737
738 buf = (char *)__get_free_page(GFP_KERNEL);
739 if (!buf)
740 return -ENOMEM;
741 if (copy_from_user(buf, user_buf, count)) {
742 free_page((unsigned long) buf);
743 return -EFAULT;
744 }
745 /* NULL-terminate and remove enter */
746 buf[count] = '\0';
747 strim(buf);
748
749 type = parse_cp_type(buf, count);
750 free_page((unsigned long) buf);
Namhyung Kim93e2f582010-10-26 14:22:40 -0700751 if (type == CT_NONE)
Simon Kagstrom0347af42010-03-05 13:42:49 -0800752 return -EINVAL;
753
Kees Cookfeac6e22014-02-09 13:48:46 -0800754 pr_info("Performing direct entry %s\n", cp_type_to_str(type));
Simon Kagstrom0347af42010-03-05 13:42:49 -0800755 lkdtm_do_action(type);
756 *off += count;
757
758 return count;
759}
760
761struct crash_entry {
762 const char *name;
763 const struct file_operations fops;
764};
765
766static const struct crash_entry crash_entries[] = {
767 {"DIRECT", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200768 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800769 .open = lkdtm_debugfs_open,
770 .write = direct_entry} },
771 {"INT_HARDWARE_ENTRY", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200772 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800773 .open = lkdtm_debugfs_open,
774 .write = int_hardware_entry} },
775 {"INT_HW_IRQ_EN", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200776 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800777 .open = lkdtm_debugfs_open,
778 .write = int_hw_irq_en} },
779 {"INT_TASKLET_ENTRY", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200780 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800781 .open = lkdtm_debugfs_open,
782 .write = int_tasklet_entry} },
783 {"FS_DEVRW", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200784 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800785 .open = lkdtm_debugfs_open,
786 .write = fs_devrw_entry} },
787 {"MEM_SWAPOUT", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200788 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800789 .open = lkdtm_debugfs_open,
790 .write = mem_swapout_entry} },
791 {"TIMERADD", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200792 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800793 .open = lkdtm_debugfs_open,
794 .write = timeradd_entry} },
795 {"SCSI_DISPATCH_CMD", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200796 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800797 .open = lkdtm_debugfs_open,
798 .write = scsi_dispatch_cmd_entry} },
799 {"IDE_CORE_CP", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200800 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800801 .open = lkdtm_debugfs_open,
802 .write = ide_core_cp_entry} },
803};
804
805static struct dentry *lkdtm_debugfs_root;
806
807static int __init lkdtm_module_init(void)
808{
809 int ret = -EINVAL;
810 int n_debugfs_entries = 1; /* Assume only the direct entry */
811 int i;
812
813 /* Register debugfs interface */
814 lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
815 if (!lkdtm_debugfs_root) {
Kees Cookfeac6e22014-02-09 13:48:46 -0800816 pr_err("creating root dir failed\n");
Simon Kagstrom0347af42010-03-05 13:42:49 -0800817 return -ENODEV;
818 }
819
820#ifdef CONFIG_KPROBES
821 n_debugfs_entries = ARRAY_SIZE(crash_entries);
822#endif
823
824 for (i = 0; i < n_debugfs_entries; i++) {
825 const struct crash_entry *cur = &crash_entries[i];
826 struct dentry *de;
827
828 de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
829 NULL, &cur->fops);
830 if (de == NULL) {
Kees Cookfeac6e22014-02-09 13:48:46 -0800831 pr_err("could not create %s\n", cur->name);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800832 goto out_err;
833 }
834 }
835
836 if (lkdtm_parse_commandline() == -EINVAL) {
Kees Cookfeac6e22014-02-09 13:48:46 -0800837 pr_info("Invalid command\n");
Simon Kagstrom0347af42010-03-05 13:42:49 -0800838 goto out_err;
839 }
840
Namhyung Kim93e2f582010-10-26 14:22:40 -0700841 if (cpoint != CN_INVALID && cptype != CT_NONE) {
Simon Kagstrom0347af42010-03-05 13:42:49 -0800842 ret = lkdtm_register_cpoint(cpoint);
843 if (ret < 0) {
Kees Cookfeac6e22014-02-09 13:48:46 -0800844 pr_info("Invalid crash point %d\n", cpoint);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800845 goto out_err;
846 }
Kees Cookfeac6e22014-02-09 13:48:46 -0800847 pr_info("Crash point %s of type %s registered\n",
848 cpoint_name, cpoint_type);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800849 } else {
Kees Cookfeac6e22014-02-09 13:48:46 -0800850 pr_info("No crash points registered, enable through debugfs\n");
Simon Kagstrom0347af42010-03-05 13:42:49 -0800851 }
852
853 return 0;
854
855out_err:
856 debugfs_remove_recursive(lkdtm_debugfs_root);
857 return ret;
858}
859
Adrian Bunk21181162008-02-06 01:36:50 -0800860static void __exit lkdtm_module_exit(void)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700861{
Simon Kagstrom0347af42010-03-05 13:42:49 -0800862 debugfs_remove_recursive(lkdtm_debugfs_root);
863
864 unregister_jprobe(&lkdtm);
Kees Cookfeac6e22014-02-09 13:48:46 -0800865 pr_info("Crash point unregistered\n");
Ankita Garg8bb31b92006-10-02 02:17:36 -0700866}
867
868module_init(lkdtm_module_init);
869module_exit(lkdtm_module_exit);
870
871MODULE_LICENSE("GPL");