[PATCH v1] Bluetooth: hci_sync: fix double free in 'hci_discovery_filter_clear()'

Arseniy Krasnov posted 1 patch 4 months ago
There is a newer version of this series
net/bluetooth/hci_sync.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
[PATCH v1] Bluetooth: hci_sync: fix double free in 'hci_discovery_filter_clear()'
Posted by Arseniy Krasnov 4 months ago
Function 'hci_discovery_filter_clear()' frees 'uuids' array and then
sets it to NULL. There is a tiny chance of the following race:

'hci_cmd_sync_work()'

 'update_passive_scan_sync()'

   'hci_update_passive_scan_sync()'

     'hci_discovery_filter_clear()'
       kfree(uuids);

       <-------------------------preempted-------------------------------->
                                           'start_service_discovery()'

                                             'hci_discovery_filter_clear()'
                                               kfree(uuids); // DOUBLE FREE

       <-------------------------preempted-------------------------------->

      uuids = NULL;

To fix it let's add locking around call  'hci_update_passive_scan_sync()' in
'update_passive_scan_sync()'. Otherwise the following backtrace fires:

[ ] ------------[ cut here ]------------
[ ] kernel BUG at mm/slub.c:547!
[ ] Internal error: Oops - BUG: 00000000f2000800 [#1] PREEMPT SMP
[ ] CPU: 3 UID: 0 PID: 246 Comm: bluetoothd Tainted: G O 6.12.19-sdkernel #1
[ ] Tainted: [O]=OOT_MODULE
[ ] pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[ ] pc : __slab_free+0xf8/0x348
[ ] lr : __slab_free+0x48/0x348
...
[ ] Call trace:
[ ]  __slab_free+0xf8/0x348
[ ]  kfree+0x164/0x27c
[ ]  start_service_discovery+0x1d0/0x2c0
[ ]  hci_sock_sendmsg+0x518/0x924
[ ]  __sock_sendmsg+0x54/0x60
[ ]  sock_write_iter+0x98/0xf8
[ ]  do_iter_readv_writev+0xe4/0x1c8
[ ]  vfs_writev+0x128/0x2b0
[ ]  do_writev+0xfc/0x118
[ ]  __arm64_sys_writev+0x20/0x2c
[ ]  invoke_syscall+0x68/0xf0
[ ]  el0_svc_common.constprop.0+0x40/0xe0
[ ]  do_el0_svc+0x1c/0x28
[ ]  el0_svc+0x30/0xd0
[ ]  el0t_64_sync_handler+0x100/0x12c
[ ]  el0t_64_sync+0x194/0x198
[ ] Code: 8b0002e6 eb17031f 54fffbe1 d503201f (d4210000) 
[ ] ---[ end trace 0000000000000000 ]---

Cc: stable@vger.kernel.org
Fixes: ad383c2c65a5 ("Bluetooth: hci_sync: Enable advertising when LL privacy is enabled")
Signed-off-by: Arseniy Krasnov <avkrasnov@salutedevices.com>
---
 net/bluetooth/hci_sync.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
index e56b1cbedab90..61a9922eb820d 100644
--- a/net/bluetooth/hci_sync.c
+++ b/net/bluetooth/hci_sync.c
@@ -3167,7 +3167,15 @@ int hci_update_scan(struct hci_dev *hdev)
 
 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
 {
-	return hci_update_passive_scan_sync(hdev);
+	int ret;
+
+	hci_dev_lock(hdev);
+
+	ret = hci_update_passive_scan_sync(hdev);
+
+	hci_dev_unlock(hdev);
+
+	return ret;
 }
 
 int hci_update_passive_scan(struct hci_dev *hdev)
-- 
2.30.1

Re: [PATCH v1] Bluetooth: hci_sync: fix double free in 'hci_discovery_filter_clear()'
Posted by Luiz Augusto von Dentz 4 months ago
Hi Arseniy,

On Wed, Jun 11, 2025 at 12:31 PM Arseniy Krasnov
<avkrasnov@salutedevices.com> wrote:
>
> Function 'hci_discovery_filter_clear()' frees 'uuids' array and then
> sets it to NULL. There is a tiny chance of the following race:
>
> 'hci_cmd_sync_work()'
>
>  'update_passive_scan_sync()'
>
>    'hci_update_passive_scan_sync()'
>
>      'hci_discovery_filter_clear()'
>        kfree(uuids);
>
>        <-------------------------preempted-------------------------------->
>                                            'start_service_discovery()'
>
>                                              'hci_discovery_filter_clear()'
>                                                kfree(uuids); // DOUBLE FREE
>
>        <-------------------------preempted-------------------------------->
>
>       uuids = NULL;
>
> To fix it let's add locking around call  'hci_update_passive_scan_sync()' in
> 'update_passive_scan_sync()'. Otherwise the following backtrace fires:
>
> [ ] ------------[ cut here ]------------
> [ ] kernel BUG at mm/slub.c:547!
> [ ] Internal error: Oops - BUG: 00000000f2000800 [#1] PREEMPT SMP
> [ ] CPU: 3 UID: 0 PID: 246 Comm: bluetoothd Tainted: G O 6.12.19-sdkernel #1
> [ ] Tainted: [O]=OOT_MODULE
> [ ] pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
> [ ] pc : __slab_free+0xf8/0x348
> [ ] lr : __slab_free+0x48/0x348
> ...
> [ ] Call trace:
> [ ]  __slab_free+0xf8/0x348
> [ ]  kfree+0x164/0x27c
> [ ]  start_service_discovery+0x1d0/0x2c0
> [ ]  hci_sock_sendmsg+0x518/0x924
> [ ]  __sock_sendmsg+0x54/0x60
> [ ]  sock_write_iter+0x98/0xf8
> [ ]  do_iter_readv_writev+0xe4/0x1c8
> [ ]  vfs_writev+0x128/0x2b0
> [ ]  do_writev+0xfc/0x118
> [ ]  __arm64_sys_writev+0x20/0x2c
> [ ]  invoke_syscall+0x68/0xf0
> [ ]  el0_svc_common.constprop.0+0x40/0xe0
> [ ]  do_el0_svc+0x1c/0x28
> [ ]  el0_svc+0x30/0xd0
> [ ]  el0t_64_sync_handler+0x100/0x12c
> [ ]  el0t_64_sync+0x194/0x198
> [ ] Code: 8b0002e6 eb17031f 54fffbe1 d503201f (d4210000)
> [ ] ---[ end trace 0000000000000000 ]---
>
> Cc: stable@vger.kernel.org
> Fixes: ad383c2c65a5 ("Bluetooth: hci_sync: Enable advertising when LL privacy is enabled")
> Signed-off-by: Arseniy Krasnov <avkrasnov@salutedevices.com>
> ---
>  net/bluetooth/hci_sync.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
> index e56b1cbedab90..61a9922eb820d 100644
> --- a/net/bluetooth/hci_sync.c
> +++ b/net/bluetooth/hci_sync.c
> @@ -3167,7 +3167,15 @@ int hci_update_scan(struct hci_dev *hdev)
>
>  static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
>  {
> -       return hci_update_passive_scan_sync(hdev);
> +       int ret;
> +
> +       hci_dev_lock(hdev);
> +
> +       ret = hci_update_passive_scan_sync(hdev);
> +
> +       hci_dev_unlock(hdev);
> +
> +       return ret;

This deadlocks since it is a callback on hci_cmd_sync_work which shall
never use hci_dev_lock while waiting for events processed by
hci_event_packet which will attempt to do:

    hci_dev_lock(hdev);
    kfree_skb(hdev->recv_event);
    hdev->recv_event = skb_clone(skb, GFP_KERNEL);
    hci_dev_unlock(hdev);

This is why there are no test results because all tests seem to be failing now:

https://github.com/BluezTestBot/bluetooth-next/pull/2884

>  }
>
>  int hci_update_passive_scan(struct hci_dev *hdev)
> --
> 2.30.1
>


-- 
Luiz Augusto von Dentz
Re: [PATCH v1] Bluetooth: hci_sync: fix double free in 'hci_discovery_filter_clear()'
Posted by Arseniy Krasnov 4 months ago

On 12.06.2025 16:12, Luiz Augusto von Dentz wrote:
> Hi Arseniy,
> 
> On Wed, Jun 11, 2025 at 12:31 PM Arseniy Krasnov
> <avkrasnov@salutedevices.com> wrote:
>>
>> Function 'hci_discovery_filter_clear()' frees 'uuids' array and then
>> sets it to NULL. There is a tiny chance of the following race:
>>
>> 'hci_cmd_sync_work()'
>>
>>  'update_passive_scan_sync()'
>>
>>    'hci_update_passive_scan_sync()'
>>
>>      'hci_discovery_filter_clear()'
>>        kfree(uuids);
>>
>>        <-------------------------preempted-------------------------------->
>>                                            'start_service_discovery()'
>>
>>                                              'hci_discovery_filter_clear()'
>>                                                kfree(uuids); // DOUBLE FREE
>>
>>        <-------------------------preempted-------------------------------->
>>
>>       uuids = NULL;
>>
>> To fix it let's add locking around call  'hci_update_passive_scan_sync()' in
>> 'update_passive_scan_sync()'. Otherwise the following backtrace fires:
>>
>> [ ] ------------[ cut here ]------------
>> [ ] kernel BUG at mm/slub.c:547!
>> [ ] Internal error: Oops - BUG: 00000000f2000800 [#1] PREEMPT SMP
>> [ ] CPU: 3 UID: 0 PID: 246 Comm: bluetoothd Tainted: G O 6.12.19-sdkernel #1
>> [ ] Tainted: [O]=OOT_MODULE
>> [ ] pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
>> [ ] pc : __slab_free+0xf8/0x348
>> [ ] lr : __slab_free+0x48/0x348
>> ...
>> [ ] Call trace:
>> [ ]  __slab_free+0xf8/0x348
>> [ ]  kfree+0x164/0x27c
>> [ ]  start_service_discovery+0x1d0/0x2c0
>> [ ]  hci_sock_sendmsg+0x518/0x924
>> [ ]  __sock_sendmsg+0x54/0x60
>> [ ]  sock_write_iter+0x98/0xf8
>> [ ]  do_iter_readv_writev+0xe4/0x1c8
>> [ ]  vfs_writev+0x128/0x2b0
>> [ ]  do_writev+0xfc/0x118
>> [ ]  __arm64_sys_writev+0x20/0x2c
>> [ ]  invoke_syscall+0x68/0xf0
>> [ ]  el0_svc_common.constprop.0+0x40/0xe0
>> [ ]  do_el0_svc+0x1c/0x28
>> [ ]  el0_svc+0x30/0xd0
>> [ ]  el0t_64_sync_handler+0x100/0x12c
>> [ ]  el0t_64_sync+0x194/0x198
>> [ ] Code: 8b0002e6 eb17031f 54fffbe1 d503201f (d4210000)
>> [ ] ---[ end trace 0000000000000000 ]---
>>
>> Cc: stable@vger.kernel.org
>> Fixes: ad383c2c65a5 ("Bluetooth: hci_sync: Enable advertising when LL privacy is enabled")
>> Signed-off-by: Arseniy Krasnov <avkrasnov@salutedevices.com>
>> ---
>>  net/bluetooth/hci_sync.c | 10 +++++++++-
>>  1 file changed, 9 insertions(+), 1 deletion(-)
>>
>> diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
>> index e56b1cbedab90..61a9922eb820d 100644
>> --- a/net/bluetooth/hci_sync.c
>> +++ b/net/bluetooth/hci_sync.c
>> @@ -3167,7 +3167,15 @@ int hci_update_scan(struct hci_dev *hdev)
>>
>>  static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
>>  {
>> -       return hci_update_passive_scan_sync(hdev);
>> +       int ret;
>> +
>> +       hci_dev_lock(hdev);
>> +
>> +       ret = hci_update_passive_scan_sync(hdev);
>> +
>> +       hci_dev_unlock(hdev);
>> +
>> +       return ret;
> 
> This deadlocks since it is a callback on hci_cmd_sync_work which shall
> never use hci_dev_lock while waiting for events processed by
> hci_event_packet which will attempt to do:
> 
>     hci_dev_lock(hdev);
>     kfree_skb(hdev->recv_event);
>     hdev->recv_event = skb_clone(skb, GFP_KERNEL);
>     hci_dev_unlock(hdev);
> 
> This is why there are no test results because all tests seem to be failing now:

Thanks,

ahhhh, may be more valid solution will be to add spinlock to 'struct discovery_state', which protects
operations on this structure at least in 'hci_discovery_filter_clear()' ? 

Thanks, Arseniy

> 
> https://github.com/BluezTestBot/bluetooth-next/pull/2884
> 
>>  }
>>
>>  int hci_update_passive_scan(struct hci_dev *hdev)
>> --
>> 2.30.1
>>
> 
>