xfrm: hold dev ref until after transport_finish NF_HOOK
Subsystems (3)
PATCH 1/1 xfrm: hold dev ref until after transport_finish NF_HOOK
From: Qi Tang <[email protected]>
[ Upstream commit 1c428b03840094410c5fb6a5db30640486bbbfcb ]
After async crypto completes, xfrm_input_resume() calls dev_put() immediately on re-entry before the skb reaches transport_finish. The skb->dev pointer is then used inside NF_HOOK and its okfn, which can race with device teardown.
Remove the dev_put from the async resumption entry and instead drop the reference after the NF_HOOK call in transport_finish, using a saved device pointer since NF_HOOK may consume the skb. This covers NF_DROP, NF_QUEUE and NF_STOLEN paths that skip the okfn.
For non-transport exits (decaps, gro, drop) and secondary async return points, release the reference inline when async is set.
[ net/xfrm/xfrm_input.c: dev_hold/dev_put are unconditional here rather than inside !crypto_done as in mainline, and the dev_put in the encap_type == -1 async-resumption block does not exist; adapted by gating dev_put at resume: with if (!async), adding if (async) dev_put at -EINPROGRESS return, gro_cells_receive paths, and drop label. ]
--- net/ipv4/xfrm4_input.c | 5 ++++- net/ipv6/xfrm6_input.c | 5 ++++- net/xfrm/xfrm_input.c | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-)
net/ipv4/xfrm4_input.c +4 -1
@@ -48,6 +48,7 @@ int xfrm4_transport_finish(struct sk_buff *skb, int async) {struct xfrm_offload *xo =xfrm_offload (skb);struct iphdr *iph =ip_hdr (skb); +struct net_device *dev = skb->dev; iph->protocol =XFRM_MODE_SKB_CB (skb)->protocol;
@@ -71,8 +72,10 @@ int xfrm4_transport_finish(struct sk_buff *skb, int async) }NF_HOOK (NFPROTO_IPV4 ,NF_INET_PRE_ROUTING , -dev_net (skb->dev),NULL , skb, skb->dev,NULL , +dev_net (dev),NULL , skb, dev,NULL , xfrm4_rcv_encap_finish); +if (async) +dev_put (dev);return 0 ; }
net/ipv6/xfrm6_input.c +4 -1
@@ -41,6 +41,7 @@ static int xfrm6_transport_finish2(struct net *net, struct sock *sk,int xfrm6_transport_finish (struct sk_buff *skb,int async) {struct xfrm_offload *xo =xfrm_offload (skb); +struct net_device *dev = skb->dev;int nhlen = skb->data -skb_network_header (skb); skb_network_header(skb)[IP6CB(skb)->nhoff] =
@@ -66,8 +67,10 @@ int xfrm6_transport_finish(struct sk_buff *skb, int async) }NF_HOOK (NFPROTO_IPV6 ,NF_INET_PRE_ROUTING , -dev_net (skb->dev),NULL , skb, skb->dev,NULL , +dev_net (dev),NULL , skb, dev,NULL , xfrm6_transport_finish2); +if (async) +dev_put (dev);return 0 ; }
net/xfrm/xfrm_input.c +12 -2
@@ -636,10 +636,14 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)else nexthdr = x->type->input (x, skb); -if (nexthdr == -EINPROGRESS ) +if (nexthdr == -EINPROGRESS ) { +if (async) +dev_put (skb->dev);return 0 ; + } resume: -dev_put (skb->dev); +if (!async) +dev_put (skb->dev);spin_lock (&x->lock);if (nexthdr <0 ) {
@@ -707,6 +711,8 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) sp->olen =0 ;if (skb_valid_dst (skb))skb_dst_drop (skb); +if (async) +dev_put (skb->dev);gro_cells_receive (&gro_cells, skb);return 0 ; }else {
@@ -726,6 +732,8 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) sp->olen =0 ;if (skb_valid_dst (skb))skb_dst_drop (skb); +if (async) +dev_put (skb->dev);gro_cells_receive (&gro_cells, skb);return err; }
@@ -736,6 +744,8 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type) drop_unlock:spin_unlock (&x->lock); drop: +if (async) +dev_put (skb->dev);xfrm_rcv_cb (skb, family, x && x->type ? x->type->proto : nexthdr,-1 );kfree_skb (skb);return 0 ;
base-commit: 0a40c6fbd105802fbbcaadca249e0948fbf8095a
Signature · 12 lines
2.50.1 Amazon Web Services Development Center Germany GmbH Tamara-Danz-Str. 13 10243 Berlin Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
Sasha Levin
maintainerQuoted 1 line
[PATCH 6.6.y] xfrm: hold dev ref until after
transport_finishNF_HOOK
I'm holding all four of these (6.6, 6.1, 5.15 and 5.10) for now.
As adapted, the backport leaks a netdev reference on the nested transport-mode path where both an async and a sync decapsulation happen: the inner dev_hold is balanced by a dev_put that the older trees don't have, so the saved reference is never released. Mainline avoids this because it has b05d42eefac7 ("xfrm:
hold device only for the asynchronous decryption") as a prerequisite.
Signature · 2 lines
Thanks, Sasha
Quoted 2 lines · Sasha Levin
Quoted 1 line
[PATCH 6.6.y] xfrm: hold dev ref until after
transport_finishNF_HOOKI'm holding all four of these (6.6, 6.1, 5.15 and 5.10) for now.
You probably need to hold the backport I sent for 6.12 too:
https://lore.kernel.org/all/[email protected]/
Quoted 5 lines
As adapted, the backport leaks a netdev reference on the nested transport-mode path where both an async and a sync decapsulation happen: the inner
dev_holdis balanced by adev_putthat the older trees don't have, so the saved reference is never released. Mainline avoids this because it hasb05d42eefac7("xfrm:
hold device only for the asynchronous decryption") as a prerequisite.
Sorry I missed this and thanks for catching. I'll send v2 for this.
- Simon
Amazon Web Services Development Center Germany GmbH Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597