feat(ghidra): enable Windows x64 read-only analysis (#699)

* feat(ghidra): enable Windows x64 read-only analysis

* test(ghidra): exercise native Windows runtime boundaries

* test(ghidra): verify current Windows inventory and isolate ACL observer

* fix(ghidra): align Windows admission and address P0 review
This commit is contained in:
N0zoM1z0
2026-10-06 16:53:33 +08:00
committed by GitHub
parent cf061159ab
commit 26881da76a
65 changed files with 4091 additions and 356 deletions
+150
View File
@@ -0,0 +1,150 @@
#!/usr/bin/env node
import { createHash } from "node:crypto";
import { spawnSync } from "node:child_process";
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const root = join(dirname(fileURLToPath(import.meta.url)), "..");
const nodeVersion = "22.19.0";
const compiler = "x86_64-w64-mingw32-g++";
const cache = join(homedir(), ".cache", "rea-windows-native");
const output = join(root, "native", "windows", "build");
const run = (command, args) => {
const result = spawnSync(command, args, {
cwd: root,
encoding: "utf8",
maxBuffer: 4 * 1_024 * 1_024,
timeout: 120_000,
});
if (result.error !== undefined)
throw new Error(
`Windows native build lane requires ${command}: ${result.error.message}`,
);
if (result.status !== 0)
throw new Error(
`Windows native build failed: ${result.stdout}${result.stderr}`,
);
return result.stdout;
};
if (process.platform !== "linux")
throw new Error(
"The Windows x64 artifact build lane currently requires Linux and MinGW-w64.",
);
run(compiler, ["--version"]);
await mkdir(cache, { recursive: true });
await mkdir(output, { recursive: true });
const exists = async (path) =>
access(path).then(
() => true,
() => false,
);
const fetchBytes = async (name) => {
const response = await fetch(
`https://nodejs.org/dist/v${nodeVersion}/${name}`,
{
signal: AbortSignal.timeout(60_000),
},
);
if (!response.ok)
throw new Error(
`Node build prerequisite download failed: ${name}: HTTP ${response.status}`,
);
return Buffer.from(await response.arrayBuffer());
};
const checksums = (await fetchBytes("SHASUMS256.txt")).toString("utf8");
const verifiedDownload = async (name, path) => {
if (!(await exists(path))) await writeFile(path, await fetchBytes(name));
const digest = createHash("sha256")
.update(await readFile(path))
.digest("hex");
const expected = checksums
.split("\n")
.find((line) => line.slice(66) === name)
?.slice(0, 64);
if (expected === undefined || digest !== expected)
throw new Error(`Node build prerequisite SHA-256 mismatch: ${name}`);
};
const cachedHeaders = join(
homedir(),
".cache",
"node-gyp",
nodeVersion,
"include",
"node",
);
let headers = cachedHeaders;
if (!(await exists(join(headers, "node_api.h")))) {
const archive = join(cache, `node-v${nodeVersion}-headers.tar.gz`);
await verifiedDownload(`node-v${nodeVersion}-headers.tar.gz`, archive);
run("tar", ["xzf", archive, "-C", cache]);
headers = join(cache, `node-v${nodeVersion}`, "include", "node");
}
const artifact = "rea-windows-x64.node";
// MSVC short-import libraries do not produce a correct Node import table with
// GNU ld. Generate a GNU import library for the exact stable Node-API symbols.
const sources = ["addon.cc", "filesystem.cc", "process.cc"];
const sourceText = (
await Promise.all(
sources.map((path) =>
readFile(join(root, "native", "windows", "src", path), "utf8"),
),
)
).join("\n");
const symbols = [
...new Set(
[...sourceText.matchAll(/\b(napi_[a-z0-9_]+)\s*\(/gu)].map(
(match) => match[1],
),
),
].sort();
const definition = join(cache, "node-api.def");
const library = join(cache, "libnode-api.a");
await writeFile(
definition,
`LIBRARY node.exe\nEXPORTS\n${symbols.join("\n")}\n`,
);
run("x86_64-w64-mingw32-dlltool", ["-d", definition, "-l", library]);
run(compiler, [
"-std=c++17",
"-O2",
"-DNAPI_VERSION=8",
"-D_WIN32_WINNT=0x0A00",
"-shared",
"-static",
"-static-libgcc",
"-static-libstdc++",
"-Wl,--no-insert-timestamp",
`-I${headers}`,
"native/windows/src/addon.cc",
"native/windows/src/filesystem.cc",
"native/windows/src/process.cc",
library,
"-ladvapi32",
"-lbcrypt",
"-lntdll",
"-o",
join(output, artifact),
]);
const packageJson = JSON.parse(
await readFile(join(root, "package.json"), "utf8"),
);
const bytes = await readFile(join(output, artifact));
const metadata = {
packageVersion: packageJson.version,
platform: "win32",
architecture: "x64",
abiVersion: 1,
nodeApiVersion: 8,
artifact,
artifactSha256: createHash("sha256").update(bytes).digest("hex"),
};
await writeFile(
join(output, "manifest.json"),
`${JSON.stringify(metadata, null, 2)}\n`,
);
process.stdout.write(
`${JSON.stringify({ ...metadata, artifactBytes: bytes.length })}\n`,
);
+1 -1
View File
@@ -39,7 +39,7 @@ const observeVerifierLineage = async (run, observedAt) => {
run,
observedAt,
null,
"Windows verifier lineage ownership is unavailable without Job Objects",
"The verifier has no retained native Job Object lease for lineage observation; provider jobs are verified by their native owners",
);
try {
const { members, observerPid } = await processSnapshot();
+300
View File
@@ -0,0 +1,300 @@
#!/usr/bin/env node
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import {
mkdtemp,
mkdir,
readFile,
readdir,
rm,
writeFile,
} from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { promisify } from "node:util";
import { pathToFileURL } from "node:url";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
if (process.platform !== "win32" || process.arch !== "x64")
throw new Error(
"Packaged Windows Ghidra acceptance requires a Windows x64 host.",
);
const workspace = await mkdtemp(join(tmpdir(), "rea-packaged-ghidra-"));
const exec = promisify(execFile);
let packageRoot = resolve(process.argv[2] ?? ".");
// The default lane verifies the npm artifact in an isolated prefix. Explicit
// package roots support an already-installed artifact without a second install.
if (process.argv[2] === undefined) {
try {
const npmEntry =
process.env.npm_execpath ??
join(dirname(process.execPath), "node_modules/npm/bin/npm-cli.js");
const packed = JSON.parse(
(
await exec(
process.execPath,
[
npmEntry,
"pack",
"--json",
"--ignore-scripts",
"--pack-destination",
workspace,
],
{ timeout: 120_000, maxBuffer: 8 * 1024 * 1024 },
)
).stdout,
);
assert.equal(
packed.length,
1,
"The package lane must produce one artifact.",
);
const prefix = join(workspace, "installed");
await exec(
process.execPath,
[
npmEntry,
"install",
"--prefix",
prefix,
"--ignore-scripts",
"--omit=dev",
"--no-audit",
"--no-fund",
join(workspace, packed[0].filename),
],
{ timeout: 120_000, maxBuffer: 8 * 1024 * 1024 },
);
packageRoot = join(prefix, "node_modules", "rea-agents");
} catch (cause) {
await rm(workspace, { recursive: true, force: true });
throw cause;
}
}
const target = resolve(
process.argv[3] ?? "build/fixtures/rea-ghidra-windows.exe",
);
let TOOL_CONTRACTS, native, token, sha256;
try {
({ TOOL_CONTRACTS } = await import(
pathToFileURL(join(packageRoot, "dist/contracts/toolContracts.js"))
));
const { requireWindowsNativeAuthority } = await import(
pathToFileURL(join(packageRoot, "dist/windows/WindowsNativeLoader.js"))
);
native = requireWindowsNativeAuthority();
token = native.call("process_caller_token", []);
assert.equal(
token.administrator,
false,
"The real-provider lane must run as an ordinary user.",
);
assert.equal(
token.elevated,
false,
"The real-provider lane must use a non-elevated token.",
);
const digest = (bytes) => createHash("sha256").update(bytes).digest("hex");
sha256 = digest(await readFile(target));
} catch (cause) {
await rm(workspace, { recursive: true, force: true });
throw cause;
}
const digest = (bytes) => createHash("sha256").update(bytes).digest("hex");
const runtimeParent = join(workspace, "runtime with spaces");
await mkdir(runtimeParent);
const environment = {
...process.env,
REA_ANALYSIS_PROVIDER: "ghidra",
REA_LOG_LEVEL: "error",
GHIDRA_HEADLESS_MAXMEM: "512m",
NODE_OPTIONS: "--max-old-space-size=512",
TEMP: runtimeParent,
TMP: runtimeParent,
HOME: workspace,
USERPROFILE: workspace,
APPDATA: join(workspace, "appdata"),
LOCALAPPDATA: join(workspace, "localappdata"),
};
const entry = join(packageRoot, "scripts/rea.mjs");
const report = {
ok: false,
platform: "win32",
architecture: "x64",
nonAdmin: true,
targetSha256: sha256,
cli: {},
mcpOperations: [],
nativeControls: "available",
runtimeCleanup: false,
};
const assertCleanup = async () => {
const entries = await readdir(runtimeParent);
report.runtimeResidualCount = entries.length;
report.runtimeResidualKinds = entries.map((name) =>
name.startsWith("rea-")
? "rea-runtime"
: name.startsWith("hsperfdata_")
? "jvm-performance-data"
: "other",
);
assert.equal(entries.length, 0, "Provider runtime cleanup left files.");
};
try {
const providers = JSON.parse(
(
await exec(process.execPath, [entry, "providers", "--json"], {
env: environment,
timeout: 30_000,
})
).stdout,
);
assert.ok(JSON.stringify(providers).includes("ghidra"));
report.cli.providers = true;
const inspected = await exec(
process.execPath,
[entry, "inspect", target, "--provider", "ghidra", "--format", "json"],
{ env: environment, timeout: 360_000, maxBuffer: 8 * 1024 * 1024 },
);
const inspection = JSON.parse(inspected.stdout);
assert.ok(inspection.error === undefined);
report.cli.inspect = true;
await assertCleanup();
const transport = new StdioClientTransport({
command: process.execPath,
args: [entry, "mcp"],
env: environment,
stderr: "pipe",
});
const client = new Client({
name: "rea-packaged-windows-ghidra",
version: "1.0.0",
});
const call = async (name, arguments_) => {
report.lastOperation = name;
if (process.argv[4] !== undefined)
await writeFile(process.argv[4], `${JSON.stringify(report)}\n`);
const contract = TOOL_CONTRACTS.find((value) => value.name === name);
assert.ok(contract !== undefined, `Missing canonical contract: ${name}`);
const argumentsParsed = contract.inputSchema.parse(arguments_);
const result = await client.callTool(
{ name, arguments: argumentsParsed },
{ timeout: 360_000 },
);
assert.notEqual(result.isError, true, `Packaged operation failed: ${name}`);
const structured =
result.structuredContent ??
JSON.parse(result.content.find((item) => item.type === "text").text);
const parsed = contract.outputSchema.parse(structured);
report.mcpOperations.push(name);
return parsed.result;
};
try {
await client.connect(transport);
await call("open_binary", { path: target, provider_id: "ghidra" });
assert.equal((await call("list_documents", {})).length, 1);
await call("list_names", {});
const procedures = await call("list_procedures", {});
assert.ok(procedures.length > 0);
assert.ok((await call("list_segments", {})).length > 0);
await call("list_strings", {});
const procedure =
procedures.find((item) => item.procedure?.external === false) ??
procedures[0];
assert.ok(procedure !== undefined);
const loadImage = await call("inspect_native_load_image", {});
assert.equal(
loadImage.status,
"unsupported",
"PE load-image attestation must remain explicitly unsupported.",
);
assert.ok(loadImage.observations !== undefined);
const memory = await call("read_bytes", {
address: procedure.address,
length: 16,
});
const mapping = await call("address_to_file_offset", {
address: procedure.address,
});
assert.equal(memory.complete, true);
assert.equal(memory.returned_bytes, 16);
assert.equal(
memory.bytes_hex,
(await readFile(target))
.subarray(mapping.file_offset, mapping.file_offset + 16)
.toString("hex"),
);
await call("address_name", { address: procedure.address });
assert.equal(
await call("procedure_address", { procedure: procedure.value }),
procedure.address,
);
await call("resolve_containing_procedure", { address: procedure.address });
await call("search_procedures", { pattern: procedure.value });
await call("search_strings", { pattern: "rea" });
for (const name of [
"procedure_assembly",
"procedure_callees",
"procedure_callers",
"procedure_info",
"procedure_pseudo_code",
"read_function_instructions",
"procedure_references",
"analyze_function",
])
await call(name, { procedure: procedure.value });
await call("xrefs", { address: procedure.address });
await call("inspect_native_data_type", { type: "/undefined8" });
await call("inspect_native_instruction", { address: procedure.address });
await call("resolve_native_call_targets", { address: procedure.address });
const expected = (
await import(
pathToFileURL(join(packageRoot, "dist/ghidra/GhidraSessionValues.js"))
)
).GHIDRA_SESSION_CAPABILITIES.filter(
(name) => name !== "ping" && name !== "shutdown",
);
assert.ok(
expected.every((name) => report.mcpOperations.includes(name)),
"Packaged verification omitted an admitted operation.",
);
await call("binary_session", {});
await call("close_binary", {});
await assertCleanup();
} finally {
await client.close();
}
assert.equal(
digest(await readFile(target)),
sha256,
"Analysis modified the selected target.",
);
assert.deepEqual(
native.call("process_caller_token", []),
token,
"Analysis changed caller token authority.",
);
report.runtimeCleanup = true;
report.ok = true;
if (process.argv[4] !== undefined)
await writeFile(process.argv[4], `${JSON.stringify(report)}\n`);
process.stdout.write(`${JSON.stringify(report)}\n`);
} catch (cause) {
report.failure = {
name: cause.name,
message: cause.message
.replaceAll(packageRoot, "<package>")
.replaceAll(target, "<target>")
.replaceAll(workspace, "<workspace>"),
};
if (process.argv[4] !== undefined)
await writeFile(process.argv[4], `${JSON.stringify(report)}\n`);
throw cause;
} finally {
await rm(workspace, { recursive: true, force: true });
}
+49 -18
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import { access } from "node:fs/promises";
import { access, readFile } from "node:fs/promises";
import { isAbsolute, resolve } from "node:path";
import { fileURLToPath } from "node:url";
@@ -74,6 +74,7 @@ if (!profile.ok || profile.value.profile === null)
throw new Error("Windows Ghidra profile could not be committed");
const client = new GhidraClient({
platform: installation.platform,
launcher: new GhidraHeadlessLauncher({
analyzeHeadlessPath: installation.analyzeHeadlessPath,
...(process.env.JAVA_HOME === undefined
@@ -113,19 +114,41 @@ try {
if (
!Array.isArray(documents) ||
documents.length !== 1 ||
!pageHasItems(procedures) ||
!Array.isArray(procedures) ||
procedures.length === 0 ||
!Array.isArray(segments) ||
segments.length === 0 ||
!isPage(names) ||
!isPage(strings)
!Array.isArray(names) ||
!Array.isArray(strings)
)
throw new Error("Windows Ghidra inventory proof was incomplete");
const procedure = procedures.items.find(
const procedure = procedures.find(
(item) => item.procedure?.external === false,
);
if (procedure === undefined)
throw new Error("Windows Ghidra fixture exposed no local procedure");
await inventory("inspect_native_load_image", {});
const memory = await inventory("read_bytes", {
address: procedure.address,
length: 16,
});
const mapping = await inventory("address_to_file_offset", {
address: procedure.address,
});
const sourceBytes = await readFile(targetPath);
if (
memory.complete !== true ||
memory.returned_bytes !== 16 ||
memory.bytes_hex !==
sourceBytes
.subarray(mapping.file_offset, mapping.file_offset + 16)
.toString("hex")
)
throw new Error(
"Windows loaded bytes disagree with the observed source-file mapping",
);
await inventory("address_name", { address: procedure.address });
const resolvedAddress = await inventory("procedure_address", {
procedure: procedure.value,
@@ -149,6 +172,13 @@ try {
])
await functionOperation(operation, { procedure: procedure.value });
await functionOperation("xrefs", { address: procedure.address });
await functionOperation("inspect_native_data_type", { type: "/undefined8" });
await functionOperation("inspect_native_instruction", {
address: procedure.address,
});
await functionOperation("resolve_native_call_targets", {
address: procedure.address,
});
await functionOperation("analyze_function", {
procedure: procedure.value,
});
@@ -178,11 +208,16 @@ try {
transport: "authenticated-loopback-tcp",
operations: [...observed].sort((left, right) => left.localeCompare(right)),
cleanup: "complete",
native_controls: {
job_object: true,
protected_dacl: true,
handle_admission: true,
},
limitations: [
"approved-non-sensitive-fixtures-only",
"no-job-object-ownership",
"no-private-dacl-proof",
"no-reparse-point-authority",
"windows-x64-local-ntfs-only",
"native-x86-64-pe-applications-only",
"no-gui-or-mutation-authority",
],
};
} finally {
@@ -226,7 +261,9 @@ function assertSession(session, targetSha256, profileDigest) {
session.read_only !== true ||
session.analysis_complete !== true ||
session.analysis_timed_out !== false ||
session.capabilities.join(",") !== SESSION_CAPABILITIES.join(",")
session.capabilities.length !== SESSION_CAPABILITIES.length ||
new Set(session.capabilities).size !== SESSION_CAPABILITIES.length ||
SESSION_CAPABILITIES.some((name) => !session.capabilities.includes(name))
)
throw new Error(
`Windows Ghidra session commitment drifted: ${JSON.stringify(session)}`,
@@ -240,7 +277,9 @@ function assertRuntimeCoordinates(runtime, targetSha256) {
typeof runtime.runtime_root !== "string" ||
typeof runtime.endpoint_path !== "string" ||
typeof runtime.project_root !== "string" ||
typeof runtime.process_id !== "number"
typeof runtime.process_id !== "number" ||
typeof runtime.target_admission !== "object" ||
runtime.target_admission === null
)
throw new Error(
`Windows Ghidra runtime coordinates are incomplete: ${JSON.stringify(runtime)}`,
@@ -262,14 +301,6 @@ async function assertCleanup(runtime) {
);
}
const isPage = (value) =>
typeof value === "object" &&
value !== null &&
!Array.isArray(value) &&
Array.isArray(value.items);
const pageHasItems = (value) => isPage(value) && value.items.length > 0;
async function exists(path) {
try {
await access(path);
@@ -0,0 +1,39 @@
#!/usr/bin/env node
import { createHash } from "node:crypto";
import { readFile } from "node:fs/promises";
const root = new URL("../", import.meta.url);
const packageJson = JSON.parse(
await readFile(new URL("package.json", root), "utf8"),
);
const manifest = JSON.parse(
await readFile(new URL("native/windows/build/manifest.json", root), "utf8"),
);
const bytes = await readFile(
new URL("native/windows/build/rea-windows-x64.node", root),
);
if (
manifest.packageVersion !== packageJson.version ||
manifest.platform !== "win32" ||
manifest.architecture !== "x64" ||
manifest.abiVersion !== 1 ||
manifest.nodeApiVersion !== 8 ||
manifest.artifact !== "rea-windows-x64.node" ||
createHash("sha256").update(bytes).digest("hex") !== manifest.artifactSha256
)
throw new Error(
"Windows native artifact metadata or SHA-256 does not match this package; run the Windows native build lane.",
);
const offset = bytes.length >= 64 ? bytes.readUInt32LE(0x3c) : bytes.length;
if (
bytes.toString("ascii", 0, 2) !== "MZ" ||
offset > bytes.length - 26 ||
bytes.toString("ascii", offset, offset + 4) !== "PE\0\0" ||
bytes.readUInt16LE(offset + 4) !== 0x8664 ||
bytes.readUInt16LE(offset + 24) !== 0x20b
)
throw new Error("Windows native artifact must be a Windows x64 PE32+ image.");
process.stdout.write(
`${JSON.stringify({ ok: true, ...manifest, artifactBytes: bytes.length })}\n`,
);
+427
View File
@@ -0,0 +1,427 @@
#!/usr/bin/env node
import assert from "node:assert/strict";
import { execFileSync, spawn } from "node:child_process";
import { createHash } from "node:crypto";
import {
access,
mkdir,
mkdtemp,
readFile,
rename,
rm,
symlink,
writeFile,
open,
} from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { pathToFileURL } from "node:url";
if (process.platform !== "win32")
throw new Error(
"Windows native conformance requires a real Windows x64 host.",
);
const packageRoot = resolve(process.argv[2] ?? ".");
const { requireWindowsNativeAuthority } = await import(
pathToFileURL(join(packageRoot, "dist/windows/WindowsNativeLoader.js"))
);
const native = requireWindowsNativeAuthority();
const { WindowsPrivateRuntime } = await import(
pathToFileURL(join(packageRoot, "dist/windows/WindowsPrivateRuntime.js"))
);
const workspace = await mkdtemp(join(tmpdir(), "rea-native-conformance-"));
const environment = Object.entries(process.env)
.filter(([, value]) => value !== undefined)
.map(([key, value]) => `${key}=${value}`);
const quote = (value) =>
`"${value.replace(/(\\*)"/gu, '$1$1\\"').replace(/(\\+)$/u, "$1$1")}"`;
const delay = () => new Promise((resolve) => setTimeout(resolve, 25));
const exists = (path) =>
access(path).then(
() => true,
() => false,
);
const live = (pid) => {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
};
const launch = (arguments_) =>
native.call("process_spawn", [
process.execPath,
[process.execPath, ...arguments_].map(quote).join(" "),
workspace,
environment,
]);
const wait = async (child) => {
let output = "";
for (let attempt = 0; attempt < 400; attempt++) {
const state = native.call("process_poll", [child.handle]);
output += state.stdout.toString();
if (
state.exitCode !== null &&
state.activeProcesses === 0 &&
state.stdoutEnded &&
state.stderrEnded
)
return { ...state, output };
await delay();
}
throw new Error("Owned job did not settle within the fixture deadline.");
};
const report = { ok: false, artifact: "verified", controls: {}, processes: {} };
try {
const sourceDirectory = join(workspace, "source");
await mkdir(sourceDirectory);
const source = join(sourceDirectory, "target.bin");
await writeFile(source, Buffer.alloc(1024 * 1024, 0x37));
const snapshotOwner = WindowsPrivateRuntime.create(
workspace,
"single-flight-",
);
try {
const rejectedController = new AbortController();
const accepted = snapshotOwner.snapshot(source, "accepted.bin");
const rejected = snapshotOwner.snapshot(
source,
"rejected.bin",
rejectedController.signal,
);
rejectedController.abort();
await assert.rejects(rejected, /Runtime snapshot is still pending/u);
const completed = await accepted;
assert.equal(
completed.sha256,
createHash("sha256")
.update(await readFile(source))
.digest("hex"),
);
assert.equal(
await exists(join(snapshotOwner.observation.path, "rejected.bin")),
false,
);
report.controls.singleFlightCancellationOwnership = true;
} finally {
await snapshotOwner.close();
}
const unrelatedFile = join(workspace, "unrelated-file");
await writeFile(unrelatedFile, "preserve unrelated writes");
const admitted = native.call("open", [source]);
try {
await assert.rejects(
rename(sourceDirectory, `${sourceDirectory}-replacement`),
);
await assert.rejects(open(source, "r+"));
await rename(unrelatedFile, `${unrelatedFile}-renamed`);
if (process.argv[3] !== undefined) {
const observation = JSON.parse(
execFileSync(
resolve(process.argv[3]),
["reparse-directory", sourceDirectory, workspace],
{ encoding: "utf8" },
),
);
assert.equal(
observation.reparseConversionDenied,
true,
"An admitted ancestor was converted into a junction.",
);
report.controls.inPlaceAncestorReparseDenied = true;
}
assert.throws(() => native.call("read", [{}, 0, 1]));
assert.throws(() => native.call("read", [admitted.handle, NaN, 1]));
assert.equal(native.call("read", [admitted.handle, 0, 1])[0], 0x37);
} finally {
native.call("close", [admitted.handle]);
}
assert.throws(() => native.call("read", [admitted.handle, 0, 1]));
const junction = join(workspace, "junction");
await symlink(sourceDirectory, junction, "junction");
assert.throws(
() => native.call("open", [join(junction, "target.bin")]),
/Reparse/u,
);
assert.throws(() => native.call("open", [source + ":stream"]));
const runtime = native.call("runtime_create", [
workspace,
"private root with spaces-",
]);
const sentinel = join(sourceDirectory, "sentinel");
await writeFile(sentinel, "preserve");
try {
const acl = JSON.parse(
execFileSync(
"powershell.exe",
[
"-NoProfile",
"-NonInteractive",
"-Command",
"$a=Get-Acl -LiteralPath $env:REA_TEST_PRIVATE_RUNTIME_PATH; $u=[Security.Principal.WindowsIdentity]::GetCurrent().User; @{protected=$a.AreAccessRulesProtected; ownerMatchesUser=($a.GetOwner([Security.Principal.SecurityIdentifier]).Value -eq $u.Value); trusteesOnlyUserAndSystem=(@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]) | Where-Object {$_.IdentityReference.Value -ne $u.Value -and $_.IdentityReference.Value -ne 'S-1-5-18'}).Count -eq 0); count=@($a.Access).Count} | ConvertTo-Json -Compress",
],
{
encoding: "utf8",
// A pwsh caller can supply module paths incompatible with the
// Windows PowerShell observer. Let that observer build its own path.
env: {
...Object.fromEntries(
Object.entries(process.env).filter(
([name]) => name.toLowerCase() !== "psmodulepath",
),
),
REA_TEST_PRIVATE_RUNTIME_PATH: runtime.path,
},
},
),
);
assert.deepEqual(acl, {
protected: true,
ownerMatchesUser: true,
trusteesOnlyUserAndSystem: true,
count: 2,
});
native.call("runtime_mkdir", [runtime.handle, "inputs"]);
native.call("runtime_write", [
runtime.handle,
"descriptor.json",
Buffer.from("fixture"),
]);
assert.throws(() =>
native.call("runtime_write", [
runtime.handle,
"descriptor.json",
Buffer.from("replace"),
]),
);
assert.throws(() =>
native.call("runtime_mkdir", [runtime.handle, "..\\escape"]),
);
const pending = native.call("runtime_snapshot", [
runtime.handle,
source,
"inputs\\target.bin",
]);
assert.ok(pending instanceof Promise);
assert.throws(
() => native.call("runtime_close", [runtime.handle]),
/pending/u,
);
const snapshot = await pending;
assert.equal(
snapshot.sha256,
createHash("sha256")
.update(await readFile(source))
.digest("hex"),
);
assert.equal(snapshot.source.requestedPath, source);
await assert.rejects(
writeFile(join(runtime.path, "inputs", "target.bin"), "replace"),
);
await symlink(
sourceDirectory,
join(runtime.path, "outside-junction"),
"junction",
);
const cancelled = native.call("runtime_snapshot", [
runtime.handle,
source,
"cancelled.bin",
]);
native.call("runtime_snapshot_cancel", [runtime.handle]);
await assert.rejects(cancelled, /cancelled/u);
report.controls = {
...report.controls,
protectedDaclReadback: true,
sourceReplacementDenied: true,
sourceWriteDenied: true,
unrelatedFilesystemRenamePreserved: true,
junctionAdmissionDenied: true,
alternateStreamsDenied: true,
forgedAndClosedHandlesDenied: true,
immutableSnapshotDigest: true,
asynchronousSnapshot: true,
snapshotCancellation: true,
};
} finally {
native.call("runtime_close", [runtime.handle]);
}
assert.equal(await exists(runtime.path), false);
assert.equal(await readFile(sentinel, "utf8"), "preserve");
report.controls.cleanupDoesNotTraverseJunctions = true;
const fixture = join(workspace, "tree.mjs");
await writeFile(
fixture,
`import {spawn} from 'node:child_process'; import {writeFileSync} from 'node:fs';
const depth=Number(process.argv[2]); writeFileSync(process.argv[3]+depth,String(process.pid));
if(depth>0)spawn(process.execPath,[import.meta.filename,String(depth-1),process.argv[3]],{detached:true,stdio:'ignore'});
setInterval(()=>{},1000);\n`,
);
const pidPrefix = join(workspace, "pid-");
const child = launch(["--max-old-space-size=128", fixture, "2", pidPrefix]);
const unrelated = spawn(
process.execPath,
["--max-old-space-size=128", "-e", "setInterval(()=>{},1000)"],
{ stdio: "ignore" },
);
try {
for (
let attempt = 0;
attempt < 200 && !(await exists(pidPrefix + "0"));
attempt++
)
await delay();
const pids = await Promise.all(
[0, 1, 2].map(async (depth) =>
Number(await readFile(pidPrefix + depth, "utf8")),
),
);
assert.ok(native.call("process_poll", [child.handle]).activeProcesses >= 3);
native.call("process_terminate", [child.handle]);
assert.equal((await wait(child)).activeProcesses, 0);
assert.equal(pids.some(live), false);
assert.ok(unrelated.pid !== undefined && live(unrelated.pid));
report.processes.descendantsAndDetachedDescendantsTerminated = true;
report.processes.unrelatedProcessPreserved = true;
} finally {
native.call("process_terminate", [child.handle]);
await wait(child);
native.call("process_close", [child.handle]);
if (unrelated.exitCode === null) {
const closed = new Promise((resolve) => unrelated.once("close", resolve));
unrelated.kill();
await closed;
}
}
const ownerClose = launch([
"--max-old-space-size=128",
fixture,
"0",
join(workspace, "owner-close-"),
]);
native.call("process_close", [ownerClose.handle]);
for (let attempt = 0; attempt < 100 && live(ownerClose.pid); attempt++)
await delay();
assert.equal(live(ownerClose.pid), false);
report.processes.killOnOwnerClose = true;
const exited = launch([
"--max-old-space-size=128",
"-e",
"console.log('observed');process.exit(7)",
]);
const settled = await wait(exited);
assert.equal(settled.exitCode, 7);
assert.match(settled.output, /observed/u);
native.call("process_close", [exited.handle]);
report.processes.normalExitAndOutput = true;
const boundaryFixture = process.argv[3];
if (boundaryFixture !== undefined) {
const before = JSON.parse(
execFileSync(boundaryFixture, [], { encoding: "utf8" }),
);
const boundary = native.call("process_spawn", [
boundaryFixture,
`${quote(boundaryFixture)} breakaway`,
workspace,
environment,
]);
const denied = await wait(boundary);
native.call("process_close", [boundary.handle]);
assert.equal(JSON.parse(denied.output).breakawayDenied, true);
const tokenChild = native.call("process_spawn", [
boundaryFixture,
quote(boundaryFixture),
workspace,
environment,
]);
const observed = await wait(tokenChild);
native.call("process_close", [tokenChild.handle]);
assert.equal(JSON.parse(observed.output).defaultOwnerIsUser, true);
const after = JSON.parse(
execFileSync(boundaryFixture, [], { encoding: "utf8" }),
);
assert.deepEqual(after, before);
report.processes.breakawayDenied = true;
report.processes.childOwnerNormalizedWithoutChangingCaller = true;
report.callerToken = before;
const environmentChild = native.call("process_spawn", [
boundaryFixture,
`${quote(boundaryFixture)} environment`,
workspace,
[
"z_REA=z",
"_REA=underscore",
"A_REA=a",
"REA_CASE=first",
"rea_case=last",
"REA_ß=sharp",
"REA_SS=double",
],
]);
const childEnvironment = await wait(environmentChild);
native.call("process_close", [environmentChild.handle]);
assert.deepEqual(JSON.parse(childEnvironment.output), {
ordinallySorted: true,
lastDuplicateSelected: true,
unicodeNamesPreserved: true,
});
report.processes.windowsEnvironmentSemantics = true;
}
const ownerFixture = join(workspace, "owner.mjs");
const ownerPidFile = join(workspace, "crash-child.pid");
const moduleUrl = pathToFileURL(
join(packageRoot, "dist/windows/WindowsNativeLoader.js"),
).href;
await writeFile(
ownerFixture,
`import {writeFileSync} from 'node:fs';
import {requireWindowsNativeAuthority} from ${JSON.stringify(moduleUrl)};
const native=requireWindowsNativeAuthority();
const env=Object.entries(process.env).map(([k,v])=>k+'='+v);
const child=native.call('process_spawn',[process.execPath,${JSON.stringify([process.execPath, "--max-old-space-size=128", fixture, "0", join(workspace, "crash-descendant-")].map(quote).join(" "))},${JSON.stringify(workspace)},env]);
writeFileSync(${JSON.stringify(ownerPidFile)},String(child.pid));setInterval(()=>{},1000);\n`,
);
const owner = spawn(
process.execPath,
["--max-old-space-size=128", ownerFixture],
{ stdio: "ignore" },
);
try {
for (
let attempt = 0;
attempt < 200 && !(await exists(ownerPidFile));
attempt++
)
await delay();
const pid = Number(await readFile(ownerPidFile, "utf8"));
const closed = new Promise((resolve) => owner.once("close", resolve));
owner.kill("SIGKILL");
await closed;
for (let attempt = 0; attempt < 100 && live(pid); attempt++) await delay();
assert.equal(live(pid), false);
report.processes.ownerForcedExitKillsOwnedChild = true;
} finally {
if (owner.exitCode === null && owner.signalCode === null) {
const closed = new Promise((resolve) => owner.once("close", resolve));
owner.kill("SIGKILL");
await closed;
}
}
report.ok = true;
if (process.argv[4] !== undefined)
await writeFile(process.argv[4], `${JSON.stringify(report)}\n`);
process.stdout.write(`${JSON.stringify(report)}\n`);
} catch (cause) {
console.error(
"Native conformance failed",
cause.code ?? cause.name,
cause.constraint ?? "assertion",
);
throw cause;
} finally {
await rm(workspace, { recursive: true, force: true }).catch((cause) =>
console.error("Fixture cleanup failed", cause.code),
);
}
+2
View File
@@ -49,6 +49,8 @@ try {
const packagedPaths = new Set(packageResult.files.map(({ path }) => path));
for (const required of [
"bridge/ghidra/ReaGhidraBridge.java",
"native/windows/build/manifest.json",
"native/windows/build/rea-windows-x64.node",
"dist/main.js",
"dist/cli.js",
"scripts/rea.mjs",