lkdtm_core.c 13.1 KB
Newer Older
1
/*
K
Kees Cook 已提交
2 3 4 5 6
 * Linux Kernel Dump Test Module for testing kernel crashes conditions:
 * induces system failures at predefined crashpoints and under predefined
 * operational conditions in order to evaluate the reliability of kernel
 * sanity checking and crash dumps obtained using different dumping
 * solutions.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Copyright (C) IBM Corporation, 2006
 *
 * Author: Ankita Garg <ankita@in.ibm.com>
 *
 * It is adapted from the Linux Kernel Dump Test Tool by
 * Fernando Luis Vazquez Cao <http://lkdtt.sourceforge.net>
 *
29
 * Debugfs support added by Simon Kagstrom <simon.kagstrom@netinsight.net>
30
 *
31
 * See Documentation/fault-injection/provoke-crashes.txt for instructions
32
 */
33
#include "lkdtm.h"
34
#include <linux/fs.h>
35
#include <linux/module.h>
36
#include <linux/buffer_head.h>
37
#include <linux/kprobes.h>
38
#include <linux/list.h>
39 40
#include <linux/init.h>
#include <linux/interrupt.h>
41
#include <linux/hrtimer.h>
42
#include <linux/slab.h>
43
#include <scsi/scsi_cmnd.h>
44
#include <linux/debugfs.h>
45 46 47 48 49

#ifdef CONFIG_IDE
#include <linux/ide.h>
#endif

K
Kees Cook 已提交
50 51
#define DEFAULT_COUNT 10

52 53 54 55 56
static int lkdtm_debugfs_open(struct inode *inode, struct file *file);
static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
		size_t count, loff_t *off);
static ssize_t direct_entry(struct file *f, const char __user *user_buf,
			    size_t count, loff_t *off);
A
Arnd Bergmann 已提交
57 58

#ifdef CONFIG_KPROBES
K
Kees Cook 已提交
59
static int lkdtm_kprobe_handler(struct kprobe *kp, struct pt_regs *regs);
60 61 62
static ssize_t lkdtm_debugfs_entry(struct file *f,
				   const char __user *user_buf,
				   size_t count, loff_t *off);
K
Kees Cook 已提交
63 64 65 66 67 68 69 70 71 72
# define CRASHPOINT_KPROBE(_symbol)				\
		.kprobe = {					\
			.symbol_name = (_symbol),		\
			.pre_handler = lkdtm_kprobe_handler,	\
		},
# define CRASHPOINT_WRITE(_symbol)				\
		(_symbol) ? lkdtm_debugfs_entry : direct_entry
#else
# define CRASHPOINT_KPROBE(_symbol)
# define CRASHPOINT_WRITE(_symbol)		direct_entry
73 74
#endif

75 76 77 78
/* Crash points */
struct crashpoint {
	const char *name;
	const struct file_operations fops;
K
Kees Cook 已提交
79
	struct kprobe kprobe;
80 81
};

K
Kees Cook 已提交
82
#define CRASHPOINT(_name, _symbol)				\
83 84 85 86 87 88
	{							\
		.name = _name,					\
		.fops = {					\
			.read	= lkdtm_debugfs_read,		\
			.llseek	= generic_file_llseek,		\
			.open	= lkdtm_debugfs_open,		\
K
Kees Cook 已提交
89
			.write	= CRASHPOINT_WRITE(_symbol)	\
90
		},						\
K
Kees Cook 已提交
91
		CRASHPOINT_KPROBE(_symbol)			\
92 93 94
	}

/* Define the possible places where we can trigger a crash point. */
K
Kees Cook 已提交
95 96
static struct crashpoint crashpoints[] = {
	CRASHPOINT("DIRECT",		 NULL),
97
#ifdef CONFIG_KPROBES
K
Kees Cook 已提交
98 99 100 101 102 103 104
	CRASHPOINT("INT_HARDWARE_ENTRY", "do_IRQ"),
	CRASHPOINT("INT_HW_IRQ_EN",	 "handle_IRQ_event"),
	CRASHPOINT("INT_TASKLET_ENTRY",	 "tasklet_action"),
	CRASHPOINT("FS_DEVRW",		 "ll_rw_block"),
	CRASHPOINT("MEM_SWAPOUT",	 "shrink_inactive_list"),
	CRASHPOINT("TIMERADD",		 "hrtimer_start"),
	CRASHPOINT("SCSI_DISPATCH_CMD",	 "scsi_dispatch_cmd"),
105
# ifdef CONFIG_IDE
K
Kees Cook 已提交
106
	CRASHPOINT("IDE_CORE_CP",	 "generic_ide_ioctl"),
107 108
# endif
#endif
109 110
};

111 112 113 114 115

/* Crash types. */
struct crashtype {
	const char *name;
	void (*func)(void);
116 117
};

118 119 120 121 122 123 124
#define CRASHTYPE(_name)			\
	{					\
		.name = __stringify(_name),	\
		.func = lkdtm_ ## _name,	\
	}

/* Define the possible types of crashes that can be triggered. */
K
Kees Cook 已提交
125
static const struct crashtype crashtypes[] = {
126 127 128 129 130 131
	CRASHTYPE(PANIC),
	CRASHTYPE(BUG),
	CRASHTYPE(WARNING),
	CRASHTYPE(EXCEPTION),
	CRASHTYPE(LOOP),
	CRASHTYPE(OVERFLOW),
132 133
	CRASHTYPE(CORRUPT_LIST_ADD),
	CRASHTYPE(CORRUPT_LIST_DEL),
K
Kees Cook 已提交
134
	CRASHTYPE(CORRUPT_USER_DS),
135
	CRASHTYPE(CORRUPT_STACK),
136
	CRASHTYPE(CORRUPT_STACK_STRONG),
137 138
	CRASHTYPE(STACK_GUARD_PAGE_LEADING),
	CRASHTYPE(STACK_GUARD_PAGE_TRAILING),
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	CRASHTYPE(UNALIGNED_LOAD_STORE_WRITE),
	CRASHTYPE(OVERWRITE_ALLOCATION),
	CRASHTYPE(WRITE_AFTER_FREE),
	CRASHTYPE(READ_AFTER_FREE),
	CRASHTYPE(WRITE_BUDDY_AFTER_FREE),
	CRASHTYPE(READ_BUDDY_AFTER_FREE),
	CRASHTYPE(SOFTLOCKUP),
	CRASHTYPE(HARDLOCKUP),
	CRASHTYPE(SPINLOCKUP),
	CRASHTYPE(HUNG_TASK),
	CRASHTYPE(EXEC_DATA),
	CRASHTYPE(EXEC_STACK),
	CRASHTYPE(EXEC_KMALLOC),
	CRASHTYPE(EXEC_VMALLOC),
	CRASHTYPE(EXEC_RODATA),
	CRASHTYPE(EXEC_USERSPACE),
	CRASHTYPE(ACCESS_USERSPACE),
	CRASHTYPE(WRITE_RO),
	CRASHTYPE(WRITE_RO_AFTER_INIT),
	CRASHTYPE(WRITE_KERN),
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	CRASHTYPE(REFCOUNT_INC_OVERFLOW),
	CRASHTYPE(REFCOUNT_ADD_OVERFLOW),
	CRASHTYPE(REFCOUNT_INC_NOT_ZERO_OVERFLOW),
	CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_OVERFLOW),
	CRASHTYPE(REFCOUNT_DEC_ZERO),
	CRASHTYPE(REFCOUNT_DEC_NEGATIVE),
	CRASHTYPE(REFCOUNT_DEC_AND_TEST_NEGATIVE),
	CRASHTYPE(REFCOUNT_SUB_AND_TEST_NEGATIVE),
	CRASHTYPE(REFCOUNT_INC_ZERO),
	CRASHTYPE(REFCOUNT_ADD_ZERO),
	CRASHTYPE(REFCOUNT_INC_SATURATED),
	CRASHTYPE(REFCOUNT_DEC_SATURATED),
	CRASHTYPE(REFCOUNT_ADD_SATURATED),
	CRASHTYPE(REFCOUNT_INC_NOT_ZERO_SATURATED),
	CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_SATURATED),
	CRASHTYPE(REFCOUNT_DEC_AND_TEST_SATURATED),
	CRASHTYPE(REFCOUNT_SUB_AND_TEST_SATURATED),
176 177
	CRASHTYPE(REFCOUNT_TIMING),
	CRASHTYPE(ATOMIC_TIMING),
178 179 180 181 182 183 184 185
	CRASHTYPE(USERCOPY_HEAP_SIZE_TO),
	CRASHTYPE(USERCOPY_HEAP_SIZE_FROM),
	CRASHTYPE(USERCOPY_HEAP_FLAG_TO),
	CRASHTYPE(USERCOPY_HEAP_FLAG_FROM),
	CRASHTYPE(USERCOPY_STACK_FRAME_TO),
	CRASHTYPE(USERCOPY_STACK_FRAME_FROM),
	CRASHTYPE(USERCOPY_STACK_BEYOND),
	CRASHTYPE(USERCOPY_KERNEL),
186 187
};

188

K
Kees Cook 已提交
189 190
/* Global kprobe entry and crashtype. */
static struct kprobe *lkdtm_kprobe;
K
Kees Cook 已提交
191 192
static struct crashpoint *lkdtm_crashpoint;
static const struct crashtype *lkdtm_crashtype;
193

K
Kees Cook 已提交
194 195
/* Module parameters */
static int recur_count = -1;
196
module_param(recur_count, int, 0644);
197
MODULE_PARM_DESC(recur_count, " Recursion level for the stack overflow test");
K
Kees Cook 已提交
198 199

static char* cpoint_name;
200
module_param(cpoint_name, charp, 0444);
201
MODULE_PARM_DESC(cpoint_name, " Crash Point, where kernel is to be crashed");
K
Kees Cook 已提交
202 203

static char* cpoint_type;
204
module_param(cpoint_type, charp, 0444);
205 206
MODULE_PARM_DESC(cpoint_type, " Crash Point Type, action to be taken on "\
				"hitting the crash point");
K
Kees Cook 已提交
207 208

static int cpoint_count = DEFAULT_COUNT;
209 210 211
module_param(cpoint_count, int, 0644);
MODULE_PARM_DESC(cpoint_count, " Crash Point Count, number of times the "\
				"crash point is to be hit to trigger action");
212

213

214
/* Return the crashtype number or NULL if the name is invalid */
K
Kees Cook 已提交
215
static const struct crashtype *find_crashtype(const char *name)
216 217
{
	int i;
218

219 220 221
	for (i = 0; i < ARRAY_SIZE(crashtypes); i++) {
		if (!strcmp(name, crashtypes[i].name))
			return &crashtypes[i];
222 223
	}

224
	return NULL;
225 226
}

227 228 229 230
/*
 * This is forced noinline just so it distinctly shows up in the stackdump
 * which makes validation of expected lkdtm crashes easier.
 */
K
Kees Cook 已提交
231
static noinline void lkdtm_do_action(const struct crashtype *crashtype)
232
{
K
Kees Cook 已提交
233 234
	if (WARN_ON(!crashtype || !crashtype->func))
		return;
235
	crashtype->func();
236 237
}

238
static int lkdtm_register_cpoint(struct crashpoint *crashpoint,
K
Kees Cook 已提交
239
				 const struct crashtype *crashtype)
240 241 242
{
	int ret;

243
	/* If this doesn't have a symbol, just call immediately. */
K
Kees Cook 已提交
244
	if (!crashpoint->kprobe.symbol_name) {
245
		lkdtm_do_action(crashtype);
246
		return 0;
247 248
	}

K
Kees Cook 已提交
249 250
	if (lkdtm_kprobe != NULL)
		unregister_kprobe(lkdtm_kprobe);
251 252 253

	lkdtm_crashpoint = crashpoint;
	lkdtm_crashtype = crashtype;
K
Kees Cook 已提交
254 255
	lkdtm_kprobe = &crashpoint->kprobe;
	ret = register_kprobe(lkdtm_kprobe);
256
	if (ret < 0) {
K
Kees Cook 已提交
257 258 259
		pr_info("Couldn't register kprobe %s\n",
			crashpoint->kprobe.symbol_name);
		lkdtm_kprobe = NULL;
260 261
		lkdtm_crashpoint = NULL;
		lkdtm_crashtype = NULL;
262 263 264 265 266
	}

	return ret;
}

A
Arnd Bergmann 已提交
267 268 269 270 271
#ifdef CONFIG_KPROBES
/* Global crash counter and spinlock. */
static int crash_count = DEFAULT_COUNT;
static DEFINE_SPINLOCK(crash_count_lock);

K
Kees Cook 已提交
272 273
/* Called by kprobe entry points. */
static int lkdtm_kprobe_handler(struct kprobe *kp, struct pt_regs *regs)
A
Arnd Bergmann 已提交
274 275 276 277
{
	unsigned long flags;
	bool do_it = false;

K
Kees Cook 已提交
278 279
	if (WARN_ON(!lkdtm_crashpoint || !lkdtm_crashtype))
		return 0;
A
Arnd Bergmann 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293

	spin_lock_irqsave(&crash_count_lock, flags);
	crash_count--;
	pr_info("Crash point %s of type %s hit, trigger in %d rounds\n",
		lkdtm_crashpoint->name, lkdtm_crashtype->name, crash_count);

	if (crash_count == 0) {
		do_it = true;
		crash_count = cpoint_count;
	}
	spin_unlock_irqrestore(&crash_count_lock, flags);

	if (do_it)
		lkdtm_do_action(lkdtm_crashtype);
K
Kees Cook 已提交
294 295

	return 0;
A
Arnd Bergmann 已提交
296 297
}

298 299 300
static ssize_t lkdtm_debugfs_entry(struct file *f,
				   const char __user *user_buf,
				   size_t count, loff_t *off)
301
{
302
	struct crashpoint *crashpoint = file_inode(f)->i_private;
K
Kees Cook 已提交
303
	const struct crashtype *crashtype = NULL;
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	char *buf;
	int err;

	if (count >= PAGE_SIZE)
		return -EINVAL;

	buf = (char *)__get_free_page(GFP_KERNEL);
	if (!buf)
		return -ENOMEM;
	if (copy_from_user(buf, user_buf, count)) {
		free_page((unsigned long) buf);
		return -EFAULT;
	}
	/* NULL-terminate and remove enter */
	buf[count] = '\0';
	strim(buf);

321 322
	crashtype = find_crashtype(buf);
	free_page((unsigned long)buf);
323

324
	if (!crashtype)
325 326
		return -EINVAL;

327
	err = lkdtm_register_cpoint(crashpoint, crashtype);
328 329 330 331 332 333 334
	if (err < 0)
		return err;

	*off += count;

	return count;
}
A
Arnd Bergmann 已提交
335
#endif
336 337 338 339 340 341 342 343 344

/* Generic read callback that just prints out the available crash types */
static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
		size_t count, loff_t *off)
{
	char *buf;
	int i, n, out;

	buf = (char *)__get_free_page(GFP_KERNEL);
345 346
	if (buf == NULL)
		return -ENOMEM;
347 348

	n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
349 350 351 352
	for (i = 0; i < ARRAY_SIZE(crashtypes); i++) {
		n += snprintf(buf + n, PAGE_SIZE - n, "%s\n",
			      crashtypes[i].name);
	}
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
	buf[n] = '\0';

	out = simple_read_from_buffer(user_buf, count, off,
				      buf, n);
	free_page((unsigned long) buf);

	return out;
}

static int lkdtm_debugfs_open(struct inode *inode, struct file *file)
{
	return 0;
}

/* Special entry to just crash directly. Available without KPROBEs */
static ssize_t direct_entry(struct file *f, const char __user *user_buf,
		size_t count, loff_t *off)
{
K
Kees Cook 已提交
371
	const struct crashtype *crashtype;
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	char *buf;

	if (count >= PAGE_SIZE)
		return -EINVAL;
	if (count < 1)
		return -EINVAL;

	buf = (char *)__get_free_page(GFP_KERNEL);
	if (!buf)
		return -ENOMEM;
	if (copy_from_user(buf, user_buf, count)) {
		free_page((unsigned long) buf);
		return -EFAULT;
	}
	/* NULL-terminate and remove enter */
	buf[count] = '\0';
	strim(buf);

390
	crashtype = find_crashtype(buf);
391
	free_page((unsigned long) buf);
392
	if (!crashtype)
393 394
		return -EINVAL;

395 396
	pr_info("Performing direct entry %s\n", crashtype->name);
	lkdtm_do_action(crashtype);
397 398 399 400 401 402 403 404 405
	*off += count;

	return count;
}

static struct dentry *lkdtm_debugfs_root;

static int __init lkdtm_module_init(void)
{
406
	struct crashpoint *crashpoint = NULL;
K
Kees Cook 已提交
407
	const struct crashtype *crashtype = NULL;
408 409 410
	int ret = -EINVAL;
	int i;

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
	/* Neither or both of these need to be set */
	if ((cpoint_type || cpoint_name) && !(cpoint_type && cpoint_name)) {
		pr_err("Need both cpoint_type and cpoint_name or neither\n");
		return -EINVAL;
	}

	if (cpoint_type) {
		crashtype = find_crashtype(cpoint_type);
		if (!crashtype) {
			pr_err("Unknown crashtype '%s'\n", cpoint_type);
			return -EINVAL;
		}
	}

	if (cpoint_name) {
		for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
			if (!strcmp(cpoint_name, crashpoints[i].name))
				crashpoint = &crashpoints[i];
		}

		/* Refuse unknown crashpoints. */
		if (!crashpoint) {
			pr_err("Invalid crashpoint %s\n", cpoint_name);
			return -EINVAL;
		}
	}

A
Arnd Bergmann 已提交
438
#ifdef CONFIG_KPROBES
439 440
	/* Set crash count. */
	crash_count = cpoint_count;
A
Arnd Bergmann 已提交
441
#endif
442

443
	/* Handle test-specific initialization. */
444
	lkdtm_bugs_init(&recur_count);
445
	lkdtm_perms_init();
446 447
	lkdtm_usercopy_init();

448 449 450
	/* Register debugfs interface */
	lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
	if (!lkdtm_debugfs_root) {
451
		pr_err("creating root dir failed\n");
452 453 454
		return -ENODEV;
	}

455 456 457
	/* Install debugfs trigger files. */
	for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
		struct crashpoint *cur = &crashpoints[i];
458 459 460
		struct dentry *de;

		de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
461
					 cur, &cur->fops);
462
		if (de == NULL) {
463
			pr_err("could not create crashpoint %s\n", cur->name);
464 465 466 467
			goto out_err;
		}
	}

468 469 470
	/* Install crashpoint if one was selected. */
	if (crashpoint) {
		ret = lkdtm_register_cpoint(crashpoint, crashtype);
471
		if (ret < 0) {
472
			pr_info("Invalid crashpoint %s\n", crashpoint->name);
473 474
			goto out_err;
		}
475
		pr_info("Crash point %s of type %s registered\n",
476
			crashpoint->name, cpoint_type);
477
	} else {
478
		pr_info("No crash points registered, enable through debugfs\n");
479 480 481
	}

	return 0;
482 483 484 485

out_err:
	debugfs_remove_recursive(lkdtm_debugfs_root);
	return ret;
486 487
}

A
Adrian Bunk 已提交
488
static void __exit lkdtm_module_exit(void)
489
{
490 491
	debugfs_remove_recursive(lkdtm_debugfs_root);

492 493
	/* Handle test-specific clean-up. */
	lkdtm_usercopy_exit();
K
Kees Cook 已提交
494

K
Kees Cook 已提交
495 496
	if (lkdtm_kprobe != NULL)
		unregister_kprobe(lkdtm_kprobe);
497

498
	pr_info("Crash point unregistered\n");
499 500 501 502 503 504
}

module_init(lkdtm_module_init);
module_exit(lkdtm_module_exit);

MODULE_LICENSE("GPL");
505
MODULE_DESCRIPTION("Kernel crash testing module");