net/packet: check TX_RING fragments before memory accounting
Subsystems (3)
PATCH 1/1 net/packet: check TX_RING fragments before memory accounting
From: Kyle Zeng <[email protected]>
[ Upstream commit dc4dd12f80e499dc034710cd8195489abcf0b059 ]
tpacket_fill_skb() adds the full non-linear payload length to skb->truesize and sk_wmem_alloc before checking MAX_SKB_FRAGS. With PACKET_VNET_HDR, userspace can submit a ring-sized frame even when it greatly exceeds the device MTU.
A near-INT_MAX frame can saturate sk_wmem_alloc before the fragment loop rejects it. A subsequent refcount_add(0x40000001) can temporarily turn REFCOUNT_SATURATED into 1. A concurrent TX completion can then drop that apparent last reference and free the still-live packet socket before refcount_warn_saturate() restores the counter.
Check that the remaining payload fits in MAX_SKB_FRAGS pages before accounting it. Include the initial page offset in the calculation, and move the existing -EFAULT handling out of the fragment loop. This preserves the accepted packet sizes and error code while preventing an invalid frame from injecting an oversized byte charge.
[ 6.1: adjust for context in kernels lacking 3948b05950fd ("net:
introduce a config option to tweak MAX_SKB_FRAGS") ]
--- net/packet/af_packet.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
net/packet/af_packet.c +6 -6
@@ -2705,6 +2705,12 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb, } offset =offset_in_page (data); +if (unlikely (to_write >MAX_SKB_FRAGS *PAGE_SIZE - offset)) { +net_warn_ratelimited ("Packet exceed the number of skb frags(%u)\n" , + (unsigned int )MAX_SKB_FRAGS ); +return -EFAULT ; + } + len_max =PAGE_SIZE - offset; len = ((to_write > len_max) ? len_max : to_write);
@@ -2716,12 +2722,6 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,while (likely (to_write)) { nr_frags =skb_shinfo (skb)->nr_frags; -if (unlikely (nr_frags >=MAX_SKB_FRAGS )) { -pr_err ("Packet exceed the number of skb frags(%lu)\n" , -MAX_SKB_FRAGS ); -return -EFAULT ; - } - page =pgv_to_page (data); data += len;flush_dcache_page (page);
Signature · 1 line
2.50.1
Quoted 1 line · Bjoern Doebel
Subject: [PATCH 6.1.y] net/packet: check
TX_RINGfragments before memory accounting
Queued for 6.1, thanks.
Signature · 2 lines
Thanks, Sasha