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 {...@@ -1205,12 +1205,14 @@ pub const Coff = struct {
1205 return .{ .buffer = self.data[offset..][0..size] };1205 return .{ .buffer = self.data[offset..][0..size] };
1206 }1206 }
12071207
1208 pub fn getStrtab(self: *const Coff) ?Strtab {1208 pub fn getStrtab(self: *const Coff) error{InvalidStrtabSize}!?Strtab {
1209 const coff_header = self.getCoffHeader();1209 const coff_header = self.getCoffHeader();
1210 if (coff_header.pointer_to_symbol_table == 0) return null;1210 if (coff_header.pointer_to_symbol_table == 0) return null;
12111211
1212 const offset = coff_header.pointer_to_symbol_table + Symbol.sizeOf() * coff_header.number_of_symbols;1212 const offset = coff_header.pointer_to_symbol_table + Symbol.sizeOf() * coff_header.number_of_symbols;
1213 const size = mem.readIntLittle(u32, self.data[offset..][0..4]);1213 const size = mem.readIntLittle(u32, self.data[offset..][0..4]);
1214 if ((offset + size) > self.data.len) return error.InvalidStrtabSize;
1215
1214 return Strtab{ .buffer = self.data[offset..][0..size] };1216 return Strtab{ .buffer = self.data[offset..][0..size] };
1215 }1217 }
12161218
...@@ -1235,9 +1237,9 @@ pub const Coff = struct {...@@ -1235,9 +1237,9 @@ pub const Coff = struct {
1235 return out_buff;1237 return out_buff;
1236 }1238 }
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 {
1239 const name = sect_hdr.getName() orelse blk: {1241 const name = sect_hdr.getName() orelse blk: {
1240 const strtab = self.getStrtab().?;1242 const strtab = (try self.getStrtab()).?;
1241 const name_offset = sect_hdr.getNameOffset().?;1243 const name_offset = sect_hdr.getNameOffset().?;
1242 break :blk strtab.get(name_offset);1244 break :blk strtab.get(name_offset);
1243 };1245 };
...@@ -1246,7 +1248,10 @@ pub const Coff = struct {...@@ -1246,7 +1248,10 @@ pub const Coff = struct {
12461248
1247 pub fn getSectionByName(self: *const Coff, comptime name: []const u8) ?*align(1) const SectionHeader {1249 pub fn getSectionByName(self: *const Coff, comptime name: []const u8) ?*align(1) const SectionHeader {
1248 for (self.getSectionHeaders()) |*sect| {1250 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)) {
1250 return sect;1255 return sect;
1251 }1256 }
1252 }1257 }