Skip to content

Commit 24f1378

Browse files
nvlsianpupennam
authored andcommitted
loader: Allow image header bigger than 1 KB for encrypted images
boot_copy_region() was written so it assumes that the image header must fit int the intermediary buffer of 1 KB size. A bigger header will cause a overflow in calculation of size of data chunk to be decrypted. This patch allow to use header bigger than that buffer size and mitigate the limitation described above. Signed-off-by: Andrzej Puzdrowski <[email protected]>
1 parent ce82455 commit 24f1378

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

boot/bootutil/src/loader.c

+24-18
Original file line numberDiff line numberDiff line change
@@ -965,33 +965,39 @@ boot_copy_region(struct boot_loader_state *state,
965965
}
966966
#endif
967967
if (IS_ENCRYPTED(hdr)) {
968-
blk_sz = chunk_sz;
969-
idx = 0;
970968
if (off + bytes_copied < hdr->ih_hdr_size) {
971969
/* do not decrypt header */
972-
blk_off = 0;
973-
if (hdr->ih_hdr_size < chunk_sz) {
974-
blk_sz = chunk_sz - hdr->ih_hdr_size;
970+
if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size) {
971+
/* The lower part of the chunk contains header data */
972+
blk_off = 0;
973+
blk_sz = chunk_sz - (hdr->ih_hdr_size - off -
974+
bytes_copied);
975+
idx = hdr->ih_hdr_size - off - bytes_copied;
975976
} else {
976-
blk_sz = 0;
977+
/* The chunk contains exclusively header data */
978+
blk_sz = 0; /* nothing to decrypt */
977979
}
978-
/* FIXME: idx variable could be grater than buffer size */
979-
idx = hdr->ih_hdr_size;
980980
} else {
981+
idx = 0;
982+
blk_sz = chunk_sz;
981983
blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
982984
}
983-
tlv_off = BOOT_TLV_OFF(hdr);
984-
if (off + bytes_copied + chunk_sz > tlv_off) {
985-
/* do not decrypt TLVs */
986-
if (off + bytes_copied >= tlv_off) {
987-
blk_sz = 0;
988-
} else {
989-
blk_sz = tlv_off - (off + bytes_copied);
985+
986+
if (blk_sz > 0)
987+
{
988+
tlv_off = BOOT_TLV_OFF(hdr);
989+
if (off + bytes_copied + chunk_sz > tlv_off) {
990+
/* do not decrypt TLVs */
991+
if (off + bytes_copied >= tlv_off) {
992+
blk_sz = 0;
993+
} else {
994+
blk_sz = tlv_off - (off + bytes_copied);
995+
}
990996
}
997+
boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
998+
(off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
999+
blk_off, &buf[idx]);
9911000
}
992-
boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
993-
(off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
994-
blk_off, &buf[idx]);
9951001
}
9961002
}
9971003
#endif

0 commit comments

Comments
 (0)