Skip to contentSkip to conversation

iproute2: xfrm state add abort issue

PATCH In review Sohny Thomas 4 participants 8 messages Sep 26, 2013 – Oct 8, 2013 1 message not in the archive

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

PATCH 1/1 iproute2: xfrm state add abort issue

In reply to a message not in the archive

ip xfrm state add causes a SIGABRT due to a strncpy_chk . This happens since strncpy doesn't account for the '\0' . I have fixed this using sizeof instead of strlen .

There is a redhat bug which documents this issue

https://bugzilla.redhat.com/show_bug.cgi?id=982761

--------------

ip/xfrm_state.c +1 -1
index 389942c..7dd8799 100644
@@ -117,7 +117,7 @@ static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type,
                            char *name, char *key, char *buf, int max)
  {
         int len;
-       int slen = strlen(key);
+       int slen = sizeof(key);
 
  #if 0
         /* XXX: verifying both name and key is required! */
#2 lore raw
Quoted 17 lines

ip xfrm state add causes a SIGABRT due to a strncpy_chk . This happens since strncpy doesn't account for the '\0' . I have fixed this using sizeof instead of strlen .

There is a redhat bug which documents this issue

https://bugzilla.redhat.com/show_bug.cgi?id=982761

--------------

ip/xfrm_state.c +0 -0
index 389942c..7dd8799 100644

ip/xfrm_state.c static int xfrm_algo_parse(struct xfrm_algo *alg, · line 121

Quoted 6 lines
ip/xfrm_state.c +1 -1
@@ -117,7 +117,7 @@ static int xfrm_algo_parse(struct xfrm_algo *alg,
 enum xfrm_attr_type_t type,
                             char *name, char *key, char *buf, int max)
   {
          int len;
-       int slen = strlen(key);
+       int slen = sizeof(key);

you definitely don't want sizeof(key) - that is either 4 or 8.

David

#3 lore raw
Quoted 12 lines · David Laight
Quoted 12 lines

ip xfrm state add causes a SIGABRT due to a strncpy_chk . This happens since strncpy doesn't account for the '\0' . I have fixed this using sizeof instead of strlen .

There is a redhat bug which documents this issue

https://bugzilla.redhat.com/show_bug.cgi?id=982761

--------------

Quoted 13 lines

ip/xfrm_state.c static int xfrm_algo_parse(struct xfrm_algo *alg, · line 121

Quoted 11 lines
ip/xfrm_state.c +1 -1
index 389942c..7dd8799 100644
@@ -117,7 +117,7 @@ static int xfrm_algo_parse(struct xfrm_algo *alg,
 enum xfrm_attr_type_t type,
                              char *name, char *key, char *buf, int max)
    {
           int len;
-       int slen = strlen(key);
+       int slen = sizeof(key);

you definitely don't want sizeof(key) - that is either 4 or 8.

oh damn my bad.
I think i will go with strlen(key) + 1.

or i will pass slen+1 to strncpy .

Regards,
Sohny

Quoted 6 lines

David

#4 lore raw

PATCH 1/1 {iproute2, xfrm}: Use memcpy to suppress gcc phony buffer overflow warning

Quoted 26 lines · Sohny Thomas
Quoted 12 lines · David Laight
Quoted 12 lines

ip xfrm state add causes a SIGABRT due to a strncpy_chk . This happens since strncpy doesn't account for the '\0' . I have fixed this using sizeof instead of strlen .

There is a redhat bug which documents this issue

https://bugzilla.redhat.com/show_bug.cgi?id=982761

--------------

Quoted 13 lines

ip/xfrm_state.c static int xfrm_algo_parse(struct xfrm_algo *alg, · line 121

Quoted 11 lines
ip/xfrm_state.c +1 -1
index 389942c..7dd8799 100644
@@ -117,7 +117,7 @@ static int xfrm_algo_parse(struct xfrm_algo *alg,
 enum xfrm_attr_type_t type,
 char *name, char *key, char *buf, int max)
 {
 int len;
- int slen = strlen(key);
+ int slen = sizeof(key);

you definitely don't want sizeof(key) - that is either 4 or 8.

Quoted 4 lines

oh damn my bad.
I think i will go with strlen(key) + 1.

or i will pass slen+1 to strncpy .

You must be kidding about this slen+1 or strlen(key) + 1 , right?

From 3895b3d88bcb873988089392079645f2c9665e95 Mon Sep 17 00:00:00 2001
From: Fan Du <[email protected]>
Date: Sun, 29 Sep 2013 09:38:12 +0800
Subject: [PATCH] {iproute2, xfrm}: Use memcpy to suppress gcc phony buffer
overflow warning.

This bug is reported from below link:
https://bugzilla.redhat.com/show_bug.cgi?id=982761

An simplified command from its original reproducing method in bugzilla:
ip xfrm state add src 10.0.0.2 dst 10.0.0.1 proto ah spi 0x12345678 auth md5 12 will cause below spew from gcc:

*** buffer overflow detected ***: ./ip terminated
======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x37)[0x7f894ae2b817] /lib/x86_64-linux-gnu/libc.so.6(+0x109710)[0x7f894ae2a710] /lib/x86_64-linux-gnu/libc.so.6(+0x1089f6)[0x7f894ae299f6] ./ip[0x42119d]
./ip(do_xfrm_state+0x231)[0x4217f1]
./ip[0x405d05]
./ip(main+0x2b4)[0x405934] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f894ad4276d] ./ip[0x405bb9]
======= Memory map: ========

00400000-0043c000 r-xp 00000000 08:01 1997183                            /workbench/iproute2/ip/ip
0063b000-0063c000 r--p 0003b000 08:01 1997183                            /workbench/iproute2/ip/ip
0063c000-00640000 rw-p 0003c000 08:01 1997183                            /workbench/iproute2/ip/ip
00640000-00643000 rw-p 00000000 00:00 0
017c7000-017e8000 rw-p 00000000 00:00 0                                  [heap]
7f894ab0b000-7f894ab20000 r-xp 00000000 08:01 6032986                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f894ab20000-7f894ad1f000 ---p 00015000 08:01 6032986                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f894ad1f000-7f894ad20000 r--p 00014000 08:01 6032986                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f894ad20000-7f894ad21000 rw-p 00015000 08:01 6032986                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f894ad21000-7f894aed6000 r-xp 00000000 08:01 6033273                    /lib/x86_64-linux-gnu/libc-2.15.so
7f894aed6000-7f894b0d5000 ---p 001b5000 08:01 6033273                    /lib/x86_64-linux-gnu/libc-2.15.so
7f894b0d5000-7f894b0d9000 r--p 001b4000 08:01 6033273                    /lib/x86_64-linux-gnu/libc-2.15.so
7f894b0d9000-7f894b0db000 rw-p 001b8000 08:01 6033273                    /lib/x86_64-linux-gnu/libc-2.15.so
7f894b0db000-7f894b0e0000 rw-p 00000000 00:00 0
7f894b0e0000-7f894b0e2000 r-xp 00000000 08:01 6033272                    /lib/x86_64-linux-gnu/libdl-2.15.so
7f894b0e2000-7f894b2e2000 ---p 00002000 08:01 6033272                    /lib/x86_64-linux-gnu/libdl-2.15.so
7f894b2e2000-7f894b2e3000 r--p 00002000 08:01 6033272                    /lib/x86_64-linux-gnu/libdl-2.15.so
7f894b2e3000-7f894b2e4000 rw-p 00003000 08:01 6033272                    /lib/x86_64-linux-gnu/libdl-2.15.so
7f894b2e4000-7f894b306000 r-xp 00000000 08:01 6033287                    /lib/x86_64-linux-gnu/ld-2.15.so
7f894b4e3000-7f894b4e6000 rw-p 00000000 00:00 0
7f894b503000-7f894b506000 rw-p 00000000 00:00 0
7f894b506000-7f894b507000 r--p 00022000 08:01 6033287                    /lib/x86_64-linux-gnu/ld-2.15.so
7f894b507000-7f894b509000 rw-p 00023000 08:01 6033287                    /lib/x86_64-linux-gnu/ld-2.15.so
7fff9a3c2000-7fff9a3e3000 rw-p 00000000 00:00 0                          [stack]
7fff9a3ff000-7fff9a400000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

This is a false positive warning as the destination pointer "buf" pointers to an ZERO length array, which actually will occupy alg.buf mostly. Fix this by using memcpy.

struct xfrm_algo {
        char            alg_name[64];
        unsigned int    alg_key_len;    /* in bits */
        char            alg_key[0];
};
struct {
        union {
                struct xfrm_algo alg;
                struct xfrm_algo_aead aead;
                struct xfrm_algo_auth auth;
        } u;
        char buf[XFRM_ALGO_KEY_BUF_SIZE];
} alg = {};

buf = alg.u.alg.alg_key;

---
 ip/xfrm_state.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ip/xfrm_state.c +1 -1
index 0d98e78..5cc87d3 100644
@@ -159,7 +159,7 @@ static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type,
 			if (len > max)
 				invarg("\"ALGO-KEY\" makes buffer overflow\n", key);
 
-			strncpy(buf, key, len);
+			memcpy(buf, key, len);
 		}
 	}
 
Signature · 1 line
1.7.9.5
Quoted 14 lines

Regards,
Sohny

Quoted 6 lines

David

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

Signature · 3 lines
浮沉随浪只记今朝笑

--fan
#5 lore raw

Re: [PATCH] {iproute2, xfrm}: Use memcpy to suppress gcc phony buffer overflow warning

Quoted 29 lines · Fan Du
Quoted 26 lines · Sohny Thomas
Quoted 25 lines · David Laight
Quoted 17 lines

ip xfrm state add causes a SIGABRT due to a strncpy_chk . This happens since strncpy doesn't account for the '\0' . I have fixed this using sizeof instead of strlen .

There is a redhat bug which documents this issue

https://bugzilla.redhat.com/show_bug.cgi?id=982761

--------------

ip/xfrm_state.c +0 -0
index 389942c..7dd8799 100644

ip/xfrm_state.c static int xfrm_algo_parse(struct xfrm_algo *alg, · line 121

Quoted 6 lines
ip/xfrm_state.c +1 -1
@@ -117,7 +117,7 @@ static int xfrm_algo_parse(struct xfrm_algo *alg,
 enum xfrm_attr_type_t type,
 char *name, char *key, char *buf, int max)
 {
 int len;
- int slen = strlen(key);
+ int slen = sizeof(key);

you definitely don't want sizeof(key) - that is either 4 or 8.

Quoted 6 lines
Quoted 4 lines

oh damn my bad.
I think i will go with strlen(key) + 1.

or i will pass slen+1 to strncpy .

You must be kidding about this slen+1 or strlen(key) + 1 , right?

strncpy fails with an abort at strncpy_chk.
So I was assuming it was because of the '\0' , so the +1 Thanks for the below patch

Quoted 117 lines

From 3895b3d88bcb873988089392079645f2c9665e95 Mon Sep 17 00:00:00 2001
From: Fan Du <[email protected]>
Date: Sun, 29 Sep 2013 09:38:12 +0800
Subject: [PATCH] {iproute2, xfrm}: Use memcpy to suppress gcc phony buffer
overflow warning.

This bug is reported from below link:
https://bugzilla.redhat.com/show_bug.cgi?id=982761

An simplified command from its original reproducing method in bugzilla:
ip xfrm state add src 10.0.0.2 dst 10.0.0.1 proto ah spi 0x12345678 auth md5 12
will cause below spew from gcc:

*** buffer overflow detected ***: ./ip terminated
======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x37)[0x7f894ae2b817] /lib/x86_64-linux-gnu/libc.so.6(+0x109710)[0x7f894ae2a710] /lib/x86_64-linux-gnu/libc.so.6(+0x1089f6)[0x7f894ae299f6] ./ip[0x42119d]
./ip(do_xfrm_state+0x231)[0x4217f1]
./ip[0x405d05]
./ip(main+0x2b4)[0x405934] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f894ad4276d] ./ip[0x405bb9]
======= Memory map: ========
00400000-0043c000 r-xp 00000000 08:01 1997183 /workbench/iproute2/ip/ip
0063b000-0063c000 r--p 0003b000 08:01 1997183 /workbench/iproute2/ip/ip
0063c000-00640000 rw-p 0003c000 08:01 1997183 /workbench/iproute2/ip/ip
00640000-00643000 rw-p 00000000 00:00 0
017c7000-017e8000 rw-p 00000000 00:00 0
[heap]
7f894ab0b000-7f894ab20000 r-xp 00000000 08:01 6032986 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f894ab20000-7f894ad1f000 ---p 00015000 08:01 6032986 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f894ad1f000-7f894ad20000 r--p 00014000 08:01 6032986 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f894ad20000-7f894ad21000 rw-p 00015000 08:01 6032986 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f894ad21000-7f894aed6000 r-xp 00000000 08:01 6033273 /lib/x86_64-linux-gnu/libc-2.15.so
7f894aed6000-7f894b0d5000 ---p 001b5000 08:01 6033273 /lib/x86_64-linux-gnu/libc-2.15.so
7f894b0d5000-7f894b0d9000 r--p 001b4000 08:01 6033273 /lib/x86_64-linux-gnu/libc-2.15.so
7f894b0d9000-7f894b0db000 rw-p 001b8000 08:01 6033273 /lib/x86_64-linux-gnu/libc-2.15.so
7f894b0db000-7f894b0e0000 rw-p 00000000 00:00 0 7f894b0e0000-7f894b0e2000 r-xp 00000000 08:01 6033272 /lib/x86_64-linux-gnu/libdl-2.15.so 7f894b0e2000-7f894b2e2000 ---p 00002000 08:01 6033272 /lib/x86_64-linux-gnu/libdl-2.15.so 7f894b2e2000-7f894b2e3000 r--p 00002000 08:01 6033272 /lib/x86_64-linux-gnu/libdl-2.15.so 7f894b2e3000-7f894b2e4000 rw-p 00003000 08:01 6033272 /lib/x86_64-linux-gnu/libdl-2.15.so 7f894b2e4000-7f894b306000 r-xp 00000000 08:01 6033287 /lib/x86_64-linux-gnu/ld-2.15.so
7f894b4e3000-7f894b4e6000 rw-p 00000000 00:00 0 7f894b503000-7f894b506000 rw-p 00000000 00:00 0 7f894b506000-7f894b507000 r--p 00022000 08:01 6033287 /lib/x86_64-linux-gnu/ld-2.15.so
7f894b507000-7f894b509000 rw-p 00023000 08:01 6033287 /lib/x86_64-linux-gnu/ld-2.15.so
7fff9a3c2000-7fff9a3e3000 rw-p 00000000 00:00 0
[stack]
7fff9a3ff000-7fff9a400000 r-xp 00000000 00:00 0
[vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]

This is a false positive warning as the destination pointer "buf" pointers to
an ZERO length array, which actually will occupy alg.buf mostly. Fix this by using memcpy.

struct xfrm_algo {
         char            alg_name[64];
         unsigned int    alg_key_len;    /* in bits */
         char            alg_key[0];
};
struct {
         union {
                 struct xfrm_algo alg;
                 struct xfrm_algo_aead aead;
                 struct xfrm_algo_auth auth;
         } u;
         char buf[XFRM_ALGO_KEY_BUF_SIZE];
} alg = {};

buf = alg.u.alg.alg_key;

---
  ip/xfrm_state.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
ip/xfrm_state.c +1 -1
index 0d98e78..5cc87d3 100644
@@ -159,7 +159,7 @@ static int xfrm_algo_parse(struct xfrm_algo *alg,
 enum xfrm_attr_type_t type,
              if (len > max)
                  invarg("\"ALGO-KEY\" makes buffer overflow\n", key);
 
-            strncpy(buf, key, len);
+            memcpy(buf, key, len);
          }
      }
#6 lore raw

RE: [PATCH] {iproute2, xfrm}: Use memcpy to suppress gcc phony buffer overflow warning

Quoted 20 lines
Quoted 20 lines

This is a false positive warning as the destination pointer "buf" pointers to
an ZERO length array, which actually will occupy alg.buf mostly. Fix this by using memcpy.

struct xfrm_algo {
         char            alg_name[64];
         unsigned int    alg_key_len;    /* in bits */
         char            alg_key[0];
};
struct {
         union {
                 struct xfrm_algo alg;
                 struct xfrm_algo_aead aead;
                 struct xfrm_algo_auth auth;
         } u;
         char buf[XFRM_ALGO_KEY_BUF_SIZE];
} alg = {};
Quoted 1 line
Quoted 1 line

buf = alg.u.alg.alg_key;

That is worse than horrid...
The tools have every right to complain about any accesses to alg_key[].

Quoted 4 lines
Quoted 4 lines
---
  ip/xfrm_state.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 8 lines

ip/xfrm_state.c static int xfrm_algo_parse(struct xfrm_algo *alg, · line 161

Quoted 8 lines
ip/xfrm_state.c +0 -0
index 0d98e78..5cc87d3 100644
@@ -159,7 +159,7 @@ static int xfrm_algo_parse(struct xfrm_algo *alg,
 enum xfrm_attr_type_t type,
              if (len > max)
                  invarg("\"ALGO-KEY\" makes buffer overflow\n", key);

I presume there is a return hiding in invarg().

Quoted 3 lines

ip/xfrm_state.c static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type, · line 162

Quoted 3 lines
ip/xfrm_state.c +1 -1
-            strncpy(buf, key, len);
+            memcpy(buf, key, len);

Passing the length of the SOURCE to strncpy() is almost always wrong. You are still not terminating the copied string.

David

#8 lore raw

Re: [PATCH] {iproute2, xfrm}: Use memcpy to suppress gcc phony buffer overflow warning

In reply to Fan Du
Quoted 6 lines
ip/xfrm_state.c +0 -0
index 0d98e78..5cc87d3 100644

ip/xfrm_state.c static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type, · line 165

Quoted 8 lines
ip/xfrm_state.c +1 -1
@@ -159,7 +159,7 @@ static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type,
  			if (len > max)
  				invarg("\"ALGO-KEY\" makes buffer overflow\n", key);
 
-			strncpy(buf, key, len);
+			memcpy(buf, key, len);
  		}
  	}
 

Applied this patch. With some edits to make commit message logical.

#7 lore raw

Re: [PATCH] {iproute2, xfrm}: Use memcpy to suppress gcc phony buffer overflow warning

In reply to David Laight

Back from vacation, sorry for the late reply.

Quoted 20 lines · David Laight
Quoted 20 lines
Quoted 20 lines

This is a false positive warning as the destination pointer "buf" pointers to
an ZERO length array, which actually will occupy alg.buf mostly. Fix this by using memcpy.

struct xfrm_algo {
          char            alg_name[64];
          unsigned int    alg_key_len;    /* in bits */
          char            alg_key[0];
};
struct {
          union {
                  struct xfrm_algo alg;
                  struct xfrm_algo_aead aead;
                  struct xfrm_algo_auth auth;
          } u;
          char buf[XFRM_ALGO_KEY_BUF_SIZE];
} alg = {};
Quoted 4 lines
Quoted 1 line
Quoted 1 line

buf = alg.u.alg.alg_key;

That is worse than horrid...
The tools have every right to complain about any accesses to alg_key[].

Only when using strcpy, because a build in checking inserted in this function.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
---
   ip/xfrm_state.c |    2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 10 lines
Quoted 8 lines

ip/xfrm_state.c static int xfrm_algo_parse(struct xfrm_algo *alg, · line 161

Quoted 8 lines
ip/xfrm_state.c +0 -0
index 0d98e78..5cc87d3 100644
@@ -159,7 +159,7 @@ static int xfrm_algo_parse(struct xfrm_algo *alg,
 enum xfrm_attr_type_t type,
               if (len>  max)
                   invarg("\"ALGO-KEY\" makes buffer overflow\n", key);

I presume there is a return hiding in invarg().

Good guess :)

Quoted 6 lines
Quoted 3 lines

ip/xfrm_state.c static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type, · line 162

Quoted 3 lines
ip/xfrm_state.c +1 -1
-            strncpy(buf, key, len);
+            memcpy(buf, key, len);

Passing the length of the SOURCE to strncpy() is almost always wrong. You are still not terminating the copied string.

Don't worry.

The length using here has been increased by 1 at the beginning of the function, so the copied string to the destination is terminated well.

Quoted 3 lines

David

Signature · 3 lines
浮沉随浪只记今朝笑

--fan