Re: Problem with copy_from_user

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

 



Laurent Nadeau wrote:
> 
> rt_instance is an instance of a structure in user space.
> rt_u is a variable in user space which points to rt_instance.
> rt_k is a variable in kernel space which I want to use to read rt_instance,
> hence, it should point to rt_instance too.
> 
> The following code is a system call which I wrote, which basically passes
> the address of rt_instance to the kernel by copying rt_u to rt_u . Then, I
> use copy_from_user to copy rt_instance to a local variable (local_rt) in
> kernel space. Then, I read the content of local_rt and the code works fine:
> n = 0, and local_rt.session_list shows the correct value.
> 
> asmlinkage int sys_asimcast(struct routing_table *rt_u) {
> 	rt_k = rt_u;

This seems like a pointless assignment, just s/rt_k/rt_u/ below.

> 	if (rt_k != NULL) {
> 		struct routing_table local_rt;
> 		int n = copy_from_user(&local_rt, rt_k, sizeof(struct
> routing_table));
> 		printk("rt_k.session_list = %d; n = %d.\n",
> (int)local_rt.session_list, n);
> 	}
> 	return 2;
> }
> 
> Now, I inserted the same code into ip_rcv function (/net/IPv4/ip_input.c,
> right before the return statement) to read again the rt_instance, using the
> same pointer rt_k.
> copy_from_user returns 0 (means that it could read the structure properly)
> BUT the data which has been read is incorrect.
> 
> if (rt_k != NULL) {
> 	struct routing_table local_rt;
> 	int n = copy_from_user(&local_rt, rt_k, sizeof(struct
> routing_table));	printk("local_rt.session_list = %d; n = %d.\n",
> (int)local_rt.session_list, n);
> }
> 
> The bottom line is: the same code, written in the system call works, but
> doesn't work in the kernel. Whenever I read the structure from the sys call,
> it is read correctly, while from the ip_rcv function, it is not.
> BTW, I made sure that rt_k is pointing to the same address in both cases (a
> print statement showed the same value).

Isn't ip_rcv NOT called in process context?

> I am wondering if this problem has to do with the fact that copy_from_user
> is user context only?

That would be my call.

>    I am not sure exactly what this means. Should I change
> the context and how?

No.  You need to redesign your code so you don't try to access
user-space memory when from code that is run when not in process context.


--
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