Skip to content

Commit fe8f9fc

Browse files
ahasztagde-nordic
authored andcommitted
bootutil: Fixed security counter overflow detected to late
This commit fixes the issue, occuring when the maximum amount of security counter updates has been reached. This fact was only detected after a permament update already happened - the updated firmware was unable to boot, as it failed when trying to update the security counter after the permament swap. This commit adds the check if the security counter can be updated (i. e. free security counter slots are still available) before the swap is performed, fixing the issue. Signed-off-by: Artur Hadasz <artur.hadasz@nordicsemi.no>
1 parent 42fc9cd commit fe8f9fc

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

boot/bootutil/include/bootutil/security_cnt.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ fih_ret boot_nv_security_counter_get(uint32_t image_id, fih_int *security_cnt);
6363
int32_t boot_nv_security_counter_update(uint32_t image_id,
6464
uint32_t img_security_cnt);
6565

66+
/**
67+
* This function verifies whether the security counter update to a given value is possible.
68+
* The update might not be possible if the maximum amount of security counter updates
69+
* was reached.
70+
*
71+
* @param image_id Index of the image (from 0).
72+
* @param img_security_cnt New security counter value.
73+
*
74+
* @return FIH_SUCCESS if update is possible; FIH_FAILURE otherwise
75+
*/
76+
fih_ret boot_nv_security_counter_is_update_possible(uint32_t image_id,
77+
uint32_t img_security_cnt);
78+
6679
#ifdef __cplusplus
6780
}
6881
#endif

boot/bootutil/src/image_validate.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,19 @@ bootutil_img_validate(struct boot_loader_state *state,
445445
goto out;
446446
}
447447

448+
#ifdef MCUBOOT_HW_ROLLBACK_PROT_COUNTER_LIMITED
449+
if (img_security_cnt > (uint32_t)fih_int_decode(security_cnt)) {
450+
FIH_CALL(boot_nv_security_counter_is_update_possible, fih_rc, image_index,
451+
img_security_cnt);
452+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
453+
FIH_SET(fih_rc, FIH_FAILURE);
454+
BOOT_LOG_ERR("Security counter update is not possible, possibly the maximum "
455+
"number of security updates has been reached.");
456+
goto out;
457+
}
458+
}
459+
#endif
460+
448461
/* The image's security counter has been successfully verified. */
449462
security_counter_valid = fih_rc;
450463
break;

boot/zephyr/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,17 @@ config MCUBOOT_HW_DOWNGRADE_PREVENTION
10581058
Because of the acceptance of equal values it allows for software
10591059
downgrade to some extent.
10601060

1061+
config MCUBOOT_HW_DOWNGRADE_PREVENTION_COUNTER_LIMITED
1062+
bool "HW based downgrade prevention counter has limited number of updates"
1063+
depends on MCUBOOT_HW_DOWNGRADE_PREVENTION
1064+
help
1065+
When this option is set, the hardware downgrade prevention counter
1066+
has limited number of updates. This option will enable checking
1067+
if it is possible to update the counter before performing
1068+
the upgrade. If an update package contains a security counter
1069+
value as a TLV but it is not possible to update the counter,
1070+
the update will be rejected.
1071+
10611072
endchoice
10621073

10631074
config BOOT_WATCHDOG_FEED

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@
228228
#define MCUBOOT_HW_ROLLBACK_PROT
229229
#endif
230230

231+
#ifdef CONFIG_MCUBOOT_HW_DOWNGRADE_PREVENTION_COUNTER_LIMITED
232+
#define MCUBOOT_HW_ROLLBACK_PROT_COUNTER_LIMITED
233+
#endif
234+
231235
#ifdef CONFIG_MEASURED_BOOT
232236
#define MCUBOOT_MEASURED_BOOT
233237
#endif

0 commit comments

Comments
 (0)