Skip to content

Commit

Permalink
[LibOS] Add support for timerfd system calls
Browse files Browse the repository at this point in the history
This commit adds support for system calls that create and operate on a
timer that delivers timer expiration notifications via a file
descriptor, specifically: `timerfd_create()`, `timerfd_settime()` and
`timerfd_gettime()`. The timerfd object is associated with a dummy
eventfd created on the host to trigger notifications (e.g., in epoll).
The object is created inside Gramine, with all it operations resolved
entirely inside Gramine.

The emulation is currently implemented at the level of a single process.
However, it may sometimes work for multi-process applications, e.g.,
if the child process inherits the timerfd object but doesn't use it. However,
all timerfds created in the parent process are marked as invalid in child
processes, i.e. inter-process timing signals via timerfds are not allowed.

LibOS regression tests are also added.

Signed-off-by: Kailun Qin <[email protected]>
  • Loading branch information
kailun-qin committed Jun 5, 2024
1 parent 929bb9d commit fb57269
Show file tree
Hide file tree
Showing 25 changed files with 1,014 additions and 49 deletions.
27 changes: 19 additions & 8 deletions Documentation/devel/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ The below list is generated from the [syscall table of Linux
-`signalfd()`
<sup>[7](#signals-and-process-state-changes)</sup>

- `timerfd_create()`
- `timerfd_create()`
<sup>[20](#sleeps-timers-and-alarms)</sup>

-`eventfd()`
Expand All @@ -1045,10 +1045,10 @@ The below list is generated from the [syscall table of Linux
-`fallocate()`
<sup>[9a](#file-system-operations)</sup>

- `timerfd_settime()`
- `timerfd_settime()`
<sup>[20](#sleeps-timers-and-alarms)</sup>

- `timerfd_gettime()`
- `timerfd_gettime()`
<sup>[20](#sleeps-timers-and-alarms)</sup>

-`accept4()`
Expand Down Expand Up @@ -2871,9 +2871,20 @@ Gramine implements getting and setting the interval timer: `getitimer()` and `se

Gramine implements alarm clocks via `alarm()`.

Gramine implements timers that notify via file descriptors: `timerfd_create()`, `timerfd_settime()`
and `timerfd_gettime()`. The timerfd object is created inside Gramine, and all operations are
resolved entirely inside Gramine. Each timerfd object is associated with a dummy eventfd created on
the host. This is purely for triggering read notifications (e.g., in epoll); timerfd data is
verified inside Gramine and is never exposed to the host. Since the host is used purely for
notifications, a malicious host can only induce Denial of Service (DoS) attacks.

The emulation is currently implemented at the level of a single process. The emulation *may* work for
multi-process applications, e.g., if the child process inherits the timerfd object but doesn't use
it. However, all timerfds created in the parent process are marked as invalid in child processes,
i.e. inter-process timing signals via timerfds are not allowed.

Gramine does *not* currently implement the POSIX per-process timer: `timer_create()`, etc. Gramine
also does not currently implement timers that notify via file descriptors. Gramine could implement
these timers in the future, if need arises.
could implement it in the future, if need arises.

<details><summary>Related system calls</summary>

Expand All @@ -2889,9 +2900,9 @@ these timers in the future, if need arises.
-`timer_getoverrun()`: may be implemented in the future
-`timer_delete()`: may be implemented in the future

- `timerfd_create()`: may be implemented in the future
- `timerfd_settime()`: may be implemented in the future
- `timerfd_gettime()`: may be implemented in the future
- `timerfd_create()`: see notes above
- `timerfd_settime()`: see notes above
- `timerfd_gettime()`: see notes above

</details><br />

Expand Down
3 changes: 2 additions & 1 deletion libos/include/libos_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ struct libos_fs_ops {
int (*poll)(struct libos_handle* hdl, int in_events, int* out_events);

/* Verify a single handle after poll. Must update `pal_ret_events` in-place with only allowed
* ones. Used in e.g. secure eventfd FS to verify if the host is not lying to us. */
* ones. Used in e.g. secure eventfd and timerfd FS to verify if the host is not lying to us. */
void (*post_poll)(struct libos_handle* hdl, pal_wait_flags_t* pal_ret_events);

/* checkpoint/migrate the file system */
Expand Down Expand Up @@ -942,6 +942,7 @@ extern struct libos_fs eventfd_builtin_fs;
extern struct libos_fs synthetic_builtin_fs;
extern struct libos_fs path_builtin_fs;
extern struct libos_fs shm_builtin_fs;
extern struct libos_fs timerfd_builtin_fs;

struct libos_fs* find_fs(const char* name);

Expand Down
17 changes: 16 additions & 1 deletion libos/include/libos_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum libos_handle_type {
/* Special handles: */
TYPE_EPOLL, /* epoll handles, see `libos_epoll.c` */
TYPE_EVENTFD, /* eventfd handles, used by `eventfd` filesystem */
TYPE_TIMERFD, /* timerfd handles, used by `timerfd` filesystem */
};

struct libos_pipe_handle {
Expand Down Expand Up @@ -142,6 +143,18 @@ struct libos_eventfd_handle {
uint64_t dummy_host_val;
};

struct libos_timerfd_handle {
bool broken_in_child;

spinlock_t expiration_lock; /* protecting below fields */
uint64_t num_expirations;
uint64_t dummy_host_val;

spinlock_t timer_lock; /* protecting below fields */
uint64_t timeout;
uint64_t reset;
};

struct libos_handle {
enum libos_handle_type type;
bool is_dir;
Expand Down Expand Up @@ -217,6 +230,8 @@ struct libos_handle {

struct libos_epoll_handle epoll; /* TYPE_EPOLL */
struct libos_eventfd_handle eventfd; /* TYPE_EVENTFD */

struct libos_timerfd_handle timerfd; /* TYPE_TIMERFD */
} info;

struct libos_dir_handle dir_info;
Expand All @@ -232,7 +247,7 @@ struct libos_handle {
* `read`, `seek` but not `pread`). This lock should be taken *before* `libos_handle.lock` and
* `libos_inode.lock`. Must be used *only* via maybe_lock_pos_handle() and
* maybe_unlock_pos_handle(); these functions make sure that the lock is acquired only on those
* handle types that are seekable (e.g. not on eventfds or pipes). */
* handle types that are seekable (e.g. not on eventfds, timerfds or pipes). */
struct libos_lock pos_lock;
};

Expand Down
4 changes: 4 additions & 0 deletions libos/include/libos_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,7 @@ long libos_syscall_getrandom(char* buf, size_t count, unsigned int flags);
long libos_syscall_mlock2(unsigned long start, size_t len, int flags);
long libos_syscall_sysinfo(struct sysinfo* info);
long libos_syscall_close_range(unsigned int first, unsigned int last, unsigned int flags);
long libos_syscall_timerfd_create(int clockid, int flags);
long libos_syscall_timerfd_settime(int fd, int flags, const struct __kernel_itimerspec* value,
struct __kernel_itimerspec* ovalue);
long libos_syscall_timerfd_gettime(int fd, struct __kernel_itimerspec* value);
8 changes: 7 additions & 1 deletion libos/include/libos_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ void clean_link_map_list(void);
int create_pipe(char* name, char* uri, size_t size, PAL_HANDLE* hdl, bool use_vmid_for_name);

/* Asynchronous event support */
enum async_event_type {
ASYNC_EVENT_TYPE_IO = 1,
ASYNC_EVENT_TYPE_ALARM_TIMER = 2,
};

int init_async_worker(void);
int64_t install_async_event(PAL_HANDLE object, unsigned long time,
int64_t install_async_event(enum async_event_type type, PAL_HANDLE object,
unsigned long time_us, bool absolute_time,
void (*callback)(IDTYPE caller, void* arg), void* arg);
struct libos_thread* terminate_async_worker(void);

Expand Down
11 changes: 11 additions & 0 deletions libos/include/linux_abi/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ struct __kernel_timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of dst correction */
};

#define TFD_TIMER_ABSTIME (1 << 0)
#define TFD_TIMER_CANCEL_ON_SET (1 << 1)
#define TFD_CLOEXEC O_CLOEXEC
#define TFD_NONBLOCK O_NONBLOCK

#define TFD_SHARED_FCNTL_FLAGS (TFD_CLOEXEC | TFD_NONBLOCK)
/* Flags for timerfd_create. */
#define TFD_CREATE_FLAGS TFD_SHARED_FCNTL_FLAGS
/* Flags for timerfd_settime. */
#define TFD_SETTIME_FLAGS (TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)
6 changes: 3 additions & 3 deletions libos/src/arch/x86_64/libos_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,11 @@ libos_syscall_t libos_syscall_table[LIBOS_SYSCALL_BOUND] = {
[__NR_utimensat] = (libos_syscall_t)0, // libos_syscall_utimensat
[__NR_epoll_pwait] = (libos_syscall_t)libos_syscall_epoll_pwait,
[__NR_signalfd] = (libos_syscall_t)0, // libos_syscall_signalfd
[__NR_timerfd_create] = (libos_syscall_t)0, // libos_syscall_timerfd_create
[__NR_timerfd_create] = (libos_syscall_t)libos_syscall_timerfd_create,
[__NR_eventfd] = (libos_syscall_t)libos_syscall_eventfd,
[__NR_fallocate] = (libos_syscall_t)libos_syscall_fallocate,
[__NR_timerfd_settime] = (libos_syscall_t)0, // libos_syscall_timerfd_settime
[__NR_timerfd_gettime] = (libos_syscall_t)0, // libos_syscall_timerfd_gettime
[__NR_timerfd_settime] = (libos_syscall_t)libos_syscall_timerfd_settime,
[__NR_timerfd_gettime] = (libos_syscall_t)libos_syscall_timerfd_gettime,
[__NR_accept4] = (libos_syscall_t)libos_syscall_accept4,
[__NR_signalfd4] = (libos_syscall_t)0, // libos_syscall_signalfd4
[__NR_eventfd2] = (libos_syscall_t)libos_syscall_eventfd2,
Expand Down
1 change: 1 addition & 0 deletions libos/src/fs/libos_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static struct libos_fs* g_builtin_fs[] = {
&synthetic_builtin_fs,
&path_builtin_fs,
&shm_builtin_fs,
&timerfd_builtin_fs,
};

static struct libos_lock g_mount_mgr_lock;
Expand Down
1 change: 1 addition & 0 deletions libos/src/fs/proc/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ static char* describe_handle(struct libos_handle* hdl) {
case TYPE_EPOLL: str = "epoll:[?]"; break;
case TYPE_EVENTFD: str = "eventfd:[?]"; break;
case TYPE_SHM: str = "shm:[?]"; break;
case TYPE_TIMERFD: str = "timerfd:[?]"; break;
default: str = "unknown:[?]"; break;
}
return strdup(str);
Expand Down
131 changes: 131 additions & 0 deletions libos/src/fs/timerfd/fs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2024 Intel Corporation
* Kailun Qin <[email protected]>
*/

/*
* This file contains code for implementation of "timerfd" filesystem. For more information, see
* `libos/src/sys/libos_timerfd.c`.
*/

#include "libos_fs.h"
#include "libos_handle.h"
#include "libos_internal.h"
#include "libos_lock.h"
#include "linux_abi/errors.h"
#include "pal.h"

/* Enforce a restriction that all timerfds created in the parent process are marked as invalid in
* child processes, i.e. inter-process timing signals via timerfds are not allowed. This restriction
* is because LibOS doesn't yet implement sync between timerfd objects. */
static int timerfd_checkin(struct libos_handle* hdl) {
assert(hdl->type == TYPE_TIMERFD);
hdl->info.timerfd.broken_in_child = true;
return 0;
}

static void timerfd_dummy_host_read(struct libos_handle* hdl) {
int ret;
uint64_t buf_dummy_host_val = 0;
size_t dummy_host_val_count = sizeof(buf_dummy_host_val);
do {
ret = PalStreamRead(hdl->pal_handle, /*offset=*/0, &dummy_host_val_count,
&buf_dummy_host_val);
} while (ret == -PAL_ERROR_INTERRUPTED);
if (ret < 0 || dummy_host_val_count != sizeof(buf_dummy_host_val)) {
/* must not happen in benign case, consider it an attack and panic */
BUG();
}
}

static void timerfd_dummy_host_wait(struct libos_handle* hdl) {
pal_wait_flags_t wait_for_events = PAL_WAIT_READ;
pal_wait_flags_t ret_events = 0;
int ret = PalStreamsWaitEvents(1, &hdl->pal_handle, &wait_for_events, &ret_events, NULL);
if (ret < 0 && ret != -PAL_ERROR_INTERRUPTED) {
BUG();
}
(void)ret_events; /* we don't care what events the host returned, we can't trust them anyway */
}

static ssize_t timerfd_read(struct libos_handle* hdl, void* buf, size_t count, file_off_t* pos) {
__UNUSED(pos);
assert(hdl->type == TYPE_TIMERFD);

if (count < sizeof(uint64_t))
return -EINVAL;

if (hdl->info.timerfd.broken_in_child) {
log_warning("Child process tried to access timerfd created by parent process. This is "
"disallowed in Gramine.");
return -EIO;
}

int ret;
spinlock_lock(&hdl->info.timerfd.expiration_lock);

while (!hdl->info.timerfd.num_expirations) {
if (hdl->flags & O_NONBLOCK) {
ret = -EAGAIN;
goto out;
}
spinlock_unlock(&hdl->info.timerfd.expiration_lock);
timerfd_dummy_host_wait(hdl);
spinlock_lock(&hdl->info.timerfd.expiration_lock);
}

memcpy(buf, &hdl->info.timerfd.num_expirations, sizeof(uint64_t));
hdl->info.timerfd.num_expirations = 0;

/* perform a read (not supposed to block) to clear the event from polling threads and to send an
* event to writing threads */
if (hdl->info.timerfd.dummy_host_val) {
timerfd_dummy_host_read(hdl);
hdl->info.timerfd.dummy_host_val = 0;
}

ret = (ssize_t)count;
out:
spinlock_unlock(&hdl->info.timerfd.expiration_lock);
maybe_epoll_et_trigger(hdl, ret, /*in=*/true, /*unused was_partial=*/false);
return ret;
}

static void timerfd_post_poll(struct libos_handle* hdl, pal_wait_flags_t* pal_ret_events) {
assert(hdl->type == TYPE_TIMERFD);

if (hdl->info.timerfd.broken_in_child) {
log_warning("Child process tried to access timerfd created by parent process. This is "
"disallowed in Gramine.");
*pal_ret_events = PAL_WAIT_ERROR;
return;
}

if (*pal_ret_events & (PAL_WAIT_ERROR | PAL_WAIT_HANG_UP | PAL_WAIT_WRITE)) {
/* impossible: we control timerfd inside the LibOS, and we never raise such conditions */
BUG();
}

spinlock_lock(&hdl->info.timerfd.expiration_lock);
if (*pal_ret_events & PAL_WAIT_READ) {
/* there is data to read: verify if timerfd has number of expirations greater than zero */
if (!hdl->info.timerfd.num_expirations) {
/* spurious or malicious notification, can legitimately happen if another thread
* consumed this event between this thread's poll wakeup and the post_poll callback;
* we currently choose to return a spurious notification to the user */
*pal_ret_events &= ~PAL_WAIT_READ;
}
}
spinlock_unlock(&hdl->info.timerfd.expiration_lock);
}

struct libos_fs_ops timerfd_fs_ops = {
.checkin = &timerfd_checkin,
.read = &timerfd_read,
.post_poll = &timerfd_post_poll,
};

struct libos_fs timerfd_builtin_fs = {
.name = "timerfd",
.fs_ops = &timerfd_fs_ops,
};
Loading

0 comments on commit fb57269

Please sign in to comment.