Skip to content

Commit 334b6a6

Browse files
committed
boot: add precise check of the image size
It is possible that image in the slot is so big that MCUboot swap metadata will interfere with its content during the swap operation. This patch introduces additional check to the image validation procedure. Signed-off-by: Andrzej Puzdrowski <[email protected]>
1 parent b22eb6a commit 334b6a6

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

boot/bootutil/src/bootutil_misc.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,27 @@ boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
387387
return 0;
388388
}
389389
#endif
390+
391+
uint32_t bootutil_max_image_size(const struct flash_area *fap)
392+
{
393+
#if defined(MCUBOOT_SWAP_USING_SCRATCH)
394+
return boot_status_off(fap);
395+
#elif defined(MCUBOOT_SWAP_USING_MOVE)
396+
struct flash_sector sector;
397+
/* get the last sector offset */
398+
int rc = flash_area_sector_from_off(boot_status_off(fap), &sector);
399+
if (rc) {
400+
BOOT_LOG_ERR("Unable to determine flash sector of the image trailer");
401+
return 0; /* Returning of zero here should cause any check which uses
402+
* this value to fail.
403+
*/
404+
}
405+
return flash_sector_get_off(&sector);
406+
#elif defined(MCUBOOT_OVERWRITE_ONLY)
407+
return boot_swap_info_off(fap);
408+
#elif defined(MCUBOOT_DIRECT_XIP)
409+
return boot_swap_info_off(fap);
410+
#elif defined(MCUBOOT_RAM_LOAD)
411+
return boot_swap_info_off(fap);
412+
#endif
413+
}

boot/bootutil/src/bootutil_priv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ struct bootsim_ram_info *bootsim_get_ram_info(void);
463463
(flash_area_read((fap), (start), (output), (size)))
464464
#endif /* MCUBOOT_RAM_LOAD */
465465

466+
uint32_t bootutil_max_image_size(const struct flash_area *fap);
467+
466468
#ifdef __cplusplus
467469
}
468470
#endif

boot/bootutil/src/image_validate.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
376376
goto out;
377377
}
378378

379+
if (it.tlv_end > bootutil_max_image_size(fap)) {
380+
rc = -1;
381+
goto out;
382+
}
383+
379384
/*
380385
* Traverse through all of the TLVs, performing any checks we know
381386
* and are able to do.

0 commit comments

Comments
 (0)