Skip to content

Commit c26d9f6

Browse files
leroycepandrewrk
authored andcommitted
Read dynstr starting at rpath offset
Since we know the offset, we may as well read starting there. Still expects rpath to fit in 4096 bytes; that might be worth fixing in the future. Fixes issue #12112
1 parent b93a388 commit c26d9f6

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

lib/std/zig/system/NativeTargetInfo.zig

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,12 +653,17 @@ pub fn abiAndDynamicLinkerFromFile(
653653
} else null;
654654

655655
if (dynstr) |ds| {
656-
const strtab_len = std.math.min(ds.size, strtab_buf.len);
657-
const strtab_read_len = try preadMin(file, &strtab_buf, ds.offset, strtab_len);
658-
const strtab = strtab_buf[0..strtab_read_len];
659656
// TODO this pointer cast should not be necessary
660657
const rpoff_usize = std.math.cast(usize, rpoff) orelse return error.InvalidElfFile;
661-
const rpath_list = mem.sliceTo(std.meta.assumeSentinel(strtab[rpoff_usize..].ptr, 0), 0);
658+
if (rpoff_usize > ds.size) return error.InvalidElfFile;
659+
const rpoff_file = ds.offset + rpoff_usize;
660+
const rp_max_size = ds.size - rpoff_usize;
661+
662+
const strtab_len = std.math.min(rp_max_size, strtab_buf.len);
663+
const strtab_read_len = try preadMin(file, &strtab_buf, rpoff_file, strtab_len);
664+
const strtab = strtab_buf[0..strtab_read_len];
665+
666+
const rpath_list = mem.sliceTo(std.meta.assumeSentinel(strtab.ptr, 0), 0);
662667
var it = mem.tokenize(u8, rpath_list, ":");
663668
while (it.next()) |rpath| {
664669
var dir = fs.cwd().openDir(rpath, .{}) catch |err| switch (err) {

0 commit comments

Comments
 (0)