Skip to content

Commit

Permalink
Auto merge of rust-lang#76345 - okready:sgx-mem-range-overflow-checks…
Browse files Browse the repository at this point in the history
…, r=joshtriplett

Add is_enclave_range/is_user_range overflow checks

Fixes rust-lang#76343.

This adds overflow checking to `is_enclave_range` and `is_user_range` in `sgx::os::fortanix_sgx::mem` in order to mitigate possible security issues with enclave code. It also accounts for an edge case where the memory range provided ends exactly at the end of the address space, where calculating `p + len` would overflow back to zero despite the range potentially being valid.
  • Loading branch information
bors committed Mar 3, 2021
2 parents 35dbef2 + c989de5 commit cbca568
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions library/std/src/sys/sgx/abi/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,46 @@ pub fn image_base() -> u64 {

/// Returns `true` if the specified memory range is in the enclave.
///
/// `p + len` must not overflow.
/// For safety, this function also checks whether the range given overflows,
/// returning `false` if so.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn is_enclave_range(p: *const u8, len: usize) -> bool {
let start = p as u64;
let end = start + (len as u64);
start >= image_base() && end <= image_base() + (unsafe { ENCLAVE_SIZE } as u64) // unsafe ok: link-time constant
let start = p as usize;

// Subtract one from `len` when calculating `end` in case `p + len` is
// exactly at the end of addressable memory (`p + len` would overflow, but
// the range is still valid).
let end = if len == 0 {
start
} else if let Some(end) = start.checked_add(len - 1) {
end
} else {
return false;
};

let base = image_base() as usize;
start >= base && end <= base + (unsafe { ENCLAVE_SIZE } - 1) // unsafe ok: link-time constant
}

/// Returns `true` if the specified memory range is in userspace.
///
/// `p + len` must not overflow.
/// For safety, this function also checks whether the range given overflows,
/// returning `false` if so.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn is_user_range(p: *const u8, len: usize) -> bool {
let start = p as u64;
let end = start + (len as u64);
end <= image_base() || start >= image_base() + (unsafe { ENCLAVE_SIZE } as u64) // unsafe ok: link-time constant
let start = p as usize;

// Subtract one from `len` when calculating `end` in case `p + len` is
// exactly at the end of addressable memory (`p + len` would overflow, but
// the range is still valid).
let end = if len == 0 {
start
} else if let Some(end) = start.checked_add(len - 1) {
end
} else {
return false;
};

let base = image_base() as usize;
end < base || start > base + (unsafe { ENCLAVE_SIZE } - 1) // unsafe ok: link-time constant
}

0 comments on commit cbca568

Please sign in to comment.