Skip to content

Commit fe74fdc

Browse files
dhowellsintel-lab-lkp
authored andcommitted
net: Fix suspicious RCU usage in bpf_sk_reuseport_detach()
bpf_sk_reuseport_detach() calls __rcu_dereference_sk_user_data_with_flags() to obtain the value of sk->sk_user_data, but that function is only usable if the RCU read lock is held, and neither that function nor any of its callers hold it. Fix this by adding a new helper, __rcu_dereference_sk_user_data_with_flags_check() that checks to see if sk->sk_callback_lock() is held and use that here instead. __rcu_dereference_sk_user_data_with_flags() then calls that, supplying false as condition indicating only the RCU read lock should be checked. Without this, the following warning can be occasionally observed: ============================= WARNING: suspicious RCU usage 6.0.0-rc1-build2+ torvalds#563 Not tainted ----------------------------- include/net/sock.h:592 suspicious rcu_dereference_check() usage! other info that might help us debug this: rcu_scheduler_active = 2, debug_locks = 1 5 locks held by locktest/29873: #0: ffff88812734b550 (&sb->s_type->i_mutex_key#9){+.+.}-{3:3}, at: __sock_release+0x77/0x121 #1: ffff88812f5621b0 (sk_lock-AF_INET){+.+.}-{0:0}, at: tcp_close+0x1c/0x70 #2: ffff88810312f5c8 (&h->lhash2[i].lock){+.+.}-{2:2}, at: inet_unhash+0x76/0x1c0 #3: ffffffff83768bb8 (reuseport_lock){+...}-{2:2}, at: reuseport_detach_sock+0x18/0xdd #4: ffff88812f562438 (clock-AF_INET){++..}-{2:2}, at: bpf_sk_reuseport_detach+0x24/0xa4 stack backtrace: CPU: 1 PID: 29873 Comm: locktest Not tainted 6.0.0-rc1-build2+ torvalds#563 Hardware name: ASUS All Series/H97-PLUS, BIOS 2306 10/09/2014 Call Trace: <TASK> dump_stack_lvl+0x4c/0x5f bpf_sk_reuseport_detach+0x6d/0xa4 reuseport_detach_sock+0x75/0xdd inet_unhash+0xa5/0x1c0 tcp_set_state+0x169/0x20f ? lockdep_sock_is_held+0x3a/0x3a ? __lock_release.isra.0+0x13e/0x220 ? reacquire_held_locks+0x1bb/0x1bb ? hlock_class+0x31/0x96 ? mark_lock+0x9e/0x1af __tcp_close+0x50/0x4b6 tcp_close+0x28/0x70 inet_release+0x8e/0xa7 __sock_release+0x95/0x121 sock_close+0x14/0x17 __fput+0x20f/0x36a task_work_run+0xa3/0xcc exit_to_user_mode_prepare+0x9c/0x14d syscall_exit_to_user_mode+0x18/0x44 entry_SYSCALL_64_after_hwframe+0x63/0xcd Changes ======= ver #2) - Changed to suggestion from Hawkins Jiawei to have a ..._check() function and make the original a special case of that. Fixes: cf8c1e9 ("net: refactor bpf_sk_reuseport_detach()") Signed-off-by: David Howells <[email protected]> cc: Hawkins Jiawei <[email protected]> cc: Jakub Kicinski <[email protected]> cc: [email protected] Link: https://lore.kernel.org/r/166064248071.3502205.10036394558814861778.stgit@warthog.procyon.org.uk # v1
1 parent ae806c7 commit fe74fdc

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

include/net/sock.h

+14-4
Original file line numberDiff line numberDiff line change
@@ -578,18 +578,22 @@ static inline bool sk_user_data_is_nocopy(const struct sock *sk)
578578
#define __sk_user_data(sk) ((*((void __rcu **)&(sk)->sk_user_data)))
579579

580580
/**
581-
* __rcu_dereference_sk_user_data_with_flags - return the pointer
581+
* __rcu_dereference_sk_user_data_with_flags_check - return the pointer
582582
* only if argument flags all has been set in sk_user_data. Otherwise
583583
* return NULL
584584
*
585585
* @sk: socket
586586
* @flags: flag bits
587+
* @condition: Condition under which non-RCU access may take place
588+
*
589+
* The caller must be holding the RCU read lock
587590
*/
588591
static inline void *
589-
__rcu_dereference_sk_user_data_with_flags(const struct sock *sk,
590-
uintptr_t flags)
592+
__rcu_dereference_sk_user_data_with_flags_check(const struct sock *sk,
593+
uintptr_t flags, bool condition)
591594
{
592-
uintptr_t sk_user_data = (uintptr_t)rcu_dereference(__sk_user_data(sk));
595+
uintptr_t sk_user_data =
596+
(uintptr_t)rcu_dereference_check(__sk_user_data(sk), condition);
593597

594598
WARN_ON_ONCE(flags & SK_USER_DATA_PTRMASK);
595599

@@ -598,6 +602,12 @@ __rcu_dereference_sk_user_data_with_flags(const struct sock *sk,
598602
return NULL;
599603
}
600604

605+
static inline void *
606+
__rcu_dereference_sk_user_data_with_flags(const struct sock *sk, uintptr_t flags)
607+
{
608+
return __rcu_dereference_sk_user_data_with_flags_check(sk, flags, false);
609+
}
610+
601611
#define rcu_dereference_sk_user_data(sk) \
602612
__rcu_dereference_sk_user_data_with_flags(sk, 0)
603613
#define __rcu_assign_sk_user_data_with_flags(sk, ptr, flags) \

kernel/bpf/reuseport_array.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ void bpf_sk_reuseport_detach(struct sock *sk)
2424
struct sock __rcu **socks;
2525

2626
write_lock_bh(&sk->sk_callback_lock);
27-
socks = __rcu_dereference_sk_user_data_with_flags(sk, SK_USER_DATA_BPF);
27+
socks = __rcu_dereference_sk_user_data_with_flags_check(
28+
sk, SK_USER_DATA_BPF, lockdep_is_held(&sk->sk_callback_lock));
2829
if (socks) {
2930
WRITE_ONCE(sk->sk_user_data, NULL);
3031
/*

0 commit comments

Comments
 (0)