Skip to content

Commit 4544d10

Browse files
committed
openhcl_tdisp: authorize VBS device DMA
1 parent 35dfc23 commit 4544d10

18 files changed

Lines changed: 355 additions & 16 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5808,6 +5808,7 @@ version = "0.0.0"
58085808
dependencies = [
58095809
"anyhow",
58105810
"futures",
5811+
"guid",
58115812
"hcl",
58125813
"hvdef",
58135814
"inspect",

‎Guide/src/reference/architecture/openhcl/product_policy.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ it only if the manifest sets `static_command_line` to false and OpenHCL trusts
8383
the host: the guest isn't isolated, or the image's measured command line enables
8484
confidential debugging. Otherwise only the measured command line sets it.
8585

86+
`allow_unattested_devices` lets the relay offer devices that can't be attested
87+
with TDISP, for hosts that don't support it. It defaults to `false`; only the
88+
SIVM policy sets it. With it set, OpenHCL sends the host no TDISP command and
89+
checks no TDI report, so nothing attests the device's identity or
90+
configuration. On VBS, OpenHCL authorizes the device's DMA when the guest first
91+
enables its MMIO and doesn't revoke it while the device is assigned.
92+
8693
The catalog defines each device once, with any of `vendor_id`, `device_id`,
8794
`revision_id`, `base_class`, `sub_class`, and `prog_if`. A field that is left
8895
out matches any value, but an allowed device must set at least one:
@@ -119,5 +126,5 @@ in `vm/loader/manifests/policies/devices.toml`:
119126
|---|---|---|
120127
| `openhcl-x64-cvm-{dev,release}.toml` | `azure` | NVMe, MANA, and the Azure Integrated HSM; `default_on` |
121128
| `openhcl-x64-{dev,release}.toml`, `openhcl-x64-direct-{dev,release}.toml`, `openhcl-aarch64-{dev,release}.toml` | `azure` | The same devices; `default_off` |
122-
| `openhcl-x64-cvm-sivm-{dev,release}.toml` | `sivm` | A GPU; `must_be_on` |
129+
| `openhcl-x64-cvm-sivm-{dev,release}.toml` | `sivm` | A GPU; `must_be_on`; `allow_unattested_devices` |
123130
| `openhcl-x64-test-sivm.toml` | `sivm` | None |

‎openhcl/hcl/src/ioctl.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,36 @@ impl MshvHvcall {
14261426
Err(status.result().unwrap_err())
14271427
}
14281428
}
1429+
1430+
/// Asks the vTSM (virtual TSM) to authorize, or with `authorize` false to
1431+
/// unauthorize, DMA for the device the host assigned with
1432+
/// `logical_device_id`, on a VBS-isolated partition. The hypervisor denies
1433+
/// DMA from every device assigned to the partition until it's authorized.
1434+
///
1435+
/// [`HypercallCode::HvCallVbsVmCallAuthorizeDeviceIo`] must be allowed.
1436+
pub fn authorize_device_io(
1437+
&self,
1438+
logical_device_id: u64,
1439+
authorize: bool,
1440+
) -> Result<(), HvError> {
1441+
let input = hvdef::hypercall::VbsVmCallAuthorizeDeviceIo {
1442+
logical_device_id,
1443+
authorize: authorize.into(),
1444+
reserved: [0; 7],
1445+
};
1446+
1447+
// SAFETY: The input is the correct type for this hypercall, which has
1448+
// no output.
1449+
unsafe {
1450+
self.hvcall(
1451+
HypercallCode::HvCallVbsVmCallAuthorizeDeviceIo,
1452+
&input,
1453+
&mut (),
1454+
)
1455+
.expect("submitting hypercall should not fail")
1456+
.result()
1457+
}
1458+
}
14291459
}
14301460

14311461
/// The HCL device and collection of fds.

‎openhcl/openhcl_tdisp/Cargo.toml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ rust-version.workspace = true
77
edition.workspace = true
88

99
[dependencies]
10+
guid.workspace = true
1011
hvdef.workspace = true
1112
inspect.workspace = true
1213
memory_range.workspace = true
@@ -20,7 +21,7 @@ anyhow.workspace = true
2021
futures.workspace = true
2122
tracing.workspace = true
2223

23-
# Both validators talk to /dev/mshv_vtl, and SEV-TIO also to /dev/sev-guest.
24+
# The platform validators talk to mshv, and SEV-TIO also to /dev/sev-guest.
2425
[target.'cfg(target_os = "linux")'.dependencies]
2526
hcl.workspace = true
2627
sev_guest_device.workspace = true

‎openhcl/openhcl_tdisp/src/client.rs‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,23 @@ impl TdispClient {
7979
/// assigned to, which decides the guest protocol to negotiate.
8080
/// * `target_vtl` - The VTL the device is assigned to.
8181
/// * `bar_masks` - Which BARs the device implements.
82+
/// * `allow_unattested_devices` - Allow the device without TDISP attestation,
83+
/// skipping the steps that need it.
8284
pub fn new(
8385
transport: Box<dyn TdispCommandTransport>,
8486
resource_validator: Arc<dyn TdispResourceValidationInterface>,
8587
isolation_type: IsolationType,
8688
target_vtl: Vtl,
8789
bar_masks: [bool; 6],
90+
allow_unattested_devices: bool,
8891
) -> Self {
8992
Self(futures::lock::Mutex::new(TdispClientState::new(
9093
transport,
9194
resource_validator,
9295
isolation_type,
9396
target_vtl,
9497
bar_masks,
98+
allow_unattested_devices,
9599
)))
96100
}
97101

@@ -323,6 +327,9 @@ struct TdispClientState {
323327
/// Platform hooks used to gate attestation and unblock device resources.
324328
#[inspect(skip)]
325329
resource_validator: Arc<dyn TdispResourceValidationInterface>,
330+
/// Allow the device without TDISP attestation, skipping the steps that
331+
/// need it.
332+
allow_unattested_devices: bool,
326333
}
327334

328335
impl TdispClientState {
@@ -332,6 +339,7 @@ impl TdispClientState {
332339
isolation_type: IsolationType,
333340
target_vtl: Vtl,
334341
bar_masks: [bool; 6],
342+
allow_unattested_devices: bool,
335343
) -> Self {
336344
Self {
337345
transport,
@@ -347,6 +355,7 @@ impl TdispClientState {
347355
target_vtl,
348356
bar_masks,
349357
resource_validator,
358+
allow_unattested_devices,
350359
}
351360
}
352361

@@ -496,6 +505,11 @@ impl TdispClientState {
496505
/// state exists to keep the device from modifying its resources between
497506
/// the bind and attestation.
498507
async fn bind_interface(&mut self) -> anyhow::Result<()> {
508+
if self.allow_unattested_devices {
509+
self.mutable_state.update_tdi_state(TdispTdiState::Locked);
510+
return Ok(());
511+
}
512+
499513
let state_before = self.tdi_state();
500514
let res = self
501515
.send_command(crate::new_bind_command(self.transport.bus_device_id()))
@@ -529,6 +543,11 @@ impl TdispClientState {
529543
/// Start a bound device, transitioning the TDI from Locked to Run. This is
530544
/// the point from which resources can be accepted into the guest context.
531545
async fn start_device(&mut self) -> anyhow::Result<()> {
546+
if self.allow_unattested_devices {
547+
self.mutable_state.update_tdi_state(TdispTdiState::Run);
548+
return Ok(());
549+
}
550+
532551
let state_before = self.tdi_state();
533552
let res = self
534553
.send_command(crate::new_start_tdi_command(self.transport.bus_device_id()))
@@ -749,6 +768,11 @@ impl TdispClientState {
749768
/// with the state the host advertised after unbind, the function will
750769
/// panic.
751770
async fn unbind(&mut self, reason: TdispGuestUnbindReason) {
771+
if self.allow_unattested_devices {
772+
// Nothing was bound on the host, and DMA stays unblocked.
773+
return;
774+
}
775+
752776
let validator = self.resource_validator.clone();
753777
let device_id = self.mutable_state.guest_device_id;
754778

@@ -866,6 +890,10 @@ impl TdispClientState {
866890
"querying TDISP capabilities for device given VM isolation type"
867891
);
868892

893+
if self.allow_unattested_devices {
894+
return Ok(TdispDeviceInterfaceInfo::default());
895+
}
896+
869897
let target_protocol = match self.isolation_type {
870898
IsolationType::Snp => TdispGuestProtocolType::AmdSevTioV1,
871899
IsolationType::Tdx => TdispGuestProtocolType::IntelTdxConnectV1,
@@ -1076,6 +1104,10 @@ impl TdispClientState {
10761104
message: format!("tdisp_attest_device: post-start validation failed: {}", e),
10771105
})?;
10781106

1107+
if self.allow_unattested_devices {
1108+
return Ok(());
1109+
}
1110+
10791111
// Fetch and save the TDI interface report so callers can inspect the
10801112
// attested device's reported capabilities and MMIO ranges.
10811113
let tdi_report = self.get_tdi_report().await.context(
@@ -1240,6 +1272,22 @@ impl TdispClientState {
12401272
return Ok(());
12411273
}
12421274

1275+
if self.allow_unattested_devices {
1276+
// There's no TDI report to classify the BAR by, so only unblock DMA.
1277+
if !self.mutable_state.dma_unblocked {
1278+
let device_id = self
1279+
.mutable_state
1280+
.guest_device_id
1281+
.id()
1282+
.context("tdisp_on_mmio_reconfigured: no TDI device id is known")?;
1283+
self.resource_validator
1284+
.tdisp_unblock_dma(self.target_vtl, device_id)
1285+
.context("tdisp_on_mmio_reconfigured: failed to unblock DMA")?;
1286+
self.mutable_state.dma_unblocked = true;
1287+
}
1288+
return Ok(());
1289+
}
1290+
12431291
if self.mutable_state.validated_mmio_bars.contains_key(&bar_id) {
12441292
tracing::debug!(
12451293
bar_id,
@@ -1405,6 +1453,7 @@ mod tests {
14051453
IsolationType::None,
14061454
Vtl::Vtl0,
14071455
present_bars,
1456+
false,
14081457
)
14091458
}
14101459

@@ -1569,4 +1618,31 @@ mod tests {
15691618
);
15701619
assert_eq!(dma, TdispResourceIsolation::Private);
15711620
}
1621+
1622+
/// With the host checks bypassed, attestation sends the host no TDISP
1623+
/// command, and DMA is unblocked and stays unblocked across an unbind.
1624+
#[test]
1625+
fn bypassed_host_checks_unblock_dma_without_the_host() {
1626+
let validator = Arc::new(crate::noop::TdispNoopResourceValidator::new());
1627+
let mut state = TdispClientState::new(
1628+
Box::new(PanickingTransport),
1629+
validator.clone(),
1630+
IsolationType::Vbs,
1631+
Vtl::Vtl0,
1632+
[true, true, false, false, false, false],
1633+
true,
1634+
);
1635+
futures::executor::block_on(async {
1636+
let info = state.query_capabilities().await.unwrap();
1637+
state.attest(info).await.unwrap();
1638+
assert_eq!(state.tdi_state(), TdispTdiState::Run);
1639+
state
1640+
.on_mmio_reconfigured(0, 0x1000_0000, 0x1000)
1641+
.await
1642+
.unwrap();
1643+
assert!(validator.dma_unblocked());
1644+
state.unbind(TdispGuestUnbindReason::Graceful).await;
1645+
assert!(validator.dma_unblocked());
1646+
});
1647+
}
15721648
}

‎openhcl/openhcl_tdisp/src/lib.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub mod noop;
1414
mod sevtio;
1515
#[cfg(target_os = "linux")]
1616
mod tdxconnect;
17+
#[cfg(target_os = "linux")]
18+
mod vbs;
1719

1820
pub use client::TdispClient;
1921
pub use client::TdispCommandTransport;
@@ -49,9 +51,12 @@ pub use tdisp_proto::TdispTdiState;
4951
pub use sevtio::TdispSevTioResourceValidator;
5052
#[cfg(target_os = "linux")]
5153
pub use tdxconnect::TdispTdxConnectResourceValidator;
54+
#[cfg(target_os = "linux")]
55+
pub use vbs::TdispVbsResourceValidator;
5256

5357
#[cfg(target_os = "linux")]
5458
use anyhow::Context;
59+
use guid::Guid;
5560
use hvdef::Vtl;
5661
use std::future::Future;
5762
use std::pin::Pin;
@@ -211,15 +216,21 @@ pub trait TdispResourceValidationInterface: Send + Sync {
211216
/// to.
212217
/// * `vtom` - The address mask with the VTOM bit set, marking where VTOM
213218
/// addresses start in the CVM. `None` on a partition without one.
219+
/// * `instance_id` - The instance ID of the VPCI bus the device is on. The VBS
220+
/// validator derives the hypervisor's logical device ID from it. That's
221+
/// temporary, to enable the VBS TDISP flows, which will eventually use the
222+
/// same guest device ID as the other platforms.
214223
/// * `is_test_environment` - `true` if running in a test environment.
215224
pub fn new_resource_validator(
216225
isolation: IsolationType,
217226
vtom: Option<u64>,
227+
instance_id: Guid,
218228
is_test_environment: bool,
219229
) -> anyhow::Result<Arc<dyn TdispResourceValidationInterface>> {
220230
tracing::info!(
221231
?isolation,
222232
?vtom,
233+
%instance_id,
223234
is_test_environment,
224235
"selecting a TDISP resource validator"
225236
);
@@ -244,6 +255,9 @@ pub fn new_resource_validator(
244255
Ok(Arc::new(TdispTdxConnectResourceValidator::new(vtom)?))
245256
}
246257

258+
#[cfg(target_os = "linux")]
259+
IsolationType::Vbs => Ok(Arc::new(TdispVbsResourceValidator::new(instance_id)?)),
260+
247261
// Isolation types with no validator of their own, or running in an
248262
// environment outside of a OpenHCL Linux build
249263
_ => Ok(Arc::new(noop::TdispNoopResourceValidator::new())),

0 commit comments

Comments
 (0)