2005-05-05 12:41:25

by linux

[permalink] [raw]
Subject: copy_to_user question

Hi all, i have a question about copy_to_user function.
I have a module which declares a struct test { int size; char *name; int
value_add };
I like to transfer that struct to userspace through an ioctl command like
that:

struct test test_struct;
memset(&test_struct,0,sizeof(test_struct);
test_struct.size= 100;
test_struct.name = "test name";
test_struct.value_add = 2;
copy_to_user((void __user *)arg,&test_struct,sizeof(struct test));


When i use the ioctl command in user-space and try to get the name item a
segfault occurs.Can u tell me why??
Can we transfer from kernel-space to user-space pointers like the one i use
or this is a fault approach???


Best regards,
Chris.



2005-05-05 13:13:51

by linux-os (Dick Johnson)

[permalink] [raw]
Subject: Re: copy_to_user question

On Thu, 5 May 2005, linux wrote:

> Hi all, i have a question about copy_to_user function.
> I have a module which declares a struct test { int size; char *name; int
> value_add };
> I like to transfer that struct to userspace through an ioctl command like
> that:
>
> struct test test_struct;
> memset(&test_struct,0,sizeof(test_struct);
> test_struct.size= 100;
> test_struct.name = "test name";
> test_struct.value_add = 2;
> copy_to_user((void __user *)arg,&test_struct,sizeof(struct test));
>
>
> When i use the ioctl command in user-space and try to get the name item a
> segfault occurs.Can u tell me why??
> Can we transfer from kernel-space to user-space pointers like the one i use
> or this is a fault approach???
>
>
> Best regards,
> Chris.

Accessing a kernel pointer from user-space isn't going to work.
But you can transfer data to/from user-space/kernel-space so
just don't use a pointer, do...

struct test struct {
char name[BIG_ENOUGN];
int size;
....
};


Write your strings to the name[] buffer-member and everybody is happy.


Cheers,
Dick Johnson
Penguin : Linux version 2.6.11 on an i686 machine (5537.79 BogoMips).
Notice : All mail here is now cached for review by Dictator Bush.
98.36% of all statistics are fiction.