Netdev List
 help / color / mirror / Atom feed
* [bpf-net PATCH v2] bpf: sockmap missing NULL psock check
@ 2018-01-05  4:02 John Fastabend
  2018-01-06 23:08 ` Daniel Borkmann
  0 siblings, 1 reply; 2+ messages in thread
From: John Fastabend @ 2018-01-05  4:02 UTC (permalink / raw)
  To: borkmann, alexei.starovoitov; +Cc: netdev

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>
---
 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();

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [bpf-net PATCH v2] bpf: sockmap missing NULL psock check
  2018-01-05  4:02 [bpf-net PATCH v2] bpf: sockmap missing NULL psock check John Fastabend
@ 2018-01-06 23:08 ` Daniel Borkmann
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Borkmann @ 2018-01-06 23:08 UTC (permalink / raw)
  To: John Fastabend, borkmann, alexei.starovoitov; +Cc: netdev

On 01/05/2018 05:02 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 <john.fastabend@gmail.com>

Applied to bpf, thanks John!

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-01-06 23:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-01-05  4:02 [bpf-net PATCH v2] bpf: sockmap missing NULL psock check John Fastabend
2018-01-06 23:08 ` Daniel Borkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox