Netdev List
 help / color / mirror / Atom feed
* [bpf PATCH v2 0/2] sockmap, syzbot fix error path and RCU fix
@ 2018-07-05 15:05 John Fastabend
  2018-07-05 15:05 ` [bpf PATCH v2 1/2] bpf: sockmap, error path can not release psock in multi-map case John Fastabend
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: John Fastabend @ 2018-07-05 15:05 UTC (permalink / raw)
  To: ast, daniel; +Cc: netdev, john.fastabend

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.

---

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


 kernel/bpf/sockmap.c |   19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)

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

* [bpf PATCH v2 1/2] bpf: sockmap, error path can not release psock in multi-map case
  2018-07-05 15:05 [bpf PATCH v2 0/2] sockmap, syzbot fix error path and RCU fix John Fastabend
@ 2018-07-05 15:05 ` John Fastabend
  2018-07-05 15:06 ` [bpf PATCH v2 2/2] bpf: sockmap, hash table is RCU so readers do not need locks John Fastabend
  2018-07-07 22:23 ` [bpf PATCH v2 0/2] sockmap, syzbot fix error path and RCU fix Alexei Starovoitov
  2 siblings, 0 replies; 4+ messages in thread
From: John Fastabend @ 2018-07-05 15:05 UTC (permalink / raw)
  To: ast, daniel; +Cc: netdev, 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: syzbot+d464d2c20c717ef5a6a8@syzkaller.appspotmail.com
Fixes: 81110384441a ("bpf: sockmap, add hash map support")
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
 kernel/bpf/sockmap.c |   17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/kernel/bpf/sockmap.c b/kernel/bpf/sockmap.c
index cf7b6a6..3847a7c 100644
--- a/kernel/bpf/sockmap.c
+++ b/kernel/bpf/sockmap.c
@@ -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;
 		}
 	}
 
@@ -2342,7 +2342,10 @@ 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 is valid here because otherwise above *ctx_update_elem would
+	 * have thrown an error. It is safe to skip error check.
+	 */
+	psock = smap_psock_sk(sock);
 	raw_spin_lock_bh(&b->lock);
 	l_old = lookup_elem_raw(head, hash, key, key_size);
 	if (l_old && map_flags == BPF_NOEXIST) {
@@ -2360,12 +2363,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));
@@ -2388,12 +2385,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;
 }
 

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

* [bpf PATCH v2 2/2] bpf: sockmap, hash table is RCU so readers do not need locks
  2018-07-05 15:05 [bpf PATCH v2 0/2] sockmap, syzbot fix error path and RCU fix John Fastabend
  2018-07-05 15:05 ` [bpf PATCH v2 1/2] bpf: sockmap, error path can not release psock in multi-map case John Fastabend
@ 2018-07-05 15:06 ` John Fastabend
  2018-07-07 22:23 ` [bpf PATCH v2 0/2] sockmap, syzbot fix error path and RCU fix Alexei Starovoitov
  2 siblings, 0 replies; 4+ messages in thread
From: John Fastabend @ 2018-07-05 15:06 UTC (permalink / raw)
  To: ast, daniel; +Cc: netdev, john.fastabend

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

Fixes: 81110384441a ("bpf: sockmap, add hash map support")
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
 kernel/bpf/sockmap.c |    2 --
 1 file changed, 2 deletions(-)

diff --git a/kernel/bpf/sockmap.c b/kernel/bpf/sockmap.c
index 3847a7c..00fb2e3 100644
--- a/kernel/bpf/sockmap.c
+++ b/kernel/bpf/sockmap.c
@@ -2467,10 +2467,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;
 }
 

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

* Re: [bpf PATCH v2 0/2] sockmap, syzbot fix error path and RCU fix
  2018-07-05 15:05 [bpf PATCH v2 0/2] sockmap, syzbot fix error path and RCU fix John Fastabend
  2018-07-05 15:05 ` [bpf PATCH v2 1/2] bpf: sockmap, error path can not release psock in multi-map case John Fastabend
  2018-07-05 15:06 ` [bpf PATCH v2 2/2] bpf: sockmap, hash table is RCU so readers do not need locks John Fastabend
@ 2018-07-07 22:23 ` Alexei Starovoitov
  2 siblings, 0 replies; 4+ messages in thread
From: Alexei Starovoitov @ 2018-07-07 22:23 UTC (permalink / raw)
  To: John Fastabend; +Cc: ast, daniel, netdev

On Thu, Jul 05, 2018 at 08:05:51AM -0700, John Fastabend wrote:
> 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.

Applied, Thanks

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

end of thread, other threads:[~2018-07-07 22:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-07-05 15:05 [bpf PATCH v2 0/2] sockmap, syzbot fix error path and RCU fix John Fastabend
2018-07-05 15:05 ` [bpf PATCH v2 1/2] bpf: sockmap, error path can not release psock in multi-map case John Fastabend
2018-07-05 15:06 ` [bpf PATCH v2 2/2] bpf: sockmap, hash table is RCU so readers do not need locks John Fastabend
2018-07-07 22:23 ` [bpf PATCH v2 0/2] sockmap, syzbot fix error path and RCU fix Alexei Starovoitov

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