Skip to content

libnetwork/d/bridge: fix cleanup of 27.x iptables - #54129

Merged
thaJeztah merged 2 commits into
moby:masterfrom
corhere:fix-nftables-cleanup-legacy-chains
Nov 4, 2026
Merged

thaJeztah merged 2 commits into
moby:masterfrom
corhere:fix-nftables-cleanup-legacy-chains

Conversation

@corhere

@corhere corhere commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Summary

When the daemon starts with the nftables firewall backend after a release older than 28.0.1 ran with iptables, the iptables cleaner leaves behind the per-bridge ACCEPT and ICC rules that release put in filter-FORWARD. Delete them all as each network is restored, including internal networks.

With the iptables backend, the legacy -i <bridge> ! -o <bridge> -j ACCEPT rule is now deleted after the DOCKER-FORWARD rule that replaces it is added. For a moment, both are present, and both accept outgoing packets from the bridge.

Releases older than 28.0.0 also jumped from filter-FORWARD to the filter table's DOCKER chain for each bridge. The cleaner can't remove the chain at startup while those jumps exist, and the empty chain remains. Remove Docker's chains again once the bridge driver has restored its networks.

Release notes (optional)

- Fix iptables rules and an empty `DOCKER` chain left behind when the daemon switches to the nftables firewall backend after a release older than 28.0.1 ran with iptables.

Created with: Claude Code

When the daemon started with the nftables backend after a release older
than 28.0.1 had run with iptables, the iptables cleaner's DelNetwork
left the per-bridge rules those releases put in filter-FORWARD:
"-i docker0 ! -o docker0 -j ACCEPT" and the ICC rules. The iptables
backend deleted them in setupNonInternalNetworkRules and setIcc, but
only when adding a network's rules, and DelNetwork returned early for an
internal network.

Delete all the legacy per-bridge FORWARD rules in
deleteLegacyFilterRules, which the iptables backend and the cleaner call
for every network.

With the iptables backend, setupIPTables deletes the legacy rules after
calling setupNonInternalNetworkRules, which used to delete
"-i docker0 ! -o docker0 -j ACCEPT" before adding the DOCKER-FORWARD
rule that replaces it. Now, for a moment, both rules are present. Both
accept outgoing packets from the bridge.

Signed-off-by: Cory Snider <csnider@mirantis.com>
When the daemon started with the nftables backend after a release older
than 28.0.0 had run with iptables, NewCleaner deleted the jumps from
filter-FORWARD to DOCKER-FORWARD and DOCKER-ISOLATION-STAGE-1, then
removed Docker's chains. Those releases also jumped from FORWARD to the
filter table's DOCKER chain for each bridge ("-o docker0 -j DOCKER").
iptables refuses to delete a chain that a rule still jumps to, and
removeIPChains ignores the error. The cleaner's DelNetwork deleted the
per-bridge jump later, when the bridge driver restored the network, but
nothing tried to remove the chain again. The empty DOCKER chain
remained.

When the bridge driver has restored its networks and endpoints, call
the cleaner's new Done method, which removes the chains again.

Signed-off-by: Cory Snider <csnider@mirantis.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the legacy iptables cleanup changes.

No open issues 🎉

What changed in this PR

This PR improves the bridge driver’s cleanup of legacy iptables rules when switching to nftables, removing per-bridge rules and chains left by older daemon releases.

Changes:

  • Centralizes legacy FORWARD-rule cleanup, including internal networks, while preserving outgoing acceptance during iptables setup.
  • Adds a cleanup-completion hook to retry chain removal after networks and endpoints are restored.
  • Adds IPv4/IPv6 cleaner tests and an integration test simulating a switch from the 27.x firewall layout.
File Description
integration/​network/​bridge/​bridge_linux_test.go Tests cleanup when switching from simulated 27.x rules to nftables.
daemon/​libnetwork/​drivers/​bridge/​internal/​nftabler/​cleaner.go Finalizes cleanup before releasing the cleaner.
daemon/​libnetwork/​drivers/​bridge/​internal/​iptabler/​network.go Consolidates legacy per-bridge rule deletion and covers internal networks.
daemon/​libnetwork/​drivers/​bridge/​internal/​iptabler/​iptabler_test.go Tests legacy rule and chain removal for IPv4 and IPv6.
daemon/​libnetwork/​drivers/​bridge/​internal/​iptabler/​cleaner.go Cleans internal-network rules and retries chain deletion after restoration.
daemon/​libnetwork/​drivers/​bridge/​internal/​firewaller/​firewaller.go Adds the cleaner’s Done lifecycle method.
daemon/​libnetwork/​drivers/​bridge/​bridge_store.go Invokes cleanup completion after restoring stored networks and endpoints.

Review effort: Balanced

@robmry robmry left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thank you

@corhere corhere added this to the 29.10.0 milestone Nov 4, 2026

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thaJeztah
thaJeztah merged commit 43ffffd into moby:master Nov 4, 2026
390 of 405 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants