Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bluetooth: Host: Fix not clearing IDs and keys upon bt_disable() #72680

Merged

Conversation

rugeGerritsen
Copy link
Collaborator

@rugeGerritsen rugeGerritsen commented May 13, 2024

Bluetooth: Host: Fix not clearing IDs and keys upon bt_disable()

Expectation: After calling `bt_disable()` it is possible to
use the Bluetooth APIs as if `bt_enable()` was never called.

This was not the case for `bt_id_create()`, it was not possible
to set the default identity. This prevented an application
developer to restart the stack as a different identity.

Keys also need to be cleared to avoid the following pattern:
1. Pair two devices
2. Central calls `bt_disable()` and `bt_enable()`.
   The central will now generate a new identity address.
3. Connect the two devices.
4. Re-establish encryption. Now the central will try to use
   the previously used keys. The procedure will fail
   because the peripheral does not have any keys associated
   with the new central address.

The API documentation is updated accordingly.

include/zephyr/bluetooth/bluetooth.h Outdated Show resolved Hide resolved
jori-nordic
jori-nordic previously approved these changes May 13, 2024
Thalley
Thalley previously approved these changes May 13, 2024
@@ -35,6 +35,55 @@ static void test_disable_main(void)
PASS("Disable test passed\n");
}

static void test_disable_set_default_id(void)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{
{
/* Provide some idle time for boot sequence to complete.
* Otherwise, it is observed that nRF POWER_CLOCK ISR does not get serviced.
*/
k_sleep(K_MSEC(1));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aescolar any comments, why some test case work without this hack?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. I would need to dig into why that is happening, but I don't have time now. It would be nice if you or Emil did.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Thalley you are facing controller assertions, is similar workaround needed for your tests too?

Copy link
Collaborator

@Thalley Thalley May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I haven't seen it for the bt_disable tests I'm implementing right now (#72690)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you already have couple k_sleep() calls that would allow ISRs to be vectored into.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a temporary workaround. #73342 fixes the bug in the controller

@rugeGerritsen rugeGerritsen force-pushed the fix-bt-disable-id-count branch 2 times, most recently from 70c6ad8 to c11681c Compare May 27, 2024 12:18
Comment on lines 90 to 93
/* BSIM test framework requires at to run some time before marking a
* test case as passed.
*/
k_sleep(K_MSEC(1));
Copy link
Member

@aescolar aescolar May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Thalley it is because of this: 919d27b#r142428001

@rugeGerritsen
Copy link
Collaborator Author

The new CIS disable test is failing because bt_disable() doesn't clear the stored keys. As a result, the central uses the previously stored keys when re-establishing security. As the device uses a new identity, this fails. We need to figure out if we want keys to be cleared as well.

@Thalley
Copy link
Collaborator

Thalley commented May 27, 2024

The new CIS disable test is failing because bt_disable() doesn't clear the stored keys. As a result, the central uses the previously stored keys when re-establishing security. As the device uses a new identity, this fails. We need to figure out if we want keys to be cleared as well.

If we are clearing stuff like IRKs etc. (which I believe we are/should when doing an HCI reset), then we should clear all the other keys as well, unless they are store in BT_SETTINGS.

Comment on lines 42 to 45
/* FIXME: Temporary workaround to get around a bug in the controller
* Open PR: https://github.com/zephyrproject-rtos/zephyr/pull/73342
*/
k_sleep(K_MSEC(1));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Describe the symptom of the bug so it can be reproducible. As I see, it is the POWER_CLOCK_IRQn that is not executed if a test is developed without any cpu_idle() call.
  2. The workaround to sleep is only needed once, not needed in a loop. See the original suggestion here: https://github.com/zephyrproject-rtos/zephyr/pull/72680/files#r1599770565

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the description. Hopefully it can be removed soon. The linked PR should give enough information

jori-nordic
jori-nordic previously approved these changes May 28, 2024
@rugeGerritsen rugeGerritsen changed the title Bluetooth: Host: Fix not clearing IDs upon bt_disable() Bluetooth: Host: Fix not clearing IDs and keys upon bt_disable() May 28, 2024
jori-nordic
jori-nordic previously approved these changes May 28, 2024
Thalley
Thalley previously approved these changes May 28, 2024
Comment on lines +4239 to +4240
bt_dev.le.rl_entries = 0;
bt_keys_reset();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should bt_dev.le.rl_entries = 0; have been part of bt_keys_reset?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or should we have a bt_id_reset that does this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I will leave that up to you and @jori-nordic

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rugeGerritsen let's merge as-is and follow-up with another one if necessary. id.c is "here be dragons" territory.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Expectation: After calling `bt_disable()` it is possible to
use the Bluetooth APIs as if `bt_enable()` was never called.

This was not the case for `bt_id_create()`, it was not possible
to set the default identity. This prevented an application
developer to restart the stack as a different identity.

Keys also need to be cleared to avoid the following pattern:
1. Pair two devices
2. Central calls `bt_disable()` and `bt_enable()`.
   The central will now generate a new identity address.
3. Connect the two devices.
4. Re-establish encryption. Now the central will try to use
   the previously used keys. The procedure will fail
   because the peripheral does not have any keys associated
   with the new central address.

The API documentation is updated accordingly.

Signed-off-by: Rubin Gerritsen <rubin.gerritsen@nordicsemi.no>
@rugeGerritsen
Copy link
Collaborator Author

Sorry for the last pushes. I forgot to include the change in the prj.conf which I had locally

@cvinayak cvinayak dismissed their stale review May 28, 2024 10:03

Requests addressed.

@carlescufi carlescufi merged commit 3ce106c into zephyrproject-rtos:main May 28, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants