Merge remote-tracking branch 'agent/benma-agent/stage0-install-auth'
What changed, and why it matters
This commit hardens the BitBox02 bootloader's stage0 code against fault-injection attacks during firmware updates. It adds a new authorization check that re-verifies the installed firmware image and ensures a glitch cannot trick the device into installing an older, invalid, or attacker-chosen update. The change also prevents the compiler from optimizing away safety checks. Pre-built stage0 bootloader binaries were regenerated, and unit tests were added for the new logic.
Treat this as a security-hardening fix for the bootloader update path. Review whether the new `stage0_install_auth()` logic fully covers downgrade and recovery scenarios, and verify that the regenerated stage0 binaries match the source code and are reproducibly built. Consider whether prior firmware versions need a security advisory if a fault-injection downgrade was practically exploitable before this patch.
Security signals we found
New authorization gate `stage0_install_auth()` before bootloader update installation
Re-verification of installed stage1 image before update to detect fault-induced mismatches
Use of `__attribute__((noinline, noclone))` to protect security-critical functions from LTO optimization
Use of `volatile` on `update_ok` and `install_auth` to prevent compiler assumption of success after fault-bypassed branch
Comment explicitly describes fault-injection threat model: 'A fault can bypass the branch below even when update verification failed'
Unit tests enumerate non-canonical `secbool_u32` values, indicating awareness of faulted return codes
Pre-built stage0 bootloader binaries regenerated, implying this is a shipped fix
Evidence from the diff
The patch introduces stage0_install_auth() in stage0_install.h, which is called from stage0.c before installing a stage1 update. It requires: (1) the update verification succeeded, (2) the installed image’s two verification results match, and (3) either the installed image is valid and the update version is >= installed version, or the installed image is canonically invalid (recovery case). The function and _stage1_image_ok() are marked noinline, noclone to resist LTO-driven optimization that could collapse redundant checks. update_ok and install_auth are made volatile to stop the compiler from assuming success after a fault-bypassed branch. The precompiled stage0 bootloader binaries and their checksums are updated, and unit tests cover version comparisons, recovery behavior, and non-canonical verification results.
Changed components
src/bootloader/stage0/stage0.csrc/bootloader/stage0/stage0_install.htest/unit-test/test_stage0_install.ctest/unit-test/CMakeLists.txtPre-built stage0 bootloader binaries for BitBox02 and BitBox02 Nova (development and production variants)Inspect captured patch +121 / −11
### src/bootloader/stage0/stage0.c
@@ -10,6 +10,7 @@
#include "memory/memory_shared.h"
#include "pac_ext.h"
#include "stage0_flash.h"
+#include "stage0_install.h"
#include "ui/oled/oled.h"
#include "util.h"
#ifndef BB02_STAGE0_DEVELOPMENT
@@ -466,7 +467,11 @@ static secbool_u32 _header_signatures_ok(const bb02_stage1_header_t* header)
}
#endif
-static secbool_u32 _stage1_image_ok(const bb02_stage1_header_t* header, uint8_t hash_out[32])
+// Keep the installation recheck independent of the initial boot decision under LTO.
+// NOLINTNEXTLINE(clang-diagnostic-unknown-attributes)
+static secbool_u32 __attribute__((noinline, noclone)) _stage1_image_ok(
+ const bb02_stage1_header_t* header,
+ uint8_t hash_out[32])
{
if (_header_basic_ok(header) != sectrue_u32) {
return secfalse_u32;
@@ -622,8 +627,11 @@ static void __attribute__((noreturn, noinline)) _stage0_main(void)
const secbool_u32 installed_ok = installed_header_ok == sectrue_u32
? _stage1_image_ok(installed, installed_hash)
: secfalse_u32;
- const secbool_u32 update_ok = _stage1_image_ok(update, update_hash);
- volatile secbool_u32 install_auth = update_ok;
+ // A fault can bypass the branch below even when update verification failed.
+ // Keep the result volatile so the compiler cannot replace the later
+ // stage0_install_auth() check with an assumed success.
+ volatile secbool_u32 update_ok = _stage1_image_ok(update, update_hash);
+ volatile secbool_u32 install_auth = secfalse_u32;
volatile secbool_u32 boot_auth = installed_ok;
if (update_ok == sectrue_u32) {
@@ -639,6 +647,14 @@ static void __attribute__((noreturn, noinline)) _stage0_main(void)
_oled_init();
#endif
_oled_progress(0, 6);
+ // Recheck even a failed result: a fault must not turn a valid image into recovery.
+ const secbool_u32 rechecked_installed_ok = _stage1_image_ok(installed, installed_hash);
+ install_auth = stage0_install_auth(
+ update_ok,
+ installed_ok,
+ rechecked_installed_ok,
+ installed->monotonic_version,
+ update->monotonic_version);
_install_stage1(update, installed_header_ok, &install_auth);
installed = bb02_stage1_installed_header();
if (_stage1_image_ok(installed, installed_hash) == sectrue_u32) {
### src/bootloader/stage0/stage0_install.h
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#ifndef _STAGE0_INSTALL_H_
+#define _STAGE0_INSTALL_H_
+
+#include "util.h"
+
+// Keep this decision independent of the earlier boot-path checks under LTO.
+// NOLINTNEXTLINE(clang-diagnostic-unknown-attributes)
+static secbool_u32 __attribute__((noinline, noclone)) stage0_install_auth(
+ secbool_u32 update_ok,
+ secbool_u32 installed_ok,
+ secbool_u32 rechecked_installed_ok,
+ uint16_t installed_version,
+ uint16_t update_version)
+{
+ if (update_ok != sectrue_u32 || installed_ok != rechecked_installed_ok) {
+ return secfalse_u32;
+ }
+ if (installed_ok == sectrue_u32) {
+ return update_version >= installed_version ? sectrue_u32 : secfalse_u32;
+ }
+ // Recovery is allowed only when both checks returned a canonical failure.
+ return installed_ok == secfalse_u32 ? sectrue_u32 : secfalse_u32;
+}
+
+#endif
### src/bootloader_upgrade/bin/bootloader-stage0-bitbox02-btconly-development.v3.bin
[binary or diff unavailable]
### src/bootloader_upgrade/bin/bootloader-stage0-bitbox02-btconly-production.v3.bin
[binary or diff unavailable]
### src/bootloader_upgrade/bin/bootloader-stage0-bitbox02-multi-development.v3.bin
[binary or diff unavailable]
### src/bootloader_upgrade/bin/bootloader-stage0-bitbox02-multi-production.v3.bin
[binary or diff unavailable]
### src/bootloader_upgrade/bin/bootloader-stage0-bitbox02nova-btconly-development.v3.bin
[binary or diff unavailable]
### src/bootloader_upgrade/bin/bootloader-stage0-bitbox02nova-btconly-production.v3.bin
[binary or diff unavailable]
### src/bootloader_upgrade/bin/bootloader-stage0-bitbox02nova-multi-development.v3.bin
[binary or diff unavailable]
### src/bootloader_upgrade/bin/bootloader-stage0-bitbox02nova-multi-production.v3.bin
[binary or diff unavailable]
### src/bootloader_upgrade/bin/checksums.txt
@@ -1,11 +1,11 @@
-f0f5ca14d629c66ff450f24b8cd31fc3ab28600fcbea42554f922958b317b089 bootloader-stage0-bitbox02-btconly-development.v3.bin
-f18fba258882d573df79d1b643c1be5c8a0f08ba75dee02e0fecf8bddcb9f419 bootloader-stage0-bitbox02-btconly-production.v3.bin
-5db5dfdb41ea2d85e46039d96068e3c20e654a1c273fcc6331d33ed4f2c4bd2a bootloader-stage0-bitbox02-multi-development.v3.bin
-78b6e2484684304624d94ebe57275ffff73262789c0d1f899b383452de20bad3 bootloader-stage0-bitbox02-multi-production.v3.bin
-6f316d34c8ad66de31620d94981ae3c6cb1288b8c7ff57f788fcfc44dbcab976 bootloader-stage0-bitbox02nova-btconly-development.v3.bin
-a50763453e7aba78385cb7c8413dc2971a0e5eab1172250e2edde12386665285 bootloader-stage0-bitbox02nova-btconly-production.v3.bin
-d2bb2fa6bd1c285a7907c4cfc9cf142cb58f42b88133487b65e328fe3555a65d bootloader-stage0-bitbox02nova-multi-development.v3.bin
-e54afcc0a089e3062bfbb2a0213de9fc5ab3f79a7e32bbeae276aea41cca58cc bootloader-stage0-bitbox02nova-multi-production.v3.bin
+6b2605af8e975ad58450a35fe72a45cb4d664c65da38f8e46ad08350bd16b500 bootloader-stage0-bitbox02-btconly-development.v3.bin
+76ef68e79580c47e0ec2be3babe25457bada7e9c372dd5f170db8ca988d271a9 bootloader-stage0-bitbox02-btconly-production.v3.bin
+5b43e24b916f89935cecbc8605055b7e0daf540619a461ddf56178c8692cc74c bootloader-stage0-bitbox02-multi-development.v3.bin
+2fcbcb1a11ef03c6ea9a7f738ccb05b302df9839e32dd1304fd968bc8c9b1b3c bootloader-stage0-bitbox02-multi-production.v3.bin
+c44150bf7677ea4b679b363ae9ff0077ee13a283b6459208ff1835a2bb247a13 bootloader-stage0-bitbox02nova-btconly-development.v3.bin
+f526d2af9c2a892759d477e4aa959c0acb8f38df4333a4b0ad27ad2d9b777ec9 bootloader-stage0-bitbox02nova-btconly-production.v3.bin
+1c285b0e89c56252c218487c70630b38512149667153729cab54d4721f99befc bootloader-stage0-bitbox02nova-multi-development.v3.bin
+f5e907069d4604a04371e16fe107a5aad57ad3ffe33a2c9cad02624b5608cb21 bootloader-stage0-bitbox02nova-multi-production.v3.bin
e12efe809ad9a17098979242eb762cf9b682ed4208e44fb21748c2256aa4489b bootloader-stage1-bitbox02-btconly-development.v1.2.0.signed.bin
ebb93bb2c23b5266fccb93ca623fe9aadf15dd166999ba8380de37eb1ff26cae bootloader-stage1-bitbox02-btconly-production.v1.2.2.signed.bin
662b663a7fe41550a56fa76defa06f54f40d599e4416082ebab77eae35878279 bootloader-stage1-bitbox02-multi-development.v1.2.0.signed.bin
### test/unit-test/CMakeLists.txt
@@ -65,6 +65,8 @@ else()
"-Wl,--wrap=pukcc_ecdsa_verify,--wrap=sha_sync_sha256_start,--wrap=sha_sync_sha256_update,--wrap=sha_sync_sha256_finish"
stage0_flash
""
+ stage0_install
+ ""
stage0_descriptor
""
bootloader_upgrade_check
### test/unit-test/test_stage0_install.c
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <cmocka.h>
+
+#include "bootloader/stage0/stage0_install.h"
+
+static void test_stage0_install_auth_versions(void** state)
+{
+ (void)state;
+
+ const uint16_t versions[] = {0, 1, 2, 0x7fff, 0x8000, UINT16_MAX};
+ for (size_t i = 0; i < COUNT_OF(versions); i++) {
+ for (size_t j = 0; j < COUNT_OF(versions); j++) {
+ const secbool_u32 expected = versions[j] >= versions[i] ? sectrue_u32 : secfalse_u32;
+ assert_int_equal(
+ stage0_install_auth(
+ sectrue_u32, sectrue_u32, sectrue_u32, versions[i], versions[j]),
+ expected);
+ }
+ }
+}
+
+static void test_stage0_install_auth_recovery(void** state)
+{
+ (void)state;
+
+ // A damaged header's version is not a trustworthy recovery floor.
+ assert_int_equal(
+ stage0_install_auth(sectrue_u32, secfalse_u32, secfalse_u32, UINT16_MAX, 0), sectrue_u32);
+ assert_int_equal(
+ stage0_install_auth(sectrue_u32, secfalse_u32, secfalse_u32, 0, UINT16_MAX), sectrue_u32);
+}
+
+static void test_stage0_install_auth_verification_results(void** state)
+{
+ (void)state;
+
+ const secbool_u32 results[] = {
+ secfalse_u32, sectrue_u32, 1, UINT32_MAX, sectrue_u32 ^ 1U, sectrue_u32 ^ 2U};
+ for (size_t u = 0; u < COUNT_OF(results); u++) {
+ for (size_t i = 0; i < COUNT_OF(results); i++) {
+ for (size_t r = 0; r < COUNT_OF(results); r++) {
+ const bool allowed = results[u] == sectrue_u32 && results[i] == results[r] &&
+ (results[i] == sectrue_u32 || results[i] == secfalse_u32);
+ assert_int_equal(
+ stage0_install_auth(results[u], results[i], results[r], 1, 2),
+ allowed ? sectrue_u32 : secfalse_u32);
+ }
+ }
+ }
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_stage0_install_auth_versions),
+ cmocka_unit_test(test_stage0_install_auth_recovery),
+ cmocka_unit_test(test_stage0_install_auth_verification_results),
+ };
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}Why this scored 72/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.