Skip to contentSkip to conversation

Found a dead-freeze bug, in xt_ipv4options.c -- patch provided

PATCH Eivind Naess 2 participants 5 messages Nov 3, 2011 – Nov 5, 2011 netfilter

lore
Share & tools
lore
#0 lore raw

All,

I found this bug running Ubuntu 11.10, I managed to hang the operating system with the CPU pegged at 100% after sending it a malformed IP option packet. I had installed the xtables-addons-dmks package installed via apt-get. The Ubuntu 11.10 distribution runs with the "3.0.0-12 #20-Ubuntu SMP" kernel. AFAIK, there should be little modifications to the code compared what is installed in git repository available on the web.

To reproduce this issue:

1. Install Ubuntu 11.10
2. apt-get install xtables-addon-dmks
3. iptables -I INPUT 1 -m ipv4options --any -j LOG --log-prefix "PACKET WITH IP-OPTIONS DETECTED"

On a different computer (attacker),
4. Download, and install the isic (ip stack integrity checker) from http://isic.sourceforge.net
5. Run a test of the ip-stack to the victim (Ubuntu 11.10) computer (attacker: 192.168.111.100, victim: 192.168.111.200).

    sudo tcpsic -s 192.168.111.100,69 -d 192.168.111.200 -x 2 -m 1000 -T 30 -t 50
6. Victim PC freezes, no mouse, no movement; just dead.

Further investigation showed that the xt_ipv4options.ko module was the culprit. The problem appeared to be that the ipv4option_rd() function entered a infinite loop when an ip-option of length 0 was encountered. I grepped the Linux kernel for similar code and modified the code in xt_ipv4options.c to correct this behavior. In addition, it seems like the --any option wasn't working correctly to begin with.

Any chances for this patch being accepted?

Regards,

- Eivind

extensions/xt_ipv4options.c    2011-11-02 13:03:14.000000000 -0700 renamed from tmp/tmp.13465.100    2011-11-03 09:20:11.000000000 -0700 +0 -0
@@ -15,14 +15,32 @@

 #include "xt_ipv4options.h"
 #include "compat_xtables.h"

-static uint32_t ipv4options_rd(const uint8_t *data, int len) +static uint32_t ipv4options_rd(const uint8_t *ptr, int len) {
     uint32_t opts = 0;
+    uint32_t olen = 0;

     while (len >= 2) {
-        opts |= 1 << (data[0] & 0x1F);
-        len  -= data[1];
-        data += data[1];
+
+        /* Handle special options */
+        switch (*ptr) {
+        case IPOPT_END:
+            return opts;
+        case IPOPT_NOOP:
+            len--;
+            ptr++;
+            continue;
+        }
+
+        /* Get the length of the option */
+        olen = ptr[1];
+        if (olen < 2 || olen > len)
+            return opts;
+
+        /* Set the option bit, and incr/decr the invariants */ +        opts |= 1 << (*ptr & 0x1F);
+        len  -= olen;
+        ptr  += olen;
     }

     return opts;
@@ -36,13 +54,20 @@
     uint32_t opts = 0;
     uint16_t len  = ip_hdrlen(skb) - sizeof(struct iphdr);
-    if (len > 0)
+    if (len > 0) {
         opts = ipv4options_rd((const void *)iph + sizeof(struct iphdr), len);
+        if(info->flags & XT_V4OPTS_ANY) {
+            return  !!opts; /* !! to make bool value */
+        }else {

     opts ^= info->invert;
     opts &= info->map;
-    return (info->flags & XT_V4OPTS_ANY) ? opts : opts == info->map; +            return (opts == info->map);
+        }
+    }
+
+    return 0;
 }

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#1 lore raw
Quoted 25 lines · Eivind Naess
(quoted diff) +1 -1
-static uint32_t ipv4options_rd(const uint8_t *data, int len)
+static uint32_t ipv4options_rd(const uint8_t *ptr, int len)

 {
     uint32_t opts = 0;
+    uint32_t olen = 0;

     while (len >= 2) {
-        opts |= 1 << (data[0] & 0x1F);
-        len  -= data[1];
-        data += data[1];
+
+        /* Handle special options */
+        switch (*ptr) {
+        case IPOPT_END:
+            return opts;
+        case IPOPT_NOOP:
+            len--;
+            ptr++;
+            continue;
+        }
+
+        /* Get the length of the option */
+        olen = ptr[1];
+        if (olen < 2 || olen > len)
+            return opts;

Quoted 8 lines

+
+        /* Set the option bit, and incr/decr the invariants */ +        opts |= 1 << (*ptr & 0x1F);
+        len  -= olen;
+        ptr  += olen;
     }

     return opts;

Whitespace damage, I had to apply this manually, and in doing so, reduced the size. You can test the patch from the "v4options" branch, copied below.

Quoted 13 lines
(quoted diff) +0 -0
@@ -36,13 +54,20 @@
      uint32_t opts = 0;
      uint16_t len  = ip_hdrlen(skb) - sizeof(struct iphdr);


-    if (len > 0)
+    if (len > 0) {
         opts = ipv4options_rd((const void *)iph + sizeof(struct iphdr), len);
+        if(info->flags & XT_V4OPTS_ANY) {
+            return  !!opts; /* !! to make bool value */ +        }else {

     opts ^= info->invert;

Quoted 8 lines

     opts &= info->map;
-    return (info->flags & XT_V4OPTS_ANY) ? opts : opts == info->map; +            return (opts == info->map);
+        }
+    }
+
+    return 0;
 }

XT_V4OPTS_ANY is meant to match on {any of {the bits present in info->map}}, not {any of {all possible IPv4 options}} according to the documentation. The test for XT_V4OPTS_ANY is therefore supposed to come after the ^ and & operations, and that is what the current (/^-/) code does AFAICS, or did I miss something?

commit 75cd1d7d6a3747ec297f24f67f589acd8f5a9859

Author: Eivind Naess <[email protected]>
Date:   Thu Nov 3 09:28:46 2011 -0700

xt_ipv4options: fix an infinite loop

---
 doc/changelog.txt           |    1 +
 extensions/xt_ipv4options.c |   11 +++++++++++
 2 files changed, 12 insertions(+), 0 deletions(-)
doc/changelog.txt +1 -0
index 3557175..81fcbdc 100644
@@ -5,6 +5,7 @@ Fixes:
 - build: the code actually requires at least iptables 1.4.5 (would yield a
   compile error otherwise), make sure configure checks for it; update INSTALL
 - xt_ECHO: fix kernel warning about RTAX_HOPLIMIT being used
+- xt_ipv4options: fix an infinite loop
 Changes:
 - xt_ECHO: now calculates UDP checksum
 Enhancements:
extensions/xt_ipv4options.c +11 -0
index 42481f7..5e9d34c 100644
@@ -20,6 +20,17 @@ static uint32_t ipv4options_rd(const uint8_t *data, int len)
 	uint32_t opts = 0;
 
 	while (len >= 2) {
+		switch (data[0]) {
+		case IPOPT_END:
+			return opts;
+		case IPOPT_NOOP:
+			--len;
+			++data;
+			continue;
+		}
+
+		if (data[1] < 2 || data[1] > len)
+			return opts;
 		opts |= 1 << (data[0] & 0x1F);
 		len  -= data[1];
 		data += data[1];

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw

Hi Jan,

As far as the security hole goes, only the changes to
make it parse the in ipv4options_rd is needed. If my understanding for the option *any* was wrong given the manual, then I'll take that back. Let me know how you want to proceed with this, I can provide you another
patch with the update to ipv4options_rd only, and with proper
indentation.

Regards,
- Eivind

----- Original Message -----
From: Jan Engelhardt <[email protected]>
To: Eivind Naess <[email protected]>
Cc: "[email protected]" <[email protected]>
Sent: Saturday, November 5, 2011 7:34 AM
Subject: Re: Found a dead-freeze bug, in xt_ipv4options.c -- patch provided

Quoted 25 lines · Eivind Naess
(quoted diff) +1 -1
-static uint32_t ipv4options_rd(const uint8_t *data, int len)
+static uint32_t ipv4options_rd(const uint8_t *ptr, int len)

 {
     uint32_t opts = 0;
+    uint32_t olen = 0;

     while (len >= 2) {
-        opts |= 1 << (data[0] & 0x1F);
-        len  -= data[1];
-        data += data[1];
+
+        /* Handle special options */
+        switch (*ptr) {
+        case IPOPT_END:
+            return opts;
+        case IPOPT_NOOP:
+            len--;
+            ptr++;
+            continue;
+        }
+
+        /* Get the length of the option */
+        olen = ptr[1];
+        if (olen < 2 || olen > len)
+            return opts;

Quoted 8 lines

+
+        /* Set the option bit, and incr/decr the invariants */ +        opts |= 1 << (*ptr & 0x1F);
+        len  -= olen;
+        ptr  += olen;
     }

     return opts;

Whitespace damage, I had to apply this manually, and in doing so, reduced the size. You can test the patch from the "v4options" branch, copied below.

Quoted 13 lines
(quoted diff) +0 -0
@@ -36,13 +54,20 @@
      uint32_t opts = 0;
      uint16_t len  = ip_hdrlen(skb) - sizeof(struct iphdr);


-    if (len > 0)
+    if (len > 0) {
         opts = ipv4options_rd((const void *)iph + sizeof(struct iphdr), len);
+        if(info->flags & XT_V4OPTS_ANY) {
+            return  !!opts; /* !! to make bool value */ +        }else {

     opts ^= info->invert;

Quoted 8 lines

     opts &= info->map;
-    return (info->flags & XT_V4OPTS_ANY) ? opts : opts == info->map; +            return (opts == info->map);
+        }
+    }
+
+    return 0;
 }

XT_V4OPTS_ANY is meant to match on {any of {the bits present in info->map}}, not {any of {all possible IPv4 options}} according to the documentation. The test for XT_V4OPTS_ANY is therefore supposed to come after the ^ and & operations, and that is what the current (/^-/) code does AFAICS, or did I miss something?

commit 75cd1d7d6a3747ec297f24f67f589acd8f5a9859
Author: Eivind Naess <[email protected]>
Date:  Thu Nov 3 09:28:46 2011 -0700

xt_ipv4options: fix an infinite loop
---
doc/changelog.txt          |    1 +
extensions/xt_ipv4options.c |  11 +++++++++++

2 files changed, 12 insertions(+), 0 deletions(-)
doc/changelog.txt +0 -1
index 3557175..81fcbdc 100644
@@ -5,6 +5,7 @@ Fixes:
- build: the code actually requires at least iptables 1.4.5 (would yield a

  compile error otherwise), make sure configure checks for it; update INSTALL
- xt_ECHO: fix kernel warning about RTAX_HOPLIMIT being used
+- xt_ipv4options: fix an infinite loop
Changes:
- xt_ECHO: now calculates UDP checksum
Enhancements:

extensions/xt_ipv4options.c +0 -0
index 42481f7..5e9d34c 100644
@@ -20,6 +20,17 @@ static uint32_t ipv4options_rd(const uint8_t *data, int len)

    uint32_t opts = 0;

    while (len >= 2) {
+        switch (data[0]) {
+        case IPOPT_END:
+            return opts;
+        case IPOPT_NOOP:
+            --len;
+            ++data;
+            continue;
+        }
+
+        if (data[1] < 2 || data[1] > len)
+            return opts;
        opts |= 1 << (data[0] & 0x1F);
        len  -= data[1];
        data += data[1];
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 3 lines · Eivind Naess

As far as the security hole goes, only the changes to
make it parse the in ipv4options_rd is needed. If my understanding for the option *any* was wrong given the manual, then I'll take that back.

It seems that your version of "any" is representable using

--any --flags 1,2,3,4,5,6,7,8,9,10,11,12...,31

Quoted 3 lines

Let me know how you want to proceed with this, I can provide you another patch with the update to ipv4options_rd only, and with proper indentation.

I proceeded like this:

Quoted 35 lines

commit 75cd1d7d6a3747ec297f24f67f589acd8f5a9859
Author: Eivind Naess <[email protected]>
Date:  Thu Nov 3 09:28:46 2011 -0700

xt_ipv4options: fix an infinite loop
---
doc/changelog.txt          |    1 +
extensions/xt_ipv4options.c |  11 +++++++++++

2 files changed, 12 insertions(+), 0 deletions(-)
doc/changelog.txt +0 -1
index 3557175..81fcbdc 100644
@@ -5,6 +5,7 @@ Fixes:
- build: the code actually requires at least iptables 1.4.5 (would yield a

  compile error otherwise), make sure configure checks for it; update INSTALL
- xt_ECHO: fix kernel warning about RTAX_HOPLIMIT being used
+- xt_ipv4options: fix an infinite loop
Changes:
- xt_ECHO: now calculates UDP checksum
Enhancements:

extensions/xt_ipv4options.c +4 -0
index 42481f7..5e9d34c 100644
@@ -20,6 +20,17 @@ static uint32_t ipv4options_rd(const uint8_t *data, int len)
     uint32_t opts = 0;
 
     while (len >= 2) {
+        switch (data[0]) {
+        case IPOPT_END:
+            return opts;
+        case IPOPT_NOOP:

extensions/xt_ipv4options.c static uint32_t ipv4options_rd(const uint8_t *data, int len) · line 36

Quoted 10 lines
extensions/xt_ipv4options.c +7 -0
+            --len;
+            ++data;
+            continue;
+        }
+
+        if (data[1] < 2 || data[1] > len)
+            return opts;
         opts |= 1 << (data[0] & 0x1F);
         len  -= data[1];
         data += data[1];

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw

Hi Jan,
That change looks good to me as it provides the same functionality. I'll re-test on my end on Monday. Thanks you for accepting the suggested patch despite the extra work on your end.

Regards,

- Eivind

----- Original Message -----
From: Jan Engelhardt <[email protected]>
To: Eivind Naess <[email protected]>
Cc: "[email protected]" <[email protected]>
Sent: Saturday, November 5, 2011 11:20 AM
Subject: Re: Found a dead-freeze bug, in xt_ipv4options.c -- patch provided

Quoted 3 lines · Eivind Naess

As far as the security hole goes, only the changes to
make it parse the in ipv4options_rd is needed. If my understanding for the option *any* was wrong given the manual, then I'll take that back.

It seems that your version of "any" is representable using

--any --flags 1,2,3,4,5,6,7,8,9,10,11,12...,31

Quoted 3 lines

Let me know how you want to proceed with this, I can provide you another patch with the update to ipv4options_rd only, and with proper indentation.

I proceeded like this:

Quoted 35 lines

commit 75cd1d7d6a3747ec297f24f67f589acd8f5a9859
Author: Eivind Naess <[email protected]>
Date:   Thu Nov 3 09:28:46 2011 -0700

xt_ipv4options: fix an infinite loop
---
doc/changelog.txt           |    1 +
extensions/xt_ipv4options.c |   11 +++++++++++

2 files changed, 12 insertions(+), 0 deletions(-)
doc/changelog.txt +0 -1
index 3557175..81fcbdc 100644
@@ -5,6 +5,7 @@ Fixes:
- build: the code actually requires at least iptables 1.4.5 (would yield a

   compile error otherwise), make sure configure checks for it; update INSTALL
- xt_ECHO: fix kernel warning about RTAX_HOPLIMIT being used
+- xt_ipv4options: fix an infinite loop
Changes:
- xt_ECHO: now calculates UDP checksum
Enhancements:

extensions/xt_ipv4options.c +4 -0
index 42481f7..5e9d34c 100644
@@ -20,6 +20,17 @@ static uint32_t ipv4options_rd(const uint8_t *data, int len)
     uint32_t opts = 0;
 
     while (len >= 2) {
+        switch (data[0]) {
+        case IPOPT_END:
+            return opts;
+        case IPOPT_NOOP:

extensions/xt_ipv4options.c static uint32_t ipv4options_rd(const uint8_t *data, int len) · line 36

Quoted 10 lines
extensions/xt_ipv4options.c +7 -0
+            --len;
+            ++data;
+            continue;
+        }
+
+        if (data[1] < 2 || data[1] > len)
+            return opts;
         opts |= 1 << (data[0] & 0x1F);
         len  -= data[1];
         data += data[1];

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html