interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / test / seccomp-table.py

120 lines · 5409 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Run forged-seccomp.bpf, as data, over a table of system calls and compare each result.
3
4Usage: seccomp-table.py FILTER.bpf
5This is an interpreter for the classic BPF subset the filter uses (LD W ABS, JEQ K, RET K). It
6does not trust forged-seccomp.om's notes: every row says what the sandbox needs, and the program
7must give exactly that. Exit 0 when every row matches, 1 on a mismatch, 2 on a malformed program.
8The kernel is the real judge; infra/test/listener-poison.py and leakprobe check the installed
9filter. This table catches a wrong jump count or a missing rule before anything runs.
10"""
11import struct
12import sys
13
14AARCH64 = 0xC00000B7
15ALLOW, EPERM, KILL = 0x7FFF0000, 0x00050001, 0x80000000
16NAMES = {ALLOW: "allow", EPERM: "EPERM", KILL: "kill"}
17
18
19def load(path):
20 raw = open(path, "rb").read()
21 if not raw or len(raw) % 8:
22 sys.exit(f"seccomp-table: {path}: {len(raw)} bytes is not a whole program")
23 return [struct.unpack("<HBBI", raw[i:i + 8]) for i in range(0, len(raw), 8)]
24
25
26def run(prog, nr, args=(), arch=AARCH64):
27 a = list(args) + [0] * (6 - len(args))
28 data = struct.pack("<iI", nr, arch) + struct.pack("<Q", 0) + b"".join(struct.pack("<Q", x) for x in a)
29 acc, pc, steps = 0, 0, 0
30 while pc < len(prog):
31 steps += 1
32 if steps > 4096:
33 sys.exit("seccomp-table: loop")
34 code, jt, jf, k = prog[pc]
35 if code == 0x20: # BPF_LD | BPF_W | BPF_ABS
36 acc = struct.unpack_from("<I", data, k)[0]
37 pc += 1
38 elif code == 0x15: # BPF_JMP | BPF_JEQ | BPF_K
39 pc += 1 + (jt if acc == k else jf)
40 elif code == 0x06: # BPF_RET | BPF_K
41 return k
42 else:
43 sys.exit(f"seccomp-table: instruction {pc}: code {code:#x} is outside the subset")
44 sys.exit("seccomp-table: ran off the end of the program")
45
46
47# (name, nr, args, expected). AArch64 numbers; args are the low 32 bits the kernel compares.
48SOL_SOCKET, IPPROTO_TCP = 1, 6
49ROWS = [
50 ("read", 63, (), ALLOW),
51 ("accept4", 242, (0,), ALLOW),
52 ("listen (the successor re-arms the held socket)", 201, (0, 64), ALLOW),
53 ("getsockname", 204, (0,), ALLOW),
54 ("sendto", 206, (3,), ALLOW),
55 ("close", 57, (0,), ALLOW),
56 ("ioctl FIONBIO", 29, (0, 0x5421), ALLOW),
57 ("ioctl FIOCLEX", 29, (0, 0x5451), ALLOW),
58 ("ioctl FIONCLEX", 29, (0, 0x5450), ALLOW),
59 ("ioctl FIONREAD", 29, (0, 0x541B), ALLOW),
60 ("ioctl TCGETS", 29, (0, 0x5401), ALLOW),
61 ("ioctl TIOCGWINSZ", 29, (0, 0x5413), ALLOW),
62 ("ioctl SIOCGIFCONF", 29, (0, 0x8912), EPERM),
63 ("ioctl SIOCGIFNAME", 29, (0, 0x8910), EPERM),
64 ("ioctl SIOCGSKNS", 29, (0, 0x894C), EPERM),
65 ("socket AF_UNIX", 198, (1, 1), ALLOW),
66 ("socket AF_INET", 198, (2, 1), EPERM),
67 ("socket AF_INET6", 198, (10, 1), EPERM),
68 ("socket AF_NETLINK", 198, (16, 3), EPERM),
69 ("socket AF_PACKET", 198, (17, 3), EPERM),
70 ("socketpair AF_UNIX", 199, (1, 1), ALLOW),
71 ("socketpair AF_INET", 199, (2, 1), EPERM),
72 ("setsockopt TCP_NODELAY", 208, (3, IPPROTO_TCP, 1), ALLOW),
73 ("setsockopt TCP other (TCP_MAXSEG)", 208, (3, IPPROTO_TCP, 2), EPERM),
74 ("setsockopt SO_REUSEADDR", 208, (3, SOL_SOCKET, 2), ALLOW),
75 ("setsockopt SO_SNDBUF", 208, (3, SOL_SOCKET, 7), ALLOW),
76 ("setsockopt SO_RCVBUF", 208, (3, SOL_SOCKET, 8), ALLOW),
77 ("setsockopt SO_KEEPALIVE", 208, (3, SOL_SOCKET, 9), ALLOW),
78 ("setsockopt SO_REUSEPORT (finding 2)", 208, (0, SOL_SOCKET, 15), EPERM),
79 ("setsockopt SO_BINDTODEVICE", 208, (3, SOL_SOCKET, 25), EPERM),
80 ("setsockopt SO_BINDTOIFINDEX", 208, (3, SOL_SOCKET, 62), EPERM),
81 ("setsockopt other level (IPPROTO_IP)", 208, (3, 0, 8), EPERM),
82 ("shutdown SHUT_RD (finding 1)", 210, (0, 0), EPERM),
83 ("shutdown SHUT_RDWR (finding 1)", 210, (0, 2), EPERM),
84 ("shutdown SHUT_WR", 210, (4, 1), EPERM),
85 ("connect (AF_UNSPEC on the listener disconnects it)", 203, (0,), EPERM),
86 ("io_uring_setup", 425, (), EPERM),
87 ("io_uring_enter", 426, (), EPERM),
88 ("io_uring_register", 427, (), EPERM),
89 ("bind (allowed; the namespace has only lo)", 200, (3,), ALLOW),
90 ("getsockopt", 209, (0, SOL_SOCKET, 15), ALLOW),
91 ("execve", 221, (), ALLOW),
92]
93
94
95def main():
96 if len(sys.argv) != 2:
97 sys.exit("usage: seccomp-table.py FILTER.bpf")
98 prog = load(sys.argv[1])
99 print(f"program {len(prog)} instructions")
100 bad = 0
101 for name, nr, args, want in ROWS:
102 got = run(prog, nr, args)
103 ok = got == want
104 bad += not ok
105 print(f"{'ok ' if ok else 'FAIL'} {name}: {NAMES.get(got, hex(got))} (want {NAMES[want]})")
106 # High halves do not change the outcome; other architectures are killed.
107 for name, nr, args, want in [("SO_REUSEPORT with high bits", 208, (0, SOL_SOCKET | 1 << 32, 15 | 1 << 40), EPERM)]:
108 got = run(prog, nr, args)
109 bad += got != want
110 print(f"{'ok ' if got == want else 'FAIL'} {name}: {NAMES.get(got, hex(got))} (want {NAMES[want]})")
111 for arch in (0x40000028, 0xC000003E):
112 got = run(prog, 63, (), arch)
113 bad += got != KILL
114 print(f"{'ok ' if got == KILL else 'FAIL'} arch {arch:#x} read: {NAMES.get(got, hex(got))} (want kill)")
115 print(f"seccomp-table {'PASS' if not bad else 'FAIL'} rows={len(ROWS) + 3} mismatches={bad}")
116 return 1 if bad else 0
117
118
119if __name__ == "__main__":
120 sys.exit(main())