Skip to content

Commit

Permalink
af_unix: Annotate data-races around sk->sk_state in sendmsg() and rec…
Browse files Browse the repository at this point in the history
…vmsg().

[ Upstream commit 8a34d4e ]

The following functions read sk->sk_state locklessly and proceed only if
the state is TCP_ESTABLISHED.

  * unix_stream_sendmsg
  * unix_stream_read_generic
  * unix_seqpacket_sendmsg
  * unix_seqpacket_recvmsg

Let's use READ_ONCE() there.

Fixes: a05d2ad ("af_unix: Only allow recv on connected seqpacket sockets.")
Fixes: 1da177e ("Linux-2.6.12-rc2")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
q2ven authored and gregkh committed Jul 5, 2024
1 parent cc5d123 commit b5a6507
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions net/unix/af_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,7 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
goto out_err;

if (msg->msg_namelen) {
err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
err = READ_ONCE(sk->sk_state) == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP;
goto out_err;
} else {
err = -ENOTCONN;
Expand Down Expand Up @@ -2112,7 +2112,7 @@ static int unix_seqpacket_sendmsg(struct socket *sock, struct msghdr *msg,
if (err)
return err;

if (sk->sk_state != TCP_ESTABLISHED)
if (READ_ONCE(sk->sk_state) != TCP_ESTABLISHED)
return -ENOTCONN;

if (msg->msg_namelen)
Expand All @@ -2126,7 +2126,7 @@ static int unix_seqpacket_recvmsg(struct socket *sock, struct msghdr *msg,
{
struct sock *sk = sock->sk;

if (sk->sk_state != TCP_ESTABLISHED)
if (READ_ONCE(sk->sk_state) != TCP_ESTABLISHED)
return -ENOTCONN;

return unix_dgram_recvmsg(sock, msg, size, flags);
Expand Down Expand Up @@ -2326,7 +2326,7 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
size_t size = state->size;
unsigned int last_len;

if (unlikely(sk->sk_state != TCP_ESTABLISHED)) {
if (unlikely(READ_ONCE(sk->sk_state) != TCP_ESTABLISHED)) {
err = -EINVAL;
goto out;
}
Expand Down

0 comments on commit b5a6507

Please sign in to comment.