netfilter: nf_nat: also check reverse tuple to obtain clashing entry
Subsystems (3)
Florian Westphal
maintainerPATCH 1/2 netfilter: nf_nat: also check reverse tuple to obtain clashing entry
The logic added in the blamed commit was supposed to only omit nat source port allocation if neither the existing nor the new entry are subject to NAT.
However, its not enough to lookup the conntrack based on the proposed tuple, we must also check the reverse direction.
Otherwise there are esoteric cases where the collision is in the reverse direction because that colliding connection has a port rewrite, but the new entry doesn't. In this case, we only check the new entry and then erronously conclude that no clash exists anymore.
The existing (udp) tuple is:
a:p -> b:P, with nat translation to s:P, i.e. pure daddr rewrite, reverse tuple in conntrack table is s:P -> a:p.
When another UDP packet is sent directly to s, i.e. a:p->s:P, this is correctly detected as a colliding entry: tuple is taken by existing reply tuple in reverse direction.
But the colliding conntrack is only searched for with unreversed direction, and we can't find such entry matching a:p->s:P.
The incorrect conclusion is that the clashing entry has timed out and that no port address translation is required.
Such conntrack will then be discarded at nf_confirm time because the proposed reverse direction clashes with an existing mapping in the conntrack table.
Search for the reverse tuple too, this will then check the NAT bits of the colliding entry and triggers port reallocation.
Followp patch extends nft_nat.sh selftest to cover this scenario.
The IPS_SEQ_ADJUST change is also a bug fix:
Instead of checking for SEQ_ADJ this tested for SEEN_REPLY and ASSURED by accident -- _BIT is only for use with the test_bit() API.
This bug has little consequence in practice, because the sequence number adjustments are only useful for TCP which doesn't support clash resolution.
The existing test case (conntrack_clash_clash.sh) exercise a race condition path (parallel conntrack creation on different CPUs), so the colliding entries have neither SEEN_REPLY nor ASSURED set.
Thanks to Yafang Shao and Shaun Brady for an initial investigation of this bug.
--- net/netfilter/nf_nat_core.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
net/netfilter/nf_nat_core.c +9 -3
@@ -248,7 +248,7 @@ static noinline bool nf_nat_used_tuple_new(const struct nf_conntrack_tuple *tuple, const struct nf_conn *ignored_ct) { -static const unsigned long uses_nat =IPS_NAT_MASK |IPS_SEQ_ADJUST_BIT ; +static const unsigned long uses_nat =IPS_NAT_MASK |IPS_SEQ_ADJUST ;const struct nf_conntrack_tuple_hash *thash;const struct nf_conntrack_zone *zone;struct nf_conn *ct;
@@ -287,8 +287,14 @@ nf_nat_used_tuple_new(const struct nf_conntrack_tuple *tuple, zone =nf_ct_zone (ignored_ct); thash =nf_conntrack_find_get (net, zone, tuple); -if (unlikely (!thash))/* clashing entry went away */ -return false; +if (unlikely (!thash)) { +struct nf_conntrack_tuple reply; + +nf_ct_invert_tuple (&reply, tuple); + thash =nf_conntrack_find_get (net, zone, &reply); +if (!thash)/* clashing entry went away */ +return false; + } ct =nf_ct_tuplehash_to_ctrack (thash);
Signature · 1 line
2.49.0
Florian Westphal
maintainerPATCH 2/2 selftests: netfilter: nft_nat.sh: add test for reverse clash with nat
This will fail without the previous bug fix because we erronously believe that the clashing entry went way.
However, the clash exists in the opposite direction due to an
existing nat mapping:
PASS: IP statless for ns2-LgTIuS
ERROR: failed to test udp ns1-x4iyOW to ns2-LgTIuS with dnat rule step 2, result: ""
This is partially adapted from test instructions from the below ubuntu tracker.
--- .../selftests/net/netfilter/nft_nat.sh | 81 +++++++++++++++++-- 1 file changed, 76 insertions(+), 5 deletions(-)
tools/testing/selftests/net/netfilter/nft_nat.sh +76 -5
@@ -866,6 +866,24 @@ EOF ip netns exec "$ns0" nft delete table $family nat } +file_cmp() +{ + local infile="$1" + local outfile="$2" + + if ! cmp "$infile" "$outfile";then + echo -n "Infile " + ls -l "$infile" + echo -n "Outfile " + ls -l "$outfile" + echo "ERROR: in and output file mismatch when checking $msg" 1>&1 + ret=1 + return 1 + fi + + return 0 +} + test_stateless_nat_ip() { local lret=0
@@ -966,11 +984,7 @@ EOF wait - if ! cmp "$INFILE" "$OUTFILE";then - ls -l "$INFILE" "$OUTFILE" - echo "ERROR: in and output file mismatch when checking udp with stateless nat" 1>&2 - lret=1 - fi + file_cmp "$INFILE" "$OUTFILE" "udp with stateless nat" || lret=1 :> "$OUTFILE"
@@ -991,6 +1005,62 @@ EOF return $lret } +test_dnat_clash() +{ + local lret=0 + + if ! socat -h > /dev/null 2>&1;then + echo "SKIP: Could not run dnat clash test without socat tool" + [ $ret -eq 0 ] && ret=$ksft_skip + return $ksft_skip + fi + +ip netns exec "$ns0" nft -f /dev/stdin <<EOF +flush ruleset +table ip dnat-test { + chain prerouting { + type nat hook prerouting priority dstnat; policy accept; + ip daddr 10.0.2.1 udp dport 1234 counter dnat to 10.0.1.1:1234 + } +} +EOF + if [ $? -ne 0 ]; then + echo "SKIP: Could not add dnat rules" + [ $ret -eq 0 ] && ret=$ksft_skip + return $ksft_skip + fi + + local udpdaddr="10.0.2.1" + for i in 1 2;do + echo "PING $udpdaddr" > "$INFILE" + echo "PONG 10.0.1.1 step $i" | ip netns exec "$ns0" timeout 3 socat STDIO UDP4-LISTEN:1234,bind=10.0.1.1 > "$OUTFILE" 2>/dev/null & + local lpid=$! + + busywait $BUSYWAIT_TIMEOUT listener_ready "$ns0" 1234 "-u" + + result=$(ip netns exec "$ns1" timeout 3 socat STDIO UDP4-SENDTO:"$udpdaddr:1234,sourceport=4321" < "$INFILE") + udpdaddr="10.0.1.1" + + if [ "$result" != "PONG 10.0.1.1 step $i" ] ; then + echo "ERROR: failed to test udp $ns1 to $ns2 with dnat rule step $i, result: \"$result\"" 1>&2 + lret=1 + ret=1 + fi + + wait + + file_cmp "$INFILE" "$OUTFILE" "udp dnat step $i" || lret=1 + + :> "$OUTFILE" + done + + test $lret -eq 0 && echo "PASS: IP dnat clash $ns1:$ns2" + + ip netns exec "$ns0" nft flush ruleset + + return $lret +} + # ip netns exec "$ns0" ping -c 1 -q 10.0.$i.99 for i in "$ns0" "$ns1" "$ns2" ;do ip netns exec "$i" nft -f /dev/stdin <<EOF
@@ -1147,6 +1217,7 @@ $test_inet_nat && test_redirect6 inet test_port_shadowing test_stateless_nat_ip +test_dnat_clash if [ $ret -ne 0 ];then echo -n "FAIL: "
Signature · 1 line
2.49.0
Re: [PATCH nf 2/2] selftests: netfilter: nft_nat.sh: add test for reverse clash with nat
tools/testing/selftests/net/netfilter/nft_nat.sh
Quoted 2 lines · Florian Westphal
This will fail without the previous bug fix because we erronously
tools/testing/selftests/net/netfilter/nft_nat.sh +0
-0
...>8...
tools/testing/selftests/net/netfilter/nft_nat.sh EOF · line 1057
Quoted 2 lines
tools/testing/selftests/net/netfilter/nft_nat.sh +1
-0+ test $lret -eq 0 && echo "PASS: IP dnat clash $ns1:$ns2"
# PASS: IP dnat clash ns1-Mx4scW:ns2-wDqp2o
ok 21 selftests: net/netfilter: nft_nat.sh
Quoted 47 lines · Florian Westphal
The logic added in the blamed commit was supposed to only omit nat source port allocation if neither the existing nor the new entry are subject to NAT.
However, its not enough to lookup the conntrack based on the proposed tuple, we must also check the reverse direction.
Otherwise there are esoteric cases where the collision is in the reverse direction because that colliding connection has a port rewrite, but the new entry doesn't. In this case, we only check the new entry and then erronously conclude that no clash exists anymore.
The existing (udp) tuple is:
a:p -> b:P, with nat translation to s:P, i.e. pure daddr rewrite, reverse tuple in conntrack table is s:P -> a:p.When another UDP packet is sent directly to s, i.e. a:p->s:P, this is correctly detected as a colliding entry: tuple is taken by existing reply tuple in reverse direction.
But the colliding conntrack is only searched for with unreversed direction, and we can't find such entry matching a:p->s:P.
The incorrect conclusion is that the clashing entry has timed out and that no port address translation is required.
Such conntrack will then be discarded at
nf_confirmtime because the proposed reverse direction clashes with an existing mapping in the conntrack table.Search for the reverse tuple too, this will then check the NAT bits of the colliding entry and triggers port reallocation.
Followp patch extends
nft_nat.shselftest to cover this scenario.The
IPS_SEQ_ADJUSTchange is also a bug fix:
Instead of checking forSEQ_ADJthis tested forSEEN_REPLYand ASSURED by accident --_BITis only for use with thetest_bit()API.This bug has little consequence in practice, because the sequence number adjustments are only useful for TCP which doesn't support clash resolution.
The existing test case (
conntrack_clash_clash.sh) exercise a race condition path (parallel conntrack creation on different CPUs), so the colliding entries have neitherSEEN_REPLYnor ASSURED set.
Quoted 8 lines
Thanks to Yafang Shao and Shaun Brady for an initial investigation of this bug.
Fixes d8f84a9bc7c4 ("netfilter: nf_nat: don't try nat source port reallocation for reverse dir clash")Reported-by Yafang Shao <[email protected]>Reported-by Shaun Brady <[email protected]>Signed-off-by Florian Westphal <[email protected]>
Quoted 37 lines
--- net/netfilter/nf_nat_core.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)net/netfilter/nf_nat_core.c +9
-4@@ -248,7 +248,7 @@ static noinline bool nf_nat_used_tuple_new(const struct nf_conntrack_tuple *tuple, const struct nf_conn *ignored_ct) { -static const unsigned long uses_nat =IPS_NAT_MASK |IPS_SEQ_ADJUST_BIT ; +static const unsigned long uses_nat =IPS_NAT_MASK |IPS_SEQ_ADJUST ;const struct nf_conntrack_tuple_hash *thash;const struct nf_conntrack_zone *zone;struct nf_conn *ct;@@ -287,8 +287,14 @@ nf_nat_used_tuple_new(const struct nf_conntrack_tuple *tuple, zone =nf_ct_zone (ignored_ct); thash =nf_conntrack_find_get (net, zone, tuple); -if (unlikely (!thash))/* clashing entry went away */ -return false; +if (unlikely (!thash)) { +struct nf_conntrack_tuple reply; + +nf_ct_invert_tuple (&reply, tuple); + thash =nf_conntrack_find_get (net, zone, &reply); +if (!thash)/* clashing entry went away */ +return false; + } ct =nf_ct_tuplehash_to_ctrack (thash); --2.49.0
Signature · 2 lines
Regards Yafang