Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(bot-box): kill x32 syscall numbers instead of letting them through

DeryFerda1b649aea9a2
diff --git a/server/bot-box/seccomp.test.ts b/server/bot-box/seccomp.test.ts
index b5b2d46af5..cead1a608a 100644
--- a/server/bot-box/seccomp.test.ts
+++ b/server/bot-box/seccomp.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { DENIED_SYSCALLS, SECCOMP_ARCH_X86_64, seccompBytes, seccompProgram, seccompSupported, simulate } from "./seccomp.ts";
+import { DENIED_SYSCALLS, SECCOMP_ARCH_X86_64, X32_SYSCALL_BIT, seccompBytes, seccompProgram, seccompSupported, simulate } from "./seccomp.ts";
const denied = (name: string) => DENIED_SYSCALLS.find(([n]) => n === name)![1];
@@ -14,18 +14,33 @@ describe("the box's seccomp filter", () => {
expect(simulate(program, 0xc00000b7, 0)).toBe("kill");
});
+ it("kills the x32 ABI's numbering of a denied call instead of letting it through", () => {
+ const program = seccompProgram();
+ // The Kernel reaches the same call as `nr | 0x40000000`; an exact-match deny list alone would allow it.
+ for (const name of ["mount", "bpf", "open_by_handle_at", "fsopen", "keyctl"]) {
+ expect(simulate(program, SECCOMP_ARCH_X86_64, X32_SYSCALL_BIT | denied(name)), name).toBe("kill");
+ }
+ // A call that is allowed plainly is not allowed through the x32 numbering either.
+ expect(simulate(program, SECCOMP_ARCH_X86_64, X32_SYSCALL_BIT | 0)).toBe("kill");
+ expect(simulate(program, SECCOMP_ARCH_X86_64, 0)).toBe("allow");
+ });
+
it("packs instructions as the kernel reads struct sock_filter", () => {
const bytes = seccompBytes(seccompProgram([["mount", 165]]));
- expect(bytes.length).toBe(7 * 8);
+ expect(bytes.length).toBe(9 * 8);
// ld [4] (arch)
expect([...bytes.subarray(0, 8)]).toEqual([0x20, 0x00, 0, 0, 4, 0, 0, 0]);
// jeq AUDIT_ARCH_X86_64, +1, +0
expect([...bytes.subarray(8, 16)]).toEqual([0x15, 0x00, 1, 0, 0x3e, 0x00, 0x00, 0xc0]);
// ret KILL_PROCESS
expect([...bytes.subarray(16, 24)]).toEqual([0x06, 0x00, 0, 0, 0x00, 0x00, 0x00, 0x80]);
- // jeq 165 → +1 (the EPERM return past the allow)
- expect([...bytes.subarray(32, 40)]).toEqual([0x15, 0x00, 1, 0, 165, 0, 0, 0]);
- expect([...bytes.subarray(48, 56)]).toEqual([0x06, 0x00, 0, 0, 0x01, 0x00, 0x05, 0x00]);
+ // ld [0] (nr)
+ expect([...bytes.subarray(24, 32)]).toEqual([0x20, 0x00, 0, 0, 0, 0, 0, 0]);
+ // jge the x32 bit, +3 (the KILL_PROCESS return after the EPERM one)
+ expect([...bytes.subarray(32, 40)]).toEqual([0x35, 0x00, 3, 0, 0x00, 0x00, 0x00, 0x40]);
+ // jeq 165, +1 (the EPERM return past the allow)
+ expect([...bytes.subarray(40, 48)]).toEqual([0x15, 0x00, 1, 0, 165, 0, 0, 0]);
+ expect([...bytes.subarray(56, 64)]).toEqual([0x06, 0x00, 0, 0, 0x01, 0x00, 0x05, 0x00]);
});
it("every denied number is unique and the filter is x86-64 Linux only", () => {
diff --git a/server/bot-box/seccomp.ts b/server/bot-box/seccomp.ts
index fbc5fba77a..64987baf2b 100644
--- a/server/bot-box/seccomp.ts
+++ b/server/bot-box/seccomp.ts
@@ -8,10 +8,12 @@
// u16 code, u8 jt, u8 jf, u32 k (little-endian, as the kernel reads struct sock_filter on x86-64).
export const SECCOMP_ARCH_X86_64 = 0xc000003e;
+/** The x32 ABI's syscall-number bit: `open(2)` and friends are also reachable as `nr | 0x40000000`. */
+export const X32_SYSCALL_BIT = 0x40000000;
const SECCOMP_RET_ALLOW = 0x7fff0000, SECCOMP_RET_ERRNO = 0x00050000, SECCOMP_RET_KILL_PROCESS = 0x80000000;
const EPERM = 1;
// BPF opcodes
-const BPF_LD_W_ABS = 0x20, BPF_JMP_JEQ_K = 0x15, BPF_RET_K = 0x06;
+const BPF_LD_W_ABS = 0x20, BPF_JMP_JEQ_K = 0x15, BPF_JMP_JGE_K = 0x35, BPF_RET_K = 0x06;
// struct seccomp_data offsets
const OFFSET_NR = 0, OFFSET_ARCH = 4;
@@ -36,10 +38,17 @@ export function seccompProgram(denied: ReadonlyArray<[string, number]> = DENIED_
{ code: BPF_RET_K, jt: 0, jf: 0, k: SECCOMP_RET_KILL_PROCESS },
{ code: BPF_LD_W_ABS, jt: 0, jf: 0, k: OFFSET_NR },
];
+ // Security: a number at or above the x32 bit is the same call through the x32 ABI, so the exact-match
+ // compares below would miss it and fall through to ALLOW. Kill the whole range outright.
+ const guard = program.length;
+ program.push({ code: BPF_JMP_JGE_K, jt: 0, jf: 0, k: X32_SYSCALL_BIT });
// Each denied number jumps straight to the EPERM return at the end; a miss falls through to the next check.
denied.forEach(([, nr], index) => program.push({ code: BPF_JMP_JEQ_K, jt: denied.length - index, jf: 0, k: nr }));
program.push({ code: BPF_RET_K, jt: 0, jf: 0, k: SECCOMP_RET_ALLOW });
program.push({ code: BPF_RET_K, jt: 0, jf: 0, k: SECCOMP_RET_ERRNO | EPERM });
+ const kill = program.length;
+ program.push({ code: BPF_RET_K, jt: 0, jf: 0, k: SECCOMP_RET_KILL_PROCESS });
+ program[guard]!.jt = kill - (guard + 1);
return program;
}
@@ -67,6 +76,7 @@ export function simulate(program: BpfInstruction[], arch: number, nr: number): "
const instruction = program[pc]!;
if (instruction.code === BPF_LD_W_ABS) { accumulator = instruction.k === OFFSET_ARCH ? arch : nr; pc += 1; }
else if (instruction.code === BPF_JMP_JEQ_K) pc += 1 + (accumulator === instruction.k ? instruction.jt : instruction.jf);
+ else if (instruction.code === BPF_JMP_JGE_K) pc += 1 + (accumulator >= instruction.k ? instruction.jt : instruction.jf);
else if (instruction.code === BPF_RET_K) return instruction.k === SECCOMP_RET_ALLOW ? "allow" : instruction.k === SECCOMP_RET_KILL_PROCESS ? "kill" : "eperm";
else throw new Error(`unknown instruction at ${pc}`);
}