Merge pull request #887 from morluto/fix/managed-native-boundaries-non-managed
fix(dotnet): inspect native boundaries of non-managed and malformed PEs
This commit is contained in:
@@ -320,19 +320,21 @@ const boundaryKind = (
|
|||||||
return "mixed-or-unknown";
|
return "mixed-or-unknown";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** CLI header facets of a PE without an admitted CLI header. */
|
||||||
|
export const NO_CLI_NATIVE: ManagedNativeBoundaryInspection["cli_native"] = {
|
||||||
|
il_only: false,
|
||||||
|
requires_32bit: false,
|
||||||
|
strong_name_signed: false,
|
||||||
|
native_entry_point: false,
|
||||||
|
ready_to_run_signature: false,
|
||||||
|
managed_native_header_rva: 0,
|
||||||
|
managed_native_header_size: 0,
|
||||||
|
};
|
||||||
|
|
||||||
export const cliNative = (
|
export const cliNative = (
|
||||||
pe: ManagedPeLayout,
|
pe: ManagedPeLayout,
|
||||||
): ManagedNativeBoundaryInspection["cli_native"] => {
|
): ManagedNativeBoundaryInspection["cli_native"] => {
|
||||||
if (pe.cli === null)
|
if (pe.cli === null) return NO_CLI_NATIVE;
|
||||||
return {
|
|
||||||
il_only: false,
|
|
||||||
requires_32bit: false,
|
|
||||||
strong_name_signed: false,
|
|
||||||
native_entry_point: false,
|
|
||||||
ready_to_run_signature: false,
|
|
||||||
managed_native_header_rva: 0,
|
|
||||||
managed_native_header_size: 0,
|
|
||||||
};
|
|
||||||
return {
|
return {
|
||||||
il_only: (pe.cli.flags & 0x0000_0001) !== 0,
|
il_only: (pe.cli.flags & 0x0000_0001) !== 0,
|
||||||
requires_32bit: (pe.cli.flags & 0x0000_0002) !== 0,
|
requires_32bit: (pe.cli.flags & 0x0000_0002) !== 0,
|
||||||
@@ -344,6 +346,22 @@ export const cliNative = (
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Summarize declaration counts with the native facets of the CLI header. */
|
||||||
|
export const nativeBoundarySummary = (
|
||||||
|
native: ManagedNativeBoundaryInspection["cli_native"],
|
||||||
|
counts: Pick<
|
||||||
|
ManagedNativeBoundaryInspection["summary"],
|
||||||
|
"module_ref_count" | "pinvoke_import_count" | "native_implementation_count"
|
||||||
|
>,
|
||||||
|
): ManagedNativeBoundaryInspection["summary"] => ({
|
||||||
|
...counts,
|
||||||
|
ready_to_run: native.ready_to_run_signature,
|
||||||
|
mixed_mode_or_native_header:
|
||||||
|
native.managed_native_header_rva !== 0 ||
|
||||||
|
native.managed_native_header_size !== 0 ||
|
||||||
|
native.native_entry_point,
|
||||||
|
});
|
||||||
|
|
||||||
interface BoundaryInspectionContext {
|
interface BoundaryInspectionContext {
|
||||||
readonly target: BinaryTarget;
|
readonly target: BinaryTarget;
|
||||||
readonly bytes: Buffer;
|
readonly bytes: Buffer;
|
||||||
@@ -390,16 +408,11 @@ export const buildNativeBoundaryInspection = ({
|
|||||||
module_refs: moduleRefs,
|
module_refs: moduleRefs,
|
||||||
pinvoke_imports: imports,
|
pinvoke_imports: imports,
|
||||||
native_implementations: implementations,
|
native_implementations: implementations,
|
||||||
summary: {
|
summary: nativeBoundarySummary(native, {
|
||||||
module_ref_count: moduleRefs.length,
|
module_ref_count: moduleRefs.length,
|
||||||
pinvoke_import_count: imports.length,
|
pinvoke_import_count: imports.length,
|
||||||
native_implementation_count: implementations.length,
|
native_implementation_count: implementations.length,
|
||||||
ready_to_run: native.ready_to_run_signature,
|
}),
|
||||||
mixed_mode_or_native_header:
|
|
||||||
native.managed_native_header_rva !== 0 ||
|
|
||||||
native.managed_native_header_size !== 0 ||
|
|
||||||
native.native_entry_point,
|
|
||||||
},
|
|
||||||
coverage: {
|
coverage: {
|
||||||
state: issues.length === 0 ? "complete" : "partial",
|
state: issues.length === 0 ? "complete" : "partial",
|
||||||
issues,
|
issues,
|
||||||
|
|||||||
@@ -101,4 +101,60 @@ describe("managed native boundaries", () => {
|
|||||||
expect.objectContaining({ code: "invalid-metadata-root" }),
|
expect.objectContaining({ code: "invalid-metadata-root" }),
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("reports unavailable boundaries for native and malformed PE files", () => {
|
||||||
|
const nativeBytes = buildNativePeFixture();
|
||||||
|
const native = inspectManagedNativeBoundariesBytes(
|
||||||
|
nativeBytes,
|
||||||
|
managedPeFixtureTarget(nativeBytes),
|
||||||
|
);
|
||||||
|
expect(native).toMatchObject({
|
||||||
|
metadata: { status: "absent" },
|
||||||
|
pinvoke_imports: [],
|
||||||
|
coverage: { state: "unavailable", issues: [] },
|
||||||
|
});
|
||||||
|
|
||||||
|
const malformedBytes = buildManagedPeFixture({
|
||||||
|
corruptMetadataSignature: true,
|
||||||
|
readyToRun: true,
|
||||||
|
});
|
||||||
|
const malformed = inspectManagedNativeBoundariesBytes(
|
||||||
|
malformedBytes,
|
||||||
|
managedPeFixtureTarget(malformedBytes),
|
||||||
|
);
|
||||||
|
expect(malformed).toMatchObject({
|
||||||
|
metadata: { status: "malformed" },
|
||||||
|
cli_native: {
|
||||||
|
il_only: true,
|
||||||
|
ready_to_run_signature: true,
|
||||||
|
managed_native_header_rva: 0x2700,
|
||||||
|
managed_native_header_size: 4,
|
||||||
|
},
|
||||||
|
summary: { ready_to_run: true, mixed_mode_or_native_header: true },
|
||||||
|
coverage: {
|
||||||
|
state: "unavailable",
|
||||||
|
issues: [expect.objectContaining({ code: "invalid-metadata-root" })],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(malformed.limitations).not.toContainEqual(
|
||||||
|
expect.stringContaining("CLI header was not admitted"),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("marks CLI header facets as defaults when the CLI header is unreadable", () => {
|
||||||
|
const bytes = buildManagedPeFixture();
|
||||||
|
bytes.writeUInt32LE(0x7fff_0000, 0x84 + 20 + 96 + 14 * 8);
|
||||||
|
const result = inspectManagedNativeBoundariesBytes(
|
||||||
|
bytes,
|
||||||
|
managedPeFixtureTarget(bytes),
|
||||||
|
);
|
||||||
|
expect(result).toMatchObject({
|
||||||
|
metadata: { status: "malformed" },
|
||||||
|
cli_native: { il_only: false },
|
||||||
|
coverage: { state: "unavailable", issues: [expect.anything()] },
|
||||||
|
});
|
||||||
|
expect(result.limitations).toContainEqual(
|
||||||
|
expect.stringContaining("CLI header was not admitted"),
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ import { ManagedReaderFailure } from "./ManagedReaderFailure.js";
|
|||||||
import {
|
import {
|
||||||
buildNativeBoundaryInspection,
|
buildNativeBoundaryInspection,
|
||||||
cliNative,
|
cliNative,
|
||||||
|
NO_CLI_NATIVE,
|
||||||
|
nativeBoundarySummary,
|
||||||
nativeImplementations,
|
nativeImplementations,
|
||||||
parseFields,
|
parseFields,
|
||||||
parseImplMaps,
|
parseImplMaps,
|
||||||
@@ -29,13 +31,25 @@ import {
|
|||||||
|
|
||||||
type Inventory = ReturnType<typeof readManagedMetadataInventory>;
|
type Inventory = ReturnType<typeof readManagedMetadataInventory>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Report a PE whose CLI metadata is absent or cannot be admitted. `native`
|
||||||
|
* holds the CLI header facts when the header itself was admitted; otherwise
|
||||||
|
* the header is absent (`not-managed`) or unreadable (`malformed`).
|
||||||
|
*/
|
||||||
const emptyInspection = (
|
const emptyInspection = (
|
||||||
target: BinaryTarget,
|
target: BinaryTarget,
|
||||||
bytes: Buffer,
|
bytes: Buffer,
|
||||||
classification: "not-managed" | "malformed" | "not-available" | "managed",
|
classification: "not-managed" | "malformed",
|
||||||
issues: readonly ManagedParseIssue[] = [],
|
{
|
||||||
): ManagedNativeBoundaryInspection =>
|
native = null,
|
||||||
managedNativeBoundaryInspectionSchema.parse({
|
issues = [],
|
||||||
|
}: {
|
||||||
|
readonly native?: ManagedNativeBoundaryInspection["cli_native"] | null;
|
||||||
|
readonly issues?: readonly ManagedParseIssue[];
|
||||||
|
} = {},
|
||||||
|
): ManagedNativeBoundaryInspection => {
|
||||||
|
const header = native ?? NO_CLI_NATIVE;
|
||||||
|
return managedNativeBoundaryInspectionSchema.parse({
|
||||||
artifact: {
|
artifact: {
|
||||||
path: target.path,
|
path: target.path,
|
||||||
sha256: target.sha256,
|
sha256: target.sha256,
|
||||||
@@ -44,10 +58,7 @@ const emptyInspection = (
|
|||||||
},
|
},
|
||||||
module: null,
|
module: null,
|
||||||
metadata: {
|
metadata: {
|
||||||
status:
|
status: classification === "malformed" ? "malformed" : "absent",
|
||||||
classification === "malformed" || classification === "not-available"
|
|
||||||
? "malformed"
|
|
||||||
: "absent",
|
|
||||||
version: null,
|
version: null,
|
||||||
table_row_counts: {},
|
table_row_counts: {},
|
||||||
},
|
},
|
||||||
@@ -56,31 +67,27 @@ const emptyInspection = (
|
|||||||
requires_artifact_sha256: target.sha256,
|
requires_artifact_sha256: target.sha256,
|
||||||
requires_mvid: null,
|
requires_mvid: null,
|
||||||
},
|
},
|
||||||
cli_native: {
|
cli_native: header,
|
||||||
il_only: false,
|
|
||||||
requires_32bit: false,
|
|
||||||
strong_name_signed: false,
|
|
||||||
native_entry_point: false,
|
|
||||||
ready_to_run_signature: false,
|
|
||||||
managed_native_header_rva: 0,
|
|
||||||
managed_native_header_size: 0,
|
|
||||||
},
|
|
||||||
module_refs: [],
|
module_refs: [],
|
||||||
pinvoke_imports: [],
|
pinvoke_imports: [],
|
||||||
native_implementations: [],
|
native_implementations: [],
|
||||||
summary: {
|
summary: nativeBoundarySummary(header, {
|
||||||
module_ref_count: 0,
|
module_ref_count: 0,
|
||||||
pinvoke_import_count: 0,
|
pinvoke_import_count: 0,
|
||||||
native_implementation_count: 0,
|
native_implementation_count: 0,
|
||||||
ready_to_run: false,
|
}),
|
||||||
mixed_mode_or_native_header: false,
|
coverage: { state: "unavailable", issues },
|
||||||
},
|
|
||||||
coverage: { state: classification, issues },
|
|
||||||
limitations: [
|
limitations: [
|
||||||
"No CLI data was admitted; native boundary declarations are unavailable.",
|
"No CLI metadata was admitted; native boundary declarations are unavailable.",
|
||||||
|
...(native === null && classification === "malformed"
|
||||||
|
? [
|
||||||
|
"The CLI header was not admitted, so cli_native and the summary's ready_to_run and mixed_mode_or_native_header values are defaults, not observations.",
|
||||||
|
]
|
||||||
|
: []),
|
||||||
"Static inspection does not load or execute target code, so native export resolution is not performed.",
|
"Static inspection does not load or execute target code, so native export resolution is not performed.",
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const readBoundaryInventory = (
|
const readBoundaryInventory = (
|
||||||
bytes: Buffer,
|
bytes: Buffer,
|
||||||
@@ -126,14 +133,16 @@ export const inspectManagedNativeBoundariesBytes = (
|
|||||||
pe = readManagedPeLayout(bytes);
|
pe = readManagedPeLayout(bytes);
|
||||||
} catch (cause: unknown) {
|
} catch (cause: unknown) {
|
||||||
if (!(cause instanceof ManagedReaderFailure)) throw cause;
|
if (!(cause instanceof ManagedReaderFailure)) throw cause;
|
||||||
return emptyInspection(target, bytes, "malformed", [cause.issue]);
|
return emptyInspection(target, bytes, "malformed", {
|
||||||
|
issues: [cause.issue],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (pe.cli === null)
|
if (pe.cli === null)
|
||||||
return emptyInspection(
|
return emptyInspection(
|
||||||
target,
|
target,
|
||||||
bytes,
|
bytes,
|
||||||
pe.cliDirectoryPresent ? "malformed" : "not-managed",
|
pe.cliDirectoryPresent ? "malformed" : "not-managed",
|
||||||
pe.cliIssue === null ? [] : [pe.cliIssue],
|
{ issues: pe.cliIssue === null ? [] : [pe.cliIssue] },
|
||||||
);
|
);
|
||||||
let layout: ManagedMetadataLayout;
|
let layout: ManagedMetadataLayout;
|
||||||
let inventory: Inventory;
|
let inventory: Inventory;
|
||||||
@@ -141,7 +150,10 @@ export const inspectManagedNativeBoundariesBytes = (
|
|||||||
({ layout, inventory } = readBoundaryInventory(bytes, pe, pe.cli));
|
({ layout, inventory } = readBoundaryInventory(bytes, pe, pe.cli));
|
||||||
} catch (cause: unknown) {
|
} catch (cause: unknown) {
|
||||||
if (!(cause instanceof ManagedReaderFailure)) throw cause;
|
if (!(cause instanceof ManagedReaderFailure)) throw cause;
|
||||||
return emptyInspection(target, bytes, "malformed", [cause.issue]);
|
return emptyInspection(target, bytes, "malformed", {
|
||||||
|
native: cliNative(pe),
|
||||||
|
issues: [cause.issue],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
const heapExtent = Math.max(layout.strings.size, layout.blob.size);
|
const heapExtent = Math.max(layout.strings.size, layout.blob.size);
|
||||||
const moduleRefs = parseModuleRefs(bytes, layout, heapExtent);
|
const moduleRefs = parseModuleRefs(bytes, layout, heapExtent);
|
||||||
|
|||||||
Reference in New Issue
Block a user