Skip to content

Commit 9fa443f

Browse files
authored
Merge pull request #745 from danielinux/fixes-20260408
Fixes 20260408
2 parents 993b4f4 + 2fcc986 commit 9fa443f

39 files changed

+2426
-203
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ tools/keytools/Debug
8686
tools/keytools/Release
8787
tools/keytools/otp/otp-keystore-primer
8888

89+
8990
# delta binaries
9091
tools/delta/bmdiff
9192
tools/delta/bmpatch
@@ -176,6 +177,10 @@ tools/unit-tests/unit-hal-otp
176177
tools/unit-tests/unit-rot-auth
177178
tools/unit-tests/unit-sdhci-response-bits
178179
tools/unit-tests/unit-tpm-check-rot-auth
180+
tools/unit-tests/unit-policy-create
181+
tools/unit-tests/unit-sign-encrypted-output
182+
tools/unit-tests/unit-update-flash-delta
183+
tools/unit-tests/unit-update-flash-self-update
179184

180185

181186

CMakeLists.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,20 @@ if(ARCH STREQUAL "AARCH64")
747747

748748
endif()
749749

750+
if(NOT DEFINED WOLFBOOT_ORIGIN)
751+
set(WOLFBOOT_ORIGIN ${ARCH_FLASH_OFFSET})
752+
endif()
753+
754+
if(NOT DEFINED BOOTLOADER_PARTITION_SIZE)
755+
math(EXPR BOOTLOADER_PARTITION_SIZE
756+
"${WOLFBOOT_PARTITION_BOOT_ADDRESS} - ${ARCH_FLASH_OFFSET}"
757+
OUTPUT_FORMAT HEXADECIMAL)
758+
endif()
759+
750760
list(APPEND WOLFBOOT_DEFS ARCH_FLASH_OFFSET=${ARCH_FLASH_OFFSET})
761+
list(APPEND WOLFBOOT_DEFS
762+
WOLFBOOT_ORIGIN=${WOLFBOOT_ORIGIN}
763+
BOOTLOADER_PARTITION_SIZE=${BOOTLOADER_PARTITION_SIZE})
751764

752765
if(${WOLFBOOT_TARGET} STREQUAL "x86_64_efi")
753766
if(NOT DEFINED GNU_EFI_LIB_PATH)
@@ -1139,7 +1152,7 @@ if(TZEN)
11391152
endif()
11401153
endif()
11411154

1142-
target_sources(wolfboothal PRIVATE include/hal.h hal/${WOLFBOOT_TARGET}.c ${WOLFBOOT_FLASH_SOURCES}
1155+
target_sources(wolfboothal PRIVATE include/hal.h hal/hal.c hal/${WOLFBOOT_TARGET}.c ${WOLFBOOT_FLASH_SOURCES}
11431156
${PARTITION_SOURCE} ${WOLFBOOT_TZ_HAL_SOURCES})
11441157

11451158

docs/compile.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ To circumvent the compile-time checks on the maximum allowed stack size, use `WO
193193

194194
Optionally, it is possible to disable the backup copy of the current running firmware upon the installation of the
195195
update. This implies that no fall-back mechanism is protecting the target from a faulty firmware installation, but may be useful
196-
in some cases where it is not possible to write on the update partition from the bootloader.
196+
in some cases where it is not possible to write on the update partition from the bootloader. This also removes the
197+
power-fail-safe swap behavior: if power is lost while the update is being copied into the BOOT partition, the original
198+
firmware may already be partially overwritten and the device can be left unrecoverable.
197199
The associated compile-time option is
198200

199201
`DISABLE_BACKUP=1`

hal/hal.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,13 @@ int hal_flash_test_dualbank(void)
281281

282282
#endif /* TEST_FLASH */
283283

284+
WEAKFUNCTION int RAMFUNCTION hal_flash_protect(haladdr_t address, int len)
285+
{
286+
(void)address;
287+
(void)len;
288+
return 0;
289+
}
290+
284291
WEAKFUNCTION int hal_uds_derive_key(uint8_t *out, size_t out_len)
285292
{
286293
(void)out;

hal/nrf5340.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ void hal_init(void)
827827

828828
#ifdef __WOLFBOOT
829829
/* enable write protection for the region of flash specified */
830-
int hal_flash_protect(uint32_t start, uint32_t len)
830+
int RAMFUNCTION hal_flash_protect(haladdr_t start, int len)
831831
{
832832
/* only application core supports SPU */
833833
#ifdef TARGET_nrf5340_app
@@ -884,14 +884,6 @@ static void periph_unsecure()
884884

885885
void hal_prepare_boot(void)
886886
{
887-
/* Write protect bootloader region of flash.
888-
* Not needed in TrustZone configs because the application
889-
* runs in non-secure mode and the bootloader partition is marked as
890-
* secure. */
891-
#ifndef TZEN
892-
hal_flash_protect(WOLFBOOT_ORIGIN, BOOTLOADER_PARTITION_SIZE);
893-
#endif
894-
895887
if (enableShm) {
896888
#ifdef TARGET_nrf5340_net
897889
if (doUpdateNet) {

hal/skeleton.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ void hal_init(void)
3434

3535
void hal_prepare_boot(void)
3636
{
37+
/* wolfBoot calls hal_flash_protect() before this hook.
38+
* Override int hal_flash_protect(haladdr_t address, int len) to lock
39+
* the bootloader region on targets that support runtime write
40+
* protection. Return 0 on success or a negative value on failure, and
41+
* use this hook only for any remaining platform-specific handoff work.
42+
*/
3743
}
3844

3945
#endif
@@ -55,4 +61,3 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
5561
{
5662
return 0; /* on success. */
5763
}
58-

include/hal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ uint64_t hal_get_timer_us(void);
8787
#endif
8888
void hal_flash_unlock(void);
8989
void hal_flash_lock(void);
90+
/*
91+
* Lock the flash region [address, address + len) against writes.
92+
* Return 0 on success, or a negative value on failure.
93+
*/
94+
int hal_flash_protect(haladdr_t address, int len);
9095
void hal_prepare_boot(void);
9196

9297
#ifdef DUALBANK_SWAP

include/image.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ struct wolfBoot_image {
166166
* With ARMORED setup, the flag is redundant, and the information is wrapped in
167167
* between canary variables, to mitigate attacks based on memory corruptions.
168168
*/
169-
static void __attribute__((noinline)) wolfBoot_image_confirm_signature_ok(
169+
static void NOINLINEFUNCTION wolfBoot_image_confirm_signature_ok(
170170
struct wolfBoot_image *img)
171171
{
172172
img->canary_FEED4567 = 0xFEED4567UL;
@@ -176,7 +176,7 @@ static void __attribute__((noinline)) wolfBoot_image_confirm_signature_ok(
176176
img->canary_FEED89AB = 0xFEED89ABUL;
177177
}
178178

179-
static void __attribute__((noinline)) wolfBoot_image_clear_signature_ok(
179+
static void NOINLINEFUNCTION wolfBoot_image_clear_signature_ok(
180180
struct wolfBoot_image *img)
181181
{
182182
img->canary_FEED4567 = 0xFEED4567UL;
@@ -424,7 +424,8 @@ static void __attribute__((noinline)) wolfBoot_image_clear_signature_ok(
424424
asm volatile("mov r0, #50":::"r0"); \
425425
asm volatile("mov r0, #50":::"r0"); \
426426
asm volatile("mov r0, #50":::"r0"); \
427-
compare_res = XMEMCMP(digest, img->sha_hash, WOLFBOOT_SHA_DIGEST_SIZE); \
427+
compare_res = image_CT_compare(digest, img->sha_hash, \
428+
WOLFBOOT_SHA_DIGEST_SIZE); \
428429
/* Redundant checks that ensure the function actually returned 0 */ \
429430
asm volatile("cmp r0, #0":::"cc"); \
430431
asm volatile("cmp r0, #0":::"cc"); \
@@ -442,8 +443,9 @@ static void __attribute__((noinline)) wolfBoot_image_clear_signature_ok(
442443
asm volatile("cmp r0, #0":::"cc"); \
443444
asm volatile("cmp r0, #0":::"cc"); \
444445
asm volatile("bne hnope"); \
445-
/* Repeat memcmp call */ \
446-
compare_res = XMEMCMP(digest, img->sha_hash, WOLFBOOT_SHA_DIGEST_SIZE); \
446+
/* Repeat comparison call */ \
447+
compare_res = image_CT_compare(digest, img->sha_hash, \
448+
WOLFBOOT_SHA_DIGEST_SIZE); \
447449
compare_res; \
448450
/* Redundant checks that ensure the function actually returned 0 */ \
449451
asm volatile("cmp r0, #0":::"cc"); \
@@ -1234,7 +1236,7 @@ static void UNUSEDFUNCTION wolfBoot_image_clear_signature_ok(
12341236
ret = fn(__VA_ARGS__);
12351237

12361238
#define RSA_VERIFY_HASH(img,digest) \
1237-
if (XMEMCMP(img->sha_hash, digest, WOLFBOOT_SHA_DIGEST_SIZE) == 0) \
1239+
if (image_CT_compare(img->sha_hash, digest, WOLFBOOT_SHA_DIGEST_SIZE) == 0) \
12381240
wolfBoot_image_confirm_signature_ok(img);
12391241

12401242
#define PART_SANITY_CHECK(p) \
@@ -1250,6 +1252,8 @@ static void UNUSEDFUNCTION wolfBoot_image_clear_signature_ok(
12501252
#endif
12511253

12521254
/* Defined in image.c */
1255+
int image_CT_compare(const uint8_t *expected, const uint8_t *actual,
1256+
uint32_t len);
12531257
int wolfBoot_open_image(struct wolfBoot_image *img, uint8_t part);
12541258
#ifdef EXT_FLASH
12551259
int wolfBoot_open_image_external(struct wolfBoot_image* img, uint8_t part, uint8_t* addr);

include/tpm.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ int wolfBoot_load_pubkey(const uint8_t* pubkey_hint, WOLFTPM2_KEY* pubKey,
7979
TPM_ALG_ID* pAlg);
8080
#endif
8181

82+
#if defined(WOLFBOOT_TPM_KEYSTORE) || defined(WOLFBOOT_TPM_SEAL)
83+
int wolfBoot_constant_compare(const uint8_t* a, const uint8_t* b, uint32_t len);
84+
#endif
85+
8286
#ifdef WOLFBOOT_TPM_KEYSTORE
8387
int wolfBoot_check_rot(int key_slot, uint8_t* pubkey_hint);
8488
#endif

include/wolfboot/wolfboot.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ extern "C" {
8383
# endif
8484
#endif
8585

86+
#ifndef NOINLINEFUNCTION
87+
# if defined(__has_attribute)
88+
# if __has_attribute(noinline)
89+
# define NOINLINEFUNCTION __attribute__((noinline))
90+
# else
91+
# define NOINLINEFUNCTION
92+
# endif
93+
# elif defined(__GNUC__) || defined(__CC_ARM)
94+
# define NOINLINEFUNCTION __attribute__((noinline))
95+
# else
96+
# define NOINLINEFUNCTION
97+
# endif
98+
#endif
99+
86100

87101
/* Helpers for memory alignment */
88102
#ifndef XALIGNED
@@ -185,6 +199,15 @@ extern "C" {
185199
#endif
186200
#endif /* WOLFBOOT_SELF_HEADER */
187201

202+
#if defined(WOLFBOOT_SKIP_BOOT_VERIFY) && !defined(WOLFBOOT_SELF_HEADER)
203+
#error "WOLFBOOT_SKIP_BOOT_VERIFY requires WOLFBOOT_SELF_HEADER"
204+
#endif
205+
206+
#if defined(WOLFBOOT_SKIP_BOOT_VERIFY) && \
207+
!defined(WOLFBOOT_SELF_UPDATE_MONOLITHIC)
208+
#error "WOLFBOOT_SKIP_BOOT_VERIFY requires WOLFBOOT_SELF_UPDATE_MONOLITHIC"
209+
#endif
210+
188211
#ifdef BIG_ENDIAN_ORDER
189212
# define WOLFBOOT_MAGIC 0x574F4C46 /* WOLF */
190213
# define WOLFBOOT_MAGIC_TRAIL 0x424F4F54 /* BOOT */

0 commit comments

Comments
 (0)