Skip to content

Commit 633f75d

Browse files
author
Hornet Agent
committed
fix: add -w flag to iptables calls to prevent boot race condition
The systemd service was failing on boot because another process held the xtables lock. The -w flag makes iptables wait for the lock instead of failing immediately. Also updated the stale persistence warning since hornet-firewall.service already handles that.
1 parent 5e6da35 commit 633f75d

1 file changed

Lines changed: 23 additions & 24 deletions

File tree

bin/setup-firewall.sh

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,64 +34,63 @@ CHAIN="HORNET_OUTPUT"
3434
echo "🔒 Setting up firewall rules for hornet_agent (uid $UID_HORNET)..."
3535

3636
# Clean up any existing rules first
37-
iptables -D OUTPUT -m owner --uid-owner "$UID_HORNET" -j "$CHAIN" 2>/dev/null || true
38-
iptables -F "$CHAIN" 2>/dev/null || true
39-
iptables -X "$CHAIN" 2>/dev/null || true
37+
iptables -w -D OUTPUT -m owner --uid-owner "$UID_HORNET" -j "$CHAIN" 2>/dev/null || true
38+
iptables -w -F "$CHAIN" 2>/dev/null || true
39+
iptables -w -X "$CHAIN" 2>/dev/null || true
4040

4141
# Create a dedicated chain for hornet_agent
42-
iptables -N "$CHAIN"
42+
iptables -w -N "$CHAIN"
4343

4444
# ── Localhost: allow only specific services ──────────────────────────────────
4545

4646
# Allow Slack bridge (outbound API)
47-
iptables -A "$CHAIN" -o lo -p tcp --dport 7890 -j ACCEPT
47+
iptables -w -A "$CHAIN" -o lo -p tcp --dport 7890 -j ACCEPT
4848

4949
# Allow Ollama (local LLM inference)
50-
iptables -A "$CHAIN" -o lo -p tcp --dport 11434 -j ACCEPT
50+
iptables -w -A "$CHAIN" -o lo -p tcp --dport 11434 -j ACCEPT
5151

5252
# Allow DNS on localhost
53-
iptables -A "$CHAIN" -o lo -p udp --dport 53 -j ACCEPT
54-
iptables -A "$CHAIN" -o lo -p tcp --dport 53 -j ACCEPT
53+
iptables -w -A "$CHAIN" -o lo -p udp --dport 53 -j ACCEPT
54+
iptables -w -A "$CHAIN" -o lo -p tcp --dport 53 -j ACCEPT
5555

5656
# Allow localhost responses (established connections back to us)
57-
iptables -A "$CHAIN" -o lo -m state --state ESTABLISHED,RELATED -j ACCEPT
57+
iptables -w -A "$CHAIN" -o lo -m state --state ESTABLISHED,RELATED -j ACCEPT
5858

5959
# Block everything else on localhost
60-
iptables -A "$CHAIN" -o lo -j LOG --log-prefix "HORNET_LOCAL_BLOCKED: " --log-level 4
61-
iptables -A "$CHAIN" -o lo -j DROP
60+
iptables -w -A "$CHAIN" -o lo -j LOG --log-prefix "HORNET_LOCAL_BLOCKED: " --log-level 4
61+
iptables -w -A "$CHAIN" -o lo -j DROP
6262

6363
# ── Internet: allow only standard ports ──────────────────────────────────────
6464

6565
# Allow DNS (UDP + TCP)
66-
iptables -A "$CHAIN" -p udp --dport 53 -j ACCEPT
67-
iptables -A "$CHAIN" -p tcp --dport 53 -j ACCEPT
66+
iptables -w -A "$CHAIN" -p udp --dport 53 -j ACCEPT
67+
iptables -w -A "$CHAIN" -p tcp --dport 53 -j ACCEPT
6868

6969
# Allow HTTP/HTTPS (web browsing, all APIs)
70-
iptables -A "$CHAIN" -p tcp --dport 80 -j ACCEPT
71-
iptables -A "$CHAIN" -p tcp --dport 443 -j ACCEPT
70+
iptables -w -A "$CHAIN" -p tcp --dport 80 -j ACCEPT
71+
iptables -w -A "$CHAIN" -p tcp --dport 443 -j ACCEPT
7272

7373
# Allow SSH (git push/pull)
74-
iptables -A "$CHAIN" -p tcp --dport 22 -j ACCEPT
74+
iptables -w -A "$CHAIN" -p tcp --dport 22 -j ACCEPT
7575

7676
# Allow established/related (responses to allowed outbound)
77-
iptables -A "$CHAIN" -m state --state ESTABLISHED,RELATED -j ACCEPT
77+
iptables -w -A "$CHAIN" -m state --state ESTABLISHED,RELATED -j ACCEPT
7878

7979
# Log and drop everything else
80-
iptables -A "$CHAIN" -j LOG --log-prefix "HORNET_BLOCKED: " --log-level 4
81-
iptables -A "$CHAIN" -j DROP
80+
iptables -w -A "$CHAIN" -j LOG --log-prefix "HORNET_BLOCKED: " --log-level 4
81+
iptables -w -A "$CHAIN" -j DROP
8282

8383
# Jump to our chain for all hornet_agent traffic
84-
iptables -A OUTPUT -m owner --uid-owner "$UID_HORNET" -j "$CHAIN"
84+
iptables -w -A OUTPUT -m owner --uid-owner "$UID_HORNET" -j "$CHAIN"
8585

8686
echo "✅ Firewall active. Rules:"
8787
echo ""
88-
iptables -L "$CHAIN" -n -v --line-numbers
88+
iptables -w -L "$CHAIN" -n -v --line-numbers
8989
echo ""
9090
echo "Localhost allowed: 7890 (bridge), 11434 (ollama), 53 (dns)"
9191
echo "Internet allowed: 80, 443, 22, 53"
9292
echo "Everything else: BLOCKED + LOGGED"
9393
echo ""
94-
echo "To remove: sudo iptables -D OUTPUT -m owner --uid-owner $UID_HORNET -j $CHAIN && sudo iptables -F $CHAIN && sudo iptables -X $CHAIN"
94+
echo "To remove: sudo iptables -w -D OUTPUT -m owner --uid-owner $UID_HORNET -j $CHAIN && sudo iptables -w -F $CHAIN && sudo iptables -w -X $CHAIN"
9595
echo ""
96-
echo "⚠️ These rules are NOT persistent across reboots."
97-
echo " To persist, add to a systemd unit or use iptables-save/iptables-restore."
96+
echo "Persistence: hornet-firewall.service (systemd)"

0 commit comments

Comments
 (0)