[英]How to allocate an executable page in a Linux kernel module?
I'm writing a Linux kernel module, and I'd like to allocate an executable page.我正在编写一个 Linux 内核模块,我想分配一个可执行页面。 Plain kmalloc()
returns a pointer within a non-executable page, and I get a kernel panic when executing code there.普通的kmalloc()
返回一个不可执行页面内的指针,并且在那里执行代码时出现内核恐慌。 It has to work on Ubuntu Karmic x86, 2.6.31-20-generic-pae.它必须在 Ubuntu Karmic x86、2.6.31-20-generic-pae 上工作。
#include <linux/vmalloc.h>
#include <asm/pgtype_types.h>
...
char *p = __vmalloc(byte_size, GFP_KERNEL, PAGE_KERNEL_EXEC);
...
if (p != NULL) vfree(p);
/**
* vmalloc_exec - allocate virtually contiguous, executable memory
* @size: allocation size
*
* Kernel-internal function to allocate enough pages to cover @size
* the page level allocator and map them into contiguous and
* executable kernel virtual space.
*
* For tight control over page level allocator and protection flags
* use __vmalloc() instead.
*
* Return: pointer to the allocated memory or %NULL on error
*/
void *vmalloc_exec(unsigned long size)
{
return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL_EXEC,
NUMA_NO_NODE, __builtin_return_address(0));
}
Linux 5.4 and above no longer make interfaces available so that arbitrary kernel modules won't modify page attributes to turn the executable bit on/off. Linux 5.4及更高版本不再提供接口,因此任意内核模块不会修改页面属性来打开/关闭可执行位。 However, there might be a specific configuration that will allow to do so, but I'm not aware of such.但是,可能有一个特定的配置允许这样做,但我不知道。
If someone uses a kernel version that is lower than 5.4, you can use set_memory_x(unsigned long addr, int numpages);
如果有人使用低于 5.4 的内核版本,可以使用set_memory_x(unsigned long addr, int numpages);
and friends to change the attributes of a page, or if you need to pass a struct page
, you could use set_pages_x
.和朋友改变页面的属性,或者如果你需要传递一个struct page
,你可以使用set_pages_x
。
Keep in mind that it is considered dangerous and you have to know what you are doing.请记住,这被认为是危险的,您必须知道自己在做什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.