Skip to content

Commit ca7e076

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 eb2f314 commit ca7e076

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

kernel/src/attest.rs

Lines changed: 41 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, vec_sized},
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,21 +43,56 @@ 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) => vsock.write(buf),
4856
Transport::Serial(serial) => serial.write(buf),
4957
}
5058
}
5159

5260
fn read(&mut self, buf: &mut [u8]) -> Result<usize, SvsmError> {
5361
match self {
62+
#[cfg(feature = "vsock")]
63+
Transport::Vsock(vsock) => vsock.read(buf),
5464
Transport::Serial(serial) => serial.read(buf),
5565
}
5666
}
67+
68+
#[cfg(feature = "vsock")]
69+
fn new() -> Self {
70+
const VSOCK_ATTEST_DEFAULT_PORT: u32 = 1995;
71+
72+
match VsockStream::connect(1234, VSOCK_ATTEST_DEFAULT_PORT, 2) {
73+
Ok(value) => Transport::Vsock(value),
74+
Err(e) => {
75+
log::info!(
76+
"Vsock Error: {:?} during attestation. Trying again using the serial port",
77+
e
78+
);
79+
create_serial_transport()
80+
}
81+
}
82+
}
83+
84+
#[cfg(not(feature = "vsock"))]
85+
fn new() -> Self {
86+
create_serial_transport()
87+
}
88+
}
89+
90+
fn create_serial_transport<'a>() -> Transport<'a> {
91+
const COM3: u16 = 0x3e8;
92+
93+
let sp = SerialPort::new(&DEFAULT_IO_DRIVER, COM3); // COM3
94+
sp.init();
95+
Transport::Serial(sp)
5796
}
5897

5998
/// The attestation driver that communicates with the proxy via some communication channel (serial
@@ -71,8 +110,6 @@ impl TryFrom<Tee> for AttestationDriver<'_> {
71110
fn try_from(tee: Tee) -> Result<Self, Self::Error> {
72111
// TODO: Make the IO port configurable/discoverable for other transport mechanisms such as
73112
// virtio-vsock.
74-
let sp = SerialPort::new(&DEFAULT_IO_DRIVER, 0x3e8); // COM3
75-
sp.init();
76113

77114
match tee {
78115
Tee::Snp => (),
@@ -82,7 +119,8 @@ impl TryFrom<Tee> for AttestationDriver<'_> {
82119
let curve = Curve::new(TpmEccCurve::NistP521).map_err(AttestationError::Crypto)?;
83120
let ecc = sc_key_generate(&curve).map_err(AttestationError::Crypto)?;
84121

85-
let transport = Transport::Serial(sp);
122+
let transport = Transport::new();
123+
86124
Ok(Self {
87125
transport,
88126
tee,

0 commit comments

Comments
 (0)