Skip to content

Instantly share code, notes, and snippets.

@DispatchCode
Created September 19, 2026 17:18
Show Gist options
  • Select an option

  • Save DispatchCode/c61e1c1428e5d84d12a09259e466ce68 to your computer and use it in GitHub Desktop.

Select an option

Save DispatchCode/c61e1c1428e5d84d12a09259e466ce68 to your computer and use it in GitHub Desktop.
Linux Virtual Memory: Walking Page Tables with a Custom Syscall

When a user-space application accesses a virtual memory address, that address belongs to the address space of the corresponding process. Each user-space task has its own address space: the translation from a virtual address to a physical address starts from the CR3 value. Using this syscall, it is possible to read the memory of another running task.

This is intended for educational purposes only, so the code is by no means production-ready. It could also be interesting to add a syscall and walk the address space :)

Add a new syscall

I created a new folder called memory. This is where the source code will be placed. Kbuild also needs to be updated by adding the following line:

obj-y                  += memory/

I placed this line after "kernel/".

Optionally, you can create a configuration file called Kconfig inside the memory/ folder and add the following line:

source "memory/Kconfig"

to the Kconfig file in the root directory of the kernel source tree.

Now it is time to add the source code. I placed the include file under include/linux/vminfo.h:

struct vminfo_struct {
    unsigned long pgd;
    unsigned long p4d;
    unsigned long pud;
    unsigned long pmd;
    unsigned long pte;
};

The source file inside the memory/ folder is:

#include <linux/syscalls.h>
#include <linux/mm.h>
#include <linux/kernel.h>
#include <linux/vminfo.h>
#include <linux/pgtable.h>
#include <linux/slab.h>
#include <linux/highmem.h>

#include <asm/processor.h>
#include <asm/io.h>
#include <asm/page.h>


static int bad_address(void *p)
{
        unsigned long dummy;

        return get_kernel_nofault(dummy, (unsigned long *)p);
}

static int get_pte(struct mm_struct *mm, unsigned long address, struct vminfo_struct *vminfo_kern, pte_t *pte_v) {
        pgd_t *pgd;
        p4d_t *p4d;
        pud_t *pud;
        pmd_t *pmd;
        pte_t *pte;

        pgd = pgd_offset(mm, address);

        if(pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
                return -EINVAL;

        p4d = p4d_offset(pgd, address);
        if(p4d_none(*p4d) || unlikely(p4d_bad(*p4d)))
                return -EINVAL;

        pud = pud_offset(p4d, address);
        if(pud_none(*pud) || unlikely(pud_bad(*pud)))
                return -EINVAL;

        pmd = pmd_offset(pud, address);
        if(pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
                return -EINVAL;

        pte = pte_offset_kernel(pmd, address);
        if(bad_address(pte))
                return -EINVAL;

        vminfo_kern->pgd = pgd_val(*pgd);
        vminfo_kern->p4d = p4d_val(*p4d);
        vminfo_kern->pud = pud_val(*pud);
        vminfo_kern->pmd = pmd_val(*pmd);
        vminfo_kern->pte = pte_val(*pte);

        *pte_v = *pte;

        pr_info("get_pte completed");

        return 0;
}

static int read_at_page_offset(unsigned long address, pte_t pte_v, void __user *buffer, unsigned int buff_len) {
        pr_info("Try to read memory");

        size_t offset;
        void *kern_buffer;
        void *kaddr;

        kern_buffer = kmalloc(buff_len, GFP_KERNEL);

        kaddr = kmap_local_page(pfn_to_page(pte_pfn(pte_v)));
        if(!kaddr) {
                return -ENOMEM;
        }

        offset = address & (PAGE_SIZE -1);
        pr_info("offset %lu", offset);

        memcpy(kern_buffer, kaddr + offset, buff_len);
        kunmap_local(kaddr);

        if(copy_to_user(buffer, kern_buffer, buff_len)) {
                kfree(kern_buffer);
                return -EFAULT;
        }

        pr_info("copy_to_user of the buffer worked successfully");

        kfree(kern_buffer);

        return 0;
}

SYSCALL_DEFINE5(vminfo, unsigned long, address, pid_t, pid, struct vminfo_struct __user *, vminfo, void __user *, buffer, unsigned int, buff_len) {
        pr_info("vminfo SYSCALL, pid %d address %lu",pid,address);

        struct vminfo_struct vminfo_tmp;
        struct task_struct *task;
        pte_t pte_v;
        int err;

        task = pid_task(find_vpid(pid), PIDTYPE_PID);
        if(!task) {
                err = -ESRCH;
                goto error;
        }

        pr_info("Task status: %u", task->__state);
        get_task_struct(task);

        if(get_pte(task->mm, address, &vminfo_tmp, &pte_v)) {
                err = -EINVAL;
                goto clean_error;
        }

        if(read_at_page_offset(address, pte_v, buffer, buff_len)) {
                err = -ENOMEM;
                goto clean_error;
        }

        put_task_struct(task);

        if(copy_to_user(vminfo, &vminfo_tmp, sizeof(vminfo_tmp))) {
                err = -EFAULT;
                goto error;
        }

        return 0;

clean_error:
        put_task_struct(task);

error:
        pr_err("SYSCALL_VMINFO error: %d", err);
        return err;
}

Edit kernel/sys_ni.c and add COND_SYSCALL(vminfo);. Create a file named Kconfig and add the following content (you can change the description as needed):

menu "Hack the process address space"

config VMINFO_SYSCALL
       prompt "Hack the process address space"
       def_bool y
       help
         [add here your text]

endmenu

Now add the syscall to the x86 syscall table, arch/x86/entry/syscalls/syscall_64.tbl:

548    64      vminfo                  sys_vminfo

Then add the declaration to include/linux/syscalls.h:

asmlinkage long sys_vminfo(unsigned long address, pid_t pid, struct vminfo_struct __user *vminfo, void __user *buffer, unsigned int buff_len);

Create a Makefile under the memory/ directory and add:

obj-$(CONFIG_VMINFO_SYSCALL) += vminfo.o

That's it.

Sources used as a test

We now need two processes: one that is running and waiting for input, and another one that reads a memory location. I chose to read an array of data. The first program is:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>

int main(int argc, char *argv[]) {
        if(argc != 2) {
                printf("Usage: ./test <n_of_elements>");
                return -1;
        }

        srand(time(NULL));
        
        int n_elements = atoi(argv[1]);
        int malloc_size = n_elements * sizeof(int);

        int *buffer = malloc(malloc_size);
        memset(buffer, 0, malloc_size);

        printf("PID: %d\n", getpid());
        printf("address of buffer: %ld, buffer size: %d, n_elements: %d\n", buffer, malloc_size, n_elements);

        for(int i=0; i < n_elements; i++) {
                buffer[i] = rand() % 100;
                printf("[%d] = %d\n", i, buffer[i]);

                char dummy;
                scanf("%c", &dummy);
        }

        free(buffer);

        return 0;

}

The next program, vminfo, needs some information from the previous program as command-line arguments:

#include <stdio.h>
#include <stdlib.h> 
#include <errno.h> 

#include <linux/kernel.h> 
#include <sys/syscall.h> 
#include <unistd.h> 
#include <linux/unistd.h> 
#include <linux/types.h>

struct vminfo_struct {
        unsigned long pgd;
        unsigned long p4d;
        unsigned long pud;
        unsigned long pmd;
        unsigned long pte;
};


int main(int argc, char *argv[]) {
        if(argc != 5) {
                printf("Usage: ./vminfo <pid> <address> <len_buffer> <n_elements>\n");
                exit(1);
        }

        struct vminfo_struct vminfo;
        pid_t pid;
        unsigned long address;
        // Size of the variable to access
        int buffer_len, n_elements;

        pid =  atoi(argv[1]);
        address = atol(argv[2]);
        buffer_len = atoi(argv[3]);
        n_elements = atoi(argv[4]);
        
        // stack allocation, but will be the same with malloc
        void *buffer = malloc(buffer_len);
        
        printf("[ pid: %d, addr: %lu, 0x%llx, size of data: %d, n_elements %d]\n\n", pid, address, address, buffer_len, n_elements);
        printf("Starting Page Structures walk...\n\n");

        char dummy;
        int *buff = (int*) buffer;

        while(1) {
                int ret = syscall(548, address, pid, &vminfo, buffer, buffer_len); 

                printf("PGD: 0x%llx P4D: 0x%llx PUD: 0x%llx PMD: 0x%llx PTE: 0x%llx\n",
                        vminfo.pgd, vminfo.p4d, vminfo.pud, vminfo.pmd, vminfo.pte);
       
                printf("Buffer content:\n");
                for(int i=0; i<n_elements; i++) {
                        printf("[%d] = %d\n", i, buff[i]);
                }

                printf("\nRET syscall: %d, errno: %d\n\n", ret, errno);
                printf("Enter to keep reading the address, or CTRL+C");
                scanf("%c",&dummy);
        }

        return 0; 
} 

Run the test

You can use a VM or boot the modified kernel. I am using virtme-ng here and built the kernel with vng -b for simplicity. Here is the output:

user@localhost:~/kernel/linux/linux-6.8> vng
          _      _
   __   _(_)_ __| |_ _ __ ___   ___       _ __   __ _
   \ \ / / |  __| __|  _   _ \ / _ \_____|  _ \ / _  |
    \ V /| | |  | |_| | | | | |  __/_____| | | | (_| |
     \_/ |_|_|   \__|_| |_| |_|\___|     |_| |_|\__  |
                                                |___/
   kernel version: 6.8.0-1-default-virtme x86_64
   (CTRL+d to exit)

user@virtme-ng:~/kernel/linux/linux-6.8> cd ../../
user@virtme-ng:~/kernel> mkdir data && mkfifo data/in
user@virtme-ng:~/kernel> sleep infinity > data/in &
[1] 442
user@virtme-ng:~/kernel> ./test 10 < data/in &
[2] 448
user@virtme-ng:~/kernel> PID: 448
address of buffer: 24801952, buffer size: 40, n_elements: 10
[0] = 37

I will put the ./test task in the background so that I can use the same shell. Notice the output: element 0 of the array contains the value 37. We can now run the other task, vminfo:

user@virtme-ng:~/kernel> ./vminfo 448 24801952 40 10
[ pid: 448, addr: 24801952, 0x17a72a0, size of data: 40, n_elements 10]

Starting Page Structures walk...

PGD: 0x38bf8067 P4D: 0x38bf8067 PUD: 0x3f03b067 PMD: 0x5e24067 PTE: 0x8000000003213867
Buffer content:
[0] = 37
[1] = 0
[2] = 0
[3] = 0
[4] = 0
[5] = 0
[6] = 0
[7] = 0
[8] = 0
[9] = 0

RET syscall: 0, errno: 0

Enter to keep reading the address, or CTRL+C

I exit vminfo and write a ".." to the stdin of the previous task:

user@virtme-ng:~/kernel> echo '..' > data/in 
[1] = 14
[2] = 31
[3] = 14

Running vminfo again:

user@virtme-ng:~/kernel> ./vminfo 448 24801952 40 10
[ pid: 448, addr: 24801952, 0x17a72a0, size of data: 40, n_elements 10]

Starting Page Structures walk...

PGD: 0x38bf8067 P4D: 0x38bf8067 PUD: 0x3f03b067 PMD: 0x5e24067 PTE: 0x8000000003213867
Buffer content:
[0] = 37
[1] = 14
[2] = 31
[3] = 14
[4] = 0
[5] = 0
[6] = 0
[7] = 0
[8] = 0
[9] = 0

RET syscall: 0, errno: 0

Enter to keep reading the address, or CTRL+C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment