Skip to content

Commit 0eaf666

Browse files
taltenbachd3zd3z
authored andcommitted
boot: bootutil: Only update the security counter for confirmed images
When an upgrade is performed, the security counter must only be updated after the upgrade has been confirmed, to make possible to rollback if needed. To that end, the security counter was only updated for a given image if the swap type is BOOT_SWAP_TYPE_NONE, meaning in most cases that no update has been performed by MCUboot at this run. However, the swap type is also set to BOOT_SWAP_TYPE_NONE after an interrupted upgrade is completed, so at the time boot_update_hw_rollback_protection is called, having a "none" swap type doesn't guarantee that no upgrade is waiting for confirmation. This means MCUboot was wrongly updating the security counter immediately after the completion of a resumed upgrade, preventing any rollback in that case. Instead, the boot_update_hw_rollback_protection now checks the trailer of the primary image to determine if the security counter has to be updated. The update occurs only if the trailer is empty (no update has ever been made) or if the "image-ok" flag is set (the image has been confirmed). Signed-off-by: Thomas Altenbach <[email protected]>
1 parent 792d411 commit 0eaf666

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

boot/bootutil/src/loader.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,18 +2246,26 @@ boot_update_hw_rollback_protection(struct boot_loader_state *state)
22462246
{
22472247
#ifdef MCUBOOT_HW_ROLLBACK_PROT
22482248
int rc;
2249+
uint8_t image_index;
2250+
struct boot_swap_state swap_state;
2251+
2252+
image_index = BOOT_CURR_IMG(state);
2253+
2254+
rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index), &swap_state);
2255+
if (rc != 0) {
2256+
return rc;
2257+
}
22492258

22502259
/* Update the stored security counter with the active image's security
2251-
* counter value. It will only be updated if the new security counter is
2252-
* greater than the stored value.
2253-
*
2254-
* In case of a successful image swapping when the swap type is TEST the
2255-
* security counter can be increased only after a reset, when the swap
2256-
* type is NONE and the image has marked itself "OK" (the image_ok flag
2257-
* has been set). This way a "revert" can be performed when it's
2258-
* necessary.
2259-
*/
2260-
if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
2260+
* counter value. It will only be updated if the new security counter is
2261+
* greater than the stored value.
2262+
*
2263+
* In case of a successful image swapping when the swap type is TEST the
2264+
* security counter can be increased only after a reset, when the image has
2265+
* marked itself "OK" (the image_ok flag has been set). This way a "revert"
2266+
* can be performed when it's necessary.
2267+
*/
2268+
if (swap_state.magic != BOOT_MAGIC_GOOD || swap_state.image_ok == BOOT_FLAG_SET) {
22612269
rc = boot_update_security_counter(state, BOOT_PRIMARY_SLOT, BOOT_PRIMARY_SLOT);
22622270
if (rc != 0) {
22632271
BOOT_LOG_ERR("Security counter update failed after image "

0 commit comments

Comments
 (0)