authorgravatar for SonnyBonds@noreply.codeberg.orgAnders Stenberg <SonnyBonds@noreply.codeberg.org> 2026-05-12 20:43:37+02:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-05-17 18:55:27+01:00
log9ba45527931e9e43a77d42a52b63a0203e029bd0
tree8ec8496adea59f0124adbad1c0eb01e246d28902
parent97fe49a80f1aa24ebd7101635367c31bfb563078
signaturelock-open Commit is signed but in an unrecognized format.

Elf2: avoid overflow in shentsize * shnum calculations


1 files changed, 3 insertions(+), 3 deletions(-)

src/link/Elf2.zig+3-3
...@@ -2091,7 +2091,7 @@ fn initHeaders(...@@ -2091,7 +2091,7 @@ fn initHeaders(
2091 }2091 }
20922092
2093 assert(elf.ni.shdr == try elf.mf.addLastChildNode(gpa, elf.ni.file, .{2093 assert(elf.ni.shdr == try elf.mf.addLastChildNode(gpa, elf.ni.file, .{
2094 .size = elf.ehdrField(.shentsize) * elf.ehdrField(.shnum),2094 .size = @as(u64, elf.ehdrField(.shentsize)) * @as(u64, elf.ehdrField(.shnum)),
2095 .alignment = elf.mf.flags.block_size,2095 .alignment = elf.mf.flags.block_size,
2096 .moved = true,2096 .moved = true,
2097 .resized = true,2097 .resized = true,
...@@ -3076,7 +3076,7 @@ fn loadObject(...@@ -3076,7 +3076,7 @@ fn loadObject(
3076 if (ehdr.machine != elf.ehdrField(.machine))3076 if (ehdr.machine != elf.ehdrField(.machine))
3077 return diags.failParse(path, "bad machine", .{});3077 return diags.failParse(path, "bad machine", .{});
3078 if (ehdr.shoff == 0 or ehdr.shnum <= 1) return;3078 if (ehdr.shoff == 0 or ehdr.shnum <= 1) return;
3079 if (ehdr.shoff + ehdr.shentsize * ehdr.shnum > fl.size)3079 if (ehdr.shoff + @as(u64, ehdr.shentsize) * @as(u64, ehdr.shnum) > fl.size)
3080 return diags.failParse(path, "bad section header location", .{});3080 return diags.failParse(path, "bad section header location", .{});
3081 if (ehdr.shentsize < @sizeOf(ElfN.Shdr))3081 if (ehdr.shentsize < @sizeOf(ElfN.Shdr))
3082 return diags.failParse(path, "unsupported shentsize", .{});3082 return diags.failParse(path, "unsupported shentsize", .{});
...@@ -3638,7 +3638,7 @@ fn addSection(elf: *Elf, segment_ni: MappedFile.Node.Index, opts: struct {...@@ -3638,7 +3638,7 @@ fn addSection(elf: *Elf, segment_ni: MappedFile.Node.Index, opts: struct {
3638 },3638 },
3639 };3639 };
3640 assert(shndx < @intFromEnum(Section.Index.LORESERVE));3640 assert(shndx < @intFromEnum(Section.Index.LORESERVE));
3641 break :shndx .{ @enumFromInt(shndx), elf.targetLoad(&ehdr.shentsize) * shnum };3641 break :shndx .{ @enumFromInt(shndx), @as(u64, elf.targetLoad(&ehdr.shentsize)) * @as(u64, shnum) };
3642 },3642 },
3643 };3643 };
3644 _, const shdr_node_size = elf.ni.shdr.location(&elf.mf).resolve(&elf.mf);3644 _, const shdr_node_size = elf.ni.shdr.location(&elf.mf).resolve(&elf.mf);