Skip to content

Commit 885470b

Browse files
committed
[nrf fromlist] bootutil: Add API to lock HW counter
Add a new API to lock further updates of the HW-based security counter. This API may prevent the non-bootloader application from accidental invalidation of the bootable firmware. Upstream PR #: 2543 Signed-off-by: Tomasz Chyrowicz <[email protected]>
1 parent 0d263fa commit 885470b

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

boot/bootutil/include/bootutil/security_cnt.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ int32_t boot_nv_security_counter_update(uint32_t image_id,
8585
fih_ret boot_nv_security_counter_is_update_possible(uint32_t image_id,
8686
uint32_t img_security_cnt);
8787

88+
#ifdef MCUBOOT_HW_ROLLBACK_PROT_LOCK
89+
/**
90+
* Lock updates of the stored value of a given image's security counter.
91+
*
92+
* @param image_id Index of the image (from 0).
93+
*
94+
* @return 0 on success; nonzero on failure.
95+
*/
96+
int32_t boot_nv_security_counter_lock(uint32_t image_id);
97+
#endif /* MCUBOOT_HW_ROLLBACK_PROT_LOCK */
98+
8899
#ifdef __cplusplus
89100
}
90101
#endif

boot/bootutil/src/loader.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,10 +2581,19 @@ boot_update_hw_rollback_protection(struct boot_loader_state *state)
25812581
if (swap_state.magic != BOOT_MAGIC_GOOD || swap_state.image_ok == BOOT_FLAG_SET) {
25822582
rc = boot_update_security_counter(state, BOOT_SLOT_PRIMARY, BOOT_SLOT_PRIMARY);
25832583
if (rc != 0) {
2584-
BOOT_LOG_ERR("Security counter update failed after image "
2585-
"validation.");
2584+
BOOT_LOG_ERR("Security counter update failed after image %d validation.",
2585+
BOOT_CURR_IMG(state));
2586+
return rc;
2587+
}
2588+
2589+
#ifdef MCUBOOT_HW_ROLLBACK_PROT_LOCK
2590+
rc = boot_nv_security_counter_lock(BOOT_CURR_IMG(state));
2591+
if (rc != 0) {
2592+
BOOT_LOG_ERR("Security counter lock failed after image %d validation.",
2593+
BOOT_CURR_IMG(state));
25862594
return rc;
25872595
}
2596+
#endif /* MCUBOOT_HW_ROLLBACK_PROT_LOCK */
25882597
}
25892598

25902599
return 0;
@@ -3361,9 +3370,19 @@ boot_update_hw_rollback_protection(struct boot_loader_state *state)
33613370
state->slot_usage[BOOT_CURR_IMG(state)].active_slot,
33623371
state->slot_usage[BOOT_CURR_IMG(state)].active_slot);
33633372
if (rc != 0) {
3364-
BOOT_LOG_ERR("Security counter update failed after image %d validation.", BOOT_CURR_IMG(state));
3373+
BOOT_LOG_ERR("Security counter update failed after image %d validation.",
3374+
BOOT_CURR_IMG(state));
3375+
return rc;
3376+
}
3377+
3378+
#ifdef MCUBOOT_HW_ROLLBACK_PROT_LOCK
3379+
rc = boot_nv_security_counter_lock(BOOT_CURR_IMG(state));
3380+
if (rc != 0) {
3381+
BOOT_LOG_ERR("Security counter lock failed after image %d validation.",
3382+
BOOT_CURR_IMG(state));
33653383
return rc;
33663384
}
3385+
#endif /* MCUBOOT_HW_ROLLBACK_PROT_LOCK */
33673386
#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
33683387
}
33693388
#endif

boot/zephyr/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,17 @@ config MCUBOOT_HW_DOWNGRADE_PREVENTION_COUNTER_LIMITED
11541154
value as a TLV but it is not possible to update the counter,
11551155
the update will be rejected.
11561156

1157+
config MCUBOOT_HW_DOWNGRADE_PREVENTION_LOCK
1158+
bool "Lock updates of HW based downgrade prevention counter before boot"
1159+
depends on MCUBOOT_HW_DOWNGRADE_PREVENTION
1160+
help
1161+
When this option is set, the hardware downgrade prevention counter
1162+
updates are locked before booting the application.
1163+
This means that after booting the application it is not possible to
1164+
update the counter until a reboot.
1165+
This prevents the application from accidental updates of the counter,
1166+
that may invalidate the currently running image.
1167+
11571168
endchoice
11581169

11591170
config MCUBOOT_UUID_VID

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@
239239
#define MCUBOOT_HW_ROLLBACK_PROT_COUNTER_LIMITED
240240
#endif
241241

242+
#ifdef CONFIG_MCUBOOT_HW_DOWNGRADE_PREVENTION_LOCK
243+
#define MCUBOOT_HW_ROLLBACK_PROT_LOCK
244+
#endif
245+
242246
#ifdef CONFIG_MCUBOOT_UUID_VID
243247
#define MCUBOOT_UUID_VID
244248
#endif

0 commit comments

Comments
 (0)