-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathimage.c
More file actions
1550 lines (1413 loc) · 58.4 KB
/
image.c
File metadata and controls
1550 lines (1413 loc) · 58.4 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
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
/* SPDX-License-Identifier: MIT */
/* Image mode lifecycle.
*
* Opens a rootfs disk image, registers it as an LKL block device, boots the
* kernel, mounts the filesystem, chroots in, applies recommended / bind
* mounts, sets identity, and then forks the seccomp-supervised child process.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/random.h>
#include <sys/resource.h>
#include <unistd.h>
#include "fd-table.h"
#include "kbox/compiler.h"
#include "kbox/elf.h"
#include "kbox/identity.h"
#include "kbox/image.h"
#include "kbox/mount.h"
#include "kbox/probe.h"
#include "lkl-wrap.h"
#include "loader-launch.h"
#include "net.h"
#include "rewrite.h"
#include "seccomp.h"
#include "shadow-fd.h"
#include "syscall-trap.h"
#ifdef KBOX_HAS_WEB
#include "web.h"
#endif
int kbox_rewrite_has_fork_sites_memfd(int fd,
const struct kbox_host_nrs *host_nrs);
/* Determine the root image path from the three mutually exclusive options.
* Returns the path, or NULL on error.
*/
static const char *select_root_path(const struct kbox_image_args *a)
{
if (a->system_root)
return a->root_dir;
if (a->recommended)
return a->root_dir;
if (a->root_dir)
return a->root_dir;
fprintf(stderr, "no rootfs image specified (-r, -R, or -S)\n");
return NULL;
}
/* Join mount_opts[] into a single comma-separated string.
* Writes into buf[bufsz]. Returns buf, or "" if no options.
*/
static const char *join_mount_opts(const struct kbox_image_args *a,
char *buf,
size_t bufsz)
{
size_t pos = 0;
int i;
buf[0] = '\0';
for (i = 0; i < a->mount_opt_count; i++) {
size_t len = strlen(a->mount_opts[i]);
if (pos + len + 2 > bufsz) {
fprintf(stderr, "kbox: mount options overflow (%zu bytes)\n",
bufsz);
return NULL;
}
if (pos > 0)
buf[pos++] = ',';
memcpy(buf + pos, a->mount_opts[i], len);
pos += len;
}
buf[pos] = '\0';
return buf;
}
extern char **environ;
/* AUTO fast-path selection.
*
* When KBOX_AUTO_FAST_PATH is set (via Kconfig SYSCALL_FAST_PATH=y), AUTO
* mode selects rewrite/trap for non-shell, non-networking binaries on
* both x86_64 and aarch64. The guest-thread local fast-path handles 50+
* CONTINUE syscalls without service-thread IPC, giving trap/rewrite mode
* a throughput advantage for workloads heavy on futex/brk/epoll/mmap.
*
* Users can override with --syscall-mode=rewrite or --syscall-mode=trap.
*/
#ifndef KBOX_AUTO_FAST_PATH
#define KBOX_AUTO_FAST_PATH 1
#endif
/* ASAN and the trap/rewrite path are fundamentally incompatible: the
* trap path switches to a guest stack that ASAN doesn't track, and the
* dispatch chain accesses ASAN-tracked memory. AUTO uses seccomp under
* ASAN; release builds get the full fast-path. This is the correct
* separation: ASAN tests correctness, release tests performance.
*/
#if KBOX_HAS_ASAN
#define KBOX_AUTO_ENABLE_USERSPACE_FAST_PATH 0
#else
#define KBOX_AUTO_ENABLE_USERSPACE_FAST_PATH KBOX_AUTO_FAST_PATH
#endif
static int is_shell_command(const char *command)
{
static const char *const shells[] = {
"sh", "bash", "ash", "zsh", "dash", "fish", "csh", "tcsh", "ksh", NULL,
};
const char *base = strrchr(command, '/');
base = base ? base + 1 : command;
for (const char *const *s = shells; *s; s++) {
if (strcmp(base, *s) == 0)
return 1;
}
return 0;
}
#if KBOX_AUTO_ENABLE_USERSPACE_FAST_PATH
/* Decide whether AUTO mode should prefer the userspace fast path (trap/rewrite)
* over the seccomp supervisor path for a given binary.
*
* Considers the combined syscall site count from both the main binary and its
* interpreter (if dynamic). The fast path is viable as long as ANY executable
* segment has rewritable sites -- trap mode catches everything via SIGSYS, and
* rewrite mode patches sites in both the main binary and the interpreter.
*
* Returns 1 to use trap/rewrite, 0 to fall back to seccomp.
*/
static int auto_prefers_userspace_fast_path(
const struct kbox_rewrite_report *exec_report,
const struct kbox_rewrite_report *interp_report,
int has_fork_sites)
{
if (!exec_report)
return 0;
/* Trap/rewrite mode duplicates the in-process LKL state on fork, so
* parent and child see independent filesystem state. AUTO selects the
* fast path for non-shell commands that do not contain fork/clone
* wrapper sites in the main executable. The interpreter (libc) is
* NOT scanned because it always contains fork wrappers regardless of
* whether the specific program uses them.
*
* Dynamic binaries whose main executable has no fork wrappers get
* the fast path. If the program does fork through libc, children
* inherit their own LKL copy and run independently. This is a known
* trade-off: cross-process filesystem coherence is not guaranteed in
* trap/rewrite mode.
*/
if (has_fork_sites)
return 0;
if (exec_report->candidate_count == 0 &&
(!interp_report || interp_report->candidate_count == 0))
return 0;
return 1;
}
#endif
static void maybe_apply_virtual_procinfo_fast_path(int fd,
const char *label,
int verbose)
{
struct kbox_rewrite_report report;
size_t applied = 0;
if (fd < 0)
return;
if (kbox_rewrite_analyze_memfd(fd, &report) < 0)
return;
if (report.arch != KBOX_REWRITE_ARCH_X86_64 &&
report.arch != KBOX_REWRITE_ARCH_AARCH64)
return;
if (kbox_rewrite_apply_virtual_procinfo_memfd(fd, &applied, &report) < 0)
return;
if (verbose && applied > 0) {
fprintf(stderr,
"kbox: seccomp procinfo fast path: %s: patched %zu wrapper%s\n",
label ? label : "memfd", applied, applied == 1 ? "" : "s");
}
}
static uint32_t memfd_wrapper_family_mask(int fd,
const struct kbox_host_nrs *host_nrs)
{
uint32_t mask = 0;
if (fd < 0 || !host_nrs)
return 0;
if (kbox_rewrite_wrapper_family_mask_memfd(fd, host_nrs, &mask) < 0)
return 0;
return mask;
}
static int wrapper_family_mask_has_stat(uint32_t mask)
{
return (mask & KBOX_REWRITE_WRAPPER_FAMILY_STAT) != 0;
}
static int wrapper_family_mask_has_open(uint32_t mask)
{
return (mask & KBOX_REWRITE_WRAPPER_FAMILY_OPEN) != 0;
}
static int memfd_has_stat_wrapper_fast_candidates(
int fd,
const struct kbox_host_nrs *host_nrs)
{
return wrapper_family_mask_has_stat(
memfd_wrapper_family_mask(fd, host_nrs));
}
static int memfd_has_open_wrapper_fast_candidates(
int fd,
const struct kbox_host_nrs *host_nrs)
{
return wrapper_family_mask_has_open(
memfd_wrapper_family_mask(fd, host_nrs));
}
struct wrapper_candidate_count_ctx {
size_t count;
int filter_enabled;
enum kbox_rewrite_wrapper_candidate_kind kind_filter;
};
static int count_wrapper_candidate_cb(
const struct kbox_rewrite_wrapper_candidate *candidate,
void *opaque)
{
struct wrapper_candidate_count_ctx *ctx = opaque;
if (!candidate || !ctx)
return -1;
if (ctx->filter_enabled && candidate->kind != ctx->kind_filter)
return 0;
ctx->count++;
return 0;
}
static size_t memfd_count_wrapper_family_candidates(
int fd,
const struct kbox_host_nrs *host_nrs,
uint32_t family_mask)
{
struct wrapper_candidate_count_ctx ctx;
memset(&ctx, 0, sizeof(ctx));
ctx.filter_enabled = 0;
if (fd < 0 || !host_nrs || family_mask == 0)
return 0;
if (kbox_rewrite_visit_memfd_wrapper_candidates(
fd, host_nrs, family_mask, count_wrapper_candidate_cb, &ctx) < 0) {
return 0;
}
return ctx.count;
}
static size_t memfd_count_wrapper_family_candidates_by_kind(
int fd,
const struct kbox_host_nrs *host_nrs,
uint32_t family_mask,
enum kbox_rewrite_wrapper_candidate_kind kind)
{
struct wrapper_candidate_count_ctx ctx;
memset(&ctx, 0, sizeof(ctx));
ctx.filter_enabled = 1;
ctx.kind_filter = kind;
if (fd < 0 || !host_nrs || family_mask == 0)
return 0;
if (kbox_rewrite_visit_memfd_wrapper_candidates(
fd, host_nrs, family_mask, count_wrapper_candidate_cb, &ctx) < 0) {
return 0;
}
return ctx.count;
}
static size_t memfd_count_phase1_path_candidates(
int fd,
const struct kbox_host_nrs *host_nrs)
{
struct kbox_rewrite_wrapper_candidate candidates[16];
size_t count = 0;
if (fd < 0 || !host_nrs)
return 0;
if (kbox_rewrite_collect_memfd_phase1_path_candidates(
fd, host_nrs, candidates,
sizeof(candidates) / sizeof(candidates[0]), &count) < 0) {
return 0;
}
return count;
}
struct wrapper_candidate_log_ctx {
const char *label;
const char *family_name;
const char *prefix;
};
static const char *wrapper_candidate_kind_name(
enum kbox_rewrite_wrapper_candidate_kind kind)
{
switch (kind) {
case KBOX_REWRITE_WRAPPER_CANDIDATE_DIRECT:
return "direct";
case KBOX_REWRITE_WRAPPER_CANDIDATE_SYSCALL_CANCEL:
return "cancel";
default:
return "unknown";
}
}
static int log_wrapper_candidate_cb(
const struct kbox_rewrite_wrapper_candidate *candidate,
void *opaque)
{
const struct wrapper_candidate_log_ctx *ctx = opaque;
if (!candidate || !ctx)
return -1;
fprintf(stderr,
"kbox: %s %s%s: off=0x%llx vaddr=0x%llx "
"nr=%llu kind=%s\n",
ctx->label ? ctx->label : "memfd",
ctx->family_name ? ctx->family_name : "path",
ctx->prefix ? ctx->prefix : "-wrapper candidate",
(unsigned long long) candidate->file_offset,
(unsigned long long) candidate->vaddr,
(unsigned long long) candidate->nr,
wrapper_candidate_kind_name(candidate->kind));
return 0;
}
static void maybe_log_wrapper_family_candidates(
const char *label,
int fd,
const struct kbox_host_nrs *host_nrs,
uint32_t family_mask,
const char *family_name,
int verbose)
{
struct kbox_rewrite_wrapper_candidate candidates[16];
struct wrapper_candidate_log_ctx ctx;
size_t count = 0;
if (!verbose || fd < 0 || !host_nrs || family_mask == 0)
return;
if (kbox_rewrite_collect_memfd_wrapper_candidates(
fd, host_nrs, family_mask, candidates,
sizeof(candidates) / sizeof(candidates[0]), &count) < 0) {
return;
}
ctx.label = label;
ctx.family_name = family_name;
ctx.prefix = "-wrapper candidate";
for (size_t i = 0;
i < count && i < (sizeof(candidates) / sizeof(candidates[0])); i++) {
(void) log_wrapper_candidate_cb(&candidates[i], &ctx);
}
}
static void maybe_log_phase1_path_candidates(
const char *label,
int fd,
const struct kbox_host_nrs *host_nrs,
int verbose)
{
struct kbox_rewrite_wrapper_candidate candidates[16];
struct wrapper_candidate_log_ctx ctx;
size_t count = 0;
if (!verbose || fd < 0 || !host_nrs)
return;
if (kbox_rewrite_collect_memfd_phase1_path_candidates(
fd, host_nrs, candidates,
sizeof(candidates) / sizeof(candidates[0]), &count) < 0) {
return;
}
ctx.label = label;
ctx.family_name = "phase1-path";
ctx.prefix = " target";
for (size_t i = 0;
i < count && i < (sizeof(candidates) / sizeof(candidates[0])); i++) {
(void) log_wrapper_candidate_cb(&candidates[i], &ctx);
}
}
static size_t count_envp(char *const *envp)
{
size_t n = 0;
if (!envp)
return 0;
while (envp[n])
n++;
return n;
}
static const char **build_loader_argv(const char *command,
const char *const *extra_args,
int extra_argc)
{
size_t argc = (size_t) extra_argc + 1;
const char **argv = calloc(argc + 1, sizeof(*argv));
if (!argv)
return NULL;
argv[0] = command;
for (int i = 0; i < extra_argc; i++)
argv[i + 1] = extra_args[i];
return argv;
}
static int prepare_userspace_launch(const struct kbox_image_args *args,
const char *command,
int exec_memfd,
int interp_memfd,
uid_t override_uid,
gid_t override_gid,
struct kbox_loader_launch *launch)
{
unsigned char launch_random[KBOX_LOADER_RANDOM_SIZE];
struct kbox_loader_launch_spec spec;
const char **argv = NULL;
size_t argc = (size_t) args->extra_argc + 1;
uint32_t uid = (uint32_t) (args->root_id || args->system_root
? 0
: (override_uid != (uid_t) -1 ? override_uid
: getuid()));
uint32_t gid = (uint32_t) (args->root_id || args->system_root
? 0
: (override_gid != (gid_t) -1 ? override_gid
: getgid()));
int rc;
if (!launch || exec_memfd < 0)
return -1;
/* Fill AT_RANDOM with real entropy for stack canary and libc PRNG seeding.
* Fall back to zeros only if getrandom is unavailable.
*/
memset(launch_random, 0, sizeof(launch_random));
{
ssize_t n = getrandom(launch_random, sizeof(launch_random), 0);
(void) n;
}
argv = build_loader_argv(command, args->extra_args, args->extra_argc);
if (!argv)
return -1;
memset(&spec, 0, sizeof(spec));
spec.exec_fd = exec_memfd;
spec.interp_fd = interp_memfd;
spec.argv = argv;
spec.argc = argc;
spec.envp = (const char *const *) environ;
spec.envc = count_envp(environ);
spec.execfn = command;
spec.random_bytes = launch_random;
spec.page_size = (uint64_t) sysconf(_SC_PAGESIZE);
spec.stack_top = 0x700000010000ULL;
spec.main_load_bias = 0x600000000000ULL;
spec.interp_load_bias = 0x610000000000ULL;
spec.uid = uid;
spec.euid = uid;
spec.gid = gid;
spec.egid = gid;
spec.secure = 0;
rc = kbox_loader_prepare_launch(&spec, launch);
free(argv);
return rc;
}
static const struct kbox_host_nrs *select_host_nrs(void)
{
#if defined(__x86_64__)
return &HOST_NRS_X86_64;
#elif defined(__aarch64__) || (defined(__riscv) && (__riscv_xlen == 64))
return &HOST_NRS_GENERIC;
#else
return NULL;
#endif
}
static int collect_trap_exec_ranges(const struct kbox_loader_launch *launch,
struct kbox_syscall_trap_ip_range *ranges,
size_t range_cap,
size_t *range_count)
{
struct kbox_loader_exec_range exec_ranges[KBOX_LOADER_MAX_MAPPINGS];
size_t exec_count = 0;
if (!launch || !ranges || !range_count)
return -1;
if (kbox_loader_collect_exec_ranges(
launch, exec_ranges, KBOX_LOADER_MAX_MAPPINGS, &exec_count) < 0) {
return -1;
}
if (exec_count > range_cap)
return -1;
for (size_t i = 0; i < exec_count; i++) {
ranges[i].start = (uintptr_t) exec_ranges[i].start;
ranges[i].end = (uintptr_t) exec_ranges[i].end;
}
*range_count = exec_count;
return 0;
}
static void drop_launch_caps(void)
{
prctl(47 /* PR_CAP_AMBIENT */, 4 /* PR_CAP_AMBIENT_CLEAR_ALL */, 0, 0, 0);
for (int cap = 0; cap <= 63; cap++)
prctl(24 /* PR_CAPBSET_DROP */, cap, 0, 0, 0);
}
static int set_launch_rlimits(void)
{
struct rlimit nofile = {65536, 65536};
struct rlimit rtprio = {0, 0};
struct rlimit current;
rlim_t required_nofile = (rlim_t) (KBOX_FD_BASE + KBOX_FD_TABLE_MAX);
if (setrlimit(RLIMIT_NOFILE, &nofile) != 0)
return -1;
if (getrlimit(RLIMIT_NOFILE, ¤t) != 0)
return -1;
if (current.rlim_cur < required_nofile) {
errno = EMFILE;
return -1;
}
if (setrlimit(RLIMIT_RTPRIO, &rtprio) != 0)
return -1;
return 0;
}
static void dump_loader_launch(const struct kbox_loader_launch *launch)
{
if (!launch)
return;
fprintf(stderr, "kbox: trap launch: pc=0x%llx sp=0x%llx mappings=%zu\n",
(unsigned long long) launch->transfer.pc,
(unsigned long long) launch->transfer.sp,
launch->layout.mapping_count);
for (size_t i = 0; i < launch->layout.mapping_count; i++) {
const struct kbox_loader_mapping *mapping = &launch->layout.mappings[i];
fprintf(stderr,
"kbox: trap launch: map[%zu] src=%d addr=0x%llx size=0x%llx "
"prot=%d flags=0x%x file_off=0x%llx file_size=0x%llx "
"zero_fill=0x%llx+0x%llx\n",
i, mapping->source, (unsigned long long) mapping->addr,
(unsigned long long) mapping->size, mapping->prot,
mapping->flags, (unsigned long long) mapping->file_offset,
(unsigned long long) mapping->file_size,
(unsigned long long) mapping->zero_fill_start,
(unsigned long long) mapping->zero_fill_size);
}
}
static void init_launch_ctx(struct kbox_supervisor_ctx *ctx,
struct kbox_fd_table *fd_table,
const struct kbox_image_args *args,
const struct kbox_sysnrs *sysnrs,
const struct kbox_host_nrs *host_nrs,
struct kbox_web_ctx *web_ctx)
{
kbox_fd_table_init(fd_table);
for (int i = 0; i < 3; i++)
kbox_fd_table_insert_at(fd_table, i, KBOX_LKL_FD_SHADOW_ONLY, 0);
memset(ctx, 0, sizeof(*ctx));
#if KBOX_STAT_CACHE_ENABLED
for (int ci = 0; ci < KBOX_STAT_CACHE_MAX; ci++)
ctx->stat_cache[ci].lkl_fd = -1;
#endif
ctx->sysnrs = sysnrs;
ctx->host_nrs = host_nrs;
ctx->fd_table = fd_table;
ctx->listener_fd = -1;
ctx->proc_self_fd_dirfd = -1;
ctx->proc_mem_fd = -1;
ctx->child_pid = getpid();
ctx->host_root = NULL;
ctx->verbose = args->verbose;
ctx->root_identity = args->root_id || args->system_root;
ctx->override_uid = (uid_t) -1;
ctx->override_gid = (gid_t) -1;
ctx->normalize = args->normalize;
ctx->guest_mem_ops = &kbox_current_guest_mem_ops;
ctx->active_guest_mem.ops = &kbox_current_guest_mem_ops;
ctx->active_guest_mem.opaque = 0;
ctx->fd_inject_ops = NULL;
ctx->web = web_ctx;
}
/* After the exec-range seccomp filter is installed, the success path must
* branch directly into guest code. ASAN/UBSAN runtimes and stack-protector
* epilogues may issue host syscalls from unregistered IPs, which the filter
* rejects with EPERM.
*/
__attribute__((no_stack_protector))
#if KBOX_HAS_ASAN
__attribute__((no_sanitize("address")))
#endif
__attribute__((no_sanitize("undefined"))) static int
install_exec_filter_and_transfer(
int (*install_filter)(const struct kbox_host_nrs *h,
const struct kbox_syscall_trap_ip_range *trap_ranges,
size_t trap_range_count),
const struct kbox_host_nrs *host_nrs,
const struct kbox_syscall_trap_ip_range *ranges,
size_t range_count,
const struct kbox_loader_transfer_state *transfer)
{
if (!install_filter || !host_nrs || !ranges || range_count == 0 ||
!transfer) {
errno = EINVAL;
return -1;
}
if (install_filter(host_nrs, ranges, range_count) < 0)
return -1;
kbox_loader_transfer_to_guest(transfer);
}
static int run_trap_launch(const struct kbox_image_args *args,
const struct kbox_sysnrs *sysnrs,
struct kbox_loader_launch *launch,
struct kbox_web_ctx *web_ctx)
{
struct kbox_syscall_trap_ip_range ranges[KBOX_LOADER_MAX_MAPPINGS];
const struct kbox_host_nrs *host_nrs = select_host_nrs();
struct kbox_fd_table fd_table;
struct kbox_supervisor_ctx ctx;
struct kbox_syscall_trap_runtime runtime;
size_t range_count = 0;
if (!host_nrs || !launch)
return -1;
#if defined(__x86_64__) && KBOX_HAS_ASAN
(void) sysnrs;
(void) web_ctx;
fprintf(stderr,
"kbox: trap mode is unsupported in x86_64 ASAN builds; "
"use --syscall-mode=seccomp or BUILD=release\n");
errno = ENOTSUP;
return -1;
#endif
if (collect_trap_exec_ranges(launch, ranges, KBOX_LOADER_MAX_MAPPINGS,
&range_count) < 0) {
fprintf(stderr,
"kbox: trap launch failed: cannot collect guest exec ranges\n");
return -1;
}
if (args->verbose)
dump_loader_launch(launch);
init_launch_ctx(&ctx, &fd_table, args, sysnrs, host_nrs, web_ctx);
runtime.sqpoll = args->sqpoll;
if (kbox_syscall_trap_runtime_install(&runtime, &ctx) < 0) {
fprintf(stderr,
"kbox: trap launch failed: cannot install SIGSYS handler\n");
return -1;
}
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
fprintf(stderr, "prctl(PR_SET_NO_NEW_PRIVS): %s\n", strerror(errno));
kbox_syscall_trap_runtime_uninstall(&runtime);
return -1;
}
drop_launch_caps();
if (set_launch_rlimits() < 0) {
fprintf(stderr, "kbox: trap launch failed: RLIMIT_NOFILE too low\n");
kbox_syscall_trap_runtime_uninstall(&runtime);
return -1;
}
/* Install the IP-gated trap filter for the guest's executable ranges.
* The userspace trap runtime handles the intercepted syscalls; kbox's
* own text stays outside the trapped ranges and executes natively.
*/
if (args->verbose) {
for (size_t ri = 0; ri < range_count; ri++)
fprintf(stderr, "kbox: trap exec range[%zu]: %p-%p\n", ri,
(void *) ranges[ri].start, (void *) ranges[ri].end);
}
if (install_exec_filter_and_transfer(kbox_install_seccomp_trap_ranges,
host_nrs, ranges, range_count,
&launch->transfer) < 0) {
fprintf(stderr,
"kbox: trap launch failed: cannot install guest trap filter\n");
kbox_syscall_trap_runtime_uninstall(&runtime);
return -1;
}
__builtin_unreachable();
}
static int run_rewrite_launch(const struct kbox_image_args *args,
const struct kbox_sysnrs *sysnrs,
struct kbox_loader_launch *launch,
struct kbox_web_ctx *web_ctx)
{
struct kbox_syscall_trap_ip_range ranges[KBOX_LOADER_MAX_MAPPINGS];
const struct kbox_host_nrs *host_nrs = select_host_nrs();
struct kbox_fd_table fd_table;
struct kbox_supervisor_ctx ctx;
struct kbox_syscall_trap_runtime trap_runtime;
struct kbox_rewrite_runtime rewrite_runtime;
size_t range_count;
if (!host_nrs || !launch)
return -1;
#if defined(__x86_64__)
#if KBOX_HAS_ASAN
fprintf(stderr,
"kbox: rewrite mode is unsupported in x86_64 ASAN builds; "
"use --syscall-mode=seccomp or BUILD=release\n");
errno = ENOTSUP;
return -1;
#endif
if (args->verbose) {
fprintf(
stderr,
"kbox: rewrite launch on x86_64 currently falls back to trap\n");
}
return run_trap_launch(args, sysnrs, launch, web_ctx);
#endif
if (collect_trap_exec_ranges(launch, ranges, KBOX_LOADER_MAX_MAPPINGS,
&range_count) < 0) {
fprintf(
stderr,
"kbox: rewrite launch failed: cannot collect guest exec ranges\n");
return -1;
}
if (args->verbose)
dump_loader_launch(launch);
init_launch_ctx(&ctx, &fd_table, args, sysnrs, host_nrs, web_ctx);
if (kbox_rewrite_runtime_install(&rewrite_runtime, &ctx, launch) < 0) {
fprintf(
stderr,
"kbox: rewrite launch failed: cannot install rewrite runtime: %s\n",
strerror(errno));
return -1;
}
trap_runtime.sqpoll = args->sqpoll;
if (kbox_syscall_trap_runtime_install(&trap_runtime, &ctx) < 0) {
fprintf(stderr,
"kbox: rewrite launch failed: cannot install SIGSYS handler\n");
kbox_rewrite_runtime_reset(&rewrite_runtime);
return -1;
}
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) {
fprintf(stderr, "prctl(PR_SET_NO_NEW_PRIVS): %s\n", strerror(errno));
kbox_syscall_trap_runtime_uninstall(&trap_runtime);
kbox_rewrite_runtime_reset(&rewrite_runtime);
return -1;
}
drop_launch_caps();
if (set_launch_rlimits() < 0) {
fprintf(stderr, "kbox: rewrite launch failed: RLIMIT_NOFILE too low\n");
kbox_syscall_trap_runtime_uninstall(&trap_runtime);
kbox_rewrite_runtime_reset(&rewrite_runtime);
return -1;
}
if (install_exec_filter_and_transfer(kbox_install_seccomp_rewrite_ranges,
host_nrs, ranges, range_count,
&launch->transfer) < 0) {
fprintf(
stderr,
"kbox: rewrite launch failed: cannot install guest trap filter\n");
kbox_syscall_trap_runtime_uninstall(&trap_runtime);
kbox_rewrite_runtime_reset(&rewrite_runtime);
return -1;
}
__builtin_unreachable();
}
/* Public entry point. */
int kbox_run_image(const struct kbox_image_args *args)
{
const char *root_path;
int image_fd;
struct lkl_disk disk;
int disk_id;
char mount_buf[256];
char opts_buf[1024];
const char *opts;
const char *fs_type;
const char *work_dir;
const char *command;
const struct kbox_sysnrs *sysnrs;
enum kbox_syscall_mode probe_mode;
long ret;
struct kbox_bind_spec bind_specs[KBOX_MAX_BIND_MOUNTS];
int bind_count = 0;
int i;
uid_t override_uid = (uid_t) -1;
gid_t override_gid = (gid_t) -1;
int rewrite_requested = 0;
/* Resolve parameters with defaults. */
root_path = select_root_path(args);
if (!root_path)
return -1;
fs_type = args->fs_type ? args->fs_type : "ext4";
work_dir = args->work_dir ? args->work_dir : "/";
command = args->command ? args->command : "/bin/sh";
probe_mode = args->syscall_mode;
rewrite_requested = args->syscall_mode == KBOX_SYSCALL_MODE_REWRITE;
/* AUTO enables rewrite analysis for non-shell commands so the
* auto_prefers_userspace_fast_path() selection function can see the
* exec_report and make an informed decision. Shell commands always
* fall back to seccomp (fork coherence), so skip the analysis.
*/
if (args->syscall_mode == KBOX_SYSCALL_MODE_AUTO) {
#if KBOX_AUTO_ENABLE_USERSPACE_FAST_PATH
if (!is_shell_command(command) && !args->net)
rewrite_requested = 1;
#else
/* On current x86_64 and aarch64 builds, AUTO is intentionally
* pinned to seccomp. Skip rewrite/trap analysis entirely and probe
* the supervisor path directly so startup does not do dead work or
* print duplicate probe messages.
*/
probe_mode = KBOX_SYSCALL_MODE_SECCOMP;
#endif
}
/* Parse bind mount specs. */
for (i = 0; i < args->bind_mount_count; i++) {
if (kbox_parse_bind_spec(args->bind_mounts[i], &bind_specs[i]) < 0)
return -1;
bind_count++;
}
/* Open image file. */
image_fd = open(root_path, O_RDWR);
if (image_fd < 0) {
fprintf(stderr, "open(%s): %s\n", root_path, strerror(errno));
return -1;
}
/* Register as LKL block device. */
memset(&disk, 0, sizeof(disk));
disk.dev = NULL;
disk.fd = image_fd;
disk.ops = &lkl_dev_blk_ops;
disk_id = lkl_disk_add(&disk);
if (disk_id < 0) {
fprintf(stderr, "lkl_disk_add: %s (%d)\n",
kbox_err_text((long) disk_id), disk_id);
return -1;
}
/* Register netdev before boot (LKL probes during boot). */
if (args->net) {
if (kbox_net_add_device() < 0)
return -1;
}
/* Boot the LKL kernel. */
if (kbox_boot_kernel(args->cmdline) < 0) {
if (args->net)
kbox_net_cleanup();
return -1;
}
/* Mount the filesystem. */
opts = join_mount_opts(args, opts_buf, sizeof(opts_buf));
if (!opts)
goto err_post_boot;
ret = lkl_mount_dev((unsigned) disk_id, args->part, fs_type, 0,
opts[0] ? opts : NULL, mount_buf, sizeof(mount_buf));
if (ret < 0) {
fprintf(stderr, "lkl_mount_dev: %s (%ld)\n", kbox_err_text(ret), ret);
goto err_post_boot;
}
/* Detect syscall ABI. */
sysnrs = detect_sysnrs();
if (!sysnrs) {
fprintf(stderr, "detect_sysnrs failed\n");
goto err_post_boot;
}
/* Chroot into mountpoint. */
ret = kbox_lkl_chroot(sysnrs, mount_buf);
if (ret < 0) {
fprintf(stderr, "chroot(%s): %s\n", mount_buf, kbox_err_text(ret));
goto err_post_boot;
}
/* Recommended mounts. */
if (args->recommended || args->system_root) {
if (kbox_apply_recommended_mounts(sysnrs, args->mount_profile) < 0)
goto err_post_boot;
}
/* Bind mounts. */
if (bind_count > 0) {
if (kbox_apply_bind_mounts(sysnrs, bind_specs, bind_count) < 0)
goto err_post_boot;
}
/* Working directory. */
ret = kbox_lkl_chdir(sysnrs, work_dir);
if (ret < 0) {
fprintf(stderr, "chdir(%s): %s\n", work_dir, kbox_err_text(ret));
goto err_post_boot;
}
/* Identity. */
if (args->change_id) {
if (kbox_parse_change_id(args->change_id, &override_uid,
&override_gid) < 0)
goto err_post_boot;
}
{
int root_id = args->root_id || args->system_root;
if (kbox_apply_guest_identity(sysnrs, root_id, override_uid,
override_gid) < 0)
goto err_post_boot;
}
/* Probe host features. Rewrite mode skips seccomp-specific probes. */
if (kbox_probe_host_features(probe_mode) < 0)
goto err_post_boot;
/* Networking: configure interface (optional). */
if (args->net) {
if (kbox_net_configure(sysnrs) < 0) {
kbox_net_cleanup();
goto err_post_boot;
}
}
/* Web observatory (optional). */
struct kbox_web_ctx *web_ctx = NULL;
#ifdef KBOX_HAS_WEB
if (args->web || args->trace_format) {
struct kbox_web_config wcfg;
memset(&wcfg, 0, sizeof(wcfg));
wcfg.enable_web = args->web;
wcfg.port = args->web_port;
wcfg.bind = args->web_bind;
wcfg.guest_name = command;
if (args->trace_format) {
wcfg.enable_trace = 1;
wcfg.trace_fd = STDERR_FILENO;
}
web_ctx = kbox_web_init(&wcfg, sysnrs);
if (!web_ctx) {
fprintf(stderr, "warning: failed to initialize web observatory\n");
/* Non-fatal: continue without telemetry */
}
}
#endif
/* Extract binary from LKL into memfd.
*
* The child process will exec via fexecve(memfd), because the binary lives
* inside the LKL-mounted filesystem and does not exist on the host.
*
* For dynamically-linked binaries, ELF contains a PT_INTERP segment naming
* the interpreter (e.g. /lib/ld-musl-x86_64.so.1). The host kernel resolves
* PT_INTERP from the host VFS, not the LKL image, so the interpreter cannot
* be found. Fix: extract the interpreter into a second memfd and patch
* PT_INTERP in the main binary to /proc/self/fd/<interp_fd>. The kernel
* opens /proc/self/fd/N during load_elf_binary (before close-on-exec), so
* both memfds can keep MFD_CLOEXEC.
*/