inet: frags: limit the truesize of a reassembly queue
Subsystems (4)
Eric Dumazet
maintainerPATCH 1/1 inet: frags: limit the truesize of a reassembly queue
The truesize of fragments in a reassembly queue is only bounded by the per netns ipfrag_high_thresh sysctl, which has no upper limit.
inet_frag_reasm_finish() sums the truesize of all fragments in an int. If this sum reaches 2^32, it wraps, and the final sk_wmem_alloc adjustment of the socket owning the reassembled skb can cancel its own reference, leading to a use-after-free of this socket.
This can be triggered from an unprivileged user/network namespace, raising ipfrag_high_thresh and using padded TUN packets, which keep a large truesize after being trimmed by ip_rcv_core().
Track the sum of the truesize of fragments in each queue, and refuse a fragment that would make it bigger than INT_MAX / 2. Callers kill the queue on this error. This bound is far above the needs of legitimate traffic.
---
Kyle Zeng provided a more complicated patch, I wrote something much simpler. LLM was used to discover the bug, build a repro, and test the fix.
include/net/inet_frag.h | 8 ++++++++ net/ipv4/inet_fragment.c | 4 ++++ 2 files changed, 12 insertions(+)
include/net/inet_frag.h +8 -0
@@ -100,6 +100,7 @@ struct inet_frag_queue {u8 tstamp_type;__u8 flags;u16 max_size; +unsigned int truesize;struct fqdir *fqdir;struct rcu_head rcu; };
@@ -172,6 +173,13 @@ extern const u8 ip_frag_ecn_table[16];#define IPFRAG_DUP 1#define IPFRAG_OVERLAP 2#define IPFRAG_MIXED_SECPATH 3 +#define IPFRAG_TOOBIG 4 + +/* Max sum of the truesize of fragments in a queue. This keeps the sums +* done in inet_frag_reasm_prepare() and inet_frag_reasm_finish() well +* below INT_MAX. Legitimate traffic stays far below this limit. +*/ +#define INET_FRAG_MAX_TRUESIZE (INT_MAX / 2)int inet_frag_queue_insert (struct inet_frag_queue *q,struct sk_buff *skb,int offset,int end); void *inet_frag_reasm_prepare(struct inet_frag_queue *q, struct sk_buff *skb,
net/ipv4/inet_fragment.c +4 -0
@@ -446,6 +446,9 @@ int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,if (last && !secpath_equal (last, skb))return IPFRAG_MIXED_SECPATH ; +if (skb->truesize >INET_FRAG_MAX_TRUESIZE - q->truesize) +return IPFRAG_TOOBIG ; +/* An IP fragment is never a GSO packet, but an untrusted source * (virtio_net_hdr) may have attached GSO metadata to it. Do not let * that reach the reassembled skb, whose head keeps the first
@@ -510,6 +513,7 @@ int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,if (offset)nf_reset_ct (skb); + q->truesize += skb->truesize;return IPFRAG_OK ; }EXPORT_SYMBOL (inet_frag_queue_insert);
Signature · 1 line
2.53.0
Hi,
net/ipv4/inet_fragment.c int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb, · line 516
Quoted 5 lines · On Wed, 28 Oct 2026 19:29:34 +0100 Eric Dumazet wrote:
net/ipv4/inet_fragment.c +1
-0@@ -510,6 +513,7 @@ int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,if (offset)nf_reset_ct (skb); + q->truesize += skb->truesize;
I am not sure whether q->truesize should be cleared in inet_frag_queue_flush(). Was this intentional or an oversight? Although this does not affect the fix, after reinit q->truesize includes fragments that have already been freed.
The call path:
ip_frag_queue()
if (ip_frag_too_far())
ip_frag_reinit()
inet_frag_queue_flush()
inet_frag_queue_insert()Eric Dumazet
maintainerQuoted 15 lines · Wang Zhan
Hi,
net/ipv4/inet_fragment.c int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb, · line 516
Quoted 5 lines · On Wed, 28 Oct 2026 19:29:34 +0100 Eric Dumazet wrote:
net/ipv4/inet_fragment.c +1
-0@@ -510,6 +513,7 @@ int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,if (offset)nf_reset_ct (skb); + q->truesize += skb->truesize;I am not sure whether
q->truesizeshould be cleared ininet_frag_queue_flush(). Was this intentional or an oversight? Although this does not affect the fix, after reinitq->truesizeincludes fragments that have already been freed.
Quoted 6 lines
The call path:
ip_frag_queue()if (ip_frag_too_far()) ip_frag_reinit() inet_frag_queue_flush() inet_frag_queue_insert()
Good catch, thanks.
This was an oversight: inet_frag_queue_flush() should reset q->truesize.
It is harmless for the overflow itself (we only over-count), but a queue repeatedly reinitialized by ip_frag_too_far() could eventually hit IPFRAG_TOOBIG for no reason.
I will send a v2.
pw-bot: cr