fix(native): bind signature observations to registered target versions (#1099)
* fix(native): bind signature observations to registered target versions * fix(native): preserve target drift and cancellation across signature captures * fix(native): inspect signatures from a verified byte snapshot
This commit is contained in:
@@ -54,6 +54,11 @@ import {
|
||||
type NativeCommandCapture,
|
||||
type NativeCommandRunner,
|
||||
} from "./CommandRunner.js";
|
||||
import {
|
||||
withSignatureTarget,
|
||||
verifySignatureTarget,
|
||||
type SignatureTargetBinding,
|
||||
} from "./SignatureTargetBinding.js";
|
||||
import { parseCodeSignature } from "./parsers/codesign.js";
|
||||
import { parseDemangledSymbols } from "./parsers/demangle.js";
|
||||
import { parseLipoArchitectures } from "./parsers/lipo.js";
|
||||
@@ -318,38 +323,46 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
async #inspectSignature(
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<NativeObservation, AnalysisError>> {
|
||||
const display = await this.#run(
|
||||
"inspect_signature",
|
||||
"codesign",
|
||||
["-d", "--verbose=4", this.target.path],
|
||||
{ signal, acceptNonZero: true },
|
||||
return withSignatureTarget(
|
||||
this.target,
|
||||
(binding) => this.#inspectBoundSignature(binding, signal),
|
||||
signal,
|
||||
);
|
||||
}
|
||||
|
||||
async #inspectBoundSignature(
|
||||
binding: SignatureTargetBinding,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<NativeObservation, AnalysisError>> {
|
||||
const display = await this.#captureSignature(
|
||||
binding,
|
||||
["-d", "--verbose=4"],
|
||||
signal,
|
||||
);
|
||||
if (!display.ok) return display;
|
||||
const unsigned = isNonzeroUnsignedObservation(display.value);
|
||||
const displayFailure = codeSignCaptureFailure(display.value);
|
||||
if (displayFailure !== null) return err(displayFailure);
|
||||
const requirements = await this.#run(
|
||||
"inspect_signature",
|
||||
"codesign",
|
||||
["-d", "-r-", this.target.path],
|
||||
{ signal, acceptNonZero: true },
|
||||
const requirements = await this.#captureSignature(
|
||||
binding,
|
||||
["-d", "-r-"],
|
||||
signal,
|
||||
);
|
||||
if (!requirements.ok) return requirements;
|
||||
const requirementsFailure = codeSignCaptureFailure(requirements.value);
|
||||
if (requirementsFailure !== null) return err(requirementsFailure);
|
||||
const entitlements = await this.#run(
|
||||
"inspect_signature",
|
||||
"codesign",
|
||||
["-d", "--entitlements", ":-", this.target.path],
|
||||
{ signal, acceptNonZero: true },
|
||||
const entitlements = await this.#captureSignature(
|
||||
binding,
|
||||
["-d", "--entitlements", ":-"],
|
||||
signal,
|
||||
);
|
||||
if (!entitlements.ok) return entitlements;
|
||||
const entitlementsFailure = codeSignCaptureFailure(entitlements.value);
|
||||
if (entitlementsFailure !== null) return err(entitlementsFailure);
|
||||
// codesign echoes the resolved executable path in its diagnostics.
|
||||
const parsed = parseCodeSignature(display.value.stderr, unsigned, [
|
||||
this.target.path,
|
||||
...(await canonicalPath(this.target.path)),
|
||||
binding.snapshotPath,
|
||||
...(await canonicalPath(binding.snapshotPath)),
|
||||
]);
|
||||
// The requirement is printed to stdout; stderr echoes the path.
|
||||
const requirementText =
|
||||
@@ -359,6 +372,7 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
const captures = [display.value, requirements.value, entitlements.value];
|
||||
const limitations = [
|
||||
...parsed.limitations,
|
||||
"Inspected a digest-verified Mach-O byte snapshot; filesystem-bound signing context and trust validation are not examined.",
|
||||
...(entitlementValue.omittedPrototypeKeys === 0
|
||||
? []
|
||||
: [
|
||||
@@ -371,9 +385,12 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
isNonzeroUnsignedObservation(entitlements.value));
|
||||
if (mixedSigning) {
|
||||
const slices = await this.#inspectMixedSignatureSlices(
|
||||
binding,
|
||||
parsed.format,
|
||||
requirements.value.exitCode !== 0,
|
||||
entitlements.value.exitCode !== 0,
|
||||
{
|
||||
requirements: requirements.value.exitCode,
|
||||
entitlements: entitlements.value.exitCode,
|
||||
},
|
||||
signal,
|
||||
);
|
||||
if (!slices.ok) return slices;
|
||||
@@ -381,8 +398,10 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
limitations.push(...slices.value.limitations);
|
||||
}
|
||||
const provenance = captures.map((capture) =>
|
||||
invocation(capture, this.target.path),
|
||||
invocation(capture, binding.snapshotPath),
|
||||
);
|
||||
const version = await verifySignatureTarget(this.target, binding, signal);
|
||||
if (!version.ok) return version;
|
||||
const result = inspectSignatureSchema.parse({
|
||||
...parsed,
|
||||
designated_requirement: requirementText,
|
||||
@@ -399,20 +418,22 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
}
|
||||
|
||||
async #inspectMixedSignatureSlices(
|
||||
binding: SignatureTargetBinding,
|
||||
format: string | null,
|
||||
requirementsUnavailable: boolean,
|
||||
entitlementsUnavailable: boolean,
|
||||
aggregateExits: Readonly<{
|
||||
requirements: number | null;
|
||||
entitlements: number | null;
|
||||
}>,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<MixedSignatureSlices, AnalysisError>> {
|
||||
const captures: NativeCommandCapture[] = [];
|
||||
const unsigned: string[] = [];
|
||||
const unclassified: string[] = [];
|
||||
for (const architecture of codeSignArchitectures(format, this.target)) {
|
||||
const slice = await this.#run(
|
||||
"inspect_signature",
|
||||
"codesign",
|
||||
["-d", "-a", architecture, "--verbose=4", this.target.path],
|
||||
{ signal, acceptNonZero: true },
|
||||
const slice = await this.#captureSignature(
|
||||
binding,
|
||||
["-d", "-a", architecture, "--verbose=4"],
|
||||
signal,
|
||||
);
|
||||
if (!slice.ok) return slice;
|
||||
captures.push(slice.value);
|
||||
@@ -430,17 +451,53 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
limitations.push(
|
||||
`Signature state could not be classified for Mach-O slices: ${unclassified.join(", ")}.`,
|
||||
);
|
||||
if (requirementsUnavailable)
|
||||
if (aggregateExits.requirements !== 0)
|
||||
limitations.push(
|
||||
"The aggregate designated requirement is unavailable because Mach-O slices have mixed signing states.",
|
||||
);
|
||||
if (entitlementsUnavailable)
|
||||
if (aggregateExits.entitlements !== 0)
|
||||
limitations.push(
|
||||
"The aggregate entitlements are unavailable because Mach-O slices have mixed signing states.",
|
||||
);
|
||||
return ok({ captures, limitations });
|
||||
}
|
||||
|
||||
async #captureSignature(
|
||||
binding: SignatureTargetBinding,
|
||||
args: readonly string[],
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<NativeCommandCapture, AnalysisError>> {
|
||||
const before = await verifySignatureTarget(this.target, binding, signal);
|
||||
if (!before.ok) return before;
|
||||
let capture: Result<NativeCommandCapture, AnalysisError>;
|
||||
try {
|
||||
capture = await this.#run(
|
||||
"inspect_signature",
|
||||
"codesign",
|
||||
[...args, binding.snapshotPath],
|
||||
{
|
||||
signal,
|
||||
acceptNonZero: true,
|
||||
},
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
capture = err(
|
||||
new ProviderAdapterError(IDENTITY.id, "inspect_signature", {
|
||||
cause,
|
||||
diagnostics: {
|
||||
phase: "codesign-capture",
|
||||
path: this.target.path,
|
||||
reason: cause instanceof Error ? cause.message : String(cause),
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
// Verify failed captures too: a disappearing target is not a codesign
|
||||
// execution failure, and later captures must never use another version.
|
||||
const after = await verifySignatureTarget(this.target, binding, signal);
|
||||
return after.ok ? capture : after;
|
||||
}
|
||||
|
||||
async #inspectPlist(
|
||||
parameters: Readonly<Record<string, JsonValue>>,
|
||||
signal?: AbortSignal,
|
||||
|
||||
Reference in New Issue
Block a user