Skip to content

Commit 3fea433

Browse files
bors[bot]Jethro Beekman
andauthored
Merge #399
399: Fix sgx_enclave_common loader & cleanup r=raoulstrackx a=jethrogb Co-authored-by: Jethro Beekman <[email protected]>
2 parents 2fb7b67 + 09414af commit 3fea433

File tree

5 files changed

+11
-22
lines changed

5 files changed

+11
-22
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

intel-sgx/sgxs-loaders/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ no_sgx_enclave_common = []
3131
# External dependencies
3232
bitflags = "1" # MIT/Apache-2.0
3333
nix = "0.15" # MIT
34-
libc = "0.2" # MIT/Apache-2.0
3534
failure = "0.1.1" # MIT/Apache-2.0
3635
failure_derive = "0.1.1" # MIT/Apache-2.0
3736
libloading = "0.5" # ISC

intel-sgx/sgxs-loaders/src/isgx/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
mod ioctl;
88

9-
use libc;
109
use std::convert::TryFrom;
1110
use std::fs::{File, OpenOptions};
1211
use std::io::{self, Error as IoError, Result as IoResult};
@@ -17,6 +16,7 @@ use std::ptr;
1716
use std::sync::Arc;
1817

1918
use nix::sys::mman::{mmap, munmap, ProtFlags as Prot, MapFlags as Map};
19+
use nix::errno::Errno;
2020

2121
use sgx_isa::{Attributes, Einittoken, ErrorCode, Miscselect, Secinfo, Secs, Sigstruct, PageType, SecinfoFlags};
2222
use sgxs::einittoken::EinittokenProvider;
@@ -309,7 +309,7 @@ impl EnclaveLoad for InnerDevice {
309309
fn is_enotty(result: &Result<(), Error>) -> bool {
310310
match result {
311311
Err(Error::Init(SgxIoctlError::Io(ref err))) => {
312-
err.raw_os_error() == Some(libc::ENOTTY)
312+
err.raw_os_error() == Some(Errno::ENOTTY as _)
313313
}
314314
_ => false,
315315
}
@@ -351,7 +351,7 @@ impl EnclaveLoad for InnerDevice {
351351
)
352352
},
353353
Augusta => {
354-
Err(Error::Init(SgxIoctlError::Io(IoError::from_raw_os_error(libc::ENOTTY))))
354+
Err(Error::Init(SgxIoctlError::Io(Errno::ENOTTY.into())))
355355
}
356356
}
357357
}
@@ -382,7 +382,7 @@ impl EnclaveLoad for InnerDevice {
382382
}
383383

384384
fn destroy(mapping: &mut Mapping<Self>) {
385-
unsafe { libc::munmap(mapping.base as usize as *mut _, mapping.size as usize) };
385+
unsafe { let _ = munmap(mapping.base as usize as *mut _, mapping.size as usize); }
386386
}
387387
}
388388

@@ -450,7 +450,7 @@ impl Device {
450450
for &(path, family) in DEFAULT_DEVICE_PATHS {
451451
match Self::open(path, family) {
452452
Err(ref e) if e.kind() == io::ErrorKind::NotFound => continue,
453-
Err(ref e) if e.raw_os_error() == Some(libc::ENOTDIR as _) => continue,
453+
Err(ref e) if e.raw_os_error() == Some(Errno::ENOTDIR as _) => continue,
454454
result => return result,
455455
}
456456
}

intel-sgx/sgxs-loaders/src/sgx_enclave_common/defs.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ pub const LIBRARY: &str = "libsgx_enclave_common.so.1";
5656
#[cfg(windows)]
5757
pub const LIBRARY: &str = "sgx_enclave_common.dll";
5858

59+
#[repr(align(4096))]
60+
pub struct Align4096<T>(pub T);
61+
5962
pub const SYM_ENCLAVE_CREATE: &[u8] = b"enclave_create\0";
6063
pub type EnclaveCreateFn = unsafe extern "C" fn(
6164
base_address: *mut c_void,
@@ -71,7 +74,7 @@ pub const SYM_ENCLAVE_LOAD_DATA: &[u8] = b"enclave_load_data\0";
7174
pub type EnclaveLoadDataFn = unsafe extern "C" fn(
7275
target_address: *mut c_void,
7376
target_size: usize,
74-
source_buffer: *const u8,
77+
source_buffer: *const Align4096<[u8; 4096]>,
7578
data_properties: PageProperties,
7679
enclave_error: Option<&mut u32>,
7780
) -> usize;

intel-sgx/sgxs-loaders/src/sgx_enclave_common/mod.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use std::io::{Result as IoResult, Error as IoError};
1111
use std::os::raw::c_void;
1212
use std::sync::Arc;
1313
use std::{fmt, mem, ptr};
14-
#[cfg(unix)]
15-
use libc;
1614

1715
use sgx_isa::{Attributes, Einittoken, Miscselect, PageType, SecinfoFlags, Secs, Sigstruct};
1816
use sgxs::einittoken::EinittokenProvider;
@@ -175,6 +173,7 @@ impl EnclaveLoad for InnerLibrary {
175173
page: (MeasEAdd, PageChunks, [u8; 4096]),
176174
) -> Result<(), Self::Error> {
177175
let (eadd, chunks, data) = page;
176+
let data = Align4096(data);
178177

179178
let mut flags = PageProperties::empty();
180179
if eadd
@@ -209,7 +208,7 @@ impl EnclaveLoad for InnerLibrary {
209208
if (mapping.device.enclave_load_data)(
210209
(mapping.base + eadd.offset) as _,
211210
0x1000,
212-
data.as_ptr(),
211+
&data,
213212
flags,
214213
Some(&mut error),
215214
) != 0x1000
@@ -254,17 +253,6 @@ impl EnclaveLoad for InnerLibrary {
254253
return Err(Error::Init(error.into()));
255254
}
256255

257-
#[cfg(unix)]
258-
{
259-
if libc::mprotect(
260-
mapping.base as _,
261-
mapping.size as _,
262-
libc::PROT_READ | libc::PROT_WRITE | libc::PROT_EXEC,
263-
) == -1 {
264-
return Err(Error::Init(LibraryError::PageTableFailure(IoError::last_os_error())));
265-
}
266-
}
267-
268256
Ok(())
269257
}
270258
}

0 commit comments

Comments
 (0)