diff options
author | Daniel Borkmann <daniel@iogearbox.net> | 2018-08-16 15:49:09 -0400 |
---|---|---|
committer | Alexei Starovoitov <ast@kernel.org> | 2018-08-16 17:58:08 -0400 |
commit | 166ab6f0a0702fdd4d865ad5090bf3094ed83428 (patch) | |
tree | 16e5a7b40f898efe4a8976cb2e179ec2f1b7ce3b | |
parent | d40b0116c94bd8fc2b63aae35ce8e66bb53bba42 (diff) |
bpf, sockmap: fix map elem deletion race with smap_stop_sock
The smap_start_sock() and smap_stop_sock() are each protected under
the sock->sk_callback_lock from their call-sites except in the case
of sock_map_delete_elem() where we drop the old socket from the map
slot. This is racy because the same sock could be part of multiple
sock maps, so we run smap_stop_sock() in parallel, and given at that
point psock->strp_enabled might be true on both CPUs, we might for
example wrongly restore the sk->sk_data_ready / sk->sk_write_space.
Therefore, hold the sock->sk_callback_lock as well on delete. Looks
like 2f857d04601a ("bpf: sockmap, remove STRPARSER map_flags and add
multi-map support") had this right, but later on e9db4ef6bf4c ("bpf:
sockhash fix omitted bucket lock in sock_close") removed it again
from delete leaving this smap_stop_sock() instance unprotected.
Fixes: e9db4ef6bf4c ("bpf: sockhash fix omitted bucket lock in sock_close")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
-rw-r--r-- | kernel/bpf/sockmap.c | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/kernel/bpf/sockmap.c b/kernel/bpf/sockmap.c index 94a324bc6afc..921cb6b8c862 100644 --- a/kernel/bpf/sockmap.c +++ b/kernel/bpf/sockmap.c | |||
@@ -1786,8 +1786,11 @@ static int sock_map_delete_elem(struct bpf_map *map, void *key) | |||
1786 | if (!psock) | 1786 | if (!psock) |
1787 | goto out; | 1787 | goto out; |
1788 | 1788 | ||
1789 | if (psock->bpf_parse) | 1789 | if (psock->bpf_parse) { |
1790 | write_lock_bh(&sock->sk_callback_lock); | ||
1790 | smap_stop_sock(psock, sock); | 1791 | smap_stop_sock(psock, sock); |
1792 | write_unlock_bh(&sock->sk_callback_lock); | ||
1793 | } | ||
1791 | smap_list_map_remove(psock, &stab->sock_map[k]); | 1794 | smap_list_map_remove(psock, &stab->sock_map[k]); |
1792 | smap_release_sock(psock, sock); | 1795 | smap_release_sock(psock, sock); |
1793 | out: | 1796 | out: |