Correct way to use copy_to_user?

I am trying to define a system call that modifies the character buffer passed to it. In particular, something like this:

...
asmlinkage int sys_mycall( char __user *buff, int len )
{
   char tmp[1000];
   copy_from_user(tmp, buff, len);
   /* change tmp here */
   copy_to_user( buff, &tmp, len );
}

Here copy_to_user returns -1, and the buffer from the calling program does not change. What's happening?

+3
source share
2 answers

It looks good. It is possible that a buffer that has passed through user space is read-only — for example, if it is in a text segment (for example, a string literal). By the way, this is probably what you want:

return copy_to_user(buff, &tmp, len) ? -EFAULT : 0;
+5
source

Remember that tmp is already a pointer! The correct way to do this is:

copy_to_user( buff, tmp, len );
+8
source

Source: https://habr.com/ru/post/1724560/


All Articles