Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ futures = "0.3"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
toml = "1.1"
toml_edit = "0.25"

# Error handling
anyhow = "1"
Expand Down
8 changes: 8 additions & 0 deletions docs/ENV-VARS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ environment override for this one.
|`+APP_PORT+` / `+APP_HOST+` |host:port |`+[::]` / `+8081+`
|`+container/entrypoint.sh+` |Read by the container entrypoint and passed to
`+echidna server+` as flags. The binary itself does not read them.

|`+ECHIDNA_SOLVER_MANIFEST+` |path |`+config/solver-manifest.toml+`
(relative to the working directory) |`+src/rust/integrity/solver_integrity.rs+`
(`+default_manifest_path+`), `+echidna integrity pin+` and
`+echidna integrity check+` |The solver binary manifest. An empty value
means the default. `+echidna integrity pin+` replaces placeholder hashes
with the SHAKE3-512 of the binaries found now; it refuses to change a real
hash that differs unless given `+--force+`.
|===

=== Training & evaluation (Julia)
Expand Down
34 changes: 34 additions & 0 deletions src/rust/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ pub(crate) enum Commands {
op: DesignOp,
},

/// Pin solver binary hashes into the manifest, or check binaries against it
Integrity {
#[command(subcommand)]
op: IntegrityOp,
},

/// Suggest tactic variants that close a failing lemma
Suggest {
/// Target in `<file>:<lemma>` form, e.g. `Foo.thy:bar`
Expand Down Expand Up @@ -436,3 +442,31 @@ pub(crate) enum DesignOp {
adopt_threshold: i64,
},
}

#[derive(Subcommand)]
pub(crate) enum IntegrityOp {
/// Replace placeholder hashes with the SHAKE3-512 of the binaries found
/// now. A real hash that differs is refused unless `--force` is given.
Pin {
/// Manifest path (default: $ECHIDNA_SOLVER_MANIFEST, else
/// config/solver-manifest.toml)
#[arg(long)]
manifest: Option<PathBuf>,
/// Pin only these solvers (manifest keys); repeatable
#[arg(long)]
solver: Vec<String>,
/// Replace a real hash that differs from the binary
#[arg(long)]
force: bool,
/// Report what would change without writing the manifest
#[arg(long)]
dry_run: bool,
},
/// Hash every listed binary and print its status. Fails on `Tampered`.
Check {
/// Manifest path (default: $ECHIDNA_SOLVER_MANIFEST, else
/// config/solver-manifest.toml)
#[arg(long)]
manifest: Option<PathBuf>,
},
}
148 changes: 148 additions & 0 deletions src/rust/cli/integrity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>

//! `echidna integrity`: pin solver binary hashes into the manifest, and
//! check the installed binaries against it.

use super::IntegrityOp;
use anyhow::{Context, Result};
use echidna::integrity::solver_integrity::{
apply_pins, default_manifest_path, IntegrityChecker, IntegrityStatus, PinOutcome,
};
use std::path::PathBuf;

/// Integrity command.
///
/// # Errors
///
/// `pin` fails when the manifest cannot be read, parsed or written, when a
/// named solver is not in the manifest or not found, when a binary cannot be
/// read, or when a real hash differs from the binary and `--force` was not
/// given. It writes nothing in the last case. `check` fails when the
/// manifest cannot be read or when any binary is `Tampered`.
pub(crate) async fn integrity_command(op: IntegrityOp) -> Result<()> {
match op {
IntegrityOp::Pin {
manifest,
solver,
force,
dry_run,
} => pin(
manifest.unwrap_or_else(default_manifest_path),
&solver,
force,
dry_run,
),
IntegrityOp::Check { manifest } => {
check(manifest.unwrap_or_else(default_manifest_path)).await
},
}
}

fn pin(path: PathBuf, solver: &[String], force: bool, dry_run: bool) -> Result<()> {
let checker = IntegrityChecker::from_manifest_file(&path)?;
let outcomes = checker.pin_entries(solver, force)?;

let mut pins = std::collections::BTreeMap::new();
let mut failures = Vec::new();
for (name, outcome) in &outcomes {
match outcome {
PinOutcome::Pinned { hash, path } => {
println!("{name}: pinned {} ({})", short(hash), path.display());
},
PinOutcome::Unchanged { path } => {
println!("{name}: unchanged ({})", path.display());
},
PinOutcome::Repinned { old, hash, path } => {
println!(
"{name}: REPINNED {} -> {} ({}, --force)",
short(old),
short(hash),
path.display()
);
},
PinOutcome::Refused {
old,
computed,
path,
} => {
println!(
"{name}: REFUSED: manifest {} but binary {} ({})",
short(old),
short(computed),
path.display()
);
failures.push(format!(
"{name}: the binary differs from its pinned hash (possible tampering); \
re-pin with --force only if the change is expected"
));
},
PinOutcome::NotFound => {
println!("{name}: not found, left as is");
if solver.contains(name) {
failures.push(format!("{name}: no binary found"));
}
},
PinOutcome::Unreadable { path, error } => {
println!("{name}: UNREADABLE ({}): {error}", path.display());
failures.push(format!("{name}: binary cannot be read"));
},
}
if let Some(hash) = outcome.new_hash() {
pins.insert(name.clone(), hash.to_string());
}
}

if !failures.is_empty() {
anyhow::bail!(
"integrity pin wrote nothing to {}:\n {}",
path.display(),
failures.join("\n ")
);
}
if pins.is_empty() {
println!("nothing to pin in {}", path.display());
return Ok(());
}
if dry_run {
println!(
"dry run: {} hash(es) not written to {}",
pins.len(),
path.display()
);
return Ok(());
}
let text = std::fs::read_to_string(&path)
.with_context(|| format!("Failed to read solver manifest: {}", path.display()))?;
let updated = apply_pins(&text, &pins)?;
std::fs::write(&path, updated)
.with_context(|| format!("Failed to write solver manifest: {}", path.display()))?;
println!("wrote {} hash(es) to {}", pins.len(), path.display());
Ok(())
}

async fn check(path: PathBuf) -> Result<()> {
let checker = IntegrityChecker::from_manifest_file(&path)?;
let mut reports = checker.verify_all().await?;
reports.sort_by(|a, b| a.name.cmp(&b.name));
let mut tampered = Vec::new();
for r in &reports {
let at = r
.actual_path
.as_ref()
.map(|p| format!(" ({})", p.display()))
.unwrap_or_default();
println!("{}: {:?}{at}", r.name, r.status);
if r.status == IntegrityStatus::Tampered {
tampered.push(r.name.clone());
}
}
if !tampered.is_empty() {
anyhow::bail!("tampered solver binaries: {}", tampered.join(", "));
}
Ok(())
}

fn short(hash: &str) -> &str {
hash.get(..16).unwrap_or(hash)
}
2 changes: 2 additions & 0 deletions src/rust/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
mod args;
mod corpus;
mod design;
mod integrity;
mod lexicon;
mod prove;
mod provers;
Expand All @@ -19,6 +20,7 @@ mod verify;
pub(crate) use args::*;
pub(crate) use corpus::*;
pub(crate) use design::*;
pub(crate) use integrity::*;
pub(crate) use lexicon::*;
pub(crate) use prove::*;
pub(crate) use provers::*;
Expand Down
Loading
Loading