Skip to main content

enclave_runner_sgx/
loader.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
7use std::arch::x86_64::{self, CpuidResult};
8use std::fs::File;
9use std::io::{Error as IoError, ErrorKind, Read, Result as IoResult};
10use std::marker::PhantomData;
11use std::os::raw::c_void;
12use std::path::Path;
13use std::{arch, str};
14
15use anyhow::{format_err, Context};
16use thiserror::Error as ThisError;
17
18#[cfg(feature = "crypto-openssl")]
19use openssl::{hash::Hasher, pkey::PKey};
20
21use enclave_runner::platform::{
22    CommandConfiguration, EnclaveConfiguration, EnclavePlatform, LibraryConfiguration,
23};
24use sgx_isa::{Attributes, AttributesFlags, Miscselect, Sigstruct};
25use sgxs::crypto::{SgxHashOps, SgxRsaOps};
26use sgxs::loader::{Load, MappingInfo, Tcs};
27use sgxs::sgxs::PageReader;
28use sgxs::sigstruct::{self, EnclaveHash, Signer};
29
30use crate::tcs::DebugBuffer;
31use crate::{Command, Library};
32
33enum EnclaveSource<'a> {
34    Path(&'a Path),
35    File(File),
36    Data(&'a [u8]),
37}
38
39impl<'a> EnclaveSource<'a> {
40    fn try_clone(&self) -> Option<Self> {
41        match *self {
42            EnclaveSource::Path(path) => Some(EnclaveSource::Path(path)),
43            EnclaveSource::Data(data) => Some(EnclaveSource::Data(data)),
44            EnclaveSource::File(_) => None,
45        }
46    }
47}
48
49impl<'a> Read for EnclaveSource<'a> {
50    fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
51        if let &mut EnclaveSource::Path(path) = self {
52            let file = File::open(path)?;
53            *self = EnclaveSource::File(file);
54        }
55
56        match *self {
57            EnclaveSource::File(ref mut file) => file.read(buf),
58            EnclaveSource::Data(ref mut data) => data.read(buf),
59            EnclaveSource::Path(_) => unreachable!(),
60        }
61    }
62}
63
64pub struct EnclaveBuilder<'a, L> {
65    enclave: EnclaveSource<'a>,
66    signature: Option<Sigstruct>,
67    attributes: Option<Attributes>,
68    miscselect: Option<Miscselect>,
69    load_and_sign: Option<Box<dyn FnOnce(Signer) -> Result<Sigstruct, anyhow::Error>>>,
70    hash_enclave:
71        Option<Box<dyn FnOnce(&mut EnclaveSource<'_>) -> Result<EnclaveHash, anyhow::Error>>>,
72    force_time_usercalls: bool,
73    loader: PhantomData<L>,
74}
75
76#[derive(Debug, ThisError)]
77pub enum EnclavePanic {
78    /// The first byte of the debug buffer was 0
79    #[error("Enclave panicked.")]
80    NoDebugBuf,
81    /// The debug buffer could be interpreted as a zero-terminated UTF-8 string
82    #[error("Enclave panicked: {}", _0)]
83    DebugStr(String),
84    /// The first byte of the debug buffer was not 0, but it was also not a
85    /// zero-terminated UTF-8 string
86    #[error("Enclave panicked: {:?}", _0)]
87    DebugBuf(Vec<u8>),
88}
89
90impl From<DebugBuffer> for EnclavePanic {
91    fn from(buf: DebugBuffer) -> EnclavePanic {
92        if buf[0] == 0 {
93            EnclavePanic::NoDebugBuf
94        } else {
95            match str::from_utf8(buf.split(|v| *v == 0).next().unwrap()) {
96                Ok(s) => EnclavePanic::DebugStr(s.to_owned()),
97                Err(_) => EnclavePanic::DebugBuf(buf.to_vec()),
98            }
99        }
100    }
101}
102
103// Erased here refers to Type Erasure
104#[derive(Debug)]
105pub(crate) struct ErasedTcs {
106    address: *mut c_void,
107    // This represents a resource so we need to maintain ownership even if not
108    // used
109    #[allow(dead_code)]
110    tcs: Box<dyn Tcs>,
111}
112
113// Would be `send` if we didn't cache the raw pointer address
114unsafe impl Send for ErasedTcs {}
115
116impl ErasedTcs {
117    fn new<T: Tcs + 'static>(tcs: T) -> ErasedTcs {
118        ErasedTcs {
119            address: tcs.address(),
120            tcs: Box::new(tcs),
121        }
122    }
123}
124
125impl Tcs for ErasedTcs {
126    fn address(&self) -> *mut c_void {
127        self.address
128    }
129}
130
131impl<'a, L> EnclaveBuilder<'a, L> {
132    pub fn new(enclave_path: &'a Path) -> EnclaveBuilder<'a, L> {
133        Self::new_with_source(EnclaveSource::Path(enclave_path))
134    }
135
136    pub fn new_from_memory(enclave_data: &'a [u8]) -> EnclaveBuilder<'a, L> {
137        Self::new_with_source(EnclaveSource::Data(enclave_data))
138    }
139
140    fn new_with_source(enclave: EnclaveSource<'a>) -> EnclaveBuilder<'a, L> {
141        let mut ret = EnclaveBuilder {
142            enclave,
143            attributes: None,
144            miscselect: None,
145            signature: None,
146            load_and_sign: None,
147            hash_enclave: None,
148            force_time_usercalls: true, // By default, keep the old behavior of always doing a usercall on an insecure_time call
149            loader: PhantomData,
150        };
151
152        let _ = ret.coresident_signature();
153
154        #[cfg(feature = "crypto-openssl")]
155        ret.with_dummy_signature_signer::<Hasher, _, _, _, _>(|der| {
156            PKey::private_key_from_der(der).unwrap().rsa().unwrap()
157        });
158
159        ret
160    }
161
162    fn generate_xfrm(max_ssaframesize_in_pages: u32) -> u64 {
163        fn cpuid(eax: u32, ecx: u32) -> Option<CpuidResult> {
164            #![allow(unused_unsafe)]
165            unsafe {
166                if eax <= x86_64::__get_cpuid_max(0).0 {
167                    Some(x86_64::__cpuid_count(eax, ecx))
168                } else {
169                    None
170                }
171            }
172        }
173
174        fn xgetbv0() -> u64 {
175            unsafe { arch::x86_64::_xgetbv(0) }
176        }
177
178        debug_assert_ne!(0, max_ssaframesize_in_pages);
179        let xcr0 = xgetbv0();
180
181        // See algorithm of Intel x86 dev manual Chpt 40.7.2.2
182        let xfrm = (0..64)
183            .map(|bit| {
184                let select = 0x1 << bit;
185
186                if bit == 0 || bit == 1 {
187                    return select; // Bit 0 and 1 always need to be set
188                }
189
190                if xcr0 & select == 0 {
191                    return 0;
192                }
193
194                let CpuidResult {
195                    ebx: base,
196                    eax: size,
197                    ..
198                } = match cpuid(0x0d, bit) {
199                    None | Some(CpuidResult { ebx: 0, .. }) => return 0,
200                    Some(v) => v,
201                };
202
203                if max_ssaframesize_in_pages * 0x1000 < base + size {
204                    return 0;
205                }
206
207                select
208            })
209            .fold(0, |xfrm, b| xfrm | b);
210
211        // Intel x86 dev manual Vol 3 Chpt 13.3:
212        // "Executing the XSETBV instruction causes a general-protection fault (#GP) if ECX = 0
213        // and EAX[17] ≠ EAX[18] (TILECFG and TILEDATA must be enabled together). This implies
214        // that the value of XCR0[18:17] is always either 00b or 11b."
215        // The enclave entry code executes xrstor, and we may have just cleared bit 18, so we
216        // need to correct the invariant
217        const XCR0_TILE_BITS: u64 = 0b11 << 17;
218        match xfrm & XCR0_TILE_BITS {
219            XCR0_TILE_BITS | 0 => xfrm,
220            _ => xfrm & !XCR0_TILE_BITS,
221        }
222    }
223
224    fn generate_dummy_signature(&mut self) -> Result<Sigstruct, anyhow::Error> {
225        let mut enclave = self.enclave.try_clone().unwrap();
226        let create_info = PageReader::new(&mut enclave)?;
227        let mut enclave = self.enclave.try_clone().unwrap();
228        let hash = match self.hash_enclave.take() {
229            Some(f) => f(&mut enclave)?,
230            None => {
231                return Err(format_err!(
232                    "either compile with default features or use with_dummy_signature_signer()"
233                ))
234            }
235        };
236        let mut signer = Signer::new(hash);
237
238        let attributes = self.attributes.unwrap_or_else(|| Attributes {
239            flags: AttributesFlags::DEBUG | AttributesFlags::MODE64BIT,
240            xfrm: Self::generate_xfrm(create_info.0.ecreate.ssaframesize),
241        });
242        signer
243            .attributes_flags(attributes.flags, !0)
244            .attributes_xfrm(attributes.xfrm, !0);
245
246        if let Some(miscselect) = self.miscselect {
247            signer.miscselect(miscselect, !0);
248        }
249
250        match self.load_and_sign.take() {
251            Some(f) => f(signer),
252            None => Err(format_err!(
253                "either compile with default features or use with_dummy_signature_signer()"
254            )),
255        }
256    }
257
258    pub fn dummy_signature(&mut self) -> &mut Self {
259        self.signature = None;
260        self
261    }
262
263    /// Use custom implemetations of [`SgxHashOps`] and [`SgxRsaOps`] for producing dummy signature.
264    ///
265    /// The hasher is specified through type parameter `H`, and the signer through `S`.
266    /// `load_key` is used to parse an RSA private key in DER format and should return a type `T`
267    /// that implements `AsRef<S>` where `S` is a type that implements [`SgxRsaOps`]. `E` is the
268    /// associated `Error` type of `S` when implementing [`SgxRsaOps`].
269    ///
270    /// [`SgxHashOps`]: ../sgxs/crypto/trait.SgxHashOps.html
271    /// [`SgxRsaOps`]: ../sgxs/crypto/trait.SgxRsaOps.html
272    pub fn with_dummy_signature_signer<H, S, F, E, T>(&mut self, load_key: F)
273    where
274        H: SgxHashOps,
275        E: std::error::Error + Send + Sync + 'static,
276        S: SgxRsaOps<Error = E>,
277        T: AsRef<S>,
278        F: 'static + FnOnce(&[u8]) -> T,
279    {
280        self.load_and_sign = Some(Box::new(move |signer| {
281            let key = load_key(include_bytes!("dummy.key"));
282            signer.sign::<_, H>(key.as_ref()).map_err(|e| e.into())
283        }));
284        self.hash_enclave = Some(Box::new(|stream| EnclaveHash::from_stream::<_, H>(stream)));
285    }
286
287    pub fn coresident_signature(&mut self) -> IoResult<&mut Self> {
288        if let EnclaveSource::Path(path) = self.enclave {
289            let sigfile = path.with_extension("sig");
290            self.signature(sigfile)
291        } else {
292            Err(IoError::new(
293                ErrorKind::NotFound,
294                "Can't load coresident signature for non-file enclave",
295            ))
296        }
297    }
298
299    pub fn signature<P: AsRef<Path>>(&mut self, path: P) -> IoResult<&mut Self> {
300        let mut file = File::open(path)?;
301        self.signature = Some(sigstruct::read(&mut file)?);
302        Ok(self)
303    }
304
305    pub fn sigstruct(&mut self, sigstruct: Sigstruct) -> &mut Self {
306        self.signature = Some(sigstruct);
307        self
308    }
309
310    pub fn attributes(&mut self, attributes: Attributes) -> &mut Self {
311        self.attributes = Some(attributes);
312        self
313    }
314
315    pub fn miscselect(&mut self, miscselect: Miscselect) -> &mut Self {
316        self.miscselect = Some(miscselect);
317        self
318    }
319
320    /// SGXv2 platforms allow enclaves to use the `rdtsc` instruction. This can speed up
321    /// performance significantly as enclave no longer need to call out to userspace to request the
322    /// current time. Unfortunately, older enclaves are not compatible with new enclave runners.
323    /// Also, sometimes the behavior of enclaves always calling out the userspace needs to be
324    /// simulated. This setting enforces the old behavior.
325    pub fn force_insecure_time_usercalls(&mut self, force_time_usercalls: bool) -> &mut Self {
326        self.force_time_usercalls = force_time_usercalls;
327        self
328    }
329
330    fn load<T: Load>(
331        mut self,
332        mut loader: T,
333        forward_panics: bool,
334    ) -> Result<(Vec<ErasedTcs>, *mut c_void, usize, bool, bool), anyhow::Error> {
335        let signature = match self.signature {
336            Some(sig) => sig,
337            None => self
338                .generate_dummy_signature()
339                .context("While generating dummy signature")?,
340        };
341        let attributes = self.attributes.unwrap_or(signature.attributes);
342        let miscselect = self.miscselect.unwrap_or(signature.miscselect);
343        let mapping = loader.load(&mut self.enclave, &signature, attributes, miscselect)?;
344        let force_time_usercalls = self.force_time_usercalls;
345        if mapping.tcss.is_empty() {
346            unimplemented!()
347        }
348        Ok((
349            mapping.tcss.into_iter().map(ErasedTcs::new).collect(),
350            mapping.info.address(),
351            mapping.info.size(),
352            forward_panics,
353            force_time_usercalls,
354        ))
355    }
356}
357
358impl<'a, L: Load> EnclavePlatform<enclave_runner::Command> for EnclaveBuilder<'a, L> {
359    type Loader = L;
360
361    fn build(
362        self,
363        loader: L,
364        configuration: EnclaveConfiguration,
365        cmd_configuration: CommandConfiguration,
366    ) -> Result<enclave_runner::Command, anyhow::Error> {
367        self.load(loader, configuration.forward_panics)
368            .map(|(t, a, s, fp, dti)| {
369                Command::internal_new(
370                    t,
371                    a,
372                    s,
373                    configuration.stream_router,
374                    fp,
375                    dti,
376                    cmd_configuration,
377                )
378            })
379    }
380}
381
382impl<'a, L: Load> EnclavePlatform<enclave_runner::Library> for EnclaveBuilder<'a, L> {
383    type Loader = L;
384
385    fn build(
386        self,
387        loader: L,
388        configuration: EnclaveConfiguration,
389        _: LibraryConfiguration,
390    ) -> Result<enclave_runner::Library, anyhow::Error> {
391        self.load(loader, configuration.forward_panics)
392            .map(|(t, a, s, fp, dti)| {
393                Library::internal_new(t, a, s, configuration.stream_router, fp, dti)
394            })
395    }
396}