Files
rea/src/domain/native/elfNotes.ts
T
morluto 6ab595cfd6 fix(limits)!: make the IL2CPP pointer index adjustable and require owned refusal limits (#14243)
* fix(limits): make the IL2CPP pointer index adjustable and name ELF note budgets in details.limit

inspect_il2cpp_metadata accepts max_native_index_bytes (default 1 GiB,
needs binary_path) for the estimated ELF virtual pointer index. The index
refusal now carries its own limit name (virtual-pointer-index-bytes) and a
details.limit naming the parameter, its effective value, default and range,
instead of a fixed ceiling with no override. The binary file allowance and
the loader's fixed capacities are unchanged.

inspect_elf_notes and inspect_elf_debug_link refusals for max_file_bytes,
the decoded section-name budget and max_entries now set details.limit. One
domain-owned bounds object per operation feeds both the schema and the limit.

Two guards keep new in-code refusal limits owned:
- scripts/verify/fixed-ceilings.mjs (run by npm run lint) requires every
  fixedAllowanceOptions, fixedProducerInputAllowance,
  artifactFixedLimitFailure or inline fixedAllowance declaration to be
  listed in fixed-ceiling-registry.json with its class (format, protocol,
  representation, derived, forwarding, or pending with an issue) and reason,
  and fails on stale entries.
- The untyped-budget-refusal architecture guard counts refusals whose only
  recovery is an authored remediationAction, with a shrink-only baseline.

Fixes #14175
Part of #14234

BREAKING CHANGE: an inspect_il2cpp_metadata refusal at the ELF pointer index
reports reported_limits.boundary "virtual-pointer-index-bytes" and
details.limit for max_native_index_bytes, and no longer carries a
details.refusal fixed_ceiling.

* fix(limits): track IL2CPP's remaining fixed allowances with the native readers

* fix(limits): run the authored-only refusal count from lint, where TypeScript is installed

The architecture guards run before npm ci in CI and must stay
dependency-free, so the AST-based count moves into fixed-ceilings.mjs with
its own shrink-only baseline and --shrink-baselines.

* fix(limits): register producer and native wrapper ceilings by the allowance they name

fixedProducerInputAllowance names its allowance in its second argument, and
fixedNativeAllowance forwards its first, so the guard now reads each helper's
allowance argument and checks the callers of both.

* fix(limits): delete the authored-only baseline when its last count is removed

reconcileBaselineFile returns delete once no count remains, but the
verifier ignored it and the next run read the file unconditionally, so
--shrink-baselines could not finish. An absent baseline now reads as empty
and shrinking removes the file.

* fix(limits): record the advertised IL2CPP input schema with max_native_index_bytes
2026-11-23 23:37:23 -05:00

303 lines
8.1 KiB
TypeScript

import { z } from "zod";
import { digestSchema } from "../digests.js";
import { jsonObjectSchema } from "../jsonValue.js";
import { absoluteLocalPathStringSchema } from "../localPath.js";
import {
DEFAULT_ELF_READ_FILE_BYTES,
MINIMUM_ELF_READ_FILE_BYTES,
type ElfReadBudgets,
} from "./elfReadBudgets.js";
const uint = z.number().int().min(0).max(0xffffffff);
const range = z.strictObject({ offset: uint, bytes: uint });
/** Hard ceiling on the caller-selected input read budget. */
export const MAX_INSPECT_ELF_NOTE_FILE_BYTES = 536870912;
/** Hard ceiling on decoded note entries across every declared table. */
export const MAX_INSPECT_ELF_NOTE_ENTRIES = 256;
/**
* Default note entry budget. Distribution glibc builds carry dozens of
* SystemTap `.note.stapsdt` probes. Ubuntu 26.04's libc.so.6 has 82 distinct
* notes, examined as 85 entries because its allocated notes appear in both the
* program and section tables; the default admits that with room to spare.
*/
export const DEFAULT_INSPECT_ELF_NOTE_ENTRIES = 128;
/** The per-call budgets `inspect_elf_notes` accepts, shared by its schema and refusals. */
export const ELF_NOTES_READ_BUDGETS: ElfReadBudgets = {
max_file_bytes: {
minimum: MINIMUM_ELF_READ_FILE_BYTES,
maximum: MAX_INSPECT_ELF_NOTE_FILE_BYTES,
default_value: DEFAULT_ELF_READ_FILE_BYTES,
},
max_entries: {
minimum: 1,
maximum: MAX_INSPECT_ELF_NOTE_ENTRIES,
default_value: DEFAULT_INSPECT_ELF_NOTE_ENTRIES,
},
};
export const ELF_NOTE_TYPE_NAMES = [
"gnu_abi_tag",
"gnu_hwcap",
"gnu_build_id",
"gnu_gold_version",
"gnu_property",
"core_prpsinfo",
"core_file",
"core_siginfo",
"core_auxv",
"android_ident",
"fdo_packaging_metadata",
"fdo_dlopen_metadata",
"unknown",
] as const;
export type ElfNoteTypeName = (typeof ELF_NOTE_TYPE_NAMES)[number];
export const ELF_OBJECT_TYPE_NAMES = [
"relocatable",
"executable",
"shared",
"core",
"unknown",
] as const;
export type ElfObjectTypeName = (typeof ELF_OBJECT_TYPE_NAMES)[number];
/** Map e_type without inventing names for unknown object kinds. */
export const elfObjectTypeName = (type: number): ElfObjectTypeName => {
switch (type) {
case 1:
return "relocatable";
case 2:
return "executable";
case 3:
return "shared";
case 4:
return "core";
default:
return "unknown";
}
};
/** Map GNU n_type when the owner is GNU; other owners stay unknown. */
const gnuNoteTypeName = (type: number): ElfNoteTypeName => {
switch (type) {
case 1:
return "gnu_abi_tag";
case 2:
return "gnu_hwcap";
case 3:
return "gnu_build_id";
case 4:
return "gnu_gold_version";
case 5:
return "gnu_property";
default:
return "unknown";
}
};
/** Map CORE n_type when the owner is CORE; other types stay unknown. */
const coreNoteTypeName = (type: number): ElfNoteTypeName => {
switch (type) {
case 3:
return "core_prpsinfo";
case 6:
return "core_auxv";
case 0x46494c45:
return "core_file";
case 0x53494749:
return "core_siginfo";
default:
return "unknown";
}
};
/** UAPI package metadata for executable files (`.note.package`). */
export const FDO_PACKAGING_METADATA = 0xcafe1a7e;
/** UAPI ELF dlopen metadata (`.note.dlopen`). */
export const FDO_DLOPEN_METADATA = 0x407c0c0a;
/** Interpret n_type in its owner namespace and eligible ABI profile. */
export const elfNoteTypeName = (
owner: string,
type: number,
linuxCore: boolean,
): ElfNoteTypeName => {
switch (owner) {
case "GNU":
return gnuNoteTypeName(type);
case "CORE":
return linuxCore ? coreNoteTypeName(type) : "unknown";
case "Android":
return type === 1 ? "android_ident" : "unknown";
case "FDO":
if (type === FDO_PACKAGING_METADATA) return "fdo_packaging_metadata";
if (type === FDO_DLOPEN_METADATA) return "fdo_dlopen_metadata";
return "unknown";
default:
return "unknown";
}
};
/** Complete bounded inspection of ELF PT_NOTE and SHT_NOTE tables. */
export const inspectElfNotesInputSchema = z.strictObject({
path: absoluteLocalPathStringSchema.describe(
"Absolute filesystem path to an ELF image",
),
max_file_bytes: z
.number()
.int()
.min(ELF_NOTES_READ_BUDGETS.max_file_bytes.minimum)
.max(ELF_NOTES_READ_BUDGETS.max_file_bytes.maximum)
.default(ELF_NOTES_READ_BUDGETS.max_file_bytes.default_value)
.describe("Maximum stable artifact bytes to read (default 64 MiB)"),
max_entries: z
.number()
.int()
.min(ELF_NOTES_READ_BUDGETS.max_entries.minimum)
.max(ELF_NOTES_READ_BUDGETS.max_entries.maximum)
.default(ELF_NOTES_READ_BUDGETS.max_entries.default_value)
.describe(
"Maximum note entries to examine across every table; exceeding the budget fails without partial success",
),
});
const gnuAbiTagSchema = z.strictObject({
os: uint,
os_name: z.string().nullable(),
major: uint,
minor: uint,
subminor: uint,
});
const coreFileMappingSchema = z.strictObject({
start_address: z.string().regex(/^0x[0-9a-f]+$/),
end_address: z.string().regex(/^0x[0-9a-f]+$/),
file_offset_pages: z.string().regex(/^0x[0-9a-f]+$/),
path: z.string(),
path_hex: z.string().regex(/^[0-9a-f]*$/),
});
const coreFileSchema = z.strictObject({
count: uint,
page_size: uint,
mappings: z.array(coreFileMappingSchema),
});
const corePrpsinfoSchema = z.strictObject({
state: uint,
pid: z.number().int(),
ppid: z.number().int(),
pgrp: z.number().int(),
sid: z.number().int(),
uid: uint,
gid: uint,
filename: z.string(),
psargs: z.string(),
});
const coreSiginfoSchema = z.strictObject({
signo: z.number().int(),
code: z.number().int(),
errno: z.number().int(),
});
const coreAuxvEntrySchema = z.strictObject({
type: uint,
type_name: z.string().nullable(),
value: z.string().regex(/^0x[0-9a-f]+$/),
});
export const coreNoteSchema = z.strictObject({
prpsinfo: corePrpsinfoSchema.nullable(),
file: coreFileSchema.nullable(),
siginfo: coreSiginfoSchema.nullable(),
auxv: z.array(coreAuxvEntrySchema).nullable(),
});
const androidIdentSchema = z.strictObject({
android_api: uint,
ndk_version: z.string().nullable(),
ndk_build_number: z.string().nullable(),
});
const fdoNoteSchema = z.union([
z.strictObject({
metadata: z.union([jsonObjectSchema, z.array(jsonObjectSchema)]),
issue: z.null(),
}),
z.strictObject({ metadata: z.null(), issue: z.string() }),
]);
export type FdoNote = z.infer<typeof fdoNoteSchema>;
const noteEntrySchema = z.strictObject({
index: uint,
table_kind: z.enum(["program", "section"]),
table_index: uint,
location: range,
namesz: uint,
descsz: uint,
type: uint,
type_name: z.enum(ELF_NOTE_TYPE_NAMES),
owner: z.string(),
owner_bytes_hex: z.string().regex(/^[0-9a-f]*$/),
descriptor: z.strictObject({
location: range,
bytes: uint,
sha256: digestSchema,
hex: z
.string()
.regex(/^[0-9a-f]*$/)
.nullable(),
}),
gnu: z
.strictObject({
build_id_hex: z
.string()
.regex(/^[0-9a-f]*$/)
.nullable(),
abi_tag: gnuAbiTagSchema.nullable(),
gold_version: z.string().nullable(),
})
.nullable(),
core: coreNoteSchema.nullable(),
android: androidIdentSchema.nullable(),
fdo: fdoNoteSchema.nullable(),
});
/** Observed ELF note tables; absence is an empty list, not proof notes never existed. */
export const elfNotesSchema = z.strictObject({
artifact: z.strictObject({
path: z.string().min(1),
sha256: digestSchema,
bytes: uint,
}),
format: z.enum(["elf32", "elf64"]),
byte_order: z.enum(["little", "big"]),
machine: uint,
type: uint,
type_name: z.enum(ELF_OBJECT_TYPE_NAMES),
tables: z.array(
z.strictObject({
kind: z.enum(["program", "section"]),
index: uint,
name: z.string().nullable(),
location: range,
alignment: uint,
}),
),
entries: z.array(noteEntrySchema),
coverage: z.strictObject({
status: z.literal("complete"),
examined_tables: uint,
examined_entries: uint,
}),
limitations: z.array(z.string()),
});
export type ElfNotes = z.infer<typeof elfNotesSchema>;
export type InspectElfNotesInput = z.infer<typeof inspectElfNotesInputSchema>;