diff --git a/services/orchestrator/capabilities/src/lib.rs b/services/orchestrator/capabilities/src/lib.rs index f0b119405..a7e9f72b5 100644 --- a/services/orchestrator/capabilities/src/lib.rs +++ b/services/orchestrator/capabilities/src/lib.rs @@ -104,7 +104,8 @@ pub use progress::Progress; pub use recovery::{Recovery, RestoreOutcome}; pub use self_reset::{NoReset, SelfReset}; pub use self_update::{ - trial_outcome, NoSession, RunningImage, SelfUpdate, SelfUpdateState, TrialOutcome, + trial_outcome, BootSelection, NoSession, RunningImage, SelfUpdate, SelfUpdateState, + TrialOutcome, }; pub use svn_floor::{Svn, SvnFloor}; pub use updatable::{StageProgress, Updatable, UpdateError}; diff --git a/services/orchestrator/capabilities/src/self_reset.rs b/services/orchestrator/capabilities/src/self_reset.rs index 87ea05c2f..5f78aef3b 100644 --- a/services/orchestrator/capabilities/src/self_reset.rs +++ b/services/orchestrator/capabilities/src/self_reset.rs @@ -6,7 +6,7 @@ /// Actuation capability: reset the eRoT itself. /// /// Activating the eRoT's own update ends here: record the session, mark -/// the trial pending, reset. The boot selector then runs the trial image +/// the trial pending, reset. The boot stage then runs the trial image /// and [`settle_self_update`](crate::SelfUpdate) judges it on the way up. /// /// Not [`BootControl`](crate::BootControl), whose `hold_in_reset` and diff --git a/services/orchestrator/capabilities/src/self_update.rs b/services/orchestrator/capabilities/src/self_update.rs index 25738b22e..e7906f30d 100644 --- a/services/orchestrator/capabilities/src/self_update.rs +++ b/services/orchestrator/capabilities/src/self_update.rs @@ -29,24 +29,75 @@ //! what happened to the trial. What to do about it is the state machine's, the //! same split [`BootWatch`](crate::BootWatch) and [`Recovery`](crate::Recovery) //! already use: the seam answers with a verdict, the machine decides. +//! +//! The boot stage reports the SVN of the image it started, read from the +//! signed header it checked before the jump. `trial_outcome` compares that +//! SVN with the session SVN before it calls a trial in progress or +//! confirmed. A session that names one SVN while an image at another SVN +//! runs, because the boot stage fell back or a writer with flash access +//! replaced the session, reads as unconfirmed, and the SVN floor keeps the +//! SVN it has. use crate::Svn; -/// Which image the eRoT is running, as durable storage records it. +/// Which image the boot stage started: the trial one or the confirmed one. /// /// This, not whether the pending mark is still set, is what tells a trial boot -/// from a fallback. Platforms differ on the mark: a boot selector that consumes +/// from a fallback. Platforms differ on the mark: a boot stage that consumes /// it leaves nothing set by the time the trial image runs, while one that holds /// it until the trial is decided still does. Which image booted reads the same /// on both. #[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub enum RunningImage { +pub enum BootSelection { /// The image the pending mark selects. This boot is the trial. Trial, /// The image the last confirmed update left in place. Confirmed, } +/// The image the eRoT is running, as the boot stage reports it. +/// +/// The boot stage checks the signature of the image it starts and reads +/// the SVN from the signed header, so the SVN here comes from a checked +/// image. [`trial_outcome`] compares that SVN with the session SVN. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub struct RunningImage { + /// Which image the boot stage started. + pub selected: BootSelection, + /// The SVN of the running image. `None` when the platform cannot report + /// the SVN. [`trial_outcome`] then reads every session in flight as + /// unconfirmed, because the platform read no SVN to compare with the + /// session SVN. + pub svn: Option, +} + +impl RunningImage { + /// The trial image is running, at `svn`. + #[must_use] + pub const fn trial(svn: Svn) -> Self { + Self { + selected: BootSelection::Trial, + svn: Some(svn), + } + } + + /// The confirmed image is running, at `svn`. + #[must_use] + pub const fn confirmed(svn: Svn) -> Self { + Self { + selected: BootSelection::Confirmed, + svn: Some(svn), + } + } + + /// Whether the boot stage reported `svn` as the SVN of the running + /// image. Compares the inner integers because `==` on `Svn` is not + /// usable in a const fn. + const fn runs_at(self, svn: Svn) -> bool { + matches!(self.svn, Some(running) if running.0 == svn.0) + } +} + /// Where the eRoT's own update session stands, as held in durable storage. /// /// `Copy` and lifetime-free, like every capability vocabulary type. @@ -89,8 +140,9 @@ pub enum TrialOutcome { NoSession, /// A session exists that nothing will confirm: the trial booted and fell /// back, or it was never marked pending, or an image is running that the - /// session does not claim. All of them leave an update that has to be - /// dropped before another can start. + /// session does not claim, or the running image carries an SVN other + /// than the session SVN. In each case the caller has to revert the + /// session before another update can start. /// /// When the unclaimed trial is the image running right now, reverting /// only moves slot metadata: the part keeps running it until the next @@ -111,10 +163,10 @@ pub enum TrialOutcome { /// /// Where the session lives (protected flash, a reserved region, a mock in /// tests) is the implementor's concern, as is how the platform marks the boot -/// selector. A platform whose boot selector reads a record the eRoT writes +/// stage. A platform whose boot stage reads a record the eRoT writes /// folds the pending mark into the session's own write, and its /// [`set_trial_pending`](Self::set_trial_pending) does nothing beyond moving -/// the state. A platform whose boot selector is a register or a ROM-defined +/// the state. A platform whose boot stage is a register or a ROM-defined /// table writes twice behind the seam. Neither shows here. /// /// # Transitions @@ -144,10 +196,10 @@ pub enum TrialOutcome { /// /// # Boot bound /// -/// A boot selector that holds the pending mark across resets boots the trial +/// A boot stage that holds the pending mark across resets boots the trial /// again /// after every reset, so a trial image that dies before anything confirms it -/// loops forever unless something counts attempts. A selector that consumes +/// loops forever unless something counts attempts. A boot stage that consumes /// the mark does not have this problem: the second boot runs the confirmed /// image. Platforms in the first category need a ROM-side boot counter or /// equivalent bound on how many boots a trial gets. @@ -170,16 +222,17 @@ pub trait SelfUpdate { /// The session as durable storage holds it. fn state(&self) -> Result; - /// Which image this boot is running. + /// Which image this boot is running, and at which SVN. /// - /// [`Confirmed`](RunningImage::Confirmed) whenever no trial is in flight, - /// an `Idle` session included: with nothing pending there is nothing else - /// to be running. + /// [`Confirmed`](BootSelection::Confirmed) whenever no trial is in + /// flight, an `Idle` session included: with nothing pending there is + /// nothing else to be running. The SVN is what the boot stage read + /// from the signed header of the image it started, handed over at boot; + /// `None` when the platform has no such handover. /// - /// Read together with [`state`](Self::state) once at boot, as one - /// snapshot. Nothing moves the pair in between: the eRoT is the only - /// writer and it is not running its own update while deciding the last - /// one. + /// Read together with [`state`](Self::state) once at boot, before + /// settlement writes the session, as one snapshot. The boot stage reports + /// one image per boot. fn running(&self) -> Result; /// Records a new session with the verified SVN, leaving the state @@ -228,31 +281,50 @@ pub trait SelfUpdate { /// What became of the trial, from the session and the image that booted. /// /// A session and a running image that disagree are not an error: they are the -/// crash windows this function exists to name. +/// crash windows this function exists to name. A trial is in progress only +/// while the trial image runs at the SVN the session recorded, and a +/// confirmed session commits only while the image at that SVN runs; any +/// other SVN, or none, reads as unconfirmed. #[must_use] pub const fn trial_outcome(state: SelfUpdateState, running: RunningImage) -> TrialOutcome { - match (state, running) { + match (state, running.selected) { // Nothing in flight. - (SelfUpdateState::Idle, RunningImage::Confirmed) => TrialOutcome::NoSession, + (SelfUpdateState::Idle, BootSelection::Confirmed) => TrialOutcome::NoSession, // A trial image running with no session behind it: nothing records // what it was for, so nothing can confirm it. - (SelfUpdateState::Idle, RunningImage::Trial) => TrialOutcome::Unconfirmed, + (SelfUpdateState::Idle, BootSelection::Trial) => TrialOutcome::Unconfirmed, // Prepared, and the reset that would have started the trial never // came. - (SelfUpdateState::Prepared { .. }, RunningImage::Confirmed) => TrialOutcome::Unconfirmed, + (SelfUpdateState::Prepared { .. }, BootSelection::Confirmed) => TrialOutcome::Unconfirmed, // A trial booted while the session still reads Prepared: the mark // ran ahead of the record, the ordering rule's other side. - (SelfUpdateState::Prepared { .. }, RunningImage::Trial) => TrialOutcome::Unconfirmed, - // The trial is what is running now. - (SelfUpdateState::TrialPending { .. }, RunningImage::Trial) => TrialOutcome::InProgress, + (SelfUpdateState::Prepared { .. }, BootSelection::Trial) => TrialOutcome::Unconfirmed, + // The trial is what is running now, if it is the image the session + // recorded. A trial image at another SVN is one the session does + // not claim. + (SelfUpdateState::TrialPending { svn }, BootSelection::Trial) => { + if running.runs_at(svn) { + TrialOutcome::InProgress + } else { + TrialOutcome::Unconfirmed + } + } // The trial booted and the platform fell back, so the image either // failed or never reached the point that confirms it. - (SelfUpdateState::TrialPending { .. }, RunningImage::Confirmed) => { + (SelfUpdateState::TrialPending { .. }, BootSelection::Confirmed) => { TrialOutcome::Unconfirmed } - // Confirmed. The floor advance is the only step that may still be - // missing, and repeating it is harmless. - (SelfUpdateState::Committed { svn }, _) => TrialOutcome::ConfirmedUncommitted { svn }, + // Confirmed, and the image at that SVN runs. The SVN floor advance + // is the only step that may still be missing, and repeating it is + // harmless. + (SelfUpdateState::Committed { svn }, _) if running.runs_at(svn) => { + TrialOutcome::ConfirmedUncommitted { svn } + } + // Confirmed, but an image at another SVN runs: the boot stage + // fell back after the confirm, or a writer with flash access + // replaced the session. Advancing the SVN floor to the session SVN + // would push the floor past the image that runs. + (SelfUpdateState::Committed { .. }, _) => TrialOutcome::Unconfirmed, } } @@ -271,7 +343,12 @@ impl SelfUpdate for () { } fn running(&self) -> Result { - Ok(RunningImage::Confirmed) + // `state` reads `Idle` on this board, and `trial_outcome` compares + // no SVN for an `Idle` session, so `running` reports none. + Ok(RunningImage { + selected: BootSelection::Confirmed, + svn: None, + }) } fn prepare(&mut self, _svn: Svn) -> Result<(), Self::Error> { @@ -340,7 +417,7 @@ mod tests { fn idle() -> Self { Self { state: Cell::new(SelfUpdateState::Idle), - running: Cell::new(RunningImage::Confirmed), + running: Cell::new(RunningImage::confirmed(OLD)), fail: false, } } @@ -348,14 +425,16 @@ mod tests { fn faulty() -> Self { Self { state: Cell::new(SelfUpdateState::Idle), - running: Cell::new(RunningImage::Confirmed), + running: Cell::new(RunningImage::confirmed(OLD)), fail: true, } } - /// Stands in for the reset that boots what `set_trial_pending` marked. - fn reboot_into_the_trial(&self) { - self.running.set(RunningImage::Trial); + /// Stands in for the reset that boots what `set_trial_pending` + /// marked: the boot stage starts the trial image and reports the + /// SVN it read from that image. + fn reboot_into_the_trial(&self, svn: Svn) { + self.running.set(RunningImage::trial(svn)); } } @@ -432,6 +511,8 @@ mod tests { } } + /// The SVN of the image the eRoT ran before the update. + const OLD: Svn = Svn(3); const SVN: Svn = Svn(7); const NEWER: Svn = Svn(9); @@ -445,7 +526,7 @@ mod tests { session.state(), Ok(SelfUpdateState::TrialPending { svn: SVN }) ); - session.reboot_into_the_trial(); + session.reboot_into_the_trial(SVN); assert_eq!( trial_outcome( session.state().expect("store answers"), @@ -545,7 +626,19 @@ mod tests { #[test] fn reports_no_session_when_nothing_is_in_flight() { assert_eq!( - trial_outcome(SelfUpdateState::Idle, RunningImage::Confirmed), + trial_outcome(SelfUpdateState::Idle, RunningImage::confirmed(OLD)), + TrialOutcome::NoSession + ); + } + + /// A board with no session reports no SVN either, and `trial_outcome` + /// still reads the boot as nothing in flight. + #[test] + fn reports_no_session_without_a_running_svn() { + let running = ().running().expect("the no-session board answers"); + assert_eq!(running.svn, None); + assert_eq!( + trial_outcome(SelfUpdateState::Idle, running), TrialOutcome::NoSession ); } @@ -555,7 +648,7 @@ mod tests { #[test] fn reports_a_trial_image_with_no_session_unconfirmed() { assert_eq!( - trial_outcome(SelfUpdateState::Idle, RunningImage::Trial), + trial_outcome(SelfUpdateState::Idle, RunningImage::trial(SVN)), TrialOutcome::Unconfirmed ); } @@ -567,7 +660,7 @@ mod tests { assert_eq!( trial_outcome( SelfUpdateState::Prepared { svn: SVN }, - RunningImage::Confirmed + RunningImage::confirmed(OLD) ), TrialOutcome::Unconfirmed ); @@ -578,31 +671,63 @@ mod tests { #[test] fn reports_a_trial_ahead_of_its_session_unconfirmed() { assert_eq!( - trial_outcome(SelfUpdateState::Prepared { svn: SVN }, RunningImage::Trial), + trial_outcome( + SelfUpdateState::Prepared { svn: SVN }, + RunningImage::trial(SVN) + ), TrialOutcome::Unconfirmed ); } - /// The boot that is running is the trial itself, on a selector that + /// The boot that is running is the trial itself, on a boot stage that /// consumes the pending mark and on one that holds it alike. #[test] fn reports_the_running_trial_in_progress() { assert_eq!( trial_outcome( SelfUpdateState::TrialPending { svn: SVN }, - RunningImage::Trial + RunningImage::trial(SVN) ), TrialOutcome::InProgress ); } + /// The trial image runs, but at an SVN the session never recorded: a + /// writer with flash access replaced the session, or the boot stage + /// started an image the eRoT did not stage. The session does not claim + /// that image, so `trial_outcome` reads the session as unconfirmed. + #[test] + fn reports_a_trial_at_another_svn_unconfirmed() { + assert_eq!( + trial_outcome( + SelfUpdateState::TrialPending { svn: SVN }, + RunningImage::trial(NEWER) + ), + TrialOutcome::Unconfirmed + ); + } + + /// A platform that reports no SVN cannot have its trial confirmed: no + /// SVN equals the session SVN, so the session reads as unconfirmed. + #[test] + fn reports_a_trial_without_a_running_svn_unconfirmed() { + let running = RunningImage { + selected: BootSelection::Trial, + svn: None, + }; + assert_eq!( + trial_outcome(SelfUpdateState::TrialPending { svn: SVN }, running), + TrialOutcome::Unconfirmed + ); + } + /// The trial booted and the platform fell back, so nothing confirmed it. #[test] fn reports_a_trial_that_fell_back_unconfirmed() { assert_eq!( trial_outcome( SelfUpdateState::TrialPending { svn: SVN }, - RunningImage::Confirmed + RunningImage::confirmed(OLD) ), TrialOutcome::Unconfirmed ); @@ -610,14 +735,50 @@ mod tests { /// Crashed between `confirm` and the floor advance: the one case that /// commits rather than drops, and the reason the session carries a state - /// and not only an SVN. + /// and not only an SVN. The image at the session SVN runs whether the + /// boot stage still calls it the trial or already calls it + /// confirmed. An image other than the trial image, at the same SVN, + /// reads the same way; the SVN floor advance then lands on the SVN that + /// runs. #[test] fn reports_a_confirmed_trial_whose_floor_is_unmoved() { - for running in [RunningImage::Trial, RunningImage::Confirmed] { + for running in [RunningImage::trial(SVN), RunningImage::confirmed(SVN)] { assert_eq!( trial_outcome(SelfUpdateState::Committed { svn: SVN }, running), TrialOutcome::ConfirmedUncommitted { svn: SVN } ); } } + + /// Confirmed, but the image that runs carries the old SVN: the boot + /// stage fell back after the confirm. Committing would advance the + /// SVN floor past the image that runs, so the session reads as + /// unconfirmed. + #[test] + fn reports_a_confirmed_trial_the_boot_stage_fell_back_from_unconfirmed() { + assert_eq!( + trial_outcome( + SelfUpdateState::Committed { svn: SVN }, + RunningImage::confirmed(OLD) + ), + TrialOutcome::Unconfirmed + ); + } + + /// Confirmed, but the platform reports no running SVN, whichever image + /// the boot stage says it started: a platform that cannot say what + /// runs cannot commit an SVN floor advance. + #[test] + fn reports_a_confirmed_trial_without_a_running_svn_unconfirmed() { + for selected in [BootSelection::Trial, BootSelection::Confirmed] { + let running = RunningImage { + selected, + svn: None, + }; + assert_eq!( + trial_outcome(SelfUpdateState::Committed { svn: SVN }, running), + TrialOutcome::Unconfirmed + ); + } + } } diff --git a/services/orchestrator/driver/src/driver/self_update.rs b/services/orchestrator/driver/src/driver/self_update.rs index a05a637a8..4345174af 100644 --- a/services/orchestrator/driver/src/driver/self_update.rs +++ b/services/orchestrator/driver/src/driver/self_update.rs @@ -10,7 +10,7 @@ use openprot_orchestrator_sm::{Event, Orchestrator}; use orchestrator_capabilities::{ - RunningImage, SelfUpdate, SelfUpdateState, Svn, SvnFloor, TrialOutcome, + BootSelection, SelfUpdate, SelfUpdateState, Svn, SvnFloor, TrialOutcome, }; use super::{DriverError, PlatformDriver}; @@ -147,9 +147,16 @@ impl SelfUpdateSettlement { /// update never ran, or its trial failed and the platform fell back. The /// session is reverted, so the update is done again rather than counted as /// finished. -/// - A trial image is running that no session claims: the session is reverted, -/// which also clears the pending mark, so the confirmed image runs after a -/// reset. The eRoT must not keep running an image nothing vouched for. +/// - A trial image is running that no session claims, because no session +/// exists or the session names another SVN: settlement reverts any +/// session, which also clears the pending mark, so the confirmed image +/// runs after a reset. The eRoT must not keep running an image that no +/// session claims. +/// - The session reads `Committed` but the boot stage started the +/// confirmed image, at an SVN other than the session SVN or with no SVN: +/// the boot stage fell back after the confirm. Settlement reverts the +/// session and the SVN floor keeps the SVN it has, which is at or below +/// the SVN of the image that runs. /// - The session is confirmed and the floor is still below its SVN: the floor /// advance is what is left, and the Update Agent requests it, so this /// boot must not do it. @@ -192,7 +199,7 @@ pub fn settle_self_update( } TrialOutcome::Unconfirmed => { session.revert().map_err(SettleError::Session)?; - if running == RunningImage::Trial { + if running.selected == BootSelection::Trial { return Ok(SelfUpdateSettlement::UnclaimedImageRunning); } Ok(SelfUpdateSettlement::Settled) @@ -305,10 +312,13 @@ pub enum CommitFloorError { Session(S), /// Writing the eRoT's own anti-rollback floor failed. Floor(F), - /// No confirmed self-update is waiting for the floor, so there is no SVN - /// an image has proven itself at. Carries what the session says: `Idle` - /// when no self-update is in flight or the request arrived a second time, - /// `TrialPending` when it arrived before the trial boot was judged. + /// No confirmed self-update is waiting for the SVN floor advance, so the + /// request has no SVN to write into the SVN floor. Carries what the + /// session says: `Idle` when + /// no self-update is in flight or the request arrived a second time, + /// `TrialPending` when it arrived before the trial boot was judged, and + /// `Committed` when the image that runs carries an SVN other than the + /// session SVN, or no SVN. NotConfirmed(SelfUpdateState), } diff --git a/services/orchestrator/driver/src/tests.rs b/services/orchestrator/driver/src/tests.rs index 489736fda..db0163a11 100644 --- a/services/orchestrator/driver/src/tests.rs +++ b/services/orchestrator/driver/src/tests.rs @@ -817,18 +817,27 @@ impl SelfReset for MockSelfReset { } /// The eRoT's own update session, in RAM. `running` says which image this -/// boot runs, the trial one or the confirmed one, as a test sets it. +/// boot runs, the trial one or the confirmed one and at which SVN, as a +/// test sets it. struct MockSelfUpdate { state: SelfUpdateState, running: RunningImage, fail: bool, } +/// The SVN of the image the eRoT ran before any update in these tests. +/// Every session SVN below is higher, so no session SVN equals the SVN of +/// this image. +const OLD_SVN: Svn = Svn(3); +/// An SVN no session in these tests records: the image of some other +/// update, started by the boot stage without a session behind it. +const FOREIGN_SVN: Svn = Svn(9); + impl MockSelfUpdate { fn idle() -> Self { Self { state: SelfUpdateState::Idle, - running: RunningImage::Confirmed, + running: RunningImage::confirmed(OLD_SVN), fail: false, } } @@ -2823,7 +2832,7 @@ fn settle_with_no_session_changes_nothing() { #[test] fn settle_reports_a_running_trial_and_leaves_the_session() { let pending = SelfUpdateState::TrialPending { svn: Svn(7) }; - let mut session = MockSelfUpdate::with_session(pending, RunningImage::Trial); + let mut session = MockSelfUpdate::with_session(pending, RunningImage::trial(Svn(7))); assert_eq!( settle_self_update(&mut session, &MockFloor::new()), @@ -2839,7 +2848,7 @@ fn settle_reports_a_running_trial_and_leaves_the_session() { fn settle_reverts_a_trial_that_fell_back() { let mut session = MockSelfUpdate::with_session( SelfUpdateState::TrialPending { svn: Svn(7) }, - RunningImage::Confirmed, + RunningImage::confirmed(OLD_SVN), ); assert_eq!( @@ -2857,7 +2866,7 @@ fn settle_reverts_a_trial_that_fell_back() { #[test] fn settle_keeps_a_confirmed_session_the_floor_has_not_taken() { let committed = SelfUpdateState::Committed { svn: Svn(7) }; - let mut session = MockSelfUpdate::with_session(committed, RunningImage::Trial); + let mut session = MockSelfUpdate::with_session(committed, RunningImage::trial(Svn(7))); let floor = MockFloor::with_floor(Svn(3)); assert_eq!( @@ -2876,7 +2885,7 @@ fn settle_keeps_a_confirmed_session_the_floor_has_not_taken() { fn settle_completes_a_session_whose_floor_already_moved() { let mut session = MockSelfUpdate::with_session( SelfUpdateState::Committed { svn: Svn(7) }, - RunningImage::Trial, + RunningImage::trial(Svn(7)), ); assert_eq!( @@ -2893,7 +2902,7 @@ fn settle_completes_a_session_whose_floor_already_moved() { fn settle_completes_a_session_whose_floor_moved_past_it() { let mut session = MockSelfUpdate::with_session( SelfUpdateState::Committed { svn: Svn(7) }, - RunningImage::Trial, + RunningImage::trial(Svn(7)), ); assert_eq!( @@ -2923,7 +2932,7 @@ fn settle_with_an_unreadable_session_fails_secure() { #[test] fn settle_with_an_unreadable_floor_fails_secure() { let committed = SelfUpdateState::Committed { svn: Svn(7) }; - let mut session = MockSelfUpdate::with_session(committed, RunningImage::Trial); + let mut session = MockSelfUpdate::with_session(committed, RunningImage::trial(Svn(7))); assert_eq!( settle_self_update(&mut session, &MockFloor::faulting()), @@ -2938,7 +2947,69 @@ fn settle_with_an_unreadable_floor_fails_secure() { // after a reset. #[test] fn settle_reports_an_unclaimed_image_and_reverts_the_session() { - let mut session = MockSelfUpdate::with_session(SelfUpdateState::Idle, RunningImage::Trial); + let mut session = + MockSelfUpdate::with_session(SelfUpdateState::Idle, RunningImage::trial(Svn(7))); + + assert_eq!( + settle_self_update(&mut session, &MockFloor::new()), + Ok(SelfUpdateSettlement::UnclaimedImageRunning) + ); + + assert_eq!(session.state, SelfUpdateState::Idle); +} + +// Confirmed, but the boot stage fell back after the confirm: the old +// image runs. Settlement reverts the session, the SVN floor keeps the SVN +// it has, and settlement returns `Settled`. +#[test] +fn settle_reverts_a_confirmed_session_the_boot_stage_fell_back_from() { + let mut session = MockSelfUpdate::with_session( + SelfUpdateState::Committed { svn: Svn(7) }, + RunningImage::confirmed(OLD_SVN), + ); + let floor = MockFloor::with_floor(OLD_SVN); + + assert_eq!( + settle_self_update(&mut session, &floor), + Ok(SelfUpdateSettlement::Settled) + ); + + assert_eq!(session.state, SelfUpdateState::Idle); + assert_eq!( + floor.floor(), + Ok(OLD_SVN), + "the SVN floor keeps the SVN it has" + ); +} + +// Confirmed, and the boot stage still calls the running image the +// trial, but at an SVN the session never recorded. The session does not +// claim that image, so settlement reports the image unclaimed and reverts +// the session. +#[test] +fn settle_reports_a_confirmed_session_whose_trial_runs_at_another_svn() { + let mut session = MockSelfUpdate::with_session( + SelfUpdateState::Committed { svn: Svn(7) }, + RunningImage::trial(FOREIGN_SVN), + ); + + assert_eq!( + settle_self_update(&mut session, &MockFloor::with_floor(OLD_SVN)), + Ok(SelfUpdateSettlement::UnclaimedImageRunning) + ); + + assert_eq!(session.state, SelfUpdateState::Idle); +} + +// A trial image at an SVN other than the session SVN: the session does +// not claim the image, so settlement reverts the session and reports the +// image unclaimed. +#[test] +fn settle_reports_a_trial_at_another_svn_unclaimed() { + let mut session = MockSelfUpdate::with_session( + SelfUpdateState::TrialPending { svn: Svn(7) }, + RunningImage::trial(FOREIGN_SVN), + ); assert_eq!( settle_self_update(&mut session, &MockFloor::new()), @@ -2954,7 +3025,7 @@ fn settle_reports_an_unclaimed_image_and_reverts_the_session() { fn commit_advances_the_floor_and_closes_the_session() { let mut session = MockSelfUpdate::with_session( SelfUpdateState::Committed { svn: Svn(7) }, - RunningImage::Trial, + RunningImage::trial(Svn(7)), ); let mut floor = MockFloor::with_floor(Svn(3)); @@ -2984,7 +3055,7 @@ fn commit_without_a_session_is_refused() { #[test] fn commit_during_a_trial_is_refused() { let pending = SelfUpdateState::TrialPending { svn: Svn(7) }; - let mut session = MockSelfUpdate::with_session(pending, RunningImage::Trial); + let mut session = MockSelfUpdate::with_session(pending, RunningImage::trial(Svn(7))); let mut floor = MockFloor::with_floor(Svn(3)); assert_eq!( @@ -2995,6 +3066,23 @@ fn commit_during_a_trial_is_refused() { assert_eq!(floor.floor(), Ok(Svn(3))); } +// Confirmed, but the image that runs carries another SVN. The SVN floor +// must not move past the image that runs, so the commit refuses the +// request. +#[test] +fn commit_while_an_image_at_another_svn_runs_is_refused() { + let committed = SelfUpdateState::Committed { svn: Svn(7) }; + let mut session = MockSelfUpdate::with_session(committed, RunningImage::confirmed(OLD_SVN)); + let mut floor = MockFloor::with_floor(OLD_SVN); + + assert_eq!( + commit_self_svn_floor(&mut session, &mut floor), + Err(CommitFloorError::NotConfirmed(committed)) + ); + + assert_eq!(floor.floor(), Ok(OLD_SVN)); +} + // The eRoT crashed after the floor moved to 7 but before the session closed. // The Update Agent requests again: advancing to 7 a second time changes // nothing, and this run closes the session. A third request has no @@ -3003,7 +3091,7 @@ fn commit_during_a_trial_is_refused() { fn commit_repeated_after_a_crash_finishes_the_close() { let mut session = MockSelfUpdate::with_session( SelfUpdateState::Committed { svn: Svn(7) }, - RunningImage::Trial, + RunningImage::trial(Svn(7)), ); let mut floor = MockFloor::with_floor(Svn(7)); @@ -3025,7 +3113,7 @@ fn commit_repeated_after_a_crash_finishes_the_close() { #[test] fn commit_with_a_floor_that_cannot_advance_leaves_the_session_open() { let committed = SelfUpdateState::Committed { svn: Svn(7) }; - let mut session = MockSelfUpdate::with_session(committed, RunningImage::Trial); + let mut session = MockSelfUpdate::with_session(committed, RunningImage::trial(Svn(7))); assert_eq!( commit_self_svn_floor(&mut session, &mut MockFloor::faulting()), @@ -3044,7 +3132,7 @@ fn commit_with_a_floor_that_cannot_advance_leaves_the_session_open() { fn commit_with_an_unreadable_session_fails_secure() { let mut session = MockSelfUpdate::with_session( SelfUpdateState::Committed { svn: Svn(7) }, - RunningImage::Trial, + RunningImage::trial(Svn(7)), ); session.fail = true; let mut floor = MockFloor::with_floor(Svn(3)); @@ -3574,7 +3662,13 @@ fn self_trial_driver(session: MockSelfUpdate) -> PlatformDriver { /// The eRoT's own session in the state the test needs, with the trial /// image running. fn trial_session(state: SelfUpdateState) -> MockSelfUpdate { - MockSelfUpdate::with_session(state, RunningImage::Trial) + let svn = match state { + SelfUpdateState::Idle => OLD_SVN, + SelfUpdateState::Prepared { svn } + | SelfUpdateState::TrialPending { svn } + | SelfUpdateState::Committed { svn } => svn, + }; + MockSelfUpdate::with_session(state, RunningImage::trial(svn)) } /// What the board's self-update session reads. @@ -4159,7 +4253,7 @@ fn an_update_aimed_at_the_erot_is_refused_when_the_board_wires_no_self_update() fn a_second_self_update_is_refused_while_the_first_awaits_its_floor() { let mut driver = erot_driver(MockSelfUpdate::with_session( SelfUpdateState::Committed { svn: Svn(7) }, - RunningImage::Confirmed, + RunningImage::confirmed(Svn(7)), )); assert_eq!( @@ -4196,7 +4290,22 @@ fn a_self_update_is_refused_while_the_trial_is_being_judged() { fn a_self_update_may_start_over_a_trial_that_fell_back() { let mut driver = erot_driver(MockSelfUpdate::with_session( SelfUpdateState::TrialPending { svn: Svn(7) }, - RunningImage::Confirmed, + RunningImage::confirmed(OLD_SVN), + )); + + assert_eq!(driver.submit_update(C0, CANDIDATE_LEN), Ok(())); +} + +// A confirmed session while an image at another SVN runs: the boot +// stage fell back after the confirm. Settlement reverts the session, and +// the commit refuses the SVN floor request for the session. The session +// must not block the next self-update until a reset, so `submit_update` +// accepts a new update. +#[test] +fn a_self_update_may_start_over_a_confirmed_session_the_boot_stage_fell_back_from() { + let mut driver = erot_driver(MockSelfUpdate::with_session( + SelfUpdateState::Committed { svn: Svn(7) }, + RunningImage::confirmed(OLD_SVN), )); assert_eq!(driver.submit_update(C0, CANDIDATE_LEN), Ok(())); diff --git a/target/ast10x0/tests/integration/mock_downstream_device/orchestrator_main.rs b/target/ast10x0/tests/integration/mock_downstream_device/orchestrator_main.rs index b3bb2a7af..627c7af69 100644 --- a/target/ast10x0/tests/integration/mock_downstream_device/orchestrator_main.rs +++ b/target/ast10x0/tests/integration/mock_downstream_device/orchestrator_main.rs @@ -51,9 +51,9 @@ use orchestrator_capabilities::AuthenticatedImage; #[cfg(measured)] use orchestrator_capabilities::Measurement; use orchestrator_capabilities::{ - BootControl, BootStatus, EvidenceReader, IncrementalVerifier, PollOutcome, RunningImage, - SelfUpdate, SelfUpdateState, StageProgress, Svn, SvnFloor, Updatable, UpdateError, - VerifySession, + BootControl, BootSelection, BootStatus, EvidenceReader, IncrementalVerifier, PollOutcome, + RunningImage, SelfUpdate, SelfUpdateState, StageProgress, Svn, SvnFloor, Updatable, + UpdateError, VerifySession, }; #[cfg(self_update)] use orchestrator_capabilities::{Progress, SelfReset}; @@ -406,13 +406,20 @@ impl SelfUpdate for SelfSession { } fn running(&self) -> Result { - // Which image this boot runs is the boot selector's answer, not the - // session's. Here it is the scenario's: a self-trial build is the - // boot that runs the trial image. + // The boot stage, not the session, answers which image this + // boot runs and at which SVN. Here the scenario answers: a + // self-trial build is the boot that runs the trial image, at the + // SVN `install` recorded for the session. Every other build boots + // the image that was there before any session, and the scenario + // never compares that image with a session, so `running` reports + // no SVN. Ok(if EXPECT_SELF_TRIAL { - RunningImage::Trial + RunningImage::trial(TRIAL_SVN) } else { - RunningImage::Confirmed + RunningImage { + selected: BootSelection::Confirmed, + svn: None, + } }) }