net: annotate data-races around sock->ops
Subsystems (8)
PATCH 1/1 net: annotate data-races around sock->ops
IPV6_ADDRFORM socket option is evil, because it can change sock->ops while other threads might read it. Same issue for sk->sk_family being set to AF_INET.
Adding READ_ONCE() over sock->ops reads is needed for sockets that might be impacted by IPV6_ADDRFORM.
Note that mptcp_is_tcpsk() can also overwrite sock->ops.
Adding annotations for all sk->sk_family reads will require more patches :/
BUG: KCSAN: data-race in ____sys_sendmsg / do_ipv6_setsockopt
write to 0xffff888109f24ca0 of 8 bytes by task 4470 on cpu 0:
do_ipv6_setsockopt+0x2c5e/0x2ce0 net/ipv6/ipv6_sockglue.c:491 ipv6_setsockopt+0x57/0x130 net/ipv6/ipv6_sockglue.c:1012 udpv6_setsockopt+0x95/0xa0 net/ipv6/udp.c:1690 sock_common_setsockopt+0x61/0x70 net/core/sock.c:3663 __sys_setsockopt+0x1c3/0x230 net/socket.c:2273 __do_sys_setsockopt net/socket.c:2284 [inline] __se_sys_setsockopt net/socket.c:2281 [inline] __x64_sys_setsockopt+0x66/0x80 net/socket.c:2281 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x41/0xc0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd
read to 0xffff888109f24ca0 of 8 bytes by task 4469 on cpu 1:
sock_sendmsg_nosec net/socket.c:724 [inline] sock_sendmsg net/socket.c:747 [inline] ____sys_sendmsg+0x349/0x4c0 net/socket.c:2503 ___sys_sendmsg net/socket.c:2557 [inline] __sys_sendmmsg+0x263/0x500 net/socket.c:2643 __do_sys_sendmmsg net/socket.c:2672 [inline] __se_sys_sendmmsg net/socket.c:2669 [inline] __x64_sys_sendmmsg+0x57/0x60 net/socket.c:2669 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x41/0xc0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd
value changed: 0xffffffff850e32b8 -> 0xffffffff850da890
Reported by Kernel Concurrency Sanitizer on:
CPU: 1 PID: 4469 Comm: syz-executor.1 Not tainted 6.4.0-rc5-syzkaller-00313-g4c605260bc60 #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/25/2023
--- include/linux/net.h | 2 +- net/9p/trans_fd.c | 4 +- net/core/scm.c | 3 +- net/core/skmsg.c | 8 ++- net/core/sock.c | 24 +++++-- net/ipv6/ipv6_sockglue.c | 8 +-- net/mptcp/protocol.c | 8 +-- net/socket.c | 136 +++++++++++++++++++++++---------------- net/unix/scm.c | 3 +- 9 files changed, 118 insertions(+), 78 deletions(-)
include/linux/net.h +1 -1
@@ -123,7 +123,7 @@ struct socket {struct file *file;struct sock *sk; -const struct proto_ops *ops; +const struct proto_ops *ops;/* Might change with IPV6_ADDRFORM or MPTCP. */ struct socket_wq wq; };
net/9p/trans_fd.c +2 -2
@@ -1019,7 +1019,7 @@ p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args) } } - err = csocket->ops->connect (csocket, + err =READ_ONCE (csocket->ops)->connect (csocket, (struct sockaddr *)&sin_server,sizeof (struct sockaddr_in ),0 );if (err <0 ) {
@@ -1060,7 +1060,7 @@ p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)return err; } - err = csocket->ops->connect (csocket, (struct sockaddr *)&sun_server, + err =READ_ONCE (csocket->ops)->connect (csocket, (struct sockaddr *)&sun_server,sizeof (struct sockaddr_un ) -1 ,0 );if (err <0 ) { pr_err("%s (%d): problem connecting socket: %s: %d\n",
net/core/scm.c +2 -1
@@ -130,6 +130,7 @@ EXPORT_SYMBOL(__scm_destroy);int __scm_send (struct socket *sock,struct msghdr *msg,struct scm_cookie *p) { +const struct proto_ops *ops =READ_ONCE (sock->ops);struct cmsghdr *cmsg;int err;
@@ -153,7 +154,7 @@ int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)switch (cmsg->cmsg_type) {case SCM_RIGHTS : -if (!sock->ops || sock->ops->family !=PF_UNIX ) +if (!ops || ops->family !=PF_UNIX ) goto error; err=scm_fp_copy (cmsg, &p->fp);if (err<0 )
net/core/skmsg.c +6 -2
@@ -1198,13 +1198,17 @@ static int sk_psock_verdict_recv(struct sock *sk, struct sk_buff *skb)static void sk_psock_verdict_data_ready (struct sock *sk) {struct socket *sock = sk->sk_socket; +const struct proto_ops *ops;int copied;trace_sk_data_ready (sk); -if (unlikely (!sock || !sock->ops || !sock->ops->read_skb)) +if (unlikely (!sock))return ; - copied = sock->ops->read_skb (sk, sk_psock_verdict_recv); + ops =READ_ONCE (sock->ops); +if (!ops || !ops->read_skb) +return ; + copied = ops->read_skb (sk, sk_psock_verdict_recv);if (copied >=0 ) {struct sk_psock *psock;
net/core/sock.c +17 -7
@@ -1277,14 +1277,19 @@ int sk_setsockopt(struct sock *sk, int level, int optname,break ;case SO_RCVLOWAT : + { +int (*set_rcvlowat)(struct sock *sk,int val) =NULL ; +if (val <0 ) val =INT_MAX ; -if (sock && sock->ops->set_rcvlowat) - ret = sock->ops->set_rcvlowat (sk, val); +if (sock) + set_rcvlowat =READ_ONCE (sock->ops)->set_rcvlowat; +if (set_rcvlowat) + ret =set_rcvlowat (sk, val);else WRITE_ONCE (sk->sk_rcvlowat, val ? :1 );break ; - + }case SO_RCVTIMEO_OLD :case SO_RCVTIMEO_NEW : ret = sock_set_timeout(&sk->sk_rcvtimeo, optval,
@@ -1379,11 +1384,16 @@ int sk_setsockopt(struct sock *sk, int level, int optname,break ;case SO_PEEK_OFF : -if (sock->ops->set_peek_off) - ret = sock->ops->set_peek_off (sk, val); + { +int (*set_peek_off)(struct sock *sk,int val); + + set_peek_off =READ_ONCE (sock->ops)->set_peek_off; +if (set_peek_off) + ret =set_peek_off (sk, val);else ret = -EOPNOTSUPP ;break ; + }case SO_NOFCS :sock_valbool_flag (sk,SOCK_NOFCS , valbool);
@@ -1816,7 +1826,7 @@ int sk_getsockopt(struct sock *sk, int level, int optname, {struct sockaddr_storage address; - lv = sock->ops->getname (sock, (struct sockaddr *)&address,2 ); + lv =READ_ONCE (sock->ops)->getname (sock, (struct sockaddr *)&address,2 );if (lv <0 )return -ENOTCONN ;if (lv < len)
@@ -1858,7 +1868,7 @@ int sk_getsockopt(struct sock *sk, int level, int optname,break ;case SO_PEEK_OFF : -if (!sock->ops->set_peek_off) +if (!READ_ONCE (sock->ops)->set_peek_off)return -EOPNOTSUPP ; v.val =READ_ONCE (sk->sk_peek_off);
net/ipv6/ipv6_sockglue.c +4 -4
@@ -474,8 +474,8 @@ int do_ipv6_setsockopt(struct sock *sk, int level, int optname,WRITE_ONCE (sk->sk_prot, &tcp_prot);/* Paired with READ_ONCE() in tcp_(get|set)sockopt() */ WRITE_ONCE (icsk->icsk_af_ops, &ipv4_specific); - sk->sk_socket->ops = &inet_stream_ops; - sk->sk_family =PF_INET ; +WRITE_ONCE (sk->sk_socket->ops, &inet_stream_ops); +WRITE_ONCE (sk->sk_family,PF_INET );tcp_sync_mss (sk, icsk->icsk_pmtu_cookie); }else {struct proto *prot = &udp_prot;
@@ -488,8 +488,8 @@ int do_ipv6_setsockopt(struct sock *sk, int level, int optname,/* 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 ; +WRITE_ONCE (sk->sk_socket->ops, &inet_dgram_ops); +WRITE_ONCE (sk->sk_family,PF_INET ); }/* Disable all options not to allocate memory anymore,
net/mptcp/protocol.c +4 -4
@@ -67,11 +67,11 @@ static bool mptcp_is_tcpsk(struct sock *sk)* Hand the socket over to tcp so all further socket ops * bypass mptcp. */ - sock->ops = &inet_stream_ops; +WRITE_ONCE (sock->ops, &inet_stream_ops);return true;#if IS_ENABLED (CONFIG_MPTCP_IPV6 ) } else if (unlikely(sk->sk_prot == &tcpv6_prot)) { - sock->ops = &inet6_stream_ops; +WRITE_ONCE (sock->ops, &inet6_stream_ops);return true;#endif }
@@ -3683,7 +3683,7 @@ static int mptcp_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) goto unlock; } - err = ssock->ops->bind (ssock, uaddr, addr_len); + err =READ_ONCE (ssock->ops)->bind (ssock, uaddr, addr_len);if (!err)mptcp_copy_inaddrs (sock->sk, ssock->sk);
@@ -3717,7 +3717,7 @@ static int mptcp_listen(struct socket *sock, int backlog)inet_sk_state_store (sk,TCP_LISTEN );sock_set_flag (sk,SOCK_RCU_FREE ); - err = ssock->ops->listen (ssock, backlog); + err =READ_ONCE (ssock->ops)->listen (ssock, backlog);inet_sk_state_store (sk,inet_sk_state_load (ssock->sk));if (!err) {sock_prot_inuse_add (sock_net (sk), sk->sk_prot,1 );
net/socket.c +80 -56
438 lines not on this page · raw message
net/unix/scm.c +2 -1
@@ -29,10 +29,11 @@ struct sock *unix_get_socket(struct file *filp)/* Socket ? */ if (S_ISSOCK (inode->i_mode) && !(filp->f_mode &FMODE_PATH )) {struct socket *sock =SOCKET_I (inode); +const struct proto_ops *ops =READ_ONCE (sock->ops);struct sock *s = sock->sk;/* PF_UNIX ? */ -if (s && sock->ops && sock->ops->family ==PF_UNIX ) +if (s && ops && ops->family ==PF_UNIX ) u_sock = s; }else {/* Could be an io_uring instance */
Signature · 1 line
2.41.0.640.ga95def55d0-goog
From: Eric Dumazet <[email protected]>
Date: Tue, 8 Aug 2023 13:58:09 +0000
Quoted 40 lines
IPV6_ADDRFORMsocket option is evil, because it can changesock->opswhile other threads might read it. Same issue forsk->sk_familybeing set toAF_INET.Adding
READ_ONCE()oversock->opsreads is needed for sockets that might be impacted byIPV6_ADDRFORM.Note that
mptcp_is_tcpsk()can also overwritesock->ops.Adding annotations for all
sk->sk_familyreads will require more patches :/BUG: KCSAN: data-race in ____sys_sendmsg / do_ipv6_setsockoptwrite to
0xffff888109f24ca0of 8 bytes by task 4470 on cpu 0:do_ipv6_setsockopt+0x2c5e/0x2ce0 net/ipv6/ipv6_sockglue.c:491 ipv6_setsockopt+0x57/0x130 net/ipv6/ipv6_sockglue.c:1012 udpv6_setsockopt+0x95/0xa0 net/ipv6/udp.c:1690 sock_common_setsockopt+0x61/0x70 net/core/sock.c:3663 __sys_setsockopt+0x1c3/0x230 net/socket.c:2273 __do_sys_setsockopt net/socket.c:2284 [inline] __se_sys_setsockopt net/socket.c:2281 [inline] __x64_sys_setsockopt+0x66/0x80 net/socket.c:2281 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x41/0xc0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcdread to
0xffff888109f24ca0of 8 bytes by task 4469 on cpu 1:sock_sendmsg_nosec net/socket.c:724 [inline] sock_sendmsg net/socket.c:747 [inline] ____sys_sendmsg+0x349/0x4c0 net/socket.c:2503 ___sys_sendmsg net/socket.c:2557 [inline] __sys_sendmmsg+0x263/0x500 net/socket.c:2643 __do_sys_sendmmsg net/socket.c:2672 [inline] __se_sys_sendmmsg net/socket.c:2669 [inline] __x64_sys_sendmmsg+0x57/0x60 net/socket.c:2669 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x41/0xc0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd
Quoted 8 lines
value changed:
0xffffffff850e32b8->0xffffffff850da890Reported by Kernel Concurrency Sanitizer on:
CPU: 1 PID: 4469 Comm: syz-executor.1 Not tainted 6.4.0-rc5-syzkaller-00313-g4c605260bc60 #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/25/2023Reported-by syzbot <[email protected]>Signed-off-by Eric Dumazet <[email protected]>
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Tue, 8 Aug 2023 13:58:09 +0000 you wrote:
IPV6_ADDRFORMsocket option is evil, because it can changesock->opswhile other threads might read it. Same issue forsk->sk_familybeing set toAF_INET.Adding
READ_ONCE()oversock->opsreads is needed for sockets that might be impacted byIPV6_ADDRFORM.[...]
Here is the summary with links:
- [net-next] net: annotate data-races around sock->ops
https://git.kernel.org/netdev/net-next/c/1ded5e5a5931
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Paolo Abeni
maintainerHi,
Quoted 11 lines · Eric Dumazet
IPV6_ADDRFORMsocket option is evil, because it can changesock->opswhile other threads might read it. Same issue forsk->sk_familybeing set toAF_INET.Adding
READ_ONCE()oversock->opsreads is needed for sockets that might be impacted byIPV6_ADDRFORM.Note that
mptcp_is_tcpsk()can also overwritesock->ops.Adding annotations for all
sk->sk_familyreads will require more patches :/
I was unable to give the above a proper look before due to OoO on my side.
The mptcp code calls mptcp_is_tcpsk() only before the fd for the newly accepted socket is installed, so we should not have concurrent racing access to sock->ops?!? Do you have any related splat handy?
Thanks,
Paolo
Quoted 16 lines · Paolo Abeni
Hi,
Quoted 11 lines · Eric Dumazet
IPV6_ADDRFORMsocket option is evil, because it can changesock->opswhile other threads might read it. Same issue forsk->sk_familybeing set toAF_INET.Adding
READ_ONCE()oversock->opsreads is needed for sockets that might be impacted byIPV6_ADDRFORM.Note that
mptcp_is_tcpsk()can also overwritesock->ops.Adding annotations for all
sk->sk_familyreads will require more patches :/
Quoted 7 lines
I was unable to give the above a proper look before due to OoO on my side.
The mptcp code calls
mptcp_is_tcpsk()only before the fd for the newly accepted socket is installed, so we should not have concurrent racing access tosock->ops?!? Do you have any related splat handy?
syzbot splat was on another layer. I tried to fix all sites that could trigger a similar issue.
BUG: KCSAN: data-race in ____sys_sendmsg / do_ipv6_setsockopt
write to 0xffff888109f24ca0 of 8 bytes by task 4470 on cpu 0:
do_ipv6_setsockopt+0x2c5e/0x2ce0 net/ipv6/ipv6_sockglue.c:491 ipv6_setsockopt+0x57/0x130 net/ipv6/ipv6_sockglue.c:1012 udpv6_setsockopt+0x95/0xa0 net/ipv6/udp.c:1690 sock_common_setsockopt+0x61/0x70 net/core/sock.c:3663 __sys_setsockopt+0x1c3/0x230 net/socket.c:2273 __do_sys_setsockopt net/socket.c:2284 [inline] __se_sys_setsockopt net/socket.c:2281 [inline] __x64_sys_setsockopt+0x66/0x80 net/socket.c:2281 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x41/0xc0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd
read to 0xffff888109f24ca0 of 8 bytes by task 4469 on cpu 1:
sock_sendmsg_nosec net/socket.c:724 [inline] sock_sendmsg net/socket.c:747 [inline] ____sys_sendmsg+0x349/0x4c0 net/socket.c:2503 ___sys_sendmsg net/socket.c:2557 [inline] __sys_sendmmsg+0x263/0x500 net/socket.c:2643 __do_sys_sendmmsg net/socket.c:2672 [inline] __se_sys_sendmmsg net/socket.c:2669 [inline] __x64_sys_sendmmsg+0x57/0x60 net/socket.c:2669 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x41/0xc0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd
value changed: 0xffffffff850e32b8 -> 0xffffffff850da890
Reported by Kernel Concurrency Sanitizer on:
CPU: 1 PID: 4469 Comm: syz-executor.1 Not tainted 6.4.0-rc5-syzkaller-00313-g4c605260bc60 #0 Hardware name: Google Google Compute Engine/Google Compute Engine,
BIOS Google 05/25/2023
Maybe MPTCP side (mptcp_is_tcpsk()) was ok,
but the fact that mptcp_is_tcpsk() was able to write over sock->ops was a bit strange to me.
mptcp_is_tcpsk() should answer a question, with a read-only argument.
I suggest changing the name of the helper to better reflect what it is doing.
Paolo Abeni
maintainerQuoted 61 lines · Eric Dumazet
Quoted 13 lines · Paolo Abeni
Quoted 11 lines · Eric Dumazet
IPV6_ADDRFORMsocket option is evil, because it can changesock->opswhile other threads might read it. Same issue forsk->sk_familybeing set toAF_INET.Adding
READ_ONCE()oversock->opsreads is needed for sockets that might be impacted byIPV6_ADDRFORM.Note that
mptcp_is_tcpsk()can also overwritesock->ops.Adding annotations for all
sk->sk_familyreads will require more patches :/Quoted 7 lines
I was unable to give the above a proper look before due to OoO on my side.
The mptcp code calls
mptcp_is_tcpsk()only before the fd for the newly accepted socket is installed, so we should not have concurrent racing access tosock->ops?!? Do you have any related splat handy?syzbot splat was on another layer. I tried to fix all sites that could trigger a similar issue.
BUG: KCSAN: data-race in ____sys_sendmsg / do_ipv6_setsockoptwrite to
0xffff888109f24ca0of 8 bytes by task 4470 on cpu 0:do_ipv6_setsockopt+0x2c5e/0x2ce0 net/ipv6/ipv6_sockglue.c:491 ipv6_setsockopt+0x57/0x130 net/ipv6/ipv6_sockglue.c:1012 udpv6_setsockopt+0x95/0xa0 net/ipv6/udp.c:1690 sock_common_setsockopt+0x61/0x70 net/core/sock.c:3663 __sys_setsockopt+0x1c3/0x230 net/socket.c:2273 __do_sys_setsockopt net/socket.c:2284 [inline] __se_sys_setsockopt net/socket.c:2281 [inline] __x64_sys_setsockopt+0x66/0x80 net/socket.c:2281 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x41/0xc0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcdread to
0xffff888109f24ca0of 8 bytes by task 4469 on cpu 1:sock_sendmsg_nosec net/socket.c:724 [inline] sock_sendmsg net/socket.c:747 [inline] ____sys_sendmsg+0x349/0x4c0 net/socket.c:2503 ___sys_sendmsg net/socket.c:2557 [inline] __sys_sendmmsg+0x263/0x500 net/socket.c:2643 __do_sys_sendmmsg net/socket.c:2672 [inline] __se_sys_sendmmsg net/socket.c:2669 [inline] __x64_sys_sendmmsg+0x57/0x60 net/socket.c:2669 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x41/0xc0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcdvalue changed:
0xffffffff850e32b8->0xffffffff850da890Reported by Kernel Concurrency Sanitizer on:
CPU: 1 PID: 4469 Comm: syz-executor.1 Not tainted 6.4.0-rc5-syzkaller-00313-g4c605260bc60 #0 Hardware name: Google Google Compute Engine/Google Compute Engine,BIOS Google 05/25/2023
Quoted 7 lines
Maybe MPTCP side (
mptcp_is_tcpsk()) was ok,
but the fact thatmptcp_is_tcpsk()was able to write oversock->opswas a bit strange to me.
mptcp_is_tcpsk()should answer a question, with a read-only argument.I suggest changing the name of the helper to better reflect what it is doing.
Thanks for double checking. Indeed such helper name is confusing, and should be renamed; I'll try to take care of it after the merge window.
Cheers,
Paolo