user1272246
user1272246

Reputation:

The implementation of copy_from_user()

I am just wondering why does copy_from_user(to, from, bytes) do real copy? Because it just wants kernel to access user-space data, can it directly maps physical address to kernel's address space without moving the data? Thanks,

Upvotes: 1

Views: 4634

Answers (4)

Sorcrer
Sorcrer

Reputation: 1644

"Before this it's better to know why copy_from_user() is used"

Because the Kernel never allow a user space application to access Kernel memory directly, because if the memory pointed is invalid or a fault occurs while reading, this would the kernel to panic by just simply using a user space application.

"And that's why!!!!!!"

So while using copy_from_user is all that it could create an error to the user and it won't affect the kernel functionality

Even though it's an extra effort it ensures the safe and secure operation of Kernel

Upvotes: 1

ritesh ghosh
ritesh ghosh

Reputation: 1

one of the major requirement in system call implementation is to check the validity of user parameter pointer passed as argument, kernel should not blindly follow the user pointer as the user pointer can play tricks in many ways. Major concerns are: 1. it should be a pointer from that process address space - so that it cant get into some other process address space. 2. it should be a pointer from user space - it should not trick to play with a kernel space pointer. 3. it should not bypass memory access restrictions.

that is why copy_from_user() is performed. It is blocking and process sleeps until page fault handler can bring the page from swap file to physical memory.

Upvotes: 0

Bibhu Mohapatra
Bibhu Mohapatra

Reputation: 17

copy_from_user() does a few checks before it starts copying data. Directly manipulating data from user-space is never a good idea because it exists in a virtual address space which might get swapped out.

http://www.ibm.com/developerworks/linux/library/l-kernel-memory-access/

Upvotes: 1

nickolayratchev
nickolayratchev

Reputation: 1206

copy_from_user() is usually used when writing certain device drivers. Note that there is no "mapping" of bytes here, the only thing that is happening is the copying of bytes from a certain virtual location mapped in user-space to bytes in a location in kernel-space. This is done to enforce separation of kernel and user and to prevent any security flaws -- you never want the kernel to start accessing and reading arbitrary user memory locations or vice-versa. That is why arguments and results from syscalls are copied to/from the user before they actually run.

Upvotes: 2

Related Questions