Skip to contentSkip to conversation

sock_map: fix a potential use-after-free in sock_map_close()

PATCH Applied Cong Wang 4 participants 6 messages Apr 8, 2021 – Apr 12, 2021

Subsystems (5)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 sock_map: fix a potential use-after-free in sock_map_close()

From: Cong Wang <[email protected]>

The last refcnt of the psock can be gone right after sock_map_remove_links(), so sk_psock_stop() could trigger a UAF. The reason why I placed sk_psock_stop() there is to avoid RCU read critical section, and more importantly, some callee of sock_map_remove_links() is supposed to be called with RCU read lock, we can not simply get rid of RCU read lock here. Therefore, the only choice we have is to grab an additional refcnt with sk_psock_get() and put it back after sk_psock_stop().

Reported-by [email protected]
Fixes 799aa7f98d53 ("skmsg: Avoid lock_sock() in sk_psock_backlog()")
Cc John Fastabend <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Jakub Sitnicki <[email protected]>
Cc Lorenz Bauer <[email protected]>
---
 net/core/sock_map.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/core/sock_map.c +2 -1
index f473c51cbc4b..6f1b82b8ad49 100644
@@ -1521,7 +1521,7 @@ void sock_map_close(struct sock *sk, long timeout)
 
 	lock_sock(sk);
 	rcu_read_lock();
-	psock = sk_psock(sk);
+	psock = sk_psock_get(sk);
 	if (unlikely(!psock)) {
 		rcu_read_unlock();
 		release_sock(sk);
@@ -1532,6 +1532,7 @@ void sock_map_close(struct sock *sk, long timeout)
 	sock_map_remove_links(sk, psock);
 	rcu_read_unlock();
 	sk_psock_stop(psock, true);
+	sk_psock_put(sk, psock);
 	release_sock(sk);
 	saved_close(sk, timeout);
 }
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 27 lines · Cong Wang

From: Cong Wang <[email protected]>

The last refcnt of the psock can be gone right after sock_map_remove_links(), so sk_psock_stop() could trigger a UAF. The reason why I placed sk_psock_stop() there is to avoid RCU read critical section, and more importantly, some callee of sock_map_remove_links() is supposed to be called with RCU read lock, we can not simply get rid of RCU read lock here. Therefore, the only choice we have is to grab an additional refcnt with sk_psock_get() and put it back after sk_psock_stop().

Reported-by [email protected]
Fixes 799aa7f98d53 ("skmsg: Avoid lock_sock() in sk_psock_backlog()")
Cc John Fastabend <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Jakub Sitnicki <[email protected]>
Cc Lorenz Bauer <[email protected]>
---
 net/core/sock_map.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/core/sock_map.c +0 -0
index f473c51cbc4b..6f1b82b8ad49 100644

net/core/sock_map.c void sock_map_close(struct sock *sk, long timeout) · line 1523

Quoted 3 lines
net/core/sock_map.c +0 -0
@@ -1521,7 +1521,7 @@ void sock_map_close(struct sock *sk, long timeout)
 
 	lock_sock(sk);
 	rcu_read_lock();

It looks like we can drop the rcu_read_lock()/unlock() section then if we take a reference on the psock? Before it was there to ensure we didn't lose the psock from some other context, but with a reference held this can not happen.

Quoted 16 lines
net/core/sock_map.c +2 -1
-	psock = sk_psock(sk);
+	psock = sk_psock_get(sk);
 	if (unlikely(!psock)) {
 		rcu_read_unlock();
 		release_sock(sk);
@@ -1532,6 +1532,7 @@ void sock_map_close(struct sock *sk, long timeout)
 	sock_map_remove_links(sk, psock);
 	rcu_read_unlock();
 	sk_psock_stop(psock, true);
+	sk_psock_put(sk, psock);
 	release_sock(sk);
 	saved_close(sk, timeout);
 }
Signature · 1 line
2.25.1
author
#2 lore raw
Quoted 30 lines · John Fastabend
Quoted 28 lines · Cong Wang

From: Cong Wang <[email protected]>

The last refcnt of the psock can be gone right after sock_map_remove_links(), so sk_psock_stop() could trigger a UAF. The reason why I placed sk_psock_stop() there is to avoid RCU read critical section, and more importantly, some callee of sock_map_remove_links() is supposed to be called with RCU read lock, we can not simply get rid of RCU read lock here. Therefore, the only choice we have is to grab an additional refcnt with sk_psock_get() and put it back after sk_psock_stop().

Reported-by [email protected]
Fixes 799aa7f98d53 ("skmsg: Avoid lock_sock() in sk_psock_backlog()")
Cc John Fastabend <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Jakub Sitnicki <[email protected]>
Cc Lorenz Bauer <[email protected]>
---
 net/core/sock_map.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/core/sock_map.c +0 -0
index f473c51cbc4b..6f1b82b8ad49 100644
@@ -1521,7 +1521,7 @@ void sock_map_close(struct sock *sk, long timeout)
 
Quoted 7 lines

net/core/sock_map.c void sock_map_close(struct sock *sk, long timeout) · line 1523

Quoted 2 lines
net/core/sock_map.c +0 -0
      lock_sock(sk);
      rcu_read_lock();

It looks like we can drop the rcu_read_lock()/unlock() section then if we take a reference on the psock? Before it was there to ensure we didn't lose the psock from some other context, but with a reference held this can not happen.

Some callees under sock_map_remove_links() still assert RCU read lock, so we can not simply drop the RCU read lock here. Some additional efforts are needed to take care of those assertions, which can be a separate patch.

Thanks.

maintainer Acked-by
#3 lore raw
Quoted 39 lines · Cong Wang
Quoted 30 lines · John Fastabend
Quoted 28 lines · Cong Wang

From: Cong Wang <[email protected]>

The last refcnt of the psock can be gone right after sock_map_remove_links(), so sk_psock_stop() could trigger a UAF. The reason why I placed sk_psock_stop() there is to avoid RCU read critical section, and more importantly, some callee of sock_map_remove_links() is supposed to be called with RCU read lock, we can not simply get rid of RCU read lock here. Therefore, the only choice we have is to grab an additional refcnt with sk_psock_get() and put it back after sk_psock_stop().

Reported-by [email protected]
Fixes 799aa7f98d53 ("skmsg: Avoid lock_sock() in sk_psock_backlog()")
Cc John Fastabend <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Jakub Sitnicki <[email protected]>
Cc Lorenz Bauer <[email protected]>
---
 net/core/sock_map.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/core/sock_map.c +0 -0
index f473c51cbc4b..6f1b82b8ad49 100644
@@ -1521,7 +1521,7 @@ void sock_map_close(struct sock *sk, long timeout)
 
Quoted 7 lines

net/core/sock_map.c void sock_map_close(struct sock *sk, long timeout) · line 1523

Quoted 2 lines
net/core/sock_map.c +0 -0
      lock_sock(sk);
      rcu_read_lock();

It looks like we can drop the rcu_read_lock()/unlock() section then if we take a reference on the psock? Before it was there to ensure we didn't lose the psock from some other context, but with a reference held this can not happen.

Quoted 6 lines

Some callees under sock_map_remove_links() still assert RCU read lock, so we can not simply drop the RCU read lock here. Some additional efforts are needed to take care of those assertions, which can be a separate patch.

Thanks.

OK at least this case exists,

sock_map_close
sock_map_remove_links
sock_map_unlink

    sock_hash_delete_from_link
      WARN_ON_ONCE(!rcu_read_lock_held()); 

also calls into sock_map_unref through similar path use sk_psock(sk) depending on rcu critical section.

Its certainly non-trivial to remove. I don't really like taking a ref here but seems necessary for now.

maintainer Acked-by on v1 1/1
#4 lore raw
In reply to Cong Wang
Quoted 11 lines · Cong Wang

From: Cong Wang <[email protected]>

The last refcnt of the psock can be gone right after sock_map_remove_links(), so sk_psock_stop() could trigger a UAF. The reason why I placed sk_psock_stop() there is to avoid RCU read critical section, and more importantly, some callee of sock_map_remove_links() is supposed to be called with RCU read lock, we can not simply get rid of RCU read lock here. Therefore, the only choice we have is to grab an additional refcnt with sk_psock_get() and put it back after sk_psock_stop().

Quoted 8 lines
Reported-by [email protected]
Fixes 799aa7f98d53 ("skmsg: Avoid lock_sock() in sk_psock_backlog()")
Cc John Fastabend <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Jakub Sitnicki <[email protected]>
Cc Lorenz Bauer <[email protected]>

---

#5 lore raw
In reply to Cong Wang
This patch was applied to bpf/bpf-next.git (refs/heads/master):

Hello:

This patch was applied to bpf/bpf-next.git (refs/heads/master):

Quoted 11 lines · On Wed, 7 Apr 2021 20:05:56 -0700 you wrote:

From: Cong Wang <[email protected]>

The last refcnt of the psock can be gone right after sock_map_remove_links(), so sk_psock_stop() could trigger a UAF. The reason why I placed sk_psock_stop() there is to avoid RCU read critical section, and more importantly, some callee of sock_map_remove_links() is supposed to be called with RCU read lock, we can not simply get rid of RCU read lock here. Therefore, the only choice we have is to grab an additional refcnt with sk_psock_get() and put it back after sk_psock_stop().

Quoted 1 line

[...]

Here is the summary with links:
- [bpf-next] sock_map: fix a potential use-after-free in sock_map_close()

    https://git.kernel.org/bpf/bpf-next/c/aadb2bb83ff7

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html