]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
bootctl: tweak DOS header magic check
authorGerd Hoffmann <kraxel@redhat.com>
Tue, 24 Jan 2023 09:28:25 +0000 (10:28 +0100)
committerGerd Hoffmann <kraxel@redhat.com>
Tue, 7 Mar 2023 07:14:46 +0000 (08:14 +0100)
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.

src/boot/bootctl-uki.c

index e304cad06ae1304cf7c7159bb0cb4b08dface950..261e687e3f8c2c17ff972f48c218e203c3e665f0 100644 (file)
@@ -31,11 +31,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");