linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 1/4] Bluetooth: Fix potential double free caused by hci_conn_unlink
@ 2023-05-03 13:39 Ruihan Li
  2023-05-03 13:39 ` [PATCH v4 2/4] Bluetooth: Refcnt drop must be placed last in hci_conn_unlink Ruihan Li
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Ruihan Li @ 2023-05-03 13:39 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: Marcel Holtmann, Johan Hedberg, Luiz Augusto von Dentz, Ruihan Li,
	syzbot+690b90b14f14f43f4688, Luiz Augusto von Dentz

The hci_conn_unlink function is being called by hci_conn_del, which
means it should not call hci_conn_del with the input parameter conn
again. If it does, conn may have already been released when
hci_conn_unlink returns, leading to potential UAF and double-free
issues.

This patch resolves the problem by modifying hci_conn_unlink to release
only conn's child links when necessary, but never release conn itself.

Reported-by: syzbot+690b90b14f14f43f4688@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/linux-bluetooth/000000000000484a8205faafe216@google.com/
Fixes: 06149746e720 ("Bluetooth: hci_conn: Add support for linking multiple hcon")
Signed-off-by: Ruihan Li <lrh2000@pku.edu.cn>
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
  * Fix tags in commit messages. No code was changed.

 net/bluetooth/hci_conn.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
index 640b951bf..70e1655a9 100644
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -1083,8 +1083,18 @@ static void hci_conn_unlink(struct hci_conn *conn)
 	if (!conn->parent) {
 		struct hci_link *link, *t;
 
-		list_for_each_entry_safe(link, t, &conn->link_list, list)
-			hci_conn_unlink(link->conn);
+		list_for_each_entry_safe(link, t, &conn->link_list, list) {
+			struct hci_conn *child = link->conn;
+
+			hci_conn_unlink(child);
+
+			/* Due to race, SCO connection might be not established
+			 * yet at this point. Delete it now, otherwise it is
+			 * possible for it to be stuck and can't be deleted.
+			 */
+			if (child->handle == HCI_CONN_HANDLE_UNSET)
+				hci_conn_del(child);
+		}
 
 		return;
 	}
@@ -1100,13 +1110,6 @@ static void hci_conn_unlink(struct hci_conn *conn)
 
 	kfree(conn->link);
 	conn->link = NULL;
-
-	/* Due to race, SCO connection might be not established
-	 * yet at this point. Delete it now, otherwise it is
-	 * possible for it to be stuck and can't be deleted.
-	 */
-	if (conn->handle == HCI_CONN_HANDLE_UNSET)
-		hci_conn_del(conn);
 }
 
 int hci_conn_del(struct hci_conn *conn)
-- 
2.40.0


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

end of thread, other threads:[~2023-05-03 17:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-03 13:39 [PATCH v4 1/4] Bluetooth: Fix potential double free caused by hci_conn_unlink Ruihan Li
2023-05-03 13:39 ` [PATCH v4 2/4] Bluetooth: Refcnt drop must be placed last in hci_conn_unlink Ruihan Li
2023-05-03 13:39 ` [PATCH v4 3/4] Bluetooth: Fix UAF in hci_conn_hash_flush again Ruihan Li
2023-05-03 13:39 ` [PATCH v4 4/4] Bluetooth: Unlink CISes when LE disconnects in hci_conn_del Ruihan Li
2023-05-03 14:38 ` [v4,1/4] Bluetooth: Fix potential double free caused by hci_conn_unlink bluez.test.bot
2023-05-03 17:30 ` [PATCH v4 1/4] " patchwork-bot+bluetooth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).