Skip to content

Commit f9ad3ee

Browse files
tomchyde-nordic
authored andcommitted
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. Signed-off-by: Tomasz Chyrowicz <tomasz.chyrowicz@nordicsemi.no>
1 parent 6e60200 commit f9ad3ee

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
@@ -76,6 +76,17 @@ int32_t boot_nv_security_counter_update(uint32_t image_id,
7676
fih_ret boot_nv_security_counter_is_update_possible(uint32_t image_id,
7777
uint32_t img_security_cnt);
7878

79+
#ifdef MCUBOOT_HW_ROLLBACK_PROT_LOCK
80+
/**
81+
* Lock updates of the stored value of a given image's security counter.
82+
*
83+
* @param image_id Index of the image (from 0).
84+
*
85+
* @return 0 on success; nonzero on failure.
86+
*/
87+
int32_t boot_nv_security_counter_lock(uint32_t image_id);
88+
#endif /* MCUBOOT_HW_ROLLBACK_PROT_LOCK */
89+
7990
#ifdef __cplusplus
8091
}
8192
#endif

boot/bootutil/src/loader.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,10 +1614,19 @@ boot_update_hw_rollback_protection(struct boot_loader_state *state)
16141614
if (swap_state.magic != BOOT_MAGIC_GOOD || swap_state.image_ok == BOOT_FLAG_SET) {
16151615
rc = boot_update_security_counter(state, BOOT_SLOT_PRIMARY, BOOT_SLOT_PRIMARY);
16161616
if (rc != 0) {
1617-
BOOT_LOG_ERR("Security counter update failed after image "
1618-
"validation.");
1617+
BOOT_LOG_ERR("Security counter update failed after image %d validation.",
1618+
BOOT_CURR_IMG(state));
1619+
return rc;
1620+
}
1621+
1622+
#ifdef MCUBOOT_HW_ROLLBACK_PROT_LOCK
1623+
rc = boot_nv_security_counter_lock(BOOT_CURR_IMG(state));
1624+
if (rc != 0) {
1625+
BOOT_LOG_ERR("Security counter lock failed after image %d validation.",
1626+
BOOT_CURR_IMG(state));
16191627
return rc;
16201628
}
1629+
#endif /* MCUBOOT_HW_ROLLBACK_PROT_LOCK */
16211630
}
16221631

16231632
return 0;
@@ -2341,9 +2350,19 @@ boot_update_hw_rollback_protection(struct boot_loader_state *state)
23412350
state->slot_usage[BOOT_CURR_IMG(state)].active_slot,
23422351
state->slot_usage[BOOT_CURR_IMG(state)].active_slot);
23432352
if (rc != 0) {
2344-
BOOT_LOG_ERR("Security counter update failed after image %d validation.", BOOT_CURR_IMG(state));
2353+
BOOT_LOG_ERR("Security counter update failed after image %d validation.",
2354+
BOOT_CURR_IMG(state));
2355+
return rc;
2356+
}
2357+
2358+
#ifdef MCUBOOT_HW_ROLLBACK_PROT_LOCK
2359+
rc = boot_nv_security_counter_lock(BOOT_CURR_IMG(state));
2360+
if (rc != 0) {
2361+
BOOT_LOG_ERR("Security counter lock failed after image %d validation.",
2362+
BOOT_CURR_IMG(state));
23452363
return rc;
23462364
}
2365+
#endif /* MCUBOOT_HW_ROLLBACK_PROT_LOCK */
23472366
#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
23482367
}
23492368
#endif

boot/zephyr/Kconfig

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

1073+
config MCUBOOT_HW_DOWNGRADE_PREVENTION_LOCK
1074+
bool "Lock updates of HW based downgrade prevention counter before boot"
1075+
depends on MCUBOOT_HW_DOWNGRADE_PREVENTION
1076+
help
1077+
When this option is set, the hardware downgrade prevention counter
1078+
updates are locked before booting the application.
1079+
This means that after booting the application it is not possible to
1080+
update the counter until a reboot.
1081+
This prevents the application from accidental updates of the counter,
1082+
that may invalidate the currently running image.
1083+
10731084
endchoice
10741085

10751086
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
@@ -233,6 +233,10 @@
233233
#define MCUBOOT_HW_ROLLBACK_PROT_COUNTER_LIMITED
234234
#endif
235235

236+
#ifdef CONFIG_MCUBOOT_HW_DOWNGRADE_PREVENTION_LOCK
237+
#define MCUBOOT_HW_ROLLBACK_PROT_LOCK
238+
#endif
239+
236240
#ifdef CONFIG_MCUBOOT_UUID_VID
237241
#define MCUBOOT_UUID_VID
238242
#endif

0 commit comments

Comments
 (0)