From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Fastabend Subject: [bpf-net PATCH v2] bpf: sockmap missing NULL psock check Date: Thu, 04 Jan 2018 20:02:09 -0800 Message-ID: <20180105040209.11195.85577.stgit@john-Precision-Tower-5810> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Cc: netdev@vger.kernel.org To: borkmann@iogearbox.net, alexei.starovoitov@gmail.com Return-path: Received: from mail-pf0-f193.google.com ([209.85.192.193]:43927 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751364AbeAEECV (ORCPT ); Thu, 4 Jan 2018 23:02:21 -0500 Received: by mail-pf0-f193.google.com with SMTP id e3so1715253pfi.10 for ; Thu, 04 Jan 2018 20:02:21 -0800 (PST) Sender: netdev-owner@vger.kernel.org List-ID: 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 --- kernel/bpf/sockmap.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/sockmap.c b/kernel/bpf/sockmap.c index 5ee2e41..1712d31 100644 --- a/kernel/bpf/sockmap.c +++ b/kernel/bpf/sockmap.c @@ -591,8 +591,15 @@ static void sock_map_free(struct bpf_map *map) write_lock_bh(&sock->sk_callback_lock); psock = smap_psock_sk(sock); - smap_list_remove(psock, &stab->sock_map[i]); - smap_release_sock(psock, sock); + /* This check handles 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. + */ + if (likely(psock)) { + smap_list_remove(psock, &stab->sock_map[i]); + smap_release_sock(psock, sock); + } write_unlock_bh(&sock->sk_callback_lock); } rcu_read_unlock();