feat(native): read embedded cargo-auditable Rust dependency lists (#2456)

* feat(native): read embedded cargo-auditable Rust dependency lists

* fix(native): trim PE section padding and compute dependency cycles iteratively

* fix(native): ignore SHT_NOBITS sections and validate dependency edges linearly

* fix(native): bound compiler path scanning and retained hashes

* fix(rust): pass package arrays as named parameterized test cases

* fix(rust): admit graph expansion and preserve resource units

* fix(rust): reject malformed packages, false cycles, and hidden compiler paths

Fail the first malformed cargo-auditable package before Zod collects an
issue per field. Require cyclic dependency indices to match the cycles in
package dependencies. Keep read-only ELF ranges when a section name does
not fit the bounded name window, so a rustc path in that range is still
reported.

* fix(ci): repair integrated checks and Hopper test baseline

* fix(go): verify Mach-O sections against segments

---------

Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
This commit is contained in:
Matt Van Horn
2026-10-14 02:50:30 +08:00
committed by GitHub
co-authored by Matt Van Horn morluto Cursor Agent
parent c7ceaadb70
commit 0e6eca4085
59 changed files with 2767 additions and 12 deletions
+37
View File
@@ -34,6 +34,16 @@ export interface GoZeroFillMapping {
readonly size: bigint;
}
/** One named file section. Invalid declared ranges stay visible with `file_backed: false`. */
export interface GoImageSection {
readonly name: string;
readonly offset: number;
readonly size: number;
readonly file_backed: boolean;
readonly read_only: boolean;
readonly compressed: boolean;
}
/** Native container facts and its producer-defined build-info search area. */
export interface GoBinaryContainer {
readonly format: "elf" | "pe" | "macho";
@@ -42,6 +52,7 @@ export interface GoBinaryContainer {
readonly byte_order: "little" | "big";
readonly mappings: readonly GoFileMapping[];
readonly zero_fills: readonly GoZeroFillMapping[];
readonly sections: readonly GoImageSection[];
readonly search: GoFileMapping | null;
}
@@ -110,6 +121,32 @@ export class GoBinaryReader {
}
}
/** Admit a declared file span, or mark it unbacked when it leaves the file. */
export const declaredFileRange = (
fileBytes: number,
offset: bigint,
size: bigint,
): {
readonly offset: number;
readonly size: number;
readonly file_backed: boolean;
} => {
if (
offset < 0n ||
size < 0n ||
offset > BigInt(Number.MAX_SAFE_INTEGER) ||
size > BigInt(Number.MAX_SAFE_INTEGER) ||
offset > BigInt(fileBytes) ||
size > BigInt(fileBytes) - offset
)
return { offset: 0, size: 0, file_backed: false };
return {
offset: Number(offset),
size: Number(size),
file_backed: size > 0n,
};
};
/** Map a virtual span without losing precision or selecting an ambiguous alias. */
export const mappedFileOffset = (
mappings: readonly GoFileMapping[],
+3 -2
View File
@@ -55,7 +55,7 @@ export interface GoBinaryImage {
/** Inspect bounded executable bytes using native mappings, without loading or executing code. */
export const readGoBinaryImage = (bytes: Buffer): GoBinaryImage => {
const container = readContainer(bytes);
const container = readGoBinaryContainer(bytes);
const { format, architecture, bits, byte_order } = container;
const search = container.search;
const header = findHeader(bytes, search);
@@ -184,7 +184,8 @@ interface LocatedString {
readonly location: GoStringLocation;
}
const readContainer = (bytes: Buffer): GoBinaryContainer => {
/** Identify an ELF, PE, or thin Mach-O image and its named file sections. */
export const readGoBinaryContainer = (bytes: Buffer): GoBinaryContainer => {
if (bytes.subarray(0, 4).equals(Buffer.from([0x7f, 0x45, 0x4c, 0x46])))
return readGoElfImage(bytes);
if (bytes.subarray(0, 2).equals(Buffer.from("MZ")))
+107
View File
@@ -1,9 +1,11 @@
import {
declaredFileRange,
GoBinaryFormatFailure,
GoBinaryReader,
mappedFileOffset,
type GoBinaryContainer,
type GoFileMapping,
type GoImageSection,
type GoZeroFillMapping,
} from "./GoBinaryContainer.js";
import { readElfFixedHeader } from "../domain/elfFixedHeader.js";
@@ -100,6 +102,7 @@ export const readGoElfImage = (bytes: Buffer): GoBinaryContainer => {
);
const mappings: GoFileMapping[] = [];
const zeroFills: GoZeroFillMapping[] = [];
const sections: GoImageSection[] = [];
let fallback: GoFileMapping | null = null;
for (let index = 0; index < phCount; index++) {
const offset = phStart + index * phWidth;
@@ -158,6 +161,15 @@ export const readGoElfImage = (bytes: Buffer): GoBinaryContainer => {
"malformed",
"ELF section name offset is outside its string table",
);
recordElfSection(sections, {
bytes,
names,
nameOffset,
reader,
bits,
wide,
header: offset,
});
// Comparing the complete fixed name avoids quadratic scans through malicious string tables.
const expectedName = Buffer.from(".go.buildinfo\0");
if (
@@ -213,10 +225,105 @@ export const readGoElfImage = (bytes: Buffer): GoBinaryContainer => {
byte_order: reader.little ? "little" : "big",
mappings,
zero_fills: zeroFills,
sections,
search: search ?? fallback,
};
};
const recordElfSection = (
sections: GoImageSection[],
input: {
readonly bytes: Buffer;
readonly names: { readonly offset: number; readonly size: number };
readonly nameOffset: number;
readonly reader: GoBinaryReader;
readonly bits: 32 | 64;
readonly wide: boolean;
readonly header: number;
},
): void => {
const decoded = elfSectionName(input.bytes, input.names, input.nameOffset);
const header = {
reader: input.reader,
bits: input.bits,
wide: input.wide,
header: input.header,
};
switch (decoded.status) {
case "named":
sections.push(elfImageSection({ ...header, name: decoded.name }));
return;
case "truncated": {
// Names past this window stay out of .dep-v0 matching. Read-only
// bytes can still hold the only compiler path.
const section = elfImageSection({ ...header, name: "" });
if (section.read_only) sections.push(section);
return;
}
case "unnamed":
return;
default: {
const unreachable: never = decoded;
return unreachable;
}
}
};
const elfSectionName = (
bytes: Buffer,
names: { readonly offset: number; readonly size: number },
nameOffset: number,
):
| { readonly status: "named"; readonly name: string }
| { readonly status: "unnamed" }
| { readonly status: "truncated" } => {
const start = names.offset + nameOffset;
const limit = Math.min(start + 64, names.offset + names.size);
const nul = bytes.subarray(start, limit).indexOf(0);
if (nul === 0) return { status: "unnamed" };
if (nul < 0) return { status: "truncated" };
return {
status: "named",
name: bytes.toString("latin1", start, start + nul),
};
};
const elfImageSection = ({
reader,
bits,
wide,
header,
name,
}: {
readonly reader: GoBinaryReader;
readonly bits: 32 | 64;
readonly wide: boolean;
readonly header: number;
readonly name: string;
}): GoImageSection => {
const type = reader.u32(header + 4);
const flags = reader.word(header + 8, bits);
const declared = declaredFileRange(
reader.bytes.length,
reader.word(header + (wide ? 24 : 16), bits),
reader.word(header + (wide ? 32 : 20), bits),
);
// SHT_NOBITS offsets and sizes do not describe stored bytes.
const range =
type === SHT_NOBITS ? { ...declared, file_backed: false } : declared;
const compressed = (flags & 0x800n) !== 0n;
const readOnly =
type === 1 &&
range.file_backed &&
!compressed &&
(flags & 1n) === 0n &&
(flags & 4n) === 0n &&
(flags & 2n) !== 0n;
return { name, ...range, read_only: readOnly, compressed };
};
const SHT_NOBITS = 8;
const elfArchitecture = (machine: number): string => {
const names = new Map([
[3, "386"],
+86 -5
View File
@@ -1,9 +1,11 @@
import {
declaredFileRange,
GoBinaryFormatFailure,
GoBinaryReader,
mappedFileOffset,
type GoBinaryContainer,
type GoFileMapping,
type GoImageSection,
type GoZeroFillMapping,
} from "./GoBinaryContainer.js";
@@ -30,6 +32,7 @@ export const readGoMachoImage = (bytes: Buffer): GoBinaryContainer => {
);
const mappings: GoFileMapping[] = [];
const zeroFills: GoZeroFillMapping[] = [];
const sections: GoImageSection[] = [];
const named: GoFileMapping[] = [];
let fallback: GoFileMapping | null = null;
let offset = commandStart;
@@ -105,11 +108,25 @@ export const readGoMachoImage = (bytes: Buffer): GoBinaryContainer => {
fallback = mapping;
for (let sectionIndex = 0; sectionIndex < sectionCount; sectionIndex++) {
const section = offset + segmentHeader + sectionIndex * sectionWidth;
if (
fixedName(reader.range(section, 16, "Mach-O section name")) !==
"__go_buildinfo"
)
continue;
const sectionName = fixedName(
reader.range(section, 16, "Mach-O section name"),
);
sections.push(
machoImageSection({
reader,
bits,
wide,
header: section,
name: `${fixedName(reader.range(section + 16, 16, "Mach-O section segment"))},${sectionName}`,
protection,
unmapped: unmapped || mapping === undefined,
segmentAddress: address,
segmentMemorySize: memorySize,
segmentFileOffset: fileOffset,
segmentFileSize: fileSize,
}),
);
if (sectionName !== "__go_buildinfo") continue;
if (unmapped)
throw new GoBinaryFormatFailure(
"malformed",
@@ -175,10 +192,74 @@ export const readGoMachoImage = (bytes: Buffer): GoBinaryContainer => {
byte_order: little ? "little" : "big",
mappings,
zero_fills: zeroFills,
sections,
search: named[0] ?? fallback,
};
};
const machoImageSection = ({
reader,
bits,
wide,
header,
name,
protection,
unmapped,
segmentAddress,
segmentMemorySize,
segmentFileOffset,
segmentFileSize,
}: {
readonly reader: GoBinaryReader;
readonly bits: 32 | 64;
readonly wide: boolean;
readonly header: number;
readonly name: string;
readonly protection: number;
readonly unmapped: boolean;
readonly segmentAddress: bigint;
readonly segmentMemorySize: bigint;
readonly segmentFileOffset: bigint;
readonly segmentFileSize: bigint;
}): GoImageSection => {
const flags = reader.u32(header + (wide ? 64 : 56));
const zeroFill = [1, 0xc, 0x12].includes(flags & 0xff);
const sectionAddress = reader.word(header + 32, bits);
const sectionSize = reader.word(header + (wide ? 40 : 36), bits);
const sectionOffset = BigInt(reader.u32(header + (wide ? 48 : 40)));
const addressDelta = sectionAddress - segmentAddress;
const fileDelta = sectionOffset - segmentFileOffset;
const inSegmentMemory =
addressDelta >= 0n &&
addressDelta <= segmentMemorySize &&
sectionSize <= segmentMemorySize - addressDelta;
const inSegmentFile =
fileDelta >= 0n &&
fileDelta <= segmentFileSize &&
sectionSize <= segmentFileSize - fileDelta;
const range = unmapped
? { offset: 0, size: 0, file_backed: false }
: declaredFileRange(
reader.bytes.length,
BigInt(reader.u32(header + (wide ? 48 : 40))),
reader.word(header + (wide ? 40 : 36), bits),
);
const fileBacked =
range.file_backed && !zeroFill && inSegmentMemory && inSegmentFile;
const readOnly =
fileBacked &&
(protection & 1) !== 0 &&
(protection & 2) === 0 &&
(flags & 0x80000400) === 0;
return {
name,
...range,
file_backed: fileBacked,
read_only: readOnly,
compressed: false,
};
};
const fixedName = (bytes: Buffer): string => {
const nul = bytes.indexOf(0);
return bytes.subarray(0, nul < 0 ? bytes.length : nul).toString("latin1");
+51
View File
@@ -1,9 +1,11 @@
import { observeMzWindowsHeader, parseDosMzHeader } from "../domain/dosMz.js";
import {
declaredFileRange,
GoBinaryFormatFailure,
GoBinaryReader,
type GoBinaryContainer,
type GoFileMapping,
type GoImageSection,
type GoZeroFillMapping,
} from "./GoBinaryContainer.js";
@@ -86,14 +88,30 @@ export const readGoPeImage = (bytes: Buffer): GoBinaryContainer => {
);
const mappings: GoFileMapping[] = [];
const zeroFills: GoZeroFillMapping[] = [];
const sections: GoImageSection[] = [];
let search: GoFileMapping | null = null;
for (let index = 0; index < count; index++) {
const entry = start + index * 40;
const sectionName = peSectionName(
reader.range(entry, 8, "PE section name"),
);
const virtualSize = reader.u32(entry + 8);
const virtualAddress = reader.u32(entry + 12);
const size = reader.u32(entry + 16);
const offset = reader.u32(entry + 20);
const characteristics = reader.u32(entry + 36);
// SizeOfRawData includes file-alignment padding. VirtualSize is the content.
const contentSize = virtualSize === 0 ? size : Math.min(virtualSize, size);
if (sectionName.length > 0)
sections.push(
peImageSection({
reader,
name: sectionName,
size: contentSize,
offset,
characteristics,
}),
);
const address = imageBase + BigInt(virtualAddress);
if (address + BigInt(Math.max(size, virtualSize)) > 1n << BigInt(bits))
throw new GoBinaryFormatFailure(
@@ -138,6 +156,39 @@ export const readGoPeImage = (bytes: Buffer): GoBinaryContainer => {
byte_order: "little",
mappings,
zero_fills: zeroFills,
sections,
search,
};
};
const peSectionName = (bytes: Buffer): string => {
const nul = bytes.indexOf(0);
return bytes.toString("latin1", 0, nul < 0 ? bytes.length : nul);
};
const peImageSection = ({
reader,
name,
size,
offset,
characteristics,
}: {
readonly reader: GoBinaryReader;
readonly name: string;
readonly size: number;
readonly offset: number;
readonly characteristics: number;
}): GoImageSection => {
const range = declaredFileRange(
reader.bytes.length,
BigInt(offset),
BigInt(size),
);
const readOnly =
range.file_backed &&
(characteristics & 0x40000000) !== 0 &&
(characteristics & 0x80000000) === 0 &&
(characteristics & 0x20000000) === 0 &&
(characteristics & 0x40) !== 0;
return { name, ...range, read_only: readOnly, compressed: false };
};