Commit 4828fff27b for qemu.org

commit 4828fff27b5b895791f9e258a92f31701fbc9a44
Author: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>
Date:   Mon Nov 9 14:23:51 2026 +0900

    net/eth.c: In eth_parse_ipv6_hdr(), fix check of input size

    We already have some L4/L5 header bound checks. Add the missing
    coverage for IPv6.

    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 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: qemu-stable@nongnu.org
    Reported-by: Lazymio <mio@lazym.io>
    Signed-off-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
    Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
    Message-id: 20261109-eth-v2-6-334cc08e2f2e@rsg.ci.i.u-tokyo.ac.jp
    [PMM: fixed typo in comment]
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

diff --git a/net/eth.c b/net/eth.c
index 55d85cecd1..484f61ef31 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"
@@ -473,7 +474,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;
@@ -513,25 +516,73 @@ 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;
+            }
+
+            /*
+             * Per RFC 8200, the Fragment header does not have the Hdr Ext Len.
+             */
+            info->full_hdr_len += 8;
+
             info->fragment = true;
+
+            /*
+             * Detect a subsequent fragment per RFC 8200.
+             *
+             * A subsequent fragment contains no extension headers while the
+             * Next Header value identifies the first header after the
+             * Per-Fragment headers of the original packet.
+             *
+             * The subsequent fragment is detected by checking if the Fragment
+             * Offset is nonzero.
+             */
+            subsequent = extract16(be16_to_cpu(buf), 3, 13);
+            break;
+
+        case IP6_AUTHENTICATION:
+            /*
+             * Per RFC 4302, the Authentication Header has the Payload Len
+             * instead of the Hdr Ext Len. The Payload Len specifies the length
+             * of AH in 4-byte units.
+             */
+            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;
+    }

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