Skip to content
Advertisement

Pointers given from user space to kernel space are null

Sorry if this question has been asked before as it seems kind of basic, but I can’t find it anywhere on this site. I’m doing OS programming with linux (debian) and I’m trying to give a pointer from my user space to my kernel space function.

Namely in the userspace I do

    ...
    long res;
    int x = -1;
    int *p1 = &x;  // also tried doing a malloc, didn't change anything
    res = kernel_function(p1);
    ...

and in the kernel space file kernel_function.c I do

asmlinkage long kernel_function( int __user *p){
    printk("%p n", (void*) p);
    return 0;
}

I also did the usual other steps, like adding the prototype of the function to syscalls.h, adding the file name to syscall_32.tbl and such. But when I run my user space function the only thing that gets printed to the kernel log is (NULL). So the function does get called, but the pointer is lost, any idea why this happens ?

Advertisement

Answer

You didn’t look at any other Linux kernel functions?

You cannot just pass a pointer. That would be horribly unsafe.

You are required to use copy_from_user to get data into kernel space.

There seem to be some answers already on SO like this one: how is the correct way to use copy_from_user?

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement