Skip to contentSkip to conversation

xfrm: vti: hold device reference for tunnel pointer stored in skb cb

PATCH In review Zhenghang Xiao 2 participants 2 messages May 26 – May 28

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfrm: vti: hold device reference for tunnel pointer stored in skb cb

vti_input() and vti6_input_proto() stash a tunnel device pointer in XFRM_TUNNEL_SKB_CB(skb)->tunnel without taking a device reference. When xfrm processes the packet asynchronously (hardware crypto or pcrypt workqueue), the completion callback vti_rcv_cb() / vti6_rcv_cb() dereferences this pointer from workqueue context. If the tunnel is destroyed between stash and callback, the pointer references freed memory.

Add dev_hold() when stashing the tunnel pointer and dev_put() in the receive callbacks after use, matching the existing dev_hold(skb->dev) pattern in xfrm_input() for the physical ingress device.

Fixes df3893c176e9 ("vti: Update the ipv4 side to use it's own receive hook.")
---
 net/ipv4/ip_vti.c  |  8 +++++++-
 net/ipv6/ip6_vti.c | 10 ++++++++--
 2 files changed, 15 insertions(+), 3 deletions(-)
net/ipv4/ip_vti.c +7 -1
index 95b6bb78fcd2..3be298c5a80c 100644
@@ -62,6 +62,7 @@ static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
 			goto drop;
 
 		XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
+		dev_hold(tunnel->dev);
 
 		if (update_skb_dev)
 			skb->dev = tunnel->dev;
@@ -112,6 +113,7 @@ static int vti_rcv_cb(struct sk_buff *skb, int err)
 	if (err) {
 		DEV_STATS_INC(dev, rx_errors);
 		DEV_STATS_INC(dev, rx_dropped);
+		dev_put(dev);
 
 		return 0;
 	}
@@ -125,6 +127,7 @@ static int vti_rcv_cb(struct sk_buff *skb, int err)
 		if (inner_mode == NULL) {
 			XFRM_INC_STATS(dev_net(skb->dev),
 				       LINUX_MIB_XFRMINSTATEMODEERROR);
+			dev_put(dev);
 			return -EINVAL;
 		}
 	}
@@ -135,12 +138,15 @@ static int vti_rcv_cb(struct sk_buff *skb, int err)
 	ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
 	skb->mark = orig_mark;
 
-	if (!ret)
+	if (!ret) {
+		dev_put(dev);
 		return -EPERM;
+	}
 
 	skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(skb->dev)));
 	skb->dev = dev;
 	dev_sw_netstats_rx_add(dev, skb->len);
+	dev_put(dev);
 
 	return 0;
 }
net/ipv6/ip6_vti.c +8 -2
index ad5290be4dd6..367a3325cf9c 100644
@@ -317,9 +317,10 @@ static int vti6_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
 			goto discard;
 		}
 
+		XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
+		dev_hold(t->dev);
 		rcu_read_unlock();
 
-		XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
 		XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
 		XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
 		return xfrm_input(skb, nexthdr, spi, encap_type);
@@ -356,6 +357,7 @@ static int vti6_rcv_cb(struct sk_buff *skb, int err)
 	if (err) {
 		DEV_STATS_INC(dev, rx_errors);
 		DEV_STATS_INC(dev, rx_dropped);
+		dev_put(dev);
 
 		return 0;
 	}
@@ -369,6 +371,7 @@ static int vti6_rcv_cb(struct sk_buff *skb, int err)
 		if (inner_mode == NULL) {
 			XFRM_INC_STATS(dev_net(skb->dev),
 				       LINUX_MIB_XFRMINSTATEMODEERROR);
+			dev_put(dev);
 			return -EINVAL;
 		}
 	}
@@ -379,12 +382,15 @@ static int vti6_rcv_cb(struct sk_buff *skb, int err)
 	ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
 	skb->mark = orig_mark;
 
-	if (!ret)
+	if (!ret) {
+		dev_put(dev);
 		return -EPERM;
+	}
 
 	skb_scrub_packet(skb, !net_eq(t->net, dev_net(skb->dev)));
 	skb->dev = dev;
 	dev_sw_netstats_rx_add(dev, skb->len);
+	dev_put(dev);
 
 	return 0;
 }
Signature · 1 line
2.50.1 (Apple Git-155)
reviewer
#1 lore raw

From: 'Simon Horman' <[email protected]>

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://netdev-ai.bots.linux.dev/sashiko/
---
xfrm: vti: hold device reference for tunnel pointer stored in skb cb

This patch adds dev_hold() in vti_input() and vti6_input_proto() when stashing the tunnel device pointer into XFRM_TUNNEL_SKB_CB(skb), with matching dev_put() calls on every return path of vti_rcv_cb() and vti6_rcv_cb() to balance the new reference.

net/ipv4/ip_vti.c static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi, · line 68

Quoted 12 lines
net/ipv4/ip_vti.c +1 -0
index 95b6bb78fcd2..3be298c5a80c 100644
@@ -62,6 +62,7 @@ static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
 			goto drop;
 
 		XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
+		dev_hold(tunnel->dev);
 
 		if (update_skb_dev)
 			skb->dev = tunnel->dev;

[High]
Is the new dev_hold() balanced on the async-resume drop path? xfrm_input() with encap_type == -1 does:

    if (encap_type < 0 || ...) {
        x = xfrm_input_state(skb);

        if (unlikely(x->km.state != XFRM_STATE_VALID)) {
            ...
            if (encap_type == -1)
                dev_put(skb->dev);
            goto drop;
        }

        family = x->props.family;

so when the state has been invalidated between submission and async completion, the function jumps to drop with family still at its function-local default of AF_UNSPEC.

The drop path then calls xfrm_rcv_cb(skb, family, ..., -1). Since xfrm_input_afinfo only has slots populated for AF_INET and AF_INET6, xfrm_rcv_cb returns -EAFNOSUPPORT before invoking vti_rcv_cb at all.

Does that mean the dev_hold(tunnel->dev) added in vti_input() is never released on this path, leaving the vti netdev unable to be unregistered ("unregister_netdevice: waiting for vtiN to become free")?

This is the hardware-crypto / pcrypt async path the commit message calls out, so does the fix actually cover it?

Quoted 23 lines
net/ipv4/ip_vti.c +4 -1
@@ -112,6 +113,7 @@ static int vti_rcv_cb(struct sk_buff *skb, int err)
 	if (err) {
 		DEV_STATS_INC(dev, rx_errors);
 		DEV_STATS_INC(dev, rx_dropped);
+		dev_put(dev);
 
 		return 0;
 	}
@@ -125,6 +127,7 @@ static int vti_rcv_cb(struct sk_buff *skb, int err)
 		if (inner_mode == NULL) {
 			XFRM_INC_STATS(dev_net(skb->dev),
 				       LINUX_MIB_XFRMINSTATEMODEERROR);
+			dev_put(dev);
 			return -EINVAL;
 		}
 	}
@@ -135,12 +138,15 @@ static int vti_rcv_cb(struct sk_buff *skb, int err)
 	ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
 	skb->mark = orig_mark;
 
-	if (!ret)
+	if (!ret) {
+		dev_put(dev);

net/ipv4/ip_vti.c static int vti_rcv_cb(struct sk_buff *skb, int err) · line 152

Quoted 10 lines
net/ipv4/ip_vti.c +2 -0
 		return -EPERM;
+	}
 
 	skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(skb->dev)));
 	skb->dev = dev;
 	dev_sw_netstats_rx_add(dev, skb->len);
+	dev_put(dev);
 
 	return 0;
 }

[Critical]
Can the err == 0 paths that return a non-zero value cause a double dev_put()?

xfrm_input() invokes the cb chain twice when the first call returns an error:

    err = xfrm_rcv_cb(skb, family, x->type->proto, 0);
    if (err)
        goto drop;
    ...
drop:
    ...
    xfrm_rcv_cb(skb, family, x->type->proto, -1);

In vti_rcv_cb the inner_mode == NULL branch and the !ret branch both return a negative value after performing dev_put(dev):

    if (inner_mode == NULL) {
        ...
        dev_put(dev);
        return -EINVAL;
    }
    ...
    if (!ret) {
        dev_put(dev);
        return -EPERM;
    }

Since XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 is not cleared, the second invocation enters with err == -1 and reaches:

    dev = tunnel->dev;
    if (err) {
        ...
        dev_put(dev);
        return 0;
    }

so the same tunnel->dev is dev_put() twice for a single dev_hold() taken in vti_input(), giving a netdev refcount underflow.

Should XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 be cleared after dev_put(), or the error-return paths take the dev_put() only once?

Quoted 6 lines
net/ipv6/ip6_vti.c +0 -0
index df793c8bfffb..ab5f8480eed7 100644
@@ -317,9 +317,10 @@ static int vti6_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
 			goto discard;

net/ipv6/ip6_vti.c static int vti6_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi, · line 326

Quoted 10 lines
net/ipv6/ip6_vti.c +2 -1
 		}
 
+		XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
+		dev_hold(t->dev);
 		rcu_read_unlock();
 
-		XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
 		XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
 		XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
 		return xfrm_input(skb, nexthdr, spi, encap_type);

[High]
Same async-resume question as for vti_input(): if x->km.state becomes invalid between submission and the resume call, xfrm_input() goes to drop with family still at AF_UNSPEC, xfrm_rcv_cb() bails out via xfrm_input_get_afinfo() returning NULL, and vti6_rcv_cb() is never called. Is the dev_hold(t->dev) added here ever released on that path?

Quoted 23 lines
net/ipv6/ip6_vti.c +4 -1
@@ -356,6 +357,7 @@ static int vti6_rcv_cb(struct sk_buff *skb, int err)
 	if (err) {
 		DEV_STATS_INC(dev, rx_errors);
 		DEV_STATS_INC(dev, rx_dropped);
+		dev_put(dev);
 
 		return 0;
 	}
@@ -369,6 +371,7 @@ static int vti6_rcv_cb(struct sk_buff *skb, int err)
 		if (inner_mode == NULL) {
 			XFRM_INC_STATS(dev_net(skb->dev),
 				       LINUX_MIB_XFRMINSTATEMODEERROR);
+			dev_put(dev);
 			return -EINVAL;
 		}
 	}
@@ -379,12 +382,15 @@ static int vti6_rcv_cb(struct sk_buff *skb, int err)
 	ret = xfrm_policy_check(NULL, XFRM_POLICY_IN, skb, family);
 	skb->mark = orig_mark;
 
-	if (!ret)
+	if (!ret) {
+		dev_put(dev);

net/ipv6/ip6_vti.c static int vti6_rcv_cb(struct sk_buff *skb, int err) · line 396

Quoted 10 lines
net/ipv6/ip6_vti.c +2 -0
 		return -EPERM;
+	}
 
 	skb_scrub_packet(skb, !net_eq(t->net, dev_net(skb->dev)));
 	skb->dev = dev;
 	dev_sw_netstats_rx_add(dev, skb->len);
+	dev_put(dev);
 
 	return 0;
 }

[Critical]
The same double-put concern applies here. When inner_mode == NULL the function does dev_put(dev) and returns -EINVAL, and when xfrm_policy_check fails it does dev_put(dev) and returns -EPERM. xfrm_input() then re-runs the cb chain with err == -1:

    if (err) {
        ...
        dev_put(dev);
        return 0;
    }

XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 still holds t, so this second invocation does not exit at the !t check and performs another dev_put on the same tunnel device. Should the cb pointer be cleared or the put be done once?