Skip to content

Commit

Permalink
Removing unwraps in measure, fixing typos
Browse files Browse the repository at this point in the history
Signed-off-by: Diego Gonzalez Villalobos <[email protected]>
  • Loading branch information
DGonzalezVillal authored and tylerfanelli committed Nov 8, 2023
1 parent c5bd7a0 commit 2a9bbb5
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 31 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/target
.vscode/
Cargo.lock
.vscode/
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,9 @@ pub enum OVMFError {
/// Desired entry is missing from table
EntryMissingInTable(String),

/// Failed to get item from table
GetTableItemError,

/// Invalid Entry Size was provided
InvalidSize(String, usize, usize),

Expand All @@ -672,6 +675,9 @@ impl std::fmt::Display for OVMFError {
OVMFError::EntryMissingInTable(entry) => {
write!(f, "Can't find {entry} entry in OVMF table")
}
OVMFError::GetTableItemError => {
write!(f, "OVMF table failed to return item")
}
OVMFError::InvalidSize(entry, actual, expected) => {
write!(f, "Invalid size of {entry}: {actual} < {expected}")
}
Expand Down
2 changes: 1 addition & 1 deletion src/measurement/gctx.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0

//! Operations to handle and create a Guest Context
use std::convert::TryInto;

use openssl::sha::sha384;
Expand Down
3 changes: 0 additions & 3 deletions src/measurement/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// SPDX-License-Identifier: Apache-2.0

//! Everything one needs to calculate a launch measurement for a SEV encrypted confidential guest.
//!
//! This module contains the structures and functions needed to calculate a launch measurement.
//! This includes, GCTX, SEV-HASHES, VMSA and OVMF pages.
///
#[cfg(all(any(feature = "sev", feature = "snp"), feature = "openssl"))]
pub mod gctx;

Expand Down
31 changes: 20 additions & 11 deletions src/measurement/ovmf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ impl TryFrom<u8> for SectionType {
}
}
}
/// Creating strucutre from bytes
/// Creating structure from bytes
pub trait TryFromBytes {
/// Error when attempting to deserialize from_bytes
/// Error when attempting to deserialize from bytes
type Error;
/// Creating structure from bytes function
fn try_from_bytes(value: &[u8], offset: usize) -> Result<Self, Self::Error>
Expand Down Expand Up @@ -137,7 +137,7 @@ struct OvmfFooterTableEntry {
impl TryFrom<&[u8]> for OvmfFooterTableEntry {
type Error = MeasurementError;

/// Grenerate footer from data
/// Generate footer from data
fn try_from(value: &[u8]) -> Result<OvmfFooterTableEntry, MeasurementError> {
// Bytes 2-17 are the GUID
let guid: [u8; 16] = value[2..18].try_into()?;
Expand All @@ -158,14 +158,14 @@ const OVMF_SEV_META_DATA_GUID: Uuid = uuid!("dc886566-984a-4798-a75e-5585a7bf67c
pub struct OVMF {
/// OVMF data
data: Vec<u8>,
/// Table matching GUID to it's data
/// Table matching GUID to its data
table: HashMap<Uuid, Vec<u8>>,
/// Metadata item description
metadata_items: Vec<OvmfSevMetadataSectionDesc>,
}

impl OVMF {
/// Generate new OVMF structure by parsing the foorter table and SEV metadata
/// Generate new OVMF structure by parsing the footer table and SEV metadata
pub fn new(ovmf_file: PathBuf) -> Result<Self, MeasurementError> {
let mut data = Vec::new();
let mut file = match File::open(ovmf_file) {
Expand Down Expand Up @@ -222,31 +222,40 @@ impl OVMF {

/// Get the SEV HASHES GPA
pub fn sev_hashes_table_gpa(&self) -> Result<u64, OVMFError> {
if !(self.table.contains_key(&SEV_HASH_TABLE_RV_GUID)) {
if !self.table.contains_key(&SEV_HASH_TABLE_RV_GUID) {
return Err(OVMFError::EntryMissingInTable(
"SEV_HASH_TABLE_RV_GUID".to_string(),
));
}
Ok(self

if let Some(gpa) = self
.table_item(&SEV_HASH_TABLE_RV_GUID)
.and_then(|entry| entry.get(..4))
.map(|bytes| LittleEndian::read_u32(bytes) as u64)
.unwrap())
{
Ok(gpa)
} else {
Err(OVMFError::GetTableItemError)
}
}

/// Get the SEV-ES EIP
pub fn sev_es_reset_eip(&self) -> Result<u32, OVMFError> {
if !(self.table.contains_key(&SEV_ES_RESET_BLOCK_GUID)) {
if !self.table.contains_key(&SEV_ES_RESET_BLOCK_GUID) {
return Err(OVMFError::EntryMissingInTable(
"SEV_ES_RESET_BLOCK_GUID".to_string(),
));
}

Ok(self
if let Some(eip) = self
.table_item(&SEV_ES_RESET_BLOCK_GUID)
.and_then(|entry| entry.get(..4))
.map(LittleEndian::read_u32)
.unwrap())
{
Ok(eip)
} else {
Err(OVMFError::GetTableItemError)
}
}

/// Parse footer table data
Expand Down
14 changes: 7 additions & 7 deletions src/measurement/sev_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ impl FromStr for GuidLe {
#[repr(C)]
#[derive(Debug, Clone, Copy, Serialize, Default)]
struct SevHashTableEntry {
/// Entry GUID
/// GUID of the SEV hash
guid: GuidLe,
/// Length
/// Length of the hash
length: u16,
/// Hash
/// SEV HASH
hash: Sha256Hash,
}

Expand All @@ -75,9 +75,9 @@ impl SevHashTableEntry {
#[repr(C)]
#[derive(Debug, Clone, Copy, Serialize, Default)]
struct SevHashTable {
/// GUID
/// GUID of the SEV hash table entry
guid: GuidLe,
/// Length
/// Length of the SEV Has table entry
length: u16,
/// Cmd line append table entry
cmdline: SevHashTableEntry,
Expand Down Expand Up @@ -180,8 +180,8 @@ impl SevHashes {
})
}

///Generate the SEV hashes area - this must be *identical* to the way QEMU
///generates this info in order for the measurement to match.
/// Generate the SEV hashes area - this must be *identical* to the way QEMU
/// generates this info in order for the measurement to match.
pub fn construct_table(&self) -> Result<Vec<u8>, MeasurementError> {
let sev_hash_table = SevHashTable::new(
SEV_HASH_TABLE_HEADER_GUID.to_string().as_str(),
Expand Down
6 changes: 3 additions & 3 deletions src/measurement/vcpu_types.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: Apache-2.0

//! Enum of different exisitng AMD EPYC VCPUs
//! Exisiting AMD EPYC vCPUs
use std::{convert::TryFrom, fmt, str::FromStr};

use crate::error::MeasurementError;

/// All currently available QEMU VCPU types
/// All currently available QEMU vCPU types
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CpuType {
/// EPYC
Expand All @@ -24,7 +24,7 @@ pub enum CpuType {
EpycRome,
/// EPYC ROME V1
EpycRomeV1,
///EPYC ROME V2
/// EPYC ROME V2
EpycRomeV2,
/// EPYC ROME V3
EpycRomeV3,
Expand Down
3 changes: 1 addition & 2 deletions src/measurement/vmsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

//! Operations to build and interact with an SEV-ES VMSA
use crate::error::MeasurementError;
// use crate::measurement::{measurement_functions::SevMode, vcpu_types::CpuType};
use crate::measurement::vcpu_types::CpuType;
use serde::{Deserialize, Serialize};
use serde_big_array::BigArray;
Expand Down Expand Up @@ -32,7 +31,7 @@ impl FromStr for SevMode {
}
}

/// VmmTypes
/// Supported Virtual Machine Monitors
#[derive(Clone, Copy, PartialEq)]
pub enum VMMType {
/// QEMU
Expand Down
4 changes: 2 additions & 2 deletions tests/measurement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#[cfg(feature = "snp")]
mod snp_tests {
use sev::measurement::{snp::*, vmsa::VMMType};
// Test of we can generate a good OVMF hash
// Testing if we can generate a good OVMF hash
#[test]
fn test_snp_ovmf_hash_gen() {
let ovmf_hash = "cab7e085874b3acfdbe2d96dcaa3125111f00c35c6fc9708464c2ae74bfdb048a198cb9a9ccae0b3e5e1a33f5f249819";
Expand All @@ -30,7 +30,7 @@ mod snp_tests {
assert_eq!(ld_hex.as_str(), exp_result);
}

// Test of we can a full LD from the OVMF hash
// Test if we can compute a full LD from the OVMF hash
#[test]
fn test_snp_ovmf_hash_full() {
let ovmf_hash = hex::encode(
Expand Down

0 comments on commit 2a9bbb5

Please sign in to comment.