xfrm: iptfs: fix use-after-free on first_skb in __input_process_payload
Subsystems (3)
PATCH 1/1 xfrm: iptfs: fix use-after-free on first_skb in __input_process_payload
__input_process_payload() stores first_skb into xtfs->ra_newskb under drop_lock when starting partial reassembly, then unlocks and breaks out of the processing loop. The post-loop check reads xtfs->ra_newskb without the lock to decide whether first_skb is still owned:
if (first_skb && first_iplen && !defer && first_skb != xtfs->ra_newskb)
Between spin_unlock and this read, a concurrent CPU running iptfs_reassem_cont() (or the drop_timer hrtimer) can complete reassembly, NULL xtfs->ra_newskb, and free the skb. The check then evaluates first_skb != NULL as true, and pskb_trim/ip_summed/consume_skb operate on the freed skb — a use-after-free in skbuff_head_cache.
Replace the unlocked read with a local bool that records whether first_skb was handed to the reassembly state in the current call. The flag is set after the existing spin_unlock, before the break, using the pointer equality that is stable at that point (first_skb == skb iff first_skb was stored in ra_newskb).
--- net/xfrm/xfrm_iptfs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/xfrm/xfrm_iptfs.c +3 -1
@@ -954,6 +954,7 @@ static bool __input_process_payload(struct xfrm_state *x, u32 data,u32 first_iplen, iphlen, iplen, remaining, tail;u32 capturelen;u64 seq; +bool first_skb_partial = false; xtfs = x->mode_data; net =xs_net (x);
@@ -1161,6 +1162,7 @@ static bool __input_process_payload(struct xfrm_state *x, u32 data,spin_unlock (&xtfs->drop_lock); + first_skb_partial = (first_skb == skb);break ; }
@@ -1172,7 +1174,7 @@ static bool __input_process_payload(struct xfrm_state *x, u32 data,/* this should not happen from the above code */ XFRM_INC_STATS (net,LINUX_MIB_XFRMINIPTFSERROR ); -if (first_skb && first_iplen && !defer && first_skb != xtfs->ra_newskb) { +if (first_skb && first_iplen && !defer && !first_skb_partial) {/* first_skb is queued b/c !defer and not partial */ if (pskb_trim (first_skb, first_iplen)) {/* error trimming */
Signature · 1 line
2.50.1 (Apple Git-155)
Simon Horman
reviewerQuoted 13 lines · Zhenghang Xiao
__input_process_payload()storesfirst_skbintoxtfs->ra_newskbunderdrop_lockwhen starting partial reassembly, then unlocks and breaks out of the processing loop. The post-loop check readsxtfs->ra_newskbwithout the lock to decide whetherfirst_skbis still owned:if (first_skb && first_iplen && !defer && first_skb != xtfs->ra_newskb)Between
spin_unlockand this read, a concurrent CPU runningiptfs_reassem_cont()(or thedrop_timerhrtimer) can complete reassembly, NULLxtfs->ra_newskb, and free the skb. The check then evaluatesfirst_skb!= NULL as true, and pskb_trim/ip_summed/consume_skb operate on the freed skb — a use-after-free inskbuff_head_cache.
Quoted 8 lines
Replace the unlocked read with a local bool that records whether
first_skbwas handed to the reassembly state in the current call. The flag is set after the existingspin_unlock, before the break, using the pointer equality that is stable at that point (first_skb== skb ifffirst_skbwas stored inra_newskb).Fixes 3f3339885fb3 ("xfrm: iptfs: add reusing received skb for the tunnel egress packet")Signed-off-by Zhenghang Xiao <[email protected]>
FTR: An AI generated review of this patch is available on sashiko.dev. I believe that review can be treated in the context of possible follow-up and should not effect the progress of this patch.
Quoted 13 lines · Zhenghang Xiao
__input_process_payload()storesfirst_skbintoxtfs->ra_newskbunderdrop_lockwhen starting partial reassembly, then unlocks and breaks out of the processing loop. The post-loop check readsxtfs->ra_newskbwithout the lock to decide whetherfirst_skbis still owned:if (first_skb && first_iplen && !defer && first_skb != xtfs->ra_newskb)Between
spin_unlockand this read, a concurrent CPU runningiptfs_reassem_cont()(or thedrop_timerhrtimer) can complete reassembly, NULLxtfs->ra_newskb, and free the skb. The check then evaluatesfirst_skb!= NULL as true, and pskb_trim/ip_summed/consume_skb operate on the freed skb — a use-after-free inskbuff_head_cache.
Quoted 8 lines
Replace the unlocked read with a local bool that records whether
first_skbwas handed to the reassembly state in the current call. The flag is set after the existingspin_unlock, before the break, using the pointer equality that is stable at that point (first_skb== skb ifffirst_skbwas stored inra_newskb).Fixes 3f3339885fb3 ("xfrm: iptfs: add reusing received skb for the tunnel egress packet")Signed-off-by Zhenghang Xiao <[email protected]>
Applied to the ipsec tree, thanks a lot!