Merge commit 'refs/pull/2002/head' of https://github.com/BitBoxSwiss/bitbox02-firmware
What changed, and why it matters
This commit rewrites how the BitBox02 bootloader accepts and writes firmware updates. It adds stricter ordering (chunks must now arrive in ascending order), replaces a combined write-and-erase operation with explicit erase-before-write, and verifies that flash is blank before writing. These changes look like hardening against a bug where an attacker or glitch could cause the bootloader to accept out-of-order or repeated firmware chunks, potentially leaving old code in flash or allowing a downgrade/partial overwrite. The commit itself does not say it fixes a security issue, and no independent report is supplied, so the security relevance is inferred from the defensive nature of the changes.
Treat this as a security-relevant hardening patch. Review the full PR #2002 context, prior commits, and any associated release notes to determine whether it addresses a known vulnerability. If the device has already shipped firmware without this patch, assess whether the previous behavior could be exploited via the USB firmware-update interface or fault injection, and consider issuing a security advisory if confirmed.
Security signals we found
Bootloader firmware-update path modified
Strict ordering enforced for firmware chunks
Explicit erase-before-write with post-erase verification
Rejection of writes to non-erased flash regions
Prevention of repeated/rewound chunk writes unless identical
Compile-time alignment invariant added for firmware region
Evidence from the diff
The patch modifies src/bootloader/bootloader.c. Key changes: (1) introduces _firmware_next_chunk to enforce strictly ascending chunk order and to detect re-sent chunks; (2) replaces flash_write with flash_append after an explicit _firmware_erase_block and an _is_erased check; (3) rewrites _api_firmware_erase to erase only the unused tail of the firmware region starting at an erase-block boundary; (4) adds a compile-time check that FLASH_APP_LEN is a multiple of FIRMWARE_BLOCK_LEN; (5) fixes a divide-by-zero in progress rendering when only one chunk exists. The old code allowed any chunk number on each write, performed erase inside flash_write, and accepted a chunk as valid if it already matched the buffer in flash. The new behavior prevents writing a chunk unless all previous chunks have been written, and refuses to write over non-erased flash. This closes a class of bugs around out-of-order writes, skipped chunks, and stale flash contents surviving an update.
Changed components
BitBox02 bootloadersrc/bootloader/bootloader.cFirmware update protocol (OP_ERASE / OP_WRITE_FIRMWARE_CHUNK)Inspect captured patch +76 / −46
### src/bootloader/bootloader.c
@@ -46,16 +46,15 @@
// Bootloader API command op codes
//
-// OP_ERASE - Receives the total number of firmware chunks to write,
-// then pads any left over FLASH that would be empty by 0xFFs;
-// FLASH areas that will contain firmware are erased when writing the
-// firmware chunk to FLASH.
+// OP_ERASE - Erases unused firmware flash starting at an erase-block boundary.
+// Receives the total number of firmware chunks to write. If non-zero, starts
+// a new firmware loading session.
#define OP_ERASE ((uint8_t)'e') /* 0x65 */
// OP_REBOOT - Reboot the MCU and clear the auto_enter flag.
#define OP_REBOOT ((uint8_t)'r') /* 0x72 */
// OP_WRITE - Write a firmware chunk; the binary must be streamed in chunks.
// The command must include the chunk number in order to be written to the
-// correct FLASH location.
+// correct FLASH location. New chunks must arrive in ascending order.
#define OP_WRITE_FIRMWARE_CHUNK ((uint8_t)'w') /* 0x77 */
// OP_WRITE_SIG_DATA - Write the firmware's signature data, which is used
// for firmware verification.
@@ -95,11 +94,15 @@ static struct sha_context _pukcc_sha256_context;
COMPILER_PACK_RESET()
#define FIRMWARE_CHUNK_LEN (8U * FLASH_PAGE_SIZE) // 4kB
+#define FIRMWARE_BLOCK_LEN FLASH_ERASE_MIN_LEN // 8kB
#define FIRMWARE_MAX_NUM_CHUNKS \
(FLASH_APP_LEN / FIRMWARE_CHUNK_LEN) // app len must be a multiple of chunk len
#if (FIRMWARE_MAX_NUM_CHUNKS > UINT8_MAX)
#error "incompatible variable type"
#endif
+#if (FLASH_APP_LEN % FIRMWARE_BLOCK_LEN)
+ #error "app flash len must be a multiple of the firmware block len"
+#endif
// Be sure to not overflow boot data area
static_assert(sizeof(((boot_data_t*)0)->fields) <= FLASH_BOOTDATA_LEN, "boot_data_t too large");
@@ -115,6 +118,7 @@ static_assert(sizeof(((boot_data_t*)0)->fields) <= FLASH_BOOTDATA_LEN, "boot_dat
static bool _loading_ready = false;
static uint8_t _firmware_num_chunks = 0;
+static uint8_t _firmware_next_chunk = 0;
// Indicates whether the whole app flash contains only 0xFF.
// This controls bootloader text messages on the screen.
// The value is computed at bootloader enter.
@@ -386,6 +390,35 @@ static size_t _report_status(uint8_t status, uint8_t* output)
return BOOT_OP_LEN;
}
+static bool _is_erased(const void* addr, size_t len)
+{
+ const uint8_t* bytes = (const uint8_t*)addr;
+ for (size_t i = 0; i < len; i++) {
+ if (bytes[i] != 0xff) {
+ return false;
+ }
+ }
+ return true;
+}
+
+static uint8_t _firmware_erase_block(uint32_t addr)
+{
+ if (addr < FLASH_APP_START || addr + FIRMWARE_BLOCK_LEN > FLASH_APP_START + FLASH_APP_LEN ||
+ addr % FIRMWARE_BLOCK_LEN != 0) {
+ return OP_STATUS_ERR_ERASE;
+ }
+ if (_is_erased((const void*)addr, FIRMWARE_BLOCK_LEN)) {
+ return OP_STATUS_OK;
+ }
+ if (flash_erase(&FLASH_0, addr, FLASH_ERASE_PAGE_NUM) != ERR_NONE) {
+ return OP_STATUS_ERR_ERASE;
+ }
+ if (!_is_erased((const void*)addr, FIRMWARE_BLOCK_LEN)) {
+ return OP_STATUS_ERR_CHECK;
+ }
+ return OP_STATUS_OK;
+}
+
static size_t _api_write_chunk(const uint8_t* buf, uint8_t chunknum, uint8_t* output)
{
if (!_loading_ready) {
@@ -403,48 +436,45 @@ static size_t _api_write_chunk(const uint8_t* buf, uint8_t chunknum, uint8_t* ou
return _report_status(OP_STATUS_ERR_LEN, output);
}
- if (MEMEQ(
- (const void*)(FLASH_APP_START + (chunknum * FIRMWARE_CHUNK_LEN)),
- buf,
- FIRMWARE_CHUNK_LEN)) {
+ const uint32_t addr = FLASH_APP_START + chunknum * FIRMWARE_CHUNK_LEN;
+ if (chunknum < _firmware_next_chunk) {
+ if (!MEMEQ((const void*)addr, buf, FIRMWARE_CHUNK_LEN)) {
+ return _report_status(OP_STATUS_ERR_CHECK, output);
+ }
_loading_ready = true;
return _report_status(OP_STATUS_OK, output);
}
+ if (chunknum != _firmware_next_chunk) {
+ return _report_status(OP_STATUS_ERR_LEN, output);
+ }
- // Erase is handled inside of flash_write
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
- if (flash_write(
- &FLASH_0, FLASH_APP_START + (chunknum * FIRMWARE_CHUNK_LEN), buf, FIRMWARE_CHUNK_LEN) !=
- ERR_NONE) {
+ if (addr % FIRMWARE_BLOCK_LEN == 0) {
+ uint8_t status = _firmware_erase_block(addr);
+ if (status != OP_STATUS_OK) {
+ return _report_status(status, output);
+ }
+ }
+ if (!_is_erased((const void*)addr, FIRMWARE_CHUNK_LEN)) {
+ return _report_status(OP_STATUS_ERR_CHECK, output);
+ }
+
+ // flash_append reads its source as uint32_t words.
+ uint32_t aligned_buf[FIRMWARE_CHUNK_LEN / sizeof(uint32_t)];
+ memcpy(aligned_buf, buf, sizeof(aligned_buf));
+ if (flash_append(&FLASH_0, addr, (uint8_t*)aligned_buf, sizeof(aligned_buf)) != ERR_NONE) {
return _report_status(OP_STATUS_ERR_WRITE, output);
}
-#pragma GCC diagnostic pop
- if (!MEMEQ(
- (const void*)(FLASH_APP_START + (chunknum * FIRMWARE_CHUNK_LEN)),
- buf,
- FIRMWARE_CHUNK_LEN)) {
+ if (!MEMEQ((const void*)addr, buf, FIRMWARE_CHUNK_LEN)) {
return _report_status(OP_STATUS_ERR_CHECK, output);
}
size_t len = _report_status(OP_STATUS_OK, output);
+ _firmware_next_chunk++;
_loading_ready = true;
return len;
}
-static bool _flash_pages_erased(uint32_t addr, uint32_t num_pages)
-{
- const uint32_t* words = (const uint32_t*)addr;
- const uint32_t num_words = num_pages * FLASH_PAGE_SIZE / sizeof(uint32_t);
- for (uint32_t i = 0; i < num_words; i++) {
- if (words[i] != UINT32_MAX) {
- return false;
- }
- }
- return true;
-}
-
/**
* This function erases the padding bytes, if not already erased. Erasing starts
* at an erase-block boundary, so if the firmware ends halfway through a block,
@@ -468,23 +498,19 @@ static size_t _api_firmware_erase(uint8_t firmware_num_chunks, uint8_t* output)
return _report_status(OP_STATUS_ERR_UNLOCK, output);
}
}
- uint16_t firmware_num_pages = firmware_num_chunks * FIRMWARE_CHUNK_LEN / FLASH_PAGE_SIZE;
- uint16_t erase_start_page = firmware_num_pages - (firmware_num_pages % FLASH_ERASE_PAGE_NUM);
- for (uint32_t i = erase_start_page; i < (uint32_t)FLASH_APP_PAGE_NUM;
- i += FLASH_ERASE_PAGE_NUM) {
- const uint32_t addr = FLASH_APP_START + i * FLASH_PAGE_SIZE;
- if (_flash_pages_erased(addr, FLASH_ERASE_PAGE_NUM)) {
- continue;
- }
- if (flash_erase(&FLASH_0, addr, FLASH_ERASE_PAGE_NUM) != ERR_NONE) {
- return _report_status(OP_STATUS_ERR_ERASE, output);
- }
- if (!_flash_pages_erased(addr, FLASH_ERASE_PAGE_NUM)) {
- return _report_status(OP_STATUS_ERR_CHECK, output);
+ const uint32_t firmware_len = firmware_num_chunks * FIRMWARE_CHUNK_LEN;
+ const uint32_t erase_start = FLASH_APP_START + firmware_len - firmware_len % FIRMWARE_BLOCK_LEN;
+ for (uint32_t addr = erase_start; addr < FLASH_APP_START + FLASH_APP_LEN;
+ addr += FIRMWARE_BLOCK_LEN) {
+ uint8_t status = _firmware_erase_block(addr);
+ if (status != OP_STATUS_OK) {
+ return _report_status(status, output);
}
}
+
if (firmware_num_chunks > 0) {
_firmware_num_chunks = firmware_num_chunks;
+ _firmware_next_chunk = 0;
_loading_ready = true;
}
return _report_status(OP_STATUS_OK, output);
@@ -850,7 +876,11 @@ static size_t _api_command(const uint8_t* input, uint8_t* output, const size_t m
if (output[1] != OP_STATUS_OK) {
bootloader_render_default_screen();
} else {
- _render_progress((float)chunk_num / (float)(_firmware_num_chunks - 1));
+ float progress = 1;
+ if (_firmware_num_chunks > 1) {
+ progress = (float)chunk_num / (float)(_firmware_num_chunks - 1);
+ }
+ _render_progress(progress);
}
break;
}Why this scored 57/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.