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?
Always use
copy_from_userandcopy_to_userwhen dealing with user space pointers. Even if simplememcpysometimes works for you there are situations where it can fail. See this thread for the information.Speaking about
procfsit’s necessary to take into account that it use a little trick with kernel memory preallocation. See this link for details.