Files
reverse-engineering-anything/src/native/MachoCommandNames.ts
T
560674e2de feat(native): report binary hardening for ELF, PE and Mach-O (#8524)
* feat(native): report binary hardening for ELF, PE and Mach-O

Documents inspect_binary_hardening and its CLI equivalent, including
per-property states, supporting evidence, read budgets, and one report
per Mach-O slice. Explains that missing protection imports remain
unknown, signature presence does not establish validity, and hardened
runtime requires inspect_signature. Adds hardening links throughout the
documentation. The supplied diff also removes the advanced CodeQL
workflow and configuration, along with snapshot-binding mismatch
guidance.

* fix(native): validate declared payloads and dynamic tables before hardening rows

Run the shared range checks for ELF, read imports from the dynamic tables
rather than section metadata, and keep an unresolved symbol count apart from
an absent table. Check every Mach-O command before building rows and retain
only the offsets the rows need.

Make the verifier's readelf output wide, check the emitted format before
parsing Mach-O, and place the sources before the libraries in the link.

* fix(native): share Mach-O segment admission with hardening reads

A segment with a section count its cmdsize cannot hold passed the hardening
reader and could enable restrict_segment. Move the size, section count and
file range checks to a shared admission that readMachoImage and the hardening
reader both use, and pin the ELF load-size case with a regression test.

* fix(hardening): reject Mach-O section payloads past the slice and empty ELF interpreters

Segment admission never checked each section's payload range, so a
file-backed section past the slice still produced rows. Decode sections with
the section header owner and mark the slice malformed when a payload leaves
it; zero-fill and empty sections own no file bytes and are exempt.

admitInterp accepted a NUL first byte in a longer extent, which counted an
empty interpreter as PIE evidence. It now requires a nonempty path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KuWHGvzJkrhCqjtEHUwqyH

* fix(native): read CFG instrumentation from decoded GuardFlags

Use the IMAGE_GUARD_CF_INSTRUMENTED flag that guardFlagRecord already
decodes instead of masking the raw GuardFlags word a second time.

* fix(native): reject ELF table spans a second PT_LOAD remaps

The hardening reader mapped DT_STRTAB, DT_SYMTAB and hash tables through
the one PT_LOAD containing the table's first address. A second PT_LOAD
starting inside the table could map the rest of it from other file bytes
and the reader still reported parsed import evidence.

Cap each mapping at the next PT_LOAD that starts inside it, reject a
requested range that crosses that boundary, and stop hash reads at the
same limit, matching the conflicting-mapping refusal in readElfSymbols.

* fix(native): dedupe ELF note tables only on shared boundaries

The hardening reader dropped any note table whose bytes another table
already covered. A covering SHT_NOTE can hold a non-GNU note whose
descriptor is itself a complete GNU property note, while PT_NOTE names
that inner note directly. Walking only the outer stream missed the GNU
marker and reported CET disabled.

Drop an alias only when it starts and ends on boundaries of admitted
tables, which are note boundaries of the walked streams. Any other
covered alias leaves the feature rows unknown, as a partial overlap
already does.

* fix(native): admit every Mach-O symbol record after a canary match

The Mach-O stack protector scan returned as soon as it found an
undefined ___stack_chk_fail, so a later undefined symbol whose n_strx
lay outside the string table was never checked. The same damaged table
parsed or failed as malformed depending only on record order.

Keep the match and finish the bounded record walk. Records after the
match are only checked against the string table bounds, so their names
are not read or charged to the scan budget. The ELF reader already
admits its whole symbol table before matching names.

* fix(native): accept ELF load aliases with the same file translation

The hardening reader treated every PT_LOAD that starts inside a table,
or that also contains the table's address, as a conflicting mapping.
A load with the same address-to-file translation names the same file
bytes, so tables it overlaps still have one interpretation, and the
canonical ElfLoadMap already merges such spans.

Treat equivalent loads as one interpretation. A load with a different
translation, or the zero-filled tail of an equivalent load, still ends
the usable range, so a remapped table is still rejected.

* fix(native): keep same-span ELF note tables with different alignments unknown

A section and a program header that share endpoints were treated as one
note stream even when their effective note alignments differed. Walking the
same bytes at 4 and 8 can frame different notes, so dropping the second table
could hide a GNU property word and report CET or BTI as disabled.

Require matching effective alignment at both endpoints, and no alignment
change inside the covered span, before treating a table as an alias.
Otherwise mark the feature rows unknown. The fixture's .note.gnu.property
section now declares the class's note alignment, as toolchains emit it.

* refactor(native): read LC_SYMTAB through the shared fixed-word decoder

Move the Mach-O fixed uint32 command layouts out of MachoCommandNames
into domain/apple/machoLoadCommands so there is one owner of those
offsets. MachoCommandNames and the hardening reader now both decode
through it; the hardening reader keeps its own bounds and import policy.

* fix(native): skip Mach-O common definitions in canary imports

A common definition sets N_UNDF|N_EXT like an import but stores its
allocation size in n_value. An object holding only
`int __stack_chk_fail;` therefore reported stack_protector: enabled.

Require a zero n_value before an undefined external counts as an
import, for both nlist widths.

* refactor(native): read linkedit data commands through the shared fixed-word decoder

readMachoLinkeditDataCommand reproduced the dataoff/datasize offsets that
FIXED_WORD_FIELDS already owns. Take the command and decode through
decodeMachoFixedWordFields, as readMachoSymtabCommand does, keeping the
truncation and endianness behavior.

* fix(native): skip empty SHT_NOTE sections in ELF hardening

A zero-sized SHT_NOTE owns no bytes, so the section directory marks it
not file-backed and the hardening reader rejected the whole image as
malformed, while readElfImage accepted the same bytes. Skip empty note
sections before the file-backing check; they have nothing to walk.

* fix(scripts): run hardening verifier commands through the shared owner

The binary hardening verifier started its probes and compiler drivers with
raw spawnSync, which bypassed the src/process/ supervision that AGENTS.md
requires. Every launch now goes through createVerifierCommands, and the
temporary root is removed only after the owner closes cleanly.

* fix(scripts): clean up hardening verifier commands on Ctrl-C

SIGINT or SIGTERM ended Node before withVerifierCleanup ran, so detached
compiler trees and the temporary root could outlive the verifier. The
verifier now turns those signals into an abort that every owned command
receives, so cleanup closes the owner and removes the root before exit.

* fix(scripts): fail the hardening verifier when a signal lands outside a command

Only the owned commands read the interruption signal, so a SIGINT or
SIGTERM during the final cleanup or between commands was recorded and
then dropped, and the verifier exited 0. After cleanup settles, the
verifier now throws the interruption reason. A failure the operation or
its cleanup already raised still leaves first and is not replaced.

* fix(native): fail Mach-O slices with a malformed encryption command

The hardening reader turned every malformed status from the shared
encryption reader into an unknown `encrypted` row, so a short
LC_ENCRYPTION_INFO_64 or an encrypted range past the slice still left the
slice parsed with a full report.

Only two conflicting encryption commands stay an unknown row now. Any other
encryption defect fails the slice like other structural command failures.

* fix(native): ignore STB_LOCAL undefined ELF symbols as imports

A local undefined __stack_chk_fail or __memcpy_chk is never resolved
against another object, so it does not establish an external import.
Skip STB_LOCAL bindings when reading protector signs, and give the
fixture's positive symbols STB_GLOBAL binding.

---------

Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 01:07:00 +08:00

717 lines
26 KiB
TypeScript

import {
MachoLoadCommandCursor,
decodeMachoFixedWordFields,
machoFixedWordFieldNames,
readMachoLoadCommandTableHeader,
} from "../domain/apple/machoLoadCommands.js";
import { readMachoSegmentHeader } from "../domain/apple/machoSegmentHeader.js";
import { readMachoSectionHeader } from "../domain/apple/machoSectionHeader.js";
import { createHash } from "node:crypto";
const DYLIB_COMMANDS = new Set([
0xc, 0xd, 0x18, 0x80000018, 0x1f, 0x8000001f, 0x20, 0x23, 0x80000023,
]);
const LC_STR_COMMANDS = new Set([
...DYLIB_COMMANDS,
0x6,
0x7,
0x9,
0x10,
0xe,
0xf,
0x12,
0x13,
0x14,
0x15,
0x8000001c,
0x27,
0x39,
]);
/** Read exact ranges from an already admitted and identity-verified slice. */
export type MachoRangeReader = (
offset: number,
size: number,
) => Promise<Uint8Array>;
/** Invalid on-disk Mach-O framing or payload at the binary producer boundary. */
export class MachoBinaryFormatError extends Error {}
/** Producer-derived output capacity exceeded while retaining binary strings. */
export class MachoCommandNamesLimitError extends Error {
constructor(
readonly maximum: number,
readonly unit: "bytes" | "records" | "projected_bytes",
) {
super(
`Mach-O load-command ${unit === "projected_bytes" ? "projected bytes" : unit} exceed the retained output budget`,
);
}
}
// Reserve fixed projection keys, scalar metadata, arrays and bounded limitation
// messages even when the input has no load commands.
const PROJECTED_ENVELOPE_BYTES = 8192;
// A section becomes both a segment-view object and a set of indexed command
// fields. Reserve for their keys, numbers, identities and JSON framing before
// retaining the section. Strings are charged separately for escaping and hex.
const PROJECTED_COMMAND_BYTES = 2048;
const PROJECTED_SECTION_BYTES = 2048;
const PROJECTED_CHILD_BYTES = 256;
const PROJECTED_STRING_EXPANSION = 24;
const DEFAULT_PROJECTED_BYTES = 64 * 1024 * 1024;
export interface MachoCommandNames {
readonly sliceSize: number;
readonly header: {
readonly wide: boolean;
readonly littleEndian: boolean;
readonly cpuType: number;
readonly cpuSubtype: number;
readonly fileType: number;
readonly flags: number;
};
readonly commands: readonly {
readonly index: number;
readonly command: number;
readonly offset: number;
readonly size: number;
/** Every byte after the command header belongs to a decoded field. */
readonly fullyDecoded?: boolean;
/** Whole-command digest when it fits the retained output allowance. */
readonly rawSha256: string | null;
readonly fixedFields?: Readonly<Record<string, number>>;
readonly segmentName?: Uint8Array;
readonly segment?: {
readonly address: bigint;
readonly memorySize: bigint;
readonly fileOffset: bigint;
readonly fileSize: bigint;
readonly maxProtection: number;
readonly protection: number;
readonly flags: number;
};
readonly sections?: readonly {
readonly name: Uint8Array;
readonly segmentName: Uint8Array;
readonly address: bigint;
readonly size: bigint;
readonly fileOffset: number;
readonly alignmentExponent: number;
readonly relocationOffset: number;
readonly relocationCount: number;
readonly reserved1: number;
readonly reserved2: number;
readonly reserved3?: number;
readonly flags: number;
}[];
readonly commandString?: Uint8Array;
readonly commandStringOffset?: number;
readonly dylib?: {
readonly nameOffset: number;
readonly currentVersion: number;
readonly compatibilityVersion: number;
readonly encoding:
| { readonly kind: "timestamp"; readonly timestamp: number }
| { readonly kind: "use"; readonly flags: number };
};
readonly legacyBuild?: {
readonly version: number;
readonly sdk: number;
};
readonly linkerOptions?: readonly Uint8Array[];
readonly note?: {
readonly owner: Uint8Array;
readonly offset: bigint;
readonly size: bigint;
};
readonly fileset?: {
readonly address: bigint;
readonly fileOffset: bigint;
readonly entryId: Uint8Array;
readonly reserved: number;
};
readonly prebound?: {
readonly moduleCount: number;
readonly linkedModulesOffset: number;
readonly linkedModules: Uint8Array;
};
readonly fvmlib?: {
readonly minorVersion: number;
readonly headerAddress: number;
};
readonly fvmfile?: { readonly headerAddress: number };
readonly uuid?: Uint8Array;
readonly entrypoint?: {
readonly offset: bigint;
readonly stackSize: bigint;
};
readonly build?: {
readonly platform: number;
readonly minimumOs: number;
readonly sdk: number;
readonly tools: readonly {
readonly tool: number;
readonly version: number;
}[];
};
}[];
}
/**
* Walk only fixed records and declared strings from a selected thin image.
* The caller owns identity, cancellation, closing, and the per-string byte cap.
*/
export const readMachoCommandNames = async (
read: MachoRangeReader,
imageSize: number,
maximumStringBytes: number,
maximumRecords: number,
maximumProjectedBytes = DEFAULT_PROJECTED_BYTES,
): Promise<MachoCommandNames> => {
if (
!Number.isSafeInteger(maximumStringBytes) ||
maximumStringBytes < 0 ||
!Number.isSafeInteger(maximumRecords) ||
maximumRecords < 0 ||
!Number.isSafeInteger(maximumProjectedBytes) ||
maximumProjectedBytes < 0
)
throw new TypeError("Mach-O read limits must be nonnegative safe integers");
const exact = async (offset: number, size: number): Promise<Uint8Array> => {
if (
!Number.isSafeInteger(offset) ||
!Number.isSafeInteger(size) ||
offset < 0 ||
size < 0 ||
offset > imageSize - size
)
throw new MachoBinaryFormatError("Mach-O range exceeds selected slice");
const value = await read(offset, size);
if (value.byteLength !== size)
throw new MachoBinaryFormatError("Mach-O range is truncated");
return value;
};
const header = await exact(0, Math.min(32, imageSize));
const parsed = readMachoLoadCommandTableHeader(header, imageSize);
if (!parsed.ok) throw new MachoBinaryFormatError(parsed.error);
if (parsed.value === null)
throw new MachoBinaryFormatError("Selected slice is not Mach-O");
const table = parsed.value;
const cursor = new MachoLoadCommandCursor(table);
if (table.commandCount > maximumRecords)
throw new MachoCommandNamesLimitError(maximumRecords, "records");
let projectedBytes = 0;
const chargeProjection = (count: number, perItem: number): void => {
if (count > Math.floor((maximumProjectedBytes - projectedBytes) / perItem))
throw new MachoCommandNamesLimitError(
maximumProjectedBytes,
"projected_bytes",
);
projectedBytes += count * perItem;
};
chargeProjection(1, PROJECTED_ENVELOPE_BYTES);
chargeProjection(table.commandCount, PROJECTED_COMMAND_BYTES);
let records = table.commandCount;
let retainedStringBytes = 0;
const retain = (bytes: Uint8Array): Uint8Array => {
if (bytes.byteLength > maximumStringBytes - retainedStringBytes)
throw new MachoCommandNamesLimitError(maximumStringBytes, "bytes");
chargeProjection(bytes.byteLength, PROJECTED_STRING_EXPANSION);
retainedStringBytes += bytes.byteLength;
// Range readers may return Buffer views into a much larger read window.
// Charge the retained length before allocating an independently owned copy.
return Uint8Array.from(bytes);
};
const commands: MachoCommandNames["commands"][number][] = [];
while (cursor.nextOffset !== null) {
const row = cursor.read(
await exact(table.headerSize + cursor.nextOffset, 8),
);
if (!row.ok) throw new MachoBinaryFormatError(row.error);
const frame = row.value;
const at = table.headerSize + frame.offset;
const fixedNames = machoFixedWordFieldNames(frame.command);
let fixedFields: Readonly<Record<string, number>> | undefined;
if (fixedNames !== undefined) {
const byteLength = fixedNames.length * 4;
const decoded =
frame.size < 8 + byteLength
? null
: decodeMachoFixedWordFields(
frame.command,
await exact(at + 8, byteLength),
table.littleEndian,
);
if (decoded === null)
throw new MachoBinaryFormatError(
"Mach-O fixed command fields are truncated",
);
fixedFields = decoded;
}
const segmentWide =
frame.command === 0x19 ? true : frame.command === 1 ? false : null;
let segmentName: Uint8Array | undefined;
let segmentFacts: MachoCommandNames["commands"][number]["segment"];
let sections:
| NonNullable<MachoCommandNames["commands"][number]["sections"]>[number][]
| undefined;
if (segmentWide !== null) {
const segmentHeaderSize = segmentWide ? 72 : 56;
if (frame.size < segmentHeaderSize)
throw new MachoBinaryFormatError("Mach-O segment is truncated");
const segmentBytes = await exact(at, segmentHeaderSize);
const segment = readMachoSegmentHeader(
segmentBytes,
segmentWide,
table.littleEndian,
);
if (!segment.ok) throw new MachoBinaryFormatError(segment.error.message);
const width = segmentWide ? 80 : 68;
if (
segment.value.sectionCount >
Math.floor((frame.size - segmentHeaderSize) / width)
)
throw new MachoBinaryFormatError("Mach-O section table is truncated");
if (segment.value.sectionCount > maximumRecords - records)
throw new MachoCommandNamesLimitError(maximumRecords, "records");
chargeProjection(segment.value.sectionCount, PROJECTED_SECTION_BYTES);
records += segment.value.sectionCount;
segmentName = retain(segment.value.nameBytes);
const segmentView = new DataView(
segmentBytes.buffer,
segmentBytes.byteOffset,
segmentBytes.byteLength,
);
segmentFacts = {
address: segment.value.address,
memorySize: segment.value.memorySize,
fileOffset: segment.value.fileOffset,
fileSize: segment.value.fileSize,
maxProtection: segment.value.maxProtection,
protection: segment.value.protection,
flags: segmentView.getUint32(segmentWide ? 68 : 52, table.littleEndian),
};
sections = [];
for (let i = 0; i < segment.value.sectionCount; i++) {
const sectionBytes = await exact(
at + segmentHeaderSize + i * width,
width,
);
const section = readMachoSectionHeader(
sectionBytes,
segmentWide,
table.littleEndian,
);
if (!section.ok)
throw new MachoBinaryFormatError(section.error.message);
const sectionView = new DataView(
sectionBytes.buffer,
sectionBytes.byteOffset,
sectionBytes.byteLength,
);
sections.push({
name: retain(section.value.nameBytes),
segmentName: retain(section.value.segmentNameBytes),
address: section.value.address,
size: section.value.size,
fileOffset: section.value.offset,
alignmentExponent: sectionView.getUint32(
segmentWide ? 52 : 44,
table.littleEndian,
),
relocationOffset: sectionView.getUint32(
segmentWide ? 56 : 48,
table.littleEndian,
),
relocationCount: sectionView.getUint32(
segmentWide ? 60 : 52,
table.littleEndian,
),
reserved1: sectionView.getUint32(
segmentWide ? 68 : 60,
table.littleEndian,
),
reserved2: sectionView.getUint32(
segmentWide ? 72 : 64,
table.littleEndian,
),
...(segmentWide
? { reserved3: sectionView.getUint32(76, table.littleEndian) }
: {}),
flags: section.value.flags,
});
}
}
// Dylib and rpath commands own an lc_str at offset 8. Retain bytes up to
// the first NUL without allocating the command's full declared extent.
let commandString: Uint8Array | undefined;
let commandStringOffset: number | undefined;
let dylib: MachoCommandNames["commands"][number]["dylib"];
if (LC_STR_COMMANDS.has(frame.command)) {
if (frame.size < 12)
throw new MachoBinaryFormatError(
"Mach-O command string offset is truncated",
);
const data = await exact(at + 8, 4);
const offset = new DataView(
data.buffer,
data.byteOffset,
data.byteLength,
).getUint32(0, table.littleEndian);
const minimum = DYLIB_COMMANDS.has(frame.command)
? 24
: frame.command === 0x6 ||
frame.command === 0x7 ||
frame.command === 0x10
? 20
: frame.command === 0x9
? 16
: 12;
if (offset < minimum || offset >= frame.size)
throw new MachoBinaryFormatError(
"Mach-O command string offset is invalid",
);
const limit = Math.min(frame.size - offset, maximumStringBytes + 1);
const bytes = await exact(at + offset, limit);
const end = bytes.indexOf(0);
if (end < 0) {
if (limit < frame.size - offset)
throw new MachoCommandNamesLimitError(maximumStringBytes, "bytes");
throw new MachoBinaryFormatError(
"Mach-O command string is unterminated",
);
}
commandString = retain(bytes.subarray(0, end));
commandStringOffset = offset;
if (DYLIB_COMMANDS.has(frame.command)) {
const fixed = await exact(at + 12, 12);
const view = new DataView(
fixed.buffer,
fixed.byteOffset,
fixed.byteLength,
);
const marker = view.getUint32(0, table.littleEndian);
const useEncoding =
(frame.command === 0xc || frame.command === 0x80000018) &&
offset === 28 &&
marker === 0x1a741800;
if (useEncoding && frame.size < 28)
throw new MachoBinaryFormatError(
"Mach-O dylib use command is truncated",
);
const flags = useEncoding ? await exact(at + 24, 4) : undefined;
dylib = {
nameOffset: offset,
currentVersion: view.getUint32(4, table.littleEndian),
compatibilityVersion: view.getUint32(8, table.littleEndian),
encoding:
flags === undefined
? { kind: "timestamp", timestamp: marker }
: {
kind: "use",
flags: new DataView(
flags.buffer,
flags.byteOffset,
flags.byteLength,
).getUint32(0, table.littleEndian),
},
};
}
}
let prebound: MachoCommandNames["commands"][number]["prebound"];
if (frame.command === 0x10) {
if (frame.size < 20)
throw new MachoBinaryFormatError(
"Mach-O prebound dylib command is truncated",
);
const data = await exact(at + 12, 8);
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
const moduleCount = view.getUint32(0, table.littleEndian);
const linkedModulesOffset = view.getUint32(4, table.littleEndian);
const length = Math.ceil(moduleCount / 8);
if (linkedModulesOffset < 20 || linkedModulesOffset > frame.size - length)
throw new MachoBinaryFormatError(
"Mach-O prebound module bitmap exceeds its command",
);
if (length > maximumStringBytes - retainedStringBytes)
throw new MachoCommandNamesLimitError(maximumStringBytes, "bytes");
prebound = {
moduleCount,
linkedModulesOffset,
linkedModules: retain(await exact(at + linkedModulesOffset, length)),
};
}
let fvmlib: MachoCommandNames["commands"][number]["fvmlib"];
if (frame.command === 0x6 || frame.command === 0x7) {
if (frame.size < 20)
throw new MachoBinaryFormatError("Mach-O fvmlib command is truncated");
const data = await exact(at + 12, 8);
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
fvmlib = {
minorVersion: view.getUint32(0, table.littleEndian),
headerAddress: view.getUint32(4, table.littleEndian),
};
}
let fvmfile: MachoCommandNames["commands"][number]["fvmfile"];
if (frame.command === 0x9) {
if (frame.size < 16)
throw new MachoBinaryFormatError("Mach-O fvmfile command is truncated");
const data = await exact(at + 12, 4);
fvmfile = {
headerAddress: new DataView(
data.buffer,
data.byteOffset,
data.byteLength,
).getUint32(0, table.littleEndian),
};
}
let linkerOptions: Uint8Array[] | undefined;
if (frame.command === 0x2d) {
if (frame.size < 12)
throw new MachoBinaryFormatError(
"Mach-O linker option count is truncated",
);
const countBytes = await exact(at + 8, 4);
const count = new DataView(
countBytes.buffer,
countBytes.byteOffset,
countBytes.byteLength,
).getUint32(0, table.littleEndian);
if (count > maximumRecords - records)
throw new MachoCommandNamesLimitError(maximumRecords, "records");
if (count > frame.size - 12)
throw new MachoBinaryFormatError(
"Mach-O linker option table is truncated",
);
chargeProjection(count, PROJECTED_CHILD_BYTES);
records += count;
linkerOptions = [];
let offset = 12;
let current = new Uint8Array(0);
let currentLength = 0;
while (linkerOptions.length < count && offset < frame.size) {
const length = Math.min(frame.size - offset, 4096);
const bytes = await exact(at + offset, length);
offset += length;
for (const byte of bytes) {
if (byte === 0) {
linkerOptions.push(retain(current.subarray(0, currentLength)));
current = new Uint8Array(0);
currentLength = 0;
if (linkerOptions.length === count) break;
} else {
const remaining = maximumStringBytes - retainedStringBytes;
if (currentLength >= remaining)
throw new MachoCommandNamesLimitError(
maximumStringBytes,
"bytes",
);
if (currentLength === current.byteLength) {
const capacity = Math.min(
remaining,
Math.max(32, current.byteLength * 2),
);
const expanded = new Uint8Array(capacity);
expanded.set(current);
current = expanded;
}
current[currentLength++] = byte;
}
}
}
if (linkerOptions.length !== count)
throw new MachoBinaryFormatError(
"Mach-O linker option is unterminated",
);
}
let legacyBuild: MachoCommandNames["commands"][number]["legacyBuild"];
if ([0x24, 0x25, 0x2f, 0x30].includes(frame.command)) {
if (frame.size < 16)
throw new MachoBinaryFormatError(
"Mach-O minimum version command is truncated",
);
const bytes = await exact(at + 8, 8);
const view = new DataView(
bytes.buffer,
bytes.byteOffset,
bytes.byteLength,
);
legacyBuild = {
version: view.getUint32(0, table.littleEndian),
sdk: view.getUint32(4, table.littleEndian),
};
}
let note: MachoCommandNames["commands"][number]["note"];
if (frame.command === 0x31) {
if (frame.size < 40)
throw new MachoBinaryFormatError("Mach-O note command is truncated");
const data = await exact(at + 8, 32);
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
note = {
owner: retain(data.subarray(0, 16)),
offset: view.getBigUint64(16, table.littleEndian),
size: view.getBigUint64(24, table.littleEndian),
};
}
let fileset: MachoCommandNames["commands"][number]["fileset"];
if (frame.command === 0x80000035) {
if (frame.size < 32)
throw new MachoBinaryFormatError("Mach-O fileset command is truncated");
const data = await exact(at + 8, 24);
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
const offset = view.getUint32(16, table.littleEndian);
if (offset < 32 || offset >= frame.size)
throw new MachoBinaryFormatError(
"Mach-O fileset entry string offset is invalid",
);
const length = Math.min(frame.size - offset, maximumStringBytes + 1);
const value = await exact(at + offset, length);
const end = value.indexOf(0);
if (end < 0) {
if (length < frame.size - offset)
throw new MachoCommandNamesLimitError(maximumStringBytes, "bytes");
throw new MachoBinaryFormatError(
"Mach-O fileset entry string is unterminated",
);
}
fileset = {
address: view.getBigUint64(0, table.littleEndian),
fileOffset: view.getBigUint64(8, table.littleEndian),
entryId: retain(value.subarray(0, end)),
reserved: view.getUint32(20, table.littleEndian),
};
}
let uuid: Uint8Array | undefined;
if (frame.command === 0x1b) {
if (frame.size < 24)
throw new MachoBinaryFormatError("Mach-O UUID command is truncated");
uuid = Uint8Array.from(await exact(at + 8, 16));
}
let entrypoint: MachoCommandNames["commands"][number]["entrypoint"];
if (frame.command === 0x80000028) {
if (frame.size < 24)
throw new MachoBinaryFormatError(
"Mach-O entrypoint command is truncated",
);
const bytes = await exact(at + 8, 16);
const view = new DataView(
bytes.buffer,
bytes.byteOffset,
bytes.byteLength,
);
entrypoint = {
offset: view.getBigUint64(0, table.littleEndian),
stackSize: view.getBigUint64(8, table.littleEndian),
};
}
let build: MachoCommandNames["commands"][number]["build"];
if (frame.command === 0x32) {
if (frame.size < 24)
throw new MachoBinaryFormatError(
"Mach-O build version command is truncated",
);
const header = await exact(at + 8, 16);
const view = new DataView(
header.buffer,
header.byteOffset,
header.byteLength,
);
const toolCount = view.getUint32(12, table.littleEndian);
if (toolCount > Math.floor((frame.size - 24) / 8))
throw new MachoBinaryFormatError(
"Mach-O build tool table is truncated",
);
if (toolCount > maximumRecords - records)
throw new MachoCommandNamesLimitError(maximumRecords, "records");
chargeProjection(toolCount, PROJECTED_CHILD_BYTES);
records += toolCount;
const tools: { tool: number; version: number }[] = [];
for (let i = 0; i < toolCount; i++) {
const tool = await exact(at + 24 + i * 8, 8);
const toolView = new DataView(
tool.buffer,
tool.byteOffset,
tool.byteLength,
);
tools.push({
tool: toolView.getUint32(0, table.littleEndian),
version: toolView.getUint32(4, table.littleEndian),
});
}
build = {
platform: view.getUint32(0, table.littleEndian),
minimumOs: view.getUint32(4, table.littleEndian),
sdk: view.getUint32(8, table.littleEndian),
tools,
};
}
const fullyDecoded =
(fixedNames !== undefined && frame.size === 8 + fixedNames.length * 4) ||
(uuid !== undefined && frame.size === 24) ||
(entrypoint !== undefined && frame.size === 24) ||
(legacyBuild !== undefined && frame.size === 16) ||
(segmentWide !== null &&
sections !== undefined &&
frame.size ===
(segmentWide ? 72 : 56) +
sections.length * (segmentWide ? 80 : 68)) ||
(build !== undefined && frame.size === 24 + build.tools.length * 8);
let rawSha256: string | null = null;
if (!fullyDecoded && frame.size <= maximumStringBytes) {
const digest = createHash("sha256");
for (let position = 0; position < frame.size;) {
const length = Math.min(64 * 1024, frame.size - position);
digest.update(await exact(at + position, length));
position += length;
}
rawSha256 = digest.digest("hex");
}
commands.push({
index: frame.index,
command: frame.command,
offset: frame.offset,
size: frame.size,
fullyDecoded,
rawSha256,
...(fixedFields === undefined ? {} : { fixedFields }),
...(segmentName === undefined ||
sections === undefined ||
segmentFacts === undefined
? {}
: { segmentName, segment: segmentFacts, sections }),
...(commandString === undefined ? {} : { commandString }),
...(commandStringOffset === undefined ? {} : { commandStringOffset }),
...(dylib === undefined ? {} : { dylib }),
...(legacyBuild === undefined ? {} : { legacyBuild }),
...(linkerOptions === undefined ? {} : { linkerOptions }),
...(note === undefined ? {} : { note }),
...(fileset === undefined ? {} : { fileset }),
...(prebound === undefined ? {} : { prebound }),
...(fvmlib === undefined ? {} : { fvmlib }),
...(fvmfile === undefined ? {} : { fvmfile }),
...(uuid === undefined ? {} : { uuid }),
...(entrypoint === undefined ? {} : { entrypoint }),
...(build === undefined ? {} : { build }),
});
}
const end = cursor.finish();
if (!end.ok) throw new MachoBinaryFormatError(end.error);
return {
sliceSize: imageSize,
header: {
wide: table.wide,
littleEndian: table.littleEndian,
cpuType: table.identity.cpuType,
cpuSubtype: table.identity.cpuSubtype,
fileType: table.fileType,
flags: new DataView(
header.buffer,
header.byteOffset,
header.byteLength,
).getUint32(24, table.littleEndian),
},
commands,
};
};