blob: 187cd9b63e9ad64370a487954fc6e4d0822dd9c9 [file] [log] [blame]
Ankita Garg8bb31b92006-10-02 02:17:36 -07001/*
Kees Cook426f3a52016-06-03 11:16:32 -07002 * Linux Kernel Dump Test Module for testing kernel crashes conditions:
3 * induces system failures at predefined crashpoints and under predefined
4 * operational conditions in order to evaluate the reliability of kernel
5 * sanity checking and crash dumps obtained using different dumping
6 * solutions.
Ankita Garg8bb31b92006-10-02 02:17:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 * Copyright (C) IBM Corporation, 2006
23 *
24 * Author: Ankita Garg <[email protected]>
25 *
Ankita Garg8bb31b92006-10-02 02:17:36 -070026 * 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 <[email protected]>
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 Cook426f3a52016-06-03 11:16:32 -070033#define pr_fmt(fmt) "lkdtm: " 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>
Kees Cook1bc9fac2014-02-14 15:58:50 -080049#include <asm/cacheflush.h>
Ankita Garg8bb31b92006-10-02 02:17:36 -070050
51#ifdef CONFIG_IDE
52#include <linux/ide.h>
53#endif
54
Kees Cook9a49a522016-02-22 14:09:29 -080055#include "lkdtm.h"
56
Kees Cook7d196ac2013-10-24 09:25:39 -070057/*
58 * Make sure our attempts to over run the kernel stack doesn't trigger
59 * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
60 * recurse past the end of THREAD_SIZE by default.
61 */
62#if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
63#define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
64#else
65#define REC_STACK_SIZE (THREAD_SIZE / 8)
66#endif
67#define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
68
Ankita Garg8bb31b92006-10-02 02:17:36 -070069#define DEFAULT_COUNT 10
Kees Cookcc33c5372013-07-08 10:01:33 -070070#define EXEC_SIZE 64
Ankita Garg8bb31b92006-10-02 02:17:36 -070071
72enum cname {
Namhyung Kim93e2f582010-10-26 14:22:40 -070073 CN_INVALID,
74 CN_INT_HARDWARE_ENTRY,
75 CN_INT_HW_IRQ_EN,
76 CN_INT_TASKLET_ENTRY,
77 CN_FS_DEVRW,
78 CN_MEM_SWAPOUT,
79 CN_TIMERADD,
80 CN_SCSI_DISPATCH_CMD,
81 CN_IDE_CORE_CP,
82 CN_DIRECT,
Ankita Garg8bb31b92006-10-02 02:17:36 -070083};
84
85enum ctype {
Namhyung Kim93e2f582010-10-26 14:22:40 -070086 CT_NONE,
87 CT_PANIC,
88 CT_BUG,
Kees Cook65892722013-07-08 10:01:31 -070089 CT_WARNING,
Namhyung Kim93e2f582010-10-26 14:22:40 -070090 CT_EXCEPTION,
91 CT_LOOP,
92 CT_OVERFLOW,
93 CT_CORRUPT_STACK,
94 CT_UNALIGNED_LOAD_STORE_WRITE,
95 CT_OVERWRITE_ALLOCATION,
96 CT_WRITE_AFTER_FREE,
Laura Abbottbc0b8cc2016-02-25 16:36:42 -080097 CT_READ_AFTER_FREE,
Laura Abbott920d4512016-02-25 16:36:44 -080098 CT_WRITE_BUDDY_AFTER_FREE,
99 CT_READ_BUDDY_AFTER_FREE,
Namhyung Kim93e2f582010-10-26 14:22:40 -0700100 CT_SOFTLOCKUP,
101 CT_HARDLOCKUP,
Kees Cook274a5852013-07-08 10:01:32 -0700102 CT_SPINLOCKUP,
Namhyung Kim93e2f582010-10-26 14:22:40 -0700103 CT_HUNG_TASK,
Kees Cookcc33c5372013-07-08 10:01:33 -0700104 CT_EXEC_DATA,
105 CT_EXEC_STACK,
106 CT_EXEC_KMALLOC,
107 CT_EXEC_VMALLOC,
Kees Cook9a49a522016-02-22 14:09:29 -0800108 CT_EXEC_RODATA,
Kees Cook9ae113c2013-10-24 09:25:57 -0700109 CT_EXEC_USERSPACE,
110 CT_ACCESS_USERSPACE,
111 CT_WRITE_RO,
Kees Cook7cca0712016-02-17 14:41:16 -0800112 CT_WRITE_RO_AFTER_INIT,
Kees Cookdc2b9e92014-02-09 13:48:48 -0800113 CT_WRITE_KERN,
David Windsor5fd9e482015-12-17 00:56:36 -0500114 CT_WRAP_ATOMIC
Ankita Garg8bb31b92006-10-02 02:17:36 -0700115};
116
117static char* cp_name[] = {
118 "INT_HARDWARE_ENTRY",
119 "INT_HW_IRQ_EN",
120 "INT_TASKLET_ENTRY",
121 "FS_DEVRW",
122 "MEM_SWAPOUT",
123 "TIMERADD",
124 "SCSI_DISPATCH_CMD",
Simon Kagstrom0347af42010-03-05 13:42:49 -0800125 "IDE_CORE_CP",
126 "DIRECT",
Ankita Garg8bb31b92006-10-02 02:17:36 -0700127};
128
129static char* cp_type[] = {
130 "PANIC",
131 "BUG",
Kees Cook65892722013-07-08 10:01:31 -0700132 "WARNING",
Ankita Garg8bb31b92006-10-02 02:17:36 -0700133 "EXCEPTION",
134 "LOOP",
Simon Kagstrom0347af42010-03-05 13:42:49 -0800135 "OVERFLOW",
136 "CORRUPT_STACK",
137 "UNALIGNED_LOAD_STORE_WRITE",
138 "OVERWRITE_ALLOCATION",
139 "WRITE_AFTER_FREE",
Laura Abbottbc0b8cc2016-02-25 16:36:42 -0800140 "READ_AFTER_FREE",
Laura Abbott920d4512016-02-25 16:36:44 -0800141 "WRITE_BUDDY_AFTER_FREE",
142 "READ_BUDDY_AFTER_FREE",
Frederic Weisbeckera48223f2010-05-26 14:44:29 -0700143 "SOFTLOCKUP",
144 "HARDLOCKUP",
Kees Cook274a5852013-07-08 10:01:32 -0700145 "SPINLOCKUP",
Frederic Weisbeckera48223f2010-05-26 14:44:29 -0700146 "HUNG_TASK",
Kees Cookcc33c5372013-07-08 10:01:33 -0700147 "EXEC_DATA",
148 "EXEC_STACK",
149 "EXEC_KMALLOC",
150 "EXEC_VMALLOC",
Kees Cook9a49a522016-02-22 14:09:29 -0800151 "EXEC_RODATA",
Kees Cook9ae113c2013-10-24 09:25:57 -0700152 "EXEC_USERSPACE",
153 "ACCESS_USERSPACE",
154 "WRITE_RO",
Kees Cook7cca0712016-02-17 14:41:16 -0800155 "WRITE_RO_AFTER_INIT",
Kees Cookdc2b9e92014-02-09 13:48:48 -0800156 "WRITE_KERN",
David Windsor5fd9e482015-12-17 00:56:36 -0500157 "WRAP_ATOMIC"
Ankita Garg8bb31b92006-10-02 02:17:36 -0700158};
159
160static struct jprobe lkdtm;
161
162static int lkdtm_parse_commandline(void);
163static void lkdtm_handler(void);
164
Al Viroec1c6202007-02-09 16:05:17 +0000165static char* cpoint_name;
166static char* cpoint_type;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700167static int cpoint_count = DEFAULT_COUNT;
168static int recur_count = REC_NUM_DEFAULT;
169
Namhyung Kim93e2f582010-10-26 14:22:40 -0700170static enum cname cpoint = CN_INVALID;
171static enum ctype cptype = CT_NONE;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700172static int count = DEFAULT_COUNT;
Josh Huntaa2c96d2011-06-27 16:18:08 -0700173static DEFINE_SPINLOCK(count_lock);
Kees Cook274a5852013-07-08 10:01:32 -0700174static DEFINE_SPINLOCK(lock_me_up);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700175
Kees Cookcc33c5372013-07-08 10:01:33 -0700176static u8 data_area[EXEC_SIZE];
177
Kees Cook9ae113c2013-10-24 09:25:57 -0700178static const unsigned long rodata = 0xAA55AA55;
Kees Cook7cca0712016-02-17 14:41:16 -0800179static unsigned long ro_after_init __ro_after_init = 0x55AA5500;
Kees Cook9ae113c2013-10-24 09:25:57 -0700180
Ankita Garg8bb31b92006-10-02 02:17:36 -0700181module_param(recur_count, int, 0644);
Kees Cook7d196ac2013-10-24 09:25:39 -0700182MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
Rusty Russelldca41302010-08-11 23:04:21 -0600183module_param(cpoint_name, charp, 0444);
Randy Dunlap5d861d92006-11-02 22:07:06 -0800184MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
Rusty Russelldca41302010-08-11 23:04:21 -0600185module_param(cpoint_type, charp, 0444);
Randy Dunlap5d861d92006-11-02 22:07:06 -0800186MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
187 "hitting the crash point");
188module_param(cpoint_count, int, 0644);
189MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
190 "crash point is to be hit to trigger action");
Ankita Garg8bb31b92006-10-02 02:17:36 -0700191
Adrian Bunk21181162008-02-06 01:36:50 -0800192static unsigned int jp_do_irq(unsigned int irq)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700193{
194 lkdtm_handler();
195 jprobe_return();
196 return 0;
197}
198
Adrian Bunk21181162008-02-06 01:36:50 -0800199static irqreturn_t jp_handle_irq_event(unsigned int irq,
200 struct irqaction *action)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700201{
202 lkdtm_handler();
203 jprobe_return();
204 return 0;
205}
206
Adrian Bunk21181162008-02-06 01:36:50 -0800207static void jp_tasklet_action(struct softirq_action *a)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700208{
209 lkdtm_handler();
210 jprobe_return();
211}
212
Adrian Bunk21181162008-02-06 01:36:50 -0800213static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
Ankita Garg8bb31b92006-10-02 02:17:36 -0700214{
215 lkdtm_handler();
216 jprobe_return();
217}
218
219struct scan_control;
220
Adrian Bunk21181162008-02-06 01:36:50 -0800221static unsigned long jp_shrink_inactive_list(unsigned long max_scan,
222 struct zone *zone,
223 struct scan_control *sc)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700224{
225 lkdtm_handler();
226 jprobe_return();
227 return 0;
228}
229
Adrian Bunk21181162008-02-06 01:36:50 -0800230static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim,
231 const enum hrtimer_mode mode)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700232{
233 lkdtm_handler();
234 jprobe_return();
235 return 0;
236}
237
Adrian Bunk21181162008-02-06 01:36:50 -0800238static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700239{
240 lkdtm_handler();
241 jprobe_return();
242 return 0;
243}
244
245#ifdef CONFIG_IDE
Rashika Kheria44629432013-12-13 12:29:42 +0530246static int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file,
Ankita Garg8bb31b92006-10-02 02:17:36 -0700247 struct block_device *bdev, unsigned int cmd,
248 unsigned long arg)
249{
250 lkdtm_handler();
251 jprobe_return();
252 return 0;
253}
254#endif
255
Simon Kagstrom0347af42010-03-05 13:42:49 -0800256/* Return the crashpoint number or NONE if the name is invalid */
257static enum ctype parse_cp_type(const char *what, size_t count)
258{
259 int i;
260
261 for (i = 0; i < ARRAY_SIZE(cp_type); i++) {
262 if (!strcmp(what, cp_type[i]))
263 return i + 1;
264 }
265
Namhyung Kim93e2f582010-10-26 14:22:40 -0700266 return CT_NONE;
Simon Kagstrom0347af42010-03-05 13:42:49 -0800267}
268
269static const char *cp_type_to_str(enum ctype type)
270{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700271 if (type == CT_NONE || type < 0 || type > ARRAY_SIZE(cp_type))
Simon Kagstrom0347af42010-03-05 13:42:49 -0800272 return "None";
273
274 return cp_type[type - 1];
275}
276
277static const char *cp_name_to_str(enum cname name)
278{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700279 if (name == CN_INVALID || name < 0 || name > ARRAY_SIZE(cp_name))
Simon Kagstrom0347af42010-03-05 13:42:49 -0800280 return "INVALID";
281
282 return cp_name[name - 1];
283}
284
285
Ankita Garg8bb31b92006-10-02 02:17:36 -0700286static int lkdtm_parse_commandline(void)
287{
288 int i;
Josh Huntaa2c96d2011-06-27 16:18:08 -0700289 unsigned long flags;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700290
Simon Kagstrom0347af42010-03-05 13:42:49 -0800291 if (cpoint_count < 1 || recur_count < 1)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700292 return -EINVAL;
293
Josh Huntaa2c96d2011-06-27 16:18:08 -0700294 spin_lock_irqsave(&count_lock, flags);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700295 count = cpoint_count;
Josh Huntaa2c96d2011-06-27 16:18:08 -0700296 spin_unlock_irqrestore(&count_lock, flags);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700297
Simon Kagstrom0347af42010-03-05 13:42:49 -0800298 /* No special parameters */
299 if (!cpoint_type && !cpoint_name)
300 return 0;
301
302 /* Neither or both of these need to be set */
303 if (!cpoint_type || !cpoint_name)
304 return -EINVAL;
305
306 cptype = parse_cp_type(cpoint_type, strlen(cpoint_type));
Namhyung Kim93e2f582010-10-26 14:22:40 -0700307 if (cptype == CT_NONE)
Simon Kagstrom0347af42010-03-05 13:42:49 -0800308 return -EINVAL;
309
310 for (i = 0; i < ARRAY_SIZE(cp_name); i++) {
311 if (!strcmp(cpoint_name, cp_name[i])) {
312 cpoint = i + 1;
313 return 0;
314 }
315 }
316
317 /* Could not find a valid crash point */
318 return -EINVAL;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700319}
320
Kees Cook7d196ac2013-10-24 09:25:39 -0700321static int recursive_loop(int remaining)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700322{
Kees Cook7d196ac2013-10-24 09:25:39 -0700323 char buf[REC_STACK_SIZE];
Ankita Garg8bb31b92006-10-02 02:17:36 -0700324
Kees Cook7d196ac2013-10-24 09:25:39 -0700325 /* Make sure compiler does not optimize this away. */
326 memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE);
327 if (!remaining)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700328 return 0;
329 else
Kees Cook7d196ac2013-10-24 09:25:39 -0700330 return recursive_loop(remaining - 1);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700331}
332
Kees Cookcc33c5372013-07-08 10:01:33 -0700333static void do_nothing(void)
334{
335 return;
336}
337
Kees Cookdc2b9e92014-02-09 13:48:48 -0800338/* Must immediately follow do_nothing for size calculuations to work out. */
339static void do_overwritten(void)
340{
341 pr_info("do_overwritten wasn't overwritten!\n");
342 return;
343}
344
Kees Cook629c66a2013-10-24 18:05:42 -0700345static noinline void corrupt_stack(void)
346{
347 /* Use default char array length that triggers stack protection. */
348 char data[8];
349
350 memset((void *)data, 0, 64);
351}
352
Kees Cook9a49a522016-02-22 14:09:29 -0800353static noinline void execute_location(void *dst, bool write)
Kees Cookcc33c5372013-07-08 10:01:33 -0700354{
355 void (*func)(void) = dst;
356
Kees Cookaac416f2014-02-09 13:48:47 -0800357 pr_info("attempting ok execution at %p\n", do_nothing);
358 do_nothing();
359
Kees Cook9a49a522016-02-22 14:09:29 -0800360 if (write) {
361 memcpy(dst, do_nothing, EXEC_SIZE);
362 flush_icache_range((unsigned long)dst,
363 (unsigned long)dst + EXEC_SIZE);
364 }
Kees Cookaac416f2014-02-09 13:48:47 -0800365 pr_info("attempting bad execution at %p\n", func);
Kees Cookcc33c5372013-07-08 10:01:33 -0700366 func();
367}
368
Kees Cook9ae113c2013-10-24 09:25:57 -0700369static void execute_user_location(void *dst)
370{
Kees Cook51236622013-11-11 11:23:49 -0800371 /* Intentionally crossing kernel/user memory boundary. */
Kees Cook9ae113c2013-10-24 09:25:57 -0700372 void (*func)(void) = dst;
373
Kees Cookaac416f2014-02-09 13:48:47 -0800374 pr_info("attempting ok execution at %p\n", do_nothing);
375 do_nothing();
376
Kees Cook51236622013-11-11 11:23:49 -0800377 if (copy_to_user((void __user *)dst, do_nothing, EXEC_SIZE))
Kees Cook9ae113c2013-10-24 09:25:57 -0700378 return;
Kees Cookaac416f2014-02-09 13:48:47 -0800379 flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE);
380 pr_info("attempting bad execution at %p\n", func);
Kees Cook9ae113c2013-10-24 09:25:57 -0700381 func();
382}
383
Simon Kagstrom0347af42010-03-05 13:42:49 -0800384static void lkdtm_do_action(enum ctype which)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700385{
Simon Kagstrom0347af42010-03-05 13:42:49 -0800386 switch (which) {
Namhyung Kim93e2f582010-10-26 14:22:40 -0700387 case CT_PANIC:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800388 panic("dumptest");
389 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700390 case CT_BUG:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800391 BUG();
392 break;
Kees Cook65892722013-07-08 10:01:31 -0700393 case CT_WARNING:
394 WARN_ON(1);
395 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700396 case CT_EXCEPTION:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800397 *((int *) 0) = 0;
398 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700399 case CT_LOOP:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800400 for (;;)
401 ;
402 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700403 case CT_OVERFLOW:
Kees Cook7d196ac2013-10-24 09:25:39 -0700404 (void) recursive_loop(recur_count);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800405 break;
Kees Cook629c66a2013-10-24 18:05:42 -0700406 case CT_CORRUPT_STACK:
407 corrupt_stack();
Simon Kagstrom0347af42010-03-05 13:42:49 -0800408 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700409 case CT_UNALIGNED_LOAD_STORE_WRITE: {
Simon Kagstrom0347af42010-03-05 13:42:49 -0800410 static u8 data[5] __attribute__((aligned(4))) = {1, 2,
411 3, 4, 5};
412 u32 *p;
413 u32 val = 0x12345678;
414
415 p = (u32 *)(data + 1);
416 if (*p == 0)
417 val = 0x87654321;
418 *p = val;
419 break;
420 }
Namhyung Kim93e2f582010-10-26 14:22:40 -0700421 case CT_OVERWRITE_ALLOCATION: {
Simon Kagstrom0347af42010-03-05 13:42:49 -0800422 size_t len = 1020;
423 u32 *data = kmalloc(len, GFP_KERNEL);
424
425 data[1024 / sizeof(u32)] = 0x12345678;
426 kfree(data);
427 break;
428 }
Namhyung Kim93e2f582010-10-26 14:22:40 -0700429 case CT_WRITE_AFTER_FREE: {
Kees Cook7c0ae5b2016-02-26 15:27:35 -0800430 int *base, *again;
Simon Kagstrom0347af42010-03-05 13:42:49 -0800431 size_t len = 1024;
Laura Abbott250a8982016-02-25 16:36:43 -0800432 /*
433 * The slub allocator uses the first word to store the free
434 * pointer in some configurations. Use the middle of the
435 * allocation to avoid running into the freelist
436 */
437 size_t offset = (len / sizeof(*base)) / 2;
Simon Kagstrom0347af42010-03-05 13:42:49 -0800438
Laura Abbott250a8982016-02-25 16:36:43 -0800439 base = kmalloc(len, GFP_KERNEL);
440 pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
Laura Abbott250a8982016-02-25 16:36:43 -0800441 pr_info("Attempting bad write to freed memory at %p\n",
442 &base[offset]);
Kees Cook7c0ae5b2016-02-26 15:27:35 -0800443 kfree(base);
Laura Abbott250a8982016-02-25 16:36:43 -0800444 base[offset] = 0x0abcdef0;
Kees Cook7c0ae5b2016-02-26 15:27:35 -0800445 /* Attempt to notice the overwrite. */
446 again = kmalloc(len, GFP_KERNEL);
447 kfree(again);
448 if (again != base)
449 pr_info("Hmm, didn't get the same memory range.\n");
450
Simon Kagstrom0347af42010-03-05 13:42:49 -0800451 break;
452 }
Laura Abbottbc0b8cc2016-02-25 16:36:42 -0800453 case CT_READ_AFTER_FREE: {
454 int *base, *val, saw;
455 size_t len = 1024;
456 /*
457 * The slub allocator uses the first word to store the free
458 * pointer in some configurations. Use the middle of the
459 * allocation to avoid running into the freelist
460 */
461 size_t offset = (len / sizeof(*base)) / 2;
462
463 base = kmalloc(len, GFP_KERNEL);
464 if (!base)
465 break;
466
467 val = kmalloc(len, GFP_KERNEL);
Sudip Mukherjeed2e10082016-04-05 22:41:06 +0530468 if (!val) {
469 kfree(base);
Laura Abbottbc0b8cc2016-02-25 16:36:42 -0800470 break;
Sudip Mukherjeed2e10082016-04-05 22:41:06 +0530471 }
Laura Abbottbc0b8cc2016-02-25 16:36:42 -0800472
473 *val = 0x12345678;
474 base[offset] = *val;
475 pr_info("Value in memory before free: %x\n", base[offset]);
476
477 kfree(base);
478
479 pr_info("Attempting bad read from freed memory\n");
480 saw = base[offset];
481 if (saw != *val) {
482 /* Good! Poisoning happened, so declare a win. */
Kees Cook7c0ae5b2016-02-26 15:27:35 -0800483 pr_info("Memory correctly poisoned (%x)\n", saw);
Laura Abbottbc0b8cc2016-02-25 16:36:42 -0800484 BUG();
485 }
486 pr_info("Memory was not poisoned\n");
487
488 kfree(val);
489 break;
490 }
Laura Abbott920d4512016-02-25 16:36:44 -0800491 case CT_WRITE_BUDDY_AFTER_FREE: {
492 unsigned long p = __get_free_page(GFP_KERNEL);
493 if (!p)
494 break;
495 pr_info("Writing to the buddy page before free\n");
496 memset((void *)p, 0x3, PAGE_SIZE);
497 free_page(p);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800498 schedule();
Laura Abbott920d4512016-02-25 16:36:44 -0800499 pr_info("Attempting bad write to the buddy page after free\n");
500 memset((void *)p, 0x78, PAGE_SIZE);
Kees Cook7c0ae5b2016-02-26 15:27:35 -0800501 /* Attempt to notice the overwrite. */
502 p = __get_free_page(GFP_KERNEL);
503 free_page(p);
504 schedule();
505
Laura Abbott920d4512016-02-25 16:36:44 -0800506 break;
507 }
508 case CT_READ_BUDDY_AFTER_FREE: {
509 unsigned long p = __get_free_page(GFP_KERNEL);
Sudip Mukherjee50fbd972016-04-05 22:41:05 +0530510 int saw, *val;
Laura Abbott920d4512016-02-25 16:36:44 -0800511 int *base;
512
513 if (!p)
514 break;
515
Sudip Mukherjee50fbd972016-04-05 22:41:05 +0530516 val = kmalloc(1024, GFP_KERNEL);
Kees Cook3d085c72016-04-06 15:53:27 -0700517 if (!val) {
518 free_page(p);
Laura Abbott920d4512016-02-25 16:36:44 -0800519 break;
Kees Cook3d085c72016-04-06 15:53:27 -0700520 }
Laura Abbott920d4512016-02-25 16:36:44 -0800521
522 base = (int *)p;
523
524 *val = 0x12345678;
525 base[0] = *val;
526 pr_info("Value in memory before free: %x\n", base[0]);
527 free_page(p);
528 pr_info("Attempting to read from freed memory\n");
529 saw = base[0];
530 if (saw != *val) {
531 /* Good! Poisoning happened, so declare a win. */
Kees Cook7c0ae5b2016-02-26 15:27:35 -0800532 pr_info("Memory correctly poisoned (%x)\n", saw);
Laura Abbott920d4512016-02-25 16:36:44 -0800533 BUG();
534 }
535 pr_info("Buddy page was not poisoned\n");
536
537 kfree(val);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800538 break;
539 }
Namhyung Kim93e2f582010-10-26 14:22:40 -0700540 case CT_SOFTLOCKUP:
Frederic Weisbeckera48223f2010-05-26 14:44:29 -0700541 preempt_disable();
542 for (;;)
543 cpu_relax();
544 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700545 case CT_HARDLOCKUP:
Frederic Weisbeckera48223f2010-05-26 14:44:29 -0700546 local_irq_disable();
547 for (;;)
548 cpu_relax();
549 break;
Kees Cook274a5852013-07-08 10:01:32 -0700550 case CT_SPINLOCKUP:
551 /* Must be called twice to trigger. */
552 spin_lock(&lock_me_up);
Kees Cook51236622013-11-11 11:23:49 -0800553 /* Let sparse know we intended to exit holding the lock. */
554 __release(&lock_me_up);
Kees Cook274a5852013-07-08 10:01:32 -0700555 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700556 case CT_HUNG_TASK:
Frederic Weisbeckera48223f2010-05-26 14:44:29 -0700557 set_current_state(TASK_UNINTERRUPTIBLE);
558 schedule();
559 break;
Kees Cookcc33c5372013-07-08 10:01:33 -0700560 case CT_EXEC_DATA:
Kees Cook9a49a522016-02-22 14:09:29 -0800561 execute_location(data_area, true);
Kees Cookcc33c5372013-07-08 10:01:33 -0700562 break;
563 case CT_EXEC_STACK: {
564 u8 stack_area[EXEC_SIZE];
Kees Cook9a49a522016-02-22 14:09:29 -0800565 execute_location(stack_area, true);
Kees Cookcc33c5372013-07-08 10:01:33 -0700566 break;
567 }
568 case CT_EXEC_KMALLOC: {
569 u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
Kees Cook9a49a522016-02-22 14:09:29 -0800570 execute_location(kmalloc_area, true);
Kees Cookcc33c5372013-07-08 10:01:33 -0700571 kfree(kmalloc_area);
572 break;
573 }
574 case CT_EXEC_VMALLOC: {
575 u32 *vmalloc_area = vmalloc(EXEC_SIZE);
Kees Cook9a49a522016-02-22 14:09:29 -0800576 execute_location(vmalloc_area, true);
Kees Cookcc33c5372013-07-08 10:01:33 -0700577 vfree(vmalloc_area);
578 break;
579 }
Kees Cook9a49a522016-02-22 14:09:29 -0800580 case CT_EXEC_RODATA:
581 execute_location(lkdtm_rodata_do_nothing, false);
582 break;
Kees Cook9ae113c2013-10-24 09:25:57 -0700583 case CT_EXEC_USERSPACE: {
584 unsigned long user_addr;
585
586 user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
587 PROT_READ | PROT_WRITE | PROT_EXEC,
588 MAP_ANONYMOUS | MAP_PRIVATE, 0);
589 if (user_addr >= TASK_SIZE) {
590 pr_warn("Failed to allocate user memory\n");
591 return;
592 }
593 execute_user_location((void *)user_addr);
594 vm_munmap(user_addr, PAGE_SIZE);
595 break;
596 }
597 case CT_ACCESS_USERSPACE: {
Stephen Smalley2cb202c2015-10-27 16:47:53 -0400598 unsigned long user_addr, tmp = 0;
Kees Cook9ae113c2013-10-24 09:25:57 -0700599 unsigned long *ptr;
600
601 user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
602 PROT_READ | PROT_WRITE | PROT_EXEC,
603 MAP_ANONYMOUS | MAP_PRIVATE, 0);
604 if (user_addr >= TASK_SIZE) {
605 pr_warn("Failed to allocate user memory\n");
606 return;
607 }
608
Stephen Smalley2cb202c2015-10-27 16:47:53 -0400609 if (copy_to_user((void __user *)user_addr, &tmp, sizeof(tmp))) {
610 pr_warn("copy_to_user failed\n");
611 vm_munmap(user_addr, PAGE_SIZE);
612 return;
613 }
614
Kees Cook9ae113c2013-10-24 09:25:57 -0700615 ptr = (unsigned long *)user_addr;
Kees Cookaac416f2014-02-09 13:48:47 -0800616
617 pr_info("attempting bad read at %p\n", ptr);
Kees Cook9ae113c2013-10-24 09:25:57 -0700618 tmp = *ptr;
619 tmp += 0xc0dec0de;
Kees Cookaac416f2014-02-09 13:48:47 -0800620
621 pr_info("attempting bad write at %p\n", ptr);
Kees Cook9ae113c2013-10-24 09:25:57 -0700622 *ptr = tmp;
623
624 vm_munmap(user_addr, PAGE_SIZE);
625
626 break;
627 }
628 case CT_WRITE_RO: {
Kees Cook7cca0712016-02-17 14:41:16 -0800629 /* Explicitly cast away "const" for the test. */
630 unsigned long *ptr = (unsigned long *)&rodata;
Kees Cook9ae113c2013-10-24 09:25:57 -0700631
Kees Cook7cca0712016-02-17 14:41:16 -0800632 pr_info("attempting bad rodata write at %p\n", ptr);
633 *ptr ^= 0xabcd1234;
Kees Cookaac416f2014-02-09 13:48:47 -0800634
Kees Cook7cca0712016-02-17 14:41:16 -0800635 break;
636 }
637 case CT_WRITE_RO_AFTER_INIT: {
638 unsigned long *ptr = &ro_after_init;
639
640 /*
641 * Verify we were written to during init. Since an Oops
642 * is considered a "success", a failure is to just skip the
643 * real test.
644 */
645 if ((*ptr & 0xAA) != 0xAA) {
646 pr_info("%p was NOT written during init!?\n", ptr);
647 break;
648 }
649
650 pr_info("attempting bad ro_after_init write at %p\n", ptr);
Kees Cook9ae113c2013-10-24 09:25:57 -0700651 *ptr ^= 0xabcd1234;
652
653 break;
654 }
Kees Cookdc2b9e92014-02-09 13:48:48 -0800655 case CT_WRITE_KERN: {
656 size_t size;
657 unsigned char *ptr;
658
659 size = (unsigned long)do_overwritten -
660 (unsigned long)do_nothing;
661 ptr = (unsigned char *)do_overwritten;
662
663 pr_info("attempting bad %zu byte write at %p\n", size, ptr);
664 memcpy(ptr, (unsigned char *)do_nothing, size);
665 flush_icache_range((unsigned long)ptr,
666 (unsigned long)(ptr + size));
667
668 do_overwritten();
669 break;
670 }
David Windsor5fd9e482015-12-17 00:56:36 -0500671 case CT_WRAP_ATOMIC: {
672 atomic_t under = ATOMIC_INIT(INT_MIN);
673 atomic_t over = ATOMIC_INIT(INT_MAX);
674
675 pr_info("attempting atomic underflow\n");
676 atomic_dec(&under);
677 pr_info("attempting atomic overflow\n");
678 atomic_inc(&over);
679
680 return;
681 }
Namhyung Kim93e2f582010-10-26 14:22:40 -0700682 case CT_NONE:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800683 default:
684 break;
685 }
686
687}
688
689static void lkdtm_handler(void)
690{
Josh Huntaa2c96d2011-06-27 16:18:08 -0700691 unsigned long flags;
Cong Wang92618182012-02-03 15:37:15 -0800692 bool do_it = false;
Josh Huntaa2c96d2011-06-27 16:18:08 -0700693
694 spin_lock_irqsave(&count_lock, flags);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800695 count--;
Kees Cookfeac6e22014-02-09 13:48:46 -0800696 pr_info("Crash point %s of type %s hit, trigger in %d rounds\n",
697 cp_name_to_str(cpoint), cp_type_to_str(cptype), count);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700698
699 if (count == 0) {
Cong Wang92618182012-02-03 15:37:15 -0800700 do_it = true;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700701 count = cpoint_count;
702 }
Josh Huntaa2c96d2011-06-27 16:18:08 -0700703 spin_unlock_irqrestore(&count_lock, flags);
Cong Wang92618182012-02-03 15:37:15 -0800704
705 if (do_it)
706 lkdtm_do_action(cptype);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700707}
708
Simon Kagstrom0347af42010-03-05 13:42:49 -0800709static int lkdtm_register_cpoint(enum cname which)
Ankita Garg8bb31b92006-10-02 02:17:36 -0700710{
711 int ret;
712
Namhyung Kim93e2f582010-10-26 14:22:40 -0700713 cpoint = CN_INVALID;
Simon Kagstrom0347af42010-03-05 13:42:49 -0800714 if (lkdtm.entry != NULL)
715 unregister_jprobe(&lkdtm);
Ankita Garg8bb31b92006-10-02 02:17:36 -0700716
Simon Kagstrom0347af42010-03-05 13:42:49 -0800717 switch (which) {
Namhyung Kim93e2f582010-10-26 14:22:40 -0700718 case CN_DIRECT:
Simon Kagstrom0347af42010-03-05 13:42:49 -0800719 lkdtm_do_action(cptype);
720 return 0;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700721 case CN_INT_HARDWARE_ENTRY:
M. Mohan Kumarf58f2fa2009-09-22 16:43:29 -0700722 lkdtm.kp.symbol_name = "do_IRQ";
Ankita Garg8bb31b92006-10-02 02:17:36 -0700723 lkdtm.entry = (kprobe_opcode_t*) jp_do_irq;
724 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700725 case CN_INT_HW_IRQ_EN:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700726 lkdtm.kp.symbol_name = "handle_IRQ_event";
727 lkdtm.entry = (kprobe_opcode_t*) jp_handle_irq_event;
728 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700729 case CN_INT_TASKLET_ENTRY:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700730 lkdtm.kp.symbol_name = "tasklet_action";
731 lkdtm.entry = (kprobe_opcode_t*) jp_tasklet_action;
732 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700733 case CN_FS_DEVRW:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700734 lkdtm.kp.symbol_name = "ll_rw_block";
735 lkdtm.entry = (kprobe_opcode_t*) jp_ll_rw_block;
736 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700737 case CN_MEM_SWAPOUT:
Ankita Garg18a61e42006-11-05 23:52:07 -0800738 lkdtm.kp.symbol_name = "shrink_inactive_list";
739 lkdtm.entry = (kprobe_opcode_t*) jp_shrink_inactive_list;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700740 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700741 case CN_TIMERADD:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700742 lkdtm.kp.symbol_name = "hrtimer_start";
743 lkdtm.entry = (kprobe_opcode_t*) jp_hrtimer_start;
744 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700745 case CN_SCSI_DISPATCH_CMD:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700746 lkdtm.kp.symbol_name = "scsi_dispatch_cmd";
747 lkdtm.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd;
748 break;
Namhyung Kim93e2f582010-10-26 14:22:40 -0700749 case CN_IDE_CORE_CP:
Ankita Garg8bb31b92006-10-02 02:17:36 -0700750#ifdef CONFIG_IDE
751 lkdtm.kp.symbol_name = "generic_ide_ioctl";
752 lkdtm.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl;
753#else
Kees Cookfeac6e22014-02-09 13:48:46 -0800754 pr_info("Crash point not available\n");
Simon Kagstrom0347af42010-03-05 13:42:49 -0800755 return -EINVAL;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700756#endif
757 break;
758 default:
Kees Cookfeac6e22014-02-09 13:48:46 -0800759 pr_info("Invalid Crash Point\n");
Simon Kagstrom0347af42010-03-05 13:42:49 -0800760 return -EINVAL;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700761 }
762
Simon Kagstrom0347af42010-03-05 13:42:49 -0800763 cpoint = which;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700764 if ((ret = register_jprobe(&lkdtm)) < 0) {
Kees Cookfeac6e22014-02-09 13:48:46 -0800765 pr_info("Couldn't register jprobe\n");
Namhyung Kim93e2f582010-10-26 14:22:40 -0700766 cpoint = CN_INVALID;
Ankita Garg8bb31b92006-10-02 02:17:36 -0700767 }
768
Simon Kagstrom0347af42010-03-05 13:42:49 -0800769 return ret;
770}
771
772static ssize_t do_register_entry(enum cname which, struct file *f,
773 const char __user *user_buf, size_t count, loff_t *off)
774{
775 char *buf;
776 int err;
777
778 if (count >= PAGE_SIZE)
779 return -EINVAL;
780
781 buf = (char *)__get_free_page(GFP_KERNEL);
782 if (!buf)
783 return -ENOMEM;
784 if (copy_from_user(buf, user_buf, count)) {
785 free_page((unsigned long) buf);
786 return -EFAULT;
787 }
788 /* NULL-terminate and remove enter */
789 buf[count] = '\0';
790 strim(buf);
791
792 cptype = parse_cp_type(buf, count);
793 free_page((unsigned long) buf);
794
Namhyung Kim93e2f582010-10-26 14:22:40 -0700795 if (cptype == CT_NONE)
Simon Kagstrom0347af42010-03-05 13:42:49 -0800796 return -EINVAL;
797
798 err = lkdtm_register_cpoint(which);
799 if (err < 0)
800 return err;
801
802 *off += count;
803
804 return count;
805}
806
807/* Generic read callback that just prints out the available crash types */
808static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
809 size_t count, loff_t *off)
810{
811 char *buf;
812 int i, n, out;
813
814 buf = (char *)__get_free_page(GFP_KERNEL);
Alan Cox086ff4b2012-07-30 14:43:24 -0700815 if (buf == NULL)
816 return -ENOMEM;
Simon Kagstrom0347af42010-03-05 13:42:49 -0800817
818 n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
819 for (i = 0; i < ARRAY_SIZE(cp_type); i++)
820 n += snprintf(buf + n, PAGE_SIZE - n, "%s\n", cp_type[i]);
821 buf[n] = '\0';
822
823 out = simple_read_from_buffer(user_buf, count, off,
824 buf, n);
825 free_page((unsigned long) buf);
826
827 return out;
828}
829
830static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
831{
Ankita Garg8bb31b92006-10-02 02:17:36 -0700832 return 0;
833}
834
Simon Kagstrom0347af42010-03-05 13:42:49 -0800835
836static ssize_t int_hardware_entry(struct file *f, const char __user *buf,
837 size_t count, loff_t *off)
838{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700839 return do_register_entry(CN_INT_HARDWARE_ENTRY, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800840}
841
842static ssize_t int_hw_irq_en(struct file *f, const char __user *buf,
843 size_t count, loff_t *off)
844{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700845 return do_register_entry(CN_INT_HW_IRQ_EN, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800846}
847
848static ssize_t int_tasklet_entry(struct file *f, const char __user *buf,
849 size_t count, loff_t *off)
850{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700851 return do_register_entry(CN_INT_TASKLET_ENTRY, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800852}
853
854static ssize_t fs_devrw_entry(struct file *f, const char __user *buf,
855 size_t count, loff_t *off)
856{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700857 return do_register_entry(CN_FS_DEVRW, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800858}
859
860static ssize_t mem_swapout_entry(struct file *f, const char __user *buf,
861 size_t count, loff_t *off)
862{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700863 return do_register_entry(CN_MEM_SWAPOUT, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800864}
865
866static ssize_t timeradd_entry(struct file *f, const char __user *buf,
867 size_t count, loff_t *off)
868{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700869 return do_register_entry(CN_TIMERADD, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800870}
871
872static ssize_t scsi_dispatch_cmd_entry(struct file *f,
873 const char __user *buf, size_t count, loff_t *off)
874{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700875 return do_register_entry(CN_SCSI_DISPATCH_CMD, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800876}
877
878static ssize_t ide_core_cp_entry(struct file *f, const char __user *buf,
879 size_t count, loff_t *off)
880{
Namhyung Kim93e2f582010-10-26 14:22:40 -0700881 return do_register_entry(CN_IDE_CORE_CP, f, buf, count, off);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800882}
883
884/* Special entry to just crash directly. Available without KPROBEs */
885static ssize_t direct_entry(struct file *f, const char __user *user_buf,
886 size_t count, loff_t *off)
887{
888 enum ctype type;
889 char *buf;
890
891 if (count >= PAGE_SIZE)
892 return -EINVAL;
893 if (count < 1)
894 return -EINVAL;
895
896 buf = (char *)__get_free_page(GFP_KERNEL);
897 if (!buf)
898 return -ENOMEM;
899 if (copy_from_user(buf, user_buf, count)) {
900 free_page((unsigned long) buf);
901 return -EFAULT;
902 }
903 /* NULL-terminate and remove enter */
904 buf[count] = '\0';
905 strim(buf);
906
907 type = parse_cp_type(buf, count);
908 free_page((unsigned long) buf);
Namhyung Kim93e2f582010-10-26 14:22:40 -0700909 if (type == CT_NONE)
Simon Kagstrom0347af42010-03-05 13:42:49 -0800910 return -EINVAL;
911
Kees Cookfeac6e22014-02-09 13:48:46 -0800912 pr_info("Performing direct entry %s\n", cp_type_to_str(type));
Simon Kagstrom0347af42010-03-05 13:42:49 -0800913 lkdtm_do_action(type);
914 *off += count;
915
916 return count;
917}
918
919struct crash_entry {
920 const char *name;
921 const struct file_operations fops;
922};
923
924static const struct crash_entry crash_entries[] = {
925 {"DIRECT", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200926 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800927 .open = lkdtm_debugfs_open,
928 .write = direct_entry} },
929 {"INT_HARDWARE_ENTRY", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200930 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800931 .open = lkdtm_debugfs_open,
932 .write = int_hardware_entry} },
933 {"INT_HW_IRQ_EN", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200934 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800935 .open = lkdtm_debugfs_open,
936 .write = int_hw_irq_en} },
937 {"INT_TASKLET_ENTRY", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200938 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800939 .open = lkdtm_debugfs_open,
940 .write = int_tasklet_entry} },
941 {"FS_DEVRW", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200942 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800943 .open = lkdtm_debugfs_open,
944 .write = fs_devrw_entry} },
945 {"MEM_SWAPOUT", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200946 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800947 .open = lkdtm_debugfs_open,
948 .write = mem_swapout_entry} },
949 {"TIMERADD", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200950 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800951 .open = lkdtm_debugfs_open,
952 .write = timeradd_entry} },
953 {"SCSI_DISPATCH_CMD", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200954 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800955 .open = lkdtm_debugfs_open,
956 .write = scsi_dispatch_cmd_entry} },
957 {"IDE_CORE_CP", {.read = lkdtm_debugfs_read,
Arnd Bergmann05271ec2010-07-06 19:10:26 +0200958 .llseek = generic_file_llseek,
Simon Kagstrom0347af42010-03-05 13:42:49 -0800959 .open = lkdtm_debugfs_open,
960 .write = ide_core_cp_entry} },
961};
962
963static struct dentry *lkdtm_debugfs_root;
964
965static int __init lkdtm_module_init(void)
966{
967 int ret = -EINVAL;
968 int n_debugfs_entries = 1; /* Assume only the direct entry */
969 int i;
970
Kees Cook7cca0712016-02-17 14:41:16 -0800971 /* Make sure we can write to __ro_after_init values during __init */
972 ro_after_init |= 0xAA;
973
Simon Kagstrom0347af42010-03-05 13:42:49 -0800974 /* Register debugfs interface */
975 lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
976 if (!lkdtm_debugfs_root) {
Kees Cookfeac6e22014-02-09 13:48:46 -0800977 pr_err("creating root dir failed\n");
Simon Kagstrom0347af42010-03-05 13:42:49 -0800978 return -ENODEV;
979 }
980
981#ifdef CONFIG_KPROBES
982 n_debugfs_entries = ARRAY_SIZE(crash_entries);
983#endif
984
985 for (i = 0; i < n_debugfs_entries; i++) {
986 const struct crash_entry *cur = &crash_entries[i];
987 struct dentry *de;
988
989 de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
990 NULL, &cur->fops);
991 if (de == NULL) {
Kees Cookfeac6e22014-02-09 13:48:46 -0800992 pr_err("could not create %s\n", cur->name);
Simon Kagstrom0347af42010-03-05 13:42:49 -0800993 goto out_err;
994 }
995 }
996
997 if (lkdtm_parse_commandline() == -EINVAL) {
Kees Cookfeac6e22014-02-09 13:48:46 -0800998 pr_info("Invalid command\n");
Simon Kagstrom0347af42010-03-05 13:42:49 -0800999 goto out_err;
1000 }
1001
Namhyung Kim93e2f582010-10-26 14:22:40 -07001002 if (cpoint != CN_INVALID && cptype != CT_NONE) {
Simon Kagstrom0347af42010-03-05 13:42:49 -08001003 ret = lkdtm_register_cpoint(cpoint);
1004 if (ret < 0) {
Kees Cookfeac6e22014-02-09 13:48:46 -08001005 pr_info("Invalid crash point %d\n", cpoint);
Simon Kagstrom0347af42010-03-05 13:42:49 -08001006 goto out_err;
1007 }
Kees Cookfeac6e22014-02-09 13:48:46 -08001008 pr_info("Crash point %s of type %s registered\n",
1009 cpoint_name, cpoint_type);
Simon Kagstrom0347af42010-03-05 13:42:49 -08001010 } else {
Kees Cookfeac6e22014-02-09 13:48:46 -08001011 pr_info("No crash points registered, enable through debugfs\n");
Simon Kagstrom0347af42010-03-05 13:42:49 -08001012 }
1013
1014 return 0;
1015
1016out_err:
1017 debugfs_remove_recursive(lkdtm_debugfs_root);
1018 return ret;
1019}
1020
Adrian Bunk21181162008-02-06 01:36:50 -08001021static void __exit lkdtm_module_exit(void)
Ankita Garg8bb31b92006-10-02 02:17:36 -07001022{
Simon Kagstrom0347af42010-03-05 13:42:49 -08001023 debugfs_remove_recursive(lkdtm_debugfs_root);
1024
1025 unregister_jprobe(&lkdtm);
Kees Cookfeac6e22014-02-09 13:48:46 -08001026 pr_info("Crash point unregistered\n");
Ankita Garg8bb31b92006-10-02 02:17:36 -07001027}
1028
1029module_init(lkdtm_module_init);
1030module_exit(lkdtm_module_exit);
1031
1032MODULE_LICENSE("GPL");
Terry Chiada869202014-07-02 21:02:25 +08001033MODULE_DESCRIPTION("Kprobe module for testing crash dumps");