Commit 434b0d52dc for qemu.org

commit 434b0d52dc8c8515f3129aa1bf1b5b3d8cb8184c
Author: Peter Maydell <peter.maydell@linaro.org>
Date:   Tue Nov 24 10:23:08 2026 +0000

    net/checksum.c: Correct handling of IP header length

    In net_checksum_calculate() we are careless about handling of the IP
    header length, in a way which can allow it to become negative for a
    suitably crafted packet.  This bypasses the attempt to check that
    it's big enough, because in comparisons like
      if (ip_len < sizeof(tcp_header))
    ip_len is converted to size_t and we do this as an unsigned
    comparison, so negative values of ip_len pass the check.

    This results in net_checksum_tcpudp() attempting to checksum nearly
    64K of memory (because it takes the length as a uint16_t), which will
    typically be a read off the end of a buffer resulting in a segfault.
    If the attacker is able to set up a situation where the read doesn't
    fault, it's possible for some callers that the 2-byte store of the
    updated checksum is an out-of-bounds write.

    This code is used by at least xen-nic, virtio-net, and the COLOj
    filter-rewriter, all of which are in QEMU's security "virtualization
    use case", though the write is probably only exploitable for the
    filter-rewriter; otherwise it's just a theoretical "information leak
    to guest" of a 2-byte checksum of 64K of host RAM.

    Improve the checking of lengths in this function:
     * make the input length size_t as it is a buffer length
     * use unsigned int for internal always-positive lengths, so
       comparisons are always unsigned-vs-unsigned
     * check length before subtracting various header sizes,
       rather than afterwards
     * check that the packet's IHL field reports a valid header
       length that's not larger than the packet itself
     * check that the packet's total length field doesn't claim
       to be smaller than the header

    All the callsites are already passing an unsigned value to this
    function, with the exception of those in ftgmac100.c and imx_fec.c.
    Those two pass an "int" value which can't ever be negative.

    Cc: qemu-stable@nongnu.org
    Fixes: 50dbce6538d1 ("net: improve UDP/TCP checksum computation.")
    Fixes: CVE-2026-127233
    Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4720
    Reported-by: Xiang Chen <https://gitlab.com/x14ngch3n>
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
    Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
    Reviewed-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>
    Message-id: 20261114132350.1456994-2-peter.maydell@linaro.org

diff --git a/include/net/checksum.h b/include/net/checksum.h
index 188e4cca0b..6c565f2e0a 100644
--- a/include/net/checksum.h
+++ b/include/net/checksum.h
@@ -30,7 +30,7 @@ uint32_t net_checksum_add_cont(int len, uint8_t *buf, int seq);
 uint16_t net_checksum_finish(uint32_t sum);
 uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
                              uint8_t *addrs, uint8_t *buf);
-void net_checksum_calculate(void *data, int length, int csum_flag);
+void net_checksum_calculate(void *data, size_t length, int csum_flag);

 static inline uint32_t
 net_checksum_add(int len, uint8_t *buf)
diff --git a/net/checksum.c b/net/checksum.c
index 537457d89d..9e4ba1bc91 100644
--- a/net/checksum.c
+++ b/net/checksum.c
@@ -57,9 +57,9 @@ uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
     return net_checksum_finish(sum);
 }

-void net_checksum_calculate(void *data, int length, int csum_flag)
+void net_checksum_calculate(void *data, size_t length, int csum_flag)
 {
-    int mac_hdr_len, ip_len;
+    unsigned int mac_hdr_len, ip_len, ip_hdr_len;
     struct ip_header *ip;
     uint16_t csum;

@@ -94,23 +94,33 @@ void net_checksum_calculate(void *data, int length, int csum_flag)
         break;
     }

-    length -= mac_hdr_len;
-
     /* Now check we have an IP header (with an optional VLAN header) */
-    if (length < sizeof(struct ip_header)) {
+    if (length < mac_hdr_len + sizeof(struct ip_header)) {
         return;
     }

+    length -= mac_hdr_len;
+
     ip = (struct ip_header *)((uint8_t *)data + mac_hdr_len);

     if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
         return; /* not IPv4 */
     }

+    ip_hdr_len = IP_HDR_GET_LEN(ip);
+
+    if (ip_hdr_len < sizeof(struct ip_header) || ip_hdr_len > length) {
+        /*
+         * Ignore packet with an invalidly short IHL, or which claims the
+         * IP header is larger than the buffer
+         */
+        return;
+    }
+
     /* Calculate IP checksum */
     if (csum_flag & CSUM_IP) {
         stw_he_p(&ip->ip_sum, 0);
-        csum = net_raw_checksum((uint8_t *)ip, IP_HDR_GET_LEN(ip));
+        csum = net_raw_checksum((uint8_t *)ip, ip_hdr_len);
         stw_be_p(&ip->ip_sum, csum);
     }

@@ -120,12 +130,16 @@ void net_checksum_calculate(void *data, int length, int csum_flag)

     ip_len = lduw_be_p(&ip->ip_len);

-    /* Last, check that we have enough data for the all IP frame */
-    if (length < ip_len) {
+    /*
+     * Last, check that the packet's total length field doesn't
+     * imply a negative amount of data given the header length,
+     * and that it's not larger than the data buffer we have.
+     */
+    if (ip_len < ip_hdr_len || length < ip_len) {
         return;
     }

-    ip_len -= IP_HDR_GET_LEN(ip);
+    ip_len -= ip_hdr_len;

     switch (ip->ip_p) {
     case IP_PROTO_TCP: