dkb
dkb

Reputation: 561

copy_from_user gives null pointer

I'm attempting to write an Open RG kernel module that, at given intervals, sends a message up to the user space. To this end, I need the kernel to hold a pointer to a static buffer in the user space where this message will be stored. I'm having trouble sending the pointer to the kernel.

The user space function call is something like this (simplified, obviously):

typedef struct {
 char msg[MAX_BOOT_MSG];
} msg_t;
static msg_t common_mem;

void user_space_func() {
    openrg_module_ctrl(KOS_CDT_TEST, TEST_IOCTL_SET_COMMON_MEM, &common_mem.msg);
}

The kernel space usage is like this:

static void* msg_write;
static int do_ioctl(kos_chardev_t *context, unsigned int cmd, 
unsigned long data) {

switch (cmd)
     {
case TEST_IOCTL_SET_COMMON_MEM:
        received_ioctl = 1;
        int ret = copy_from_user(&msg_write, (void *)data, sizeof(char*));
        printk("setting common mem to %p, received %d\n", msg_write, ret);
        return 0;
}
    default:
    return -1;
}

The output is setting common mem to 0000000000000000, received 0. I see that common_mem.msg isn't NULL. Any idea what I'm doing wrong?

Upvotes: 1

Views: 1009

Answers (1)

CL.
CL.

Reputation: 180020

data is the address of the buffer, so by reading from that address, you are copying the contents of the buffer.

Please note that memory in user space can be moved or swapped out, so this address is valid only for the duration of the system call; you must not store the address for later usage. Better allocate some memory in your driver, and allow the application to access it with mmap.

Upvotes: 3

Related Questions