I know there have been questions like this before, but I'm hoping that I can get some help. As an academic exercise, I'm trying to write to a file from a kernel module. I have saved the original write call from the system call table to a typedef (sys_write_orig) and have replaced it with my own function. That all works fine.
In my new sys_write function, if I use sys_write_orig with the original buffer passed in from userland - it works fine. But when I try to create a new buffer - the issues begin. I understand the separation of kernel memory and user memory - but I thought there was a way to do all this. Any ideas? Here's kind of what I'm trying to do:
char* kernbuf = "foo";
char __user* userbuf = (char*) kmalloc(3*sizeof(char), GFP_USER);
int n = copy_to_user(userbuf,kernbuf,3);
printk("%d bytes copied to user space (I think).\n",n);
n = sys_write_orig(fd,userbuf,3);
printk("%d is the result from the write.\n",n);
I'm kind of new to kernel-land. So any help is appreciated. Thanks!