用户函数不能访问内核空间,而且用户函数进行系统调用的参数也是经过寄存器进行传递,那么内核能不能访问用户空间呢?
内核函数是有访问用户空间的要求:
- 如果一个系统调用需要超过6个参数,它们就只能借助进程内存空间中的C结构实例进行传递。系统调用将借助寄存器,将指向该结构实例的一个指针传递给内核。
- 由系统调用的副效应产生的大量数据,必须通过指定的内存区交换数据。当然,该内存区必须在用户空间中,使得用户应用程序能够访问。
但是内核函数却不能直接
解引用用户空间的指针,基于以下原因:
- 用户空间的指针指向的地址可能在内核运行期间无效
- 即使指针有效但是指针指向的数据却不驻留在内存中,就会造成缺页异常
- 指针可能由于程序的恶意或错误产生,直接访问就会造成系统的不稳定
内核可以通过以下函数间接的访问用户空间:
access_ok | Checks the validity of the user space memory pointer |
get_user | Gets a simple variable from user space |
put_user | Puts a simple variable to user space |
clear_user | Clears, or zeros, a block in user space |
copy_to_user | Copies a block of data from the kernel to user space |
copy_from_user | Copies a block of data from user space to the kernel |
strnlen_user | Gets the size of a string buffer in user space |
strncpy_from_user | Copies a string from user space into the kernel |
参考: