tcp: fix data-race in tcp_recv_should_stop
Subsystems (3)
PATCH v2 1/1 tcp: fix data-race in tcp_recv_should_stop
BUG: KCSAN: data-race in do_recvmmsg / mptcp_recvmsg
read-write (marked) to 0xffff8880134d391c of 4 bytes by task 2619 on cpu 1:
instrument_atomic_read_write include/linux/instrumented.h:113 [inline] sock_error include/net/sock.h:2565 [inline] do_recvmmsg+0x50c/0x580 net/socket.c:3049 __sys_recvmmsg net/socket.c:3144 [inline] __do_sys_recvmmsg net/socket.c:3167 [inline] __se_sys_recvmmsg net/socket.c:3160 [inline] __x64_sys_recvmmsg+0x161/0x180 net/socket.c:3160 x64_sys_call+0x19c7/0x1ca0 arch/x86/include/generated/asm/syscalls_64.h:300 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline] do_syscall_64+0xde/0x3d0 arch/x86/entry/syscall_64.c:84 entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffff8880134d391c of 4 bytes by task 2620 on cpu 0:
tcp_recv_should_stop include/net/tcp.h:3086 [inline] mptcp_recvmsg+0x54d/0xd50 net/mptcp/protocol.c:2466 inet_recvmsg+0x204/0x210 net/ipv4/af_inet.c:894 sock_recvmsg_nosec net/socket.c:1151 [inline] sock_recvmsg+0x11a/0x140 net/socket.c:1173 ____sys_recvmsg+0x14b/0x3c0 net/socket.c:2933 ___sys_recvmsg+0x116/0x160 net/socket.c:2975 __sys_recvmsg net/socket.c:3008 [inline] __do_sys_recvmsg net/socket.c:3014 [inline] __se_sys_recvmsg net/socket.c:3011 [inline] __x64_sys_recvmsg+0xeb/0x160 net/socket.c:3011 x64_sys_call+0x1319/0x1ca0 arch/x86/include/generated/asm/syscalls_64.h:48 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline] do_syscall_64+0xde/0x3d0 arch/x86/entry/syscall_64.c:84 entry_SYSCALL_64_after_hwframe+0x77/0x7f
value changed: 0x0000006b -> 0x00000000
Reported by Kernel Concurrency Sanitizer on:
CPU: 0 UID: 0 PID: 2620 Comm: syz.2.33 Not tainted 7.2.0-g39d4f32c5d53 #76 PREEMPT(full) Hardware name: QEMU Ubuntu 26.04 PC (i440FX + PIIX, 1996), BIOS 1.17.0-debian-1.17.0-1ubuntu1 04/01/2014
do_recvmmsg() calls sock_error() without the socket lock. sock_error() clears sk_err with xchg(), which races with the unmarked load in tcp_recv_should_stop(). The same race exists for plain TCP with concurrent recvmmsg() readers; KCSAN reported it on an MPTCP socket because mptcp_recvmsg() uses this helper.
Use READ_ONCE() for the lockless peek. No extra ordering is needed:
the value is only used to decide whether receiving should stop. This does not consume sk_err; the check-then-sock_error() TOCTOU on the no-data paths is a separate issue.
---
---
Changes in v2:
- add Reported-by and Closes for the MPTCP syzkaller report
- Link to v1: https://patch.msgid.link/[email protected]
--- include/net/tcp.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/net/tcp.h +2 -1
@@ -3082,7 +3082,8 @@ enum skb_drop_reason tcp_inbound_hash(struct sock *sk,static inline int tcp_recv_should_stop (struct sock *sk) { -return sk->sk_err || +/* sk_err can be cleared locklessly by sock_error(). */ +return READ_ONCE (sk->sk_err) || sk->sk_state ==TCP_CLOSE || (sk->sk_shutdown &RCV_SHUTDOWN ) ||signal_pending (current);
---
base-commit: 38b6be101006d3e7af972999f45d4f1e8250587a
change-id: 20260908-mptcp-sk-err-net-7ff88ef05044
Best regards,
Signature · 1 line
Quanye Yang <[email protected]>
Quoted 46 lines · On 9/9/26 10:05 AM, Quanye Yang via B4 Relay wrote:
From: Quanye Yang <[email protected]>
BUG: KCSAN: data-race in do_recvmmsg / mptcp_recvmsgread-write (marked) to
0xffff8880134d391cof 4 bytes by task 2619 on cpu 1:instrument_atomic_read_write include/linux/instrumented.h:113 [inline] sock_error include/net/sock.h:2565 [inline] do_recvmmsg+0x50c/0x580 net/socket.c:3049 __sys_recvmmsg net/socket.c:3144 [inline] __do_sys_recvmmsg net/socket.c:3167 [inline] __se_sys_recvmmsg net/socket.c:3160 [inline] __x64_sys_recvmmsg+0x161/0x180 net/socket.c:3160 x64_sys_call+0x19c7/0x1ca0 arch/x86/include/generated/asm/syscalls_64.h:300 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline] do_syscall_64+0xde/0x3d0 arch/x86/entry/syscall_64.c:84 entry_SYSCALL_64_after_hwframe+0x77/0x7fread to
0xffff8880134d391cof 4 bytes by task 2620 on cpu 0:tcp_recv_should_stop include/net/tcp.h:3086 [inline] mptcp_recvmsg+0x54d/0xd50 net/mptcp/protocol.c:2466 inet_recvmsg+0x204/0x210 net/ipv4/af_inet.c:894 sock_recvmsg_nosec net/socket.c:1151 [inline] sock_recvmsg+0x11a/0x140 net/socket.c:1173 ____sys_recvmsg+0x14b/0x3c0 net/socket.c:2933 ___sys_recvmsg+0x116/0x160 net/socket.c:2975 __sys_recvmsg net/socket.c:3008 [inline] __do_sys_recvmsg net/socket.c:3014 [inline] __se_sys_recvmsg net/socket.c:3011 [inline] __x64_sys_recvmsg+0xeb/0x160 net/socket.c:3011 x64_sys_call+0x1319/0x1ca0 arch/x86/include/generated/asm/syscalls_64.h:48 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline] do_syscall_64+0xde/0x3d0 arch/x86/entry/syscall_64.c:84 entry_SYSCALL_64_after_hwframe+0x77/0x7fvalue changed:
0x0000006b->0x00000000Reported by Kernel Concurrency Sanitizer on:
CPU: 0 UID: 0 PID: 2620 Comm: syz.2.33 Not tainted 7.2.0-g39d4f32c5d53 #76 PREEMPT(full) Hardware name: QEMU Ubuntu 26.04 PC (i440FX + PIIX, 1996), BIOS 1.17.0-debian-1.17.0-1ubuntu1 04/01/2014
do_recvmmsg()callssock_error()without the socket lock.sock_error()clearssk_errwithxchg(), which races with the unmarked load intcp_recv_should_stop(). The same race exists for plain TCP with concurrentrecvmmsg()readers; KCSAN reported it on an MPTCP socket becausemptcp_recvmsg()uses this helper.
Quoted 6 lines
Use
READ_ONCE()for the lockless peek. No extra ordering is needed:
the value is only used to decide whether receiving should stop. This does not consumesk_err; the check-then-sock_error() TOCTOU on the no-data paths is a separate issue.Fixes eb477fdd6803 ("tcp: add recv_should_stop helper")
Is the Fixes tag right? eb477fdd6803 only moved the existing check into a helper, the plain read was already there before.
Quoted 18 lines
Reported-by Matthieu Baerts (NGI0) <[email protected]>Signed-off-by Quanye Yang <[email protected]>---
---
Changes in v2:
- add Reported-by and Closes for the MPTCP syzkaller report
- Link to v1: https://patch.msgid.link/[email protected]--- include/net/tcp.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)include/net/tcp.h +0
-0
include/net/tcp.h enum skb_drop_reason tcp_inbound_hash(struct sock *sk, · line 3089
Quoted 9 lines
include/net/tcp.h +2
-1@@ -3082,7 +3082,8 @@ enum skb_drop_reason tcp_inbound_hash(struct sock *sk,static inline int tcp_recv_should_stop (struct sock *sk) { -return sk->sk_err || +/* sk_err can be cleared locklessly by sock_error(). */ +return READ_ONCE (sk->sk_err) || sk->sk_state ==TCP_CLOSE || (sk->sk_shutdown &RCV_SHUTDOWN ) ||signal_pending (current);
The lockless writers are sock_error() from SO_ERROR and recvmmsg(),
so every plain read of sk_err on a TCP socket has the same race, not only this helper:
tcp_recvmsg_locked
tcp_sendmsg_locked
tcp_splice_read
sk_stream_wait_memory
tcp_bpf_sendmsg
tcp_bpf_recvmsg_parser
mptcp_splice_read
mptcp_recvmsg
I'd drop the Fixes tag, target net-next and annotate all of them like e13ec3da05d1, instead of pointing at a MPTCP commit.
Eric, Matthieu, does that sound OK?
Hi Jiayuan,
Quoted 108 lines · Jiayuan Chen
Quoted 51 lines · On 9/9/26 10:05 AM, Quanye Yang via B4 Relay wrote:
From: Quanye Yang <[email protected]>
BUG: KCSAN: data-race in do_recvmmsg / mptcp_recvmsgread-write (marked) to
0xffff8880134d391cof 4 bytes by task 2619 on cpu 1:instrument_atomic_read_write include/linux/instrumented.h:113 [inline] sock_error include/net/sock.h:2565 [inline] do_recvmmsg+0x50c/0x580 net/socket.c:3049 __sys_recvmmsg net/socket.c:3144 [inline] __do_sys_recvmmsg net/socket.c:3167 [inline] __se_sys_recvmmsg net/socket.c:3160 [inline] __x64_sys_recvmmsg+0x161/0x180 net/socket.c:3160 x64_sys_call+0x19c7/0x1ca0 arch/x86/include/generated/asm/ syscalls_64.h:300 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline] do_syscall_64+0xde/0x3d0 arch/x86/entry/syscall_64.c:84 entry_SYSCALL_64_after_hwframe+0x77/0x7fread to
0xffff8880134d391cof 4 bytes by task 2620 on cpu 0:tcp_recv_should_stop include/net/tcp.h:3086 [inline] mptcp_recvmsg+0x54d/0xd50 net/mptcp/protocol.c:2466 inet_recvmsg+0x204/0x210 net/ipv4/af_inet.c:894 sock_recvmsg_nosec net/socket.c:1151 [inline] sock_recvmsg+0x11a/0x140 net/socket.c:1173 ____sys_recvmsg+0x14b/0x3c0 net/socket.c:2933 ___sys_recvmsg+0x116/0x160 net/socket.c:2975 __sys_recvmsg net/socket.c:3008 [inline] __do_sys_recvmsg net/socket.c:3014 [inline] __se_sys_recvmsg net/socket.c:3011 [inline] __x64_sys_recvmsg+0xeb/0x160 net/socket.c:3011 x64_sys_call+0x1319/0x1ca0 arch/x86/include/generated/asm/ syscalls_64.h:48 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline] do_syscall_64+0xde/0x3d0 arch/x86/entry/syscall_64.c:84 entry_SYSCALL_64_after_hwframe+0x77/0x7fvalue changed:
0x0000006b->0x00000000Reported by Kernel Concurrency Sanitizer on:
CPU: 0 UID: 0 PID: 2620 Comm: syz.2.33 Not tainted 7.2.0-g39d4f32c5d53 #76 PREEMPT(full) Hardware name: QEMU Ubuntu 26.04 PC (i440FX + PIIX, 1996), BIOS1.17.0-debian-1.17.0-1ubuntu1 04/01/2014
do_recvmmsg()callssock_error()without the socket lock.sock_error()clearssk_errwithxchg(), which races with the unmarked load intcp_recv_should_stop(). The same race exists for plain TCP with concurrentrecvmmsg()readers; KCSAN reported it on an MPTCP socket becausemptcp_recvmsg()uses this helper.Quoted 6 lines
Use
READ_ONCE()for the lockless peek. No extra ordering is needed:
the value is only used to decide whether receiving should stop. This does not consumesk_err; the check-then-sock_error() TOCTOU on the no-data paths is a separate issue.Fixes eb477fdd6803 ("tcp: add recv_should_stop helper")Is the Fixes tag right?
eb477fdd6803only moved the existing check into a helper, the plain read was already there before.Quoted 26 lines
Reported-by Matthieu Baerts (NGI0) <[email protected]>Signed-off-by Quanye Yang <[email protected]>---
[email protected]/
---
Changes in v2:
- add Reported-by and Closes for the MPTCP syzkaller report
- Link to v1: https://patch.msgid.link/20260908-mptcp-sk-err-net-v1-1-
[email protected]
---
include/net/tcp.h| 3 ++-1 file changed, 2 insertions(+), 1 deletion(-)include/net/tcp.h +2
-1@@ -3082,7 +3082,8 @@ enum skb_drop_reason tcp_inbound_hash(struct sock *sk, static inline int tcp_recv_should_stop(struct sock *sk) { - return sk->sk_err || + /* sk_err can be cleared locklessly by sock_error(). */ + return READ_ONCE(sk->sk_err) ||Quoted 3 lines
sk->sk_state==TCP_CLOSE||
(sk->sk_shutdown&RCV_SHUTDOWN) ||
signal_pending(current);The lockless writers are
sock_error()fromSO_ERRORandrecvmmsg(), so every plain read ofsk_erron a TCP socket has the same race, not only this helper:
tcp_recvmsg_locked
tcp_sendmsg_locked
tcp_splice_read
sk_stream_wait_memory
tcp_bpf_sendmsg
tcp_bpf_recvmsg_parser
mptcp_splice_read
mptcp_recvmsg
Quoted 4 lines
I'd drop the Fixes tag, target net-next and annotate all of them like
e13ec3da05d1, instead of pointing at a MPTCP commit.Eric, Matthieu, does that sound OK?
Sounds good to me to target net-next without the Fixes tag.
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
Quoted 12 lines
The lockless writers are
sock_error()fromSO_ERRORandrecvmmsg(), so every plain read ofsk_erron a TCP socket has the same race, not only this helper:
tcp_recvmsg_locked
tcp_sendmsg_locked
tcp_splice_read
sk_stream_wait_memory
tcp_bpf_sendmsg
tcp_bpf_recvmsg_parser
mptcp_splice_read
mptcp_recvmsg
Quoted 5 lines
I'd drop the Fixes tag, target net-next and annotate all of them like
e13ec3da05d1, instead of pointing at a MPTCP commit.Eric, Matthieu, does that sound OK?
Thanks Jiayuan, Matt.
Agreed that eb477fdd6803 only moved the existing load, so I will drop the Fixes tag and retarget to net-next.
I will also annotate the remaining unmarked sk_err reads on the TCP/MPTCP recv and splice paths (the other arm of the same loops that Sashiko pointed out). I would keep this as KCSAN annotations in the style of e13ec3da05d1, and leave the check-then-sock_error() TOCTOU as a separate change.
sk_stream_wait_memory() already uses READ_ONCE(sk->sk_err). I will double-check the sendmsg/bpf sites before expanding further.
Happy to wait if Eric prefers a different scope.
Quanye
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Critical: 0 · High: 1 · Medium: 0 · Low: 0
- [High] Incomplete data-race fix: the patch converts only the sk_err load inside tcp_recv_should_stop() (include/net/tcp.h:3086) to…
--
tcp: fix data-race in tcp_recv_should_stop
This converts the sk_err load inside tcp_recv_should_stop() to READ_ONCE() because do_recvmmsg() calls sock_error() without the socket lock and sock_error() clears sk_err with xchg(). It carries a Fixes: tag for eb477fdd6803 and a Closes: tag for the MPTCP syzkaller report.
Quoted 5 lines
include/net/tcp.h +0
-0
include/net/tcp.h enum skb_drop_reason tcp_inbound_hash(struct sock *sk, · line 3089
Quoted 9 lines
include/net/tcp.h +2
-1@@ -3082,7 +3082,8 @@ enum skb_drop_reason tcp_inbound_hash(struct sock *sk,static inline int tcp_recv_should_stop (struct sock *sk) { -return sk->sk_err || +/* sk_err can be cleared locklessly by sock_error(). */ +return READ_ONCE (sk->sk_err) || sk->sk_state ==TCP_CLOSE || (sk->sk_shutdown &RCV_SHUTDOWN ) ||signal_pending (current);
[Severity: High]
Is annotating only this read enough to close the reported race?
Every caller of tcp_recv_should_stop() keeps a plain read of the same field in the other arm of the very same loop. In mptcp_recvmsg(), the function named in the KCSAN splat:
net/mptcp/protocol.c:mptcp_recvmsg() {
...
if (copied) {
if (tcp_recv_should_stop(sk) ||
!timeo)
break;
} else {
if (sk->sk_err) {
copied = sock_error(sk);
break;
}
...
}The same shape exists in tcp_recvmsg_locked():
net/ipv4/tcp.c:tcp_recvmsg_locked() {
...
if (copied) {
if (!timeo ||
tcp_recv_should_stop(sk))
break;
} else {
if (sock_flag(sk, SOCK_DONE))
break;
if (sk->sk_err) {
copied = sock_error(sk);
break;
}
...
}and in tcp_splice_read() and mptcp_splice_read(), whose loops both end with the now-annotated helper:
net/ipv4/tcp.c:tcp_splice_read() {
...
if (sk->sk_err) {
ret = sock_error(sk);
break;
}
...
if (tcp_recv_should_stop(sk))
break;
...
}The writer described in the changelog, do_recvmmsg() calling sock_error(sock->sk) with no lock_sock() and clearing the field with xchg(&sk->sk_err, 0), races with those four plain reads exactly as it raced with the one that was annotated, since lock_sock() in the readers gives no exclusion against it.
Would a reproducer that lands in the blocking, copied == 0 arm still produce the identical "data-race in do_recvmmsg / mptcp_recvmsg" report, just attributed to mptcp_recvmsg() rather than to tcp_recv_should_stop()? If so, should the Closes: tag be dropped, or should the four remaining reads be converted in the same patch?
The Fixes: tag points at eb477fdd6803, but that commit only moved these pre-existing plain reads into the new helper; the sibling reads left behind are part of the same defect. Does the Fixes: attribution still hold with only the helper annotated?
The added comment states "sk_err can be cleared locklessly by sock_error()", which reads as a property of the field rather than of this one call site. Should the write side conventions be noted too, since the writers in these files already use WRITE_ONCE() and sock_error() itself uses data_race() for its own read?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260909-mptcp-sk-err-net-v2-1-5044abecac90%40proton.me