What happens when I printk a char * that was initialized in userspace?
-
02-07-2021 - |
Pregunta
I implemented a new system call as an intro exercise. All it does is take in a buffer and printk that buffer. I later learned that the correct practice would be to use copy_from_user.
Is this just a precautionary measure to validate the address, or is my system call causing some error (page fault?) that I cannot see?
If it is just a precautionary measure, what is it protecting against?
Thanks!
Solución
There are several reasons.
- Some architectures employ segmented memory, where there is a separate segment for the user memory. In that case,
copy_from_user
is essential to actually get the right memory address. - The kernel has access to everything, including (almost by definition) a lot of privileged information. Not using
copy_from_user
could allow information disclosure if a user passes in a kernel address. Worse, if you are writing to a user-supplied buffer withoutcopy_to_user
, the user could overwrite kernel memory. - You'd like to prevent the user from crashing the kernel module just by passing in a bad pointer; using
copy_from_user
protects against faults so e.g. a system call handler can returnEFAULT
in response to a bad user pointer.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow