diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/process.zig | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/src/lib/process.zig b/src/lib/process.zig index 2a3ad73..641de7e 100644 --- a/src/lib/process.zig +++ b/src/lib/process.zig @@ -30,6 +30,7 @@ pub const ExeError = error{ BadArch, BadBitLen, NotStaticExe, + SizeMismatch, BranchPerms, }; @@ -150,11 +151,12 @@ pub fn create(allocator: std.mem.Allocator, elf_buf: []align(@alignOf(elf.Elf64_ var it = hdr.program_header_iterator(parse_source); while (try it.next()) |phdr| { if (phdr.p_type != elf.PT_LOAD) continue; + if (phdr.p_filesz == 0) continue; if (phdr.p_memsz == 0) continue; + if (phdr.p_filesz != phdr.p_memsz) return ExeError.SizeMismatch; // fixme: Could crash (out-of-bounds read). - // fixme: Use filesz instead of memsz for elf_buf and confirm that they're equal. - @memcpy(pages[phdr.p_offset .. phdr.p_offset + phdr.p_memsz], elf_buf[phdr.p_offset .. phdr.p_offset + phdr.p_memsz]); + @memcpy(pages[phdr.p_offset .. phdr.p_offset + phdr.p_memsz], elf_buf[phdr.p_offset .. phdr.p_offset + phdr.p_filesz]); const memsz_aligned = std.mem.alignForwardLog2(phdr.p_memsz, paging.log2_page_size); const num_mappings = @divExact(memsz_aligned, paging.page_size); |