Skip to content

Commit

Permalink
Declassifying stimecmp when resuming a confidential VM
Browse files Browse the repository at this point in the history
Signed-off-by: Wojciech Ozga <woz@zurich.ibm.com>
  • Loading branch information
wojciechozga committed Jun 6, 2024
1 parent c0b689f commit 45d6630
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
3 changes: 1 addition & 2 deletions security-monitor/src/core/control_data/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ pub struct ControlData {
}

impl ControlData {
const NOT_INITIALIZED_CONTROL_DATA: &'static str =
"Bug. Could not access the control data static variable because has not been initialized";
const NOT_INITIALIZED_CONTROL_DATA: &'static str = "Bug. Control data has not been initialized";

pub fn new() -> Self {
Self { confidential_vms: BTreeMap::new() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::non_confidential_flow::{ApplyToHypervisorHart, NonConfidentialFlow};
pub struct RunConfidentialHart {
confidential_vm_id: ConfidentialVmId,
confidential_hart_id: usize,
allowed_external_interrupts: usize,
stimecmp: usize,
hvip: usize,
}

Expand All @@ -19,14 +21,16 @@ impl RunConfidentialHart {
Self {
confidential_vm_id: ConfidentialVmId::new(hypervisor_hart.gprs().read(GeneralPurposeRegister::a0)),
confidential_hart_id: hypervisor_hart.gprs().read(GeneralPurposeRegister::a1),
allowed_external_interrupts: 0,
stimecmp: hypervisor_hart.csrs().stimecmp.read(),
hvip: hypervisor_hart.csrs().hvip.read(),
}
}

pub fn handle(mut self, non_confidential_flow: NonConfidentialFlow) -> ! {
match non_confidential_flow.into_confidential_flow(self.confidential_vm_id, self.confidential_hart_id) {
Ok((allowed_external_interrupts, confidential_flow)) => {
self.hvip &= allowed_external_interrupts;
self.allowed_external_interrupts = allowed_external_interrupts;
confidential_flow
.declassify_to_confidential_hart(DeclassifyToConfidentialVm::Resume(self))
.resume_confidential_hart_execution()
Expand All @@ -43,6 +47,13 @@ impl RunConfidentialHart {
}

pub fn declassify_to_confidential_hart(&self, confidential_hart: &mut ConfidentialHart) {
confidential_hart.csrs_mut().hvip.save_value(self.hvip);
// Guard against stepping attacks by adding random delay to the timer
let delay = 10; // TODO: generate random number

// We write directly to the CSR because we are after the heavy context switch
confidential_hart.csrs_mut().stimecmp.set(self.stimecmp + delay);

// Inject external interrupts
confidential_hart.csrs_mut().hvip.save_value(self.hvip & self.allowed_external_interrupts);
}
}

0 comments on commit 45d6630

Please sign in to comment.