Skip to content
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

[nrf noup] boot: bootutil: loader: Fix monotomic counter update issues #402

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions boot/bootutil/include/bootutil/security_cnt.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ extern "C" {
*/
fih_ret boot_nv_security_counter_init(void);

/**
* Checks if the specified image should have a security counter present on it or not
*
* @param image_index Index of the image to check (from 0).
*
* @return FIH_SUCCESS if security counter should be present; FIH_FAILURE if otherwise
*/
fih_ret boot_nv_image_should_have_security_counter(uint32_t image_index);

/**
* Reads the stored value of a given image's security counter.
*
Expand Down
20 changes: 20 additions & 0 deletions boot/bootutil/src/image_validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,15 @@ bootutil_img_validate(struct boot_loader_state *state,
fih_int security_cnt = fih_int_encode(INT_MAX);
uint32_t img_security_cnt = 0;
FIH_DECLARE(security_counter_valid, FIH_FAILURE);
FIH_DECLARE(security_counter_should_be_present, FIH_FAILURE);

FIH_CALL(boot_nv_image_should_have_security_counter, security_counter_should_be_present,
image_index);
if (FIH_NOT_EQ(security_counter_should_be_present, FIH_SUCCESS) &&
FIH_NOT_EQ(security_counter_should_be_present, FIH_FAILURE)) {
rc = -1;
goto out;
}
#endif

#ifdef MCUBOOT_DECOMPRESS_IMAGES
Expand Down Expand Up @@ -773,6 +782,10 @@ bootutil_img_validate(struct boot_loader_state *state,
goto out;
}

if (FIH_EQ(security_counter_should_be_present, FIH_FAILURE)) {
goto skip_security_counter_read;
}

FIH_CALL(boot_nv_security_counter_get, fih_rc, image_index,
&security_cnt);
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Expand All @@ -792,6 +805,7 @@ bootutil_img_validate(struct boot_loader_state *state,

/* The image's security counter has been successfully verified. */
security_counter_valid = fih_rc;
skip_security_counter_read:
break;
}
#endif /* MCUBOOT_HW_ROLLBACK_PROT */
Expand All @@ -811,10 +825,16 @@ bootutil_img_validate(struct boot_loader_state *state,
FIH_SET(fih_rc, valid_signature);
#endif
#ifdef MCUBOOT_HW_ROLLBACK_PROT
if (FIH_EQ(security_counter_should_be_present, FIH_FAILURE)) {
goto skip_security_counter_check;
}

if (FIH_NOT_EQ(security_counter_valid, FIH_SUCCESS)) {
rc = -1;
goto out;
}

skip_security_counter_check:
#endif

#ifdef MCUBOOT_DECOMPRESS_IMAGES
Expand Down
67 changes: 66 additions & 1 deletion boot/bootutil/src/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,38 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
}

#ifdef MCUBOOT_HW_ROLLBACK_PROT
/**
* Checks if the specified image should have a security counter present on it or not
*
* @param image_index Index of the image to check.
*
* @return true if security counter should be present; false if otherwise
*/
fih_ret boot_nv_image_should_have_security_counter(uint32_t image_index)
{
#if defined(PM_S1_ADDRESS)
if (owner_nsib[image_index]) {
/*
* Downgrade prevention on S0/S1 image is managed by NSIB, which is a software (not
* hardware) check
*/
return FIH_FAILURE;
}
#endif

#if defined(CONFIG_SOC_NRF5340_CPUAPP) && CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER != -1
if (image_index == CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER) {
/*
* Downgrade prevention on network core image is managed by NSIB which is a software (not
* hardware) check
*/
return FIH_FAILURE;
}
#endif

return FIH_SUCCESS;
}

/**
* Updates the stored security counter value with the image's security counter
* value which resides in the given slot, only if it's greater than the stored
Expand All @@ -1324,6 +1356,26 @@ boot_update_security_counter(struct boot_loader_state *state, int slot, int hdr_
uint32_t img_security_cnt;
int rc;

#if defined(PM_S1_ADDRESS)
if (owner_nsib[BOOT_CURR_IMG(state)]) {
/*
* Downgrade prevention on S0/S1 image is managed by NSIB which is a software (not
* hardware) check
*/
return 0;
}
#endif

#if defined(CONFIG_SOC_NRF5340_CPUAPP) && CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER != -1
if (BOOT_CURR_IMG(state) == CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER) {
/*
* Downgrade prevention on network core image is managed by NSIB which is a software (not
* hardware) check
*/
return 0;
}
#endif

fap = BOOT_IMG_AREA(state, slot);
assert(fap != NULL);

Expand Down Expand Up @@ -2574,7 +2626,20 @@ check_downgrade_prevention(struct boot_loader_state *state)

#if defined(PM_S1_ADDRESS)
if (owner_nsib[BOOT_CURR_IMG(state)]) {
/* Downgrade prevention on S0/S1 image is managed by NSIB */
/*
* Downgrade prevention on S0/S1 image is managed by NSIB which is a software (not
* hardware) check
*/
return 0;
}
#endif

#if defined(CONFIG_SOC_NRF5340_CPUAPP) && CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER != -1
if (BOOT_CURR_IMG(state) == CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER) {
/*
* Downgrade prevention on network core image is managed by NSIB which is a software (not
* hardware) check
*/
return 0;
}
#endif
Expand Down