Skip to contentSkip to conversation

udp: drop secpath before storing an skb in a receive queue

PATCH In review Eric Dumazet 5 participants 17 messages Oct 14, 2025

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

PATCH 1/1 udp: drop secpath before storing an skb in a receive queue

Michal reported and bisected an issue after recent adoption of skb_attempt_defer_free() in UDP.

We had the same issue for TCP, that Sabrina fixed in commit 9b6412e6979f ("tcp: drop secpath at the same time as we currently drop dst")

Many thanks to Michal and Sabrina.

---
 net/ipv4/udp.c | 2 ++
 1 file changed, 2 insertions(+)
net/ipv4/udp.c +2 -0
index 95241093b7f0..3f05ee70029c 100644
@@ -1709,6 +1709,8 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
 	int dropcount;
 	int nb = 0;
 
+	secpath_reset(skb);
+
 	rmem = atomic_read(&sk->sk_rmem_alloc);
 	rcvbuf = READ_ONCE(sk->sk_rcvbuf);
 	size = skb->truesize;
Signature · 1 line
2.51.0.788.g6d19910ace-goog
#1 lore raw
Quoted 5 lines · Eric Dumazet

Michal reported and bisected an issue after recent adoption of skb_attempt_defer_free() in UDP.

We had the same issue for TCP, that Sabrina fixed in commit 9b6412e6979f ("tcp: drop secpath at the same time as we currently drop dst")

I'm not convinced this is the same bug. The TCP one was a "leaked" reference (delayed put). This looks more like a double put/missing hold to me (we get to the destroy path without having done the proper delete, which would set XFRM_STATE_DEAD).

And this shouldn't be an issue after b441cf3f8c4b ("xfrm: delete x->tunnel as we delete x").

Quoted 16 lines

Many thanks to Michal and Sabrina.

---
 net/ipv4/udp.c | 2 ++
 1 file changed, 2 insertions(+)
net/ipv4/udp.c +0 -0
index 95241093b7f0..3f05ee70029c 100644

net/ipv4/udp.c int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) · line 1712

Quoted 4 lines
net/ipv4/udp.c +1 -0
@@ -1709,6 +1709,8 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
 	int dropcount;
 	int nb = 0;
 
+	secpath_reset(skb);

See also the comment for udp_try_make_stateless:

/* all head states (dst, sk, nf conntrack) except skb extensions are
* cleared by udp_rcv().
*
* We need to preserve secpath, if present, to eventually process
* IP_CMSG_PASSSEC at recvmsg() time.
*
* Other extensions can be cleared.
*/

It looks like this patch would re-introduce the problem fixed by dce4551cb2ad ("udp: preserve head state for IP_CMSG_PASSSEC").

Quoted 7 lines
net/ipv4/udp.c +1 -0
+
 	rmem = atomic_read(&sk->sk_rmem_alloc);
 	rcvbuf = READ_ONCE(sk->sk_rcvbuf);
 	size = skb->truesize;
Signature · 1 line
2.51.0.788.g6d19910ace-goog
Signature · 1 line
Sabrina
#2 lore raw
Quoted 5 lines · Sabrina Dubroca
Quoted 3 lines · Eric Dumazet

Michal reported and bisected an issue after recent adoption of skb_attempt_defer_free() in UDP.

Quoted 8 lines
Quoted 2 lines

We had the same issue for TCP, that Sabrina fixed in commit 9b6412e6979f ("tcp: drop secpath at the same time as we currently drop dst")

I'm not convinced this is the same bug. The TCP one was a "leaked" reference (delayed put). This looks more like a double put/missing hold to me (we get to the destroy path without having done the proper delete, which would set XFRM_STATE_DEAD).

Hmm, this was bisected to use of skb_attempt_defer_free(), surely holding xfrm in a per-cpu queue looks the same to me.

We also had recent syzbot reports hinting at that.

Quoted 36 lines

And this shouldn't be an issue after b441cf3f8c4b ("xfrm: delete x->tunnel as we delete x").

Quoted 16 lines

Many thanks to Michal and Sabrina.

---
 net/ipv4/udp.c | 2 ++
 1 file changed, 2 insertions(+)
net/ipv4/udp.c +0 -0
index 95241093b7f0..3f05ee70029c 100644

net/ipv4/udp.c int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb) · line 1712

Quoted 4 lines
net/ipv4/udp.c +1 -0
@@ -1709,6 +1709,8 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
      int dropcount;
      int nb = 0;
 
+     secpath_reset(skb);

See also the comment for udp_try_make_stateless:

/* all head states (dst, sk, nf conntrack) except skb extensions are
* cleared by udp_rcv().
*
* We need to preserve secpath, if present, to eventually process
* IP_CMSG_PASSSEC at recvmsg() time.
*
* Other extensions can be cleared.
*/

Quoted 2 lines

It looks like this patch would re-introduce the problem fixed by dce4551cb2ad ("udp: preserve head state for IP_CMSG_PASSSEC").

Arg, I tried to not slow down the consumer part, too bad for XFRM then.

What about then :

net/ipv4/udp.c +0 -0
index 95241093b7f0..ac45e4056c51 100644
@@ -1853,6 +1853,7 @@ void skb_consume_udp(struct sock *sk, struct

sk_buff *skb, int len)

        if (!skb_shared(skb)) {
                if (unlikely(udp_skb_has_head_state(skb)))
                        skb_release_head_state(skb);
+               secpath_reset(skb);
                skb_attempt_defer_free(skb);
                return;
        }
maintainer
#3 lore raw
In reply to Sabrina Dubroca
Quoted 7 lines · Sabrina Dubroca
Quoted 5 lines · Eric Dumazet

Michal reported and bisected an issue after recent adoption of skb_attempt_defer_free() in UDP.

We had the same issue for TCP, that Sabrina fixed in commit 9b6412e6979f ("tcp: drop secpath at the same time as we currently drop dst")

Quoted 7 lines

I'm not convinced this is the same bug. The TCP one was a "leaked" reference (delayed put). This looks more like a double put/missing hold to me (we get to the destroy path without having done the proper delete, which would set XFRM_STATE_DEAD).

And this shouldn't be an issue after b441cf3f8c4b ("xfrm: delete x->tunnel as we delete x").

I think Sabrina is right. If the skb carries a secpath, UDP_SKB_IS_STATELESS is not set, and skb_release_head_state() will be called by skb_consume_udp().

skb_ext_put() does not clear skb->extensions nor ext->refcnt, if skb_attempt_defer_free() enters the slow path (kfree_skb_napi_cache()), the skb will go through again skb_release_head_state(), with a double free.

I think something alike the following (completely untested) should work:
---

net/ipv4/udp.c +0 -0
index 95241093b7f0..4a308fd6aa6c 100644
@@ -1851,8 +1851,10 @@ void skb_consume_udp(struct sock *sk, struct

sk_buff *skb, int len)
sk_peek_offset_bwd(sk, len);

if (!skb_shared(skb)) {
- if (unlikely(udp_skb_has_head_state(skb)))
+ if (unlikely(udp_skb_has_head_state(skb))) {
skb_release_head_state(skb);
+ skb->active_extensions = 0;
+ }
skb_attempt_defer_free(skb); return;
}
---

#4 lore raw
Quoted 34 lines · Paolo Abeni
Quoted 7 lines · Sabrina Dubroca
Quoted 5 lines · Eric Dumazet

Michal reported and bisected an issue after recent adoption of skb_attempt_defer_free() in UDP.

We had the same issue for TCP, that Sabrina fixed in commit 9b6412e6979f ("tcp: drop secpath at the same time as we currently drop dst")

Quoted 7 lines

I'm not convinced this is the same bug. The TCP one was a "leaked" reference (delayed put). This looks more like a double put/missing hold to me (we get to the destroy path without having done the proper delete, which would set XFRM_STATE_DEAD).

And this shouldn't be an issue after b441cf3f8c4b ("xfrm: delete x->tunnel as we delete x").

I think Sabrina is right. If the skb carries a secpath, UDP_SKB_IS_STATELESS is not set, and skb_release_head_state() will be called by skb_consume_udp().

skb_ext_put() does not clear skb->extensions nor ext->refcnt, if skb_attempt_defer_free() enters the slow path (kfree_skb_napi_cache()), the skb will go through again skb_release_head_state(), with a double free.

I think something alike the following (completely untested) should work:
---

net/ipv4/udp.c +0 -0
index 95241093b7f0..4a308fd6aa6c 100644

net/ipv4/udp.c void skb_consume_udp(struct sock *sk, struct · line 1857

Quoted 8 lines
net/ipv4/udp.c +2 -1
@@ -1851,8 +1851,10 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
                sk_peek_offset_bwd(sk, len);
 
        if (!skb_shared(skb)) {
-               if (unlikely(udp_skb_has_head_state(skb)))
+               if (unlikely(udp_skb_has_head_state(skb))) {
                        skb_release_head_state(skb);
+                       skb->active_extensions = 0;

Wait, what, skb_ext_put() can not be called twice ?

Because we prefer not dirtying skb in skb_release_head_state() ?

Perhaps add a big comment in skb_release_head_state() to avoid mistakes.

net/core/skbuff.c +0 -0
index bc12790017b0b5c0be99f8fb9d362b3730fa4eb0..8d274ddd54ad4c59cc29f821fc371c89052bf875

100644

net/core/skbuff.c +4 -0
@@ -1141,6 +1141,10 @@ void skb_release_head_state(struct sk_buff *skb)
 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
        nf_conntrack_put(skb_nfct(skb));
 #endif
+
+       /* Note: we do not call skb_ext_reset() to avoid dirtying
+        * a cache line. Callers might have to clear skb->active_extensions.
+        */
        skb_ext_put(skb);
 }
#5 lore raw
Quoted 39 lines · Eric Dumazet
Quoted 37 lines · Paolo Abeni
Quoted 7 lines · Sabrina Dubroca
Quoted 5 lines · Eric Dumazet

Michal reported and bisected an issue after recent adoption of skb_attempt_defer_free() in UDP.

We had the same issue for TCP, that Sabrina fixed in commit 9b6412e6979f ("tcp: drop secpath at the same time as we currently drop dst")

Quoted 7 lines

I'm not convinced this is the same bug. The TCP one was a "leaked" reference (delayed put). This looks more like a double put/missing hold to me (we get to the destroy path without having done the proper delete, which would set XFRM_STATE_DEAD).

And this shouldn't be an issue after b441cf3f8c4b ("xfrm: delete x->tunnel as we delete x").

I think Sabrina is right. If the skb carries a secpath, UDP_SKB_IS_STATELESS is not set, and skb_release_head_state() will be called by skb_consume_udp().

skb_ext_put() does not clear skb->extensions nor ext->refcnt, if skb_attempt_defer_free() enters the slow path (kfree_skb_napi_cache()), the skb will go through again skb_release_head_state(), with a double free.

I think something alike the following (completely untested) should work:
---

net/ipv4/udp.c +0 -0
index 95241093b7f0..4a308fd6aa6c 100644
@@ -1851,8 +1851,10 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
                sk_peek_offset_bwd(sk, len);
 
Quoted 5 lines

net/ipv4/udp.c void skb_consume_udp(struct sock *sk, struct · line 1857

Quoted 5 lines
net/ipv4/udp.c +2 -1
        if (!skb_shared(skb)) {
-               if (unlikely(udp_skb_has_head_state(skb)))
+               if (unlikely(udp_skb_has_head_state(skb))) {
                        skb_release_head_state(skb);
+                       skb->active_extensions = 0;

We probably also want to clear CONNTRACK state as well.

#6 lore raw
Quoted 41 lines · Eric Dumazet
Quoted 39 lines · Eric Dumazet
Quoted 37 lines · Paolo Abeni
Quoted 7 lines · Sabrina Dubroca
Quoted 5 lines · Eric Dumazet

Michal reported and bisected an issue after recent adoption of skb_attempt_defer_free() in UDP.

We had the same issue for TCP, that Sabrina fixed in commit 9b6412e6979f ("tcp: drop secpath at the same time as we currently drop dst")

Quoted 7 lines

I'm not convinced this is the same bug. The TCP one was a "leaked" reference (delayed put). This looks more like a double put/missing hold to me (we get to the destroy path without having done the proper delete, which would set XFRM_STATE_DEAD).

And this shouldn't be an issue after b441cf3f8c4b ("xfrm: delete x->tunnel as we delete x").

I think Sabrina is right. If the skb carries a secpath, UDP_SKB_IS_STATELESS is not set, and skb_release_head_state() will be called by skb_consume_udp().

skb_ext_put() does not clear skb->extensions nor ext->refcnt, if skb_attempt_defer_free() enters the slow path (kfree_skb_napi_cache()), the skb will go through again skb_release_head_state(), with a double free.

I think something alike the following (completely untested) should work:
---

net/ipv4/udp.c +0 -0
index 95241093b7f0..4a308fd6aa6c 100644
@@ -1851,8 +1851,10 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
                sk_peek_offset_bwd(sk, len);
 
Quoted 7 lines
Quoted 5 lines

net/ipv4/udp.c void skb_consume_udp(struct sock *sk, struct · line 1857

Quoted 5 lines
net/ipv4/udp.c +2 -1
        if (!skb_shared(skb)) {
-               if (unlikely(udp_skb_has_head_state(skb)))
+               if (unlikely(udp_skb_has_head_state(skb))) {
                        skb_release_head_state(skb);
+                       skb->active_extensions = 0;

We probably also want to clear CONNTRACK state as well.

Perhaps not use skb_release_head_state() ?

We know there is no dst, and no destructor.

net/ipv4/udp.c +0 -0
index 95241093b7f0..98628486c4c5 100644
@@ -1851,8 +1851,10 @@ void skb_consume_udp(struct sock *sk, struct

sk_buff *skb, int len)

                sk_peek_offset_bwd(sk, len);

        if (!skb_shared(skb)) {

- if (unlikely(udp_skb_has_head_state(skb)))
- skb_release_head_state(skb);
+ if (unlikely(udp_skb_has_head_state(skb))) {
+ nf_reset_ct(skb);
+ skb_ext_reset(skb);
+ }

                skb_attempt_defer_free(skb);
                return;
        }
#7 lore raw
Quoted 51 lines · Eric Dumazet
Quoted 41 lines · Eric Dumazet
Quoted 39 lines · Eric Dumazet
Quoted 37 lines · Paolo Abeni
Quoted 7 lines · Sabrina Dubroca
Quoted 5 lines · Eric Dumazet

Michal reported and bisected an issue after recent adoption of skb_attempt_defer_free() in UDP.

We had the same issue for TCP, that Sabrina fixed in commit 9b6412e6979f ("tcp: drop secpath at the same time as we currently drop dst")

Quoted 7 lines

I'm not convinced this is the same bug. The TCP one was a "leaked" reference (delayed put). This looks more like a double put/missing hold to me (we get to the destroy path without having done the proper delete, which would set XFRM_STATE_DEAD).

And this shouldn't be an issue after b441cf3f8c4b ("xfrm: delete x->tunnel as we delete x").

I think Sabrina is right. If the skb carries a secpath, UDP_SKB_IS_STATELESS is not set, and skb_release_head_state() will be called by skb_consume_udp().

skb_ext_put() does not clear skb->extensions nor ext->refcnt, if skb_attempt_defer_free() enters the slow path (kfree_skb_napi_cache()), the skb will go through again skb_release_head_state(), with a double free.

I think something alike the following (completely untested) should work:
---

net/ipv4/udp.c +0 -0
index 95241093b7f0..4a308fd6aa6c 100644
@@ -1851,8 +1851,10 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
                sk_peek_offset_bwd(sk, len);
 
Quoted 7 lines
Quoted 5 lines

net/ipv4/udp.c void skb_consume_udp(struct sock *sk, struct · line 1857

Quoted 5 lines
net/ipv4/udp.c +2 -1
        if (!skb_shared(skb)) {
-               if (unlikely(udp_skb_has_head_state(skb)))
+               if (unlikely(udp_skb_has_head_state(skb))) {
                        skb_release_head_state(skb);
+                       skb->active_extensions = 0;

We probably also want to clear CONNTRACK state as well.

Quoted 4 lines

Perhaps not use skb_release_head_state() ?

We know there is no dst, and no destructor.

An no conntrack either from UDP

net/ipv4/udp.c +0 -0
index 95241093b7f0..932c21838b9b 100644
@@ -1851,8 +1851,13 @@ void skb_consume_udp(struct sock *sk, struct

sk_buff *skb, int len)

                sk_peek_offset_bwd(sk, len);

        if (!skb_shared(skb)) {

- if (unlikely(udp_skb_has_head_state(skb)))
- skb_release_head_state(skb);
+ if (unlikely(udp_skb_has_head_state(skb))) {
+ /* Make sure that skb_release_head_state()
will have nothing to do. */

+                       DEBUG_NET_WARN_ON_ONCE(skb_dst(skb));
+                       DEBUG_NET_WARN_ON_ONCE(skb->destructor);
+                       DEBUG_NET_WARN_ON_ONCE(skb_nfct(skb));
+                       skb_ext_reset(skb);
+               }
                skb_attempt_defer_free(skb);
                return;
        }
maintainer
#8 lore raw
Quoted 22 lines · Eric Dumazet
Quoted 4 lines · Eric Dumazet

Perhaps not use skb_release_head_state() ?

We know there is no dst, and no destructor.

An no conntrack either from UDP

net/ipv4/udp.c +2 -2
index 95241093b7f0..932c21838b9b 100644
@@ -1851,8 +1851,13 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
                sk_peek_offset_bwd(sk, len);
 
        if (!skb_shared(skb)) {
-               if (unlikely(udp_skb_has_head_state(skb)))
-                       skb_release_head_state(skb);
+               if (unlikely(udp_skb_has_head_state(skb))) {
+                       /* Make sure that skb_release_head_state()

will have nothing to do. */

net/ipv4/udp.c

Quoted 8 lines
net/ipv4/udp.c +5 -0
+                       DEBUG_NET_WARN_ON_ONCE(skb_dst(skb));
+                       DEBUG_NET_WARN_ON_ONCE(skb->destructor);
+                       DEBUG_NET_WARN_ON_ONCE(skb_nfct(skb));
+                       skb_ext_reset(skb);
+               }
                skb_attempt_defer_free(skb);
                return;
        }

LGTM, thanks!

/P

Tested-by
#9 lore raw
In reply to Eric Dumazet
Quoted 14 lines · Eric Dumazet
net/ipv4/udp.c +2 -2
index 95241093b7f0..932c21838b9b 100644
@@ -1851,8 +1851,13 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
                sk_peek_offset_bwd(sk, len);
 
        if (!skb_shared(skb)) {
-               if (unlikely(udp_skb_has_head_state(skb)))
-                       skb_release_head_state(skb);
+               if (unlikely(udp_skb_has_head_state(skb))) {
+                       /* Make sure that skb_release_head_state()

will have nothing to do. */

net/ipv4/udp.c

Quoted 8 lines
net/ipv4/udp.c +5 -0
+                       DEBUG_NET_WARN_ON_ONCE(skb_dst(skb));
+                       DEBUG_NET_WARN_ON_ONCE(skb->destructor);
+                       DEBUG_NET_WARN_ON_ONCE(skb_nfct(skb));
+                       skb_ext_reset(skb);
+               }
                skb_attempt_defer_free(skb);
                return;
        }

Tested this version on my system (with DEBUG_NET enabled) and everything seems to work fine so far.

Michal

#10 lore raw
Quoted 28 lines · Michal Kubecek
Quoted 14 lines · Eric Dumazet
net/ipv4/udp.c +2 -2
index 95241093b7f0..932c21838b9b 100644
@@ -1851,8 +1851,13 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
                sk_peek_offset_bwd(sk, len);
 
        if (!skb_shared(skb)) {
-               if (unlikely(udp_skb_has_head_state(skb)))
-                       skb_release_head_state(skb);
+               if (unlikely(udp_skb_has_head_state(skb))) {
+                       /* Make sure that skb_release_head_state()

will have nothing to do. */

net/ipv4/udp.c

Quoted 8 lines
net/ipv4/udp.c +5 -0
+                       DEBUG_NET_WARN_ON_ONCE(skb_dst(skb));
+                       DEBUG_NET_WARN_ON_ONCE(skb->destructor);
+                       DEBUG_NET_WARN_ON_ONCE(skb_nfct(skb));
+                       skb_ext_reset(skb);
+               }
                skb_attempt_defer_free(skb);
                return;
        }

Tested this version on my system (with DEBUG_NET enabled) and everything seems to work fine so far.

Quoted 1 line

Thanks for testing. I will follow Sabrina suggestion and send :

net/ipv4/udp.c +0 -0
index 95241093b7f0..d66f273f9070 100644
@@ -1851,8 +1851,13 @@ void skb_consume_udp(struct sock *sk, struct

sk_buff *skb, int len)

                sk_peek_offset_bwd(sk, len);

        if (!skb_shared(skb)) {
-               if (unlikely(udp_skb_has_head_state(skb)))
-                       skb_release_head_state(skb);
+               /* Make sure that this skb has no dst, destructor
+                * or conntracking parts, because it might stay
+                * in a remote cpu list for a very long time.
+                */
+               DEBUG_NET_WARN_ON_ONCE(skb_dst(skb));
+               DEBUG_NET_WARN_ON_ONCE(skb->destructor);
+               DEBUG_NET_WARN_ON_ONCE(skb_nfct(skb));
                skb_attempt_defer_free(skb);
                return;
        }
Tested-by
#11 lore raw
Quoted 53 lines · Eric Dumazet
Quoted 28 lines · Michal Kubecek
Quoted 14 lines · Eric Dumazet
net/ipv4/udp.c +2 -2
index 95241093b7f0..932c21838b9b 100644
@@ -1851,8 +1851,13 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
                sk_peek_offset_bwd(sk, len);
 
        if (!skb_shared(skb)) {
-               if (unlikely(udp_skb_has_head_state(skb)))
-                       skb_release_head_state(skb);
+               if (unlikely(udp_skb_has_head_state(skb))) {
+                       /* Make sure that skb_release_head_state()

will have nothing to do. */

net/ipv4/udp.c

Quoted 8 lines
net/ipv4/udp.c +5 -0
+                       DEBUG_NET_WARN_ON_ONCE(skb_dst(skb));
+                       DEBUG_NET_WARN_ON_ONCE(skb->destructor);
+                       DEBUG_NET_WARN_ON_ONCE(skb_nfct(skb));
+                       skb_ext_reset(skb);
+               }
                skb_attempt_defer_free(skb);
                return;
        }

Tested this version on my system (with DEBUG_NET enabled) and everything seems to work fine so far.

Quoted 1 line

Thanks for testing. I will follow Sabrina suggestion and send :

net/ipv4/udp.c +7 -2
index 95241093b7f0..d66f273f9070 100644
@@ -1851,8 +1851,13 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
                sk_peek_offset_bwd(sk, len);
 
        if (!skb_shared(skb)) {
-               if (unlikely(udp_skb_has_head_state(skb)))
-                       skb_release_head_state(skb);
+               /* Make sure that this skb has no dst, destructor
+                * or conntracking parts, because it might stay
+                * in a remote cpu list for a very long time.
+                */
+               DEBUG_NET_WARN_ON_ONCE(skb_dst(skb));
+               DEBUG_NET_WARN_ON_ONCE(skb->destructor);
+               DEBUG_NET_WARN_ON_ONCE(skb_nfct(skb));
                skb_attempt_defer_free(skb);
                return;
Quoted 1 line
        }

Running 6.18-rc1 with this patch right now, everything seems to work as well. Thanks to everyone for quick response.

Michal

#12 lore raw
In reply to Eric Dumazet
Quoted 11 lines · Eric Dumazet

Thanks for testing. I will follow Sabrina suggestion and send :

net/ipv4/udp.c +0 -0
index 95241093b7f0..d66f273f9070 100644
@@ -1851,8 +1851,13 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
                sk_peek_offset_bwd(sk, len);
 
        if (!skb_shared(skb)) {

net/ipv4/udp.c void skb_consume_udp(struct sock *sk, struct · line 1862

Quoted 10 lines
net/ipv4/udp.c +7 -2
-               if (unlikely(udp_skb_has_head_state(skb)))
-                       skb_release_head_state(skb);
+               /* Make sure that this skb has no dst, destructor
+                * or conntracking parts, because it might stay
+                * in a remote cpu list for a very long time.
+                */
+               DEBUG_NET_WARN_ON_ONCE(skb_dst(skb));
+               DEBUG_NET_WARN_ON_ONCE(skb->destructor);
+               DEBUG_NET_WARN_ON_ONCE(skb_nfct(skb));
                skb_attempt_defer_free(skb);

Are there cases where we expect to pass skb which violate this to skb_attempt_defer_free()?

If no, then maybe move existing checks in skb_attempt_defer_free() up and apped the skb_nfct check there so syzbot can blame other offenders too.

#13 lore raw
Quoted 12 lines · Florian Westphal
Quoted 10 lines · Eric Dumazet

Thanks for testing. I will follow Sabrina suggestion and send :

net/ipv4/udp.c +0 -0
index 95241093b7f0..d66f273f9070 100644
@@ -1851,8 +1851,13 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
                sk_peek_offset_bwd(sk, len);
 
Quoted 14 lines

net/ipv4/udp.c void skb_consume_udp(struct sock *sk, struct · line 1862

Quoted 11 lines
net/ipv4/udp.c +7 -2
        if (!skb_shared(skb)) {
-               if (unlikely(udp_skb_has_head_state(skb)))
-                       skb_release_head_state(skb);
+               /* Make sure that this skb has no dst, destructor
+                * or conntracking parts, because it might stay
+                * in a remote cpu list for a very long time.
+                */
+               DEBUG_NET_WARN_ON_ONCE(skb_dst(skb));
+               DEBUG_NET_WARN_ON_ONCE(skb->destructor);
+               DEBUG_NET_WARN_ON_ONCE(skb_nfct(skb));
                skb_attempt_defer_free(skb);

Are there cases where we expect to pass skb which violate this to skb_attempt_defer_free()?

So far UDP makes sure these fields are cleared.

Quoted 3 lines

If no, then maybe move existing checks in skb_attempt_defer_free() up and apped the skb_nfct check there so syzbot can blame other offenders too.

You are right, I forgot skb_attempt_defer_free() already had:

commit e8e1ce8454c9cc8ad2e4422bef346428e52455e3

Author: Eric Dumazet <[email protected]>
Date:   Fri Apr 21 09:43:53 2023 +0000

net: add debugging checks in skb_attempt_defer_free()

Make sure skbs that are stored in softnet_data.defer_list do not have a dst attached.

Also make sure the the skb was orphaned.

net/core/skbuff.c +3 -0
index 0d998806b377..bd815a00d2af 100644
@@ -6881,6 +6881,9 @@ nodefer:  __kfree_skb(skb);
                return;
        }
 
+       DEBUG_NET_WARN_ON_ONCE(skb_dst(skb));
+       DEBUG_NET_WARN_ON_ONCE(skb->destructor);
+
        sd = &per_cpu(softnet_data, cpu);
        defer_max = READ_ONCE(sysctl_skb_defer_max);
        if (READ_ONCE(sd->defer_count) >= defer_max)
#14 lore raw
In reply to Eric Dumazet
Quoted 50 lines · Eric Dumazet
Quoted 41 lines · Eric Dumazet
Quoted 39 lines · Eric Dumazet
Quoted 37 lines · Paolo Abeni
Quoted 7 lines · Sabrina Dubroca
Quoted 5 lines · Eric Dumazet

Michal reported and bisected an issue after recent adoption of skb_attempt_defer_free() in UDP.

We had the same issue for TCP, that Sabrina fixed in commit 9b6412e6979f ("tcp: drop secpath at the same time as we currently drop dst")

Quoted 7 lines

I'm not convinced this is the same bug. The TCP one was a "leaked" reference (delayed put). This looks more like a double put/missing hold to me (we get to the destroy path without having done the proper delete, which would set XFRM_STATE_DEAD).

And this shouldn't be an issue after b441cf3f8c4b ("xfrm: delete x->tunnel as we delete x").

I think Sabrina is right. If the skb carries a secpath, UDP_SKB_IS_STATELESS is not set, and skb_release_head_state() will be called by skb_consume_udp().

skb_ext_put() does not clear skb->extensions nor ext->refcnt, if skb_attempt_defer_free() enters the slow path (kfree_skb_napi_cache()), the skb will go through again skb_release_head_state(), with a double free.

I think something alike the following (completely untested) should work:
---

net/ipv4/udp.c +0 -0
index 95241093b7f0..4a308fd6aa6c 100644
@@ -1851,8 +1851,10 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
                sk_peek_offset_bwd(sk, len);
 
Quoted 7 lines
Quoted 5 lines

net/ipv4/udp.c void skb_consume_udp(struct sock *sk, struct · line 1857

Quoted 5 lines
net/ipv4/udp.c +2 -1
        if (!skb_shared(skb)) {
-               if (unlikely(udp_skb_has_head_state(skb)))
+               if (unlikely(udp_skb_has_head_state(skb))) {
                        skb_release_head_state(skb);
+                       skb->active_extensions = 0;

We probably also want to clear CONNTRACK state as well.

Quoted 3 lines

Perhaps not use skb_release_head_state() ?

We know there is no dst, and no destructor.

Then, do we need to do anything before calling skb_attempt_defer_free()? skb_attempt_defer_free() only wants no dst and no destructor, and the secpath issue that we dealt with in TCP is not a problem anymore.

Can we just drop the udp_skb_has_head_state() special handling and simply call skb_attempt_defer_free()?

Quoted 18 lines
net/ipv4/udp.c +4 -2
index 95241093b7f0..98628486c4c5 100644
@@ -1851,8 +1851,10 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
                sk_peek_offset_bwd(sk, len);
 
        if (!skb_shared(skb)) {
-               if (unlikely(udp_skb_has_head_state(skb)))
-                       skb_release_head_state(skb);
+               if (unlikely(udp_skb_has_head_state(skb))) {
+                       nf_reset_ct(skb);
+                       skb_ext_reset(skb);
+               }
                skb_attempt_defer_free(skb);
                return;
        }
Signature · 1 line
Sabrina
#15 lore raw
Quoted 54 lines · Sabrina Dubroca
Quoted 52 lines · Eric Dumazet
Quoted 41 lines · Eric Dumazet
Quoted 39 lines · Eric Dumazet
Quoted 37 lines · Paolo Abeni
Quoted 7 lines · Sabrina Dubroca
Quoted 5 lines · Eric Dumazet

Michal reported and bisected an issue after recent adoption of skb_attempt_defer_free() in UDP.

We had the same issue for TCP, that Sabrina fixed in commit 9b6412e6979f ("tcp: drop secpath at the same time as we currently drop dst")

Quoted 7 lines

I'm not convinced this is the same bug. The TCP one was a "leaked" reference (delayed put). This looks more like a double put/missing hold to me (we get to the destroy path without having done the proper delete, which would set XFRM_STATE_DEAD).

And this shouldn't be an issue after b441cf3f8c4b ("xfrm: delete x->tunnel as we delete x").

I think Sabrina is right. If the skb carries a secpath, UDP_SKB_IS_STATELESS is not set, and skb_release_head_state() will be called by skb_consume_udp().

skb_ext_put() does not clear skb->extensions nor ext->refcnt, if skb_attempt_defer_free() enters the slow path (kfree_skb_napi_cache()), the skb will go through again skb_release_head_state(), with a double free.

I think something alike the following (completely untested) should work:
---

net/ipv4/udp.c +0 -0
index 95241093b7f0..4a308fd6aa6c 100644
@@ -1851,8 +1851,10 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
                sk_peek_offset_bwd(sk, len);
 
Quoted 7 lines
Quoted 5 lines

net/ipv4/udp.c void skb_consume_udp(struct sock *sk, struct · line 1857

Quoted 5 lines
net/ipv4/udp.c +2 -1
        if (!skb_shared(skb)) {
-               if (unlikely(udp_skb_has_head_state(skb)))
+               if (unlikely(udp_skb_has_head_state(skb))) {
                        skb_release_head_state(skb);
+                       skb->active_extensions = 0;

We probably also want to clear CONNTRACK state as well.

Perhaps not use skb_release_head_state() ?

Quoted 8 lines
Quoted 1 line

We know there is no dst, and no destructor.

Then, do we need to do anything before calling skb_attempt_defer_free()? skb_attempt_defer_free() only wants no dst and no destructor, and the secpath issue that we dealt with in TCP is not a problem anymore.

Can we just drop the udp_skb_has_head_state() special handling and simply call skb_attempt_defer_free()?

Good point ! I need a second cup of coffee !

#16 lore raw
In reply to Paolo Abeni
Quoted 18 lines · Paolo Abeni
Quoted 7 lines · Sabrina Dubroca
Quoted 5 lines · Eric Dumazet

Michal reported and bisected an issue after recent adoption of skb_attempt_defer_free() in UDP.

We had the same issue for TCP, that Sabrina fixed in commit 9b6412e6979f ("tcp: drop secpath at the same time as we currently drop dst")

Quoted 7 lines

I'm not convinced this is the same bug. The TCP one was a "leaked" reference (delayed put). This looks more like a double put/missing hold to me (we get to the destroy path without having done the proper delete, which would set XFRM_STATE_DEAD).

And this shouldn't be an issue after b441cf3f8c4b ("xfrm: delete x->tunnel as we delete x").

Quoted 7 lines

I think Sabrina is right. If the skb carries a secpath, UDP_SKB_IS_STATELESS is not set, and skb_release_head_state() will be called by skb_consume_udp().

skb_ext_put() does not clear skb->extensions nor ext->refcnt, if skb_attempt_defer_free() enters the slow path (kfree_skb_napi_cache()), the skb will go through again skb_release_head_state(), with a double free.

Thanks Paolo, I was going down the same path, you got there faster :)

Quoted 19 lines

I think something alike the following (completely untested) should work:
---

net/ipv4/udp.c +3 -1
index 95241093b7f0..4a308fd6aa6c 100644
@@ -1851,8 +1851,10 @@ void skb_consume_udp(struct sock *sk, struct
 sk_buff *skb, int len)
 		sk_peek_offset_bwd(sk, len);
 
 	if (!skb_shared(skb)) {
-		if (unlikely(udp_skb_has_head_state(skb)))
+		if (unlikely(udp_skb_has_head_state(skb))) {
 			skb_release_head_state(skb);
+			skb->active_extensions = 0;
+		}
 		skb_attempt_defer_free(skb);
 		return;

}

Signature · 1 line
Sabrina