Re: copy_from_user() for userspace data containing pointers

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

 



hi,
There is one important things you are missing.
First of all you should not transfer pointers pointing
to data in user space, to kernel space.

One work around is to have a whole chunk of memory
allocated for "struct foo" array and another chunk for
"struct bar" array. Now replace the bar pointer in foo
with the index of the bar element in the array.(u can
also write some macros to make dereferencing easy)

Now, if u transfer the whole foo array and bar array
between kernel and userspace it becomes very easy to
handle the data.

eg:

#define TransformToPointer(index) (struct bar *)(index
+ BarArrayBasePointer)
#define TransformToIndex(BarPointer)
(int)(BarPointer-BarArrayBasePointer)

struct foo {
  int num;
  int index;
};
 
struct bar {
  u32 ipaddr;
  unsigned char mac_addr[6];
};

hope this helps :)
Cheers,
Mohan S

--- Amit Kucheria <amitk@ittc.ku.edu> wrote:
> Hi,
> 
> 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];
> };
> 
> Now from the userspace, I calloc() (num *
> sizeof(struct bar)) bytes to
> bartable, initialize it and call an ioctl. So
> essentially I have 'num'
> records of format 'struct bar' pointed to by
> 'bartable'. 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 but
> entry->mac_addr entries are zero.
> 
> 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?
> 
> TIA.
> Regards,
> Amit
> 
> -- 
> I'm an angel!!! Honest!
> The horns are just there to hold the halo up
> straight.
>
^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^-^
>                   Amit Kucheria
>           EECS Grad. Research Assistant
>          University of Kansas @ Lawrence
>    (R)+1-(785)-830 8521 ||| (O)+1-(785)-864 7774
> ____________________________________________________
> 
> --
> Kernelnewbies: Help each other learn about the Linux
> kernel.
> Archive:      
> http://mail.nl.linux.org/kernelnewbies/
> FAQ:           http://kernelnewbies.org/faq/
> 


__________________________________________________
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