Skip to content

boot: bootutil: Fix security counter updated before revert after resuming upgrade #2282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

taltenbach
Copy link
Contributor

@taltenbach taltenbach commented Apr 26, 2025

This PR fixes an issue that was preventing an interrupted then resumed upgrade to be reverted when the hardware rollback protection is enabled and the swap-scratch, swap-move or swap-offset strategy is used.

After a "test" upgrade has been performed, the security counter of every image must not be updated immediately to be able to roll back in case the upgrade is not confirmed. To that end, the boot_update_hw_rollback_protection routine, called just before exiting MCUboot, was checking the swap type of the image and updating the security counter only if the swap type was none, with the assumption that it implies no upgrade has been performed by MCUboot. This was working properly in the nominal case, when the upgrades are not interrupted, however this assumption is false if MCUboot has just resumed an upgrade after a power cycle because in that case the swap type is set to none after the end of the upgrade process:

/* Swap has finished set to NONE */
BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;

So, let's suppose an image A with a security counter N is upgraded, in "test" mode, to an image B with a security counter M > N:

  1. MCUboot starts the upgrade from A to B.
  2. During the upgrade, the device is power cycled.
  3. MCUboot resumes and completes the upgrade.
  4. Before booting image B, MCUboot updates the security counter value to M.
  5. The image B is faulty and is therefore not confirmed, the device is rebooted.
  6. MCUboot wants to revert the upgrade but refuses to roll back to image A since its security counter is lower than M. Therefore no revert is performed and the device might be bricked since B is faulty.

This was not detected by the tests since in the simulator the upgrades were performed with images having the same security counter value. In addition to fixing the issue, this MR also updates the simulator to use images with different security counter values, enabling to catch such kind of bugs.

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]>
When testing upgrades, the simulator was always using two images having
the same security counter. This was preventing to test that the security
counters are updated at the right time in the scenarios where a revert
is possible. The upgrade image is now generated with a higher security
counter than the original image, enabling to detect e.g. the issue fixed
by the previous commit.

Signed-off-by: Thomas Altenbach <[email protected]>
@nordicjm nordicjm requested a review from de-nordic May 2, 2025 06:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants