Skip to contentSkip to conversation

ip_conntrack_ftp: enhancement

PATCH New 高昌利 1 participant 1 message May 28, 2007

Subsystems (2)
lore
Share & tools
lore

高昌利

author
#0 lore raw

PATCH 1/1 ip_conntrack_ftp: enhancement

[NETFILTER]: nf_conntrack_ftp: Enhance the support to ftp and security.

Includes:
1. Command terminated with LF instead of CRLF.
2. More spaces exists between command and arguments.
3. Multi-commands in one packets.

Explains:
to 1: Though rfc959 doesn't think LF is valid terminator, there are many ftp servers and clients support it. In fact, it is a real standard. As it isn't very difficult to implement it. So I did it. to 2: It is in rfc959. "The command codes and the argument fields are separated by one or more spaces."(section 5.3)
to 3: You can't prevent the transfer layer or a user to do that.

Beside of these, I also make a stricter police: all the packets which don't recognized properly aren't allow to pass the firewall. I assume that the ftp command packets should be started after a new line and they are must terminated by LF or CRLF. If not, they are considered to be invalid, and will be dropped silently. I think it is better than passing them, because of a relative security hole mentioned in phrack63-0x13[http://www.phreakmail.com/phrack63-19.html].

Perhaps you will worry about the packets dropped. It is not a real problem:
1. A normal programmer won't send a command by calling send/sendto/write more than once.
2. The dropped data will be sent again by transfer layer TCP.
3. The data after the packets dropped won't pass the firewall until the packets dropped former passed at last. So SACK option of TCP isn't a problem too.

----

----

 ip_conntrack_ftp.c |   94 +++++++++++++++++++++++++++++------------------------
 1 file changed, 52 insertions(+), 42 deletions(-)

----
--- linux/net/ipv4/netfilter/ip_conntrack_ftp.c 2007-05-28 21:59:22.000000000 +0800
+++ linux-new/net/ipv4/netfilter/ip_conntrack_ftp.c 2007-05-28
22:03:40.000000000 +0800
@@ -105,9 +105,9 @@

                else if (*data == sep)
                        i++;
                else {

- /* Unexpected character; true if it's the
- terminator and we're finished. */
- if (*data == term && i == array_size - 1)
+ /* Unexpected character; true if it's the terminator or
'\n'
+ * and we're finished. */
+ if ((*data == term || *data == '\n') && i == array_size -
1)
return len;

DEBUGP("Char %u (got %u nums) `%u' unexpected\n",
@@ -205,16 +205,9 @@

        size_t i;

        DEBUGP("find_pattern `%s': dlen = %u\n", pattern, dlen);
-       if (dlen == 0)
+       if (dlen == 0 || dlen <= plen)
                return 0;

- if (dlen <= plen) {
- /* Short packet: try for partial? */
- if (strnicmp(data, pattern, dlen) == 0)
- return -1;
- else return 0;
- }
-

        if (strnicmp(data, pattern, plen) != 0) {
 #if 0
                size_t i;
@@ -235,8 +228,10 @@
        for (i = plen; data[i] != skip; i++)
                if (i == dlen - 1) return -1;
-       /* Skip over the last character */
-       i++;
+       /* rfc959: The command codes and the argument fields are

+ * separated by one or more spaces.*/
+ for (; data[i] == skip; i ++)
+ if (i == dlen - 1) return -1;

        DEBUGP("Skipped up to `%c'!\n", skip);

@@ -291,15 +286,15 @@

 {
        unsigned int dataoff, datalen;
        struct tcphdr _tcph, *th;
-       char *fb_ptr;
+       char *fb_ptr, *ptr;
        int ret;
        u32 seq, array[6] = { 0 };
        int dir = CTINFO2DIR(ctinfo);
        unsigned int matchlen, matchoff;
        struct ip_ct_ftp_master *ct_ftp_info = &ct->help.ct_ftp_info;
        struct ip_conntrack_expect *exp;
-       unsigned int i;
-       int found = 0, ends_in_nl;
+       unsigned int i, i_old;
+       int found = 0, found_old = 0;
        /* Until there's been traffic both ways, don't look in packets. */
        if (ctinfo != IP_CT_ESTABLISHED
@@ -326,18 +321,20 @@
                                    (*pskb)->len - dataoff, ftp_buffer);
        BUG_ON(fb_ptr == NULL);

- ends_in_nl = (fb_ptr[datalen - 1] == '\n');
- seq = ntohl(th->seq) + datalen;
-
- /* Look up to see if we're just after a \n. */
- if (!find_nl_seq(ntohl(th->seq), ct_ftp_info, dir)) {
- /* Now if this ends in \n, update ftp info. */
- DEBUGP("ip_conntrack_ftp_help: wrong seq pos %s(%u) or %s(%u)\n",
- ct_ftp_info->seq_aft_nl[0][dir]
- old_seq_aft_nl_set ? "":"(UNSET) ", old_seq_aft_nl);
- ret = NF_ACCEPT;
- goto out_update_nl;
+ /* We don't allow such a command or reply splitted into
+ * multi-packages. */
+ if (fb_ptr[datalen - 1] != '\n') {
+ ret = NF_DROP;
+ goto out;

+       }
+       seq = ntohl(th->seq);
+       if (ct_ftp_info->seq_aft_nl_num[dir] == 0) {
+               update_nl_seq(seq, ct_ftp_info, dir, *pskb);
+       } else if (!find_nl_seq(seq, ct_ftp_info, dir)) {
+               ret = NF_DROP;
+               goto out;
        }

+ seq += datalen;

/* Initialize IP array to expected address (it's not mentioned
in EPSV responses) */
@@ -346,10 +343,11 @@

        array[2] = (ntohl(ct->tuplehash[dir].tuple.src.ip) >> 8) & 0xFF;
        array[3] = ntohl(ct->tuplehash[dir].tuple.src.ip) & 0xFF;

+again:

        for (i = 0; i < ARRAY_SIZE(search); i++) {
                if (search[i].dir != dir) continue;

- found = find_pattern(fb_ptr, (*pskb)->len - dataoff,
+ found = find_pattern(fb_ptr, datalen,

                                     search[i].pattern,
                                     search[i].plen,
                                     search[i].skip,
@@ -360,19 +358,32 @@
                if (found) break;
        }
        if (found == -1) {
-               /* We don't usually drop packets.  After all, this is
-                  connection tracking, not packet filtering.
-                  However, it is necessary for accurate tracking in
-                  this case. */

- if (net_ratelimit())
- printk("conntrack_ftp: partial %s %u+%u\n",
- search[i].pattern,
- ntohl(th->seq), datalen);
+ /* In order to prevent a kind of attrack based
+ * on non-tracked sub-connection[phrack63_0x13].
+ * We will drop it instead of accepting it. */

                ret = NF_DROP;
                goto out;

- } else if (found == 0) { /* No match */
- ret = NF_ACCEPT;
- goto out_update_nl;
+ }
+

+       /* Is there another line nested? */
+       ptr = memchr(fb_ptr, '\n', datalen);
+       if (ptr != NULL && ptr != &fb_ptr[datalen - 1]) {
+               datalen -= ++ptr - fb_ptr;
+               fb_ptr = ptr;
+               if (found) {

+ found_old = found;
+ i_old = i;
+ }
+ goto again;
+ }
+
+ if (found == 0) {
+ if (found_old == 0) { /* No match */
+ ret = NF_ACCEPT;
+ goto out_update_nl;

+               }
+               found = found_old;
+               i = i_old;
        }

        DEBUGP("conntrack_ftp: match `%s' (%u bytes at %u)\n",

@@ -442,9 +453,8 @@
out_update_nl:
/* Now if this ends in \n, update ftp info. Seq may have been
* adjusted by NAT code. */
- if (ends_in_nl)
- update_nl_seq(seq, ct_ftp_info,dir, *pskb);
- out:
+ update_nl_seq(seq, ct_ftp_info, dir, *pskb);
+out:

        spin_unlock_bh(&ip_ftp_lock);
        return ret;

}

net/ipv4/netfilter/ip_conntrack_ftp.c +52 -42
@@ -105,9 +105,9 @@
 		else if (*data == sep)
 			i++;
 		else {
-			/* Unexpected character; true if it's the
-			   terminator and we're finished. */
-			if (*data == term && i == array_size - 1)
+			/* Unexpected character; true if it's the terminator or '\n'
+			 * and we're finished. */
+			if ((*data == term || *data == '\n') && i == array_size - 1)
 				return len;
 
 			DEBUGP("Char %u (got %u nums) `%u' unexpected\n",
@@ -205,16 +205,9 @@
 	size_t i;
 
 	DEBUGP("find_pattern `%s': dlen = %u\n", pattern, dlen);
-	if (dlen == 0)
+	if (dlen == 0 || dlen <= plen)
 		return 0;
 
-	if (dlen <= plen) {
-		/* Short packet: try for partial? */
-		if (strnicmp(data, pattern, dlen) == 0)
-			return -1;
-		else return 0;
-	}
-
 	if (strnicmp(data, pattern, plen) != 0) {
 #if 0
 		size_t i;
@@ -235,8 +228,10 @@
 	for (i = plen; data[i] != skip; i++)
 		if (i == dlen - 1) return -1;
 
-	/* Skip over the last character */
-	i++;
+	/* rfc959: The command codes and the argument fields are
+	 * separated by one or more spaces.*/
+	for (; data[i] == skip; i ++)
+		if (i == dlen - 1) return -1;
 
 	DEBUGP("Skipped up to `%c'!\n", skip);
 
@@ -291,15 +286,15 @@
 {
 	unsigned int dataoff, datalen;
 	struct tcphdr _tcph, *th;
-	char *fb_ptr;
+	char *fb_ptr, *ptr;
 	int ret;
 	u32 seq, array[6] = { 0 };
 	int dir = CTINFO2DIR(ctinfo);
 	unsigned int matchlen, matchoff;
 	struct ip_ct_ftp_master *ct_ftp_info = &ct->help.ct_ftp_info;
 	struct ip_conntrack_expect *exp;
-	unsigned int i;
-	int found = 0, ends_in_nl;
+	unsigned int i, i_old;
+	int found = 0, found_old = 0;
 
 	/* Until there's been traffic both ways, don't look in packets. */
 	if (ctinfo != IP_CT_ESTABLISHED
@@ -326,18 +321,20 @@
 				    (*pskb)->len - dataoff, ftp_buffer);
 	BUG_ON(fb_ptr == NULL);
 
-	ends_in_nl = (fb_ptr[datalen - 1] == '\n');
-	seq = ntohl(th->seq) + datalen;
-
-	/* Look up to see if we're just after a \n. */
-	if (!find_nl_seq(ntohl(th->seq), ct_ftp_info, dir)) {
-		/* Now if this ends in \n, update ftp info. */
-		DEBUGP("ip_conntrack_ftp_help: wrong seq pos %s(%u) or %s(%u)\n",
-		       ct_ftp_info->seq_aft_nl[0][dir] 
-		       old_seq_aft_nl_set ? "":"(UNSET) ", old_seq_aft_nl);
-		ret = NF_ACCEPT;
-		goto out_update_nl;
+	 /* We don't allow such a command or reply splitted into
+	 * multi-packages. */
+	if (fb_ptr[datalen - 1] != '\n') {
+		ret = NF_DROP;
+		goto out;
+	}
+	seq = ntohl(th->seq);
+	if (ct_ftp_info->seq_aft_nl_num[dir] == 0) {
+		update_nl_seq(seq, ct_ftp_info, dir, *pskb);
+	} else if (!find_nl_seq(seq, ct_ftp_info, dir)) {
+		ret = NF_DROP;
+		goto out;
 	}
+	seq += datalen;
 
 	/* Initialize IP array to expected address (it's not mentioned
            in EPSV responses) */
@@ -346,10 +343,11 @@
 	array[2] = (ntohl(ct->tuplehash[dir].tuple.src.ip) >> 8) & 0xFF;
 	array[3] = ntohl(ct->tuplehash[dir].tuple.src.ip) & 0xFF;
 
+again:
 	for (i = 0; i < ARRAY_SIZE(search); i++) {
 		if (search[i].dir != dir) continue;
 
-		found = find_pattern(fb_ptr, (*pskb)->len - dataoff,
+		found = find_pattern(fb_ptr, datalen,
 				     search[i].pattern,
 				     search[i].plen,
 				     search[i].skip,
@@ -360,19 +358,32 @@
 		if (found) break;
 	}
 	if (found == -1) {
-		/* We don't usually drop packets.  After all, this is
-		   connection tracking, not packet filtering.
-		   However, it is necessary for accurate tracking in
-		   this case. */
-		if (net_ratelimit())
-			printk("conntrack_ftp: partial %s %u+%u\n",
-			       search[i].pattern,
-			       ntohl(th->seq), datalen);
+		/* In order to prevent a kind of attrack based
+		 * on non-tracked sub-connection[phrack63_0x13].
+		 * We will drop it instead of accepting it. */
 		ret = NF_DROP;
 		goto out;
-	} else if (found == 0) { /* No match */
-		ret = NF_ACCEPT;
-		goto out_update_nl;
+	}
+	
+	/* Is there another line nested? */
+	ptr = memchr(fb_ptr, '\n', datalen);
+	if (ptr != NULL && ptr != &fb_ptr[datalen - 1]) {
+		datalen -= ++ptr - fb_ptr;
+		fb_ptr = ptr;
+		if (found) {
+			found_old = found;
+			i_old = i;
+		}
+		goto again;
+	}
+
+	if (found == 0) {
+		if (found_old == 0) { /* No match */
+			ret = NF_ACCEPT;
+			goto out_update_nl;
+		}
+		found = found_old;
+		i = i_old;
 	}
 
 	DEBUGP("conntrack_ftp: match `%s' (%u bytes at %u)\n",
@@ -442,9 +453,8 @@
 out_update_nl:
 	/* Now if this ends in \n, update ftp info.  Seq may have been
 	 * adjusted by NAT code. */
-	if (ends_in_nl)
-		update_nl_seq(seq, ct_ftp_info,dir, *pskb);
- out:
+	update_nl_seq(seq, ct_ftp_info, dir, *pskb);
+out:
 	spin_unlock_bh(&ip_ftp_lock);
 	return ret;
 }