Qualify atomic deny-only helper abandonment on actual Linux VM
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -52,6 +52,40 @@ impl Binding {
|
||||
request: &Request,
|
||||
now: DateTime<Utc>,
|
||||
verifier: &impl Ed25519Verifier,
|
||||
) -> Result<(JobPayload, ExecutionPermitPayload)> {
|
||||
let (job, p) = self.verify_signed_request(request, verifier)?;
|
||||
ensure!(
|
||||
permit_window(&job, &p) && now >= p.not_before && now < p.start_before,
|
||||
"PERMIT_TIME"
|
||||
);
|
||||
Ok((job, p))
|
||||
}
|
||||
/// Evidence-only verification. This never supplies a current start window
|
||||
/// and cannot be substituted for verify_with at an execution boundary.
|
||||
pub fn verify_historical_for_denial_with(
|
||||
&self,
|
||||
request: &Request,
|
||||
identity: HelperIdentity,
|
||||
verifier: &impl Ed25519Verifier,
|
||||
) -> Result<VerifiedHistoricalDenial> {
|
||||
let (job, permit) = self.verify_signed_request(request, verifier)?;
|
||||
ensure!(permit_window(&job, &permit), "PERMIT_TIME");
|
||||
ensure!(
|
||||
job.execution_identity == identity.as_str()
|
||||
&& matches!(&job.action, rmm_domain::jobs::Action::SetServiceState { state, .. }
|
||||
if matches!(state.as_str(), "running" | "stopped")),
|
||||
"DENIAL_HELPER_SCOPE"
|
||||
);
|
||||
Ok(VerifiedHistoricalDenial {
|
||||
request: request.clone(),
|
||||
permit,
|
||||
request_sha256: hash(&serde_json::to_vec(request)?),
|
||||
})
|
||||
}
|
||||
fn verify_signed_request(
|
||||
&self,
|
||||
request: &Request,
|
||||
verifier: &impl Ed25519Verifier,
|
||||
) -> Result<(JobPayload, ExecutionPermitPayload)> {
|
||||
let job = self.job(&request.job_bytes)?;
|
||||
ensure!(
|
||||
@@ -91,19 +125,25 @@ impl Binding {
|
||||
&& p.max_runtime_seconds == job.max_runtime_seconds,
|
||||
"PERMIT_BINDING"
|
||||
);
|
||||
ensure!(
|
||||
p.not_before >= job.not_before
|
||||
&& p.start_before <= job.start_before
|
||||
&& p.not_before < p.start_before
|
||||
&& (p.start_before - p.not_before).num_seconds() <= 60
|
||||
&& now >= p.not_before
|
||||
&& now < p.start_before,
|
||||
"PERMIT_TIME"
|
||||
);
|
||||
Ok((job, p))
|
||||
}
|
||||
}
|
||||
|
||||
fn permit_window(job: &JobPayload, p: &ExecutionPermitPayload) -> bool {
|
||||
p.not_before >= job.not_before
|
||||
&& p.start_before <= job.start_before
|
||||
&& p.not_before < p.start_before
|
||||
&& (p.start_before - p.not_before).num_seconds() <= 60
|
||||
}
|
||||
|
||||
/// Not serializable or constructible by an IPC caller. Only the deny-only
|
||||
/// helper path consumes this evidence; it is never an execution authorization.
|
||||
pub struct VerifiedHistoricalDenial {
|
||||
pub(crate) request: Request,
|
||||
pub(crate) permit: ExecutionPermitPayload,
|
||||
pub(crate) request_sha256: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Request {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//! an operation or grants authority from an agent-provided ready/unlock flag.
|
||||
//! Both general and APT listeners must use the SAME executor.lock and SQLite DB.
|
||||
use crate::auth::HelperIdentity;
|
||||
use crate::auth::{Binding, Request};
|
||||
use crate::auth::{Binding, Outcome, Request};
|
||||
use crate::crypto::{DefaultVerifier, Ed25519Verifier};
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use chrono::{DateTime, Utc};
|
||||
@@ -27,6 +27,72 @@ pub trait BootSource {
|
||||
pub trait ExecutorLedgerGuard {
|
||||
fn check(&self, db: &Connection) -> Result<()>;
|
||||
}
|
||||
/// OS adapter contract, not an IPC assertion. The implementation must pin the
|
||||
/// live helper PID/creation and ledger/lock file identities, and independently
|
||||
/// read the fixed protected intent store's immutable known_no_dispatch CAS.
|
||||
/// The same executor guard stays held throughout this check and the commit.
|
||||
pub trait NoStartGuard: ExecutorLedgerGuard {
|
||||
fn live_instance_sha256(&self) -> Result<String>;
|
||||
fn verify_no_dispatch(
|
||||
&self,
|
||||
original: &Request,
|
||||
anchor: &NoStartAnchor,
|
||||
) -> Result<NoDispatchObservation>;
|
||||
}
|
||||
/// Trusted adapter return value only: deliberately has no IPC deserializer.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct NoDispatchObservation {
|
||||
pub intent_id: Uuid,
|
||||
pub intent_sha256: String,
|
||||
pub intent_recorded_at: DateTime<Utc>,
|
||||
pub original_boot_id: Uuid,
|
||||
}
|
||||
/// Historical ready context, never a new lease or execution authorization.
|
||||
/// Deserialization alone conveys no authority: every field is checked against
|
||||
/// the still-held helper instance and the original independently stored history.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct NoStartAnchor {
|
||||
journal_id: Uuid,
|
||||
checkpoint: u64,
|
||||
decision_id: Uuid,
|
||||
connection_generation: u64,
|
||||
boot_id: Uuid,
|
||||
binding_sha256: String,
|
||||
execution_identity: String,
|
||||
helper_instance_sha256: String,
|
||||
helper_born_at: DateTime<Utc>,
|
||||
sync_request_sha256: String,
|
||||
signed_response_sha256: String,
|
||||
ledger_fingerprint: String,
|
||||
captured_at: DateTime<Utc>,
|
||||
}
|
||||
impl NoStartAnchor {
|
||||
pub fn journal_id(&self) -> Uuid {
|
||||
self.journal_id
|
||||
}
|
||||
pub fn checkpoint(&self) -> u64 {
|
||||
self.checkpoint
|
||||
}
|
||||
pub fn decision_id(&self) -> Uuid {
|
||||
self.decision_id
|
||||
}
|
||||
pub fn connection_generation(&self) -> u64 {
|
||||
self.connection_generation
|
||||
}
|
||||
pub fn boot_id(&self) -> Uuid {
|
||||
self.boot_id
|
||||
}
|
||||
pub fn helper_instance_sha256(&self) -> &str {
|
||||
&self.helper_instance_sha256
|
||||
}
|
||||
pub fn sync_request_sha256(&self) -> &str {
|
||||
&self.sync_request_sha256
|
||||
}
|
||||
pub fn signed_response_sha256(&self) -> &str {
|
||||
&self.signed_response_sha256
|
||||
}
|
||||
}
|
||||
/// Same two fields as public SyncResponse, without adding a new dependency or
|
||||
/// wire type. RPC glue copies the ORIGINAL bytes/signature, never reserializes.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
@@ -122,6 +188,183 @@ impl<V: Ed25519Verifier> HelperSync<V> {
|
||||
pub fn active_generation(&self) -> Option<u64> {
|
||||
self.lease.as_ref().map(|lease| lease.generation)
|
||||
}
|
||||
/// Capture only an actually confirmed live ready lease, before an intent
|
||||
/// or fence closes it. Retaining this anchor cannot keep that lease alive.
|
||||
pub fn no_start_anchor(
|
||||
&self,
|
||||
db: &Connection,
|
||||
guard: &impl NoStartGuard,
|
||||
generation: u64,
|
||||
clock: &impl Clock,
|
||||
boot: &impl BootSource,
|
||||
) -> Result<NoStartAnchor> {
|
||||
ensure!(
|
||||
self.can_start(db, guard, generation, clock, boot),
|
||||
"DENIAL_READY_ANCHOR_REQUIRED"
|
||||
);
|
||||
let lease = self
|
||||
.lease
|
||||
.as_ref()
|
||||
.context("DENIAL_READY_ANCHOR_REQUIRED")?;
|
||||
let meta = metadata(db)?;
|
||||
let (response, request): (Vec<u8>, Vec<u8>) = db.query_row(
|
||||
"SELECT response,request FROM helper_sync_decisions WHERE decision_id=?1",
|
||||
[lease.decision_id.to_string()],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)?;
|
||||
let instance = guard.live_instance_sha256()?;
|
||||
ensure!(digest(&instance), "DENIAL_HELPER_INSTANCE_INVALID");
|
||||
let capture: Vec<u8> = db.query_row(
|
||||
"SELECT snapshot FROM helper_sync_captures WHERE checkpoint=?1",
|
||||
[meta.checkpoint],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
let capture: Snapshot = serde_json::from_slice(&capture)?;
|
||||
let anchor = NoStartAnchor {
|
||||
journal_id: meta.journal_id,
|
||||
checkpoint: meta.checkpoint,
|
||||
decision_id: lease.decision_id,
|
||||
connection_generation: generation,
|
||||
boot_id: lease.boot_id,
|
||||
binding_sha256: hash(&serde_json::to_vec(&self.binding)?),
|
||||
execution_identity: self.identity.as_str().into(),
|
||||
helper_instance_sha256: instance,
|
||||
helper_born_at: self.born,
|
||||
sync_request_sha256: hash(&request),
|
||||
signed_response_sha256: hash(&response),
|
||||
ledger_fingerprint: lease.fingerprint.clone(),
|
||||
captured_at: capture.captured_at,
|
||||
};
|
||||
self.verify_no_start_anchor(db, guard, &anchor, clock, boot)?;
|
||||
Ok(anchor)
|
||||
}
|
||||
/// Deny-only: the grant may be expired, but no historical timestamp is ever
|
||||
/// passed to an execute verifier. Existing consumption is immutable. A new
|
||||
/// c/start/no-effect outcome is committed atomically and remains locked for
|
||||
/// the ordinary independently signed Sync decision.
|
||||
pub fn abandon_no_start(
|
||||
&mut self,
|
||||
db: &mut Connection,
|
||||
guard: &impl NoStartGuard,
|
||||
anchor: &NoStartAnchor,
|
||||
request: &Request,
|
||||
clock: &impl Clock,
|
||||
boot: &impl BootSource,
|
||||
) -> Result<Outcome> {
|
||||
self.close();
|
||||
guard.check(db)?;
|
||||
let original = self.binding.verify_historical_for_denial_with(
|
||||
request,
|
||||
self.identity,
|
||||
&self.verifier,
|
||||
)?;
|
||||
let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
||||
if let Some(outcome) = crate::ledger::existing_no_start(&tx, &original)? {
|
||||
// No changed row and no classification of a missing original start.
|
||||
tx.commit()?;
|
||||
return Ok(outcome);
|
||||
}
|
||||
self.verify_no_start_anchor(&tx, guard, anchor, clock, boot)?;
|
||||
let observation = guard.verify_no_dispatch(request, anchor)?;
|
||||
let now = self.verify_no_start_anchor(&tx, guard, anchor, clock, boot)?;
|
||||
ensure!(
|
||||
!observation.intent_id.is_nil()
|
||||
&& digest(&observation.intent_sha256)
|
||||
&& observation.original_boot_id == anchor.boot_id
|
||||
&& observation.intent_recorded_at >= anchor.captured_at
|
||||
&& observation.intent_recorded_at <= now,
|
||||
"DENIAL_INTENT_BINDING"
|
||||
);
|
||||
let outcome =
|
||||
crate::ledger::record_no_start_in(&tx, &original, anchor.boot_id, now, &observation)?;
|
||||
guard.check(&tx)?;
|
||||
ensure!(
|
||||
guard.live_instance_sha256()? == anchor.helper_instance_sha256,
|
||||
"DENIAL_HELPER_INSTANCE_CHANGED"
|
||||
);
|
||||
ensure!(
|
||||
guard.verify_no_dispatch(request, anchor)? == observation,
|
||||
"DENIAL_INTENT_CHANGED_DURING_COMMIT"
|
||||
);
|
||||
tx.commit()?;
|
||||
Ok(outcome)
|
||||
}
|
||||
fn verify_no_start_anchor(
|
||||
&self,
|
||||
db: &Connection,
|
||||
guard: &impl NoStartGuard,
|
||||
anchor: &NoStartAnchor,
|
||||
clock: &impl Clock,
|
||||
boot: &impl BootSource,
|
||||
) -> Result<DateTime<Utc>> {
|
||||
guard.check(db)?;
|
||||
let now = clock.now()?;
|
||||
ensure!(
|
||||
anchor.helper_born_at == self.born
|
||||
&& now >= self.born
|
||||
&& boot.current_boot()? == anchor.boot_id
|
||||
&& !anchor.boot_id.is_nil()
|
||||
&& anchor.execution_identity == self.identity.as_str()
|
||||
&& anchor.binding_sha256 == hash(&serde_json::to_vec(&self.binding)?)
|
||||
&& digest(&anchor.helper_instance_sha256)
|
||||
&& guard.live_instance_sha256()? == anchor.helper_instance_sha256,
|
||||
"DENIAL_HELPER_CONTEXT_CHANGED"
|
||||
);
|
||||
let meta = metadata(db)?;
|
||||
ensure!(
|
||||
meta.fault.is_none()
|
||||
&& meta.journal_id == anchor.journal_id
|
||||
&& meta.checkpoint == anchor.checkpoint
|
||||
&& meta.decision == Some(anchor.decision_id)
|
||||
&& meta.generation == anchor.connection_generation
|
||||
&& ledger_fingerprint(db)? == anchor.ledger_fingerprint,
|
||||
"DENIAL_HELPER_LINEAGE_CHANGED"
|
||||
);
|
||||
// Exact equality is deliberate: a first implementation refuses even
|
||||
// legitimate intervening progress rather than inventing lineage links.
|
||||
verify_checkpoint_repair_history_with(
|
||||
db,
|
||||
&self.binding,
|
||||
None,
|
||||
self.identity,
|
||||
&self.verifier,
|
||||
)?;
|
||||
let (response, raw): (Vec<u8>, Vec<u8>) = db.query_row(
|
||||
"SELECT response,request FROM helper_sync_decisions WHERE decision_id=?1",
|
||||
[anchor.decision_id.to_string()],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)?;
|
||||
ensure!(
|
||||
hash(&response) == anchor.signed_response_sha256
|
||||
&& hash(&raw) == anchor.sync_request_sha256,
|
||||
"DENIAL_READY_BYTES_CHANGED"
|
||||
);
|
||||
let signed: SignedResponse = serde_json::from_slice(&response)?;
|
||||
let parsed: ReconcileDecisionPayload = serde_json::from_slice(&signed.decision_bytes)?;
|
||||
// Historical Sync evidence only, never a lease. Its original signature
|
||||
// and immutable stored request remain mandatory after expiry.
|
||||
let decision = verify(&self.verifier, &self.binding, &signed, parsed.issued_at)?;
|
||||
let capture: Vec<u8> = db.query_row(
|
||||
"SELECT snapshot FROM helper_sync_captures WHERE checkpoint=?1",
|
||||
[anchor.checkpoint],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
let capture: Snapshot = serde_json::from_slice(&capture)?;
|
||||
ensure!(
|
||||
decision.ready
|
||||
&& decision.helper_ready
|
||||
&& decision.decision_id == anchor.decision_id
|
||||
&& decision.boot_id == anchor.boot_id
|
||||
&& decision.helper_journal_id == Some(anchor.journal_id)
|
||||
&& decision.helper_checkpoint == anchor.checkpoint
|
||||
&& decision.connection_generation == anchor.connection_generation
|
||||
&& capture.captured_at == anchor.captured_at
|
||||
&& capture.boot_id == anchor.boot_id
|
||||
&& capture.captured_at <= now,
|
||||
"DENIAL_CONFIRMED_READY_REQUIRED"
|
||||
);
|
||||
Ok(now)
|
||||
}
|
||||
/// Every call creates a NEW durable checkpoint challenge and closes the old
|
||||
/// lease. Use apply's returned snapshot for its ACK, not another snapshot().
|
||||
pub fn snapshot(
|
||||
@@ -494,6 +737,12 @@ impl<V: Ed25519Verifier> HelperSync<V> {
|
||||
}
|
||||
}
|
||||
|
||||
fn digest(value: &str) -> bool {
|
||||
value.len() == 64
|
||||
&& value
|
||||
.bytes()
|
||||
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
|
||||
}
|
||||
fn pinned_key(binding: &Binding, verifier: &impl Ed25519Verifier) -> Result<Vec<u8>> {
|
||||
let raw = hex::decode(&binding.public_key_hex)?;
|
||||
ensure!(raw.len() == 32, "PINNED_HELPER_KEY_SIZE");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//! Connection-level ledger rules, with OS path/lock/ACL handling left to adapters.
|
||||
use crate::auth::{Outcome, Request};
|
||||
use crate::auth::{Outcome, Request, VerifiedHistoricalDenial};
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use chrono::{DateTime, Utc};
|
||||
use rmm_domain::jobs::{ExecutionPermitPayload, JobPayload};
|
||||
@@ -77,6 +77,111 @@ pub fn begin_execution(
|
||||
tx.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Retained evidence only. In particular, a legacy consumption without its
|
||||
/// original start can never be repaired into a proven nonstart by this path.
|
||||
pub(crate) fn existing_no_start(
|
||||
db: &rusqlite::Connection,
|
||||
original: &VerifiedHistoricalDenial,
|
||||
) -> Result<Option<Outcome>> {
|
||||
let p = &original.permit;
|
||||
type Row = (
|
||||
String,
|
||||
String,
|
||||
u64,
|
||||
String,
|
||||
String,
|
||||
Option<Vec<u8>>,
|
||||
Option<Vec<u8>>,
|
||||
Option<Vec<u8>>,
|
||||
);
|
||||
let mut statement = db.prepare("SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) WHERE c.grant_id=?1 OR (c.agent=?2 AND c.generation=?3 AND c.job=?4 AND c.attempt=?5)")?;
|
||||
let rows = statement
|
||||
.query_map(
|
||||
params![
|
||||
p.grant_id.to_string(),
|
||||
p.agent_id.to_string(),
|
||||
p.enrollment_generation,
|
||||
p.job_id.to_string(),
|
||||
p.attempt_id.to_string()
|
||||
],
|
||||
|r| {
|
||||
Ok((
|
||||
r.get(0)?,
|
||||
r.get(1)?,
|
||||
r.get(2)?,
|
||||
r.get(3)?,
|
||||
r.get(4)?,
|
||||
r.get(5)?,
|
||||
r.get(6)?,
|
||||
r.get(7)?,
|
||||
))
|
||||
},
|
||||
)?
|
||||
.collect::<rusqlite::Result<Vec<Row>>>()?;
|
||||
ensure!(rows.len() <= 1, "DENIAL_EXISTING_ATTEMPT_CONFLICT");
|
||||
let Some((grant, agent, generation, job, attempt, finished, start, outcome)) =
|
||||
rows.into_iter().next()
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
ensure!(
|
||||
grant == p.grant_id.to_string()
|
||||
&& agent == p.agent_id.to_string()
|
||||
&& generation == p.enrollment_generation
|
||||
&& job == p.job_id.to_string()
|
||||
&& attempt == p.attempt_id.to_string(),
|
||||
"DENIAL_EXISTING_ATTEMPT_CONFLICT"
|
||||
);
|
||||
let Some(start) = start else {
|
||||
return Ok(Some(Outcome::unknown("HELPER_PREVIOUS_START_UNRESOLVED")));
|
||||
};
|
||||
let start: serde_json::Value = serde_json::from_slice(&start)?;
|
||||
let saved: Request = serde_json::from_value(start["request"].clone())?;
|
||||
ensure!(
|
||||
serde_json::to_vec(&saved)? == serde_json::to_vec(&original.request)?,
|
||||
"DENIAL_EXISTING_REQUEST_CONFLICT"
|
||||
);
|
||||
let boot: Uuid = serde_json::from_value(start["previous_boot_id"].clone())?;
|
||||
ensure!(!boot.is_nil(), "DENIAL_EXISTING_ORIGINAL_BOOT_MISSING");
|
||||
let _: DateTime<Utc> = serde_json::from_value(start["reserved_at"].clone())?;
|
||||
if let (Some(a), Some(b)) = (&finished, &outcome) {
|
||||
ensure!(
|
||||
canonical(a)? == canonical(b)?,
|
||||
"DENIAL_EXISTING_OUTCOME_CONFLICT"
|
||||
);
|
||||
}
|
||||
let Some(bytes) = outcome.or(finished) else {
|
||||
return Ok(Some(Outcome::unknown("HELPER_PREVIOUS_EFFECT_UNRESOLVED")));
|
||||
};
|
||||
Ok(Some(serde_json::from_slice(&canonical(&bytes)?)?))
|
||||
}
|
||||
|
||||
/// Called only inside HelperSync's guarded Immediate transaction after its
|
||||
/// independent intent/lineage checks. Never finish/unlock here. All three
|
||||
/// witnesses commit together, so no crash can expose a bare new consumption.
|
||||
pub(crate) fn record_no_start_in(
|
||||
db: &rusqlite::Connection,
|
||||
original: &VerifiedHistoricalDenial,
|
||||
boot: Uuid,
|
||||
observed_at: DateTime<Utc>,
|
||||
witness: &crate::helper_reconcile::NoDispatchObservation,
|
||||
) -> Result<Outcome> {
|
||||
let p = &original.permit;
|
||||
validate_start_request(p, &original.request)?;
|
||||
consume_in(db, p)?;
|
||||
save_start(db, p, &original.request, boot, observed_at)?;
|
||||
let outcome = Outcome {
|
||||
state: "failed".into(),
|
||||
evidence: serde_json::json!({"not_started":true,"postconditions_passed":false,
|
||||
"deny_only":true,"reservation_only":true,"original_request_sha256":original.request_sha256,
|
||||
"intent_id":witness.intent_id,"intent_sha256":witness.intent_sha256,
|
||||
"intent_recorded_at":witness.intent_recorded_at,"observed_at":observed_at}),
|
||||
..Outcome::unknown("HELPER_ABANDONED_BEFORE_DISPATCH")
|
||||
};
|
||||
persist_outcome_in(db, p, &outcome)?;
|
||||
Ok(outcome)
|
||||
}
|
||||
fn validate_start_request(p: &ExecutionPermitPayload, request: &Request) -> Result<()> {
|
||||
ensure!(
|
||||
request.job_bytes.len() <= 65536
|
||||
|
||||
Reference in New Issue
Block a user