Advertisement
benaryorg

Linux Arbitary RAM Access Kernel Module (not working well)

Mar 30th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. #include <linux/module.h>
  2. #include <linux/version.h>
  3. #include <linux/kernel.h>
  4. #include <linux/types.h>
  5. #include <linux/kdev_t.h>
  6. #include <linux/fs.h>
  7. #include <linux/device.h>
  8. #include <linux/cdev.h>
  9.  
  10. static dev_t first;
  11. static struct cdev c_dev;
  12. static struct class *cl;
  13.  
  14. static int dopen(struct inode *i,struct file *f)
  15. {
  16.     printk(KERN_INFO "Driver: open()\n");
  17.     return 0;
  18. }
  19.  
  20. static int dclose(struct inode *i,struct file *f)
  21. {
  22.     printk(KERN_INFO "Driver: close()\n");
  23.     return 0;
  24. }
  25.  
  26. static ssize_t dread(struct file *f,char __user *buf,size_t len,loff_t *off)
  27. {
  28.     printk(KERN_INFO "Driver: read()\n");
  29.     memcpy(buf,off,len);
  30.     return len;
  31. }
  32.  
  33. static ssize_t dwrite(struct file *f,const char __user *buf,size_t len,loff_t *off)
  34. {
  35.     printk(KERN_INFO "Driver: write()\n");
  36.     memcpy(off,buf,len);
  37.     return len;
  38. }
  39. static int duevent(struct device *dev,struct kobj_uevent_env *env)
  40. {
  41.     printk(KERN_INFO "Driver: uevent()\n");
  42.     add_uevent_var(env,"DEVMODE=%#o",0666);
  43.     return 0;
  44. }
  45.  
  46. static struct file_operations pugs_fops=
  47. {
  48.     .owner=THIS_MODULE,
  49.     .open=dopen,
  50.     .release=dclose,
  51.     .read=dread,
  52.     .write=dwrite,
  53. };
  54.  
  55. static int __init benarydev_init(void)
  56. {
  57.     printk(KERN_INFO "benarydev: starting");
  58.     if(alloc_chrdev_region(&first,0,1,"benarydev")<0)
  59.     {
  60.         return -1;
  61.     }
  62.     if ((cl = class_create(THIS_MODULE,"chardrv")) == NULL)
  63.     {
  64.         unregister_chrdev_region(first,1);
  65.         return -1;
  66.     }
  67.     cl->dev_uevent=duevent;
  68.     if (device_create(cl,NULL,first,NULL,"benarydevnull") == NULL)
  69.     {
  70.         class_destroy(cl);
  71.         unregister_chrdev_region(first,1);
  72.         return -1;
  73.     }
  74.     cdev_init(&c_dev,&pugs_fops);
  75.     if (cdev_add(&c_dev,first,1) == -1)
  76.     {
  77.         device_destroy(cl,first);
  78.         class_destroy(cl);
  79.         unregister_chrdev_region(first,1);
  80.         return -1;
  81.     }
  82.     printk(KERN_INFO "benarydev: started");
  83.     return 0;
  84. }
  85.  
  86. static void __exit benarydev_exit(void)
  87. {
  88.     printk(KERN_INFO "benarydev: stopping");
  89.     cdev_del(&c_dev);
  90.     device_destroy(cl, first);
  91.     class_destroy(cl);
  92.     unregister_chrdev_region(first, 1);
  93.     printk(KERN_INFO "benarydev: stopped");
  94. }
  95.  
  96. module_init(benarydev_init);
  97. module_exit(benarydev_exit);
  98.  
  99. MODULE_LICENSE("GPL");
  100. MODULE_AUTHOR("benaryorg <binary at benary dot org>");
  101. MODULE_DESCRIPTION("benaryorg's device driver");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement