Re: [PATCH] net: initialize sk_peer_lock in cloned sockets

From: Eric Dumazet

Date: Wed Nov 18 2026 - 04:01:08 EST


Le mer. 18 nov. 2026 à 09:49, Henry Martin <bsdhenrymartin@xxxxxxxxx> a écrit :
>
> sock_copy() clones a struct sock with two memcpys that both include
> sk_peer_lock. sk_clone() re-initializes sk_lock, but nothing resets
> sk_peer_lock, so a clone created while the parent's sk_peer_lock is held
> inherits the lock in the locked state.
>
> The first getsockopt(SO_PEERCRED) on such a child spins forever in
> queued_spin_lock_slowpath(). spin_lock() disables preemption, so the
> task never schedules and its CPU stops reporting quiescent states; RCU
> grace periods then stop and callers of synchronize_rcu(), such as
> expand_files() from accept(), hang.
>
> Initialize sk_peer_lock explicitly for the clone.
>
> This issue was discovered by Tencent CodeBuddy Security.
>
> Fixes: 35306eb23814 ("af_unix: fix races in sk_peer_pid and sk_peer_cred accesses")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Henry Martin <bsdhenrymartin@xxxxxxxxx>
> ---
> net/core/sock.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/net/core/sock.c b/net/core/sock.c
> index ad414878dc5b..b5e2a2a7741c 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c
> @@ -2545,6 +2545,10 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,
>
> sk_node_init(&newsk->sk_node);
> sock_lock_init(newsk);
> + /* sock_copy() copies the parent's sk_peer_lock verbatim, including a
> + * held state, so the clone must start with a fresh lock.
> + */
> + spin_lock_init(&newsk->sk_peer_lock);

The comment is not needed, the changelog already explains this.

Also, please use [PATCH net] in the subject for the next version
(after a 24 hours delay)

With these addressed:

Reviewed-by: Eric Dumazet <edumazet@xxxxxxxxxx>

Two side notes, nothing to change in your patch:

sk_stamp_seq has the same problem on 32-bit kernels. SCTP can clone
a socket while SIOCGSTAMP holds the seqlock in sock_gettstamp().
We will send a separate fix for this one.

For net-next, I am considering to restrict SO_PEERCRED, SO_PEERGROUPS and
SO_PEERPIDFD to the families that actually set peer credentials
(AF_UNIX and bluetooth), so that other sockets no longer touch
sk_peer_lock at all.