Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(apple): bound lazy-load string scans and contained reads
Reject payload strings that start in the 24-byte header, validate each
file-offset string once, and reuse payload bytes already read for a
containing range.

Co-authored-by: morluto <morluto@users.noreply.github.com>
  • Loading branch information
cursoragent and morluto committed Oct 20, 2026
commit 938bb694ad0c037f55c2073c8ea908b8c3403a79
5 changes: 3 additions & 2 deletions docs/apple-application-analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ initialization, and `LC_LAZY_LOAD_DYLIB_INFO` (`0x3a`). That command stores its
install name in a linkedit payload rather than in the load command itself.
Its flag and chain-start image offsets must lie in the slice VM range, which
is dyld's one-page floor extended by every segment except `__LINKEDIT`,
measured from the `__TEXT` vmaddr. Distinct payloads are read once per file
range and refused when their combined size exceeds 16 MiB.
measured from the `__TEXT` vmaddr. Payload strings must start after the 24-byte
header. Distinct payload bytes and string scans are each limited to 16 MiB per
image; a repeated file range or string is validated once.
Nothing is executed and no Apple tool is required, so the tool also works off
macOS.

Expand Down
91 changes: 90 additions & 1 deletion src/artifacts/apple/MachoLoadCommandReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,14 +611,103 @@ describe("LC_LAZY_LOAD_DYLIB_INFO payload budget", () => {
lazyLoadDylibInfoCommand(0, MAX_LOAD_COMMAND_BYTES),
],
});
const bytes = new Uint8Array(image.length + payload.length);
const bytes = new Uint8Array(MAX_LOAD_COMMAND_BYTES);
bytes.set(image);
bytes.set(payload, image.length);
expect(await read(bytes)).toMatchObject({
status: "unsupported",
reason: expect.stringContaining("aggregate limit"),
});
});

it("does not reread a payload range contained in an earlier read", async () => {
const payload = lazyLoadDylibInfoPayload({
path: "@rpath/libl.dylib",
symbols: ["_lf"],
});
const image = machoImage({
commands: [
lazyLoadDylibInfoCommand(0, payload.length + 8),
lazyLoadDylibInfoCommand(0, payload.length),
],
});
const bytes = new Uint8Array(image.length + payload.length + 8);
bytes.set(image);
bytes.set(payload, image.length);
const view = new DataView(bytes.buffer);
view.setUint32(32 + 8, image.length, true);
view.setUint32(48 + 8, image.length, true);
let payloadReads = 0;
const facts = await readMachoImage(async (offset, length) => {
if (offset === image.length) payloadReads += 1;
return bytes.subarray(offset, offset + length);
}, bytes.length);
expect(payloadReads).toBe(1);
expect(facts).toMatchObject({
status: "parsed",
slices: [
{
dependencies: [
{ install_name: "@rpath/libl.dylib" },
{ install_name: "@rpath/libl.dylib" },
],
},
],
});
});

it("rejects a load path taken from the payload header", async () => {
const payload = new Uint8Array(24);
const view = new DataView(payload.buffer);
view.setUint32(0, 4, true);
view.setUint32(20, 24, true);
const image = machoImage({
commands: [lazyLoadDylibInfoCommand(0, payload.length)],
});
const bytes = new Uint8Array(image.length + payload.length);
bytes.set(image);
bytes.set(payload, image.length);
new DataView(bytes.buffer).setUint32(32 + 8, image.length, true);
expect(await read(bytes)).toMatchObject({
status: "malformed",
reason: expect.stringContaining("inside the payload header"),
});
});

it("validates a repeated symbol string once and bounds distinct scans", async () => {
const symbolLength = 8 * 1024 * 1024 + 16;
const payloadFor = (step: number) => {
const count = 3;
const pathBytes = new TextEncoder().encode("@rpath/libl.dylib");
const pathOffset = 24 + count * 4;
const symbolOffset = pathOffset + pathBytes.length + 1;
const payload = new Uint8Array(symbolOffset + symbolLength + 1);
const view = new DataView(payload.buffer);
view.setUint32(0, pathOffset, true);
view.setUint32(16, count, true);
view.setUint32(20, 24, true);
payload.set(pathBytes, pathOffset);
payload.fill(0x61, symbolOffset, symbolOffset + symbolLength);
for (let index = 0; index < count; index += 1)
view.setUint32(24 + index * 4, symbolOffset + index * step, true);
return payload;
};
const imageFor = (payload: Uint8Array) => {
const image = machoImage({
commands: [lazyLoadDylibInfoCommand(0, payload.length)],
});
const bytes = new Uint8Array(image.length + payload.length);
bytes.set(image);
bytes.set(payload, image.length);
new DataView(bytes.buffer).setUint32(32 + 8, image.length, true);
return bytes;
};
expect((await read(imageFor(payloadFor(0)))).status).toBe("parsed");
expect(await read(imageFor(payloadFor(1)))).toMatchObject({
status: "unsupported",
reason: expect.stringContaining("string validation"),
});
});
});

describe("LC_LAZY_LOAD_DYLIB_INFO resolution", () => {
Expand Down
124 changes: 107 additions & 17 deletions src/artifacts/apple/MachoLoadCommandReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,31 @@ const readSegmentSpan = (
};
};

interface LazyPayloadWork {
readonly dependencies: Map<string, MachoDependency>;
readonly reads: { fileOffset: number; bytes: Uint8Array }[];
readonly strings: Map<number, string>;
payloadBytes: number;
stringBytes: number;
}

const containedPayload = (
reads: readonly { fileOffset: number; bytes: Uint8Array }[],
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
fileOffset: number,
size: number,
): Uint8Array | undefined => {
for (const read of reads) {
const start = fileOffset - read.fileOffset;
if (
start >= 0 &&
Number.isSafeInteger(start) &&
start + size <= read.bytes.byteLength
)
return read.bytes.subarray(start, start + size);
}
return undefined;
};

const appendDependencies = async (
slice: MachoSlice,
dependencies: readonly (
Expand All @@ -494,33 +519,33 @@ const appendDependencies = async (
length: number,
maxVmOffset: bigint,
): Promise<void> => {
const payloads = new Map<string, MachoDependency>();
let payloadBytes = 0;
const work: LazyPayloadWork = {
dependencies: new Map(),
reads: [],
strings: new Map(),
payloadBytes: 0,
stringBytes: 0,
};
for (const dependency of dependencies) {
if (dependency.kind === "parsed") {
slice.dependencies.push(dependency.value);
continue;
}
const key = `${dependency.dataOffset}:${dependency.dataSize}`;
const cached = payloads.get(key);
const cached = work.dependencies.get(key);
if (cached !== undefined) {
slice.dependencies.push(cached);
continue;
}
payloadBytes += dependency.dataSize;
if (payloadBytes > MAX_LOAD_COMMAND_BYTES)
throw new MachoFormatIssue(
"unsupported",
`LC_LAZY_LOAD_DYLIB_INFO payloads occupy ${payloadBytes} bytes, above the ${MAX_LOAD_COMMAND_BYTES}-byte aggregate limit`,
);
const value = await readLazyLoadDylibInfo(
readAt,
base,
length,
dependency,
maxVmOffset,
work,
);
payloads.set(key, value);
work.dependencies.set(key, value);
slice.dependencies.push(value);
}
};
Expand Down Expand Up @@ -620,12 +645,38 @@ const decodeLazyLoadDylibInfoCommand = (
return { kind: "lazy-info", index, dataOffset, dataSize };
};

const readLazyPayloadBytes = async (
readAt: ReadAt,
fileOffset: number,
size: number,
index: number,
work: LazyPayloadWork,
): Promise<Uint8Array> => {
const covered = containedPayload(work.reads, fileOffset, size);
if (covered !== undefined) return covered;
work.payloadBytes += size;
if (work.payloadBytes > MAX_LOAD_COMMAND_BYTES)
throw new MachoFormatIssue(
"unsupported",
`LC_LAZY_LOAD_DYLIB_INFO payloads occupy ${work.payloadBytes} bytes, above the ${MAX_LOAD_COMMAND_BYTES}-byte aggregate limit`,
);
const payload = await readExact(
readAt,
fileOffset,
size,
`load command ${index} LC_LAZY_LOAD_DYLIB_INFO payload`,
);
work.reads.push({ fileOffset, bytes: payload });
return payload;
};

const readLazyLoadDylibInfo = async (
readAt: ReadAt,
base: number,
length: number,
command: LazyLoadDylibInfoCommand,
maxVmOffset: bigint,
work: LazyPayloadWork,
): Promise<MachoDependency> => {
if (
command.dataOffset > length ||
Expand All @@ -641,19 +692,28 @@ const readLazyLoadDylibInfo = async (
"malformed",
`load command ${command.index} LC_LAZY_LOAD_DYLIB_INFO payload offset is not a safe integer`,
);
const payload = await readExact(
const payload = await readLazyPayloadBytes(
readAt,
fileOffset,
command.dataSize,
`load command ${command.index} LC_LAZY_LOAD_DYLIB_INFO payload`,
command.index,
work,
);
return decodeLazyLoadDylibInfoPayload(
payload,
command.index,
fileOffset,
maxVmOffset,
work,
);
return decodeLazyLoadDylibInfoPayload(payload, command.index, maxVmOffset);
};

const decodeLazyLoadDylibInfoPayload = (
payload: Uint8Array,
index: number,
fileOffset: number,
maxVmOffset: bigint,
work: LazyPayloadWork,
): MachoDependency => {
const view = viewOf(payload);
const pathOffset = view.getUint32(0, true);
Expand Down Expand Up @@ -685,10 +745,17 @@ const decodeLazyLoadDylibInfoPayload = (
"malformed",
`load command ${index} LC_LAZY_LOAD_DYLIB_INFO symbol table extends beyond the payload`,
);
const installName = payloadString(payload, pathOffset, index, "load path");
const installName = payloadString(
payload,
pathOffset,
fileOffset,
index,
"load path",
work,
);
for (let symbol = 0; symbol < symbolCount; symbol += 1) {
const offset = view.getUint32(symbolArrayOffset + symbol * 4, true);
payloadString(payload, offset, index, `symbol ${symbol}`);
payloadString(payload, offset, fileOffset, index, `symbol ${symbol}`, work);
}
return {
command: "LC_LAZY_LOAD_DYLIB_INFO",
Expand All @@ -703,26 +770,47 @@ const decodeLazyLoadDylibInfoPayload = (
};
};

/** NUL-terminated UTF-8 string at a payload-relative offset. */
/** NUL-terminated UTF-8 string after the fixed payload header. */
const payloadString = (
payload: Uint8Array,
offset: number,
fileOffset: number,
index: number,
label: string,
work: LazyPayloadWork,
): string => {
if (offset < LAZY_INFO_HEADER_BYTES)
throw new MachoFormatIssue(
"malformed",
`load command ${index} LC_LAZY_LOAD_DYLIB_INFO ${label} offset ${offset} is inside the payload header`,
);
const absolute = Number.isSafeInteger(fileOffset + offset)
? fileOffset + offset
: undefined;
const cached =
absolute === undefined ? undefined : work.strings.get(absolute);
if (cached !== undefined) return cached;
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
if (offset >= payload.byteLength)
throw new MachoFormatIssue(
"malformed",
`load command ${index} LC_LAZY_LOAD_DYLIB_INFO ${label} offset ${offset} is outside the payload`,
);
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
const end = payload.indexOf(0, offset);
const scanned = (end < 0 ? payload.byteLength : end) - offset;
work.stringBytes += scanned;
if (work.stringBytes > MAX_LOAD_COMMAND_BYTES)
throw new MachoFormatIssue(
"unsupported",
`LC_LAZY_LOAD_DYLIB_INFO string validation occupies ${work.stringBytes} bytes, above the ${MAX_LOAD_COMMAND_BYTES}-byte limit`,
);
if (end < 0)
throw new MachoFormatIssue(
"malformed",
`load command ${index} LC_LAZY_LOAD_DYLIB_INFO ${label} is unterminated`,
);
let text: string;
try {
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
payload.subarray(offset, end),
);
} catch {
Expand All @@ -731,6 +819,8 @@ const payloadString = (
`load command ${index} LC_LAZY_LOAD_DYLIB_INFO ${label} is not UTF-8`,
);
}
if (absolute !== undefined) work.strings.set(absolute, text);
return text;
};

const decodeDependency = (
Expand Down
Loading