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
fix(apple): cap lazy-info symbol string references
A payload of distinct one-byte symbol strings stayed under the byte budgets while caching one entry per offset. A slice now stops at 65536 symbol references.

Co-authored-by: morluto <morluto@users.noreply.github.com>
  • Loading branch information
cursoragent and morluto committed Oct 20, 2026
commit e8f68f18d228662b837bb043c398a87ca093d73a
4 changes: 3 additions & 1 deletion docs/apple-application-analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ dyld's scan. Distinct payload bytes and payload-range lookups stay within the 16
limit, and a file range already read is reused. String scanning and UTF-8
checks count every reference, including repeats and overlaps, and stop at that
same aggregate. A slice retains at most 1024 of these commands; further copies are
unsupported instead of becoming one traced edge each.
unsupported instead of becoming one traced edge each. Symbol references,
including repeats, stop at 65536 per slice. A larger table is unsupported
instead of caching one string per offset.
Nothing is executed and no Apple tool is required, so the tool also works off
macOS.

Expand Down
82 changes: 82 additions & 0 deletions src/artifacts/apple/MachoLoadCommandReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from "./MachoImage.fixture.js";
import {
MAX_LAZY_INFO_DEPENDENCIES,
MAX_LAZY_INFO_SYMBOL_REFERENCES,
MAX_LOAD_COMMAND_BYTES,
hasMachoMagic,
readMachoImage,
Expand Down Expand Up @@ -838,6 +839,87 @@ describe("LC_LAZY_LOAD_DYLIB_INFO string bounds", () => {
});
});

const lazyInfoPayloadWithSymbolCount = (
symbolCount: number,
pathText: string,
): Uint8Array => {
const path = new TextEncoder().encode(pathText);
const pathOffset = 24 + symbolCount * 4;
// One byte after the path NUL, because a load-path terminator in the
// payload's final byte is unterminated.
const payload = new Uint8Array(pathOffset + path.length + 2);
const view = new DataView(payload.buffer);
view.setUint32(0, pathOffset, true);
view.setUint32(16, symbolCount, true);
view.setUint32(20, 24, true);
payload.set(path, pathOffset);
const emptySymbol = pathOffset + path.length;
for (let symbol = 0; symbol < symbolCount; symbol += 1)
view.setUint32(24 + symbol * 4, emptySymbol, true);
return payload;
};

const imageWithLazyPayload = (payload: Uint8Array): 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);
const view = new DataView(bytes.buffer);
view.setUint32(32 + 8, image.length, true);
view.setUint32(32 + 12, payload.length, true);
return bytes;
};

describe("LC_LAZY_LOAD_DYLIB_INFO symbol cardinality", () => {
it("stops before caching one string for every symbol offset", async () => {
const atLimit = lazyInfoPayloadWithSymbolCount(
MAX_LAZY_INFO_SYMBOL_REFERENCES,
"@rpath/libl.dylib",
);
expect(await read(imageWithLazyPayload(atLimit))).toMatchObject({
status: "parsed",
slices: [{ dependencies: [{ install_name: "@rpath/libl.dylib" }] }],
});
const overLimit = lazyInfoPayloadWithSymbolCount(
MAX_LAZY_INFO_SYMBOL_REFERENCES + 1,
"@rpath/libl.dylib",
);
expect(await read(imageWithLazyPayload(overLimit))).toMatchObject({
status: "unsupported",
reason: expect.stringContaining("65536-reference limit"),
});
});

it("counts symbol references across distinct lazy-info payloads", async () => {
const first = lazyInfoPayloadWithSymbolCount(
MAX_LAZY_INFO_SYMBOL_REFERENCES,
"@rpath/libl.dylib",
);
const second = lazyInfoPayloadWithSymbolCount(1, "@rpath/libm.dylib");
const image = machoImage({
commands: [
lazyLoadDylibInfoCommand(0, first.length),
lazyLoadDylibInfoCommand(0, second.length),
],
});
const bytes = new Uint8Array(image.length + first.length + second.length);
bytes.set(image);
bytes.set(first, image.length);
bytes.set(second, image.length + first.length);
const view = new DataView(bytes.buffer);
view.setUint32(32 + 8, image.length, true);
view.setUint32(32 + 12, first.length, true);
view.setUint32(48 + 8, image.length + first.length, true);
view.setUint32(48 + 12, second.length, true);
expect(await read(bytes)).toMatchObject({
status: "unsupported",
reason: expect.stringContaining("65536-reference limit"),
});
});
});

describe("LC_LAZY_LOAD_DYLIB_INFO path terminator", () => {
it("rejects a load path whose NUL is the payload's final byte", async () => {
const payload = new Uint8Array(27);
Expand Down
16 changes: 16 additions & 0 deletions src/artifacts/apple/MachoLoadCommandReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export const MAX_LOAD_COMMAND_BYTES = 16 * 1024 * 1024;
* those results are kept or traced.
*/
export const MAX_LAZY_INFO_DEPENDENCIES = 1024;
/**
* Symbol names are validated and then discarded. Distinct one-byte strings
* stay under the 16 MiB byte budgets while retaining one cache entry per
* offset, so stop before that map and those decodes grow.
*/
export const MAX_LAZY_INFO_SYMBOL_REFERENCES = 64 * 1024;
const MH_MAGIC = 0xfeedface;
const MH_MAGIC_64 = 0xfeedfacf;
const MH_CIGAM = 0xcefaedfe;
Expand Down Expand Up @@ -511,6 +517,8 @@ interface LazyPayloadWork {
payloadBytes: number;
/** Scanned string bytes. Repeats and overlaps each count in full. */
stringBytes: number;
/** Symbol-string references. Repeats each count. */
symbolReferences: number;
rangeChecks: number;
}

Expand Down Expand Up @@ -555,6 +563,7 @@ const appendDependencies = async (
strings: new Map(),
payloadBytes: 0,
stringBytes: 0,
symbolReferences: 0,
rangeChecks: 0,
};
for (const dependency of dependencies) {
Expand Down Expand Up @@ -785,6 +794,13 @@ const decodeLazyLoadDylibInfoPayload = (
work,
true,
);
const symbolReferences = work.symbolReferences + symbolCount;
if (symbolReferences > MAX_LAZY_INFO_SYMBOL_REFERENCES)
throw new MachoFormatIssue(
"unsupported",
`LC_LAZY_LOAD_DYLIB_INFO symbol validation exceeds the ${MAX_LAZY_INFO_SYMBOL_REFERENCES}-reference limit`,
);
work.symbolReferences = symbolReferences;
for (let symbol = 0; symbol < symbolCount; symbol += 1) {
const offset = view.getUint32(symbolArrayOffset + symbol * 4, true);
payloadString(
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
Expand Down
Loading