Files
rea/tests/acceptance/kernelModuleCli.test.ts
T
Matt Van Horn dba8a31978 feat(elf): inspect Linux kernel module metadata (#7010)
* feat(elf): inspect Linux kernel module metadata

Adds inspect-kernel-module and inspect_kernel_module for ELF32/ELF64
ET_REL Linux kernel modules, read statically without executing them.
Reports ordered .modinfo entries with duplicates preserved, __versions
symbol CRCs, the presence of .gnu.linkonce.this_module and __ksymtab,
.init.text and .exit.text sizes, and an appended module signature
trailer with its PKCS#7 byte range and SHA-256. gzip, xz and zstd
containers are reported unsupported with external decompression as the
next probe. Malformed headers and exhausted entry budgets return no
partial success.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016y53PdpyMAp9RSW65fatHe

* fix(elf): bound kernel module ksymtab listing and tighten contracts

Charge listed __ksymtab* sections against max_entries, omit section names
longer than 256 bytes with ksymtab_name_exceeds_limit, and aggregate the
non-file-backed limitation so repeated huge names cannot inflate Evidence.
Use fixedTupleSchema for the signature pad bytes with projection parity
tests, and assert the late-cancellation reason and retained observation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016y53PdpyMAp9RSW65fatHe

* fix: resolve merge integration errors

---------

Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-10-24 22:35:20 +08:00

101 lines
3.5 KiB
TypeScript

import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "vitest";
import { toolContract } from "../../src/contracts/toolContracts.js";
import {
compressedKernelModuleContainer,
kernelModuleFixture,
} from "../../src/native/elf/KernelModule.fixture.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { cliTest } from "../support/cli/cliFixture.js";
cliTest(
"reports license, duplicate aliases, and version CRCs through the public CLI",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-kernel-module-cli-");
const path = join(root, "demo.ko");
await writeFile(path, kernelModuleFixture());
const result = await cli.run({
arguments: ["inspect-kernel-module", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = toolContract("inspect_kernel_module").outputSchema.parse(
result.json,
);
expect(
evidence.normalized_result.modinfo?.entries.map((entry) => [
entry.key,
entry.value,
]),
).toEqual([
["license", "GPL"],
["vermagic", "5.15.0 SMP mod_unload"],
["alias", "pci:v1"],
["alias", "pci:v2"],
["name", "demo"],
["depends", ""],
]);
expect(evidence.normalized_result.versions?.entries[0]?.crc_hex).toBe(
"0000000012345678",
);
expect(evidence.normalized_result.signature?.status).toBe("present");
},
);
cliTest(
"resolves a relative kernel module path against the working directory",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-kernel-module-cli-rel-");
await writeFile(join(root, "demo.ko"), kernelModuleFixture({ type: 3 }));
const result = await cli.run({
arguments: ["inspect-kernel-module", "demo.ko", "--json"],
cwd: root,
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = toolContract("inspect_kernel_module").outputSchema.parse(
result.json,
);
expect(evidence.normalized_result.applicability).toBe("not_applicable");
expect(evidence.normalized_result.type_name).toBe("shared");
expect(evidence.subject?.local_path).toBe(join(root, "demo.ko"));
},
);
cliTest("reports a non-ELF file as invalid input", async ({ cli }) => {
const root = await createTestTempDirectory("rea-kernel-module-cli-bad-");
const path = join(root, "broken.ko");
await writeFile(path, Buffer.from("not an elf"));
const result = await cli.run({
arguments: ["inspect-kernel-module", path, "--json"],
});
expect(result.exitCode).toBe(1);
expect(result.json).toMatchObject({
code: "invalid_request",
details: { operation: "inspect_kernel_module" },
});
});
cliTest(
"reports a compressed container without claiming the payload is a module",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-kernel-module-cli-xz-");
const path = join(root, "demo.ko.xz");
await writeFile(path, compressedKernelModuleContainer("xz"));
const result = await cli.run({
arguments: ["inspect-kernel-module", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = toolContract("inspect_kernel_module").outputSchema.parse(
result.json,
);
expect(evidence.normalized_result).toMatchObject({
applicability: "unsupported",
format: null,
compression: { codec: "xz", status: "unsupported" },
next_probe: { operation: "external_decompression" },
modinfo: null,
});
expect(evidence.subject?.format).toBe("file");
},
);