Skip to content

Commit c62aa0b

Browse files
committed
Fix UserRef<[T]>::copy_to_enclave_vec
It reinterprets uninitialized memory as initialized and does not drop existing elements of the Vec. Fix that. Additionally, make it more general by appending, instead of overwriting existing elements, and rename it to `append_to_enclave_vec`. A caller can simply call `.clear()` before, for the old behavior.
1 parent 8c7a94e commit c62aa0b

File tree

1 file changed

+6
-13
lines changed
  • library/std/src/sys/pal/sgx/abi/usercalls

1 file changed

+6
-13
lines changed

library/std/src/sys/pal/sgx/abi/usercalls/alloc.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -678,25 +678,18 @@ where
678678
unsafe { (*self.0.get()).len() }
679679
}
680680

681-
/// Copies the value from user memory and place it into `dest`. Afterwards,
682-
/// `dest` will contain exactly `self.len()` elements.
683-
///
684-
/// # Panics
685-
/// This function panics if the destination doesn't have the same size as
686-
/// the source. This can happen for dynamically-sized types such as slices.
687-
pub fn copy_to_enclave_vec(&self, dest: &mut Vec<T>) {
688-
if let Some(missing) = self.len().checked_sub(dest.capacity()) {
689-
dest.reserve(missing)
690-
}
681+
/// Copies the value from user memory and appends it to `dest`.
682+
pub fn append_to_enclave_vec(&self, dest: &mut Vec<T>) {
683+
dest.reserve(self.len());
684+
self.copy_to_enclave(&mut dest.spare_capacity_mut()[..self.len()]);
691685
// SAFETY: We reserve enough space above.
692-
unsafe { dest.set_len(self.len()) };
693-
self.copy_to_enclave(&mut dest[..]);
686+
unsafe { dest.set_len(dest.len() + self.len()) };
694687
}
695688

696689
/// Copies the value from user memory into a vector in enclave memory.
697690
pub fn to_enclave(&self) -> Vec<T> {
698691
let mut ret = Vec::with_capacity(self.len());
699-
self.copy_to_enclave_vec(&mut ret);
692+
self.append_to_enclave_vec(&mut ret);
700693
ret
701694
}
702695

0 commit comments

Comments
 (0)