42 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
borysp 287762501b Add infinite loop behind each call to exit syscall
This commit additionally replaces all `while (true) {}` inf loops with
`die_or_inf_loop` which either crashes the process or loops infinitely
and is not an undefined behavior like the original one (C disallows inf
loops without side effects).
2021-01-15 16:35:03 +01:00
Stefan Berger fff5e9818e [PAL] Move PAGE_SIZE to cpu.h 2021-01-15 00:30:53 +01:00
Michał Kowalczyk 3d31f2d18d Introduce one, central manifest, zero-config children and constant MRENCLAVE
This is the next part of the great loader rework, with a lot of breaking changes:

- Complete removal of the "trusted children" thing - now children
  processes can be spawned arbitrarily and from arbitrary mountpoint
  types, without any additional configuration needed.

- There's a new, required option in the manifest: `libos.entrypoint` - it
  specifies the URI to the entry binary in the first process. There's no
  need anymore to name the manifest and the first binary identically.

- On SGX, the main binary is not measured in MRENCLAVE anymore - only
  PAL, LibOS and the manifest are measured. This is enough to bind
  MRENCLAVE to a specific entrypoint user executable if wanted - it
  just has to be mounted as a trusted file.

- All Graphene SGX enclaves have now exactly the same MRENCLAVE. This is
  a hash of a "Graphene stub", which can "fork" into one of two states
  in runtime: initial process or child. The initial process creates a
  new "Graphene namespace" with a clean state, it can also be attested
  remotely (contrary to child processes). The initial process can spawn
  children processes by spawning a Graphene stub and directing it to
  start in the child mode. It then attests it locally, and if
  successful, establishes an encrypted pipe, "connects" to its own
  namespace and treats as trusted (including sending protected files
  key).

- Now, there's only one, central manifest describing the initial state
  of a Graphene instance which can be spawned from it (previously, each
  process required a separate manifest which could have different
  configuration - which wasn't actually supported and didn't make sense
  design-wise). One downside of central manifests is that all processes
  require the same enclave configuration (e.g. size), but that was
  already the case so far because of broken checkpointing code. Also,
  this is only a temporary problem, which will cease to exist after the
  introduction of EDMM.

- `sgx.static_address` was renamed to `sgx.nonpie_binary` and now has to
  be inserted manually by users (`sgx_sign` tools doesn't know about the
  binaries run inside, which can be even provided or generated in
  runtime by the user's workload).

- Caveat: the memory gap for non-PIE executables was removed because it
  requires adding a new option to the manifest to be cleanly
  implemented. This is left for some future loader rework PR.
2021-01-12 19:53:24 +01:00
Stefan Berger d0862242f5 [Pal/Linux] Set stack protector canary in an arch-specific func 2021-01-11 23:46:44 -08:00
Dmitrii KuvaiskiiandIsaku Yamahata 3cb2112b05 [LibOS,Pal] Use GCC's stack protector in LibOS and PAL functions
GCC (and other compilers, e.g. Clang) provide a stack protector
feature to detect stack corruptions. This is achieved by storing
a 64-bit canary value on the stack frame on function entry and
verifying this value on function exit. Previously, Graphene disabled
stack protector completely. This commit enables it in LibOS and PAL
code (only if `-mstack-protector` feature is supported by compiler).

The stack protector uses a random per-thread canary stored in the
TLS/TCB of each thread. Each PAL implementation must follow the
rule that TLS/TCB is accessed via the GS register and that the offset
of canary in TLS/TCB is 0x8. Since LibOS re-uses TLS/TCB of the PAL,
there is no need for additional enabling at the LibOS layer.

Since `-mstack-protector` feature is architecture-specific, it is
currently enabled only for x86-64 (and above rules on using gs:[0x8]
to access the canary apply only to x86-64).

Co-authored-by: Isaku Yamahata <isaku.yamahata@gmail.com>
2021-01-07 05:19:14 -08:00
Dmitrii Kuvaiskii dd85aef596 [Pal/Linux-SGX] Fix type cast issue during async signal handling
Untrusted Linux-SGX PAL handles host-level asynchronous signals by
emulating the interrupt (-EINTR) of the pending OCALL. Unfortunately,
there was a type cast issue such that int32_t -EINTR (`-4`) was
casted to a positive uint64_t and then OCALL consumed this positive
number instead of erroring out on -EINTR. This commit adds explicit
type casting to fix this bug.
2020-12-28 01:28:26 -08:00
Dmitrii Kuvaiskii 66d8e61453 [Pal/Linux] Move pal_linux_defs.h under Pal/Linux and remove its macros
Previously, pal_linux_defs.h was located in a common include dir,
though it was only used by Linux PAL (Linux-SGX PAL has its own
and very different version of this header). This commit moves this
header under Pal/Linux and performs a cleanup of its macros without
logic changes. A couple other tiny cleanups is done as part of this
commit: moving x86-64 non-SGX instruction wrappers into "cpu.h",
renaming USER_ADDRESS_LOWEST to DEFAULT_HEAP_MIN for consistency with
Linux-SGX, removing redundant _DkSystemTimeQueryEarly(), increasing
THREAD_STACK_SIZE from 8KB to 64KB (to be on the safe side).
2020-12-23 07:46:57 -08:00
Michał Kowalczyk cffd457698 [Pal] Fix return type in INTERNAL_SYSCALL macros 2020-12-05 01:46:03 +01: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
Vijay Dhanraj 41a87ad2c8 [Pal,LibOS] Add physical id and fix cpu cores in /proc/cpuinfo
Applications tend to use `/proc/cpuinfo` to get the `cpu cores`
and `physical id` for computing number of physical cores in a
socket. Currently `cpu cores` field is incorrectly implemented as
it is set to number of logical processors online and `physical id`
isn't implemented. This patch addresses both of these issues.
2020-11-06 00:25:03 +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
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
Michał Kowalczyk ad477ec7bf Reformat repository to our C formatting rules (final iteration) 2020-09-15 02:00:54 +02:00
Michał Kowalczyk 19f0e486a6 [Pal] Prefix globals with g_ (part 2) 2020-07-23 21:18:32 +02:00
Gary f24bb06fdd [Pal/Linux-SGX] Optimize _DkSystemTimeQuery performance using TSC
This patch optimizes _DkSystemTimeQuery() using RDTSC instead of
ocall_gettime.

This optimization won't take effect if there is no reliable TSC source
available to use i.e. nonstop/invariant TSC.

The TSC drift is bound by syncing with system clock periodically.
2020-07-16 01:34:34 +02:00
Stefan Berger 08311935b5 [Pal] Move x86_64-specific CPUID into cpu.h, pal-arch.h, db_main-x86_64.c 2020-06-18 23:16:23 +00:00
Stefan Berger 4ee3e8242b [LibOS,Pal] Implement arch-specific pal_context_has_user_pagefault() 2020-06-16 19:55:04 +00:00
Michał Kowalczyk 0f55c6de04 Use SPDX IDs for licenses in source files 2020-06-13 03:25:33 +02:00
Stefan Berger 7b74da7e05 [Pal/Linux-SGX] Implement pal_ucontext_set_function_parameters() 2020-06-11 10:57:02 -07:00
Stefan Berger 7383da296a [Pal/{Linux,Linux-SGX}] Implement pal_ucontext_get_ip() 2020-06-11 10:57:02 -07: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 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 9fa7fd5186 [PAL] Change comment in code to proper filename dl-machine.h 2020-05-29 11:43:21 +02:00
Stefan Berger c1c085ca15 Remove duplicate ucontext from shim_types-arch.h
Use the ucontext from PAL instead. LibOS now has access to the inline
functions for copying PAL_CONTEXT to ucontext and vice versa and we use
them where possible.

We need to introduce a ucontext.h for Skeleton. It does need ucontext
to be defined for being able to compile shim_signal.c. The easiest way
to achieve this is to rely on Linux's ucontext.h.

SGX can reuse Linux's ucontext.h and sigcontext.h.
2020-05-28 21:08:49 +02:00
Stefan Berger 94480ba1c6 [PAL+LibOS] Wrap accesses to PAL_CONTEXT's instruction poiner
Implement and use pal_context_set_ip and pal_context_get_ip to access
the instruction pointer.
2020-05-28 12:38:41 +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 122bc94a80 [Pal] Move x86_64-specific DEFAULT_OBJECT_EXEC_ADDR to pal-arch.h 2020-05-20 03:07:58 +02:00
Stefan Berger 9e6af37f42 [Pal] Move pal_linux_defs.h to include/arch/x86_64/Linux 2020-05-20 03:07:56 +02:00
Stefan Berger e0ca25a413 [Pal] Move x86_64 syscall arch_prctl into Linux-specific pal_set_tcb()
Move the Linux x86_64 specific syscall arch_prctrl into a new inline function
pal_set_tcb located in include/arch/x86_64/Linux/pal_host-arch.h. The SGX and
Skeleton builds now also need a pal_host-arch.h file, empty for now.
2020-05-18 21:26:28 -07:00
Stefan Berger 54d6c87589 [Pal] Implement functions for copying to and from PAL_CONTEXT
Implement inline functions for copying CPU context between PAL_CONTEXT
and ucontext_t. Add an assert to make sure that the number of registers
in both contexts is the same.
2020-05-15 19:27:43 -07:00
Stefan Berger f781cc9192 [Pal] Move Linux elf-x86_64.h to include/arch/x86_64/Linux/elf-arch.h
Also, adapt #includes where needed. Avoid the name elf.h to avoid
clashes. We do not touch the Linux-SGX/elf-x86_64.h file since it is
slightly different.
2020-05-13 14:53:09 +02:00
Stefan Berger a179c79e3c [Pal] Factor out arch-specific TCB part to pal-arch.h 2020-05-12 21:22:04 +02:00
Stefan Berger 14db27c34a [Pal] Define PAGE_SIZE in pal-arch.h and use in regression test
Define the typically used PAGE_SIZE in pal-arch.h and use it in the
File.c regression test that maps memory of one page.
2020-05-12 18:31:35 +02:00
Stefan Berger 2e3e7c526c [Pal] Move PRESET_PAGESIZE into arch/x86_64/pal-arch.h 2020-05-12 18:31:35 +02:00
Stefan Berger 98187f6b93 [Pal] Move Linux x86_64 specific ucontext.h to arch/x86_64/Linux
Move the Linux x86_64 specific ucontext.h to arch/x86_64/Linux.
The SGX and non-SGX files are identical.
2020-05-12 06:17:41 +00:00
Stefan Berger ea2c54d39c [Pal] Move x86-64 sigcontext.h and sigset.h to arch/x86_64/Linux
Move the x86-64-specific sigcontext header files to arch/x86_64/Linux.
The SGX and non-SGX files are identical.

We are also moving sigset.h since on ppc64 the following defines are
different:

x86_64: #define _SIGSET_NWORDS (64 / (8 * sizeof(unsigned long int)))
ppc64:  #define _SIGSET_NWORDS (1024 / (8 * sizeof (unsigned long int)))
2020-05-12 03:08:15 +02:00
Stefan Berger 31b752a335 [Pal] Separately declare struct PAL_CONTEXT_ for Doxygen
This is required to cleanly pass the Doxygen build on Ubuntu 18.04.
2020-05-09 14:43:17 +02:00
Stefan Berger 8bbe609bb9 [Pal] Move x86_64/Linux sysdep-x86_64.h to arch/x86_64/Linux/
Also, adapt the Makefiles to add the arch specific directory to the CFLAGS.
The Linux-SGX sysdep-x86_64.h was identical and could therefore be removed.
2020-05-09 14:43:17 +02:00
Stefan Berger 74dc2ebfc3 [Pal] Move x86_64/Linux specifics from pal.h into arch/x86_64/pal-arch.h
Also, adapt the Makefiles to add the directory to the CFLAGS.
2020-05-09 14:43:17 +02:00
Stefan Berger 5354f12a17 [Pal] Move host-generic dl-machine-x86_64.h to Pal/include/arch/x86_64
Also, adapt the Makefiles and INPUT paths in Doxyfile-pal to include the
new directory.
2020-05-09 14:43:17 +02:00