From mboxrd@z Thu Jan 1 00:00:00 1970 From: Daniel Borkmann Subject: Re: [bpf PATCH] bpf: sockmap missing NULL psock check Date: Thu, 4 Jan 2018 12:21:55 +0100 Message-ID: References: <20180103034705.11636.98766.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: John Fastabend , borkmann@iogearbox.net, alexei.starovoitov@gmail.com Return-path: Received: from www62.your-server.de ([213.133.104.62]:34812 "EHLO www62.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752676AbeADLV5 (ORCPT ); Thu, 4 Jan 2018 06:21:57 -0500 In-Reply-To: <20180103034705.11636.98766.stgit@john-Precision-Tower-5810> Content-Language: en-US Sender: netdev-owner@vger.kernel.org List-ID: On 01/03/2018 04:47 AM, John Fastabend wrote: > 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 | 7 +++++++ > 1 file changed, 7 insertions(+) > > diff --git a/kernel/bpf/sockmap.c b/kernel/bpf/sockmap.c > index 5ee2e41..dfbbde2 100644 > --- a/kernel/bpf/sockmap.c > +++ b/kernel/bpf/sockmap.c > @@ -591,6 +591,13 @@ static void sock_map_free(struct bpf_map *map) > > write_lock_bh(&sock->sk_callback_lock); > psock = smap_psock_sk(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 (unlikely(!psock)) > + continue; Don't we need a write_unlock_bh(&sock->sk_callback_lock) before the continue? > smap_list_remove(psock, &stab->sock_map[i]); > smap_release_sock(psock, sock); > write_unlock_bh(&sock->sk_callback_lock); >