Re: copy_from_user() for userspace data containing pointers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





> I have two structures that i share between kernel/userspace as
> follows:
> 
> struct foo {
>   int num;
>   struct bar *bartable;
> };
> 
> struct bar {
>   u32 ipaddr;
>   unsigned char mac_addr[6];
> };

> The ioctl
> handling routine in the kernel is as follows:
> -- 
> struct foo fookern;
> struct foo *userinfo = (struct foo *)ifr->ifr_data; 
> struct bar *entry;
> 
> copy_from_user(&fookern, userinfo, sizeof(struct foo));
> 
> for(i=0, entry = fookern.bartable; i < fookern.num; i++, entry++) {
>    print(entry->ipaddr, entry->mac_addr)  // pseudo code
> }
> --
> 
> The complete table calloc'ed to 'bartable' and initialized in the
> userspace is not reflected in the kernel. Printing entry->ipaddr and
> entry->mac_addr 'fookern.num' times prints all entry->ipaddr entries

Are you sure this prints the right values? After the copy_from_user(),
fookern.bartable still points to a userland address. How can you
dereference it in the kernel?

> So, how can be correctly copy pointer variables from userspace to
> kernelspace? Or should there be a change in the sizeof parameter in
> the copy_from_user() call?

 You need a second copy_from_user() to copy data that the pointer
variable refers to. In your example, you'd need to copy in 
sizeof(struct bar) bytes from each fookern.bartable pointer.

Hope this helps,
Ravi.

__________________________________________________
Do You Yahoo!?
Yahoo! Movies - coverage of the 74th Academy Awards®
http://movies.yahoo.com/
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux