interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / spark / forged-seccomp.om

140 lines · 5282 bytes Raw Blame History
1om2
2note forged-seccomp: writes the sandbox's socket filter, a classic BPF seccomp program, to stdout.
3note run.sh builds it beside forged and runs it once into forged-seccomp.bpf; forged-sandbox.sh
4note hands those bytes to bubblewrap (--seccomp), which installs the filter in the sandbox just
5note before it executes forged. Every process in the sandbox inherits it.
6note Why: the listener is made outside, in the host network namespace, and so is every connection
7note forged accepts from it. Interface questions asked through such a socket (SIOCGIFCONF,
8note SIOCGIFNAME, SIOCGIFADDR, SIOCGIFHWADDR, ...) are answered about the host network, and a
9note socket that is not yet bound to a device can be bound by index and read back by name
10note (SO_BINDTOIFINDEX, then SO_BINDTODEVICE). No namespace setting can change that, because the
11note namespace belongs to the socket. The filter refuses those calls by number, whatever socket
12note they target:
13note ioctl: only FIOCLEX, FIONCLEX, FIONBIO, FIONREAD, TCGETS and TIOCGWINSZ; everything else,
14note including every 0x89xx socket request, fails with EPERM.
15note socket and socketpair: only AF_UNIX (glibc's name-cache lookup); no netlink, inet or packet
16note socket can be made inside.
17note setsockopt: only IPPROTO_TCP TCP_NODELAY and SOL_SOCKET SO_REUSEADDR, SO_SNDBUF, SO_RCVBUF,
18note SO_KEEPALIVE; SO_REUSEPORT, SO_BINDTODEVICE and SO_BINDTOIFINDEX fail with EPERM.
19note connect and shutdown: EPERM, on every socket.
20note io_uring_setup, io_uring_enter, io_uring_register: EPERM (io_uring carries socket commands).
21note A system call from any architecture other than AArch64 kills the process.
22note Everything else is allowed; the namespaces, no_new_privs, limits and mounts do the rest.
23note The listener is shared with the supervisor, which holds it for its whole life, so the child
24note must not be able to give the port away (netns r2 review of 8f6b4a29, findings 1 and 2, and
25note the same class through connect):
26note shutdown(SHUT_RD or SHUT_RDWR) on a listening socket, and connect with an AF_UNSPEC address,
27note disconnect it: it stops listening, and a local process with SO_REUSEADDR can then bind
28note 127.0.0.1 on the port and receive the connections. Forged never calls either; inside the
29note sandbox the only connect is glibc's name-cache lookup (git, ssh-keygen), which finds no
30note socket there and falls back to the files, as it did with ENOENT before.
31note SO_REUSEPORT on the listener lets a local process with SO_REUSEPORT bind the same address
32note and take a share of the new connections. The listener is bound outside the filter, so
33note nothing inside needs the option.
34note Arguments are compared on their low 32 bits: the kernel reads ioctl's request, socket's domain
35note and setsockopt's level and name as 32-bit values, so the high half cannot change the outcome.
36note Layout: struct sock_filter {u16 code; u8 jt; u8 jf; u32 k}, little endian, 8 bytes each.
37note struct seccomp_data: nr at 0, arch at 4, args[i] at 16 + 8 i (low half first).
38note Linux AArch64 numbers: ioctl 29, socket 198, socketpair 199, connect 203, setsockopt 208,
39note shutdown 210, io_uring_setup 425, io_uring_enter 426, io_uring_register 427.
40note Exit 0 after writing the whole program, 1 if the write fails.
41global bp uchar 2048
42global bn ulong 1
43fn op c ulong t ulong f ulong k ulong to void
44o = mul get bn 0 8
45put bp o uchar and c 255
46put bp add o 1 uchar shr c 8
47put bp add o 2 uchar t
48put bp add o 3 uchar f
49put bp add o 4 uchar and k 255
50put bp add o 5 uchar and shr k 8 255
51put bp add o 6 uchar and shr k 16 255
52put bp add o 7 uchar shr k 24
53put bn 0 add get bn 0 1
54end
55note BPF_LD|BPF_W|BPF_ABS: A = the 32-bit word at offset k of seccomp_data.
56fn ld k ulong to void
57op 32 0 0 k
58end
59note BPF_JMP|BPF_JEQ|BPF_K: skip t instructions when A == k, else skip f.
60fn jeq k ulong t ulong f ulong to void
61op 21 t f k
62end
63note BPF_RET|BPF_K
64fn rt k ulong to void
65op 6 0 0 k
66end
67fn allow to void
68rt 2147418112
69end
70fn deny to void
71note SECCOMP_RET_ERRNO | EPERM
72rt 327681
73end
74fn allowif k ulong to void
75note When A == k return ALLOW; otherwise fall through to the next test.
76jeq k 0 1
77allow
78end
79fn denyif k ulong to void
80note When A == k return EPERM; otherwise fall through to the next test.
81jeq k 0 1
82deny
83end
84fn familyonly to void
85note 4 instructions: allow only AF_UNIX (1) in args[0].
86ld 16
87allowif 1
88deny
89end
90fn main to int
91note Architecture: AUDIT_ARCH_AARCH64 0xC00000B7, otherwise SECCOMP_RET_KILL_PROCESS.
92ld 4
93jeq 3221225655 1 0
94rt 2147483648
95ld 0
96note ioctl: 1 + 2 * 6 + 1 = 14 instructions follow.
97jeq 29 0 14
98ld 24
99allowif 21585
100allowif 21584
101allowif 21537
102allowif 21531
103allowif 21505
104allowif 21523
105deny
106jeq 198 0 4
107familyonly
108jeq 199 0 4
109familyonly
110note setsockopt: 18 instructions follow.
111jeq 208 0 18
112ld 24
113note level IPPROTO_TCP (6): 4 instructions.
114jeq 6 0 4
115ld 32
116allowif 1
117deny
118note level SOL_SOCKET (1): 10 instructions. SO_REUSEPORT (15) is not on the list.
119jeq 1 0 10
120ld 32
121allowif 2
122allowif 7
123allowif 8
124allowif 9
125deny
126deny
127note connect and shutdown: the listener shared with the supervisor stays listening.
128denyif 203
129denyif 210
130denyif 425
131denyif 426
132denyif 427
133allow
134n = mul get bn 0 8
135w = write 1 bp 0 n
136if ne w long n
137ret 1
138end
139ret 0
140end