xfrm: add missing rcu verbs to fix data-race
Subsystems (4)
PATCH 1/1 xfrm: add missing rcu verbs to fix data-race
KCSAN reported a data-race in xfrm_lookup_with_ifid() and xfrm_sk_free_policy() [1]
This should be solved using rcu_access_pointer() and rcu_assign_pointer() to avoid potential load/store-tearing.
[1]
BUG: KCSAN: data-race in sk_common_release / xfrm_lookup_with_ifid
write to 0xffff888121172668 of 8 bytes by task 22196 on cpu 0:
xfrm_sk_free_policy include/net/xfrm.h:1193 [inline] sk_common_release+0x18c/0x1d0 net/core/sock.c:3198 udp_lib_close+0x1f/0x30 include/net/udp.h:202 inet_release+0x86/0x100 net/ipv4/af_inet.c:427 inet6_release+0x4a/0x70 net/ipv6/af_inet6.c:470 __sock_release+0x85/0x160 net/socket.c:590 sock_close+0x24/0x30 net/socket.c:1268 __fput+0x1e1/0x520 fs/file_table.c:280 ____fput+0x1f/0x30 fs/file_table.c:313 task_work_run+0xf6/0x130 kernel/task_work.c:113 tracehook_notify_resume include/linux/tracehook.h:188 [inline] exit_to_usermode_loop+0x2b4/0x2c0 arch/x86/entry/common.c:163 prepare_exit_to_usermode arch/x86/entry/common.c:194 [inline] syscall_return_slowpath arch/x86/entry/common.c:274 [inline] do_syscall_64+0x353/0x370 arch/x86/entry/common.c:300 entry_SYSCALL_64_after_hwframe+0x44/0xa9
read to 0xffff888121172668 of 8 bytes by task 22201 on cpu 1:
xfrm_lookup_with_ifid+0xc0/0x1310 net/xfrm/xfrm_policy.c:3035 xfrm_lookup net/xfrm/xfrm_policy.c:3174 [inline] xfrm_lookup_route+0x44/0x100 net/xfrm/xfrm_policy.c:3185 ip6_dst_lookup_flow+0xde/0x120 net/ipv6/ip6_output.c:1159 inet6_csk_route_socket+0x2f7/0x420 net/ipv6/inet6_connection_sock.c:106 inet6_csk_xmit+0x91/0x1f0 net/ipv6/inet6_connection_sock.c:121 l2tp_xmit_core net/l2tp/l2tp_core.c:1030 [inline] l2tp_xmit_skb+0x8c9/0x8e0 net/l2tp/l2tp_core.c:1132 pppol2tp_sendmsg+0x2fc/0x3c0 net/l2tp/l2tp_ppp.c:325 sock_sendmsg_nosec net/socket.c:637 [inline] sock_sendmsg+0x9f/0xc0 net/socket.c:657 ___sys_sendmsg+0x2b7/0x5d0 net/socket.c:2311 __sys_sendmmsg+0x123/0x350 net/socket.c:2413 __do_sys_sendmmsg net/socket.c:2442 [inline] __se_sys_sendmmsg net/socket.c:2439 [inline] __x64_sys_sendmmsg+0x64/0x80 net/socket.c:2439 do_syscall_64+0xcc/0x370 arch/x86/entry/common.c:290 entry_SYSCALL_64_after_hwframe+0x44/0xa9
Reported by Kernel Concurrency Sanitizer on:
CPU: 1 PID: 22201 Comm: syz-executor.5 Not tainted 5.4.0-rc6+ #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
--- include/net/xfrm.h | 9 +++++---- net/smc/smc.h | 4 ++-- net/xfrm/xfrm_policy.c | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-)
include/net/xfrm.h +5 -4
@@ -1093,7 +1093,7 @@ static inline int __xfrm_policy_check2(struct sock *sk, int dir,struct net *net =dev_net (skb->dev);int ndir = dir | (reverse ?XFRM_POLICY_MASK +1 :0 ); -if (sk && sk->sk_policy[XFRM_POLICY_IN ]) +if (sk &&rcu_access_pointer (sk->sk_policy[XFRM_POLICY_IN ]))return __xfrm_policy_check (sk, ndir, skb, family); return (!net->xfrm.policy_count[dir] && !secpath_exists(skb)) ||
@@ -1171,7 +1171,8 @@ static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) { sk->sk_policy[0 ] =NULL ; sk->sk_policy[1 ] =NULL ; -if (unlikely (osk->sk_policy[0 ] || osk->sk_policy[1 ])) +if (unlikely (rcu_access_pointer (osk->sk_policy[0 ]) || +rcu_access_pointer (osk->sk_policy[1 ])))return __xfrm_sk_clone_policy (sk, osk);return 0 ; }
@@ -1185,12 +1186,12 @@ static inline void xfrm_sk_free_policy(struct sock *sk) pol =rcu_dereference_protected (sk->sk_policy[0 ],1 );if (unlikely (pol !=NULL )) {xfrm_policy_delete (pol,XFRM_POLICY_MAX ); - sk->sk_policy[0 ] =NULL ; +rcu_assign_pointer (sk->sk_policy[0 ],NULL ); } pol =rcu_dereference_protected (sk->sk_policy[1 ],1 );if (unlikely (pol !=NULL )) {xfrm_policy_delete (pol,XFRM_POLICY_MAX +1 ); - sk->sk_policy[1 ] =NULL ; +rcu_assign_pointer (sk->sk_policy[1 ],NULL ); } }
net/smc/smc.h +2 -2
@@ -253,8 +253,8 @@ static inline u32 ntoh24(u8 *net)#ifdef CONFIG_XFRM static inline bool using_ipsec (struct smc_sock *smc) { -return (smc->clcsock->sk->sk_policy[0 ] || - smc->clcsock->sk->sk_policy[1 ]) ? true : false; +return (rcu_access_pointer (smc->clcsock->sk->sk_policy[0 ]) || +rcu_access_pointer (smc->clcsock->sk->sk_policy[1 ])) ? true : false; }#else static inline bool using_ipsec (struct smc_sock *smc)
net/xfrm/xfrm_policy.c +2 -2
@@ -3032,7 +3032,7 @@ struct dst_entry *xfrm_lookup_with_ifid(struct net *net, route =NULL ; sk =sk_const_to_full_sk (sk); -if (sk && sk->sk_policy[XFRM_POLICY_OUT ]) { +if (sk &&rcu_access_pointer (sk->sk_policy[XFRM_POLICY_OUT ])) { num_pols =1 ; pols[0 ] =xfrm_sk_policy_lookup (sk,XFRM_POLICY_OUT , fl, family, if_id);
@@ -3557,7 +3557,7 @@ int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, pol =NULL ; sk =sk_to_full_sk (sk); -if (sk && sk->sk_policy[dir]) { +if (sk &&rcu_access_pointer (sk->sk_policy[dir])) { pol =xfrm_sk_policy_lookup (sk, dir, &fl, family, if_id);if (IS_ERR (pol)) {XFRM_INC_STATS (net,LINUX_MIB_XFRMINPOLERROR );
Signature · 1 line
2.24.0.432.g9d3f5f5b63-goog
David Miller
maintainerFrom: Eric Dumazet <[email protected]>
Date: Thu, 7 Nov 2019 19:47:01 -0800
Quoted 2 lines
KCSAN reported a data-race in
xfrm_lookup_with_ifid()andxfrm_sk_free_policy()[1]
Steffen please pick this up, thank you.
Herbert Xu
maintainerQuoted 2 lines · Eric Dumazet
KCSAN reported a data-race in
xfrm_lookup_with_ifid()andxfrm_sk_free_policy()[1]
I'm very uncomfortable with these warnings being enabled in KASAN unless there is a way to opt out of them without adding unnecessary READ_ONCE/WRITE_ONCE tags.
All they do is create patches such as this one that simply adds these tags without resolving the underlying issues (if there are any).
include/net/xfrm.h static inline int __xfrm_policy_check2(struct sock *sk, int dir, · line 1097
Quoted 11 lines
include/net/xfrm.h +1
-1@@ -1093,7 +1093,7 @@ static inline int __xfrm_policy_check2(struct sock *sk, int dir,struct net *net =dev_net (skb->dev);int ndir = dir | (reverse ?XFRM_POLICY_MASK +1 :0 ); -if (sk && sk->sk_policy[XFRM_POLICY_IN ]) +if (sk &&rcu_access_pointer (sk->sk_policy[XFRM_POLICY_IN ]))return __xfrm_policy_check (sk, ndir, skb, family);
This is simply an optimisation and we don't care if we get it wrong due to the lack of READ_ONCE/WRITE_ONCE. Even with the READ_ONCE tag, there is nothing stopping a policy from being added after the test returns false, or all policies from being deleted after the test returns true.
IOW this is simply unnecessary.
include/net/xfrm.h static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) · line 1177
Quoted 9 lines
include/net/xfrm.h +2
-1@@ -1171,7 +1171,8 @@ static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) { sk->sk_policy[0 ] =NULL ; sk->sk_policy[1 ] =NULL ; -if (unlikely (osk->sk_policy[0 ] || osk->sk_policy[1 ])) +if (unlikely (rcu_access_pointer (osk->sk_policy[0 ]) || +rcu_access_pointer (osk->sk_policy[1 ])))return __xfrm_sk_clone_policy (sk, osk);return 0 ;
These on the other hand are done under socket lock. IOW they are completely synchronised with respect to the write side which is also under socket lock so no tagging is necessary.
Incidentally rcu_access_pointer is now practically the same as rcu_dereference because the smp_read_barrier_depends has been moved over to READ_ONCE. In fact there is no performance difference between rcu_dereference and rcu_dereference_raw either.
I think we should either remove the smp_barrier_depends from rcu_access_pointer, or just get rid of rcu_access_pointer completely.
include/net/xfrm.h static inline void xfrm_sk_free_policy(struct sock *sk) · line 1194
Quoted 12 lines
include/net/xfrm.h +2
-2@@ -1185,12 +1186,12 @@ static inline void xfrm_sk_free_policy(struct sock *sk) pol =rcu_dereference_protected (sk->sk_policy[0 ],1 );if (unlikely (pol !=NULL )) {xfrm_policy_delete (pol,XFRM_POLICY_MAX ); - sk->sk_policy[0 ] =NULL ; +rcu_assign_pointer (sk->sk_policy[0 ],NULL ); } pol =rcu_dereference_protected (sk->sk_policy[1 ],1 );if (unlikely (pol !=NULL )) {xfrm_policy_delete (pol,XFRM_POLICY_MAX +1 ); - sk->sk_policy[1 ] =NULL ; +rcu_assign_pointer (sk->sk_policy[1 ],NULL );
These should use RCU_INIT_POINTER.
Quoted 5 lines
net/smc/smc.h +0
-0
net/smc/smc.h static inline u32 ntoh24(u8 *net) · line 260
Quoted 10 lines
net/smc/smc.h +2
-2@@ -253,8 +253,8 @@ static inline u32 ntoh24(u8 *net)#ifdef CONFIG_XFRM static inline bool using_ipsec (struct smc_sock *smc) { -return (smc->clcsock->sk->sk_policy[0 ] || - smc->clcsock->sk->sk_policy[1 ]) ? true : false; +return (rcu_access_pointer (smc->clcsock->sk->sk_policy[0 ]) || +rcu_access_pointer (smc->clcsock->sk->sk_policy[1 ])) ? true : false; }#else static inline bool using_ipsec (struct smc_sock *smc)
Now this could actually be a real bug because it doesn't appear to be an optimisation and it doesn't seem to be holding the socket lock either.
Again this shows that we shouldn't just add READ_ONCE/WRITE_ONCE tags because they might be papering over real bugs and help them hide better because people will automatically assume that using READ_ONCE/WRITE_ONCE means that the code is *safe*.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Quoted 31 lines · Herbert Xu
Quoted 2 lines · Eric Dumazet
KCSAN reported a data-race in
xfrm_lookup_with_ifid()andxfrm_sk_free_policy()[1]I'm very uncomfortable with these warnings being enabled in KASAN unless there is a way to opt out of them without adding unnecessary READ_ONCE/WRITE_ONCE tags.
All they do is create patches such as this one that simply adds these tags without resolving the underlying issues (if there are any).
include/net/xfrm.h static inline int __xfrm_policy_check2(struct sock *sk, int dir, · line 1097
Quoted 11 lines
include/net/xfrm.h +1
-1@@ -1093,7 +1093,7 @@ static inline int __xfrm_policy_check2(struct sock *sk, int dir,struct net *net =dev_net (skb->dev);int ndir = dir | (reverse ?XFRM_POLICY_MASK +1 :0 ); -if (sk && sk->sk_policy[XFRM_POLICY_IN ]) +if (sk &&rcu_access_pointer (sk->sk_policy[XFRM_POLICY_IN ]))return __xfrm_policy_check (sk, ndir, skb, family);This is simply an optimisation and we don't care if we get it wrong due to the lack of READ_ONCE/WRITE_ONCE. Even with the
READ_ONCEtag, there is nothing stopping a policy from being added after the test returns false, or all policies from being deleted after the test returns true.
Quoted 1 line
IOW this is simply unnecessary.
How do you suggest to silence the KCSAN warnings then ?
Quoted 23 lines
include/net/xfrm.h static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) · line 1177
Quoted 10 lines
include/net/xfrm.h +2
-1@@ -1171,7 +1171,8 @@ static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) { sk->sk_policy[0 ] =NULL ; sk->sk_policy[1 ] =NULL ; -if (unlikely (osk->sk_policy[0 ] || osk->sk_policy[1 ])) +if (unlikely (rcu_access_pointer (osk->sk_policy[0 ]) || +rcu_access_pointer (osk->sk_policy[1 ])))return __xfrm_sk_clone_policy (sk, osk);return 0 ;These on the other hand are done under socket lock. IOW they are completely synchronised with respect to the write side which is also under socket lock so no tagging is necessary.
Incidentally
rcu_access_pointeris now practically the same asrcu_dereferencebecause thesmp_read_barrier_dependshas been moved over toREAD_ONCE. In fact there is no performance difference betweenrcu_dereferenceandrcu_dereference_raweither.I think we should either remove the
smp_barrier_dependsfromrcu_access_pointer, or just get rid ofrcu_access_pointercompletely.
Quoted 14 lines
include/net/xfrm.h static inline void xfrm_sk_free_policy(struct sock *sk) · line 1194
Quoted 12 lines
include/net/xfrm.h +2
-2@@ -1185,12 +1186,12 @@ static inline void xfrm_sk_free_policy(struct sock *sk) pol =rcu_dereference_protected (sk->sk_policy[0 ],1 );if (unlikely (pol !=NULL )) {xfrm_policy_delete (pol,XFRM_POLICY_MAX ); - sk->sk_policy[0 ] =NULL ; +rcu_assign_pointer (sk->sk_policy[0 ],NULL ); } pol =rcu_dereference_protected (sk->sk_policy[1 ],1 );if (unlikely (pol !=NULL )) {xfrm_policy_delete (pol,XFRM_POLICY_MAX +1 ); - sk->sk_policy[1 ] =NULL ; +rcu_assign_pointer (sk->sk_policy[1 ],NULL );These should use
RCU_INIT_POINTER.
This does not matter anymore.
Please double check rcu_assign_pointer(), it handles the NULL case.
commit 3a37f7275cda5ad25c1fe9be8f20c76c60d175fa
Author: Paul E. McKenney <[email protected]> Date: Sun May 1 18:46:54 2016 -0700
rcu: No ordering for rcu_assign_pointer() of NULL
This commit does a compile-time check for rcu_assign_pointer() of NULL, and uses WRITE_ONCE() rather than smp_store_release() in that case.
Quoted 21 lines
Quoted 6 lines
net/smc/smc.h +0
-0net/smc/smc.h static inline u32 ntoh24(u8 *net) · line 260
Quoted 10 lines
net/smc/smc.h +2
-2@@ -253,8 +253,8 @@ static inline u32 ntoh24(u8 *net)#ifdef CONFIG_XFRM static inline bool using_ipsec (struct smc_sock *smc) { -return (smc->clcsock->sk->sk_policy[0 ] || - smc->clcsock->sk->sk_policy[1 ]) ? true : false; +return (rcu_access_pointer (smc->clcsock->sk->sk_policy[0 ]) || +rcu_access_pointer (smc->clcsock->sk->sk_policy[1 ])) ? true : false; }#else static inline bool using_ipsec (struct smc_sock *smc)Now this could actually be a real bug because it doesn't appear to be an optimisation and it doesn't seem to be holding the socket lock either.
Quoted 5 lines
Again this shows that we shouldn't just add READ_ONCE/WRITE_ONCE tags because they might be papering over real bugs and help them hide better because people will automatically assume that using READ_ONCE/WRITE_ONCE means that the code is *safe*.
Hmm, I will leave to you the resolution of this bug.
Thanks.