Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use copy_to_user

I'm trying to add a custom system call into the linux kernel. Here is a simple code:

#include <linux/mysyscall.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>
#include <asm/system.h>

asmlinkage int sys_mysyscall(int *data){

    int a = 3;

    cli();
    copy_to_user(data, &a, 1);
    sti();

    printk(KERN_EMERG "Called with %d\n", a);

    return a;
}

I can compile a kernel with mysyscall added and when I try to access it with a user program like:

#include <linux/mysyscall.h>

int main(void){

    int *data;
    int r;
    int a = 0;
    data = &a;

    r = mysyscall(data);

    printf("r is %d and data is %d", r, *data);
}

*data does not equal to 3 it equals to 0.

How should I use copy_to_user to fix it?

like image 799
yildizabdullah Avatar asked Nov 17 '13 14:11

yildizabdullah


People also ask

What is Copy_to_user in Linux?

The copy_to_user function copies a block of data from the kernel into user space. This function accepts a pointer to a user space buffer, a pointer to a kernel buffer, and a length defined in bytes. The function returns zero on success or non-zero to indicate the number of bytes that weren't transferred.

How do I copy data from user space to kernel space?

You can use the copy_from_user() and copy_to_user() functions to move data between kernel space and user space.

What does Copy_from_user return?

Returns number of bytes that could not be copied. On success, this will be zero. If some data could not be copied, this function will pad the copied data to the requested size using zero bytes. An alternate version - __copy_from_user_inatomic - may be called from atomic context and will fail rather than sleep.

What is __ user in C?

__user is used to mark a pointer as userspace, as in, to indicate that the pointer exists in userspace and that it should not be dereferenced.


1 Answers

The copy to user line of code copies only one byte from 'a'. In case of little endian systems it is going to be 0. Copy all the 4 bytes to get the correct result.

like image 91
prasannatsm Avatar answered Oct 03 '22 06:10

prasannatsm