mprotect() like functionality within Linux kernel
Asked Answered
E

2

8

I am in a Linux kernel module, and I allocate some memory with, say, vmalloc(). I want to make the memory have read, write, and execute permission. What is the clean and appropriate way of doing that? Basically, this is generally the equivalent of calling mprotect(), but in kernel space.

If I do the page walk, pgd_offset(), pud_offset(), pmd_offset(), pte_offset_map(), and then pte_mkwrite(), I run into linking errors when I tried it on 2.6.39. Also, it seems that if I am doing the page walk, it is a hack, and there ought to be a cleaner and more appropriate method.

My kernel module will be a loadable module, so internal symbols are not available to me.

Thanks, in advance, for your guidance.

Extraneous answered 16/7, 2013 at 3:1 Comment(0)
F
5

There is a good answer to this question here: https://unix.stackexchange.com/questions/450557/is-there-any-function-analogous-to-mprotect-in-the-linux-kernel.

asm-generic/set_memory.h:int set_memory_ro(unsigned long addr, int numpages);
asm-generic/set_memory.h:int set_memory_rw(unsigned long addr, int numpages);
asm-generic/set_memory.h:int set_memory_x(unsigned long addr, int numpages);
asm-generic/set_memory.h:int set_memory_nx(unsigned long addr, int numpages);

they are defined here: https://elixir.bootlin.com/linux/v4.3/source/arch/x86/include/asm/cacheflush.h#L47

Finfoot answered 5/2, 2019 at 20:21 Comment(0)
R
0

Have you tried by invoking do_mprotect() [kernel function corresponding to mprotect()] directly ?

Rendarender answered 23/7, 2017 at 21:59 Comment(1)
__do_sysmprotect is a static function. Not sure if it can be called in modules.Ponceau

© 2022 - 2024 — McMap. All rights reserved.