Commit Graph
208 Commits
Author SHA1 Message Date
Dmitrii Kuvaiskii 46d21eccaf [LibOS,Pal] Add MSG_DONTWAIT and SCM_RIGHTS for recvmsg/sendmsg
This commit improves the emulation of recvfrom/sendfrom,
recvmsg/sendmsg, and recvmmsg/sendmmsg system calls. In particular,
MSG_DONTWAIT flag is allowed though not really emulated (benign in
most cases). Also, it is possible now to send/receive FDs via
SCM_RIGHTS on a UNIX domain socket (only send/recv of pipes and UNIX
domain sockets is currently supported). Corresponding LibOS test
is added.
2020-05-15 23:36:11 +00:00
Michał Kowalczyk 297b959bcb Use proper integer types in socket-related syscalls and ocalls
This contains two socket-related changes, which would be hard to factor
out into two commits: (because of compilation warnings)

	- Making ocalls use size_t in their interface. It's not the same as
	  Linux syscall interface anyway, so why not make it sane?
	- Fixing sycalls arguments types to match the ones use by kernel.
	  Note: there's no such type as "socklen_t" in the kernel, so it got
	  removed.
2020-05-14 12:21:20 +02:00
borysp f78c67cc47 [LibOS] Remove simple threads
They are not really used anywhere and have broken refcounting.
2020-05-13 19:42:40 +02:00
Stefan Berger 4a53659df1 [LibOS] Move dl-machine-x86_64.h to arch-specific dir
Move the shim's dl-machine-x86_64.h to include/arch/x86_64/shim_dl-machine.h.
Rename the file to avoid name clashes with Pal's dl-machine.h.
2020-05-13 14:53:09 +02:00
Stefan Berger da54b0d413 [LibOS] Move shim's ldsodefs.h to include/elf/ldsodefs.h
As a preparatory step for the next patch, move the shim's ldsodefs.h
to include/elf/ldsodefs.h.
2020-05-13 14:53:09 +02:00
borysp 743cc2dba1 [LibOS] Remove what's left of checkpointing to files
PR #1424 partially removed checkpointing to files, this commit removes
the rest.
2020-05-12 01:19:38 +02:00
Dmitrii Kuvaiskii d1fe5ec273 [LibOS] Remove DkSynchronizationObjectWait() from wait_event()
LibOS events create_event() / set_event() / wait_event() are emulated
as reads/writes on a private pipe. On the other hand, PAL API
DkSynchronizationObjectWait() works only on event/mutex objects, not
on pipes. So wait_event(), which previously used this API, failed
on assert because it provided a pipe object. This bug manifests
only in rare circumstances (I found it with Erlang workload) because
wait_event() is called very rarely, on data-race path of epoll wait.
This commit simply removes DkSynchronizationObjectWait() call, so
that the event is awaited via reading from the pipe.
2020-05-10 00:15:58 +02:00
Dmitrii Kuvaiskii 32695531dc [LibOS] Add support for FIFOs (named pipes)
This commit adds support for FIFOs and the corresponding syscalls
mknod() and mknodat(). Internally, FIFOs are emulated as pseudo-
files in chroot mount points (not visible in host FS). FIFOs'
read/write operations are emulated via pipes at PAL level (this
means that they are transparently encrypted under SGX PAL).
Generally, emulation of FIFOs is similar to emulation of named
UNIX domain sockets, i.e., they are "ephemeral" and only allow
communication between two related processes.

New LibOS test is added: multi-process `mkfifo`.
2020-05-08 16:09:51 -07:00
Michał Kowalczyk 0848aadc0d [LibOS] Fix a bunch of crashes found by socket-related LTP tests 2020-05-07 18:35:25 +02:00
borysp 04ec16c30a [LibOS] Add removing of unmapped VMAs when reexecuting the same binary 2020-05-02 16:22:47 +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
Dmitrii Kuvaiskii 301587065a [LibOS,Pal] Make Graphene build with GCC 9.3
GCC 9.3 adds more static checks on C headers and sources. This
commit fixes all detected issues (mainly possible NULL pointer
dereferences and VLAs on stack).
2020-05-01 23:15:30 +00:00
Michał Kowalczyk 12bedb08a7 [LibOS] Rename SHIM_SYSCALL_PASSTHROUGH to SHIM_SYSCALL_RETURN_ENOSYS 2020-05-01 13:31:09 +02:00
Michał Kowalczyk e39ee4767f Convert flags between PAL API and host syscalls
Currently various flags in file and memory syscalls work mostly by an
accident, because values of some of them align with corresponding Linux
syscall flags. Some APIs weren't that lucky though - e.g.
DkStreamOpen(..., /*options=*/PAL_OPTION_CLOEXEC) deletes file contents
(sic!) intead of opening it with O_CLOEXEC. This is because
PAL_OPTION_CLOEXEC == O_TRUNC.

This commit fixes all this mess and also adds asserts to check validity
of flags passed to Dk* handlers.
2020-04-27 19:54:55 +02:00
Michał Kowalczyk 5a0fa0ed72 [LibOS] Remove half-implemented checkpointing to files
Current implementation isn't finished, doesn't have a single test and
has quite bad code quality. If we decide we want to implement this
feature, it will be easier to just implement it from scratch.
2020-04-14 21:03:49 +02:00
Michał Kowalczyk a783fa2f4b Remove unused profiling system 2020-04-13 18:59:03 +02:00
Michał Kowalczyk 5a05e53730 Remove unused hash functions 2020-04-13 18:59:03 +02: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
Michał Kowalczyk ee594256cd [LibOS] Fix handling of O_CLOEXEC in dup* syscalls 2020-04-09 20:45:26 +02:00
Michał Kowalczyk 4ddbb8a973 [LibOS] Fix handling of dup* with invalid args 2020-04-09 20:45:26 +02:00
Stefan Berger dc0cbd0b68 [LibOS] Rework inet_copy_addr to avoid stack smashing when using IPv6
Rework inet_copy_addr by using sockaddr_storage (ported from glibc's
bits/socket.h) for writing the data into, which is large enough to hold
sockaddr_in6. Then copy back into the user's buffer.

Previously some calls to inet_copy_addr were passing in a
'struct sockaddr' that is too small to hold sockaddr_in6 data, thus
causing stack corruption when the socket was an IPv6 socket.
2020-04-06 21:09:10 +02:00
Dmitrii Kuvaiskii 3e9e0755ab [LibOS] Refactor implementation of /proc and /dev pseudo-FSs
Previously, /proc and /dev pseudo-filesystems were implemented in
completely different ways. This commit introduces a set of generic
functions for all pseudo-FSs (based on previous implementation of
/proc) and refactors both /proc and /dev to use these functions.

This commit also expands a LibOS test `proc` to cover more cases
and pseudo-files, as well as introduces a new LibOS test `dev`.
Several bugs in pseudo-FSs were detected and fixed in the process.
2020-04-01 19:47:50 -07:00
borysp 6fd54058d5 [LibOS] Disable signals in critical section in shim_futex.c 2020-04-01 01:01:54 +02:00
Dmitrii Kuvaiskii 69997ad460 [LibOS] Add clock_nanosleep() syscall emulation 2020-03-17 21:57:11 +01:00
Dmitrii Kuvaiskii 95bfd8aeb5 [Pal/Linux-SGX] Refactor memory management of enclave pages
Previously, Linux-SGX logic of allocating/freeing enclave pages was
complicated and hard to read. This commit refactors this code for
readability, without changes in functionality.
2020-03-04 08:41:28 -08:00
Isaku Yamahata 467033f4a2 [LibOS] Remove mmap logic on read/write in chroot FS
Previously, the LibOS logic in chroot FS's read/write emulation tried
to be (too) smart: it mmapped regular files and performed a memcpy on
the user-requested file offset + size. This mmap trick required the
use of DkStreamMap() which is inefficient on Linux-SGX PAL, since for
SGX, the PAL must memcpy from untrusted file buffer into the trusted
enclave buffer. Thus, reads/writes in chroot FS resulted in two
memcpys under SGX.

This commit removes this complicated mmap logic and simply calls
DkStreamRead/Write() for regular files. In other words, the mmap
optimization is moved out of the LibOS layer and into the PAL layer.
2020-02-26 13:48:51 -08:00
borysp 6c0c0c526a [Make] Fix LibOS tests build errors 2020-02-14 05:16:50 +01:00
Isaku Yamahata 4182c29476 [LibOS] Eliminate unnecessary nested functions
BEGIN_MIGRATION_DEF() is used to define nested functions unnecessarily.
Make them normal (non-nested) functions.
2020-02-12 02:11:58 +01:00
Isaku Yamahata 5a84d36e8c [LibOS] Use right type, struct wake_queue_node*, instead of void* 2020-02-11 02:30:49 +01:00
Isaku Yamahata d2d88e16fe [LibOS] Change ioctl(, int cmd) to ioctl(, unsigned long cmd)
The second argument to ioctl is unsigned long, not int.
2020-02-10 22:38:07 +01:00
Isaku Yamahata a09c0897f5 [LibOS] Cleanup of shim_thread::shim_signal_logs
- Make struct shim_signal_log opaque.
- Introduce signal_logs_alloc() and signal_logs_free() to properly
  initialize and free signal_logs.
- Introduce helper signal_logs_pending() to simplify signal_logs check.
- Rename shim_signal_log's {head, tail} to {tail, head}.
- Fix PID leak during get_new_thread().
2020-02-05 13:16:12 -08:00
Dmitrii Kuvaiskii c685e56e67 [LibOS] Update TYPE_DEV handles with correct function pointers in dev_ops
For device handles, `info.dev.dev_ops` contains function pointers into
LibOS. They may become invalid due to relocation of LibOS text section
in the child process on fork. This commit forces an update of these
function pointers.
2020-02-04 18:25:00 -08:00
borysp 5c37c4d21a [LibOS] Miscellaneous bugfixes, mostly found by the previous commit
The previous commit added some asserts on locks state, which unveiled
a bunch of bugs. This fixes all of them and a bit more found in the
process.
2020-02-02 17:58:51 +01:00
borysp bdc6a48c2c [LibOS] Add a bunch of asserts on locks state 2020-02-02 17:58:39 +01:00
Dmitrii Kuvaiskii 5784d97375 [LibOS] Allow MSG_PEEK on recv()
Previously, Graphene failed if recv() contained MSG_PEEK flag. This
resulted in many TLS-based applications failing, including Nginx,
Apache, and Lighttpd in SSL/TLS mode. This commit adds emulation of
MSG_PEEK at LibOS level. A simple TCP test case is provided.
2020-01-25 02:26:59 +01:00
Dmitrii Kuvaiskii 57f1bdd47f [LibOS] Fix type of checkpoint's total memory size variable
Checkpoint's total memory size is stored in shim_cp_store::mem_size
field. Previously, this field was of type `int`. When a process
allocates more than 2GB of memory and then tries to spawn a child,
the checkpoint send/receive fails due to int overflow of mem_size.
This commit simply changes mem_size type to `size_t`. This is enough
to make the bug go away on e.g. a huge Python app with TensorFlow.
2020-01-17 17:36:07 -08:00
Dmitrii Kuvaiskii 40c08ff75a [LibOS, Pal/{Linux,Linux-SGX}] Remove DkObjectsWaitAny()
Now Graphene supports an improved version of DkObjectsWaitAny() with
correct polling semantics -- DkObjectsWaitEvents(). This makes
DkObjectsWaitAny() obsolete. This commit removes DkObjectsWaitAny()
and replaces it with:
- DkSynchronizationObjectWait() to wait on a single synchronization
  object like mutex or event.
- DkStreamsWaitEvents() to wait on stream-like objects (this is the
  renamed DkObjectsWaitEvents()).

The corresponding tests are fixed to use the new PAL interfaces.
Also, IPC helper and Async helper threads are significantly refactored
to make better use of DkStreamsWaitEvents().
2020-01-14 20:31:40 -08:00
Michał Kowalczyk f1c334357c Reformat repository to our clang-format rules (2nd iteration) 2020-01-09 02:35:50 +01:00
Isaku Yamahata f8f6a0c42f [LibOS] Replace init_fs_base() with update_fs_base()
Now init_fs_base() is same to update_fs_base() except struct shim_thread
argument which is unused.
2020-01-08 01:20:26 +01:00
Isaku Yamahata 1902c3862b [LibOS] Use set_cur_thread() to associate shim_tcb_t and shim_thread
Uniformly use set_cur_thread() to associate shim_tcb_t to struct
shim_thread. Remove unnecessary logic from init_fs_base() and
update_fs_base().
2020-01-08 01:20:26 +01:00
Isaku Yamahata 29bd2bdf9c [LibOS] Use uint64_t intead of unsigned long for GP register type 2020-01-08 01:06:58 +01:00
Isaku Yamahata a7cb199b9a [LibOS] Optimize shim_get_tcb() and its variants
- apply SHIM_TCB_GET() and SHIM_TCB_SET()
- remove dead code
2020-01-08 01:06:52 +01:00
Dmitrii Kuvaiskii da2dd37dd8 [LibOS, Pal/{Linux,Linux-SGX}] Better emulation of polling
This commit improves the emulation of polling mechanisms (select,
pselect, poll, ppoll, epoll_wait) and cleans up the corresponding
code:
- New DkObjectsWaitEvents() PAL interface, replaces the inefficient
  DkObjectsWaitAny() interface. This interface closely resembles
  Linux/POSIX poll() in semantics.
- Improved shim_do_epoll_wait() implementation, now using the new
  DkObjectsWaitEvents() interface.
- Improved shim_do_poll() implementation, now using the new
  DkObjectsWaitEvents() interface.
- Small cleanups of polling code.
2020-01-07 12:11:00 -08:00
Dmitrii Kuvaiskii 56bc2b49ad [LibOS] Comprehensive cleanup of epoll_create()/epoll_ctl()/epoll_wait()
Accurate cleanup of shim_do_epoll_create1(), shim_do_epoll_ctl(),
shim_do_epoll_wait(), and other epoll helper functions. This cleanup
also adds error handling (missing previously).

The commit makes (most of) the corresponding LTP tests pass now.

Note that epoll semantics are still incorrect and inefficient: current
epoll_wait() emulation returns only one event to the user.
2020-01-07 12:11:00 -08:00
borysp 1bf6423384 [LibOS/Pal] Replace hardcoded URI prefix strings 2020-01-06 16:17:26 -08:00
Dmitrii Kuvaiskii 7d99d4788c [LibOS,Pal] Remove Graphene IPC completely
Graphene IPC (GIPC) was introduced to perform faster bulk IPC by sharing pages as copy-on-write
across processes. This feature became stale, and it was shown that recent Linux kernels (4.2+)
have zero-copy transfers over UNIX sockets and exhibit similar performance. GIPC is not built and
not tested in Jenkins. Also, GIPC does not work under SGX. This commit completely removes GIPC.
2019-12-30 12:17:51 -08:00
borysp 6cfaf420b3 [LibOS] Rework futexes implementation
This commit completely rewrites futex implementation to (hopefully)
remove all races (both on memory access level and between waits and
wakes), make it compatible with actual Linux implementation and make
it more maintainable and readable.
2019-12-19 22:04:12 -08:00
Dmitrii Kuvaiskii 05cc50945f [LibOS] Allow inaccessible files during getdents()
Previously, if user performed getdents() on a directory containing
inaccessible files (because user doesn't have permission), whole
getdents failed with -EACCES. This is incorrect behavior: files must
still be listed. This commit fixes the root cause of this bug by
marking inaccessible files as DENTRY_NEGATIVE.
2019-12-18 02:59:02 -08:00
Dmitrii Kuvaiskii 7c45430355 [LibOS] Remove the data race on thread::is_alive
Previously, there was a data race on thread::is_alive between one thread
checking whether it is the last thread alive via check_last_thread() and
another thread exiting via thread_exit(). The former checks if is_alive
is true, the latter sets it to false. However, the exiting thread will
truly exit only after it called DkThreadExit(), thus the race on is_alive
led to scenarios where two threads believe to be the last threads alive
and compete on terminating Async Helper/IPC threads and exiting the whole
process. This commit introduces cleanup_thread() called by Async Helper
to set is_alive to false and delete the thread, freeing its resources.
The data race is thus removed, and shim_thread object leak is prevented.
2019-12-18 01:56:25 -08:00