-
Notifications
You must be signed in to change notification settings - Fork 88
/
setup.c
1523 lines (1314 loc) · 43.1 KB
/
setup.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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/random.h>
#include <stdlib.h>
#define _GNU_SOURCE // Needed for strchrnul
#include <lkl.h>
#include <lkl_host.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/types.h>
/* This must be included before oe_compat.h */
#include "libdevmapper.h"
#include <syscall.h>
#include <time.h>
#include <lkl.h>
#include <lkl_host.h>
#include <unistd.h>
#define USE_CRYPT_SETUP
#include "enclave/enclave_oe.h"
#include "enclave/enclave_util.h"
#include "enclave/sgxlkl_t.h"
#include "enclave/wireguard.h"
#include "enclave/wireguard_util.h"
#ifdef USE_CRYPT_SETUP
#include "libcryptsetup.h"
#endif
#include "lkl/disk.h"
#include "lkl/ext4_create.h"
#include "lkl/posix-host.h"
#include "lkl/setup.h"
#include "lkl/syscall-overrides.h"
#include "lkl/virtio_device.h"
#include "lkl/virtio_net.h"
#ifdef USE_CRYPT_SETUP
#include "libcryptsetup.h"
#endif
#include "enclave/enclave_util.h"
#include "enclave/lthread.h"
#include "enclave/sgxlkl_t.h"
#include "enclave/wireguard.h"
#include "enclave/wireguard_util.h"
#include "shared/env.h"
#include "shared/sgxlkl_enclave_config.h"
#include "shared/timer_dev.h"
#include "openenclave/corelibc/oestring.h"
#define UMOUNT_DISK_TIMEOUT 2000
// Block size in bytes of the ext4 filesystem for newly created empty disks.
// Should be a multiple of the kernel page size to avoid
// issues with sparse/unwiped dm-integrity devices.
#define CREATED_DISK_EXT4_BLOCK_SIZE 4096
// The size of the generated key in bits for newly created empty disks.
#define CREATED_DISK_KEY_LENGTH 512
// The assumed size overhead for encrypted & integrity-protected disks
// as ratio of the original disk size.
#define CREATED_DISK_ENCRYPTION_OVERHEAD 0.15
#define BOOTARGS_LEN 128
/* Console argument for bootargs */
#define BOOTARGS_CONSOLE_OPTION "console=hvc0"
#define BOOTARGS_QUIET_OPTION "quiet"
int sethostname(const char*, size_t);
int sgxlkl_trace_lkl_syscall = 0;
int sgxlkl_trace_internal_syscall = 0;
int sgxlkl_trace_ignored_syscall = 0;
int sgxlkl_trace_unsupported_syscall = 0;
int sgxlkl_trace_redirect_syscall = 0;
int sgxlkl_trace_mmap = 0;
int sgxlkl_trace_signal = 0;
int sgxlkl_trace_thread = 0;
int sgxlkl_trace_disk = 0;
int sgxlkl_use_host_network = 0;
int sgxlkl_mtu = 0;
extern struct timespec sgxlkl_app_starttime;
/* Function to setup bounce buffer in LKL */
extern void initialize_enclave_event_channel(
enc_dev_config_t* enc_dev_config,
size_t evt_channel_num);
extern void lkl_virtio_netdev_remove(void);
extern void vio_terminate(void);
/* Set by sgx-lkl-disk measure */
const uint8_t disk_dm_verity_root_hash[32] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
static void lkl_prepare_rootfs(const char* dirname, int perm)
{
int err = lkl_sys_access(dirname, /*LKL_S_IRWXO*/ F_OK);
if (err < 0)
{
if (err == -LKL_ENOENT)
err = lkl_sys_mkdir(dirname, perm);
if (err < 0)
{
sgxlkl_fail("Unable to mkdir %s: %s\n", dirname, lkl_strerror(err));
}
}
else
{
lkl_sys_chmod(dirname, perm);
}
}
static void lkl_copy_blkdev_nodes(const char* srcdir, const char* dstdir)
{
int err = 0;
struct lkl_dir* dir = lkl_opendir(srcdir, &err);
if (dir == NULL || err != 0)
{
sgxlkl_fail("Unable to opendir(%s)\n", srcdir);
}
char srcbuf[512] = {0};
char dstbuf[512] = {0};
strncpy(srcbuf, srcdir, sizeof(srcbuf));
strncpy(dstbuf, dstdir, sizeof(dstbuf));
int srcdir_len = strlen(srcbuf);
int dstdir_len = strlen(dstbuf);
if (srcbuf[srcdir_len - 1] != '/')
srcbuf[srcdir_len++] = '/';
if (dstbuf[dstdir_len - 1] != '/')
dstbuf[dstdir_len++] = '/';
struct lkl_linux_dirent64* dev = NULL;
while ((dev = lkl_readdir(dir)) != NULL)
{
strncpy(srcbuf + srcdir_len, dev->d_name, sizeof(srcbuf) - srcdir_len);
strncpy(dstbuf + dstdir_len, dev->d_name, sizeof(dstbuf) - dstdir_len);
struct lkl_stat stat;
err = lkl_sys_stat(srcbuf, &stat);
if (err != 0)
{
sgxlkl_fail("lkl_sys_stat(%s) %s\n", srcbuf, lkl_strerror(err));
}
if (!LKL_S_ISBLK(stat.st_mode))
continue;
lkl_sys_unlink(dstbuf);
err = lkl_sys_mknod(dstbuf, LKL_S_IFBLK | 0600, stat.st_rdev);
if (err != 0)
{
sgxlkl_fail("lkl_sys_mknod(%s) %s\n", dstbuf, lkl_strerror(err));
}
}
err = lkl_errdir(dir);
if (err != 0)
{
sgxlkl_fail("lkl_readdir(%s) = %d\n", srcdir, err);
}
err = lkl_closedir(dir);
if (err != 0)
{
sgxlkl_fail("lkl_closedir(%s) = %d\n", srcdir, err);
}
}
static void lkl_mount_devtmpfs(const char* mntpoint)
{
int err = lkl_sys_mount("devtmpfs", (char*)mntpoint, "devtmpfs", 0, NULL);
if (err != 0)
{
sgxlkl_fail("lkl_sys_mount(devtmpfs): %s\n", lkl_strerror(err));
}
}
static void lkl_mount_shmtmpfs()
{
int err = lkl_sys_mount("tmpfs", "/dev/shm", "tmpfs", 0, "mode=1777,rw");
if (err != 0)
{
sgxlkl_fail("lkl_sys_mount(tmpfs) (/dev/shm): %s\n", lkl_strerror(err));
}
}
static void lkl_mount_tmpfs()
{
int err = lkl_sys_mount("tmpfs", "/tmp", "tmpfs", 0, "mode=1777");
if (err != 0)
{
sgxlkl_fail("lkl_sys_mount(tmpfs): %s\n", lkl_strerror(err));
}
}
static void lkl_mount_mntfs()
{
int err = lkl_sys_mount("tmpfs", "/mnt", "tmpfs", 0, "mode=0777");
if (err != 0)
{
sgxlkl_fail("lkl_sys_mount(tmpfs): %s\n", lkl_strerror(err));
}
}
static void lkl_mount_sysfs()
{
int err = lkl_sys_mount("none", "/sys", "sysfs", 0, NULL);
if (err != 0)
{
sgxlkl_fail("lkl_sys_mount(sysfs): %s\n", lkl_strerror(err));
}
}
static void lkl_mount_runfs()
{
int err = lkl_sys_mount("tmpfs", "/run", "tmpfs", 0, "mode=0700");
if (err != 0)
{
sgxlkl_fail("lkl_sys_mount(tmpfs): %s\n", lkl_strerror(err));
}
}
static void lkl_mount_procfs()
{
int err = lkl_sys_mount("proc", "/proc", "proc", 0, NULL);
if (err != 0)
{
sgxlkl_fail("lkl_sys_mount(procfs): %s\n", lkl_strerror(err));
}
}
static void lkl_mknods()
{
lkl_sys_unlink("/dev/null");
int err = lkl_sys_mknod("/dev/null", LKL_S_IFCHR | 0666, LKL_MKDEV(1, 3));
if (err != 0)
{
sgxlkl_fail("lkl_sys_mknod(/dev/null) %s\n", lkl_strerror(err));
}
lkl_sys_unlink("/dev/zero");
err = lkl_sys_mknod("/dev/zero", LKL_S_IFCHR | 0666, LKL_MKDEV(1, 5));
if (err != 0)
{
sgxlkl_fail("lkl_sys_mknod(/dev/zero) %s\n", lkl_strerror(err));
}
lkl_sys_unlink("/dev/random");
err = lkl_sys_mknod("/dev/random", LKL_S_IFCHR | 0444, LKL_MKDEV(1, 8));
if (err != 0)
{
sgxlkl_fail("lkl_sys_mknod(/dev/random) %s\n", lkl_strerror(err));
}
lkl_sys_unlink("/dev/urandom");
err = lkl_sys_mknod("/dev/urandom", LKL_S_IFCHR | 0444, LKL_MKDEV(1, 9));
if (err != 0)
{
sgxlkl_fail("lkl_sys_mknod(/dev/urandom) %s\n", lkl_strerror(err));
}
}
static int lkl_mount_blockdev(
const char* dev_str,
const char* mnt_point,
const char* fs_type,
int flags,
const char* data)
{
char _data[4096];
int err;
err = lkl_sys_access("/mnt", LKL_S_IRWXO);
if (err < 0)
{
if (err == -LKL_ENOENT)
err = lkl_sys_mkdir("/mnt", 0755);
if (err < 0)
goto fail;
}
// Create mount directory if it does not exist.
// Allow existing directories so that disks can be mounted in read-only root
// fs.
const int mkdir_err = lkl_sys_mkdir(mnt_point, 0755);
if (mkdir_err < 0 && mkdir_err != -LKL_EEXIST)
goto fail;
if (data)
{
strncpy(_data, data, sizeof(_data));
_data[sizeof(_data) - 1] = 0;
}
else
{
_data[0] = 0;
}
err = lkl_sys_mount(
(char*)dev_str, (char*)mnt_point, (char*)fs_type, flags, _data);
if (err < 0)
{
if (mkdir_err >= 0)
lkl_sys_rmdir(mnt_point);
goto fail;
}
fail:
return err;
}
static void lkl_mount_overlay_tmpfs(const char* mnt_point)
{
int err = lkl_sys_mount("tmpfs", (char*)mnt_point, "tmpfs", 0, "mode=0777");
if (err != 0)
{
sgxlkl_fail("lkl_sys_mount(tmpfs): %s\n", lkl_strerror(err));
}
}
static void lkl_mount_overlayfs(
const char* lower_dir,
const char* upper_dir,
const char* work_dir,
const char* mnt_point)
{
char opts[200];
oe_snprintf(
opts,
sizeof(opts),
"lowerdir=%s,upperdir=%s,workdir=%s",
lower_dir,
upper_dir,
work_dir);
int err = lkl_sys_mount("overlay", (char*)mnt_point, "overlay", 0, opts);
if (err != 0)
{
sgxlkl_fail("lkl_sys_mount(overlayfs): %s\n", lkl_strerror(err));
}
}
typedef struct
{
bool create;
const char* destination;
size_t key_len;
uint8_t* key;
const char* key_id;
bool fresh_key;
bool readonly;
const char* roothash;
size_t roothash_offset;
size_t size;
bool overlay;
} disk_config_t;
struct lkl_crypt_device
{
char* disk_path;
int readonly;
disk_config_t disk_config;
char* crypt_name;
};
#ifdef USE_CRYPT_SETUP
static void* lkl_activate_crypto_disk_thread(struct lkl_crypt_device* lkl_cd)
{
int err;
char* disk_path = lkl_cd->disk_path;
struct crypt_device* cd;
err = crypt_init(&cd, disk_path);
if (err != 0)
{
sgxlkl_fail("crypt_init(): %s (%d)\n", strerror(-err), err);
}
err = crypt_load(cd, CRYPT_LUKS2, NULL);
if (err != 0)
{
sgxlkl_fail("crypt_load(): %s (%d)\n", strerror(-err), err);
}
err = crypt_activate_by_passphrase(
cd,
lkl_cd->crypt_name,
CRYPT_ANY_SLOT,
(char*)lkl_cd->disk_config.key,
lkl_cd->disk_config.key_len,
lkl_cd->readonly ? CRYPT_ACTIVATE_READONLY : 0);
if (err == -1)
{
sgxlkl_fail("Unable to activate encrypted disk. Please ensure you "
"have provided the correct passphrase/keyfile!\n");
}
else if (err != 0)
{
sgxlkl_fail(
"Unable to activate encrypted disk due to unknown error (error "
"code: %d, message: %s)\n",
err,
strerror(-err));
}
crypt_free(cd);
// The key is only needed during activation, so don't keep it around
// afterwards and free up space.
memset(lkl_cd->disk_config.key, 0, lkl_cd->disk_config.key_len);
lkl_cd->disk_config.key = NULL;
lkl_cd->disk_config.key_len = 0;
return 0;
}
#endif
// ATTN: integrity is disabled for now because dm-ioctl() depletes kernel
// memory when formatting large integrity devices (when executing within the
// dm-integrity module). This can be overcome either by making the disk image
// small or making the kernel memory large. The following turns out to work
// for 3GB disks.
//
// export SGXLKL_CMDLINE="mem=80M"
//
// But allocating this much memory to the kernel is unreasonable. The only
// workable solution seems to be formatting integrity devices in user space.
// It is unclear why dm-crypt and dm-verity formatting is done in user space
// while dm-intetrity formatting is done in kernel space.
//
// The failure begins in vic_dm_create_integrity() and occcurs when libdevmapper
// invokes icotl() with the DM_RELOAD_CMD request. The kernel then panics with
// an out-of-memory error. Custom device-mapper user-space code was written
// that bypasses libdevmapper but the error is the same.
//
// Recall that integrity formatting is performed by the dm-integrity module
// within the kernel. The integrity superblock is initially zero-filled. When
// activated for the first time, the kernel module formats the device.
// #define ENABLE_INTEGRITY
#ifdef USE_CRYPT_SETUP
static void* lkl_create_crypto_disk_thread(struct lkl_crypt_device* lkl_cd)
{
int err;
/* ATTN: vicsetup only supports 512 bytes sectors for integrity */
/* The sector size for the encrypted disk image */
static const size_t SECTOR_SIZE = 512;
/* The number of iterations performed during key derivation (LUKS2) */
static const size_t MIN_LUKS2_ITERATIONS = 1000;
char* disk_path = lkl_cd->disk_path;
struct crypt_device* cd;
err = crypt_init(&cd, disk_path);
if (err != 0)
sgxlkl_fail("crypt_init(): %s (%d)\n", strerror(-err), err);
// As we generate our own key and don't use a simple "password" we use
// the minimal kdf settings possible.
struct crypt_pbkdf_type pbkdf =
{
.type = "pbkdf2",
.hash = "sha256",
.iterations = MIN_LUKS2_ITERATIONS,
};
struct crypt_params_luks2 params =
{
.sector_size = SECTOR_SIZE,
.pbkdf = &pbkdf,
#ifdef ENABLE_INTEGRITY
.integrity = "hmac(sha256)"
#endif
};
#ifdef ENABLE_INTEGRITY
size_t volume_key_size = 96; /* key size plus integrity tag size */
#else
size_t volume_key_size = 64;
#endif
const char* cipher = "aes";
const char* cipher_mode = "xts-plain64";
err = crypt_format(
cd,
CRYPT_LUKS2,
cipher,
cipher_mode,
NULL,
NULL,
volume_key_size,
¶ms);
if (err != 0)
sgxlkl_fail("crypt_format(): %s (%d)\n", strerror(-err), err);
err = crypt_keyslot_add_by_key(
cd,
CRYPT_ANY_SLOT,
NULL,
0,
(void*)lkl_cd->disk_config.key,
lkl_cd->disk_config.key_len,
0);
if (err != 0)
sgxlkl_fail(
"crypt_keyslot_add_by_key(): %s (%d)\n", strerror(-err), err);
crypt_free(cd);
return 0;
}
#endif
#ifdef USE_CRYPT_SETUP
static void* lkl_activate_verity_disk_thread(struct lkl_crypt_device* lkl_cd)
{
int err;
char* disk_path = lkl_cd->disk_path;
struct crypt_device* cd;
// cryptsetup!
err = crypt_init(&cd, disk_path);
if (err != 0)
{
sgxlkl_fail("crypt_init(): %s (%d)\n", strerror(err), err);
}
/*
* The dm-verity Merkle tree that contains the hashes of all data blocks is
* stored on the disk image following the actual data blocks. The offset
* that signifies both the end of the data region as well as the start of
* the hash region has to be provided to SGX-LKL.
*/
struct crypt_params_verity verity_params = {
.data_device = disk_path,
.hash_device = disk_path,
.hash_area_offset = lkl_cd->disk_config.roothash_offset,
.data_size = lkl_cd->disk_config.roothash_offset /
512, // In blocks, divide by block size
.data_block_size = 512,
.hash_block_size = 512,
};
err = crypt_load(cd, CRYPT_VERITY, &verity_params);
if (err != 0)
{
sgxlkl_fail("crypt_load(): %s (%d)\n", strerror(err), err);
}
uint8_t* volume_hash_bytes = NULL;
ssize_t hash_size = crypt_get_volume_key_size(cd);
if (hex_to_bytes(lkl_cd->disk_config.roothash, &volume_hash_bytes) !=
hash_size)
{
sgxlkl_fail("Invalid root hash string specified!\n");
}
err = crypt_activate_by_volume_key(
cd,
lkl_cd->crypt_name,
(char*)volume_hash_bytes,
hash_size,
lkl_cd->readonly ? CRYPT_ACTIVATE_READONLY : 0);
if (err != 0)
{
sgxlkl_fail(
"crypt_activate_by_volume_key(): %s (%d)\n", strerror(err), err);
}
// ATTN: This crashes!
free(volume_hash_bytes);
crypt_free(cd);
return NULL;
}
#endif
static bool is_encrypted_cfg(disk_config_t* cfg)
{
return cfg->key || cfg->key_id || cfg->fresh_key;
}
static void lkl_mount_virtual()
{
lkl_mount_devtmpfs("/dev");
lkl_prepare_rootfs("/proc", 0700);
lkl_mount_procfs();
lkl_prepare_rootfs("/sys", 0700);
lkl_mount_sysfs();
lkl_prepare_rootfs("/run", 0700);
lkl_mount_runfs();
lkl_mknods();
}
static void lkl_mount_disk(
disk_config_t* disk,
char device,
const char* mnt_point,
size_t disk_index)
{
char dev_str_raw[] = {"/dev/vdX"};
char dev_str_enc[] = {"/dev/mapper/cryptX"};
char dev_str_verity[] = {"/dev/mapper/verityX"};
const size_t offset_dev_str_crypt_name = sizeof "/dev/mapper/" - 1;
dev_str_raw[sizeof dev_str_raw - 2] = device;
char* dev_str = dev_str_raw;
SGXLKL_VERBOSE(
"lkl_mount_disk(dev=\"%s\", mnt=\"%s\", ro=%i)\n",
dev_str,
mnt_point,
disk->readonly);
struct lkl_crypt_device lkl_cd;
lkl_cd.disk_path = dev_str;
lkl_cd.readonly = disk->readonly;
lkl_cd.disk_config = *disk;
(void)lkl_cd;
if (disk->create && disk->fresh_key)
{
disk->key_len = CREATED_DISK_KEY_LENGTH / 8;
SGXLKL_VERBOSE("Generating random disk encryption key\n");
disk->key = malloc(disk->key_len);
if (disk->key == NULL)
sgxlkl_fail("Could not allocate memory for disk encryption key\n");
for (size_t i = 0; i < disk->key_len; i++)
/* TODO: keys should be set up prior to reaching this function.
* Also, if we need fresh keys at all, they should be generated
* properly, e.g. by using the DRNG instructions or mbedTLS for RSA
* keys. */
disk->key[i] = rand();
}
int lkl_trace_lkl_syscall_bak = sgxlkl_trace_lkl_syscall;
int lkl_trace_internal_syscall_bak = sgxlkl_trace_internal_syscall;
if ((sgxlkl_trace_lkl_syscall || sgxlkl_trace_internal_syscall) &&
(disk->roothash || is_encrypted_cfg(disk)))
{
sgxlkl_trace_lkl_syscall = 0;
sgxlkl_trace_internal_syscall = 0;
SGXLKL_VERBOSE("Disk encryption/integrity enabled: Temporarily "
"disabling tracing to reduce noise.\n");
}
if (disk->roothash != NULL)
{
SGXLKL_VERBOSE("Activating verity disk\n");
dev_str_verity[sizeof dev_str_verity - 2] = device;
lkl_cd.crypt_name = dev_str_verity + offset_dev_str_crypt_name;
#ifdef USE_CRYPT_SETUP
lkl_activate_verity_disk_thread(&lkl_cd);
#endif
// We now want to mount the verified volume
dev_str = dev_str_verity;
lkl_cd.disk_path = dev_str_verity;
// dm-verity is read only
disk->readonly = 1;
lkl_cd.readonly = 1;
}
if (is_encrypted_cfg(disk))
{
dev_str_enc[sizeof dev_str_enc - 2] = device;
lkl_cd.crypt_name = dev_str_enc + offset_dev_str_crypt_name;
if (disk->create)
{
SGXLKL_VERBOSE("Creating empty crypto disk\n");
#ifdef USE_CRYPT_SETUP
lkl_create_crypto_disk_thread(&lkl_cd);
#endif
}
SGXLKL_VERBOSE("Activating crypto disk\n");
#ifdef USE_CRYPT_SETUP
lkl_activate_crypto_disk_thread(&lkl_cd);
#endif
// We now want to mount the decrypted volume
dev_str = dev_str_enc;
}
if ((lkl_trace_lkl_syscall_bak && !sgxlkl_trace_lkl_syscall) ||
(lkl_trace_internal_syscall_bak && !sgxlkl_trace_internal_syscall))
{
SGXLKL_VERBOSE(
"Disk encryption/integrity enabled: Re-enabling tracing.\n");
sgxlkl_trace_lkl_syscall = lkl_trace_lkl_syscall_bak;
sgxlkl_trace_internal_syscall = lkl_trace_internal_syscall_bak;
}
if (disk->create)
{
size_t fs_size = disk->size;
if (is_encrypted_cfg(disk))
{
SGXLKL_VERBOSE(
"Assuming a disk encryption/integrity overhead of %d %%\n",
(int)(CREATED_DISK_ENCRYPTION_OVERHEAD * 100));
fs_size = fs_size * (1 - CREATED_DISK_ENCRYPTION_OVERHEAD);
// align to block size
fs_size = (fs_size + CREATED_DISK_EXT4_BLOCK_SIZE - 1) /
CREATED_DISK_EXT4_BLOCK_SIZE *
CREATED_DISK_EXT4_BLOCK_SIZE;
}
int result;
unsigned long long num_blocks = fs_size / CREATED_DISK_EXT4_BLOCK_SIZE;
SGXLKL_VERBOSE("Creating ext4 filesystem of size %ld\n", fs_size);
SGXLKL_VERBOSE(
"make_ext4_dev(block_size=\"%d\", num_blocks=\"%lld\")\n",
CREATED_DISK_EXT4_BLOCK_SIZE,
num_blocks);
result =
make_ext4_dev(dev_str, CREATED_DISK_EXT4_BLOCK_SIZE, num_blocks);
if (result != 0)
sgxlkl_fail("make_ext4_dev()=%s\n", result);
}
const int err = lkl_mount_blockdev(
dev_str, mnt_point, "ext4", disk->readonly ? LKL_MS_RDONLY : 0, NULL);
if (err < 0)
sgxlkl_fail("lkl_mount_blockdev()=%s (%d)\n", lkl_strerror(err), err);
sgxlkl_enclave_state.disk_state[disk_index].mounted = true;
}
static void lkl_mount_root_disk(
const sgxlkl_enclave_root_config_t* root,
size_t disk_index)
{
int err = 0;
char mnt_point[] = "/mnt/vda";
char new_dev_str[] = "/mnt/vda/dev/";
// If any byte of disk_dm_verity_root_hash is not 0xff, the verification
// is run to compare disk_dm_verity_root_hash against disk->roothash.
// We assume no valid root hash would be all 0xff.
for (size_t i = 0; i < sizeof(disk_dm_verity_root_hash); ++i)
{
if (disk_dm_verity_root_hash[i] != 0xff)
{
SGXLKL_VERBOSE("Verifing root hash with embedded one.\n");
char buf[2 * sizeof(disk_dm_verity_root_hash) + 1];
if (bytes_to_hex(
buf,
sizeof(buf),
disk_dm_verity_root_hash,
sizeof(disk_dm_verity_root_hash)) == NULL)
{
sgxlkl_fail("bytes_to_hex() failed.\n");
}
if (root->roothash == NULL || strcmp(root->roothash, buf) != 0)
{
sgxlkl_fail(
"The root hash does not match with embedded one.\n");
}
break;
}
}
disk_config_t cfg = {.create = false,
.destination = "/",
.key_len = root->key_len,
.key = root->key,
.key_id = root->key_id,
.fresh_key = false,
.readonly = root->readonly,
.roothash = root->roothash,
.roothash_offset = root->roothash_offset,
.size = 0,
.overlay = root->overlay};
lkl_mount_disk(&cfg, 'a', mnt_point, 0);
if (root->overlay)
{
oe_host_printf("Creating writable in-memory overlay for rootfs\n");
SGXLKL_VERBOSE("Creating writable in-memory overlay for rootfs.\n");
const char mnt_point_overlay[] = "/mnt/oda";
const char mnt_point_overlay_upper[] = "/mnt/oda-upper";
const char overlay_upper_dir[] = "/mnt/oda-upper/upper";
const char overlay_work_dir[] = "/mnt/oda-upper/work";
lkl_prepare_rootfs(mnt_point_overlay_upper, 0755);
lkl_mount_overlay_tmpfs(mnt_point_overlay_upper);
lkl_prepare_rootfs(overlay_upper_dir, 0755);
lkl_prepare_rootfs(overlay_work_dir, 0755);
lkl_prepare_rootfs(mnt_point_overlay, 0755);
lkl_mount_overlayfs(
mnt_point, overlay_upper_dir, overlay_work_dir, mnt_point_overlay);
strcpy(mnt_point, mnt_point_overlay);
strcpy(new_dev_str, "/mnt/oda/dev/");
}
/* set up /dev in the new root */
lkl_prepare_rootfs(new_dev_str, 0700);
lkl_mount_devtmpfs(new_dev_str);
lkl_copy_blkdev_nodes("/dev/", new_dev_str);
/* clean up */
lkl_sys_umount("/proc", 0);
lkl_sys_umount("/sys", 0);
lkl_sys_umount("/run", 0);
lkl_sys_umount("/dev", 0);
/* pivot */
err = lkl_sys_chroot(mnt_point);
if (err != 0)
{
sgxlkl_fail("lkl_sys_chroot(%s): %s\n", mnt_point, lkl_strerror(err));
}
err = lkl_sys_chdir("/");
if (err != 0)
{
sgxlkl_fail("lkl_sys_chdir(%s): %s\n", mnt_point, lkl_strerror(err));
}
lkl_prepare_rootfs("/", 0755);
lkl_prepare_rootfs("/dev", 0755);
lkl_prepare_rootfs("/dev/shm", 0777);
lkl_prepare_rootfs("/mnt", 0755);
lkl_prepare_rootfs("/tmp", 0777);
lkl_prepare_rootfs("/sys", 0755);
lkl_prepare_rootfs("/run", 0755);
lkl_prepare_rootfs("/proc", 0755);
lkl_mount_shmtmpfs();
lkl_mount_tmpfs();
lkl_mount_mntfs();
lkl_mount_sysfs();
lkl_mount_runfs();
lkl_mount_procfs();
}
void lkl_mount_disks(
const sgxlkl_enclave_root_config_t* root,
const sgxlkl_enclave_mount_config_t* mounts,
size_t num_mounts,
const char* cwd)
{
#ifdef DEBUG
if (sgxlkl_trace_disk)
crypt_set_debug_level(CRYPT_LOG_DEBUG);
#endif
if (!root)
sgxlkl_fail("No root disk provided. Aborting...\n");
if (lkl_add_disks(root, mounts, num_mounts) != 0)
sgxlkl_fail("Add root disk failed. Aborting...\n");
lkl_mount_root_disk(root, 0);
for (size_t mnt_idx = 0; mnt_idx < num_mounts; mnt_idx++)
{
size_t dsk_idx = mnt_idx + 1;
SGXLKL_ASSERT(strcmp(mounts[mnt_idx].destination, "/") != 0);
// We assign dev paths from /dev/vda to /dev/vdz, assuming we won't need
// support for more than 26 disks.
if ('a' + dsk_idx > 'z')
{
sgxlkl_warn(
"Too many disks (maximum is 26). Failed to mount disk %d at "
"%s.\n",
dsk_idx,
mounts[mnt_idx].destination);
// Adjust number to number of mounted disks.
num_mounts = 25;
return;
}
disk_config_t cfg = {.create = mounts[mnt_idx].create,
.destination = mounts[mnt_idx].destination,
.key_len = mounts[mnt_idx].key_len,
.key = mounts[mnt_idx].key,
.key_id = mounts[mnt_idx].key_id,
.fresh_key = mounts[mnt_idx].fresh_key,
.readonly = mounts[mnt_idx].readonly,
.roothash = mounts[mnt_idx].roothash,
.roothash_offset = mounts[mnt_idx].roothash_offset,
.size = mounts[mnt_idx].size,
.overlay = false};
lkl_mount_disk(&cfg, 'a' + dsk_idx, cfg.destination, dsk_idx);
}
if (cwd)
{
SGXLKL_VERBOSE("Set working directory: \'%s\'\n", cwd);
int ret = lkl_sys_chdir(cwd);
if (ret != 0)
{
sgxlkl_fail(
"lkl_sys_chdir(%s) failed: ret=%i error=\"%s\"\n",
cwd,
ret,
lkl_strerror(ret));
}
}
}
static uint32_t _parse_ip4(const char* str)
{
struct in_addr ia_tmp = {0};
if (inet_pton(AF_INET, str, &ia_tmp) != 1)
sgxlkl_fail("Invalid IPv4 address: %s\n", str);
return ia_tmp.s_addr;
}
void lkl_poststart_net(int net_dev_id)
{
const sgxlkl_enclave_config_t* cfg = sgxlkl_enclave_state.config;
int res = 0;
if (net_dev_id >= 0)
{
int ifidx = lkl_netdev_get_ifindex(net_dev_id);
uint32_t ip4 = _parse_ip4(cfg->net_ip4);
res = lkl_if_set_ipv4(ifidx, ip4, atoi(cfg->net_mask4));
if (res < 0)
{
sgxlkl_fail("lkl_if_set_ipv4(): %s\n", lkl_strerror(res));
}
res = lkl_if_up(ifidx);
if (res < 0)
{
sgxlkl_fail("lkl_if_up(eth0): %s\n", lkl_strerror(res));
}
if (cfg->net_gw4 > 0)
{
uint32_t gw4 = _parse_ip4(cfg->net_gw4);
res = lkl_set_ipv4_gateway(gw4);
if (res < 0)
{
sgxlkl_fail("lkl_set_ipv4_gateway(): %s\n", lkl_strerror(res));
}
}
if (sgxlkl_mtu)
{
lkl_if_set_mtu(ifidx, sgxlkl_mtu);
}
}
res = lkl_if_up(1);
if (res < 0)
{
sgxlkl_fail("lkl_if_up(1=lo): %s\n", lkl_strerror(res));
}
}
static void do_sysctl()
{
const sgxlkl_enclave_config_t* cfg = sgxlkl_enclave_state.config;
if (!cfg->sysctl)
return;
char* sysctl_all = strdup(cfg->sysctl);
char* sysctl = sysctl_all;
while (*sysctl)
{
while (*sysctl == ' ')
sysctl++;
char* path = sysctl;
char* val = strchrnul(path, '=');
if (!*val)
{
sgxlkl_warn(
"Failed to set sysctl config \"%s\", key and value not "
"seperated by '='.\n",
path);
break;
}
*val = '\0';
val++;
char* val_end = strchrnul(val, ';');
if (*val_end)
{
*val_end = '\0';
val_end++;
}
sysctl = val_end;