ccrama
ccrama

Reputation: 754

"Bad Address" error when executing copy_to_user from a Linux kernel module

I am implementing a kernel module (version 2.6.32) that can write to /proc, and allows writing and reading from/to the user space and kernel space. My write to kernel space code is working perfectly, but when trying to copy_to_user, I get a message that it has a "bad address".

I am running echo "test" > procmodule (the name of my module) and cat procmodule to retrieve the string from the module memory. The result of the second command is

cat: procmodule: Bad address

Below is the relevant code:

int read_info( char *page , char **start, off_t off, int count, int *eof, void *data) {    
    if(copy_to_user(page, info, count)){
     return -EFAULT;
    }
    return count;
}

Page is the address of the user buffer, and info is the char array that currently holds the string I want to print. Count is the length of this string.

What I've tried:

Any help would be greatly appreciated, please let me know if I'm missing anything obvious (much googling leads to code that seems to be implemented identically to mine).

Upvotes: 1

Views: 2195

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 66118

The first argument to .read_proc function (of type read_proc_t) is actually in-kernel address. You may write to this address directly, and shouldn't use copy_to_user for that.


You, probably, confuse your function with .read function in file_operations structure. That function has signature

ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);

and its second argument is actually pointer to the user-space (thus it has __user specifier), for which copy_to_user should be used.

Note, that the first argument for your function doesn't have __user specifier:

typedef int (read_proc_t)(char *page, char **start, off_t off,
          int count, int *eof, void *data);

Upvotes: 0

Related Questions