pub const RECOVERY_EXIT_MAGIC: u32 = 0x5245_5854; pub const RECOVERY_EXIT_VERSION: u16 = 1; pub const MAX_GENERATION_STEP: u64 = 64; pub const CLEAN_RESET_SENTINEL: u64 = 0x434C_4E5F_5253_5431; #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ExitPhase { InRecovery = 0, Refused = 1, NormalBootActive = 2, } #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ExitError { InvalidMagic = 1, UnsupportedVersion = 2, ZeroRecoveryGeneration = 3, ZeroTargetSlotId = 4, ZeroExportReceiptDigest = 5, MismatchedExportReceipt = 6, PendingRecoveryIoNotDrained = 7, InvalidCleanResetSentinel = 8, InheritedServiceHandlesPresent = 9, InheritedCapabilityLeasesPresent = 10, NonAdvancingRuntimeGeneration = 11, ExcessiveGenerationStep = 12, StaleHandleAuthorityRejected = 13, UnboundTransitionDigest = 14, AlreadyExited = 15, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct RecoveryExitRequest { pub magic: u32, pub version: u16, pub target_slot_id: u16, pub recovery_runtime_generation: u64, pub target_runtime_generation: u64, pub claimed_export_receipt_digest: u64, pub expected_export_receipt_digest: u64, pub pending_recovery_io_ops: u32, pub inherited_service_handles: u32, pub inherited_capability_leases: u32, pub presented_handle_generation: u64, pub clean_reset_sentinel: u64, pub transition_binding_digest: u64, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct NormalBootReceipt { pub target_slot_id: u16, pub previous_recovery_generation: u64, pub active_normal_generation: u64, pub revoked_recovery_handles: u32, pub verified_export_receipt_digest: u64, pub transition_binding_digest: u64, pub normal_boot_digest: u64, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct ExitStatus { pub phase: ExitPhase, pub exited: bool, pub target_slot_id: u16, pub previous_recovery_generation: u64, pub active_normal_generation: u64, pub revoked_recovery_handles: u32, pub transition_binding_digest: u64, pub normal_boot_digest: u64, pub handle_inheritance_refusals: u64, pub stale_generation_refusals: u64, pub state_hygiene_refusals: u64, pub rejected_total: u64, pub exited_total: u64, } pub const fn compute_transition_binding( recovery_gen: u64, target_gen: u64, target_slot_id: u16, export_receipt_digest: u64, clean_reset_sentinel: u64, ) -> u64 { let mut h: u64 = 0xCBF2_9CE4_8422_2325; h = (h ^ recovery_gen).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ target_gen).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ (target_slot_id as u64)).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ export_receipt_digest).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ clean_reset_sentinel).wrapping_mul(0x0000_0100_0000_01B3); h } pub const fn compute_normal_boot_digest( transition_binding_digest: u64, active_normal_generation: u64, revoked_recovery_handles: u32, ) -> u64 { let mut h = transition_binding_digest ^ 0x517C_C1B7_2722_0A95; h = (h ^ active_normal_generation).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ (revoked_recovery_handles as u64)).wrapping_mul(0x0000_0100_0000_01B3); h } pub fn verify_handle_generation( presented_handle_generation: u64, recovery_runtime_generation: u64, target_runtime_generation: u64, ) -> Result<(), ExitError> { if presented_handle_generation <= recovery_runtime_generation || presented_handle_generation != target_runtime_generation { return Err(ExitError::StaleHandleAuthorityRejected); } Ok(()) } pub fn transition_to_normal_boot( req: &RecoveryExitRequest, revoked_recovery_handles: u32, ) -> Result { if req.magic != RECOVERY_EXIT_MAGIC { return Err(ExitError::InvalidMagic); } if req.version != RECOVERY_EXIT_VERSION { return Err(ExitError::UnsupportedVersion); } if req.recovery_runtime_generation == 0 { return Err(ExitError::ZeroRecoveryGeneration); } if req.target_slot_id == 0 { return Err(ExitError::ZeroTargetSlotId); } if req.expected_export_receipt_digest == 0 { return Err(ExitError::ZeroExportReceiptDigest); } if req.claimed_export_receipt_digest != req.expected_export_receipt_digest { return Err(ExitError::MismatchedExportReceipt); } if req.pending_recovery_io_ops != 0 { return Err(ExitError::PendingRecoveryIoNotDrained); } if req.clean_reset_sentinel != CLEAN_RESET_SENTINEL { return Err(ExitError::InvalidCleanResetSentinel); } if req.inherited_service_handles != 0 { return Err(ExitError::InheritedServiceHandlesPresent); } if req.inherited_capability_leases != 0 { return Err(ExitError::InheritedCapabilityLeasesPresent); } if req.target_runtime_generation <= req.recovery_runtime_generation { return Err(ExitError::NonAdvancingRuntimeGeneration); } if req.target_runtime_generation - req.recovery_runtime_generation > MAX_GENERATION_STEP { return Err(ExitError::ExcessiveGenerationStep); } verify_handle_generation( req.presented_handle_generation, req.recovery_runtime_generation, req.target_runtime_generation, )?; let expected_binding = compute_transition_binding( req.recovery_runtime_generation, req.target_runtime_generation, req.target_slot_id, req.expected_export_receipt_digest, req.clean_reset_sentinel, ); if req.transition_binding_digest != expected_binding { return Err(ExitError::UnboundTransitionDigest); } let normal_boot_digest = compute_normal_boot_digest( expected_binding, req.target_runtime_generation, revoked_recovery_handles, ); Ok(NormalBootReceipt { target_slot_id: req.target_slot_id, previous_recovery_generation: req.recovery_runtime_generation, active_normal_generation: req.target_runtime_generation, revoked_recovery_handles, verified_export_receipt_digest: req.expected_export_receipt_digest, transition_binding_digest: expected_binding, normal_boot_digest, }) } pub struct RecoveryExitCoordinator { phase: ExitPhase, exited: bool, target_slot_id: u16, recovery_generation: u64, revoked_recovery_handles: u32, committed: Option, handle_inheritance_refusals: u64, stale_generation_refusals: u64, state_hygiene_refusals: u64, rejected_total: u64, exited_total: u64, } impl RecoveryExitCoordinator { pub const fn new( recovery_generation: u64, target_slot_id: u16, revoked_recovery_handles: u32, ) -> Self { Self { phase: ExitPhase::InRecovery, exited: false, target_slot_id, recovery_generation, revoked_recovery_handles, committed: None, handle_inheritance_refusals: 0, stale_generation_refusals: 0, state_hygiene_refusals: 0, rejected_total: 0, exited_total: 0, } } pub fn exit_to_normal( &mut self, req: &RecoveryExitRequest, ) -> Result { if self.exited { self.rejected_total += 1; return Err(ExitError::AlreadyExited); } match transition_to_normal_boot(req, self.revoked_recovery_handles) { Ok(receipt) => { self.phase = ExitPhase::NormalBootActive; self.exited = true; self.exited_total += 1; self.committed = Some(receipt); Ok(receipt) } Err(e) => { self.phase = ExitPhase::Refused; self.rejected_total += 1; match e { ExitError::InheritedServiceHandlesPresent | ExitError::InheritedCapabilityLeasesPresent => { self.handle_inheritance_refusals += 1; } ExitError::NonAdvancingRuntimeGeneration | ExitError::ExcessiveGenerationStep | ExitError::StaleHandleAuthorityRejected => { self.stale_generation_refusals += 1; } _ => { self.state_hygiene_refusals += 1; } } Err(e) } } } pub fn committed_receipt(&self) -> Option { self.committed } pub fn status(&self) -> ExitStatus { let (active_normal_generation, transition_binding_digest, normal_boot_digest) = match self.committed { Some(r) => ( r.active_normal_generation, r.transition_binding_digest, r.normal_boot_digest, ), None => (0, 0, 0), }; ExitStatus { phase: self.phase, exited: self.exited, target_slot_id: self.target_slot_id, previous_recovery_generation: self.recovery_generation, active_normal_generation, revoked_recovery_handles: self.revoked_recovery_handles, transition_binding_digest, normal_boot_digest, handle_inheritance_refusals: self.handle_inheritance_refusals, stale_generation_refusals: self.stale_generation_refusals, state_hygiene_refusals: self.state_hygiene_refusals, rejected_total: self.rejected_total, exited_total: self.exited_total, } } }