mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 13:45:30 +00:00
* fix(elf): decode a 32-bit prpsinfo whose ABI gives pr_uid 32 bits
`decodeCorePrpsinfo` assumed every ELFCLASS32 core carries the i386/ARM `struct elf_prpsinfo`:
16-bit `pr_uid`/`pr_gid` and a 124-byte descriptor.
`pr_uid` is `__kernel_uid_t`, and its width is an ABI property, not an ELF-class one. Only
`arch/x86/include/uapi/asm/posix_types_32.h` and its ARM/m68k/sparc counterparts narrow it to
`unsigned short`; everything else takes `asm-generic/posix_types.h`'s `unsigned int`, so MIPS o32,
PowerPC-32, s390 and `compat_elf_prpsinfo` (`include/asm-generic/compat.h`:
`typedef u32 __compat_uid_t`) write a 128-byte struct with every member after `pr_flag` four bytes
later.
Reading a 128-byte descriptor with the 124-byte offsets published wrong values rather than null.
Measured through `parseElfNotes`, with `uid 70000, gid 80000, pid 4242, fname mips-proc`:
before: {"pid":80000,"ppid":4242,"pgrp":1,"sid":4242,"uid":4464,"gid":1,
"filename":"\u{10}","psargs":""}
after: {"pid":4242,"ppid":1,"pgrp":4242,"sid":4242,"uid":70000,"gid":80000,
"filename":"mips-proc","psargs":"./mips-proc --serve"}
`pid` came out of the middle of `gid` and the two text fields came from the wrong offsets
entirely.
The kernel writes `sizeof(*psinfo)` as the descriptor — `fs/binfmt_elf.c`:
`fill_note(&info->psinfo, PRPSINFO, sizeof(*psinfo), psinfo)` — so the declared length already
carries the distinction and no machine table is needed. The 32-bit id width is now
`descriptor.length >= 128 ? 4 : 2`, and `pr_pid`/`pr_fname`/`pr_psargs` are derived from it. The
124-byte and 136-byte decodes are unchanged; the second new case pins that.
`src/native/elf` and `src/domain/native` — 535 passing. Forcing the 16-bit layout fails the new case.
* test(elf): produce the 32-bit prpsinfo layout from the shared note builder
Review finding: the regression hand-encoded a second NT_PRPSINFO layout while `corePrpsinfoNote` is
the repository's producer for this format, so nothing else could represent the MIPS/PowerPC/s390
ABI and the two encodings could drift.
`corePrpsinfoNote` takes `idWidth: 2 | 4` now. It allocates 124 or 128 bytes from it, writes
`pr_uid`/`pr_gid` at that width, and derives `pr_pid` through `pr_psargs` from `8 + idWidth * 2`
instead of the fixed 12/28/44 offsets. The option is ignored for a 64-bit image, whose layout is
fixed at 136.
Both new cases build their descriptor through the producer, so the test no longer carries offsets
the fixture does not know about.
* test(elf): drop the duplicate 124-byte prpsinfo case
The previous commit's rewrite left the original hand-built 124-byte block in place, so the same
regression ran twice — once through `corePrpsinfoNote`, once with the offsets written out again.
Keeping both is exactly the drift the shared producer was supposed to end.
* fix(elf): preserve SuperH PRPSINFO decoding
---------
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
Co-authored-by: wunianze666-netizen <wunianze666@gmail.com>
334 lines
9.3 KiB
TypeScript
334 lines
9.3 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { expect } from "vitest";
|
|
|
|
import { toolContract } from "../../src/contracts/toolContracts.js";
|
|
import {
|
|
coreAuxvNote,
|
|
coreFileNote,
|
|
corePrpsinfoDescriptor,
|
|
corePrpsinfoNote,
|
|
coreSiginfoNote,
|
|
elfNote,
|
|
elfNoteFixture,
|
|
} from "../../src/native/elf/ElfNotes.fixture.js";
|
|
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
|
import { cliTest } from "../support/cli/cliFixture.js";
|
|
|
|
const descriptorHash = (bytes: Buffer): string =>
|
|
createHash("sha256").update(bytes).digest("hex");
|
|
|
|
const buildCoreNotes = (): Buffer =>
|
|
Buffer.concat([
|
|
corePrpsinfoNote({
|
|
bits: 64,
|
|
little: true,
|
|
state: 0,
|
|
sname: "R",
|
|
pid: 31337,
|
|
ppid: 1,
|
|
pgrp: 31337,
|
|
sid: 31337,
|
|
uid: 1000,
|
|
gid: 1000,
|
|
fname: "target-service",
|
|
psargs: "/opt/target-service --worker 1",
|
|
}),
|
|
coreFileNote(
|
|
[
|
|
{
|
|
start: 0x555555554000n,
|
|
end: 0x555555558000n,
|
|
file_ofs: 0n,
|
|
path: "/opt/target-service",
|
|
},
|
|
{
|
|
start: 0x7ffff7dc0000n,
|
|
end: 0x7ffff7de0000n,
|
|
file_ofs: 0n,
|
|
path: "/lib/x86_64-linux-gnu/libc.so.6",
|
|
},
|
|
],
|
|
4096,
|
|
64,
|
|
true,
|
|
),
|
|
coreSiginfoNote(11, 1, 0, true),
|
|
coreAuxvNote(
|
|
[
|
|
[6, 4096n],
|
|
[3, 0x555555554040n],
|
|
],
|
|
64,
|
|
true,
|
|
),
|
|
]);
|
|
|
|
const assertCoreEntries = (entries: unknown[]): void => {
|
|
expect(entries).toHaveLength(4);
|
|
expect(entries[0]).toMatchObject({
|
|
owner: "CORE",
|
|
type: 3,
|
|
type_name: "core_prpsinfo",
|
|
gnu: null,
|
|
core: {
|
|
prpsinfo: {
|
|
state: 0,
|
|
pid: 31337,
|
|
ppid: 1,
|
|
pgrp: 31337,
|
|
sid: 31337,
|
|
uid: 1000,
|
|
gid: 1000,
|
|
filename: "target-service",
|
|
psargs: "/opt/target-service --worker 1",
|
|
},
|
|
},
|
|
});
|
|
expect(entries[1]).toMatchObject({
|
|
owner: "CORE",
|
|
type: 0x46494c45,
|
|
type_name: "core_file",
|
|
gnu: null,
|
|
core: {
|
|
file: {
|
|
count: 2,
|
|
page_size: 4096,
|
|
mappings: [
|
|
{
|
|
start_address: "0x555555554000",
|
|
end_address: "0x555555558000",
|
|
file_offset_pages: "0x0",
|
|
path: "/opt/target-service",
|
|
path_hex: Buffer.from("/opt/target-service").toString("hex"),
|
|
},
|
|
{
|
|
start_address: "0x7ffff7dc0000",
|
|
end_address: "0x7ffff7de0000",
|
|
file_offset_pages: "0x0",
|
|
path: "/lib/x86_64-linux-gnu/libc.so.6",
|
|
path_hex: Buffer.from("/lib/x86_64-linux-gnu/libc.so.6").toString(
|
|
"hex",
|
|
),
|
|
},
|
|
],
|
|
},
|
|
},
|
|
});
|
|
expect(entries[2]).toMatchObject({
|
|
owner: "CORE",
|
|
type: 0x53494749,
|
|
type_name: "core_siginfo",
|
|
gnu: null,
|
|
core: {
|
|
siginfo: { signo: 11, errno: 0, code: 1 },
|
|
},
|
|
});
|
|
expect(entries[3]).toMatchObject({
|
|
owner: "CORE",
|
|
type: 6,
|
|
type_name: "core_auxv",
|
|
gnu: null,
|
|
core: {
|
|
auxv: [
|
|
{ type: 6, type_name: "AT_PAGESZ", value: "0x1000" },
|
|
{ type: 3, type_name: "AT_PHDR", value: "0x555555554040" },
|
|
],
|
|
},
|
|
});
|
|
};
|
|
|
|
cliTest(
|
|
"decodes Linux core dump notes through CLI and stdio MCP",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-elf-core-notes-");
|
|
const path = join(root, "core.elf");
|
|
const notes = buildCoreNotes();
|
|
const bytes = elfNoteFixture({
|
|
bits: 64,
|
|
little: true,
|
|
type: 4,
|
|
ownership: "program",
|
|
notes,
|
|
});
|
|
await writeFile(path, bytes);
|
|
|
|
const output = await cli.run({
|
|
arguments: ["inspect-elf-notes", path, "--json"],
|
|
});
|
|
expect(output.exitCode, output.stderr).toBe(0);
|
|
|
|
const call = await cli.sharedMcpCall!();
|
|
const response = await call("inspect_elf_notes", { path });
|
|
expect(response.isError, JSON.stringify(response.content)).not.toBe(true);
|
|
|
|
const schema = toolContract("inspect_elf_notes").outputSchema;
|
|
for (const raw of [output.json, response.structuredContent]) {
|
|
const evidence = schema.parse(raw);
|
|
expect(evidence.subject).toMatchObject({
|
|
local_path: path,
|
|
identity: { sha256: descriptorHash(bytes) },
|
|
});
|
|
const report = evidence.normalized_result;
|
|
expect(report).toMatchObject({
|
|
format: "elf64",
|
|
byte_order: "little",
|
|
machine: 62,
|
|
coverage: {
|
|
status: "complete",
|
|
examined_tables: 1,
|
|
examined_entries: 4,
|
|
},
|
|
});
|
|
assertCoreEntries(report.entries);
|
|
}
|
|
},
|
|
);
|
|
|
|
const uid32CoreLayouts = [
|
|
{ name: "MIPS little", machine: 8, byteOrder: "little", coreFlags: 0 },
|
|
{ name: "MIPS big", machine: 8, byteOrder: "big", coreFlags: 0 },
|
|
{ name: "PowerPC big", machine: 20, byteOrder: "big", coreFlags: 0 },
|
|
{ name: "RISC-V little", machine: 243, byteOrder: "little", coreFlags: 0 },
|
|
{
|
|
name: "Hexagon V4 little",
|
|
machine: 164,
|
|
byteOrder: "little",
|
|
coreFlags: 3,
|
|
},
|
|
{ name: "PA-RISC big", machine: 15, byteOrder: "big", coreFlags: 0 },
|
|
{ name: "OpenRISC big", machine: 92, byteOrder: "big", coreFlags: 0 },
|
|
{
|
|
name: "OpenRISC Linux big",
|
|
machine: 0x8472,
|
|
byteOrder: "big",
|
|
coreFlags: 0,
|
|
},
|
|
{
|
|
name: "ARC compact little",
|
|
machine: 93,
|
|
byteOrder: "little",
|
|
coreFlags: 0,
|
|
},
|
|
{ name: "ARCv2 big", machine: 195, byteOrder: "big", coreFlags: 0 },
|
|
{ name: "Xtensa little", machine: 94, byteOrder: "little", coreFlags: 0 },
|
|
{ name: "MicroBlaze big", machine: 189, byteOrder: "big", coreFlags: 0 },
|
|
{ name: "C-SKY little", machine: 252, byteOrder: "little", coreFlags: 0 },
|
|
{ name: "C-SKY big", machine: 252, byteOrder: "big", coreFlags: 0 },
|
|
{ name: "Blackfin little", machine: 106, byteOrder: "little", coreFlags: 0 },
|
|
{ name: "UniCore little", machine: 110, byteOrder: "little", coreFlags: 0 },
|
|
{ name: "Nios II little", machine: 113, byteOrder: "little", coreFlags: 0 },
|
|
{ name: "C6x little", machine: 140, byteOrder: "little", coreFlags: 0 },
|
|
{ name: "C6x big", machine: 140, byteOrder: "big", coreFlags: 0 },
|
|
{ name: "NDS32 little", machine: 167, byteOrder: "little", coreFlags: 0 },
|
|
{ name: "NDS32 big", machine: 167, byteOrder: "big", coreFlags: 0 },
|
|
{ name: "Meta little", machine: 174, byteOrder: "little", coreFlags: 0 },
|
|
{ name: "TILEPro little", machine: 188, byteOrder: "little", coreFlags: 0 },
|
|
{
|
|
name: "TILE-Gx m32 little",
|
|
machine: 191,
|
|
byteOrder: "little",
|
|
coreFlags: 0,
|
|
},
|
|
] as const;
|
|
|
|
cliTest.for([
|
|
...uid32CoreLayouts.map((layout) => ({ ...layout, idWidth: 4 as const })),
|
|
{
|
|
name: "SuperH little",
|
|
machine: 42,
|
|
byteOrder: "little",
|
|
coreFlags: 0,
|
|
idWidth: 2,
|
|
},
|
|
{
|
|
name: "SuperH big",
|
|
machine: 42,
|
|
byteOrder: "big",
|
|
coreFlags: 0,
|
|
idWidth: 2,
|
|
},
|
|
] as const)(
|
|
"decodes Linux ELF32 process identity through CLI and MCP ($name)",
|
|
async ({ machine, byteOrder, coreFlags, idWidth }, { cli }) => {
|
|
const root = await createTestTempDirectory("rea-elf-prpsinfo32-core-");
|
|
const path = join(root, "core.elf");
|
|
const little = byteOrder === "little";
|
|
const uid = idWidth === 4 ? 70001 : 500;
|
|
const gid = idWidth === 4 ? 90002 : 600;
|
|
const descriptor = corePrpsinfoDescriptor({
|
|
bits: 32,
|
|
idWidth,
|
|
little,
|
|
state: 1,
|
|
uid,
|
|
gid,
|
|
pid: 1234,
|
|
ppid: 12,
|
|
pgrp: 4321,
|
|
sid: 5678,
|
|
fname: "core-probe",
|
|
psargs: "core-probe --flag",
|
|
});
|
|
const shortened = descriptor.subarray(0, descriptor.length - 4);
|
|
const bytes = elfNoteFixture({
|
|
bits: 32,
|
|
machine,
|
|
little,
|
|
type: 4,
|
|
ownership: "program",
|
|
notes: Buffer.concat([
|
|
elfNote("CORE", 3, descriptor, little),
|
|
elfNote("CORE", 3, shortened, little),
|
|
]),
|
|
});
|
|
if (little) bytes.writeUInt32LE(coreFlags, 0x24);
|
|
else bytes.writeUInt32BE(coreFlags, 0x24);
|
|
await writeFile(path, bytes);
|
|
|
|
const output = await cli.run({
|
|
arguments: ["inspect-elf-notes", path, "--json"],
|
|
});
|
|
expect(output.exitCode, output.stderr).toBe(0);
|
|
const call = await cli.sharedMcpCall!();
|
|
const response = await call("inspect_elf_notes", { path });
|
|
expect(response.isError, JSON.stringify(response.content)).not.toBe(true);
|
|
|
|
const schema = toolContract("inspect_elf_notes").outputSchema;
|
|
for (const raw of [output.json, response.structuredContent]) {
|
|
const evidence = schema.parse(raw);
|
|
expect(evidence.subject).toMatchObject({
|
|
local_path: path,
|
|
identity: { sha256: descriptorHash(bytes) },
|
|
});
|
|
const report = evidence.normalized_result;
|
|
expect(report).toMatchObject({
|
|
format: "elf32",
|
|
byte_order: byteOrder,
|
|
machine,
|
|
coverage: { status: "complete", examined_entries: 2 },
|
|
});
|
|
expect(report.entries[0]?.core?.prpsinfo).toEqual({
|
|
state: 1,
|
|
uid,
|
|
gid,
|
|
pid: 1234,
|
|
ppid: 12,
|
|
pgrp: 4321,
|
|
sid: 5678,
|
|
filename: "core-probe",
|
|
psargs: "core-probe --flag",
|
|
});
|
|
expect(report.entries[1]).toMatchObject({
|
|
type_name: "core_prpsinfo",
|
|
descriptor: {
|
|
bytes: shortened.length,
|
|
sha256: descriptorHash(shortened),
|
|
},
|
|
core: null,
|
|
});
|
|
}
|
|
},
|
|
);
|