Bluetooth: hci: fix refcounts in LE remote features command
Subsystems (2)
PATCH 1/1 Bluetooth: hci: fix refcounts in LE remote features command
KASAN reported a slab-use-after-free in le_read_features_complete() running from hci_cmd_sync_work. le_read_features_complete() can run after hci_rx_work/hci_conn_del() has removed the link, so the destroy callback may touch a freed hci_conn and trigger a KASAN use-after-free. Duplicate submissions also need to drop the references to avoid leaking the hold and blocking teardown.
Fix this by taking hci_conn_get() before queueing, passing the conn directly as work data, and balancing hci_conn_hold()/drop() and hci_conn_get()/put() in the completion and all error/-EEXIST paths so the connection stays referenced while the work runs and is released afterwards.
--- net/bluetooth/hci_sync.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-)
I am not entirely sure that removing the err == -ECANCELED early return is strictly correct. My assumption is that, with the changes in this patch, there does not appear to be another cancellation path that reliably balances hci_conn_drop() and hci_conn_put() for this command. Based on that assumption, keeping the early return could leave the references taken before queuing unbalanced on cancellation, so I opted to remove it to keep the lifetime handling consistent.
net/bluetooth/hci_sync.c +26 -11
@@ -776,14 +776,23 @@ _hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,* - Lookup if an entry already exist and only if it doesn't creates a new entry * and queue it. */ -int hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func, +static int __hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func,void *data,hci_cmd_sync_work_destroy_t destroy) {if (hci_cmd_sync_lookup_entry (hdev, func, data, destroy)) -return 0 ; +return -EEXIST ;return hci_cmd_sync_queue (hdev, func, data, destroy); } + +int hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func, +void *data,hci_cmd_sync_work_destroy_t destroy) +{ +int ret; + + ret =__hci_cmd_sync_queue_once (hdev, func, data, destroy); +return ret == -EEXIST ?0 : ret; +}EXPORT_SYMBOL (hci_cmd_sync_queue_once);/* Run HCI command:
@@ -7338,10 +7347,8 @@ static void le_read_features_complete(struct hci_dev *hdev, void *data, int err)bt_dev_dbg (hdev,"err %d" , err); -if (err == -ECANCELED ) -return ; -hci_conn_drop (conn); +hci_conn_put (conn); } static int hci_le_read_all_remote_features_sync(struct hci_dev *hdev,
@@ -7408,12 +7415,20 @@ int hci_le_read_remote_features(struct hci_conn *conn)* role is possible. Otherwise just transition into the * connected state without requesting the remote features. */ -if (conn->out || (hdev->le_features[0 ] &HCI_LE_PERIPHERAL_FEATURES )) - err =hci_cmd_sync_queue_once (hdev, - hci_le_read_remote_features_sync, -hci_conn_hold (conn), - le_read_features_complete); -else +if (conn->out || (hdev->le_features[0 ] &HCI_LE_PERIPHERAL_FEATURES )) { +hci_conn_get (conn); +hci_conn_hold (conn); + err =__hci_cmd_sync_queue_once (hdev, + hci_le_read_remote_features_sync, + conn, + le_read_features_complete); +if (err) { +hci_conn_drop (conn); +hci_conn_put (conn); +if (err == -EEXIST ) + err =0 ; + } + }else err = -EOPNOTSUPP ;return err;
Signature · 1 line
2.52.0
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=1033902
---Test result---
Test Summary:
CheckPatch PENDING 0.36 seconds GitLint PENDING 0.36 seconds SubjectPrefix PASS 0.12 seconds BuildKernel PASS 26.09 seconds CheckAllWarning PASS 28.88 seconds CheckSparse PASS 31.99 seconds BuildKernel32 PASS 25.82 seconds TestRunnerSetup PASS 566.38 seconds TestRunner_l2cap-tester PASS 25.56 seconds TestRunner_iso-tester PASS 86.13 seconds TestRunner_bnep-tester PASS 6.20 seconds TestRunner_mgmt-tester FAIL 116.07 seconds TestRunner_rfcomm-tester PASS 9.38 seconds TestRunner_sco-tester FAIL 14.36 seconds TestRunner_ioctl-tester PASS 10.09 seconds TestRunner_mesh-tester FAIL 11.42 seconds TestRunner_smp-tester PASS 8.52 seconds TestRunner_userchan-tester PASS 6.68 seconds IncrementalBuild PENDING 0.64 seconds
Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script
Output:
##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner Output:
Total: 494, Passed: 489 (99.0%), Failed: 1, Not Run: 4
Failed Test Cases
Read Exp Feature - Success Failed 0.106 seconds
##############################
Test: TestRunner_sco-tester - FAIL
Desc: Run sco-tester with test-runner
Output:
WARNING: possible circular locking dependency detected
BUG: sleeping function called from invalid context at net/core/sock.c:3782
Total: 30, Passed: 30 (100.0%), Failed: 0, Not Run: 0
##############################
Test: TestRunner_mesh-tester - FAIL
Desc: Run mesh-tester with test-runner Output:
Total: 10, Passed: 8 (80.0%), Failed: 2, Not Run: 0
Failed Test Cases
Mesh - Send cancel - 1 Timed out 1.974 seconds Mesh - Send cancel - 2 Timed out 1.996 seconds
##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series Output:
---
Regards,
Linux Bluetooth
Luiz Augusto von Dentz
maintainerHi Cihangir,
Quoted 84 lines · Cihangir Akturk
KASAN reported a slab-use-after-free in
le_read_features_complete()running fromhci_cmd_sync_work.le_read_features_complete()can run after hci_rx_work/hci_conn_del() has removed the link, so the destroy callback may touch a freedhci_connand trigger a KASAN use-after-free. Duplicate submissions also need to drop the references to avoid leaking the hold and blocking teardown.Fix this by taking
hci_conn_get()before queueing, passing the conn directly as work data, and balancing hci_conn_hold()/drop() and hci_conn_get()/put() in the completion and all error/-EEXIST paths so the connection stays referenced while the work runs and is released afterwards.Reported-by [email protected]Signed-off-by Cihangir Akturk <[email protected]>--- net/bluetooth/hci_sync.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-)I am not entirely sure that removing the err ==
-ECANCELEDearly return is strictly correct. My assumption is that, with the changes in this patch, there does not appear to be another cancellation path that reliably balanceshci_conn_drop()andhci_conn_put()for this command. Based on that assumption, keeping the early return could leave the references taken before queuing unbalanced on cancellation, so I opted to remove it to keep the lifetime handling consistent.net/bluetooth/hci_sync.c +16
-11@@ -776,14 +776,23 @@ _hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,* - Lookup if an entry already exist and only if it doesn't creates a new entry * and queue it. */ -int hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func, +static int __hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func,void *data,hci_cmd_sync_work_destroy_t destroy) {if (hci_cmd_sync_lookup_entry (hdev, func, data, destroy)) -return 0 ; +return -EEXIST ;return hci_cmd_sync_queue (hdev, func, data, destroy); } + +int hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func, +void *data,hci_cmd_sync_work_destroy_t destroy) +{ +int ret; + + ret =__hci_cmd_sync_queue_once (hdev, func, data, destroy); +return ret == -EEXIST ?0 : ret; +}EXPORT_SYMBOL (hci_cmd_sync_queue_once);/* Run HCI command: @@ -7338,10 +7347,8 @@ static void le_read_features_complete(struct hci_dev *hdev, void *data, int err)bt_dev_dbg (hdev,"err %d" , err); -if (err == -ECANCELED ) -return ; -hci_conn_drop (conn); +hci_conn_put (conn); } static int hci_le_read_all_remote_features_sync(struct hci_dev *hdev,@@ -7408,12 +7415,20 @@ int hci_le_read_remote_features(struct hci_conn *conn)* role is possible. Otherwise just transition into the * connected state without requesting the remote features. */ -if (conn->out || (hdev->le_features[0 ] &HCI_LE_PERIPHERAL_FEATURES )) - err =hci_cmd_sync_queue_once (hdev, - hci_le_read_remote_features_sync, -hci_conn_hold (conn), - le_read_features_complete); -else +if (conn->out || (hdev->le_features[0 ] &HCI_LE_PERIPHERAL_FEATURES )) { +hci_conn_get (conn); +hci_conn_hold (conn); + err = __hci_cmd_sync_queue_once(hdev,
net/bluetooth/hci_sync.c int hci_le_read_remote_features(struct hci_conn *conn) · line 7431
Quoted 10 lines
net/bluetooth/hci_sync.c +10
-0+ hci_le_read_remote_features_sync, + conn, + le_read_features_complete); +if (err) { +hci_conn_drop (conn); +hci_conn_put (conn); +if (err == -EEXIST ) + err =0 ; + } + }else
Sort of overkill, why do we have to use 2 references? Also we do have code for dequeuing callbacks using conn as user_data so either that is not working or there is something else at play here. Maybe we need to change the order so that dequeue happens before hci_conn_cleanup:
net/bluetooth/hci_conn.c +3 -3
@@ -1232,15 +1232,15 @@ void hci_conn_del(struct hci_conn *conn)skb_queue_purge (&conn->data_q);skb_queue_purge (&conn->tx_q.queue); +/* Dequeue callbacks using connection pointer as data */ +hci_cmd_sync_dequeue (hdev,NULL , conn,NULL ); +/* Remove the connection from the list and cleanup its remaining * state. This is a separate function since for some cases like * BT_CONNECT_SCAN we *only* want the cleanup part without the * rest of hci_conn_del. */ hci_conn_cleanup (conn); - -/* Dequeue callbacks using connection pointer as data */ -hci_cmd_sync_dequeue (hdev,NULL , conn,NULL ); }struct hci_dev *hci_get_route (bdaddr_t *dst,bdaddr_t *src,uint8_t src_type)
Quoted 6 lines
net/bluetooth/hci_sync.c +0
-1err = -EOPNOTSUPP ;return err; --2.52.0
Signature · 1 line
Luiz Augusto von Dentz
Hi,
ke, 2025-12-17 kello 16:36 -0500, Luiz Augusto von Dentz kirjoitti:
Quoted 107 lines
Hi Cihangir,
Quoted 84 lines · Cihangir Akturk
KASAN reported a slab-use-after-free in
le_read_features_complete()running fromhci_cmd_sync_work.le_read_features_complete()can run after hci_rx_work/hci_conn_del() has removed the link, so the destroy callback may touch a freedhci_connand trigger a KASAN use-after-free. Duplicate submissions also need to drop the references to avoid leaking the hold and blocking teardown.Fix this by taking
hci_conn_get()before queueing, passing the conn directly as work data, and balancing hci_conn_hold()/drop() and hci_conn_get()/put() in the completion and all error/-EEXIST paths so the connection stays referenced while the work runs and is released afterwards.Reported-by [email protected]Signed-off-by Cihangir Akturk <[email protected]>--- net/bluetooth/hci_sync.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-)I am not entirely sure that removing the err ==
-ECANCELEDearly return is strictly correct. My assumption is that, with the changes in this patch, there does not appear to be another cancellation path that reliably balanceshci_conn_drop()andhci_conn_put()for this command. Based on that assumption, keeping the early return could leave the references taken before queuing unbalanced on cancellation, so I opted to remove it to keep the lifetime handling consistent.net/bluetooth/hci_sync.c +16
-11@@ -776,14 +776,23 @@ _hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,* - Lookup if an entry already exist and only if it doesn't creates a new entry * and queue it. */ -int hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func, +static int __hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func,void *data,hci_cmd_sync_work_destroy_t destroy) {if (hci_cmd_sync_lookup_entry (hdev, func, data, destroy)) -return 0 ; +return -EEXIST ;return hci_cmd_sync_queue (hdev, func, data, destroy); } + +int hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func, +void *data,hci_cmd_sync_work_destroy_t destroy) +{ +int ret; + + ret =__hci_cmd_sync_queue_once (hdev, func, data, destroy); +return ret == -EEXIST ?0 : ret; +}EXPORT_SYMBOL (hci_cmd_sync_queue_once);/* Run HCI command: @@ -7338,10 +7347,8 @@ static void le_read_features_complete(struct hci_dev *hdev, void *data, int err)bt_dev_dbg (hdev,"err %d" , err); -if (err == -ECANCELED ) -return ; -hci_conn_drop (conn); +hci_conn_put (conn); } static int hci_le_read_all_remote_features_sync(struct hci_dev *hdev,@@ -7408,12 +7415,20 @@ int hci_le_read_remote_features(struct hci_conn *conn)* role is possible. Otherwise just transition into the * connected state without requesting the remote features. */ -if (conn->out || (hdev->le_features[0 ] &HCI_LE_PERIPHERAL_FEATURES )) - err =hci_cmd_sync_queue_once (hdev, - hci_le_read_remote_features_sync, -hci_conn_hold (conn), - le_read_features_complete); -else +if (conn->out || (hdev->le_features[0 ] &HCI_LE_PERIPHERAL_FEATURES )) { +hci_conn_get (conn); +hci_conn_hold (conn); + err = __hci_cmd_sync_queue_once(hdev,net/bluetooth/hci_sync.c int hci_le_read_remote_features(struct hci_conn *conn) · line 7431
Quoted 10 lines
net/bluetooth/hci_sync.c +10
-0+ hci_le_read_remote_features_sync, + conn, + le_read_features_complete); +if (err) { +hci_conn_drop (conn); +hci_conn_put (conn); +if (err == -EEXIST ) + err =0 ; + } + }else Sort of overkill, why do we have to use 2 references? Also we do have code for dequeuing callbacks using conn as
user_dataso either that is not working or there is something else at play here. Maybe we need to change the order so that dequeue happens beforehci_conn_cleanup:net/bluetooth/hci_conn.c +0
-0
net/bluetooth/hci_conn.c void hci_conn_del(struct hci_conn *conn) · line 1237
Quoted 6 lines
net/bluetooth/hci_conn.c +3
-0@@ -1232,15 +1232,15 @@ void hci_conn_del(struct hci_conn *conn)skb_queue_purge (&conn->data_q);skb_queue_purge (&conn->tx_q.queue); +/* Dequeue callbacks using connection pointer as data */ +hci_cmd_sync_dequeue (hdev,NULL , conn,NULL ); +
hci_cmd_sync_dequeue() does not (i) cancel + wait for a job that is already running, (ii) prevent further jobs for this conn from being queued. So it's not guaranteed to work here AFAICS.
For (i): note running hci_sync job may be blocked on taking hdev->lock, which is held here, so trying to cancel + wait deadlocks. Does not seem straightforward to fix.
For (ii): one would need to audit the places where these jobs are queued, and make sure they are all done with hdev->lock held, to avoid racing with the code here. Maybe doable with separate queueing function that has lockdep asserts.
I suggested some time ago to always hold either refcount or lock to keep the hci_conn alive everywhere, also in these hci_sync callbacks:
https://lore.kernel.org/linux-bluetooth/[email protected]/
with similar changes as suggested in this patch. This may be the simpler fix.
Quoted 20 lines
net/bluetooth/hci_conn.c +0
-3/* Remove the connection from the list and cleanup its remaining * state. This is a separate function since for some cases like * BT_CONNECT_SCAN we *only* want the cleanup part without the * rest of hci_conn_del. */ hci_conn_cleanup (conn); - -/* Dequeue callbacks using connection pointer as data */ -hci_cmd_sync_dequeue (hdev,NULL , conn,NULL ); }struct hci_dev *hci_get_route (bdaddr_t *dst,bdaddr_t *src,uint8_t src_type)Quoted 7 lines
net/bluetooth/hci_sync.c +0
-1err = -EOPNOTSUPP ;return err; --2.52.0
Signature · 1 line
Pauli Virtanen
Luiz Augusto von Dentz
maintainerHi Pauli,
Quoted 122 lines · Pauli Virtanen
Hi,
ke, 2025-12-17 kello 16:36 -0500, Luiz Augusto von Dentz kirjoitti:
Quoted 107 lines
Hi Cihangir,
Quoted 84 lines · Cihangir Akturk
KASAN reported a slab-use-after-free in
le_read_features_complete()running fromhci_cmd_sync_work.le_read_features_complete()can run after hci_rx_work/hci_conn_del() has removed the link, so the destroy callback may touch a freedhci_connand trigger a KASAN use-after-free. Duplicate submissions also need to drop the references to avoid leaking the hold and blocking teardown.Fix this by taking
hci_conn_get()before queueing, passing the conn directly as work data, and balancing hci_conn_hold()/drop() and hci_conn_get()/put() in the completion and all error/-EEXIST paths so the connection stays referenced while the work runs and is released afterwards.Reported-by [email protected]Signed-off-by Cihangir Akturk <[email protected]>--- net/bluetooth/hci_sync.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-)I am not entirely sure that removing the err ==
-ECANCELEDearly return is strictly correct. My assumption is that, with the changes in this patch, there does not appear to be another cancellation path that reliably balanceshci_conn_drop()andhci_conn_put()for this command. Based on that assumption, keeping the early return could leave the references taken before queuing unbalanced on cancellation, so I opted to remove it to keep the lifetime handling consistent.net/bluetooth/hci_sync.c +16
-11@@ -776,14 +776,23 @@ _hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,* - Lookup if an entry already exist and only if it doesn't creates a new entry * and queue it. */ -int hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func, +static int __hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func,void *data,hci_cmd_sync_work_destroy_t destroy) {if (hci_cmd_sync_lookup_entry (hdev, func, data, destroy)) -return 0 ; +return -EEXIST ;return hci_cmd_sync_queue (hdev, func, data, destroy); } + +int hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func, +void *data,hci_cmd_sync_work_destroy_t destroy) +{ +int ret; + + ret =__hci_cmd_sync_queue_once (hdev, func, data, destroy); +return ret == -EEXIST ?0 : ret; +}EXPORT_SYMBOL (hci_cmd_sync_queue_once);/* Run HCI command: @@ -7338,10 +7347,8 @@ static void le_read_features_complete(struct hci_dev *hdev, void *data, int err)bt_dev_dbg (hdev,"err %d" , err); -if (err == -ECANCELED ) -return ; -hci_conn_drop (conn); +hci_conn_put (conn); } static int hci_le_read_all_remote_features_sync(struct hci_dev *hdev,@@ -7408,12 +7415,20 @@ int hci_le_read_remote_features(struct hci_conn *conn)* role is possible. Otherwise just transition into the * connected state without requesting the remote features. */ -if (conn->out || (hdev->le_features[0 ] &HCI_LE_PERIPHERAL_FEATURES )) - err =hci_cmd_sync_queue_once (hdev, - hci_le_read_remote_features_sync, -hci_conn_hold (conn), - le_read_features_complete); -else +if (conn->out || (hdev->le_features[0 ] &HCI_LE_PERIPHERAL_FEATURES )) { +hci_conn_get (conn); +hci_conn_hold (conn); + err = __hci_cmd_sync_queue_once(hdev,net/bluetooth/hci_sync.c int hci_le_read_remote_features(struct hci_conn *conn) · line 7431
Quoted 10 lines
net/bluetooth/hci_sync.c +10
-0+ hci_le_read_remote_features_sync, + conn, + le_read_features_complete); +if (err) { +hci_conn_drop (conn); +hci_conn_put (conn); +if (err == -EEXIST ) + err =0 ; + } + }else Sort of overkill, why do we have to use 2 references? Also we do have code for dequeuing callbacks using conn as
user_dataso either that is not working or there is something else at play here. Maybe we need to change the order so that dequeue happens beforehci_conn_cleanup:net/bluetooth/hci_conn.c +0
-0net/bluetooth/hci_conn.c void hci_conn_del(struct hci_conn *conn) · line 1237
Quoted 6 lines
net/bluetooth/hci_conn.c +3
-0@@ -1232,15 +1232,15 @@ void hci_conn_del(struct hci_conn *conn)skb_queue_purge (&conn->data_q);skb_queue_purge (&conn->tx_q.queue); +/* Dequeue callbacks using connection pointer as data */ +hci_cmd_sync_dequeue (hdev,NULL , conn,NULL ); +
hci_cmd_sync_dequeue()does not (i) cancel + wait for a job that is already running, (ii) prevent further jobs for this conn from being queued. So it's not guaranteed to work here AFAICS.
Quoted 3 lines
For (i): note running
hci_syncjob may be blocked on takinghdev->lock, which is held here, so trying to cancel + wait deadlocks. Does not seem straightforward to fix.
Hmm, there is a lock though that is used when running the callbacks:
hci_req_sync_lock(hdev);
err = entry->func(hdev, entry->data);
if (entry->destroy)
entry->destroy(hdev, entry->data, err);
hci_req_sync_unlock(hdev);We could attempt to acquire hci_req_sync_lock on dequeue, but it looks like there are code paths that already do call hci_conn_del with that lock so the likes of mgmt-tester deadlock, anyway if there are code paths already doing with hci_req_sync_lock held then it should be safe to already require it when doing hci_conn_del or maybe rename hci_conn_del to hci_conn_del_sync and then have hci_conn_del performing the hci_req_sync_lock before calling hci_conn_del_sync then work out the code paths where hci_req_sync_lock is already held to use hci_conn_del_sync.
Quoted 36 lines
For (ii): one would need to audit the places where these jobs are queued, and make sure they are all done with
hdev->lockheld, to avoid racing with the code here. Maybe doable with separate queueing function that has lockdep asserts.I suggested some time ago to always hold either refcount or lock to keep the
hci_connalive everywhere, also in thesehci_synccallbacks:https://lore.kernel.org/linux-bluetooth/[email protected]/
with similar changes as suggested in this patch. This may be the simpler fix.
Quoted 13 lines
net/bluetooth/hci_conn.c +0
-3/* Remove the connection from the list and cleanup its remaining * state. This is a separate function since for some cases like * BT_CONNECT_SCAN we *only* want the cleanup part without the * rest of hci_conn_del. */ hci_conn_cleanup (conn); - -/* Dequeue callbacks using connection pointer as data */ -hci_cmd_sync_dequeue (hdev,NULL , conn,NULL ); }struct hci_dev *hci_get_route (bdaddr_t *dst,bdaddr_t *src,uint8_t src_type)Quoted 7 lines
Quoted 6 lines
net/bluetooth/hci_sync.c +0
-1err = -EOPNOTSUPP ;return err; --2.52.0
--
Pauli Virtanen
Signature · 1 line
Luiz Augusto von Dentz
Hi,
to, 2025-12-18 kello 12:09 -0500, Luiz Augusto von Dentz kirjoitti:
Quoted 137 lines
Hi Pauli,
Quoted 122 lines · Pauli Virtanen
Hi,
ke, 2025-12-17 kello 16:36 -0500, Luiz Augusto von Dentz kirjoitti:
Quoted 107 lines
Hi Cihangir,
Quoted 84 lines · Cihangir Akturk
KASAN reported a slab-use-after-free in
le_read_features_complete()running fromhci_cmd_sync_work.le_read_features_complete()can run after hci_rx_work/hci_conn_del() has removed the link, so the destroy callback may touch a freedhci_connand trigger a KASAN use-after-free. Duplicate submissions also need to drop the references to avoid leaking the hold and blocking teardown.Fix this by taking
hci_conn_get()before queueing, passing the conn directly as work data, and balancing hci_conn_hold()/drop() and hci_conn_get()/put() in the completion and all error/-EEXIST paths so the connection stays referenced while the work runs and is released afterwards.Reported-by [email protected]Signed-off-by Cihangir Akturk <[email protected]>--- net/bluetooth/hci_sync.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-)I am not entirely sure that removing the err ==
-ECANCELEDearly return is strictly correct. My assumption is that, with the changes in this patch, there does not appear to be another cancellation path that reliably balanceshci_conn_drop()andhci_conn_put()for this command. Based on that assumption, keeping the early return could leave the references taken before queuing unbalanced on cancellation, so I opted to remove it to keep the lifetime handling consistent.net/bluetooth/hci_sync.c +16
-11@@ -776,14 +776,23 @@ _hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,* - Lookup if an entry already exist and only if it doesn't creates a new entry * and queue it. */ -int hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func, +static int __hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func,void *data,hci_cmd_sync_work_destroy_t destroy) {if (hci_cmd_sync_lookup_entry (hdev, func, data, destroy)) -return 0 ; +return -EEXIST ;return hci_cmd_sync_queue (hdev, func, data, destroy); } + +int hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func, +void *data,hci_cmd_sync_work_destroy_t destroy) +{ +int ret; + + ret =__hci_cmd_sync_queue_once (hdev, func, data, destroy); +return ret == -EEXIST ?0 : ret; +}EXPORT_SYMBOL (hci_cmd_sync_queue_once);/* Run HCI command: @@ -7338,10 +7347,8 @@ static void le_read_features_complete(struct hci_dev *hdev, void *data, int err)bt_dev_dbg (hdev,"err %d" , err); -if (err == -ECANCELED ) -return ; -hci_conn_drop (conn); +hci_conn_put (conn); } static int hci_le_read_all_remote_features_sync(struct hci_dev *hdev,@@ -7408,12 +7415,20 @@ int hci_le_read_remote_features(struct hci_conn *conn)* role is possible. Otherwise just transition into the * connected state without requesting the remote features. */ -if (conn->out || (hdev->le_features[0 ] &HCI_LE_PERIPHERAL_FEATURES )) - err =hci_cmd_sync_queue_once (hdev, - hci_le_read_remote_features_sync, -hci_conn_hold (conn), - le_read_features_complete); -else +if (conn->out || (hdev->le_features[0 ] &HCI_LE_PERIPHERAL_FEATURES )) { +hci_conn_get (conn); +hci_conn_hold (conn); + err = __hci_cmd_sync_queue_once(hdev,net/bluetooth/hci_sync.c int hci_le_read_remote_features(struct hci_conn *conn) · line 7431
Quoted 10 lines
net/bluetooth/hci_sync.c +10
-0+ hci_le_read_remote_features_sync, + conn, + le_read_features_complete); +if (err) { +hci_conn_drop (conn); +hci_conn_put (conn); +if (err == -EEXIST ) + err =0 ; + } + }else Sort of overkill, why do we have to use 2 references? Also we do have code for dequeuing callbacks using conn as
user_dataso either that is not working or there is something else at play here. Maybe we need to change the order so that dequeue happens beforehci_conn_cleanup:net/bluetooth/hci_conn.c +0
-0net/bluetooth/hci_conn.c void hci_conn_del(struct hci_conn *conn) · line 1237
Quoted 6 lines
net/bluetooth/hci_conn.c +3
-0@@ -1232,15 +1232,15 @@ void hci_conn_del(struct hci_conn *conn)skb_queue_purge (&conn->data_q);skb_queue_purge (&conn->tx_q.queue); +/* Dequeue callbacks using connection pointer as data */ +hci_cmd_sync_dequeue (hdev,NULL , conn,NULL ); +
hci_cmd_sync_dequeue()does not (i) cancel + wait for a job that is already running, (ii) prevent further jobs for this conn from being queued. So it's not guaranteed to work here AFAICS.Quoted 3 lines
For (i): note running
hci_syncjob may be blocked on takinghdev->lock, which is held here, so trying to cancel + wait deadlocks. Does not seem straightforward to fix.Hmm, there is a lock though that is used when running the callbacks:
hci_req_sync_lock(hdev); err = entry->func(hdev, entry->data); if (entry->destroy) entry->destroy(hdev, entry->data, err); hci_req_sync_unlock(hdev);
Quoted 9 lines
We could attempt to acquire
hci_req_sync_lockon dequeue, but it looks like there are code paths that already do callhci_conn_delwith that lock so the likes of mgmt-tester deadlock, anyway if there are code paths already doing withhci_req_sync_lockheld then it should be safe to already require it when doinghci_conn_delor maybe renamehci_conn_deltohci_conn_del_syncand then havehci_conn_delperforming thehci_req_sync_lockbefore callinghci_conn_del_syncthen work out the code paths wherehci_req_sync_lockis already held to usehci_conn_del_sync.
Taking hci_req_sync() before hci_conn_del() seems to face some issues:
[hdev->req_workqueue] hci_disconnect_sync()
[hdev->req_workqueue] __hci_cmd_sync_status() -- req_lock still held
[hdev->workqueue] hci_disconn_complete_evt()
[hdev->workqueue] hci_conn_del() -- blocks on req_lock
since the command may end up waiting for hci_conn_del. The disconnect may also be spontaneous from controller, so looks like any __hci_cmd_sync_status() could lock up.
AFAICS, you'd need something like a dedicated hdev->workqueue work that does the final hci_conn put under req_lock, so that there is no job running in either workqueue during the final put. And get it also right in the hdev teardown.
***
For how the other refcount / lock approach would look in practice, iirc the earlier RFC had all hci_lookup callsites dealt with (aside from 6lowpan)
https://lore.kernel.org/linux-bluetooth/[email protected]/
Quoted 38 lines
Quoted 36 lines
For (ii): one would need to audit the places where these jobs are queued, and make sure they are all done with
hdev->lockheld, to avoid racing with the code here. Maybe doable with separate queueing function that has lockdep asserts.I suggested some time ago to always hold either refcount or lock to keep the
hci_connalive everywhere, also in thesehci_synccallbacks:https://lore.kernel.org/linux-bluetooth/[email protected]/
with similar changes as suggested in this patch. This may be the simpler fix.
Quoted 13 lines
net/bluetooth/hci_conn.c +0
-3/* Remove the connection from the list and cleanup its remaining * state. This is a separate function since for some cases like * BT_CONNECT_SCAN we *only* want the cleanup part without the * rest of hci_conn_del. */ hci_conn_cleanup (conn); - -/* Dequeue callbacks using connection pointer as data */ -hci_cmd_sync_dequeue (hdev,NULL , conn,NULL ); }struct hci_dev *hci_get_route (bdaddr_t *dst,bdaddr_t *src,uint8_t src_type)Quoted 7 lines
Quoted 6 lines
net/bluetooth/hci_sync.c +0
-1err = -EOPNOTSUPP ;return err; --2.52.0
--
Pauli Virtanen
Quoted 2 lines · Luiz Augusto von Dentz
Hi Cihangir,
Hi Luiz,
Quoted 92 lines · Cihangir Akturk
Quoted 91 lines
KASAN reported a slab-use-after-free in
le_read_features_complete()running fromhci_cmd_sync_work.le_read_features_complete()can run after hci_rx_work/hci_conn_del() has removed the link, so the destroy callback may touch a freedhci_connand trigger a KASAN use-after-free. Duplicate submissions also need to drop the references to avoid leaking the hold and blocking teardown.Fix this by taking
hci_conn_get()before queueing, passing the conn directly as work data, and balancing hci_conn_hold()/drop() and hci_conn_get()/put() in the completion and all error/-EEXIST paths so the connection stays referenced while the work runs and is released afterwards.Reported-by [email protected]Signed-off-by Cihangir Akturk <[email protected]>--- net/bluetooth/hci_sync.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-)I am not entirely sure that removing the err ==
-ECANCELEDearly return is strictly correct. My assumption is that, with the changes in this patch, there does not appear to be another cancellation path that reliably balanceshci_conn_drop()andhci_conn_put()for this command. Based on that assumption, keeping the early return could leave the references taken before queuing unbalanced on cancellation, so I opted to remove it to keep the lifetime handling consistent.net/bluetooth/hci_sync.c +23
-11@@ -776,14 +776,23 @@ _hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,* - Lookup if an entry already exist and only if it doesn't creates a new entry * and queue it. */ -int hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func, +static int __hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func,void *data,hci_cmd_sync_work_destroy_t destroy) {if (hci_cmd_sync_lookup_entry (hdev, func, data, destroy)) -return 0 ; +return -EEXIST ;return hci_cmd_sync_queue (hdev, func, data, destroy); } + +int hci_cmd_sync_queue_once (struct hci_dev *hdev,hci_cmd_sync_work_func_t func, +void *data,hci_cmd_sync_work_destroy_t destroy) +{ +int ret; + + ret =__hci_cmd_sync_queue_once (hdev, func, data, destroy); +return ret == -EEXIST ?0 : ret; +}EXPORT_SYMBOL (hci_cmd_sync_queue_once);/* Run HCI command: @@ -7338,10 +7347,8 @@ static void le_read_features_complete(struct hci_dev *hdev, void *data, int err)bt_dev_dbg (hdev,"err %d" , err); -if (err == -ECANCELED ) -return ; -hci_conn_drop (conn); +hci_conn_put (conn); } static int hci_le_read_all_remote_features_sync(struct hci_dev *hdev,@@ -7408,12 +7415,20 @@ int hci_le_read_remote_features(struct hci_conn *conn)* role is possible. Otherwise just transition into the * connected state without requesting the remote features. */ -if (conn->out || (hdev->le_features[0 ] &HCI_LE_PERIPHERAL_FEATURES )) - err =hci_cmd_sync_queue_once (hdev, - hci_le_read_remote_features_sync, -hci_conn_hold (conn), - le_read_features_complete); -else +if (conn->out || (hdev->le_features[0 ] &HCI_LE_PERIPHERAL_FEATURES )) { +hci_conn_get (conn); +hci_conn_hold (conn); + err =__hci_cmd_sync_queue_once (hdev, + hci_le_read_remote_features_sync, + conn, + le_read_features_complete); +if (err) { +hci_conn_drop (conn); +hci_conn_put (conn); +if (err == -EEXIST )
Quoted 9 lines
net/bluetooth/hci_sync.c int hci_le_read_remote_features(struct hci_conn *conn) · line 7431
Quoted 3 lines
net/bluetooth/hci_sync.c +3
-0+ err =0 ; + } + }else Sort of overkill, why do we have to use 2 references? Also we do have code for dequeuing callbacks using conn as
user_dataso either that is not working or there is something else at play here. Maybe we need to change the order so that dequeue happens beforehci_conn_cleanup:
From what I understand based on the KASAN trace, the issue happens when a disconnect event is handled in hci_event_work while, at the same time, hci_cmd_sync_work processes the LE Read Remote Features command. So, le_read_features_complete() ends up calling hci_conn_drop() on a connection that appears to have already been freed.
Holding a reference with hci_conn_hold() alone does not seem sufficient to prevent the disconnect path from removing and freeing the hci_conn. That was the reason I tried taking an additional reference with hci_conn_get(), to keep the connection object around until the work finishes.
Quoted 25 lines
net/bluetooth/hci_conn.c +3
-3@@ -1232,15 +1232,15 @@ void hci_conn_del(struct hci_conn *conn)skb_queue_purge (&conn->data_q);skb_queue_purge (&conn->tx_q.queue); +/* Dequeue callbacks using connection pointer as data */ +hci_cmd_sync_dequeue (hdev,NULL , conn,NULL ); +/* Remove the connection from the list and cleanup its remaining * state. This is a separate function since for some cases like * BT_CONNECT_SCAN we *only* want the cleanup part without the * rest of hci_conn_del. */ hci_conn_cleanup (conn); - -/* Dequeue callbacks using connection pointer as data */ -hci_cmd_sync_dequeue (hdev,NULL , conn,NULL ); }struct hci_dev *hci_get_route (bdaddr_t *dst,bdaddr_t *src,uint8_t src_type)
Quoted 7 lines
Quoted 6 lines
net/bluetooth/hci_sync.c +0
-1err = -EOPNOTSUPP ;return err; --2.52.0
I tried this change and tested the suggested ordering, but in my testing the issue still appears to reproduce, so it does not seem to fully address the problem.
Quoted 3 lines
--
Luiz Augusto von Dentz