From nobody Mon Nov 23 04:34:33 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1794662692; cv=none; d=zohomail.com; s=zohoarc; b=C4cGlbkjwobLuQGzZVdrwZSLYe1W6ZcQGmeMcQopy1UgpVQb4jALWAAD0q95wgS77mwtPskV4UqjtYNxRlQH3qWLdRKuE77pEKzv45ySQHbK93iMCFELfQWTBPrE7u847YTT6dvqcJNwxCO7y9MWvAC8MTuDrKaKJstHLraQ2hk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794662692; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=AYigO0Jz9iiefYHwMEhCa+1yikT++DWVv7odFVY6EkM=; b=Mpj5eGUYZHKzF0NCW95hoM1cjAAZQL827YHRek+YCjiynb9vv4q5ZR9YLehS/cQW2A9FfomeLsXe99dTZ6Wx+cFEQAJiIjnBgAx1AAZFJO4Wx+ert+TeuKCaoUReCcoZmriAu/+U83IRyE3BgmZUIrPx0hqsgUTaxwzSpWdfAmE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1794662692937564.2519692535046; Sat, 14 Nov 2026 05:24:52 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xSDjn-0007lP-0y; Sat, 14 Nov 2026 08:24:11 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xSDja-0007hq-BX for qemu-devel@nongnu.org; Sat, 14 Nov 2026 08:23:59 -0500 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xSDjY-00081H-5F for qemu-devel@nongnu.org; Sat, 14 Nov 2026 08:23:58 -0500 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-4a207b02a2cso10400175e9.1 for ; Sat, 14 Nov 2026 05:23:55 -0800 (PST) Received: from lanath.. (wildly.archaic.org.uk. [81.2.115.145]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb55a0ffcdsm130357045e9.0.2026.11.14.05.23.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Nov 2026 05:23:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1794662634; x=1795267434; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=AYigO0Jz9iiefYHwMEhCa+1yikT++DWVv7odFVY6EkM=; b=wMULg9u5RgHJORL2hjCPq7NiFRt5r1ngoQHG6dPz7xkkjJTqaqrm7NVMjretN3aGte YPWbUnfGt4YLwYJn4zjUntLYxaVLl52Jx2CDYHZmUqBoYDl2FwkOW44QzEwxXQeqf5zn DzacNLTM1DK4U9bgtJK9yzL/TcuCukFOkd51J0pkzuSUqCc8wjeVrMWyMQDBdJk1ODtc azP/TjfbM36spUSWBXyeLhZYqO/6NXCB4ArpiE51NSxL9n2lVB9cxX8h8m4aNM+y/2a8 vmu7+JMEwo1kkF2lIIVQp+Bt91vAxkznF9NI2ZLSFTjvoRXSTVUkQFXaFVUjs2aCoFFh mBgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794662634; x=1795267434; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=AYigO0Jz9iiefYHwMEhCa+1yikT++DWVv7odFVY6EkM=; b=g+0Ko25xbF4Q2i1mBmbaHz78rqEJTHNRgKvVxcFJQ/hKngbfrzFrmNc6gMBtfjA9KH YNFtOcQqZHqiaoLJugOOk26wEMplDi5+0AVlVQAC3WhNQI/sOlAWOrgkBB32LJWuU0ZZ O8l3bnhU7cc0vmqXtbbt/1TYqo9HPdR5lPNXwVWGx2BXeBfweG4QsgjPG9Ko+h0ObW/B rZaGZwc5sm4VXHuMlXGV/0vtJZJ5nZjvRn0o7ZJStTxOEtncg5/NycevizI0gZwLk2Ut BpcRRl7zDzVP4QuPLeypJACHjYJwC2IAWbWQcg4fA72G3VdNzdJj7/WVhi93xhVgMNy3 ZLyA== X-Gm-Message-State: APuU4Z+nYsEDhVyKNd4dcSRDN3ZZWRHpM3Ns9D3ByjSzj7tJCsdF84m6 XSmiiZmfQgTHn/j4gG65O5S/M18Lovdw+q+Hxpco3KBCUpTgo/+ZLQAAsJ+C22eGUm4Ps0w+z2/ qSX9gWow= X-Gm-Gg: AYBFou3fj6kb+AbXnc2nIDRJ8dVHX0u41aEYbWQwDDtX2PeuWjSA3wqjdzXxcfkXMCO j0un3E064maM/pwCw6vn+GEHjamhSUabiTDPuJnwYKOcfRJ49xofYtNPW3+xusLSILgAM4xEZ7b iuHsdiQQIpJlzC5qSd3i11FEYKSp60iMzEpJxdTiKFgGBBwYj8GtzFVedsO0Dru0bcKwApyIJuy DRtWbr2VelgSNqrwZg49eQswFlYPEe7On8pXC3gZczMEQ2vNyAeujvbrGrxSkpzI0sGxZ/wRi3k bEh//E1pQiMhghgmRA2KvbaDG+Q9hvcckwWGmBk9P26D1Cc3epStr73jsmpDX1io6Yd7nXBAL+K xjlBZ+xXe/lSaY0VM1lG7exiosgpltqFRp2VGfAnsYnMdFYNj6ncNvM3cvDJ7cXBbEeqxvkuzaw ufJ18EEHzpV53EQwMBYt7o/Zo7UzpqPLqxcJrPky0n7hHd2kGrCEqZFmuwqXkrxpKPBZ8ruzcIo F7DViyyCqQqZRJjam6N+8ixqsbDbP9fjViCk3J8DyrMJzjzrn6x5Z991nv0G3ZzRGqMd6dL+wcV 3PYFR4BkAA/nLm0238oj7Djite82i4T56yyMh+7i1POLYXLNCX7ld40/ogUW18l/ewQnTdM9CBN JiJ2dV8bjqEC/w6Ykyrb49yOwvKBEBsceGA== X-Received: by 2002:a05:600c:4585:b0:4a1:cf60:dcc3 with SMTP id 5b1f17b1804b1-4bb55a8ccaemr116807415e9.29.1794662634197; Sat, 14 Nov 2026 05:23:54 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Cc: Richard Henderson , Jason Wang , Akihiko Odaki Subject: [PATCH v2 for-11.2 1/2] net/checksum.c: Correct handling of IP header length Date: Sat, 14 Nov 2026 13:23:49 +0000 Message-ID: <20261114132350.1456994-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261114132350.1456994-1-peter.maydell@linaro.org> References: <20261114132350.1456994-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::330; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x330.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1794662696336154100 Content-Type: text/plain; charset="utf-8" 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 Signed-off-by: Peter Maydell Reviewed-by: Akihiko Odaki Reviewed-by: Richard Henderson --- v1->v2: * use 'size_t' for length argument, and drop assert(length >=3D 0) * use 'unsigned int' for mac_hdr_len, ip_len, ip_hdr_len --- include/net/checksum.h | 2 +- net/checksum.c | 32 +++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/include/net/checksum.h b/include/net/checksum.h index 188e4cca0b7..6c565f2e0ab 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); =20 static inline uint32_t net_checksum_add(int len, uint8_t *buf) diff --git a/net/checksum.c b/net/checksum.c index 537457d89d0..9e4ba1bc910 100644 --- a/net/checksum.c +++ b/net/checksum.c @@ -57,9 +57,9 @@ uint16_t net_checksum_tcpudp(uint16_t length, uint16_t pr= oto, return net_checksum_finish(sum); } =20 -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; =20 @@ -94,23 +94,33 @@ void net_checksum_calculate(void *data, int length, int= csum_flag) break; } =20 - length -=3D 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; } =20 + length -=3D mac_hdr_len; + ip =3D (struct ip_header *)((uint8_t *)data + mac_hdr_len); =20 if (IP_HEADER_VERSION(ip) !=3D IP_HEADER_VERSION_4) { return; /* not IPv4 */ } =20 + ip_hdr_len =3D 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 =3D net_raw_checksum((uint8_t *)ip, IP_HDR_GET_LEN(ip)); + csum =3D net_raw_checksum((uint8_t *)ip, ip_hdr_len); stw_be_p(&ip->ip_sum, csum); } =20 @@ -120,12 +130,16 @@ void net_checksum_calculate(void *data, int length, i= nt csum_flag) =20 ip_len =3D lduw_be_p(&ip->ip_len); =20 - /* 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; } =20 - ip_len -=3D IP_HDR_GET_LEN(ip); + ip_len -=3D ip_hdr_len; =20 switch (ip->ip_p) { case IP_PROTO_TCP: --=20 2.43.0 From nobody Mon Nov 23 04:34:33 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1794662687; cv=none; d=zohomail.com; s=zohoarc; b=HdvQDF85CyqFW3jyiboTwZvDzUctL/K1UnlQ6bfat8Fnn/EpArSIzCyzteKBqT4dUVARZHBr/fOy8WgJ99r914pM4dfL9BIXy93gFHh86mFATcLq/2lcWP/wKVpB1D8n0/HR3Vd9he8tEzWrJEfZ3EZKQxfn9ZMgqMy9e3O7Xyo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794662687; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=+0maCb+diTV7wSImSXlX11VAHe0aCu50WZLJ8OVLd7w=; b=XsuXG2wvDNob88aAAwPK1RTP9m/HHcvSLUv27o3M6pCn+DTbYHqyDCE+7HqVzn3ZSQar9KL4Dq8VS/cFZLVGMD6T8w1fJUa4zjH/28BuaQcGnkvLd4scXsjz52UMFEZsOkNKfzn0v7m0wh4noyPPLLEep2OHFo1NKcCCTsy00GY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 179466268722240.392424773449534; Sat, 14 Nov 2026 05:24:47 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xSDjk-0007kH-6l; Sat, 14 Nov 2026 08:24:08 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xSDjb-0007ht-F9 for qemu-devel@nongnu.org; Sat, 14 Nov 2026 08:23:59 -0500 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xSDjZ-000848-8G for qemu-devel@nongnu.org; Sat, 14 Nov 2026 08:23:59 -0500 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-4b7252a32c8so26048035e9.0 for ; Sat, 14 Nov 2026 05:23:56 -0800 (PST) Received: from lanath.. (wildly.archaic.org.uk. [81.2.115.145]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb55a0ffcdsm130357045e9.0.2026.11.14.05.23.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Nov 2026 05:23:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1794662636; x=1795267436; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+0maCb+diTV7wSImSXlX11VAHe0aCu50WZLJ8OVLd7w=; b=LKehK5DMeZMJchiEDzwNFGsTzJQloIbmXEQ5KuIKX1vMQRE2fsGm2RkAwZINe/fchQ bSR2OdfBhZY1Mp3KNuhzHg2R6petXXru3JLVqKtYEUEVbhRHbP7gsAt5andAYC4xOR/S 8VYp+vS/aCb6Xi2oq/tJ9GeEJh46ekiK87fcR5sIk8szprrfC+r096Kzju71cb1wbnog sT+IhLV79tSpfUuuBIaYbTA7STTS4LG/vxAR0S7IG5F0OMAOE9+oDK/tgv7n/9xtUXT/ u4D3fDich1xRfjqKWaeK+LXxFWwKgiI4joKjTWz7i1a1v4v27jTX6TOcKLYyB66HIqcz ir5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794662636; x=1795267436; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=+0maCb+diTV7wSImSXlX11VAHe0aCu50WZLJ8OVLd7w=; b=gAMCnrXpElueqo/4JO/JiijACGj05tRnkPZr+3Fy3gm83RyLV240FWCkz0U87eyMQg vFwoRCZFeUluvKLfQsUVv9Bfc+6ESp5Us+Phzk9RiPukCTtL9XHAYO6Kdi+YXaPNIrBZ NLwoO5eBcs+3OF6U3q3WKhgbmnn/9sYPRUWrgm+xfih9yGiEm10cBvMzAqcseDWxWl1u GiyHHkvaVnFxpBjRVZZKRAJqCSjLY2RGSYhAwsaAhdGjACkEuZhoohoTyPfrOtyVQlJC UNwJDz8tHyoIMcwyRyN+xHEqTLauTATWlRYNtJndeZSuvG26GqqFeFVH+Tm9SvDsYp/P Yt+Q== X-Gm-Message-State: APuU4Z9lKOAM81MtkJMB8fCe0/rWYbXcBBhEmdhguJhkb2gtuiUbAbv/ qI4F4CWdsU0payOAaBR0hc6ybqMSzokXtO+RdqGfS2kdgGqMdiHLOQG0W9ClyWLoIp4qjEaX+Dv TQntxtsI= X-Gm-Gg: AYBFou3h5a4+60I5MPds7WZ438yZy6ZmVJWSOaK9rJcg8YWC20phqEv5smYi1Q7GCqt l/K+zAoO2wnx1jqhyYU99MFGmHqB16HJtnATW3HZv5zHOXw13sWGQBo0hDAhysgKs4kTsrCz/+j CtqDha4C13rDvMqNcYmdbZh8nVXiV4dzmyKmYzDVr0kBDcEZTBsoNIBSY2DT8/EsGkCRJC1WRJ8 B34xedXQNokhbcTaU0W9aKP033nvGvQzAFTiu5jsLgPmyxBOqB3q73RiObbRtGznJ1Y15tDMM+z dIuuS7DL63FQsjxJvu03rLo4hHGkFtgslgKiNbsVv9EZ3/DnZ+tKrbw+6sAOWINZCL/W+4GArri ujWYUN6uoAXasXc8Rgw4YjPhhwTms81QVxTtjmWpFm9IvACJWcBTDEJgjWRqh95azkujWJCMlsz NKYdI3U+U9NESF2OKpJOSxnyUVylCVCdRfNAvhFENY/1YMAXjRp69/1AVN9PAXnHjJlm76bvBm1 pWRppxCk4x4nry7Vr7obwYk/ORdsTouhOCJGwUzSa+7V66l9ZGOrLhnh6OOJfpcbPmXMdO/Bd9l l1D5J7JIdhOjqg0wMaXNHIkJfAHGmnRhuCKZ3M9dAqPinNtO59buaUTTMS3mnuvFcq/fLBeJsdX VjLyBdFYntPGlbL7+gmJ6l3emjyd69nnbNQ== X-Received: by 2002:a05:600c:3551:b0:4bb:534b:ada4 with SMTP id 5b1f17b1804b1-4bb55abdc18mr119972705e9.33.1794662635622; Sat, 14 Nov 2026 05:23:55 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Cc: Richard Henderson , Jason Wang , Akihiko Odaki Subject: [PATCH v2 for-11.2 2/2] net/checksum: Correct handling of checksums when IP options present Date: Sat, 14 Nov 2026 13:23:50 +0000 Message-ID: <20261114132350.1456994-3-peter.maydell@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261114132350.1456994-1-peter.maydell@linaro.org> References: <20261114132350.1456994-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::334; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x334.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1794662690572154100 Content-Type: text/plain; charset="utf-8" In net_checksum_calculate() we calculate the position of the TCP or UDP header in the buffer with: tcp_header *tcp =3D (tcp_header *)(ip + 1); where ip is a 'struct ip_header' (and similarly for udp). This only gives the correct location if the IP packet has no options and its header is the minimum length. Otherwise we will corrupt some part of the packet in writing the checksum word. Fortunately packets with IP options are rare (and often get blocked by routers or firewalls), so this probably didn't often cause problems, and the incorrect offset we write to is always inside the packet, not out-of-bounds. Use the IP header length value from the packet to locate the TCP or UDP header, instead of the fixed minimum size. While we're adjusting this code, make sure that we don't assign to the tcp_header or udp_header pointer until after we've made the length check that the buffer is big enough (the previous ordering of operations was harmless as we never used the resulting pointer, but not ideal). This was mentioned in passing in issue #4791 (which is otherwise a duplicate of #4720). Cc: qemu-stable@nongnu.org Fixes: 50dbce6538d1 ("net: improve UDP/TCP checksum computation.") Reported-by: Alex Meiburg Signed-off-by: Peter Maydell Reviewed-by: Akihiko Odaki Reviewed-by: Richard Henderson --- v1->v2: use 'void *' for ip_payload, and drop casts --- net/checksum.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/net/checksum.c b/net/checksum.c index 9e4ba1bc910..d6925e9a496 100644 --- a/net/checksum.c +++ b/net/checksum.c @@ -62,6 +62,7 @@ void net_checksum_calculate(void *data, size_t length, in= t csum_flag) unsigned int mac_hdr_len, ip_len, ip_hdr_len; struct ip_header *ip; uint16_t csum; + void *ip_payload; =20 /* * Note: We cannot assume "data" is aligned, so the all code uses @@ -140,20 +141,23 @@ void net_checksum_calculate(void *data, size_t length= , int csum_flag) } =20 ip_len -=3D ip_hdr_len; + ip_payload =3D (uint8_t *)ip + ip_hdr_len; =20 switch (ip->ip_p) { case IP_PROTO_TCP: { + tcp_header *tcp; + if (!(csum_flag & CSUM_TCP)) { return; } =20 - tcp_header *tcp =3D (tcp_header *)(ip + 1); - if (ip_len < sizeof(tcp_header)) { return; } =20 + tcp =3D ip_payload; + /* Set csum to 0 */ stw_he_p(&tcp->th_sum, 0); =20 @@ -168,16 +172,18 @@ void net_checksum_calculate(void *data, size_t length= , int csum_flag) } case IP_PROTO_UDP: { + udp_header *udp; + if (!(csum_flag & CSUM_UDP)) { return; } =20 - udp_header *udp =3D (udp_header *)(ip + 1); - if (ip_len < sizeof(udp_header)) { return; } =20 + udp =3D ip_payload; + /* Set csum to 0 */ stw_he_p(&udp->uh_sum, 0); =20 --=20 2.43.0