diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2023-12-05 13:28:47 +0100 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2023-12-05 13:28:47 +0100 |
| commit | af8621db2d2de4675240dad0ff885f23dc33f518 (patch) | |
| tree | 6d3085ccc8bd161aacfd2d5d1b3c9f9a771ea33d /src | |
| parent | 72568c131dcfc9303de0a809e02290c7ac464663 (diff) | |
| download | zig-af8621db2d2de4675240dad0ff885f23dc33f518.tar.gz zig-af8621db2d2de4675240dad0ff885f23dc33f518.zip | |
elf: report error at the point where it is happening
Diffstat (limited to 'src')
| -rw-r--r-- | src/link/Elf.zig | 14 | ||||
| -rw-r--r-- | src/link/Elf/Object.zig | 15 |
2 files changed, 29 insertions, 0 deletions
diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 7c1e46dd89..859932e133 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -1760,6 +1760,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void { } const ParseError = error{ + LinkFail, UnknownFileType, InvalidCpuArch, OutOfMemory, @@ -1769,6 +1770,7 @@ const ParseError = error{ FileSystem, NotSupported, InvalidCharacter, + MalformedObject, } || LdScript.Error || std.os.AccessError || std.os.SeekError || std.fs.File.OpenError || std.fs.File.ReadError; fn parsePositional(self: *Elf, path: []const u8, must_link: bool, ctx: *ParseErrorCtx) ParseError!void { @@ -6057,6 +6059,7 @@ fn handleAndReportParseError( ) error{OutOfMemory}!void { const cpu_arch = self.base.options.target.cpu.arch; switch (err) { + error.LinkFail => {}, // already reported error.UnknownFileType => try self.reportParseError(path, "unknown file type", .{}), error.InvalidCpuArch => try self.reportParseError( path, @@ -6082,6 +6085,17 @@ fn reportParseError( try err.addNote(self, "while parsing {s}", .{path}); } +pub fn reportParseError2( + self: *Elf, + file_index: File.Index, + comptime format: []const u8, + args: anytype, +) error{OutOfMemory}!void { + var err = try self.addErrorWithNotes(1); + try err.addMsg(self, format, args); + try err.addNote(self, "while parsing {}", .{self.file(file_index).?.fmtPath()}); +} + const FormatShdrCtx = struct { elf_file: *Elf, shdr: elf.Elf64_Shdr, diff --git a/src/link/Elf/Object.zig b/src/link/Elf/Object.zig index 2bcc56b038..4f390f31fa 100644 --- a/src/link/Elf/Object.zig +++ b/src/link/Elf/Object.zig @@ -58,6 +58,17 @@ 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 + self.header.?.e_shnum * @sizeOf(elf.Elf64_Shdr)) + { + try elf_file.reportParseError2( + self.index, + "corrupted header: section header table extends past the end of file", + .{}, + ); + return error.LinkFail; + } + const shoff = math.cast(usize, self.header.?.e_shoff) orelse return error.Overflow; const shdrs = @as( [*]align(1) const elf.Elf64_Shdr, @@ -66,6 +77,10 @@ pub fn parse(self: *Object, elf_file: *Elf) !void { try self.shdrs.ensureTotalCapacityPrecise(gpa, shdrs.len); for (shdrs) |shdr| { + if (self.data.len < shdr.sh_offset or self.data.len < shdr.sh_offset + shdr.sh_size) { + try elf_file.reportParseError2(self.index, "corrupted section header", .{}); + return error.LinkFail; + } self.shdrs.appendAssumeCapacity(try ElfShdr.fromElf64Shdr(shdr)); } |
