Skip to content

Commit 6fd7e24

Browse files
committed
[nrf noup] 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 <[email protected]>
1 parent 4bfb139 commit 6fd7e24

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

boot/bootutil/include/bootutil/security_cnt.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ 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+
* @param image_id Index of the image (from 0).
81+
*
82+
* @return FIH_SUCCESS if update is possible; FIH_FAILURE otherwise
83+
*/
84+
fih_ret boot_nv_security_counter_is_update_possible(void);
85+
7586
#ifdef __cplusplus
7687
}
7788
#endif

boot/bootutil/src/image_validate.c

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

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

0 commit comments

Comments
 (0)