authorgravatar for rohlemF@gmail.comRohlem <rohlemF@gmail.com> 2023-04-10 22:41:34+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-27 10:21:12-07:00
log7e1af51c4d04b82bd95323f46b8976deed90aad8
treebe64dfaacc0bc496f4b850c67ea9d58e1e74ca9b
parent775da34268b8133eae47c1840a38e922189dc0f1

`std.coff`: check strtab lengths against `data` length

Fixes illegal behavior. Invalid-length sections are now skipped in `Coff.getSectionByName`.

1 files changed, 9 insertions(+), 4 deletions(-)

lib/std/coff.zig+9-4
......@@ -1205,12 +1205,14 @@ pub const Coff = struct {
12051205 return .{ .buffer = self.data[offset..][0..size] };
12061206 }
12071207
1208 pub fn getStrtab(self: *const Coff) ?Strtab {
1208 pub fn getStrtab(self: *const Coff) error{InvalidStrtabSize}!?Strtab {
12091209 const coff_header = self.getCoffHeader();
12101210 if (coff_header.pointer_to_symbol_table == 0) return null;
12111211
12121212 const offset = coff_header.pointer_to_symbol_table + Symbol.sizeOf() * coff_header.number_of_symbols;
12131213 const size = mem.readIntLittle(u32, self.data[offset..][0..4]);
1214 if ((offset + size) > self.data.len) return error.InvalidStrtabSize;
1215
12141216 return Strtab{ .buffer = self.data[offset..][0..size] };
12151217 }
12161218
......@@ -1235,9 +1237,9 @@ pub const Coff = struct {
12351237 return out_buff;
12361238 }
12371239
1238 pub fn getSectionName(self: *const Coff, sect_hdr: *align(1) const SectionHeader) []const u8 {
1240 pub fn getSectionName(self: *const Coff, sect_hdr: *align(1) const SectionHeader) error{InvalidStrtabSize}![]const u8 {
12391241 const name = sect_hdr.getName() orelse blk: {
1240 const strtab = self.getStrtab().?;
1242 const strtab = (try self.getStrtab()).?;
12411243 const name_offset = sect_hdr.getNameOffset().?;
12421244 break :blk strtab.get(name_offset);
12431245 };
......@@ -1246,7 +1248,10 @@ pub const Coff = struct {
12461248
12471249 pub fn getSectionByName(self: *const Coff, comptime name: []const u8) ?*align(1) const SectionHeader {
12481250 for (self.getSectionHeaders()) |*sect| {
1249 if (mem.eql(u8, self.getSectionName(sect), name)) {
1251 const section_name = self.getSectionName(sect) catch |e| switch (e) {
1252 error.InvalidStrtabSize => continue, //ignore invalid(?) strtab entries - see also GitHub issue #15238
1253 };
1254 if (mem.eql(u8, section_name, name)) {
12501255 return sect;
12511256 }
12521257 }