Skip to content

Commit d55ff35

Browse files
Fix: Update FFI WrappedVlekHashstick and SnpVlekLoad
WrappedVlekHashstick now owns the VLEK byte data ([u8; 432]) instead of borrowing it via a reference. This change ensures that the structure is self-contained and safely passed to the kernel, which expects a pointer to a buffer that includes the full hashstick data. Referencing the buffer (even with careful lifetime management) was not working reliably and added complexity. Owning the data is simpler, safer, and easier to maintain. SnpVlekLoad.len is now correctly set to the size of the SnpVlekLoad struct, not the size of the hashstick. According to the SEV-SNP spec, this field represents the size of the command buffer, not the size of the payload. Signed-off-by: DGonzalezVillal <Diego.GonzalezVillalobos@amd.com>
1 parent f23a2be commit d55ff35

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

  • src/firmware/linux/host/types

src/firmware/linux/host/types/snp.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -251,23 +251,23 @@ impl Default for SnpSetConfig {
251251
}
252252
}
253253

254-
// Length defined in the Linux Kernel for the IOCTL.
254+
// Expected length for the VLEK hashstick.
255255
const HASHSTICK_BUFFER_LEN: usize = 432;
256256

257257
#[cfg(feature = "snp")]
258258
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
259259
#[repr(C, packed)]
260-
/// Wrapped VLEK data.
261-
pub struct WrappedVlekHashstick<'a> {
260+
/// Wrapped VLEK data for FFI layer.
261+
pub struct WrappedVlekHashstick {
262262
/// Wrapped VLEK data provided by AMD Key Distribution Server as bytes.
263-
/// Address to this data is passed to the AMD Secure Processor.
264-
pub data: &'a [u8], // 432 bytes of data
263+
/// Address to this data is passed to the kernel.
264+
pub data: [u8; HASHSTICK_BUFFER_LEN],
265265
}
266266

267-
impl<'a, 'b: 'a> std::convert::TryFrom<&'b [u8]> for WrappedVlekHashstick<'a> {
267+
impl std::convert::TryFrom<&[u8]> for WrappedVlekHashstick {
268268
type Error = HashstickError;
269269

270-
fn try_from(value: &'b [u8]) -> Result<Self, Self::Error> {
270+
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
271271
if value.len() != HASHSTICK_BUFFER_LEN {
272272
return Err(HashstickError::InvalidLength);
273273
}
@@ -287,7 +287,10 @@ impl<'a, 'b: 'a> std::convert::TryFrom<&'b [u8]> for WrappedVlekHashstick<'a> {
287287
return Err(HashstickError::InvalidReservedField);
288288
}
289289

290-
Ok(Self { data: value })
290+
let mut data = [0u8; HASHSTICK_BUFFER_LEN];
291+
data.copy_from_slice(value);
292+
293+
Ok(Self { data })
291294
}
292295
}
293296

@@ -296,15 +299,15 @@ impl<'a, 'b: 'a> std::convert::TryFrom<&'b [u8]> for WrappedVlekHashstick<'a> {
296299
#[repr(C, packed)]
297300
/// Structure used to load a VLEK hashstick into the AMD Secure Processor.
298301
pub struct SnpVlekLoad {
299-
/// Length of the command buffer read by the AMD Secure Processor.
302+
/// Length of this command buffer in bytes.
300303
pub len: u32,
301304

302305
/// Version of wrapped VLEK hashstick (Must be 0h).
303306
pub vlek_wrapped_version: u8,
304307

305308
_reserved: [u8; 3],
306309

307-
/// Address of wrapped VLEK hashstick ([WrappedVlekHashstick])
310+
/// System Physical Address of wrapped VLEK hashstick ([WrappedVlekHashstick])
308311
pub vlek_wrapped_address: u64,
309312
}
310313

@@ -316,10 +319,10 @@ impl SnpVlekLoad {
316319
}
317320
}
318321

319-
impl<'a> std::convert::From<&WrappedVlekHashstick<'a>> for SnpVlekLoad {
320-
fn from(value: &WrappedVlekHashstick<'a>) -> Self {
322+
impl From<&WrappedVlekHashstick> for SnpVlekLoad {
323+
fn from(value: &WrappedVlekHashstick) -> Self {
321324
Self {
322-
len: value.data.len() as u32,
325+
len: std::mem::size_of::<SnpVlekLoad>() as u32,
323326
vlek_wrapped_version: 0u8,
324327
_reserved: Default::default(),
325328
vlek_wrapped_address: value as *const WrappedVlekHashstick as u64,
@@ -463,7 +466,7 @@ mod test {
463466
#[test]
464467
fn test_bytes_to_wrapped_hashstick() {
465468
let bytes: [u8; HASHSTICK_BUFFER_LEN] = VALID_HASHSTICK_BYTES;
466-
let expected: WrappedVlekHashstick = WrappedVlekHashstick { data: &bytes };
469+
let expected: WrappedVlekHashstick = WrappedVlekHashstick { data: bytes };
467470
let actual: WrappedVlekHashstick =
468471
WrappedVlekHashstick::try_from(VALID_HASHSTICK_BYTES.as_slice()).unwrap();
469472

0 commit comments

Comments
 (0)