Skip to content

Commit 8238033

Browse files
committed
[lib][uefi] Add error checking for certain block reads
1 parent 83b099e commit 8238033

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

lib/uefi/pe.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct IMAGE_DOS_HEADER { // DOS .EXE header
6363
u16 e_res2[10]; // Reserved words
6464
u32 e_lfanew; // File address of new exe header
6565

66+
// ASCII "PE\x0\x0"
6667
constexpr bool CheckMagic() const { return LE32(e_magic) == 0x5A4D; }
6768
IMAGE_NT_HEADERS64 *GetPEHeader() {
6869
auto address = reinterpret_cast<char *>(this);

lib/uefi/uefi.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ namespace {
5151
constexpr auto EFI_SYSTEM_TABLE_SIGNATURE =
5252
static_cast<u64>(0x5453595320494249ULL);
5353

54-
// ASCII "PE\x0\x0"
55-
5654
using EfiEntry = int (*)(void *, struct EfiSystemTable *);
5755

5856
template <typename T>
@@ -103,12 +101,22 @@ int load_sections_and_execute(bdev_t *dev,
103101
}
104102
memset(image_base, 0, virtual_size);
105103
DEFER { free_pages(image_base, virtual_size / PAGE_SIZE); };
106-
bio_read(dev, image_base, 0, section_header[0].PointerToRawData);
104+
ssize_t bytes_reads =
105+
bio_read(dev, image_base, 0, section_header[0].PointerToRawData);
106+
if (bytes_reads != section_header[0].SizeOfRawData) {
107+
printf("Failed to read section %s\n", section_header[0].Name);
108+
return ERR_IO;
109+
}
107110

108111
for (size_t i = 0; i < sections; i++) {
109112
const auto &section = section_header[i];
110-
bio_read(dev, image_base + section.VirtualAddress, section.PointerToRawData,
111-
section.SizeOfRawData);
113+
ssize_t bytes_read =
114+
bio_read(dev, image_base + section.VirtualAddress,
115+
section.PointerToRawData, section.SizeOfRawData);
116+
if (bytes_read != section.SizeOfRawData) {
117+
printf("Failed to read section %s %zu\n", section.Name, bytes_read);
118+
return ERR_IO;
119+
}
112120
}
113121
printf("Relocating image from 0x%llx to %p\n", optional_header->ImageBase,
114122
image_base);

0 commit comments

Comments
 (0)