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.
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.
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).
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.
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>
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.
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).
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.
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.
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.
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.
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.
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.
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.
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)))
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.