Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(flutter): honor ELF note alignment when reading build IDs (#6423)

RRFZ572d0c234089
diff --git a/src/domain/native/elfNoteLayout.ts b/src/domain/native/elfNoteLayout.ts
new file mode 100644
index 000000000..ca878fedf
--- /dev/null
+++ b/src/domain/native/elfNoteLayout.ts
@@ -0,0 +1,16 @@
+/** ELF note padding follows binutils: only a declared alignment of 8 uses 8. */
+export const elfNoteAlignment = (declared: number | bigint): number =>
+ declared === 8 || declared === 8n ? 8 : 4;
+
+/** Descriptor and next-note offsets relative to the note's 12-byte header. */
+export const elfNoteLayout = (
+ namesz: number,
+ descsz: number,
+ alignment: number,
+) => {
+ const descriptor = Math.ceil((12 + namesz) / alignment) * alignment;
+ return {
+ descriptor,
+ next: Math.ceil((descriptor + descsz) / alignment) * alignment,
+ };
+};
diff --git a/src/flutter/ElfBuildId.ts b/src/flutter/ElfBuildId.ts
index a7eb6145d..91a6ac1c3 100644
--- a/src/flutter/ElfBuildId.ts
+++ b/src/flutter/ElfBuildId.ts
@@ -1,5 +1,9 @@
import type { FileHandle } from "node:fs/promises";
import { setImmediate as yieldToEventLoop } from "node:timers/promises";
+import {
+ elfNoteAlignment,
+ elfNoteLayout,
+} from "../domain/native/elfNoteLayout.js";
import {
readElfHeader,
ELF_HEADER_MAX_BYTES,
@@ -62,7 +66,10 @@ export const readElfBuildIds = async (
const parsed = readElfHeader(prefix, totalBytes);
if (parsed.failure !== undefined) return failure(parsed.failure.reason);
const h = parsed.header;
- const ranges = new Map<string, { offset: number; size: number }>();
+ const ranges = new Map<
+ string,
+ { offset: number; size: number; alignment: number }
+ >();
tables: for (const table of [
{
offset: h.e_phoff,
@@ -110,7 +117,14 @@ export const readElfBuildIds = async (
)
return failure("ELF note range is not file-backed");
if (size === 0) continue;
- const key = `${offset}:${size}`;
+ const alignmentAt =
+ base + (table.program ? (h.is64 ? 48 : 28) : h.is64 ? 48 : 32);
+ const alignment = elfNoteAlignment(
+ h.is64
+ ? bytes.readBigUInt64LE(alignmentAt)
+ : bytes.readUInt32LE(alignmentAt),
+ );
+ const key = `${offset}:${size}:${alignment}`;
if (ranges.has(key)) continue;
if (ranges.size === MAX_NOTE_RANGES) {
failures.push({
@@ -119,10 +133,9 @@ export const readElfBuildIds = async (
});
break tables;
}
- ranges.set(key, { offset, size });
+ ranges.set(key, { offset, size, alignment });
}
}
- const align4 = (value: number): number => Math.ceil(value / 4) * 4;
for (const range of ranges.values()) {
const notes = await read(range.offset, range.size);
if (notes === null)
@@ -144,7 +157,8 @@ export const readElfBuildIds = async (
const namesz = notes.readUInt32LE(at);
const descsz = notes.readUInt32LE(at + 4);
const type = notes.readUInt32LE(at + 8);
- const descriptor = at + 12 + align4(namesz);
+ const layout = elfNoteLayout(namesz, descsz, range.alignment);
+ const descriptor = at + layout.descriptor;
const end = descriptor + descsz;
if (end > notes.length)
return failure("ELF note descriptor exceeds its declared note range");
@@ -164,7 +178,7 @@ export const readElfBuildIds = async (
);
candidates.add(candidate);
}
- at = align4(end);
+ at += layout.next;
}
}
signal?.throwIfAborted();
diff --git a/src/native/elf/ElfNoteParser.ts b/src/native/elf/ElfNoteParser.ts
index 80474c552..272d13a2b 100644
--- a/src/native/elf/ElfNoteParser.ts
+++ b/src/native/elf/ElfNoteParser.ts
@@ -1,6 +1,10 @@
import { createHash } from "node:crypto";
import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
import { readElfFixedHeader } from "../../domain/elfFixedHeader.js";
+import {
+ elfNoteAlignment as noteAlignment,
+ elfNoteLayout,
+} from "../../domain/native/elfNoteLayout.js";
import type {
ElfNotes,
InspectElfNotesInput,
@@ -49,11 +53,6 @@ const chargeText = (
const sha256 = (bytes: Buffer): string =>
createHash("sha256").update(bytes).digest("hex");
-const alignTo = (value: number, alignment: number): number => {
- if (alignment <= 1) return value;
- return Math.ceil(value / alignment) * alignment;
-};
-
const GNU_OS_NAMES: Readonly<Record<number, string>> = {
0: "linux",
1: "hurd",
@@ -140,11 +139,6 @@ class ElfReader {
}
}
-/** Note name/descriptor padding follows the containing table's p_align or
- * sh_addralign: 8 stays 8; 0, 1, 4, and other values pad as 4, matching
- * readelf/binutils. */
-const noteAlignment = (declared: number): number => (declared === 8 ? 8 : 4);
-
interface ParsedNote {
readonly location: { offset: number; bytes: number };
readonly namesz: number;
@@ -187,8 +181,11 @@ const parseNoteStream = (
// namesz/descsz disagrees with binutils when alignment is 8 because the
// 12-byte note header is not 8-aligned (GNU property namesz=4 then starts
// its descriptor at 16, not 20).
- const descRel = alignTo(12 + namesz, alignment);
- const nextRel = alignTo(descRel + descsz, alignment);
+ const { descriptor: descRel, next: nextRel } = elfNoteLayout(
+ namesz,
+ descsz,
+ alignment,
+ );
const next = at + nextRel;
if (next > size) fail("ELF note entry exceeds its table.");
if (next <= at) fail("ELF note alignment did not advance.");
diff --git a/tests/boundary/filesystem/flutterElfNotes.test.ts b/tests/boundary/filesystem/flutterElfNotes.test.ts
index bdf461117..80062460b 100644
--- a/tests/boundary/filesystem/flutterElfNotes.test.ts
+++ b/tests/boundary/filesystem/flutterElfNotes.test.ts
@@ -2,6 +2,10 @@ import { open, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, vi } from "vitest";
import { readElfBuildIds } from "../../../src/flutter/ElfBuildId.js";
+import {
+ elfNote,
+ elfNoteFixture,
+} from "../../../src/native/elf/ElfNotes.fixture.js";
import {
appendElfRanges,
engineFixtureImage,
@@ -13,6 +17,86 @@ const note = Buffer.from(
"hex",
);
+it.each([32, 64] as const)(
+ "reads aligned consecutive notes in ELF%d",
+ async (bits) => {
+ for (const alignment of [4, 8]) {
+ for (const table of ["program", "section", "both"] as const) {
+ const ids = ["11".repeat(20), "22".repeat(20)];
+ const image = elfNoteFixture({
+ bits,
+ alignment,
+ ownership: table === "program" ? "program" : "both",
+ notes: Buffer.concat(
+ ids.map((id) =>
+ elfNote("GNU", 3, Buffer.from(id, "hex"), true, alignment),
+ ),
+ ),
+ });
+ if (table === "section") image.writeUInt32LE(1, bits === 64 ? 64 : 52);
+ const root = await createTestTempDirectory("rea-flutter-alignment-");
+ const path = join(root, "engine.so");
+ await writeFile(path, image);
+ const file = await open(path, "r");
+ try {
+ expect(
+ await readElfBuildIds(file, image.length, undefined),
+ `${bits}/${table}/${alignment}`,
+ ).toEqual({ candidates: ids, failures: [] });
+ } finally {
+ await file.close();
+ }
+ }
+ }
+ },
+);
+
+it.each(
+ ([32, 64] as const).flatMap((bits) => [
+ { bits, program: 8, section: 4, reads: 5 },
+ { bits, program: 1, section: 4, reads: 4 },
+ ]),
+)(
+ "keys shared $bits-bit note ranges by effective alignment (p_align $program, sh_addralign $section)",
+ async ({ bits, program, section, reads }) => {
+ // 24-byte IDs pad identically at four and eight bytes, so only the range
+ // key decides whether the second declaration is read.
+ const ids = ["11".repeat(24), "22".repeat(24)];
+ const image = elfNoteFixture({
+ bits,
+ alignment: 4,
+ ownership: "both",
+ notes: Buffer.concat(
+ ids.map((id) => elfNote("GNU", 3, Buffer.from(id, "hex"))),
+ ),
+ });
+ const header = bits === 64 ? 64 : 52;
+ const noteSection =
+ header + (bits === 64 ? 56 : 32) + (bits === 64 ? 64 : 40);
+ const writeAlignment = (offset: number, value: number) => {
+ if (bits === 64) image.writeBigUInt64LE(BigInt(value), offset);
+ else image.writeUInt32LE(value, offset);
+ };
+ writeAlignment(header + (bits === 64 ? 48 : 28), program);
+ writeAlignment(noteSection + (bits === 64 ? 48 : 32), section);
+ const root = await createTestTempDirectory("rea-flutter-alignment-");
+ const path = join(root, "engine.so");
+ await writeFile(path, image);
+ const file = await open(path, "r");
+ const read = vi.spyOn(file, "read");
+ try {
+ expect(await readElfBuildIds(file, image.length, undefined)).toEqual({
+ candidates: ids,
+ failures: [],
+ });
+ // Header prefix, both tables, then one read per distinct note range.
+ expect(read.mock.calls.length).toBe(reads);
+ } finally {
+ await file.close();
+ }
+ },
+);
+
it.each([
{ bits: 32, table: "program" },
{ bits: 64, table: "section" },