mirror of
https://github.com/morluto/rea.git
synced 2026-12-02 13:11:18 -05:00
fix(windows): reject native addons with unusable delay-import tables (#15216)
* fix: deepen verified coverage for PR #14258 * fix(windows): recognize GNU delay-import terminator placement * fix(windows): validate complete delay-import address tables * fix(windows): validate delay-load descriptor targets * fix(windows): validate delay-import function names * fix(windows): bound delay imports to the dedicated IAT section * test(windows): cover delay-import verifier refusals * test(windows): cover nonzero delay-import padding * fix(windows): require readable delay-import descriptors * test(windows): accept read-only delay import names * fix(windows): validate initial delay loader state --------- Co-authored-by: ShuaixinHuang <ShuaixinHuang@users.noreply.github.com> Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com> Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
This commit is contained in:
@@ -844,7 +844,7 @@ jobs:
|
||||
- name: Compute Windows native cache key
|
||||
id: windows-native-key
|
||||
env:
|
||||
SOURCE_HASH: ${{ hashFiles('native/windows/**', 'src/domain/windowsNativeArtifact*.ts', 'src/domain/dosMz*.ts', 'src/domain/peCoffHeader*.ts', 'scripts/build-windows-native.mjs', 'scripts/verify-windows-native-artifact.mjs', 'scripts/lib/verifier-source-loader.mjs', 'tests/fixtures/windows-native-inspection-failure.cc', 'tests/fixtures/windows/processBoundary.cc', '.github/actions/windows-cross-compiler/action.yml', '.github/actions/ubuntu-archive/action.yml', '.github/actions/host-apt-packages/action.yml', 'package.json') }}
|
||||
SOURCE_HASH: ${{ hashFiles('native/windows/**', 'src/domain/windowsNativeArtifact*.ts', 'src/domain/dosMz*.ts', 'src/domain/peCoffHeader*.ts', 'src/domain/peOptionalHeader*.ts', 'src/domain/peSectionHeader*.ts', 'scripts/build-windows-native.mjs', 'scripts/verify-windows-native-artifact.mjs', 'scripts/lib/verifier-source-loader.mjs', 'tests/fixtures/windows-native-inspection-failure.cc', 'tests/fixtures/windows/processBoundary.cc', '.github/actions/windows-cross-compiler/action.yml', '.github/actions/ubuntu-archive/action.yml', '.github/actions/host-apt-packages/action.yml', 'package.json') }}
|
||||
run: |
|
||||
utc_date="$(date -u +%Y-%m-%d)"
|
||||
printf 'key=rea-windows-native-v3-%s-%s-%s-%s-%s-%s\n' \
|
||||
|
||||
@@ -26,6 +26,15 @@ its manifest. Release CI builds both, and `prepublishOnly` rejects a missing,
|
||||
stale, mismatched, or wrong-architecture artifact. A development tarball built
|
||||
without this lane reports Windows native controls as unavailable.
|
||||
|
||||
The build and artifact verifier also require a terminated delay-import directory
|
||||
and one readable, writable delay address table in GNU ld's dedicated
|
||||
`.didat` section. Both tables must end at the same slot. The rest of the
|
||||
dedicated section must be zero padding. The cached module handle must start at
|
||||
zero in writable image data; zero-filled image tails are allowed. Bound and
|
||||
unload table RVAs and the bound timestamp must be zero, matching GNU dlltool
|
||||
output. A refusal means the linker output must be rebuilt with the required
|
||||
MinGW toolchain before publication.
|
||||
|
||||
## Restore the artifact in a source checkout
|
||||
|
||||
The source tree omits `native/windows/build/` from Git. To run Windows
|
||||
|
||||
@@ -16,11 +16,14 @@ import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { importVerifierSource } from "./lib/verifier-source-loader.mjs";
|
||||
|
||||
const { WINDOWS_NATIVE_ARTIFACT, createWindowsNativeManifest } =
|
||||
await importVerifierSource(
|
||||
new URL("../src/domain/windowsNativeArtifact.ts", import.meta.url).href,
|
||||
import.meta.url,
|
||||
);
|
||||
const {
|
||||
WINDOWS_NATIVE_ARTIFACT,
|
||||
createWindowsNativeManifest,
|
||||
validateWindowsNativeDelayImports,
|
||||
} = await importVerifierSource(
|
||||
new URL("../src/domain/windowsNativeArtifact.ts", import.meta.url).href,
|
||||
import.meta.url,
|
||||
);
|
||||
|
||||
const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const nodeVersion = "22.19.0";
|
||||
@@ -180,6 +183,8 @@ const packageJson = JSON.parse(
|
||||
await readFile(join(root, "package.json"), "utf8"),
|
||||
);
|
||||
const bytes = await readFile(join(output, artifact));
|
||||
const delayImports = validateWindowsNativeDelayImports(bytes);
|
||||
if (!delayImports.ok) throw delayImports.error;
|
||||
const created = createWindowsNativeManifest(packageJson.version, bytes);
|
||||
if (!created.ok) throw created.error;
|
||||
const metadata = created.value;
|
||||
|
||||
@@ -20,6 +20,7 @@ const {
|
||||
WINDOWS_NATIVE_ARTIFACT,
|
||||
parseWindowsNativeManifest,
|
||||
validateWindowsNativeArtifact,
|
||||
validateWindowsNativeDelayImports,
|
||||
} = owner;
|
||||
const packageJson = JSON.parse(
|
||||
await readFile(new URL("package.json", root), "utf8"),
|
||||
@@ -37,6 +38,8 @@ const bytes = await readFile(
|
||||
);
|
||||
const valid = validateWindowsNativeArtifact(manifest, bytes);
|
||||
if (!valid.ok) throw valid.error;
|
||||
const delayImports = validateWindowsNativeDelayImports(bytes);
|
||||
if (!delayImports.ok) throw delayImports.error;
|
||||
process.stdout.write(
|
||||
`${JSON.stringify({ ok: true, ...manifest, artifactBytes: bytes.length })}\n`,
|
||||
);
|
||||
|
||||
@@ -1,6 +1,17 @@
|
||||
import { BinaryReader } from "./binaryContainer.js";
|
||||
import { keyedBy, uniqueKeyed } from "./keyedCollection.js";
|
||||
import { observeMzWindowsHeader } from "./dosMz.js";
|
||||
import { readPeCoffHeader } from "./peCoffHeader.js";
|
||||
import { readPeOptionalHeaderIdentity } from "./peOptionalHeader.js";
|
||||
import {
|
||||
readPeOptionalHeaderIdentity,
|
||||
readPeOptionalHeaderStructure,
|
||||
} from "./peOptionalHeader.js";
|
||||
import {
|
||||
PE_SECTION_HEADER_BYTES,
|
||||
peSectionTableExtent,
|
||||
peSectionVirtualExtent,
|
||||
readPeSectionHeader,
|
||||
} from "./peSectionHeader.js";
|
||||
import { err, ok, type Result } from "./result.js";
|
||||
import { sha256Hex } from "../kernel/sha256Hex.js";
|
||||
|
||||
@@ -83,9 +94,7 @@ export const parseWindowsNativeManifest = (
|
||||
});
|
||||
};
|
||||
|
||||
const validateWindowsX64Artifact = (
|
||||
bytes: Buffer,
|
||||
): Result<void, WindowsNativeArtifactError> => {
|
||||
const validateWindowsX64Artifact = (bytes: Buffer) => {
|
||||
if (bytes.length < 64 || bytes.toString("latin1", 0, 2) !== "MZ")
|
||||
return err(
|
||||
new WindowsNativeArtifactError(
|
||||
@@ -120,7 +129,7 @@ const validateWindowsX64Artifact = (
|
||||
"Windows native artifact is not a Windows x64 PE32+ image",
|
||||
),
|
||||
);
|
||||
return ok(undefined);
|
||||
return ok({ optionalHeaderOffset: offset + 24, ...coff.value });
|
||||
};
|
||||
|
||||
/** Bind the parsed manifest to its exact bytes before native code can load. */
|
||||
@@ -134,7 +143,217 @@ export const validateWindowsNativeArtifact = (
|
||||
"Windows native artifact SHA-256 does not match package metadata",
|
||||
),
|
||||
);
|
||||
return validateWindowsX64Artifact(bytes);
|
||||
const valid = validateWindowsX64Artifact(bytes);
|
||||
return valid.ok ? ok(undefined) : valid;
|
||||
};
|
||||
|
||||
const DELAY_IMPORT_DIRECTORY = 13;
|
||||
const DELAY_DESCRIPTOR_BYTES = 32;
|
||||
const PE64_THUNK_BYTES = 8;
|
||||
const PE64_IMPORT_BY_ORDINAL = 1n << 63n;
|
||||
const IMAGE_SCN_MEM_READ = 0x4000_0000;
|
||||
const IMAGE_SCN_MEM_WRITE = 0x8000_0000;
|
||||
|
||||
/** Check the linker output before publication, independently of artifact identity. */
|
||||
export const validateWindowsNativeDelayImports = (
|
||||
bytes: Buffer,
|
||||
): Result<void, WindowsNativeArtifactError> => {
|
||||
const image = validateWindowsX64Artifact(bytes);
|
||||
if (!image.ok) return image;
|
||||
const { optionalHeaderOffset, optionalHeaderSize, sectionCount } =
|
||||
image.value;
|
||||
const { sectionTableOffset, sectionTableBytes } = peSectionTableExtent(
|
||||
optionalHeaderOffset,
|
||||
optionalHeaderSize,
|
||||
sectionCount,
|
||||
);
|
||||
const fail = (message: string) =>
|
||||
err(new WindowsNativeArtifactError(`Windows native artifact ${message}`));
|
||||
const optional = readPeOptionalHeaderStructure(
|
||||
bytes.subarray(optionalHeaderOffset, sectionTableOffset),
|
||||
);
|
||||
const directory = optional.ok
|
||||
? optional.value.dataDirectories[DELAY_IMPORT_DIRECTORY]
|
||||
: undefined;
|
||||
if (
|
||||
directory === undefined ||
|
||||
directory.address === 0 ||
|
||||
directory.size < DELAY_DESCRIPTOR_BYTES ||
|
||||
directory.size % DELAY_DESCRIPTOR_BYTES !== 0
|
||||
)
|
||||
return fail("has no valid delay-import directory; check the MinGW linker");
|
||||
|
||||
const sections: Extract<
|
||||
ReturnType<typeof readPeSectionHeader>,
|
||||
{ ok: true }
|
||||
>["value"][] = [];
|
||||
if (sectionTableOffset + sectionTableBytes > bytes.length)
|
||||
return fail("has a truncated PE section table");
|
||||
for (
|
||||
let offset = sectionTableOffset;
|
||||
offset < sectionTableOffset + sectionTableBytes;
|
||||
offset += PE_SECTION_HEADER_BYTES
|
||||
) {
|
||||
const section = readPeSectionHeader(
|
||||
bytes.subarray(offset, offset + PE_SECTION_HEADER_BYTES),
|
||||
);
|
||||
if (section.ok) sections.push(section.value);
|
||||
}
|
||||
const locate = (
|
||||
rva: number,
|
||||
length: number,
|
||||
backing: "file" | "image" = "file",
|
||||
) => {
|
||||
const candidates = sections.filter(
|
||||
(section) =>
|
||||
rva >= section.virtual_address &&
|
||||
rva - section.virtual_address + length <=
|
||||
(backing === "file"
|
||||
? section.size
|
||||
: peSectionVirtualExtent(section.virtual_size, section.raw_size)) &&
|
||||
section.offset + section.size <= bytes.length,
|
||||
);
|
||||
const selected = uniqueKeyed(
|
||||
keyedBy(candidates, () => rva),
|
||||
rva,
|
||||
);
|
||||
return selected.ok ? selected.value : undefined;
|
||||
};
|
||||
const owner = locate(directory.address, directory.size);
|
||||
if (owner === undefined)
|
||||
return fail("has an unmapped delay-import directory");
|
||||
if ((owner.characteristics & IMAGE_SCN_MEM_READ) === 0)
|
||||
return fail("has an unreadable delay-import directory");
|
||||
const start = owner.offset + directory.address - owner.virtual_address;
|
||||
const reader = new BinaryReader(bytes, true, "pe");
|
||||
const thunkAt = (rva: number, writable: boolean): bigint | undefined => {
|
||||
const section = locate(rva, PE64_THUNK_BYTES);
|
||||
if (
|
||||
section === undefined ||
|
||||
(section.characteristics & IMAGE_SCN_MEM_READ) === 0 ||
|
||||
(writable && (section.characteristics & IMAGE_SCN_MEM_WRITE) === 0)
|
||||
)
|
||||
return undefined;
|
||||
return reader.word(section.offset + rva - section.virtual_address, 64);
|
||||
};
|
||||
// GNU ld puts the delay IAT in its own .didat section. This package
|
||||
// links one delay-import library, so the descriptor must own that extent.
|
||||
const writableIat = (iatRva: number, namesRva: number): boolean => {
|
||||
const section = locate(iatRva, PE64_THUNK_BYTES);
|
||||
if (
|
||||
section?.name !== ".didat" ||
|
||||
sections.filter((candidate) => candidate.name === ".didat").length !==
|
||||
1 ||
|
||||
section.virtual_address !== iatRva ||
|
||||
section.size !==
|
||||
peSectionVirtualExtent(section.virtual_size, section.raw_size) ||
|
||||
section.size % PE64_THUNK_BYTES !== 0 ||
|
||||
locate(iatRva, section.size) !== section ||
|
||||
(section.characteristics & IMAGE_SCN_MEM_WRITE) === 0
|
||||
)
|
||||
return false;
|
||||
let terminated = false;
|
||||
for (let offset = 0; offset < section.size; offset += PE64_THUNK_BYTES) {
|
||||
const address = thunkAt(iatRva + offset, !terminated);
|
||||
if (address === undefined) return false;
|
||||
if (terminated) {
|
||||
if (address !== 0n) return false;
|
||||
continue;
|
||||
}
|
||||
const name = thunkAt(namesRva + offset, false);
|
||||
if (name === undefined || (name === 0n) !== (address === 0n))
|
||||
return false;
|
||||
if (name === 0n) terminated = true;
|
||||
else if (!validImportName(name)) return false;
|
||||
}
|
||||
return terminated;
|
||||
};
|
||||
const readableName = (rva: number, prefixBytes = 0): boolean => {
|
||||
const section = locate(rva, prefixBytes + 1);
|
||||
if (
|
||||
section === undefined ||
|
||||
(section.characteristics & IMAGE_SCN_MEM_READ) === 0
|
||||
)
|
||||
return false;
|
||||
const start = section.offset + rva - section.virtual_address + prefixBytes;
|
||||
return bytes.subarray(start, section.offset + section.size).indexOf(0) > 0;
|
||||
};
|
||||
const validImportName = (thunk: bigint): boolean =>
|
||||
(thunk & PE64_IMPORT_BY_ORDINAL) !== 0n
|
||||
? (thunk & ~(PE64_IMPORT_BY_ORDINAL | 0xffffn)) === 0n
|
||||
: thunk <= 0x7fff_ffffn && readableName(Number(thunk), 2);
|
||||
const writableModuleHandle = (rva: number): boolean => {
|
||||
// The loader may keep its initially zero handle in zero-filled image data.
|
||||
const section = locate(rva, PE64_THUNK_BYTES, "image");
|
||||
if (
|
||||
section === undefined ||
|
||||
(section.characteristics & IMAGE_SCN_MEM_WRITE) === 0
|
||||
)
|
||||
return false;
|
||||
const offset = rva - section.virtual_address;
|
||||
const backedBytes = Math.min(
|
||||
PE64_THUNK_BYTES,
|
||||
Math.max(0, section.size - offset),
|
||||
);
|
||||
const start = section.offset + offset;
|
||||
// The helper treats a nonzero slot as an already-loaded HMODULE.
|
||||
// An unbacked image tail is zero-filled by the loader.
|
||||
return bytes
|
||||
.subarray(start, start + backedBytes)
|
||||
.every((byte) => byte === 0);
|
||||
};
|
||||
const isNullDescriptor = (offset: number) =>
|
||||
bytes
|
||||
.subarray(offset, offset + DELAY_DESCRIPTOR_BYTES)
|
||||
.every((byte) => byte === 0);
|
||||
const end = start + directory.size;
|
||||
for (
|
||||
let descriptor = start;
|
||||
descriptor < end;
|
||||
descriptor += DELAY_DESCRIPTOR_BYTES
|
||||
) {
|
||||
const attributes = reader.u32(descriptor);
|
||||
const nameRva = reader.u32(descriptor + 4);
|
||||
const moduleHandleRva = reader.u32(descriptor + 8);
|
||||
const iatRva = reader.u32(descriptor + 12);
|
||||
const namesRva = reader.u32(descriptor + 16);
|
||||
if (descriptor > start && isNullDescriptor(descriptor))
|
||||
return ok(undefined);
|
||||
if (
|
||||
descriptor !== start ||
|
||||
attributes !== 1 ||
|
||||
nameRva === 0 ||
|
||||
iatRva === 0 ||
|
||||
namesRva === 0
|
||||
)
|
||||
return fail("has an invalid RVA-based delay-import descriptor");
|
||||
// GNU dlltool emits no bound/unload tables or bound timestamp.
|
||||
if (
|
||||
reader.u32(descriptor + 20) !== 0 ||
|
||||
reader.u32(descriptor + 24) !== 0 ||
|
||||
reader.u32(descriptor + 28) !== 0
|
||||
)
|
||||
return fail("has unsupported bound/unload/timestamp delay-import fields");
|
||||
if (!writableIat(iatRva, namesRva))
|
||||
return fail(
|
||||
"has a delay-import address table outside writable PE data or inconsistent with its name table; check the MinGW linker",
|
||||
);
|
||||
if (!readableName(nameRva))
|
||||
return fail("has an unreadable or unterminated delay-import DLL name");
|
||||
if (moduleHandleRva === 0 || !writableModuleHandle(moduleHandleRva))
|
||||
return fail(
|
||||
"has a delay-import module handle outside writable PE data or with a nonzero initial value",
|
||||
);
|
||||
}
|
||||
// GNU ld places the null descriptor immediately after the advertised size.
|
||||
// Require the complete descriptor to remain mapped in the same section.
|
||||
if (
|
||||
locate(directory.address, directory.size + DELAY_DESCRIPTOR_BYTES) ===
|
||||
owner &&
|
||||
isNullDescriptor(end)
|
||||
)
|
||||
return ok(undefined);
|
||||
return fail("has an unterminated delay-import directory");
|
||||
};
|
||||
|
||||
/** Produce metadata only for a Windows x64 PE32+ artifact. */
|
||||
|
||||
@@ -53,6 +53,54 @@ const copyModuleGraph = async (root: string, entry: string): Promise<void> => {
|
||||
}
|
||||
};
|
||||
|
||||
const delayImportFixture = () => {
|
||||
const { bytes: identity, manifest } = windowsNativeArtifactFixture();
|
||||
const optional = 88;
|
||||
const directory = optional + 112 + 13 * 8;
|
||||
const section = optional + 240;
|
||||
const descriptors = 512;
|
||||
const delaySection = section + 40;
|
||||
const delaySlots = descriptors + 192;
|
||||
const bytes = Buffer.alloc(delaySlots + 32);
|
||||
identity.copy(bytes);
|
||||
bytes.writeUInt16LE(2, 70); // NumberOfSections
|
||||
bytes.writeUInt16LE(240, 84); // SizeOfOptionalHeader
|
||||
bytes.writeUInt32LE(16, optional + 108);
|
||||
bytes.writeUInt32LE(0x1000, directory);
|
||||
bytes.writeUInt32LE(64, directory + 4);
|
||||
bytes.writeUInt32LE(192, section + 8); // VirtualSize
|
||||
bytes.writeUInt32LE(0x1000, section + 12);
|
||||
bytes.writeUInt32LE(192, section + 16); // SizeOfRawData
|
||||
bytes.writeUInt32LE(descriptors, section + 20);
|
||||
bytes.writeUInt32LE(0xc000_0040, section + 36);
|
||||
bytes.writeUInt32LE(1, descriptors);
|
||||
bytes.writeUInt32LE(0x10b0, descriptors + 4);
|
||||
bytes.writeUInt32LE(0x10a0, descriptors + 8);
|
||||
bytes.write("fixture.dll\0", descriptors + 176, "ascii");
|
||||
bytes.write(".didat", delaySection, "ascii");
|
||||
bytes.writeUInt32LE(32, delaySection + 8);
|
||||
bytes.writeUInt32LE(0x2000, delaySection + 12);
|
||||
bytes.writeUInt32LE(32, delaySection + 16);
|
||||
bytes.writeUInt32LE(delaySlots, delaySection + 20);
|
||||
bytes.writeUInt32LE(0xc000_0040, delaySection + 36);
|
||||
bytes.writeUInt32LE(0x2000, descriptors + 12);
|
||||
bytes.writeUInt32LE(0x1040, descriptors + 16);
|
||||
bytes.write("One\0", descriptors + 106, "ascii");
|
||||
bytes.writeBigUInt64LE(0x1068n, descriptors + 64);
|
||||
bytes.writeBigUInt64LE((1n << 63n) | 2n, descriptors + 72);
|
||||
bytes.writeBigUInt64LE(1n, delaySlots);
|
||||
bytes.writeBigUInt64LE(2n, delaySlots + 8);
|
||||
return {
|
||||
bytes,
|
||||
manifest,
|
||||
directory,
|
||||
section,
|
||||
descriptors,
|
||||
delaySection,
|
||||
delaySlots,
|
||||
};
|
||||
};
|
||||
|
||||
for (const mode of ["preinstall source", "packed source-free"]) {
|
||||
processTest(
|
||||
`verifies native artifact identity from ${mode} without installed dependencies`,
|
||||
@@ -90,10 +138,24 @@ for (const mode of ["preinstall source", "packed source-free"]) {
|
||||
);
|
||||
const artifactRoot = join(root, "native/windows/build");
|
||||
await mkdir(artifactRoot, { recursive: true });
|
||||
const { bytes, manifest } = windowsNativeArtifactFixture();
|
||||
await writeFile(join(artifactRoot, manifest.artifact), bytes);
|
||||
const {
|
||||
bytes,
|
||||
manifest,
|
||||
directory,
|
||||
section,
|
||||
descriptors,
|
||||
delaySection,
|
||||
delaySlots,
|
||||
} = delayImportFixture();
|
||||
const manifestPath = join(artifactRoot, "manifest.json");
|
||||
await writeFile(manifestPath, JSON.stringify(manifest));
|
||||
const writeArtifact = async (artifact = bytes) => {
|
||||
manifest.artifactSha256 = createHash("sha256")
|
||||
.update(artifact)
|
||||
.digest("hex");
|
||||
await writeFile(join(artifactRoot, manifest.artifact), artifact);
|
||||
await writeFile(manifestPath, JSON.stringify(manifest));
|
||||
};
|
||||
await writeArtifact();
|
||||
const run = () =>
|
||||
processes.run(
|
||||
process.execPath,
|
||||
@@ -107,6 +169,154 @@ for (const mode of ["preinstall source", "packed source-free"]) {
|
||||
...manifest,
|
||||
artifactBytes: bytes.length,
|
||||
});
|
||||
// A module handle can be backed by zero-filled writable image data.
|
||||
bytes.writeUInt32LE(200, section + 8);
|
||||
bytes.writeUInt32LE(0x10c0, descriptors + 8);
|
||||
await writeArtifact();
|
||||
expect((await run()).exitCode).toBe(0);
|
||||
// A partially file-backed slot keeps its virtual tail zero-filled.
|
||||
bytes.writeUInt32LE(0x10bc, descriptors + 8);
|
||||
await writeArtifact();
|
||||
expect((await run()).exitCode).toBe(0);
|
||||
bytes[descriptors + 188] = 1;
|
||||
await writeArtifact();
|
||||
expect((await run()).stderr).toContain("nonzero initial value");
|
||||
bytes[descriptors + 188] = 0;
|
||||
bytes.writeUInt32LE(0x10c0, descriptors + 8);
|
||||
bytes.writeUInt32LE(192, section + 8);
|
||||
|
||||
await writeArtifact();
|
||||
expect((await run()).stderr).toContain(
|
||||
"module handle outside writable PE data",
|
||||
);
|
||||
bytes.writeUInt32LE(0x10a0, descriptors + 8);
|
||||
for (const handle of [1n, 1n << 56n]) {
|
||||
bytes.writeBigUInt64LE(handle, descriptors + 160);
|
||||
await writeArtifact();
|
||||
expect((await run()).stderr).toContain("nonzero initial value");
|
||||
}
|
||||
bytes.writeBigUInt64LE(0n, descriptors + 160);
|
||||
|
||||
for (const fieldOffset of [20, 24, 28]) {
|
||||
bytes.writeUInt32LE(1, descriptors + fieldOffset);
|
||||
await writeArtifact();
|
||||
expect((await run()).stderr).toContain(
|
||||
"unsupported bound/unload/timestamp delay-import fields",
|
||||
);
|
||||
bytes.writeUInt32LE(0, descriptors + fieldOffset);
|
||||
}
|
||||
|
||||
bytes.writeUInt32LE(0x10c0, descriptors + 4);
|
||||
await writeArtifact();
|
||||
expect((await run()).stderr).toContain(
|
||||
"unreadable or unterminated delay-import DLL name",
|
||||
);
|
||||
bytes.writeUInt32LE(0x10b0, descriptors + 4);
|
||||
|
||||
bytes.writeUInt32LE(32, directory + 4);
|
||||
await writeArtifact();
|
||||
expect((await run()).exitCode).toBe(0);
|
||||
|
||||
bytes.writeUInt32LE(1, descriptors + 32);
|
||||
await writeArtifact();
|
||||
const unterminated = await run();
|
||||
expect(unterminated.exitCode).not.toBe(0);
|
||||
expect(unterminated.stderr).toContain(
|
||||
"unterminated delay-import directory",
|
||||
);
|
||||
bytes.writeUInt32LE(0, descriptors + 32);
|
||||
bytes.writeUInt32LE(64, directory + 4);
|
||||
|
||||
// A writable IAT cannot rescue a read-only module-handle slot.
|
||||
bytes.writeUInt32LE(0x4000_0040, section + 36);
|
||||
await writeArtifact();
|
||||
expect((await run()).stderr).toContain(
|
||||
"module handle outside writable PE data",
|
||||
);
|
||||
bytes.writeUInt32LE(0xc000_0040, section + 36);
|
||||
|
||||
// Read-only names are valid when both loader write locations stay writable.
|
||||
const readOnlyNames = Buffer.alloc(bytes.length + 8);
|
||||
bytes.copy(readOnlyNames);
|
||||
readOnlyNames.writeUInt16LE(3, 70);
|
||||
readOnlyNames.writeUInt32LE(0x4000_0040, section + 36);
|
||||
const handleSection = section + 80;
|
||||
readOnlyNames.write(".data", handleSection, "ascii");
|
||||
readOnlyNames.writeUInt32LE(8, handleSection + 8);
|
||||
readOnlyNames.writeUInt32LE(0x3000, handleSection + 12);
|
||||
readOnlyNames.writeUInt32LE(8, handleSection + 16);
|
||||
readOnlyNames.writeUInt32LE(bytes.length, handleSection + 20);
|
||||
readOnlyNames.writeUInt32LE(0xc000_0040, handleSection + 36);
|
||||
readOnlyNames.writeUInt32LE(0x3000, descriptors + 8);
|
||||
await writeArtifact(readOnlyNames);
|
||||
const validReadOnlyNames = await run();
|
||||
expect(validReadOnlyNames.exitCode, validReadOnlyNames.stderr).toBe(0);
|
||||
|
||||
bytes.writeUInt32LE(40, delaySection + 8);
|
||||
await writeArtifact();
|
||||
expect((await run()).stderr).toContain("outside writable PE data");
|
||||
bytes.writeUInt32LE(32, delaySection + 8);
|
||||
|
||||
bytes.writeUInt32LE(0x8000_0040, section + 36);
|
||||
await writeArtifact();
|
||||
const unreadableDirectory = await run();
|
||||
expect(unreadableDirectory.exitCode).not.toBe(0);
|
||||
expect(unreadableDirectory.stderr).toContain(
|
||||
"unreadable delay-import directory",
|
||||
);
|
||||
bytes.writeUInt32LE(0xc000_0040, section + 36);
|
||||
|
||||
// Padding belongs to the checked section even after both tables end.
|
||||
const padding = bytes.length - 1;
|
||||
bytes[padding] = 1;
|
||||
await writeArtifact();
|
||||
const nonzeroPadding = await run();
|
||||
expect(nonzeroPadding.exitCode).not.toBe(0);
|
||||
expect(nonzeroPadding.stderr).toContain(
|
||||
"inconsistent with its name table",
|
||||
);
|
||||
bytes[padding] = 0;
|
||||
|
||||
bytes.writeBigUInt64LE(0n, delaySlots + 8);
|
||||
await writeArtifact();
|
||||
expect((await run()).stderr).toContain(
|
||||
"inconsistent with its name table",
|
||||
);
|
||||
bytes.writeBigUInt64LE(2n, delaySlots + 8);
|
||||
|
||||
bytes.writeUInt32LE(0x4000_0040, delaySection + 36);
|
||||
await writeArtifact();
|
||||
expect((await run()).stderr).toContain("outside writable PE data");
|
||||
bytes.writeUInt32LE(0xc000_0040, delaySection + 36);
|
||||
|
||||
// Keep the package identity valid so the verifier reaches the table checks.
|
||||
const originalName = bytes[descriptors + 106]!;
|
||||
bytes[descriptors + 106] = 0;
|
||||
await writeArtifact();
|
||||
const emptyImportName = await run();
|
||||
expect(emptyImportName.exitCode).not.toBe(0);
|
||||
expect(emptyImportName.stderr).toContain(
|
||||
"inconsistent with its name table",
|
||||
);
|
||||
bytes[descriptors + 106] = originalName;
|
||||
|
||||
bytes.writeUInt32LE(0x8000_0040, delaySection + 36);
|
||||
await writeArtifact();
|
||||
const unreadableIat = await run();
|
||||
expect(unreadableIat.exitCode).not.toBe(0);
|
||||
expect(unreadableIat.stderr).toContain(
|
||||
"inconsistent with its name table",
|
||||
);
|
||||
bytes.writeUInt32LE(0xc000_0040, delaySection + 36);
|
||||
|
||||
bytes.fill(0, directory, directory + 8);
|
||||
await writeArtifact();
|
||||
const missingDirectory = await run();
|
||||
expect(missingDirectory.exitCode).not.toBe(0);
|
||||
expect(missingDirectory.stderr).toContain(
|
||||
"no valid delay-import directory",
|
||||
);
|
||||
|
||||
await writeFile(
|
||||
manifestPath,
|
||||
JSON.stringify({ ...manifest, extra: true }),
|
||||
|
||||
Reference in New Issue
Block a user