All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/6] Bluetooth: Fix potential double free caused by hci_conn_unlink
@ 2023-05-02 14:57 Ruihan Li
  2023-05-02 14:57 ` [PATCH v3 1/6] " Ruihan Li
                   ` (5 more replies)
  0 siblings, 6 replies; 12+ messages in thread
From: Ruihan Li @ 2023-05-02 14:57 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: Marcel Holtmann, Johan Hedberg, Luiz Augusto von Dentz, Ruihan Li

This patch series contains six fixes related to hci_conn_unlink. The
purpose is to prevent merge conflicts between each other. I'm not
intentially linking them together. So if any patch is not suitable,
please just let me know (I'll be grateful if you can explain the
reason).

The first three patches are the most important, each fixing a
triggerable use-after-free bug (see the report URL for details). And the
fourth through sixth patches are a bit more minor, containing mostly
tweaks and refactorings.

Changes since v2:
  * Put all fixes, adjustments, and refactorings about hci_conn_unlink
    in one patch series.
Link to v2:
  * https://lore.kernel.org/linux-bluetooth/20230430180535.168270-1-lrh2000@pku.edu.cn/
See also:
  * https://lore.kernel.org/linux-bluetooth/20230430171847.156825-1-lrh2000@pku.edu.cn/

Changes since v1:
  * Resolve merge conflicts.
Link to v1:
  * https://lore.kernel.org/linux-bluetooth/20230430172937.157999-1-lrh2000@pku.edu.cn/

Ruihan Li (6):
  Bluetooth: Fix potential double free caused by hci_conn_unlink
  Bluetooth: Refcnt drop must be placed last in hci_conn_unlink
  Bluetooth: Fix UAF in hci_conn_hash_flush again
  Bluetooth: Perform hci_conn_drop in hci_conn_unlink
  Bluetooth: Unlink CISes when LE disconnects in hci_conn_del
  Bluetooth: Avoid recursion in hci_conn_unlink

 include/net/bluetooth/hci_core.h |  2 +-
 net/bluetooth/hci_conn.c         | 96 ++++++++++++++++++--------------
 2 files changed, 54 insertions(+), 44 deletions(-)

-- 
2.40.0


^ permalink raw reply	[flat|nested] 12+ messages in thread
* [PATCH] Bluetooth: Fix potential double free caused by hci_conn_unlink
@ 2023-04-30 17:29 Ruihan Li
  2023-04-30 17:37 ` bluez.test.bot
  0 siblings, 1 reply; 12+ messages in thread
From: Ruihan Li @ 2023-04-30 17:29 UTC (permalink / raw)
  To: linux-bluetooth
  Cc: Marcel Holtmann, Johan Hedberg, Luiz Augusto von Dentz, Ruihan Li

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.

Fixes: 06149746e720 ("Bluetooth: hci_conn: Add support for linking multiple hcon")
Signed-off-by: Ruihan Li <lrh2000@pku.edu.cn>
---
 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 85c34c837..5f388202f 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);
 }
 
 void hci_conn_del(struct hci_conn *conn)
-- 
2.40.0


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

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

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-02 14:57 [PATCH v3 0/6] Bluetooth: Fix potential double free caused by hci_conn_unlink Ruihan Li
2023-05-02 14:57 ` [PATCH v3 1/6] " Ruihan Li
2023-05-02 15:35   ` bluez.test.bot
2023-05-02 14:57 ` [PATCH v3 2/6] Bluetooth: Refcnt drop must be placed last in hci_conn_unlink Ruihan Li
2023-05-02 14:57 ` [PATCH v3 3/6] Bluetooth: Fix UAF in hci_conn_hash_flush again Ruihan Li
2023-05-02 14:57 ` [PATCH v3 4/6] Bluetooth: Perform hci_conn_drop in hci_conn_unlink Ruihan Li
2023-05-02 16:22   ` Luiz Augusto von Dentz
2023-05-02 14:57 ` [PATCH v3 5/6] Bluetooth: Unlink CISes when LE disconnects in hci_conn_del Ruihan Li
2023-05-02 14:57 ` [PATCH v3 6/6] Bluetooth: Avoid recursion in hci_conn_unlink Ruihan Li
2023-05-02 16:18   ` Luiz Augusto von Dentz
2023-05-03 13:15     ` Ruihan Li
  -- strict thread matches above, loose matches on Subject: below --
2023-04-30 17:29 [PATCH] Bluetooth: Fix potential double free caused by hci_conn_unlink Ruihan Li
2023-04-30 17:37 ` bluez.test.bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.