-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathdb_main.c
607 lines (510 loc) · 21.7 KB
/
db_main.c
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2014 Stony Brook University */
/*
* This file contains the main function of the PAL loader, which loads and processes environment,
* arguments and manifest.
*/
#include <stdbool.h>
#include "api.h"
#include "elf/elf.h"
#include "pal.h"
#include "pal_defs.h"
#include "pal_error.h"
#include "pal_internal.h"
#include "pal_rtld.h"
#include "sysdeps/generic/ldsodefs.h"
#include "toml.h"
PAL_CONTROL g_pal_control = {
/* Enable log to catch early initialization errors; it will be overwritten in pal_main(). */
.log_level = PAL_LOG_DEFAULT_LEVEL,
};
const PAL_CONTROL* DkGetPalControl(void) {
return &g_pal_control;
}
struct pal_internal_state g_pal_state;
static void load_libraries(void) {
int ret = 0;
char* preload_str = NULL;
/* FIXME: rewrite to use array-of-strings TOML syntax */
/* string with preload libs: can be multiple URIs separated by commas */
ret = toml_string_in(g_pal_state.manifest_root, "loader.preload", &preload_str);
if (ret < 0)
INIT_FAIL_MANIFEST(PAL_ERROR_INVAL, "Cannot parse 'loader.preload'");
if (!preload_str)
return;
size_t len = strlen(preload_str);
if (len == 0)
return;
char* c = preload_str;
char* library_name = c;
for (;; c++) {
if (*c == ',' || !*c) {
if (c > library_name) {
*c = 0;
if ((ret = load_elf_object(library_name, OBJECT_PRELOAD)) < 0)
INIT_FAIL(-ret, "Unable to load preload library");
}
if (c == preload_str + len)
break;
library_name = c + 1;
}
}
}
/* Finds `loader.env.{key}` in the manifest and returns its value in `out_val`/`out_passthrough`.
* Recognizes several formats:
* - `loader.env.{key} = "val"`
* - `loader.env.{key} = { value = "val" }`
* - `loader.env.{key} = { passthrough = true|false }`
*
* If `loader.env.{key}` doesn't exist, then returns 0 and `*out_exists = false`. If the key exists,
* then `*out_exists = true` and the corresponding `*out_val` or `*out_passthrough` is set.
*
* For any other format, returns negative error code. The caller is responsible to free `out_val`.
*/
static int get_env_value_from_manifest(toml_table_t* toml_envs, const char* key, bool* out_exists,
char** out_val, bool* out_passthrough) {
int ret;
toml_table_t* toml_env_table = toml_table_in(toml_envs, key);
if (!toml_env_table) {
/* not table like `loader.env.key = {...}`, must be string like `loader.env.key = "val"` */
toml_raw_t toml_env_val_raw = toml_raw_in(toml_envs, key);
if (!toml_env_val_raw) {
*out_val = NULL;
*out_passthrough = false;
*out_exists = false;
return 0;
}
if (!out_val)
return -PAL_ERROR_INVAL;
ret = toml_rtos(toml_env_val_raw, out_val);
if (ret < 0)
return -PAL_ERROR_NOMEM;
*out_passthrough = false;
*out_exists = true;
return 0;
}
toml_raw_t toml_passthrough_raw = toml_raw_in(toml_env_table, "passthrough");
toml_raw_t toml_value_raw = toml_raw_in(toml_env_table, "value");
if (toml_passthrough_raw && toml_value_raw) {
/* disallow `loader.env.key = { passthrough = true, value = "val" }` */
return -PAL_ERROR_INVAL;
}
if (!toml_passthrough_raw) {
/* not passthrough like `loader.env.key = { passthrough = true }`, must be value-table like
* `loader.env.key = { value = "val" }` */
if (!toml_value_raw || !out_val)
return -PAL_ERROR_INVAL;
ret = toml_rtos(toml_value_raw, out_val);
if (ret < 0)
return -PAL_ERROR_NOMEM;
*out_passthrough = false;
*out_exists = true;
return 0;
}
/* at this point, it must be `loader.env.key = { passthrough = true|false }`*/
if (!out_passthrough)
return -PAL_ERROR_INVAL;
int passthrough_int;
ret = toml_rtob(toml_passthrough_raw, &passthrough_int);
if (ret < 0)
return -PAL_ERROR_INVAL;
*out_passthrough = !!passthrough_int;
*out_val = NULL;
*out_exists = true;
return 0;
}
static int create_empty_envs(const char*** out_envp) {
const char** new_envp = malloc(sizeof(*new_envp));
if (!new_envp)
return -PAL_ERROR_NOMEM;
new_envp[0] = NULL;
*out_envp = new_envp;
return 0;
}
/* This function leaks memory on failure (and this is non-trivial to fix), but the assumption is
* that its failure finishes the execution of the whole process right away. */
static int deep_copy_envs(const char** envp, const char*** out_envp) {
size_t orig_envs_cnt = 0;
for (const char** orig_env = envp; *orig_env; orig_env++)
orig_envs_cnt++;
const char** new_envp = calloc(orig_envs_cnt + 1, sizeof(const char*));
if (!new_envp)
return -PAL_ERROR_NOMEM;
size_t idx = 0;
for (const char** orig_env = envp; *orig_env; orig_env++) {
new_envp[idx] = strdup(*orig_env);
if (!new_envp[idx])
return -PAL_ERROR_NOMEM;
idx++;
}
assert(idx == orig_envs_cnt);
*out_envp = new_envp;
return 0;
}
/* Build environment for Gramine process, based on original environment (from host or file) and
* manifest. If `propagate` is true, copies all variables from `orig_envp`, except the ones
* overriden in manifest. Otherwise, copies only the variables from `orig_envp` that the manifest
* specifies as passthrough.
*
* This function leaks memory on failure (and this is non-trivial to fix), but the assumption is
* that its failure finishes the execution of the whole process right away. */
static int build_envs(const char** orig_envp, bool propagate, const char*** out_envp) {
int ret;
toml_table_t* toml_loader = toml_table_in(g_pal_state.manifest_root, "loader");
if (!toml_loader)
return propagate ? deep_copy_envs(orig_envp, out_envp) : create_empty_envs(out_envp);
toml_table_t* toml_envs = toml_table_in(toml_loader, "env");
if (!toml_envs)
return propagate ? deep_copy_envs(orig_envp, out_envp) : create_empty_envs(out_envp);
ssize_t toml_envs_cnt = toml_table_nkval(toml_envs);
if (toml_envs_cnt < 0)
return -PAL_ERROR_INVAL;
ssize_t toml_env_tables_cnt = toml_table_ntab(toml_envs);
if (toml_env_tables_cnt < 0)
return -PAL_ERROR_INVAL;
toml_envs_cnt += toml_env_tables_cnt;
if (toml_envs_cnt == 0)
return propagate ? deep_copy_envs(orig_envp, out_envp) : create_empty_envs(out_envp);
size_t orig_envs_cnt = 0;
for (const char** orig_env = orig_envp; *orig_env; orig_env++)
orig_envs_cnt++;
/* For simplicity, overapproximate the number of envs by summing up the ones found in the
* original environment and the ones found in the manifest. */
size_t total_envs_cnt = orig_envs_cnt + toml_envs_cnt;
const char** new_envp = calloc(total_envs_cnt + 1, sizeof(const char*));
if (!new_envp)
return -PAL_ERROR_NOMEM;
size_t idx = 0;
/* First, go through original variables and copy the ones that we're going to use (because of
* `propagate`, or because passthrough is specified for that variable in manifest). */
for (const char** orig_env = orig_envp; *orig_env; orig_env++) {
char* orig_env_key_end = strchr(*orig_env, '=');
if (!orig_env_key_end)
return -PAL_ERROR_INVAL;
char* env_key = alloc_substr(*orig_env, orig_env_key_end - *orig_env);
if (!env_key)
return -PAL_ERROR_NOMEM;
bool exists;
char* env_val;
bool passthrough;
ret = get_env_value_from_manifest(toml_envs, env_key, &exists, &env_val, &passthrough);
if (ret < 0) {
log_error("Invalid environment variable in manifest: '%s'", env_key);
return ret;
}
if ((propagate && !exists) || (exists && passthrough)) {
new_envp[idx] = strdup(*orig_env);
if (!new_envp[idx])
return -PAL_ERROR_NOMEM;
idx++;
}
free(env_key);
free(env_val);
}
/* Then, go through the manifest variables and copy the ones with value provided. */
for (ssize_t i = 0; i < toml_envs_cnt; i++) {
const char* toml_env_key = toml_key_in(toml_envs, i);
assert(toml_env_key);
bool exists;
char* env_val;
bool passthrough;
ret = get_env_value_from_manifest(toml_envs, toml_env_key, &exists, &env_val, &passthrough);
if (ret < 0) {
log_error("Invalid environment variable in manifest: '%s'", toml_env_key);
return ret;
}
assert(exists);
if (!env_val && !passthrough) {
log_error("Detected environment variable '%s' with `passthrough = false`. For security"
" reasons, Gramine doesn't allow this. Please see documentation on"
" 'loader.env' for details.", toml_env_key);
return -PAL_ERROR_DENIED;
}
if (!env_val) {
/* this is envvar with passthrough = true, we accounted for it in previous loop */
continue;
}
char* final_env = alloc_concat3(toml_env_key, strlen(toml_env_key), "=", 1, env_val,
strlen(env_val));
if (!final_env)
return -PAL_ERROR_NOMEM;
new_envp[idx++] = final_env;
free(env_val);
}
assert(idx <= total_envs_cnt);
*out_envp = new_envp;
return 0;
}
static void configure_logging(void) {
int ret = 0;
int log_level = PAL_LOG_DEFAULT_LEVEL;
char* debug_type = NULL;
ret = toml_string_in(g_pal_state.manifest_root, "loader.debug_type", &debug_type);
if (ret < 0)
INIT_FAIL_MANIFEST(PAL_ERROR_DENIED, "Cannot parse 'loader.debug_type'");
if (debug_type) {
free(debug_type);
INIT_FAIL_MANIFEST(PAL_ERROR_DENIED,
"'loader.debug_type' has been replaced by 'loader.log_level' and 'loader.log_file'");
}
char* log_level_str = NULL;
ret = toml_string_in(g_pal_state.manifest_root, "loader.log_level", &log_level_str);
if (ret < 0)
INIT_FAIL_MANIFEST(PAL_ERROR_DENIED, "Cannot parse 'loader.log_level'");
if (log_level_str) {
if (!strcmp(log_level_str, "none")) {
log_level = LOG_LEVEL_NONE;
} else if (!strcmp(log_level_str, "error")) {
log_level = LOG_LEVEL_ERROR;
} else if (!strcmp(log_level_str, "warning")) {
log_level = LOG_LEVEL_WARNING;
} else if (!strcmp(log_level_str, "debug")) {
log_level = LOG_LEVEL_DEBUG;
} else if (!strcmp(log_level_str, "trace")) {
log_level = LOG_LEVEL_TRACE;
} else if (!strcmp(log_level_str, "all")) {
log_level = LOG_LEVEL_ALL;
} else {
INIT_FAIL_MANIFEST(PAL_ERROR_DENIED, "Unknown 'loader.log_level'");
}
}
free(log_level_str);
char* log_file = NULL;
ret = toml_string_in(g_pal_state.manifest_root, "loader.log_file", &log_file);
if (ret < 0)
INIT_FAIL_MANIFEST(PAL_ERROR_DENIED, "Cannot parse 'loader.log_file'");
if (log_file && log_level > LOG_LEVEL_NONE) {
ret = _DkInitDebugStream(log_file);
if (ret < 0)
INIT_FAIL(-ret, "Cannot open log file");
}
free(log_file);
g_pal_control.log_level = log_level;
}
/* Loads a file containing a concatenation of C-strings. The resulting array of pointers is
* NULL-terminated. All C-strings inside it reside in a single malloc-ed buffer starting at
* `(*res)[0]`. The caller must free `(*res)[0]` and then `*res` when done with the array. */
static int load_cstring_array(const char* uri, const char*** res) {
PAL_HANDLE hdl;
PAL_STREAM_ATTR attr;
char* buf = NULL;
const char** array = NULL;
int ret;
ret = _DkStreamOpen(&hdl, uri, PAL_ACCESS_RDONLY, 0, 0, 0);
if (ret < 0)
return ret;
ret = _DkStreamAttributesQueryByHandle(hdl, &attr);
if (ret < 0)
goto out_fail;
size_t file_size = attr.pending_size;
buf = malloc(file_size);
if (!buf) {
ret = -PAL_ERROR_NOMEM;
goto out_fail;
}
ret = _DkStreamRead(hdl, 0, file_size, buf, NULL, 0);
if (ret < 0)
goto out_fail;
if (file_size > 0 && buf[file_size - 1] != '\0') {
ret = -PAL_ERROR_INVAL;
goto out_fail;
}
size_t count = 0;
for (size_t i = 0; i < file_size; i++)
if (buf[i] == '\0')
count++;
array = malloc(sizeof(char*) * (count + 1));
if (!array) {
ret = -PAL_ERROR_NOMEM;
goto out_fail;
}
array[count] = NULL;
if (file_size > 0) {
const char** argv_it = array;
*(argv_it++) = buf;
for (size_t i = 0; i < file_size - 1; i++)
if (buf[i] == '\0')
*(argv_it++) = buf + i + 1;
}
*res = array;
return _DkObjectClose(hdl);
out_fail:
(void)_DkObjectClose(hdl);
free(buf);
free(array);
return ret;
}
/* 'pal_main' must be called by the host-specific loader.
* At this point the manifest is assumed to be already parsed, because some PAL loaders use manifest
* configuration for early initialization.
*/
noreturn void pal_main(uint64_t instance_id, /* current instance id */
PAL_HANDLE parent_process, /* parent process if it's a child */
PAL_HANDLE first_thread, /* first thread handle */
PAL_STR* arguments, /* application arguments */
PAL_STR* environments /* environment variables */) {
if (!instance_id) {
assert(!parent_process);
if (_DkRandomBitsRead(&instance_id, sizeof(instance_id)) < 0) {
INIT_FAIL(PAL_ERROR_DENIED, "Could not generate random instance_id");
}
}
g_pal_state.instance_id = instance_id;
g_pal_state.parent_process = parent_process;
ssize_t ret;
assert(g_pal_state.manifest_root);
assert(g_pal_state.alloc_align && IS_POWER_OF_2(g_pal_state.alloc_align));
configure_logging();
char* dummy_exec_str = NULL;
ret = toml_string_in(g_pal_state.manifest_root, "loader.exec", &dummy_exec_str);
if (ret < 0 || dummy_exec_str)
INIT_FAIL(PAL_ERROR_INVAL, "loader.exec is not supported anymore. Please update your "
"manifest according to the current documentation.");
free(dummy_exec_str);
bool disable_aslr;
ret = toml_bool_in(g_pal_state.manifest_root, "loader.insecure__disable_aslr",
/*defaultval=*/false, &disable_aslr);
if (ret < 0) {
INIT_FAIL_MANIFEST(PAL_ERROR_DENIED, "Cannot parse 'loader.insecure__disable_aslr' "
"(the value must be `true` or `false`)");
}
/* Load argv */
/* TODO: Add an option to specify argv inline in the manifest. This requires an upgrade to the
* manifest syntax. See https://github.com/oscarlab/graphene/issues/870 (Use YAML or TOML syntax
* for manifests). 'loader.argv0_override' won't be needed after implementing this feature and
* resolving https://github.com/oscarlab/graphene/issues/1053 (RFC: graphene invocation).
*/
bool argv0_overridden = false;
char* argv0_override = NULL;
ret = toml_string_in(g_pal_state.manifest_root, "loader.argv0_override", &argv0_override);
if (ret < 0)
INIT_FAIL_MANIFEST(PAL_ERROR_DENIED, "Cannot parse 'loader.argv0_override'");
if (argv0_override) {
argv0_overridden = true;
if (!arguments[0]) {
arguments = malloc(sizeof(const char*) * 2);
if (!arguments)
INIT_FAIL(PAL_ERROR_NOMEM, "malloc() failed");
arguments[1] = NULL;
}
arguments[0] = argv0_override;
}
bool use_cmdline_argv;
ret = toml_bool_in(g_pal_state.manifest_root, "loader.insecure__use_cmdline_argv",
/*defaultval=*/false, &use_cmdline_argv);
if (ret < 0) {
INIT_FAIL_MANIFEST(PAL_ERROR_DENIED, "Cannot parse 'loader.insecure__use_cmdline_argv' "
"(the value must be `true` or `false`)");
}
if (use_cmdline_argv) {
/* Warn only in the first process. */
if (!parent_process) {
log_error("Using insecure argv source. Graphene will continue application execution, "
"but this configuration must not be used in production!");
}
} else {
char* argv_src_file = NULL;
ret = toml_string_in(g_pal_state.manifest_root, "loader.argv_src_file", &argv_src_file);
if (ret < 0)
INIT_FAIL_MANIFEST(PAL_ERROR_DENIED, "Cannot parse 'loader.argv_src_file'");
if (argv_src_file) {
/* Load argv from a file and discard cmdline argv. We trust the file contents (this can
* be achieved using protected or trusted files). */
if (arguments[0] && arguments[1])
log_error("Discarding cmdline arguments (%s %s [...]) because loader.argv_src_file "
"was specified in the manifest.", arguments[0], arguments[1]);
ret = load_cstring_array(argv_src_file, &arguments);
if (ret < 0)
INIT_FAIL(-ret, "Cannot load arguments from 'loader.argv_src_file'");
free(argv_src_file);
} else if (!argv0_overridden || (arguments[0] && arguments[1])) {
INIT_FAIL(PAL_ERROR_INVAL, "argv handling wasn't configured in the manifest, but "
"cmdline arguments were specified.");
}
}
/* we don't modify original `environments` but deep-copy them into `final_environments`,
* augmented based on `loader.env.key` manifest options */
const char** orig_environments = NULL;
const char** final_environments = NULL;
bool use_host_env;
ret = toml_bool_in(g_pal_state.manifest_root, "loader.insecure__use_host_env",
/*defaultval=*/false, &use_host_env);
if (ret < 0) {
INIT_FAIL_MANIFEST(PAL_ERROR_DENIED, "Cannot parse 'loader.insecure__use_host_env' "
"(the value must be `true` or `false`)");
}
if (use_host_env) {
/* Warn only in the first process. */
if (!parent_process) {
log_error("Forwarding host environment variables to the app is enabled. All "
"'passthrough' environment variables in the manifest are ignored. Graphene "
"will continue application execution, but this configuration must not be "
"used in production!");
}
}
char* env_src_file = NULL;
ret = toml_string_in(g_pal_state.manifest_root, "loader.env_src_file", &env_src_file);
if (ret < 0)
INIT_FAIL_MANIFEST(PAL_ERROR_DENIED, "Cannot parse 'loader.env_src_file'");
if (use_host_env && env_src_file)
INIT_FAIL(PAL_ERROR_INVAL, "Wrong manifest configuration - cannot use "
"loader.insecure__use_host_env and loader.env_src_file at the same time.");
if (env_src_file) {
/* Insert environment variables from a file. We trust the file contents (this can be
* achieved using protected or trusted files). */
ret = load_cstring_array(env_src_file, &orig_environments);
if (ret < 0)
INIT_FAIL(-ret, "Cannot load environment variables from 'loader.env_src_file'");
} else {
/* Environment variables are taken from the host. */
orig_environments = environments;
}
// TODO: Envs from file should be able to override ones from the manifest, but current
// code makes this hard to implement.
ret = build_envs(orig_environments, use_host_env || env_src_file, &final_environments);
if (ret < 0)
INIT_FAIL(-ret, "Building the final environment based on the original environment and the"
" manifest failed");
if (orig_environments != environments) {
free((char*)orig_environments[0]);
free(orig_environments);
}
free(env_src_file);
load_libraries();
// TODO: This is just an ugly, temporary hack for PAL regression tests and should only be used
// there until we clean up the way LibOS is loaded.
char* entrypoint;
ret = toml_string_in(g_pal_state.manifest_root, "pal.entrypoint", &entrypoint);
if (ret < 0)
INIT_FAIL_MANIFEST(PAL_ERROR_INVAL, "Cannot parse 'pal.entrypoint'");
if (entrypoint) {
if (!strstartswith(entrypoint, URI_PREFIX_FILE))
INIT_FAIL(PAL_ERROR_INVAL, "'pal.entrypoint' is missing 'file:' prefix");
}
g_pal_control.host_type = XSTRINGIFY(HOST_TYPE);
g_pal_control.manifest_root = g_pal_state.manifest_root;
g_pal_control.parent_process = parent_process;
g_pal_control.first_thread = first_thread;
g_pal_control.disable_aslr = disable_aslr;
_DkGetAvailableUserAddressRange(&g_pal_control.user_address.start,
&g_pal_control.user_address.end);
g_pal_control.alloc_align = g_pal_state.alloc_align;
if (_DkGetCPUInfo(&g_pal_control.cpu_info) < 0) {
goto out_fail;
}
g_pal_control.mem_info.mem_total = _DkMemoryQuota();
if (_DkGetTopologyInfo(&g_pal_control.topo_info) < 0) {
goto out_fail;
}
if (entrypoint) {
// Temporary hack: Assume we're in PAL regression test suite and load the test binary
// directly, without LibOS.
if ((ret = load_elf_object(entrypoint, OBJECT_EXEC)) < 0)
INIT_FAIL(-ret, "Unable to load pal.entrypoint");
}
/* Now we will start the execution */
start_execution(arguments, final_environments);
out_fail:
/* We wish we will never reached here */
INIT_FAIL(PAL_ERROR_DENIED, "unexpected termination");
}