Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 1 | /* |
Kees Cook | 426f3a5 | 2016-06-03 11:16:32 -0700 | [diff] [blame] | 2 | * 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 Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 7 | * |
| 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 <ankita@in.ibm.com> |
| 25 | * |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 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 | 426f3a5 | 2016-06-03 11:16:32 -0700 | [diff] [blame] | 33 | #define pr_fmt(fmt) "lkdtm: " 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> |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 47 | |
| 48 | #ifdef CONFIG_IDE |
| 49 | #include <linux/ide.h> |
| 50 | #endif |
| 51 | |
Kees Cook | 9a49a52 | 2016-02-22 14:09:29 -0800 | [diff] [blame] | 52 | #include "lkdtm.h" |
| 53 | |
Kees Cook | d87c978 | 2016-06-29 08:07:11 -0700 | [diff] [blame] | 54 | #define DEFAULT_COUNT 10 |
| 55 | |
| 56 | static int lkdtm_parse_commandline(void); |
| 57 | static void lkdtm_handler(void); |
| 58 | |
Kees Cook | f2c6edc | 2016-06-29 08:10:36 -0700 | [diff] [blame^] | 59 | /* jprobe entry point handlers. */ |
| 60 | static unsigned int jp_do_irq(unsigned int irq) |
| 61 | { |
| 62 | lkdtm_handler(); |
| 63 | jprobe_return(); |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static irqreturn_t jp_handle_irq_event(unsigned int irq, |
| 68 | struct irqaction *action) |
| 69 | { |
| 70 | lkdtm_handler(); |
| 71 | jprobe_return(); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static void jp_tasklet_action(struct softirq_action *a) |
| 76 | { |
| 77 | lkdtm_handler(); |
| 78 | jprobe_return(); |
| 79 | } |
| 80 | |
| 81 | static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[]) |
| 82 | { |
| 83 | lkdtm_handler(); |
| 84 | jprobe_return(); |
| 85 | } |
| 86 | |
| 87 | struct scan_control; |
| 88 | |
| 89 | static unsigned long jp_shrink_inactive_list(unsigned long max_scan, |
| 90 | struct zone *zone, |
| 91 | struct scan_control *sc) |
| 92 | { |
| 93 | lkdtm_handler(); |
| 94 | jprobe_return(); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim, |
| 99 | const enum hrtimer_mode mode) |
| 100 | { |
| 101 | lkdtm_handler(); |
| 102 | jprobe_return(); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd) |
| 107 | { |
| 108 | lkdtm_handler(); |
| 109 | jprobe_return(); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | #ifdef CONFIG_IDE |
| 114 | static int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file, |
| 115 | struct block_device *bdev, unsigned int cmd, |
| 116 | unsigned long arg) |
| 117 | { |
| 118 | lkdtm_handler(); |
| 119 | jprobe_return(); |
| 120 | return 0; |
| 121 | } |
| 122 | #endif |
| 123 | |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 124 | enum cname { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 125 | CN_INVALID, |
| 126 | CN_INT_HARDWARE_ENTRY, |
| 127 | CN_INT_HW_IRQ_EN, |
| 128 | CN_INT_TASKLET_ENTRY, |
| 129 | CN_FS_DEVRW, |
| 130 | CN_MEM_SWAPOUT, |
| 131 | CN_TIMERADD, |
| 132 | CN_SCSI_DISPATCH_CMD, |
| 133 | CN_IDE_CORE_CP, |
| 134 | CN_DIRECT, |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | enum ctype { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 138 | CT_NONE, |
| 139 | CT_PANIC, |
| 140 | CT_BUG, |
Kees Cook | 6589272 | 2013-07-08 10:01:31 -0700 | [diff] [blame] | 141 | CT_WARNING, |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 142 | CT_EXCEPTION, |
| 143 | CT_LOOP, |
| 144 | CT_OVERFLOW, |
| 145 | CT_CORRUPT_STACK, |
| 146 | CT_UNALIGNED_LOAD_STORE_WRITE, |
| 147 | CT_OVERWRITE_ALLOCATION, |
| 148 | CT_WRITE_AFTER_FREE, |
Laura Abbott | bc0b8cc | 2016-02-25 16:36:42 -0800 | [diff] [blame] | 149 | CT_READ_AFTER_FREE, |
Laura Abbott | 920d451 | 2016-02-25 16:36:44 -0800 | [diff] [blame] | 150 | CT_WRITE_BUDDY_AFTER_FREE, |
| 151 | CT_READ_BUDDY_AFTER_FREE, |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 152 | CT_SOFTLOCKUP, |
| 153 | CT_HARDLOCKUP, |
Kees Cook | 274a585 | 2013-07-08 10:01:32 -0700 | [diff] [blame] | 154 | CT_SPINLOCKUP, |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 155 | CT_HUNG_TASK, |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 156 | CT_EXEC_DATA, |
| 157 | CT_EXEC_STACK, |
| 158 | CT_EXEC_KMALLOC, |
| 159 | CT_EXEC_VMALLOC, |
Kees Cook | 9a49a52 | 2016-02-22 14:09:29 -0800 | [diff] [blame] | 160 | CT_EXEC_RODATA, |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 161 | CT_EXEC_USERSPACE, |
| 162 | CT_ACCESS_USERSPACE, |
| 163 | CT_WRITE_RO, |
Kees Cook | 7cca071 | 2016-02-17 14:41:16 -0800 | [diff] [blame] | 164 | CT_WRITE_RO_AFTER_INIT, |
Kees Cook | dc2b9e9 | 2014-02-09 13:48:48 -0800 | [diff] [blame] | 165 | CT_WRITE_KERN, |
Kees Cook | b548452 | 2016-06-07 14:27:02 -0700 | [diff] [blame] | 166 | CT_ATOMIC_UNDERFLOW, |
| 167 | CT_ATOMIC_OVERFLOW, |
Kees Cook | aa981a6 | 2016-06-03 12:06:52 -0700 | [diff] [blame] | 168 | CT_USERCOPY_HEAP_SIZE_TO, |
| 169 | CT_USERCOPY_HEAP_SIZE_FROM, |
| 170 | CT_USERCOPY_HEAP_FLAG_TO, |
| 171 | CT_USERCOPY_HEAP_FLAG_FROM, |
| 172 | CT_USERCOPY_STACK_FRAME_TO, |
| 173 | CT_USERCOPY_STACK_FRAME_FROM, |
| 174 | CT_USERCOPY_STACK_BEYOND, |
Kees Cook | 6c35214 | 2016-06-23 22:01:26 -0700 | [diff] [blame] | 175 | CT_USERCOPY_KERNEL, |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | static char* cp_name[] = { |
Kees Cook | 329d416 | 2016-06-26 22:26:11 -0700 | [diff] [blame] | 179 | "INVALID", |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 180 | "INT_HARDWARE_ENTRY", |
| 181 | "INT_HW_IRQ_EN", |
| 182 | "INT_TASKLET_ENTRY", |
| 183 | "FS_DEVRW", |
| 184 | "MEM_SWAPOUT", |
| 185 | "TIMERADD", |
| 186 | "SCSI_DISPATCH_CMD", |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 187 | "IDE_CORE_CP", |
| 188 | "DIRECT", |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | static char* cp_type[] = { |
Kees Cook | 329d416 | 2016-06-26 22:26:11 -0700 | [diff] [blame] | 192 | "NONE", |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 193 | "PANIC", |
| 194 | "BUG", |
Kees Cook | 6589272 | 2013-07-08 10:01:31 -0700 | [diff] [blame] | 195 | "WARNING", |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 196 | "EXCEPTION", |
| 197 | "LOOP", |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 198 | "OVERFLOW", |
| 199 | "CORRUPT_STACK", |
| 200 | "UNALIGNED_LOAD_STORE_WRITE", |
| 201 | "OVERWRITE_ALLOCATION", |
| 202 | "WRITE_AFTER_FREE", |
Laura Abbott | bc0b8cc | 2016-02-25 16:36:42 -0800 | [diff] [blame] | 203 | "READ_AFTER_FREE", |
Laura Abbott | 920d451 | 2016-02-25 16:36:44 -0800 | [diff] [blame] | 204 | "WRITE_BUDDY_AFTER_FREE", |
| 205 | "READ_BUDDY_AFTER_FREE", |
Frederic Weisbecker | a48223f | 2010-05-26 14:44:29 -0700 | [diff] [blame] | 206 | "SOFTLOCKUP", |
| 207 | "HARDLOCKUP", |
Kees Cook | 274a585 | 2013-07-08 10:01:32 -0700 | [diff] [blame] | 208 | "SPINLOCKUP", |
Frederic Weisbecker | a48223f | 2010-05-26 14:44:29 -0700 | [diff] [blame] | 209 | "HUNG_TASK", |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 210 | "EXEC_DATA", |
| 211 | "EXEC_STACK", |
| 212 | "EXEC_KMALLOC", |
| 213 | "EXEC_VMALLOC", |
Kees Cook | 9a49a52 | 2016-02-22 14:09:29 -0800 | [diff] [blame] | 214 | "EXEC_RODATA", |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 215 | "EXEC_USERSPACE", |
| 216 | "ACCESS_USERSPACE", |
| 217 | "WRITE_RO", |
Kees Cook | 7cca071 | 2016-02-17 14:41:16 -0800 | [diff] [blame] | 218 | "WRITE_RO_AFTER_INIT", |
Kees Cook | dc2b9e9 | 2014-02-09 13:48:48 -0800 | [diff] [blame] | 219 | "WRITE_KERN", |
Kees Cook | b548452 | 2016-06-07 14:27:02 -0700 | [diff] [blame] | 220 | "ATOMIC_UNDERFLOW", |
| 221 | "ATOMIC_OVERFLOW", |
Kees Cook | aa981a6 | 2016-06-03 12:06:52 -0700 | [diff] [blame] | 222 | "USERCOPY_HEAP_SIZE_TO", |
| 223 | "USERCOPY_HEAP_SIZE_FROM", |
| 224 | "USERCOPY_HEAP_FLAG_TO", |
| 225 | "USERCOPY_HEAP_FLAG_FROM", |
| 226 | "USERCOPY_STACK_FRAME_TO", |
| 227 | "USERCOPY_STACK_FRAME_FROM", |
| 228 | "USERCOPY_STACK_BEYOND", |
Kees Cook | 6c35214 | 2016-06-23 22:01:26 -0700 | [diff] [blame] | 229 | "USERCOPY_KERNEL", |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
Kees Cook | d87c978 | 2016-06-29 08:07:11 -0700 | [diff] [blame] | 232 | /* Global jprobe entry and crashtype. */ |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 233 | static struct jprobe lkdtm_jprobe; |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 234 | static enum cname lkdtm_crashpoint = CN_INVALID; |
| 235 | static enum ctype lkdtm_crashtype = CT_NONE; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 236 | |
Kees Cook | d87c978 | 2016-06-29 08:07:11 -0700 | [diff] [blame] | 237 | /* Global crash counter and spinlock. */ |
| 238 | static int crash_count = DEFAULT_COUNT; |
| 239 | static DEFINE_SPINLOCK(crash_count_lock); |
| 240 | |
| 241 | /* Module parameters */ |
| 242 | static int recur_count = -1; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 243 | module_param(recur_count, int, 0644); |
Kees Cook | 7d196ac | 2013-10-24 09:25:39 -0700 | [diff] [blame] | 244 | MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test"); |
Kees Cook | d87c978 | 2016-06-29 08:07:11 -0700 | [diff] [blame] | 245 | |
| 246 | static char* cpoint_name; |
Rusty Russell | dca4130 | 2010-08-11 23:04:21 -0600 | [diff] [blame] | 247 | module_param(cpoint_name, charp, 0444); |
Randy Dunlap | 5d861d9 | 2006-11-02 22:07:06 -0800 | [diff] [blame] | 248 | MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed"); |
Kees Cook | d87c978 | 2016-06-29 08:07:11 -0700 | [diff] [blame] | 249 | |
| 250 | static char* cpoint_type; |
Rusty Russell | dca4130 | 2010-08-11 23:04:21 -0600 | [diff] [blame] | 251 | module_param(cpoint_type, charp, 0444); |
Randy Dunlap | 5d861d9 | 2006-11-02 22:07:06 -0800 | [diff] [blame] | 252 | MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\ |
| 253 | "hitting the crash point"); |
Kees Cook | d87c978 | 2016-06-29 08:07:11 -0700 | [diff] [blame] | 254 | |
| 255 | static int cpoint_count = DEFAULT_COUNT; |
Randy Dunlap | 5d861d9 | 2006-11-02 22:07:06 -0800 | [diff] [blame] | 256 | module_param(cpoint_count, int, 0644); |
| 257 | MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\ |
| 258 | "crash point is to be hit to trigger action"); |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 259 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 260 | /* Return the crashpoint number or NONE if the name is invalid */ |
| 261 | static enum ctype parse_cp_type(const char *what, size_t count) |
| 262 | { |
| 263 | int i; |
| 264 | |
| 265 | for (i = 0; i < ARRAY_SIZE(cp_type); i++) { |
| 266 | if (!strcmp(what, cp_type[i])) |
Kees Cook | 329d416 | 2016-06-26 22:26:11 -0700 | [diff] [blame] | 267 | return i; |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 270 | return CT_NONE; |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static const char *cp_type_to_str(enum ctype type) |
| 274 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 275 | if (type == CT_NONE || type < 0 || type > ARRAY_SIZE(cp_type)) |
Kees Cook | 329d416 | 2016-06-26 22:26:11 -0700 | [diff] [blame] | 276 | return "NONE"; |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 277 | |
Kees Cook | 329d416 | 2016-06-26 22:26:11 -0700 | [diff] [blame] | 278 | return cp_type[type]; |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static const char *cp_name_to_str(enum cname name) |
| 282 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 283 | if (name == CN_INVALID || name < 0 || name > ARRAY_SIZE(cp_name)) |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 284 | return "INVALID"; |
| 285 | |
Kees Cook | 329d416 | 2016-06-26 22:26:11 -0700 | [diff] [blame] | 286 | return cp_name[name]; |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 290 | static int lkdtm_parse_commandline(void) |
| 291 | { |
| 292 | int i; |
Josh Hunt | aa2c96d | 2011-06-27 16:18:08 -0700 | [diff] [blame] | 293 | unsigned long flags; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 294 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 295 | if (cpoint_count < 1 || recur_count < 1) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 296 | return -EINVAL; |
| 297 | |
Kees Cook | 76a10e2 | 2016-06-26 22:40:13 -0700 | [diff] [blame] | 298 | spin_lock_irqsave(&crash_count_lock, flags); |
| 299 | crash_count = cpoint_count; |
| 300 | spin_unlock_irqrestore(&crash_count_lock, flags); |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 301 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 302 | /* No special parameters */ |
| 303 | if (!cpoint_type && !cpoint_name) |
| 304 | return 0; |
| 305 | |
| 306 | /* Neither or both of these need to be set */ |
| 307 | if (!cpoint_type || !cpoint_name) |
| 308 | return -EINVAL; |
| 309 | |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 310 | lkdtm_crashtype = parse_cp_type(cpoint_type, strlen(cpoint_type)); |
| 311 | if (lkdtm_crashtype == CT_NONE) |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 312 | return -EINVAL; |
| 313 | |
Kees Cook | 329d416 | 2016-06-26 22:26:11 -0700 | [diff] [blame] | 314 | /* Refuse INVALID as a selectable crashpoint name. */ |
| 315 | if (!strcmp(cpoint_name, "INVALID")) |
| 316 | return -EINVAL; |
| 317 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 318 | for (i = 0; i < ARRAY_SIZE(cp_name); i++) { |
| 319 | if (!strcmp(cpoint_name, cp_name[i])) { |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 320 | lkdtm_crashpoint = i; |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /* Could not find a valid crash point */ |
| 326 | return -EINVAL; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 329 | static void lkdtm_do_action(enum ctype which) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 330 | { |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 331 | switch (which) { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 332 | case CT_PANIC: |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 333 | lkdtm_PANIC(); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 334 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 335 | case CT_BUG: |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 336 | lkdtm_BUG(); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 337 | break; |
Kees Cook | 6589272 | 2013-07-08 10:01:31 -0700 | [diff] [blame] | 338 | case CT_WARNING: |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 339 | lkdtm_WARNING(); |
Kees Cook | 6589272 | 2013-07-08 10:01:31 -0700 | [diff] [blame] | 340 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 341 | case CT_EXCEPTION: |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 342 | lkdtm_EXCEPTION(); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 343 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 344 | case CT_LOOP: |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 345 | lkdtm_LOOP(); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 346 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 347 | case CT_OVERFLOW: |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 348 | lkdtm_OVERFLOW(); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 349 | break; |
Kees Cook | 629c66a | 2013-10-24 18:05:42 -0700 | [diff] [blame] | 350 | case CT_CORRUPT_STACK: |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 351 | lkdtm_CORRUPT_STACK(); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 352 | break; |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 353 | case CT_UNALIGNED_LOAD_STORE_WRITE: |
| 354 | lkdtm_UNALIGNED_LOAD_STORE_WRITE(); |
| 355 | break; |
Kees Cook | ffc514f | 2016-06-26 21:45:23 -0700 | [diff] [blame] | 356 | case CT_OVERWRITE_ALLOCATION: |
| 357 | lkdtm_OVERWRITE_ALLOCATION(); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 358 | break; |
Kees Cook | ffc514f | 2016-06-26 21:45:23 -0700 | [diff] [blame] | 359 | case CT_WRITE_AFTER_FREE: |
| 360 | lkdtm_WRITE_AFTER_FREE(); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 361 | break; |
Kees Cook | ffc514f | 2016-06-26 21:45:23 -0700 | [diff] [blame] | 362 | case CT_READ_AFTER_FREE: |
| 363 | lkdtm_READ_AFTER_FREE(); |
Laura Abbott | bc0b8cc | 2016-02-25 16:36:42 -0800 | [diff] [blame] | 364 | break; |
Kees Cook | ffc514f | 2016-06-26 21:45:23 -0700 | [diff] [blame] | 365 | case CT_WRITE_BUDDY_AFTER_FREE: |
| 366 | lkdtm_WRITE_BUDDY_AFTER_FREE(); |
Laura Abbott | 920d451 | 2016-02-25 16:36:44 -0800 | [diff] [blame] | 367 | break; |
Kees Cook | ffc514f | 2016-06-26 21:45:23 -0700 | [diff] [blame] | 368 | case CT_READ_BUDDY_AFTER_FREE: |
| 369 | lkdtm_READ_BUDDY_AFTER_FREE(); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 370 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 371 | case CT_SOFTLOCKUP: |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 372 | lkdtm_SOFTLOCKUP(); |
Frederic Weisbecker | a48223f | 2010-05-26 14:44:29 -0700 | [diff] [blame] | 373 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 374 | case CT_HARDLOCKUP: |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 375 | lkdtm_HARDLOCKUP(); |
Frederic Weisbecker | a48223f | 2010-05-26 14:44:29 -0700 | [diff] [blame] | 376 | break; |
Kees Cook | 274a585 | 2013-07-08 10:01:32 -0700 | [diff] [blame] | 377 | case CT_SPINLOCKUP: |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 378 | lkdtm_SPINLOCKUP(); |
Kees Cook | 274a585 | 2013-07-08 10:01:32 -0700 | [diff] [blame] | 379 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 380 | case CT_HUNG_TASK: |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 381 | lkdtm_HUNG_TASK(); |
Frederic Weisbecker | a48223f | 2010-05-26 14:44:29 -0700 | [diff] [blame] | 382 | break; |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 383 | case CT_EXEC_DATA: |
Kees Cook | 0d9eb29 | 2016-06-26 15:12:31 -0700 | [diff] [blame] | 384 | lkdtm_EXEC_DATA(); |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 385 | break; |
Kees Cook | 0d9eb29 | 2016-06-26 15:12:31 -0700 | [diff] [blame] | 386 | case CT_EXEC_STACK: |
| 387 | lkdtm_EXEC_STACK(); |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 388 | break; |
Kees Cook | 0d9eb29 | 2016-06-26 15:12:31 -0700 | [diff] [blame] | 389 | case CT_EXEC_KMALLOC: |
| 390 | lkdtm_EXEC_KMALLOC(); |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 391 | break; |
Kees Cook | 0d9eb29 | 2016-06-26 15:12:31 -0700 | [diff] [blame] | 392 | case CT_EXEC_VMALLOC: |
| 393 | lkdtm_EXEC_VMALLOC(); |
Kees Cook | cc33c537 | 2013-07-08 10:01:33 -0700 | [diff] [blame] | 394 | break; |
Kees Cook | 9a49a52 | 2016-02-22 14:09:29 -0800 | [diff] [blame] | 395 | case CT_EXEC_RODATA: |
Kees Cook | 0d9eb29 | 2016-06-26 15:12:31 -0700 | [diff] [blame] | 396 | lkdtm_EXEC_RODATA(); |
Kees Cook | 9a49a52 | 2016-02-22 14:09:29 -0800 | [diff] [blame] | 397 | break; |
Kees Cook | 0d9eb29 | 2016-06-26 15:12:31 -0700 | [diff] [blame] | 398 | case CT_EXEC_USERSPACE: |
| 399 | lkdtm_EXEC_USERSPACE(); |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 400 | break; |
Kees Cook | 0d9eb29 | 2016-06-26 15:12:31 -0700 | [diff] [blame] | 401 | case CT_ACCESS_USERSPACE: |
| 402 | lkdtm_ACCESS_USERSPACE(); |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 403 | break; |
Kees Cook | 0d9eb29 | 2016-06-26 15:12:31 -0700 | [diff] [blame] | 404 | case CT_WRITE_RO: |
| 405 | lkdtm_WRITE_RO(); |
Kees Cook | 7cca071 | 2016-02-17 14:41:16 -0800 | [diff] [blame] | 406 | break; |
Kees Cook | 0d9eb29 | 2016-06-26 15:12:31 -0700 | [diff] [blame] | 407 | case CT_WRITE_RO_AFTER_INIT: |
| 408 | lkdtm_WRITE_RO_AFTER_INIT(); |
Kees Cook | 9ae113c | 2013-10-24 09:25:57 -0700 | [diff] [blame] | 409 | break; |
Kees Cook | 0d9eb29 | 2016-06-26 15:12:31 -0700 | [diff] [blame] | 410 | case CT_WRITE_KERN: |
| 411 | lkdtm_WRITE_KERN(); |
Kees Cook | dc2b9e9 | 2014-02-09 13:48:48 -0800 | [diff] [blame] | 412 | break; |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 413 | case CT_ATOMIC_UNDERFLOW: |
| 414 | lkdtm_ATOMIC_UNDERFLOW(); |
Kees Cook | b548452 | 2016-06-07 14:27:02 -0700 | [diff] [blame] | 415 | break; |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 416 | case CT_ATOMIC_OVERFLOW: |
| 417 | lkdtm_ATOMIC_OVERFLOW(); |
| 418 | break; |
Kees Cook | aa981a6 | 2016-06-03 12:06:52 -0700 | [diff] [blame] | 419 | case CT_USERCOPY_HEAP_SIZE_TO: |
Kees Cook | a3dff71 | 2016-06-26 08:46:23 -0700 | [diff] [blame] | 420 | lkdtm_USERCOPY_HEAP_SIZE_TO(); |
Kees Cook | aa981a6 | 2016-06-03 12:06:52 -0700 | [diff] [blame] | 421 | break; |
| 422 | case CT_USERCOPY_HEAP_SIZE_FROM: |
Kees Cook | a3dff71 | 2016-06-26 08:46:23 -0700 | [diff] [blame] | 423 | lkdtm_USERCOPY_HEAP_SIZE_FROM(); |
Kees Cook | aa981a6 | 2016-06-03 12:06:52 -0700 | [diff] [blame] | 424 | break; |
| 425 | case CT_USERCOPY_HEAP_FLAG_TO: |
Kees Cook | a3dff71 | 2016-06-26 08:46:23 -0700 | [diff] [blame] | 426 | lkdtm_USERCOPY_HEAP_FLAG_TO(); |
Kees Cook | aa981a6 | 2016-06-03 12:06:52 -0700 | [diff] [blame] | 427 | break; |
| 428 | case CT_USERCOPY_HEAP_FLAG_FROM: |
Kees Cook | a3dff71 | 2016-06-26 08:46:23 -0700 | [diff] [blame] | 429 | lkdtm_USERCOPY_HEAP_FLAG_FROM(); |
Kees Cook | aa981a6 | 2016-06-03 12:06:52 -0700 | [diff] [blame] | 430 | break; |
| 431 | case CT_USERCOPY_STACK_FRAME_TO: |
Kees Cook | a3dff71 | 2016-06-26 08:46:23 -0700 | [diff] [blame] | 432 | lkdtm_USERCOPY_STACK_FRAME_TO(); |
Kees Cook | aa981a6 | 2016-06-03 12:06:52 -0700 | [diff] [blame] | 433 | break; |
| 434 | case CT_USERCOPY_STACK_FRAME_FROM: |
Kees Cook | a3dff71 | 2016-06-26 08:46:23 -0700 | [diff] [blame] | 435 | lkdtm_USERCOPY_STACK_FRAME_FROM(); |
Kees Cook | aa981a6 | 2016-06-03 12:06:52 -0700 | [diff] [blame] | 436 | break; |
| 437 | case CT_USERCOPY_STACK_BEYOND: |
Kees Cook | a3dff71 | 2016-06-26 08:46:23 -0700 | [diff] [blame] | 438 | lkdtm_USERCOPY_STACK_BEYOND(); |
Kees Cook | aa981a6 | 2016-06-03 12:06:52 -0700 | [diff] [blame] | 439 | break; |
Kees Cook | 6c35214 | 2016-06-23 22:01:26 -0700 | [diff] [blame] | 440 | case CT_USERCOPY_KERNEL: |
Kees Cook | a3dff71 | 2016-06-26 08:46:23 -0700 | [diff] [blame] | 441 | lkdtm_USERCOPY_KERNEL(); |
Kees Cook | 6c35214 | 2016-06-23 22:01:26 -0700 | [diff] [blame] | 442 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 443 | case CT_NONE: |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 444 | default: |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | } |
| 449 | |
| 450 | static void lkdtm_handler(void) |
| 451 | { |
Josh Hunt | aa2c96d | 2011-06-27 16:18:08 -0700 | [diff] [blame] | 452 | unsigned long flags; |
Cong Wang | 9261818 | 2012-02-03 15:37:15 -0800 | [diff] [blame] | 453 | bool do_it = false; |
Josh Hunt | aa2c96d | 2011-06-27 16:18:08 -0700 | [diff] [blame] | 454 | |
Kees Cook | 76a10e2 | 2016-06-26 22:40:13 -0700 | [diff] [blame] | 455 | spin_lock_irqsave(&crash_count_lock, flags); |
| 456 | crash_count--; |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 457 | pr_info("Crash point %s of type %s hit, trigger in %d rounds\n", |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 458 | cp_name_to_str(lkdtm_crashpoint), |
| 459 | cp_type_to_str(lkdtm_crashtype), crash_count); |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 460 | |
Kees Cook | 76a10e2 | 2016-06-26 22:40:13 -0700 | [diff] [blame] | 461 | if (crash_count == 0) { |
Cong Wang | 9261818 | 2012-02-03 15:37:15 -0800 | [diff] [blame] | 462 | do_it = true; |
Kees Cook | 76a10e2 | 2016-06-26 22:40:13 -0700 | [diff] [blame] | 463 | crash_count = cpoint_count; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 464 | } |
Kees Cook | 76a10e2 | 2016-06-26 22:40:13 -0700 | [diff] [blame] | 465 | spin_unlock_irqrestore(&crash_count_lock, flags); |
Cong Wang | 9261818 | 2012-02-03 15:37:15 -0800 | [diff] [blame] | 466 | |
| 467 | if (do_it) |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 468 | lkdtm_do_action(lkdtm_crashtype); |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 471 | static int lkdtm_register_cpoint(enum cname which) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 472 | { |
| 473 | int ret; |
| 474 | |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 475 | lkdtm_crashpoint = CN_INVALID; |
| 476 | if (lkdtm_jprobe.entry != NULL) |
| 477 | unregister_jprobe(&lkdtm_jprobe); |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 478 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 479 | switch (which) { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 480 | case CN_DIRECT: |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 481 | lkdtm_do_action(lkdtm_crashtype); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 482 | return 0; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 483 | case CN_INT_HARDWARE_ENTRY: |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 484 | lkdtm_jprobe.kp.symbol_name = "do_IRQ"; |
| 485 | lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_do_irq; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 486 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 487 | case CN_INT_HW_IRQ_EN: |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 488 | lkdtm_jprobe.kp.symbol_name = "handle_IRQ_event"; |
| 489 | lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_handle_irq_event; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 490 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 491 | case CN_INT_TASKLET_ENTRY: |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 492 | lkdtm_jprobe.kp.symbol_name = "tasklet_action"; |
| 493 | lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_tasklet_action; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 494 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 495 | case CN_FS_DEVRW: |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 496 | lkdtm_jprobe.kp.symbol_name = "ll_rw_block"; |
| 497 | lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_ll_rw_block; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 498 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 499 | case CN_MEM_SWAPOUT: |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 500 | lkdtm_jprobe.kp.symbol_name = "shrink_inactive_list"; |
| 501 | lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_shrink_inactive_list; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 502 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 503 | case CN_TIMERADD: |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 504 | lkdtm_jprobe.kp.symbol_name = "hrtimer_start"; |
| 505 | lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_hrtimer_start; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 506 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 507 | case CN_SCSI_DISPATCH_CMD: |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 508 | lkdtm_jprobe.kp.symbol_name = "scsi_dispatch_cmd"; |
| 509 | lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_scsi_dispatch_cmd; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 510 | break; |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 511 | case CN_IDE_CORE_CP: |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 512 | #ifdef CONFIG_IDE |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 513 | lkdtm_jprobe.kp.symbol_name = "generic_ide_ioctl"; |
| 514 | lkdtm_jprobe.entry = (kprobe_opcode_t*) jp_generic_ide_ioctl; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 515 | #else |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 516 | pr_info("Crash point not available\n"); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 517 | return -EINVAL; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 518 | #endif |
| 519 | break; |
| 520 | default: |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 521 | pr_info("Invalid Crash Point\n"); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 522 | return -EINVAL; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 525 | lkdtm_crashpoint = which; |
| 526 | if ((ret = register_jprobe(&lkdtm_jprobe)) < 0) { |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 527 | pr_info("Couldn't register jprobe\n"); |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 528 | lkdtm_crashpoint = CN_INVALID; |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 531 | return ret; |
| 532 | } |
| 533 | |
| 534 | static ssize_t do_register_entry(enum cname which, struct file *f, |
| 535 | const char __user *user_buf, size_t count, loff_t *off) |
| 536 | { |
| 537 | char *buf; |
| 538 | int err; |
| 539 | |
| 540 | if (count >= PAGE_SIZE) |
| 541 | return -EINVAL; |
| 542 | |
| 543 | buf = (char *)__get_free_page(GFP_KERNEL); |
| 544 | if (!buf) |
| 545 | return -ENOMEM; |
| 546 | if (copy_from_user(buf, user_buf, count)) { |
| 547 | free_page((unsigned long) buf); |
| 548 | return -EFAULT; |
| 549 | } |
| 550 | /* NULL-terminate and remove enter */ |
| 551 | buf[count] = '\0'; |
| 552 | strim(buf); |
| 553 | |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 554 | lkdtm_crashtype = parse_cp_type(buf, count); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 555 | free_page((unsigned long) buf); |
| 556 | |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 557 | if (lkdtm_crashtype == CT_NONE) |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 558 | return -EINVAL; |
| 559 | |
| 560 | err = lkdtm_register_cpoint(which); |
| 561 | if (err < 0) |
| 562 | return err; |
| 563 | |
| 564 | *off += count; |
| 565 | |
| 566 | return count; |
| 567 | } |
| 568 | |
| 569 | /* Generic read callback that just prints out the available crash types */ |
| 570 | static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf, |
| 571 | size_t count, loff_t *off) |
| 572 | { |
| 573 | char *buf; |
| 574 | int i, n, out; |
| 575 | |
| 576 | buf = (char *)__get_free_page(GFP_KERNEL); |
Alan Cox | 086ff4b | 2012-07-30 14:43:24 -0700 | [diff] [blame] | 577 | if (buf == NULL) |
| 578 | return -ENOMEM; |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 579 | |
| 580 | n = snprintf(buf, PAGE_SIZE, "Available crash types:\n"); |
| 581 | for (i = 0; i < ARRAY_SIZE(cp_type); i++) |
| 582 | n += snprintf(buf + n, PAGE_SIZE - n, "%s\n", cp_type[i]); |
| 583 | buf[n] = '\0'; |
| 584 | |
| 585 | out = simple_read_from_buffer(user_buf, count, off, |
| 586 | buf, n); |
| 587 | free_page((unsigned long) buf); |
| 588 | |
| 589 | return out; |
| 590 | } |
| 591 | |
| 592 | static int lkdtm_debugfs_open(struct inode *inode, struct file *file) |
| 593 | { |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 594 | return 0; |
| 595 | } |
| 596 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 597 | |
| 598 | static ssize_t int_hardware_entry(struct file *f, const char __user *buf, |
| 599 | size_t count, loff_t *off) |
| 600 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 601 | return do_register_entry(CN_INT_HARDWARE_ENTRY, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | static ssize_t int_hw_irq_en(struct file *f, const char __user *buf, |
| 605 | size_t count, loff_t *off) |
| 606 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 607 | 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] | 608 | } |
| 609 | |
| 610 | static ssize_t int_tasklet_entry(struct file *f, const char __user *buf, |
| 611 | size_t count, loff_t *off) |
| 612 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 613 | return do_register_entry(CN_INT_TASKLET_ENTRY, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | static ssize_t fs_devrw_entry(struct file *f, const char __user *buf, |
| 617 | size_t count, loff_t *off) |
| 618 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 619 | return do_register_entry(CN_FS_DEVRW, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static ssize_t mem_swapout_entry(struct file *f, const char __user *buf, |
| 623 | size_t count, loff_t *off) |
| 624 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 625 | return do_register_entry(CN_MEM_SWAPOUT, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | static ssize_t timeradd_entry(struct file *f, const char __user *buf, |
| 629 | size_t count, loff_t *off) |
| 630 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 631 | return do_register_entry(CN_TIMERADD, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static ssize_t scsi_dispatch_cmd_entry(struct file *f, |
| 635 | const char __user *buf, size_t count, loff_t *off) |
| 636 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 637 | return do_register_entry(CN_SCSI_DISPATCH_CMD, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static ssize_t ide_core_cp_entry(struct file *f, const char __user *buf, |
| 641 | size_t count, loff_t *off) |
| 642 | { |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 643 | return do_register_entry(CN_IDE_CORE_CP, f, buf, count, off); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /* Special entry to just crash directly. Available without KPROBEs */ |
| 647 | static ssize_t direct_entry(struct file *f, const char __user *user_buf, |
| 648 | size_t count, loff_t *off) |
| 649 | { |
| 650 | enum ctype type; |
| 651 | char *buf; |
| 652 | |
| 653 | if (count >= PAGE_SIZE) |
| 654 | return -EINVAL; |
| 655 | if (count < 1) |
| 656 | return -EINVAL; |
| 657 | |
| 658 | buf = (char *)__get_free_page(GFP_KERNEL); |
| 659 | if (!buf) |
| 660 | return -ENOMEM; |
| 661 | if (copy_from_user(buf, user_buf, count)) { |
| 662 | free_page((unsigned long) buf); |
| 663 | return -EFAULT; |
| 664 | } |
| 665 | /* NULL-terminate and remove enter */ |
| 666 | buf[count] = '\0'; |
| 667 | strim(buf); |
| 668 | |
| 669 | type = parse_cp_type(buf, count); |
| 670 | free_page((unsigned long) buf); |
Namhyung Kim | 93e2f58 | 2010-10-26 14:22:40 -0700 | [diff] [blame] | 671 | if (type == CT_NONE) |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 672 | return -EINVAL; |
| 673 | |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 674 | pr_info("Performing direct entry %s\n", cp_type_to_str(type)); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 675 | lkdtm_do_action(type); |
| 676 | *off += count; |
| 677 | |
| 678 | return count; |
| 679 | } |
| 680 | |
| 681 | struct crash_entry { |
| 682 | const char *name; |
| 683 | const struct file_operations fops; |
| 684 | }; |
| 685 | |
| 686 | static const struct crash_entry crash_entries[] = { |
| 687 | {"DIRECT", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 688 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 689 | .open = lkdtm_debugfs_open, |
| 690 | .write = direct_entry} }, |
| 691 | {"INT_HARDWARE_ENTRY", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 692 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 693 | .open = lkdtm_debugfs_open, |
| 694 | .write = int_hardware_entry} }, |
| 695 | {"INT_HW_IRQ_EN", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 696 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 697 | .open = lkdtm_debugfs_open, |
| 698 | .write = int_hw_irq_en} }, |
| 699 | {"INT_TASKLET_ENTRY", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 700 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 701 | .open = lkdtm_debugfs_open, |
| 702 | .write = int_tasklet_entry} }, |
| 703 | {"FS_DEVRW", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 704 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 705 | .open = lkdtm_debugfs_open, |
| 706 | .write = fs_devrw_entry} }, |
| 707 | {"MEM_SWAPOUT", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 708 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 709 | .open = lkdtm_debugfs_open, |
| 710 | .write = mem_swapout_entry} }, |
| 711 | {"TIMERADD", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 712 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 713 | .open = lkdtm_debugfs_open, |
| 714 | .write = timeradd_entry} }, |
| 715 | {"SCSI_DISPATCH_CMD", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 716 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 717 | .open = lkdtm_debugfs_open, |
| 718 | .write = scsi_dispatch_cmd_entry} }, |
| 719 | {"IDE_CORE_CP", {.read = lkdtm_debugfs_read, |
Arnd Bergmann | 05271ec | 2010-07-06 19:10:26 +0200 | [diff] [blame] | 720 | .llseek = generic_file_llseek, |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 721 | .open = lkdtm_debugfs_open, |
| 722 | .write = ide_core_cp_entry} }, |
| 723 | }; |
| 724 | |
| 725 | static struct dentry *lkdtm_debugfs_root; |
| 726 | |
| 727 | static int __init lkdtm_module_init(void) |
| 728 | { |
| 729 | int ret = -EINVAL; |
| 730 | int n_debugfs_entries = 1; /* Assume only the direct entry */ |
| 731 | int i; |
| 732 | |
Kees Cook | a3dff71 | 2016-06-26 08:46:23 -0700 | [diff] [blame] | 733 | /* Handle test-specific initialization. */ |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 734 | lkdtm_bugs_init(&recur_count); |
Kees Cook | 0d9eb29 | 2016-06-26 15:12:31 -0700 | [diff] [blame] | 735 | lkdtm_perms_init(); |
Kees Cook | a3dff71 | 2016-06-26 08:46:23 -0700 | [diff] [blame] | 736 | lkdtm_usercopy_init(); |
| 737 | |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 738 | /* Register debugfs interface */ |
| 739 | lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL); |
| 740 | if (!lkdtm_debugfs_root) { |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 741 | pr_err("creating root dir failed\n"); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 742 | return -ENODEV; |
| 743 | } |
| 744 | |
| 745 | #ifdef CONFIG_KPROBES |
| 746 | n_debugfs_entries = ARRAY_SIZE(crash_entries); |
| 747 | #endif |
| 748 | |
| 749 | for (i = 0; i < n_debugfs_entries; i++) { |
| 750 | const struct crash_entry *cur = &crash_entries[i]; |
| 751 | struct dentry *de; |
| 752 | |
| 753 | de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root, |
| 754 | NULL, &cur->fops); |
| 755 | if (de == NULL) { |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 756 | pr_err("could not create %s\n", cur->name); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 757 | goto out_err; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | if (lkdtm_parse_commandline() == -EINVAL) { |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 762 | pr_info("Invalid command\n"); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 763 | goto out_err; |
| 764 | } |
| 765 | |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 766 | if (lkdtm_crashpoint != CN_INVALID && lkdtm_crashtype != CT_NONE) { |
| 767 | ret = lkdtm_register_cpoint(lkdtm_crashpoint); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 768 | if (ret < 0) { |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 769 | pr_info("Invalid crash point %d\n", lkdtm_crashpoint); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 770 | goto out_err; |
| 771 | } |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 772 | pr_info("Crash point %s of type %s registered\n", |
| 773 | cpoint_name, cpoint_type); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 774 | } else { |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 775 | pr_info("No crash points registered, enable through debugfs\n"); |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | return 0; |
| 779 | |
| 780 | out_err: |
| 781 | debugfs_remove_recursive(lkdtm_debugfs_root); |
| 782 | return ret; |
| 783 | } |
| 784 | |
Adrian Bunk | 2118116 | 2008-02-06 01:36:50 -0800 | [diff] [blame] | 785 | static void __exit lkdtm_module_exit(void) |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 786 | { |
Simon Kagstrom | 0347af4 | 2010-03-05 13:42:49 -0800 | [diff] [blame] | 787 | debugfs_remove_recursive(lkdtm_debugfs_root); |
| 788 | |
Kees Cook | a3dff71 | 2016-06-26 08:46:23 -0700 | [diff] [blame] | 789 | /* Handle test-specific clean-up. */ |
| 790 | lkdtm_usercopy_exit(); |
Kees Cook | aa981a6 | 2016-06-03 12:06:52 -0700 | [diff] [blame] | 791 | |
Kees Cook | 38f95fe | 2016-06-29 08:02:32 -0700 | [diff] [blame] | 792 | unregister_jprobe(&lkdtm_jprobe); |
Kees Cook | feac6e2 | 2014-02-09 13:48:46 -0800 | [diff] [blame] | 793 | pr_info("Crash point unregistered\n"); |
Ankita Garg | 8bb31b9 | 2006-10-02 02:17:36 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | module_init(lkdtm_module_init); |
| 797 | module_exit(lkdtm_module_exit); |
| 798 | |
| 799 | MODULE_LICENSE("GPL"); |
Terry Chia | da86920 | 2014-07-02 21:02:25 +0800 | [diff] [blame] | 800 | MODULE_DESCRIPTION("Kprobe module for testing crash dumps"); |