Skip to content

Commit 82976c4

Browse files
committed
kernel/attest: use vsock when feature is available
Use vsock for attestation when the `vsock` feature is enabled. If vsock fails it will fallback on the serial port. Signed-off-by: Luigi Leonardi <leonardi@redhat.com>
1 parent 083507f commit 82976c4

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

kernel/src/attest.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ use crate::{
1414
serial::SerialPort,
1515
utils::vec::try_to_vec,
1616
};
17+
18+
#[cfg(feature = "vsock")]
19+
use crate::vsock::virtio_vsock::VsockStream;
20+
1721
use aes::{cipher::BlockDecrypt, Aes256Dec};
1822
use aes_gcm::KeyInit;
1923
use alloc::{string::ToString, vec::Vec};
@@ -39,12 +43,18 @@ use sha2::{Digest, Sha512};
3943
use zerocopy::{FromBytes, IntoBytes};
4044

4145
enum Transport <'a> {
46+
#[cfg(feature = "vsock")]
47+
Vsock(VsockStream),
4248
Serial(SerialPort<'a>)
4349
}
4450

4551
impl Transport <'_> {
4652
fn write(&mut self, buf: &[u8]) -> Result<usize, SvsmError> {
4753
match self {
54+
#[cfg(feature = "vsock")]
55+
Transport::Vsock(vsock) => {
56+
vsock.write(buf)
57+
},
4858
Transport::Serial(serial) => {
4959
serial.write(buf)
5060
}
@@ -53,13 +63,23 @@ impl Transport <'_> {
5363

5464
fn read(&mut self, buf: &mut [u8]) -> Result<usize, SvsmError> {
5565
match self {
66+
#[cfg(feature = "vsock")]
67+
Transport::Vsock(vsock) => {
68+
vsock.read(buf)
69+
},
5670
Transport::Serial(serial) => {
5771
serial.read(buf)
5872
}
5973
}
6074
}
6175
}
6276

77+
fn create_serial_transport<'a>() -> Transport<'a> {
78+
let sp = SerialPort::new(&DEFAULT_IO_DRIVER, 0x3e8); // COM3
79+
sp.init();
80+
Transport::Serial(sp)
81+
}
82+
6383
/// The attestation driver that communicates with the proxy via some communication channel (serial
6484
/// port, virtio-vsock, etc...).
6585
#[allow(missing_debug_implementations)]
@@ -75,8 +95,6 @@ impl TryFrom<Tee> for AttestationDriver<'_> {
7595
fn try_from(tee: Tee) -> Result<Self, Self::Error> {
7696
// TODO: Make the IO port configurable/discoverable for other transport mechanisms such as
7797
// virtio-vsock.
78-
let sp = SerialPort::new(&DEFAULT_IO_DRIVER, 0x3e8); // COM3
79-
sp.init();
8098

8199
match tee {
82100
Tee::Snp => (),
@@ -86,7 +104,24 @@ impl TryFrom<Tee> for AttestationDriver<'_> {
86104
let curve = Curve::new(TpmEccCurve::NistP521).map_err(AttestationError::Crypto)?;
87105
let ecc = sc_key_generate(&curve).map_err(AttestationError::Crypto)?;
88106

89-
let transport = Transport::Serial(sp);
107+
let transport = {
108+
#[cfg(feature = "vsock")]
109+
{
110+
match VsockStream::connect(1234, 12345, 2) {
111+
Ok(value) => {
112+
Transport::Vsock(value)
113+
},
114+
Err(_) => {
115+
create_serial_transport()
116+
}
117+
}
118+
}
119+
#[cfg(not(feature = "vsock"))]
120+
{
121+
create_serial_transport()
122+
}
123+
};
124+
90125
Ok(Self {transport, tee, ecc })
91126
}
92127
}

0 commit comments

Comments
 (0)