I am finishing up project for OS class and can't figure several things that have to do with SAFELY copying data from user-space into kernel and back from kernel into user space, and how to properly discard this information.
Say I have several system calls:
//copies data into kernel space
long sys_into(void __user *data, long length);
// copies data into user space
long sys_from(void __user *data, long length);
In both cases long length
is the number of bytes to be copied.
Things I was able to figure out so far:
1. Validate that pointers *data
are not null
.
2. Validate that length < 0
3. I need to use access_ok
. However, I am not sure if I need to use it for both functions or only for the long sys_into()
3. When copying into kernel using kmalloc(length) to allocate number of bytes and make sure that I can actually allocate this memory.
4. Finally use copy_from_user
& copy_to_user
to copy the data.
So far, I found very little information. 1. Source code example from "Linux kernel programming" (as was pointed out, example in the Linux Kernel Development is dangerous). 2. http://www.quora.com/Linux-Kernel/How-does-copy_to_user-work
Thanks !!!