Skip to content

Commit c9a368f

Browse files
iamkafaiborkmann
authored andcommitted
bpf: net: Avoid incorrect bpf_sk_reuseport_detach call
bpf_sk_reuseport_detach is currently called when sk->sk_user_data is not NULL. It is incorrect because sk->sk_user_data may not be managed by the bpf's reuseport_array. It has been reported in [1] that, the bpf_sk_reuseport_detach() which is called from udp_lib_unhash() has corrupted the sk_user_data managed by l2tp. This patch solves it by using another bit (defined as SK_USER_DATA_BPF) of the sk_user_data pointer value. It marks that a sk_user_data is managed/owned by BPF. The patch depends on a PTRMASK introduced in commit f1ff5ce ("net, sk_msg: Clear sk_user_data pointer on clone if tagged"). [ Note: sk->sk_user_data is used by bpf's reuseport_array only when a sk is added to the bpf's reuseport_array. i.e. doing setsockopt(SO_REUSEPORT) and having "sk->sk_reuseport == 1" alone will not stop sk->sk_user_data being used by other means. ] [1]: https://lore.kernel.org/netdev/[email protected]/ Fixes: 5dc4c4b ("bpf: Introduce BPF_MAP_TYPE_REUSEPORT_SOCKARRAY") Reported-by: James Chapman <[email protected]> Reported-by: [email protected] Signed-off-by: Martin KaFai Lau <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Tested-by: James Chapman <[email protected]> Acked-by: James Chapman <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent f3dda7a commit c9a368f

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

include/net/sock.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,8 @@ enum sk_pacing {
533533
* be copied.
534534
*/
535535
#define SK_USER_DATA_NOCOPY 1UL
536-
#define SK_USER_DATA_PTRMASK ~(SK_USER_DATA_NOCOPY)
536+
#define SK_USER_DATA_BPF 2UL /* Managed by BPF */
537+
#define SK_USER_DATA_PTRMASK ~(SK_USER_DATA_NOCOPY | SK_USER_DATA_BPF)
537538

538539
/**
539540
* sk_user_data_is_nocopy - Test if sk_user_data pointer must not be copied

kernel/bpf/reuseport_array.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void bpf_sk_reuseport_detach(struct sock *sk)
2424

2525
write_lock_bh(&sk->sk_callback_lock);
2626
sk_user_data = (uintptr_t)sk->sk_user_data;
27-
if (sk_user_data) {
27+
if (sk_user_data & SK_USER_DATA_BPF) {
2828
struct sock __rcu **socks;
2929

3030
socks = (void *)(sk_user_data & SK_USER_DATA_PTRMASK);
@@ -309,7 +309,8 @@ int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
309309
if (err)
310310
goto put_file_unlock;
311311

312-
sk_user_data = (uintptr_t)&array->ptrs[index] | SK_USER_DATA_NOCOPY;
312+
sk_user_data = (uintptr_t)&array->ptrs[index] | SK_USER_DATA_NOCOPY |
313+
SK_USER_DATA_BPF;
313314
WRITE_ONCE(nsk->sk_user_data, (void *)sk_user_data);
314315
rcu_assign_pointer(array->ptrs[index], nsk);
315316
free_osk = osk;

0 commit comments

Comments
 (0)