Skip to content

Commit a340455

Browse files
committed
Auto merge of #60496 - jethrogb:jb/address-integer-overflow, r=alexcrichton
Fix potential integer overflow in SGX memory range calculation. Thanks to Eduard Marin and David Oswald at the University of Burmingham, and Jo Van Bulck at KU Leuven for discovering this issue.
2 parents 3af1bdc + 1dc4a38 commit a340455

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/libstd/sys/sgx/abi/mem.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,23 @@ pub fn image_base() -> u64 {
2727
}
2828

2929
/// Returns `true` if the specified memory range is in the enclave.
30+
///
31+
/// `p + len` must not overflow.
3032
#[unstable(feature = "sgx_platform", issue = "56975")]
3133
pub fn is_enclave_range(p: *const u8, len: usize) -> bool {
32-
let start=p as u64;
33-
let end=start + (len as u64);
34+
let start = p as u64;
35+
let end = start + (len as u64);
3436
start >= image_base() &&
3537
end <= image_base() + (unsafe { ENCLAVE_SIZE } as u64) // unsafe ok: link-time constant
3638
}
3739

3840
/// Returns `true` if the specified memory range is in userspace.
41+
///
42+
/// `p + len` must not overflow.
3943
#[unstable(feature = "sgx_platform", issue = "56975")]
4044
pub fn is_user_range(p: *const u8, len: usize) -> bool {
41-
let start=p as u64;
42-
let end=start + (len as u64);
45+
let start = p as u64;
46+
let end = start + (len as u64);
4347
end <= image_base() ||
4448
start >= image_base() + (unsafe { ENCLAVE_SIZE } as u64) // unsafe ok: link-time constant
4549
}

src/libstd/sys/sgx/abi/usercalls/alloc.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ pub unsafe trait UserSafe {
8585
///
8686
/// * the pointer is not aligned.
8787
/// * the pointer is null.
88+
/// * the pointed-to range does not fit in the address space.
8889
/// * the pointed-to range is not in user memory.
8990
unsafe fn from_raw_sized(ptr: *mut u8, size: usize) -> NonNull<Self> {
91+
assert!(ptr.wrapping_add(size) >= ptr);
9092
let ret = Self::from_raw_sized_unchecked(ptr, size);
9193
Self::check_ptr(ret);
9294
NonNull::new_unchecked(ret as _)
@@ -268,6 +270,7 @@ impl<T> User<[T]> where [T]: UserSafe {
268270
///
269271
/// * The pointer is not aligned
270272
/// * The pointer is null
273+
/// * The pointed-to range does not fit in the address space
271274
/// * The pointed-to range is not in user memory
272275
pub unsafe fn from_raw_parts(ptr: *mut T, len: usize) -> Self {
273276
User(NonNull::new_userref(<[T]>::from_raw_sized(ptr as _, len * mem::size_of::<T>())))
@@ -372,6 +375,7 @@ impl<T> UserRef<[T]> where [T]: UserSafe {
372375
///
373376
/// * The pointer is not aligned
374377
/// * The pointer is null
378+
/// * The pointed-to range does not fit in the address space
375379
/// * The pointed-to range is not in user memory
376380
pub unsafe fn from_raw_parts<'a>(ptr: *const T, len: usize) -> &'a Self {
377381
&*(<[T]>::from_raw_sized(ptr as _, len * mem::size_of::<T>()).as_ptr() as *const Self)
@@ -389,6 +393,7 @@ impl<T> UserRef<[T]> where [T]: UserSafe {
389393
///
390394
/// * The pointer is not aligned
391395
/// * The pointer is null
396+
/// * The pointed-to range does not fit in the address space
392397
/// * The pointed-to range is not in user memory
393398
pub unsafe fn from_raw_parts_mut<'a>(ptr: *mut T, len: usize) -> &'a mut Self {
394399
&mut*(<[T]>::from_raw_sized(ptr as _, len * mem::size_of::<T>()).as_ptr() as *mut Self)
@@ -552,10 +557,11 @@ impl UserRef<super::raw::ByteBuffer> {
552557
/// enclave memory.
553558
///
554559
/// # Panics
555-
/// This function panics if:
560+
/// This function panics if, in the user `ByteBuffer`:
556561
///
557-
/// * The pointer in the user `ByteBuffer` is null
558-
/// * The pointed-to range in the user `ByteBuffer` is not in user memory
562+
/// * The pointer is null
563+
/// * The pointed-to range does not fit in the address space
564+
/// * The pointed-to range is not in user memory
559565
pub fn copy_user_buffer(&self) -> Vec<u8> {
560566
unsafe {
561567
let buf = self.to_enclave();

0 commit comments

Comments
 (0)