Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve clippy warnings #345

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/attestation/src/attest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use crate::{
binding::get_quote as get_quote_inner, binding::init_heap, binding::verify_quote_integrity,
binding::AttestLibError, root_ca::ROOT_CA, Error,
binding::AttestLibError, root_ca::ROOT_CA, Error, TD_VERIFIED_REPORT_SIZE,
};
use alloc::{vec, vec::Vec};
use core::{alloc::Layout, ffi::c_void, ops::Range};
Expand All @@ -13,7 +13,6 @@ use tdx_tdcall::tdreport::*;
const TD_QUOTE_SIZE: usize = 0x2000;
const TD_REPORT_VERIFY_SIZE: usize = 1024;
const ATTEST_HEAP_SIZE: usize = 0x80000;
const TD_VERIFIED_REPORT_SIZE: usize = 734;

pub fn attest_init_heap() -> Option<usize> {
unsafe {
Expand Down
157 changes: 100 additions & 57 deletions src/attestation/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
//
// SPDX-License-Identifier: BSD-2-Clause-Patent

#[cfg(not(feature = "test"))]
pub(crate) use attest_lib_binding::*;
#[cfg(feature = "test")]
pub(crate) use null_binding::*;

#[allow(unused)]
#[repr(C)]
#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -32,64 +37,102 @@ pub(crate) enum AttestLibError {
InvalidRtmrIndex = 0x000b,
}

extern "C" {
/// Get MigTD's Quote by passing tdx_report.
/// Note: all IN/OUT memory should be managed by Caller
///
/// @param p_tdx_report [in] pointer to the input buffer for tdx_report. Must not be NULL.
/// @param tdx_report_size [in] length of p_tdx_report(in bytes), should be = TDX_REPORT_SIZE.
/// @param p_quote [in, out] pointer to the quote buffer. Must not be NULL.
/// @param p_quote_size [in, out] This function will place the size of the Quote, in
/// bytes, in the uint32_t pointed to by the
/// p_quote_size parameter. Must not be NULL.
/// @return Status code of the operation, one of:
/// - MIGTD_ATTEST_SUCCESS: Successfully generate the Quote
/// - MIGTD_ATTEST_ERROR_UNEXPECTED: An unexpected internal error occurred. E.g.
/// the parameter is incorrect, failed to get quote from QGS, heap memory allocation error,
/// the input (*p_quote_size) is not enough to place the real Quote, etc.
pub(crate) fn get_quote(
p_tdx_report: *const ::core::ffi::c_void,
tdx_report_size: u32,
p_quote: *mut ::core::ffi::c_void,
p_quote_size: *mut u32,
) -> AttestLibError;
}
#[cfg(not(feature = "test"))]
mod attest_lib_binding {
use super::*;

extern "C" {
/// Get MigTD's Quote by passing tdx_report.
/// Note: all IN/OUT memory should be managed by Caller
///
/// @param p_tdx_report [in] pointer to the input buffer for tdx_report. Must not be NULL.
/// @param tdx_report_size [in] length of p_tdx_report(in bytes), should be = TDX_REPORT_SIZE.
/// @param p_quote [in, out] pointer to the quote buffer. Must not be NULL.
/// @param p_quote_size [in, out] This function will place the size of the Quote, in
/// bytes, in the uint32_t pointed to by the
/// p_quote_size parameter. Must not be NULL.
/// @return Status code of the operation, one of:
/// - MIGTD_ATTEST_SUCCESS: Successfully generate the Quote
/// - MIGTD_ATTEST_ERROR_UNEXPECTED: An unexpected internal error occurred. E.g.
/// the parameter is incorrect, failed to get quote from QGS, heap memory allocation error,
/// the input (*p_quote_size) is not enough to place the real Quote, etc.
pub fn get_quote(
p_tdx_report: *const ::core::ffi::c_void,
tdx_report_size: u32,
p_quote: *mut ::core::ffi::c_void,
p_quote_size: *mut u32,
) -> AttestLibError;

/// Verify the integrity of MigTD's Quote and return td report of MigTD
/// Note: all IN/OUT memory should be managed by Caller
/// @param p_quote [in] pointer to the input buffer for td_quote
/// @param quote_size [in] length of p_quote(in bytes), should be the real size of MigTD td quote
/// @param p_quote_collateral [in] quote collateral that get from PCS by get_collateral
/// @param root_pub_key [in] pointer to Intel Root Public Key
/// @param root_pub_key_size [in] length of Intel Root Public Key(in bytes)
/// @param p_tdx_report_verify [in, out] pointer to the output buffer for tdx_report
/// @param p_tdx_report_verify_size [in, out], out_size should be = TDX_REPORT_SIZE
///
/// @return Status code of the operation, one of:
/// - MIGTD_ATTEST_SUCCESS
/// - MIGTD_ATTEST_ERROR_UNEXPECTED
pub fn verify_quote_integrity(
p_quote: *const ::core::ffi::c_void,
quote_size: u32,
root_pub_key: *const ::core::ffi::c_void,
root_pub_key_size: u32,
p_tdx_report_verify: *mut ::core::ffi::c_void,
p_tdx_report_verify_size: *mut u32,
) -> AttestLibError;

extern "C" {
/// Verify the integrity of MigTD's Quote and return td report of MigTD
/// Note: all IN/OUT memory should be managed by Caller
/// @param p_quote [in] pointer to the input buffer for td_quote
/// @param quote_size [in] length of p_quote(in bytes), should be the real size of MigTD td quote
/// @param p_quote_collateral [in] quote collateral that get from PCS by get_collateral
/// @param root_pub_key [in] pointer to Intel Root Public Key
/// @param root_pub_key_size [in] length of Intel Root Public Key(in bytes)
/// @param p_tdx_report_verify [in, out] pointer to the output buffer for tdx_report
/// @param p_tdx_report_verify_size [in, out], out_size should be = TDX_REPORT_SIZE
///
/// @return Status code of the operation, one of:
/// - MIGTD_ATTEST_SUCCESS
/// - MIGTD_ATTEST_ERROR_UNEXPECTED
pub(crate) fn verify_quote_integrity(
p_quote: *const ::core::ffi::c_void,
quote_size: u32,
root_pub_key: *const ::core::ffi::c_void,
root_pub_key_size: u32,
p_tdx_report_verify: *mut ::core::ffi::c_void,
p_tdx_report_verify_size: *mut u32,
) -> AttestLibError;
/// Allocate heap space for MigTD Attestation library internal use,
/// Must be called only once by MigTD before other attestation lib APIs
///
/// @param p_td_heap_base [in] the heap base address allocated by MigTD, the address should be aligned(0x1000).
/// @param td_heap_size [in] the capacity of the heap, should be multiples of 0x1000 (in bytes)
///
/// @return true: Successfully init heap for internal use.
/// @return false: Failed to init heap for internal use. E.g. the parameter is incorrect, etc.
pub fn init_heap(
p_td_heap_base: *const ::core::ffi::c_void,
td_heap_size: u32,
) -> AttestLibError;
}
}

extern "C" {
/// Allocate heap space for MigTD Attestation library internal use,
/// Must be called only once by MigTD before other attestation lib APIs
///
/// @param p_td_heap_base [in] the heap base address allocated by MigTD, the address should be aligned(0x1000).
/// @param td_heap_size [in] the capacity of the heap, should be multiples of 0x1000 (in bytes)
///
/// @return true: Successfully init heap for internal use.
/// @return false: Failed to init heap for internal use. E.g. the parameter is incorrect, etc.
pub(crate) fn init_heap(
p_td_heap_base: *const ::core::ffi::c_void,
td_heap_size: u32,
) -> AttestLibError;
#[cfg(feature = "test")]
mod null_binding {
use super::*;
use crate::TD_VERIFIED_REPORT_SIZE;

#[no_mangle]
pub unsafe extern "C" fn get_quote(
_p_tdx_report: *const ::core::ffi::c_void,
_tdx_report_size: u32,
_p_quote: *mut ::core::ffi::c_void,
_p_quote_size: *mut u32,
) -> AttestLibError {
*_p_quote_size = TD_VERIFIED_REPORT_SIZE as u32;
AttestLibError::Success
}

#[no_mangle]
pub unsafe extern "C" fn verify_quote_integrity(
_p_quote: *const ::core::ffi::c_void,
_quote_size: u32,
_root_pub_key: *const ::core::ffi::c_void,
_root_pub_key_size: u32,
_p_tdx_report_verify: *mut ::core::ffi::c_void,
_p_tdx_report_verify_size: *mut u32,
) -> AttestLibError {
AttestLibError::Success
}

#[no_mangle]
pub unsafe extern "C" fn init_heap(
_p_td_heap_base: *const ::core::ffi::c_void,
_td_heap_size: u32,
) -> AttestLibError {
AttestLibError::Success
}
}
13 changes: 5 additions & 8 deletions src/attestation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@

extern crate alloc;

#[cfg(not(test))]
mod ghci;

#[cfg(not(test))]
mod attest;
mod binding;
mod ghci;
mod root_ca;

#[cfg(not(test))]
mod attest;
#[cfg(not(test))]
pub use attest::*;
pub use root_ca::set_ca;

pub mod root_ca;
pub const TD_VERIFIED_REPORT_SIZE: usize = 734;

#[derive(Debug)]
pub enum Error {
Expand Down
14 changes: 7 additions & 7 deletions src/crypto/src/x509.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl<'a> Decode<'a> for Version<'a> {
}
}

impl<'a> Encode for Version<'a> {
impl Encode for Version<'_> {
fn encoded_len(&self) -> der::Result<der::Length> {
let len = self.0.encoded_len()?;
let explicit = Header::new(
Expand All @@ -234,7 +234,7 @@ impl<'a> Encode for Version<'a> {
}
}

impl<'a> Tagged for Version<'a> {
impl Tagged for Version<'_> {
fn tag(&self) -> Tag {
Tag::ContextSpecific {
constructed: true,
Expand Down Expand Up @@ -265,7 +265,7 @@ pub struct DistinguishedName<'a> {
value: AnyRef<'a>,
}

impl<'a> DerOrd for DistinguishedName<'a> {
impl DerOrd for DistinguishedName<'_> {
fn der_cmp(&self, other: &Self) -> der::Result<core::cmp::Ordering> {
Ok(self.attribute_type.cmp(&other.attribute_type))
}
Expand Down Expand Up @@ -317,7 +317,7 @@ impl<'a, const N: u8> Decode<'a> for UniqueIdentifier<'a, N> {
}
}

impl<'a, const N: u8> Encode for UniqueIdentifier<'a, N> {
impl<const N: u8> Encode for UniqueIdentifier<'_, N> {
fn encoded_len(&self) -> der::Result<der::Length> {
let len = self.0.encoded_len()?;
let explicit = Header::new(
Expand All @@ -344,7 +344,7 @@ impl<'a, const N: u8> Encode for UniqueIdentifier<'a, N> {
}
}

impl<'a, const N: u8> Tagged for UniqueIdentifier<'a, N> {
impl<const N: u8> Tagged for UniqueIdentifier<'_, N> {
fn tag(&self) -> Tag {
Tag::ContextSpecific {
constructed: true,
Expand Down Expand Up @@ -380,7 +380,7 @@ impl<'a> Decode<'a> for Extensions<'a> {
}
}

impl<'a> Encode for Extensions<'a> {
impl Encode for Extensions<'_> {
fn encoded_len(&self) -> der::Result<der::Length> {
let len = self.0.encoded_len()?;
let explicit = Header::new(
Expand All @@ -407,7 +407,7 @@ impl<'a> Encode for Extensions<'a> {
}
}

impl<'a> Tagged for Extensions<'a> {
impl Tagged for Extensions<'_> {
fn tag(&self) -> Tag {
Tag::ContextSpecific {
constructed: true,
Expand Down
1 change: 0 additions & 1 deletion src/devices/pci/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ pub fn find_device(vendor_id: u16, device_id: u16) -> Option<(u8, u8, u8)> {
}

/// Configure Space Access Mechanism #1

/// 32-bit I/O locations CONFIG_ADDRESS (0xCF8)
/// 0-7 register offset
/// 8-10 funtion number
Expand Down
3 changes: 1 addition & 2 deletions src/devices/virtio_serial/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,8 @@ impl VirtioSerial {
}
}

self.pop_used_rx(port_id).map_err(|e| {
self.pop_used_rx(port_id).inspect(|_| {
self.timer.reset_timeout();
e
})?;
if let Some(data) = Self::port_queue_pop(port_id) {
self.timer.reset_timeout();
Expand Down
1 change: 0 additions & 1 deletion src/devices/vsock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2021"
[dependencies]
atomic_refcell = "0.1.7"
byteorder = { version = "1.0", default-features = false }
cfg-if = "1.0"
conquer-once = { version = "0.3.2", default-features = false }
pci = { path = "../pci" }
virtio = { path = "../virtio", default-features = false }
Expand Down
18 changes: 8 additions & 10 deletions src/devices/vsock/src/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// SPDX-License-Identifier: BSD-2-Clause-Patent

use cfg_if::cfg_if;
use core::fmt::{self, Display};
use rust_std_stub::error;
#[cfg(feature = "vmcall-vsock")]
Expand All @@ -11,15 +10,14 @@ use tdx_tdcall::TdVmcallError;
use virtio::VirtioError;

mod event;
cfg_if! {
if #[cfg(feature = "vmcall-vsock")] {
mod vmcall;
pub use vmcall::*;
} else if #[cfg(feature = "virtio-vsock")] {
mod virtio_pci;
pub use virtio_pci::*;
}
}
#[cfg(feature = "vmcall-vsock")]
mod vmcall;
#[cfg(feature = "vmcall-vsock")]
pub use vmcall::*;
#[cfg(feature = "virtio-vsock")]
mod virtio_pci;
#[cfg(feature = "virtio-vsock")]
pub use virtio_pci::*;

type Result<T> = core::result::Result<T, VsockTransportError>;

Expand Down
10 changes: 4 additions & 6 deletions src/devices/vsock/src/transport/vmcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl VmcallVsock {

self.timer.set_timeout(timeout);
tdx::tdvmcall_service(command, response, VMCALL_VECTOR as u64, timeout)
.map_err(|e| VsockTransportError::Vmcall(e))?;
.map_err(VsockTransportError::Vmcall)?;

if !wait_for_event(&VMCALL_FLAG, self.timer.as_ref()) {
return Err(VsockTransportError::Timeout);
Expand Down Expand Up @@ -171,7 +171,7 @@ impl VmcallVsock {

self.timer.set_timeout(timeout);
tdx::tdvmcall_service(command, response, VMCALL_VECTOR as u64, timeout)
.map_err(|e| VsockTransportError::Vmcall(e))?;
.map_err(VsockTransportError::Vmcall)?;

// TO DO:
// Refactor the waiting logic
Expand Down Expand Up @@ -270,10 +270,9 @@ impl VsockTransport for VmcallVsock {

// Request sending out the message
self.vmcall_service_migtd_send(command, response, hdr, buf, timeout)
.map(|res| {
.inspect(|_| {
self.free_dma(command);
self.free_dma(response);
res
})
}

Expand All @@ -292,10 +291,9 @@ impl VsockTransport for VmcallVsock {
};

self.vmcall_service_migtd_receive(command, response, &stream.addr(), timeout)
.map(|res| {
.inspect(|_| {
self.free_dma(command);
self.free_dma(response);
res
})
}

Expand Down
3 changes: 3 additions & 0 deletions src/migtd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ edition = "2021"
name = "migtd"
required-features = ["main"]

[dev-dependencies]
attestation = { path = "../attestation", default-features = false, features = ["test"] }

[dependencies]
bitfield = "0.13.2"
anyhow = { version = "1.0.68", default-features = false }
Expand Down
Loading
Loading