fix(evidence): retain unreadable PE and Darwin process facts
Distinguish absent CLI directories from unreadable directory counts and truncated slots while preserving known PE headers and readable CLI data. Retain environment-entry coverage in the Darwin parser and classify restricted empty, unavailable and ambiguous environments at the native boundary. Keep cleanup fail-closed; code-signing flags do not establish ownership. Validation: 116 managed source tests, 13 ownership tests, native Swift parser probe and two live Darwin run-token tests; build, static checks and docs checks pass. Restricted-flag cases use parser fixtures; no real restricted capture is claimed. Related to #2035.
This commit is contained in:
@@ -22,6 +22,163 @@ const metadataLayoutFor = (bytes: Buffer) => {
|
||||
);
|
||||
};
|
||||
|
||||
const resizedOptionalHeader = (
|
||||
size: number,
|
||||
magic: number,
|
||||
): { readonly bytes: Buffer; readonly optional: number } => {
|
||||
const bytes = buildManagedPeFixture();
|
||||
const coff = bytes.readUInt32LE(0x3c) + 4;
|
||||
const optional = coff + 20;
|
||||
const originalSize = bytes.readUInt16LE(coff + 16);
|
||||
const section = Buffer.from(
|
||||
bytes.subarray(optional + originalSize, optional + originalSize + 40),
|
||||
);
|
||||
section.copy(bytes, optional + size);
|
||||
bytes.writeUInt16LE(size, coff + 16);
|
||||
bytes.writeUInt16LE(magic, optional);
|
||||
return { bytes, optional };
|
||||
};
|
||||
|
||||
describe("managed PE optional-header directory count", () => {
|
||||
it.each([96, 108, 111])(
|
||||
"retains an unreadable PE32+ directory count at optional size %i",
|
||||
(optionalSize) => {
|
||||
const { bytes, optional } = resizedOptionalHeader(optionalSize, 0x020b);
|
||||
// The section's first four bytes look like a count that excludes CLI.
|
||||
bytes.writeUInt32LE(14, optional + optionalSize);
|
||||
|
||||
const layout = readManagedPeLayout(bytes);
|
||||
expect(layout).toMatchObject({
|
||||
optionalHeader: "pe32-plus",
|
||||
machine: 0x014c,
|
||||
cli: null,
|
||||
cliDirectoryState: "unreadable",
|
||||
cliIssue: {
|
||||
code: "invalid-directory",
|
||||
scope: "pe.optional-header",
|
||||
},
|
||||
});
|
||||
const result = inspectManagedArtifactBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
expect(result.classification.status).toBe("malformed");
|
||||
expect(result.metadata.status).toBe("malformed");
|
||||
expect(result.coverage.issues).toContainEqual(
|
||||
expect.objectContaining({
|
||||
code: "invalid-directory",
|
||||
scope: "pe.optional-header",
|
||||
}),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
{ size: 96, magic: 0x010b, countOffset: 92, header: "pe32" },
|
||||
{ size: 112, magic: 0x020b, countOffset: 108, header: "pe32-plus" },
|
||||
])("accepts a minimum $header header with no CLI directory", (fixture) => {
|
||||
const { bytes, optional } = resizedOptionalHeader(
|
||||
fixture.size,
|
||||
fixture.magic,
|
||||
);
|
||||
bytes.writeUInt32LE(0, optional + fixture.countOffset);
|
||||
|
||||
expect(readManagedPeLayout(bytes)).toMatchObject({
|
||||
optionalHeader: fixture.header,
|
||||
cli: null,
|
||||
cliDirectoryState: "absent",
|
||||
cliIssue: null,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed CLI directory availability", () => {
|
||||
it("keeps a declared but truncated CLI directory malformed", () => {
|
||||
const bytes = buildManagedPeFixture();
|
||||
const peOffset = bytes.readUInt32LE(0x3c);
|
||||
const coff = peOffset + 4;
|
||||
const optional = coff + 20;
|
||||
const originalOptionalSize = bytes.readUInt16LE(coff + 16);
|
||||
const truncatedOptionalSize = 96 + 14 * 8 + 7;
|
||||
const sectionHeader = Buffer.from(
|
||||
bytes.subarray(
|
||||
optional + originalOptionalSize,
|
||||
optional + originalOptionalSize + 40,
|
||||
),
|
||||
);
|
||||
sectionHeader.copy(bytes, optional + truncatedOptionalSize);
|
||||
bytes.writeUInt16LE(truncatedOptionalSize, coff + 16);
|
||||
bytes.writeUInt32LE(15, optional + 92);
|
||||
|
||||
const layout = readManagedPeLayout(bytes);
|
||||
expect(layout).toMatchObject({
|
||||
cli: null,
|
||||
cliDirectoryState: "unreadable",
|
||||
cliIssue: {
|
||||
code: "invalid-directory",
|
||||
scope: "pe.data-directories",
|
||||
},
|
||||
});
|
||||
|
||||
const target = managedPeFixtureTarget(bytes);
|
||||
const results = [
|
||||
inspectManagedArtifactBytes(bytes, target),
|
||||
inspectManagedMembersBytes(bytes, target),
|
||||
inspectManagedNativeBoundariesBytes(bytes, target),
|
||||
];
|
||||
for (const [index, result] of results.entries()) {
|
||||
expect(result.metadata.status).toBe("malformed");
|
||||
expect(result.coverage).toMatchObject({
|
||||
state: index === 0 ? "partial" : "unavailable",
|
||||
issues: [
|
||||
expect.objectContaining({
|
||||
code: "invalid-directory",
|
||||
scope: "pe.data-directories",
|
||||
}),
|
||||
],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("treats a CLI directory excluded by NumberOfRvaAndSizes as absent", () => {
|
||||
const bytes = buildManagedPeFixture();
|
||||
const peOffset = bytes.readUInt32LE(0x3c);
|
||||
const optional = peOffset + 4 + 20;
|
||||
bytes.writeUInt32LE(14, optional + 92);
|
||||
|
||||
const layout = readManagedPeLayout(bytes);
|
||||
expect(layout).toMatchObject({
|
||||
cli: null,
|
||||
cliDirectoryState: "absent",
|
||||
cliIssue: null,
|
||||
});
|
||||
const result = inspectManagedArtifactBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
expect(result.classification.status).toBe("not-managed");
|
||||
expect(result.metadata.status).toBe("absent");
|
||||
});
|
||||
|
||||
it("retains an in-range CLI directory when the count overstates later entries", () => {
|
||||
const bytes = buildManagedPeFixture();
|
||||
const peOffset = bytes.readUInt32LE(0x3c);
|
||||
const optional = peOffset + 4 + 20;
|
||||
bytes.writeUInt32LE(17, optional + 92);
|
||||
|
||||
const layout = readManagedPeLayout(bytes);
|
||||
expect(layout.cliDirectoryState).toBe("present");
|
||||
expect(layout.cliIssue).toBeNull();
|
||||
expect(layout.cli).not.toBeNull();
|
||||
const result = inspectManagedArtifactBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
expect(result.classification.status).toBe("managed");
|
||||
expect(result.metadata.status).toBe("complete");
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed artifact inventory", () => {
|
||||
it.each([
|
||||
{ moduleRowCount: 0, assemblyRowCount: 1, scope: "metadata.Module" },
|
||||
|
||||
@@ -102,7 +102,7 @@ const unavailableResult = (
|
||||
bytes: Buffer,
|
||||
layout: ManagedPeLayout,
|
||||
): ManagedArtifactInspection => {
|
||||
const malformed = layout.cliDirectoryPresent;
|
||||
const malformed = layout.cliDirectoryState !== "absent";
|
||||
const issues = layout.cliIssue === null ? [] : [layout.cliIssue];
|
||||
const limitations = malformed
|
||||
? [
|
||||
|
||||
@@ -134,7 +134,7 @@ export const inspectManagedNativeBoundariesBytes = (
|
||||
return emptyInspection(
|
||||
target,
|
||||
bytes,
|
||||
pe.cliDirectoryPresent ? "malformed" : "not-managed",
|
||||
pe.cliDirectoryState !== "absent" ? "malformed" : "not-managed",
|
||||
{ issues: pe.cliIssue === null ? [] : [pe.cliIssue] },
|
||||
);
|
||||
const issues: ManagedParseIssue[] = [];
|
||||
|
||||
@@ -39,7 +39,7 @@ export interface ManagedPeLayout {
|
||||
readonly characteristics: number;
|
||||
readonly sections: readonly PeSection[];
|
||||
readonly cli: ManagedCliHeader | null;
|
||||
readonly cliDirectoryPresent: boolean;
|
||||
readonly cliDirectoryState: "absent" | "present" | "unreadable";
|
||||
readonly cliIssue: ManagedParseIssue | null;
|
||||
rvaToOffset(rva: number, size: number, scope: string): number;
|
||||
rvaAvailableBytes(rva: number, scope: string): number;
|
||||
@@ -68,6 +68,11 @@ interface OptionalHeader {
|
||||
readonly directoryOffset: number;
|
||||
}
|
||||
|
||||
type CliDirectoryRead =
|
||||
| { readonly kind: "absent" }
|
||||
| { readonly kind: "present"; readonly directory: PeDataDirectory }
|
||||
| { readonly kind: "unreadable"; readonly issue: ManagedParseIssue };
|
||||
|
||||
const requireRange = (
|
||||
bytes: Buffer,
|
||||
offset: number,
|
||||
@@ -355,15 +360,38 @@ const readCliDirectory = (
|
||||
bytes: Buffer,
|
||||
coff: CoffHeader,
|
||||
optional: OptionalHeader,
|
||||
): PeDataDirectory => {
|
||||
requireRange(bytes, optional.directoryCountOffset, 4, "pe.data-directories");
|
||||
): CliDirectoryRead => {
|
||||
const countOffset = optional.directoryCountOffset - optional.optionalOffset;
|
||||
if (countOffset + 4 > coff.optionalSize)
|
||||
return {
|
||||
kind: "unreadable",
|
||||
issue: {
|
||||
code: "invalid-directory",
|
||||
scope: "pe.optional-header",
|
||||
detail: "PE optional header does not contain its directory count",
|
||||
offset: optional.directoryCountOffset,
|
||||
},
|
||||
};
|
||||
const directoryCount = bytes.readUInt32LE(optional.directoryCountOffset);
|
||||
if (
|
||||
directoryCount <= 14 ||
|
||||
coff.optionalSize < optional.directoryOffset - optional.optionalOffset + 120
|
||||
)
|
||||
return { rva: 0, size: 0 };
|
||||
return readDirectory(bytes, optional.directoryOffset + 14 * 8);
|
||||
if (directoryCount <= 14) return { kind: "absent" };
|
||||
const availableDirectoryCount = Math.floor(
|
||||
(coff.optionalSize - (optional.directoryOffset - optional.optionalOffset)) /
|
||||
8,
|
||||
);
|
||||
if (availableDirectoryCount < 15)
|
||||
return {
|
||||
kind: "unreadable",
|
||||
issue: {
|
||||
code: "invalid-directory",
|
||||
scope: "pe.data-directories",
|
||||
detail: "PE data-directory count exceeds the optional-header extent",
|
||||
offset: optional.directoryCountOffset,
|
||||
},
|
||||
};
|
||||
const directory = readDirectory(bytes, optional.directoryOffset + 14 * 8);
|
||||
return directory.rva !== 0 || directory.size !== 0
|
||||
? { kind: "present", directory }
|
||||
: { kind: "absent" };
|
||||
};
|
||||
|
||||
/** Parse the file-backed PE and CLI layout without loading target code. */
|
||||
@@ -379,26 +407,28 @@ export const readManagedPeLayout = (bytes: Buffer): ManagedPeLayout => {
|
||||
const coff = readCoffHeader(bytes);
|
||||
const optional = readOptionalHeader(bytes, coff);
|
||||
const sections = readSections(bytes, coff, optional);
|
||||
const cliDirectory = readCliDirectory(bytes, coff, optional);
|
||||
const cliDirectoryPresent = cliDirectory.rva !== 0 || cliDirectory.size !== 0;
|
||||
const cliDirectoryRead = readCliDirectory(bytes, coff, optional);
|
||||
const cliDirectoryState = cliDirectoryRead.kind;
|
||||
let cli: ManagedCliHeader | null = null;
|
||||
let cliIssue: ManagedParseIssue | null = null;
|
||||
if (cliDirectoryPresent)
|
||||
let cliIssue: ManagedParseIssue | null =
|
||||
cliDirectoryRead.kind === "unreadable" ? cliDirectoryRead.issue : null;
|
||||
if (cliDirectoryRead.kind === "present") {
|
||||
try {
|
||||
cli = readCliHeader(bytes, sections, cliDirectory);
|
||||
cli = readCliHeader(bytes, sections, cliDirectoryRead.directory);
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof ManagedReaderFailure)) throw cause;
|
||||
cliIssue = cause.issue;
|
||||
}
|
||||
}
|
||||
return {
|
||||
machine: coff.machine,
|
||||
architecture: architectureFor(coff.machine, cliDirectoryPresent),
|
||||
architecture: architectureFor(coff.machine, cliDirectoryState !== "absent"),
|
||||
optionalHeader: optional.optionalHeader,
|
||||
sectionCount: coff.sectionCount,
|
||||
characteristics: coff.characteristics,
|
||||
sections,
|
||||
cli,
|
||||
cliDirectoryPresent,
|
||||
cliDirectoryState,
|
||||
cliIssue,
|
||||
rvaToOffset: (rva, size, scope) =>
|
||||
mapRva({ bytes, sections, rva, size, scope }),
|
||||
|
||||
@@ -138,7 +138,10 @@ describe("opaque process neighbors during cleanup", () => {
|
||||
expect(signalGroup).not.toHaveBeenCalledWith(900, "SIGKILL");
|
||||
});
|
||||
|
||||
it("fails closed for an unrelated unreadable candidate the OS should expose", async () => {
|
||||
it.each([
|
||||
"environment_unavailable",
|
||||
"restricted_process_environment_withheld",
|
||||
])("fails closed for an unrelated candidate with %s", async (diagnostic) => {
|
||||
const processes = [
|
||||
{ pid: 100, parentPid: 1, processGroupId: 100, command: "capture" },
|
||||
{
|
||||
@@ -171,7 +174,7 @@ describe("opaque process neighbors during cleanup", () => {
|
||||
pid === 900
|
||||
? {
|
||||
state: "unavailable" as const,
|
||||
reason: "environment_unavailable",
|
||||
reason: diagnostic,
|
||||
}
|
||||
: { state: "readable" as const, runId: "run-token" },
|
||||
]),
|
||||
@@ -191,13 +194,11 @@ describe("opaque process neighbors during cleanup", () => {
|
||||
|
||||
expect(result).toMatchObject({
|
||||
cleaned: false,
|
||||
reason: expect.stringContaining("environment_unavailable=1"),
|
||||
reason: expect.stringContaining(`${diagnostic}=1`),
|
||||
});
|
||||
if (result.cleaned)
|
||||
throw new Error("Expected unknown ownership to fail closed");
|
||||
expect(result.reason).toContain(
|
||||
"live candidates 900=environment_unavailable",
|
||||
);
|
||||
expect(result.reason).toContain(`live candidates 900=${diagnostic}`);
|
||||
expect(result.reason).not.toContain("unowned");
|
||||
expect(result.reason).not.toContain("run-token");
|
||||
expect(signalGroup.mock.calls).toEqual([[100, "SIGKILL"]]);
|
||||
|
||||
@@ -642,6 +642,8 @@ const cleanupValidationFailure = (
|
||||
const sanitizedTokenReadFailure = (diagnostic: string | undefined): string => {
|
||||
if (diagnostic === "environment_unavailable") return diagnostic;
|
||||
if (diagnostic === "platform_binary_environment_withheld") return diagnostic;
|
||||
if (diagnostic === "restricted_process_environment_withheld")
|
||||
return diagnostic;
|
||||
const environmentErrno = /^(EACCES|EPERM|ENOENT|ESRCH):/u.exec(
|
||||
diagnostic ?? "",
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user