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:
morluto
2026-10-12 11:59:23 +08:00
parent d0249282c6
commit 4a4d36f881
9 changed files with 426 additions and 84 deletions
+157
View File
@@ -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" },