gpt4 book ai didi

memory-management - 如何从Linux内核访问用户空间内存?

转载 作者:行者123 更新时间:2023-12-03 23:38:31 26 4
gpt4 key购买 nike

我知道copy_to_user/copy_from_userget_user/put_user函数是用于此目的的。

我的问题是,给定一个用户空间地址/指针,通常如何从内核访问该地址指向的数据?

我可以想象,首先我必须确保包含页面应该在物理内存中(而不是磁盘中)。

你下一步怎么做?我可以使用*p,其中p是指向某些用户空间数据的指针,直接指向该数据吗?

还是我必须首先调用kmap才能将包含的物理页面框架映射到内核虚拟地址空间?为什么?

最佳答案

您可能会发现这很有用。

Let us repeat that the buff argument to the read and write methods is a user-space pointer. Therefore, it cannot be directly dereferenced by kernel code. There are a few reasons for this restriction:

  • Depending on which architecture your driver is running on, and how the kernel was configured, the user-space pointer may not be valid while running in kernel mode at all. There may be no mapping for that address, or it could point to some other, random data.

  • Even if the pointer does mean the same thing in kernel space, user-space memory is paged, and the memory in question might not be resident in RAM when the system call is made. Attempting to reference the user-space memory directly could generate a page fault, which is something that kernel code is not allowed to do. The result would be an "oops," which would result in the death of the process that made the system call.

  • The pointer in question has been supplied by a user program, which could be buggy or malicious. If your driver ever blindly dereferences a user-supplied pointer, it provides an open doorway allowing a user-space program to access or overwrite memory anywhere in the system. If you do not wish to be responsible for compromising the security of your users' systems, you cannot ever dereference a user-space pointer directly.



资料来源: http://www.makelinux.net/ldd3/chp-3-sect-7

就是说,我自己很想知道如果用户空间地址确实有效,并且以上条件均不适用,该怎么办...

关于memory-management - 如何从Linux内核访问用户空间内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10509850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com