|
| 1 | +/* SPDX-License-Identifier: LGPL-3.0-or-later */ |
| 2 | +/* Copyright (C) 2024 Intel Corporation |
| 3 | + |
| 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 | +/* This implementation is the same as `eventfd_dummy_host_read()` in "fs/eventfd/fs.c". */ |
| 28 | +static void timerfd_dummy_host_read(struct libos_handle* hdl) { |
| 29 | + int ret; |
| 30 | + uint64_t buf_dummy_host_val = 0; |
| 31 | + size_t dummy_host_val_count = sizeof(buf_dummy_host_val); |
| 32 | + do { |
| 33 | + ret = PalStreamRead(hdl->pal_handle, /*offset=*/0, &dummy_host_val_count, |
| 34 | + &buf_dummy_host_val); |
| 35 | + } while (ret == -PAL_ERROR_INTERRUPTED); |
| 36 | + if (ret < 0 || dummy_host_val_count != sizeof(buf_dummy_host_val)) { |
| 37 | + /* must not happen in benign case, consider it an attack and panic */ |
| 38 | + BUG(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/* This implementation is the same as `eventfd_dummy_host_wait()` in "fs/eventfd/fs.c". */ |
| 43 | +static void timerfd_dummy_host_wait(struct libos_handle* hdl) { |
| 44 | + pal_wait_flags_t wait_for_events = PAL_WAIT_READ; |
| 45 | + pal_wait_flags_t ret_events = 0; |
| 46 | + int ret = PalStreamsWaitEvents(1, &hdl->pal_handle, &wait_for_events, &ret_events, NULL); |
| 47 | + if (ret < 0 && ret != -PAL_ERROR_INTERRUPTED) { |
| 48 | + BUG(); |
| 49 | + } |
| 50 | + (void)ret_events; /* we don't care what events the host returned, we can't trust them anyway */ |
| 51 | +} |
| 52 | + |
| 53 | +static ssize_t timerfd_read(struct libos_handle* hdl, void* buf, size_t count, file_off_t* pos) { |
| 54 | + __UNUSED(pos); |
| 55 | + assert(hdl->type == TYPE_TIMERFD); |
| 56 | + |
| 57 | + if (count < sizeof(uint64_t)) |
| 58 | + return -EINVAL; |
| 59 | + |
| 60 | + if (hdl->info.timerfd.broken_in_child) { |
| 61 | + log_warning("Child process tried to access timerfd created by parent process. This is " |
| 62 | + "disallowed in Gramine."); |
| 63 | + return -EIO; |
| 64 | + } |
| 65 | + |
| 66 | + int ret; |
| 67 | + spinlock_lock(&hdl->info.timerfd.expiration_lock); |
| 68 | + |
| 69 | + while (!hdl->info.timerfd.num_expirations) { |
| 70 | + if (hdl->flags & O_NONBLOCK) { |
| 71 | + ret = -EAGAIN; |
| 72 | + goto out; |
| 73 | + } |
| 74 | + spinlock_unlock(&hdl->info.timerfd.expiration_lock); |
| 75 | + timerfd_dummy_host_wait(hdl); |
| 76 | + spinlock_lock(&hdl->info.timerfd.expiration_lock); |
| 77 | + } |
| 78 | + |
| 79 | + memcpy(buf, &hdl->info.timerfd.num_expirations, sizeof(uint64_t)); |
| 80 | + hdl->info.timerfd.num_expirations = 0; |
| 81 | + |
| 82 | + /* perform a read (not supposed to block) to clear the event from polling threads */ |
| 83 | + if (hdl->info.timerfd.dummy_host_val) { |
| 84 | + timerfd_dummy_host_read(hdl); |
| 85 | + hdl->info.timerfd.dummy_host_val = 0; |
| 86 | + } |
| 87 | + |
| 88 | + ret = (ssize_t)count; |
| 89 | +out: |
| 90 | + spinlock_unlock(&hdl->info.timerfd.expiration_lock); |
| 91 | + maybe_epoll_et_trigger(hdl, ret, /*in=*/true, /*unused was_partial=*/false); |
| 92 | + return ret; |
| 93 | +} |
| 94 | + |
| 95 | +static void timerfd_post_poll(struct libos_handle* hdl, pal_wait_flags_t* pal_ret_events) { |
| 96 | + assert(hdl->type == TYPE_TIMERFD); |
| 97 | + |
| 98 | + if (hdl->info.timerfd.broken_in_child) { |
| 99 | + log_warning("Child process tried to access timerfd created by parent process. This is " |
| 100 | + "disallowed in Gramine."); |
| 101 | + *pal_ret_events = PAL_WAIT_ERROR; |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + if (*pal_ret_events & (PAL_WAIT_ERROR | PAL_WAIT_HANG_UP | PAL_WAIT_WRITE)) { |
| 106 | + /* impossible: we control timerfd inside the LibOS, and we never raise such conditions */ |
| 107 | + BUG(); |
| 108 | + } |
| 109 | + |
| 110 | + spinlock_lock(&hdl->info.timerfd.expiration_lock); |
| 111 | + if (*pal_ret_events & PAL_WAIT_READ) { |
| 112 | + /* there is data to read: verify if timerfd has number of expirations greater than zero */ |
| 113 | + if (!hdl->info.timerfd.num_expirations) { |
| 114 | + /* spurious or malicious notification, can legitimately happen if another thread |
| 115 | + * consumed this event between this thread's poll wakeup and the post_poll callback; |
| 116 | + * we currently choose to return a spurious notification to the user */ |
| 117 | + *pal_ret_events &= ~PAL_WAIT_READ; |
| 118 | + } |
| 119 | + } |
| 120 | + spinlock_unlock(&hdl->info.timerfd.expiration_lock); |
| 121 | +} |
| 122 | + |
| 123 | +static int timerfd_close(struct libos_handle* hdl) { |
| 124 | + if (hdl->info.timerfd.broken_in_child) { |
| 125 | + log_warning("Child process tried to access timerfd created by parent process. This is " |
| 126 | + "disallowed in Gramine."); |
| 127 | + return -EIO; |
| 128 | + } |
| 129 | + |
| 130 | + /* cancel the pending timerfd object */ |
| 131 | + return install_async_event(ASYNC_EVENT_TYPE_ALARM_TIMER, hdl->pal_handle, |
| 132 | + /*time_us=*/0, /*absolute_time=*/false, /*callback=*/NULL, |
| 133 | + /*arg=*/NULL); |
| 134 | +} |
| 135 | + |
| 136 | +struct libos_fs_ops timerfd_fs_ops = { |
| 137 | + .checkin = &timerfd_checkin, |
| 138 | + .read = &timerfd_read, |
| 139 | + .close = &timerfd_close, |
| 140 | + .post_poll = &timerfd_post_poll, |
| 141 | +}; |
| 142 | + |
| 143 | +struct libos_fs timerfd_builtin_fs = { |
| 144 | + .name = "timerfd", |
| 145 | + .fs_ops = &timerfd_fs_ops, |
| 146 | +}; |
0 commit comments