doubts for copy_from_user

er krishna erkrishna at gmail.com
Sun Jul 3 15:55:16 EDT 2011


On Sun, Jul 3, 2011 at 10:45 PM, Dave Hylands <dhylands at gmail.com> wrote:

> Hi Vishal,
>
> On Sun, Jul 3, 2011 at 5:09 AM, Vishal Thanki <vishalthanki at gmail.com>
> wrote:
> > Hi,
> >
> > We use copy_from_user/copy_to_user functions to copy data transfer
> > user data between user space and kernel space. As per my
> > understanding, these functions are required because we cannot access
> > userspace data directly within kernel space.
>

Basically if you want to aceess some data from userspace and if that data is
not present in memory...or if your user pointer is invalid then it can
create lots of problem in your kernel module and kernel may crash.

So, its better to acess something safely, thats why copy to usr/ copy from
user is. it does all the validation on user space pointers. Try with memcpy
also in your code in place of copy to usr. It will also work if your user
space pointer is valid enough. if the user space address is swapped
out,  this will not work. See what happens then...



> But, the implementation
> > of these functions actually access the user-space data right? Can
> > someone explain me how this actually happens?
>
> How it actually happens is architecture dependent. Whether kernel
> space can access user space while in the context of the user-process
> is also architecture dependent. I know with ARM, for example, it's
> possible to do memcpy instead of copy_to_user, but using memcpy isn't
> guaranteed to work on all architectures.
>
> The other difference between memcpy and copy_to/from_user is that
> memcpy will typically cause a segmentation fault if passed a bad
> pointer, where copy_to/from_user won't.
>
> --
> Dave Hylands
> Shuswap, BC, Canada
> http://www.davehylands.com
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20110704/d1071d3b/attachment.html 


More information about the Kernelnewbies mailing list