Keep other machines on the network from reaching Docker containers
shellcheck / shellcheck (push) Failing after 19s

60-security.sh now checks network exposure (step 4), from two other
machines made of network namespaces in the VM: one on private addresses,
like a neighbour on the same Wi-Fi, and one on public-looking ones. From
each, over IPv4 and IPv6: only SSH and LocalSend are open, a container's
published port can't be reached, containers still reach both machines,
and SSH takes keys only. It also checks ufw's rules and sshd -T.

It found that ufw-docker's rules let any private address (10/8,
172.16/12, 192.168/16) reach published ports, so anyone on a café's or
hotel's Wi-Fi could. system/docker-firewall.sh lets only Docker's own
bridges (docker0, br-+) past instead; firewall.sh runs it on a new
install and migration 1794878760 on existing machines. To let other
machines reach a container's port: sudo ufw-docker allow <container> <port>.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 07:32:44 -07:00
co-authored by Claude Opus 5.5
parent 203754c632
commit ffbaf71ba7
6 changed files with 413 additions and 27 deletions
+178 -2
View File
@@ -1,6 +1,6 @@
#!/bin/bash
# shellcheck disable=SC2034,SC2016,SC2028
DESCRIPTION="security: the agent sandbox, what root runs, and the install path"
DESCRIPTION="security: the agent sandbox, network exposure, what root runs, and the install path"
IMAGE=installed
TIER="nightly"
@@ -17,7 +17,8 @@ TIER="nightly"
# is in a user's home, and a security key or fingerprint for sudo keeps the
# password working. Step 6, last: federated.sh hands out install.sh over HTTPS
# only, and pacman takes [federated]'s packages only when signed by a key it
# trusts. Step 4 (network exposure) needs a second machine and is still to come.
# trusts. Step 4, network exposure, is seen from two other machines: network
# namespaces in the VM, one on private addresses and one on public-looking ones.
AGENTS=(opencode hermes cline-desktop)
FAKE_BIN="\$HOME/smoketest-agents"
@@ -462,6 +463,180 @@ install_path_checks() {
check "install: pacman refuses an unsigned package from a signed repo" "$(refused unsigned-pkg trusted)"
}
# --- Step 4: network exposure ------------------------------------------------
# Two other machines, each a network namespace joined to this one by a veth
# pair. What they send arrives on an ordinary interface, not loopback, so ufw
# and Docker treat it as they would a machine on the network. One has private
# addresses, like a neighbour on the same Wi-Fi; the other public-looking ones
# (TEST-NET-2 and 2001:db8::/32, the documentation ranges), like the internet.
# name this machine's side the other machine
OUTSIDE=(
"lan 192.168.77.1 fd77:1::1 192.168.77.2 fd77:1::2"
"wan 198.51.100.1 2001:db8:77::1 198.51.100.2 2001:db8:77::2"
)
# What system/firewall.sh lets in; tailscale0 isn't here (it needs a Tailscale
# login, so it's not checked)
OPEN_TCP="22 53317"
# Listeners started for the checks: one on every address, and a container's
TEST_TCP=18090
TEST_UDP=18091
LOCALSEND=53317
DOCKER_PORT=18095
# A server on each other machine, for containers to reach
OUTSIDE_PORT=18096
# udp_to ADDRESS PORT: socat's address for a UDP port on ADDRESS
udp_to() {
if [[ $1 == *:* ]]; then echo "UDP6:[$1]:$2"; else echo "UDP4:$1:$2"; fi
}
# url_host ADDRESS: ADDRESS as it goes in a URL
url_host() {
if [[ $1 == *:* ]]; then echo "[$1]"; else echo "$1"; fi
}
# outside NAME SCRIPT: a VM script that runs SCRIPT as root on machine NAME
outside() {
echo "sudo ip netns exec smoketest-$1 bash -c $(q "$2")"
}
setup_outside() {
local machine name here here6 there there6
for machine in "${OUTSIDE[@]}"; do
read -r name here here6 there there6 <<< "$machine"
assert_vm "make the other machine '$name' ($there, $there6)" "set -e
sudo ip netns add smoketest-$name
sudo ip link add sm-$name type veth peer name sm-$name-out
sudo ip link set sm-$name-out netns smoketest-$name
sudo ip addr add $here/24 dev sm-$name
sudo ip -6 addr add $here6/64 dev sm-$name nodad
sudo ip link set sm-$name up
sudo ip netns exec smoketest-$name sh -c '
ip link set lo up
ip addr add $there/24 dev sm-$name-out
ip -6 addr add $there6/64 dev sm-$name-out nodad
ip link set sm-$name-out up'
sudo ip netns exec smoketest-$name ping -c 1 -W 5 $here > /dev/null"
done
assert_vm "install nmap" "sudo pacman -S --needed --noconfirm nmap > /dev/null"
assert_vm "start test listeners on every address, TCP $TEST_TCP and UDP $TEST_UDP and $LOCALSEND" "set -e
printf '#!/bin/sh\nprintf \"HTTP/1.0 200 OK\\\\r\\\\n\\\\r\\\\nlistener\\\\n\"\n' > \$HOME/smoketest-respond
chmod +x \$HOME/smoketest-respond
# The other machines run it as root, from here
cp \$HOME/smoketest-respond $SMOKETEST_VM_DIR/
setsid socat TCP6-LISTEN:$TEST_TCP,ipv6only=0,fork,reuseaddr EXEC:\$HOME/smoketest-respond > /dev/null 2>&1 < /dev/null &
for port in $TEST_UDP $LOCALSEND; do
ss -Hlun \"sport = :\$port\" | grep -q . && { echo \"UDP \$port is taken\"; exit 1; }
# One packet each, then out after a second idle: socat with fork
# answered only the first sender
setsid bash -c \"while :; do socat -T 1 UDP6-RECVFROM:\$port,ipv6only=0,reuseaddr EXEC:cat; done\" \
> /dev/null 2>&1 < /dev/null &
done
sleep 1
curl -sf http://127.0.0.1:$TEST_TCP/ > /dev/null
[ \"\$(echo ping | socat -t 3 - UDP4:127.0.0.1:$TEST_UDP)\" = ping ]
sleep 2
[ \"\$(echo ping | socat -t 3 - UDP6:[::1]:$TEST_UDP)\" = ping ]"
for machine in "${OUTSIDE[@]}"; do
read -r name _ <<< "$machine"
assert_vm "start a server on '$name', port $OUTSIDE_PORT" "
$(outside "$name" "setsid socat TCP-LISTEN:$OUTSIDE_PORT,fork,reuseaddr EXEC:$SMOKETEST_VM_DIR/smoketest-respond > /dev/null 2>&1 < /dev/null &")
sleep 1"
done
assert_vm "make a Docker network of its own (a br- bridge)" "sudo docker network create smoketest-net > /dev/null"
assert_vm "start a container that publishes port $DOCKER_PORT" "set -e
sudo docker run -d --rm --name smoketest-web -p $DOCKER_PORT:8080 busybox httpd -f -p 8080 > /dev/null
for _ in \$(seq 50); do
[ \"\$(curl -s -o /dev/null -w '%{http_code}' -m 2 http://127.0.0.1:$DOCKER_PORT/)\" != 000 ] && exit 0
sleep 0.2
done
exit 1"
}
network_checks() {
local machine name here here6 there there6 address
setup_outside
# --- The firewall's own rules ---
check "net: ufw is on, denies incoming by default, and allows only SSH, mDNS, LocalSend and Tailscale" "
status=\$(sudo ufw status verbose)
printf '%s\n' \"\$status\"
grep -qx 'Status: active' <<< \"\$status\" && grep -q '^Default: deny (incoming)' <<< \"\$status\" || exit 1
added=\$(sudo ufw show added | grep '^ufw ' | sort)
expected=\$(printf '%s\n' \"ufw allow 22/tcp comment 'SSH'\" \"ufw allow 5353/udp comment 'mDNS'\" \
\"ufw allow 53317 comment 'LocalSend'\" \"ufw allow in on tailscale0 comment 'Tailscale'\" | sort)
printf 'rules:\n%s\n' \"\$added\"
[ \"\$added\" = \"\$expected\" ]"
for machine in "${OUTSIDE[@]}"; do
read -r name here here6 there there6 <<< "$machine"
# --- What's open, seen from the other machine ---
for address in "$here" "$here6"; do
check "net: from $name, the only TCP ports open on $address are $OPEN_TCP" "
out=\$($(outside "$name" "nmap -n -Pn -sS -p- --min-rate 5000 --max-retries 1 \
$([[ $address == *:* ]] && echo -6) -oG - $address"))
open=\$(grep -o '[0-9]*/open/tcp' <<< \"\$out\" | cut -d/ -f1 | sort -n | tr '\n' ' ')
echo \"open: \$open\"
for port in \$open; do
case ' $OPEN_TCP ' in *\" \$port \"*) ;; *) echo \"\$port shouldn't be open\"; bad=1 ;; esac
done
# nmap saw the machine at all: SSH answered
[[ \" \$open \" == *' 22 '* ]] && [ -z \"\${bad:-}\" ]"
check "net: from $name, UDP $TEST_UDP on $address doesn't answer" "
reply=\$($(outside "$name" "echo ping | socat -t 3 - $(udp_to "$address" $TEST_UDP)"))
[ \"\$reply\" != ping ]"
check "net: from $name, UDP $LOCALSEND (LocalSend's) on $address answers" "
reply=\$($(outside "$name" "echo ping | socat -t 3 - $(udp_to "$address" $LOCALSEND)"))
[ \"\$reply\" = ping ]"
# ufw-docker's job: Docker's own rules publish ports around ufw
check "net: from $name, the container's port $DOCKER_PORT on $address can't be reached" "
code=\$($(outside "$name" "curl -s -o /dev/null -w '%{http_code}' -m 5 http://$(url_host "$address"):$DOCKER_PORT/"))
echo \"HTTP \$code\"
[ \"\$code\" = 000 ] || { sudo sed -n '/BEGIN UFW AND DOCKER/,/END UFW AND DOCKER/p' /etc/ufw/after.rules; exit 1; }"
done
# Containers still reach other machines, from Docker's default bridge and
# from a network of their own
for network in bridge smoketest-net; do
check "net: a container on Docker's $network network reaches $name ($there)" "
sudo docker run --rm --network $network busybox wget -q -T 10 -O - http://$there:$OUTSIDE_PORT/ | grep -qx listener"
done
# --- SSH: keys only ---
check "net: from $name, SSH offers only public keys" "
out=\$($(outside "$name" "ssh -o BatchMode=yes -o PubkeyAuthentication=no -o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null -o ConnectTimeout=10 tester@$here true 2>&1"))
echo \"\$out\"
[[ \$out == *'Permission denied (publickey).'* ]]"
check "net: from $name, tester's real password doesn't get in over SSH" "
out=\$($(outside "$name" "printf '#!/bin/sh\necho tester\n' > /tmp/smoketest-askpass; chmod +x /tmp/smoketest-askpass
SSH_ASKPASS=/tmp/smoketest-askpass SSH_ASKPASS_REQUIRE=force DISPLAY=none setsid -w ssh \
-o PubkeyAuthentication=no -o PreferredAuthentications=password,keyboard-interactive \
-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=10 \
-o NumberOfPasswordPrompts=1 tester@$here 'echo LOGGED-IN' 2>&1 < /dev/null"))
echo \"\$out\"
[[ \$out != *LOGGED-IN* && \$out == *'Permission denied'* ]]"
done
# The way to let other machines reach a container, as docker-firewall.sh says
read -r name here _ <<< "${OUTSIDE[0]}"
check "net: after ufw-docker allow, $name reaches the container's port" "
sudo ufw-docker allow smoketest-web 8080/tcp > /dev/null
code=\$($(outside "$name" "curl -s -o /dev/null -w '%{http_code}' -m 5 http://$here:$DOCKER_PORT/"))
echo \"HTTP \$code\"
[ \"\$code\" != 000 ]"
check "net: sshd allows no passwords, no keyboard-interactive and no root login" "
sudo sshd -T > /tmp/smoketest-sshd-T 2>&1 || { cat /tmp/smoketest-sshd-T; exit 1; }
# Newer OpenSSH prints the names in CamelCase
conf=\$(grep -iE '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin) ' /tmp/smoketest-sshd-T \
| tr '[:upper:]' '[:lower:]' | sort)
echo \"\$conf\"
[ \"\$conf\" = \"\$(printf '%s\n' 'kbdinteractiveauthentication no' 'passwordauthentication no' 'permitrootlogin no')\" ]"
}
sandbox_checks() {
local agent secret lan="http://\$(cat \$HOME/smoketest-lan-ip)"
@@ -532,6 +707,7 @@ sandbox_checks() {
scenario() {
sandbox_checks
network_checks
root_checks
install_path_checks