Bad Address Error from copy_to_user

I am trying to copy a custom structure from kernel space to user space. inside user space, errno returns a "bad address". What is the common cause of an invalid address error?

if(copy_to_user(info, &kernel_info, sizeof(struct prinfo)))
+3
source share
3 answers

A Bad Address error means that the address you entered is not valid. In case you are already higher, I would suggest that this is because you are passing a copy infoinstead of a pointer to the memory location info.

Looking at documents is copy_to_userdefined as

copy_to_user(void __user * to, const void * from, unsigned long n);

So, if your variable is infonot specified, I would update your code:

if(copy_to_user(&info, &kernel_info, sizeof(struct prinfo)) ) {
    //some stuff here i guess
}
+5
source

, , , , , .

+2

I had the same problem when writing a small char driver. All I did wrong is the copy_to_user function returns a non-zero value on failure, so you need to do this.

if(copy_to_user(void *userbuf, void *kernelbuf, long len))
    return -EFAULT;

// Continue with code..
-1
source

Source: https://habr.com/ru/post/1696856/


All Articles