diff options
author | Himbeer <himbeer@disroot.org> | 2024-06-21 11:19:47 +0200 |
---|---|---|
committer | Himbeer <himbeer@disroot.org> | 2024-06-21 11:19:47 +0200 |
commit | 986c51e032c886e2bdd461414112ae7a79be43f0 (patch) | |
tree | 56e3f78dc65bd795855bad142c2e2a185e9f05dc /src | |
parent | 88ee047a24518161478dc1f79fb95f3343bc642a (diff) |
process: Use ELF PHDR filesz instead of memsz where applicable (checked for consistency)
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); |