security: pod egress guard + remove banned members' pods

Member pods egress through pasta as the host. A pod could reach every
0.0.0.0 listener on the BBS host (ufw does not filter local delivery;
pasta maps the host at 169.254.1.2) and could open SSH to other fleet
servers from the BBS IP. On 2026-11-19 an attacker with a pod did both.

- deploy/podguard.sh: nftables table matched by socket cgroup (the
  service user's slice), so the agentbbs daemon is unaffected. Pods may
  only use the host's public ports + DNS; servers listed in
  /etc/agentbbs/podguard.env are dropped. Loaded after user@<uid> and
  every 5 minutes (nft binds the cgroup id at load time).
- pods.Manager.Remove + reapBannedPods: a banned member's pod is removed
  at start and every 2 minutes (it previously kept running for days).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Anthony Ettinger 2026-11-20 07:48:03 +00:00
parent 388360012c
commit aefaec6ecf
4 changed files with 134 additions and 0 deletions

View file

@ -252,6 +252,7 @@ func main() {
if m, err := pods.Detect(filepath.Join(dataDir, "users")); err == nil {
a.pods = m
log.Info("pods enabled", "engine", m.Engine())
go a.reapBannedPods()
} else {
log.Warn("pods disabled", "reason", err)
}
@ -2461,3 +2462,28 @@ func (a *app) handleHealth(w http.ResponseWriter, r *http.Request) {
}
_, _ = w.Write([]byte(`{"status":"ok","db":"ok"}`))
}
// reapBannedPods removes the pod of every banned member, at start and every
// two minutes. Banning blocks new sessions, but a pod is a long-running
// container (--restart unless-stopped) and shells left inside it keep
// running; on 2026-11-20 a banned member's pod had been up for four days.
func (a *app) reapBannedPods() {
reap := func() {
users, err := a.st.ListUsers(1_000_000)
if err != nil {
log.Warn("reap banned pods: list users", "err", err)
return
}
for _, u := range users {
if u.Banned && a.pods.Remove(u.Name) {
log.Info("removed pod of banned member", "user", u.Name)
}
}
}
reap()
t := time.NewTicker(2 * time.Minute)
defer t.Stop()
for range t.C {
reap()
}
}

62
deploy/podguard.sh Executable file
View file

@ -0,0 +1,62 @@
#!/bin/sh
# agentbbs-podguard: keep member pods away from this host's private services
# and from the operator's other servers.
#
# Pods are rootless podman under the service user's systemd slice and egress
# through pasta AS THIS HOST. Without this guard a pod can reach every
# 0.0.0.0 listener here (ufw does not filter local delivery; pasta also maps
# the host at 169.254.1.2) and can open connections to the operator's other
# boxes from this box's IP, which those boxes may trust. Both were abused on
# 2026-11-19.
#
# Matching is by socket cgroup, so the agentbbs daemon itself (system.slice)
# is unaffected. nft resolves the cgroup path to an id at load time, so this
# re-runs after user@<uid>.service starts and on a short timer.
#
# Config (optional): /etc/agentbbs/podguard.env
# PODGUARD_BLOCK4="1.2.3.4, 5.6.7.8" # other servers pods must never reach
# PODGUARD_BLOCK6="2001:db8::1"
# PODGUARD_PUBPORTS="22, 70, 80, 119, 443, 563, 2222, 6697" # host ports pods MAY use
set -eu
[ -f /etc/agentbbs/podguard.env ] && . /etc/agentbbs/podguard.env
SVC_USER="${SVC_USER:-agentbbs}"
U=$(id -u "$SVC_USER")
CG="user.slice/user-$U.slice"
[ -d "/sys/fs/cgroup/$CG" ] || { echo "podguard: $CG not present yet"; exit 0; }
SELF4="127.0.0.0/8, 169.254.0.0/16"
for a in $(hostname -I 2>/dev/null); do
case "$a" in *:*) ;; *) SELF4="$SELF4, $a" ;; esac
done
PUBPORTS="${PODGUARD_PUBPORTS:-22, 70, 80, 119, 443, 563, 2222, 6697}"
B4=""; B6=""
[ -n "${PODGUARD_BLOCK4:-}" ] && B4="set block4 { type ipv4_addr; flags interval; elements = { $PODGUARD_BLOCK4 } }"
[ -n "${PODGUARD_BLOCK6:-}" ] && B6="set block6 { type ipv6_addr; flags interval; elements = { $PODGUARD_BLOCK6 } }"
R4=""; R6=""
[ -n "$B4" ] && R4="ip daddr @block4 counter drop"
[ -n "$B6" ] && R6="ip6 daddr @block6 counter drop"
nft -f - <<NFT
table inet agentbbs_podguard
delete table inet agentbbs_podguard
table inet agentbbs_podguard {
$B4
$B6
set self4 { type ipv4_addr; flags interval; elements = { $SELF4 } }
set pubports { type inet_service; elements = { $PUBPORTS } }
chain out {
type filter hook output priority 0; policy accept;
socket cgroupv2 level 2 "$CG" jump pod
}
chain pod {
$R4
$R6
ip daddr @self4 udp dport 53 accept
ip daddr @self4 tcp dport @pubports accept
ip daddr @self4 counter drop
ip6 daddr ::1 counter drop
}
}
NFT
echo "podguard: loaded for $CG"

View file

@ -153,6 +153,17 @@ func (m *Manager) startAgent(s ssh.Session, user string) (sock string, cleanup f
return "/run/agentbbs-agent/" + fname, func() { _ = l.Close(); _ = os.Remove(hostSock) }
}
// Remove force-removes the member's pod container (stopping any shells in it).
// The home volume is kept so an unban restores their files. It returns true
// when a container was actually removed.
func (m *Manager) Remove(user string) bool {
name := m.containerName(user)
if err := exec.Command(m.engine, "container", "inspect", name).Run(); err != nil {
return false
}
return exec.Command(m.engine, "rm", "-f", name).Run() == nil
}
// Engine reports the active container engine.
func (m *Manager) Engine() string { return m.engine }

View file

@ -418,6 +418,41 @@ if [ -f "$SRC_DIR/pods/Containerfile" ]; then
fi
fi
# Pod egress guard: member pods must not reach this host's private services or
# the operator's other servers (see deploy/podguard.sh). Applied now, after the
# service user's systemd instance starts, and every 5 minutes.
install -m 0755 "$SRC_DIR/deploy/podguard.sh" /usr/local/sbin/agentbbs-podguard
cat > /etc/systemd/system/agentbbs-podguard.service <<UNIT
[Unit]
Description=AgentBBS pod egress guard (nftables)
After=user@${SVC_UID}.service nftables.service
Wants=user@${SVC_UID}.service
[Service]
Type=oneshot
Environment=SVC_USER=${SVC_USER}
ExecStart=/usr/local/sbin/agentbbs-podguard
[Install]
WantedBy=multi-user.target user@${SVC_UID}.service
UNIT
cat > /etc/systemd/system/agentbbs-podguard.timer <<UNIT
[Unit]
Description=Re-apply the AgentBBS pod egress guard
[Timer]
OnBootSec=2min
OnUnitActiveSec=5min
[Install]
WantedBy=timers.target
UNIT
systemctl daemon-reload
systemctl enable agentbbs-podguard.service agentbbs-podguard.timer >/dev/null 2>&1 || true
systemctl start agentbbs-podguard.timer >/dev/null 2>&1 || true
systemctl start agentbbs-podguard.service \
|| warn "pod egress guard failed to load — check: journalctl -u agentbbs-podguard -n20"
# ---- 6. environment file ---------------------------------------------------
ENV_DIR=/etc/agentbbs
install -d -m 0750 "$ENV_DIR"