Howto pass pointer to char parameter to kernel via copy_to_user()

669 views
Skip to first unread message

Dwi Sasongko Supriyadi

unread,
Jul 18, 2010, 10:39:11 PM7/18/10
to Android Linux Kernel Development
Hello,

I tried to pass "string" parameter from user to kernel and the driver
never get the parameter correctly.

I have read following link as a reference:

http://tldp.org/LDP/lkmpg/2.6/html/x892.html

Then I modify get_user() with copy_from_user() as follow:

char *tmp = kmalloc(sizeof(char), GFP_KERNEL);

if (copy_from_user(tmp, (unsigned long *) param), sizeof (tmp)) {
return -EFAULT;
}

It always failed to copy user data. Did I miss anything?

In addition, I also tried it on embedded system to pass int and struct
data types with success but passing string always failed (I use 2.6.28
kernel on MIPS 4KSd family). What is the constraint on passing
parameter from user to kernel especially for "string" data type? Is it
a must to pass it via write() file operations?

I appreciate any comments or suggestions, Thanks in advance.

Regards,
DWI
--
"A mathematician is a device for turning coffee into theorems." - Paul Erdos

Contacts:
+62 857 8038 8298
+62 813 9876 6576

Skype: dwi.sasongko
GTalk: ruckuus

Danke Xie

unread,
Jul 21, 2010, 12:25:47 AM7/21/10
to Android Linux Kernel Development
Hi Dwi,

I couldn't really figure out what is being done in the code. But I
think I noticed a reason why the call might fail (though not 100%
sure). You allocated one byte with kmalloc, which is sizeof(char), but
you would be copying 4 bytes into the area, that is sizeof(tmp). I
think that's why you got a seg fault. Do you think so?

Regards,
Danke

Dwi Sasongko Supriyadi

unread,
Jul 21, 2010, 11:56:39 AM7/21/10
to android...@googlegroups.com
Hi,

Thanks Abhinav and Danke. I found the issue:

Both of you are right, I wrongly allocate the memory. I never try to
pass any string before (so pity) ... I am trapped in the equality of
sizeof(int) and sizeof(*int). Different for pointer to char, the
sizeof() must not be used, instead using length of string as
parameter.

Regards,
DWI

> --
> unsubscribe: android-kerne...@googlegroups.com
> website: http://groups.google.com/group/android-kernel

Reply all
Reply to author
Forward
0 new messages