Merge pull request #748 from Foundation-Devices/sft-8654-passport-core-bounded-text-building-in-the
What changed, and why it matters
This commit replaces unsafe string-copying functions in the Passport hardware wallet's bootloader with a bounded helper that prevents text fields from overflowing their buffers. The change is defensive: it stops malicious or malformed firmware headers (whose version/date fields are not guaranteed to end with a NUL byte) from causing buffer overflows when the bootloader displays information or asks the user to approve a firmware recovery.
Treat this as a security-hardening fix and include it in the next firmware release. Review other bootloader and firmware sites for remaining unbounded string operations on header fields. Run the new `test_bootloader_text_is_bounded_c` test in CI.
Security signals we found
Replacement of strcpy/strcat with bounded append_text helper
Handling of non-NUL-terminated firmware header text fields
Buffer overflow mitigation in bootloader UI message construction
Addition of host-compiled C unit test for bounded text behavior
Evidence from the diff
The bootloader’s version(), show_more_info(), and microsd_firmware_recovery() functions previously used strcpy/strcat on fwhdr->info.fwversion, fwhdr->info.fwdate, and similar fields from firmware headers. Because those header text fields are fixed-size and not NUL-terminated, unbounded strcat could read past field boundaries and overflow local message/version buffers. The patch introduces append_text(), plus APPEND_TEXT, APPEND_FIELD, and SET_TEXT macros, that always write within a known destination size and explicitly stop at either the source length or a NUL. A new host-side C test compiles and exercises the helper to verify truncation and guard-byte integrity.
Changed components
ports/stm32/boards/Passport/bootloader/main.cports/stm32/boards/Passport/modules/tests/test_firmware_update_host.pyInspect captured patch +89 / −22
### ports/stm32/boards/Passport/bootloader/main.c
@@ -305,12 +305,34 @@ static void MPU_Config(void) {
#endif /* ifndef FACTORY_TEST */
+// Appends at most src_len bytes of src to the string in dst, stopping at a NUL,
+// and truncates rather than writing past dst_size. Header text fields are not
+// NUL-terminated, so they are appended with their field size as the limit.
+static void append_text(char* dst, size_t dst_size, const char* src, size_t src_len) {
+ if (dst_size == 0) {
+ return;
+ }
+
+ size_t used = 0;
+ while (used < dst_size - 1 && dst[used] != '\0') {
+ used++;
+ }
+ for (size_t i = 0; i < src_len && src[i] != '\0' && used < dst_size - 1; i++) {
+ dst[used++] = src[i];
+ }
+ dst[used] = '\0';
+}
+
+#define APPEND_TEXT(dst, src) append_text(dst, sizeof(dst), src, SIZE_MAX)
+#define APPEND_FIELD(dst, field) append_text(dst, sizeof(dst), (const char*)(field), sizeof(field))
+#define SET_TEXT(dst, src) ((dst)[0] = '\0', APPEND_TEXT(dst, src))
+
static void version(void) {
passport_firmware_header_t* fwhdr = (passport_firmware_header_t*)FW_HDR;
char version[22] = {0};
- strcpy(version, "Version ");
- strcat(version, (char*)fwhdr->info.fwversion);
+ APPEND_TEXT(version, "Version ");
+ APPEND_FIELD(version, fwhdr->info.fwversion);
show_splash(version);
}
@@ -353,10 +375,10 @@ static void show_more_info(void) {
KEY_ID key;
switch (page) {
case 0:
- strcpy(message, "Version ");
- strcat(message, build_version);
- strcat(message, "\n\nBuilt on ");
- strcat(message, build_date);
+ SET_TEXT(message, "Version ");
+ APPEND_TEXT(message, build_version);
+ APPEND_TEXT(message, "\n\nBuilt on ");
+ APPEND_TEXT(message, build_date);
key = ui_show_page(heading, "Bootloader Info", message, &LARGE_ICON_INFO, &ICON_SHUTDOWN, &ICON_EXIT,
true, page, NUM_INFO_PAGES);
@@ -369,10 +391,10 @@ static void show_more_info(void) {
break;
case 1:
- strcpy(message, "Version ");
- strcat(message, (char*)fwhdr->info.fwversion);
- strcat(message, "\n\nBuilt on ");
- strcat(message, (char*)fwhdr->info.fwdate);
+ SET_TEXT(message, "Version ");
+ APPEND_FIELD(message, fwhdr->info.fwversion);
+ APPEND_TEXT(message, "\n\nBuilt on ");
+ APPEND_FIELD(message, fwhdr->info.fwdate);
key = ui_show_page(heading, "Firmware Info", message, &LARGE_ICON_INFO, &ICON_SHUTDOWN, &ICON_EXIT,
true, page, NUM_INFO_PAGES);
if (key == KEY_RIGHT_SELECT) {
@@ -384,10 +406,9 @@ static void show_more_info(void) {
break;
case 2: {
- strcpy(message, "");
hash_fw_user((uint8_t*)fwhdr, FW_HEADER_SIZE + fwhdr->info.fwlength, fw_hash, sizeof(fw_hash), false);
- bytes_to_hex_str(fw_hash, 32, &message[strlen(message)], 8, "\n");
+ bytes_to_hex_str(fw_hash, 32, message, 8, "\n");
key = ui_show_page(heading, "Firmware Hash", message, &LARGE_ICON_INFO, &ICON_SHUTDOWN, &ICON_EXIT,
true, page, NUM_INFO_PAGES);
@@ -401,10 +422,9 @@ static void show_more_info(void) {
}
case 3: {
- strcpy(message, "");
hash_fw_user((uint8_t*)fwhdr, FW_HEADER_SIZE + fwhdr->info.fwlength, fw_hash, sizeof(fw_hash), true);
- bytes_to_hex_str(fw_hash, 32, &message[strlen(message)], 8, "\n");
+ bytes_to_hex_str(fw_hash, 32, message, 8, "\n");
key = ui_show_page(heading, "Build Hash", message, &LARGE_ICON_INFO, &ICON_SHUTDOWN, &ICON_EXIT, true,
page, NUM_INFO_PAGES);
@@ -499,12 +519,12 @@ static void microsd_firmware_recovery(void) {
}
if ((res = sd_card_init()) != HAL_OK) {
- strcpy(message, "Unable to read from microSD.");
+ SET_TEXT(message, "Unable to read from microSD.");
goto fail;
}
if (sd_read_block(0, sizeof(passport_firmware_header_t), (uint8_t*)&sd_card_hdr) != HAL_OK) {
- strcpy(message, "Unable to read from microSD.");
+ SET_TEXT(message, "Unable to read from microSD.");
goto fail;
}
@@ -521,7 +541,7 @@ static void microsd_firmware_recovery(void) {
// User signed firmware cannot be used as a factory reset firmware.
if (firmware_is_user_signed(&sd_card_hdr)) {
- strcpy(message, "Firmware signed by a Developer PubKey cannot be used for recovery.");
+ SET_TEXT(message, "Firmware signed by a Developer PubKey cannot be used for recovery.");
goto fail;
}
@@ -561,11 +581,10 @@ static void microsd_firmware_recovery(void) {
}
#endif // PRODUCTION_BUILD
- strcpy(message, "Install this firmware?\n\n");
- strcat(message, "Version ");
- strcat(message, (char*)sd_card_hdr.info.fwversion);
- strcat(message, "\nBuilt on ");
- strcat(message, (char*)sd_card_hdr.info.fwdate);
+ SET_TEXT(message, "Install this firmware?\n\nVersion ");
+ APPEND_FIELD(message, sd_card_hdr.info.fwversion);
+ APPEND_TEXT(message, "\nBuilt on ");
+ APPEND_FIELD(message, sd_card_hdr.info.fwdate);
ask_to_recover:
if (ui_show_question("FIRMWARE RECOVERY", "Recover Passport", message, true) == KEY_RIGHT_SELECT) {
### ports/stm32/boards/Passport/modules/tests/test_firmware_update_host.py
@@ -256,6 +256,54 @@ def test_bootloader_header_validation_c(tmp_path, screen):
subprocess.run([str(binary)], check=True)
+def test_bootloader_text_is_bounded_c(tmp_path):
+ source = (BOARD / 'bootloader/main.c').read_text()
+ start = source.index('// Appends at most')
+ helper = source[start:source.index('static void version(void)', start)]
+ harness = tmp_path / 'text.c'
+ harness.write_text('''
+#include <assert.h>
+#include <string.h>
+#include "fwheader.h"
+''' + helper + '''
+int main(void) {
+ passport_firmware_header_t header;
+ memset(&header, 'x', sizeof(header));
+
+ // Header text fields are not terminated, so they stop at their own size.
+ char version[22] = {0};
+ APPEND_TEXT(version, "Version ");
+ APPEND_FIELD(version, header.info.fwversion);
+ assert(strcmp(version, "Version xxxxxxxx") == 0);
+
+ // A terminated field stops at its NUL, and SET_TEXT starts over.
+ memcpy(header.info.fwversion, "2.4.0", 6);
+ SET_TEXT(version, "Version ");
+ APPEND_FIELD(version, header.info.fwversion);
+ assert(strcmp(version, "Version 2.4.0") == 0);
+
+ // Text that does not fit is cut short without writing past the buffer.
+ struct {
+ char text[16];
+ char guard[8];
+ } out;
+ memset(out.guard, 'G', sizeof(out.guard));
+ SET_TEXT(out.text, "Install this firmware? Version ");
+ APPEND_FIELD(out.text, header.info.fwdate);
+ assert(strcmp(out.text, "Install this fi") == 0);
+ APPEND_TEXT(out.text, "more");
+ assert(strcmp(out.text, "Install this fi") == 0);
+ assert(memcmp(out.guard, "GGGGGGGG", sizeof(out.guard)) == 0);
+ return 0;
+}
+''')
+ binary = tmp_path / 'text'
+ subprocess.run(shlex.split(os.environ.get('CC', 'cc')) +
+ ['-std=gnu99', '-Wall', '-Werror', '-I', str(BOARD / 'include'), str(harness), '-o', str(binary)],
+ check=True)
+ subprocess.run([str(binary)], check=True)
+
+
@pytest.mark.parametrize('developer', [True, False])
def test_key_removal_guard(monkeypatch, developer):
events = []Why this scored 60/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.