Skip to content

Commit 3bd1796

Browse files
committed
[nrf noup] boot: bootutil: loader: Fix monotomic counter update issues
nrf-squash! [nrf noup] boot/../loader: skip downgrade prevention for s1/s0 Fixes 4 issues with monotomic counter usage: 1. Where the NSIB update skipped the check but would then wrongly update the monotomic counter after 2. Where a network core update on nRF5340 used the monotonic counter which only supports a single image 3. Where an NSIB update used the monotonic counter which only supports a single image 4. Where security counter validation was wrongly performed on other images against the main image security counter Signed-off-by: Jamie McCrae <[email protected]>
1 parent d4f3b7d commit 3bd1796

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

boot/bootutil/include/bootutil/security_cnt.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ extern "C" {
3939
*/
4040
fih_ret boot_nv_security_counter_init(void);
4141

42+
/**
43+
* Checks if the specified image should have a security counter present on it or not
44+
*
45+
* @param image_index Index of the image to check (from 0).
46+
*
47+
* @return FIH_SUCCESS if security counter should be present; FIH_FAILURE if otherwise
48+
*/
49+
fih_ret boot_nv_image_should_have_security_counter(uint32_t image_index);
50+
4251
/**
4352
* Reads the stored value of a given image's security counter.
4453
*

boot/bootutil/src/image_validate.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,15 @@ bootutil_img_validate(struct boot_loader_state *state,
527527
fih_int security_cnt = fih_int_encode(INT_MAX);
528528
uint32_t img_security_cnt = 0;
529529
FIH_DECLARE(security_counter_valid, FIH_FAILURE);
530+
FIH_DECLARE(security_counter_should_be_present, FIH_FAILURE);
531+
532+
FIH_CALL(boot_nv_image_should_have_security_counter, security_counter_should_be_present,
533+
image_index);
534+
if (FIH_NOT_EQ(security_counter_should_be_present, FIH_SUCCESS) &&
535+
FIH_NOT_EQ(security_counter_should_be_present, FIH_FAILURE)) {
536+
rc = -1;
537+
goto out;
538+
}
530539
#endif
531540

532541
#ifdef MCUBOOT_DECOMPRESS_IMAGES
@@ -773,6 +782,10 @@ bootutil_img_validate(struct boot_loader_state *state,
773782
goto out;
774783
}
775784

785+
if (FIH_EQ(security_counter_should_be_present, FIH_FAILURE)) {
786+
goto skip_security_counter_read;
787+
}
788+
776789
FIH_CALL(boot_nv_security_counter_get, fih_rc, image_index,
777790
&security_cnt);
778791
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
@@ -792,6 +805,7 @@ bootutil_img_validate(struct boot_loader_state *state,
792805

793806
/* The image's security counter has been successfully verified. */
794807
security_counter_valid = fih_rc;
808+
skip_security_counter_read:
795809
break;
796810
}
797811
#endif /* MCUBOOT_HW_ROLLBACK_PROT */
@@ -811,10 +825,16 @@ bootutil_img_validate(struct boot_loader_state *state,
811825
FIH_SET(fih_rc, valid_signature);
812826
#endif
813827
#ifdef MCUBOOT_HW_ROLLBACK_PROT
828+
if (FIH_EQ(security_counter_should_be_present, FIH_FAILURE)) {
829+
goto skip_security_counter_check;
830+
}
831+
814832
if (FIH_NOT_EQ(security_counter_valid, FIH_SUCCESS)) {
815833
rc = -1;
816834
goto out;
817835
}
836+
837+
skip_security_counter_check:
818838
#endif
819839

820840
#ifdef MCUBOOT_DECOMPRESS_IMAGES

boot/bootutil/src/loader.c

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,38 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
13031303
}
13041304

13051305
#ifdef MCUBOOT_HW_ROLLBACK_PROT
1306+
/**
1307+
* Checks if the specified image should have a security counter present on it or not
1308+
*
1309+
* @param image_index Index of the image to check.
1310+
*
1311+
* @return true if security counter should be present; false if otherwise
1312+
*/
1313+
fih_ret boot_nv_image_should_have_security_counter(uint32_t image_index)
1314+
{
1315+
#if defined(PM_S1_ADDRESS)
1316+
if (owner_nsib[image_index]) {
1317+
/*
1318+
* Downgrade prevention on S0/S1 image is managed by NSIB, which is a software (not
1319+
* hardware) check
1320+
*/
1321+
return FIH_FAILURE;
1322+
}
1323+
#endif
1324+
1325+
#if defined(CONFIG_SOC_NRF5340_CPUAPP) && CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER != -1
1326+
if (image_index == CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER) {
1327+
/*
1328+
* Downgrade prevention on network core image is managed by NSIB which is a software (not
1329+
* hardware) check
1330+
*/
1331+
return FIH_FAILURE;
1332+
}
1333+
#endif
1334+
1335+
return FIH_SUCCESS;
1336+
}
1337+
13061338
/**
13071339
* Updates the stored security counter value with the image's security counter
13081340
* value which resides in the given slot, only if it's greater than the stored
@@ -1324,6 +1356,26 @@ boot_update_security_counter(struct boot_loader_state *state, int slot, int hdr_
13241356
uint32_t img_security_cnt;
13251357
int rc;
13261358

1359+
#if defined(PM_S1_ADDRESS)
1360+
if (owner_nsib[BOOT_CURR_IMG(state)]) {
1361+
/*
1362+
* Downgrade prevention on S0/S1 image is managed by NSIB which is a software (not
1363+
* hardware) check
1364+
*/
1365+
return 0;
1366+
}
1367+
#endif
1368+
1369+
#if defined(CONFIG_SOC_NRF5340_CPUAPP) && CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER != -1
1370+
if (BOOT_CURR_IMG(state) == CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER) {
1371+
/*
1372+
* Downgrade prevention on network core image is managed by NSIB which is a software (not
1373+
* hardware) check
1374+
*/
1375+
return 0;
1376+
}
1377+
#endif
1378+
13271379
fap = BOOT_IMG_AREA(state, slot);
13281380
assert(fap != NULL);
13291381

@@ -2574,7 +2626,20 @@ check_downgrade_prevention(struct boot_loader_state *state)
25742626

25752627
#if defined(PM_S1_ADDRESS)
25762628
if (owner_nsib[BOOT_CURR_IMG(state)]) {
2577-
/* Downgrade prevention on S0/S1 image is managed by NSIB */
2629+
/*
2630+
* Downgrade prevention on S0/S1 image is managed by NSIB which is a software (not
2631+
* hardware) check
2632+
*/
2633+
return 0;
2634+
}
2635+
#endif
2636+
2637+
#if defined(CONFIG_SOC_NRF5340_CPUAPP) && CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER != -1
2638+
if (BOOT_CURR_IMG(state) == CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER) {
2639+
/*
2640+
* Downgrade prevention on network core image is managed by NSIB which is a software (not
2641+
* hardware) check
2642+
*/
25782643
return 0;
25792644
}
25802645
#endif

0 commit comments

Comments
 (0)