-
Notifications
You must be signed in to change notification settings - Fork 27
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
Azure detection + attestation report fetch #16
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3af8a24
verify: Fix clippy warning
tylerfanelli c685f16
Reformat with cargo fmt
tylerfanelli 92c21b6
hyperv: Detect if in a Hyper-V SEV-SNP guest
tylerfanelli e704493
hyperv: Fetch attestation report from Azure vTPM
tylerfanelli b8983b4
hyperv: Check for hypervisor guest + Hyper-V functions
tylerfanelli c7ad0fe
hyperv: Require VMPL 0 for attestation report
tylerfanelli 34b0e6b
report: Add --platform flag for Hyper-V
tylerfanelli deea7e3
docs: Document report --platform flag
tylerfanelli 49a6f10
azure: Use mutable variable instead of consistently recreating
tylerfanelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,4 @@ hex = "0.4" | |
x509-parser = { version="^0.14", features=["verify"] } | ||
asn1-rs = "*" | ||
rand = "*" | ||
tss-esapi = "7.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use super::*; | ||
|
||
use std::arch::x86_64::__cpuid; | ||
|
||
const CPUID_GET_HIGHEST_FUNCTION: u32 = 0x80000000; | ||
const CPUID_PROCESSOR_INFO_AND_FEATURE_BITS: u32 = 0x1; | ||
|
||
const CPUID_FEATURE_HYPERVISOR: u32 = 1 << 31; | ||
|
||
const CPUID_HYPERV_SIG: &str = "Microsoft Hv"; | ||
const CPUID_HYPERV_VENDOR_AND_MAX_FUNCTIONS: u32 = 0x40000000; | ||
const CPUID_HYPERV_FEATURES: u32 = 0x40000003; | ||
const CPUID_HYPERV_MIN: u32 = 0x40000005; | ||
const CPUID_HYPERV_MAX: u32 = 0x4000ffff; | ||
const CPUID_HYPERV_ISOLATION: u32 = 1 << 22; | ||
const CPUID_HYPERV_CPU_MANAGEMENT: u32 = 1 << 12; | ||
const CPUID_HYPERV_ISOLATION_CONFIG: u32 = 0x4000000C; | ||
const CPUID_HYPERV_ISOLATION_TYPE_MASK: u32 = 0xf; | ||
const CPUID_HYPERV_ISOLATION_TYPE_SNP: u32 = 2; | ||
|
||
pub fn present() -> bool { | ||
let mut cpuid = unsafe { __cpuid(CPUID_PROCESSOR_INFO_AND_FEATURE_BITS) }; | ||
if (cpuid.ecx & CPUID_FEATURE_HYPERVISOR) == 0 { | ||
return false; | ||
} | ||
|
||
cpuid = unsafe { __cpuid(CPUID_GET_HIGHEST_FUNCTION) }; | ||
if cpuid.eax < CPUID_HYPERV_VENDOR_AND_MAX_FUNCTIONS { | ||
return false; | ||
} | ||
|
||
cpuid = unsafe { __cpuid(CPUID_HYPERV_VENDOR_AND_MAX_FUNCTIONS) }; | ||
if cpuid.eax < CPUID_HYPERV_MIN || cpuid.eax > CPUID_HYPERV_MAX { | ||
return false; | ||
} | ||
|
||
let mut sig: Vec<u8> = vec![]; | ||
sig.append(&mut cpuid.ebx.to_le_bytes().to_vec()); | ||
sig.append(&mut cpuid.ecx.to_le_bytes().to_vec()); | ||
sig.append(&mut cpuid.edx.to_le_bytes().to_vec()); | ||
|
||
if sig != CPUID_HYPERV_SIG.as_bytes() { | ||
return false; | ||
} | ||
|
||
cpuid = unsafe { __cpuid(CPUID_HYPERV_FEATURES) }; | ||
|
||
let isolated: bool = (cpuid.ebx & CPUID_HYPERV_ISOLATION) != 0; | ||
let managed: bool = (cpuid.ebx & CPUID_HYPERV_CPU_MANAGEMENT) != 0; | ||
|
||
if !isolated || managed { | ||
return false; | ||
} | ||
|
||
cpuid = unsafe { __cpuid(CPUID_HYPERV_ISOLATION_CONFIG) }; | ||
let mask = cpuid.ebx & CPUID_HYPERV_ISOLATION_TYPE_MASK; | ||
let snp = CPUID_HYPERV_ISOLATION_TYPE_SNP; | ||
|
||
if mask != snp { | ||
return false; | ||
} | ||
|
||
true | ||
} | ||
|
||
pub mod report { | ||
use super::*; | ||
|
||
use anyhow::{anyhow, Context}; | ||
use serde::{Deserialize, Serialize}; | ||
use sev::firmware::guest::AttestationReport; | ||
use tss_esapi::{ | ||
abstraction::nv, | ||
handles::NvIndexTpmHandle, | ||
interface_types::{resource_handles::NvAuth, session_handles::AuthSession}, | ||
tcti_ldr::{DeviceConfig, TctiNameConf}, | ||
}; | ||
|
||
const VTPM_HCL_REPORT_NV_INDEX: u32 = 0x01400001; | ||
|
||
#[repr(C)] | ||
#[derive(Deserialize, Serialize, Debug, Clone, Copy)] | ||
struct Hcl { | ||
rsv1: [u32; 8], | ||
report: AttestationReport, | ||
rsv2: [u32; 5], | ||
} | ||
|
||
pub fn get(vmpl: u32) -> Result<AttestationReport> { | ||
if vmpl > 0 { | ||
return Err(anyhow!("Azure vTPM attestation report requires VMPL 0")); | ||
} | ||
let bytes = tpm2_read().context("unable to read attestation report bytes from vTPM")?; | ||
|
||
hcl_report(&bytes) | ||
} | ||
|
||
fn tpm2_read() -> Result<Vec<u8>> { | ||
let handle = NvIndexTpmHandle::new(VTPM_HCL_REPORT_NV_INDEX) | ||
.context("unable to initialize TPM handle")?; | ||
let mut ctx = tss_esapi::Context::new(TctiNameConf::Device(DeviceConfig::default()))?; | ||
ctx.set_sessions((Some(AuthSession::Password), None, None)); | ||
|
||
nv::read_full(&mut ctx, NvAuth::Owner, handle) | ||
.context("unable to read non-volatile vTPM data") | ||
} | ||
|
||
fn hcl_report(bytes: &[u8]) -> Result<AttestationReport> { | ||
let hcl: Hcl = | ||
bincode::deserialize(bytes).context("unable to deserialize bytes from vTPM")?; | ||
|
||
Ok(hcl.report) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to check for Hyper-V every single time the tool is used? Why wouldn't we just check whenever people request a report?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There will be parity with other commands.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, I think it would make more sense to only check for Hyper-V when we are potentially going to interact with the PSP. All other functionality from the tool does not rely on architecture. The only other locations I could potentially think this check would be useful is for enabling/disabling
--random
and/or extended certs, as Hyper-V disallows the use of these flagsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An example I currently see is certs. In non-Azure, certs are fetched from the KDS. However, in Azure they are fetched from another MS-defined endpoint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense