Skip to contentSkip to conversation

static portptr pointer in tcp/udp_unique_tuple

PATCH KOVACS Krisztian 1 participant 1 message Mar 1, 2004

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

Hi,

I think I've found two possible problems in the current Netfilter NAT
code. In ip_nat_proto_udp.c the start of udp_unique_tuple() looks like this:

- 8< -
static int
udp_unique_tuple(struct ip_conntrack_tuple *tuple,
const struct ip_nat_range *range, enum ip_nat_manip_type maniptype, const struct ip_conntrack *conntrack)

{
	static u_int16_t port, *portptr;
	unsigned int range_size, min, i;

- 8< -

This declares the portptr pointer as static, which I think is a
mistake, as portptr is initialized right at the beginning at the function to the tuple's source/destination port. The same goes for ip_nat_proto_tcp.c.

It would mean a serious race condition if two of these functions were
running at the same time, thus corrupting each others' portptr pointers. Luckily this won't happen, since proto->unique_tuple() is called only with ip_nat_lock held for write.

The attached patch fixes the problem, anyway.

Signature · 2 lines
 Regards,
   Krisztian KOVACS
net/ipv4/netfilter/ip_nat_proto_tcp.c +2 -1
Index: linux-2.4.25/net/ipv4/netfilter/ip_nat_proto_tcp.c
===================================================================
@@ -31,7 +31,8 @@
 		 enum ip_nat_manip_type maniptype,
 		 const struct ip_conntrack *conntrack)
 {
-	static u_int16_t port = 0, *portptr;
+	static u_int16_t port = 0;
+	u_int16_t *portptr;
 	unsigned int range_size, min, i;
 
 	if (maniptype == IP_NAT_MANIP_SRC)
net/ipv4/netfilter/ip_nat_proto_udp.c +2 -1
Index: linux-2.4.25/net/ipv4/netfilter/ip_nat_proto_udp.c
===================================================================
@@ -32,7 +32,8 @@
 		 enum ip_nat_manip_type maniptype,
 		 const struct ip_conntrack *conntrack)
 {
-	static u_int16_t port = 0, *portptr;
+	static u_int16_t port = 0;
+	u_int16_t *portptr;
 	unsigned int range_size, min, i;
 
 	if (maniptype == IP_NAT_MANIP_SRC)
net/ipv4/netfilter/ip_nat_proto_tcp.c +2 -1
Index: linux-2.6.3/net/ipv4/netfilter/ip_nat_proto_tcp.c
===================================================================
@@ -40,7 +40,8 @@
 		 enum ip_nat_manip_type maniptype,
 		 const struct ip_conntrack *conntrack)
 {
-	static u_int16_t port, *portptr;
+	static u_int16_t port;
+	u_int16_t *portptr;
 	unsigned int range_size, min, i;
 
 	if (maniptype == IP_NAT_MANIP_SRC)
net/ipv4/netfilter/ip_nat_proto_udp.c +2 -1
Index: linux-2.6.3/net/ipv4/netfilter/ip_nat_proto_udp.c
===================================================================
@@ -41,7 +41,8 @@
 		 enum ip_nat_manip_type maniptype,
 		 const struct ip_conntrack *conntrack)
 {
-	static u_int16_t port, *portptr;
+	static u_int16_t port;
+	u_int16_t *portptr;
 	unsigned int range_size, min, i;
 
 	if (maniptype == IP_NAT_MANIP_SRC)