From: Kuniyuki Iwashima <kuniyu@xxxxxxxxxx> commit 364f997b5cfe1db0d63a390fe7c801fa2b3115f6 upstream. Commit 086d49058cd8 ("ipv6: annotate some data-races around sk->sk_prot") fixed some data-races around sk->sk_prot but it was not enough. Some functions in inet6_(stream|dgram)_ops still access sk->sk_prot without lock_sock() or rtnl_lock(), so they need READ_ONCE() to avoid load tearing. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Kuniyuki Iwashima <kuniyu@xxxxxxxxxx> Signed-off-by: Jakub Kicinski <kuba@xxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- net/core/sock.c | 6 ++++-- net/ipv4/af_inet.c | 23 ++++++++++++++++------- net/ipv6/ipv6_sockglue.c | 4 ++-- 3 files changed, 22 insertions(+), 11 deletions(-) --- a/net/core/sock.c +++ b/net/core/sock.c @@ -3413,7 +3413,8 @@ int sock_common_getsockopt(struct socket { struct sock *sk = sock->sk; - return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen); + /* IPV6_ADDRFORM can change sk->sk_prot under us. */ + return READ_ONCE(sk->sk_prot)->getsockopt(sk, level, optname, optval, optlen); } EXPORT_SYMBOL(sock_common_getsockopt); @@ -3440,7 +3441,8 @@ int sock_common_setsockopt(struct socket { struct sock *sk = sock->sk; - return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen); + /* IPV6_ADDRFORM can change sk->sk_prot under us. */ + return READ_ONCE(sk->sk_prot)->setsockopt(sk, level, optname, optval, optlen); } EXPORT_SYMBOL(sock_common_setsockopt); --- a/net/ipv4/af_inet.c +++ b/net/ipv4/af_inet.c @@ -565,22 +565,27 @@ int inet_dgram_connect(struct socket *so int addr_len, int flags) { struct sock *sk = sock->sk; + const struct proto *prot; int err; if (addr_len < sizeof(uaddr->sa_family)) return -EINVAL; + + /* IPV6_ADDRFORM can change sk->sk_prot under us. */ + prot = READ_ONCE(sk->sk_prot); + if (uaddr->sa_family == AF_UNSPEC) - return sk->sk_prot->disconnect(sk, flags); + return prot->disconnect(sk, flags); if (BPF_CGROUP_PRE_CONNECT_ENABLED(sk)) { - err = sk->sk_prot->pre_connect(sk, uaddr, addr_len); + err = prot->pre_connect(sk, uaddr, addr_len); if (err) return err; } if (data_race(!inet_sk(sk)->inet_num) && inet_autobind(sk)) return -EAGAIN; - return sk->sk_prot->connect(sk, uaddr, addr_len); + return prot->connect(sk, uaddr, addr_len); } EXPORT_SYMBOL(inet_dgram_connect); @@ -743,10 +748,11 @@ EXPORT_SYMBOL(inet_stream_connect); int inet_accept(struct socket *sock, struct socket *newsock, int flags, bool kern) { - struct sock *sk1 = sock->sk; + struct sock *sk1 = sock->sk, *sk2; int err = -EINVAL; - struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err, kern); + /* IPV6_ADDRFORM can change sk->sk_prot under us. */ + sk2 = READ_ONCE(sk1->sk_prot)->accept(sk1, flags, &err, kern); if (!sk2) goto do_err; @@ -834,12 +840,15 @@ ssize_t inet_sendpage(struct socket *soc size_t size, int flags) { struct sock *sk = sock->sk; + const struct proto *prot; if (unlikely(inet_send_prepare(sk))) return -EAGAIN; - if (sk->sk_prot->sendpage) - return sk->sk_prot->sendpage(sk, page, offset, size, flags); + /* IPV6_ADDRFORM can change sk->sk_prot under us. */ + prot = READ_ONCE(sk->sk_prot); + if (prot->sendpage) + return prot->sendpage(sk, page, offset, size, flags); return sock_no_sendpage(sock, page, offset, size, flags); } EXPORT_SYMBOL(inet_sendpage); --- a/net/ipv6/ipv6_sockglue.c +++ b/net/ipv6/ipv6_sockglue.c @@ -475,7 +475,7 @@ static int do_ipv6_setsockopt(struct soc sock_prot_inuse_add(net, sk->sk_prot, -1); sock_prot_inuse_add(net, &tcp_prot, 1); - /* Paired with READ_ONCE(sk->sk_prot) in net/ipv6/af_inet6.c */ + /* Paired with READ_ONCE(sk->sk_prot) in inet6_stream_ops */ WRITE_ONCE(sk->sk_prot, &tcp_prot); icsk->icsk_af_ops = &ipv4_specific; sk->sk_socket->ops = &inet_stream_ops; @@ -490,7 +490,7 @@ static int do_ipv6_setsockopt(struct soc sock_prot_inuse_add(net, sk->sk_prot, -1); sock_prot_inuse_add(net, prot, 1); - /* Paired with READ_ONCE(sk->sk_prot) in net/ipv6/af_inet6.c */ + /* Paired with READ_ONCE(sk->sk_prot) in inet6_dgram_ops */ WRITE_ONCE(sk->sk_prot, prot); sk->sk_socket->ops = &inet_dgram_ops; sk->sk_family = PF_INET; Patches currently in stable-queue which might be from kuniyu@xxxxxxxxxx are queue-5.15/af_unix-annotate-data-race-of-net-unx.sysctl_max_dgr.patch queue-5.15/nfs-leave-pages-in-the-pagecache-if-readpage-failed.patch queue-5.15/af_unix-annotate-data-race-of-sk-sk_shutdown-in-sk_d.patch queue-5.15/af_unix-clean-up-some-sock_net-uses.patch queue-5.15/af_unix-read-with-msg_peek-loops-if-the-first-unread.patch queue-5.15/af_unix-annotate-data-race-of-sk-sk_state-in-unix_st.patch-5290 queue-5.15/tcp-fix-data-races-around-icsk-icsk_af_ops.patch queue-5.15/net-do-not-leave-a-dangling-sk-pointer-when-socket-creation-fails.patch queue-5.15/af_unix-annotate-data-race-of-sk-sk_state-in-unix_in.patch queue-5.15/af_unix-annodate-data-races-around-sk-sk_state-for-w.patch queue-5.15/ipv6-fix-data-races-around-sk-sk_prot.patch queue-5.15/af_unix-set-sk-sk_state-under-unix_state_lock-for-tr.patch queue-5.15/af_unix-annotate-data-race-of-sk-sk_state-in-unix_st.patch queue-5.15/af_unix-annotate-data-races-around-sk-sk_state-in-un.patch-6162 queue-5.15/fix-race-for-duplicate-reqsk-on-identical-syn.patch queue-5.15/af_unix-annotate-data-races-around-sk-sk_state-in-se.patch queue-5.15/af_unix-use-unix_recvq_full_lockless-in-unix_stream_.patch queue-5.15/af_unix-use-skb_queue_len_lockless-in-sk_diag_show_r.patch queue-5.15/af_unix-use-skb_queue_empty_lockless-in-unix_release.patch queue-5.15/af_unix-annotate-data-races-around-sk-sk_state-in-un.patch