From 117831931394fcbb6e547ec0dcde10ad6ac135a9 Mon Sep 17 00:00:00 2001 From: Cory Snider Date: Tue, 30 May 2023 14:32:27 -0400 Subject: [PATCH] libn: fix resolver restore w/ chatty 'iptables -C' Resolver.setupIPTable() checks whether it needs to flush or create the user chains used for NATing container DNS requests by testing for the existence of the rules which jump to said user chains. Unfortunately it does so using the IPTable.RawCombinedOutputNative() method, which returns a non-nil error if the iptables command returns any output even if the command exits with a zero status code. While that is fine with iptables-legacy as it prints no output if the rule exists, iptables-nft v1.8.7 prints some information about the rule. Consequently, Resolver.setupIPTable() would incorrectly think that the rule does not exist during container restore and attempt to create it. This happened work work by coincidence before 8f5a9a741b70852bc6c9d675c6e0c4944022b467 because the failure to create the already-existing table would be ignored and the new NAT rules would be inserted before the stale rules left in the table from when the container was last started/restored. Now that failing to create the table is treated as a fatal error, the incompatibility with iptables-nft is no longer hidden. Switch to using IPTable.ExistsNative() to test for the existence of the jump rules as it correctly only checks the iptables command's exit status without regard for whether it outputs anything. Signed-off-by: Cory Snider --- libnetwork/resolver_unix.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libnetwork/resolver_unix.go b/libnetwork/resolver_unix.go index 62ced7d520..d0f47ddeeb 100644 --- a/libnetwork/resolver_unix.go +++ b/libnetwork/resolver_unix.go @@ -37,8 +37,7 @@ func (r *Resolver) setupIPTable() error { iptable := iptables.GetIptable(iptables.IPv4) // insert outputChain and postroutingchain - err := iptable.RawCombinedOutputNative("-t", "nat", "-C", "OUTPUT", "-d", resolverIP, "-j", outputChain) - if err == nil { + if iptable.ExistsNative("nat", "OUTPUT", "-d", resolverIP, "-j", outputChain) { if err := iptable.RawCombinedOutputNative("-t", "nat", "-F", outputChain); err != nil { setupErr = err return @@ -54,8 +53,7 @@ func (r *Resolver) setupIPTable() error { } } - err = iptable.RawCombinedOutputNative("-t", "nat", "-C", "POSTROUTING", "-d", resolverIP, "-j", postroutingChain) - if err == nil { + if iptable.ExistsNative("nat", "POSTROUTING", "-d", resolverIP, "-j", postroutingChain) { if err := iptable.RawCombinedOutputNative("-t", "nat", "-F", postroutingChain); err != nil { setupErr = err return