Skip to contentSkip to conversation

Is commit 4d94f0555827 safe?

DISCUSSION Takashi Iwai 3 participants 10 messages Mar 2, 2025 – Mar 4, 2025 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

[ I posted without the subject line mistakenly, so resent again;
sorry if you have seen already read it ]

Hi Luiz,

due to the CVE assignment, I stumbled on the recent fix for BT hci_core, the commit 4d94f0555827 ("Bluetooth: hci_core: Fix sleeping function called from invalid context"), and wonder whether it's really safe.

As already asked question at the patch review:
https://patchwork.kernel.org/comment/26147087/
the code allows the callbacks to be called even after hci_unregister_cb() returns.

Your assumption was that it's never called without the module removal, but isn't hci_unregister_cb() also called from iso_exit() which can be triggered via set_iso_socket_func() in mgmt.c? Also, any 3rd party module could call hci_unregister_cb() in a wild way, too -- even if the function still remains, it doesn't mean that you can call it safely if the caller already assumes it being unregistered.

In addition to that, I feel what the patch does as a bit too
heavy-lifting: it does kmalloc() and copy the whole hci_cb object, which isn't quite small for each. If the callback is still safe to call after RCU protection, you may just keep the hci_cb pointer instead of copying the whole content, too?

I couldn't find v1 patch in the patchwork, so not sure whether this has been already discussed. If so, let me know.

Thanks!

Takashi

#1 lore raw

Hi Takashi,

Well the assumption was that because we are doing a copy of the struct being unregistered/freed would never cause any errors, so to trigger something like UAF like the comment was suggesting the function callback would need to be unmapped so even if the likes of iso_exit is called it function (e.g. iso_connect_cfm) remains in memory.

You can find the previous version here:

https://syzkaller.appspot.com/text?tag=Patch&x=100c0de8580000

Problem with it was that it is invalid to unlock and relock like that.

Quoted 37 lines · Takashi Iwai

[ I posted without the subject line mistakenly, so resent again;
sorry if you have seen already read it ]

Hi Luiz,

due to the CVE assignment, I stumbled on the recent fix for BT hci_core, the commit 4d94f0555827 ("Bluetooth: hci_core: Fix sleeping function called from invalid context"), and wonder whether it's really safe.

As already asked question at the patch review:
https://patchwork.kernel.org/comment/26147087/
the code allows the callbacks to be called even after hci_unregister_cb() returns.

Your assumption was that it's never called without the module removal, but isn't hci_unregister_cb() also called from iso_exit() which can be triggered via set_iso_socket_func() in mgmt.c? Also, any 3rd party module could call hci_unregister_cb() in a wild way, too -- even if the function still remains, it doesn't mean that you can call it safely if the caller already assumes it being unregistered.

In addition to that, I feel what the patch does as a bit too
heavy-lifting: it does kmalloc() and copy the whole hci_cb object, which isn't quite small for each. If the callback is still safe to call after RCU protection, you may just keep the hci_cb pointer instead of copying the whole content, too?

I couldn't find v1 patch in the patchwork, so not sure whether this has been already discussed. If so, let me know.

Thanks!

Takashi

Signature · 1 line
Luiz Augusto von Dentz
#2 lore raw
Quoted 8 lines · Luiz Augusto von Dentz

Hi Takashi,

Well the assumption was that because we are doing a copy of the struct being unregistered/freed would never cause any errors, so to trigger something like UAF like the comment was suggesting the function callback would need to be unmapped so even if the likes of iso_exit is called it function (e.g. iso_connect_cfm) remains in memory.

But it doesn't guarantee that the callback function would really work. e.g. if the callback accesses some memory that was immediately freed after the unregister call, it will lead to a UAF, even though the function itself is still present on the memory.

That said, the current situation makes hard to judge the object life time.

Quoted 5 lines

You can find the previous version here:

https://syzkaller.appspot.com/text?tag=Patch&x=100c0de8580000

Problem with it was that it is invalid to unlock and relock like that.

Thanks for the pointer!

BTW, I saw another patch posted to replace the mutex with spinlock (and you replied later on that it's been already fixed). Is it an acceptable approach at all?

Takashi

Quoted 43 lines · Takashi Iwai
Quoted 37 lines

[ I posted without the subject line mistakenly, so resent again;
sorry if you have seen already read it ]

Hi Luiz,

due to the CVE assignment, I stumbled on the recent fix for BT hci_core, the commit 4d94f0555827 ("Bluetooth: hci_core: Fix sleeping function called from invalid context"), and wonder whether it's really safe.

As already asked question at the patch review:
https://patchwork.kernel.org/comment/26147087/
the code allows the callbacks to be called even after hci_unregister_cb() returns.

Your assumption was that it's never called without the module removal, but isn't hci_unregister_cb() also called from iso_exit() which can be triggered via set_iso_socket_func() in mgmt.c? Also, any 3rd party module could call hci_unregister_cb() in a wild way, too -- even if the function still remains, it doesn't mean that you can call it safely if the caller already assumes it being unregistered.

In addition to that, I feel what the patch does as a bit too
heavy-lifting: it does kmalloc() and copy the whole hci_cb object, which isn't quite small for each. If the callback is still safe to call after RCU protection, you may just keep the hci_cb pointer instead of copying the whole content, too?

I couldn't find v1 patch in the patchwork, so not sure whether this has been already discussed. If so, let me know.

Thanks!

Takashi

Signature · 1 line
Luiz Augusto von Dentz
#3 lore raw

Hi Takashi,

Quoted 24 lines · Takashi Iwai
Quoted 8 lines · Luiz Augusto von Dentz

Hi Takashi,

Well the assumption was that because we are doing a copy of the struct being unregistered/freed would never cause any errors, so to trigger something like UAF like the comment was suggesting the function callback would need to be unmapped so even if the likes of iso_exit is called it function (e.g. iso_connect_cfm) remains in memory.

But it doesn't guarantee that the callback function would really work. e.g. if the callback accesses some memory that was immediately freed after the unregister call, it will lead to a UAF, even though the function itself is still present on the memory.

That said, the current situation makes hard to judge the object life time.

Quoted 4 lines

You can find the previous version here:

https://syzkaller.appspot.com/text?tag=Patch&x=100c0de8580000

Quoted 8 lines
Quoted 1 line

Problem with it was that it is invalid to unlock and relock like that.

Thanks for the pointer!

BTW, I saw another patch posted to replace the mutex with spinlock (and you replied later on that it's been already fixed). Is it an acceptable approach at all?

I don't remember if I saw that, but yeah anything that makes the issue go away, and doesn't create new problems, would probably be acceptable.

#4 lore raw
Quoted 33 lines · Luiz Augusto von Dentz

Hi Takashi,

Quoted 29 lines · Takashi Iwai
Quoted 8 lines · Luiz Augusto von Dentz

Hi Takashi,

Well the assumption was that because we are doing a copy of the struct being unregistered/freed would never cause any errors, so to trigger something like UAF like the comment was suggesting the function callback would need to be unmapped so even if the likes of iso_exit is called it function (e.g. iso_connect_cfm) remains in memory.

But it doesn't guarantee that the callback function would really work. e.g. if the callback accesses some memory that was immediately freed after the unregister call, it will lead to a UAF, even though the function itself is still present on the memory.

That said, the current situation makes hard to judge the object life time.

Quoted 5 lines

You can find the previous version here:

https://syzkaller.appspot.com/text?tag=Patch&x=100c0de8580000

Problem with it was that it is invalid to unlock and relock like that.

Thanks for the pointer!

Quoted 7 lines
Quoted 3 lines

BTW, I saw another patch posted to replace the mutex with spinlock (and you replied later on that it's been already fixed). Is it an acceptable approach at all?

I don't remember if I saw that, but yeah anything that makes the issue go away, and doesn't create new problems, would probably be acceptable.

I saw this one:
https://lore.kernel.org/all/[email protected]/

Takashi

#5 lore raw

Hi,

Quoted 40 lines · Takashi Iwai
Quoted 37 lines · Luiz Augusto von Dentz

Hi Takashi,

Quoted 32 lines · Takashi Iwai
Quoted 8 lines · Luiz Augusto von Dentz

Hi Takashi,

Well the assumption was that because we are doing a copy of the struct being unregistered/freed would never cause any errors, so to trigger something like UAF like the comment was suggesting the function callback would need to be unmapped so even if the likes of iso_exit is called it function (e.g. iso_connect_cfm) remains in memory.

But it doesn't guarantee that the callback function would really work. e.g. if the callback accesses some memory that was immediately freed after the unregister call, it will lead to a UAF, even though the function itself is still present on the memory.

That said, the current situation makes hard to judge the object life time.

Quoted 5 lines

You can find the previous version here:

https://syzkaller.appspot.com/text?tag=Patch&x=100c0de8580000

Problem with it was that it is invalid to unlock and relock like that.

Thanks for the pointer!

BTW, I saw another patch posted to replace the mutex with spinlock (and you replied later on that it's been already fixed). Is it an acceptable approach at all?

Quoted 6 lines
Quoted 3 lines

I don't remember if I saw that, but yeah anything that makes the issue go away, and doesn't create new problems, would probably be acceptable.

I saw this one:
https://lore.kernel.org/all/[email protected]/

Ive might have missed it, we will probably need to rebase it but other than that it should be acceptable.

Signature · 1 line
Luiz Augusto von Dentz
#6 lore raw
Quoted 48 lines · Luiz Augusto von Dentz

Hi,

Quoted 44 lines · Takashi Iwai
Quoted 40 lines · Luiz Augusto von Dentz

Hi Takashi,

Quoted 24 lines · Takashi Iwai
Quoted 8 lines · Luiz Augusto von Dentz

Hi Takashi,

Well the assumption was that because we are doing a copy of the struct being unregistered/freed would never cause any errors, so to trigger something like UAF like the comment was suggesting the function callback would need to be unmapped so even if the likes of iso_exit is called it function (e.g. iso_connect_cfm) remains in memory.

But it doesn't guarantee that the callback function would really work. e.g. if the callback accesses some memory that was immediately freed after the unregister call, it will lead to a UAF, even though the function itself is still present on the memory.

That said, the current situation makes hard to judge the object life time.

Quoted 4 lines

You can find the previous version here:

https://syzkaller.appspot.com/text?tag=Patch&x=100c0de8580000

Quoted 8 lines
Quoted 1 line

Problem with it was that it is invalid to unlock and relock like that.

Thanks for the pointer!

BTW, I saw another patch posted to replace the mutex with spinlock (and you replied later on that it's been already fixed). Is it an acceptable approach at all?

I don't remember if I saw that, but yeah anything that makes the issue go away, and doesn't create new problems, would probably be acceptable.

Quoted 5 lines
Quoted 2 lines

I saw this one:
https://lore.kernel.org/all/[email protected]/

Ive might have missed it, we will probably need to rebase it but other than that it should be acceptable.

Does it mean that you'd revert the change and apply the above one (with rebase or modification)? Or would you keep a part of the current change (e.g. match callback looks neat) while applying the similar fix using the spinlock?

thanks,

Takashi

#7 lore raw

Hi,

ma, 2025-03-03 kello 17:38 +0100, Takashi Iwai kirjoitti:

Quoted 53 lines
Quoted 51 lines · Luiz Augusto von Dentz

Hi,

Quoted 46 lines · Takashi Iwai
Quoted 33 lines · Luiz Augusto von Dentz

Hi Takashi,

Quoted 29 lines · Takashi Iwai
Quoted 8 lines · Luiz Augusto von Dentz

Hi Takashi,

Well the assumption was that because we are doing a copy of the struct being unregistered/freed would never cause any errors, so to trigger something like UAF like the comment was suggesting the function callback would need to be unmapped so even if the likes of iso_exit is called it function (e.g. iso_connect_cfm) remains in memory.

But it doesn't guarantee that the callback function would really work. e.g. if the callback accesses some memory that was immediately freed after the unregister call, it will lead to a UAF, even though the function itself is still present on the memory.

That said, the current situation makes hard to judge the object life time.

Quoted 5 lines

You can find the previous version here:

https://syzkaller.appspot.com/text?tag=Patch&x=100c0de8580000

Problem with it was that it is invalid to unlock and relock like that.

Thanks for the pointer!

Quoted 7 lines
Quoted 3 lines

BTW, I saw another patch posted to replace the mutex with spinlock (and you replied later on that it's been already fixed). Is it an acceptable approach at all?

I don't remember if I saw that, but yeah anything that makes the issue go away, and doesn't create new problems, would probably be acceptable.

I saw this one:
https://lore.kernel.org/all/[email protected]/

Quoted 7 lines
Quoted 2 lines

Ive might have missed it, we will probably need to rebase it but other than that it should be acceptable.

Does it mean that you'd revert the change and apply the above one (with rebase or modification)? Or would you keep a part of the current change (e.g. match callback looks neat) while applying the similar fix using the spinlock?

My current understanding of this is that the actual problem for 4d94f0555827 was incorrect RCU use at the callsite in hci_le_create_big_complete_evt(). That part was rewritten in

commit 581dd2dc168f ("Bluetooth: hci_event: Fix using rcu_read_(un)lock while iterating")

and now it no longer calls hci_connect_cfm() from atomic context. I suspect there were no other callsites that needed hci callbacks be rcu- safe, so the original mutex is maybe OK as well.

For the other patch

https://lore.kernel.org/all/[email protected]/

The current code is doing rcu_read_unlock() in list_for_each_entry_rcu, so it's not quite correct. This could be reorganized to restart the loop after unlock and skip if (conn->abort_reason), which may be preferable to spinlock in rcu critical section.

Signature · 1 line
Pauli Virtanen
#8 lore raw

Hi Pauli, Takashi,

Quoted 65 lines · Pauli Virtanen

Hi,

ma, 2025-03-03 kello 17:38 +0100, Takashi Iwai kirjoitti:

Quoted 53 lines
Quoted 51 lines · Luiz Augusto von Dentz

Hi,

Quoted 46 lines · Takashi Iwai
Quoted 33 lines · Luiz Augusto von Dentz

Hi Takashi,

Quoted 29 lines · Takashi Iwai
Quoted 8 lines · Luiz Augusto von Dentz

Hi Takashi,

Well the assumption was that because we are doing a copy of the struct being unregistered/freed would never cause any errors, so to trigger something like UAF like the comment was suggesting the function callback would need to be unmapped so even if the likes of iso_exit is called it function (e.g. iso_connect_cfm) remains in memory.

But it doesn't guarantee that the callback function would really work. e.g. if the callback accesses some memory that was immediately freed after the unregister call, it will lead to a UAF, even though the function itself is still present on the memory.

That said, the current situation makes hard to judge the object life time.

Quoted 5 lines

You can find the previous version here:

https://syzkaller.appspot.com/text?tag=Patch&x=100c0de8580000

Problem with it was that it is invalid to unlock and relock like that.

Thanks for the pointer!

Quoted 7 lines
Quoted 3 lines

BTW, I saw another patch posted to replace the mutex with spinlock (and you replied later on that it's been already fixed). Is it an acceptable approach at all?

I don't remember if I saw that, but yeah anything that makes the issue go away, and doesn't create new problems, would probably be acceptable.

I saw this one:
https://lore.kernel.org/all/[email protected]/

Quoted 7 lines
Quoted 2 lines

Ive might have missed it, we will probably need to rebase it but other than that it should be acceptable.

Does it mean that you'd revert the change and apply the above one (with rebase or modification)? Or would you keep a part of the current change (e.g. match callback looks neat) while applying the similar fix using the spinlock?

Quoted 6 lines

My current understanding of this is that the actual problem for 4d94f0555827 was incorrect RCU use at the callsite in hci_le_create_big_complete_evt(). That part was rewritten in

commit 581dd2dc168f ("Bluetooth: hci_event: Fix using rcu_read_(un)lock while iterating")

In that case maybe we can just revert the 4d94f0555827 ("Bluetooth:
hci_core: Fix sleeping
function called from invalid context") and see if that works, might need to trigger syzbot just to confirm we don't introduce the original problem.

Quoted 4 lines

and now it no longer calls hci_connect_cfm() from atomic context. I suspect there were no other callsites that needed hci callbacks be rcu- safe, so the original mutex is maybe OK as well.

Quoted 8 lines

For the other patch

https://lore.kernel.org/all/[email protected]/

The current code is doing rcu_read_unlock() in list_for_each_entry_rcu, so it's not quite correct. This could be reorganized to restart the loop after unlock and skip if (conn->abort_reason), which may be preferable to spinlock in rcu critical section.

I guess you referring to hci_cmd_sync_clear, yeah that is doing unlock/lock in the loop which is incorrect, that said we don't really need this change if for the above issue, although we need to re-evaluate the use of mutex vs spin locks given that apparently there could be issues other than just the BIG code hitting this sort of problem.

Quoted 2 lines

--
Pauli Virtanen

Signature · 1 line
Luiz Augusto von Dentz
#9 lore raw
Quoted 73 lines · Luiz Augusto von Dentz

Hi Pauli, Takashi,

Quoted 69 lines · Pauli Virtanen

Hi,

ma, 2025-03-03 kello 17:38 +0100, Takashi Iwai kirjoitti:

Quoted 53 lines
Quoted 51 lines · Luiz Augusto von Dentz

Hi,

Quoted 46 lines · Takashi Iwai
Quoted 33 lines · Luiz Augusto von Dentz

Hi Takashi,

Quoted 29 lines · Takashi Iwai
Quoted 8 lines · Luiz Augusto von Dentz

Hi Takashi,

Well the assumption was that because we are doing a copy of the struct being unregistered/freed would never cause any errors, so to trigger something like UAF like the comment was suggesting the function callback would need to be unmapped so even if the likes of iso_exit is called it function (e.g. iso_connect_cfm) remains in memory.

But it doesn't guarantee that the callback function would really work. e.g. if the callback accesses some memory that was immediately freed after the unregister call, it will lead to a UAF, even though the function itself is still present on the memory.

That said, the current situation makes hard to judge the object life time.

Quoted 5 lines

You can find the previous version here:

https://syzkaller.appspot.com/text?tag=Patch&x=100c0de8580000

Problem with it was that it is invalid to unlock and relock like that.

Thanks for the pointer!

Quoted 7 lines
Quoted 3 lines

BTW, I saw another patch posted to replace the mutex with spinlock (and you replied later on that it's been already fixed). Is it an acceptable approach at all?

I don't remember if I saw that, but yeah anything that makes the issue go away, and doesn't create new problems, would probably be acceptable.

I saw this one:
https://lore.kernel.org/all/[email protected]/

Quoted 7 lines
Quoted 2 lines

Ive might have missed it, we will probably need to rebase it but other than that it should be acceptable.

Does it mean that you'd revert the change and apply the above one (with rebase or modification)? Or would you keep a part of the current change (e.g. match callback looks neat) while applying the similar fix using the spinlock?

My current understanding of this is that the actual problem for 4d94f0555827 was incorrect RCU use at the callsite in hci_le_create_big_complete_evt(). That part was rewritten in

Quoted 8 lines
Quoted 2 lines

commit 581dd2dc168f ("Bluetooth: hci_event: Fix using rcu_read_(un)lock while iterating")

In that case maybe we can just revert the 4d94f0555827 ("Bluetooth:
hci_core: Fix sleeping
function called from invalid context") and see if that works, might need to trigger syzbot just to confirm we don't introduce the original problem.

Fair enough, it sounds reasonable.

thanks,

Takashi