Files
frame/tools/frame_tools/check_plugin_policy.py
Lainandlain e71842aa8a feat(poc-a): gate-a dynamic elf proof of concept (#13)
## Summary

PoC-A proved that dynamically loaded ELF plugins are workable on
ESP32-S3: plugins ship as signed MPB packages, are parsed by a strict
single-type parser shared between host and target, and execute from
PSRAM+IRAM within a measured admission budget. A 1000-cycle load/unload
soak passed 18/18 trend gates with stable heaps, the capacity ladder
settled at 8 coexisting plugin instances with a 3356B per-instance
budget, and the fail-closed corpus executed zero attacker-controlled
bytes across 14 rejection classes. Scheduler semantics (strand FIFO
ordering, completion-cancel CAS, quiesce-as-noop ABI) were validated on
both host (TSan suite) and target. **Gate-A verdict: PASS** — ADR-0002
moves to **Accepted** with the capacity value 8 and global-sum admission
rule as PoC-derived proposals.

## Changes by area

Grouped from `git log main..HEAD --oneline` (18 commits):

- **Vendored loader + patches**: `4bb53f2` vendor elf_loader 1.3.3 +
harness scaffold; `2f67b4f` plugin_iram loader patch with budget
consistency (p1-p5 patch series in `PATCHES.md`).
- **MPB toolchain**: `4793cc2` deterministic MPB manifest builder with
test signing; `408df44` strict single-type MPB parser shared
host/target; `796f9d2` host MPB corpus + policy CI lane.
- **Plugin pipeline + probes**: `21d2842` plugin pipeline and first
PSRAM execution on target; `cab9664` relocation/import allowlist matrix
with probes; `c5d6c6c` C++ feature rejection matrix.
- **Harness commands**: `e0e7159` generation coexistence with measured
admission budget; `2a37556` thousand-cycle lifecycle soak with heap
trend gate; `093a72b` on-target scheduler semantics validation (+
`eea3876` clang-format-18 sweep); `a302151` capacity ladder with signed
budget; `f34dc72` fail-closed corpus with execution sentinel.
- **Host prototypes**: `6198bca` strand FIFO prototype with TSan suite;
`1c3ad59` completion-cancel CAS and cleanup semantics.
- **Evidence + ADR/docs**: `887d0f7` Gate-A manifest and report;
`1c29e8a` ADR-0002 outcome + frozen PoC values.

## QA evidence

- `poc/evidence/poc-a-20260901/manifest.json` — 67-artifact hash index,
every sha256 recomputed at assembly.
- `poc/evidence/poc-a-20260901/GATE-A-REPORT.md` — full nine-item
development-plan §4 mapping; TEST-003 10/10 clauses.
- 14 verbatim per-task transcripts (`task-1` … `task-15-*.log`) + fresh
build artifacts (`build/frame_poc_a.map`, size components).
- Frozen in-repo references: `ALLOWLIST.md`, `REJECT-MATRIX.md`,
`ADMISSION-BUDGET.md`, `soak_samples_1000.csv`, fc corpus (33 committed
`.mpb` containers).
- F1-F4 final-wave verdicts: **APPROVE**.
- Key numbers: soak **18/18** trend gates over 1000 cycles; capacity
**8/8** instances, **3356B** per-instance; fc corpus **14 rejection
classes, zero attacker bytes executed**; **4 upstream loader defects
found and patched** (p1-p5).

## Requirement IDs (GOV-006)

ADR-0002; TEST-002, TEST-003; PLUG-001, PLUG-004, PLUG-008, PLUG-009;
MEM-001, MEM-003, MEM-007, MEM-008; LIFE-001, LIFE-002, LIFE-005,
LIFE-007; SEC-002, SEC-003, SEC-004, SEC-005; GH-003; GOV-004, GOV-005.

## Risks / known limitations

From `GATE-A-REPORT.md` §(f) plus the trade-off table:

1. **Capacity descent path 7-5 unexercised** — rung 8 passed first
attempt; rungs 7/6/5 and retry-once logic never ran on hardware
(untested against real OOM).
2. **No Wi-Fi in the harness (MEM-001 caveat)** — all PSRAM/IRAM budget
numbers are radio-off harness-class; product admission budgets must be
re-measured with the network stack active.
3. **On-target scheduler vectors are a subset of the host suite**
(256-node pool / 2e4 storm posts vs host 31000 / 1e5); consistency
declaration covers only the vectors actually run.
4. **Quiesce is a no-op by ABI semantics** — plugin ABI 1.2 has no
quiesce slot; NULL entry is defined as trivial success; real quiesce
coordination is later-gate work.
5. **MEMPROT=n trade-off** — `CONFIG_ESP_SYSTEM_MEMPROT=n` is required
for the IDF exec heap (`MALLOC_CAP_EXEC`) that `.plugin_iram` needs;
software-only PMP config, no eFuse burned (documented in `PATCHES.md`
p5); production must re-decide security vs plugin IRAM.
6. **Owner sign-off PENDING** — Gate-A PASS is the PoC team's verdict;
formal acceptance of ADR-0002 awaits the owner.

## Not in scope

- Product components (GOV-004) — this PR is PoC evidence only; no
product code paths change.
- **Merge is the owner's call** — do not merge without owner sign-off on
ADR-0002.

---------

Co-authored-by: ania-lain <[email protected]>
2026-09-07 20:02:18 +08:00

319 lines
11 KiB
Python

"""Static plugin-ELF policy gate for PoC-A (GH-003, appendix C 11.3 subset).
Host-side re-implementation of the build-time gates in
``poc/apps/poc_a/plugins/check_plugin_elf.cmake`` and loader patch p4, so the
policy stays enforced (and testable) without the xtensa toolchain:
* shape - ELF32, little-endian, Xtensa, ET_DYN (gate 3 subset);
* reloc - every dynamic relocation type must be one of the five types the
vendored elf_loader implements on xtensa (gate 1 / patch p1; this also
rejects every ``R_XTENSA_TLS_*`` type, so the ``neg_tls_nosect`` shape - TLS
relocations whose sections were renamed away - is still caught);
* section - no C++ runtime feature section (gate 4 / patch p4; the stem list
is pinned to a single source by ``tests/test_check_plugin_policy.py``,
which asserts equality with both the cmake gate and PATCHES.md);
* import - closed world: every undefined global must be declared (gate 2);
* export - exactly one exported query entry symbol, a GLOBAL FUNC;
* allocator - no direct malloc/free/operator-new/delete references.
Usage:
uv run python -m frame_tools.check_plugin_policy \
--allow-import poca_host_add --allow-import poca_host_sentinel \
plugin.elf [more.elf ...]
uv run python -m frame_tools.check_plugin_policy \
--allow-imports-file ALLOWLIST.txt plugin.elf
Exit status is 0 when every ELF satisfies the policy, 1 otherwise.
"""
from __future__ import annotations
import argparse
import re
import struct
import sys
from collections.abc import Sequence
from dataclasses import dataclass
from pathlib import Path
ELFCLASS32 = 1
ELFDATA2LSB = 1
ET_DYN = 3
EM_XTENSA = 94
SHT_SYMTAB = 2
SHT_RELA = 4
SHT_REL = 9
SHN_UNDEF = 0
SHN_LORESERVE = 0xFF00
STB_GLOBAL = 1
STT_FUNC = 2
DEFAULT_ENTRY = "frame_plugin_entry"
ALLOWED_RELOC_TYPES: dict[int, str] = {
0: "R_XTENSA_NONE",
2: "R_XTENSA_RTLD",
3: "R_XTENSA_GLOB_DAT",
4: "R_XTENSA_JMP_SLOT",
5: "R_XTENSA_RELATIVE",
}
RELOC_TYPE_NAMES: dict[int, str] = {
**ALLOWED_RELOC_TYPES,
1: "R_XTENSA_32",
6: "R_XTENSA_PLT",
8: "R_XTENSA_OP0",
9: "R_XTENSA_OP1",
10: "R_XTENSA_OP2",
11: "R_XTENSA_ASM_EXPAND",
12: "R_XTENSA_ASM_SIMPLIFY",
15: "R_XTENSA_GNU_VTINHERIT",
16: "R_XTENSA_GNU_VTENTRY",
50: "R_XTENSA_TLSDESC_ARG",
51: "R_XTENSA_TLSDESC_FN",
**{20 + slot: f"R_XTENSA_SLOT{slot}_OP" for slot in range(15)},
}
FORBIDDEN_SECTION_STEMS = (
"init_array",
"fini_array",
"preinit_array",
"ctors",
"dtors",
"tdata",
"tbss",
"eh_frame",
"eh_frame_hdr",
"gcc_except_table",
)
ALLOCATOR_IMPORTS = frozenset(
{"malloc", "free", "calloc", "realloc", "posix_memalign", "memalign", "aligned_alloc"}
)
OPERATOR_ALLOC_PREFIXES = ("_Zna", "_Zda", "_Zdl")
_REL_WRAPPER = re.compile(r"^\.(rela?)?\.")
def section_stem(name: str) -> str:
"""Normalize a section name to its stem, mirroring cmake gate 4.
Strips an optional ``.rel``/``.rela`` wrapper and the leading dot, then
keeps everything up to the next dot, so ``.rela.init_array.00001``
normalizes to ``init_array`` exactly like the build-time gate.
"""
stripped = _REL_WRAPPER.sub("", name, count=1).lstrip(".")
return stripped.split(".", 1)[0] if stripped else name
@dataclass(frozen=True)
class Section:
name: str
sh_type: int
offset: int
size: int
link: int
entsize: int
@dataclass(frozen=True)
class Symbol:
name: str
bind: int
sym_type: int
shndx: int
class PolicyError(ValueError):
"""The file is not a parsable plugin ELF; the policy fails closed."""
def _string_at(table: bytes, offset: int) -> str:
end = table.find(b"\x00", offset)
if end < 0:
end = len(table)
return table[offset:end].decode("utf-8", errors="replace")
def parse_elf32(data: bytes) -> tuple[list[Section], list[Symbol]]:
"""Parse the section and symbol tables of an ELF32 LE Xtensa ET_DYN image."""
if len(data) < 52:
raise PolicyError("truncated ELF header")
(
ident,
e_type,
e_machine,
_e_version,
_e_entry,
_e_phoff,
e_shoff,
_e_flags,
_e_ehsize,
_e_phentsize,
_e_phnum,
e_shentsize,
e_shnum,
e_shstrndx,
) = struct.unpack_from("<16sHHIIIIIHHHHHH", data, 0)
if ident[:4] != b"\x7fELF":
raise PolicyError("bad ELF magic")
if ident[4] != ELFCLASS32:
raise PolicyError("expected ELF32 image")
if ident[5] != ELFDATA2LSB:
raise PolicyError("expected little-endian image")
if e_type != ET_DYN:
raise PolicyError(f"expected ET_DYN shared image, got e_type {e_type}")
if e_machine != EM_XTENSA:
raise PolicyError(f"expected Xtensa machine, got e_machine {e_machine}")
if e_shoff == 0 or e_shnum == 0 or e_shentsize != 40:
raise PolicyError("missing or malformed section header table")
if e_shstrndx >= e_shnum:
raise PolicyError("section name string table index out of range")
raw_shdrs: list[tuple[int, ...]] = []
for index in range(e_shnum):
start = e_shoff + index * e_shentsize
if start + 40 > len(data):
raise PolicyError("truncated section header table")
raw_shdrs.append(struct.unpack_from("<10I", data, start))
shstr = raw_shdrs[e_shstrndx]
if shstr[4] + shstr[5] > len(data):
raise PolicyError("truncated section name string table")
shstrtab = data[shstr[4] : shstr[4] + shstr[5]]
sections = [
Section(_string_at(shstrtab, sh[0]), sh[1], sh[4], sh[5], sh[6], sh[9]) for sh in raw_shdrs
]
symbols: list[Symbol] = []
for section in sections:
if section.sh_type != SHT_SYMTAB:
continue
if section.link >= len(sections):
raise PolicyError("symbol table link out of range")
strtab_section = sections[section.link]
if strtab_section.offset + strtab_section.size > len(data):
raise PolicyError("truncated symbol string table")
strtab = data[strtab_section.offset : strtab_section.offset + strtab_section.size]
entsize = section.entsize or 16
if entsize != 16 or section.offset + section.size > len(data):
raise PolicyError("malformed symbol table")
for offset in range(0, section.size - entsize + 1, entsize):
name_off, _, _, info, _, shndx = struct.unpack_from(
"<IIIBBH", data, section.offset + offset
)
if offset == 0:
continue
symbols.append(Symbol(_string_at(strtab, name_off), info >> 4, info & 0xF, shndx))
return sections, symbols
def _reloc_type_names(data: bytes, sections: Sequence[Section]) -> dict[str, int]:
"""Count dynamic relocation types from every SHT_REL/SHT_RELA section."""
counts: dict[str, int] = {}
for section in sections:
if section.sh_type not in (SHT_REL, SHT_RELA):
continue
entsize = section.entsize or (8 if section.sh_type == SHT_REL else 12)
if entsize not in (8, 12) or section.offset + section.size > len(data):
raise PolicyError(f"malformed relocation section {section.name!r}")
for offset in range(0, section.size - entsize + 1, entsize):
info = struct.unpack_from("<I", data, section.offset + offset + 4)[0]
name = RELOC_TYPE_NAMES.get(info >> 8, f"R_XTENSA_{info >> 8}")
counts[name] = counts.get(name, 0) + 1
return counts
def _evaluate(data: bytes, entry: str, allowed_imports: frozenset[str]) -> tuple[list[str], str]:
violations: list[str] = []
try:
sections, symbols = parse_elf32(data)
reloc_counts = _reloc_type_names(data, sections)
except PolicyError as error:
return [f"shape: {error}"], "unparsable"
for name, count in sorted(reloc_counts.items()):
if name not in ALLOWED_RELOC_TYPES.values():
violations.append(f"reloc: forbidden relocation type {name} x{count}")
for section in sections:
if section_stem(section.name) in FORBIDDEN_SECTION_STEMS:
violations.append(f"section: forbidden C++ feature section {section.name!r}")
reloc_summary = (
"relocs("
+ " ".join(f"{name}={count}" for name, count in sorted(reloc_counts.items()))
+ ")"
)
if not any(s.sh_type == SHT_SYMTAB for s in sections):
violations.append("shape: no symbol table (entry lookup impossible)")
else:
defined = [
s for s in symbols if s.bind == STB_GLOBAL and SHN_UNDEF < s.shndx < SHN_LORESERVE
]
exports = sorted(s.name for s in defined)
if exports != [entry]:
violations.append(f"export: exported globals must be exactly [{entry}], got {exports}")
elif defined[0].sym_type != STT_FUNC:
violations.append(f"export: entry {entry} must be a FUNC symbol")
undefined = [s.name for s in symbols if s.bind == STB_GLOBAL and s.shndx == SHN_UNDEF]
for name in undefined:
if name.startswith(OPERATOR_ALLOC_PREFIXES) or name in ALLOCATOR_IMPORTS:
violations.append(f"allocator: direct allocation import {name!r}")
elif name not in allowed_imports:
violations.append(f"import: undeclared undefined symbol {name!r}")
reloc_summary += f" exports={exports} imports={len(undefined)}"
return violations, reloc_summary
def check_policy(
data: bytes, entry: str = DEFAULT_ENTRY, allowed_imports: frozenset[str] = frozenset()
) -> list[str]:
"""Return the policy violations for one plugin ELF (empty list = pass)."""
return _evaluate(data, entry, allowed_imports)[0]
def _load_imports_file(path: str) -> list[str]:
names: list[str] = []
for line in Path(path).read_text(encoding="utf-8").splitlines():
stripped = line.split("#", 1)[0].strip()
if stripped:
names.append(stripped)
return names
def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser(
prog="check-plugin-policy",
description="Enforce the appendix C 11.3 plugin policy subset on ELF32 "
"Xtensa plugin images (see module docstring).",
)
parser.add_argument("--entry", default=DEFAULT_ENTRY, help="the single exported query entry")
parser.add_argument(
"--allow-import", action="append", default=[], help="declared import symbol (repeatable)"
)
parser.add_argument(
"--allow-imports-file",
help="file with one declared import symbol per line (# comments allowed)",
)
parser.add_argument("elfs", nargs="+", help="plugin ELF files to check")
args = parser.parse_args(argv)
declared = list(args.allow_import)
if args.allow_imports_file:
declared += _load_imports_file(args.allow_imports_file)
allowed = frozenset(declared)
failed = False
for elf in args.elfs:
violations = check_policy(Path(elf).read_bytes(), args.entry, allowed)
if violations:
failed = True
for violation in violations:
print(f"check-plugin-policy: {elf}: {violation}")
else:
print(f"check-plugin-policy: {elf}: policy OK (entry={args.entry})")
if failed:
print("check-plugin-policy: FAILED", file=sys.stderr)
return 1
return 0
if __name__ == "__main__":
raise SystemExit(main())