Skip to content

Commit 1754c58

Browse files
projectgusdpgeorge
authored andcommitted
esp32: Fix heap corruption triggered by bluetooth.active(0).
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]>
1 parent 6f27e1c commit 1754c58

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

Diff for: ports/esp32/mpnimbleport.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
#define DEBUG_printf(...) // printf("nimble (esp32): " __VA_ARGS__)
3434

35-
#include "esp_nimble_hci.h"
3635
#include "nimble/nimble_port.h"
3736
#include "nimble/nimble_port_freertos.h"
3837

@@ -45,14 +44,13 @@ static void ble_host_task(void *param) {
4544
}
4645

4746
void mp_bluetooth_nimble_port_hci_init(void) {
48-
DEBUG_printf("mp_bluetooth_nimble_port_hci_init\n");
49-
esp_nimble_hci_init();
47+
// On ESP-IDF the standard nimble_port_init() function calls
48+
// esp_nimble_init() which initialises the HCI
5049
}
5150

5251
void mp_bluetooth_nimble_port_hci_deinit(void) {
53-
DEBUG_printf("mp_bluetooth_nimble_port_hci_deinit\n");
54-
55-
esp_nimble_hci_deinit();
52+
// As above, this is handled by ESP-IDF nimble_port_deinit()
53+
// (called below)
5654
}
5755

5856
void mp_bluetooth_nimble_port_start(void) {

0 commit comments

Comments
 (0)