Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Kprobe module for testing crash dumps |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | * |
| 18 | * Copyright (C) IBM Corporation, 2006 |
| 19 | * |
| 20 | * Author: Ankita Garg <ankita@in.ibm.com> |
| 21 | * |
| 22 | * This module induces system failures at predefined crashpoints to |
| 23 | * evaluate the reliability of crash dumps obtained using different dumping |
| 24 | * solutions. |
| 25 | * |
| 26 | * It is adapted from the Linux Kernel Dump Test Tool by |
| 27 | * Fernando Luis Vazquez Cao <http://lkdtt.sourceforge.net> |
| 28 | * |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 29 | * Debugfs support added by Simon Kagstrom <simon.kagstrom@netinsight.net> |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 30 | * |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 31 | * See Documentation/fault-injection/provoke-crashes.txt for instructions |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 32 | */ |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 33 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 34 | |
| 35 | #include <linux/kernel.h> |
Randy Dunlap | 5d861d9 | 2006-11-02 22:07:06 -0800 | [diff] [blame] | 36 | #include <linux/fs.h> |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 37 | #include <linux/module.h> |
Randy Dunlap | 5d861d9 | 2006-11-02 22:07:06 -0800 | [diff] [blame] | 38 | #include <linux/buffer_head.h> |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 39 | #include <linux/kprobes.h> |
Randy Dunlap | 5d861d9 | 2006-11-02 22:07:06 -0800 | [diff] [blame] | 40 | #include <linux/list.h> |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 41 | #include <linux/init.h> |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 42 | #include <linux/interrupt.h> |
Randy Dunlap | 5d861d9 | 2006-11-02 22:07:06 -0800 | [diff] [blame] | 43 | #include <linux/hrtimer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 44 | #include <linux/slab.h> |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 45 | #include <scsi/scsi_cmnd.h> |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 46 | #include <linux/debugfs.h> |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 47 | #include <linux/vmalloc.h> |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 48 | #include <linux/mman.h> |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 49 | |
| 50 | #ifdef CONFIG_IDE |
| 51 | #include <linux/ide.h> |
| 52 | #endif |
| 53 | |
Kees Cook | 7d196ac | 2013-10-24 09:25:39 -0700 | [diff] [blame] | 54 | /* |
| 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 Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 66 | #define DEFAULT_COUNT 10 |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 67 | #define EXEC_SIZE 64 |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 68 | |
| 69 | enum cname { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 70 | 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 Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | enum ctype { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 83 | CT_NONE, |
| 84 | CT_PANIC, |
| 85 | CT_BUG, |
Kees Cook | 6589272 | 2013-07-08 10:01:31 -0700 | [diff] [blame] | 86 | CT_WARNING, |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 87 | 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 Cook | 274a585 | 2013-07-08 10:01:32 -0700 | [diff] [blame] | 96 | CT_SPINLOCKUP, |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 97 | CT_HUNG_TASK, |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 98 | CT_EXEC_DATA, |
| 99 | CT_EXEC_STACK, |
| 100 | CT_EXEC_KMALLOC, |
| 101 | CT_EXEC_VMALLOC, |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 102 | CT_EXEC_USERSPACE, |
| 103 | CT_ACCESS_USERSPACE, |
| 104 | CT_WRITE_RO, |
Kees Cook | dc2b9e9 | 2014-02-09 13:48:48 -0800 | [diff] [blame] | 105 | CT_WRITE_KERN, |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | static 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 Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 116 | "IDE_CORE_CP", |
| 117 | "DIRECT", |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | static char* cp_type[] = { |
| 121 | "PANIC", |
| 122 | "BUG", |
Kees Cook | 6589272 | 2013-07-08 10:01:31 -0700 | [diff] [blame] | 123 | "WARNING", |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 124 | "EXCEPTION", |
| 125 | "LOOP", |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 126 | "OVERFLOW", |
| 127 | "CORRUPT_STACK", |
| 128 | "UNALIGNED_LOAD_STORE_WRITE", |
| 129 | "OVERWRITE_ALLOCATION", |
| 130 | "WRITE_AFTER_FREE", |
Frederic Weisbecker | a48223f | 2010-05-26 14:44:29 -0700 | [diff] [blame] | 131 | "SOFTLOCKUP", |
| 132 | "HARDLOCKUP", |
Kees Cook | 274a585 | 2013-07-08 10:01:32 -0700 | [diff] [blame] | 133 | "SPINLOCKUP", |
Frederic Weisbecker | a48223f | 2010-05-26 14:44:29 -0700 | [diff] [blame] | 134 | "HUNG_TASK", |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 135 | "EXEC_DATA", |
| 136 | "EXEC_STACK", |
| 137 | "EXEC_KMALLOC", |
| 138 | "EXEC_VMALLOC", |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 139 | "EXEC_USERSPACE", |
| 140 | "ACCESS_USERSPACE", |
| 141 | "WRITE_RO", |
Kees Cook | dc2b9e9 | 2014-02-09 13:48:48 -0800 | [diff] [blame] | 142 | "WRITE_KERN", |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | static struct jprobe lkdtm; |
| 146 | |
| 147 | static int lkdtm_parse_commandline(void); |
| 148 | static void lkdtm_handler(void); |
| 149 | |
Al Viro | ec1c620 | 2007-02-09 16:05:17 +0000 | [diff] [blame] | 150 | static char* cpoint_name; |
| 151 | static char* cpoint_type; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 152 | static int cpoint_count = DEFAULT_COUNT; |
| 153 | static int recur_count = REC_NUM_DEFAULT; |
| 154 | |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 155 | static enum cname cpoint = CN_INVALID; |
| 156 | static enum ctype cptype = CT_NONE; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 157 | static int count = DEFAULT_COUNT; |
Josh Hunt | aa2c96d | 2011-06-27 16:18:08 -0700 | [diff] [blame] | 158 | static DEFINE_SPINLOCK(count_lock); |
Kees Cook | 274a585 | 2013-07-08 10:01:32 -0700 | [diff] [blame] | 159 | static DEFINE_SPINLOCK(lock_me_up); |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 160 | |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 161 | static u8 data_area[EXEC_SIZE]; |
| 162 | |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 163 | static const unsigned long rodata = 0xAA55AA55; |
| 164 | |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 165 | module_param(recur_count, int, 0644); |
Kees Cook | 7d196ac | 2013-10-24 09:25:39 -0700 | [diff] [blame] | 166 | MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test"); |
Rusty Russell | dca4130 | 2010-08-11 23:04:21 -0600 | [diff] [blame] | 167 | module_param(cpoint_name, charp, 0444); |
Randy Dunlap | 5d861d9 | 2006-11-02 22:07:06 -0800 | [diff] [blame] | 168 | MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed"); |
Rusty Russell | dca4130 | 2010-08-11 23:04:21 -0600 | [diff] [blame] | 169 | module_param(cpoint_type, charp, 0444); |
Randy Dunlap | 5d861d9 | 2006-11-02 22:07:06 -0800 | [diff] [blame] | 170 | MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\ |
| 171 | "hitting the crash point"); |
| 172 | module_param(cpoint_count, int, 0644); |
| 173 | MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\ |
| 174 | "crash point is to be hit to trigger action"); |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 175 | |
Adrian Bunk | 2118116 | 2008-02-06 01:36:50 -0800 | [diff] [blame] | 176 | static unsigned int jp_do_irq(unsigned int irq) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 177 | { |
| 178 | lkdtm_handler(); |
| 179 | jprobe_return(); |
| 180 | return 0; |
| 181 | } |
| 182 | |
Adrian Bunk | 2118116 | 2008-02-06 01:36:50 -0800 | [diff] [blame] | 183 | static irqreturn_t jp_handle_irq_event(unsigned int irq, |
| 184 | struct irqaction *action) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 185 | { |
| 186 | lkdtm_handler(); |
| 187 | jprobe_return(); |
| 188 | return 0; |
| 189 | } |
| 190 | |
Adrian Bunk | 2118116 | 2008-02-06 01:36:50 -0800 | [diff] [blame] | 191 | static void jp_tasklet_action(struct softirq_action *a) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 192 | { |
| 193 | lkdtm_handler(); |
| 194 | jprobe_return(); |
| 195 | } |
| 196 | |
Adrian Bunk | 2118116 | 2008-02-06 01:36:50 -0800 | [diff] [blame] | 197 | static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[]) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 198 | { |
| 199 | lkdtm_handler(); |
| 200 | jprobe_return(); |
| 201 | } |
| 202 | |
| 203 | struct scan_control; |
| 204 | |
Adrian Bunk | 2118116 | 2008-02-06 01:36:50 -0800 | [diff] [blame] | 205 | static unsigned long jp_shrink_inactive_list(unsigned long max_scan, |
| 206 | struct zone *zone, |
| 207 | struct scan_control *sc) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 208 | { |
| 209 | lkdtm_handler(); |
| 210 | jprobe_return(); |
| 211 | return 0; |
| 212 | } |
| 213 | |
Adrian Bunk | 2118116 | 2008-02-06 01:36:50 -0800 | [diff] [blame] | 214 | static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim, |
| 215 | const enum hrtimer_mode mode) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 216 | { |
| 217 | lkdtm_handler(); |
| 218 | jprobe_return(); |
| 219 | return 0; |
| 220 | } |
| 221 | |
Adrian Bunk | 2118116 | 2008-02-06 01:36:50 -0800 | [diff] [blame] | 222 | static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 223 | { |
| 224 | lkdtm_handler(); |
| 225 | jprobe_return(); |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | #ifdef CONFIG_IDE |
Rashika Kheria | 4462943 | 2013-12-13 12:29:42 +0530 | [diff] [blame] | 230 | static int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file, |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 231 | 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 Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 240 | /* Return the crashpoint number or NONE if the name is invalid */ |
| 241 | static 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 Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 250 | return CT_NONE; |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static const char *cp_type_to_str(enum ctype type) |
| 254 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 255 | if (type == CT_NONE || type < 0 || type > ARRAY_SIZE(cp_type)) |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 256 | return "None"; |
| 257 | |
| 258 | return cp_type[type - 1]; |
| 259 | } |
| 260 | |
| 261 | static const char *cp_name_to_str(enum cname name) |
| 262 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 263 | if (name == CN_INVALID || name < 0 || name > ARRAY_SIZE(cp_name)) |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 264 | return "INVALID"; |
| 265 | |
| 266 | return cp_name[name - 1]; |
| 267 | } |
| 268 | |
| 269 | |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 270 | static int lkdtm_parse_commandline(void) |
| 271 | { |
| 272 | int i; |
Josh Hunt | aa2c96d | 2011-06-27 16:18:08 -0700 | [diff] [blame] | 273 | unsigned long flags; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 274 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 275 | if (cpoint_count < 1 || recur_count < 1) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 276 | return -EINVAL; |
| 277 | |
Josh Hunt | aa2c96d | 2011-06-27 16:18:08 -0700 | [diff] [blame] | 278 | spin_lock_irqsave(&count_lock, flags); |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 279 | count = cpoint_count; |
Josh Hunt | aa2c96d | 2011-06-27 16:18:08 -0700 | [diff] [blame] | 280 | spin_unlock_irqrestore(&count_lock, flags); |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 281 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 282 | /* 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 Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 291 | if (cptype == CT_NONE) |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 292 | 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 Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Kees Cook | 7d196ac | 2013-10-24 09:25:39 -0700 | [diff] [blame] | 305 | static int recursive_loop(int remaining) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 306 | { |
Kees Cook | 7d196ac | 2013-10-24 09:25:39 -0700 | [diff] [blame] | 307 | char buf[REC_STACK_SIZE]; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 308 | |
Kees Cook | 7d196ac | 2013-10-24 09:25:39 -0700 | [diff] [blame] | 309 | /* Make sure compiler does not optimize this away. */ |
| 310 | memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE); |
| 311 | if (!remaining) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 312 | return 0; |
| 313 | else |
Kees Cook | 7d196ac | 2013-10-24 09:25:39 -0700 | [diff] [blame] | 314 | return recursive_loop(remaining - 1); |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 317 | static void do_nothing(void) |
| 318 | { |
| 319 | return; |
| 320 | } |
| 321 | |
Kees Cook | dc2b9e9 | 2014-02-09 13:48:48 -0800 | [diff] [blame] | 322 | /* Must immediately follow do_nothing for size calculuations to work out. */ |
| 323 | static void do_overwritten(void) |
| 324 | { |
| 325 | pr_info("do_overwritten wasn't overwritten!\n"); |
| 326 | return; |
| 327 | } |
| 328 | |
Kees Cook | 629c66a | 2013-10-24 18:05:42 -0700 | [diff] [blame] | 329 | static 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 Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 337 | static void execute_location(void *dst) |
| 338 | { |
| 339 | void (*func)(void) = dst; |
| 340 | |
Kees Cook | aac416f | 2014-02-09 13:48:47 -0800 | [diff] [blame] | 341 | pr_info("attempting ok execution at %p\n", do_nothing); |
| 342 | do_nothing(); |
| 343 | |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 344 | memcpy(dst, do_nothing, EXEC_SIZE); |
Kees Cook | aac416f | 2014-02-09 13:48:47 -0800 | [diff] [blame] | 345 | flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE); |
| 346 | pr_info("attempting bad execution at %p\n", func); |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 347 | func(); |
| 348 | } |
| 349 | |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 350 | static void execute_user_location(void *dst) |
| 351 | { |
Kees Cook | 5123662 | 2013-11-11 11:23:49 -0800 | [diff] [blame] | 352 | /* Intentionally crossing kernel/user memory boundary. */ |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 353 | void (*func)(void) = dst; |
| 354 | |
Kees Cook | aac416f | 2014-02-09 13:48:47 -0800 | [diff] [blame] | 355 | pr_info("attempting ok execution at %p\n", do_nothing); |
| 356 | do_nothing(); |
| 357 | |
Kees Cook | 5123662 | 2013-11-11 11:23:49 -0800 | [diff] [blame] | 358 | if (copy_to_user((void __user *)dst, do_nothing, EXEC_SIZE)) |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 359 | return; |
Kees Cook | aac416f | 2014-02-09 13:48:47 -0800 | [diff] [blame] | 360 | flush_icache_range((unsigned long)dst, (unsigned long)dst + EXEC_SIZE); |
| 361 | pr_info("attempting bad execution at %p\n", func); |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 362 | func(); |
| 363 | } |
| 364 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 365 | static void lkdtm_do_action(enum ctype which) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 366 | { |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 367 | switch (which) { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 368 | case CT_PANIC: |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 369 | panic("dumptest"); |
| 370 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 371 | case CT_BUG: |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 372 | BUG(); |
| 373 | break; |
Kees Cook | 6589272 | 2013-07-08 10:01:31 -0700 | [diff] [blame] | 374 | case CT_WARNING: |
| 375 | WARN_ON(1); |
| 376 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 377 | case CT_EXCEPTION: |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 378 | *((int *) 0) = 0; |
| 379 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 380 | case CT_LOOP: |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 381 | for (;;) |
| 382 | ; |
| 383 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 384 | case CT_OVERFLOW: |
Kees Cook | 7d196ac | 2013-10-24 09:25:39 -0700 | [diff] [blame] | 385 | (void) recursive_loop(recur_count); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 386 | break; |
Kees Cook | 629c66a | 2013-10-24 18:05:42 -0700 | [diff] [blame] | 387 | case CT_CORRUPT_STACK: |
| 388 | corrupt_stack(); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 389 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 390 | case CT_UNALIGNED_LOAD_STORE_WRITE: { |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 391 | 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 Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 402 | case CT_OVERWRITE_ALLOCATION: { |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 403 | 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 Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 410 | case CT_WRITE_AFTER_FREE: { |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 411 | 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 Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 419 | case CT_SOFTLOCKUP: |
Frederic Weisbecker | a48223f | 2010-05-26 14:44:29 -0700 | [diff] [blame] | 420 | preempt_disable(); |
| 421 | for (;;) |
| 422 | cpu_relax(); |
| 423 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 424 | case CT_HARDLOCKUP: |
Frederic Weisbecker | a48223f | 2010-05-26 14:44:29 -0700 | [diff] [blame] | 425 | local_irq_disable(); |
| 426 | for (;;) |
| 427 | cpu_relax(); |
| 428 | break; |
Kees Cook | 274a585 | 2013-07-08 10:01:32 -0700 | [diff] [blame] | 429 | case CT_SPINLOCKUP: |
| 430 | /* Must be called twice to trigger. */ |
| 431 | spin_lock(&lock_me_up); |
Kees Cook | 5123662 | 2013-11-11 11:23:49 -0800 | [diff] [blame] | 432 | /* Let sparse know we intended to exit holding the lock. */ |
| 433 | __release(&lock_me_up); |
Kees Cook | 274a585 | 2013-07-08 10:01:32 -0700 | [diff] [blame] | 434 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 435 | case CT_HUNG_TASK: |
Frederic Weisbecker | a48223f | 2010-05-26 14:44:29 -0700 | [diff] [blame] | 436 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 437 | schedule(); |
| 438 | break; |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 439 | 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 Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 459 | 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 Cook | aac416f | 2014-02-09 13:48:47 -0800 | [diff] [blame] | 486 | |
| 487 | pr_info("attempting bad read at %p\n", ptr); |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 488 | tmp = *ptr; |
| 489 | tmp += 0xc0dec0de; |
Kees Cook | aac416f | 2014-02-09 13:48:47 -0800 | [diff] [blame] | 490 | |
| 491 | pr_info("attempting bad write at %p\n", ptr); |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 492 | *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 Cook | aac416f | 2014-02-09 13:48:47 -0800 | [diff] [blame] | 502 | |
| 503 | pr_info("attempting bad write at %p\n", ptr); |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 504 | *ptr ^= 0xabcd1234; |
| 505 | |
| 506 | break; |
| 507 | } |
Kees Cook | dc2b9e9 | 2014-02-09 13:48:48 -0800 | [diff] [blame] | 508 | 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 Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 524 | case CT_NONE: |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 525 | default: |
| 526 | break; |
| 527 | } |
| 528 | |
| 529 | } |
| 530 | |
| 531 | static void lkdtm_handler(void) |
| 532 | { |
Josh Hunt | aa2c96d | 2011-06-27 16:18:08 -0700 | [diff] [blame] | 533 | unsigned long flags; |
Cong Wang | 9261818 | 2012-02-03 15:37:15 -0800 | [diff] [blame] | 534 | bool do_it = false; |
Josh Hunt | aa2c96d | 2011-06-27 16:18:08 -0700 | [diff] [blame] | 535 | |
| 536 | spin_lock_irqsave(&count_lock, flags); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 537 | count--; |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 538 | 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 Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 540 | |
| 541 | if (count == 0) { |
Cong Wang | 9261818 | 2012-02-03 15:37:15 -0800 | [diff] [blame] | 542 | do_it = true; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 543 | count = cpoint_count; |
| 544 | } |
Josh Hunt | aa2c96d | 2011-06-27 16:18:08 -0700 | [diff] [blame] | 545 | spin_unlock_irqrestore(&count_lock, flags); |
Cong Wang | 9261818 | 2012-02-03 15:37:15 -0800 | [diff] [blame] | 546 | |
| 547 | if (do_it) |
| 548 | lkdtm_do_action(cptype); |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 551 | static int lkdtm_register_cpoint(enum cname which) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 552 | { |
| 553 | int ret; |
| 554 | |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 555 | cpoint = CN_INVALID; |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 556 | if (lkdtm.entry != NULL) |
| 557 | unregister_jprobe(&lkdtm); |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 558 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 559 | switch (which) { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 560 | case CN_DIRECT: |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 561 | lkdtm_do_action(cptype); |
| 562 | return 0; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 563 | case CN_INT_HARDWARE_ENTRY: |
M. Mohan Kumar | f58f2fa | 2009-09-22 16:43:29 -0700 | [diff] [blame] | 564 | lkdtm.kp.symbol_name = "do_IRQ"; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 565 | lkdtm.entry = (kprobe_opcode_t*) jp_do_irq; |
| 566 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 567 | case CN_INT_HW_IRQ_EN: |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 568 | lkdtm.kp.symbol_name = "handle_IRQ_event"; |
| 569 | lkdtm.entry = (kprobe_opcode_t*) jp_handle_irq_event; |
| 570 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 571 | case CN_INT_TASKLET_ENTRY: |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 572 | lkdtm.kp.symbol_name = "tasklet_action"; |
| 573 | lkdtm.entry = (kprobe_opcode_t*) jp_tasklet_action; |
| 574 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 575 | case CN_FS_DEVRW: |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 576 | lkdtm.kp.symbol_name = "ll_rw_block"; |
| 577 | lkdtm.entry = (kprobe_opcode_t*) jp_ll_rw_block; |
| 578 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 579 | case CN_MEM_SWAPOUT: |
Ankita Garg | 18a61e4 | 2006-11-05 23:52:07 -0800 | [diff] [blame] | 580 | lkdtm.kp.symbol_name = "shrink_inactive_list"; |
| 581 | lkdtm.entry = (kprobe_opcode_t*) jp_shrink_inactive_list; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 582 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 583 | case CN_TIMERADD: |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 584 | lkdtm.kp.symbol_name = "hrtimer_start"; |
| 585 | lkdtm.entry = (kprobe_opcode_t*) jp_hrtimer_start; |
| 586 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 587 | case CN_SCSI_DISPATCH_CMD: |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 588 | lkdtm.kp.symbol_name = "scsi_dispatch_cmd"; |
| 589 | lkdtm.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd; |
| 590 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 591 | case CN_IDE_CORE_CP: |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 592 | #ifdef CONFIG_IDE |
| 593 | lkdtm.kp.symbol_name = "generic_ide_ioctl"; |
| 594 | lkdtm.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl; |
| 595 | #else |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 596 | pr_info("Crash point not available\n"); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 597 | return -EINVAL; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 598 | #endif |
| 599 | break; |
| 600 | default: |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 601 | pr_info("Invalid Crash Point\n"); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 602 | return -EINVAL; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 605 | cpoint = which; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 606 | if ((ret = register_jprobe(&lkdtm)) < 0) { |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 607 | pr_info("Couldn't register jprobe\n"); |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 608 | cpoint = CN_INVALID; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 611 | return ret; |
| 612 | } |
| 613 | |
| 614 | static 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 Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 637 | if (cptype == CT_NONE) |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 638 | 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 */ |
| 650 | static 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 Cox | 086ff4b | 2012-07-30 14:43:24 -0700 | [diff] [blame] | 657 | if (buf == NULL) |
| 658 | return -ENOMEM; |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 659 | |
| 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 | |
| 672 | static int lkdtm_debugfs_open(struct inode *inode, struct file *file) |
| 673 | { |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 674 | return 0; |
| 675 | } |
| 676 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 677 | |
| 678 | static ssize_t int_hardware_entry(struct file *f, const char __user *buf, |
| 679 | size_t count, loff_t *off) |
| 680 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 681 | return do_register_entry(CN_INT_HARDWARE_ENTRY, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | static ssize_t int_hw_irq_en(struct file *f, const char __user *buf, |
| 685 | size_t count, loff_t *off) |
| 686 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 687 | return do_register_entry(CN_INT_HW_IRQ_EN, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static ssize_t int_tasklet_entry(struct file *f, const char __user *buf, |
| 691 | size_t count, loff_t *off) |
| 692 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 693 | return do_register_entry(CN_INT_TASKLET_ENTRY, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | static ssize_t fs_devrw_entry(struct file *f, const char __user *buf, |
| 697 | size_t count, loff_t *off) |
| 698 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 699 | return do_register_entry(CN_FS_DEVRW, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | static ssize_t mem_swapout_entry(struct file *f, const char __user *buf, |
| 703 | size_t count, loff_t *off) |
| 704 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 705 | return do_register_entry(CN_MEM_SWAPOUT, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static ssize_t timeradd_entry(struct file *f, const char __user *buf, |
| 709 | size_t count, loff_t *off) |
| 710 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 711 | return do_register_entry(CN_TIMERADD, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static ssize_t scsi_dispatch_cmd_entry(struct file *f, |
| 715 | const char __user *buf, size_t count, loff_t *off) |
| 716 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 717 | return do_register_entry(CN_SCSI_DISPATCH_CMD, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | static ssize_t ide_core_cp_entry(struct file *f, const char __user *buf, |
| 721 | size_t count, loff_t *off) |
| 722 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 723 | return do_register_entry(CN_IDE_CORE_CP, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /* Special entry to just crash directly. Available without KPROBEs */ |
| 727 | static 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 Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 751 | if (type == CT_NONE) |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 752 | return -EINVAL; |
| 753 | |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 754 | pr_info("Performing direct entry %s\n", cp_type_to_str(type)); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 755 | lkdtm_do_action(type); |
| 756 | *off += count; |
| 757 | |
| 758 | return count; |
| 759 | } |
| 760 | |
| 761 | struct crash_entry { |
| 762 | const char *name; |
| 763 | const struct file_operations fops; |
| 764 | }; |
| 765 | |
| 766 | static const struct crash_entry crash_entries[] = { |
| 767 | {"DIRECT", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 768 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 769 | .open = lkdtm_debugfs_open, |
| 770 | .write = direct_entry} }, |
| 771 | {"INT_HARDWARE_ENTRY", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 772 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 773 | .open = lkdtm_debugfs_open, |
| 774 | .write = int_hardware_entry} }, |
| 775 | {"INT_HW_IRQ_EN", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 776 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 777 | .open = lkdtm_debugfs_open, |
| 778 | .write = int_hw_irq_en} }, |
| 779 | {"INT_TASKLET_ENTRY", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 780 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 781 | .open = lkdtm_debugfs_open, |
| 782 | .write = int_tasklet_entry} }, |
| 783 | {"FS_DEVRW", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 784 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 785 | .open = lkdtm_debugfs_open, |
| 786 | .write = fs_devrw_entry} }, |
| 787 | {"MEM_SWAPOUT", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 788 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 789 | .open = lkdtm_debugfs_open, |
| 790 | .write = mem_swapout_entry} }, |
| 791 | {"TIMERADD", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 792 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 793 | .open = lkdtm_debugfs_open, |
| 794 | .write = timeradd_entry} }, |
| 795 | {"SCSI_DISPATCH_CMD", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 796 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 797 | .open = lkdtm_debugfs_open, |
| 798 | .write = scsi_dispatch_cmd_entry} }, |
| 799 | {"IDE_CORE_CP", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 800 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 801 | .open = lkdtm_debugfs_open, |
| 802 | .write = ide_core_cp_entry} }, |
| 803 | }; |
| 804 | |
| 805 | static struct dentry *lkdtm_debugfs_root; |
| 806 | |
| 807 | static 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 Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 816 | pr_err("creating root dir failed\n"); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 817 | 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 Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 831 | pr_err("could not create %s\n", cur->name); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 832 | goto out_err; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | if (lkdtm_parse_commandline() == -EINVAL) { |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 837 | pr_info("Invalid command\n"); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 838 | goto out_err; |
| 839 | } |
| 840 | |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 841 | if (cpoint != CN_INVALID && cptype != CT_NONE) { |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 842 | ret = lkdtm_register_cpoint(cpoint); |
| 843 | if (ret < 0) { |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 844 | pr_info("Invalid crash point %d\n", cpoint); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 845 | goto out_err; |
| 846 | } |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 847 | pr_info("Crash point %s of type %s registered\n", |
| 848 | cpoint_name, cpoint_type); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 849 | } else { |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 850 | pr_info("No crash points registered, enable through debugfs\n"); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | return 0; |
| 854 | |
| 855 | out_err: |
| 856 | debugfs_remove_recursive(lkdtm_debugfs_root); |
| 857 | return ret; |
| 858 | } |
| 859 | |
Adrian Bunk | 2118116 | 2008-02-06 01:36:50 -0800 | [diff] [blame] | 860 | static void __exit lkdtm_module_exit(void) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 861 | { |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 862 | debugfs_remove_recursive(lkdtm_debugfs_root); |
| 863 | |
| 864 | unregister_jprobe(&lkdtm); |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 865 | pr_info("Crash point unregistered\n"); |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | module_init(lkdtm_module_init); |
| 869 | module_exit(lkdtm_module_exit); |
| 870 | |
| 871 | MODULE_LICENSE("GPL"); |