Skip to content

Commit 63bad13

Browse files
authored
Merge pull request #706 from danielinux/update-ram-bounds
Fix image len bound check in update_ram.c
2 parents 5f13d7d + 00bf88b commit 63bad13

File tree

8 files changed

+97
-0
lines changed

8 files changed

+97
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ include/target.h: $(TARGET_H_TEMPLATE) FORCE
604604
sed -e "s/@WOLFBOOT_DTS_UPDATE_ADDRESS@/$(WOLFBOOT_DTS_UPDATE_ADDRESS)/g" | \
605605
sed -e "s/@WOLFBOOT_LOAD_ADDRESS@/$(WOLFBOOT_LOAD_ADDRESS)/g" | \
606606
sed -e "s/@WOLFBOOT_LOAD_DTS_ADDRESS@/$(WOLFBOOT_LOAD_DTS_ADDRESS)/g" | \
607+
sed -e "s/@WOLFBOOT_RAMBOOT_MAX_SIZE@/$(WOLFBOOT_RAMBOOT_MAX_SIZE)/g" | \
607608
sed -e "s/@WOLFBOOT_PARTITION_SELF_HEADER_ADDRESS@/$(WOLFBOOT_PARTITION_SELF_HEADER_ADDRESS)/g" \
608609
> $@
609610

config/examples/polarfire_mpfs250.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ WOLFBOOT_LOAD_ADDRESS?=0x8E000000
6262
# Partition layout for PolarFire SoC MPFS250T
6363
# Using update_disk loader we just need to specify the partition number or A/B
6464
WOLFBOOT_NO_PARTITIONS=1
65+
WOLFBOOT_RAMBOOT_MAX_SIZE=0x80000000
6566
CFLAGS_EXTRA+=-DBOOT_PART_A=1
6667
CFLAGS_EXTRA+=-DBOOT_PART_B=2
6768
# Speed up disk partition read (512KB chunks - max DMA size)

config/examples/raspi3-encrypted.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ PKA?=0
1010
WOLFTPM?=0
1111

1212
WOLFBOOT_NO_PARTITIONS=1
13+
WOLFBOOT_RAMBOOT_MAX_SIZE=0x20000000
1314
WOLFBOOT_PARTITION_BOOT_ADDRESS=0x140000
1415
WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x1140000
1516
WOLFBOOT_PARTITION_SWAP_ADDRESS=0xFFFFFFFF

config/examples/raspi3.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ NO_XIP?=1
1313
NO_QNX?=1
1414
WOLFBOOT_SECTOR_SIZE=0x400
1515
WOLFBOOT_NO_PARTITIONS=1
16+
WOLFBOOT_RAMBOOT_MAX_SIZE=0x20000000
1617
WOLFBOOT_LOAD_ADDRESS?=0x3080000
1718
WOLFBOOT_LOAD_DTS_ADDRESS?=0x400000

config/examples/versal_vmk180_sdcard.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ CROSS_COMPILE=aarch64-none-elf-
6666
# These are 0-based indices into the parsed partition array:
6767
# part[0]=boot, part[1]=OFP_A, part[2]=OFP_B, part[3]=rootfs
6868
WOLFBOOT_NO_PARTITIONS=1
69+
WOLFBOOT_RAMBOOT_MAX_SIZE=0x80000000
6970
CFLAGS_EXTRA+=-DBOOT_PART_A=1
7071
CFLAGS_EXTRA+=-DBOOT_PART_B=2
7172

include/target.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@
116116
/* Load address in RAM for staged OS (update_ram only) */
117117
#define WOLFBOOT_LOAD_ADDRESS @WOLFBOOT_LOAD_ADDRESS@
118118
#endif
119+
120+
/* Optional RAM-boot image size cap for targets without partitions */
121+
#define WOLFBOOT_RAMBOOT_MAX_SIZE @WOLFBOOT_RAMBOOT_MAX_SIZE@
119122
#define WOLFBOOT_LOAD_DTS_ADDRESS @WOLFBOOT_LOAD_DTS_ADDRESS@
120123

121124

src/update_ram.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ int wolfBoot_ramboot(struct wolfBoot_image *img, uint8_t *src, uint8_t *dst)
8282

8383
/* determine size of partition */
8484
img_size = wolfBoot_image_size((uint8_t*)dst);
85+
#if defined(WOLFBOOT_NO_PARTITIONS)
86+
# ifndef WOLFBOOT_RAMBOOT_MAX_SIZE
87+
# error "WOLFBOOT_RAMBOOT_MAX_SIZE required when WOLFBOOT_NO_PARTITIONS=1"
88+
# endif
89+
if (img_size > WOLFBOOT_RAMBOOT_MAX_SIZE) {
90+
wolfBoot_printf("Invalid image size %u at %p\n", img_size, src);
91+
return -1;
92+
}
93+
#elif defined(WOLFBOOT_PARTITION_SIZE)
94+
if (img_size > (WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE)) {
95+
wolfBoot_printf("Invalid image size %u at %p\n", img_size, src);
96+
return -1;
97+
}
98+
#endif
8599

86100
/* Read the entire image into RAM */
87101
wolfBoot_printf("Loading image %d bytes from %p to %p...",

tools/unit-tests/unit-update-ram.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,69 @@ START_TEST (test_empty_panic)
203203
END_TEST
204204

205205

206+
START_TEST (test_ramboot_invalid_header)
207+
{
208+
struct wolfBoot_image img;
209+
uint8_t bad_magic[4] = { 'G', 'O', 'L', 'F' };
210+
int ret;
211+
212+
reset_mock_stats();
213+
prepare_flash();
214+
ext_flash_unlock();
215+
ext_flash_write(WOLFBOOT_PARTITION_BOOT_ADDRESS, bad_magic, sizeof(bad_magic));
216+
ext_flash_lock();
217+
218+
memset(&img, 0, sizeof(img));
219+
ret = wolfBoot_ramboot(&img,
220+
(uint8_t *)WOLFBOOT_PARTITION_BOOT_ADDRESS, wolfboot_ram);
221+
ck_assert_int_eq(ret, -1);
222+
cleanup_flash();
223+
}
224+
END_TEST
225+
226+
START_TEST (test_ramboot_oversize_rejected)
227+
{
228+
struct wolfBoot_image img;
229+
uint32_t too_large = WOLFBOOT_PARTITION_SIZE;
230+
int ret;
231+
232+
reset_mock_stats();
233+
prepare_flash();
234+
add_payload(PART_BOOT, 1, TEST_SIZE_SMALL);
235+
236+
ext_flash_unlock();
237+
ext_flash_write(WOLFBOOT_PARTITION_BOOT_ADDRESS + 4,
238+
(const uint8_t *)&too_large, 4);
239+
ext_flash_lock();
240+
241+
memset(&img, 0, sizeof(img));
242+
ret = wolfBoot_ramboot(&img,
243+
(uint8_t *)WOLFBOOT_PARTITION_BOOT_ADDRESS, wolfboot_ram);
244+
ck_assert_int_eq(ret, -1);
245+
cleanup_flash();
246+
}
247+
END_TEST
248+
249+
START_TEST (test_ramboot_success)
250+
{
251+
struct wolfBoot_image img;
252+
int ret;
253+
254+
reset_mock_stats();
255+
prepare_flash();
256+
add_payload(PART_BOOT, 1, TEST_SIZE_SMALL);
257+
258+
memset(&img, 0, sizeof(img));
259+
ret = wolfBoot_ramboot(&img,
260+
(uint8_t *)WOLFBOOT_PARTITION_BOOT_ADDRESS, wolfboot_ram);
261+
ck_assert_int_eq(ret, 0);
262+
ck_assert_int_eq(img.not_ext, 1);
263+
ck_assert_int_eq(get_version_ramloaded(), 1);
264+
cleanup_flash();
265+
}
266+
END_TEST
267+
268+
206269
START_TEST (test_sunnyday_noupdate)
207270
{
208271
reset_mock_stats();
@@ -423,6 +486,9 @@ Suite *wolfboot_suite(void)
423486

424487
/* Test cases */
425488
TCase *empty_panic = tcase_create("Empty partition panic test");
489+
TCase *ramboot_invalid_header = tcase_create("Ramboot invalid header");
490+
TCase *ramboot_oversize = tcase_create("Ramboot oversize");
491+
TCase *ramboot_success = tcase_create("Ramboot success");
426492
TCase *sunnyday_noupdate =
427493
tcase_create("Sunny day test with no update available");
428494
TCase *forward_update_samesize =
@@ -446,6 +512,9 @@ Suite *wolfboot_suite(void)
446512

447513

448514
tcase_add_test(empty_panic, test_empty_panic);
515+
tcase_add_test(ramboot_invalid_header, test_ramboot_invalid_header);
516+
tcase_add_test(ramboot_oversize, test_ramboot_oversize_rejected);
517+
tcase_add_test(ramboot_success, test_ramboot_success);
449518
tcase_add_test(sunnyday_noupdate, test_sunnyday_noupdate);
450519
tcase_add_test(forward_update_samesize, test_forward_update_samesize);
451520
tcase_add_test(forward_update_tolarger, test_forward_update_tolarger);
@@ -463,6 +532,9 @@ Suite *wolfboot_suite(void)
463532

464533

465534
suite_add_tcase(s, empty_panic);
535+
suite_add_tcase(s, ramboot_invalid_header);
536+
suite_add_tcase(s, ramboot_oversize);
537+
suite_add_tcase(s, ramboot_success);
466538
suite_add_tcase(s, sunnyday_noupdate);
467539
suite_add_tcase(s, forward_update_samesize);
468540
suite_add_tcase(s, forward_update_tolarger);
@@ -480,6 +552,9 @@ Suite *wolfboot_suite(void)
480552

481553
/* Set timeout for tests */
482554
tcase_set_timeout(empty_panic, 5);
555+
tcase_set_timeout(ramboot_invalid_header, 5);
556+
tcase_set_timeout(ramboot_oversize, 5);
557+
tcase_set_timeout(ramboot_success, 5);
483558
tcase_set_timeout(sunnyday_noupdate, 5);
484559
tcase_set_timeout(forward_update_samesize, 5);
485560
tcase_set_timeout(forward_update_tolarger, 5);

0 commit comments

Comments
 (0)