Linux Kernel: copy_from_user - struct with pointers

前端 未结 2 1909
既然无缘
既然无缘 2021-01-17 16:53

I\'ve implemented some kind of character device and I need help with copy_ from_user function.

I\'ve a structure:

struct  my_struct{

int a;

int *b;         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-17 17:20

    You must always use copy_from_user and similar to access user space memory from kernel space, regardless of how you got the pointer. Since b is a pointer to user space memory, you must use copy_from_user to access it.

    These functions do two important additional tasks:

    1. They make sure the pointer points into user space and not kernel space. Without this check, user space programs might be able to read or write to kernel memory, bypassing normal security.
    2. They handle page faults correctly. Normally a page fault in kernel mode will result in an OOPS or panic - the copy_*_user family of functions have a special override that tells the PF handler that all is well, and the fault should be handled normally; and in the event that the fault cannot be satisfied by IO (ie, what would normally cause a SIGSEGV or SIGBUS), return an error code instead so their caller can do any necessary cleanup before returning to userspace with -EFAULT.

提交回复
热议问题