Commit 8776fe75 authored by Kees Cook's avatar Kees Cook Committed by Ingo Molnar
Browse files

lkdtm, kprobes: Convert from jprobes to kprobes



The jprobes subsystem is being removed, so convert to using kprobes instead.

Signed-off-by: default avatarKees Cook <keescook@chromium.org>
Acked-by: default avatarMasami Hiramatsu <mhiramat@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/20171020133127.GA18360@beast


Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
parent 9b17374e
Loading
Loading
Loading
Loading
+45 −109
Original line number Original line Diff line number Diff line
@@ -56,122 +56,54 @@ static ssize_t direct_entry(struct file *f, const char __user *user_buf,
			    size_t count, loff_t *off);
			    size_t count, loff_t *off);


#ifdef CONFIG_KPROBES
#ifdef CONFIG_KPROBES
static void lkdtm_handler(void);
static int lkdtm_kprobe_handler(struct kprobe *kp, struct pt_regs *regs);
static ssize_t lkdtm_debugfs_entry(struct file *f,
static ssize_t lkdtm_debugfs_entry(struct file *f,
				   const char __user *user_buf,
				   const char __user *user_buf,
				   size_t count, loff_t *off);
				   size_t count, loff_t *off);

# define CRASHPOINT_KPROBE(_symbol)				\

		.kprobe = {					\
/* jprobe entry point handlers. */
			.symbol_name = (_symbol),		\
static unsigned int jp_do_irq(unsigned int irq)
			.pre_handler = lkdtm_kprobe_handler,	\
{
		},
	lkdtm_handler();
# define CRASHPOINT_WRITE(_symbol)				\
	jprobe_return();
		(_symbol) ? lkdtm_debugfs_entry : direct_entry
	return 0;
#else
}
# define CRASHPOINT_KPROBE(_symbol)

# define CRASHPOINT_WRITE(_symbol)		direct_entry
static irqreturn_t jp_handle_irq_event(unsigned int irq,
				       struct irqaction *action)
{
	lkdtm_handler();
	jprobe_return();
	return 0;
}

static void jp_tasklet_action(struct softirq_action *a)
{
	lkdtm_handler();
	jprobe_return();
}

static void jp_ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
{
	lkdtm_handler();
	jprobe_return();
}

struct scan_control;

static unsigned long jp_shrink_inactive_list(unsigned long max_scan,
					     struct zone *zone,
					     struct scan_control *sc)
{
	lkdtm_handler();
	jprobe_return();
	return 0;
}

static int jp_hrtimer_start(struct hrtimer *timer, ktime_t tim,
			    const enum hrtimer_mode mode)
{
	lkdtm_handler();
	jprobe_return();
	return 0;
}

static int jp_scsi_dispatch_cmd(struct scsi_cmnd *cmd)
{
	lkdtm_handler();
	jprobe_return();
	return 0;
}

# ifdef CONFIG_IDE
static int jp_generic_ide_ioctl(ide_drive_t *drive, struct file *file,
			struct block_device *bdev, unsigned int cmd,
			unsigned long arg)
{
	lkdtm_handler();
	jprobe_return();
	return 0;
}
# endif
#endif
#endif


/* Crash points */
/* Crash points */
struct crashpoint {
struct crashpoint {
	const char *name;
	const char *name;
	const struct file_operations fops;
	const struct file_operations fops;
	struct jprobe jprobe;
	struct kprobe kprobe;
};
};


#define CRASHPOINT(_name, _write, _symbol, _entry)		\
#define CRASHPOINT(_name, _symbol)				\
	{							\
	{							\
		.name = _name,					\
		.name = _name,					\
		.fops = {					\
		.fops = {					\
			.read	= lkdtm_debugfs_read,		\
			.read	= lkdtm_debugfs_read,		\
			.llseek	= generic_file_llseek,		\
			.llseek	= generic_file_llseek,		\
			.open	= lkdtm_debugfs_open,		\
			.open	= lkdtm_debugfs_open,		\
			.write	= _write,			\
			.write	= CRASHPOINT_WRITE(_symbol)	\
		},						\
		.jprobe = {					\
			.kp.symbol_name = _symbol,		\
			.entry = (kprobe_opcode_t *)_entry,	\
		},						\
		},						\
		CRASHPOINT_KPROBE(_symbol)			\
	}
	}


/* Define the possible places where we can trigger a crash point. */
/* Define the possible places where we can trigger a crash point. */
struct crashpoint crashpoints[] = {
static struct crashpoint crashpoints[] = {
	CRASHPOINT("DIRECT",			direct_entry,
	CRASHPOINT("DIRECT",		 NULL),
		   NULL,			NULL),
#ifdef CONFIG_KPROBES
#ifdef CONFIG_KPROBES
	CRASHPOINT("INT_HARDWARE_ENTRY",	lkdtm_debugfs_entry,
	CRASHPOINT("INT_HARDWARE_ENTRY", "do_IRQ"),
		   "do_IRQ",			jp_do_irq),
	CRASHPOINT("INT_HW_IRQ_EN",	 "handle_IRQ_event"),
	CRASHPOINT("INT_HW_IRQ_EN",		lkdtm_debugfs_entry,
	CRASHPOINT("INT_TASKLET_ENTRY",	 "tasklet_action"),
		   "handle_IRQ_event",		jp_handle_irq_event),
	CRASHPOINT("FS_DEVRW",		 "ll_rw_block"),
	CRASHPOINT("INT_TASKLET_ENTRY",		lkdtm_debugfs_entry,
	CRASHPOINT("MEM_SWAPOUT",	 "shrink_inactive_list"),
		   "tasklet_action",		jp_tasklet_action),
	CRASHPOINT("TIMERADD",		 "hrtimer_start"),
	CRASHPOINT("FS_DEVRW",			lkdtm_debugfs_entry,
	CRASHPOINT("SCSI_DISPATCH_CMD",	 "scsi_dispatch_cmd"),
		   "ll_rw_block",		jp_ll_rw_block),
	CRASHPOINT("MEM_SWAPOUT",		lkdtm_debugfs_entry,
		   "shrink_inactive_list",	jp_shrink_inactive_list),
	CRASHPOINT("TIMERADD",			lkdtm_debugfs_entry,
		   "hrtimer_start",		jp_hrtimer_start),
	CRASHPOINT("SCSI_DISPATCH_CMD",		lkdtm_debugfs_entry,
		   "scsi_dispatch_cmd",		jp_scsi_dispatch_cmd),
# ifdef CONFIG_IDE
# ifdef CONFIG_IDE
	CRASHPOINT("IDE_CORE_CP",		lkdtm_debugfs_entry,
	CRASHPOINT("IDE_CORE_CP",	 "generic_ide_ioctl"),
		   "generic_ide_ioctl",		jp_generic_ide_ioctl),
# endif
# endif
#endif
#endif
};
};
@@ -254,8 +186,8 @@ struct crashtype crashtypes[] = {
};
};




/* Global jprobe entry and crashtype. */
/* Global kprobe entry and crashtype. */
static struct jprobe *lkdtm_jprobe;
static struct kprobe *lkdtm_kprobe;
struct crashpoint *lkdtm_crashpoint;
struct crashpoint *lkdtm_crashpoint;
struct crashtype *lkdtm_crashtype;
struct crashtype *lkdtm_crashtype;


@@ -298,7 +230,8 @@ static struct crashtype *find_crashtype(const char *name)
 */
 */
static noinline void lkdtm_do_action(struct crashtype *crashtype)
static noinline void lkdtm_do_action(struct crashtype *crashtype)
{
{
	BUG_ON(!crashtype || !crashtype->func);
	if (WARN_ON(!crashtype || !crashtype->func))
		return;
	crashtype->func();
	crashtype->func();
}
}


@@ -308,22 +241,22 @@ static int lkdtm_register_cpoint(struct crashpoint *crashpoint,
	int ret;
	int ret;


	/* If this doesn't have a symbol, just call immediately. */
	/* If this doesn't have a symbol, just call immediately. */
	if (!crashpoint->jprobe.kp.symbol_name) {
	if (!crashpoint->kprobe.symbol_name) {
		lkdtm_do_action(crashtype);
		lkdtm_do_action(crashtype);
		return 0;
		return 0;
	}
	}


	if (lkdtm_jprobe != NULL)
	if (lkdtm_kprobe != NULL)
		unregister_jprobe(lkdtm_jprobe);
		unregister_kprobe(lkdtm_kprobe);


	lkdtm_crashpoint = crashpoint;
	lkdtm_crashpoint = crashpoint;
	lkdtm_crashtype = crashtype;
	lkdtm_crashtype = crashtype;
	lkdtm_jprobe = &crashpoint->jprobe;
	lkdtm_kprobe = &crashpoint->kprobe;
	ret = register_jprobe(lkdtm_jprobe);
	ret = register_kprobe(lkdtm_kprobe);
	if (ret < 0) {
	if (ret < 0) {
		pr_info("Couldn't register jprobe %s\n",
		pr_info("Couldn't register kprobe %s\n",
			crashpoint->jprobe.kp.symbol_name);
			crashpoint->kprobe.symbol_name);
		lkdtm_jprobe = NULL;
		lkdtm_kprobe = NULL;
		lkdtm_crashpoint = NULL;
		lkdtm_crashpoint = NULL;
		lkdtm_crashtype = NULL;
		lkdtm_crashtype = NULL;
	}
	}
@@ -336,13 +269,14 @@ static int lkdtm_register_cpoint(struct crashpoint *crashpoint,
static int crash_count = DEFAULT_COUNT;
static int crash_count = DEFAULT_COUNT;
static DEFINE_SPINLOCK(crash_count_lock);
static DEFINE_SPINLOCK(crash_count_lock);


/* Called by jprobe entry points. */
/* Called by kprobe entry points. */
static void lkdtm_handler(void)
static int lkdtm_kprobe_handler(struct kprobe *kp, struct pt_regs *regs)
{
{
	unsigned long flags;
	unsigned long flags;
	bool do_it = false;
	bool do_it = false;


	BUG_ON(!lkdtm_crashpoint || !lkdtm_crashtype);
	if (WARN_ON(!lkdtm_crashpoint || !lkdtm_crashtype))
		return 0;


	spin_lock_irqsave(&crash_count_lock, flags);
	spin_lock_irqsave(&crash_count_lock, flags);
	crash_count--;
	crash_count--;
@@ -357,6 +291,8 @@ static void lkdtm_handler(void)


	if (do_it)
	if (do_it)
		lkdtm_do_action(lkdtm_crashtype);
		lkdtm_do_action(lkdtm_crashtype);

	return 0;
}
}


static ssize_t lkdtm_debugfs_entry(struct file *f,
static ssize_t lkdtm_debugfs_entry(struct file *f,
@@ -556,8 +492,8 @@ static void __exit lkdtm_module_exit(void)
	/* Handle test-specific clean-up. */
	/* Handle test-specific clean-up. */
	lkdtm_usercopy_exit();
	lkdtm_usercopy_exit();


	if (lkdtm_jprobe != NULL)
	if (lkdtm_kprobe != NULL)
		unregister_jprobe(lkdtm_jprobe);
		unregister_kprobe(lkdtm_kprobe);


	pr_info("Crash point unregistered\n");
	pr_info("Crash point unregistered\n");
}
}