| // Copyright 2026 The Fuchsia Authors | |
| // | |
| // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| // This file may not be copied, modified, or distributed except according to | |
| // those terms. | |
| #![forbid(unsafe_code)] | |
| use std::{ | |
| path::{Path, PathBuf}, | |
| process::ExitCode, | |
| }; | |
| use anneal_postlink::{ | |
| audit_files, format_human_report, AuditConfig, Manifest, ManifestSymbol, SCHEMA_VERSION, | |
| }; | |
| const USAGE: &str = "\ | |
| usage: anneal-postlink --linked ELF --object OBJ.o [--object OBJ2.o ...] | |
| (--manifest MANIFEST.json | --symbol NAME ...) | |
| [--allow-missing-gnu-stack] [--require-relro] | |
| [--require-cet-ibt] [--require-bti] [--require-pac] | |
| [--max-stack-bytes N] [--json] | |
| Audits a linked ELF64 binary (x86-64 or AArch64) against verified pre-link | |
| object files, checking exact .text/.rodata byte fidelity modulo allowlisted | |
| relocations, --icf aliasing, W^X, PT_GNU_STACK, PT_GNU_RELRO, optional | |
| .note.gnu.property CET/BTI/PAC, .stack_sizes, and forbidden syscall opcodes. | |
| Exit status: 0 on PASS, 1 on usage/IO/parse errors, 2 on audit FAIL."; | |
| struct CliArgs { | |
| linked: PathBuf, | |
| objects: Vec<PathBuf>, | |
| manifest_path: Option<PathBuf>, | |
| cli_symbols: Vec<String>, | |
| config: AuditConfig, | |
| emit_json: bool, | |
| } | |
| enum ParseOutcome { | |
| Help, | |
| Run(CliArgs), | |
| } | |
| fn parse_cli_args<I: IntoIterator<Item = String>>(iter: I) -> Result<ParseOutcome, String> { | |
| let mut linked: Option<PathBuf> = None; | |
| let mut objects: Vec<PathBuf> = Vec::new(); | |
| let mut manifest_path: Option<PathBuf> = None; | |
| let mut cli_symbols: Vec<String> = Vec::new(); | |
| let mut config = AuditConfig::default(); | |
| let mut emit_json = false; | |
| let mut args = iter.into_iter(); | |
| while let Some(arg) = args.next() { | |
| let mut next_val = |flag: &str| match args.next() { | |
| Some(v) => Ok(v), | |
| None => Err(format!("missing value for {flag}")), | |
| }; | |
| match arg.as_str() { | |
| "--linked" => { | |
| if linked.is_some() { | |
| return Err("duplicate `--linked` argument".to_owned()); | |
| } | |
| linked = Some(PathBuf::from(next_val("--linked")?)); | |
| } | |
| "--object" | "-o" => objects.push(PathBuf::from(next_val("--object")?)), | |
| "--manifest" | "-m" => { | |
| if manifest_path.is_some() { | |
| return Err("duplicate `--manifest` argument".to_owned()); | |
| } | |
| manifest_path = Some(PathBuf::from(next_val("--manifest")?)); | |
| } | |
| "--symbol" | "-s" => cli_symbols.push(next_val("--symbol")?), | |
| "--allow-missing-gnu-stack" => config.allow_missing_gnu_stack = true, | |
| "--require-relro" => config.require_relro = true, | |
| "--require-cet-ibt" => config.require_cet_ibt = true, | |
| "--require-bti" => config.require_bti = true, | |
| "--require-pac" => config.require_pac = true, | |
| "--max-stack-bytes" => { | |
| if config.max_stack_bytes.is_some() { | |
| return Err("duplicate `--max-stack-bytes` argument".to_owned()); | |
| } | |
| let v = next_val("--max-stack-bytes")?; | |
| let n = v | |
| .parse::<u64>() | |
| .map_err(|_| format!("invalid integer for --max-stack-bytes: `{v}`"))?; | |
| config.max_stack_bytes = Some(n); | |
| } | |
| "--json" => emit_json = true, | |
| "-h" | "--help" => return Ok(ParseOutcome::Help), | |
| _ => return Err(format!("unexpected argument `{arg}`")), | |
| } | |
| } | |
| let linked = linked.ok_or("missing required --linked ELF")?; | |
| if objects.is_empty() { | |
| return Err("at least one --object OBJ.o is required".to_owned()); | |
| } | |
| if manifest_path.is_some() && !cli_symbols.is_empty() { | |
| return Err("pass either --manifest or --symbol, not both".to_owned()); | |
| } | |
| if manifest_path.is_none() && cli_symbols.is_empty() { | |
| return Err("pass --manifest or at least one --symbol".to_owned()); | |
| } | |
| Ok(ParseOutcome::Run(CliArgs { | |
| linked, | |
| objects, | |
| manifest_path, | |
| cli_symbols, | |
| config, | |
| emit_json, | |
| })) | |
| } | |
| fn main() -> ExitCode { | |
| let cli = match parse_cli_args(std::env::args().skip(1)) { | |
| Ok(ParseOutcome::Help) => { | |
| println!("{USAGE}"); | |
| return ExitCode::SUCCESS; | |
| } | |
| Ok(ParseOutcome::Run(args)) => args, | |
| Err(e) => { | |
| eprintln!("anneal-postlink: {e}\n\n{USAGE}"); | |
| return ExitCode::from(1); | |
| } | |
| }; | |
| let manifest = if let Some(path) = cli.manifest_path { | |
| let text = match std::fs::read_to_string(&path) { | |
| Ok(t) => t, | |
| Err(e) => { | |
| eprintln!("anneal-postlink: cannot read manifest `{}`: {e}", path.display()); | |
| return ExitCode::from(1); | |
| } | |
| }; | |
| match serde_json::from_str::<Manifest>(&text) { | |
| Ok(m) => m, | |
| Err(e) => { | |
| eprintln!("anneal-postlink: invalid manifest `{}`: {e}", path.display()); | |
| return ExitCode::from(1); | |
| } | |
| } | |
| } else { | |
| Manifest { | |
| schema_version: SCHEMA_VERSION, | |
| symbols: cli | |
| .cli_symbols | |
| .into_iter() | |
| .map(|name| ManifestSymbol { name, object: None, allow_syscalls: false }) | |
| .collect(), | |
| } | |
| }; | |
| let obj_refs: Vec<&Path> = cli.objects.iter().map(PathBuf::as_path).collect(); | |
| let report = match audit_files(&cli.linked, &obj_refs, &manifest, &cli.config) { | |
| Ok(r) => r, | |
| Err(e) => { | |
| eprintln!("anneal-postlink: error: {e}"); | |
| return ExitCode::from(1); | |
| } | |
| }; | |
| if cli.emit_json { | |
| match serde_json::to_string_pretty(&report) { | |
| Ok(json) => println!("{json}"), | |
| Err(e) => { | |
| eprintln!("anneal-postlink: failed to serialize report: {e}"); | |
| return ExitCode::from(1); | |
| } | |
| } | |
| } else { | |
| print!("{}", format_human_report(&report)); | |
| } | |
| if report.passed() { | |
| ExitCode::SUCCESS | |
| } else { | |
| ExitCode::from(2) | |
| } | |
| } | |
| #[cfg(test)] | |
| mod tests { | |
| use super::*; | |
| fn parse(args: &[&str]) -> Result<ParseOutcome, String> { | |
| parse_cli_args(args.iter().map(|s| (*s).to_owned())) | |
| } | |
| #[test] | |
| fn cli_arg_parser_validates_duplicates_and_required_flags() { | |
| assert!(matches!(parse(&["--help"]), Ok(ParseOutcome::Help))); | |
| assert!(parse(&["--linked", "a.elf", "-o", "a.o", "-s", "f", "--json"]).is_ok()); | |
| assert!(parse(&["--linked", "a", "--linked", "b", "-o", "a.o", "-s", "f"]) | |
| .err() | |
| .is_some_and(|e| e.contains("duplicate `--linked`"))); | |
| assert!(parse(&["--linked", "a", "-o", "a.o", "-m", "m1", "-m", "m2"]) | |
| .err() | |
| .is_some_and(|e| e.contains("duplicate `--manifest`"))); | |
| assert!(parse(&[ | |
| "--linked", | |
| "a", | |
| "-o", | |
| "a.o", | |
| "-s", | |
| "f", | |
| "--max-stack-bytes", | |
| "8", | |
| "--max-stack-bytes", | |
| "16", | |
| ]) | |
| .err() | |
| .is_some_and(|e| e.contains("duplicate `--max-stack-bytes`"))); | |
| assert!(parse(&["--linked", "a", "-o", "a.o", "-m", "m", "-s", "f"]).is_err()); | |
| assert!(parse(&["--linked", "a", "-o", "a.o"]).is_err()); | |
| } | |
| } |