boot: Introduce file_size and use it when we're working with file_offset

When we're reading a section from disk, use file_size to use the
size on disk instead of the size in memory.
This commit is contained in:
Daan De Meyer
2024-10-09 20:36:57 +02:00
parent 78f6571505
commit 8c5697182a
3 changed files with 15 additions and 8 deletions
+5 -5
View File
@@ -1928,8 +1928,8 @@ static bool is_sd_boot(EFI_FILE *root_dir, const char16_t *loader_path) {
if (vector.memory_size != sizeof(SD_MAGIC))
return false;
err = file_handle_read(handle, vector.file_offset, vector.memory_size, &content, &read);
if (err != EFI_SUCCESS || vector.memory_size != read)
err = file_handle_read(handle, vector.file_offset, vector.file_size, &content, &read);
if (err != EFI_SUCCESS || vector.file_size != read)
return false;
return memcmp(content, SD_MAGIC, sizeof(SD_MAGIC)) == 0;
@@ -2210,7 +2210,7 @@ static void boot_entry_add_type2(
err = file_handle_read(
handle,
sections[SECTION_OSREL].file_offset,
sections[SECTION_OSREL].memory_size,
sections[SECTION_OSREL].file_size,
&content,
/* ret_size= */ NULL);
if (err != EFI_SUCCESS)
@@ -2281,7 +2281,7 @@ static void boot_entry_add_type2(
err = file_handle_read(
handle,
sections[SECTION_PROFILE].file_offset,
sections[SECTION_PROFILE].memory_size,
sections[SECTION_PROFILE].file_size,
&content,
/* ret_size= */ NULL);
if (err != EFI_SUCCESS)
@@ -2348,7 +2348,7 @@ static void boot_entry_add_type2(
err = file_handle_read(
handle,
sections[SECTION_CMDLINE].file_offset,
sections[SECTION_CMDLINE].memory_size,
sections[SECTION_CMDLINE].file_size,
&content,
&cmdline_len);
if (err == EFI_SUCCESS) {
+9 -3
View File
@@ -186,13 +186,13 @@ static void pe_locate_sections(
/* Overflow check: ignore sections that are impossibly large, relative to the file
* address for the section. */
size_t size_max = SIZE_MAX - j->PointerToRawData;
if ((size_t) j->VirtualSize > size_max)
if ((size_t) j->SizeOfRawData > size_max)
continue;
/* Overflow check: ignore sections that are impossibly large, given the virtual
* address for the section */
size_max = SIZE_MAX - j->VirtualAddress;
if (j->VirtualSize > size_max)
if ((size_t) j->VirtualSize > size_max)
continue;
/* 2nd overflow check: ignore sections that are impossibly large also taking the
@@ -209,8 +209,14 @@ static void pe_locate_sections(
/* At this time, the sizes and offsets have been validated. Store them away */
sections[i] = (PeSectionVector) {
.memory_size = j->VirtualSize,
.file_offset = j->PointerToRawData,
.memory_offset = j->VirtualAddress,
/* VirtualSize can be bigger than SizeOfRawData when the section requires
* uninitialized data. It can also be smaller than SizeOfRawData when there's
* no need for uninitialized data as SizeOfRawData is aligned to
* FileAlignment and VirtualSize isn't. The actual data that's read from disk
* is the minimum of these two fields. */
.file_size = MIN(j->SizeOfRawData, j->VirtualSize),
.file_offset = j->PointerToRawData,
};
/* First matching section wins, ignore the rest */
+1
View File
@@ -22,6 +22,7 @@ typedef struct PeSectionHeader {
typedef struct PeSectionVector {
size_t memory_size; /* Size of the section in memory (corresponds to VirtualSize field) */
size_t memory_offset; /* Offset in memory, relative to base address */
uint64_t file_size; /* Amount of bytes of the section read from disk (possibly aligned to FileAlignment in case VirtualSize > SizeOfRawData). */
uint64_t file_offset; /* Offset on disk, relative to beginning of file */
} PeSectionVector;