Linux内核中类似mprotect()的功能

8
我在 Linux 内核模块中使用了例如 vmalloc() 的函数分配一些内存。我想让这个内存拥有读、写和执行权限。请问,有何干净且合适的方式来实现这一点呢?通常情况下,这相当于在内核空间调用 mprotect()
如果我使用页面遍历(page walk)的方法,包括 pgd_offset()pud_offset()pmd_offset()pte_offset_map() 以及 pte_mkwrite(),在尝试在2.6.39版本上使用时会出现链接错误。此外,如果我采用页面遍历的方式,就属于 hack 的范畴,而应该存在更加干净和合适的方法。
我的内核模块将作为一个可加载模块,因此无法获取内部符号。
提前感谢您的指导。
2个回答

5
这个问题有一个很好的答案,可以在这里找到: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);

它们在这里被定义:https://elixir.bootlin.com/linux/v4.3/source/arch/x86/include/asm/cacheflush.h#L47


0
你尝试过直接调用do_mprotect()(对应于mprotect()的内核函数)吗?

__do_sysmprotect 是一个静态函数。不确定它是否可以在模块中调用。 - Bruce Shen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接