copy_to_user vs memcpy

前端 未结 1 1648
挽巷
挽巷 2020-12-30 08:35

I have always been told(In books and tutorials) that while copying data from kernel space to user space, we should use copy_to_user() and using memcpy() would cause problems

相关标签:
1条回答
  • 2020-12-30 08:54

    There are a couple of reasons for this.

    First, security. Because the kernel can write to any address it wants, if you just use a user-space address you got and use memcpy, an attacker could write to another process's pages, which is a huge security problem. copy_to_user checks that the target page is writable by the current process.

    There are also some architecture considerations. On x86, for example, the target pages must be pinned in memory. On some architectures, you might need special instructions. And so on. The Linux kernels goal of being very portable requires this kind of abstraction.

    0 讨论(0)
提交回复
热议问题