* [PATCH] net: initialize sk_peer_lock in cloned sockets
@ 2026-11-18 8:49 Henry Martin
2026-11-18 8:52 ` netdev-bot+sinfo
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Henry Martin @ 2026-11-18 8:49 UTC (permalink / raw)
To: Eric Dumazet, Kuniyuki Iwashima, Paolo Abeni, Willem de Bruijn,
David S . Miller, Jakub Kicinski, Simon Horman
Cc: netdev, linux-kernel, Henry Martin, stable
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@vger.kernel.org
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
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);
if (lock)
bh_lock_sock(newsk);
--
2.43.7
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] net: initialize sk_peer_lock in cloned sockets
2026-11-18 8:49 [PATCH] net: initialize sk_peer_lock in cloned sockets Henry Martin
@ 2026-11-18 8:52 ` netdev-bot+sinfo
2026-11-18 9:00 ` Eric Dumazet
2026-11-19 23:50 ` netdev-bot+sashiko
2 siblings, 0 replies; 4+ messages in thread
From: netdev-bot+sinfo @ 2026-11-18 8:52 UTC (permalink / raw)
To: Henry Martin
Cc: Eric Dumazet, Kuniyuki Iwashima, Paolo Abeni, Willem de Bruijn,
David S . Miller, Jakub Kicinski, Simon Horman, netdev,
linux-kernel, stable
Hi!
This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:
- Whether the issue was actually triggered, or is only theoretical
(e.g. found by LLM and not proven at runtime). If it was triggered
please include the symptoms, like the stack trace or kernel messages.
Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
--
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] net: initialize sk_peer_lock in cloned sockets
2026-11-18 8:49 [PATCH] net: initialize sk_peer_lock in cloned sockets Henry Martin
2026-11-18 8:52 ` netdev-bot+sinfo
@ 2026-11-18 9:00 ` Eric Dumazet
2026-11-19 23:50 ` netdev-bot+sashiko
2 siblings, 0 replies; 4+ messages in thread
From: Eric Dumazet @ 2026-11-18 9:00 UTC (permalink / raw)
To: Henry Martin
Cc: Kuniyuki Iwashima, Paolo Abeni, Willem de Bruijn,
David S . Miller, Jakub Kicinski, Simon Horman, netdev,
linux-kernel, stable
Le mer. 18 nov. 2026 à 09:49, Henry Martin <bsdhenrymartin@gmail.com> 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@vger.kernel.org
> Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
> ---
> 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@kernel.org>
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.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] net: initialize sk_peer_lock in cloned sockets
2026-11-18 8:49 [PATCH] net: initialize sk_peer_lock in cloned sockets Henry Martin
2026-11-18 8:52 ` netdev-bot+sinfo
2026-11-18 9:00 ` Eric Dumazet
@ 2026-11-19 23:50 ` netdev-bot+sashiko
2 siblings, 0 replies; 4+ messages in thread
From: netdev-bot+sashiko @ 2026-11-19 23:50 UTC (permalink / raw)
To: bsdhenrymartin
Cc: edumazet, kuniyu, pabeni, willemb, davem, kuba, horms, netdev,
linux-kernel, stable
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The fix is incomplete: on 32-bit kernels, sk_clone() still
inherits sk->sk_stamp_seq in a held state.
- [Low] The commit message says sock_copy() clones a struct sock 'with
two memcpys that both include sk_peer_lock'.
--
Here is the LKML reply:
net: initialize sk_peer_lock in cloned sockets
This commit adds a spin_lock_init() of sk_peer_lock in sk_clone(). Without
it, a clone made while the parent's sk_peer_lock is held starts with the
lock already locked, and the first getsockopt(SO_PEERCRED) on the child
hangs.
> sock_copy() clones a struct sock with two memcpys that both include
> sk_peer_lock. sk_clone() re-initializes sk_lock, but nothing resets
[Severity: Low]
Is the "two memcpys that both include sk_peer_lock" part accurate? The
first copy in sock_copy() stops at sk_dontcopy_begin, which sits inside
__sk_common:
net/core/sock.c:sock_copy() {
...
memcpy(nsk, osk, offsetof(struct sock, sk_dontcopy_begin));
unsafe_memcpy(&nsk->sk_dontcopy_end, &osk->sk_dontcopy_end,
prot->obj_size - offsetof(struct sock, sk_dontcopy_end),
/* alloc is larger than struct, see sk_prot_alloc() */);
...
}
sk_peer_lock is a field in the struct sock body, well after __sk_common.
So only the second unsafe_memcpy() copies it.
The conclusion that the lock is copied verbatim still holds. Could the
commit message describe the mechanism more precisely?
> diff --git a/net/core/sock.c b/net/core/sock.c
> index ad414878dc5ba..b5e2a2a7741cd 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);
[Severity: High]
Does the same problem remain for sk_stamp_seq on 32-bit kernels?
sk_stamp_seq is a seqlock_t (a spinlock plus a seqcount) declared under
BITS_PER_LONG==32 in struct sock:
include/net/sock.h:
#if BITS_PER_LONG==32
seqlock_t sk_stamp_seq;
#endif
It lies outside the sk_dontcopy range, so sock_copy() copies it along with
sk_peer_lock. It is only initialized by seqlock_init() in
sock_init_data_uid(), which the clone path never runs. Neither sk_clone()
nor sk_init_common() resets it.
The writer takes the spinlock and leaves the sequence count odd:
include/net/sock.h:sock_write_timestamp() {
write_seqlock(&sk->sk_stamp_seq);
sk->sk_stamp = kt;
write_sequnlock(&sk->sk_stamp_seq);
}
sock_gettstamp() (ioctl SIOCGSTAMP) calls that writer without lock_sock
whenever sk_stamp is 0:
net/core/sock.c:sock_gettstamp() {
...
if (ts.tv_sec == 0) {
ktime_t kt = ktime_get_real();
sock_write_timestamp(sk, kt);
...
}
On an SCTP one-to-many socket, sk_stamp becomes 0 after the first
sctp_recvmsg()->sock_recv_cmsgs(). With that state, can the following
happen?
CPU0
ioctl(SIOCGSTAMP)
sock_gettstamp()
sock_write_timestamp()
write_seqlock(&parent->sk_stamp_seq)
CPU1
getsockopt(SCTP_SOCKOPT_PEELOFF)
sctp_getsockopt() /* lock_sock() held */
sctp_do_peeloff()
sctp_clone_sock()
sk_clone()
sock_copy()
The lock_sock() taken on CPU1 does not exclude CPU0's lockless write. The
child would then get a locked spinlock and an odd seqcount.
After that, sock_read_timestamp() on the child would spin forever in
read_seqbegin(). That covers SIOCGSTAMP, and recvmsg via
sock_recv_cmsgs(). On PREEMPT_RT it would block on the copied held lock
instead.
Any sock_write_timestamp() on the child would spin forever in spin_lock()
with preemption disabled. That is the same RCU-stall hang the commit
message describes for sk_peer_lock.
Two threads of an unprivileged user could trigger this. Would it make
sense to also add a seqlock_init(&newsk->sk_stamp_seq) under
BITS_PER_LONG==32 next to the new spin_lock_init()? Another option is to
move all lock re-initialization for clones into one helper.
>
> if (lock)
> bh_lock_sock(newsk);
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118084927.1756747-1-bsdhenrymartin%40gmail.com
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-19 23:50 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 8:49 [PATCH] net: initialize sk_peer_lock in cloned sockets Henry Martin
2026-11-18 8:52 ` netdev-bot+sinfo
2026-11-18 9:00 ` Eric Dumazet
2026-11-19 23:50 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®