Is copy_to_user necessary with procfs?
-
28-05-2021 - |
Question
I always thought copy_to_user
was necessary when the kernel writes to users via procfs.
I forgot to do it once though (and I used snprintf
) and everything was working fine. Now that I noticed it, I have been searching. I found this link that doesn't say copy_to_user
is needed even though for the other section (kernel reading from user) it does say copy_from_user
is necessary.
So, is the buffer provided to kernel module to write data (so user reads from it), in kernel space already? Is it true that you don't need copy_to_user
when writing through procfs? Or have I been lucky not to get a crash?
Solution
Always use copy_from_user
and copy_to_user
when dealing with user space pointers. Even if simple memcpy
sometimes works for you there are situations where it can fail. See this thread for the information.
Speaking about procfs
it's necessary to take into account that it use a little trick with kernel memory preallocation. See this link for details.