Skip to main content

sgxs_loaders/isgx/
mod.rs

1/* Copyright (c) Fortanix, Inc.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7mod ioctl;
8pub mod debugging;
9
10use std::convert::TryFrom;
11use std::fs::{File, OpenOptions};
12use std::io::{self, Error as IoError, Result as IoResult};
13use std::ops::Range;
14use std::os::unix::io::AsRawFd;
15use std::path::{Path, PathBuf};
16use std::ptr;
17use std::sync::Arc;
18
19use nix::sys::mman::{mmap, munmap, ProtFlags as Prot, MapFlags as Map};
20use nix::errno::Errno;
21
22use sgx_isa::{Attributes, Einittoken, ErrorCode, Miscselect, Secinfo, Secs, Sigstruct, PageType, SecinfoFlags};
23use sgxs::einittoken::EinittokenProvider;
24use sgxs::loader;
25use sgxs::sgxs::{MeasEAdd, MeasECreate, PageChunks, SgxsRead};
26use thiserror::Error as ThisError;
27
28use crate::{MappingInfo, Tcs};
29use crate::generic::{self, EinittokenError, EnclaveLoad, Mapping};
30
31/// A Linux SGX driver API family.
32///
33/// Unfortunately, there are many different driver versions that all have a
34/// slightly different API and may also have slightly different functionality.
35/// There is no common versioning or naming scheme for referring to these
36/// drivers. These are grouped here into different families of APIs, each
37/// family represents a set of interfaces that are very similar, and as such
38/// can be called by similar logic.
39#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
40pub enum DriverFamily {
41    /// These APIs are commonly exposed via device nodes at
42    /// * `/dev/isgx`
43    /// * `/dev/sgx`
44    /// * `/dev/sgx_prv`
45    ///
46    /// # Compatibility notes
47    ///
48    /// Implementations may require a launch token, may not support providing a
49    /// launch token, or may provide both as an option.
50    Montgomery,
51    /// This API is commonly exposed via a device node at
52    /// * `/dev/sgx/enclave`
53    /// * `/dev/sgx_enclave`
54    ///
55    /// # Compatibility notes
56    ///
57    /// Currently, no upstream implementations of this API family support
58    /// partially-measured pages or providing a launch token. A Fortanix internal
59    /// SGX driver explicitly adds support for partially-measured pages for backwards
60    /// compatibility reasons.
61    Augusta,
62}
63
64use self::DriverFamily::*;
65
66#[derive(ThisError, Debug)]
67pub enum SgxIoctlError {
68    #[error("I/O ctl failed.")]
69    Io(#[source] IoError),
70    #[error("The SGX instruction returned an error: {:?}.", _0)]
71    Ret(ErrorCode),
72    #[error("The enclave was destroyed because the CPU was powered down.")]
73    PowerLostEnclave,
74    #[error("Launch enclave version rollback detected.")]
75    LeRollback,
76}
77
78#[derive(ThisError, Debug)]
79pub enum Error {
80    #[error("Failed to map enclave into memory.")]
81    Map(#[source] IoError),
82    #[error("Failed to call ECREATE.")]
83    Create(#[source] SgxIoctlError),
84    #[error("Failed to call EADD.")]
85    Add(#[source] SgxIoctlError),
86    #[error("Failed to call EEXTEND.")]
87    Extend(#[source] SgxIoctlError),
88    #[error("Failed to call EINIT.")]
89    Init(#[source] SgxIoctlError),
90}
91
92impl Error {
93    fn map(error: nix::Error) -> Self {
94        Error::Map(error.as_errno().unwrap().into())
95    }
96}
97
98impl EinittokenError for Error {
99    fn is_einittoken_error(&self) -> bool {
100        use self::Error::Init;
101        use self::SgxIoctlError::Ret;
102        match self {
103            &Init(Ret(ErrorCode::InvalidEinitToken)) |
104            &Init(Ret(ErrorCode::InvalidCpusvn)) |
105            &Init(Ret(ErrorCode::InvalidAttribute)) | // InvalidEinitAttribute according to PR, but does not exist.
106            &Init(Ret(ErrorCode::InvalidMeasurement)) => true,
107            _ => false,
108        }
109    }
110}
111
112macro_rules! ioctl_unsafe {
113    ( $f:ident, $v:expr ) => {{
114        const SGX_POWER_LOST_ENCLAVE: i32 = 0x40000000;
115        const SGX_LE_ROLLBACK: i32 = 0x40000001;
116
117        match unsafe { $v } {
118            Err(_) => Err(Error::$f(SgxIoctlError::Io(IoError::last_os_error()))),
119            Ok(0) => Ok(()),
120            Ok(SGX_POWER_LOST_ENCLAVE) => Err(Error::$f(SgxIoctlError::PowerLostEnclave)),
121            Ok(SGX_LE_ROLLBACK) => Err(Error::$f(SgxIoctlError::LeRollback)),
122            Ok(v) => Err(Error::$f(SgxIoctlError::Ret(
123                ErrorCode::try_from(v as u32).expect("Invalid ioctl return value"),
124            ))),
125        }
126    }};
127}
128
129impl EnclaveLoad for InnerDevice {
130    type Error = Error;
131    type MapData = MapData;
132
133    fn new(
134        mut device: Arc<InnerDevice>,
135        ecreate: MeasECreate,
136        attributes: Attributes,
137        miscselect: Miscselect,
138    ) -> Result<Mapping<Self>, Self::Error> {
139        let esize = ecreate.size as usize;
140        let ptr = unsafe {
141            match device.driver {
142                Montgomery => {
143                    mmap(
144                        ptr::null_mut(),
145                        esize,
146                        Prot::PROT_READ | Prot::PROT_WRITE | Prot::PROT_EXEC,
147                        Map::MAP_SHARED,
148                        device.fd.as_raw_fd(),
149                        0,
150                    ).map_err(Error::map)?
151                },
152                Augusta => {
153                    unsafe fn maybe_unmap(addr: *mut std::ffi::c_void, len: usize) {
154                        if len == 0 {
155                            return;
156                        }
157                        if let Err(e) = munmap(addr, len) {
158                            eprintln!("SGX enclave create: munmap failed: {:?}", e);
159                        }
160                    }
161
162                    // re-open device by cloning, if necessary
163                    Arc::make_mut(&mut device);
164
165                    let ptr = mmap(
166                        ptr::null_mut(),
167                        esize * 2,
168                        Prot::PROT_NONE,
169                        Map::MAP_SHARED | Map::MAP_ANONYMOUS | Map::MAP_NORESERVE,
170                        0,
171                        0,
172                    ).map_err(Error::map)?;
173
174                    let align_offset = ptr.align_offset(esize);
175                    if align_offset > esize {
176                        unreachable!()
177                    }
178                    let newptr = ptr.add(align_offset);
179                    maybe_unmap(ptr, align_offset);
180                    maybe_unmap(newptr.add(esize), esize - align_offset);
181
182                    newptr
183                },
184            }
185        };
186
187        let mapping = Mapping {
188            device,
189            mapdata: Default::default(),
190            base: ptr as u64,
191            size: ecreate.size,
192            tcss: vec![],
193        };
194        debugging::register_new_enclave(mapping.base, mapping.size);
195
196        let secs = Secs {
197            baseaddr: mapping.base,
198            size: ecreate.size,
199            ssaframesize: ecreate.ssaframesize,
200            miscselect,
201            attributes,
202            ..Default::default()
203        };
204        let createdata = ioctl::CreateData { secs: &secs };
205        ioctl_unsafe!(
206            Create,
207            ioctl::create(mapping.device.fd.as_raw_fd(), &createdata)
208        )?;
209        Ok(mapping)
210    }
211
212    fn add(
213        mapping: &mut Mapping<Self>,
214        page: (MeasEAdd, PageChunks, [u8; 4096]),
215    ) -> Result<(), Self::Error> {
216        let (eadd, chunks, data) = page;
217        let secinfo = Secinfo {
218            flags: eadd.secinfo.flags,
219            ..Default::default()
220        };
221        let dstpage = mapping.base + eadd.offset;
222        match mapping.device.driver {
223            Montgomery => {
224                let adddata = ioctl::montgomery::AddData {
225                    dstpage,
226                    srcpage: &data,
227                    secinfo: &secinfo,
228                    chunks: chunks.0,
229                };
230                ioctl_unsafe!(Add, ioctl::montgomery::add(mapping.device.fd.as_raw_fd(), &adddata))
231            },
232            Augusta => {
233                let (flags, partially_measured) = match chunks.0 {
234                    0 => (ioctl::augusta::SgxPageFlags::empty(), false),
235                    0xffff => (ioctl::augusta::SgxPageFlags::SGX_PAGE_MEASURE, false),
236                    _ => (ioctl::augusta::SgxPageFlags::empty(), true)
237                };
238
239                let data = ioctl::augusta::Align4096(data);
240                let mut adddata = ioctl::augusta::AddData {
241                    src: &data,
242                    offset: eadd.offset,
243                    length: data.0.len() as _,
244                    secinfo: &secinfo,
245                    flags,
246                    count: 0,
247                };
248                ioctl_unsafe!(Add, ioctl::augusta::add(mapping.device.fd.as_raw_fd(), &mut adddata))?;
249                assert_eq!(adddata.length, adddata.count);
250
251                if partially_measured {
252                    for chunk in 0..16 {
253                        if (0x1 << chunk) & chunks.0 != 0 {
254                            // Only supported with Fortanix' internal upstream driver patch
255                            let mut extend = ioctl::augusta::SgxEnclaveExtend {
256                                offset: eadd.offset as u64 + chunk as u64 * 256,
257                            };
258                            let fd = mapping.device.fd.as_raw_fd();
259                            ioctl_unsafe!(Extend, ioctl::augusta::extend(fd, &mut extend))
260                                .or_else(|e| {
261                                    match e {
262                                        Error::Extend(SgxIoctlError::Io(ref io_err)) if io_err.raw_os_error() == Some(Errno::ENOTTY as i32) => {
263                                            return Err(Error::Add(SgxIoctlError::Io(IoError::new(
264                                                io::ErrorKind::Other,
265                                                "Partially-measured pages not supported in this driver",
266                                            ))))
267                                        }
268                                        _ => Err(e),
269                                    }
270                                })?;
271                        }
272                    }
273                }
274
275                let prot = match PageType::try_from(secinfo.flags.page_type()) {
276                    Ok(PageType::Reg) => {
277                        let mut prot = Prot::empty();
278                        if secinfo.flags.intersects(SecinfoFlags::R) {
279                            prot |= Prot::PROT_READ
280                        }
281                        if secinfo.flags.intersects(SecinfoFlags::W) {
282                            prot |= Prot::PROT_WRITE
283                        }
284                        if secinfo.flags.intersects(SecinfoFlags::X) {
285                            prot |= Prot::PROT_EXEC
286                        }
287                        prot
288                    }
289                    Ok(PageType::Tcs) => {
290                        Prot::PROT_READ | Prot::PROT_WRITE
291                    },
292                    _ => unreachable!(),
293                };
294
295                let range = dstpage..(dstpage + data.0.len() as u64);
296
297                impl PendingMmap {
298                    fn append(&mut self, prot: Prot, range: &Range<u64>) -> bool {
299                        let can_append = prot == self.prot && self.range.end == range.start;
300                        if can_append {
301                            self.range.end = range.end;
302                        }
303                        can_append
304                    }
305                }
306
307                let (pending, map_now) = match mapping.mapdata.pending_mmap.take() {
308                    Some(mut pending_mmap) => {
309                        if pending_mmap.append(prot, &range) {
310                            (Some(pending_mmap), None)
311                        } else {
312                            (None, Some(pending_mmap))
313                        }
314                    },
315                    None => (None, None)
316                };
317
318                mapping.mapdata.pending_mmap = Some(pending.unwrap_or(PendingMmap { prot, range }));
319                if let Some(map_now) = map_now {
320                    unsafe { map_now.map(&mapping.device)? };
321                }
322
323                Ok(())
324            }
325        }
326    }
327
328    fn init(
329        mapping: &mut Mapping<Self>,
330        sigstruct: &Sigstruct,
331        einittoken: Option<&Einittoken>,
332    ) -> Result<(), Self::Error> {
333        // ioctl() may return ENOTTY if the specified request does not apply to
334        // the kind of object that the descriptor fd references.
335        fn is_enotty(result: &Result<(), Error>) -> bool {
336            match result {
337                Err(Error::Init(SgxIoctlError::Io(ref err))) => {
338                    err.raw_os_error() == Some(Errno::ENOTTY as _)
339                }
340                _ => false,
341            }
342        }
343
344        fn ioctl_init(mapping: &Mapping<InnerDevice>, sigstruct: &Sigstruct) -> Result<(), Error> {
345            match mapping.device.driver {
346                Montgomery => {
347                    let initdata = ioctl::montgomery::InitData {
348                        base: mapping.base,
349                        sigstruct,
350                    };
351                    ioctl_unsafe!(Init, ioctl::montgomery::init(mapping.device.fd.as_raw_fd(), &initdata))
352                },
353                Augusta => {
354                    let initdata = ioctl::augusta::InitData {
355                        sigstruct,
356                    };
357                    ioctl_unsafe!(Init, ioctl::augusta::init(mapping.device.fd.as_raw_fd(), &initdata))
358                }
359            }
360        }
361
362        fn ioctl_init_with_token(
363            mapping: &Mapping<InnerDevice>,
364            sigstruct: &Sigstruct,
365            einittoken: &Einittoken,
366        ) -> Result<(), Error> {
367            match mapping.device.driver {
368                Montgomery => {
369                    let initdata = ioctl::montgomery::InitDataWithToken {
370                        base: mapping.base,
371                        sigstruct,
372                        einittoken,
373                    };
374                    ioctl_unsafe!(
375                        Init,
376                        ioctl::montgomery::init_with_token(mapping.device.fd.as_raw_fd(), &initdata)
377                    )
378                },
379                Augusta => {
380                    Err(Error::Init(SgxIoctlError::Io(Errno::ENOTTY.into())))
381                }
382            }
383        }
384
385        if let Some(pending_mmap) = mapping.mapdata.pending_mmap.take() {
386            unsafe { pending_mmap.map(&mapping.device)? };
387        }
388
389        // Try either EINIT ioctl(), in the order that makes most sense given
390        // the function arguments
391        if let Some(einittoken) = einittoken {
392            let res = ioctl_init_with_token(mapping, sigstruct, einittoken);
393
394            if is_enotty(&res) {
395                ioctl_init(mapping, sigstruct)
396            } else {
397                res
398            }
399        } else {
400            let res = ioctl_init(mapping, sigstruct);
401
402            if is_enotty(&res) {
403                ioctl_init_with_token(mapping, sigstruct, &Default::default())
404            } else {
405                res
406            }
407        }
408    }
409
410    fn destroy(mapping: &mut Mapping<Self>) {
411        debugging::unregister_terminated_enclave(mapping.base);
412        unsafe { let _ = munmap(mapping.base as usize as *mut _, mapping.size as usize); }
413    }
414}
415
416#[derive(Debug)]
417#[must_use]
418struct PendingMmap {
419    prot: Prot,
420    range: Range<u64>,
421}
422
423impl PendingMmap {
424    unsafe fn map(self, device: &InnerDevice) -> Result<(), Error> {
425        mmap(
426            self.range.start as _,
427            (self.range.end - self.range.start) as _,
428            self.prot,
429            Map::MAP_SHARED | Map::MAP_FIXED,
430            device.fd.as_raw_fd(),
431            0,
432        ).map(|_| ()).map_err(Error::map)
433    }
434}
435
436#[derive(Debug, Default)]
437struct MapData {
438    pending_mmap: Option<PendingMmap>
439}
440
441#[derive(Debug)]
442struct InnerDevice {
443    fd: File,
444    path: Arc<PathBuf>,
445    driver: DriverFamily,
446}
447
448impl Clone for InnerDevice {
449    fn clone(&self) -> Self {
450        InnerDevice {
451            fd: OpenOptions::new().read(true).write(true).open(&**self.path).unwrap(),
452            path: self.path.clone(),
453            driver: self.driver,
454        }
455    }
456}
457
458#[derive(Debug)]
459pub struct Device {
460    inner: generic::Device<InnerDevice>,
461}
462
463pub struct DeviceBuilder {
464    inner: generic::DeviceBuilder<InnerDevice>,
465}
466
467impl Device {
468    /// Try to open an SGX device from a list of default paths
469    pub fn new() -> IoResult<DeviceBuilder> {
470        const DEFAULT_DEVICE_PATHS: &[(&str, DriverFamily)] = &[
471            ("/dev/sgx_enclave", Augusta),
472            ("/dev/sgx/enclave", Augusta),
473            ("/dev/isgx", Montgomery),
474            ("/dev/sgx", Montgomery),
475        ];
476
477        for &(path, family) in DEFAULT_DEVICE_PATHS {
478            match Self::open(path, family) {
479                Err(ref e) if e.kind() == io::ErrorKind::NotFound => continue,
480                Err(ref e) if e.raw_os_error() == Some(Errno::ENOTDIR as _) => continue,
481                result => return result,
482            }
483        }
484
485        Err(IoError::new(io::ErrorKind::NotFound, "None of the default SGX device paths were found"))
486    }
487
488    pub fn open<T: AsRef<Path>>(path: T, driver: DriverFamily) -> IoResult<DeviceBuilder> {
489        let path = path.as_ref();
490        let file = OpenOptions::new().read(true).write(true).open(path)?;
491        Ok(DeviceBuilder {
492            inner: generic::DeviceBuilder {
493                device: generic::Device {
494                    inner: Arc::new(InnerDevice {
495                        fd: file,
496                        path: Arc::new(path.to_owned()),
497                        driver,
498                    }),
499                    einittoken_provider: None,
500                },
501            },
502        })
503    }
504
505    pub fn path(&self) -> &Path {
506        &self.inner.inner.path
507    }
508}
509
510impl loader::Load for Device {
511    type MappingInfo = MappingInfo;
512    type Tcs = Tcs;
513
514    fn load<R: SgxsRead>(
515        &mut self,
516        reader: &mut R,
517        sigstruct: &Sigstruct,
518        attributes: Attributes,
519        miscselect: Miscselect,
520    ) -> ::std::result::Result<loader::Mapping<Self>, ::anyhow::Error> {
521        self.inner
522            .load(reader, sigstruct, attributes, miscselect)
523            .map(Into::into)
524    }
525}
526
527impl DeviceBuilder {
528    pub fn einittoken_provider<P: Into<Box<dyn EinittokenProvider>>>(
529        mut self,
530        einittoken_provider: P,
531    ) -> Self {
532        self.inner.einittoken_provider(einittoken_provider.into());
533        self
534    }
535
536    pub fn build(self) -> Device {
537        Device {
538            inner: self.inner.build(),
539        }
540    }
541}