Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/fork-fleet.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ Two more things limit what a job can do:
that each bound path is the one the launcher checked. The per-OS walls ask
the launcher (`fork-fleet-launch.sh paths`) instead of keeping a list of
their own.
Before a new kind of host joins a pool (a Windows PC's WSL2 first),
`sudo bash .github/scripts/fork-fleet-walls-proof.sh` runs step 6 there with
throwaway users, then each network block alone, prints the cgroup version,
and undoes all it made. It refuses a host with any part of a pool, running or
not.

Fleet jobs read no secrets (a test checks this). The fleet's GitHub token lives
only on the fleet host, readable by root.
Expand Down
147 changes: 147 additions & 0 deletions .github/scripts/fork-fleet-walls-proof.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#!/usr/bin/env bash
# Fork only: proves the launcher's walls (fork-fleet-launch.sh check-net) on a real host, as root, with throwaway
# users, before that host joins a pool: a Linux machine, or a Windows PC's WSL2 distribution (systemd on). It changes
# nothing it does not undo: its own runner users, temp dir and listener; the nft table inet fork_fleet, the users'
# namespaces and links; Docker's two DOCKER-USER rules when it added them; ip_forward, set back. It refuses a host
# that has any part of a pool (its files, units, users, table, namespaces or links, running or not): that table and
# those links are the pool's too. Runbook: .github/fork-fleet.md, "Walls (every pool)".
#
# sudo bash .github/scripts/fork-fleet-walls-proof.sh [HOST:PORT...]
#
# HOST:PORT: more services this host reaches that a job must not (on WSL2: one of Windows's, if any answers). It
# adds this host's loopback and its own addresses on a port it listens on, and the default gateway's first answering
# port of 445, 135 and 53 (on WSL2's NAT network that gateway is Windows). Exit 0 only when check-net passes, each of
# the two network blocks applies by itself, and every cleanup step worked.
set -euo pipefail
die() {
printf 'fork-fleet-walls-proof: %s\n' "$*" >&2
exit 1
}
((EUID == 0)) || die "run it as root (sudo): it makes throwaway users, a network namespace and the nft table"
[[ -d /run/systemd/system ]] || die "systemd is not running here (WSL2: [boot] systemd=true in /etc/wsl.conf, then wsl --shutdown)"
for c in nft ip curl python3 systemd-run useradd userdel getent; do command -v "$c" >/dev/null || die "$c is missing"; done
here=$(dirname -- "$(readlink -f -- "$0")")
launch=$here/fork-fleet-launch.sh
[[ -f $launch && -f $here/fork-fleet-job-paths.txt ]] || die "no $launch or its fork-fleet-job-paths.txt"
users=(fork-fleet-p1 fork-fleet-p2)

# A pool's parts, whether it runs now or not (after a reboot a pool has no table and no unit until its next job).
pool=()
for p in /etc/fork-fleet* /opt/fork-fleet* /var/lib/fork-fleet* /run/fork-fleet*; do [[ ! -e $p ]] || pool+=("$p"); done
while read -r unit _; do pool+=("unit $unit"); done < <(systemctl list-unit-files --no-legend --plain \
'fork-fleet*' 't3code-fleet*' 2>/dev/null; systemctl list-units --all --no-legend --plain 'fork-fleet*' 't3code-fleet*')
while IFS=: read -r name _; do pool+=("user $name"); done < <(getent passwd | grep -E '^fork-(fleet|runner)-' || true)
! nft list table inet fork_fleet >/dev/null 2>&1 || pool+=("nft table inet fork_fleet")
while read -r ns _; do pool+=("namespace $ns"); done < <(ip netns list | grep -E '^fork-fleet-' || true)
while read -r link; do pool+=("link $link"); done < <(ip -o link show | awk -F': ' '$2 ~ /^ffh-/ { print $2 }')
while read -r pid cmd; do pool+=("process $pid $cmd"); done < <(pgrep -af 'fork-fleet(\.py|-launch\.sh)' || true)
((${#pool[@]} == 0)) || die "this host has a pool's parts (a pool runs here, or did, or a run of this proof stopped" \
"half-way: undo them by hand first): ${pool[*]}"
for u in "${users[@]}"; do [[ ! -e /run/netns/fork-fleet-$u ]] || die "namespace fork-fleet-$u exists"; done

docker=() made=() failed=() tmp='' listener='' forward=$(</proc/sys/net/ipv4/ip_forward)
if iptables -w 60 -S DOCKER-USER >/dev/null 2>&1; then
for r in "-o ffh-+ -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT" "-i ffh-+ -j ACCEPT"; do
# shellcheck disable=SC2086 # $r is split into iptables' arguments on purpose
iptables -w 60 -C DOCKER-USER $r 2>/dev/null || docker+=("$r")
done
fi

# Every cleanup step runs, whichever failed before it; the failed ones are named at the end.
step() { "$@" || failed+=("$*"); }
kill_all() { pkill -KILL -u "$1" || (($? == 1)); }
cleanup() {
local u uid r unit
set +e
[[ -z $listener ]] || step kill "$listener"
while read -r unit _; do step systemctl stop -- "$unit"; done < <(systemctl list-units --no-legend --plain 'fork-fleet-*')
for u in "${made[@]}"; do
if ! uid=$(id -u -- "$u"); then failed+=("id $u"); continue; fi
[[ ! -e /run/netns/fork-fleet-$u ]] || step ip netns del "fork-fleet-$u"
! ip link show "ffh-$uid" >/dev/null 2>&1 || step ip link del "ffh-$uid"
step kill_all "$uid"
step userdel -- "$u"
done
! nft list table inet fork_fleet >/dev/null 2>&1 || step nft delete table inet fork_fleet
# Only the rules the launcher added (a run that stopped before it did leaves none): no false failed step.
# shellcheck disable=SC2086
for r in "${docker[@]}"; do ! iptables -w 60 -C DOCKER-USER $r 2>/dev/null || step iptables -w 60 -D DOCKER-USER $r; done
[[ -z $tmp ]] || step rm -rf --one-file-system -- "${tmp:?}"
step sysctl -qw "net.ipv4.ip_forward=$forward"
if ((${#failed[@]})); then
printf 'fork-fleet-walls-proof: cleanup steps that failed (undo them by hand): %s\n' "${failed[@]}" >&2
exit 1
fi
}
trap cleanup EXIT
trap 'exit 130' INT TERM

echo "kernel $(uname -r) $(uname -m); $(systemctl --version | head -n1); $(nft --version)"
cg=$(stat -fc %T /sys/fs/cgroup)
[[ $cg == cgroup2fs ]] && echo "cgroup v2 (unified): IPAddressDeny can apply" ||
echo "cgroup $cg (v1 or hybrid): IPAddressDeny cannot apply here; only the nft table blocks"
if command -v wslinfo >/dev/null; then echo "WSL networking: $(wslinfo --networking-mode 2>&1)"; fi
for u in "${users[@]}"; do
useradd --system --user-group --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin -- "$u"
made+=("$u")
done
tmp=$(mktemp -d /var/tmp/fork-fleet-walls-proof.XXXXXX)
chmod 0711 "$tmp"
mkdir "$tmp/template"
(
umask 022
printf 'nameserver 1.1.1.1\nnameserver 8.8.8.8\n' >"$tmp/resolv.conf"
cp -- "$here/fork-fleet-job-paths.txt" "$tmp/job-paths.txt"
printf '#!/bin/sh\n' >"$tmp/hook.sh"
)
printf '%s\n' "RUNNER_USERS=fork-fleet-p1,fork-fleet-p2" "TEMPLATE=$tmp/template" "WORK_ROOT=$tmp/runners" \
"RUN_DIR=$tmp/run" "RESOLV_CONF=$tmp/resolv.conf" "JOB_PATHS=$tmp/job-paths.txt" "HOOK=$tmp/hook.sh" \
>"$tmp/launch.conf"
chmod 0600 "$tmp/launch.conf"
sysctl -qw net.ipv4.ip_forward=1

python3 -I -c 'import socket, sys
s = socket.socket(); s.bind(("0.0.0.0", 0)); s.listen(64)
print(s.getsockname()[1], flush=True); sys.stdout.close()
while True: s.accept()[0].close()' >"$tmp/port" &
listener=$!
for _ in {1..50}; do [[ -s $tmp/port ]] && break; sleep 0.1; done
port=$(<"$tmp/port")
[[ $port =~ ^[0-9]+$ ]] || die "the listener did not start"
targets=("127.0.0.1:$port")
own=$(ip -4 -o addr show scope global | awk '{ sub("/.*", "", $4); print $4 }')
for a in $own; do targets+=("$a:$port"); done
gw=$(ip -4 route show default | awk '{ print $3; exit }')
for p in 445 135 53; do
[[ -n $gw ]] || break
# shellcheck disable=SC2016
if timeout 2 bash -c 'exec 3<>"/dev/tcp/$0/$1"' "$gw" "$p" 2>/dev/null; then targets+=("$gw:$p") && break; fi
done
targets+=("$@")
echo "must be closed inside: ${targets[*]}"
rc=0
# check-net: the network, the walls and the files (only the allowed mounts, none of the host's later, the pseudo
# devices only, no GPU, no socket of the host's: WSLg's X11 and PulseAudio ones, WSL's interop ones in /run).
FORK_FLEET_LAUNCH_CONF=$tmp/launch.conf bash "$launch" check-net https://github.com "${targets[@]}" || rc=$?

# Each of the two network blocks by itself (check-net proves the two together): the nft table, from the runner's
# namespace without a unit; the unit's IPAddressDeny, in a unit outside the namespace.
connect='import socket, sys
try:
socket.create_connection((sys.argv[1], int(sys.argv[2])), timeout=3).close(); print("open")
except OSError as e:
print(type(e).__name__)'
first=${own%%$'\n'*}
if [[ -n $first && -e /run/netns/fork-fleet-${users[0]} ]]; then
got=$(ip netns exec "fork-fleet-${users[0]}" python3 -I -c "$connect" "$first" "$port")
[[ $got != open ]] && echo "ok the nft table alone refuses this host's $first:$port from a runner's namespace ($got)" ||
{ echo "FAIL the nft table alone lets a runner's namespace reach $first:$port"; rc=1; }
else
echo "FAIL the nft table alone: not tried (no address of this host's, or no namespace)"; rc=1
fi
got=$(systemd-run --quiet --wait --pipe --collect -p IPAddressDeny=any -- /usr/bin/python3 -I -c "$connect" \
127.0.0.1 "$port")
[[ $got != open ]] && echo "ok IPAddressDeny alone refuses 127.0.0.1:$port in a unit ($got)" ||
{ echo "FAIL IPAddressDeny does not apply here (cgroup $cg): a unit reaches 127.0.0.1:$port"; rc=1; }
((rc == 0)) && echo "fork-fleet-walls-proof: PASSED" || echo "fork-fleet-walls-proof: FAILED (exit $rc)"
exit "$rc"
106 changes: 106 additions & 0 deletions .github/scripts/fork-fleet.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,102 @@ def seen(cmd):
"""


class WallsProof(unittest.TestCase):
"""fork-fleet-walls-proof.sh proves the walls on a new kind of host, as root, with throwaway users. Here: it
parses, refuses without root and changes nothing, and has the parts fix round 1 asked for (LauncherAsRoot runs it
as root, where it must refuse: that class's users are a pool's)."""
PROOF = os.path.join(HERE, 'fork-fleet-walls-proof.sh')

def src(self):
with open(self.PROOF) as f:
return f.read()

def test_it_parses_and_needs_root(self):
self.assertEqual(subprocess.run(['bash', '-n', self.PROOF]).returncode, 0)
if os.geteuid() != 0:
got = subprocess.run(['bash', self.PROOF], capture_output=True, text=True, timeout=30)
self.assertEqual((got.returncode, got.stdout), (1, ''))
self.assertIn('run it as root', got.stderr)
self.assertIn('"$launch" check-net https://github.com', self.src())

def test_it_looks_for_every_part_of_a_pool_running_or_not(self):
src = self.src()
for part in ('/etc/fork-fleet*', '/opt/fork-fleet*', '/var/lib/fork-fleet*', '/run/fork-fleet*',
"list-unit-files --no-legend --plain \\\n 'fork-fleet*' 't3code-fleet*'",
"list-units --all --no-legend --plain 'fork-fleet*' 't3code-fleet*'",
"grep -E '^fork-(fleet|runner)-'", 'nft list table inet fork_fleet',
"ip netns list | grep -E '^fork-fleet-'", '^ffh-', "pgrep -af 'fork-fleet(\\.py|-launch\\.sh)'",
'/run/netns/fork-fleet-$u'):
self.assertIn(part, src)
self.assertLess(src.index('die "this host has a pool'), src.index('useradd --system'))

def test_every_cleanup_step_runs_and_a_failed_one_is_named(self):
src = self.src()
cleanup = src[src.index('cleanup() {'):src.index('trap cleanup EXIT')]
self.assertIn('set +e', cleanup)
for undo in ('kill "$listener"', 'systemctl stop', 'ip netns del', 'ip link del', 'kill_all', 'userdel',
'nft delete table inet fork_fleet', 'iptables -w 60 -D DOCKER-USER', 'rm -rf', 'sysctl -qw'):
self.assertRegex(cleanup, r'step [^\n]*' + re.escape(undo), undo)
self.assertIn('cleanup steps that failed', cleanup)
# step() runs the next step whatever this one gave, and names the failed one
got = subprocess.run(['bash', '-c', src[src.index('step() {'):src.index('kill_all()')] +
'set -e; failed=(); step false one; step true; step false two; echo "${failed[*]}"'],
capture_output=True, text=True, timeout=30)
self.assertEqual((got.returncode, got.stdout), (0, 'false one false two\n'))

def test_cleanup_deletes_the_launchers_namespace_and_only_the_docker_rules_that_are_there(self):
"""Fix round 2 (#198 nits): cleanup deletes the namespace the launcher made for that user (its name and path),
and a DOCKER-USER rule the launcher never added (a run that stopped early) is not a failed step (no exit 1)."""
src = self.src()
with open(os.path.join(HERE, 'fork-fleet-launch.sh')) as f:
launch = f.read()
self.assertIn('ns=fork-fleet-$user ', launch)
self.assertIn('NetworkNamespacePath=/run/netns/fork-fleet-$user', launch)
tmp = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmp, True)
bin_, rec = os.path.join(tmp, 'bin'), os.path.join(tmp, 'rec')
os.makedirs(bin_)
os.makedirs(os.path.join(tmp, 'netns'))
open(os.path.join(tmp, 'netns', 'fork-fleet-u1'), 'w').close()
# Fakes: each records its arguments; iptables -C finds only the rules listed in $T/present.
for name in ('ip', 'iptables', 'systemctl', 'nft', 'pkill', 'userdel', 'sysctl', 'kill'):
body = f'echo "{name} $*" >>"$T/rec"\n'
if name == 'iptables':
body += 'if [[ " $* " == *" -C "* ]]; then grep -qxF -- "${*: -1}" "$T/present"; exit; fi\n'
if name in ('ip', 'nft'):
body += '[[ " $* " != *" show "* && " $* " != *" list "* ]] || exit 1\n'
with open(os.path.join(bin_, name), 'w') as f:
f.write('#!/bin/bash\n' + body + 'exit 0\n')
os.chmod(os.path.join(bin_, name), 0o755)
with open(os.path.join(bin_, 'id'), 'w') as f:
f.write('#!/bin/bash\necho 4242\n')
os.chmod(os.path.join(bin_, 'id'), 0o755)
code = src[src.index('step() {'):src.index('trap cleanup EXIT')].replace('/run/netns/', tmp + '/netns/')
rules = ['-o ffh-+ -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT', '-i ffh-+ -j ACCEPT']
for present in ([], ['ACCEPT']):
with open(os.path.join(tmp, 'present'), 'w') as f:
f.write('\n'.join(present) + '\n')
open(rec, 'w').close()
got = subprocess.run(['bash', '-c', code + '\nmade=(u1) docker=("$R1" "$R2") failed=() tmp="" listener="" '
'forward=0\ncleanup\necho done'],
env={'PATH': bin_ + ':/usr/bin:/bin', 'T': tmp, 'R1': rules[0], 'R2': rules[1]},
capture_output=True, text=True, timeout=30)
self.assertEqual((got.returncode, got.stdout, got.stderr), (0, 'done\n', ''))
with open(rec) as f:
calls = f.read().splitlines()
self.assertIn('ip netns del fork-fleet-u1', calls)
deleted = [c for c in calls if ' -D DOCKER-USER ' in c]
self.assertEqual(deleted, [f'iptables -w 60 -D DOCKER-USER {r}' for r in rules] if present else [])

def test_it_prints_the_cgroup_version_and_tries_each_block_alone(self):
src = self.src()
self.assertIn('stat -fc %T /sys/fs/cgroup', src)
self.assertIn('ip netns exec "fork-fleet-${users[0]}"', src)
self.assertIn('-p IPAddressDeny=any', src)
for key in ('JOB_PATHS=$tmp/job-paths.txt', 'HOOK=$tmp/hook.sh'):
self.assertIn(key, src)


@unittest.skipUnless(sys.platform.startswith('linux') and os.geteuid() == 0 and
os.environ.get('FORK_FLEET_ROOT_TESTS') == '1',
'as root with FORK_FLEET_ROOT_TESTS=1 only: it creates and deletes users (CI: the workflow lint '
Expand Down Expand Up @@ -2765,6 +2861,16 @@ def later(*cmd):
self.assertIn("ok this host's own public address 203.0.113.3 is refused (No route to host)", got.stdout)
self.assertIn(f'check-net: as {self.USERS[0]}, in its namespace fork-fleet-{self.USERS[0]}', got.stdout)

def test_the_walls_proof_refuses_a_host_with_a_pools_users(self):
"""As root, with this class's runner users (a pool's part, as an idle pool's after a reboot): it refuses
before it makes anything, and names them."""
users = {p.pw_name for p in pwd.getpwall()}
got = subprocess.run(['bash', WallsProof.PROOF], capture_output=True, text=True, timeout=60)
self.assertEqual((got.returncode, got.stdout), (1, ''), got.stderr)
for user in self.USERS:
self.assertIn(f'user {user}', got.stderr)
self.assertEqual({p.pw_name for p in pwd.getpwall()}, users)

def test_the_walls_hold_in_a_real_systemd_unit(self):
"""As Launcher's, in a unit of the system's systemd as a real runner user (CI: the hosted runner's), and with
all of the sandbox but its network: only the allowed mounts, the pseudo devices, no socket of the host's, no
Expand Down
Loading