Skip to content

Commit

Permalink
bootctl: tweak DOS header magic check
Browse files Browse the repository at this point in the history
Read the magic first, try reading the full DOS exe header only in case
the magic check succeeds.

This avoids throwing an header read error on small dummy files as used
by test-kernel-install.

(cherry picked from commit 78088b8)

Related: RHEL-16354
  • Loading branch information
kraxel authored and keszybz committed Nov 23, 2023
1 parent c05b78c commit 458d84a
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/boot/bootctl-uki.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ static int pe_sections(FILE *uki, struct PeSectionHeader **ret, size_t *ret_n) {
assert(ret_n);

items = fread(&dos, 1, sizeof(dos), uki);
if (items != sizeof(dos))
return log_error_errno(SYNTHETIC_ERRNO(EIO), "DOS header read error");
if (items < sizeof(dos.Magic))
return log_error_errno(SYNTHETIC_ERRNO(EIO), "File is smaller than DOS magic (got %"PRIu64" of %zu bytes)",
items, sizeof(dos.Magic));
if (memcmp(dos.Magic, dos_file_magic, sizeof(dos_file_magic)) != 0)
goto no_sections;

if (items != sizeof(dos))
return log_error_errno(SYNTHETIC_ERRNO(EIO), "File is smaller than DOS header (got %"PRIu64" of %zu bytes)",
items, sizeof(dos));

if (fseek(uki, le32toh(dos.ExeHeader), SEEK_SET) < 0)
return log_error_errno(errno, "seek to PE header");

Expand Down

0 comments on commit 458d84a

Please sign in to comment.