diff mbox series

[v2] Bluetooth: Fix potential double free caused by hci_conn_unlink

Message ID 20230430180535.168270-1-lrh2000@pku.edu.cn (mailing list archive)
State Superseded
Headers show
Series [v2] Bluetooth: Fix potential double free caused by hci_conn_unlink | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success
tedd_an/CheckPatch success CheckPatch PASS
tedd_an/GitLint fail WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search 15: B3 Line contains hard tab characters (\t): " void hci_conn_del(struct hci_conn *conn)" 17: B1 Line exceeds max length (84>80): " https://lore.kernel.org/linux-bluetooth/20230430171847.156825-1-lrh2000@pku.edu.cn/" 17: B3 Line contains hard tab characters (\t): " https://lore.kernel.org/linux-bluetooth/20230430171847.156825-1-lrh2000@pku.edu.cn/"
tedd_an/SubjectPrefix success Gitlint PASS
tedd_an/BuildKernel success BuildKernel PASS
tedd_an/CheckAllWarning success CheckAllWarning PASS
tedd_an/CheckSparse success CheckSparse PASS
tedd_an/CheckSmatch success CheckSparse PASS
tedd_an/BuildKernel32 success BuildKernel32 PASS
tedd_an/TestRunnerSetup success TestRunnerSetup PASS
tedd_an/TestRunner_l2cap-tester success TestRunner PASS
tedd_an/TestRunner_iso-tester success TestRunner PASS
tedd_an/TestRunner_bnep-tester success TestRunner PASS
tedd_an/TestRunner_mgmt-tester success TestRunner PASS
tedd_an/TestRunner_rfcomm-tester success TestRunner PASS
tedd_an/TestRunner_sco-tester success TestRunner PASS
tedd_an/TestRunner_ioctl-tester success TestRunner PASS
tedd_an/TestRunner_mesh-tester success TestRunner PASS
tedd_an/TestRunner_smp-tester success TestRunner PASS
tedd_an/TestRunner_userchan-tester success TestRunner PASS
tedd_an/IncrementalBuild success Incremental Build PASS

Commit Message

Ruihan Li April 30, 2023, 6:05 p.m. UTC
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>
---
Changes since v1:
 * CI complains that there are some merge conflicts. This is because
	void hci_conn_del(struct hci_conn *conn)
   this completely unrelated line makes the patch dependent on another fix:
	https://lore.kernel.org/linux-bluetooth/20230430171847.156825-1-lrh2000@pku.edu.cn/
   But actually, deleting that line makes the two patches independent of
   each other, and everything still works.

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

Comments

bluez.test.bot@gmail.com April 30, 2023, 6:33 p.m. UTC | #1
This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=744077

---Test result---

Test Summary:
CheckPatch                    PASS      0.70 seconds
GitLint                       FAIL      0.56 seconds
SubjectPrefix                 PASS      0.13 seconds
BuildKernel                   PASS      31.08 seconds
CheckAllWarning               PASS      34.53 seconds
CheckSparse                   PASS      39.20 seconds
CheckSmatch                   PASS      109.45 seconds
BuildKernel32                 PASS      30.18 seconds
TestRunnerSetup               PASS      441.36 seconds
TestRunner_l2cap-tester       PASS      16.54 seconds
TestRunner_iso-tester         PASS      20.36 seconds
TestRunner_bnep-tester        PASS      5.30 seconds
TestRunner_mgmt-tester        PASS      111.39 seconds
TestRunner_rfcomm-tester      PASS      8.46 seconds
TestRunner_sco-tester         PASS      7.88 seconds
TestRunner_ioctl-tester       PASS      9.17 seconds
TestRunner_mesh-tester        PASS      6.77 seconds
TestRunner_smp-tester         PASS      7.77 seconds
TestRunner_userchan-tester    PASS      5.56 seconds
IncrementalBuild              PASS      28.59 seconds

Details
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[v2] Bluetooth: Fix potential double free caused by hci_conn_unlink

WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
15: B3 Line contains hard tab characters (\t): "	void hci_conn_del(struct hci_conn *conn)"
17: B1 Line exceeds max length (84>80): "	https://lore.kernel.org/linux-bluetooth/20230430171847.156825-1-lrh2000@pku.edu.cn/"
17: B3 Line contains hard tab characters (\t): "	https://lore.kernel.org/linux-bluetooth/20230430171847.156825-1-lrh2000@pku.edu.cn/"


---
Regards,
Linux Bluetooth
Ruihan Li May 2, 2023, 2:10 p.m. UTC | #2
On Mon, May 01, 2023 at 02:05:35AM +0800, Ruihan Li wrote:
> 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.

Syzbot also found this bug several days ago (but it does not email
linux-bluetooth until today).

Reported-by: syzbot+690b90b14f14f43f4688@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=690b90b14f14f43f4688

> Fixes: 06149746e720 ("Bluetooth: hci_conn: Add support for linking multiple hcon")
> Signed-off-by: Ruihan Li <lrh2000@pku.edu.cn>
> ---
> Changes since v1:
>  * CI complains that there are some merge conflicts. This is because
> 	void hci_conn_del(struct hci_conn *conn)
>    this completely unrelated line makes the patch dependent on another fix:
> 	https://lore.kernel.org/linux-bluetooth/20230430171847.156825-1-lrh2000@pku.edu.cn/
>    But actually, deleting that line makes the two patches independent of
>    each other, and everything still works.
> 
>  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,12 +1110,5 @@ 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);
>  }
>  
> -- 
> 2.40.0

Thanks,
Ruihan Li
diff mbox series

Patch

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,12 +1110,5 @@  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);
 }