fix(elf): preserve compressed note storage through program aliases (#12390)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 15:20:25 +00:00
1 parent 88983ebac7
commit cb9fce082d
4 files changed
+151 -74

No files matched your search

+45 -6
View File
@@ -116,15 +116,54 @@ it.each([
] as const)(
"refuses compressed ELF%i %s-endian note storage without parsing its compression header",
(bits, little) => {
expect(() => parse(compressedElfNoteSectionFixture(bits, little))).toThrow(
expect.objectContaining({
kind: "unsupported",
message: expect.stringMatching(/compressed.*note/iu),
}),
);
for (const ownership of ["section", "both"] as const) {
expect(() =>
parse(compressedElfNoteSectionFixture(bits, little, ownership)),
).toThrow(
expect.objectContaining({
kind: "unsupported",
message: expect.stringMatching(/compressed.*note/iu),
}),
);
}
},
);
it("still checks an independent PT_NOTE beside an exact compressed-note alias", () => {
const original = compressedElfNoteSectionFixture(64, true, "both");
const alias = original.subarray(64, 120);
const withDisjointNotes = (notes: Buffer): Buffer => {
const disjoint = Buffer.from(alias);
disjoint.writeBigUInt64LE(BigInt(original.length), 8);
disjoint.writeBigUInt64LE(BigInt(notes.length), 32);
disjoint.writeBigUInt64LE(BigInt(notes.length), 40);
disjoint.writeBigUInt64LE(1n, 48);
const image = Buffer.concat([original, notes, alias, disjoint]);
image.writeBigUInt64LE(BigInt(original.length + notes.length), 32);
image.writeUInt16LE(2, 56);
return image;
};
expect(() => parse(withDisjointNotes(Buffer.from([1])))).toThrow(
expect.objectContaining({
reason: "format",
message: "ELF note table has trailing non-zero bytes.",
}),
);
const framed = withDisjointNotes(
Buffer.concat([gnuBuildIdNote(), gnuAbiTagNote()]),
);
expect(() => parse(framed, { ...defaults, max_entries: 1 })).toThrow(
expect.objectContaining({
failure: expect.objectContaining({
limit: { name: "max_entries", maximum: 1, required: 2 },
}),
}),
);
expect(() => parse(framed)).toThrow(
expect.objectContaining({ kind: "unsupported" }),
);
});
it("does not allocate the declared decoded note size, but still validates stored ranges", () => {
const bytes = compressedElfNoteSectionFixture();
const noteSection = 64 + 64;
+85 -54
View File
@@ -901,6 +901,34 @@ export const parseElfNotes = (
const tables: ElfNotes["tables"] = [];
const parsed: ReturnType<typeof toEntry>[] = [];
const examined = { count: 0 };
const noteSections = [];
const compressedNoteRanges = new Set<string>();
const sectionTableOverlapsHeader = shnum !== 0 && shoff < header.headerSize;
if (!sectionTableOverlapsHeader) {
for (let index = 0; index < shnum; index += 1) {
const at = shoff + index * shentsize;
const declaration = readElfSectionHeader(
bytes.subarray(at, at + shentsize),
header.elfClass,
reader.little,
);
if (!declaration.ok) return fail(declaration.error.message);
const section = declaration.value;
const storage = elfNoteSectionStorage(section);
if (storage === "not-note") continue;
const { nameOffset, offset, size, alignment } = section;
noteSections.push({
index,
nameOffset,
offset,
size,
alignment,
storage,
});
if (storage === "compressed")
compressedNoteRanges.add(`${offset}:${size}`);
}
}
if (phnum !== 0) {
if (phoff < header.headerSize)
return fail("ELF program header table overlaps the native header.");
@@ -922,6 +950,15 @@ export const parseElfNotes = (
return fail("ELF offset or size exceeds a safe integer.");
const fileOffset = Number(program.offset);
const fileSize = Number(program.fileSize);
// An exact program alias does not turn compressed storage into a note
// stream. The section still owns the unsupported-storage outcome.
if (
compressedNoteRanges.size > 0 &&
compressedNoteRanges.has(`${program.offset}:${program.fileSize}`)
) {
reader.require(fileOffset, fileSize);
continue;
}
const alignment = noteAlignment(Number(program.alignment));
tables.push({
kind: "program",
@@ -942,61 +979,55 @@ export const parseElfNotes = (
parsed.push(toEntry(note, "program", index, header, text, fdo));
}
}
if (shnum !== 0) {
if (shoff < header.headerSize)
return fail("ELF section header table overlaps the native header.");
for (let index = 0; index < shnum; index += 1) {
const at = shoff + index * shentsize;
const parsedSection = readElfSectionHeader(
bytes.subarray(at, at + shentsize),
header.elfClass,
reader.little,
if (sectionTableOverlapsHeader)
return fail("ELF section header table overlaps the native header.");
for (const {
index,
nameOffset,
offset,
size,
alignment: declaredAlignment,
storage,
} of noteSections) {
if (
offset > BigInt(Number.MAX_SAFE_INTEGER) ||
size > BigInt(Number.MAX_SAFE_INTEGER)
)
return fail("ELF offset or size exceeds a safe integer.");
const fileOffset = Number(offset);
const fileSize = Number(size);
reader.require(fileOffset, fileSize);
if (storage === "compressed")
throw new BinaryFormatFailure(
"unsupported",
"Compressed ELF note sections are unsupported; descriptor file offsets require uncompressed note storage.",
);
if (!parsedSection.ok) return fail(parsedSection.error.message);
const section = parsedSection.value;
const storage = elfNoteSectionStorage(section);
if (storage === "not-note") continue;
const nameOffset = section.nameOffset;
if (
section.offset > BigInt(Number.MAX_SAFE_INTEGER) ||
section.size > BigInt(Number.MAX_SAFE_INTEGER)
)
return fail("ELF offset or size exceeds a safe integer.");
const fileOffset = Number(section.offset);
const fileSize = Number(section.size);
reader.require(fileOffset, fileSize);
if (storage === "compressed")
throw new BinaryFormatFailure(
"unsupported",
"Compressed ELF note sections are unsupported; descriptor file offsets require uncompressed note storage.",
);
if (section.alignment > BigInt(Number.MAX_SAFE_INTEGER))
return fail("ELF offset or size exceeds a safe integer.");
const alignment = noteAlignment(Number(section.alignment));
const sectionName =
shstrtab === null
? { name: null, limitation: null }
: reader.cString(shstrtab, nameOffset, text);
if (sectionName.limitation !== null)
sectionNameLimitations.add(sectionName.limitation);
tables.push({
kind: "section",
index,
name: sectionName.name,
location: { offset: fileOffset, bytes: fileSize },
alignment,
});
for (const note of parseNoteStream(
reader,
fileOffset,
fileSize,
alignment,
input.max_entries,
examined,
text,
))
parsed.push(toEntry(note, "section", index, header, text, fdo));
}
if (declaredAlignment > BigInt(Number.MAX_SAFE_INTEGER))
return fail("ELF offset or size exceeds a safe integer.");
const alignment = noteAlignment(Number(declaredAlignment));
const sectionName =
shstrtab === null
? { name: null, limitation: null }
: reader.cString(shstrtab, nameOffset, text);
if (sectionName.limitation !== null)
sectionNameLimitations.add(sectionName.limitation);
tables.push({
kind: "section",
index,
name: sectionName.name,
location: { offset: fileOffset, bytes: fileSize },
alignment,
});
for (const note of parseNoteStream(
reader,
fileOffset,
fileSize,
alignment,
input.max_entries,
examined,
text,
))
parsed.push(toEntry(note, "section", index, header, text, fdo));
}
const absent = tables.length === 0;
return {
+7 -2
View File
@@ -230,6 +230,7 @@ export const elfNoteWithUnreadableSectionNameFixture = (
export const compressedElfNoteSectionFixture = (
bits: 32 | 64 = 64,
little = true,
ownership: "section" | "both" = "section",
): Buffer => {
const notes = concat(
gnuBuildIdNote(undefined, little),
@@ -243,12 +244,16 @@ export const compressedElfNoteSectionFixture = (
bits,
little,
type: 1,
ownership: "section",
ownership,
sectionName: ".debug.notes",
alignment: bits === 64 ? 8 : 4,
notes: concat(header, deflateSync(notes)),
});
const noteSection = (bits === 64 ? 64 : 52) + (bits === 64 ? 64 : 40);
const headerSize = bits === 64 ? 64 : 52;
const programSize = bits === 64 ? 56 : 32;
const sectionSize = bits === 64 ? 64 : 40;
const noteSection =
headerSize + (ownership === "both" ? programSize : 0) + sectionSize;
writeWord(bytes, noteSection + 8, 0x800, bits, little);
return bytes;
};
+14 -12
View File
@@ -162,18 +162,20 @@ cliTest(
"reports compressed note storage as unsupported without mutation",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-elf-notes-storage-cli-");
const path = join(root, "debug-notes.o");
const bytes = compressedElfNoteSectionFixture();
await writeFile(path, bytes);
const result = await cli.run({
arguments: ["inspect-elf-notes", path, "--json"],
});
expect(result.exitCode).toBe(1);
expect(result.json).toMatchObject({
code: "unsupported_target",
message: expect.stringMatching(/compressed.*note/iu),
});
expect(Buffer.compare(await readFile(path), bytes)).toBe(0);
for (const ownership of ["section", "both"] as const) {
const path = join(root, `${ownership}.o`);
const bytes = compressedElfNoteSectionFixture(64, true, ownership);
await writeFile(path, bytes);
const result = await cli.run({
arguments: ["inspect-elf-notes", path, "--json"],
});
expect(result.exitCode).toBe(1);
expect(result.json).toMatchObject({
code: "unsupported_target",
message: expect.stringMatching(/compressed.*note/iu),
});
expect(Buffer.compare(await readFile(path), bytes)).toBe(0);
}
},
);