tools: default_image: Verify header size
PATCH 1/2 tools: default_image: Verify header size
Before reading image header, verify that image size is at least size of the image header.
--- tools/default_image.c | 6 ++++++ 1 file changed, 6 insertions(+)
tools/default_image.c +6 -0
@@ -49,6 +49,12 @@ static int image_verify_header(unsigned char *ptr, int image_size,struct legacy_img_hdr header;struct legacy_img_hdr *hdr = &header; +if (image_size <sizeof (struct legacy_img_hdr )) { +debug ("%s: Bad image size: \"%s\" is no valid image\n" , + params->cmdname, params->imagefile); +return -FDT_ERR_BADSTRUCTURE ; + } +/* * create copy of header so that we can blank out the * checksum field for checking - this can't be done
Signature · 1 line
2.20.1
PATCH 2/2 tools: default_image: Accept images with padding
If image file is stored on flash partition then it contains padding, which is not part of the image itself. Image data size is stored in the image header. So use image size from the header instead of expecting that total image file size is size of the header plus size of the image data. This allows dumpimage to parse image files with padding (e.g. dumped from flash partition).
--- tools/default_image.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/default_image.c +7 -1
@@ -81,7 +81,13 @@ static int image_verify_header(unsigned char *ptr, int image_size, } data = (const unsigned char *)ptr +sizeof (struct legacy_img_hdr ); - len = image_size -sizeof (struct legacy_img_hdr ); + len =image_get_data_size (hdr); + +if (image_size -sizeof (struct legacy_img_hdr ) < len) { +debug ("%s: Bad image size: \"%s\" is no valid image\n" , + params->cmdname, params->imagefile); +return -FDT_ERR_BADSTRUCTURE ; + } checksum =be32_to_cpu (hdr->ih_dcrc);if (crc32 (0 , data, len) != checksum) {
Signature · 1 line
2.20.1
Quoted 8 lines · Pali Rohár
Before reading image header, verify that image size is at least size of the image header.
Signed-off-by Pali Rohár <[email protected]>--- tools/default_image.c | 6 ++++++ 1 file changed, 6 insertions(+)
Re: [PATCH u-boot 2/2] tools: default_image: Accept images with padding
In reply to Pali RohárQuoted 8 lines · Pali Rohár
If image file is stored on flash partition then it contains padding, which is not part of the image itself. Image data size is stored in the image header. So use image size from the header instead of expecting that total image file size is size of the header plus size of the image data. This allows dumpimage to parse image files with padding (e.g. dumped from flash partition).
Quoted 4 lines
Signed-off-by Pali Rohár <[email protected]>--- tools/default_image.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
Re: [PATCH u-boot 2/2] tools: default_image: Accept images with padding
In reply to Pali RohárQuoted 9 lines · Pali Rohár
If image file is stored on flash partition then it contains padding, which is not part of the image itself. Image data size is stored in the image header. So use image size from the header instead of expecting that total image file size is size of the header plus size of the image data. This allows dumpimage to parse image files with padding (e.g. dumped from flash partition).
Signed-off-by Pali Rohár <[email protected]>Reviewed-by Simon Glass <[email protected]>
This breaks imx6q_bosch_acc imx6dl_mamoj as:./tools/mkimage: Failed to verify header of u-boot-ivt.img
now happens.
Signature · 1 line
Tom
Re: [PATCH u-boot 2/2] tools: default_image: Accept images with padding
Quoted 9 lines · On Monday 06 February 2023 14:43:07 Tom Rini wrote:
Quoted 7 lines · Pali Rohár
If image file is stored on flash partition then it contains padding, which is not part of the image itself. Image data size is stored in the image header. So use image size from the header instead of expecting that total image file size is size of the header plus size of the image data. This allows dumpimage to parse image files with padding (e.g. dumped from flash partition).
Quoted 7 lines
Quoted 2 lines
Signed-off-by Pali Rohár <[email protected]>Reviewed-by Simon Glass <[email protected]>This breaks
imx6q_bosch_accimx6dl_mamojas:./tools/mkimage: Failed to verify header of u-boot-ivt.imgnow happens.
Ah :-( That is because IH_TYPE_FIRMWARE_IVT support was hacked into default_image.c and its format is not compatible with other formats supported but default_image.c
I tested following patch with imx6q_bosch_acc_defconfig and it worked:
tools/default_image.c +4 -0
@@ -83,6 +83,10 @@ static int image_verify_header(unsigned char *ptr, int image_size, data = (const unsigned char *)ptr +sizeof (struct legacy_img_hdr ); len =image_get_data_size (hdr); +if (image_get_type (hdr) ==IH_TYPE_FIRMWARE_IVT ) +/* Add size of CSF minus IVT */ + len -=0x2060 -sizeof (flash_header_v2_t); +if (image_size -sizeof (struct legacy_img_hdr ) < len) {debug ("%s: Bad image size: \"%s\" is no valid image\n" , params->cmdname, params->imagefile);
That is why validation functions _must_ always be implemented.
Feel free to amend this chunk into patch 2/2 and check if it works also for you.
And looking at the image_extract_subimage() code in default_image.c and it is also broken for IH_TYPE_FIRMWARE_IVT. Well, I'm not going to fix image_extract_subimage() as it was broken before and nobody spotted it yet.
Re: [PATCH u-boot 2/2] tools: default_image: Accept images with padding
In reply to Tom RiniHi Tom,
Quoted 10 lines · Tom Rini
Quoted 7 lines · Pali Rohár
If image file is stored on flash partition then it contains padding, which is not part of the image itself. Image data size is stored in the image header. So use image size from the header instead of expecting that total image file size is size of the header plus size of the image data. This allows dumpimage to parse image files with padding (e.g. dumped from flash partition).
Quoted 7 lines
Quoted 2 lines
Signed-off-by Pali Rohár <[email protected]>Reviewed-by Simon Glass <[email protected]>This breaks
imx6q_bosch_accimx6dl_mamojas:./tools/mkimage: Failed to verify header of u-boot-ivt.imgnow happens.
I know these failures are a pain, but it is great to see the testing having such a big impact!
- Simon
Quoted 5 lines · Pali Rohár
Before reading image header, verify that image size is at least size of the image header.
Signed-off-by Pali Rohár <[email protected]>Reviewed-by Simon Glass <[email protected]>
Applied to u-boot/master, thanks!
Signature · 1 line
Tom
Re: [PATCH u-boot 2/2] tools: default_image: Accept images with padding
In reply to Pali RohárQuoted 42 lines · Pali Rohár
Quoted 9 lines · On Monday 06 February 2023 14:43:07 Tom Rini wrote:
Quoted 7 lines · Pali Rohár
If image file is stored on flash partition then it contains padding, which is not part of the image itself. Image data size is stored in the image header. So use image size from the header instead of expecting that total image file size is size of the header plus size of the image data. This allows dumpimage to parse image files with padding (e.g. dumped from flash partition).
Quoted 7 lines
Quoted 2 lines
Signed-off-by Pali Rohár <[email protected]>Reviewed-by Simon Glass <[email protected]>This breaks
imx6q_bosch_accimx6dl_mamojas:./tools/mkimage: Failed to verify header of u-boot-ivt.imgnow happens.
Ah :-( That is because
IH_TYPE_FIRMWARE_IVTsupport was hacked intodefault_image.cand its format is not compatible with other formats supported butdefault_image.cI tested following patch with
imx6q_bosch_acc_defconfigand it worked:tools/default_image.c +4
-0@@ -83,6 +83,10 @@ static int image_verify_header(unsigned char *ptr, int image_size, data = (const unsigned char *)ptr +sizeof (struct legacy_img_hdr ); len =image_get_data_size (hdr); +if (image_get_type (hdr) ==IH_TYPE_FIRMWARE_IVT ) +/* Add size of CSF minus IVT */ + len -=0x2060 -sizeof (flash_header_v2_t); +if (image_size -sizeof (struct legacy_img_hdr ) < len) {debug ("%s: Bad image size: \"%s\" is no valid image\n" , params->cmdname, params->imagefile);That is why validation functions _must_ always be implemented.
Quoted 7 lines
Feel free to amend this chunk into patch 2/2 and check if it works also for you.
And looking at the
image_extract_subimage()code indefault_image.cand it is also broken forIH_TYPE_FIRMWARE_IVT. Well, I'm not going to fiximage_extract_subimage()as it was broken before and nobody spotted it yet.
With this included now, the original patch is applied to u-boot/master now, thanks!
Signature · 1 line
Tom