Skip to content

Commit

Permalink
elf: do basic input object file verification
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Dec 5, 2023
1 parent d1eb222 commit c07cf2f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Elf/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ pub fn parse(self: *Object, elf_file: *Elf) !void {

const gpa = elf_file.base.allocator;

if (self.data.len < self.header.?.e_shoff or
self.data.len < self.header.?.e_shoff + @as(u64, @intCast(self.header.?.e_shnum)) * @sizeOf(elf.Elf64_Shdr))
{
return elf_file.base.fatal("{}: corrupt header: section header table extends past the end of file", .{
self.fmtPath(),
});
}

const shdrs = @as(
[*]align(1) const elf.Elf64_Shdr,
@ptrCast(self.data.ptr + self.header.?.e_shoff),
Expand All @@ -68,7 +76,9 @@ pub fn parse(self: *Object, elf_file: *Elf) !void {
self.first_global = shdr.sh_info;

const symtab = self.getShdrContents(index);
const nsyms = @divExact(symtab.len, @sizeOf(elf.Elf64_Sym));
const nsyms = math.divExact(usize, symtab.len, @sizeOf(elf.Elf64_Sym)) catch {
return elf_file.base.fatal("{}: symbol table not evenly divisible", .{self.fmtPath()});
};
self.symtab = @as([*]align(1) const elf.Elf64_Sym, @ptrCast(symtab.ptr))[0..nsyms];
self.strtab = self.getShdrContents(@as(u16, @intCast(shdr.sh_link)));
}
Expand Down
9 changes: 9 additions & 0 deletions src/Elf/SharedObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ pub fn parse(self: *SharedObject, elf_file: *Elf) !void {
const reader = stream.reader();

self.header = try reader.readStruct(elf.Elf64_Ehdr);

if (self.data.len < self.header.?.e_shoff or
self.data.len < self.header.?.e_shoff + @as(u64, @intCast(self.header.?.e_shnum)) * @sizeOf(elf.Elf64_Shdr))
{
return elf_file.base.fatal("{s}: corrupt header: section header table extends past the end of file", .{
self.path,
});
}

const shdrs = self.getShdrs();

var dynsym_index: ?u16 = null;
Expand Down

0 comments on commit c07cf2f

Please sign in to comment.