fix: correct native inventories and bridge error classification (#503)
* fix(hopper): preserve normalized inventory addresses * test(hopper): isolate real inventory verification * fix(native): preserve otool addresses and section alignment * fix: preserve address spaces in call path evidence * fix(hopper): distinguish request validation from provider faults * fix(native): retain tool inventory facts and reject failed signatures
This commit is contained in:
@@ -330,23 +330,34 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
{ signal, acceptNonZero: true },
|
||||
);
|
||||
if (!display.ok) return display;
|
||||
const unsigned = /not signed at all|code object is not signed/iu.test(
|
||||
`${display.value.stdout}\n${display.value.stderr}`,
|
||||
);
|
||||
if (!unsigned && display.value.exitCode !== 0)
|
||||
return err(
|
||||
translateCommandFailure(
|
||||
"inspect_signature",
|
||||
new NativeCommandFailure(
|
||||
"codesign",
|
||||
"nonzero-exit",
|
||||
display.value.exitCode,
|
||||
),
|
||||
),
|
||||
);
|
||||
const requirements = await this.#run(
|
||||
"inspect_signature",
|
||||
"codesign",
|
||||
["-d", "-r-", this.target.path],
|
||||
{ signal, acceptNonZero: true },
|
||||
{ signal, acceptNonZero: unsigned },
|
||||
);
|
||||
if (!requirements.ok) return requirements;
|
||||
const entitlements = await this.#run(
|
||||
"inspect_signature",
|
||||
"codesign",
|
||||
["-d", "--entitlements", ":-", this.target.path],
|
||||
{ signal, acceptNonZero: true },
|
||||
{ signal, acceptNonZero: unsigned },
|
||||
);
|
||||
if (!entitlements.ok) return entitlements;
|
||||
const unsigned = /not signed at all|code object is not signed/iu.test(
|
||||
`${display.value.stdout}\n${display.value.stderr}`,
|
||||
);
|
||||
const parsed = parseCodeSignature(
|
||||
`${display.value.stdout}\n${display.value.stderr}`,
|
||||
unsigned,
|
||||
|
||||
@@ -89,9 +89,14 @@ const normalizeMacho = (
|
||||
const architectures = parseLipoArchitectures(byTool("lipo").stdout);
|
||||
const load = parseOtoolLoadCommands(byTool("otool").stdout);
|
||||
const imports = parseDyldSymbols(byTool("dyld_info", 0).stdout, "imports");
|
||||
const imageBase =
|
||||
load.segments.find(
|
||||
(segment) => segment.file_offset === 0 && (segment.file_size ?? 0) > 0,
|
||||
)?.vm_address ?? null;
|
||||
const dyldExports = parseDyldSymbols(
|
||||
byTool("dyld_info", 1).stdout,
|
||||
"exports",
|
||||
imageBase,
|
||||
);
|
||||
const exports = uniqueSymbols([
|
||||
...dyldExports,
|
||||
@@ -109,6 +114,12 @@ const normalizeMacho = (
|
||||
]
|
||||
: []),
|
||||
...additionalLimitations,
|
||||
...(imageBase === null &&
|
||||
/^\s*offset\s+symbol\s*$/mu.test(byTool("dyld_info", 1).stdout)
|
||||
? [
|
||||
"Export virtual addresses are unavailable because no file-backed image base was observed.",
|
||||
]
|
||||
: []),
|
||||
];
|
||||
return inspectMachoSchema.parse({
|
||||
format: "mach-o",
|
||||
@@ -185,7 +196,8 @@ const uniqueSymbols = <Value extends { readonly name: string }>(
|
||||
items: readonly Value[],
|
||||
): Value[] => {
|
||||
const unique = new Map<string, Value>();
|
||||
for (const item of items) unique.set(item.name, item);
|
||||
for (const item of items)
|
||||
if (!unique.has(item.name)) unique.set(item.name, item);
|
||||
return [...unique.values()].sort((left, right) =>
|
||||
left.name.localeCompare(right.name),
|
||||
);
|
||||
|
||||
@@ -1,29 +1,68 @@
|
||||
/** Parse bounded imports/exports from dyld_info's line-oriented tables. */
|
||||
export const parseDyldSymbols = (output: string, mode: "imports" | "exports") =>
|
||||
output.split(/\r?\n/u).flatMap((rawLine) => {
|
||||
interface ParsedDyldSymbol {
|
||||
readonly name: string;
|
||||
readonly address: string | null;
|
||||
readonly weak: boolean | null;
|
||||
readonly reexport: boolean | null;
|
||||
readonly source: string | null;
|
||||
}
|
||||
|
||||
/** Decode Apple dyld inventory rows, projecting export offsets to VM addresses. */
|
||||
export const parseDyldSymbols = (
|
||||
output: string,
|
||||
mode: "imports" | "exports",
|
||||
imageBase: string | null = null,
|
||||
) => {
|
||||
const offsets = /^\s*offset\s+symbol\s*$/mu.test(output);
|
||||
return output.split(/\r?\n/u).flatMap<ParsedDyldSymbol>((rawLine) => {
|
||||
const line = rawLine.trim();
|
||||
if (
|
||||
line.length === 0 ||
|
||||
/^(?:imports|exports|binding|address|segment|ordinal)\b/iu.test(line)
|
||||
)
|
||||
if (mode === "imports") {
|
||||
const imported =
|
||||
/^(?:0x[\da-f]+\s+)?(\S+)(?:\s+\[([^\]]+)\])?\s+\(from (.+)\)$/iu.exec(
|
||||
line,
|
||||
);
|
||||
if (imported?.[1] !== undefined)
|
||||
return [
|
||||
{
|
||||
name: imported[1],
|
||||
address: null,
|
||||
weak: /\bweak-import\b/u.test(imported[2] ?? "") ? true : null,
|
||||
reexport: null,
|
||||
source: imported[3] ?? null,
|
||||
},
|
||||
];
|
||||
return [];
|
||||
const tokens = line.split(/\s+/u);
|
||||
const name = tokens.at(-1);
|
||||
if (name === undefined || !/^(?:_|\$s|objc_|swift_)/u.test(name)) return [];
|
||||
const addressToken = tokens.find((token) =>
|
||||
/^0x[a-fA-F0-9]+$/u.test(token),
|
||||
);
|
||||
}
|
||||
const reexport = /^\[re-export\]\s+(\S+)(?:\s+\(from (.+)\))?$/u.exec(line);
|
||||
if (reexport?.[1] !== undefined)
|
||||
return [
|
||||
{
|
||||
name: reexport[1],
|
||||
address: null,
|
||||
weak: null,
|
||||
reexport: true,
|
||||
source: reexport[2] ?? null,
|
||||
},
|
||||
];
|
||||
const exported = /^(0x[\da-f]+)\s+(\S+)(?:\s+\[([^\]]+)\])?$/iu.exec(line);
|
||||
if (exported?.[1] === undefined || exported[2] === undefined) return [];
|
||||
const absolute = /\babsolute\b/u.test(exported[3] ?? "");
|
||||
const address =
|
||||
offsets && !absolute
|
||||
? imageBase === null
|
||||
? null
|
||||
: `0x${(BigInt(imageBase) + BigInt(exported[1])).toString(16)}`
|
||||
: canonicalHex(exported[1]);
|
||||
return [
|
||||
{
|
||||
name,
|
||||
address: addressToken ?? null,
|
||||
weak: /\bweak\b/iu.test(line) ? true : null,
|
||||
reexport: /\bre-?export\b/iu.test(line)
|
||||
? true
|
||||
: mode === "exports"
|
||||
? false
|
||||
: null,
|
||||
source: mode === "imports" ? (tokens.at(-2) ?? null) : null,
|
||||
name: exported[2],
|
||||
address,
|
||||
weak: /\bweak-def\b/u.test(exported[3] ?? "") ? true : null,
|
||||
reexport: false,
|
||||
source: null,
|
||||
},
|
||||
];
|
||||
});
|
||||
};
|
||||
|
||||
const canonicalHex = (value: string | undefined): string | null =>
|
||||
value === undefined ? null : `0x${BigInt(value).toString(16)}`;
|
||||
|
||||
@@ -207,7 +207,7 @@ const parseSections = (block: string): ParsedSection[] =>
|
||||
address: hexField(fields, "addr"),
|
||||
size: numberField(fields, "size"),
|
||||
file_offset: numberField(fields, "offset"),
|
||||
alignment: sectionAlignment(numberField(fields, "align")),
|
||||
alignment: sectionAlignment(stringField(fields, "align")),
|
||||
flags: (stringField(fields, "flags") ?? "")
|
||||
.split(/\s+/u)
|
||||
.filter((flag) => flag.length > 0),
|
||||
@@ -285,12 +285,16 @@ const hexField = (
|
||||
fields: Readonly<Record<string, string | number | null>>,
|
||||
name: string,
|
||||
): string | null => {
|
||||
const value = numberField(fields, name);
|
||||
return value === null ? null : `0x${value.toString(16)}`;
|
||||
const value = stringField(fields, name);
|
||||
if (value === null || !/^(?:0x[a-fA-F0-9]+|\d+)$/u.test(value)) return null;
|
||||
return `0x${BigInt(value).toString(16)}`;
|
||||
};
|
||||
|
||||
const stripOffsetSuffix = (value: string | null): string =>
|
||||
value?.replace(/\s+\(offset\s+\d+\)$/u, "") ?? "unknown";
|
||||
|
||||
const sectionAlignment = (exponent: number | null): number | null =>
|
||||
exponent === null || exponent > 52 ? null : 2 ** exponent;
|
||||
const sectionAlignment = (raw: string | null): number | null => {
|
||||
if (raw === null) return null;
|
||||
const exponent = numeric(/^2\^(\d+)(?:\s+\(\d+\))?$/u.exec(raw)?.[1] ?? raw);
|
||||
return exponent === null || exponent > 52 ? null : 2 ** exponent;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user