Skip to contentSkip to conversation

tcp connection tracking 2.4.19

PATCH In review Martin Renold 3 participants 5 messages Oct 16, 2002 – Oct 17, 2002

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

PATCH 1/1 tcp connection tracking 2.4.19

hi,

There is a bug in the stable 2.4.19 kernel in the ip_conntrack code that allows the final ACK of a SYN - SYN/ACK - ACK tcp handshake to establish an ASSURED connection even if it has a wrong sequence number. The current code only checks the ACK number.

This allows a DoS attack:
http://old.homeip.net/martin/cdos.tgz

I already sent this to linux-kernel, and was redirected:
http://www.uwsg.indiana.edu/hypermail/linux/kernel/0210.1/0297.html

I know about the TCP window tracking patch, but I expect from the unpatched "stable" kernel no such behaviour.

So I wrote a simple patch against 2.4.19, but I must admit that I do not really understand the code around it, especially why it does not mark such a packet as invalid (I'm new to most things here). It's running right now on 3-4 machines.

include/linux/netfilter_ipv4/ip_conntrack_tcp.h +3 -2
diff -urN -X dontdiff kernel-source-2.4.19.origin/include/linux/netfilter_ipv4/ip_conntrack_tcp.h kernel-source-2.4.19.patch/include/linux/netfilter_ipv4/ip_conntrack_tcp.h
@@ -24,8 +24,9 @@
 {
 	enum tcp_conntrack state;
 
-	/* Poor man's window tracking: sequence number of valid ACK
-           handshake completion packet */
+	/* Poor man's window tracking: expected sequence and acknowledge 
+	   number of valid ACK handshake completion packet */
+	u_int32_t handshake_seq;
 	u_int32_t handshake_ack;
 };
 
net/ipv4/netfilter/ip_conntrack_proto_tcp.c +3 -0
diff -urN -X dontdiff kernel-source-2.4.19.origin/net/ipv4/netfilter/ip_conntrack_proto_tcp.c kernel-source-2.4.19.patch/net/ipv4/netfilter/ip_conntrack_proto_tcp.c
@@ -180,6 +180,8 @@
 	if (oldtcpstate == TCP_CONNTRACK_SYN_SENT
 	    && CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY
 	    && tcph->syn && tcph->ack)
+		conntrack->proto.tcp.handshake_seq
+			= tcph->ack_seq;
 		conntrack->proto.tcp.handshake_ack
 			= htonl(ntohl(tcph->seq) + 1);
 	WRITE_UNLOCK(&tcp_lock);
@@ -196,6 +198,7 @@
 		if (oldtcpstate == TCP_CONNTRACK_SYN_RECV
 		    && CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL
 		    && tcph->ack && !tcph->syn
+		    && tcph->seq == conntrack->proto.tcp.handshake_seq
 		    && tcph->ack_seq == conntrack->proto.tcp.handshake_ack)
 			set_bit(IPS_ASSURED_BIT, &conntrack->status);
 
Signature · 2 lines
Thunder's just a noise, boys, lightnin' does the work
-- (Chad Brock/John Hadley/Kelly Garrett)
#1 lore raw
Quoted 6 lines · Martin Renold

hi,

There is a bug in the stable 2.4.19 kernel in the ip_conntrack code that allows the final ACK of a SYN - SYN/ACK - ACK tcp handshake to establish an ASSURED connection even if it has a wrong sequence number. The current code only checks the ACK number.

The question is whether this is really to be called a 'bug'. Netfilter connection tracking never claimed to do full state tracking, neither did it claim to do sequence number tracking. It does all it can without tracking sequence/ack numbers and window sizes.

You can also send FIN flagged packets with a wrong sequence number to terminate a connection (conntrack entry, that is).

This is not a bug in the code, but an architectural disadvantage of doing this kind of simplified non-sequence-number-tracking TCP state tracking.

Quoted 3 lines
Signature · 2 lines
Thunder's just a noise, boys, lightnin' does the work
-- (Chad Brock/John Hadley/Kelly Garrett)
Signature · 5 lines
Live long and prosper
- Harald Welte / [email protected]               http://www.gnumonks.org/
============================================================================
GCS/E/IT d- s-: a-- C+++ UL++++$ P+++ L++++$ E--- W- N++ o? K- w--- O- M- 
V-- PS+ PE-- Y+ PGP++ t++ 5-- !X !R tv-- b+++ DI? !D G+ e* h+ r% y+(*)
#2 lore raw
Quoted 8 lines · Harald Welte

On Wed, Oct 16, 2002 at 09:48:37AM +0200, Martin Renold wrote:
The question is whether this is really to be called a 'bug'. Netfilter connection tracking never claimed to do full state tracking, neither did it claim to do sequence number tracking. It does all it can without tracking sequence/ack numbers and window sizes.

You can also send FIN flagged packets with a wrong sequence number to terminate a connection (conntrack entry, that is).

Well, I think the point is that if somebody sends a bad FIN (or RST) packet, he has to know both used port numbers, and even then I perhaps wouldn't care, I would still be able to establish a new connection.

I do not want a full state tracking, I just don't like being shut out of my system for several days. And I called it a bug because one half of the code to handle this situation was already there.

bye,
Martin

#3 lore raw
In reply to Martin Renold
Quoted 4 lines · Martin Renold

So I wrote a simple patch against 2.4.19, but I must admit that I do not really understand the code around it, especially why it does not mark such a packet as invalid (I'm new to most things here). It's running right now on 3-4 machines.

I can't speak about the merits of this patch, since I haven't tried it, but I believe there is a bug in it:

net/ipv4/netfilter/ip_conntrack_proto_tcp.c Sat Oct 5 20:45:49 2002 line 187

Quoted 12 lines
net/ipv4/netfilter/ip_conntrack_proto_tcp.c Sat Oct 5 20:45:49 2002 renamed from net/ipv4/netfilter/ip_conntrack_proto_tcp.c Fri Oct 4 08:13:38 2002 +2 -0
@@ -180,6 +180,8 @@
       if (oldtcpstate == TCP_CONNTRACK_SYN_SENT
           && CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY
           && tcph->syn && tcph->ack)
+               conntrack->proto.tcp.handshake_seq
+                       = tcph->ack_seq;
               conntrack->proto.tcp.handshake_ack
                       = htonl(ntohl(tcph->seq) + 1);
       WRITE_UNLOCK(&tcp_lock);

Note that the original statement will now be unconditionally executed. You need to enclose the statements by brackets.

Svenning

#4 lore raw
Quoted 1 line · Svenning Sorensen

I believe there is a bug in it:

Oops! Stephane Ouelle also just reported, thank you.

Quoted 2 lines

Note that the original statement will now be unconditionally executed. You need to enclose the statements by brackets.

So here it is again, corrected (I'm going to test this in a minute):

include/linux/netfilter_ipv4/ip_conntrack_tcp.h +3 -2
diff -urN -X dontdiff kernel-source-2.4.19.origin/include/linux/netfilter_ipv4/ip_conntrack_tcp.h kernel-source-2.4.19.patch/include/linux/netfilter_ipv4/ip_conntrack_tcp.h
@@ -24,8 +24,9 @@
 {
 	enum tcp_conntrack state;
 
-	/* Poor man's window tracking: sequence number of valid ACK
-           handshake completion packet */
+	/* Poor man's window tracking: expected sequence and acknowledge 
+	   number of valid ACK handshake completion packet */
+	u_int32_t handshake_seq;
 	u_int32_t handshake_ack;
 };
 
net/ipv4/netfilter/ip_conntrack_proto_tcp.c +5 -1
diff -urN -X dontdiff kernel-source-2.4.19.origin/net/ipv4/netfilter/ip_conntrack_proto_tcp.c kernel-source-2.4.19.patch/net/ipv4/netfilter/ip_conntrack_proto_tcp.c
@@ -179,9 +179,12 @@
 	/* Poor man's window tracking: record SYN/ACK for handshake check */
 	if (oldtcpstate == TCP_CONNTRACK_SYN_SENT
 	    && CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY
-	    && tcph->syn && tcph->ack)
+	    && tcph->syn && tcph->ack) {
+		conntrack->proto.tcp.handshake_seq
+			= tcph->ack_seq;
 		conntrack->proto.tcp.handshake_ack
 			= htonl(ntohl(tcph->seq) + 1);
+	}
 	WRITE_UNLOCK(&tcp_lock);
 
 	/* If only reply is a RST, we can consider ourselves not to
@@ -196,6 +199,7 @@
 		if (oldtcpstate == TCP_CONNTRACK_SYN_RECV
 		    && CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL
 		    && tcph->ack && !tcph->syn
+		    && tcph->seq == conntrack->proto.tcp.handshake_seq
 		    && tcph->ack_seq == conntrack->proto.tcp.handshake_ack)
 			set_bit(IPS_ASSURED_BIT, &conntrack->status);
 

bye,
Martin