Skip to contentSkip to conversation

Bluetooth: SCO: Fix UAF on sco_sock_timeout

PATCH Superseded Todd Kjos 3 participants 5 messages Aug 4 – Aug 5

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Todd Kjos

author
#0 lore raw

PATCH 1/2 Bluetooth: SCO: Fix UAF on sco_sock_timeout

From: Luiz Augusto von Dentz <[email protected]>

commit 1bf4470a3939c678fb822073e9ea77a0560bc6bb upstream.

conn->sk maybe have been unlinked/freed while waiting for sco_conn_lock so this checks if the conn->sk is still valid by checking if it part of sco_sk_list.

Reported-by [email protected]
Fixes ba316be1b6a0 ("Bluetooth: schedule SCO timeouts with delayed_work")
Change-Id I0520456fb59dfdb11b0d8a4d6b7087684736a0ae
[ Resolved trivial conflicts in net/bluetooth/sco.c ]
---
 include/net/bluetooth/bluetooth.h |  1 +
 net/bluetooth/af_bluetooth.c      | 22 ++++++++++++++++++++++
 net/bluetooth/sco.c               | 16 ++++++++++++----
 3 files changed, 35 insertions(+), 4 deletions(-)
include/net/bluetooth/bluetooth.h +1 -0
index 43b4386018e26c41c914f87e050a0fe958700135..85bab90a6921ce1e9b9025647e23fafd97117ece 100644
@@ -317,6 +317,7 @@ void bt_sock_link(struct bt_sock_list *l, struct sock *s);
 void bt_sock_unlink(struct bt_sock_list *l, struct sock *s);
 struct sock *bt_sock_alloc(struct net *net, struct socket *sock,
 			   struct proto *prot, int proto, gfp_t prio, int kern);
+bool bt_sock_linked(struct bt_sock_list *l, struct sock *s);
 int  bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
 		     int flags);
 int  bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg,
net/bluetooth/af_bluetooth.c +22 -0
index bef5b6330dd807504292671301c860b96c2ed18e..0af14e3318e7e02173970a1af8e183723bef2c50 100644
@@ -184,6 +184,28 @@ void bt_sock_unlink(struct bt_sock_list *l, struct sock *sk)
 }
 EXPORT_SYMBOL(bt_sock_unlink);
 
+bool bt_sock_linked(struct bt_sock_list *l, struct sock *s)
+{
+	struct sock *sk;
+
+	if (!l || !s)
+		return false;
+
+	read_lock(&l->lock);
+
+	sk_for_each(sk, &l->head) {
+		if (s == sk) {
+			read_unlock(&l->lock);
+			return true;
+		}
+	}
+
+	read_unlock(&l->lock);
+
+	return false;
+}
+EXPORT_SYMBOL(bt_sock_linked);
+
 void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh)
 {
 	const struct cred *old_cred;
net/bluetooth/sco.c +12 -4
index 01a01d6f01c309a6333859784261d97335dda413..8ac1f9a0222f3e947e75f37ac87115c74cbc0da9 100644
@@ -76,6 +76,16 @@ struct sco_pinfo {
 #define SCO_CONN_TIMEOUT	(HZ * 40)
 #define SCO_DISCONN_TIMEOUT	(HZ * 2)
 
+static struct sock *sco_sock_hold(struct sco_conn *conn)
+{
+	if (!conn || !bt_sock_linked(&sco_sk_list, conn->sk))
+		return NULL;
+
+	sock_hold(conn->sk);
+
+	return conn->sk;
+}
+
 static void sco_sock_timeout(struct work_struct *work)
 {
 	struct sco_conn *conn = container_of(work, struct sco_conn,
@@ -87,9 +97,7 @@ static void sco_sock_timeout(struct work_struct *work)
 		sco_conn_unlock(conn);
 		return;
 	}
-	sk = conn->sk;
-	if (sk)
-		sock_hold(sk);
+	sk = sco_sock_hold(conn);
 	sco_conn_unlock(conn);
 
 	if (!sk)
@@ -192,7 +200,7 @@ static void sco_conn_del(struct hci_conn *hcon, int err)
 
 	/* Kill socket */
 	sco_conn_lock(conn);
-	sk = conn->sk;
+	sk = sco_sock_hold(conn);
 	sco_conn_unlock(conn);
 
 	if (sk) {
Signature · 1 line
2.55.0.571.g244d577d93-goog
author
#1 lore raw

PATCH 2/2 Bluetooth: SCO: Fix use-after-free in sco_recv_frame() due to missing sock_hold

From: Hyunwoo Kim <[email protected]>

[ Upstream commit 598dbba9919c5e36c54fe1709b557d64120cb94b ]

sco_recv_frame() reads conn->sk under sco_conn_lock() but immediately releases the lock without holding a reference to the socket. A concurrent close() can free the socket between the lock release and the subsequent sk->sk_state access, resulting in a use-after-free.

Other functions in the same file (sco_sock_timeout(), sco_conn_del()) correctly use sco_sock_hold() to safely hold a reference under the lock.

Fix by using sco_sock_hold() to take a reference before releasing the lock, and adding sock_put() on all exit paths.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Change-Id Iae15e58c35bddb5b78a13f48f94dcb83f00ebb51
---
 net/bluetooth/sco.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
net/bluetooth/sco.c +7 -3
index 8ac1f9a0222f3e947e75f37ac87115c74cbc0da9..d2a9e851386b6897a9fa873cdae8f191e80d7fbf 100644
@@ -313,7 +313,7 @@ static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
 	struct sock *sk;
 
 	sco_conn_lock(conn);
-	sk = conn->sk;
+	sk = sco_sock_hold(conn);
 	sco_conn_unlock(conn);
 
 	if (!sk)
@@ -322,11 +322,15 @@ static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
 	BT_DBG("sk %p len %d", sk, skb->len);
 
 	if (sk->sk_state != BT_CONNECTED)
-		goto drop;
+		goto drop_put;
 
-	if (!sock_queue_rcv_skb(sk, skb))
+	if (!sock_queue_rcv_skb(sk, skb)) {
+		sock_put(sk);
 		return;
+	}
 
+drop_put:
+	sock_put(sk);
 drop:
 	kfree_skb(skb);
 }
Signature · 1 line
2.55.0.571.g244d577d93-goog
#2 lore raw
In reply to Todd Kjos

This is an automated email and please do not reply to this email.

Dear Submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
While preparing the CI tests, the patches you submitted couldn't be applied to the current HEAD of the repository.

----- Output -----

error: patch failed: include/net/bluetooth/bluetooth.h:317
error: include/net/bluetooth/bluetooth.h: patch does not apply
error: patch failed: net/bluetooth/af_bluetooth.c:184
error: net/bluetooth/af_bluetooth.c: patch does not apply
error: patch failed: net/bluetooth/sco.c:76
error: net/bluetooth/sco.c: patch does not apply
hint: Use 'git am --show-current-patch' to see the failed patch

Please resolve the issue and submit the patches again.

---
Regards,
Linux Bluetooth

maintainer
#3 lore raw
In reply to Todd Kjos

net/bluetooth/sco.c static void sco_conn_del(struct hci_conn *hcon, int err) · line 206

Quoted 9 lines · Todd Kjos
net/bluetooth/sco.c +1 -1
@@ -192,7 +200,7 @@ static void sco_conn_del(struct hci_conn *hcon, int err)
 
 	/* Kill socket */
 	sco_conn_lock(conn);
-	sk = conn->sk;
+	sk = sco_sock_hold(conn);
 	sco_conn_unlock(conn);
 
 	if (sk) {

I think that this hunk leaks a struct sock reference on 5.10.

Unlike upstream and the 5.15/6.1 backports, where the sock_hold(sk) sits just before sco_conn_unlock() and the upstream hunk deletes it, 5.10 keeps its sock_hold(sk) inside the condition:

	sco_conn_lock(conn);
	sk = conn->sk;
	sco_conn_unlock(conn);

	if (sk) {
		sock_hold(sk);
		bh_lock_sock(sk);
		sco_sock_clear_timer(sk);
		sco_chan_del(sk, err);
		bh_unlock_sock(sk);
		sock_put(sk);
	}

So after this patch the function takes two references (one in sco_sock_hold(), one from the surviving sock_hold(sk)) and drops only one. That is a permanent struct sock leak on every SCO teardown, reachable by any user with repeated connect/disconnect.

While respinning, please also drop the Change-Id: trailers from both patches.

Signature · 2 lines
Thanks,
Sasha
author
#4 lore raw
Quoted 10 lines · Sasha Levin
Quoted 8 lines · Todd Kjos
net/bluetooth/sco.c +1 -1
@@ -192,7 +200,7 @@ static void sco_conn_del(struct hci_conn *hcon, int err)
 
      /* Kill socket */
      sco_conn_lock(conn);
-     sk = conn->sk;
+     sk = sco_sock_hold(conn);
      sco_conn_unlock(conn);
 
Quoted 3 lines

net/bluetooth/sco.c static void sco_conn_del(struct hci_conn *hcon, int err) · line 206

Quoted 1 line
net/bluetooth/sco.c +0 -0
      if (sk) {

I think that this hunk leaks a struct sock reference on 5.10.

Yes, you are right.

Quoted 11 lines

Unlike upstream and the 5.15/6.1 backports, where the sock_hold(sk) sits just before sco_conn_unlock() and the upstream hunk deletes it, 5.10 keeps its sock_hold(sk) inside the condition:

        sco_conn_lock(conn);
        sk = conn->sk;
        sco_conn_unlock(conn);

        if (sk) {
                sock_hold(sk);

This reference is not needed in the new version, since in we take the reference above.

Quoted 7 lines
                bh_lock_sock(sk);
                sco_sock_clear_timer(sk);
                sco_chan_del(sk, err);
                bh_unlock_sock(sk);
                sock_put(sk);
        }
Quoted 7 lines

So after this patch the function takes two references (one in sco_sock_hold(), one from the surviving sock_hold(sk)) and drops only one. That is a permanent struct sock leak on every SCO teardown, reachable by any user with repeated connect/disconnect.

While respinning, please also drop the Change-Id: trailers from both patches.

Will do. Sorry about that.

Quoted 4 lines

--
Thanks,
Sasha