From 84f7125d1bf73aca0f23d0a8d790695660954671 Mon Sep 17 00:00:00 2001 From: Rahul Tank Date: Sun, 26 Jul 2026 17:53:37 +0530 Subject: [PATCH] fix(nimble): Fix for BLERP attack --- components/bt/host/nimble/Kconfig.in | 66 +++++++++++++++++++ components/bt/host/nimble/nimble | 2 +- .../host/nimble/port/include/esp_nimble_cfg.h | 45 +++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) diff --git a/components/bt/host/nimble/Kconfig.in b/components/bt/host/nimble/Kconfig.in index c7e3b617a6d..7b3cd0b7ebe 100644 --- a/components/bt/host/nimble/Kconfig.in +++ b/components/bt/host/nimble/Kconfig.in @@ -202,6 +202,72 @@ menu "Security (SMP)" Enabling this option will delete the pairing of the device and stack will NOT post any event to application. If this option is disabled, application will get BLE_GAP_EVENT_REPEAT_PAIRING event. + + config BT_NIMBLE_SMP_HARDENED_REPAIRING + bool "Reject re-pairing that weakens an existing bond" + default y + help + When a bond already exists, refuse a new pairing that would lower MITM + protection, drop Secure Connections, or shorten the encryption key. + Also enforce that a Pairing Response keeps the MITM/SC bits announced + in a preceding Security Request. + + Same-level re-pairing (peer deleted its own bond and pairs again at the + same strength) is still allowed. A peer that must legitimately re-pair at + a lower level has to be unpaired first (ble_gap_unpair / NVS clear). + + Matches Bluedroid BT_BLE_SMP_HARDENED_REPAIRING. Disable only if a peer + must downgrade and the application cannot unbond it beforehand. + + config BT_NIMBLE_SMP_REQUIRE_ENC_BEFORE_REPAIR + bool "Require encryption before re-pairing with a bonded peer" + default n + help + When enabled, a bonded peer cannot start SMP pairing on an unencrypted + link. The stack replies with Pairing Failed and disconnects. This is + stricter than the Bluetooth Core Spec and stricter than Bluedroid; it + blocks same-level recovery when the peer deleted its bond and sends a + Pairing Request before encryption. + + Default is disabled so existing apps and one-sided bond-deletion + recovery keep working. Enable for maximum hardening against + unauthenticated re-pairing (BLERP V1/V2 style). + + config BT_NIMBLE_SMP_REMOVE_BOND_ON_PAIR_FAIL_AS_CENTRAL + bool "Remove the stored bond when pairing fails as Central" + default y + help + Erase NVS bonding keys when pairing or encryption fails while the local + device is Central (link-layer master). Historical Central behaviour and + the default: recovers when the peer deleted the bond and answers + encryption with Key Missing. + + A downgrade refused by BT_NIMBLE_SMP_HARDENED_REPAIRING never erases + the bond, regardless of this option. + + config BT_NIMBLE_SMP_REMOVE_BOND_ON_PAIR_FAIL_AS_PRPH + bool "Remove the stored bond when pairing fails as Peripheral" + default n + help + Erase NVS bonding keys when pairing or encryption fails while the local + device is Peripheral (link-layer slave). Default is disabled so a failed + pairing does not drop the bond; unbond explicitly from the app if needed. + + A downgrade refused by BT_NIMBLE_SMP_HARDENED_REPAIRING never erases + the bond, regardless of this option. + + config BT_NIMBLE_SMP_UNBOND_ON_KEY_MISSING + bool "Drop the local LE keys when the peer reports it has no key" + default n + help + As Central, an encryption attempt using the stored LTK can fail with + "PIN or Key Missing" when the peer deleted the bond. When enabled, the + local keys are discarded so re-pairing can proceed on the same link. + + Default is disabled: the bond is kept and the link is dropped, so a peer + cannot strip the stored security level by refusing to encrypt. With the + default, BT_NIMBLE_SMP_REMOVE_BOND_ON_PAIR_FAIL_AS_CENTRAL still recovers + Central-side Key Missing by erasing on the failure path. endif endmenu #SMP diff --git a/components/bt/host/nimble/nimble b/components/bt/host/nimble/nimble index 7f05453dbf1..1f31b4d2858 160000 --- a/components/bt/host/nimble/nimble +++ b/components/bt/host/nimble/nimble @@ -1 +1 @@ -Subproject commit 7f05453dbf1bfd46b46a93262741661972b0a16d +Subproject commit 1f31b4d28580dda4c15e9fe0c3ada812fc0f7b7a diff --git a/components/bt/host/nimble/port/include/esp_nimble_cfg.h b/components/bt/host/nimble/port/include/esp_nimble_cfg.h index 950a2d79fbe..958dbcb8058 100644 --- a/components/bt/host/nimble/port/include/esp_nimble_cfg.h +++ b/components/bt/host/nimble/port/include/esp_nimble_cfg.h @@ -2307,6 +2307,51 @@ #endif #endif +/* Bool Kconfig: defined=1 when y, absent when n. Non-ESP builds keep syscfg defaults. */ +#ifndef MYNEWT_VAL_BLE_SMP_HARDENED_REPAIRING +#if defined(CONFIG_BT_NIMBLE_SMP_HARDENED_REPAIRING) +#define MYNEWT_VAL_BLE_SMP_HARDENED_REPAIRING (1) +#elif defined(CONFIG_BT_NIMBLE_ENABLED) +#define MYNEWT_VAL_BLE_SMP_HARDENED_REPAIRING (0) +#else +#define MYNEWT_VAL_BLE_SMP_HARDENED_REPAIRING (1) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_SMP_REQUIRE_ENC_BEFORE_REPAIR +#if defined(CONFIG_BT_NIMBLE_SMP_REQUIRE_ENC_BEFORE_REPAIR) +#define MYNEWT_VAL_BLE_SMP_REQUIRE_ENC_BEFORE_REPAIR (1) +#else +#define MYNEWT_VAL_BLE_SMP_REQUIRE_ENC_BEFORE_REPAIR (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_SMP_REMOVE_BOND_ON_PAIR_FAIL_AS_CENTRAL +#if defined(CONFIG_BT_NIMBLE_SMP_REMOVE_BOND_ON_PAIR_FAIL_AS_CENTRAL) +#define MYNEWT_VAL_BLE_SMP_REMOVE_BOND_ON_PAIR_FAIL_AS_CENTRAL (1) +#elif defined(CONFIG_BT_NIMBLE_ENABLED) +#define MYNEWT_VAL_BLE_SMP_REMOVE_BOND_ON_PAIR_FAIL_AS_CENTRAL (0) +#else +#define MYNEWT_VAL_BLE_SMP_REMOVE_BOND_ON_PAIR_FAIL_AS_CENTRAL (1) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_SMP_REMOVE_BOND_ON_PAIR_FAIL_AS_PERIPHERAL +#if defined(CONFIG_BT_NIMBLE_SMP_REMOVE_BOND_ON_PAIR_FAIL_AS_PRPH) +#define MYNEWT_VAL_BLE_SMP_REMOVE_BOND_ON_PAIR_FAIL_AS_PERIPHERAL (1) +#else +#define MYNEWT_VAL_BLE_SMP_REMOVE_BOND_ON_PAIR_FAIL_AS_PERIPHERAL (0) +#endif +#endif + +#ifndef MYNEWT_VAL_BLE_SMP_UNBOND_ON_KEY_MISSING +#if defined(CONFIG_BT_NIMBLE_SMP_UNBOND_ON_KEY_MISSING) +#define MYNEWT_VAL_BLE_SMP_UNBOND_ON_KEY_MISSING (1) +#else +#define MYNEWT_VAL_BLE_SMP_UNBOND_ON_KEY_MISSING (0) +#endif +#endif + #ifndef MYNEWT_VAL_BT_NIMBLE_MEM_OPTIMIZATION #ifdef CONFIG_BT_NIMBLE_MEM_OPTIMIZATION #define MYNEWT_VAL_BT_NIMBLE_MEM_OPTIMIZATION CONFIG_BT_NIMBLE_MEM_OPTIMIZATION