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): cap retained lazy-info dependency results
Stop after 1024 LC_LAZY_LOAD_DYLIB_INFO commands so a load-command region
full of identical commands cannot become a million traced edges.

Co-authored-by: morluto <morluto@users.noreply.github.com>
  • Loading branch information
cursoragent and morluto committed Oct 20, 2026
commit e7cd50f27d9c3e454690fbe53597d323861c9161
3 changes: 2 additions & 1 deletion docs/apple-application-analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ measured from the `__TEXT` vmaddr. Payload strings must start after the 24-byte
header. Distinct payload bytes and payload-range lookups stay within the 16 MiB
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.
same aggregate. A slice retains at most 1024 of these commands; further copies are
unsupported instead of becoming one traced edge each.
Nothing is executed and no Apple tool is required, so the tool also works off
macOS.

Expand Down
41 changes: 37 additions & 4 deletions src/artifacts/apple/MachoLoadCommandReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
versionMinCommand,
} from "./MachoImage.fixture.js";
import {
MAX_LAZY_INFO_DEPENDENCIES,
MAX_LOAD_COMMAND_BYTES,
hasMachoMagic,
readMachoImage,
Expand Down Expand Up @@ -656,8 +657,8 @@ describe("LC_LAZY_LOAD_DYLIB_INFO payload budget", () => {
});
});

it("stops walking earlier payload ranges once lookup work exceeds the limit", async () => {
const count = 6000;
it("parses distinct non-nested lazy-info payloads under the dependency limit", async () => {
const count = 8;
const payloadSize = 25;
const payload = new Uint8Array(payloadSize);
new DataView(payload.buffer).setUint32(0, 24, true);
Expand All @@ -671,9 +672,41 @@ describe("LC_LAZY_LOAD_DYLIB_INFO payload budget", () => {
bytes.set(image);
for (let index = 0; index < count; index += 1)
bytes.set(payload, image.length + index * payloadSize);
expect(await read(bytes)).toMatchObject({
const facts = await read(bytes);
if (facts.status !== "parsed")
throw new Error(
facts.status === "not-mach-o" ? facts.status : facts.reason,
);
expect(facts.slices[0]?.dependencies).toHaveLength(count);
});
});

describe("LC_LAZY_LOAD_DYLIB_INFO dependency limit", () => {
it("retains one result per command until the dependency limit", async () => {
const payload = lazyLoadDylibInfoPayload({ path: "@rpath/libl.dylib" });
const imageFor = (count: number) => {
const dataOffset = 32 + 16 + count * 16;
const image = machoImage({
commands: Array.from({ length: count }, () =>
lazyLoadDylibInfoCommand(dataOffset, payload.length),
),
});
const bytes = new Uint8Array(image.length + payload.length);
bytes.set(image);
bytes.set(payload, image.length);
return bytes;
};
const atLimit = await read(imageFor(MAX_LAZY_INFO_DEPENDENCIES));
if (atLimit.status !== "parsed")
throw new Error(
atLimit.status === "not-mach-o" ? atLimit.status : atLimit.reason,
);
expect(atLimit.slices[0]?.dependencies).toHaveLength(
MAX_LAZY_INFO_DEPENDENCIES,
);
expect(await read(imageFor(MAX_LAZY_INFO_DEPENDENCIES + 1))).toMatchObject({
status: "unsupported",
reason: expect.stringContaining("range lookup"),
reason: expect.stringContaining("dependency limit"),
});
});
});
Expand Down
17 changes: 16 additions & 1 deletion src/artifacts/apple/MachoLoadCommandReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export type ReadAt = (offset: number, length: number) => Promise<Uint8Array>;

/** Load commands larger than this are refused instead of buffered. */
export const MAX_LOAD_COMMAND_BYTES = 16 * 1024 * 1024;
/**
* Identical `LC_LAZY_LOAD_DYLIB_INFO` commands are 16 bytes, so the load-command
* byte cap alone would retain about a million dependency results. Stop before
* those results are kept or traced.
*/
export const MAX_LAZY_INFO_DEPENDENCIES = 1024;
const MH_MAGIC = 0xfeedface;
const MH_MAGIC_64 = 0xfeedfacf;
const MH_CIGAM = 0xcefaedfe;
Expand Down Expand Up @@ -389,6 +395,7 @@ const decodeCommands = (
const segments: SegmentSpan[] = [];
let unreadableSegment: number | undefined;
let lazyInfo = false;
let lazyInfoCount = 0;
let offset = 0;
for (let index = 0; index < header.commandCount; index++) {
if (offset + 8 > bytes.byteLength)
Expand All @@ -413,7 +420,15 @@ const decodeCommands = (
if (span === undefined) unreadableSegment ??= index;
else segments.push(span);
Comment on lines +424 to +427

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Skip segment-span allocation without lazy-info commands

When an image has no LC_LAZY_LOAD_DYLIB_INFO, these spans are never used, but a crafted 16 MiB load-command region can contain roughly 300,000 56-byte LC_SEGMENT commands, causing every command to decode a string and retain an object with two bigint values until decoding completes. This adds substantial CPU and heap expansion to ordinary Mach-O parsing solely for metadata needed by the new lazy-info path; first detect whether a lazy-info command exists or otherwise avoid retaining segment spans unless that validation is required.

AGENTS.md reference: AGENTS.md:L77-L77

Useful? React with 👍 / 👎.

}
if (command === LC_LAZY_LOAD_DYLIB_INFO) lazyInfo = true;
if (command === LC_LAZY_LOAD_DYLIB_INFO) {
lazyInfoCount += 1;
if (lazyInfoCount > MAX_LAZY_INFO_DEPENDENCIES)
throw new MachoFormatIssue(
"unsupported",
`LC_LAZY_LOAD_DYLIB_INFO commands exceed the ${MAX_LAZY_INFO_DEPENDENCIES}-dependency limit`,
);
lazyInfo = true;
}
const decoded = decodeCommand(command, body, index, slice);
if (decoded !== undefined) dependencies.push(decoded);
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
offset += size;
Expand Down
Loading