为什么必须使用 copy_to_user()/copy_from_user() 从内核访问用户空间? [英] Why do you have to use copy_to_user()/copy_from_user() to access user space from the kernel?
本文介绍了为什么必须使用 copy_to_user()/copy_from_user() 从内核访问用户空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很好奇,因为我在尝试直接访问内存后出现内核恐慌(然后我找到了这些函数).
I'm curious, because I got a kernel panic after trying to access memory directly (then I found these functions).
推荐答案
这些函数做了一些事情:
These functions do a few things:
- 他们检查提供的用户空间块是否完全在地址空间的用户部分 (
access_ok()
) - 这可以防止用户空间应用程序要求内核读/写内核地址;莉> - 如果任何地址不可访问,它们将返回一个错误,允许将错误返回到用户空间(
EFAULT
)而不是使内核崩溃(这是通过与页面的特殊合作来实现的)故障处理程序,它可以专门检测用户内存访问功能之一何时发生故障); - 它们允许架构特定的魔法,例如确保具有虚拟标记缓存的架构的一致性、禁用 SMAP 等保护或切换具有独立用户/内核地址空间的架构(如 S/390)上的地址空间.
- They check if the supplied userspace block is entirely within the user portion of the address space (
access_ok()
) - this prevents userspace applications from asking the kernel to read/write kernel addresses; - They return an error if any of the addresses are inaccessible, allowing the error to be returned to userspace (
EFAULT
) instead of crashing the kernel (this is implemented by special co-operation with the page fault handler, which specifically can detect when a fault occurs in one of the user memory access functions); - They allow architecture-specific magic, for example to ensure consistency on architectures with virtually-tagged caches, to disable protections like SMAP or to switch address spaces on architectures with separate user/kernel address spaces like S/390.
这篇关于为什么必须使用 copy_to_user()/copy_from_user() 从内核访问用户空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文