Skip to content

Commit cea7f5d

Browse files
committed
Check fw_size when WOLFBOOT_FIXED_PARTITIONS is off
F/373
1 parent ec3e969 commit cea7f5d

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

src/image.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,14 @@ int wolfBoot_open_image_address(struct wolfBoot_image *img, uint8_t *image)
13171317
}
13181318
img->trailer = img->hdr + WOLFBOOT_PARTITION_SIZE;
13191319
#else
1320+
#ifdef WOLFBOOT_RAMBOOT_MAX_SIZE
1321+
if (img->fw_size > WOLFBOOT_RAMBOOT_MAX_SIZE) {
1322+
wolfBoot_printf("Image size %d > max %d\n",
1323+
(unsigned int)img->fw_size,
1324+
(unsigned int)WOLFBOOT_RAMBOOT_MAX_SIZE);
1325+
return -1;
1326+
}
1327+
#endif
13201328
if (img->hdr == NULL) {
13211329
img->hdr = image;
13221330
}

tools/unit-tests/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ TESTS:=unit-parser unit-extflash unit-string unit-spi-flash unit-aes128 \
4848
unit-enc-nvm-flagshome unit-delta unit-update-flash \
4949
unit-update-flash-enc unit-update-ram unit-pkcs11_store unit-psa_store unit-disk \
5050
unit-multiboot unit-boot-x86-fsp unit-qspi-flash unit-tpm-rsa-exp \
51+
unit-image-nopart \
5152
unit-tpm-blob
5253

5354
all: $(TESTS)
@@ -160,6 +161,11 @@ unit-sectorflags: ../../include/target.h unit-sectorflags.c
160161
unit-image: unit-image.c unit-common.c $(WOLFCRYPT_SRC)
161162
gcc -o $@ $^ $(CFLAGS) $(WOLFCRYPT_CFLAGS) $(LDFLAGS)
162163

164+
unit-image-nopart: ../../include/target.h unit-image.c unit-common.c $(WOLFCRYPT_SRC)
165+
gcc -o $@ unit-image.c unit-common.c $(WOLFCRYPT_SRC) \
166+
$(CFLAGS) $(WOLFCRYPT_CFLAGS) -DWOLFBOOT_NO_PARTITIONS -DMOCK_PARTITIONS \
167+
-DWOLFBOOT_RAMBOOT_MAX_SIZE=0x1000 $(LDFLAGS)
168+
163169
unit-image-rsa: CFLAGS += -DWOLFBOOT_SIGN_RSA2048
164170
unit-image-rsa: ../../include/target.h unit-image.c unit-common.c
165171
gcc -o $@ unit-image.c unit-common.c $(WOLFCRYPT_SRC) \

tools/unit-tests/unit-image.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ START_TEST(test_verify_authenticity_bad_siglen)
655655
END_TEST
656656
#endif
657657

658+
#ifdef WOLFBOOT_FIXED_PARTITIONS
658659
START_TEST(test_verify_integrity)
659660
{
660661
struct wolfBoot_image test_img;
@@ -682,7 +683,9 @@ START_TEST(test_verify_integrity)
682683
ck_assert_int_eq(ret, 0);
683684
}
684685
END_TEST
686+
#endif
685687

688+
#ifdef WOLFBOOT_FIXED_PARTITIONS
686689
START_TEST(test_open_image)
687690
{
688691
struct wolfBoot_image img;
@@ -747,6 +750,24 @@ START_TEST(test_open_image)
747750
ck_assert_int_eq(ret, -1);
748751
}
749752
END_TEST
753+
#else
754+
START_TEST(test_open_image_address_without_partitions_rejects_oversized_fw_size)
755+
{
756+
struct wolfBoot_image img;
757+
uint8_t image[IMAGE_HEADER_SIZE] = {0};
758+
int ret;
759+
760+
memset(&img, 0, sizeof(img));
761+
((uint32_t *)image)[0] = WOLFBOOT_MAGIC;
762+
((uint32_t *)image)[1] = WOLFBOOT_RAMBOOT_MAX_SIZE + 1;
763+
764+
ret = wolfBoot_open_image_address(&img, image);
765+
766+
ck_assert_int_eq(ret, -1);
767+
ck_assert_uint_eq(img.hdr_ok, 0);
768+
}
769+
END_TEST
770+
#endif
750771

751772

752773
Suite *wolfboot_suite(void)
@@ -794,14 +815,21 @@ Suite *wolfboot_suite(void)
794815
tcase_add_test(tcase_headers, test_headers);
795816
suite_add_tcase(s, tcase_headers);
796817

818+
#ifdef WOLFBOOT_FIXED_PARTITIONS
797819
TCase* tcase_verify_integrity = tcase_create("verify_integrity");
798820
tcase_set_timeout(tcase_verify_integrity, 20);
799821
tcase_add_test(tcase_verify_integrity, test_verify_integrity);
800822
suite_add_tcase(s, tcase_verify_integrity);
823+
#endif
801824

802825
TCase* tcase_open_image = tcase_create("open_image");
803826
tcase_set_timeout(tcase_open_image, 20);
827+
#ifdef WOLFBOOT_FIXED_PARTITIONS
804828
tcase_add_test(tcase_open_image, test_open_image);
829+
#else
830+
tcase_add_test(tcase_open_image,
831+
test_open_image_address_without_partitions_rejects_oversized_fw_size);
832+
#endif
805833
suite_add_tcase(s, tcase_open_image);
806834
#endif
807835
return s;

0 commit comments

Comments
 (0)