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