Skip to contentSkip to conversation

net: Fix two issues in sk_clone() error path.

PATCH Applied Kuniyuki Iwashima 5 participants 9 messages Jul 9 – Jul 21

Subsystems (3)
lore
Share & tools
lore

Kuniyuki Iwashima

author maintainer
#0 lore raw

cover net: Fix two issues in sk_clone() error path.

Sashiko reported issues in the sk_clone() error path.

https://lore.kernel.org/bpf/[email protected]/

This series fixes them.

Note that Sashiko may point out the same issue for sk_bpf_storage, but the fix was already merged in the bpf tree:

7cbd0c4cebe4 ("bpf: Fix UAF in sock clone early bailouts")

Kuniyuki Iwashima (2):
soreuseport: Clear sk_reuseport_cb before failure in sk_clone().
net: Call net_enable_timestamp() before failure in sk_clone().

 net/core/sock.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Signature · 1 line
2.55.0.795.g602f6c329a-goog
author maintainer
#1 lore raw

PATCH 1/2 soreuseport: Clear sk_reuseport_cb before failure in sk_clone().

When sk_clone() fails, sk_destruct() is called for the new socket.

If the parent socket has sk->sk_reuseport_cb, the child will call reuseport_detach_sock() for the reuseport group.

Let's clear sk->sk_reuseport_cb before any failure path in sk_clone().

Note that this was not a problem before the cited commit because reuseport_detach_sock() did nothing if the socket was not found in the reuseport array.

Fixes 5dc4c4b7d4e8 ("bpf: Introduce BPF_MAP_TYPE_REUSEPORT_SOCKARRAY")
---
 net/core/sock.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/sock.c +2 -2
index 8a59bfaa8096..fc3ff0552d68 100644
@@ -2544,6 +2544,8 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,
 
 	cgroup_sk_clone(&newsk->sk_cgrp_data);
 
+	RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
+
 	rcu_read_lock();
 	filter = rcu_dereference(sk->sk_filter);
 	if (filter != NULL)
@@ -2566,8 +2568,6 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,
 		goto free;
 	}
 
-	RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
-
 	if (bpf_sk_storage_clone(sk, newsk))
 		goto free;
 
Signature · 1 line
2.55.0.795.g602f6c329a-goog
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH v1 net 1/2] soreuseport: Clear sk_reuseport_cb before failure in sk_clone().

Quoted 7 lines · Kuniyuki Iwashima

When sk_clone() fails, sk_destruct() is called for the new socket.

If the parent socket has sk->sk_reuseport_cb, the child will call reuseport_detach_sock() for the reuseport group.

Let's clear sk->sk_reuseport_cb before any failure path in sk_clone().

Quoted 8 lines

Note that this was not a problem before the cited commit because reuseport_detach_sock() did nothing if the socket was not found in the reuseport array.

Fixes 5dc4c4b7d4e8 ("bpf: Introduce BPF_MAP_TYPE_REUSEPORT_SOCKARRAY")
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH v1 net 1/2] soreuseport: Clear sk_reuseport_cb before failure in sk_clone().

In reply to Kuniyuki Iwashima
Quoted 8 lines · Kuniyuki Iwashima

When sk_clone() fails, sk_destruct() is called for the new socket.

If the parent socket has sk->sk_reuseport_cb, the child will call reuseport_detach_sock() for the reuseport group.

Let's clear sk->sk_reuseport_cb before any failure path in sk_clone().

Quoted 8 lines

Note that this was not a problem before the cited commit because reuseport_detach_sock() did nothing if the socket was not found in the reuseport array.

Fixes 5dc4c4b7d4e8 ("bpf: Introduce BPF_MAP_TYPE_REUSEPORT_SOCKARRAY")
Quoted 30 lines
---
 net/core/sock.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/sock.c +2 -3
index 8a59bfaa8096..fc3ff0552d68 100644
@@ -2544,6 +2544,8 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,
 
        cgroup_sk_clone(&newsk->sk_cgrp_data);
 
+       RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
+
        rcu_read_lock();
        filter = rcu_dereference(sk->sk_filter);
        if (filter != NULL)
@@ -2566,8 +2568,6 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,
                goto free;
        }
 
-       RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
-
        if (bpf_sk_storage_clone(sk, newsk))
                goto free;
 
--

2.55.0.795.g602f6c329a-goog

author maintainer
#4 lore raw

PATCH 2/2 net: Call net_enable_timestamp() before failure in sk_clone().

In reply to Kuniyuki Iwashima

When sk_clone() fails, sk_destruct() is called for the new socket.

If the parent socket has SK_FLAGS_TIMESTAMP in sk->sk_flags, net_disable_timestamp() is called for the child socket even though net_enable_timestamp() is not called for it.

Let's call net_enable_timestamp() before any failure path in sk_clone().

Fixes 704da560c0a0 ("tcp: update the netstamp_needed counter when cloning sockets")
---
 net/core/sock.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/core/sock.c +3 -3
index fc3ff0552d68..504d82a3aacd 100644
@@ -2546,6 +2546,9 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,
 
 	RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
 
+	if (sock_needs_netstamp(sk) && newsk->sk_flags & SK_FLAGS_TIMESTAMP)
+		net_enable_timestamp();
+
 	rcu_read_lock();
 	filter = rcu_dereference(sk->sk_filter);
 	if (filter != NULL)
@@ -2595,9 +2598,6 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,
 
 	if (newsk->sk_prot->sockets_allocated)
 		sk_sockets_allocated_inc(newsk);
-
-	if (sock_needs_netstamp(sk) && newsk->sk_flags & SK_FLAGS_TIMESTAMP)
-		net_enable_timestamp();
 out:
 	return newsk;
 free:
Signature · 1 line
2.55.0.795.g602f6c329a-goog
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH v1 net 2/2] net: Call net_enable_timestamp() before failure in sk_clone().

Quoted 6 lines · Kuniyuki Iwashima

When sk_clone() fails, sk_destruct() is called for the new socket.

If the parent socket has SK_FLAGS_TIMESTAMP in sk->sk_flags, net_disable_timestamp() is called for the child socket even though net_enable_timestamp() is not called for it.

Quoted 7 lines

Let's call net_enable_timestamp() before any failure path in sk_clone().

Fixes 704da560c0a0 ("tcp: update the netstamp_needed counter when cloning sockets")
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH v1 net 2/2] net: Call net_enable_timestamp() before failure in sk_clone().

In reply to Kuniyuki Iwashima
Quoted 7 lines · Kuniyuki Iwashima

When sk_clone() fails, sk_destruct() is called for the new socket.

If the parent socket has SK_FLAGS_TIMESTAMP in sk->sk_flags, net_disable_timestamp() is called for the child socket even though net_enable_timestamp() is not called for it.

Quoted 7 lines

Let's call net_enable_timestamp() before any failure path in sk_clone().

Fixes 704da560c0a0 ("tcp: update the netstamp_needed counter when cloning sockets")
Quoted 32 lines
---
 net/core/sock.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/core/sock.c +3 -4
index fc3ff0552d68..504d82a3aacd 100644
@@ -2546,6 +2546,9 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,
 
        RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
 
+       if (sock_needs_netstamp(sk) && newsk->sk_flags & SK_FLAGS_TIMESTAMP)
+               net_enable_timestamp();
+
        rcu_read_lock();
        filter = rcu_dereference(sk->sk_filter);
        if (filter != NULL)
@@ -2595,9 +2598,6 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,
 
        if (newsk->sk_prot->sockets_allocated)
                sk_sockets_allocated_inc(newsk);
-
-       if (sock_needs_netstamp(sk) && newsk->sk_flags & SK_FLAGS_TIMESTAMP)
-               net_enable_timestamp();
 out:
        return newsk;
 free:
--

2.55.0.795.g602f6c329a-goog

reviewer Reviewed-by on the v1 series
#7 lore raw
In reply to Kuniyuki Iwashima
Quoted 9 lines · Kuniyuki Iwashima

Sashiko reported issues in the sk_clone() error path.

https://lore.kernel.org/bpf/[email protected]/

This series fixes them.

Note that Sashiko may point out the same issue for sk_bpf_storage, but the fix was already merged in the bpf tree:

Quoted 6 lines

7cbd0c4cebe4 ("bpf: Fix UAF in sock clone early bailouts")

Kuniyuki Iwashima (2):
soreuseport: Clear sk_reuseport_cb before failure in sk_clone().
net: Call net_enable_timestamp() before failure in sk_clone().

For the series:

#8 lore raw
In reply to Kuniyuki Iwashima
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Thu, 9 Jul 2026 18:31:38 +0000 you wrote:

Sashiko reported issues in the sk_clone() error path.

https://lore.kernel.org/bpf/[email protected]/

This series fixes them.

Note that Sashiko may point out the same issue for sk_bpf_storage, but the fix was already merged in the bpf tree:

[...]

Here is the summary with links:
- [v1,net,1/2] soreuseport: Clear sk_reuseport_cb before failure in sk_clone().

    https://git.kernel.org/netdev/net/c/98da8ce87dd5
  - [v1,net,2/2] net: Call net_enable_timestamp() before failure in sk_clone().
    https://git.kernel.org/netdev/net/c/d50557779257

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html