netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 net 0/2] af_unix: Fix two data races reported by KCSAN.
@ 2023-05-10  0:34 Kuniyuki Iwashima
  2023-05-10  0:34 ` [PATCH v1 net 1/2] af_unix: Fix a data race of sk->sk_receive_queue->qlen Kuniyuki Iwashima
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Kuniyuki Iwashima @ 2023-05-10  0:34 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

KCSAN reported data races around these two fields for AF_UNIX sockets.

  * sk->sk_receive_queue->qlen
  * sk->sk_shutdown

Let's annotate them properly.


Kuniyuki Iwashima (2):
  af_unix: Fix a data race of sk->sk_receive_queue->qlen.
  af_unix: Fix data races around sk->sk_shutdown.

 net/unix/af_unix.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

-- 
2.30.2


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v1 net 1/2] af_unix: Fix a data race of sk->sk_receive_queue->qlen.
  2023-05-10  0:34 [PATCH v1 net 0/2] af_unix: Fix two data races reported by KCSAN Kuniyuki Iwashima
@ 2023-05-10  0:34 ` Kuniyuki Iwashima
  2023-05-10 11:31   ` Eric Dumazet
  2023-05-10  0:34 ` [PATCH v1 net 2/2] af_unix: Fix data races around sk->sk_shutdown Kuniyuki Iwashima
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Kuniyuki Iwashima @ 2023-05-10  0:34 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, syzbot

KCSAN found a data race of sk->sk_receive_queue->qlen where recvmsg()
updates qlen under the queue lock and sendmsg() checks qlen under
unix_state_sock(), not the queue lock, so the reader side needs
READ_ONCE().

BUG: KCSAN: data-race in __skb_try_recv_from_queue / unix_wait_for_peer

write (marked) to 0xffff888019fe7c68 of 4 bytes by task 49792 on cpu 0:
 __skb_unlink include/linux/skbuff.h:2347 [inline]
 __skb_try_recv_from_queue+0x3de/0x470 net/core/datagram.c:197
 __skb_try_recv_datagram+0xf7/0x390 net/core/datagram.c:263
 __unix_dgram_recvmsg+0x109/0x8a0 net/unix/af_unix.c:2452
 unix_dgram_recvmsg+0x94/0xa0 net/unix/af_unix.c:2549
 sock_recvmsg_nosec net/socket.c:1019 [inline]
 ____sys_recvmsg+0x3a3/0x3b0 net/socket.c:2720
 ___sys_recvmsg+0xc8/0x150 net/socket.c:2764
 do_recvmmsg+0x182/0x560 net/socket.c:2858
 __sys_recvmmsg net/socket.c:2937 [inline]
 __do_sys_recvmmsg net/socket.c:2960 [inline]
 __se_sys_recvmmsg net/socket.c:2953 [inline]
 __x64_sys_recvmmsg+0x153/0x170 net/socket.c:2953
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x3b/0x90 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x72/0xdc

read to 0xffff888019fe7c68 of 4 bytes by task 49793 on cpu 1:
 skb_queue_len include/linux/skbuff.h:2127 [inline]
 unix_recvq_full net/unix/af_unix.c:229 [inline]
 unix_wait_for_peer+0x154/0x1a0 net/unix/af_unix.c:1445
 unix_dgram_sendmsg+0x13bc/0x14b0 net/unix/af_unix.c:2048
 sock_sendmsg_nosec net/socket.c:724 [inline]
 sock_sendmsg+0x148/0x160 net/socket.c:747
 ____sys_sendmsg+0x20e/0x620 net/socket.c:2503
 ___sys_sendmsg+0xc6/0x140 net/socket.c:2557
 __sys_sendmmsg+0x11d/0x370 net/socket.c:2643
 __do_sys_sendmmsg net/socket.c:2672 [inline]
 __se_sys_sendmmsg net/socket.c:2669 [inline]
 __x64_sys_sendmmsg+0x58/0x70 net/socket.c:2669
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x3b/0x90 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x72/0xdc

value changed: 0x0000000b -> 0x00000001

Reported by Kernel Concurrency Sanitizer on:
CPU: 1 PID: 49793 Comm: syz-executor.0 Not tainted 6.3.0-rc7-02330-gca6270c12e20 #2
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by: syzbot <syzkaller@googlegroups.com>
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/unix/af_unix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index fb31e8a4409e..08102e728b15 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -1442,7 +1442,7 @@ static long unix_wait_for_peer(struct sock *other, long timeo)
 
 	sched = !sock_flag(other, SOCK_DEAD) &&
 		!(other->sk_shutdown & RCV_SHUTDOWN) &&
-		unix_recvq_full(other);
+		unix_recvq_full_lockless(other);
 
 	unix_state_unlock(other);
 
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v1 net 2/2] af_unix: Fix data races around sk->sk_shutdown.
  2023-05-10  0:34 [PATCH v1 net 0/2] af_unix: Fix two data races reported by KCSAN Kuniyuki Iwashima
  2023-05-10  0:34 ` [PATCH v1 net 1/2] af_unix: Fix a data race of sk->sk_receive_queue->qlen Kuniyuki Iwashima
@ 2023-05-10  0:34 ` Kuniyuki Iwashima
  2023-05-10 11:32   ` Eric Dumazet
  2023-05-10 13:10 ` [PATCH v1 net 0/2] af_unix: Fix two data races reported by KCSAN Michal Kubiak
  2023-05-11  2:10 ` patchwork-bot+netdevbpf
  3 siblings, 1 reply; 7+ messages in thread
From: Kuniyuki Iwashima @ 2023-05-10  0:34 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev, syzbot,
	Rainer Weikusat

KCSAN found a data race around sk->sk_shutdown where unix_release_sock()
and unix_shutdown() update it under unix_state_lock(), OTOH unix_poll()
and unix_dgram_poll() read it locklessly.

We need to annotate the writes and reads with WRITE_ONCE() and READ_ONCE().

BUG: KCSAN: data-race in unix_poll / unix_release_sock

write to 0xffff88800d0f8aec of 1 bytes by task 264 on cpu 0:
 unix_release_sock+0x75c/0x910 net/unix/af_unix.c:631
 unix_release+0x59/0x80 net/unix/af_unix.c:1042
 __sock_release+0x7d/0x170 net/socket.c:653
 sock_close+0x19/0x30 net/socket.c:1397
 __fput+0x179/0x5e0 fs/file_table.c:321
 ____fput+0x15/0x20 fs/file_table.c:349
 task_work_run+0x116/0x1a0 kernel/task_work.c:179
 resume_user_mode_work include/linux/resume_user_mode.h:49 [inline]
 exit_to_user_mode_loop kernel/entry/common.c:171 [inline]
 exit_to_user_mode_prepare+0x174/0x180 kernel/entry/common.c:204
 __syscall_exit_to_user_mode_work kernel/entry/common.c:286 [inline]
 syscall_exit_to_user_mode+0x1a/0x30 kernel/entry/common.c:297
 do_syscall_64+0x4b/0x90 arch/x86/entry/common.c:86
 entry_SYSCALL_64_after_hwframe+0x72/0xdc

read to 0xffff88800d0f8aec of 1 bytes by task 222 on cpu 1:
 unix_poll+0xa3/0x2a0 net/unix/af_unix.c:3170
 sock_poll+0xcf/0x2b0 net/socket.c:1385
 vfs_poll include/linux/poll.h:88 [inline]
 ep_item_poll.isra.0+0x78/0xc0 fs/eventpoll.c:855
 ep_send_events fs/eventpoll.c:1694 [inline]
 ep_poll fs/eventpoll.c:1823 [inline]
 do_epoll_wait+0x6c4/0xea0 fs/eventpoll.c:2258
 __do_sys_epoll_wait fs/eventpoll.c:2270 [inline]
 __se_sys_epoll_wait fs/eventpoll.c:2265 [inline]
 __x64_sys_epoll_wait+0xcc/0x190 fs/eventpoll.c:2265
 do_syscall_x64 arch/x86/entry/common.c:50 [inline]
 do_syscall_64+0x3b/0x90 arch/x86/entry/common.c:80
 entry_SYSCALL_64_after_hwframe+0x72/0xdc

value changed: 0x00 -> 0x03

Reported by Kernel Concurrency Sanitizer on:
CPU: 1 PID: 222 Comm: dbus-broker Not tainted 6.3.0-rc7-02330-gca6270c12e20 #2
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014

Fixes: 3c73419c09a5 ("af_unix: fix 'poll for write'/ connected DGRAM sockets")
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by: syzbot <syzkaller@googlegroups.com>
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
Cc: Rainer Weikusat <rweikusat@mssgmbh.com>
---
 net/unix/af_unix.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 08102e728b15..cc695c9f09ec 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -603,7 +603,7 @@ static void unix_release_sock(struct sock *sk, int embrion)
 	/* Clear state */
 	unix_state_lock(sk);
 	sock_orphan(sk);
-	sk->sk_shutdown = SHUTDOWN_MASK;
+	WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
 	path	     = u->path;
 	u->path.dentry = NULL;
 	u->path.mnt = NULL;
@@ -628,7 +628,7 @@ static void unix_release_sock(struct sock *sk, int embrion)
 		if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
 			unix_state_lock(skpair);
 			/* No more writes */
-			skpair->sk_shutdown = SHUTDOWN_MASK;
+			WRITE_ONCE(skpair->sk_shutdown, SHUTDOWN_MASK);
 			if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
 				WRITE_ONCE(skpair->sk_err, ECONNRESET);
 			unix_state_unlock(skpair);
@@ -3008,7 +3008,7 @@ static int unix_shutdown(struct socket *sock, int mode)
 	++mode;
 
 	unix_state_lock(sk);
-	sk->sk_shutdown |= mode;
+	WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | mode);
 	other = unix_peer(sk);
 	if (other)
 		sock_hold(other);
@@ -3028,7 +3028,7 @@ static int unix_shutdown(struct socket *sock, int mode)
 		if (mode&SEND_SHUTDOWN)
 			peer_mode |= RCV_SHUTDOWN;
 		unix_state_lock(other);
-		other->sk_shutdown |= peer_mode;
+		WRITE_ONCE(other->sk_shutdown, other->sk_shutdown | peer_mode);
 		unix_state_unlock(other);
 		other->sk_state_change(other);
 		if (peer_mode == SHUTDOWN_MASK)
@@ -3160,16 +3160,18 @@ static __poll_t unix_poll(struct file *file, struct socket *sock, poll_table *wa
 {
 	struct sock *sk = sock->sk;
 	__poll_t mask;
+	u8 shutdown;
 
 	sock_poll_wait(file, sock, wait);
 	mask = 0;
+	shutdown = READ_ONCE(sk->sk_shutdown);
 
 	/* exceptional events? */
 	if (READ_ONCE(sk->sk_err))
 		mask |= EPOLLERR;
-	if (sk->sk_shutdown == SHUTDOWN_MASK)
+	if (shutdown == SHUTDOWN_MASK)
 		mask |= EPOLLHUP;
-	if (sk->sk_shutdown & RCV_SHUTDOWN)
+	if (shutdown & RCV_SHUTDOWN)
 		mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
 
 	/* readable? */
@@ -3203,9 +3205,11 @@ static __poll_t unix_dgram_poll(struct file *file, struct socket *sock,
 	struct sock *sk = sock->sk, *other;
 	unsigned int writable;
 	__poll_t mask;
+	u8 shutdown;
 
 	sock_poll_wait(file, sock, wait);
 	mask = 0;
+	shutdown = READ_ONCE(sk->sk_shutdown);
 
 	/* exceptional events? */
 	if (READ_ONCE(sk->sk_err) ||
@@ -3213,9 +3217,9 @@ static __poll_t unix_dgram_poll(struct file *file, struct socket *sock,
 		mask |= EPOLLERR |
 			(sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
 
-	if (sk->sk_shutdown & RCV_SHUTDOWN)
+	if (shutdown & RCV_SHUTDOWN)
 		mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
-	if (sk->sk_shutdown == SHUTDOWN_MASK)
+	if (shutdown == SHUTDOWN_MASK)
 		mask |= EPOLLHUP;
 
 	/* readable? */
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v1 net 1/2] af_unix: Fix a data race of sk->sk_receive_queue->qlen.
  2023-05-10  0:34 ` [PATCH v1 net 1/2] af_unix: Fix a data race of sk->sk_receive_queue->qlen Kuniyuki Iwashima
@ 2023-05-10 11:31   ` Eric Dumazet
  0 siblings, 0 replies; 7+ messages in thread
From: Eric Dumazet @ 2023-05-10 11:31 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: David S. Miller, Jakub Kicinski, Paolo Abeni, Kuniyuki Iwashima,
	netdev, syzbot

On Wed, May 10, 2023 at 2:35 AM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
>
> KCSAN found a data race of sk->sk_receive_queue->qlen where recvmsg()
> updates qlen under the queue lock and sendmsg() checks qlen under
> unix_state_sock(), not the queue lock, so the reader side needs
> READ_ONCE().
>
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Reported-by: syzbot <syzkaller@googlegroups.com>
> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> ---

Reviewed-by: Eric Dumazet <edumazet@google.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v1 net 2/2] af_unix: Fix data races around sk->sk_shutdown.
  2023-05-10  0:34 ` [PATCH v1 net 2/2] af_unix: Fix data races around sk->sk_shutdown Kuniyuki Iwashima
@ 2023-05-10 11:32   ` Eric Dumazet
  0 siblings, 0 replies; 7+ messages in thread
From: Eric Dumazet @ 2023-05-10 11:32 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: David S. Miller, Jakub Kicinski, Paolo Abeni, Kuniyuki Iwashima,
	netdev, syzbot, Rainer Weikusat

On Wed, May 10, 2023 at 2:36 AM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
>
> KCSAN found a data race around sk->sk_shutdown where unix_release_sock()
> and unix_shutdown() update it under unix_state_lock(), OTOH unix_poll()
> and unix_dgram_poll() read it locklessly.
>
> We need to annotate the writes and reads with WRITE_ONCE() and READ_ONCE().
>
> Fixes: 3c73419c09a5 ("af_unix: fix 'poll for write'/ connected DGRAM sockets")
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Reported-by: syzbot <syzkaller@googlegroups.com>
> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>

Reviewed-by: Eric Dumazet <edumazet@google.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v1 net 0/2] af_unix: Fix two data races reported by KCSAN.
  2023-05-10  0:34 [PATCH v1 net 0/2] af_unix: Fix two data races reported by KCSAN Kuniyuki Iwashima
  2023-05-10  0:34 ` [PATCH v1 net 1/2] af_unix: Fix a data race of sk->sk_receive_queue->qlen Kuniyuki Iwashima
  2023-05-10  0:34 ` [PATCH v1 net 2/2] af_unix: Fix data races around sk->sk_shutdown Kuniyuki Iwashima
@ 2023-05-10 13:10 ` Michal Kubiak
  2023-05-11  2:10 ` patchwork-bot+netdevbpf
  3 siblings, 0 replies; 7+ messages in thread
From: Michal Kubiak @ 2023-05-10 13:10 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Kuniyuki Iwashima, netdev

On Tue, May 09, 2023 at 05:34:54PM -0700, Kuniyuki Iwashima wrote:
> KCSAN reported data races around these two fields for AF_UNIX sockets.
> 
>   * sk->sk_receive_queue->qlen
>   * sk->sk_shutdown
> 
> Let's annotate them properly.
> 
> 
> Kuniyuki Iwashima (2):
>   af_unix: Fix a data race of sk->sk_receive_queue->qlen.
>   af_unix: Fix data races around sk->sk_shutdown.
> 
>  net/unix/af_unix.c | 22 +++++++++++++---------
>  1 file changed, 13 insertions(+), 9 deletions(-)
> 

For the series.
Reviewed-by: Michal Kubiak <michal.kubiak@intel.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v1 net 0/2] af_unix: Fix two data races reported by KCSAN.
  2023-05-10  0:34 [PATCH v1 net 0/2] af_unix: Fix two data races reported by KCSAN Kuniyuki Iwashima
                   ` (2 preceding siblings ...)
  2023-05-10 13:10 ` [PATCH v1 net 0/2] af_unix: Fix two data races reported by KCSAN Michal Kubiak
@ 2023-05-11  2:10 ` patchwork-bot+netdevbpf
  3 siblings, 0 replies; 7+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-05-11  2:10 UTC (permalink / raw)
  To: Kuniyuki Iwashima; +Cc: davem, edumazet, kuba, pabeni, kuni1840, netdev

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Tue, 9 May 2023 17:34:54 -0700 you wrote:
> KCSAN reported data races around these two fields for AF_UNIX sockets.
> 
>   * sk->sk_receive_queue->qlen
>   * sk->sk_shutdown
> 
> Let's annotate them properly.
> 
> [...]

Here is the summary with links:
  - [v1,net,1/2] af_unix: Fix a data race of sk->sk_receive_queue->qlen.
    https://git.kernel.org/netdev/net/c/679ed006d416
  - [v1,net,2/2] af_unix: Fix data races around sk->sk_shutdown.
    https://git.kernel.org/netdev/net/c/e1d09c2c2f57

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-05-11  2:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-10  0:34 [PATCH v1 net 0/2] af_unix: Fix two data races reported by KCSAN Kuniyuki Iwashima
2023-05-10  0:34 ` [PATCH v1 net 1/2] af_unix: Fix a data race of sk->sk_receive_queue->qlen Kuniyuki Iwashima
2023-05-10 11:31   ` Eric Dumazet
2023-05-10  0:34 ` [PATCH v1 net 2/2] af_unix: Fix data races around sk->sk_shutdown Kuniyuki Iwashima
2023-05-10 11:32   ` Eric Dumazet
2023-05-10 13:10 ` [PATCH v1 net 0/2] af_unix: Fix two data races reported by KCSAN Michal Kubiak
2023-05-11  2:10 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).