Thanks to visit codestin.com
Credit goes to github.com

Skip to content

esp32: Fix heap corruption triggered by bluetooth.active(0). #15538

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

Conversation

projectgus
Copy link
Contributor

@projectgus projectgus commented Jul 25, 2024

Summary

It seems like at some point Espressif NimBLE team changed nimble_port_init and nimble_port_deinit to manage HCI init internally:
espressif/esp-nimble@f8a79b04c9743543b8959727d7

This change is now included in all the IDF versions that current MicroPython supports.

As a result, existing code that called esp_nimble_hci_deinit() explicitly would trigger a use-after-free bug and heap corruption (specifically this calls through to ble_transport_deinit() which calls os_mempool_free(). The second time this writes out to a bunch of memory pools where the backing buffers have already been freed.)

Symptoms were intermittent random crashes after de-activating Bluetooth. Setting Heap Poisoning to Comprehensive in menuconfig caused the bug to be detected every time.

Found while testing #15523.

Testing

Ran multi_bluetooth tests on ESP32 and ESP32-S3 with and without Comprehensive heap poisoning, and on IDF v5.0.4 and v5.2.2. Failures (crashes) in ble_gatt_data_transfer.py are present without this fix.

It seems like at some point Espressif NimBLE team changed
nimble_port_init and nimble_port_deinit to manage HCI init
internally:
espressif/esp-nimble@f8a79b04c9743543b8959727d7

This change is included in all the IDF versions that MicroPython supports.

As a result, existing code that called esp_nimble_hci_deinit() explicitly
would trigger a use-after-free bug and heap corruption (specifically this
calls through to ble_transport_deinit() which calls os_mempool_free(). The
second time this writes out to a bunch of memory pools where the backing
buffers have already been freed.)

Symptoms were intermittent random crashes after de-activating Bluetooth
(running multi_bluetooth/ble_gatt_data_transfer.py could sometimes
reproduce). Setting Heap Poisoning to Comprehensive in menuconfig caused
the bug to be detected every time.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <[email protected]>
@dpgeorge
Copy link
Member

dpgeorge commented Aug 1, 2024

Thanks, this looks good. I did a quick test with an ESP32 (against PYBD-SF6) with IDF v5.0.5. Current master does indeed crash on ble_gatt_data_transfer.py, and is fixed with this PR.

@dpgeorge dpgeorge force-pushed the bugfix/esp32_bluetooth_use_after_free branch from dbd587f to 1754c58 Compare August 1, 2024 01:04
@dpgeorge dpgeorge merged commit 1754c58 into micropython:master Aug 1, 2024
8 checks passed
@projectgus projectgus deleted the bugfix/esp32_bluetooth_use_after_free branch August 1, 2024 06:04
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.

2 participants