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: Add missing buffer length check #74639

Merged
merged 1 commit into from
Jun 25, 2024

Conversation

ekleezg
Copy link
Contributor

@ekleezg ekleezg commented Jun 21, 2024

Modified to check the length of the remaining data in buffer before processing the next report. The length check is missing in the cont routine.

jhedberg
jhedberg previously approved these changes Jun 21, 2024
subsys/bluetooth/host/scan.c Outdated Show resolved Hide resolved
@jhedberg jhedberg dismissed their stale review June 22, 2024 05:44

Waiting for valid discovered issue to be resolved.

@ekleezg ekleezg force-pushed the bt_host_length_check branch from 119b820 to 22c2261 Compare June 22, 2024 07:25
@ekleezg ekleezg force-pushed the bt_host_length_check branch from 9c03965 to 081a7cb Compare June 22, 2024 07:34
Thalley
Thalley previously approved these changes Jun 22, 2024
subsys/bluetooth/host/scan.c Outdated Show resolved Hide resolved
Modified to check the length of the remaining data in buffer
before processing the next report. The length check is missing
in the cont routine.

Signed-off-by: Eunkyu Lee <mochaccino.00.00@gmail.com>
Copy link
Collaborator

@Thalley Thalley left a comment

Choose a reason for hiding this comment

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

Minor suggestions, but is also OK as is

Comment on lines +628 to +638
if (evt_type & BT_HCI_LE_ADV_EVT_TYPE_LEGACY) {
return;
}

/* Start discarding irrespective of the `more_to_come` flag. We
* assume we may have lost a partial adv report in the truncated
* data.
*/
reassembling_advertiser.state = FRAG_ADV_DISCARDING;

return;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (evt_type & BT_HCI_LE_ADV_EVT_TYPE_LEGACY) {
return;
}
/* Start discarding irrespective of the `more_to_come` flag. We
* assume we may have lost a partial adv report in the truncated
* data.
*/
reassembling_advertiser.state = FRAG_ADV_DISCARDING;
return;
if ((evt_type & BT_HCI_LE_ADV_EVT_TYPE_LEGACY) == 0) {
/* Start discarding irrespective of the `more_to_come` flag. We
* assume we may have lost a partial adv report in the truncated
* data.
*/
reassembling_advertiser.state = FRAG_ADV_DISCARDING;
}
return;

This would reduce the number of return statements to 1

@Thalley
Copy link
Collaborator

Thalley commented Jun 23, 2024

@jhedberg should this get merged for 3.7?

@jhedberg jhedberg added this to the v3.7.0 milestone Jun 24, 2024
Comment on lines +628 to +636
if (evt_type & BT_HCI_LE_ADV_EVT_TYPE_LEGACY) {
return;
}

/* Start discarding irrespective of the `more_to_come` flag. We
* assume we may have lost a partial adv report in the truncated
* data.
*/
reassembling_advertiser.state = FRAG_ADV_DISCARDING;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the early return may cause us to produce garbled advertisement reports.

Start discarding irrespective of the more_to_come flag. We assume we may have lost a partial adv report in the truncated data.

The above comment is there because we are in a while-loop over reports. By returning early, we may be skipping over one or more reports. This can lead to a bad reassembly.

A bad reassembly is not a huge problem. The application has to tolerate malicious advertisements anyway. But when we did discussed this last time, we decided to prefer dropping data.

Copy link
Collaborator

@jori-nordic jori-nordic Jun 25, 2024

Choose a reason for hiding this comment

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

can we come up with a test case for this somehow? I agree it's better to drop data instead of making a bad reassembly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

but in this particular case, it's the controller that's malicious, no? so likely not a problem for "normal" use-cases. This early-return is more for protecting the host (and its CPU / resources I guess). My opinion is that we should just shut down bluetooth when that happens. It indicates something is wrong, either HCI driver synchronization or that the controller is very buggy or compromised.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes. This is only relevant for HCI protocol violation. Either the controller did bad, or there was data corruption during HCI transport. It's not an exposed attack surface.

@ceolin
Copy link
Member

ceolin commented Jun 24, 2024

@jhedberg should this get merged for 3.7?

It gets to be merged :)

@nashif nashif merged commit e491f22 into zephyrproject-rtos:main Jun 25, 2024
25 checks passed
LingaoM added a commit to LingaoM/zephyr that referenced this pull request Dec 10, 2024
After zephyrproject-rtos#74639

If we receive corrupted adv report, will set state to discarding,
but if next adv report is new advertiser, will lead ASSERT.

Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants