Changing user space memory protection flags from the kernel module

I am writing a kernel module that has access to a specific process memory. I did anonymous mapping in some user space memory with do_mmap():

#define MAP_FLAGS   (MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS)

prot = PROT_WRITE;
retval = do_mmap(NULL, vaddr, vsize, prot, MAP_FLAGS, 0);

vaddrand vsizeare installed earlier, and the call succeeds. After I write to this memory block from the kernel module (via copy_to_user), I want to remove the permission on it PROT_WRITE(for example, mprotectin normal user space). I can't seem to find a function that will allow this.

I tried to unmount a region and reassign it with the correct protection, but this resets the memory block, erasing all the data that I just wrote; the setup MAP_UNINITIALIZEDmay fix this, but from the man pages:

MAP_UNINITIALIZED (since Linux 2.6.33)

Do not clear anonymous pages. This flag is designed to improve the performance of embedded devices. This flag is only respected if the kernel was configured using the CONFIG_MMAP_ALLOW_UNINITIALIZED parameter. Due to security implications, this option is usually only allowed on embedded devices (that is, devices in which one control the contents of user memory).

therefore, although it can do what I want, it will not be very portable. Is there a standard way to accomplish what I suggested?

+5
source share
2 answers

get_user_pages() ( , , ), , kmap() ( , kernel_read()). copy_to_user(), . , , , , .

+1

mprotect, . , . . mm/protect.c.

0

All Articles