fix(core/bootloader): reject changed headers on block-0 retry
What changed, and why it matters
This commit fixes a bug in the Trezor bootloader's firmware update process. When installing new firmware, the device first checks and approves the image's header (which includes things like the vendor identity and whether the storage should be wiped). Because of how retries work, a malicious or buggy host computer could have sent one approved header first, then on a retry of the very first data block sent a different image with a different header. The second header would be written to the device without being re-checked, potentially changing which vendor signed the firmware and skipping the security wipe of the user's secrets. The fix records a cryptographic fingerprint of the originally approved header and aborts the update if the retried block's header does not match exactly.
Ensure this fix is included in any bootloader release; verify that the headers_digest comparison cannot be bypassed and that validated_len is always set by every on_headers implementation. No user action is needed because the commit states the bug was never released.
Security signals we found
Bypass of header revalidation on block-0 retry
Potential storage-wipe decision bypass
Potential code placement shift via shorter vendor header
Fix records and enforces digest of validated header prefix
Abort instead of retry when headers change
Evidence from the diff
The bootloader’s image upload engine calls on_headers once during the IMAGE_INIT_CHUNK_SIZE prefetch to validate vendor/image headers, signatures, version policy, user confirmation, and to decide whether to wipe storage. It also caches headers_offset so per-block hashes cover only code. A prior fix made block-0 retry re-fetch from offset 0, so the retry includes header bytes again. Because headers_parsed remained set, those retried header bytes were not revalidated before on_chunk and flash write. An attacker could supply a different vendor header on retry, shifting code into the unhashed prefix and preserving the original (possibly no-wipe) storage decision. The patch adds a validated_len out-parameter from on_headers, stores a digest of that validated prefix, and aborts if block 0’s prefix does not match on retry.
Changed components
core/embed/projects/bootloader/workflow/wf_image_upload.ccore/embed/projects/bootloader/workflow/wf_image_upload.hcore/embed/projects/bootloader/workflow/wf_firmware_update.cInspect captured patch +49 / −4
### core/embed/projects/bootloader/workflow/wf_firmware_update.c
@@ -135,7 +135,7 @@ static void detect_installation(const vendor_header *current_vhdr,
static upload_status_t fw_on_headers(image_upload_handler_t *base,
protob_io_t *iface, const uint8_t *buf,
- size_t len) {
+ size_t len, size_t *validated_len) {
fw_upload_handler_t *self = (fw_upload_handler_t *)base;
(void)len;
@@ -353,6 +353,9 @@ static upload_status_t fw_on_headers(image_upload_handler_t *base,
}
self->headers_offset = IMAGE_HEADER_SIZE + vhdr.hdrlen;
+ // The per-block hashes start at headers_offset, so the headers before it are
+ // the part no later check covers; have the engine pin exactly that span.
+ *validated_len = self->headers_offset;
return UPLOAD_OK;
}
### core/embed/projects/bootloader/workflow/wf_image_upload.c
@@ -20,6 +20,7 @@
#include <trezor_model.h>
#include <trezor_rtl.h>
+#include <sec/image_hash_conf.h>
#include <sys/flash.h>
#include <sys/flash_utils.h>
#include <sys/sysevent.h>
@@ -55,6 +56,12 @@ typedef struct {
uint32_t chunk_size; // size of already received chunk data
bool headers_parsed; // true once the first chunk's headers are validated and
// confirmed
+ // Digest of the header as on_headers validated it. Block 0 is checked
+ // against it before it is written: its hash in the image covers only the
+ // code, so a retried block 0 could otherwise carry different headers than the
+ // ones that were validated and confirmed.
+ uint8_t headers_digest[IMAGE_HASH_DIGEST_LENGTH];
+ uint32_t headers_len;
bool wireless_transport; // whether the transport is over BLE
image_upload_handler_t *handler; // active image-type handler
} upload_engine_t;
@@ -107,6 +114,15 @@ static void write_image_data(const flash_area_t *area, uint32_t offset,
}
}
+// Digest of the first `len` bytes of chunk_buffer.
+static void image_prefix_digest(uint32_t len,
+ uint8_t digest[IMAGE_HASH_DIGEST_LENGTH]) {
+ IMAGE_HASH_CTX ctx;
+ IMAGE_HASH_INIT(&ctx);
+ IMAGE_HASH_UPDATE(&ctx, (const uint8_t *)chunk_buffer, len);
+ IMAGE_HASH_FINAL(&ctx, digest);
+}
+
static upload_status_t process_upload_chunk(protob_io_t *iface,
image_upload_handler_t *handler,
upload_engine_t *e) {
@@ -127,8 +143,10 @@ static upload_status_t process_upload_chunk(protob_io_t *iface,
if (!e->headers_parsed) {
// first block and headers are not yet parsed -> let the handler validate
// all headers, signatures, versions and run user confirmation / policy
- upload_status_t s = handler->on_headers(
- handler, iface, (const uint8_t *)chunk_buffer, e->chunk_size);
+ size_t validated_len = 0;
+ upload_status_t s =
+ handler->on_headers(handler, iface, (const uint8_t *)chunk_buffer,
+ e->chunk_size, &validated_len);
if (s != UPLOAD_OK) {
// handler has already sent the failure / abort message
return s;
@@ -139,6 +157,15 @@ static upload_status_t process_upload_chunk(protob_io_t *iface,
// How much of block 0 the header prefetch already delivered
const uint32_t prefetched = e->chunk_size;
+ if (validated_len > prefetched) {
+ // A handler may only pin bytes it was actually given.
+ send_msg_failure(iface, FailureType_Failure_ProcessError,
+ "Invalid firmware header");
+ return UPLOAD_ERR_INVALID_IMAGE_HEADER;
+ }
+ e->headers_len = validated_len;
+ image_prefix_digest(e->headers_len, e->headers_digest);
+
uint32_t chunk_limit =
(e->remaining > IMAGE_CHUNK_SIZE) ? IMAGE_CHUNK_SIZE : e->remaining;
// The buffer is complete only once the whole block is in it.
@@ -173,6 +200,18 @@ static upload_status_t process_upload_chunk(protob_io_t *iface,
return UPLOAD_ERR_FIRMWARE_TOO_BIG;
}
+ if (e->block == 0) {
+ uint8_t digest[IMAGE_HASH_DIGEST_LENGTH];
+ image_prefix_digest(e->headers_len, digest);
+ if (memcmp(digest, e->headers_digest, sizeof(digest)) != 0) {
+ // Not a transfer error the retry could fix: the host sent different
+ // headers. Abort.
+ send_msg_failure(iface, FailureType_Failure_ProcessError,
+ "Firmware headers changed");
+ return UPLOAD_ERR_INVALID_IMAGE_HEADER;
+ }
+ }
+
// type-specific per-chunk integrity verification
upload_status_t cs = handler->on_chunk(
handler, iface, e->block, (const uint8_t *)chunk_buffer, e->chunk_size);
### core/embed/projects/bootloader/workflow/wf_image_upload.h
@@ -144,11 +144,14 @@ struct image_upload_handler {
* @param iface Protobuf I/O interface used to send failure / abort messages.
* @param buf Buffer holding the first received chunk.
* @param len Number of valid bytes in @p buf.
+ * @param validated_len Out: bytes at the start of the image it validated
+ * that `on_chunk` does not cover (preset to 0). The engine rejects a
+ * retried first chunk whose bytes there differ.
* @return UPLOAD_OK on success, a negative upload_status_t otherwise.
*/
upload_status_t (*on_headers)(image_upload_handler_t *self,
protob_io_t *iface, const uint8_t *buf,
- size_t len);
+ size_t len, size_t *validated_len);
/**
* Verifies a fully-received chunk before it is written to flash.Why this scored 68/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.