I feel like this question has been answered but no implementation I’ve found from googling it has offered the protection I need.
I am working with linux 3.2.2.
I wish to copy variables from user space to kernal space as safely as possible. This includes a struct pointer, and a null terminated string. How could i ensure my struct pointer is valid? (access_ok on (void*) -1 does not catch it) I want it to be basically idiot proof…
For the null terminated string i don’t know the length and some of the functions that copy these null terminated strings want a size.
Solved: So the best solution I found was actually the code I was using just put an and instead of an or.
check if ptr is null first of all.
then
if this passes our usr ptr or struct is within the user address space.
finally use
and make sure destPtr points to a space in kernel thats size is obviously >= structsize.
Even now there is no guarantee that the data in the structure is useful, or what you want at all. It just is raw data that will not cause a kernel panic. So now you can check each member of the struct for valid data. there is some more useful information on this
https://www.ibm.com/developerworks/linux/library/l-kernel-memory-access/