Skip to contentSkip to conversation

bpf: sockmap missing NULL psock check

PATCH Superseded John Fastabend 2 participants 2 messages Jan 3, 2018 – Jan 4, 2018

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 kernel/bpf/sockmap.c |    7 +++++++
 1 file changed, 7 insertions(+)
kernel/bpf/sockmap.c +7 -0
index 5ee2e41..dfbbde2 100644
@@ -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;
 		smap_list_remove(psock, &stab->sock_map[i]);
 		smap_release_sock(psock, sock);
 		write_unlock_bh(&sock->sk_callback_lock);
maintainer
#1 lore raw
Quoted 18 lines · John Fastabend

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.

---
 kernel/bpf/sockmap.c |    7 +++++++
 1 file changed, 7 insertions(+)
kernel/bpf/sockmap.c +0 -0
index 5ee2e41..dfbbde2 100644

kernel/bpf/sockmap.c static void sock_map_free(struct bpf_map *map) · line 600

Quoted 10 lines
kernel/bpf/sockmap.c +7 -0
@@ -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?

Quoted 4 lines
kernel/bpf/sockmap.c +0 -0
 		smap_list_remove(psock, &stab->sock_map[i]);
 		smap_release_sock(psock, sock);
 		write_unlock_bh(&sock->sk_callback_lock);