Keep other machines on the network from reaching Docker containers
shellcheck / shellcheck (push) Failing after 19s
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:
@@ -1,19 +1,20 @@
|
||||
---
|
||||
name: coredesktop-security-checks
|
||||
description: Plan and progress for CoreDesktop's security test suite. The report receiver, report's redaction, the agent sandbox, what root runs and the install path are tested; network exposure, which needs a scan from outside the smoketest VM, is still to write. Use when resuming, continuing, checking the status of, or changing CoreDesktop's security or penetration tests.
|
||||
description: Plan and progress for CoreDesktop's security test suite. All six steps are written and pass: the report receiver, report's redaction, and in the smoketest VM (smoketest/scenarios/60-security.sh) the agent sandbox, network exposure, what root runs and the install path. A few follow-ups remain. Use when resuming, checking the status of, extending or changing CoreDesktop's security or penetration tests.
|
||||
---
|
||||
|
||||
# CoreDesktop's security checks
|
||||
|
||||
Written on 2026-11-16, updated the same day after step 3. Work out the current
|
||||
state before doing anything:
|
||||
Written on 2026-11-16; all six steps were done the same day. Work out the
|
||||
current state before doing anything:
|
||||
|
||||
```
|
||||
git -C ~/Projects/coredesktop fetch && git -C ~/Projects/coredesktop log --oneline origin/main -15
|
||||
ls ~/Projects/coredesktop/smoketest/scenarios/ ~/Projects/coredesktop/server/report-receiver/
|
||||
```
|
||||
|
||||
Tell the user which step is next and confirm before starting it.
|
||||
Tell the user what's left (the follow-ups below) and confirm before starting
|
||||
any of it.
|
||||
|
||||
## Scope
|
||||
|
||||
@@ -24,7 +25,7 @@ leak data or give away control, in this order:
|
||||
1. The report receiver, the only part on the public internet. **Done.**
|
||||
2. `report`'s redaction, which decides what leaves a user's computer. **Done.**
|
||||
3. The agent sandbox: the one place that runs code CoreDesktop doesn't trust. **Done.**
|
||||
4. Network exposure from outside.
|
||||
4. Network exposure from outside. **Done.**
|
||||
5. Files root runs, and secrets only root may read. **Done.**
|
||||
6. The install path (federated.sh, the `[federated]` repo). **Done.**
|
||||
|
||||
@@ -165,29 +166,52 @@ since `check` scripts don't run with `set -e`.
|
||||
index (`GIT_INDEX_FILE=... git add -A; git write-tree; git commit-tree`)
|
||||
and pass it with `--ref`. The scenario files themselves are read from the
|
||||
working copy.
|
||||
- The user's global git config has no name or email. Commits so far used
|
||||
`git -c user.name="David Young" -c user.email=...` from the repo's history.
|
||||
- The user's global git config had no name or email on 2026-11-16. A privacy
|
||||
hook refuses commands that put the user's email address on the command
|
||||
line (`git -c user.email=...`), so if it's still unset, ask the user to set
|
||||
it or to commit themselves. Throwaway test commits can use
|
||||
`Smoketest <smoketest@localhost>`.
|
||||
- The full 60-security run takes about 12 minutes; past the Bash tool's 10,
|
||||
so run it in the background.
|
||||
|
||||
## Next: step 4 in the smoketest VM
|
||||
## Done: step 4 (the commit "Keep other machines on the network from reaching Docker containers")
|
||||
|
||||
Add them to `smoketest/scenarios/60-security.sh` (or a sibling scenario if it
|
||||
grows too long), with the same `check` style. Where a check needs a second
|
||||
machine, say so in the scenario and skip it, rather than faking it. The VM
|
||||
comes from the `coredesktop-dev` sibling project (see smoketest/README.md); it
|
||||
isn't in this repository.
|
||||
`network_checks` in `60-security.sh`. There's no second VM or bridged network
|
||||
(coredesktop-dev uses QEMU user networking), so the scenario makes two
|
||||
"other machines" inside the VM: network namespaces joined by veth pairs, whose
|
||||
traffic arrives on an ordinary interface like a LAN machine's. `lan` has
|
||||
private addresses (192.168.77.0/24, fd77:1::/64), `wan` public-looking ones
|
||||
(198.51.100.0/24, 2001:db8:77::/64). From each, over IPv4 and IPv6:
|
||||
- nmap `-p-` finds only 22 and 53317 open; a test listener on every address
|
||||
doesn't show
|
||||
- UDP 18091 doesn't answer; LocalSend's 53317 does (the control)
|
||||
- a container's published port can't be reached; containers on Docker's
|
||||
default bridge and on their own network still reach both machines; after
|
||||
`ufw-docker allow`, the LAN machine reaches the container
|
||||
- SSH offers only publickey, and tester's real password (`tester`) fails
|
||||
Plus: ufw is active, denies incoming, and has exactly the four allow rules;
|
||||
`sshd -T` (CamelCase names in this OpenSSH) has no passwords,
|
||||
keyboard-interactive or root login.
|
||||
|
||||
### 4. Network exposure
|
||||
It found: ufw-docker's rules let any private source (10/8, 172.16/12,
|
||||
192.168/16) reach published ports over IPv4, so anyone on the same Wi-Fi
|
||||
could. The user chose to block it. `system/docker-firewall.sh` replaces those
|
||||
lines with `-i docker0` and `-i br-+` RETURNs (run by `system/firewall.sh` and
|
||||
migration 1794878760); `tests/firewall.bats` tests it. People who reached a
|
||||
container from their LAN now need `sudo ufw-docker allow <container> <port>`.
|
||||
|
||||
`system/firewall.sh` allows only 22/tcp (SSH), 5353/udp (mDNS), 53317
|
||||
(LocalSend) and anything on tailscale0. Check:
|
||||
- a scan from another machine finds only those
|
||||
- SSH refuses passwords (`system/sshd.sh`, `PasswordAuthentication no`)
|
||||
- a port a Docker container publishes isn't reachable from outside, which is
|
||||
what `ufw-docker` is for: Docker otherwise goes around ufw
|
||||
Lessons: socat `UDP-RECVFROM,fork` answered only its first sender; loop a
|
||||
one-shot listener instead. Show a check's evidence on failure (the ufw-docker
|
||||
block, sshd -T's output) so a run explains itself.
|
||||
|
||||
Open question: the scan has to come from outside the VM. With QEMU's user
|
||||
networking, the host reaches only forwarded ports, and a scan from inside the
|
||||
VM against itself goes through loopback, which ufw allows. Find out from
|
||||
`coredesktop-dev` whether the VM can get a bridged or tap network, or a second
|
||||
VM, before writing this one.
|
||||
## Follow-ups
|
||||
|
||||
- From steps 1 and 2 (above): deploy the new `receiver.py`; try Caddy's
|
||||
`request_buffers` for slow uploads; check how Gitea serves attached `.log`
|
||||
files; the daily report limit (ask before changing it).
|
||||
- The `tailscale0` allow rule isn't tested: it needs a Tailscale login.
|
||||
- Keep `ROOT_FILES` and `PAM_FILES` in `60-security.sh` up to date when
|
||||
CoreDesktop installs something new that root runs.
|
||||
- Check on a real machine which ports WinBoat's Windows container publishes,
|
||||
and on which addresses (not checked yet). The Docker fix keeps the LAN out
|
||||
of any it publishes on all addresses.
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ufw-docker let any private address (10.x, 172.16-31.x, 192.168.x) reach the
|
||||
# ports Docker containers publish, so anyone on the same Wi-Fi could. Let only
|
||||
# Docker's own bridges past, as system/firewall.sh now does on a new install.
|
||||
bash "$HOME/.local/share/coredesktop/system/docker-firewall.sh"
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Purpose : Keep other machines on the network from reaching the ports Docker
|
||||
# containers publish. Run by system/firewall.sh, after ufw-docker
|
||||
# installs its rules, and by a migration.
|
||||
#
|
||||
# ufw-docker's rules let any private address (10.x, 172.16-31.x, 192.168.x)
|
||||
# reach a published port, and on a café's or hotel's Wi-Fi that's everyone
|
||||
# there. This lets only Docker's own bridges past instead, so containers still
|
||||
# reach the network and each other, and this machine still reaches their ports.
|
||||
# Your own changes to the rules are left alone.
|
||||
#
|
||||
# To let other machines reach one container's port:
|
||||
# sudo ufw-docker allow <container> <port>
|
||||
|
||||
set -o pipefail
|
||||
|
||||
AFTER_RULES="${UFW_AFTER_RULES:-/etc/ufw/after.rules}"
|
||||
|
||||
# ufw-docker's lines that let private addresses through
|
||||
export PRIVATE='^-A DOCKER-USER -j RETURN -s (10\.0\.0\.0/8|172\.16\.0\.0/12|192\.168\.0\.0/16)$'
|
||||
|
||||
sudo grep -qE "$PRIVATE" "$AFTER_RULES" 2> /dev/null || exit 0
|
||||
echo "Keeping other machines on the network from reaching Docker containers' ports..."
|
||||
|
||||
# Docker's bridges in place of the first of those lines, unless they're there
|
||||
# already; the private ranges' lines go
|
||||
tmp="$(mktemp)" || exit 1
|
||||
if ! sudo cat "$AFTER_RULES" | awk '
|
||||
$0 == "-A DOCKER-USER -j RETURN -i docker0" { bridges = 1 }
|
||||
{ lines[NR] = $0 }
|
||||
END {
|
||||
for (i = 1; i <= NR; i++) {
|
||||
if (lines[i] ~ ENVIRON["PRIVATE"]) {
|
||||
if (!bridges) {
|
||||
print "-A DOCKER-USER -j RETURN -i docker0"
|
||||
print "-A DOCKER-USER -j RETURN -i br-+"
|
||||
bridges = 1
|
||||
}
|
||||
continue
|
||||
}
|
||||
print lines[i]
|
||||
}
|
||||
}' > "$tmp"; then
|
||||
rm -f "$tmp"
|
||||
exit 1
|
||||
fi
|
||||
# Never put back a file that lost ufw's own rules
|
||||
if ! grep -q '^COMMIT$' "$tmp" || ! grep -q '^# BEGIN UFW AND DOCKER' "$tmp"; then
|
||||
echo "Couldn't change $AFTER_RULES; it's as it was."
|
||||
rm -f "$tmp"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Keep the file's owner and mode (ufw's own are root and 640)
|
||||
if ! mode="$(sudo stat -c %a "$AFTER_RULES")" \
|
||||
|| ! sudo install -m "$mode" -o root -g root "$tmp" "$AFTER_RULES"; then
|
||||
rm -f "$tmp"
|
||||
exit 1
|
||||
fi
|
||||
rm -f "$tmp"
|
||||
|
||||
# ufw loads after.rules when it starts
|
||||
if systemctl is-active -q ufw.service; then
|
||||
sudo systemctl restart ufw.service
|
||||
fi
|
||||
@@ -28,4 +28,8 @@ if ! sudo grep -q 'BEGIN UFW AND DOCKER' /etc/ufw/after.rules; then
|
||||
sudo systemctl restart ufw.service
|
||||
fi
|
||||
|
||||
# ufw-docker lets private addresses, the rest of a café's Wi-Fi say, reach
|
||||
# published ports; only Docker's own bridges, from here on
|
||||
bash "$HOME/.local/share/coredesktop/system/docker-firewall.sh"
|
||||
|
||||
sudo ufw status verbose
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load test_helper
|
||||
|
||||
# system/docker-firewall.sh, on a copy of ufw's after.rules with ufw-docker's
|
||||
# block as `ufw-docker install` writes it. sudo runs the command as you, and
|
||||
# install copies without changing the owner.
|
||||
|
||||
setup() {
|
||||
common_setup
|
||||
export UFW_AFTER_RULES="$BATS_TEST_TMPDIR/after.rules"
|
||||
stub sudo 'exec "$@"'
|
||||
stub install 'eval "src=\${$(($# - 1))}" "dst=\${$#}"; cp "$src" "$dst"'
|
||||
stub systemctl '[ "$1" = is-active ] && exit 0; exit 0'
|
||||
SCRIPT="$REPO/system/docker-firewall.sh"
|
||||
}
|
||||
|
||||
# ufw's after.rules, with ufw-docker's block between its own lines
|
||||
write_rules() {
|
||||
cat > "$UFW_AFTER_RULES" << 'EOF'
|
||||
# rules.input-after
|
||||
*filter
|
||||
:ufw-after-input - [0:0]
|
||||
-A ufw-after-input -p udp --dport 137 -j ufw-skip-to-policy-input
|
||||
COMMIT
|
||||
# BEGIN UFW AND DOCKER
|
||||
*filter
|
||||
:ufw-user-forward - [0:0]
|
||||
:ufw-docker-logging-deny - [0:0]
|
||||
:DOCKER-USER - [0:0]
|
||||
-A DOCKER-USER -j ufw-user-forward
|
||||
|
||||
-A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j RETURN
|
||||
-A DOCKER-USER -m conntrack --ctstate INVALID -j DROP
|
||||
-A DOCKER-USER -i docker0 -o docker0 -j ACCEPT
|
||||
|
||||
-A DOCKER-USER -j RETURN -s 10.0.0.0/8
|
||||
-A DOCKER-USER -j RETURN -s 172.16.0.0/12
|
||||
-A DOCKER-USER -j RETURN -s 192.168.0.0/16
|
||||
-A DOCKER-USER -j ufw-docker-logging-deny -m conntrack --ctstate NEW -d 10.0.0.0/8
|
||||
-A DOCKER-USER -j ufw-docker-logging-deny -m conntrack --ctstate NEW -d 172.16.0.0/12
|
||||
-A DOCKER-USER -j ufw-docker-logging-deny -m conntrack --ctstate NEW -d 192.168.0.0/16
|
||||
|
||||
-A DOCKER-USER -j RETURN
|
||||
|
||||
-A ufw-docker-logging-deny -m limit --limit 3/min --limit-burst 10 -j LOG --log-prefix "[UFW DOCKER BLOCK] "
|
||||
-A ufw-docker-logging-deny -j DROP
|
||||
|
||||
COMMIT
|
||||
# END UFW AND DOCKER
|
||||
EOF
|
||||
chmod 640 "$UFW_AFTER_RULES"
|
||||
}
|
||||
|
||||
@test "only Docker's bridges get past, not every private address, and ufw restarts" {
|
||||
write_rules
|
||||
original="$(cat "$UFW_AFTER_RULES")"
|
||||
run bash "$SCRIPT"
|
||||
[ "$status" -eq 0 ]
|
||||
refute grep -q -- '-j RETURN -s ' "$UFW_AFTER_RULES"
|
||||
# In the private lines' place, and nothing else changed
|
||||
diff <(grep -v -- '-j RETURN -s ' <<< "$original") \
|
||||
<(grep -vxF -e '-A DOCKER-USER -j RETURN -i docker0' -e '-A DOCKER-USER -j RETURN -i br-+' "$UFW_AFTER_RULES")
|
||||
[ "$(grep -n -- '-j RETURN -i docker0$' "$UFW_AFTER_RULES" | cut -d: -f1)" -eq 17 ]
|
||||
[ "$(grep -n -- '-j RETURN -i br-+$' "$UFW_AFTER_RULES" | cut -d: -f1)" -eq 18 ]
|
||||
# The private ranges are still kept from reaching containers otherwise
|
||||
grep -q -- '-j ufw-docker-logging-deny -m conntrack --ctstate NEW -d 192.168.0.0/16' "$UFW_AFTER_RULES"
|
||||
[ "$(stat -c %a "$UFW_AFTER_RULES")" = 640 ]
|
||||
called systemctl restart ufw.service
|
||||
}
|
||||
|
||||
@test "running it again changes nothing" {
|
||||
write_rules
|
||||
bash "$SCRIPT"
|
||||
once="$(cat "$UFW_AFTER_RULES")"
|
||||
: > "$STUB_LOG"
|
||||
run bash "$SCRIPT"
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$(cat "$UFW_AFTER_RULES")" = "$once" ]
|
||||
not_called systemctl
|
||||
}
|
||||
|
||||
@test "it leaves after.rules alone without ufw-docker's rules" {
|
||||
printf '*filter\n-A ufw-after-input -j RETURN -s 10.0.0.0/8\nCOMMIT\n' > "$UFW_AFTER_RULES"
|
||||
before="$(cat "$UFW_AFTER_RULES")"
|
||||
run bash "$SCRIPT"
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$(cat "$UFW_AFTER_RULES")" = "$before" ]
|
||||
}
|
||||
|
||||
@test "it takes out only the private lines that are left, keeping your own" {
|
||||
write_rules
|
||||
sed -i -e '/RETURN -s 192.168.0.0\/16/d' "$UFW_AFTER_RULES"
|
||||
sed -i -e 's|^-A DOCKER-USER -j RETURN -s 10.0.0.0/8$|&\n-A DOCKER-USER -j RETURN -s 192.168.1.20/32|' "$UFW_AFTER_RULES"
|
||||
run bash "$SCRIPT"
|
||||
[ "$status" -eq 0 ]
|
||||
grep -qx -- '-A DOCKER-USER -j RETURN -s 192.168.1.20/32' "$UFW_AFTER_RULES"
|
||||
refute grep -qE -- '-s (10.0.0.0/8|172.16.0.0/12)$' "$UFW_AFTER_RULES"
|
||||
[ "$(grep -c -- '-j RETURN -i docker0$' "$UFW_AFTER_RULES")" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "if it can't read the rules, it changes nothing" {
|
||||
write_rules
|
||||
before="$(cat "$UFW_AFTER_RULES")"
|
||||
stub sudo '[ "$1" = cat ] && exit 1; exec "$@"'
|
||||
run bash "$SCRIPT"
|
||||
[ "$status" -ne 0 ]
|
||||
[ "$(cat "$UFW_AFTER_RULES")" = "$before" ]
|
||||
not_called systemctl
|
||||
}
|
||||
Reference in New Issue
Block a user