Puffy
Puffy

Reputation: 401

How to reserve memory in the same offset of the kernel module

when I use kmalloc() to reserve memory from my kernel module, I'm getting an address that starts by 0xffff (ffff9cf010feb000). But the module starts at 0xffffffff (ffffffffc01a6000).

I'm trying to relocate code from the module (ffffffffc01a6000), in a new reserved virtual memory address. But I can not relocate call offsets because the 32bit offset is not enough to reference from the new area (ffff9cf010feb000), to the kernel functions (0xffffffff....).

How can I reserve memory to be able to call from the new virtual address? Or how can I call from the new virtual address to the other virtual address?

Touching the compiler's ASM code would be a hard way. And making some mov address, rax; call *rax trick with some macros/functions maybe would be a solution, but I don't know how exactly do it.

Thanks!

Upvotes: 1

Views: 218

Answers (1)

Puffy
Puffy

Reputation: 401

I found a way in linux-source/arch/x86/kernel/module.c:

void *module_alloc(unsigned long size)
{
    void *p;

    if (PAGE_ALIGN(size) > MODULES_LEN)
        return NULL;

    p = __vmalloc_node_range(size, MODULE_ALIGN,
                    MODULES_VADDR + get_module_load_offset(),
                    MODULES_END, GFP_KERNEL,
                    PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
                    __builtin_return_address(0));
    if (p && (kasan_module_alloc(p, size) < 0)) {
        vfree(p);
        return NULL;
    }

    return p;
}

Upvotes: 1

Related Questions