58 Commits
Author SHA1 Message Date
borysp c24bddd5aa [LibOS] Rework signal handling and syscall emulation
Change log (most important only):
- unify CPU context structures - now we have only one version -
  `PAL_CONTEXT` - which is shared between LibOS and PALs and it should
  depend only on the host architecture (not OS),
- syscalls emulation changed:
  - dedicated LibOS stack is now used for syscalls emulation,
  - removed one indirection level in syscalls table - now it stores
    `shim_do_*` functions directly,
- signal handling - completely rewritten:
  - all signal queues use proper locking schemes now,
  - signals are handled *only* when returning to the user app from LibOS
    or PAL,
  - nested signals are now possible,
  - the app is allowed to jump out of signal handler with the same
    sematics as on normal Linux,
  - signal altstack is now fully supported,
  - syscall restarting is now supported,
  - doing a backtrace from the signal handler works properly,
- disallow injecting host-level signals, with one exception, see
  `sys.enable_sigterm_injection` manifest option for more details.
2021-02-05 14:11:21 +01:00
Vijay Dhanraj ec4422d415 [Pal,LibOS] Add support for /sys pseudo filesystem
This commit also fixes `pseudo_name_ops::list_name()` function pointer
type: `size_t len` argument instead of `int len`.  It also adds a
regression test to exercise the newly supported /sys pseudo filesystem.
2021-01-28 23:28:29 -08:00
Vijay Dhanraj 227b0d2053 [Pal] Add str_to_ulong() common function 2021-01-28 23:18:45 -08:00
Paweł Marczewski 5ef9bdc861 [Pal] Unify debug maps
- Use the same mechanism (debug_map) in Pal/Linux and Pal/Linux-SGX.
  Previously, Pal/Linux emulated the _r_debug structure, normally
  maintained by ld.so, but that cannot be done in SGX outer PAL,
  because it's loaded by ld.so already.
- Maintain the debug maps outside of SGX enclave. This allows
  initializing them before enclave start, and potentially makes
  them easier to use.
- Initialize PAL debug map before enclave start. Previously, this
  was done from inside the enclave, so you couldn't set a
  breakpoint too early (e.g. in pal_linux_main).
- Store only load address, without list of sections. This is to
  avoid parsing the list of sections just to report them to the
  debugger. Unfortunately, the GDB version that we support still
  needs these sections, but we can retrieve them in GDB plugin.
- Move Python GDB code related to debug maps to a common file.
2021-01-18 03:49:18 +01:00
borysp ae9b49ecb1 Remove parentheses from assert argument evaluation
This helps with catching errors like `assert(x = y)`.
2020-12-31 00:40:13 +01:00
borysp d94983731b [Pal/LibOS] Add proper size alignment in slabmgr.h and slab.c 2020-12-03 23:00:54 +01:00
Stefan Berger 74718584b5 [Pal] Enable COMPILER_BARRIER for all architectures 2020-11-23 22:13:57 +01:00
Paweł Marczewski 883ae10de1 [Pal,LibOS] Clean up includes
* Make sure "stat.h" and "perm.h" are directly included where
  necessary.
* Don't include "perm.h" inside "stat.h" but require it to be
  included separately.
* Remove workarounds with __KERNEL__, __GLIBC__, defining pid_t
  directly, and reversed include order (system headers before local
  ones).
2020-11-19 14:27:06 +01:00
Paweł Marczewski b063a2ba1b [Pal,LibOS] Introduce human-readable PERM_* macros
Instead of using S_I* flags, or hardcoded octal literals, use
helpers such as PERM_rwxrwxr_x. These are proposed in a Linux patch
by Ingo Molnar: https://lwn.net/Articles/696231/
2020-11-19 14:24:59 +01:00
Paweł Marczewski 1b3de7203e [Pal,LibOS] Introduce stat.h
Keep our own copy of S_* macros.
2020-11-19 14:24:59 +01:00
Dmitrii Kuvaiskii 8eee4a4742 [LibOS,Pal,Examples,GSC,Docs] Move manifest parsing to TOML
The manifest syntax stays exactly the same, including 0 and 1
integers to denote boolean values (this is done for ease of porting
and can be fixed in future commits). The only visible change is
surrounding strings in the manifest with quotes (requirement of
TOML). All manifests and Makefiles of our tests and example apps are
ported to the new TOML syntax. Documentation is updated.
2020-11-12 05:45:07 -08:00
Vijay DhanrajandGary 3fa93cc86f [LibOS,Pal] Add sched_setaffinity/sched_getaffinity syscall support
This patch adds syscall support for setting/getting cpu affinity
of threads.

Co-authored-by: Gary <gordon.king@intel.com>
2020-11-06 22:28:32 +01:00
borysp 3014a0dd1c Miscelaneous inline asm fixes
Mostly adds missing "memory" clobber which caused some nasty bugs - gcc
optimized those inline asms and assumed values returned by them never
change.
2020-11-05 13:48:41 +01:00
Dmitrii Kuvaiskii ec270d0761 [Pal/lib] Fix data race on memory allocation in slab memory manager
Slab memory manager logic uses enlarge_slab_mgr() (renamed to more
accurate maybe_enlarge_slab_mgr()) to allocate a new chunk of memory
for its slab objects. For allocation itself, slab manager must release
the lock before system malloc and then re-acquire it. At this point
other threads may allocate memory for the same slab level. Also,
system malloc may fail to allocate large memory region, and slab logic
will fall back to allocating smaller memory region. These two issues
were not properly handled; this commit fixes these bugs.
2020-11-03 09:31:59 -08:00
Dmitrii Kuvaiskii 892bf70d58 [Pal/lib] Download and patch TOML-parser library tomlc99
This library will be used to parse Graphene manifest files written
in TOML syntax. We patch the library slightly to remove unsupported
toml_parse_file() and toml_rtod(), as well as errno() handling.
2020-11-03 05:07:03 -08:00
Dmitrii Kuvaiskii 28050d051e [Pal/lib] Add strtoll() and funcs from ctype.h for string manipulation
These functions are required by the TOML C source code, which will
be embedded into Graphene in a future commit.
2020-10-29 02:02:05 -07:00
Michał Kowalczyk 4e26fe7e32 [LibOS+Pal] Clean up string handling utilities 2020-10-16 16:18:43 +02:00
Dmitrii Kuvaiskii 1ac4da9c0f [Pal/lib] Remove unused functions in manifest-config struct
Also move parse_size_str() to a more appropriate atoi.c file.
This is in preparation for replacing the old ad-hoc manifest
syntax with the TOML syntax.
2020-10-16 02:27:11 -07:00
Dmitrii Kuvaiskii bd5d56b1a0 [Pal/lib] Add strncmp() and strspn() string manipulation funcs
These functions are required by the TOML C source code, which will be
embedded into Graphene in a future commit.
2020-10-15 08:27:43 -07:00
Michał Kowalczyk e4d228d4b9 [LibOS+Pal] Clean up file comments
I don't know any reason why would stating the file name we're in be
helpful for anything. Moreover, this information was incorrect in a few
cases (copy-paste bugs, probably).

Additionally, a few minor type/formatting fixes included.
2020-10-14 20:01:13 +02:00
Dmitrii Kuvaiskii 70c7264f03 [Pal/Linux-SGX] Add manifest option loader.pal_internal_mem_size
Previously, Graphene preallocated 64MB for PAL internal metadata
like trusted/protected files metadata, handles metadata, etc.
If this limit was depleted, Graphene loudly failed, and the user
had no option but to change constant in source code and rebuild
Graphene. This commit adds the manifest option
`loader.pal_internal_mem_size` to allow increasing this limit.
2020-09-30 08:14:38 -07:00
Dmitrii Kuvaiskii 6f7b07b1d2 [LibOS,Pal] Move parse_int to Pal/lib/config.c 2020-09-30 08:11:28 -07:00
Michał Kowalczyk ad477ec7bf Reformat repository to our C formatting rules (final iteration) 2020-09-15 02:00:54 +02:00
Paweł Marczewski 48a7231ce3 [Pal] Rewrite IS_ARRAY macro
The (((void *) &(arg)) == ((void *) (arg))) trick does not work
with clang, because clang does not accept it as a constant
expression and doesn't allow using it in static_assert.

I was not able to write a generic (any type) IS_ARRAY macro
compatible with static_assert, but here we only need to check for
arrays of chars and uint8_t-s.
2020-09-10 16:29:47 +02:00
Dmitrii Kuvaiskii d0a34dd645 [LibOS,Pal/lib] Partially refactor slabmgr and memmgr in Pal/lib
In particular, this commit:
- Removes SLAB_DEBUG macros and corresponding code.
- Fixes memory leak in memmgr's enlarge_mem_mgr() by removing
  __set_free_mem_area() call.
- Fixes bug of double-free of the very first memmgr area in
  destroy_mem_mgr().
- De-duplicates "get new memory object" code by changing
  get_mem_obj_from_mgr() to call get_mem_obj_from_mgr_enlarge().
- Simplifies and improves performance of free_mem_obj_to_mgr() since
  there is no need to double-check that the object belongs to one of
  the memmgr's areas because we already check memory_migrated().
- Fixes bug of free of wrong object in slabmgr's destroy_slab_mgr().
2020-08-28 08:43:24 -07:00
borysp 6695458cc9 [LibOS] Fix memory corruption in free_str_obj on migrated memory 2020-08-19 15:13:36 +02:00
Michał Kowalczyk bdcf29ba33 [Pal] Simplify memset and clean up other string ops 2020-07-23 19:48:15 +02:00
Michał Kowalczyk 7b0c160296 [Pal] Protect env from untrusted world 2020-07-13 22:05:04 +02:00
Michał Kowalczyk f948351243 [Pal] Fix out-of-bounds read in strstartswith_static 2020-07-13 22:05:04 +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 cb7d430655 Remove always_inline where not needed 2020-07-05 17:24:06 +02:00
Stefan Berger 91709ac2fe [LibOS,Pal] Set weak parameter in atomics operations to false
The `weak` parameter has no influence on x86, but on ppc64 it causes
spurious faults due to a missing check on flags (missing `bne`
instruction).
2020-07-01 22:08:52 +00:00
Stefan Berger ab4f14df33 [Pal+LibOS] Drop wrappers from atomic.h
This is the first step in removing this obsolete header.
Additionally, AtomicMath test is removed, as it became obsolete after
these changes (and was relying on undefined behaviors anyway).
2020-06-26 14:31:23 +02:00
Michał Kowalczyk 0f55c6de04 Use SPDX IDs for licenses in source files 2020-06-13 03:25:33 +02:00
Stefan Berger bf155c7bc9 [Pal/lib] Implement strstr() 2020-06-10 00:27:10 +00:00
Stefan Berger 8e06bbb97a [PAL+LibOS] Replace inline assembly for atomics with atomic built-ins 2020-06-05 11:52:08 +02:00
Stefan Berger e379f21419 Implement cpu_pause() in arch-specific cpu.h
... instead of using inline asm. Required for portability to other
architectures.
2020-05-27 22:19:33 +02:00
Stefan Berger c0163a6036 [Pal] Move x86_64-specific atomic.h to Pal/include/lib/x86_64
Also, adapt the Makefiles to include the new directory in CFLAGS.
2020-05-09 14:43:17 +02:00
borysp c9742cb9cd Add READ_ONCE and WRITE_ONCE macros
Sometimes we need to prevent the compiler from reading or writing to
a memory location twice to prevent certain TOCTOU bugs. This can now
be achieved by using the introduced macros and this commit does so in
enclave_ocalls.c for Linux-SGX.
2020-05-08 01:43:12 +02:00
borysp 6cb111b6d1 [LibOS] Completely rework LibOS VMA bookkeeping
This commit completely reworks VMA subsystem along with its usages.
New version should be: cleaner (easier to maintain), faster and allow
for bookkeeping requests from Pal.
It also fixes some bugs and inconsistencies found in the process and
changes brk and mmap/munmap implementations (at least partially).
2020-05-02 16:22:00 +02:00
Michał Kowalczyk 5e2eee0086 [Pal] Remove likely/unlikely macros 2020-04-20 22:00:58 +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
borysp bbc2387d76 [Pal/lib] Add missing root node update to avl_tree_swap_node 2020-04-15 12:20:29 +02: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 2b4e50c9bb [LibOS] Clean up PAUSE macros 2020-04-10 19:23:42 -07:00
Dmitrii Kuvaiskii 7e20a8044c [LibOS,Pal] Replace 32-bit pipeid with 256-bit pipe name
Previously, Graphene used the notation "pipe:<uint32_t>" to emulate
pipes, socketpairs, and UNIX domain sockets. In particular, pipes
and socketpairs received random integer IDs, and sockets received
deterministic integer IDs. However, 32-bit randomly generated IDs
may collide quite often. Since pipe IDs/names should *not* repeat
(otherwise e.g. derived crypto keys will be reused), this commit
changes pipe IDs (pipeid) from uint32_t to char[96], and pipe IDs
(names) become 256-bit random sequences.
2020-04-10 17:39:11 -07:00
borysp bf6be81310 [Pal/lib] Add avl_tree_first and avl_tree_last 2020-04-10 23:11:01 +02:00
Michał Kowalczyk e58933a971 [Pal/lib] Simplify static_str* macros 2020-04-02 01:46:48 +02:00
Michał Kowalczyk 727c2ab049 Fix typos 2020-04-02 01:46:48 +02:00
borysp 5f1e6ca317 [Pal/lib] Add generic trees (AVL) 2020-04-01 02:46:33 +02:00