From 1996691a896a44e1d35bc4925c3b34d9b45725c0 Mon Sep 17 00:00:00 2001 From: Brett Nicholas <7547222+bigbrett@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:07:38 -0800 Subject: [PATCH 1/2] Align self-header and decrypted header RAM buffers --- src/libwolfboot.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libwolfboot.c b/src/libwolfboot.c index 9ef6d737b8..6e7e8ff392 100644 --- a/src/libwolfboot.c +++ b/src/libwolfboot.c @@ -2222,7 +2222,7 @@ int wolfBoot_get_delta_info(uint8_t part, int inverse, uint32_t **img_offset, #if defined(__WOLFBOOT) && defined(WOLFBOOT_LOAD_ADDRESS) extern uint8_t _end[]; /* linker symbol: end of wolfBoot BSS */ #endif -static uint8_t dec_hdr[IMAGE_HEADER_SIZE]; +static uint8_t dec_hdr[IMAGE_HEADER_SIZE] XALIGNED(4); static int decrypt_header(uint8_t *src) { @@ -2407,7 +2407,7 @@ static uint8_t* wolfBoot_get_image_from_part(uint8_t part) uint8_t* wolfBoot_get_self_header(void) { #if defined(EXT_FLASH) && defined(WOLFBOOT_SELF_HEADER_EXT) - static uint8_t hdr_buf[IMAGE_HEADER_SIZE]; + static uint8_t hdr_buf[IMAGE_HEADER_SIZE] XALIGNED(4); uint32_t magic; #ifdef NVM_FLASH_ECC From 6119e3b2c87460e05275953ae3ac446b324d557c Mon Sep 17 00:00:00 2001 From: Brett Nicholas <7547222+bigbrett@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:08:48 -0800 Subject: [PATCH 2/2] Reject monolithic self-update if self-header version is unreadable --- docs/firmware_update.md | 9 ++++---- src/update_flash.c | 16 +++++++++++--- tools/unit-tests/unit-update-flash.c | 33 ++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/docs/firmware_update.md b/docs/firmware_update.md index c9186b9412..47919548db 100644 --- a/docs/firmware_update.md +++ b/docs/firmware_update.md @@ -261,10 +261,11 @@ The persisted header also sets the version a new payload must exceed. A staged payload is installed only if its version is greater than both the running bootloader's `WOLFBOOT_VERSION` and the version in the self-header. The bootloader inside each payload can therefore keep the -same `WOLFBOOT_VERSION` across releases. Without the self-header only -`WOLFBOOT_VERSION` is checked, so the bootloader in each payload must be -built with `WOLFBOOT_VERSION` equal to the payload's signed version, -otherwise the update is rejected. +same `WOLFBOOT_VERSION` across releases. If the self-header is present +but its version cannot be read, the payload is not installed. Without +the self-header only `WOLFBOOT_VERSION` is checked, so the bootloader in +each payload must be built with `WOLFBOOT_VERSION` equal to the payload's +signed version, otherwise the update is rejected. ##### Restrictions diff --git a/src/update_flash.c b/src/update_flash.c index 976365a25c..9533a45095 100644 --- a/src/update_flash.c +++ b/src/update_flash.c @@ -291,10 +291,20 @@ void RAMFUNCTION wolfBoot_check_self_update(void) #if defined(WOLFBOOT_SELF_UPDATE_MONOLITHIC) && defined(WOLFBOOT_SELF_HEADER) /* The self-header has the installed payload's version, which may be * newer than the WOLFBOOT_VERSION this bootloader was built with */ - uint32_t self_version = wolfBoot_get_self_version(); + uint8_t *self_hdr = wolfBoot_get_self_header(); - if (self_version > current_version) { - current_version = self_version; + if (self_hdr != NULL) { + uint32_t self_version = wolfBoot_get_blob_version(self_hdr); + + /* A valid header always has a version. Without it the update + * cannot be checked against the installed payload. */ + if (self_version == 0) { + wolfBoot_printf("Self header version unreadable\n"); + return; + } + if (self_version > current_version) { + current_version = self_version; + } } #endif if (update_version <= current_version) { diff --git a/tools/unit-tests/unit-update-flash.c b/tools/unit-tests/unit-update-flash.c index 2eb87f8230..afb75f4fc0 100644 --- a/tools/unit-tests/unit-update-flash.c +++ b/tools/unit-tests/unit-update-flash.c @@ -670,6 +670,36 @@ START_TEST (test_self_update_self_header_newversion_installs) cleanup_flash(); } END_TEST + +/* The self-header is present but has no version field, so the update cannot + * be checked against it. It is not installed, and stays staged. */ +START_TEST (test_self_update_self_header_no_version_denied) +{ + uintptr_t hdr = WOLFBOOT_PARTITION_SELF_HEADER_ADDRESS; + uint32_t magic = WOLFBOOT_MAGIC; + uint32_t size_img = host_to_img_u32(TEST_SIZE_SMALL); + + reset_mock_stats(); + prepare_flash(); + clear_erase_stats(); + ext_flash_unlock(); + ext_flash_erase(hdr, WOLFBOOT_SELF_HEADER_SIZE); + ext_flash_write(hdr, (void *)&magic, 4); + ext_flash_write(hdr + 4, (void *)&size_img, 4); + ext_flash_lock(); + stage_self_update(WOLFBOOT_VERSION + 1); + ck_assert_ptr_nonnull(wolfBoot_get_self_header()); + ck_assert_uint_eq(wolfBoot_get_self_version(), 0); + + wolfBoot_check_self_update(); + + ck_assert_int_eq(arch_reboot_called, 0); + ck_assert_int_eq(erased_update, 0); + ck_assert_uint_eq(*(uint32_t *)(uintptr_t)WOLFBOOT_PARTITION_BOOT_ADDRESS, + 0xFFFFFFFFu); + cleanup_flash(); +} +END_TEST #endif /* WOLFBOOT_SELF_UPDATE_MONOLITHIC && WOLFBOOT_SELF_HEADER */ #endif @@ -2373,6 +2403,7 @@ Suite *wolfboot_suite(void) TCase *self_header_sameversion = tcase_create("Self update self-header same version erased"); TCase *self_header_oldversion = tcase_create("Self update self-header older version erased"); TCase *self_header_newversion = tcase_create("Self update self-header newer version installs"); + TCase *self_header_no_version = tcase_create("Self update self-header without version denied"); #endif tcase_add_test(self_update_sameversion, test_self_update_sameversion_erased); @@ -2383,6 +2414,7 @@ Suite *wolfboot_suite(void) tcase_add_test(self_header_sameversion, test_self_update_self_header_sameversion_erased); tcase_add_test(self_header_oldversion, test_self_update_self_header_oldversion_erased); tcase_add_test(self_header_newversion, test_self_update_self_header_newversion_installs); + tcase_add_test(self_header_no_version, test_self_update_self_header_no_version_denied); #endif suite_add_tcase(s, self_update_sameversion); @@ -2397,6 +2429,7 @@ Suite *wolfboot_suite(void) suite_add_tcase(s, self_header_sameversion); suite_add_tcase(s, self_header_oldversion); suite_add_tcase(s, self_header_newversion); + suite_add_tcase(s, self_header_no_version); #endif #endif return s;