On 2026/11/04 0:59, Peter Maydell wrote:
On Sun, 20 Sept 2026 at 11:52, Akihiko Odaki
<[email protected]> wrote:

We already have some L4/L5 header bound checks but they were not
comprehensive. Add the missing coverage.

The IPv6 header parser incorrectly derives L4 offsets greater than
actual values. Adding a bound check without fixing it would reject
valid packets while fixing it without adding a bound check can result
in an out-of-bounds access, so fix it along with the bound check
addition.

One fixed problem of the IPv6 header parser is that extension header
size is not properly computed for the Fragment Header and the IP
Authentication Header.

RFC 8200 "Internet Protocol, Version 6 (IPv6) Specification" section
4.5. "Fragment Header" says the Fragment Header has a fixed size.
The byte represented by ext_hdr.ip6r_len is reserved and "ignored on
reception".

RFC 4302 "IP Authentication Header" section 2.2. "Payload Length" says
it specifies the length of AH in 32-bit words (4-byte units), minus "2".

Another fixed problem is that the parser continues parsing extensions in
noninitial fragments.

RFC 8200 "Internet Protocol, Version 6 (IPv6) Specification" section
4.5 "Fragment Header" says the noninitial fragment packets have the
Fragment Header containing "the Next Header value that identifies the
first header after the Per-Fragment headers of the original packet"
followed by "the fragment itself."

Therefore, the data following the Fragment Header in the noninitial
fragment packet must be treated as a fragment instead of another
extension header, regardless of the Next Header value the Fragment
Header contains.

Fixes: eb700029c783 ("net_pkt: Extend packet abstraction as required by e1000e 
functionality")
Fixes: 75020a702151 ("Common definitions for VMWARE devices")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4155
Cc: [email protected]
Reported-by: Lazymio <[email protected]>
Signed-off-by: Akihiko Odaki <[email protected]>
---
  net/eth.c | 47 ++++++++++++++++++++++++++++++++++++++++-------
  1 file changed, 40 insertions(+), 7 deletions(-)

diff --git a/net/eth.c b/net/eth.c
index 12ec316e240e..96d3f871f4bc 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -16,6 +16,7 @@
   */

  #include "qemu/osdep.h"
+#include "qemu/bitops.h"
  #include "qemu/log.h"
  #include "net/eth.h"
  #include "net/checksum.h"
@@ -156,7 +157,8 @@ void eth_get_protocols(const struct iovec *iov, size_t 
iovcnt, size_t iovoff,

          copied = iov_to_buf(iov, iovcnt, *l3hdr_off, iphdr, sizeof(*iphdr));
          if (copied < sizeof(*iphdr) ||
-            IP_HEADER_VERSION(iphdr) != IP_HEADER_VERSION_4) {
+            IP_HEADER_VERSION(iphdr) != IP_HEADER_VERSION_4 ||
+            *l3hdr_off + IP_HDR_GET_LEN(iphdr) > input_size) {
              return;
          }

The patch I sent
https://patchew.org/QEMU/[email protected]/[email protected]/

does this as

+            IP_HDR_GET_LEN(iphdr) < sizeof(*iphdr) ||
+            input_size - *l3hdr_off < IP_HDR_GET_LEN(iphdr) ||

which (a) has an extra check and (b) does a subtraction which is
guaranteed not to underflow (because we already checked that
input_size >= *l3hdr_off) rather than an addition which isn't
immediately obviously "can't possibly overflow". So I think I prefer that.


@@ -188,7 +190,8 @@ void eth_get_protocols(const struct iovec *iov, size_t 
iovcnt, size_t iovoff,
          if (_eth_copy_chunk(input_size,
                              iov, iovcnt,
                              *l4hdr_off, sizeof(l4hdr_info->hdr.tcp),
-                            &l4hdr_info->hdr.tcp)) {
+                            &l4hdr_info->hdr.tcp) &&
+            *l4hdr_off + TCP_HEADER_DATA_OFFSET(&l4hdr_info->hdr.tcp) <= 
input_size) {

Similarly
  "input_size - *l4hdr_off >= TCP_HEADER_DATA_OFFSET(...)"


              l4hdr_info->proto = ETH_L4_HDR_PROTO_TCP;
              *l5hdr_off = *l4hdr_off +
                  TCP_HEADER_DATA_OFFSET(&l4hdr_info->hdr.tcp);
@@ -456,7 +459,9 @@ bool eth_parse_ipv6_hdr(const struct iovec *pkt, int 
pkt_frags,
      struct ip6_ext_hdr ext_hdr;
      size_t bytes_read;
      uint8_t curr_ext_hdr_type;
+    uint16_t buf;
      size_t input_size = iov_size(pkt, pkt_frags);
+    bool subsequent = false;

      info->rss_ex_dst_valid = false;
      info->rss_ex_src_valid = false;
@@ -496,25 +501,53 @@ bool eth_parse_ipv6_hdr(const struct iovec *pkt, int 
pkt_frags,
              return false;
          }

-        if (curr_ext_hdr_type == IP6_ROUTING) {
+        switch (curr_ext_hdr_type) {
+        case IP6_ROUTING:
              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->full_hdr_len += (ext_hdr.ip6r_len + 1) * IP6_EXT_GRANULARITY;
+            break;
+
+        case IP6_DESTINATON:
              info->rss_ex_src_valid =
                  _eth_get_rss_ex_src_addr(pkt, pkt_frags,
                                           ip6hdr_off + info->full_hdr_len,
                                           &ext_hdr, &info->rss_ex_src);
-        } else if (curr_ext_hdr_type == IP6_FRAGMENT) {
+            info->full_hdr_len += (ext_hdr.ip6r_len + 1) * IP6_EXT_GRANULARITY;
+            break;
+
+        case IP6_FRAGMENT:
+            bytes_read = iov_to_buf(pkt, pkt_frags,
+                                    ip6hdr_off + info->full_hdr_len + 2,
+                                    &buf, sizeof(buf));
+            if (bytes_read < sizeof(buf)) {
+                return false;
+            }
+
+            info->full_hdr_len += 8;
              info->fragment = true;
+            subsequent = extract16(be16_to_cpu(buf), 3, 13);

I don't understand what this is doing. I guess this is the
"don't parse extensions in fragments" change mentioned in the
commit message, but I don't see why this is the way to determine
that. This is testing the "fragment offset" field for being non-zero,
isn't it?

Yes. A nonzero offset identifies a noninitial fragment. Its bytes are fragment data, so we must stop parsing extension headers there. For offset zero, we can continue parsing the headers present in the
first fragment.


+            break;
+
+        case IP6_AUTHENTICATION:

Worth a brief comment, e.g.:
     /* Per RFC4302, ip6r_len for this header is different from the ipv6 norm */

It may be worthwhile to add comments throughout the extension parsing code, including the subsequent fragment detection above.


+            info->full_hdr_len += (ext_hdr.ip6r_len + 2) * 4;
+            break;
+
+        default:
+            info->full_hdr_len += (ext_hdr.ip6r_len + 1) * IP6_EXT_GRANULARITY;
          }

-        info->full_hdr_len += (ext_hdr.ip6r_len + 1) * IP6_EXT_GRANULARITY;
          curr_ext_hdr_type = ext_hdr.ip6r_nxt;
-    } while (eth_is_ip6_extension_header_type(curr_ext_hdr_type));
+    } while (!subsequent &&
+             eth_is_ip6_extension_header_type(curr_ext_hdr_type));
+
+    if (input_size < ip6hdr_off + info->full_hdr_len) {
+        return false;
+    }

I think I prefer the "do this once at the end of the loop" that my
patch does, rather than checking at the start of the loop and then
afterwards.

I can do that; I don't have a strong preference either way.



      info->l4proto = ext_hdr.ip6r_nxt;
      return true;

Otherwise the eth_parse_ipv6_hdr() changes look good to me, but I
think they're complex enough to be worth having in their own
patch separate from the eth_get_protocols() changes.

That makes sense. The changes inside eth_parse_ipv6_hdr() need to be in one patch, but it can be still separate from the eth_get_protocols() changes.

We can use your patch for the IPv4 change, as it also adds overflow safety and the additional sizeof(*iphdr) check.

A separate patch should be written for the TCP bounds check and precede the IPv6 parser fixes. Correcting the AH length can make a TCP header reachable in cases where the old parser's incorrect offset caused the header copy to fail. Without the TCP bounds check, an oversized TCP data offset could then reach igb's header-splitting path as an out-of-range L5 offset. Keeping that ordering avoids an unsafe intermediate state.

I plan to send a patch series featuring a patch for the TCP bounds check and another for the IPv6 parser fixes, while dropping the IPv4 change.

Regards,
Akihiko Odaki

Reply via email to