Skip to content

Package fetch: add executable detection for Mach-O file headers #21555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions src/Package/Fetch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1789,12 +1789,9 @@ test {
_ = UnpackResult;
}

// Detects executable header: ELF magic header or shebang line.
// Detects executable header: ELF or Macho-O magic header or shebang line.
const FileHeader = struct {
const elf_magic = std.elf.MAGIC;
const shebang = "#!";

header: [@max(elf_magic.len, shebang.len)]u8 = undefined,
header: [4]u8 = undefined,
bytes_read: usize = 0,

pub fn update(self: *FileHeader, buf: []const u8) void {
Expand All @@ -1804,22 +1801,46 @@ const FileHeader = struct {
self.bytes_read += n;
}

fn isScript(self: *FileHeader) bool {
const shebang = "#!";
return std.mem.eql(u8, self.header[0..@min(self.bytes_read, shebang.len)], shebang);
}

fn isElf(self: *FileHeader) bool {
const elf_magic = std.elf.MAGIC;
return std.mem.eql(u8, self.header[0..@min(self.bytes_read, elf_magic.len)], elf_magic);
}

fn isMachO(self: *FileHeader) bool {
if (self.bytes_read < 4) return false;
const magic_number = std.mem.readInt(u32, &self.header, builtin.cpu.arch.endian());
return magic_number == std.macho.MH_MAGIC or
magic_number == std.macho.MH_MAGIC_64 or
magic_number == std.macho.FAT_MAGIC or
magic_number == std.macho.FAT_MAGIC_64;
}

pub fn isExecutable(self: *FileHeader) bool {
return std.mem.eql(u8, self.header[0..@min(self.bytes_read, shebang.len)], shebang) or
std.mem.eql(u8, self.header[0..@min(self.bytes_read, elf_magic.len)], elf_magic);
return self.isScript() or self.isElf() or self.isMachO();
}
};

test FileHeader {
var h: FileHeader = .{};
try std.testing.expect(!h.isExecutable());

h.update(FileHeader.elf_magic[0..2]);
const elf_magic = std.elf.MAGIC;
h.update(elf_magic[0..2]);
try std.testing.expect(!h.isExecutable());
h.update(FileHeader.elf_magic[2..4]);
h.update(elf_magic[2..4]);
try std.testing.expect(h.isExecutable());

h.update(elf_magic[2..4]);
try std.testing.expect(h.isExecutable());

h.update(FileHeader.elf_magic[2..4]);
const macho64_magic_bytes = [_]u8{ 0xCF, 0xFA, 0xED, 0xFE };
h.bytes_read = 0;
h.update(&macho64_magic_bytes);
try std.testing.expect(h.isExecutable());
}

Expand Down
Loading