Skip to content

Commit

Permalink
introduce Td Info metadata section support.
Browse files Browse the repository at this point in the history
Signed-off-by: Yang, Longlong <[email protected]>
  • Loading branch information
longlongyang committed Nov 13, 2023
1 parent 8623615 commit ac6fb26
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
10 changes: 10 additions & 0 deletions devtools/td-layout-config/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct ImageConfig {
temp_heap: String,
#[serde(rename = "Payload")]
builtin_payload: Option<String>,
#[serde(rename = "TdInfo")]
td_info: Option<String>,
#[serde(rename = "Metadata")]
metadata: String,
#[serde(rename = "Ipl")]
Expand Down Expand Up @@ -65,6 +67,14 @@ pub fn parse_image(data: String) -> String {
"Reserved",
);

if let Some(td_info_config) = image_config.td_info {
image_layout.reserve_high(
"TdInfo",
parse_int::parse::<u32>(&td_info_config).unwrap() as usize,
"Reserved",
)
}

if let Some(payload_config) = image_config.builtin_payload {
image_layout.reserve_high(
"Payload",
Expand Down
5 changes: 3 additions & 2 deletions td-shim-tools/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use td_shim::metadata::{
TdxMetadataDescriptor, TDX_METADATA_GUID, TDX_METADATA_SECTION_TYPE_BFV,
TDX_METADATA_SECTION_TYPE_CFV, TDX_METADATA_SECTION_TYPE_PAYLOAD,
TDX_METADATA_SECTION_TYPE_PAYLOAD_PARAM, TDX_METADATA_SECTION_TYPE_PERM_MEM,
TDX_METADATA_SECTION_TYPE_TD_HOB, TDX_METADATA_SECTION_TYPE_TEMP_MEM, TDX_METADATA_SIGNATURE,
TDX_METADATA_VERSION,
TDX_METADATA_SECTION_TYPE_TD_HOB, TDX_METADATA_SECTION_TYPE_TD_INFO,
TDX_METADATA_SECTION_TYPE_TEMP_MEM, TDX_METADATA_SIGNATURE, TDX_METADATA_VERSION,
};
use td_uefi_pi::pi::guid::Guid;

Expand Down Expand Up @@ -75,6 +75,7 @@ where
"PermMem" => Ok(TDX_METADATA_SECTION_TYPE_PERM_MEM),
"Payload" => Ok(TDX_METADATA_SECTION_TYPE_PAYLOAD),
"PayloadParam" => Ok(TDX_METADATA_SECTION_TYPE_PAYLOAD_PARAM),
"TdInfo" => Ok(TDX_METADATA_SECTION_TYPE_TD_INFO),
_ => Err(D::Error::custom("Invalid metadata section type")),
}
}
Expand Down
7 changes: 2 additions & 5 deletions td-shim-tools/src/tee_info_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,8 @@ impl TdInfoStruct {
panic!("Memory address must be 4K aligned!\n");
}

if sec.memory_data_size % PAGE_SIZE != 0
|| sec.memory_data_size == 0
|| sec.memory_data_size < sec.raw_data_size as u64
{
panic!("Memory data size must be 4K aligned and not less than raw data size and non zero!\n");
if sec.memory_data_size % PAGE_SIZE != 0 {
panic!("Memory data size must be 4K aligned!\n");
}

if sec.r#type >= TDX_METADATA_SECTION_TYPE_MAX {
Expand Down
54 changes: 51 additions & 3 deletions td-shim/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ pub const TDX_METADATA_SECTION_TYPE_PERM_MEM: u32 = 4;
pub const TDX_METADATA_SECTION_TYPE_PAYLOAD: u32 = 5;
/// Section type for kernel parameters.
pub const TDX_METADATA_SECTION_TYPE_PAYLOAD_PARAM: u32 = 6;
/// Section type for td info.
pub const TDX_METADATA_SECTION_TYPE_TD_INFO: u32 = 7;
/// Max Section type
pub const TDX_METADATA_SECTION_TYPE_MAX: u32 = 7;
pub const TDX_METADATA_SECTION_TYPE_MAX: u32 = 8;

pub const TDX_METADATA_SECTION_TYPE_STRS: [&str; TDX_METADATA_SECTION_TYPE_MAX as usize] = [
"BFV",
Expand All @@ -56,6 +58,7 @@ pub const TDX_METADATA_SECTION_TYPE_STRS: [&str; TDX_METADATA_SECTION_TYPE_MAX a
"PermMem",
"Payload",
"PayloadParam",
"TdInfo",
];

/// Attribute flags for BFV.
Expand Down Expand Up @@ -192,10 +195,15 @@ pub enum TdxMetadataError {

pub fn validate_sections(sections: &[TdxMetadataSection]) -> Result<(), TdxMetadataError> {
let mut bfv_cnt = 0;
let mut bfv_start = 0;
let mut bfv_end = 0;
let mut hob_cnt = 0;
let mut perm_mem_cnt = 0;
let mut payload_cnt = 0;
let mut payload_param_cnt = 0;
let mut td_info_cnt = 0;
let mut td_info_start = 0;
let mut td_info_end = 0;
let check_data_memory_fields =
|data_offset: u32, data_size: u32, memory_address: u64, memory_size: u64| -> bool {
if data_size == 0 && data_offset != 0 {
Expand Down Expand Up @@ -231,6 +239,9 @@ pub fn validate_sections(sections: &[TdxMetadataSection]) -> Result<(), TdxMetad
section.memory_data_size,
) {
return Err(TdxMetadataError::InvalidSection);
} else {
bfv_start = section.data_offset;
bfv_end = bfv_start + section.raw_data_size;
}
}

Expand Down Expand Up @@ -371,6 +382,26 @@ pub fn validate_sections(sections: &[TdxMetadataSection]) -> Result<(), TdxMetad
}
}

TDX_METADATA_SECTION_TYPE_TD_INFO => {
// A TD-Shim may have zero or one TdInfo. If present, it shall be included in BFV section.
if td_info_cnt == i32::MAX {
return Err(TdxMetadataError::InvalidSection);
}
td_info_cnt += 1;
if td_info_cnt > 1 {
return Err(TdxMetadataError::InvalidSection);
}
if section.attributes != 0 {
return Err(TdxMetadataError::InvalidSection);
}
if section.raw_data_size == 0 {
return Err(TdxMetadataError::InvalidSection);
} else {
td_info_start = section.data_offset;
td_info_end = td_info_start + section.raw_data_size;
}
}

_ => {
return Err(TdxMetadataError::InvalidSection);
}
Expand All @@ -391,6 +422,13 @@ pub fn validate_sections(sections: &[TdxMetadataSection]) -> Result<(), TdxMetad
return Err(TdxMetadataError::InvalidSection);
}

//TdInfo. If present, it shall be included in BFV section.
if td_info_cnt != 0 {
if td_info_start < bfv_start || td_info_start >= bfv_end || td_info_end > bfv_end {
return Err(TdxMetadataError::InvalidSection);
}
}

Ok(())
}

Expand Down Expand Up @@ -479,8 +517,9 @@ mod tests {
TdxMetadataSection::get_type_name(6).unwrap(),
"PayloadParam"
);
assert_eq!(TdxMetadataSection::get_type_name(7).unwrap(), "TdInfo");

assert!(TdxMetadataSection::get_type_name(7).is_none())
assert!(TdxMetadataSection::get_type_name(8).is_none());
}

#[test]
Expand All @@ -490,7 +529,7 @@ mod tests {
assert!(!validate_sections(&sections).is_ok());

// init sections include all types
let mut sections: [TdxMetadataSection; 6] = [TdxMetadataSection::default(); 6];
let mut sections: [TdxMetadataSection; 7] = [TdxMetadataSection::default(); 7];
// BFV
sections[0] = TdxMetadataSection {
data_offset: 0,
Expand Down Expand Up @@ -545,6 +584,15 @@ mod tests {
attributes: 0,
r#type: TDX_METADATA_SECTION_TYPE_PAYLOAD_PARAM,
};
// TdInfo
sections[6] = TdxMetadataSection {
data_offset: 0,
raw_data_size: 0x1000,
memory_address: 0,
memory_data_size: 0,
attributes: 0,
r#type: TDX_METADATA_SECTION_TYPE_TD_INFO,
};

assert!(validate_sections(&sections).is_ok());

Expand Down

0 comments on commit ac6fb26

Please sign in to comment.