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