Skip to contentSkip to conversation

possible bug in ip_nat_fn()

PATCH Balazs Scheidler 2 participants 2 messages Aug 10, 2003 – Aug 13, 2003

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi,

The last bug I've found in my TPROXY patches might apply to core NAT code as well, possibly causing either an Oops or an infinite loop in the network softirq.

The relevant code is in the function ip_nat_fn(), more precisely the IP_CT_NEW case, where three possible branches are used to decide how the current session is to be NATed:

1) the current connection is an expected connection, the NAT helper expect function is called, which possibly calls ip_nat_setup_info()

2) the current connection is destined to LOCAL_IN (only when CONFIG_IP_NF_NAT_LOCAL is defined), no ip_nat_setup_info() is called this time

3) the nat table is looked up, and if nothing is found a null-mapping is applied

The problem occurs if no ip_nat_setup_info() is called (or it fails) _AND_ ret == NF_ACCEPT. This is always the case in 2), when 'ret' is simply set to NF_ACCEPT, and might possibly also happen if the NAT helper does not check the return value of ip_nat_setup_info(), or it simply does not set up a NAT mapping for the current connection.

In the case outlined above, ct->nat.info.initialized will not be set, but the NAT hash entry will be added. As ct->nat.info.initialized is used to check whether a connection is hashed, this might result in conntracks added to the NAT hash multiple times (when the current SYN is retransmitted for instance).

In my experience this usually results in an infinite loop in find_appropriate_src() where this list is traversed, practically rendering the box completely unresponsive.

I think the solution for this problem would be a separate flag in ip_nat_info->initialized which tells whether this conntrack has already been hashed, or to move the hash update code to ip_nat_setup_info. I've attached a patch which does the latter, and makes the place_in_hashes and replace_in_hashes functions static.

Independent of this, it might also make sense to move the manip check to the ip_nat_setup_info() as well. (To add a new NAT manip if and only if (!(info->initialized & (1 << HOOK2MANIP(hooknum)))) is 0). This would prevent the ip_nat_info->manips array to overflow. If this is agreed upon, I'm willing to provide patches.

Signature · 2 lines
Bazsi
PGP info: KeyID 9AF8D0A9 Fingerprint CD27 CFB0 802C 0944 9CFD 804E C82C 8EB1
net/ipv4/netfilter/ip_fw_compat_masq.c +0 -1
diff -urN --exclude-from=kernel-exclude linux-2.4.21/net/ipv4/netfilter/ip_fw_compat_masq.c linux-2.4.21-natfix/net/ipv4/netfilter/ip_fw_compat_masq.c
@@ -92,7 +92,6 @@
 			return ret;
 		}
 
-		place_in_hashes(ct, info);
 		info->initialized = 1;
 	} else
 		DEBUGP("Masquerading already done on this conn.\n");
net/ipv4/netfilter/ip_nat_core.c +10 -4
diff -urN --exclude-from=kernel-exclude linux-2.4.21/net/ipv4/netfilter/ip_nat_core.c linux-2.4.21-natfix/net/ipv4/netfilter/ip_nat_core.c
@@ -636,13 +636,18 @@
 		info->helper = LIST_FIND(&helpers, helper_cmp, struct ip_nat_helper *,
 					 &reply);
 
+	if (info->initialized)
+		replace_in_hashes(conntrack, info);
+	else
+		place_in_hashes(conntrack, info);
 	/* It's done. */
 	info->initialized |= (1 << HOOK2MANIP(hooknum));
 	return NF_ACCEPT;
 }
 
-void replace_in_hashes(struct ip_conntrack *conntrack,
-		       struct ip_nat_info *info)
+static void 
+replace_in_hashes(struct ip_conntrack *conntrack,
+		  struct ip_nat_info *info)
 {
 	/* Source has changed, so replace in hashes. */
 	unsigned int srchash
@@ -670,8 +675,9 @@
 	list_prepend(&byipsproto[ipsprotohash], &info->byipsproto);
 }
 
-void place_in_hashes(struct ip_conntrack *conntrack,
-		     struct ip_nat_info *info)
+static void 
+place_in_hashes(struct ip_conntrack *conntrack,
+		struct ip_nat_info *info)
 {
 	unsigned int srchash
 		= hash_by_src(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
net/ipv4/netfilter/ip_nat_standalone.c +0 -7
diff -urN --exclude-from=kernel-exclude linux-2.4.21/net/ipv4/netfilter/ip_nat_standalone.c linux-2.4.21-natfix/net/ipv4/netfilter/ip_nat_standalone.c
@@ -115,7 +115,6 @@
 		/* Seen it before?  This can happen for loopback, retrans,
 		   or local packets.. */
 		if (!(info->initialized & (1 << maniptype))) {
-			int in_hashes = info->initialized;
 			unsigned int ret;
 
 			if (ct->master
@@ -139,12 +138,6 @@
 				return ret;
 			}
 
-			if (in_hashes) {
-				IP_NF_ASSERT(info->bysource.conntrack);
-				replace_in_hashes(ct, info);
-			} else {
-				place_in_hashes(ct, info);
-			}
 		} else
 			DEBUGP("Already setup manip %s for ct %p\n",
 			       maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
#1 lore raw

Hi Bazsi,

Quoted 5 lines · Balazs Scheidler

The problem occurs if no ip_nat_setup_info() is called (or it fails) _AND_ ret == NF_ACCEPT. This is always the case in 2), when 'ret' is simply set to NF_ACCEPT, and might possibly also happen if the NAT helper does not check the return value of ip_nat_setup_info(), or it simply does not set up a NAT mapping for the current connection.

[...]

Quoted 5 lines

I think the solution for this problem would be a separate flag in ip_nat_info->initialized which tells whether this conntrack has already been hashed, or to move the hash update code to ip_nat_setup_info. I've attached a patch which does the latter, and makes the place_in_hashes and replace_in_hashes functions static.

I think your analysis is correct and I agree with your fix as well.

Could you report this and all your other findings in the workshop during presenting the TPROXY patch?

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics

          H-1525 Budapest 114, POB. 49, Hungary