Skip to content

Commit

Permalink
Create td-shim-interface crate for publishig
Browse files Browse the repository at this point in the history
Signed-off-by: OuyangHang33 <[email protected]>
  • Loading branch information
OuyangHang33 committed Mar 4, 2024
1 parent 36a9200 commit 814a8c7
Show file tree
Hide file tree
Showing 59 changed files with 239 additions and 65 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"tests/test-td-paging",
"tests/test-td-payload",
"xtask",
"td-shim-interface",
]

# the profile used for debug build of `td-shim` and `td-payload`
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ else
export BUILD_TYPE_FLAG=
endif

GENERIC_LIB_CRATES = td-layout td-logger td-uefi-pi td-loader cc-measurement
GENERIC_LIB_CRATES = td-layout td-logger td-shim-interface td-loader cc-measurement
NIGHTLY_LIB_CRATES = td-exception td-paging tdx-tdcall
SHIM_CRATES = td-shim td-payload
TEST_CRATES = test-td-exception test-td-paging
Expand Down
2 changes: 1 addition & 1 deletion sh_script/fuzzing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ readonly script_name=${0##*/}

fuzz_folder=(
"td-loader"
"td-uefi-pi"
"td-shim-interface/src"
"td-shim"
)

Expand Down
2 changes: 1 addition & 1 deletion sh_script/rudra.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ paths=(
"td-paging"
"td-payload"
"td-shim"
"td-uefi-pi"
"td-shim-interface/src"
"td-shim-tools"
"tdx-tdcall"
)
Expand Down
2 changes: 1 addition & 1 deletion td-layout/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ edition = "2018"
[dependencies]
scroll = { version = "0.10", default-features = false, features = ["derive"]}
log = "0.4.13"
td-uefi-pi = { path = "../td-uefi-pi" }
td-shim-interface = { path = "../td-shim-interface" }

[dev-dependencies]
memoffset = "0.6"
2 changes: 1 addition & 1 deletion td-payload/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
spin = "0.9"
td-logger = { path = "../td-logger" }
td-shim = { path = "../td-shim", default-features = false }
td-uefi-pi = { path = "../td-uefi-pi" }
td-shim-interface = { path = "../td-shim-interface" }
td-exception = { path = "../td-exception" }
td-paging = { path = "../td-paging" }
x86 = "0.47.0"
Expand Down
2 changes: 1 addition & 1 deletion td-payload/src/acpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use alloc::vec::Vec;
use scroll::Pread;
use spin::Once;
use td_shim::TD_ACPI_TABLE_HOB_GUID;
use td_uefi_pi::{
use td_shim_interface::td_uefi_pi::{
hob as hob_lib,
pi::hob::{GuidExtension, Header, HOB_TYPE_END_OF_HOB_LIST, HOB_TYPE_GUID_EXTENSION},
};
Expand Down
4 changes: 2 additions & 2 deletions td-payload/src/bin/example/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use td_payload as _;
use td_payload::println;
use td_shim::e820::{E820Entry, E820Type};
use td_shim::{TD_ACPI_TABLE_HOB_GUID, TD_E820_TABLE_HOB_GUID};
use td_uefi_pi::hob;
use td_uefi_pi::pi;
use td_shim_interface::td_uefi_pi::hob;
use td_shim_interface::td_uefi_pi::pi;
use zerocopy::FromBytes;

#[macro_use]
Expand Down
2 changes: 1 addition & 1 deletion td-payload/src/hob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use core::mem::size_of;
use scroll::Pread;
use spin::Once;
use td_uefi_pi::{
use td_shim_interface::td_uefi_pi::{
hob::check_hob_integrity,
pi::hob::{HandoffInfoTable, HOB_TYPE_HANDOFF},
};
Expand Down
2 changes: 1 addition & 1 deletion td-payload/src/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use td_shim::{
e820::{E820Entry, E820Type},
TD_E820_TABLE_HOB_GUID,
};
use td_uefi_pi::{
use td_shim_interface::td_uefi_pi::{
hob as hob_lib,
pi::hob::{GuidExtension, Header, HOB_TYPE_END_OF_HOB_LIST, HOB_TYPE_GUID_EXTENSION},
};
Expand Down
18 changes: 18 additions & 0 deletions td-shim-interface/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "td-shim-interface"
version = "0.1.0"
license = "BSD-2-Clause-Patent"
description = "TD-shim metadata data structures and related functions. UEFI Platform Initializaiton data structures and accessors"

edition = "2018"
homepage = "https://github.com/confidential-containers/td-shim"
repository = "https://github.com/confidential-containers/td-shim"
readme = "README.md"
keywords = ["td-shim", "TDX", "intel"]

[dependencies]
r-efi = "3.2.0"
scroll = { version = "0.10", default-features = false, features = ["derive"] }
zerocopy = { version = "0.7.31", features = ["derive"] }

log = "0.4.13"
35 changes: 35 additions & 0 deletions td-shim-interface/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fconfidential-containers%2Ftd-shim.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fconfidential-containers%2Ftd-shim?ref=badge_shield)
# TD-shim-interface - Confidential Containers Shim Firmware Interface

## Documents

* [TD-Shim specification](doc/tdshim_spec.md)

* Introduction [PDF](doc/td-shim-introduction.pdf) and [conference talk](https://fosdem.org/2023/schedule/event/cc_online_rust/)

## Introduction

This td-shim-interface is to support user for creating data structures and functions required for td-shim, such as TdxMetadataDescriptor and TdxMetadataSection.
Td-uefi-pi is used for UEFI Platform Initializaiton data structures and accessors.

To import the data structure of metadata, TD HOB and related function, such as:
```
use td_shim_interface::{TD_ACPI_TABLE_HOB_GUID, TD_E820_TABLE_HOB_GUID, TD_PAYLOAD_INFO_HOB_GUID};
use td_shim_interface::PayloadInfo;
use td_shim_interface::acpi;
use td_shim_interface::td_uefi_pi::{hob, pi, pi::guid}
```

This is a Shim Firmware to support [Intel TDX](https://software.intel.com/content/www/us/en/develop/articles/intel-trust-domain-extensions.html).

The API specification is at [td-shim specification](doc/tdshim_spec.md).

The secure boot specification for td-shim is at [secure boot specification](doc/secure_boot.md)

The design is at [td-shim design](doc/design.md).

The threat model analysis is at [td-shim threat model](doc/threat_model.md).


## License
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fconfidential-containers%2Ftd-shim.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fconfidential-containers%2Ftd-shim?ref=badge_large)
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use core::mem::size_of;
use r_efi::efi::Guid;
use std::vec::Vec;
use td_uefi_pi::{fv, hob, pi};
use td_shim_interface::td_uefi_pi::{fv, hob, pi};

const EFI_END_OF_HOB_LIST_OFFSET: usize = 48;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
134 changes: 134 additions & 0 deletions td-shim-interface/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright (c) 2022 Alibaba Cloud
//
// SPDX-License-Identifier: BSD-2-Clause-Patent

#![no_std]

use scroll::{Pread, Pwrite};

pub mod acpi;
pub mod metadata;
pub mod td_uefi_pi;

use td_uefi_pi::pi::{self, guid};

// This GUID is used for ACPI GUID Extension HOB
// Please refer to:
// https://github.com/confidential-containers/td-shim/blob/main/doc/tdshim_spec.md#acpi-guid-extension-hob
pub const TD_ACPI_TABLE_HOB_GUID: guid::Guid = guid::Guid::from_fields(
0x6a0c5870,
0xd4ed,
0x44f4,
[0xa1, 0x35, 0xdd, 0x23, 0x8b, 0x6f, 0xc, 0x8d],
);

// This GUID is used for TD Payload Info GUID Extension HOB
// Please refer to:
// https://github.com/confidential-containers/td-shim/blob/main/doc/tdshim_spec.md#td-payload-info-guid-extension-hob
pub const TD_PAYLOAD_INFO_HOB_GUID: guid::Guid = guid::Guid::from_fields(
0xb96fa412,
0x461f,
0x4be3,
[0x8c, 0xd, 0xad, 0x80, 0x5a, 0x49, 0x7a, 0xc0],
);

// This GUID is used for E820 Memory Map GUID Extension HOB
// Please refer to:
// https://github.com/confidential-containers/td-shim/blob/main/doc/tdshim_spec.md#e820-memory-map-guid-extension-hob
pub const TD_E820_TABLE_HOB_GUID: pi::guid::Guid = pi::guid::Guid::from_fields(
0x8f8072ea,
0x3486,
0x4b47,
[0x86, 0xa7, 0x23, 0x53, 0xb8, 0x8a, 0x87, 0x73],
);

#[repr(u32)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TdPayloadInfoHobType {
/// Payload Binary is a PE/COFF or ELF executable image as payload.
///
/// Entrypoint can be found by parsing the image header. This type image does not follow
/// Linux boot protocol. A payload HOB is used to pass data from TdShim to payload.
ExecutablePayload = 0,

/// Payload Binary is bzImage, follow Linux boot protocol.
///
/// The first 512 bytes are boot_param. (zero page). The entrypoint is start address of loaded
/// 64bit Linux kernel plus 0x200
BzImage,

/// Payload Binary is VMM loaded vmLinux, follow Linux boot protocol.
///
/// The entrypoint is defined at HOB_PAYLOAD_INFO_TABLE.Entrypoint.
RawVmLinux,

/// Unknown Image type
UnknownImage = u32::MAX,
}

impl From<&TdPayloadInfoHobType> for u32 {
fn from(v: &TdPayloadInfoHobType) -> Self {
*v as u32
}
}

impl From<u32> for TdPayloadInfoHobType {
fn from(v: u32) -> Self {
match v {
0 => TdPayloadInfoHobType::ExecutablePayload,
1 => TdPayloadInfoHobType::BzImage,
2 => TdPayloadInfoHobType::RawVmLinux,
_ => TdPayloadInfoHobType::UnknownImage,
}
}
}

#[repr(C)]
#[derive(Default, Clone, Copy, Pread, Pwrite)]
pub struct PayloadInfo {
pub image_type: u32,
pub reserved: u32,
pub entry_point: u64,
}

/// Write three bytes from an integer value into the buffer.
pub fn write_u24(data: u32, buf: &mut [u8; 3]) {
assert!(data <= 0xffffff);
buf[0] = (data & 0xFF) as u8;
buf[1] = ((data >> 8) & 0xFF) as u8;
buf[2] = ((data >> 16) & 0xFF) as u8;
}

// To protect against speculative attacks, place the LFENCE instruction after the range
// check and branch, but before any code that consumes the checked value.
pub fn speculation_barrier() {
unsafe { core::arch::asm!("lfence") }
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_tdpayload_info_hob_type() {
assert_eq!(
TdPayloadInfoHobType::from(0),
TdPayloadInfoHobType::ExecutablePayload
);
assert_eq!(TdPayloadInfoHobType::from(1), TdPayloadInfoHobType::BzImage);
assert_eq!(
TdPayloadInfoHobType::from(2),
TdPayloadInfoHobType::RawVmLinux
);
assert_eq!(
TdPayloadInfoHobType::from(3),
TdPayloadInfoHobType::UnknownImage
);
}

#[test]
fn test_write_u24() {
let mut buf: [u8; 3] = [0; 3];
write_u24(0xffffff, &mut buf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

extern crate alloc;

use crate::td_uefi_pi::pi::guid::Guid;
use alloc::string::String;
use core::{ptr::slice_from_raw_parts, str::FromStr};
use scroll::{Pread, Pwrite};
use td_uefi_pi::pi::guid::Guid;

/// TDX Metadata GUID defined in td-shim specification
pub const TDX_METADATA_GUID_STR: &str = "E9EAF9F3-168E-44D5-A8EB-7F4D8738F6AE";
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
//!
//! Constants and data structures defined by [UEFI PI Spec] are hosted by [crate::pi], functions
//! to access them are hosted by [crate::fv] and [crate::hob].
#![no_std]

pub mod fv;
pub mod hob;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion td-shim-tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ regex = "1"
scroll = { version = "0.10", default-features = false, features = ["derive"]}
td-layout = { path = "../td-layout" }
td-shim = { path = "../td-shim", default-features = false }
td-uefi-pi = { path = "../td-uefi-pi" }
td-shim-interface = { path = "../td-shim-interface" }
cfg-if = "1.0"

anyhow = { version = "1.0.68", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion td-shim-tools/src/bin/td-shim-checker/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use log::{error, LevelFilter};
use std::str::FromStr;
use std::vec::Vec;
use std::{env, io};
use td_shim::metadata::{TdxMetadataDescriptor, TdxMetadataSection};
use td_shim_interface::metadata::{TdxMetadataDescriptor, TdxMetadataSection};
use td_shim_tools::loader::TdShimLoader;
use td_shim_tools::read_file::read_from_binary_file;

Expand Down
2 changes: 1 addition & 1 deletion td-shim-tools/src/bin/td-shim-enroll/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use std::path::PathBuf;
use std::str::FromStr;
use std::vec::Vec;
use std::{env, io, path::Path};
use td_shim_interface::td_uefi_pi::pi::guid;
use td_shim_tools::enroller::{create_key_file, enroll_files, FirmwareRawFile};
use td_shim_tools::InputData;
use td_uefi_pi::pi::guid;
const TDSHIM_SB_NAME: &str = "final.sb.bin";

struct Config {
Expand Down
4 changes: 2 additions & 2 deletions td-shim-tools/src/enroller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use td_shim::secure_boot::{
PUBKEY_FILE_STRUCT_VERSION_V1, PUBKEY_HASH_ALGORITHM_SHA384,
};
use td_shim::write_u24;
use td_uefi_pi::pi::fv::{
use td_shim_interface::td_uefi_pi::pi::fv::{
FIRMWARE_FILE_SYSTEM3_GUID, FVH_REVISION, FVH_SIGNATURE, FV_FILETYPE_RAW,
};

Expand Down Expand Up @@ -298,7 +298,7 @@ mod test {
use std::str::FromStr;

use super::*;
use td_uefi_pi::pi::guid;
use td_shim_interface::td_uefi_pi::pi::guid;

fn read_u24(data: &[u8]) -> u32 {
let mut num = data[0] as u32;
Expand Down
4 changes: 2 additions & 2 deletions td-shim-tools/src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ use td_shim::fv::{
FvFfsFileHeader, FvFfsSectionHeader, FvHeader, IplFvFfsHeader, IplFvFfsSectionHeader,
IplFvHeader,
};
use td_shim::metadata::{TdxMetadataGuid, TdxMetadataPtr};
use td_shim::reset_vector::{ResetVectorHeader, ResetVectorParams};
use td_shim::write_u24;
use td_uefi_pi::pi::fv::{
use td_shim_interface::metadata::{TdxMetadataGuid, TdxMetadataPtr};
use td_shim_interface::td_uefi_pi::pi::fv::{
FfsFileHeader, FVH_REVISION, FVH_SIGNATURE, FV_FILETYPE_DXE_CORE, FV_FILETYPE_SECURITY_CORE,
SECTION_PE32,
};
Expand Down
2 changes: 1 addition & 1 deletion td-shim-tools/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use core::convert::TryInto;
use log::error;
use scroll::Pread;
use td_shim::metadata::{
use td_shim_interface::metadata::{
self, TdxMetadataDescriptor, TdxMetadataGuid, TdxMetadataSection, TDX_METADATA_DESCRIPTOR_LEN,
TDX_METADATA_GUID_LEN, TDX_METADATA_OFFSET, TDX_METADATA_SECTION_LEN,
};
Expand Down
Loading

0 comments on commit 814a8c7

Please sign in to comment.