Skip to contentSkip to conversation

[2.5.48]kprobes sample driver

PATCH New Rusty Lynch 1 participant 1 message Nov 18, 2002 Char and misc drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 [2.5.48]kprobes sample driver

This is a resubmit of sample kprobes driver for the 2.5.48 kernel. There is a couple of minor clean-ups, but the only major change is that I am adding a function to arch/i386/kernel/traps.c that enables the sample driver to verify the kernel addresses being passed in are valid.

(I submitted a patch for traps.c, but adding a new function to traps.c just for this little sample doesn't make for that strong of an argument.)

Oh yea, this patch can also be fetched from
http://www.stinkycat.com/patches/patch-kprobes_sample-2.5.48.diff.

As before ==>
This is a sample kprobes module that implements a simple misc char device that can cause arbitrary text to be printk'ed when arbitrary kernel addresses are executed.

This patch was created on top of a 2.5.47 kernel with the kprobes patch applied.

I created this driver just to learn what kprobes was all about. I am sending this to the mailing list for other people that are curious about kprobes (and also to have people who know more about kprobes show me where my understanding is flawed.)

To use the device, create a misc char node with a minor of 221, and then write strings of the format "0xADDRESS MESSAGE" to the device. When the file is closed all kprobes are unregistered, so I usually test by doing:

$ mknod /dev/noisy c 10 221
$ cat > /dev/noisy
0xc0107d50 sys_fork

.. and then go do something that will trigger a sys_fork.

arch/i386/kernel/i386_ksyms.c +3 -0
diff -urN linux-2.5.48-kprobes/arch/i386/kernel/i386_ksyms.c linux-2.5.48-kprobes-patched/arch/i386/kernel/i386_ksyms.c
@@ -59,6 +59,8 @@
 extern unsigned long cpu_khz;
 extern unsigned long get_cmos_time(void);
 
+extern int valid_kernel_address(unsigned long addr);
+
 /* platform dependent support */
 EXPORT_SYMBOL(boot_cpu_data);
 #ifdef CONFIG_EISA
@@ -91,6 +93,7 @@
 EXPORT_SYMBOL(get_cmos_time);
 EXPORT_SYMBOL(cpu_khz);
 EXPORT_SYMBOL(apm_info);
+EXPORT_SYMBOL(valid_kernel_address);
 
 #ifdef CONFIG_DEBUG_IOVIRT
 EXPORT_SYMBOL(__io_virt_debug);
arch/i386/kernel/traps.c +5 -0
diff -urN linux-2.5.48-kprobes/arch/i386/kernel/traps.c linux-2.5.48-kprobes-patched/arch/i386/kernel/traps.c
@@ -131,6 +131,11 @@
 
 #endif
 
+int valid_kernel_address(unsigned long addr)
+{
+	return kernel_text_address(addr);
+}
+
 void show_trace(unsigned long * stack)
 {
 	int i;
drivers/char/Kconfig +15 -0
diff -urN linux-2.5.48-kprobes/drivers/char/Kconfig linux-2.5.48-kprobes-patched/drivers/char/Kconfig
@@ -1270,5 +1270,20 @@
 	  Once bound, I/O against /dev/raw/rawN uses efficient zero-copy I/O. 
 	  See the raw(8) manpage for more details.
 
+config NOISY
+	tristate "Noisy Interface Support"
+	---help---
+	  If you say Y here and create a character special file /dev/noisy with
+	  major number 10 and minor number 221 using mknod ("man mknod"), you
+	  will get access to an interface for inserting arbitrary printk's
+	  into executing kernel code.
+
+	  This driver is also available as a module ( = code which can be
+	  inserted in and removed from the running kernel whenever you want).
+	  The module is called noisy.o. If you want to compile it as a module,
+	  say M here and read <file:Documentation/modules.txt>.
+
+	  If unsure, say N.
+
 endmenu
 
drivers/char/Makefile +1 -0
diff -urN linux-2.5.48-kprobes/drivers/char/Makefile linux-2.5.48-kprobes-patched/drivers/char/Makefile
@@ -102,6 +102,7 @@
 obj-$(CONFIG_AGP) += agp/
 obj-$(CONFIG_DRM) += drm/
 obj-$(CONFIG_PCMCIA) += pcmcia/
+obj-$(CONFIG_NOISY) += noisy.o
 
 # Files generated that shall be removed upon make clean
 clean-files := consolemap_deftbl.c defkeymap.c qtronixmap.c
drivers/char/noisy.c +222 -0

222 lines not on this page · raw message

include/linux/miscdevice.h +1 -0
diff -urN linux-2.5.48-kprobes/include/linux/miscdevice.h linux-2.5.48-kprobes-patched/include/linux/miscdevice.h
@@ -23,6 +23,7 @@
 #define MICROCODE_MINOR		184
 #define MWAVE_MINOR	219		/* ACP/Mwave Modem */
 #define MPT_MINOR	220
+#define NOISY_MINOR 221
 #define MISC_DYNAMIC_MINOR 255
 
 #define SGI_GRAPHICS_MINOR   146