Skip to contentSkip to conversation

net/eth: Fix stack-buffer-overflow in _eth_get_rss_ex_dst_addr()

PATCH Superseded Philippe Mathieu-Daudé 2 participants 17 messages Mar 10, 2021 – Mar 11, 2021

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover net/eth: Fix stack-buffer-overflow in _eth_get_rss_ex_dst_addr()

I had a look at the patch from Miroslav trying to silence a compiler warning which in fact is a nasty bug. Here is a fix.
https://www.mail-archive.com/[email protected]/msg772735.html

Since v4:
- reworked again, tested it with Fedora Raw Hide

Philippe Mathieu-Daudé (7):
net/eth: Simplify _eth_get_rss_ex_dst_addr()
net/eth: Better describe _eth_get_rss_ex_dst_addr's offset argument net/eth: Make ip6_ext_hdr *ext_hdr pointer to const net/eth: Check the size earlier
net/eth: Check iovec has enough data earlier
net/eth: Read ip6_ext_hdr_routing buffer before accessing it net/eth: Add an assert() and invert if() statement to simplify code

 net/eth.c                      | 48 +++++++++++++++---------------
 tests/qtest/fuzz-e1000e-test.c | 53 ++++++++++++++++++++++++++++++++++
 MAINTAINERS                    |  1 +
 tests/qtest/meson.build        |  1 +
 4 files changed, 79 insertions(+), 24 deletions(-)
 create mode 100644 tests/qtest/fuzz-e1000e-test.c
Signature · 1 line
2.26.2
#1 lore raw

PATCH v5 1/7 net/eth: Simplify _eth_get_rss_ex_dst_addr()

The length field is already contained in the ip6_ext_hdr structure. Check it direcly in eth_parse_ipv6_hdr() before calling _eth_get_rss_ex_dst_addr(), which gets a bit simplified.

---
 net/eth.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
net/eth.c +7 -7
index 1e0821c5f81..7d4dd48c1ff 100644
@@ -407,9 +407,7 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,
 {
     struct ip6_ext_hdr_routing *rthdr = (struct ip6_ext_hdr_routing *) ext_hdr;
 
-    if ((rthdr->rtype == 2) &&
-        (rthdr->len == sizeof(struct in6_address) / 8) &&
-        (rthdr->segleft == 1)) {
+    if ((rthdr->rtype == 2) && (rthdr->segleft == 1)) {
 
         size_t input_size = iov_size(pkt, pkt_frags);
         size_t bytes_read;
@@ -528,10 +526,12 @@ bool eth_parse_ipv6_hdr(const struct iovec *pkt, int pkt_frags,
         }
 
         if (curr_ext_hdr_type == IP6_ROUTING) {
-            info->rss_ex_dst_valid =
-                _eth_get_rss_ex_dst_addr(pkt, pkt_frags,
-                                         ip6hdr_off + info->full_hdr_len,
-                                         &ext_hdr, &info->rss_ex_dst);
+            if (ext_hdr.ip6r_len == sizeof(struct in6_address) / 8) {
+                info->rss_ex_dst_valid =
+                    _eth_get_rss_ex_dst_addr(pkt, pkt_frags,
+                                             ip6hdr_off + info->full_hdr_len,
+                                             &ext_hdr, &info->rss_ex_dst);
+            }
         } else if (curr_ext_hdr_type == IP6_DESTINATON) {
             info->rss_ex_src_valid =
                 _eth_get_rss_ex_src_addr(pkt, pkt_frags,
Signature · 1 line
2.26.2
#2 lore raw

PATCH v5 2/7 net/eth: Better describe _eth_get_rss_ex_dst_addr's offset argument

In reply to Philippe Mathieu-Daudé

The 'offset' argument represents the offset to the ip6_ext_hdr header, rename it as 'ext_hdr_offset'.

---
 net/eth.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/eth.c +3 -3
index 7d4dd48c1ff..6a5a1d04e0b 100644
@@ -401,7 +401,7 @@ eth_is_ip6_extension_header_type(uint8_t hdr_type)
 
 static bool
 _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,
-                        size_t rthdr_offset,
+                        size_t ext_hdr_offset,
                         struct ip6_ext_hdr *ext_hdr,
                         struct in6_address *dst_addr)
 {
@@ -412,12 +412,12 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,
         size_t input_size = iov_size(pkt, pkt_frags);
         size_t bytes_read;
 
-        if (input_size < rthdr_offset + sizeof(*ext_hdr)) {
+        if (input_size < ext_hdr_offset + sizeof(*ext_hdr)) {
             return false;
         }
 
         bytes_read = iov_to_buf(pkt, pkt_frags,
-                                rthdr_offset + sizeof(*ext_hdr),
+                                ext_hdr_offset + sizeof(*ext_hdr),
                                 dst_addr, sizeof(*dst_addr));
 
         return bytes_read == sizeof(*dst_addr);
Signature · 1 line
2.26.2
#3 lore raw

PATCH v5 3/7 net/eth: Make ip6_ext_hdr *ext_hdr pointer to const

In reply to Philippe Mathieu-Daudé

As the callee doesn't need to modify the ext_hdr buffer, mark it const.

---
 net/eth.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/eth.c +1 -1
index 6a5a1d04e0b..7c47a267a7a 100644
@@ -402,7 +402,7 @@ eth_is_ip6_extension_header_type(uint8_t hdr_type)
 static bool
 _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,
                         size_t ext_hdr_offset,
-                        struct ip6_ext_hdr *ext_hdr,
+                        const struct ip6_ext_hdr *ext_hdr,
                         struct in6_address *dst_addr)
 {
     struct ip6_ext_hdr_routing *rthdr = (struct ip6_ext_hdr_routing *) ext_hdr;
Signature · 1 line
2.26.2
#4 lore raw

Re: [PATCH v5 3/7] net/eth: Make ip6_ext_hdr *ext_hdr pointer to const

Quoted 19 lines · Philippe Mathieu-Daudé

As the callee doesn't need to modify the ext_hdr buffer, mark it const.

---
net/eth.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
net/eth.c +0 -0
index 6a5a1d04e0b..7c47a267a7a 100644
@@ -402,7 +402,7 @@ eth_is_ip6_extension_header_type(uint8_t hdr_type)

static bool
_eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,
size_t ext_hdr_offset,

net/eth.c +1 -1
-                        struct ip6_ext_hdr *ext_hdr,
+                        const struct ip6_ext_hdr *ext_hdr,
                        struct in6_address *dst_addr)
Quoted 5 lines
{
    struct ip6_ext_hdr_routing *rthdr = (struct ip6_ext_hdr_routing *) ext_hdr;
Signature · 1 line
2.26.2

This patch is fine, but after the last patch of this series, 'ext_hdr' is not used anymore, so what about removing this parameter?

Thanks,
Stefano

#5 lore raw

Re: [PATCH v5 3/7] net/eth: Make ip6_ext_hdr *ext_hdr pointer to const

Quoted 21 lines · Stefano Garzarella
Quoted 20 lines · Philippe Mathieu-Daudé

As the callee doesn't need to modify the ext_hdr buffer, mark it const.

---
net/eth.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
net/eth.c +0 -0
index 6a5a1d04e0b..7c47a267a7a 100644
@@ -402,7 +402,7 @@ eth_is_ip6_extension_header_type(uint8_t hdr_type)

static bool
_eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags, size_t ext_hdr_offset,
-                        struct ip6_ext_hdr *ext_hdr, +                        const struct ip6_ext_hdr *ext_hdr, struct in6_address *dst_addr) {

Quoted 8 lines
Quoted 5 lines

    struct ip6_ext_hdr_routing *rthdr = (struct ip6_ext_hdr_routing *) ext_hdr;
--
2.26.2

This patch is fine, but after the last patch of this series, 'ext_hdr' is not used anymore, so what about removing this parameter?

Clever :)

#6 lore raw

PATCH v5 4/7 net/eth: Check the size earlier

In reply to Philippe Mathieu-Daudé
---
 net/eth.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
net/eth.c +6 -8
index 7c47a267a7a..e870d02b0df 100644
@@ -406,16 +406,14 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,
                         struct in6_address *dst_addr)
 {
     struct ip6_ext_hdr_routing *rthdr = (struct ip6_ext_hdr_routing *) ext_hdr;
+    size_t input_size = iov_size(pkt, pkt_frags);
+    size_t bytes_read;
+
+    if (input_size < ext_hdr_offset + sizeof(*ext_hdr)) {
+        return false;
+    }
 
     if ((rthdr->rtype == 2) && (rthdr->segleft == 1)) {
-
-        size_t input_size = iov_size(pkt, pkt_frags);
-        size_t bytes_read;
-
-        if (input_size < ext_hdr_offset + sizeof(*ext_hdr)) {
-            return false;
-        }
-
         bytes_read = iov_to_buf(pkt, pkt_frags,
                                 ext_hdr_offset + sizeof(*ext_hdr),
                                 dst_addr, sizeof(*dst_addr));
Signature · 1 line
2.26.2
Reviewed-by on v5 4/7
#7 lore raw

Re: [PATCH v5 4/7] net/eth: Check the size earlier

Quoted 4 lines · Philippe Mathieu-Daudé

---
net/eth.c | 14 ++++++--------

1 file changed, 6 insertions(+), 8 deletions(-)
Quoted 31 lines
net/eth.c +6 -8
index 7c47a267a7a..e870d02b0df 100644
@@ -406,16 +406,14 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,
                        struct in6_address *dst_addr)
 {
    struct ip6_ext_hdr_routing *rthdr = (struct ip6_ext_hdr_routing *) ext_hdr;
+    size_t input_size = iov_size(pkt, pkt_frags);
+    size_t bytes_read;
+
+    if (input_size < ext_hdr_offset + sizeof(*ext_hdr)) {
+        return false;
+    }
 
    if ((rthdr->rtype == 2) && (rthdr->segleft == 1)) {
-
-        size_t input_size = iov_size(pkt, pkt_frags);
-        size_t bytes_read;
-
-        if (input_size < ext_hdr_offset + sizeof(*ext_hdr)) {
-            return false;
-        }
-
        bytes_read = iov_to_buf(pkt, pkt_frags,
                                ext_hdr_offset + sizeof(*ext_hdr),
                                dst_addr, sizeof(*dst_addr));
Signature · 1 line
2.26.2
#8 lore raw

PATCH v5 5/7 net/eth: Check iovec has enough data earlier

In reply to Philippe Mathieu-Daudé

We want to check fields from ip6_ext_hdr_routing structure and if correct read the full in6_address. Let's directly check if our iovec contains enough data for everything, else return early.

---
 net/eth.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/eth.c +1 -1
index e870d02b0df..28cdc843a69 100644
@@ -409,7 +409,7 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,
     size_t input_size = iov_size(pkt, pkt_frags);
     size_t bytes_read;
 
-    if (input_size < ext_hdr_offset + sizeof(*ext_hdr)) {
+    if (input_size < ext_hdr_offset + sizeof(*rthdr) + sizeof(*dst_addr)) {
         return false;
     }
 
Signature · 1 line
2.26.2
Reviewed-by on v5 5/7
#9 lore raw

Re: [PATCH v5 5/7] net/eth: Check iovec has enough data earlier

Quoted 16 lines · Philippe Mathieu-Daudé

We want to check fields from ip6_ext_hdr_routing structure and if correct read the full in6_address. Let's directly check if our iovec contains enough data for everything, else return early.

---
net/eth.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
net/eth.c +0 -0
index e870d02b0df..28cdc843a69 100644

net/eth.c _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags, · line 414

Quoted 7 lines
net/eth.c +1 -1
@@ -409,7 +409,7 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,
    size_t input_size = iov_size(pkt, pkt_frags);
    size_t bytes_read;
 
-    if (input_size < ext_hdr_offset + sizeof(*ext_hdr)) {
+    if (input_size < ext_hdr_offset + sizeof(*rthdr) + sizeof(*dst_addr)) {
        return false;
    }

If you have to respin, maybe we should also fix the offset in iov_to_buf() in this patch and queue it for stable:

@@ -415,7 +415,7 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,

if ((rthdr->rtype == 2) && (rthdr->segleft == 1)) {
bytes_read = iov_to_buf(pkt, pkt_frags,
- ext_hdr_offset + sizeof(*ext_hdr),
+ ext_hdr_offset + sizeof(*rthdr),

                                 dst_addr, sizeof(*dst_addr));
 
         return bytes_read == sizeof(*dst_addr);

Otherwise:

#10 lore raw

Re: [PATCH v5 5/7] net/eth: Check iovec has enough data earlier

Quoted 32 lines · Stefano Garzarella
Quoted 24 lines · Philippe Mathieu-Daudé

We want to check fields from ip6_ext_hdr_routing structure and if correct read the full in6_address. Let's directly check if our iovec contains enough data for everything, else return early.

---
net/eth.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
net/eth.c +1 -1
index e870d02b0df..28cdc843a69 100644
@@ -409,7 +409,7 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt,
 int pkt_frags,
     size_t input_size = iov_size(pkt, pkt_frags);
     size_t bytes_read;
 
-    if (input_size < ext_hdr_offset + sizeof(*ext_hdr)) {
+    if (input_size < ext_hdr_offset + sizeof(*rthdr) +
 sizeof(*dst_addr)) {
         return false;
Quoted 1 line

    }

If you have to respin, maybe we should also fix the offset in iov_to_buf() in this patch and queue it for stable:

(quoted diff) +0 -0
@@ -415,7 +415,7 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt,
 int pkt_frags,
Quoted 6 lines


     if ((rthdr->rtype == 2) && (rthdr->segleft == 1)) { bytes_read = iov_to_buf(pkt, pkt_frags,
-                                ext_hdr_offset + sizeof(*ext_hdr), +                                ext_hdr_offset + sizeof(*rthdr), dst_addr, sizeof(*dst_addr));

Oh, so we always screwed the address by 4 bytes...

This code never worked correctly :(

Quoted 7 lines


         return bytes_read == sizeof(*dst_addr);

Otherwise:

#11 lore raw

Re: [PATCH v5 5/7] net/eth: Check iovec has enough data earlier

Quoted 42 lines · Philippe Mathieu-Daudé
Quoted 32 lines · Stefano Garzarella
Quoted 24 lines · Philippe Mathieu-Daudé

We want to check fields from ip6_ext_hdr_routing structure and if correct read the full in6_address. Let's directly check if our iovec contains enough data for everything, else return early.

---
net/eth.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
net/eth.c +1 -1
index e870d02b0df..28cdc843a69 100644
@@ -409,7 +409,7 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt,
 int pkt_frags,
     size_t input_size = iov_size(pkt, pkt_frags);
     size_t bytes_read;
 
-    if (input_size < ext_hdr_offset + sizeof(*ext_hdr)) {
+    if (input_size < ext_hdr_offset + sizeof(*rthdr) +
 sizeof(*dst_addr)) {
         return false;
Quoted 1 line

    }

If you have to respin, maybe we should also fix the offset in iov_to_buf() in this patch and queue it for stable:

(quoted diff) +0 -0
@@ -415,7 +415,7 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt,
 int pkt_frags,
Quoted 6 lines


     if ((rthdr->rtype == 2) && (rthdr->segleft == 1)) { bytes_read = iov_to_buf(pkt, pkt_frags,
-                                ext_hdr_offset + sizeof(*ext_hdr), +                                ext_hdr_offset + sizeof(*rthdr), dst_addr, sizeof(*dst_addr));

Oh, so we always screwed the address by 4 bytes...

Quoted 1 line

This code never worked correctly :(

Confirmed with commit 4555ca6816c ("net: fix incorrect argument to iov_to_buf") when it then returns incorrect value until b2caa3b82ed ("net/eth: fix incorrect check of iov_to_buf() return value") one year later.

#12 lore raw

Re: [PATCH v5 5/7] net/eth: Check iovec has enough data earlier

Quoted 43 lines · Philippe Mathieu-Daudé
Quoted 42 lines · Philippe Mathieu-Daudé
Quoted 32 lines · Stefano Garzarella
Quoted 24 lines · Philippe Mathieu-Daudé

We want to check fields from ip6_ext_hdr_routing structure and if correct read the full in6_address. Let's directly check if our iovec contains enough data for everything, else return early.

---
net/eth.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
net/eth.c +1 -1
index e870d02b0df..28cdc843a69 100644
@@ -409,7 +409,7 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt,
 int pkt_frags,
     size_t input_size = iov_size(pkt, pkt_frags);
     size_t bytes_read;
 
-    if (input_size < ext_hdr_offset + sizeof(*ext_hdr)) {
+    if (input_size < ext_hdr_offset + sizeof(*rthdr) +
 sizeof(*dst_addr)) {
         return false;
Quoted 1 line

    }

If you have to respin, maybe we should also fix the offset in iov_to_buf() in this patch and queue it for stable:

(quoted diff) +0 -0
@@ -415,7 +415,7 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt,
 int pkt_frags,
Quoted 6 lines


     if ((rthdr->rtype == 2) && (rthdr->segleft == 1)) { bytes_read = iov_to_buf(pkt, pkt_frags,
-                                ext_hdr_offset + sizeof(*ext_hdr), +                                ext_hdr_offset + sizeof(*rthdr), dst_addr, sizeof(*dst_addr));

Oh, so we always screwed the address by 4 bytes...

Quoted 7 lines
Quoted 1 line

This code never worked correctly :(

Confirmed with commit 4555ca6816c ("net: fix incorrect argument to iov_to_buf") when it then returns incorrect value until b2caa3b82ed ("net/eth: fix incorrect check of iov_to_buf() return value") one year later.

Ooooh, I agree, it never worked but I have no idea how to test...

Thanks for fixing this code,
Stefano

#13 lore raw

PATCH v5 6/7 net/eth: Read ip6_ext_hdr_routing buffer before accessing it

In reply to Philippe Mathieu-Daudé

We can't know the caller read enough data in the memory pointed by ext_hdr to cast it as a ip6_ext_hdr_routing. Declare rt_hdr on the stack and fill it again from the iovec.

Since we already checked there is enough data in the iovec buffer, simply add an assert() call to consume the bytes_read variable.

This fix a 2 bytes buffer overrun in eth_parse_ipv6_hdr() reported by QEMU fuzzer:

  $ cat << EOF | ./qemu-system-i386 -M pc-q35-5.0 \
    -accel qtest -monitor none \
    -serial none -nographic -qtest stdio
  outl 0xcf8 0x80001010
  outl 0xcfc 0xe1020000
  outl 0xcf8 0x80001004
  outw 0xcfc 0x7
  write 0x25 0x1 0x86
  write 0x26 0x1 0xdd
  write 0x4f 0x1 0x2b
  write 0xe1020030 0x4 0x190002e1
  write 0xe102003a 0x2 0x0807
  write 0xe1020048 0x4 0x12077cdd
  write 0xe1020400 0x4 0xba077cdd
  write 0xe1020420 0x4 0x190002e1
  write 0xe1020428 0x4 0x3509d807
  write 0xe1020438 0x1 0xe2
  EOF
  =================================================================
  ==2859770==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffdef904902 at pc 0x561ceefa78de bp 0x7ffdef904820 sp 0x7ffdef904818
  READ of size 1 at 0x7ffdef904902 thread T0
      #0 0x561ceefa78dd in _eth_get_rss_ex_dst_addr net/eth.c:410:17
      #1 0x561ceefa41fb in eth_parse_ipv6_hdr net/eth.c:532:17
      #2 0x561cef7de639 in net_tx_pkt_parse_headers hw/net/net_tx_pkt.c:228:14
      #3 0x561cef7dbef4 in net_tx_pkt_parse hw/net/net_tx_pkt.c:273:9
      #4 0x561ceec29f22 in e1000e_process_tx_desc hw/net/e1000e_core.c:730:29
      #5 0x561ceec28eac in e1000e_start_xmit hw/net/e1000e_core.c:927:9
      #6 0x561ceec1baab in e1000e_set_tdt hw/net/e1000e_core.c:2444:9
      #7 0x561ceebf300e in e1000e_core_write hw/net/e1000e_core.c:3256:9
      #8 0x561cef3cd4cd in e1000e_mmio_write hw/net/e1000e.c:110:5

Address 0x7ffdef904902 is located in stack of thread T0 at offset 34 in frame

      #0 0x561ceefa320f in eth_parse_ipv6_hdr net/eth.c:486

    This frame has 1 object(s):
      [32, 34) 'ext_hdr' (line 487) <== Memory access at offset 34 overflows this variable
  HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
        (longjmp and C++ exceptions *are* supported)
  SUMMARY: AddressSanitizer: stack-buffer-overflow net/eth.c:410:17 in _eth_get_rss_ex_dst_addr
  Shadow bytes around the buggy address:
    0x10003df188d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x10003df188e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x10003df188f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x10003df18900: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x10003df18910: 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1
  =>0x10003df18920:[02]f3 f3 f3 00 00 00 00 00 00 00 00 00 00 00 00
    0x10003df18930: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x10003df18940: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x10003df18950: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x10003df18960: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    0x10003df18970: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Stack left redzone: f1
Stack right redzone: f3
==2859770==ABORTING

Add the corresponding qtest case with the fuzzer reproducer.

FWIW GCC 11 similarly reported:

net/eth.c: In function 'eth_parse_ipv6_hdr':
net/eth.c:410:15: error: array subscript 'struct ip6_ext_hdr_routing[0]' is partly outside array bounds of 'struct ip6_ext_hdr[1]' [-Werror=array-bounds]

    410 |     if ((rthdr->rtype == 2) && (rthdr->segleft == 1)) {
        |          ~~~~~^~~~~~~
  net/eth.c:485:24: note: while referencing 'ext_hdr'
    485 |     struct ip6_ext_hdr ext_hdr;
        |                        ^~~~~~~
  net/eth.c:410:38: error: array subscript 'struct ip6_ext_hdr_routing[0]' is partly outside array bounds of 'struct ip6_ext_hdr[1]' [-Werror=array-bounds]
    410 |     if ((rthdr->rtype == 2) && (rthdr->segleft == 1)) {
        |                                 ~~~~~^~~~~~~~~
  net/eth.c:485:24: note: while referencing 'ext_hdr'
    485 |     struct ip6_ext_hdr ext_hdr;
        |                        ^~~~~~~
Fixes eb700029c78 ("net_pkt: Extend packet abstraction as required by e1000e functionality")
---
 net/eth.c                      | 10 +++++--
 tests/qtest/fuzz-e1000e-test.c | 53 ++++++++++++++++++++++++++++++++++
 MAINTAINERS                    |  1 +
 tests/qtest/meson.build        |  1 +
 4 files changed, 62 insertions(+), 3 deletions(-)
 create mode 100644 tests/qtest/fuzz-e1000e-test.c
net/eth.c +7 -3
index 28cdc843a69..b150d73c13a 100644
@@ -405,15 +405,19 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,
                         const struct ip6_ext_hdr *ext_hdr,
                         struct in6_address *dst_addr)
 {
-    struct ip6_ext_hdr_routing *rthdr = (struct ip6_ext_hdr_routing *) ext_hdr;
+    struct ip6_ext_hdr_routing rt_hdr;
     size_t input_size = iov_size(pkt, pkt_frags);
     size_t bytes_read;
 
-    if (input_size < ext_hdr_offset + sizeof(*rthdr) + sizeof(*dst_addr)) {
+    if (input_size < ext_hdr_offset + sizeof(rt_hdr) + sizeof(*dst_addr)) {
         return false;
     }
 
-    if ((rthdr->rtype == 2) && (rthdr->segleft == 1)) {
+    bytes_read = iov_to_buf(pkt, pkt_frags, ext_hdr_offset,
+                            &rt_hdr, sizeof(rt_hdr));
+    assert(bytes_read == sizeof(rt_hdr));
+
+    if ((rt_hdr.rtype == 2) && (rt_hdr.segleft == 1)) {
         bytes_read = iov_to_buf(pkt, pkt_frags,
                                 ext_hdr_offset + sizeof(*ext_hdr),
                                 dst_addr, sizeof(*dst_addr));
tests/qtest/fuzz-e1000e-test.c new file +53 -0

53 lines not on this page · raw message

MAINTAINERS +1 -0
index 738786146d6..cc5f3aa6b60 100644
@@ -2003,6 +2003,7 @@ e1000e
 M: Dmitry Fleytman <[email protected]>
 S: Maintained
 F: hw/net/e1000e*
+F: tests/qtest/fuzz-e1000e-test.c
 
 eepro100
 M: Stefan Weil <[email protected]>
tests/qtest/meson.build +1 -0
index 58efc46144e..7997d895449 100644
@@ -60,6 +60,7 @@
   (config_all_devices.has_key('CONFIG_TPM_TIS_ISA') ? ['tpm-tis-test'] : []) +              \
   (config_all_devices.has_key('CONFIG_TPM_TIS_ISA') ? ['tpm-tis-swtpm-test'] : []) +        \
   (config_all_devices.has_key('CONFIG_RTL8139_PCI') ? ['rtl8139-test'] : []) +              \
+  (config_all_devices.has_key('CONFIG_E1000E_PCI_EXPRESS') ? ['fuzz-e1000e-test'] : []) +   \
   qtests_pci +                                                                              \
   ['fdc-test',
    'ide-test',
Signature · 1 line
2.26.2
Reviewed-by on v5 6/7
#14 lore raw

Re: [PATCH v5 6/7] net/eth: Read ip6_ext_hdr_routing buffer before accessing it

Quoted 92 lines · Philippe Mathieu-Daudé

We can't know the caller read enough data in the memory pointed by ext_hdr to cast it as a ip6_ext_hdr_routing. Declare rt_hdr on the stack and fill it again from the iovec.

Since we already checked there is enough data in the iovec buffer, simply add an assert() call to consume the bytes_read variable.

This fix a 2 bytes buffer overrun in eth_parse_ipv6_hdr() reported by QEMU fuzzer:

 $ cat << EOF | ./qemu-system-i386 -M pc-q35-5.0 \
   -accel qtest -monitor none \
   -serial none -nographic -qtest stdio
 outl 0xcf8 0x80001010
 outl 0xcfc 0xe1020000
 outl 0xcf8 0x80001004
 outw 0xcfc 0x7
 write 0x25 0x1 0x86
 write 0x26 0x1 0xdd
 write 0x4f 0x1 0x2b
 write 0xe1020030 0x4 0x190002e1
 write 0xe102003a 0x2 0x0807
 write 0xe1020048 0x4 0x12077cdd
 write 0xe1020400 0x4 0xba077cdd
 write 0xe1020420 0x4 0x190002e1
 write 0xe1020428 0x4 0x3509d807
 write 0xe1020438 0x1 0xe2
 EOF
 =================================================================
 ==2859770==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffdef904902 at pc 0x561ceefa78de bp 0x7ffdef904820 sp 0x7ffdef904818
 READ of size 1 at 0x7ffdef904902 thread T0
     #0 0x561ceefa78dd in _eth_get_rss_ex_dst_addr net/eth.c:410:17
     #1 0x561ceefa41fb in eth_parse_ipv6_hdr net/eth.c:532:17
     #2 0x561cef7de639 in net_tx_pkt_parse_headers hw/net/net_tx_pkt.c:228:14
     #3 0x561cef7dbef4 in net_tx_pkt_parse hw/net/net_tx_pkt.c:273:9
     #4 0x561ceec29f22 in e1000e_process_tx_desc hw/net/e1000e_core.c:730:29
     #5 0x561ceec28eac in e1000e_start_xmit hw/net/e1000e_core.c:927:9
     #6 0x561ceec1baab in e1000e_set_tdt hw/net/e1000e_core.c:2444:9
     #7 0x561ceebf300e in e1000e_core_write hw/net/e1000e_core.c:3256:9
     #8 0x561cef3cd4cd in e1000e_mmio_write hw/net/e1000e.c:110:5

Address 0x7ffdef904902 is located in stack of thread T0 at offset 34 in frame

     #0 0x561ceefa320f in eth_parse_ipv6_hdr net/eth.c:486

This frame has 1 object(s):

     [32, 34) 'ext_hdr' (line 487) <== Memory access at offset 34 overflows this variable
 HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
       (longjmp and C++ exceptions *are* supported)
 SUMMARY: AddressSanitizer: stack-buffer-overflow net/eth.c:410:17 in _eth_get_rss_ex_dst_addr
 Shadow bytes around the buggy address:
   0x10003df188d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
   0x10003df188e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
   0x10003df188f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
   0x10003df18900: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
   0x10003df18910: 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1

=>0x10003df18920:[02]f3 f3 f3 00 00 00 00 00 00 00 00 00 00 00 00
0x10003df18930: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x10003df18940: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x10003df18950: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x10003df18960: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x10003df18970: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Stack left redzone: f1
Stack right redzone: f3
==2859770==ABORTING

Add the corresponding qtest case with the fuzzer reproducer.

FWIW GCC 11 similarly reported:

net/eth.c: In function 'eth_parse_ipv6_hdr':
net/eth.c:410:15: error: array subscript 'struct ip6_ext_hdr_routing[0]' is partly outside array bounds of 'struct ip6_ext_hdr[1]' [-Werror=array-bounds]

   410 |     if ((rthdr->rtype == 2) && (rthdr->segleft == 1)) {
       |          ~~~~~^~~~~~~
 net/eth.c:485:24: note: while referencing 'ext_hdr'
   485 |     struct ip6_ext_hdr ext_hdr;
       |                        ^~~~~~~
 net/eth.c:410:38: error: array subscript 'struct ip6_ext_hdr_routing[0]' is partly outside array bounds of 'struct ip6_ext_hdr[1]' [-Werror=array-bounds]
   410 |     if ((rthdr->rtype == 2) && (rthdr->segleft == 1)) {
       |                                 ~~~~~^~~~~~~~~

net/eth.c:485:24: note: while referencing 'ext_hdr'
485 | struct ip6_ext_hdr ext_hdr;
| ^~~~~~~

Fixes eb700029c78 ("net_pkt: Extend packet abstraction as required by e1000e functionality")
Quoted 7 lines

---

net/eth.c                      | 10 +++++--
tests/qtest/fuzz-e1000e-test.c | 53 ++++++++++++++++++++++++++++++++++
MAINTAINERS                    |  1 +
tests/qtest/meson.build        |  1 +
4 files changed, 62 insertions(+), 3 deletions(-)

create mode 100644 tests/qtest/fuzz-e1000e-test.c

#15 lore raw

PATCH v5 7/7 net/eth: Add an assert() and invert if() statement to simplify code

In reply to Philippe Mathieu-Daudé

To simplify the function body, invert the if() statement, returning earlier.
Since we already checked there is enough data in the iovec buffer, simply add an assert() call to consume the bytes_read variable.

---
 net/eth.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
net/eth.c +6 -8
index b150d73c13a..c0a5ca12be2 100644
@@ -416,16 +416,14 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,
     bytes_read = iov_to_buf(pkt, pkt_frags, ext_hdr_offset,
                             &rt_hdr, sizeof(rt_hdr));
     assert(bytes_read == sizeof(rt_hdr));
-
-    if ((rt_hdr.rtype == 2) && (rt_hdr.segleft == 1)) {
-        bytes_read = iov_to_buf(pkt, pkt_frags,
-                                ext_hdr_offset + sizeof(*ext_hdr),
-                                dst_addr, sizeof(*dst_addr));
-
-        return bytes_read == sizeof(*dst_addr);
+    if ((rt_hdr.rtype != 2) || (rt_hdr.segleft != 1)) {
+        return false;
     }
+    bytes_read = iov_to_buf(pkt, pkt_frags, ext_hdr_offset + sizeof(rt_hdr),
+                            dst_addr, sizeof(*dst_addr));
+    assert(bytes_read == sizeof(*dst_addr));
 
-    return false;
+    return true;
 }
 
 static bool
Signature · 1 line
2.26.2
Reviewed-by on v5 7/7
#16 lore raw

Re: [PATCH v5 7/7] net/eth: Add an assert() and invert if() statement to simplify code

Quoted 19 lines · Philippe Mathieu-Daudé

To simplify the function body, invert the if() statement, returning earlier.
Since we already checked there is enough data in the iovec buffer, simply add an assert() call to consume the bytes_read variable.

---
net/eth.c | 14 ++++++--------

1 file changed, 6 insertions(+), 8 deletions(-)
net/eth.c +0 -1
index b150d73c13a..c0a5ca12be2 100644
@@ -416,16 +416,14 @@ _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags,
    bytes_read = iov_to_buf(pkt, pkt_frags, ext_hdr_offset,
                            &rt_hdr, sizeof(rt_hdr));
    assert(bytes_read == sizeof(rt_hdr));
-

net/eth.c _eth_get_rss_ex_dst_addr(const struct iovec *pkt, int pkt_frags, · line 422

Quoted 10 lines
net/eth.c +3 -6
-    if ((rt_hdr.rtype == 2) && (rt_hdr.segleft == 1)) {
-        bytes_read = iov_to_buf(pkt, pkt_frags,
-                                ext_hdr_offset + sizeof(*ext_hdr),
-                                dst_addr, sizeof(*dst_addr));
-
-        return bytes_read == sizeof(*dst_addr);
+    if ((rt_hdr.rtype != 2) || (rt_hdr.segleft != 1)) {
+        return false;
    }
+    bytes_read = iov_to_buf(pkt, pkt_frags, ext_hdr_offset + sizeof(rt_hdr),

If we add the offset fix in patch 5 this patch is fine, otherwise we should mention the fix here in the commit message (and queue this for stable?).

With that fixed:

Thanks,
Stefano

Quoted 11 lines
net/eth.c +3 -1
+                            dst_addr, sizeof(*dst_addr));
+    assert(bytes_read == sizeof(*dst_addr));
 
-    return false;
+    return true;
 }
 

static bool

Signature · 1 line
2.26.2