Skip to contentSkip to conversation

sockmap, syzbot fix error path and RCU fix

PATCH Superseded John Fastabend 2 participants 5 messages Jun 30, 2018 – Jul 5, 2018

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover sockmap, syzbot fix error path and RCU fix

This applies on top of "BPF fixes for sockhash" I just didn't want to confuse that series yet again by re-ordering/adding these patches in it

I missed fixing the error path in the sockhash code to align with supporting socks in multiple maps. Simply checking if the psock is present does not mean we can decrement the reference count because it could be part of another map. Fix this by cleaning up the error path so this situation does not happen.

As far as I know this should be the last fix to the fallout from relaxing the single map restriction. Sorry about the multiple fixes but these patches were all written before the initial submission then converted and I missed this detail. But at least we caught these early in the net cycle. Will continue reviewing/testing however to see if we catch anything else.

Also we need one more series to check ESTABLISH state as Eric noted. That will be sent out shortly just going over the patches once more. The ESTABLISH/unhash fix is also needed in kTLS.

---

John Fastabend (2):
bpf: sockmap, error path can not release psock in multi-map case
bpf: sockmap, hash table is RCU so readers do not need locks

 0 files changed
#1 lore raw

PATCH 1/2 bpf: sockmap, error path can not release psock in multi-map case

The current code, in the error path of sock_hash_ctx_update_elem, checks if the sock has a psock in the user data and if so decrements the reference count of the psock. However, if the error happens early in the error path we may have never incremented the psock reference count and if the psock exists because the sock is in another map then we may inadvertently decrement the reference count.

Fix this by making the error path only call smap_release_sock if the error happens after the increment.

Reported-by [email protected]
Fixes 81110384441a ("bpf: sockmap, add hash map support")

---

 0 files changed
kernel/bpf/sockmap.c +8 -11
index 4fc2cb1..63fb047 100644
@@ -1896,7 +1896,7 @@ static int __sock_map_ctx_update_elem(struct bpf_map *map,
 		e = kzalloc(sizeof(*e), GFP_ATOMIC | __GFP_NOWARN);
 		if (!e) {
 			err = -ENOMEM;
-			goto out_progs;
+			goto out_free;
 		}
 	}
 
@@ -2324,7 +2324,12 @@ static int sock_hash_ctx_update_elem(struct bpf_sock_ops_kern *skops,
 	if (err)
 		goto err;
 
-	/* bpf_map_update_elem() can be called in_irq() */
+	psock = smap_psock_sk(sock);
+	if (unlikely(!psock)) {
+		err = -EINVAL;
+		goto err;
+	}
+
 	raw_spin_lock_bh(&b->lock);
 	l_old = lookup_elem_raw(head, hash, key, key_size);
 	if (l_old && map_flags == BPF_NOEXIST) {
@@ -2342,12 +2347,6 @@ static int sock_hash_ctx_update_elem(struct bpf_sock_ops_kern *skops,
 		goto bucket_err;
 	}
 
-	psock = smap_psock_sk(sock);
-	if (unlikely(!psock)) {
-		err = -EINVAL;
-		goto bucket_err;
-	}
-
 	rcu_assign_pointer(e->hash_link, l_new);
 	rcu_assign_pointer(e->htab,
 			   container_of(map, struct bpf_htab, map));
@@ -2370,12 +2369,10 @@ static int sock_hash_ctx_update_elem(struct bpf_sock_ops_kern *skops,
 	raw_spin_unlock_bh(&b->lock);
 	return 0;
 bucket_err:
+	smap_release_sock(psock, sock);
 	raw_spin_unlock_bh(&b->lock);
 err:
 	kfree(e);
-	psock = smap_psock_sk(sock);
-	if (psock)
-		smap_release_sock(psock, sock);
 	return err;
 }
 
maintainer
#2 lore raw

Re: [bpf PATCH 1/2] bpf: sockmap, error path can not release psock in multi-map case

Quoted 30 lines · John Fastabend

The current code, in the error path of sock_hash_ctx_update_elem, checks if the sock has a psock in the user data and if so decrements the reference count of the psock. However, if the error happens early in the error path we may have never incremented the psock reference count and if the psock exists because the sock is in another map then we may inadvertently decrement the reference count.

Fix this by making the error path only call smap_release_sock if the error happens after the increment.

Reported-by [email protected]
Fixes 81110384441a ("bpf: sockmap, add hash map support")

---

 0 files changed
kernel/bpf/sockmap.c +1 -1
index 4fc2cb1..63fb047 100644
@@ -1896,7 +1896,7 @@ static int __sock_map_ctx_update_elem(struct bpf_map *map,
 		e = kzalloc(sizeof(*e), GFP_ATOMIC | __GFP_NOWARN);
 		if (!e) {
 			err = -ENOMEM;
-			goto out_progs;
+			goto out_free;
 		}
 	}
 

kernel/bpf/sockmap.c static int sock_hash_ctx_update_elem(struct bpf_sock_ops_kern *skops, · line 2331

Quoted 9 lines
kernel/bpf/sockmap.c +5 -1
@@ -2324,7 +2324,12 @@ static int sock_hash_ctx_update_elem(struct bpf_sock_ops_kern *skops,
 	if (err)
 		goto err;
 
-	/* bpf_map_update_elem() can be called in_irq() */
+	psock = smap_psock_sk(sock);
+	if (unlikely(!psock)) {
+		err = -EINVAL;
+		goto err;
+	}

Is an error even possible at this point? If __sock_map_ctx_update_elem() succeeds, we either allocated and linked a new psock to the sock or we inc'ed the existing one's refcount. From my reading it seems we should always succeed the subsequent smap_psock_sk(). If we would have failed here in between it would mean we'd have a refcount imbalance somewhere?

Quoted 22 lines
kernel/bpf/sockmap.c +1 -6
+
 	raw_spin_lock_bh(&b->lock);
 	l_old = lookup_elem_raw(head, hash, key, key_size);
 	if (l_old && map_flags == BPF_NOEXIST) {
@@ -2342,12 +2347,6 @@ static int sock_hash_ctx_update_elem(struct bpf_sock_ops_kern *skops,
 		goto bucket_err;
 	}
 
-	psock = smap_psock_sk(sock);
-	if (unlikely(!psock)) {
-		err = -EINVAL;
-		goto bucket_err;
-	}
-
 	rcu_assign_pointer(e->hash_link, l_new);
 	rcu_assign_pointer(e->htab,
 			   container_of(map, struct bpf_htab, map));
@@ -2370,12 +2369,10 @@ static int sock_hash_ctx_update_elem(struct bpf_sock_ops_kern *skops,
 	raw_spin_unlock_bh(&b->lock);
 	return 0;
 bucket_err:

kernel/bpf/sockmap.c static int sock_hash_ctx_update_elem(struct bpf_sock_ops_kern *skops, · line 2378

Quoted 10 lines
kernel/bpf/sockmap.c +1 -3
+	smap_release_sock(psock, sock);
 	raw_spin_unlock_bh(&b->lock);
 err:
 	kfree(e);
-	psock = smap_psock_sk(sock);
-	if (psock)
-		smap_release_sock(psock, sock);
 	return err;
 }
 

Thanks,
Daniel

#3 lore raw

Re: [bpf PATCH 1/2] bpf: sockmap, error path can not release psock in multi-map case

Quoted 15 lines · Daniel Borkmann
Quoted 14 lines · John Fastabend

The current code, in the error path of sock_hash_ctx_update_elem, checks if the sock has a psock in the user data and if so decrements the reference count of the psock. However, if the error happens early in the error path we may have never incremented the psock reference count and if the psock exists because the sock is in another map then we may inadvertently decrement the reference count.

Fix this by making the error path only call smap_release_sock if the error happens after the increment.

Reported-by [email protected]
Fixes 81110384441a ("bpf: sockmap, add hash map support")

---

[...]

Quoted 4 lines
Quoted 4 lines
kernel/bpf/sockmap.c +0 -0
@@ -2324,7 +2324,12 @@ static int sock_hash_ctx_update_elem(struct bpf_sock_ops_kern *skops,
 	if (err)
 		goto err;
 
Quoted 13 lines

kernel/bpf/sockmap.c static int sock_hash_ctx_update_elem(struct bpf_sock_ops_kern *skops, · line 2331

Quoted 6 lines
kernel/bpf/sockmap.c +5 -1
-	/* bpf_map_update_elem() can be called in_irq() */
+	psock = smap_psock_sk(sock);
+	if (unlikely(!psock)) {
+		err = -EINVAL;
+		goto err;
+	}

Is an error even possible at this point? If __sock_map_ctx_update_elem() succeeds, we either allocated and linked a new psock to the sock or we inc'ed the existing one's refcount. From my reading it seems we should always succeed the subsequent smap_psock_sk(). If we would have failed here in between it would mean we'd have a refcount imbalance somewhere?

Its not possible will replace with a comment. Thanks.

#4 lore raw

PATCH 2/2 bpf: sockmap, hash table is RCU so readers do not need locks

In reply to John Fastabend

This removes locking from readers of RCU hash table. Its not necessary.

Fixes 81110384441a ("bpf: sockmap, add hash map support")

---

 0 files changed
kernel/bpf/sockmap.c +0 -2
index 63fb047..12ac10a 100644
@@ -2451,10 +2451,8 @@ struct sock  *__sock_hash_lookup_elem(struct bpf_map *map, void *key)
 	b = __select_bucket(htab, hash);
 	head = &b->head;
 
-	raw_spin_lock_bh(&b->lock);
 	l = lookup_elem_raw(head, hash, key, key_size);
 	sk = l ? l->sk : NULL;
-	raw_spin_unlock_bh(&b->lock);
 	return sk;
 }