From c73b83935734bf0a9b56183a2535dd0daba221fe Mon Sep 17 00:00:00 2001 From: 0xwille Date: Fri, 24 Jun 2011 13:57:37 +0200 Subject: Commit inicial --- Makefile | 7 ++ examples/rtl8192se_linux_2.6.0019.1207.2010.tar.gz | Bin 0 -> 2023545 bytes handlers/Makefile | 8 ++ handlers/chardev.c | 105 +++++++++++++++++++++ handlers/chardev.h | 4 + handlers/procfs1.c | 50 ++++++++++ hello/Makefile | 12 +++ hello/hello-1.c | 13 +++ hello/hello-2.c | 21 +++++ hello/hello-3.c | 50 ++++++++++ hello/hello-4.c | 28 ++++++ hello/start.c | 15 +++ hello/stop.c | 14 +++ syscall.c | 77 +++++++++++++++ 14 files changed, 404 insertions(+) create mode 100644 Makefile create mode 100644 examples/rtl8192se_linux_2.6.0019.1207.2010.tar.gz create mode 100644 handlers/Makefile create mode 100644 handlers/chardev.c create mode 100644 handlers/chardev.h create mode 100644 handlers/procfs1.c create mode 100644 hello/Makefile create mode 100644 hello/hello-1.c create mode 100644 hello/hello-2.c create mode 100644 hello/hello-3.c create mode 100644 hello/hello-4.c create mode 100644 hello/start.c create mode 100644 hello/stop.c create mode 100644 syscall.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..332678d --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +obj-m += syscall.o + +all: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules + +clean: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean diff --git a/examples/rtl8192se_linux_2.6.0019.1207.2010.tar.gz b/examples/rtl8192se_linux_2.6.0019.1207.2010.tar.gz new file mode 100644 index 0000000..1c4f22d Binary files /dev/null and b/examples/rtl8192se_linux_2.6.0019.1207.2010.tar.gz differ diff --git a/handlers/Makefile b/handlers/Makefile new file mode 100644 index 0000000..10043eb --- /dev/null +++ b/handlers/Makefile @@ -0,0 +1,8 @@ +obj-m += chardev.o +obj-m += procfs1.o + +all: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules + +clean: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean diff --git a/handlers/chardev.c b/handlers/chardev.c new file mode 100644 index 0000000..3594bce --- /dev/null +++ b/handlers/chardev.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include + +#include "chardev.h" + +#define SUCCESS 0 +#define DEVICE_NAME "chardev" +#define BUF_LEN 80 + +static int Major; +static int opened = 0; +static char msg[BUF_LEN]; +static char* msg_ptr; + +static struct file_operations fops = { + .read = device_read, + .write = device_write, + .open = device_open, + .release = device_release +}; + +int init_module(void) +{ + Major = register_chrdev(0, DEVICE_NAME, &fops); + + if (Major < 0) { + printk(KERN_ALERT "Registering char device failed with %d\n", Major); + return Major; + } + + printk(KERN_INFO "I was assigned major number %d. To talk to\n", Major); + printk(KERN_INFO "the driver, create a dev file with\n"); + printk(KERN_INFO "'mknod /dev/%s c %d 0'.\n", DEVICE_NAME, Major); + printk(KERN_INFO "Try to cat and echo to the device file.\n"); + printk(KERN_INFO "Remove the device file and module when done.\n"); + + return SUCCESS; +} + +void cleanup_module(void) +{ + unregister_chrdev(Major, DEVICE_NAME); +} + +static int device_open(struct inode* inode, struct file* filp) +{ + static int counter = 0; + + if (opened) + return -EBUSY; + + opened++; + if (counter++ == 0) + sprintf(msg, "Come on, write something here :D\n"); + + msg_ptr = msg; + try_module_get(THIS_MODULE); + + return SUCCESS; +} + +static int device_release(struct inode* inode, struct file* filp) +{ + opened--; + + module_put(THIS_MODULE); + + return 0; +} + +static ssize_t device_read(struct file* filp, char* buffer, size_t len, loff_t* off) +{ + int bytes = 0; + + if (*msg_ptr == 0) + return 0; + + while (len-- && *msg_ptr) { + put_user(*msg_ptr++, buffer++); + bytes++; + } + + return bytes; +} + +static ssize_t device_write(struct file* filp, const char* buff, size_t len, loff_t* off) +{ + int bytes = 0; + + if (len > BUF_LEN) { + printk(KERN_ALERT "Error: dude you tried to overflow my buffer!\n"); + return -ENOBUFS; + } + + while (len--) { + get_user(msg[bytes], buff++); + bytes++; + } + msg[bytes] = '\0'; + + msg_ptr = msg; + return bytes; +} diff --git a/handlers/chardev.h b/handlers/chardev.h new file mode 100644 index 0000000..23b010a --- /dev/null +++ b/handlers/chardev.h @@ -0,0 +1,4 @@ +static int device_open(struct inode*, struct file*); +static int device_release(struct inode*, struct file*); +static ssize_t device_read(struct file*, char*, size_t, loff_t*); +static ssize_t device_write(struct file*, const char*, size_t, loff_t*); diff --git a/handlers/procfs1.c b/handlers/procfs1.c new file mode 100644 index 0000000..9d96319 --- /dev/null +++ b/handlers/procfs1.c @@ -0,0 +1,50 @@ +#include +#include +#include + +#define procfs_name "helloworld" + +struct proc_dir_entry* Our_Proc_File; +struct proc_dir_entry proc_root; + +int procfile_read(char* buf, char** buf_loc, off_t off, int buf_len, int* eof, void* data) +{ + int ret; + + printk(KERN_INFO "procfile_read (/proc/%s) called\n", procfs_name); + + if (off > 0) + ret = 0; + else + ret = sprintf(buf, "HelloWorld!\n"); + + return ret; +} + +int init_module(void) +{ + Our_Proc_File = create_proc_entry(procfs_name, 0644, NULL); + + if (Our_Proc_File == NULL) { + remove_proc_entry(procfs_name, NULL); + //remove_proc_entry(procfs_name, &proc_root); + printk(KERN_ALERT "Error: Could not initialize /proc/%s\n", procfs_name); + return -ENOMEM; + } + + Our_Proc_File->read_proc = procfile_read; + Our_Proc_File->mode = S_IFREG | S_IRUGO; + Our_Proc_File->uid = 0; + Our_Proc_File->gid = 0; + Our_Proc_File->size = 37; + + printk(KERN_INFO "/proc/%s created\n", procfs_name); + return 0; +} + +void cleanup_module(void) +{ + remove_proc_entry(procfs_name, NULL); + //remove_proc_entry(procfs_name, &proc_root); + printk(KERN_INFO "/proc/%s removed\n", procfs_name); +} diff --git a/hello/Makefile b/hello/Makefile new file mode 100644 index 0000000..d63aa3d --- /dev/null +++ b/hello/Makefile @@ -0,0 +1,12 @@ +obj-m += hello-1.o +obj-m += hello-2.o +obj-m += hello-3.o +obj-m += hello-4.o +obj-m += startstop.o +startstop-objs := start.o stop.o + +all: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules + +clean: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean diff --git a/hello/hello-1.c b/hello/hello-1.c new file mode 100644 index 0000000..aa434a9 --- /dev/null +++ b/hello/hello-1.c @@ -0,0 +1,13 @@ +#include +#include + +int init_module(void) +{ + printk(KERN_INFO "Hola, mundo! 1\n"); + return 0; +} + +void cleanup_module(void) +{ + printk(KERN_INFO "Adiós, mundo! 1\n"); +} diff --git a/hello/hello-2.c b/hello/hello-2.c new file mode 100644 index 0000000..5819e38 --- /dev/null +++ b/hello/hello-2.c @@ -0,0 +1,21 @@ +#include +#include +#include + +static int __init hello_2_init(void) +{ + printk(KERN_INFO "Hola, mundo! 2\n"); + return 0; +} + +static void __exit hello_2_exit(void) +{ + printk(KERN_INFO "Adiós, mundo! 2\n"); +} + +module_init(hello_2_init); +module_exit(hello_2_exit); + +MODULE_AUTHOR("Guillermo Ramos"); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Módulo tonto"); diff --git a/hello/hello-3.c b/hello/hello-3.c new file mode 100644 index 0000000..a82e4a4 --- /dev/null +++ b/hello/hello-3.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include + +static short int myshort = 1; +static int myint = 420; +static long int mylong = 9999; +static char* mystring = "blah"; +static int myintArray[2] = {-1, -1}; +static int arr_argc = 0; + +module_param(myshort, short, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); +MODULE_PARM_DESC(myshort, "A short integer"); +module_param(myint, int, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); +MODULE_PARM_DESC(myint, "An integer"); +module_param(mylong, long, S_IRUSR); +MODULE_PARM_DESC(mylong, "A long integer"); +module_param(mystring, charp, 0000); +MODULE_PARM_DESC(mystring, "A character string"); + +module_param_array(myintArray, int, &arr_argc, 0000); +MODULE_PARM_DESC(myintArray, "An array of integers"); + +static int __init hello_3_init(void) +{ + int i; + printk(KERN_INFO "Hola, mundo! 3\n============\n"); + printk(KERN_INFO "myshort is a short integer: %hd\n", myshort); + printk(KERN_INFO "myint is an integer: %d\n", myint); + printk(KERN_INFO "mylong is a long integer: %ld\n", mylong); + printk(KERN_INFO "mystring is a string: %s\n", mystring); + for (i = 0; i < sizeof(myintArray)/sizeof(int); i++) + printk(KERN_INFO "myintArray[%d] = %d\n", i, myintArray[i]); + printk(KERN_INFO "got %d arguments for myintArray\n", arr_argc); + return 0; +} + +static void __exit hello_3_exit(void) +{ + printk(KERN_INFO "Adiós, mundo! 3\n============\n"); +} + +module_init(hello_3_init); +module_exit(hello_3_exit); + +MODULE_AUTHOR("Guillermo Ramos"); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Módulo tonto"); diff --git a/hello/hello-4.c b/hello/hello-4.c new file mode 100644 index 0000000..32e2cb6 --- /dev/null +++ b/hello/hello-4.c @@ -0,0 +1,28 @@ +#include +#include +#include + +static int __init hello_4_init(void) +{ + printk(KERN_DEBUG "DEBUG: Hola, mundo! 4\n"); + printk(KERN_INFO "INFO: Hola, mundo! 4\n"); + printk(KERN_NOTICE "NOTICE: Hola, mundo! 4\n"); + printk(KERN_WARNING "WARNING: Hola, mundo! 4\n"); + printk(KERN_CRIT "CRIT: Hola, mundo! 4\n"); + printk(KERN_ALERT "ALERT: Hola, mundo! 4\n"); + printk(KERN_EMERG "EMERG: Hola, mundo! 4\n"); + + return 0; +} + +static void __exit hello_4_exit(void) +{ + printk(KERN_INFO "Adiós, mundo! 2\n"); +} + +module_init(hello_4_init); +module_exit(hello_4_exit); + +MODULE_AUTHOR("Guillermo Ramos"); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Módulo tonto"); diff --git a/hello/start.c b/hello/start.c new file mode 100644 index 0000000..224aeed --- /dev/null +++ b/hello/start.c @@ -0,0 +1,15 @@ +#include +#include +#include + +static int __init hello_2_init(void) +{ + printk(KERN_INFO "Hola, mundo! 2\n"); + return 0; +} + +module_init(hello_2_init); + +MODULE_AUTHOR("Guillermo Ramos"); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Módulo tonto"); diff --git a/hello/stop.c b/hello/stop.c new file mode 100644 index 0000000..46ba9a4 --- /dev/null +++ b/hello/stop.c @@ -0,0 +1,14 @@ +#include +#include +#include + +static void __exit hello_2_exit(void) +{ + printk(KERN_INFO "Adiós, mundo! 2\n"); +} + +module_exit(hello_2_exit); + +MODULE_AUTHOR("Guillermo Ramos"); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Módulo tonto"); diff --git a/syscall.c b/syscall.c new file mode 100644 index 0000000..6068592 --- /dev/null +++ b/syscall.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include + +//extern void* sys_call_table[]; + +static int uid; +module_param(uid, int, 0644); + +//extern asmlinkage long (*sys_open) (const char __user *filename, int flags, int mode); + +asmlinkage int (*original_call) (const char*, int, int); + +asmlinkage int our_sys_open(const char* filename, int flags, int mode) +{ + int i = 0; + char ch; + + if (uid == current->cred->uid) { + printk("Opened file by %d: ", uid); + do { + get_user(ch, filename + i++); + printk("%c", ch); + } while (ch != 0); + printk("\n"); + } + + return original_call(filename, flags, mode); +} + +unsigned long** find_sys_call_table(void) +{ + unsigned long** sctable; + unsigned long ptr; + + extern int loops_per_jiffy; + + sctable = NULL; + for (ptr = (unsigned long)&loops_per_jiffy; + ptr < (unsigned long)&boot_cpu_data; ptr += sizeof(void*)) { + unsigned long *p; + p = (unsigned long*)ptr; + if (p[__NR_open] == (unsigned long) sys_open) { + sctable = (unsigned long**)p; + return sctable; + } + } + + return sctable; +} + +int init_module(void) +{ +// original_call = sys_call_table[__NR_open]; +// sys_call_table[__NR_open] = our_sys_open; +// +// printk(KERN_INFO "Spying on uid: %d\n", uid); + printk(KERN_INFO "sys_call_table[__NR_open] = %p\n", find_sys_call_table()[__NR_open]); + + return 0; +} + +void cleanup_module(void) +{ +// if (sys_call_table[__NR_open] != our_sys_open) { +// printk(KERN_ALERT "Somebody else also played with the open syscall\n"); +// printk(KERN_ALERT "The system may be left in an unstable state\n"); +// } + +// sys_call_table[__NR_open] = original_call; +} + +MODULE_LICENSE("GPL"); -- cgit v1.2.3