38 Commits
Author SHA1 Message Date
Michał Kowalczyk ad477ec7bf Reformat repository to our C formatting rules (final iteration) 2020-09-15 02:00:54 +02:00
Rafał Wojdyła cf84489cd5 [Linux-SGX] Add protected files implementation
Protected files (PF) are a new type of file that can be specified in
the manifest (SGX only). They are encrypted on disk and transparently
decrypted when accessed by the Graphene payload.

Other features:
- data is integrity protected (tamper resistance)
- file swap protection (a PF can only be accessed when in a specific path)
- transparency (Graphene payload sees PFs as regular files, no need to modify
  the payload)

See Linux-SGX/protected-files directory for implementation. PF format is
based on protected files from the SGX SDK:
https://github.com/intel/linux-sgx/tree/master/sdk/protected_fs

The following new manifest elements are added:

sgx.protected_files_key = <16-byte hex value>
sgx.protected_files.<name> = file:<host path>

sgx.protected_files_key specifies the encryption key and is only a temporary
implementation. This key should be provisioned with local/remote attestation
in the future.

Paths specifying PF entries can be files or directories. If a directory is
specified, all files/directories within are registered as protected
recursively (and are expected to be encrypted in the PF format).

Linux-SGX/tools directory contains the pf_crypt utility that converts files
to/from the protected format.
2020-07-13 20:19:42 +02:00
Michał Kowalczyk 0f55c6de04 Use SPDX IDs for licenses in source files 2020-06-13 03:25:33 +02:00
Dmitrii Kuvaiskii a958ff0bca [LibOS,Pal] Emulate SIGPIPE via creating and sending this signal on EPIPE
Previously, Graphene simply forwarded SIGPIPE generated by the host to
LibOS/app. Unfortunately, SIGPIPE generation is a process-wide feature
and there is no portable way to restrict it only to a subset of pipes,
UNIX domain sockets, etc. This led to sporadic Graphene failures
because Graphene's internal use of pipes and sockets may result in an
unexpected (to application) SIGPIPE.

This commit removes the forwarding of SIGPIPE. Instead, PALs explicitly
ignore SIGPIPE. This forces the host to return EPIPE error code, which
is checked only on a subset of LibOS handles (the ones created by the
app), and if required, LibOS generates a SIGPIPE for the application.

While adding this logic, the whole PAL exception code was refactored,
both in Linux and Linux-SGX. Tests for SIGPIPE are now enabled for
both Linux and Linux-SGX PALs.
2020-06-10 22:30:40 +00:00
Stefan Berger fff09c00af Add -Wmissing-prototypes to CFLAGS and deal with the fallout 2020-06-04 17:22:19 +02:00
Stefan Berger ddab51259c [PAL] Move random number call to arch-specific inline function
Also include the header with the prototype of mbedtls_to_pal_error
function to verify it's the same as defined in mbedtls_adapter.c.
2020-05-29 11:43:21 +02:00
Stefan Berger 81cc69e519 [Pal/lib] Add -mrdrnd to CFLAGS only on x86_64 2020-05-21 07:08:47 +00:00
Dmitrii Kuvaiskii 4087cdbe2a [Pal/lib] Return PAL_ERROR_TRYAGAIN on try-again read/write errors in mbedTLS
Previously, lib_SSLRead() and lib_SSLWrite() returned PAL_ERROR_DENIED
on any error, even on benign try-again errors from mbedTLS. This led
to LibOS returning EACCES to the application which doesn't expect such
error code. This commit converts benign try-again errors into
corresponding PAL_ERROR_TRYAGAIN errors.
2020-05-08 16:09:51 -07:00
Dmitrii Kuvaiskii 9859fa14cf [Pal/lib] Continue TLS handshake if read/write failed with EAGAIN/EWOULDBLOCK
Linux-SGX PAL uses mbedTLS sessions for encrypted IPC. This requires
a TLS handshake on pipe/socketpair creation. Previously, if the pipe
was created with O_NONBLOCK, read/write callbacks for mbedTLS session
could return EAGAIN or EWOULDBLOCK if the pipe was occupied. We
forgot to check for these error codes, and TLS handshake failed as a
result on the first EAGAIN/EWOULDBLOCK (detected on NodeJS example).
These error codes are actually benign, and Graphene should simply
ask mbedTLS to retry read/write.
2020-04-23 15:01:27 +02:00
Dmitrii Kuvaiskii 8d9f9f567f [Pal/Linux-SGX] Put TLS-context init logic in critical section
mbedTLS configuration used in Graphene is not thread-safe (because
this would require the use of a threading library like pthread which
is not possible in the LibOS/Pal layers). However, some mbedTLS
functions use shared state, in particular TLS context initialization
functions. This led to data races during encrypted-pipe creation,
since it requires two threads performing a TLS handshake. This commit
refactors TLS init into SSLInit (not thread-safe) and SSLHandshake
(thread-safe) and adds spinlocks around SSLInit to protect the racy
mbedTLS logic.
2020-04-17 01:30:00 -07:00
Dmitrii Kuvaiskii caf1263070 [Pal/Linux-SGX] Encrypt all pipes/socketpairs with TLS-PSK
Previously, Linux-SGX PAL did not encrypt pipe/socketpair
communication (only process checkpoint send/receive was encrypted).
This commit encrypts all pipe/socketpair IPC between threads of
the same enclave and between enclave processes. In particular, all
offsprings of the "first" enclave inherit the same master key and
derive IPC session keys from this master key based on pipe name.
When two pipe/socketpair endpoints are first created, they establish
a TLS-PSK session via intra-enclave handshake (requires a spawn of
an intermediate enclave thread). During clone/fork/exec, endpoints'
TLS contexts are serialized and sent to the child that deserializes
them (using mbedtls_ssl_context_{save,load} functions).

Note that multicast pipes (with more than two communicating entities)
are not supported since TLS protocol doesn't support it.

This commit modifies the PAL `SendHandle` test to correctly test
pipe communication, as well as adds the LibOS `pipe` test.
2020-04-13 16:18:58 -07:00
Michał Kowalczyk e14d133cd7 [Pal/lib] Delete unused wolfssl_dh.c 2020-04-02 01:46:48 +02:00
Michał Kowalczyk 069e3069f8 [Pal/lib] Rewrite lib_Base64{En,De}code documentation 2020-04-02 01:46:48 +02:00
Dmitrii Kuvaiskii 3e06d28d97 [Pal/lib] Update mbedTLS to version 2.21.0
Previous version of mbedTLS used in Graphene (v2.16.3) is old and
does not have TLS-context serialization functionality. This commit
updates mbedTLS to v2.21.0 (released February 2020); this version
has mbedtls_ssl_session_save() and mbedtls_ssl_session_load() for
TLS context serialization. These functions will be needed for IPC
encryption.
2020-03-20 13:26:16 +01:00
Dmitrii Kuvaiskii 8d76f9d3fb [Pal/Linux-SGX] Remove remote attestation functionality
Previously, Graphene with SGX performed self-remote attestation
by retrieving the SGX quote from the Quoting Enclave, sending it
to the Intel Attestation Service via Curl (in an HTTPS request),
and parsing the received from IAS remote-attestation certificate.

This self-attestation functionality is meaningless and is removed.
Moreover, since EPID-based remote attestation requires client key
(Ocp-Apim-Subscription-Key), and this key must be kept secret,
specifying it in the Graphene manifest (as was done previously) is
insecure. Therefore, the whole remote attestation functionality is
moved out of Graphene and to another trusted party (or to the app
on top of Graphene). Only the SGX quote retrieval from the Quoting
Enclave is kept in Graphene.
2020-03-19 18:08:37 +00:00
Isaku Yamahata 551b32a036 [Pal/Linux-SGX] Fix ocall_{read,write,recv,send} to return ssize_t
Also, all users of these OCALLs are modified to operate on ssize_t
return values, including LIB_SSL_CONTEXT/mbedTLS callbacks.
2020-03-02 17:36:07 -08:00
Dmitrii Kuvaiskii 735f54d22c [LibOS,Pal/{lib,Linux-SGX}] Add TLS-PSK protection to process checkpoint IPC
Previously, Graphene-SGX did not protect send/recv of checkpoint from
parent to child. This leaked all memory contents of the parent process.

This commit adds TLS-PSK (TLS with Pre-Shared Key) wrapper for process
communication. Graphene-SGX already has the logic for SGX-based local
attestation and generation of the shared key for each parent <-> child
communication channel via Diffie–Hellman key exchange. This commit
uses this pre-shared key to create an mbedTLS-based session based on
UNIX domain socketpair (parent.process.stream <-> child.process.stream).

_DkStreamSecure{Init,Free,Read,Write} internal Linux-SGX PAL functions
are added and used during child process creation and sending of the
parent-generated checkpoint. These functions are backed by crypto-layer
lib_SSL{Init,Free,Read,Write} functions which in turn use mbedTLS.
Configuration of mbedTLS is expanded to support TLS-PSK; note that for
entropy source we use only rdrand instruction inside SGX enclave (i.e.,
no untrusted host-platform sources of entropy). The only ciphersuite
currently supported for IPC is MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256.

This commit adds protection only for checkpoint IPC. After the checkpoint
is sent/received, the parent/child processes downgrade their sockets to
plain non-secure ones (by disabling TLS). This is done because child
may spawn a grandchild that also wants to communicate with grandparent,
but it is impossible for multiple processes to share same TLS context.

Additionally, this commit sanitizes "process" PAL handles during send-
handle checkpoint send (via DkSendHandle): shared key and TLS context
are zeroed out.
2020-01-28 13:05:48 -08:00
Thomas Knauth 0f0eb95732 [Pal/lib] Fetch and build mbedtls 2.16.3 from official repo 2019-12-19 21:03:56 -08:00
Rafał Wojdyła a04e71c7fa [Pal] Fix errors returned from mbedTLS crypto adapter
Most errors returned from mbedTLS functions were not converted to PAL errors.
This commit adds a separate PAL-error code block for crypto errors.
2019-10-03 16:42:50 -07:00
Chia-Che Tsai 6c4f21cba2 [Pal/Linux-SGX] Simple remote attestation framework
This is the minimalistic implementation of the remote attestation
framework. The framework conducts the following steps during
start-up to verify the authenticity of the SGX platform:

1. Connect to aesmd service to retrieve platform info (targetinfo)
   of the Quoting Enclave (QE) before enclave creation.
2. Prepare the SGX report inside enclave (during initialization):
   - Read SPID (service provider ID) from sgx.ra_client_spid in
     manifest.
   - Get an SGX report for local attestation to QE.
   - Generate a random 16-byte nonce for freshness.
   - Perform an OCALL for retrieving the quote.
3. Gather attestation data (QE report, QE quote, IAS report, signature,
   certificate chain) outside of enclave:
   - Connect to aesmd to retrieve the QE quote; aesmd also returns
     QE report.
   - Connect to Intel Attestation Service using curl. A client
     subscription key (specified via sgx.ra_client_key in manifest)
     is required to authenticate the HTTPS connection.
   - Get the IAS report, signature, and certificate chain from IAS.
     Print out the attestation result.
   - Return all this attestation data back to the enclave.
2019-09-10 21:00:49 -07:00
Michał Kowalczyk de42ebabe1 Reformat repository to our clang-format rules 2019-09-09 22:11:23 +02:00
Li Lei f4507c3ddd [Pal/Linux-SGX] Turn on -Wsign-compare and fix the produced warnings 2019-05-30 01:57:43 +02:00
Michał Kowalczyk 17102eab9d Change DkRandomBitsRead interface and fix error checking
Now it returns 0 on success and -PAL_* on error.
2019-04-30 22:26:08 +02:00
Li Lei 195c2b1f7b [PAL] Enable Wunsed-parameter warning as part of Wextra 2019-04-03 14:27:43 +02:00
Don Porter 03cb0e0bb3 Address a TOCTTOU vulnerability in SGX read/map (#131)
* Fix a bug where configuration error ends up doing a huge allocation, rather than catching the error.  Add some documentation to the slabmgr code.

* Add a unit test and some documentation to answer the question in issue #107.  I can't see how offset and map_start would end up being different.

* Rewrite of SGX file_map to remove TOCTTTOU now passes all unit tests

* Apply a similar fix to file_read.  

* Factor complicated verification code into a common helper routine.

* Adjust the memory copying strategy so that all bits in the returned buffed are exactly the same bits as verified in the trusted, scratch buffer.

* Fixing the TOCTOU issue in file checking

* Adding comments for load_trusted_file() and copy_and_check_trusted_file(); Deprecate the old design

* Documenting the file checking mechanism
2018-06-25 07:54:40 -07:00
Don Porter 0433461802 License change to LGPL (#140)
* Update all headers to reflect LGPL license.

* Add submodule for gcc test inputs

* Add submodule for lmbench-2.5

* Fix Travis build with submodules

* Migrate driver to sub repository

* Migrate driver to sub repository
2017-12-08 10:02:36 -08:00
Don Porter d04f172e89 Replace the atomics implementation (#83)
Replace the atomics implementation.

* Change the PAL Semaphore to a Mutex, and fix some issues in the Mutex implementations

* Tweak the layout of a PAL Handle

* Rework some of the IPC helper synchronization

* Taking out waitpid03 - it is flaky, even on the commit where it was added to the PASSED list.
2017-12-07 11:30:48 -08:00
Don Porter 62f07c7181 Reimplement directory caching (#78)
Replace the directory cache implementation.

* Fix unix domain socket lookup.

* Fix a bug in the getdents EINVAL case

* remove profiling code in dcache.c; cleaning up some style issues

* adding a note to the recursive path_lookupat() code

* remove a few compilation warnings

* Apply Chia-Che's suggested fixes; add more now-passing cases to LTP nice list.

* Ref counting bug for /proc/self/fd/.  Document and implement expected behavior in relevant helper function.
2017-12-07 08:51:30 -08:00
Don Porter 62302518e8 Remove the wolfssl code (#77)
Delete wolfssl from the code base

A few minor issues with build, CI timeouts, and gitignores.
2017-11-15 16:58:10 +00:00
Don Porter 8d99e037ea Replace rsa implementation, from wolfssl to mbedtls. (#76)
Switch the RSA implementation from wolfssl to mbedtls
2017-10-23 01:41:24 +01:00
Don Porter b20e34cf5f Switch the AES-CMAC implementation from wolfssl to mbedtls. (#75)
* Switch the AES-CMAC implementation from wolfssl to mbedtls
2017-10-22 17:16:12 +01:00
Don Porter 8add0b94f7 Replace the wolfssl Diffie Hellman implementation with mbedtls (#74)
* Switch to the mbedtls implementation of Diffie Hellman
2017-10-22 15:29:00 +01:00
Don Porter 2e9438f71c Replace Linux List code with a new implementation (#71)
* Remove the Linux linked list implementation, replace with a new implementation that adds some type-checking that list pointers (heads) and entries/nodes match.

* Fix the debug build by consolidating assertions into one header
2017-10-21 13:35:26 +01:00
Don Porter 64a57f2241 Malloc fixes (#70)
* Several memory allocation fixes, primarily motivated by the fact that the Diffie-Hellman implementation in mbedtls is sensitive to misaligned allocations.  All malloc's are now 16-byte aligned.  This PR has several other points where remalloc was used instead of realloc, or memory needed to be zeroed upon allocation.  Finally, this PR also standardizes the definition of assert across layers, so that code in the lib directory can both use assertions and link properly in the PAL and shim.
2017-09-23 17:17:40 +01:00
Don Porter e6189f64a5 Make requested name changes 2017-09-22 16:35:11 -04:00
Don Porter 49c4a68f51 Recover the abilty to build with wolfssl, if one so desires 2017-09-22 12:07:42 -04:00
Don Porter ca981b3bcd Merge branch 'master' into sha256 2017-08-25 17:25:29 -04:00
Don Porter fd73358f59 Swap out the wolfssl SHA256 implementation with mbedtls. This also introduces a wrapper layer for pal crypto functions, which should make experimenting with different libraries easier. 2017-07-31 20:54:40 -04:00