Skip to content

Commit fb57269

Browse files
committed
[LibOS] Add support for timerfd system calls
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]>
1 parent 929bb9d commit fb57269

File tree

25 files changed

+1014
-49
lines changed

25 files changed

+1014
-49
lines changed

Documentation/devel/features.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ The below list is generated from the [syscall table of Linux
10361036
-`signalfd()`
10371037
<sup>[7](#signals-and-process-state-changes)</sup>
10381038

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

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

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

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

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

28722872
Gramine implements alarm clocks via `alarm()`.
28732873

2874+
Gramine implements timers that notify via file descriptors: `timerfd_create()`, `timerfd_settime()`
2875+
and `timerfd_gettime()`. The timerfd object is created inside Gramine, and all operations are
2876+
resolved entirely inside Gramine. Each timerfd object is associated with a dummy eventfd created on
2877+
the host. This is purely for triggering read notifications (e.g., in epoll); timerfd data is
2878+
verified inside Gramine and is never exposed to the host. Since the host is used purely for
2879+
notifications, a malicious host can only induce Denial of Service (DoS) attacks.
2880+
2881+
The emulation is currently implemented at the level of a single process. The emulation *may* work for
2882+
multi-process applications, e.g., if the child process inherits the timerfd object but doesn't use
2883+
it. However, all timerfds created in the parent process are marked as invalid in child processes,
2884+
i.e. inter-process timing signals via timerfds are not allowed.
2885+
28742886
Gramine does *not* currently implement the POSIX per-process timer: `timer_create()`, etc. Gramine
2875-
also does not currently implement timers that notify via file descriptors. Gramine could implement
2876-
these timers in the future, if need arises.
2887+
could implement it in the future, if need arises.
28772888

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

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

2892-
- `timerfd_create()`: may be implemented in the future
2893-
- `timerfd_settime()`: may be implemented in the future
2894-
- `timerfd_gettime()`: may be implemented in the future
2903+
- `timerfd_create()`: see notes above
2904+
- `timerfd_settime()`: see notes above
2905+
- `timerfd_gettime()`: see notes above
28952906

28962907
</details><br />
28972908

libos/include/libos_fs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ struct libos_fs_ops {
183183
int (*poll)(struct libos_handle* hdl, int in_events, int* out_events);
184184

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

189189
/* checkpoint/migrate the file system */
@@ -942,6 +942,7 @@ extern struct libos_fs eventfd_builtin_fs;
942942
extern struct libos_fs synthetic_builtin_fs;
943943
extern struct libos_fs path_builtin_fs;
944944
extern struct libos_fs shm_builtin_fs;
945+
extern struct libos_fs timerfd_builtin_fs;
945946

946947
struct libos_fs* find_fs(const char* name);
947948

libos/include/libos_handle.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ enum libos_handle_type {
4646
/* Special handles: */
4747
TYPE_EPOLL, /* epoll handles, see `libos_epoll.c` */
4848
TYPE_EVENTFD, /* eventfd handles, used by `eventfd` filesystem */
49+
TYPE_TIMERFD, /* timerfd handles, used by `timerfd` filesystem */
4950
};
5051

5152
struct libos_pipe_handle {
@@ -142,6 +143,18 @@ struct libos_eventfd_handle {
142143
uint64_t dummy_host_val;
143144
};
144145

146+
struct libos_timerfd_handle {
147+
bool broken_in_child;
148+
149+
spinlock_t expiration_lock; /* protecting below fields */
150+
uint64_t num_expirations;
151+
uint64_t dummy_host_val;
152+
153+
spinlock_t timer_lock; /* protecting below fields */
154+
uint64_t timeout;
155+
uint64_t reset;
156+
};
157+
145158
struct libos_handle {
146159
enum libos_handle_type type;
147160
bool is_dir;
@@ -217,6 +230,8 @@ struct libos_handle {
217230

218231
struct libos_epoll_handle epoll; /* TYPE_EPOLL */
219232
struct libos_eventfd_handle eventfd; /* TYPE_EVENTFD */
233+
234+
struct libos_timerfd_handle timerfd; /* TYPE_TIMERFD */
220235
} info;
221236

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

libos/include/libos_table.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,7 @@ long libos_syscall_getrandom(char* buf, size_t count, unsigned int flags);
207207
long libos_syscall_mlock2(unsigned long start, size_t len, int flags);
208208
long libos_syscall_sysinfo(struct sysinfo* info);
209209
long libos_syscall_close_range(unsigned int first, unsigned int last, unsigned int flags);
210+
long libos_syscall_timerfd_create(int clockid, int flags);
211+
long libos_syscall_timerfd_settime(int fd, int flags, const struct __kernel_itimerspec* value,
212+
struct __kernel_itimerspec* ovalue);
213+
long libos_syscall_timerfd_gettime(int fd, struct __kernel_itimerspec* value);

libos/include/libos_utils.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ void clean_link_map_list(void);
5252
int create_pipe(char* name, char* uri, size_t size, PAL_HANDLE* hdl, bool use_vmid_for_name);
5353

5454
/* Asynchronous event support */
55+
enum async_event_type {
56+
ASYNC_EVENT_TYPE_IO = 1,
57+
ASYNC_EVENT_TYPE_ALARM_TIMER = 2,
58+
};
59+
5560
int init_async_worker(void);
56-
int64_t install_async_event(PAL_HANDLE object, unsigned long time,
61+
int64_t install_async_event(enum async_event_type type, PAL_HANDLE object,
62+
unsigned long time_us, bool absolute_time,
5763
void (*callback)(IDTYPE caller, void* arg), void* arg);
5864
struct libos_thread* terminate_async_worker(void);
5965

libos/include/linux_abi/time.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,14 @@ struct __kernel_timezone {
3737
int tz_minuteswest; /* minutes west of Greenwich */
3838
int tz_dsttime; /* type of dst correction */
3939
};
40+
41+
#define TFD_TIMER_ABSTIME (1 << 0)
42+
#define TFD_TIMER_CANCEL_ON_SET (1 << 1)
43+
#define TFD_CLOEXEC O_CLOEXEC
44+
#define TFD_NONBLOCK O_NONBLOCK
45+
46+
#define TFD_SHARED_FCNTL_FLAGS (TFD_CLOEXEC | TFD_NONBLOCK)
47+
/* Flags for timerfd_create. */
48+
#define TFD_CREATE_FLAGS TFD_SHARED_FCNTL_FLAGS
49+
/* Flags for timerfd_settime. */
50+
#define TFD_SETTIME_FLAGS (TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)

libos/src/arch/x86_64/libos_table.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,11 @@ libos_syscall_t libos_syscall_table[LIBOS_SYSCALL_BOUND] = {
297297
[__NR_utimensat] = (libos_syscall_t)0, // libos_syscall_utimensat
298298
[__NR_epoll_pwait] = (libos_syscall_t)libos_syscall_epoll_pwait,
299299
[__NR_signalfd] = (libos_syscall_t)0, // libos_syscall_signalfd
300-
[__NR_timerfd_create] = (libos_syscall_t)0, // libos_syscall_timerfd_create
300+
[__NR_timerfd_create] = (libos_syscall_t)libos_syscall_timerfd_create,
301301
[__NR_eventfd] = (libos_syscall_t)libos_syscall_eventfd,
302302
[__NR_fallocate] = (libos_syscall_t)libos_syscall_fallocate,
303-
[__NR_timerfd_settime] = (libos_syscall_t)0, // libos_syscall_timerfd_settime
304-
[__NR_timerfd_gettime] = (libos_syscall_t)0, // libos_syscall_timerfd_gettime
303+
[__NR_timerfd_settime] = (libos_syscall_t)libos_syscall_timerfd_settime,
304+
[__NR_timerfd_gettime] = (libos_syscall_t)libos_syscall_timerfd_gettime,
305305
[__NR_accept4] = (libos_syscall_t)libos_syscall_accept4,
306306
[__NR_signalfd4] = (libos_syscall_t)0, // libos_syscall_signalfd4
307307
[__NR_eventfd2] = (libos_syscall_t)libos_syscall_eventfd2,

libos/src/fs/libos_fs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ static struct libos_fs* g_builtin_fs[] = {
3333
&synthetic_builtin_fs,
3434
&path_builtin_fs,
3535
&shm_builtin_fs,
36+
&timerfd_builtin_fs,
3637
};
3738

3839
static struct libos_lock g_mount_mgr_lock;

libos/src/fs/proc/thread.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ static char* describe_handle(struct libos_handle* hdl) {
287287
case TYPE_EPOLL: str = "epoll:[?]"; break;
288288
case TYPE_EVENTFD: str = "eventfd:[?]"; break;
289289
case TYPE_SHM: str = "shm:[?]"; break;
290+
case TYPE_TIMERFD: str = "timerfd:[?]"; break;
290291
default: str = "unknown:[?]"; break;
291292
}
292293
return strdup(str);

libos/src/fs/timerfd/fs.c

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/* SPDX-License-Identifier: LGPL-3.0-or-later */
2+
/* Copyright (C) 2024 Intel Corporation
3+
* Kailun Qin <[email protected]>
4+
*/
5+
6+
/*
7+
* This file contains code for implementation of "timerfd" filesystem. For more information, see
8+
* `libos/src/sys/libos_timerfd.c`.
9+
*/
10+
11+
#include "libos_fs.h"
12+
#include "libos_handle.h"
13+
#include "libos_internal.h"
14+
#include "libos_lock.h"
15+
#include "linux_abi/errors.h"
16+
#include "pal.h"
17+
18+
/* Enforce a restriction that all timerfds created in the parent process are marked as invalid in
19+
* child processes, i.e. inter-process timing signals via timerfds are not allowed. This restriction
20+
* is because LibOS doesn't yet implement sync between timerfd objects. */
21+
static int timerfd_checkin(struct libos_handle* hdl) {
22+
assert(hdl->type == TYPE_TIMERFD);
23+
hdl->info.timerfd.broken_in_child = true;
24+
return 0;
25+
}
26+
27+
static void timerfd_dummy_host_read(struct libos_handle* hdl) {
28+
int ret;
29+
uint64_t buf_dummy_host_val = 0;
30+
size_t dummy_host_val_count = sizeof(buf_dummy_host_val);
31+
do {
32+
ret = PalStreamRead(hdl->pal_handle, /*offset=*/0, &dummy_host_val_count,
33+
&buf_dummy_host_val);
34+
} while (ret == -PAL_ERROR_INTERRUPTED);
35+
if (ret < 0 || dummy_host_val_count != sizeof(buf_dummy_host_val)) {
36+
/* must not happen in benign case, consider it an attack and panic */
37+
BUG();
38+
}
39+
}
40+
41+
static void timerfd_dummy_host_wait(struct libos_handle* hdl) {
42+
pal_wait_flags_t wait_for_events = PAL_WAIT_READ;
43+
pal_wait_flags_t ret_events = 0;
44+
int ret = PalStreamsWaitEvents(1, &hdl->pal_handle, &wait_for_events, &ret_events, NULL);
45+
if (ret < 0 && ret != -PAL_ERROR_INTERRUPTED) {
46+
BUG();
47+
}
48+
(void)ret_events; /* we don't care what events the host returned, we can't trust them anyway */
49+
}
50+
51+
static ssize_t timerfd_read(struct libos_handle* hdl, void* buf, size_t count, file_off_t* pos) {
52+
__UNUSED(pos);
53+
assert(hdl->type == TYPE_TIMERFD);
54+
55+
if (count < sizeof(uint64_t))
56+
return -EINVAL;
57+
58+
if (hdl->info.timerfd.broken_in_child) {
59+
log_warning("Child process tried to access timerfd created by parent process. This is "
60+
"disallowed in Gramine.");
61+
return -EIO;
62+
}
63+
64+
int ret;
65+
spinlock_lock(&hdl->info.timerfd.expiration_lock);
66+
67+
while (!hdl->info.timerfd.num_expirations) {
68+
if (hdl->flags & O_NONBLOCK) {
69+
ret = -EAGAIN;
70+
goto out;
71+
}
72+
spinlock_unlock(&hdl->info.timerfd.expiration_lock);
73+
timerfd_dummy_host_wait(hdl);
74+
spinlock_lock(&hdl->info.timerfd.expiration_lock);
75+
}
76+
77+
memcpy(buf, &hdl->info.timerfd.num_expirations, sizeof(uint64_t));
78+
hdl->info.timerfd.num_expirations = 0;
79+
80+
/* perform a read (not supposed to block) to clear the event from polling threads and to send an
81+
* event to writing threads */
82+
if (hdl->info.timerfd.dummy_host_val) {
83+
timerfd_dummy_host_read(hdl);
84+
hdl->info.timerfd.dummy_host_val = 0;
85+
}
86+
87+
ret = (ssize_t)count;
88+
out:
89+
spinlock_unlock(&hdl->info.timerfd.expiration_lock);
90+
maybe_epoll_et_trigger(hdl, ret, /*in=*/true, /*unused was_partial=*/false);
91+
return ret;
92+
}
93+
94+
static void timerfd_post_poll(struct libos_handle* hdl, pal_wait_flags_t* pal_ret_events) {
95+
assert(hdl->type == TYPE_TIMERFD);
96+
97+
if (hdl->info.timerfd.broken_in_child) {
98+
log_warning("Child process tried to access timerfd created by parent process. This is "
99+
"disallowed in Gramine.");
100+
*pal_ret_events = PAL_WAIT_ERROR;
101+
return;
102+
}
103+
104+
if (*pal_ret_events & (PAL_WAIT_ERROR | PAL_WAIT_HANG_UP | PAL_WAIT_WRITE)) {
105+
/* impossible: we control timerfd inside the LibOS, and we never raise such conditions */
106+
BUG();
107+
}
108+
109+
spinlock_lock(&hdl->info.timerfd.expiration_lock);
110+
if (*pal_ret_events & PAL_WAIT_READ) {
111+
/* there is data to read: verify if timerfd has number of expirations greater than zero */
112+
if (!hdl->info.timerfd.num_expirations) {
113+
/* spurious or malicious notification, can legitimately happen if another thread
114+
* consumed this event between this thread's poll wakeup and the post_poll callback;
115+
* we currently choose to return a spurious notification to the user */
116+
*pal_ret_events &= ~PAL_WAIT_READ;
117+
}
118+
}
119+
spinlock_unlock(&hdl->info.timerfd.expiration_lock);
120+
}
121+
122+
struct libos_fs_ops timerfd_fs_ops = {
123+
.checkin = &timerfd_checkin,
124+
.read = &timerfd_read,
125+
.post_poll = &timerfd_post_poll,
126+
};
127+
128+
struct libos_fs timerfd_builtin_fs = {
129+
.name = "timerfd",
130+
.fs_ops = &timerfd_fs_ops,
131+
};

0 commit comments

Comments
 (0)