1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
| #include <linux/module.h> #include <linux/version.h> #include <linux/kernel.h> #include <linux/types.h> #include <linux/kdev_t.h> #include <linux/fs.h> #include <linux/device.h> #include <linux/cdev.h> #include <asm/uaccess.h> #include <linux/slab.h>
static char *buffer_var = NULL; static struct class *devClass; static struct cdev cdev; static dev_t stack_dev_no;
struct note { int idx; int len; char* data; };
static char* notelist[1000]; static struct note* noteChunk; static int count = 0;
static ssize_t stack_read(struct file *filp, const char __user *buf, size_t len, loff_t *f_pos);
static ssize_t stack_write(struct file *filp, const char __user *buf, size_t len, loff_t *f_pos);
static long stack_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
static int stack_open(struct inode *i, struct file *f);
static int stack_close(struct inode *i, struct file *f);
static struct file_operations stack_fops = { .owner = THIS_MODULE, .open = stack_open, .release = stack_close, .write = stack_write, .read = stack_read, .unlocked_ioctl = stack_ioctl };
static int __init stack_init(void) { printk(KERN_INFO "[i] Module stack registered"); if (alloc_chrdev_region(&stack_dev_no, 0, 1, "stack") < 0) { return -1; } if ((devClass = class_create(THIS_MODULE, "chardrv")) == NULL) { unregister_chrdev_region(stack_dev_no, 1); return -1; } if (device_create(devClass, NULL, stack_dev_no, NULL, "stack") == NULL) { printk(KERN_INFO "[i] Module stack error"); class_destroy(devClass); unregister_chrdev_region(stack_dev_no, 1); return -1; } cdev_init(&cdev, &stack_fops); if (cdev_add(&cdev, stack_dev_no, 1) == -1) { device_destroy(devClass, stack_dev_no); class_destroy(devClass); unregister_chrdev_region(stack_dev_no, 1); return -1; }
printk(KERN_INFO "[i] <Major, Minor>: <%d, %d>\n", MAJOR(stack_dev_no), MINOR(stack_dev_no)); return 0; }
static void __exit stack_exit(void) { unregister_chrdev_region(stack_dev_no, 1); cdev_del(&cdev); }
ssize_t stack_read(struct file *filp, const char __user *buf, size_t len, loff_t *f_pos) { printk(KERN_INFO "Stack_read function" ); copy_to_user(buf,buffer_var,len); }
ssize_t stack_write(struct file *filp, const char __user *buf, size_t len, loff_t *f_pos) { printk(KERN_INFO "Stack_write function" ); copy_from_user(buffer_var, buf, len); printk("[i] Module stack write: %s\n",buffer_var); return len; }
long stack_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { char* chunk = NULL; int retval = 0; printk(KERN_INFO "Ioctl Get!\n"); printk("notelist_addr:0x%llx\n",¬elist[0]); switch (cmd) {
case 1:
printk("Kernel Add function!---001\n"); noteChunk = (struct Note*)arg; chunk = (char *)kmalloc(noteChunk->len,GFP_KERNEL); printk("chunk_addr:0x%llx\n",chunk); if (!chunk) { printk("Alloca Error\n"); return 0; } memcpy(chunk, noteChunk->data,noteChunk->len); notelist[count] = chunk; chunk = NULL; count ++; printk("Add Success!\n"); break;
case 888: printk("Kernel Free function!---888\n"); noteChunk = (struct Note*)arg; printk("notelist:0x%llx\n",notelist[noteChunk->idx]); if (notelist[noteChunk->idx]) { kfree(notelist[noteChunk->idx]); printk("Free Success!\n"); } else { printk("You can't free it!There is no chunk!\n"); } break;
case 3: printk("Kernel Edit function!---003\n"); noteChunk = (struct Note*)arg; if (notelist[noteChunk->idx]) { memcpy(notelist[noteChunk->idx], noteChunk->data,noteChunk->len); printk("Edit Success!\n"); } else { printk("You can't edit it!There is no chunk!\n"); } break; case 4: printk("Kernel Read function!---004\n"); noteChunk = (struct Note*)arg; if(notelist[noteChunk->idx]){ copy_to_user(noteChunk->data,notelist[noteChunk->idx],noteChunk->len); printk("Read Success!\n"); } break;
case 111: printk("Test add chunk!---111\n"); printk(KERN_INFO "No buffer_var!Malloc now!" ); buffer_var=(char*)kmalloc(0xa8,GFP_KERNEL); printk("buffer_var:0x%llx\n",buffer_var); break;
default: retval = -1; break; }
return retval; }
static int stack_open(struct inode *i, struct file *f) { printk(KERN_INFO "[i] Module stack: open()\n"); return 0; }
static int stack_close(struct inode *i, struct file *f) { kfree(buffer_var); printk(KERN_INFO "[i] Module stack: close()\n"); return 0; }
module_init(stack_init); module_exit(stack_exit);
MODULE_LICENSE("GPL");
|