Skip to content

Commit 5bca1d0

Browse files
edumazetkuba-moo
authored andcommitted
net: datagram: fix data-races in datagram_poll()
datagram_poll() runs locklessly, we should add READ_ONCE() annotations while reading sk->sk_err, sk->sk_shutdown and sk->sk_state. Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Eric Dumazet <[email protected]> Reviewed-by: Kuniyuki Iwashima <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent cdc2e28 commit 5bca1d0

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

net/core/datagram.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -807,18 +807,21 @@ __poll_t datagram_poll(struct file *file, struct socket *sock,
807807
{
808808
struct sock *sk = sock->sk;
809809
__poll_t mask;
810+
u8 shutdown;
810811

811812
sock_poll_wait(file, sock, wait);
812813
mask = 0;
813814

814815
/* exceptional events? */
815-
if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
816+
if (READ_ONCE(sk->sk_err) ||
817+
!skb_queue_empty_lockless(&sk->sk_error_queue))
816818
mask |= EPOLLERR |
817819
(sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
818820

819-
if (sk->sk_shutdown & RCV_SHUTDOWN)
821+
shutdown = READ_ONCE(sk->sk_shutdown);
822+
if (shutdown & RCV_SHUTDOWN)
820823
mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
821-
if (sk->sk_shutdown == SHUTDOWN_MASK)
824+
if (shutdown == SHUTDOWN_MASK)
822825
mask |= EPOLLHUP;
823826

824827
/* readable? */
@@ -827,10 +830,12 @@ __poll_t datagram_poll(struct file *file, struct socket *sock,
827830

828831
/* Connection-based need to check for termination and startup */
829832
if (connection_based(sk)) {
830-
if (sk->sk_state == TCP_CLOSE)
833+
int state = READ_ONCE(sk->sk_state);
834+
835+
if (state == TCP_CLOSE)
831836
mask |= EPOLLHUP;
832837
/* connection hasn't started yet? */
833-
if (sk->sk_state == TCP_SYN_SENT)
838+
if (state == TCP_SYN_SENT)
834839
return mask;
835840
}
836841

0 commit comments

Comments
 (0)