Skip to content

Commit 6f17466

Browse files
committed
[nrf fromlist] 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. Upstream PR #: 2420 Signed-off-by: Artur Hadasz <[email protected]>
1 parent c72ed15 commit 6f17466

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

boot/bootutil/include/bootutil/security_cnt.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ fih_ret boot_nv_security_counter_get(uint32_t image_id, fih_int *security_cnt);
7272
int32_t boot_nv_security_counter_update(uint32_t image_id,
7373
uint32_t img_security_cnt);
7474

75+
/**
76+
* This function verifies whether the security counter update to a newer is possible.
77+
* The update might not be possible if the maximum amount of security counter updates
78+
* was reached.
79+
*
80+
* @return FIH_SUCCESS if update is possible; FIH_FAILURE otherwise
81+
*/
82+
fih_ret boot_nv_security_counter_is_update_possible(void);
83+
7584
#ifdef __cplusplus
7685
}
7786
#endif

boot/bootutil/src/image_validate.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,18 @@ bootutil_img_validate(struct boot_loader_state *state,
839839
goto out;
840840
}
841841

842+
#ifdef MCUBOOT_HW_ROLLBACK_PROT_COUNTER_LIMITED
843+
if (img_security_cnt > (uint32_t)fih_int_decode(security_cnt)) {
844+
FIH_CALL(boot_nv_security_counter_is_update_possible, fih_rc);
845+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
846+
FIH_SET(fih_rc, FIH_FAILURE);
847+
BOOT_LOG_ERR("Security counter update is not possible, possibly the maximum "
848+
"number of security updates has been reached.");
849+
goto out;
850+
}
851+
}
852+
#endif
853+
842854
/* The image's security counter has been successfully verified. */
843855
security_counter_valid = fih_rc;
844856
skip_security_counter_read:

boot/zephyr/Kconfig

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

1079+
config MCUBOOT_HW_DOWNGRADE_PREVENTION_COUNTER_LIMITED
1080+
bool "HW based downgrade prevention counter has limited number of updates"
1081+
depends on MCUBOOT_HW_DOWNGRADE_PREVENTION
1082+
help
1083+
When this option is set, the hardware downgrade prevention counter
1084+
has limited number of updates. This option will enable checking
1085+
if it is possible to update the counter before performing
1086+
the upgrade. If an update package contains a security counter
1087+
value as a TLV but it is not possible to update the counter,
1088+
the update will be rejected.
1089+
10791090
endchoice
10801091

10811092
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
@@ -209,6 +209,10 @@
209209
#define MCUBOOT_HW_ROLLBACK_PROT
210210
#endif
211211

212+
#ifdef CONFIG_MCUBOOT_HW_DOWNGRADE_PREVENTION_COUNTER_LIMITED
213+
#define MCUBOOT_HW_ROLLBACK_PROT_COUNTER_LIMITED
214+
#endif
215+
212216
#ifdef CONFIG_MEASURED_BOOT
213217
#define MCUBOOT_MEASURED_BOOT
214218
#endif

0 commit comments

Comments
 (0)