tpacket_rcv() calls virtio_net_hdr_from_skb() after reserving a ring frame with packet_current_rx_frame(). On TPACKET_V3, __packet_lookup_frame_in_block() -> prb_fill_curr_block() has already incremented BLOCK_NUM_PKTS(pbd), advanced BLOCK_LEN(pbd) and pkc->nxt_offset, updated pkc->prev, written ppd->tp_next_offset, and executed prb_run_all_ft_ops() (setting tp_status, VLAN fields, and tp_rxhash). If virtio_net_hdr_from_skb() then fails (for example, on an unsupported GSO type such as SKB_GSO_UDP), tpacket_rcv() only calls prb_clear_blk_fill_status() to release blk_fill_in_prog_lock and jumps to drop_n_account. None of that TPACKET_V3 block state is reverted. When the block later retires, BLOCK_NUM_PKTS(pbd) still includes the dropped frame and exposes an incomplete record with unpopulated (zero or stale) tp_len, tp_snaplen, tp_mac, timestamps, and payload to userspace. Worse, if the failing packet was the last frame in the block, prb_close_block() uses pkc->prev to zero tp_next_offset and populates h1->ts_last_pkt from that incomplete header's unpopulated or stale timestamps. Found by code inspection while working on TPACKET_V3 block retirement. Move virtio_net_hdr_from_skb() before spin_lock(&sk->sk_receive_queue.lock) into a stack virtio_net_hdr (after the snaplen < 0 clamps that clear vnet_hdr_sz), and jump to a new drop_n_account_unlocked label right after spin_unlock(&sk->sk_receive_queue.lock) on error. This preserves tp_drops accounting, sk_data_ready(), copy_skb cleanup, drop reason, and skb_head/skb_len restoration without reserving a frame or dirtying ring memory. Copy the prepared header into the reserved frame with memcpy() after packet_current_rx_frame() and rx_owner_map checks succeed, keeping virtio_net_hdr_from_skb() outside the receive queue lock. The unpopulated TPACKET_V3 frame and ts_last_pkt corruption were reproduced on an unmodified kernel (without kernel instrumentation) in ARM64 QEMU (with KASAN and lockdep) by injecting a VIRTIO_NET_HDR_GSO_UDP packet via IFF_TAP | IFF_VNET_HDR, and confirmed resolved after applying this fix across TPACKET_V3 (failing-only and mixed blocks, PACKET_VNET_HDR and PACKET_VNET_HDR_SZ), TPACKET_V2 (normal and truncated copy_skb paths, unshared and shared skb), plus psock_tpacket and psock_fanout selftests. Fixes: 58d19b19cd99 ("packet: vnet_hdr support for tpacket_rcv") Signed-off-by: Jingxuan Xie --- Split out from the earlier RFC v2 series as a standalone net fix: https://lore.kernel.org/r/20261030081704.251652-2-jingxuanxie@deeproute.ai Changes since RFC v2 1/2: - Add drop_n_account_unlocked label and jump there instead of open-coding drop accounting on virtio_net_hdr_from_skb() failure. - Expand commit message with exact TPACKET_V3 block state left un-reverted and impact on userspace readers and ts_last_pkt. - Add Fixes: tag and rebase onto latest net/main. net/packet/af_packet.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index e0ebd0627..c93a7ff7b 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -2289,6 +2289,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, unsigned short macoff, hdrlen; unsigned int netoff; struct sk_buff *copy_skb = NULL; + struct virtio_net_hdr vnet_hdr; struct timespec64 ts; __u32 ts_status; unsigned int slot_id = 0; @@ -2395,6 +2396,10 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, vnet_hdr_sz = 0; } } + if (vnet_hdr_sz && + virtio_net_hdr_from_skb(skb, &vnet_hdr, vio_le(), true, 0)) + goto drop_n_account_unlocked; + spin_lock(&sk->sk_receive_queue.lock); h.raw = packet_current_rx_frame(po, skb, TP_STATUS_KERNEL, (macoff+snaplen)); @@ -2408,16 +2413,9 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, __set_bit(slot_id, po->rx_ring.rx_owner_map); } - if (vnet_hdr_sz && - virtio_net_hdr_from_skb(skb, h.raw + macoff - - sizeof(struct virtio_net_hdr), - vio_le(), true, 0)) { - if (po->tp_version <= TPACKET_V2) - __clear_bit(slot_id, po->rx_ring.rx_owner_map); - else - prb_clear_blk_fill_status(&po->rx_ring); - goto drop_n_account; - } + if (vnet_hdr_sz) + memcpy(h.raw + macoff - sizeof(vnet_hdr), &vnet_hdr, + sizeof(vnet_hdr)); if (po->tp_version <= TPACKET_V2) { packet_increment_rx_head(po, &po->rx_ring); @@ -2550,6 +2548,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, drop_n_account: spin_unlock(&sk->sk_receive_queue.lock); +drop_n_account_unlocked: atomic_inc(&po->tp_drops); drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR; base-commit: 656683f2e292a8ab28b1863a9a8aa0f9417b58a5 -- 2.25.1