Skip to content

Commit a7b1002

Browse files
committed
Fix image len bound check in update_ram.c
1 parent 5f13d7d commit a7b1002

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/update_ram.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ 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 (img_size > (WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE)) {
86+
wolfBoot_printf("Invalid image size %u at %p\n", img_size, src);
87+
return -1;
88+
}
8589

8690
/* Read the entire image into RAM */
8791
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)