Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(go): honor amd64p32 pointer width

morluto98a232299878
diff --git a/docs/go-binaries.md b/docs/go-binaries.md
index 6dfc45674..794652003 100644
--- a/docs/go-binaries.md
+++ b/docs/go-binaries.md
@@ -112,8 +112,9 @@ module record and no dependency records has an observed empty dependency list.
The built-in TypeScript reader supports ELF, PE and thin Mach-O images, and one
selected slice of a universal Mach-O, with Go's inline or historical pointer
build-info encoding. Source ranges refer to the original stable file snapshot,
-including pointer-backed strings. It does not scan arbitrary byte strings as
-compiler metadata.
+including pointer-backed strings. Legacy pointer-backed fields are decoded at
+the width declared by Go's build-info record, independently of the image's ELF
+class. It does not scan arbitrary byte strings as compiler metadata.
A universal (FAT) Mach-O needs `architecture` (`x86`, `x86_64`, `arm`
or `arm64`; CLI `--architecture`). REA reads its own FAT table, without `lipo`,
diff --git a/src/go/GoBinaryImage.ts b/src/go/GoBinaryImage.ts
index 8519b9745..4cec34c94 100644
--- a/src/go/GoBinaryImage.ts
+++ b/src/go/GoBinaryImage.ts
@@ -118,16 +118,12 @@ export const readGoBinaryImage = (bytes: Buffer): GoBinaryImage => {
"malformed",
"Go build-info pointer width must be 4 or 8",
);
- if (ptrSize * 8 !== bits)
- throw new BinaryFormatFailure(
- "malformed",
- "Go build-info pointer width disagrees with the image",
- );
- const pointerBits = ptrSize === 8 ? 64 : 32;
+ const pointerBits = ptrSize === 4 ? 32 : 64;
version = readPointerString(
reader,
container,
reader.word(header + 16, pointerBits),
+ pointerBits,
);
const moduleField = header + 16 + ptrSize;
module = readOptionalModuleString(
@@ -135,6 +131,7 @@ export const readGoBinaryImage = (bytes: Buffer): GoBinaryImage => {
container,
reader.word(moduleField, pointerBits),
moduleField,
+ pointerBits,
);
}
if (version.location.bytes + module.location.bytes > MAX_BUILD_INFO_BYTES)
@@ -267,12 +264,13 @@ const readPointerString = (
reader: BinaryReader,
container: BinaryContainer,
address: bigint,
+ pointerBits: 32 | 64,
): LocatedString => {
- const { mappings, zero_fills: zeroFills, bits } = container;
- const width = bits / 8;
+ const { mappings, zero_fills: zeroFills } = container;
+ const width = pointerBits / 8;
const header = mappedFileOffset(mappings, address, width * 2, zeroFills);
- const valueAddress = reader.word(header, bits);
- const length = reader.word(header + width, bits);
+ const valueAddress = reader.word(header, pointerBits);
+ const length = reader.word(header + width, pointerBits);
if (length > BigInt(MAX_BUILD_INFO_BYTES))
throw new BinaryResourceFailure(
"build-info",
@@ -296,15 +294,16 @@ const readOptionalModuleString = (
container: BinaryContainer,
address: bigint,
pointerField: number,
+ pointerBits: 32 | 64,
): LocatedString =>
isZeroFilledSpan(
container.mappings,
container.zero_fills,
address,
- (container.bits / 8) * 2,
+ (pointerBits / 8) * 2,
)
? { bytes: Buffer.alloc(0), location: { offset: pointerField, bytes: 0 } }
- : readPointerString(reader, container, address);
+ : readPointerString(reader, container, address, pointerBits);
/** Decode metadata text without replacing invalid bytes or stripping an initial BOM. */
export const observedUtf8 = (bytes: Buffer, label: string): string => {
diff --git a/tests/acceptance/goBinaryP32PointerCli.test.ts b/tests/acceptance/goBinaryP32PointerCli.test.ts
new file mode 100644
index 000000000..e5b87b539
--- /dev/null
+++ b/tests/acceptance/goBinaryP32PointerCli.test.ts
@@ -0,0 +1,46 @@
+import { writeFile } from "node:fs/promises";
+import { join } from "node:path";
+import { expect } from "vitest";
+import { evidenceResultOf } from "../../src/contracts/toolOutputSchemaPrimitives.js";
+import { goBinarySchema } from "../../src/domain/go/goBinary.js";
+import { createGoBinaryFixture, GO_MODULE_TEXT } from "../fixtures/go/image.js";
+import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
+import { cliTest, runFileCli } from "../support/cli/cliFixture.js";
+
+cliTest(
+ "inspects ELF64 Go metadata with four-byte amd64p32 pointers through CLI",
+ async ({ cli }) => {
+ const root = await createTestTempDirectory("rea-go-amd64p32-cli-");
+ const fixture = createGoBinaryFixture({
+ format: "elf",
+ bits: 64,
+ encoding: "pointer",
+ pointerSize: 4,
+ goVersion: "go1.13.15",
+ });
+ const path = join(root, "nacl-amd64p32.nexe");
+ await writeFile(path, fixture.bytes);
+ const result = await runFileCli(cli, {
+ arguments: ["inspect-go-binary", path, "--json"],
+ });
+
+ expect(result.exitCode, result.stderr).toBe(0);
+ const evidence = evidenceResultOf(goBinarySchema).parse(result.json);
+ expect(evidence.subject?.architecture).toBe("x86_64");
+ expect(evidence.normalized_result).toMatchObject({
+ format: "elf",
+ architecture: "amd64",
+ bits: 64,
+ build_info: {
+ encoding: "pointer",
+ go_version: "go1.13.15",
+ module_text: GO_MODULE_TEXT,
+ version_location: { offset: fixture.versionOffset, bytes: 9 },
+ module_location: {
+ offset: fixture.moduleOffset,
+ bytes: fixture.moduleBytes.length,
+ },
+ },
+ });
+ },
+);
diff --git a/tests/boundary/go/goBinaryP32PointerWidth.test.ts b/tests/boundary/go/goBinaryP32PointerWidth.test.ts
new file mode 100644
index 000000000..0602d8549
--- /dev/null
+++ b/tests/boundary/go/goBinaryP32PointerWidth.test.ts
@@ -0,0 +1,32 @@
+import { expect, it } from "vitest";
+import { readGoBinaryImage } from "../../../src/go/GoBinaryImage.js";
+import {
+ createGoBinaryFixture,
+ GO_MODULE_TEXT,
+} from "../../fixtures/go/image.js";
+
+it("decodes legacy ELF64 Go strings at the record's four-byte pointer width", () => {
+ const fixture = createGoBinaryFixture({
+ format: "elf",
+ bits: 64,
+ encoding: "pointer",
+ pointerSize: 4,
+ goVersion: "go1.13.15",
+ });
+
+ expect(readGoBinaryImage(fixture.bytes)).toMatchObject({
+ format: "elf",
+ architecture: "amd64",
+ bits: 64,
+ build_info: {
+ encoding: "pointer",
+ go_version: "go1.13.15",
+ module_text: GO_MODULE_TEXT,
+ version_location: { offset: fixture.versionOffset, bytes: 9 },
+ module_location: {
+ offset: fixture.moduleOffset,
+ bytes: fixture.moduleBytes.length,
+ },
+ },
+ });
+});
diff --git a/tests/fixtures/go/image.ts b/tests/fixtures/go/image.ts
index 1fb3a53d8..fccc8b63a 100644
--- a/tests/fixtures/go/image.ts
+++ b/tests/fixtures/go/image.ts
@@ -53,6 +53,7 @@ export const createGoBinaryFixture = (
options: {
format?: "elf" | "pe" | "macho";
bits?: 32 | 64;
+ pointerSize?: 4 | 8;
byteOrder?: "little" | "big";
encoding?: "inline" | "pointer";
sectionless?: boolean;
@@ -69,6 +70,7 @@ export const createGoBinaryFixture = (
} => {
const format = options.format ?? "elf";
const bits = options.bits ?? 64;
+ const pointerSize = options.pointerSize ?? bits / 8;
const little = (options.byteOrder ?? "little") === "little";
const encoding = options.encoding ?? "inline";
const bytes = Buffer.alloc(4096);
@@ -83,6 +85,10 @@ export const createGoBinaryFixture = (
if (bits === 64) view.setBigUint64(offset, BigInt(value), little);
else u32(offset, value);
};
+ const pointerWord = (offset: number, value: number) => {
+ if (pointerSize === 8) view.setBigUint64(offset, BigInt(value), little);
+ else u32(offset, value);
+ };
writeFixtureContainer(format, options.sectionless ?? false, {
bytes,
bits,
@@ -111,15 +117,15 @@ export const createGoBinaryFixture = (
moduleLength.copy(bytes, versionOffset + version.length);
moduleBytes.copy(bytes, moduleOffset);
} else {
- const width = bits / 8;
- word(headerOffset + 16, base + 1536);
- word(headerOffset + 16 + width, base + 1536 + width * 2);
+ const width = pointerSize;
+ pointerWord(headerOffset + 16, base + 1536);
+ pointerWord(headerOffset + 16 + width, base + 1536 + width * 2);
versionOffset = 2048;
moduleOffset = 2304;
- word(1536, base + versionOffset);
- word(1536 + width, version.length);
- word(1536 + width * 2, base + moduleOffset);
- word(1536 + width * 3, moduleBytes.length);
+ pointerWord(1536, base + versionOffset);
+ pointerWord(1536 + width, version.length);
+ pointerWord(1536 + width * 2, base + moduleOffset);
+ pointerWord(1536 + width * 3, moduleBytes.length);
version.copy(bytes, versionOffset);
moduleBytes.copy(bytes, moduleOffset);
}
@@ -128,7 +134,7 @@ export const createGoBinaryFixture = (
bytes,
headerOffset,
);
- bytes[headerOffset + 14] = bits / 8;
+ bytes[headerOffset + 14] = pointerSize;
bytes[headerOffset + 15] =
(little ? 0 : 1) | (encoding === "inline" ? 2 : 0);
}
@@ -272,6 +278,10 @@ export const createGoBinaryDiagnosticFixtures = () => {
);
return { name: `high-bit-${signature.toLowerCase()}`, bytes };
});
+ const unresolvedWindowsCandidates = [
+ { name: "damaged-pe", bytes: damagedPe },
+ ...highBitSignatures,
+ ];
const absentDirectories = createGoBinaryFixture({ format: "pe" }).bytes;
absentDirectories.writeUInt32LE(1, 152 + 108);
const shortOptionalHeaders = [0, 1].map((size) => {
@@ -287,9 +297,12 @@ export const createGoBinaryDiagnosticFixtures = () => {
code: "unsupported_target" as const,
details: { operation: "inspect_go_binary" },
})),
+ ...unresolvedWindowsCandidates.map((fixture) => ({
+ ...fixture,
+ code: "unsupported_target" as const,
+ details: { operation: "inspect_go_binary" },
+ })),
...[
- { name: "damaged-pe", bytes: damagedPe },
- ...highBitSignatures,
{ name: "absent-directories", bytes: absentDirectories },
...shortOptionalHeaders,
].map((fixture) => ({