Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to use copy_to_user?

Tags:

c

linux-kernel

I'm trying to define a system call that modifies the character buffer passed to it. Specifically, 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 is unchanged. What's happening?

like image 472
Stefan Kendall Avatar asked Dec 02 '09 23:12

Stefan Kendall


People also ask

How copy_ from_ user works?

The copy_from_user function copies a block of data from user space into a kernel buffer. it accepts a destination buffer (in kernel space), a source buffer (from user space), and a length defined in bytes.

How do I copy data from user space to kernel space?

You can use the copy_from_user() and copy_to_user() functions to move data between kernel space and user space.

What does Copy_from_user return?

Returns number of bytes that could not be copied. On success, this will be zero. If some data could not be copied, this function will pad the copied data to the requested size using zero bytes.

What is __ user in Linux?

__user is used to mark a pointer as userspace, as in, to indicate that the pointer exists in userspace and that it should not be dereferenced.


2 Answers

Remeber that tmp is already a pointer! Correct way to do it:

copy_to_user( buff, tmp, len );
like image 95
Alex Avatar answered Oct 10 '22 03:10

Alex


That looks OK. It's possible that the buffer that userspace passed is mapped read-only - for example if it's in the text segment (eg. a string literal). By the way, this is probably what you want:

return copy_to_user(buff, &tmp, len) ? -EFAULT : 0;
like image 31
caf Avatar answered Oct 10 '22 04:10

caf