From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5947322126C for ; Sat, 25 Apr 2026 10:01:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777111296; cv=none; b=VLCe8JPAxE/EpvoCBtDecIloelrk+3OnCW/DeKWwcSooW5sBny7gl7zQJaYIwHDmgOTw31BYUUN/PHOoD5ZncvXnW2gHIc35RwSBhTJQlZoWk9gAzfYwMjNyJWEj6v/KM9bIE7V21r7n0w268VIYXb+qK3QtqF3QV56cMV2xMZs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777111296; c=relaxed/simple; bh=xDvncqueSXlrWLjio4ExuiW5EsBRBh3LV7VDWxaTmFY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FdaduzqJa2gbBRd0vHdfFh0u+ithfDwlnKb5MGiG8XBuwa+iQGJ/o/COt+LReu7CQGXpnR0fErULgnJwxoOUvdVrY3IJVp7Db8W0xUSppQOpPttTuIi1ruB4OpPSTyFg1d2FjtrE6o6lxDHdk+624+Fp+6RBmG8TX9rAEIqAMSo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GY5vhlhP; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GY5vhlhP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5FEA4C2BCB0; Sat, 25 Apr 2026 10:01:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1777111296; bh=xDvncqueSXlrWLjio4ExuiW5EsBRBh3LV7VDWxaTmFY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GY5vhlhP3xZ1I6v7YAep1jJcWGCrI2/I9B1iD22A7pumi9w7NET5KN1FhcIbGm/dH Ws/FxsbOC4P5n5tFtrpbD+ZG6jwY92432woaoKriOh6XI1HwuDnzUDuBqa17NzBjm+ CQ2Me3XJjWCjAA7tQpme9EU63YuybzckuPxbPnuFB88hSl4sTFXNXdJ1oXpVhyE0Bt AXNjFdJNbyo0AmQPo56pYUJZh1IIPB5BHhZT+I89Yf3+ik1uZ/8vstRiTOapo5iPqK +ys756pfqs1CmBBtbN8ik/uRxxADeFxK6bEgmayoBeUqsAaFy/Y89tSEHk3dILyI66 ZLbPFZpx+FnxQ== From: Sasha Levin To: stable@vger.kernel.org Cc: Bingquan Chen , Willem de Bruijn , Jakub Kicinski , Sasha Levin Subject: [PATCH 5.15.y] net/packet: fix TOCTOU race on mmap'd vnet_hdr in tpacket_snd() Date: Sat, 25 Apr 2026 06:01:33 -0400 Message-ID: <20260425100133.3487288-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026042433-handpick-copy-d2e1@gregkh> References: <2026042433-handpick-copy-d2e1@gregkh> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Bingquan Chen [ Upstream commit 2c054e17d9d41f1020376806c7f750834ced4dc5 ] In tpacket_snd(), when PACKET_VNET_HDR is enabled, vnet_hdr points directly into the mmap'd TX ring buffer shared with userspace. The kernel validates the header via __packet_snd_vnet_parse() but then re-reads all fields later in virtio_net_hdr_to_skb(). A concurrent userspace thread can modify the vnet_hdr fields between validation and use, bypassing all safety checks. The non-TPACKET path (packet_snd()) already correctly copies vnet_hdr to a stack-local variable. All other vnet_hdr consumers in the kernel (tun.c, tap.c, virtio_net.c) also use stack copies. The TPACKET TX path is the only caller of virtio_net_hdr_to_skb() that reads directly from user-controlled shared memory. Fix this by copying vnet_hdr from the mmap'd ring buffer to a stack-local variable before validation and use, consistent with the approach used in packet_snd() and all other callers. Fixes: 1d036d25e560 ("packet: tpacket_snd gso and checksum offload") Signed-off-by: Bingquan Chen Reviewed-by: Willem de Bruijn Link: https://patch.msgid.link/20260418112006.78823-1-patzilla007@gmail.com Signed-off-by: Jakub Kicinski [ replaced `vnet_hdr_sz` with `sizeof(vnet_hdr)` and `if (vnet_hdr_sz)` with `if (po->has_vnet_hdr)` ] Signed-off-by: Sasha Levin --- net/packet/af_packet.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index d1ad069271f8b..e0ea02e86b38e 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -2729,7 +2729,8 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) { struct sk_buff *skb = NULL; struct net_device *dev; - struct virtio_net_hdr *vnet_hdr = NULL; + struct virtio_net_hdr vnet_hdr; + bool has_vnet_hdr = false; struct sockcm_cookie sockc; __be16 proto; int err, reserve = 0; @@ -2829,16 +2830,20 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) hlen = LL_RESERVED_SPACE(dev); tlen = dev->needed_tailroom; if (po->has_vnet_hdr) { - vnet_hdr = data; - data += sizeof(*vnet_hdr); - tp_len -= sizeof(*vnet_hdr); - if (tp_len < 0 || - __packet_snd_vnet_parse(vnet_hdr, tp_len)) { + data += sizeof(vnet_hdr); + tp_len -= sizeof(vnet_hdr); + if (tp_len < 0) { + tp_len = -EINVAL; + goto tpacket_error; + } + memcpy(&vnet_hdr, data - sizeof(vnet_hdr), sizeof(vnet_hdr)); + if (__packet_snd_vnet_parse(&vnet_hdr, tp_len)) { tp_len = -EINVAL; goto tpacket_error; } copylen = __virtio16_to_cpu(vio_le(), - vnet_hdr->hdr_len); + vnet_hdr.hdr_len); + has_vnet_hdr = true; } copylen = max_t(int, copylen, dev->hard_header_len); skb = sock_alloc_send_skb(&po->sk, @@ -2875,12 +2880,12 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) } } - if (po->has_vnet_hdr) { - if (virtio_net_hdr_to_skb(skb, vnet_hdr, vio_le())) { + if (has_vnet_hdr) { + if (virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le())) { tp_len = -EINVAL; goto tpacket_error; } - virtio_net_hdr_set_proto(skb, vnet_hdr); + virtio_net_hdr_set_proto(skb, &vnet_hdr); } skb->destructor = tpacket_destruct_skb; -- 2.53.0