views:

37

answers:

2

Hi, all,

I have a problem with get_user() macro. What I did is as follows:

I run the following program

int main() 
{ 
    int a = 20; 
    printf("address of a: %p", &a); 
    sleep(200); 
    return 0; 
} 

When the program runs, it outputs the address of a, say, 0xbff91914.

Then I pass this address to a module running in Kernel Mode that retrieves the contents at this address (at the time when I did this, I also made sure the process didn't terminate, because I put it to sleep for 200 seconds... ):

The address is firstly sent as a string, and I cast them into pointer type.

int * ptr = (int*)simple_strtol(buffer, NULL,16); 
printk("address: %p",ptr); // I use this line to make sure the cast is correct. When running, it outputs bff91914, as expected. 
int val = 0; 
int res; 
res= get_user(val, (int*) ptr); 

However, res is always not 0, meaning that get_user returns error. I am wondering what is the problem....

Thank you!!

-- Fangkai

+2  A: 

That is probably because you're trying to get value from a different user space. That address you got is from your simple program's address space, while you're probably using another program for passing the value to the module, aren't you?

Fyodor Soikin
Well, I tried not to pass the address to module from any other program. What I do now is: after output the address of the variable, I change the code of the module, set the ptr to the address, and recompile, reload the module again. I make sure that the simple program is still running. However, get_user still returns error...
Fangkai Yang
Yes, I understand what you meant... when I refer user space, the current->pid is pointing to another process, therefore it is in a different context... How to fix this problem?
Fangkai Yang
You have to arrange things so that you can do the `get_user()` when `current` is the target process - for example, after sleeping you could have your target process read a file in `/proc` created by your module, and do the `get_user` in the proc file's read routine.
caf
Yes. The point is, your process must *somehow* call into your module. Then your module's code will be executed in that process's context. Reading a file created by the module would be one of ways to do it.
Fyodor Soikin
A: 

The call to get_user must be made in the context of the user process.

Since you write "I also made sure the process didn't terminate, because I put it to sleep for 200 seconds..." I have a feeling you are not abiding by that rule. For the call to get_user to be in the context of the user process, you would have had to make a system call from that process and there would not have been a need to sleep the process.

So, you need to have your user process make a system call (an ioctl would be fine) and from that system call make the call to get_user.

R Samuel Klatchko
Fangkai Yang