-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsandbox.c
More file actions
308 lines (251 loc) · 8.39 KB
/
sandbox.c
File metadata and controls
308 lines (251 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "sandbox.h"
#include "common.h"
#include <stddef.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdint.h>
#include <time.h>
#include <stdatomic.h>
#include <unistd.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/mman.h>
static struct {
const char *appname;
int ctlpipe_wfd;
struct sandbox_shmem_header *shmem;
} sandbox = {
.appname = NULL,
.ctlpipe_wfd = -1,
.shmem = NULL,
};
static int get_monotonic_time(uint64_t *const value)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
log_errno("Cannot get monotonic time");
return -1;
}
*value = (uint64_t) ts.tv_sec * (uint64_t) 1000000000 + (uint64_t) ts.tv_nsec;
return 0;
}
struct sandbox_shmem_header *sandbox_shmem_init(void)
{
struct sandbox_shmem_header *const shmem = mmap(NULL, SANDBOX_SHMEM_SIZE,
PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if (unlikely(shmem == MAP_FAILED)) {
log_errno("Cannot map shared memory region");
goto fail;
}
pthread_mutexattr_t mutex_attr;
if (unlikely(pthread_mutexattr_init(&mutex_attr))) {
log_errno("Cannot init mutex attributes");
goto fail_unmap;
}
if (unlikely(pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED))) {
log_errno("Cannot set process-shared mutex attribute");
goto fail_destroy_mutex_attr;
}
if (unlikely(pthread_mutex_init(&shmem->lock, &mutex_attr))) {
log_errno("Cannot init mutex");
goto fail_destroy_mutex_attr;
}
pthread_condattr_t cond_attr;
if (unlikely(pthread_condattr_init(&cond_attr))) {
log_errno("Cannot init condition variable attributes");
goto fail_destroy_mutex;
}
if (unlikely(pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED))) {
log_errno("Cannot set process-shared condition variable attribute");
goto fail_destroy_cond_attr;
}
if (unlikely(pthread_cond_init(&shmem->cond, &cond_attr))) {
log_errno("Cannot init condition variable");
goto fail_destroy_cond_attr;
}
shmem->ctl = SANDBOX_CTL_CLEAR;
shmem->msg = SANDBOX_MSG_CLEAR;
shmem->state = SANDBOX_STATE_INIT;
shmem->do_quit = false;
atomic_thread_fence(memory_order_seq_cst);
if (unlikely(pthread_mutexattr_destroy(&mutex_attr))) {
log_errno("Cannot destroy mutex attributes");
}
if (unlikely(pthread_condattr_destroy(&cond_attr))) {
log_errno("Cannot destroy condition variable attributes");
}
return shmem;
fail_destroy_cond_attr:
if (unlikely(pthread_condattr_destroy(&cond_attr))) {
log_errno("Cannot destroy condition variable attributes");
}
fail_destroy_mutex:
if (unlikely(pthread_mutex_destroy(&shmem->lock))) {
log_errno("Cannot destroy mutex");
}
fail_destroy_mutex_attr:
if (unlikely(pthread_mutexattr_destroy(&mutex_attr))) {
log_errno("Cannot destroy mutex attributes");
}
fail_unmap:
if (unlikely(munmap(shmem, SANDBOX_SHMEM_SIZE))) {
log_errno("Cannot destroy shared memory region");
}
fail:
return NULL;
}
void sandbox_shmem_destroy(struct sandbox_shmem_header *const shmem)
{
if (unlikely(pthread_mutex_destroy(&shmem->lock))) {
log_errno("Cannot destroy mutex");
}
if (unlikely(pthread_cond_destroy(&shmem->cond))) {
log_errno("Cannot destroy condition variable");
}
if (unlikely(munmap(shmem, SANDBOX_SHMEM_SIZE))) {
log_errno("Cannot unmap shared memory region");
}
}
int sandbox_notify_quit(struct sandbox_shmem_header *const shmem)
{
if (unlikely(pthread_mutex_lock(&shmem->lock))) {
log_errno("Cannot lock shared memory mutex");
return -1;
}
shmem->do_quit = true;
if (unlikely(pthread_cond_signal(&shmem->cond))) {
log_errno("Cannot signal condition variable");
}
if (unlikely(pthread_mutex_unlock(&shmem->lock))) {
log_errno("Cannot unlock shared memory mutex");
}
return 0;
}
void sandbox_init(const char *const appname, const int ctlpipe_wfd, struct sandbox_shmem_header *const shmem)
{
sandbox.appname = appname;
sandbox.ctlpipe_wfd = ctlpipe_wfd;
sandbox.shmem = shmem;
}
static inline void lock_or_abort(pthread_mutex_t *const mutex)
{
if (unlikely(pthread_mutex_lock(mutex))) {
log_errno("Cannot lock shared mutex in sandbox");
abort();
}
}
static inline void unlock_or_abort(pthread_mutex_t *const mutex)
{
if (unlikely(pthread_mutex_unlock(mutex))) {
log_errno("Cannot unlock shared mutex in sandbox");
abort();
}
}
static inline void wait_or_abort(pthread_cond_t *const cond, pthread_mutex_t *const mutex)
{
if (unlikely(pthread_cond_wait(cond, mutex))) {
log_errno("Cannot wait on shared condition variable in sandbox");
abort();
}
}
static inline void signal_supervisor(void)
{
const ssize_t written = write(sandbox.ctlpipe_wfd, &(const char) {'-'}, 1);
if (unlikely(written == -1)) {
log_errno("Cannot signal supervisor from sandbox");
} else if (unlikely(written != 1)) {
log_warn("Cannot signal supervisor from sandbox, wrote %zd bytes", written);
}
}
static inline void signal_and_wait(void)
{
signal_supervisor();
do {
wait_or_abort(&sandbox.shmem->cond, &sandbox.shmem->lock);
} while (sandbox.shmem->msg != SANDBOX_MSG_CLEAR);
}
const void *sandbox_call_supervisor(const size_t ret_size, const uint64_t call_id, const void *const args, const size_t args_size)
{
static uint8_t ret[16] __attribute__((aligned(16)));
lock_or_abort(&sandbox.shmem->lock);
sandbox.shmem->msg = SANDBOX_MSG_CALL;
*(uint64_t *) sandbox.shmem->data = call_id;
if (args_size) {
memcpy(sandbox.shmem->data + 16, args, args_size);
}
signal_and_wait();
if (ret_size) {
memcpy(ret, sandbox.shmem->data + 16, ret_size);
}
unlock_or_abort(&sandbox.shmem->lock);
return ret;
}
void cart_log(const char *const fmt, ...)
{
va_list vargs;
va_start(vargs, fmt);
lock_or_abort(&sandbox.shmem->lock);
sandbox.shmem->msg = SANDBOX_MSG_LOG;
const ssize_t maxlen = SANDBOX_SHMEM_DATA_SIZE;
const int nbytes = vsnprintf((char *) sandbox.shmem->data, maxlen, fmt, vargs);
if (unlikely(nbytes < 0)) {
*sandbox.shmem->data = '\0';
log_errno("Cannot format log message");
} else if (unlikely(nbytes >= maxlen)) {
log_warn("Truncated log message from %d to %zd bytes", nbytes + 1, maxlen);
}
signal_and_wait();
unlock_or_abort(&sandbox.shmem->lock);
va_end(vargs);
}
void sandbox_loop(void)
{
char app_sopath[256];
sprintf(app_sopath, "./apps/app-%s.so", sandbox.appname);
void *const dl_handle = dlopen(app_sopath, RTLD_NOW);
if (unlikely(!dl_handle)) {
log_warn("Cannot load application: %s", dlerror());
return;
}
const char *const *const app_id = dlsym(dl_handle, "cart_app_id");
const int *const app_major = dlsym(dl_handle, "cart_app_major");
const int *const app_minor = dlsym(dl_handle, "cart_app_minor");
if (likely(app_id && app_major && app_minor)) {
log_info("Started %s %d.%d", *app_id, *app_major, *app_minor);
} else {
log_warn("No application ID and version found");
goto unload;
}
bool (*const loadfn)(void) = (bool (*)(void)) dlsym(dl_handle, "cart_load");
if (unlikely(!loadfn)) {
log_warn("No load function found: %s", dlerror());
goto unload;
} else if (unlikely(!loadfn())) {
log_warn("Load function indicated error");
goto unload;
}
for (;;) {
lock_or_abort(&sandbox.shmem->lock);
signal_supervisor();
sandbox.shmem->state = SANDBOX_STATE_IDLE;
while (sandbox.shmem->ctl != SANDBOX_CTL_EXEC && !sandbox.shmem->do_quit) {
wait_or_abort(&sandbox.shmem->cond, &sandbox.shmem->lock);
}
void (*const callback)(void) = sandbox.shmem->do_quit ? NULL :
*(void (**const)(void)) sandbox.shmem->data;
sandbox.shmem->ctl = SANDBOX_CTL_CLEAR;
sandbox.shmem->state = SANDBOX_STATE_EXEC;
unlock_or_abort(&sandbox.shmem->lock);
if (likely(callback)) {
callback();
} else {
break;
}
}
unload:
if (dlclose(dl_handle)) {
log_error("Cannot close dylib: %s", dlerror());
}
}