netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 net 0/2] af_unix: Fix some OOB implementation.
@ 2022-03-17  3:08 Kuniyuki Iwashima
  2022-03-17  3:08 ` [PATCH v4 net 1/2] af_unix: Fix some data-races around unix_sk(sk)->oob_skb Kuniyuki Iwashima
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Kuniyuki Iwashima @ 2022-03-17  3:08 UTC (permalink / raw)
  To: David S. Miller, Jakub Kicinski, Eric Dumazet, Rao Shoaib
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

This series fixes some data-races and adds a missing feature around the
commit 314001f0bf92 ("af_unix: Add OOB support").

Changelog:
  - v4:
    - Separate nit changes from this series for net-next

  - v3: https://lore.kernel.org/netdev/20220315183855.15190-1-kuniyu@amazon.co.jp/
    - Add the first patch

  - v2: https://lore.kernel.org/netdev/20220315054801.72035-1-kuniyu@amazon.co.jp/
    - Add READ_ONCE() to avoid syzbot warning (Eric)
    - Add IS_ENABLED(CONFIG_AF_UNIX_OOB) (Shoaib)

  - v1: https://lore.kernel.org/netdev/20220314052110.53634-1-kuniyu@amazon.co.jp/

Kuniyuki Iwashima (2):
  af_unix: Fix some data-races around unix_sk(sk)->oob_skb.
  af_unix: Support POLLPRI for OOB.

 net/unix/af_unix.c                               | 16 +++++++++-------
 .../selftests/net/af_unix/test_unix_oob.c        |  6 +++---
 2 files changed, 12 insertions(+), 10 deletions(-)

-- 
2.30.2


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

* [PATCH v4 net 1/2] af_unix: Fix some data-races around unix_sk(sk)->oob_skb.
  2022-03-17  3:08 [PATCH v4 net 0/2] af_unix: Fix some OOB implementation Kuniyuki Iwashima
@ 2022-03-17  3:08 ` Kuniyuki Iwashima
  2022-03-17  3:08 ` [PATCH v4 net 2/2] af_unix: Support POLLPRI for OOB Kuniyuki Iwashima
  2022-03-18 13:50 ` [PATCH v4 net 0/2] af_unix: Fix some OOB implementation patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Kuniyuki Iwashima @ 2022-03-17  3:08 UTC (permalink / raw)
  To: David S. Miller, Jakub Kicinski, Eric Dumazet, Rao Shoaib
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

Out-of-band data automatically places a "mark" showing wherein the
sequence the out-of-band data would have been.  If the out-of-band data
implies cancelling everything sent so far, the "mark" is helpful to flush
them.  When the socket's read pointer reaches the "mark", the ioctl() below
sets a non zero value to the arg `atmark`:

The out-of-band data is queued in sk->sk_receive_queue as well as ordinary
data and also saved in unix_sk(sk)->oob_skb.  It can be used to test if the
head of the receive queue is the out-of-band data meaning the socket is at
the "mark".

While testing that, unix_ioctl() reads unix_sk(sk)->oob_skb locklessly.
Thus, all accesses to oob_skb need some basic protection to avoid
load/store tearing which KCSAN detects when these are called concurrently:

  - ioctl(fd_a, SIOCATMARK, &atmark, sizeof(atmark))
  - send(fd_b_connected_to_a, buf, sizeof(buf), MSG_OOB)

BUG: KCSAN: data-race in unix_ioctl / unix_stream_sendmsg

write to 0xffff888003d9cff0 of 8 bytes by task 175 on cpu 1:
 unix_stream_sendmsg (net/unix/af_unix.c:2087 net/unix/af_unix.c:2191)
 sock_sendmsg (net/socket.c:705 net/socket.c:725)
 __sys_sendto (net/socket.c:2040)
 __x64_sys_sendto (net/socket.c:2048)
 do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80)
 entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:113)

read to 0xffff888003d9cff0 of 8 bytes by task 176 on cpu 0:
 unix_ioctl (net/unix/af_unix.c:3101 (discriminator 1))
 sock_do_ioctl (net/socket.c:1128)
 sock_ioctl (net/socket.c:1242)
 __x64_sys_ioctl (fs/ioctl.c:52 fs/ioctl.c:874 fs/ioctl.c:860 fs/ioctl.c:860)
 do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80)
 entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:113)

value changed: 0xffff888003da0c00 -> 0xffff888003da0d00

Reported by Kernel Concurrency Sanitizer on:
CPU: 0 PID: 176 Comm: unix_race_oob_i Not tainted 5.17.0-rc5-59529-g83dc4c2af682 #12
Hardware name: Red Hat KVM, BIOS 1.11.0-2.amzn2 04/01/2014

Fixes: 314001f0bf92 ("af_unix: Add OOB support")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
---
 net/unix/af_unix.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index c19569819866..0c37e5595aae 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -2084,7 +2084,7 @@ static int queue_oob(struct socket *sock, struct msghdr *msg, struct sock *other
 	if (ousk->oob_skb)
 		consume_skb(ousk->oob_skb);
 
-	ousk->oob_skb = skb;
+	WRITE_ONCE(ousk->oob_skb, skb);
 
 	scm_stat_add(other, skb);
 	skb_queue_tail(&other->sk_receive_queue, skb);
@@ -2602,9 +2602,8 @@ static int unix_stream_recv_urg(struct unix_stream_read_state *state)
 
 	oob_skb = u->oob_skb;
 
-	if (!(state->flags & MSG_PEEK)) {
-		u->oob_skb = NULL;
-	}
+	if (!(state->flags & MSG_PEEK))
+		WRITE_ONCE(u->oob_skb, NULL);
 
 	unix_state_unlock(sk);
 
@@ -2639,7 +2638,7 @@ static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk,
 				skb = NULL;
 			} else if (sock_flag(sk, SOCK_URGINLINE)) {
 				if (!(flags & MSG_PEEK)) {
-					u->oob_skb = NULL;
+					WRITE_ONCE(u->oob_skb, NULL);
 					consume_skb(skb);
 				}
 			} else if (!(flags & MSG_PEEK)) {
@@ -3094,11 +3093,10 @@ static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 	case SIOCATMARK:
 		{
 			struct sk_buff *skb;
-			struct unix_sock *u = unix_sk(sk);
 			int answ = 0;
 
 			skb = skb_peek(&sk->sk_receive_queue);
-			if (skb && skb == u->oob_skb)
+			if (skb && skb == READ_ONCE(unix_sk(sk)->oob_skb))
 				answ = 1;
 			err = put_user(answ, (int __user *)arg);
 		}
-- 
2.30.2


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

* [PATCH v4 net 2/2] af_unix: Support POLLPRI for OOB.
  2022-03-17  3:08 [PATCH v4 net 0/2] af_unix: Fix some OOB implementation Kuniyuki Iwashima
  2022-03-17  3:08 ` [PATCH v4 net 1/2] af_unix: Fix some data-races around unix_sk(sk)->oob_skb Kuniyuki Iwashima
@ 2022-03-17  3:08 ` Kuniyuki Iwashima
  2022-03-18 13:50 ` [PATCH v4 net 0/2] af_unix: Fix some OOB implementation patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Kuniyuki Iwashima @ 2022-03-17  3:08 UTC (permalink / raw)
  To: David S. Miller, Jakub Kicinski, Eric Dumazet, Rao Shoaib
  Cc: Kuniyuki Iwashima, Kuniyuki Iwashima, netdev

The commit 314001f0bf92 ("af_unix: Add OOB support") introduced OOB for
AF_UNIX, but it lacks some changes for POLLPRI.  Let's add the missing
piece.

In the selftest, normal datagrams are sent followed by OOB data, so this
commit replaces `POLLIN | POLLPRI` with just `POLLPRI` in the first test
case.

Fixes: 314001f0bf92 ("af_unix: Add OOB support")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
---
v4:
  - Separate nit changes from this series for net-next

v3: https://lore.kernel.org/netdev/20220315183855.15190-1-kuniyu@amazon.co.jp/

v2: https://lore.kernel.org/netdev/20220315054801.72035-1-kuniyu@amazon.co.jp/
  - Add READ_ONCE() to avoid a race reported by KCSAN (Eric)
  - Add IS_ENABLED(CONFIG_AF_UNIX_OOB) (Shoaib)

v1: https://lore.kernel.org/netdev/20220314052110.53634-1-kuniyu@amazon.co.jp/
---
 net/unix/af_unix.c                                  | 4 ++++
 tools/testing/selftests/net/af_unix/test_unix_oob.c | 6 +++---
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 0c37e5595aae..1e7ed5829ed5 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -3137,6 +3137,10 @@ static __poll_t unix_poll(struct file *file, struct socket *sock, poll_table *wa
 		mask |= EPOLLIN | EPOLLRDNORM;
 	if (sk_is_readable(sk))
 		mask |= EPOLLIN | EPOLLRDNORM;
+#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
+	if (READ_ONCE(unix_sk(sk)->oob_skb))
+		mask |= EPOLLPRI;
+#endif
 
 	/* Connection-based need to check for termination and startup */
 	if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) &&
diff --git a/tools/testing/selftests/net/af_unix/test_unix_oob.c b/tools/testing/selftests/net/af_unix/test_unix_oob.c
index 3dece8b29253..b57e91e1c3f2 100644
--- a/tools/testing/selftests/net/af_unix/test_unix_oob.c
+++ b/tools/testing/selftests/net/af_unix/test_unix_oob.c
@@ -218,10 +218,10 @@ main(int argc, char **argv)
 
 	/* Test 1:
 	 * veriyf that SIGURG is
-	 * delivered and 63 bytes are
-	 * read and oob is '@'
+	 * delivered, 63 bytes are
+	 * read, oob is '@', and POLLPRI works.
 	 */
-	wait_for_data(pfd, POLLIN | POLLPRI);
+	wait_for_data(pfd, POLLPRI);
 	read_oob(pfd, &oob);
 	len = read_data(pfd, buf, 1024);
 	if (!signal_recvd || len != 63 || oob != '@') {
-- 
2.30.2


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

* Re: [PATCH v4 net 0/2] af_unix: Fix some OOB implementation.
  2022-03-17  3:08 [PATCH v4 net 0/2] af_unix: Fix some OOB implementation Kuniyuki Iwashima
  2022-03-17  3:08 ` [PATCH v4 net 1/2] af_unix: Fix some data-races around unix_sk(sk)->oob_skb Kuniyuki Iwashima
  2022-03-17  3:08 ` [PATCH v4 net 2/2] af_unix: Support POLLPRI for OOB Kuniyuki Iwashima
@ 2022-03-18 13:50 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-03-18 13:50 UTC (permalink / raw)
  To: Kuniyuki Iwashima; +Cc: davem, kuba, edumazet, Rao.Shoaib, kuni1840, netdev

Hello:

This series was applied to netdev/net.git (master)
by David S. Miller <davem@davemloft.net>:

On Thu, 17 Mar 2022 12:08:07 +0900 you wrote:
> This series fixes some data-races and adds a missing feature around the
> commit 314001f0bf92 ("af_unix: Add OOB support").
> 
> Changelog:
>   - v4:
>     - Separate nit changes from this series for net-next
> 
> [...]

Here is the summary with links:
  - [v4,net,1/2] af_unix: Fix some data-races around unix_sk(sk)->oob_skb.
    https://git.kernel.org/netdev/net/c/e82025c623e2
  - [v4,net,2/2] af_unix: Support POLLPRI for OOB.
    https://git.kernel.org/netdev/net/c/d9a232d435dc

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] 4+ messages in thread

end of thread, other threads:[~2022-03-18 13:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-17  3:08 [PATCH v4 net 0/2] af_unix: Fix some OOB implementation Kuniyuki Iwashima
2022-03-17  3:08 ` [PATCH v4 net 1/2] af_unix: Fix some data-races around unix_sk(sk)->oob_skb Kuniyuki Iwashima
2022-03-17  3:08 ` [PATCH v4 net 2/2] af_unix: Support POLLPRI for OOB Kuniyuki Iwashima
2022-03-18 13:50 ` [PATCH v4 net 0/2] af_unix: Fix some OOB implementation 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).