netlink: annotate data races around sk_state

[ Upstream commit 9b663b5cbb15b494ef132a3c937641c90646eb73 ]

netlink_getsockbyportid() reads sk_state while a concurrent
netlink_connect() can change its value.

Fixes: 1da177e4c3 ("Linux-2.6.12-rc2")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Eric Dumazet 2023-01-20 12:59:55 +00:00 committed by Greg Kroah-Hartman
parent c64bb85471
commit 4cde1d2f16

View File

@ -1080,7 +1080,8 @@ static int netlink_connect(struct socket *sock, struct sockaddr *addr,
return -EINVAL; return -EINVAL;
if (addr->sa_family == AF_UNSPEC) { if (addr->sa_family == AF_UNSPEC) {
sk->sk_state = NETLINK_UNCONNECTED; /* paired with READ_ONCE() in netlink_getsockbyportid() */
WRITE_ONCE(sk->sk_state, NETLINK_UNCONNECTED);
/* dst_portid and dst_group can be read locklessly */ /* dst_portid and dst_group can be read locklessly */
WRITE_ONCE(nlk->dst_portid, 0); WRITE_ONCE(nlk->dst_portid, 0);
WRITE_ONCE(nlk->dst_group, 0); WRITE_ONCE(nlk->dst_group, 0);
@ -1104,7 +1105,8 @@ static int netlink_connect(struct socket *sock, struct sockaddr *addr,
err = netlink_autobind(sock); err = netlink_autobind(sock);
if (err == 0) { if (err == 0) {
sk->sk_state = NETLINK_CONNECTED; /* paired with READ_ONCE() in netlink_getsockbyportid() */
WRITE_ONCE(sk->sk_state, NETLINK_CONNECTED);
/* dst_portid and dst_group can be read locklessly */ /* dst_portid and dst_group can be read locklessly */
WRITE_ONCE(nlk->dst_portid, nladdr->nl_pid); WRITE_ONCE(nlk->dst_portid, nladdr->nl_pid);
WRITE_ONCE(nlk->dst_group, ffs(nladdr->nl_groups)); WRITE_ONCE(nlk->dst_group, ffs(nladdr->nl_groups));
@ -1156,8 +1158,8 @@ static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
/* Don't bother queuing skb if kernel socket has no input function */ /* Don't bother queuing skb if kernel socket has no input function */
nlk = nlk_sk(sock); nlk = nlk_sk(sock);
/* dst_portid can be changed in netlink_connect() */ /* dst_portid and sk_state can be changed in netlink_connect() */
if (sock->sk_state == NETLINK_CONNECTED && if (READ_ONCE(sock->sk_state) == NETLINK_CONNECTED &&
READ_ONCE(nlk->dst_portid) != nlk_sk(ssk)->portid) { READ_ONCE(nlk->dst_portid) != nlk_sk(ssk)->portid) {
sock_put(sock); sock_put(sock);
return ERR_PTR(-ECONNREFUSED); return ERR_PTR(-ECONNREFUSED);