From cb9fce082def7c0003b29c93ed680801b65eef96 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 10:20:25 -0500 Subject: [PATCH] fix(elf): preserve compressed note storage through program aliases (#12390) --- src/native/elf/ElfNoteParser.test.ts | 51 ++++++++-- src/native/elf/ElfNoteParser.ts | 139 ++++++++++++++++----------- src/native/elf/ElfNotes.fixture.ts | 9 +- tests/acceptance/elfNotesCli.test.ts | 26 ++--- 4 files changed, 151 insertions(+), 74 deletions(-) diff --git a/src/native/elf/ElfNoteParser.test.ts b/src/native/elf/ElfNoteParser.test.ts index bcdbba9963..4dc29eac4c 100644 --- a/src/native/elf/ElfNoteParser.test.ts +++ b/src/native/elf/ElfNoteParser.test.ts @@ -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; diff --git a/src/native/elf/ElfNoteParser.ts b/src/native/elf/ElfNoteParser.ts index 2f0e12d148..5df6862915 100644 --- a/src/native/elf/ElfNoteParser.ts +++ b/src/native/elf/ElfNoteParser.ts @@ -901,6 +901,34 @@ export const parseElfNotes = ( const tables: ElfNotes["tables"] = []; const parsed: ReturnType[] = []; const examined = { count: 0 }; + const noteSections = []; + const compressedNoteRanges = new Set(); + 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 { diff --git a/src/native/elf/ElfNotes.fixture.ts b/src/native/elf/ElfNotes.fixture.ts index aa696b5bef..a8d0628cd0 100644 --- a/src/native/elf/ElfNotes.fixture.ts +++ b/src/native/elf/ElfNotes.fixture.ts @@ -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; }; diff --git a/tests/acceptance/elfNotesCli.test.ts b/tests/acceptance/elfNotesCli.test.ts index 5074d8e84e..3784a76560 100644 --- a/tests/acceptance/elfNotesCli.test.ts +++ b/tests/acceptance/elfNotesCli.test.ts @@ -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); + } }, );