How to access user space memory from the Linux kernel?

后端 未结 4 1721
我寻月下人不归
我寻月下人不归 2021-02-01 10:20

I know that copy_to_user/copy_from_user, get_user/put_user functions are for this purpose.

My question is that, given

4条回答
  •  失恋的感觉
    2021-02-01 10:29

    The pointer alone is not enough! You need to know which process that pointer "belongs" to.

    When the process gets preempted, the pointer points into the address space of another process. The address may not be mapped any more, yadda yadda,

    If that process will be the current process when you access the data, then you should use the copy_to_user/copy_from_user functions.

    If the process may be scheduled out, you can try to mlock() the page in RAM and find out which is the physical ram address of the page. Whenever you want to access it, you map that physical page into a kernel virtual address.

    NOTE:

    • A malicious process can munlock() the page and trick you into accessing a wrong RAM page.
    • I'm not sure mlock() semantics demand the underlining RAM page MUSTN'T change.
    • the kernel should be able to lock a page into RAM, I'm not familiar with the mm subsystem.

提交回复
热议问题