【问题标题】:mprotect() like functionality within Linux kernelLinux 内核中类似 mprotect() 的功能
【发布时间】:2013-07-14 02:08:19
【问题描述】:

我在一个 Linux 内核模块中,我分配了一些内存,例如 vmalloc()。我想让内存有读、写和执行权限。这样做的干净和适当的方法是什么?基本上,这通常相当于调用mprotect(),但在内核空间中。

如果我进行页面遍历,pgd_offset()pud_offset()pmd_offset()pte_offset_map(),然后是pte_mkwrite(),当我在 2.6.39 上尝试时遇到链接错误。另外,如果我在做page walk,这似乎是一种hack,应该有一个更干净、更合适的方法。

我的内核模块将是一个可加载模块,因此我无法使用内部符号。

提前感谢您的指导。

【问题讨论】:

    标签: linux kernel mprotect


    【解决方案1】:

    这里有一个很好的答案: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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • __do_sysmprotect 是一个静态函数。不确定是否可以在模块中调用。
      猜你喜欢
      • 2013-12-19
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多