fix(native): reject ambiguous otool load-command framing

Validate Mach command counts, byte totals, scalar uniqueness, section counts, and build-tool counts before claiming exhaustive evidence. Preserve raw output in typed parsing failures and distinguish dylib commands from the dynamic loader. Correct inconsistent handcrafted fixtures and label their provenance.

Validation: 38 native tests, combined checks, and temporary-directory hygiene. Multiline dylib-name ambiguity was reproduced with the host Apple otool; regression workflows use captured output and injected command runners.
This commit is contained in:
morluto
2026-10-12 05:24:29 +08:00
parent a8d4984404
commit 108b5f9425
10 changed files with 414 additions and 79 deletions
@@ -21,6 +21,7 @@ import {
nativeFixture as fixture,
nativeMachoTarget as machoTarget,
nativeMachoTargetForFile,
withOtoolMachHeader,
} from "../../../fixtures/nativeCommands.js";
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
@@ -143,6 +144,7 @@ it("retains native inventory facts from captured Apple tool output", async () =>
const outputs: Record<string, string> = {};
for (const tool of ["file", "lipo", "otool", "nm", "dwarfdump", "vtool"])
outputs[tool] = await fixture(`dyld-inventory/${tool}.txt`);
outputs.otool = withOtoolMachHeader(outputs.otool ?? "");
outputs["dyld_info:-imports"] = await fixture("dyld-inventory/imports.txt");
const exportsOutput = await fixture("dyld-inventory/exports.txt");
outputs["dyld_info:-exports"] = exportsOutput;
@@ -275,7 +277,8 @@ it("retains symbol names containing spaces in dyld inventory rows", () => {
});
it("preserves numeric-looking segment and section identifiers from otool", () => {
const parsed = parseOtoolLoadCommands(`Load command 1
const parsed = parseOtoolLoadCommands(
withOtoolMachHeader(`Load command 0
cmd LC_SEGMENT_64
cmdsize 152
segname 0001
@@ -299,7 +302,8 @@ Section
flags 0x00000000
reserved1 0
reserved2 0
`);
`),
);
expect(parsed.segments[0]).toMatchObject({
name: "0001",
file_offset: 16384,
@@ -367,7 +371,8 @@ it.each([
])(
"keeps load-command-looking text inside native install names: %s",
(installName) => {
const parsed = parseOtoolLoadCommands(`Load command 0
const parsed = parseOtoolLoadCommands(
withOtoolMachHeader(`Load command 0
cmd LC_ID_DYLIB
cmdsize 80
name ${installName} (offset 24)
@@ -378,7 +383,8 @@ Load command 1
cmd LC_UUID
cmdsize 24
uuid 01234567-89AB-CDEF-0123-456789ABCDEF
`);
`),
);
expect(parsed.commands.map(({ index, kind }) => ({ index, kind }))).toEqual(
[
{ index: 0, kind: "LC_ID_DYLIB" },
@@ -4,20 +4,25 @@ import { NativeMacOSProvider } from "../../../../src/native/NativeMacOSProvider.
import { parseOtoolLoadCommands } from "../../../../src/native/parsers/otool.js";
import {
NativeFixtureRunner,
nativeFixture,
nativeMachoTarget,
withOtoolMachHeader,
} from "../../../fixtures/nativeCommands.js";
it("retains lazily loaded library dependencies", () => {
const load = parseOtoolLoadCommands(
[
"Load command 9",
" cmd LC_LAZY_LOAD_DYLIB",
" name /usr/lib/libLazy.dylib (offset 24)",
" time stamp 2 Thu Jan 1 00:00:02 1970",
" current version 1.0.0",
"compatibility version 1.0.0",
"",
].join("\n"),
withOtoolMachHeader(
[
"Load command 0",
" cmd LC_LAZY_LOAD_DYLIB",
" cmdsize 48",
" name /usr/lib/libLazy.dylib (offset 24)",
" time stamp 2 Thu Jan 1 00:00:02 1970",
" current version 1.0.0",
"compatibility version 1.0.0",
"",
].join("\n"),
),
);
expect(load.dependencies).toContainEqual({
path: "/usr/lib/libLazy.dylib",
@@ -29,15 +34,18 @@ it("retains lazily loaded library dependencies", () => {
it("preserves dependency install names that begin with the word stamp", () => {
const load = parseOtoolLoadCommands(
[
"Load command 2",
" cmd LC_LOAD_DYLIB",
" name stamp plugin.dylib (offset 24)",
" time stamp 2 Thu Jan 1 00:00:02 1970",
" current version 1.0.0",
"compatibility version 1.0.0",
"",
].join("\n"),
withOtoolMachHeader(
[
"Load command 0",
" cmd LC_LOAD_DYLIB",
" cmdsize 48",
" name stamp plugin.dylib (offset 24)",
" time stamp 2 Thu Jan 1 00:00:02 1970",
" current version 1.0.0",
"compatibility version 1.0.0",
"",
].join("\n"),
),
);
expect(load.dependencies).toContainEqual({
path: "stamp plugin.dylib",
@@ -54,20 +62,25 @@ it("preserves dependency install names that begin with the word stamp", () => {
it("normalizes legacy version-minimum commands into build metadata", () => {
const load = parseOtoolLoadCommands(
[
"Load command 3",
" cmd LC_VERSION_MIN_MACOSX",
" version 10.13",
" sdk 10.15",
"Load command 4",
" cmd LC_VERSION_MIN_IPHONEOS",
" version 12.0",
" sdk 12.1",
"Load command 5",
" cmd LC_SOURCE_VERSION",
" version 1.2.3",
"",
].join("\n"),
withOtoolMachHeader(
[
"Load command 0",
" cmd LC_VERSION_MIN_MACOSX",
" cmdsize 16",
" version 10.13",
" sdk 10.15",
"Load command 1",
" cmd LC_VERSION_MIN_IPHONEOS",
" cmdsize 16",
" version 12.0",
" sdk 12.1",
"Load command 2",
" cmd LC_SOURCE_VERSION",
" cmdsize 16",
" version 1.2.3",
"",
].join("\n"),
),
);
expect(load.builds).toEqual([
{
@@ -85,15 +98,30 @@ it("normalizes legacy version-minimum commands into build metadata", () => {
]);
});
it("does not classify LC_LOAD_DYLINKER as a dylib dependency", async () => {
const load = parseOtoolLoadCommands(
await nativeFixture("native-macho-header/otool-header-load.txt"),
);
expect(load.commands.some(({ kind }) => kind === "LC_LOAD_DYLINKER")).toBe(
true,
);
expect(load.dependencies.some(({ path }) => path === "/usr/lib/dyld")).toBe(
false,
);
});
it("limits thread-state entrypoints instead of reporting none silently", async () => {
const runner = new NativeFixtureRunner({
"otool:-h": [
"Load command 0",
" cmd LC_UNIXTHREAD",
" flavor 1",
" count 1",
"",
].join("\n"),
"otool:-h": withOtoolMachHeader(
[
"Load command 0",
" cmd LC_UNIXTHREAD",
" cmdsize 16",
" flavor 1",
" count 1",
"",
].join("\n"),
),
});
const client = new NativeMacOSProvider({}, runner, "darwin").createClient(
nativeMachoTarget("/owned/legacy-fixture"),
@@ -1,6 +1,9 @@
import { expect, it } from "vitest";
import { parseOtoolLoadCommands } from "../../../../src/native/parsers/otool.js";
import { nativeFixture } from "../../../fixtures/nativeCommands.js";
import {
nativeFixture,
withOtoolMachHeader,
} from "../../../fixtures/nativeCommands.js";
it.each([
["decimal-space", "123 library.dylib"],
["hex-space", "0x123 library.dylib"],
@@ -9,7 +12,9 @@ it.each([
["ordinary", "ordinary.dylib"],
])("preserves native install-name text from %s capture", async (slug, name) => {
const load = parseOtoolLoadCommands(
await nativeFixture(`load-command-text-values/${slug}.txt`),
withOtoolMachHeader(
await nativeFixture(`load-command-text-values/${slug}.txt`),
),
);
expect(load.dependencies).toContainEqual({
path: name,
@@ -6,6 +6,7 @@ import {
NativeFixtureRunner,
nativeFixture,
nativeMachoTarget,
withOtoolMachHeader,
} from "../../../fixtures/nativeCommands.js";
it("collects the actual Mach header alongside the native load commands", async () => {
@@ -32,6 +33,36 @@ it("collects the actual Mach header alongside the native load commands", async (
});
});
it("rejects line breaks in a dylib name that manufacture a command and retains otool output", async () => {
const output = [
"/owned/fixture:",
"Mach header",
" magic cputype cpusubtype caps filetype ncmds sizeofcmds flags",
" 0xfeedfacf 16777228 0 0x00 2 1 120 0x00000000",
"Load command 0",
" cmd LC_LOAD_DYLIB",
" cmdsize 120",
" name @rpath/a",
"Load command 77",
" cmd LC_REEXPORT_DYLIB",
" name /evil.dylib (offset 24)",
" time stamp 2 Thu Jan 1 00:00:02 1970",
" current version 1.0.0",
"compatibility version 1.0.0",
"",
].join("\n");
const runner = new NativeFixtureRunner({ "otool:-h": output });
const client = new NativeMacOSProvider({}, runner, "darwin").createClient(
nativeMachoTarget("/owned/fixture"),
);
const result = await client.execute("inspect_macho", {});
if (result.ok) throw new Error("expected malformed otool framing to fail");
expect(result.error).toMatchObject({
_tag: "AnalysisOutputError",
capturedOutput: { stdout: output, stderr: "", truncated: false },
});
});
it("does not reinterpret segment and section flags as Mach header flags", async () => {
const load = parseOtoolLoadCommands(
await nativeFixture("native-macho-header/otool-header-load.txt"),
@@ -56,11 +87,8 @@ it("decodes Mach header fields by their column names", () => {
const reordered = parseOtoolLoadCommands(
[
"Mach header",
" flags filetype magic cputype",
" NOUNDEFS 2 0xfeedfacf 16777228",
"Load command 0",
" cmd LC_UUID",
" uuid 01234567-89ab-cdef-0123-456789abcdef",
" flags filetype magic cputype ncmds sizeofcmds",
" NOUNDEFS 2 0xfeedfacf 16777228 0 0",
].join("\n"),
);
expect(reordered.fileType).toBe("2");
@@ -69,19 +97,83 @@ it("decodes Mach header fields by their column names", () => {
[
"Mach header",
" filetype magic flags ncmds sizeofcmds",
" 2 0xfeedfacf NOUNDEFS DYLDLINK 4 512",
" 2 0xfeedfacf NOUNDEFS DYLDLINK 0 0",
].join("\n"),
);
expect(reorderedMultiwordFlags.fileType).toBe("2");
expect(reorderedMultiwordFlags.flags).toEqual(["DYLDLINK", "NOUNDEFS"]);
const missing = parseOtoolLoadCommands(
"Mach header\n magic cputype flags\n 0xfeedfacf 1 NOUNDEFS\n",
expect(() =>
parseOtoolLoadCommands(
"Mach header\n magic cputype flags\n 0xfeedfacf 1 NOUNDEFS\n",
),
).toThrow(/command framing/u);
expect(() =>
parseOtoolLoadCommands(
"Mach header\n filetype magic flags ncmds sizeofcmds\n 2 0xfeedfacf NOUNDEFS\n",
),
).toThrow(/command framing/u);
});
it("rejects duplicate semantic fields, injected sections, and truncated framing", () => {
const duplicateUuid = withOtoolMachHeader(`Load command 0
cmd LC_UUID
cmdsize 24
uuid 01234567-89ab-cdef-0123-456789abcdef
uuid fedcba98-7654-3210-fedc-ba9876543210
`);
expect(() => parseOtoolLoadCommands(duplicateUuid)).toThrow(/uuid/u);
const injectedSection = withOtoolMachHeader(`Load command 0
cmd LC_SEGMENT_64
cmdsize 152
segname __TEXT
Section
sectname __fake
segname __TEXT
addr 0x1000
size 0x10
offset 4096
align 2^0 (1)
flags 0x0
`);
expect(() => parseOtoolLoadCommands(injectedSection)).toThrow(/vmaddr/u);
const incorrectSectionCount = withOtoolMachHeader(`Load command 0
cmd LC_SEGMENT_64
cmdsize 152
segname __TEXT
vmaddr 0x100000000
vmsize 0x2000
fileoff 0
filesize 8192
maxprot 0x5
initprot 0x5
nsects 0
flags 0x0
Section
sectname __text
segname __TEXT
addr 0x100001000
size 0x100
offset 4096
align 4
flags 0x0
`);
expect(() => parseOtoolLoadCommands(incorrectSectionCount)).toThrow(
/section count/u,
);
expect(missing.fileType).toBeNull();
expect(missing.flags).toEqual(["NOUNDEFS"]);
const incomplete = parseOtoolLoadCommands(
"Mach header\n filetype magic flags ncmds sizeofcmds\n 2 0xfeedfacf NOUNDEFS\n",
);
expect(incomplete.fileType).toBeNull();
expect(incomplete.flags).toEqual([]);
const truncatedCount = [
"Mach header",
" magic cputype cpusubtype caps filetype ncmds sizeofcmds flags",
" 0xfeedfacf 16777228 0 0x00 2 2 48 0x00000000",
"Load command 0",
" cmd LC_UUID",
" cmdsize 24",
" uuid 01234567-89ab-cdef-0123-456789abcdef",
].join("\n");
expect(() => parseOtoolLoadCommands(truncatedCount)).toThrow(/count/u);
const truncatedBytes = truncatedCount.replace("2 2 48", "2 1 32");
expect(() => parseOtoolLoadCommands(truncatedBytes)).toThrow(/sizes/u);
});
@@ -5,11 +5,14 @@ import {
NativeFixtureRunner,
nativeFixture,
nativeMachoTarget,
withOtoolMachHeader,
} from "../../../fixtures/nativeCommands.js";
it("retains native re-export dependencies alongside ordinary and identity commands", async () => {
const runner = new NativeFixtureRunner({
otool: await nativeFixture("native-reexport/otool-load.txt"),
otool: withOtoolMachHeader(
await nativeFixture("native-reexport/otool-load.txt"),
),
});
const client = new NativeMacOSProvider({}, runner, "darwin").createClient(
nativeMachoTarget("/owned/libOuter.dylib"),
+7
View File
@@ -0,0 +1,7 @@
# Representative native load-command fixture
`otool-load.txt` is a compact, handcrafted representative used by the native
fixture runner. Its Mach header is made internally consistent with the five
listed commands (296 command bytes); it is not a capture from a real binary.
Use fixtures under `native-macho-header/` or `native-reexport/` for producer
captures.
+1 -1
View File
@@ -1,7 +1,7 @@
fixture:
Mach header
magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
0xfeedfacf 16777228 0 0x00 EXECUTE 4 512 NOUNDEFS DYLDLINK
0xfeedfacf 16777228 0 0x00 EXECUTE 5 296 NOUNDEFS DYLDLINK
Load command 0
cmd LC_SEGMENT_64
cmdsize 152
+17
View File
@@ -124,3 +124,20 @@ export const nativeMachoTargetForFile = async (
/** Load one captured native tool output. */
export const nativeFixture = (name: string): Promise<string> =>
readFile(new URL(`./native-macos/${name}`, import.meta.url), "utf8");
/** Add consistent Mach command-count metadata to a command-only test capture. */
export const withOtoolMachHeader = (output: string): string => {
const count = [...output.matchAll(/^Load command (\d+)$/gmu)].length;
const sizes = [...output.matchAll(/^\s*cmdsize\s+(\d+)\s*$/gmu)].map(
([, size]) => Number(size),
);
if (sizes.length !== count)
throw new TypeError("command-only fixture must provide every cmdsize");
const bytes = sizes.reduce((sum, size) => sum + size, 0);
return [
"Mach header",
" magic cputype cpusubtype caps filetype ncmds sizeofcmds flags",
` 0xfeedfacf 16777228 0 0x00 2 ${count} ${bytes} 0x00000000`,
output,
].join("\n");
};