fix(elf): decode a 32-bit prpsinfo whose ABI gives pr_uid 32 bits (#10189)

* 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>
This commit is contained in:
authored and GitHub committed 2026-11-20 01:23:27 +00:00
1 parent 78fdc7ecf7
commit 3cfe8480c5
6 files changed
+325 -37

No files matched your search

+5
View File
@@ -45,6 +45,11 @@ auxiliary vectors (`NT_AUXV`) across 32-bit and 64-bit images. Other owners or u
notes keep `core` null. `NT_FILE` and `NT_AUXV` words follow the ELF class and
byte order. Addresses and word values retain their unsigned hexadecimal form.
For ELF32 `NT_PRPSINFO`, REA uses the declared Linux machine ABI to select the
UID/GID width and requires the corresponding descriptor length. If either fact
is insufficient to establish a layout, the note keeps its raw range and hash
and leaves `core` null.
An `NT_FILE` descriptor with nonzero bytes after its declared filenames
retains its raw note evidence and leaves `core` null.
@@ -10,7 +10,7 @@
"src/native/AppleDispatchMetadata.ts": 4,
"src/native/AppleObjcDispatchFacets.ts": 1,
"src/native/AppleSwiftDispatchFacets.ts": 1,
"src/native/elf/ElfNoteParser.ts": 13,
"src/native/elf/ElfNoteParser.ts": 6,
"src/native/pe/PeCertificateAsn1.ts": 2,
"src/v8/V8CodeCacheParser.ts": 1
}
+82
View File
@@ -15,6 +15,7 @@ import {
compressedElfNoteSectionFixture,
coreAuxvNote,
coreFileNote,
corePrpsinfoDescriptor,
corePrpsinfoNote,
coreSiginfoNote,
elfNote,
@@ -1134,6 +1135,7 @@ it("decodes Linux core dump notes for 32-bit big-endian", () => {
const bytes = elfNoteFixture({
bits: 32,
machine: 40,
little: false,
type: 4,
ownership: "program",
@@ -1169,6 +1171,86 @@ it("decodes Linux core dump notes for 32-bit big-endian", () => {
]);
});
it.each([
{ name: "i386", machine: 3, little: true },
{ name: "s390 compat", machine: 22, little: false },
{ name: "x86 x32 compat", machine: 62, little: true },
])(
"decodes the 124-byte prpsinfo with 16-bit IDs for $name",
({ machine, little }) => {
const report = parse(
elfNoteFixture({
bits: 32,
machine,
little,
type: 4,
ownership: "program",
notes: corePrpsinfoNote({
bits: 32,
little,
state: 1,
uid: 500,
gid: 600,
pid: 4242,
ppid: 1,
pgrp: 4242,
sid: 4242,
fname: "legacy-proc",
psargs: "./legacy-proc --serve",
}),
}),
);
expect(report.entries[0]?.core?.prpsinfo).toEqual({
state: 1,
pid: 4242,
ppid: 1,
pgrp: 4242,
sid: 4242,
uid: 500,
gid: 600,
filename: "legacy-proc",
psargs: "./legacy-proc --serve",
});
},
);
it.each([
{ name: "an oversized legacy layout", machine: 3, idWidth: 4 as const },
{
name: "an unknown machine with 16-bit fields",
machine: 0xffff,
idWidth: 2 as const,
},
{
name: "an unknown machine with 32-bit fields",
machine: 0xffff,
idWidth: 4 as const,
},
])("keeps raw prpsinfo evidence for $name", ({ machine, idWidth }) => {
const descriptor = corePrpsinfoDescriptor({ bits: 32, idWidth });
const report = parse(
elfNoteFixture({
bits: 32,
machine,
type: 4,
ownership: "program",
notes: elfNote("CORE", 3, descriptor),
}),
);
expect(report.coverage).toMatchObject({
status: "complete",
examined_entries: 1,
});
expect(report.entries[0]).toMatchObject({
type_name: "core_prpsinfo",
descriptor: {
bytes: descriptor.length,
sha256: createHash("sha256").update(descriptor).digest("hex"),
},
core: null,
});
});
it.each([
{ bits: 32 as const, little: true, address: 0xf1234567n },
{ bits: 64 as const, little: false, address: 0xf123456789abcdefn },
+57 -19
View File
@@ -333,31 +333,70 @@ const AUXV_TYPE_NAMES: Readonly<Record<number, string>> = {
const hexWord = (val: bigint | number): string => `0x${val.toString(16)}`;
// Linux ELF32 core-writer ABIs with established NT_PRPSINFO UID/GID widths.
// The native and compatibility writers store sizeof(elf_prpsinfo).
const LINUX_ELF32_PRPSINFO_ID_WIDTH: Readonly<Partial<Record<number, 2 | 4>>> =
{
2: 2, // EM_SPARC
3: 2, // EM_386
4: 2, // EM_68K
8: 4, // EM_MIPS
15: 4, // EM_PARISC
20: 4, // EM_PPC
22: 2, // EM_S390 (31-bit compatibility)
40: 2, // EM_ARM
42: 2, // EM_SH
62: 2, // EM_X86_64 (x32 compatibility)
92: 4, // EM_OPENRISC
93: 4, // EM_ARCOMPACT
94: 4, // EM_XTENSA
106: 4, // EM_BLACKFIN
110: 4, // EM_UNICORE
113: 4, // EM_ALTERA_NIOS2
140: 4, // EM_TI_C6000
164: 4, // EM_HEXAGON
167: 4, // EM_NDS32
174: 4, // EM_METAG
188: 4, // EM_TILEPRO
189: 4, // EM_MICROBLAZE
191: 4, // EM_TILEGX (ELF32 compatibility)
195: 4, // EM_ARCV2
243: 4, // EM_RISCV
252: 4, // EM_CSKY
0x8472: 4, // EM_OR32 (older Linux OpenRISC cores)
};
const decodeCorePrpsinfo = (
descriptor: Buffer,
little: boolean,
wide: boolean,
machine: number,
text: TextBudget,
): ElfNotes["entries"][number]["core"] => {
const minLen = wide ? 136 : 124;
if (descriptor.length < minLen) return null;
const u8 = (off: number) => descriptor.readUInt8(off);
const u16 = (off: number) =>
little ? descriptor.readUInt16LE(off) : descriptor.readUInt16BE(off);
const u32 = (off: number) =>
little ? descriptor.readUInt32LE(off) : descriptor.readUInt32BE(off);
const i32 = (off: number) =>
little ? descriptor.readInt32LE(off) : descriptor.readInt32BE(off);
const idWidth = wide ? 4 : LINUX_ELF32_PRPSINFO_ID_WIDTH[machine];
if (idWidth === undefined) return null;
const uidOff = wide ? 16 : 8;
const gidOff = uidOff + idWidth;
const pidOff = gidOff + idWidth;
const fnameOff = pidOff + 16;
const psargsOff = fnameOff + 16;
const expectedBytes = psargsOff + 80;
if (
wide
? descriptor.length < expectedBytes
: descriptor.length !== expectedBytes
)
return null;
const state = u8(0);
const uid = wide ? u32(16) : u16(8);
const gid = wide ? u32(20) : u16(10);
const pid = wide ? i32(24) : i32(12);
const ppid = wide ? i32(28) : i32(16);
const pgrp = wide ? i32(32) : i32(20);
const sid = wide ? i32(36) : i32(24);
const reader = new BinaryReader(descriptor, little, "elf-notes");
const state = reader.u8(0);
const uid = idWidth === 4 ? reader.u32(uidOff) : reader.u16(uidOff);
const gid = idWidth === 4 ? reader.u32(gidOff) : reader.u16(gidOff);
const pid = reader.i32(pidOff);
const ppid = reader.i32(pidOff + 4);
const pgrp = reader.i32(pidOff + 8);
const sid = reader.i32(pidOff + 12);
const fnameOff = wide ? 40 : 28;
const fnameBytes = descriptor.subarray(fnameOff, fnameOff + 16);
const fnameEnd = fnameBytes.indexOf(0);
const fname = (
@@ -365,7 +404,6 @@ const decodeCorePrpsinfo = (
).toString("latin1");
chargeText(text, fname.length);
const psargsOff = wide ? 56 : 44;
const psargsBytes = descriptor.subarray(psargsOff, psargsOff + 80);
const psargsEnd = psargsBytes.indexOf(0);
const psargs = (
@@ -597,7 +635,7 @@ const decodeCore = (
): ElfNotes["entries"][number]["core"] => {
if (owner !== "CORE") return null;
if (type === 3)
return decodeCorePrpsinfo(descriptor, little, bits === 64, text);
return decodeCorePrpsinfo(descriptor, little, bits === 64, machine, text);
if (type === 0x46494c45)
return decodeCoreFile(descriptor, little, bits, text);
if (type === 0x53494749)
+32 -17
View File
@@ -443,13 +443,18 @@ export interface CorePrpsinfoOptions {
readonly sid?: number;
readonly fname?: string;
readonly psargs?: string;
/** UID/GID byte width in the ELF32 descriptor. ELF64 always uses four bytes. */
readonly idWidth?: 2 | 4;
}
/** Linux CORE NT_PRPSINFO note (type 3). */
export const corePrpsinfoNote = (options: CorePrpsinfoOptions = {}): Buffer => {
/** Descriptor bytes for a Linux NT_PRPSINFO note. */
export const corePrpsinfoDescriptor = (
options: CorePrpsinfoOptions = {},
): Buffer => {
const bits = options.bits ?? 64;
const little = options.little ?? true;
const desc = Buffer.alloc(bits === 64 ? 136 : 124);
const idWidth = bits === 64 ? 4 : (options.idWidth ?? 2);
const desc = Buffer.alloc(bits === 64 ? 136 : idWidth === 4 ? 128 : 124);
desc[0] = options.state ?? 0;
desc[1] = (options.sname ?? "R").charCodeAt(0);
desc[2] = options.zomb ?? 0;
@@ -479,30 +484,40 @@ export const corePrpsinfoNote = (options: CorePrpsinfoOptions = {}): Buffer => {
psargs.copy(desc, 56, 0, Math.min(psargs.length, 80));
} else {
write32(desc, 4, options.flag ?? 0, little);
write16(desc, 8, options.uid ?? 1000, little);
write16(desc, 10, options.gid ?? 1000, little);
if (little) {
desc.writeInt32LE(options.pid ?? 1234, 12);
desc.writeInt32LE(options.ppid ?? 1, 16);
desc.writeInt32LE(options.pgrp ?? 1234, 20);
desc.writeInt32LE(options.sid ?? 1234, 24);
if (idWidth === 4) {
write32(desc, 8, options.uid ?? 1000, little);
write32(desc, 12, options.gid ?? 1000, little);
} else {
desc.writeInt32BE(options.pid ?? 1234, 12);
desc.writeInt32BE(options.ppid ?? 1, 16);
desc.writeInt32BE(options.pgrp ?? 1234, 20);
desc.writeInt32BE(options.sid ?? 1234, 24);
write16(desc, 8, options.uid ?? 1000, little);
write16(desc, 10, options.gid ?? 1000, little);
}
const pidAt = 8 + idWidth * 2;
if (little) {
desc.writeInt32LE(options.pid ?? 1234, pidAt);
desc.writeInt32LE(options.ppid ?? 1, pidAt + 4);
desc.writeInt32LE(options.pgrp ?? 1234, pidAt + 8);
desc.writeInt32LE(options.sid ?? 1234, pidAt + 12);
} else {
desc.writeInt32BE(options.pid ?? 1234, pidAt);
desc.writeInt32BE(options.ppid ?? 1, pidAt + 4);
desc.writeInt32BE(options.pgrp ?? 1234, pidAt + 8);
desc.writeInt32BE(options.sid ?? 1234, pidAt + 12);
}
const fname = Buffer.from(`${options.fname ?? "app"}\0`, "latin1");
fname.copy(desc, 28, 0, Math.min(fname.length, 16));
fname.copy(desc, pidAt + 16, 0, Math.min(fname.length, 16));
const psargs = Buffer.from(
`${options.psargs ?? "./app --flag"}\0`,
"latin1",
);
psargs.copy(desc, 44, 0, Math.min(psargs.length, 80));
psargs.copy(desc, pidAt + 32, 0, Math.min(psargs.length, 80));
}
return elfNote("CORE", 3, desc, little);
return desc;
};
/** Linux CORE NT_PRPSINFO note (type 3). */
export const corePrpsinfoNote = (options: CorePrpsinfoOptions = {}): Buffer =>
elfNote("CORE", 3, corePrpsinfoDescriptor(options), options.little ?? true);
export interface CoreFileMapping {
readonly start: bigint | number;
readonly end: bigint | number;
+148
View File
@@ -7,8 +7,10 @@ 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";
@@ -183,3 +185,149 @@ cliTest(
}
},
);
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,
});
}
},
);