diff options
author | John Fastabend <john.fastabend@gmail.com> | 2018-01-04 23:02:09 -0500 |
---|---|---|
committer | Daniel Borkmann <daniel@iogearbox.net> | 2018-01-06 18:01:46 -0500 |
commit | 5731a879d03bdaa00265f8ebc32dfd0e65d25276 (patch) | |
tree | 3a9d68d5c946353b0f0583e3440655e555826d60 | |
parent | 5133550296d43236439494aa955bfb765a89f615 (diff) |
bpf: sockmap missing NULL psock check
Add psock NULL check to handle a racing sock event that can get the
sk_callback_lock before this case but after xchg happens causing the
refcnt to hit zero and sock user data (psock) to be null and queued
for garbage collection.
Also add a comment in the code because this is a bit subtle and
not obvious in my opinion.
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
-rw-r--r-- | kernel/bpf/sockmap.c | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/kernel/bpf/sockmap.c b/kernel/bpf/sockmap.c index 5ee2e41893d9..1712d319c2d8 100644 --- a/kernel/bpf/sockmap.c +++ b/kernel/bpf/sockmap.c | |||
@@ -591,8 +591,15 @@ static void sock_map_free(struct bpf_map *map) | |||
591 | 591 | ||
592 | write_lock_bh(&sock->sk_callback_lock); | 592 | write_lock_bh(&sock->sk_callback_lock); |
593 | psock = smap_psock_sk(sock); | 593 | psock = smap_psock_sk(sock); |
594 | smap_list_remove(psock, &stab->sock_map[i]); | 594 | /* This check handles a racing sock event that can get the |
595 | smap_release_sock(psock, sock); | 595 | * sk_callback_lock before this case but after xchg happens |
596 | * causing the refcnt to hit zero and sock user data (psock) | ||
597 | * to be null and queued for garbage collection. | ||
598 | */ | ||
599 | if (likely(psock)) { | ||
600 | smap_list_remove(psock, &stab->sock_map[i]); | ||
601 | smap_release_sock(psock, sock); | ||
602 | } | ||
596 | write_unlock_bh(&sock->sk_callback_lock); | 603 | write_unlock_bh(&sock->sk_callback_lock); |
597 | } | 604 | } |
598 | rcu_read_unlock(); | 605 | rcu_read_unlock(); |