feat(elf): decode Linux core dump process and memory mapping notes (#8830)

* feat(elf): decode Linux core dump process and memory mapping notes

Inspect Linux core dump note tables and decode structured metadata without
requiring external tools or debuggers:
- NT_PRPSINFO (type 3): decode process ID, parent ID, process group,
  session, UID, GID, execution state, filename, and command line arguments
  bounded by the text budget.
- NT_FILE (type 0x46494c45): decode mapped memory ranges (start_address,
  end_address, file_offset_pages) and mapped file paths with page size.
- NT_SIGINFO (type 0x53494749): decode faulting signal number, errno, and code.
- NT_AUXV (type 6): decode auxiliary vector pairs with known AT_* tag names.
- Support both 32-bit and 64-bit, little-endian and big-endian targets.
- Retain unknown core note types gracefully without partial success errors.
- Add comprehensive unit tests, fixtures, acceptance tests, and documentation.

* fix(elf): preserve raw path bytes and enforce auxv termination

* fix(elf): account for path hex text budget and enforce auxv trailing padding

* fix(elf): account for row and scan limits, ABI signal order, and lossless source IDs

* fix(elf): cache auxv padding scans, pre-charge entries, and isolate page-size test
This commit is contained in:
Rishikesh Gowda K K authored and GitHub committed 2026-11-04 11:46:11 +08:00
1 parent c3647ade1e
commit e75d1c741c
7 files changed
+1149 -10

No files matched your search

+356 -3
View File
@@ -19,6 +19,7 @@ import type {
InspectElfNotesInput,
} from "../../domain/native/elfNotes.js";
import {
coreNoteTypeName,
elfObjectTypeName,
gnuNoteTypeName,
} from "../../domain/native/elfNotes.js";
@@ -289,6 +290,322 @@ const decodeGnu = (
};
};
const AUXV_TYPE_NAMES: Readonly<Record<number, string>> = {
0: "AT_NULL",
1: "AT_IGNORE",
2: "AT_EXECFD",
3: "AT_PHDR",
4: "AT_PHENT",
5: "AT_PHNUM",
6: "AT_PAGESZ",
7: "AT_BASE",
8: "AT_FLAGS",
9: "AT_ENTRY",
11: "AT_UID",
12: "AT_EUID",
13: "AT_GID",
14: "AT_EGID",
15: "AT_PLATFORM",
16: "AT_HWCAP",
17: "AT_CLKTCK",
23: "AT_SECURE",
25: "AT_RANDOM",
26: "AT_HWCAP2",
31: "AT_EXECFN",
33: "AT_SYSINFO_EHDR",
};
const hexWord = (val: bigint | number): string => `0x${val.toString(16)}`;
const decodeCorePrpsinfo = (
descriptor: Buffer,
little: boolean,
wide: boolean,
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 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 fnameOff = wide ? 40 : 28;
const fnameBytes = descriptor.subarray(fnameOff, fnameOff + 16);
const fnameEnd = fnameBytes.indexOf(0);
const fname = (
fnameEnd < 0 ? fnameBytes : fnameBytes.subarray(0, fnameEnd)
).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 = (
psargsEnd < 0 ? psargsBytes : psargsBytes.subarray(0, psargsEnd)
).toString("latin1");
chargeText(text, psargs.length);
return {
prpsinfo: {
state,
pid,
ppid,
pgrp,
sid,
uid,
gid,
filename: fname,
psargs,
},
file: null,
siginfo: null,
auxv: null,
};
};
const decodeCoreFile = (
descriptor: Buffer,
little: boolean,
wide: boolean,
text: TextBudget,
): ElfNotes["entries"][number]["core"] => {
const wordSize = wide ? 8 : 4;
const headerSize = wordSize * 2;
if (descriptor.length < headerSize) return null;
const readWord = (off: number): bigint => {
if (wide) {
return little
? descriptor.readBigUInt64LE(off)
: descriptor.readBigUInt64BE(off);
}
const val = little
? descriptor.readUInt32LE(off)
: descriptor.readUInt32BE(off);
return BigInt(val);
};
const countBig = readWord(0);
const pageSizeBig = readWord(wordSize);
if (countBig > 0xffffffffn || pageSizeBig > 0xffffffffn || pageSizeBig === 0n)
return null;
const count = Number(countBig);
const pageSize = Number(pageSizeBig);
const entrySize = wordSize * 3;
const neededBytes = BigInt(headerSize) + countBig * BigInt(entrySize);
if (BigInt(descriptor.length) < neededBytes) return null;
const tableBytes = Number(neededBytes);
const rowBytes = count * (wide ? 54 : 30);
if (rowBytes > text.limit - text.used) {
throw new ArtifactReaderFailure(
"limit",
`ELF note text exceeds the ${String(text.limit)}-byte budget.`,
{ boundary: "memory" },
);
}
const scanKey = `file:${String(descriptor.byteOffset)}:${String(descriptor.length)}`;
const cachedScan = text.scans.get(scanKey);
if (cachedScan === -1) return null;
if (cachedScan === undefined) {
chargeText(text, descriptor.length - tableBytes, "scanned");
}
let strPos = tableBytes;
let totalPathBytes = 0;
for (let i = 0; i < count; i++) {
if (strPos >= descriptor.length) {
text.scans.set(scanKey, -1);
return null;
}
const nullIdx = descriptor.indexOf(0, strPos);
if (nullIdx < 0) {
text.scans.set(scanKey, -1);
return null;
}
totalPathBytes += nullIdx - strPos;
strPos = nullIdx + 1;
}
text.scans.set(scanKey, count);
const pathBytes = totalPathBytes * 3;
chargeText(text, rowBytes + pathBytes);
const mappings: {
start_address: string;
end_address: string;
file_offset_pages: string;
path: string;
path_hex: string;
}[] = new Array(count);
strPos = tableBytes;
for (let i = 0; i < count; i++) {
const entryOff = headerSize + i * entrySize;
const start = readWord(entryOff);
const end = readWord(entryOff + wordSize);
const offsetPages = readWord(entryOff + wordSize * 2);
const nullIdx = descriptor.indexOf(0, strPos);
const pathBuf = descriptor.subarray(strPos, nullIdx);
strPos = nullIdx + 1;
mappings[i] = {
start_address: hexWord(start),
end_address: hexWord(end),
file_offset_pages: hexWord(offsetPages),
path: pathBuf.toString("latin1"),
path_hex: pathBuf.toString("hex"),
};
}
return {
prpsinfo: null,
file: {
count,
page_size: pageSize,
mappings,
},
siginfo: null,
auxv: null,
};
};
const decodeCoreSiginfo = (
descriptor: Buffer,
little: boolean,
machine: number,
): ElfNotes["entries"][number]["core"] => {
if (descriptor.length < 12) return null;
const i32 = (off: number) =>
little ? descriptor.readInt32LE(off) : descriptor.readInt32BE(off);
const isMips = machine === 8 || machine === 10;
const signo = i32(0);
const errno = isMips ? i32(8) : i32(4);
const code = isMips ? i32(4) : i32(8);
return {
prpsinfo: null,
file: null,
siginfo: {
signo,
errno,
code,
},
auxv: null,
};
};
const decodeCoreAuxv = (
descriptor: Buffer,
little: boolean,
wide: boolean,
text: TextBudget,
): ElfNotes["entries"][number]["core"] => {
const wordSize = wide ? 8 : 4;
const entrySize = wordSize * 2;
if (descriptor.length < entrySize || descriptor.length % entrySize !== 0)
return null;
const totalPairs = descriptor.length / entrySize;
const scanKey = `auxv:${String(descriptor.byteOffset)}:${String(descriptor.length)}`;
let nullIndex = text.scans.get(scanKey);
if (nullIndex === -1) return null;
const readWord = (off: number): bigint => {
if (wide) {
return little
? descriptor.readBigUInt64LE(off)
: descriptor.readBigUInt64BE(off);
}
return BigInt(
little ? descriptor.readUInt32LE(off) : descriptor.readUInt32BE(off),
);
};
if (nullIndex === undefined) {
chargeText(text, descriptor.length, "scanned");
let found = false;
for (let i = 0; i < totalPairs; i++) {
const off = i * entrySize;
const aTypeBig = readWord(off);
if (aTypeBig > 0xffffffffn) {
text.scans.set(scanKey, -1);
return null;
}
if (aTypeBig === 0n) {
found = true;
nullIndex = i;
for (let j = off + entrySize; j < descriptor.length; j++) {
if (descriptor[j] !== 0) {
text.scans.set(scanKey, -1);
return null;
}
}
break;
}
}
if (!found || nullIndex === undefined) {
text.scans.set(scanKey, -1);
return null;
}
text.scans.set(scanKey, nullIndex);
}
const rowBytes = nullIndex * (wide ? 38 : 30);
chargeText(text, rowBytes);
const auxv: { type: number; type_name: string | null; value: string }[] =
new Array(nullIndex);
for (let i = 0; i < nullIndex; i++) {
const off = i * entrySize;
const aType = Number(readWord(off));
const aVal = readWord(off + wordSize);
auxv[i] = {
type: aType,
type_name: AUXV_TYPE_NAMES[aType] ?? null,
value: hexWord(aVal),
};
}
return {
prpsinfo: null,
file: null,
siginfo: null,
auxv,
};
};
const decodeCore = (
owner: string,
type: number,
descriptor: Buffer,
little: boolean,
wide: boolean,
machine: number,
text: TextBudget,
): ElfNotes["entries"][number]["core"] => {
if (owner !== "CORE") return null;
if (type === 3) return decodeCorePrpsinfo(descriptor, little, wide, text);
if (type === 0x46494c45)
return decodeCoreFile(descriptor, little, wide, text);
if (type === 0x53494749)
return decodeCoreSiginfo(descriptor, little, machine);
if (type === 6) return decodeCoreAuxv(descriptor, little, wide, text);
return null;
};
/** Decode PT_NOTE and SHT_NOTE tables from one in-memory ELF image. */
export const parseElfNotes = (
bytes: Buffer,
@@ -439,7 +756,17 @@ export const parseElfNotes = (
examined,
text,
))
parsed.push(toEntry(note, "program", index, reader.little, text));
parsed.push(
toEntry(
note,
"program",
index,
reader.little,
wide,
header.machine,
text,
),
);
}
}
if (shnum !== 0) {
@@ -494,7 +821,17 @@ export const parseElfNotes = (
examined,
text,
))
parsed.push(toEntry(note, "section", index, reader.little, text));
parsed.push(
toEntry(
note,
"section",
index,
reader.little,
wide,
header.machine,
text,
),
);
}
}
const absent = tables.length === 0;
@@ -524,9 +861,12 @@ const toEntry = (
tableKind: "program" | "section",
tableIndex: number,
little: boolean,
wide: boolean,
machine: number,
text: TextBudget,
): Omit<ElfNotes["entries"][number], "index"> => {
const gnuOwner = note.owner === "GNU";
const coreOwner = note.owner === "CORE";
const range = `${String(note.descriptor_offset)}:${String(note.descsz)}`;
let digest = text.hashes.get(range);
if (digest === undefined) {
@@ -541,7 +881,11 @@ const toEntry = (
namesz: note.namesz,
descsz: note.descsz,
type: note.type,
type_name: gnuOwner ? gnuNoteTypeName(note.type) : "unknown",
type_name: gnuOwner
? gnuNoteTypeName(note.type)
: coreOwner
? coreNoteTypeName(note.type)
: "unknown",
owner: note.owner,
owner_bytes_hex: note.owner_bytes_hex,
descriptor: {
@@ -554,5 +898,14 @@ const toEntry = (
: null,
},
gnu: decodeGnu(note.owner, note.type, note.descriptor, little, text),
core: decodeCore(
note.owner,
note.type,
note.descriptor,
little,
wide,
machine,
text,
),
};
};