Skip to content

Commit e82025c

Browse files
q2vendavem330
authored andcommitted
af_unix: Fix some data-races around unix_sk(sk)->oob_skb.
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: 314001f ("af_unix: Add OOB support") Signed-off-by: Kuniyuki Iwashima <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 4219196 commit e82025c

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

net/unix/af_unix.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,7 +2084,7 @@ static int queue_oob(struct socket *sock, struct msghdr *msg, struct sock *other
20842084
if (ousk->oob_skb)
20852085
consume_skb(ousk->oob_skb);
20862086

2087-
ousk->oob_skb = skb;
2087+
WRITE_ONCE(ousk->oob_skb, skb);
20882088

20892089
scm_stat_add(other, skb);
20902090
skb_queue_tail(&other->sk_receive_queue, skb);
@@ -2602,9 +2602,8 @@ static int unix_stream_recv_urg(struct unix_stream_read_state *state)
26022602

26032603
oob_skb = u->oob_skb;
26042604

2605-
if (!(state->flags & MSG_PEEK)) {
2606-
u->oob_skb = NULL;
2607-
}
2605+
if (!(state->flags & MSG_PEEK))
2606+
WRITE_ONCE(u->oob_skb, NULL);
26082607

26092608
unix_state_unlock(sk);
26102609

@@ -2639,7 +2638,7 @@ static struct sk_buff *manage_oob(struct sk_buff *skb, struct sock *sk,
26392638
skb = NULL;
26402639
} else if (sock_flag(sk, SOCK_URGINLINE)) {
26412640
if (!(flags & MSG_PEEK)) {
2642-
u->oob_skb = NULL;
2641+
WRITE_ONCE(u->oob_skb, NULL);
26432642
consume_skb(skb);
26442643
}
26452644
} else if (!(flags & MSG_PEEK)) {
@@ -3094,11 +3093,10 @@ static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
30943093
case SIOCATMARK:
30953094
{
30963095
struct sk_buff *skb;
3097-
struct unix_sock *u = unix_sk(sk);
30983096
int answ = 0;
30993097

31003098
skb = skb_peek(&sk->sk_receive_queue);
3101-
if (skb && skb == u->oob_skb)
3099+
if (skb && skb == READ_ONCE(unix_sk(sk)->oob_skb))
31023100
answ = 1;
31033101
err = put_user(answ, (int __user *)arg);
31043102
}

0 commit comments

Comments
 (0)