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:
 * assert that the caller provided us a positive buffer length
 * 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

Cc: [email protected]
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 <[email protected]>
---
 net/checksum.c | 32 ++++++++++++++++++++++++--------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/net/checksum.c b/net/checksum.c
index 537457d89d0..997a0dd2350 100644
--- a/net/checksum.c
+++ b/net/checksum.c
@@ -59,7 +59,7 @@ uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
 
 void net_checksum_calculate(void *data, int length, int csum_flag)
 {
-    int mac_hdr_len, ip_len;
+    int mac_hdr_len, ip_len, ip_hdr_len;
     struct ip_header *ip;
     uint16_t csum;
 
@@ -69,6 +69,8 @@ void net_checksum_calculate(void *data, int length, int 
csum_flag)
      * struct members (just in case).
      */
 
+    assert(length >= 0);
+
     /* Ensure we have at least an Eth header */
     if (length < sizeof(struct eth_header)) {
         return;
@@ -94,23 +96,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 +132,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:
-- 
2.43.0


Reply via email to