-
Notifications
You must be signed in to change notification settings - Fork 318
/
container.c
3029 lines (2548 loc) · 88.5 KB
/
container.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
/*
* crun - OCI runtime written in C
*
* Copyright (C) 2017, 2018, 2019, 2020 Giuseppe Scrivano <[email protected]>
* crun is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* crun is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with crun. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <config.h>
#include <runtime_spec_schema_config_schema.h>
#include <stdbool.h>
#include "container.h"
#include "utils.h"
#include "seccomp.h"
#include "seccomp_notify.h"
#include <stdbool.h>
#include <argp.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sched.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
#include "status.h"
#include "linux.h"
#include "terminal.h"
#include "cgroup.h"
#include <sys/prctl.h>
#include <sys/signalfd.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <grp.h>
#ifdef HAVE_DLOPEN
# include <dlfcn.h>
#endif
#ifdef HAVE_LIBKRUN
# include <libkrun.h>
#endif
#ifdef HAVE_SYSTEMD
# include <systemd/sd-daemon.h>
#endif
#include <yajl/yajl_tree.h>
#include <yajl/yajl_gen.h>
#define YAJL_STR(x) ((const unsigned char *) (x))
enum
{
SYNC_SOCKET_SYNC_MESSAGE,
SYNC_SOCKET_ERROR_MESSAGE,
SYNC_SOCKET_WARNING_MESSAGE,
};
struct container_entrypoint_s
{
libcrun_container_t *container;
libcrun_context_t *context;
int has_terminal_socket_pair;
int terminal_socketpair[2];
/* Used by log_write_to_sync_socket. */
int sync_socket;
int seccomp_fd;
int seccomp_receiver_fd;
int console_socket_fd;
int hooks_out_fd;
int hooks_err_fd;
/* If specified, it is called instead of
execve. */
int (*exec_func) (void *container, void *arg, const char *pathname, char *const argv[]);
void *exec_func_arg;
};
struct sync_socket_message_s
{
int type;
int error_value;
char message[512];
};
typedef runtime_spec_schema_defs_hook hook;
static char spec_file[] = "\
{\n\
\"ociVersion\": \"1.0.0\",\n\
\"process\": {\n\
\"terminal\": true,\n\
\"user\": {\n\
\"uid\": 0,\n\
\"gid\": 0\n\
},\n\
\"args\": [\n\
\"sh\"\n\
],\n\
\"env\": [\n\
\"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\",\n\
\"TERM=xterm\"\n\
],\n\
\"cwd\": \"/\",\n\
\"capabilities\": {\n\
\"bounding\": [\n\
\"CAP_AUDIT_WRITE\",\n\
\"CAP_KILL\",\n\
\"CAP_NET_BIND_SERVICE\"\n\
],\n\
\"effective\": [\n\
\"CAP_AUDIT_WRITE\",\n\
\"CAP_KILL\",\n\
\"CAP_NET_BIND_SERVICE\"\n\
],\n\
\"inheritable\": [\n\
\"CAP_AUDIT_WRITE\",\n\
\"CAP_KILL\",\n\
\"CAP_NET_BIND_SERVICE\"\n\
],\n\
\"permitted\": [\n\
\"CAP_AUDIT_WRITE\",\n\
\"CAP_KILL\",\n\
\"CAP_NET_BIND_SERVICE\"\n\
],\n\
\"ambient\": [\n\
\"CAP_AUDIT_WRITE\",\n\
\"CAP_KILL\",\n\
\"CAP_NET_BIND_SERVICE\"\n\
]\n\
},\n\
\"rlimits\": [\n\
{\n\
\"type\": \"RLIMIT_NOFILE\",\n\
\"hard\": 1024,\n\
\"soft\": 1024\n\
}\n\
],\n\
\"noNewPrivileges\": true\n\
},\n\
\"root\": {\n\
\"path\": \"rootfs\",\n\
\"readonly\": true\n\
},\n\
\"hostname\": \"crun\",\n\
\"mounts\": [\n\
{\n\
\"destination\": \"/proc\",\n\
\"type\": \"proc\",\n\
\"source\": \"proc\"\n\
},\n\
{\n\
\"destination\": \"/dev\",\n\
\"type\": \"tmpfs\",\n\
\"source\": \"tmpfs\",\n\
\"options\": [\n\
\"nosuid\",\n\
\"strictatime\",\n\
\"mode=755\",\n\
\"size=65536k\"\n\
]\n\
},\n\
{\n\
\"destination\": \"/dev/pts\",\n\
\"type\": \"devpts\",\n\
\"source\": \"devpts\",\n\
\"options\": [\n\
\"nosuid\",\n\
\"noexec\",\n\
\"newinstance\",\n\
\"ptmxmode=0666\",\n\
\"mode=0620\"\
%s\
]\n\
},\n\
{\n\
\"destination\": \"/dev/shm\",\n\
\"type\": \"tmpfs\",\n\
\"source\": \"shm\",\n\
\"options\": [\n\
\"nosuid\",\n\
\"noexec\",\n\
\"nodev\",\n\
\"mode=1777\",\n\
\"size=65536k\"\n\
]\n\
},\n\
{\n\
\"destination\": \"/dev/mqueue\",\n\
\"type\": \"mqueue\",\n\
\"source\": \"mqueue\",\n\
\"options\": [\n\
\"nosuid\",\n\
\"noexec\",\n\
\"nodev\"\n\
]\n\
},\n\
{\n\
\"destination\": \"/sys\",\n\
\"type\": \"sysfs\",\n\
\"source\": \"sysfs\",\n\
\"options\": [\n\
\"nosuid\",\n\
\"noexec\",\n\
\"nodev\",\n\
\"ro\"\n\
]\n\
},\n\
{\n\
\"destination\": \"/sys/fs/cgroup\",\n\
\"type\": \"cgroup\",\n\
\"source\": \"cgroup\",\n\
\"options\": [\n\
\"nosuid\",\n\
\"noexec\",\n\
\"nodev\",\n\
\"relatime\",\n\
\"ro\"\n\
]\n\
}\n\
],\n\
\"linux\": {\n\
\"resources\": {\n\
},\n\
\"namespaces\": [\n\
{\n\
\"type\": \"pid\"\n\
},\n\
{\n\
\"type\": \"network\"\n\
},\n\
{\n\
\"type\": \"ipc\"\n\
},\n\
{\n\
\"type\": \"uts\"\n\
},\n\
%s\
{\n\
\"type\": \"mount\"\n\
}\n\
],\n\
\"maskedPaths\": [\n\
\"/proc/acpi\",\n\
\"/proc/asound\",\n\
\"/proc/kcore\",\n\
\"/proc/keys\",\n\
\"/proc/latency_stats\",\n\
\"/proc/timer_list\",\n\
\"/proc/timer_stats\",\n\
\"/proc/sched_debug\",\n\
\"/sys/firmware\",\n\
\"/proc/scsi\"\n\
],\n\
\"readonlyPaths\": [\n\
\"/proc/bus\",\n\
\"/proc/fs\",\n\
\"/proc/irq\",\n\
\"/proc/sys\",\n\
\"/proc/sysrq-trigger\"\n\
]\n\
}\n\
}\n";
static const char *spec_pts_tty_group = ",\n\
\"gid=5\"\n";
static const char *spec_user = "\
{\n\
\"type\": \"user\"\n \
},\n";
#define SYNC_SOCKET_MESSAGE_LEN(x, l) (offsetof (struct sync_socket_message_s, message) + l)
static int
sync_socket_write_msg (int fd, bool warning, int err_value, const char *log_msg)
{
int ret;
size_t err_len;
struct sync_socket_message_s msg;
msg.type = warning ? SYNC_SOCKET_WARNING_MESSAGE : SYNC_SOCKET_ERROR_MESSAGE;
msg.error_value = err_value;
if (fd < 0)
return 0;
err_len = strlen (log_msg);
if (err_len >= sizeof (msg.message))
err_len = sizeof (msg.message) - 1;
memcpy (msg.message, log_msg, err_len);
msg.message[err_len] = '\0';
ret = TEMP_FAILURE_RETRY (write (fd, &msg, SYNC_SOCKET_MESSAGE_LEN (msg, err_len + 1)));
if (UNLIKELY (ret < 0))
return -1;
return 0;
}
static int
sync_socket_write_error (int fd, libcrun_error_t *out_err)
{
if (fd < 0)
return 0;
return sync_socket_write_msg (fd, false, (*out_err)->status, (*out_err)->msg);
}
static void
log_write_to_sync_socket (int errno_, const char *msg, bool warning, void *arg)
{
struct container_entrypoint_s *entrypoint_args = arg;
int fd = entrypoint_args->sync_socket;
if (fd < 0)
return;
if (sync_socket_write_msg (fd, warning, errno_, msg) < 0)
log_write_to_stderr (errno_, msg, warning, arg);
}
static int
sync_socket_wait_sync (libcrun_context_t *context, int fd, bool flush, libcrun_error_t *err)
{
struct sync_socket_message_s msg;
if (fd < 0)
return 0;
while (true)
{
int ret;
ret = TEMP_FAILURE_RETRY (read (fd, &msg, sizeof (msg)));
if (UNLIKELY (ret < 0))
{
if (flush)
return 0;
return crun_make_error (err, errno, "read from sync socket");
}
if (ret == 0)
{
if (flush)
return 0;
return crun_make_error (err, 0, "sync socket closed");
}
if (! flush && msg.type == SYNC_SOCKET_SYNC_MESSAGE)
return 0;
if (msg.type == SYNC_SOCKET_WARNING_MESSAGE)
{
if (context)
context->output_handler (msg.error_value, msg.message, 1, context->output_handler_arg);
continue;
}
if (msg.type == SYNC_SOCKET_ERROR_MESSAGE)
return crun_make_error (err, msg.error_value, "%s", msg.message);
}
}
static int
sync_socket_send_sync (int fd, bool flush_errors, libcrun_error_t *err)
{
int ret;
struct sync_socket_message_s msg = {
0,
};
msg.type = SYNC_SOCKET_SYNC_MESSAGE;
if (fd < 0)
return 0;
ret = TEMP_FAILURE_RETRY (write (fd, &msg, SYNC_SOCKET_MESSAGE_LEN (msg, 0)));
if (UNLIKELY (ret < 0))
{
if (flush_errors)
{
int saved_errno = errno;
ret = TEMP_FAILURE_RETRY (read (fd, &msg, sizeof (msg)));
if (ret >= 0 && msg.type == SYNC_SOCKET_ERROR_MESSAGE)
return crun_make_error (err, msg.error_value, "%s", msg.message);
errno = saved_errno;
}
return crun_make_error (err, errno, "write to sync socket");
}
return 0;
}
static libcrun_container_t *
make_container (runtime_spec_schema_config_schema *container_def)
{
libcrun_container_t *container = xmalloc0 (sizeof (*container));
container->container_def = container_def;
container->host_uid = geteuid ();
container->host_gid = getegid ();
return container;
}
libcrun_container_t *
libcrun_container_load_from_memory (const char *json, libcrun_error_t *err)
{
runtime_spec_schema_config_schema *container_def;
cleanup_free char *oci_error = NULL;
container_def = runtime_spec_schema_config_schema_parse_data (json, NULL, &oci_error);
if (container_def == NULL)
{
crun_make_error (err, 0, "load: %s", oci_error);
return NULL;
}
return make_container (container_def);
}
libcrun_container_t *
libcrun_container_load_from_file (const char *path, libcrun_error_t *err)
{
runtime_spec_schema_config_schema *container_def;
cleanup_free char *oci_error = NULL;
container_def = runtime_spec_schema_config_schema_parse_file (path, NULL, &oci_error);
if (container_def == NULL)
{
crun_make_error (err, 0, "load `%s`: %s", path, oci_error);
return NULL;
}
return make_container (container_def);
}
static int
block_signals (libcrun_error_t *err)
{
int ret;
sigset_t mask;
sigfillset (&mask);
ret = sigprocmask (SIG_BLOCK, &mask, NULL);
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "sigprocmask");
return 0;
}
static int
unblock_signals (libcrun_error_t *err)
{
int i;
int ret;
sigset_t mask;
struct sigaction act = {};
sigfillset (&mask);
ret = sigprocmask (SIG_UNBLOCK, &mask, NULL);
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "sigprocmask");
act.sa_handler = SIG_DFL;
for (i = 0; i < NSIG; i++)
{
ret = sigaction (i, &act, NULL);
if (ret < 0 && errno != EINVAL)
return crun_make_error (err, errno, "sigaction");
}
return 0;
}
/* must be used on the host before pivot_root(2). */
static int
initialize_security (runtime_spec_schema_config_schema_process *proc, libcrun_error_t *err)
{
int ret;
if (UNLIKELY (proc == NULL))
return 0;
if (proc->apparmor_profile)
{
ret = libcrun_initialize_apparmor (err);
if (UNLIKELY (ret < 0))
return ret;
}
ret = libcrun_initialize_selinux (err);
if (UNLIKELY (ret < 0))
return ret;
ret = libcrun_init_caps (err);
if (UNLIKELY (ret < 0))
return ret;
return 0;
}
static int
do_hooks (runtime_spec_schema_config_schema *def, pid_t pid, const char *id, bool keep_going, const char *cwd,
const char *status, hook **hooks, size_t hooks_len, int out_fd, int err_fd, libcrun_error_t *err)
{
size_t i, stdin_len;
int ret;
char *stdin = NULL;
cleanup_free char *cwd_allocated = NULL;
const char *rootfs = def->root ? def->root->path : "";
yajl_gen gen = NULL;
if (cwd == NULL)
{
cwd = cwd_allocated = get_current_dir_name ();
if (cwd == NULL)
OOM ();
}
gen = yajl_gen_alloc (NULL);
if (gen == NULL)
return crun_make_error (err, 0, "yajl_gen_alloc failed");
yajl_gen_map_open (gen);
yajl_gen_string (gen, YAJL_STR ("ociVersion"), strlen ("ociVersion"));
yajl_gen_string (gen, YAJL_STR ("1.0"), strlen ("1.0"));
yajl_gen_string (gen, YAJL_STR ("id"), strlen ("id"));
yajl_gen_string (gen, YAJL_STR (id), strlen (id));
yajl_gen_string (gen, YAJL_STR ("pid"), strlen ("pid"));
yajl_gen_integer (gen, pid);
yajl_gen_string (gen, YAJL_STR ("root"), strlen ("root"));
yajl_gen_string (gen, YAJL_STR (rootfs), strlen (rootfs));
yajl_gen_string (gen, YAJL_STR ("bundle"), strlen ("bundle"));
yajl_gen_string (gen, YAJL_STR (cwd), strlen (cwd));
yajl_gen_string (gen, YAJL_STR ("status"), strlen ("status"));
yajl_gen_string (gen, YAJL_STR (status), strlen (status));
if (def && def->annotations && def->annotations->len)
{
yajl_gen_string (gen, YAJL_STR ("annotations"), strlen ("annotations"));
yajl_gen_map_open (gen);
for (i = 0; i < def->annotations->len; i++)
{
const char *key = def->annotations->keys[i];
const char *val = def->annotations->values[i];
yajl_gen_string (gen, YAJL_STR (key), strlen (key));
yajl_gen_string (gen, YAJL_STR (val), strlen (val));
}
yajl_gen_map_close (gen);
}
yajl_gen_map_close (gen);
yajl_gen_get_buf (gen, (const unsigned char **) &stdin, &stdin_len);
ret = 0;
for (i = 0; i < hooks_len; i++)
{
ret = run_process_with_stdin_timeout_envp (hooks[i]->path, hooks[i]->args, cwd, hooks[i]->timeout, hooks[i]->env,
stdin, stdin_len, out_fd, err_fd, err);
if (UNLIKELY (ret != 0))
{
if (keep_going)
libcrun_warning ("error executing hook `%s` (exit code: %d)", hooks[i]->path, ret);
else
{
libcrun_error (0, "error executing hook `%s` (exit code: %d)", hooks[i]->path, ret);
break;
}
}
}
if (gen)
yajl_gen_free (gen);
return ret;
}
#if HAVE_DLOPEN && HAVE_LIBKRUN
static int
libkrun_do_exec (void *container, void *arg, const char *pathname, char *const argv[])
{
runtime_spec_schema_config_schema *def = ((libcrun_container_t *) container)->container_def;
int32_t (*krun_create_ctx) ();
int (*krun_start_enter) (uint32_t ctx_id);
int32_t (*krun_set_vm_config) (uint32_t ctx_id, uint8_t num_vcpus, uint32_t ram_mib);
int32_t (*krun_set_root) (uint32_t ctx_id, const char *root_path);
int32_t (*krun_set_exec) (uint32_t ctx_id, const char *exec_path, char *const argv[], char *const envp[]);
void *handle = arg;
uint32_t num_vcpus, ram_mib;
int32_t ctx_id, ret;
cpu_set_t set;
krun_create_ctx = dlsym (handle, "krun_create_ctx");
krun_start_enter = dlsym (handle, "krun_start_enter");
krun_set_vm_config = dlsym (handle, "krun_set_vm_config");
krun_set_root = dlsym (handle, "krun_set_root");
krun_set_exec = dlsym (handle, "krun_set_exec");
if (krun_create_ctx == NULL || krun_start_enter == NULL || krun_set_vm_config == NULL || krun_set_root == NULL
|| krun_set_exec == NULL)
{
fprintf (stderr, "could not find symbol in `libkrun.so`");
dlclose (handle);
return -1;
}
/* If sched_getaffinity fails, default to 1 vcpu. */
num_vcpus = 1;
/* If no memory limit is specified, default to 2G. */
ram_mib = 2 * 1024;
if (def && def->linux && def->linux->resources && def->linux->resources->memory
&& def->linux->resources->memory->limit_present)
ram_mib = def->linux->resources->memory->limit / (1024 * 1024);
CPU_ZERO (&set);
if (sched_getaffinity (getpid (), sizeof (set), &set) == 0)
num_vcpus = CPU_COUNT (&set);
ctx_id = krun_create_ctx ();
if (UNLIKELY (ctx_id < 0))
error (EXIT_FAILURE, -ret, "could not create krun context");
ret = krun_set_vm_config (ctx_id, num_vcpus, ram_mib);
if (UNLIKELY (ret < 0))
error (EXIT_FAILURE, -ret, "could not set krun vm configuration");
ret = krun_set_root (ctx_id, "/");
if (UNLIKELY (ret < 0))
error (EXIT_FAILURE, -ret, "could not set krun root");
ret = krun_set_exec (ctx_id, pathname, &argv[1], NULL);
if (UNLIKELY (ret < 0))
error (EXIT_FAILURE, -ret, "could not set krun executable");
return krun_start_enter (ctx_id);
}
#endif
static int
libcrun_configure_libkrun (struct container_entrypoint_s *args, libcrun_error_t *err)
{
#if HAVE_DLOPEN && HAVE_LIBKRUN
void *handle;
#endif
#if HAVE_DLOPEN && HAVE_LIBKRUN
handle = dlopen ("libkrun.so", RTLD_NOW);
if (handle == NULL)
return crun_make_error (err, 0, "could not load `libkrun.so`: %s", dlerror ());
args->exec_func = libkrun_do_exec;
args->exec_func_arg = handle;
return 0;
#else
(void) args;
return crun_make_error (err, ENOTSUP, "libkrun or dlopen not present");
#endif
}
static int
libcrun_configure_handler (struct container_entrypoint_s *args, libcrun_error_t *err)
{
const char *annotation;
annotation = find_annotation (args->container, "run.oci.handler");
/*Nothing to do. */
if (annotation == NULL)
return 0;
if (strcmp (annotation, "krun") == 0)
return libcrun_configure_libkrun (args, err);
return crun_make_error (err, EINVAL, "invalid handler specified `%s`", annotation);
}
/* Initialize the environment where the container process runs.
It is used by the container init process. */
static int
container_init_setup (void *args, char *notify_socket, int sync_socket, const char **exec_path, libcrun_error_t *err)
{
struct container_entrypoint_s *entrypoint_args = args;
libcrun_container_t *container = entrypoint_args->container;
int ret;
int has_terminal;
cleanup_close int console_socket = -1;
cleanup_close int console_socketpair = -1;
runtime_spec_schema_config_schema *def = container->container_def;
runtime_spec_schema_config_schema_process_capabilities *capabilities;
cleanup_free char *rootfs = NULL;
int no_new_privs;
ret = libcrun_configure_handler (args, err);
if (UNLIKELY (ret < 0))
return ret;
ret = initialize_security (def->process, err);
if (UNLIKELY (ret < 0))
return ret;
ret = libcrun_configure_network (container, err);
if (UNLIKELY (ret < 0))
return ret;
if (def->root && def->root->path)
{
rootfs = realpath (def->root->path, NULL);
if (UNLIKELY (rootfs == NULL))
{
/* If realpath failed for any reason, try the relative directory. */
rootfs = xstrdup (def->root->path);
}
}
if (entrypoint_args->terminal_socketpair[0] >= 0)
{
close_and_reset (&entrypoint_args->terminal_socketpair[0]);
console_socketpair = entrypoint_args->terminal_socketpair[1];
}
/* sync 1. */
ret = sync_socket_wait_sync (NULL, sync_socket, false, err);
if (UNLIKELY (ret < 0))
return ret;
has_terminal = container->container_def->process && container->container_def->process->terminal;
if (has_terminal && entrypoint_args->context->console_socket)
console_socket = entrypoint_args->console_socket_fd;
ret = libcrun_set_sysctl (container, err);
if (UNLIKELY (ret < 0))
return ret;
ret = libcrun_set_mounts (container, rootfs, err);
if (UNLIKELY (ret < 0))
return ret;
/* sync 2. */
ret = sync_socket_send_sync (sync_socket, false, err);
if (UNLIKELY (ret < 0))
return ret;
/* sync 3. */
ret = sync_socket_wait_sync (NULL, sync_socket, false, err);
if (UNLIKELY (ret < 0))
return ret;
if (def->hooks && def->hooks->create_container_len)
{
ret = do_hooks (def, 0, container->context->id, false, NULL, "created", (hook **) def->hooks->create_container,
def->hooks->create_container_len, entrypoint_args->hooks_out_fd, entrypoint_args->hooks_err_fd,
err);
if (UNLIKELY (ret != 0))
return ret;
}
if (def->process)
{
ret = libcrun_set_selinux_exec_label (def->process, err);
if (UNLIKELY (ret < 0))
return ret;
ret = libcrun_set_apparmor_profile (def->process, err);
if (UNLIKELY (ret < 0))
return ret;
}
ret = close_fds_ge_than (entrypoint_args->context->preserve_fds + 3, err);
if (UNLIKELY (ret < 0))
crun_error_write_warning_and_release (entrypoint_args->context->output_handler_arg, &err);
if (rootfs)
{
ret = libcrun_do_pivot_root (container, entrypoint_args->context->no_pivot, rootfs, err);
if (UNLIKELY (ret < 0))
return ret;
}
ret = libcrun_reopen_dev_null (err);
if (UNLIKELY (ret < 0))
return ret;
if (clearenv ())
return crun_make_error (err, errno, "clearenv");
if (def->process)
{
size_t i;
for (i = 0; i < def->process->env_len; i++)
if (putenv (def->process->env[i]) < 0)
return crun_make_error (err, errno, "putenv `%s`", def->process->env[i]);
}
if (getenv ("HOME") == NULL)
{
ret = set_home_env (container->container_uid);
if (UNLIKELY (ret < 0 && errno != ENOTSUP))
{
setenv ("HOME", "/", 1);
libcrun_warning ("cannot detect HOME environment variable, setting default");
}
}
if (def->process && def->process->cwd)
if (UNLIKELY (chdir (def->process->cwd) < 0))
return crun_make_error (err, errno, "chdir");
if (def->process && def->process->args)
{
*exec_path = find_executable (def->process->args[0], def->process->cwd);
if (UNLIKELY (*exec_path == NULL))
{
if (errno == ENOENT)
return crun_make_error (err, errno, "executable file `%s` not found in $PATH", def->process->args[0]);
return crun_make_error (err, errno, "open executable");
}
}
ret = setsid ();
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "setsid");
if (has_terminal)
{
cleanup_close int terminal_fd = -1;
fflush (stderr);
terminal_fd = libcrun_set_terminal (container, err);
if (UNLIKELY (terminal_fd < 0))
return terminal_fd;
if (console_socket >= 0)
{
ret = send_fd_to_socket (console_socket, terminal_fd, err);
if (UNLIKELY (ret < 0))
return ret;
close_and_reset (&console_socket);
}
else if (entrypoint_args->has_terminal_socket_pair && console_socketpair >= 0)
{
ret = send_fd_to_socket (console_socketpair, terminal_fd, err);
if (UNLIKELY (ret < 0))
return ret;
close_and_reset (&console_socketpair);
}
}
ret = libcrun_set_hostname (container, err);
if (UNLIKELY (ret < 0))
return ret;
if (container->container_def->linux && container->container_def->linux->personality)
{
ret = libcrun_set_personality (container->container_def->linux->personality, err);
if (UNLIKELY (ret < 0))
return ret;
}
if (def->process->user)
umask (def->process->user->umask_present ? def->process->user->umask : 0022);
if (def->process && ! def->process->no_new_privileges)
{
char **seccomp_flags = NULL;
size_t seccomp_flags_len = 0;
if (def->linux && def->linux->seccomp)
{
seccomp_flags = def->linux->seccomp->flags;
seccomp_flags_len = def->linux->seccomp->flags_len;
}
ret = libcrun_apply_seccomp (entrypoint_args->seccomp_fd, entrypoint_args->seccomp_receiver_fd, seccomp_flags,
seccomp_flags_len, err);
if (UNLIKELY (ret < 0))
return ret;
close_and_reset (&entrypoint_args->seccomp_fd);
close_and_reset (&entrypoint_args->seccomp_receiver_fd);
}
capabilities = def->process ? def->process->capabilities : NULL;
no_new_privs = def->process ? def->process->no_new_privileges : 1;
ret = libcrun_set_caps (capabilities, container->container_uid, container->container_gid, no_new_privs, err);
if (UNLIKELY (ret < 0))
return ret;
if (notify_socket)
{
if (putenv (notify_socket) < 0)
return crun_make_error (err, errno, "putenv `%s`", notify_socket);
}
return 0;
}
static int
open_hooks_output (libcrun_container_t *container, int *out_fd, int *err_fd, libcrun_error_t *err)
{
const char *annotation;
*err_fd = *out_fd = -1;
annotation = find_annotation (container, "run.oci.hooks.stdout");
if (annotation)
{
*out_fd = TEMP_FAILURE_RETRY (open (annotation, O_CREAT | O_WRONLY | O_APPEND, 0700));
if (UNLIKELY (*out_fd < 0))
return crun_make_error (err, errno, "open `%s`", annotation);
}
annotation = find_annotation (container, "run.oci.hooks.stderr");
if (annotation)
{
*err_fd = TEMP_FAILURE_RETRY (open (annotation, O_CREAT | O_WRONLY | O_APPEND, 0700));
if (UNLIKELY (*err_fd < 0))
return crun_make_error (err, errno, "open `%s`", annotation);
}
return 0;
}
/* Entrypoint to the container. */
static int
container_init (void *args, char *notify_socket, int sync_socket, libcrun_error_t *err)
{
struct container_entrypoint_s *entrypoint_args = args;
int ret;
runtime_spec_schema_config_schema *def = entrypoint_args->container->container_def;
cleanup_free const char *exec_path = NULL;
cleanup_free char *notify_socket_cleanup = notify_socket;
entrypoint_args->sync_socket = sync_socket;
crun_set_output_handler (log_write_to_sync_socket, args, false);
ret = container_init_setup (args, notify_socket, sync_socket, &exec_path, err);
if (UNLIKELY (ret < 0))
{
/* If it fails to write the error using the sync socket, then fallback
to stderr. */
if (sync_socket_write_error (sync_socket, err) < 0)
return ret;
crun_error_release (err);
return ret;
}
entrypoint_args->sync_socket = -1;
ret = unblock_signals (err);
if (UNLIKELY (ret < 0))
return ret;
/* sync 4. */
ret = sync_socket_send_sync (sync_socket, false, err);
if (UNLIKELY (ret < 0))
return ret;
close_and_reset (&sync_socket);
if (entrypoint_args->context->fifo_exec_wait_fd >= 0)
{
char buffer[1];
fd_set read_set;
cleanup_close int fd = entrypoint_args->context->fifo_exec_wait_fd;
entrypoint_args->context->fifo_exec_wait_fd = -1;
FD_ZERO (&read_set);
FD_SET (fd, &read_set);
do
{
ret = select (fd + 1, &read_set, NULL, NULL, NULL);
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "select");
ret = TEMP_FAILURE_RETRY (read (fd, buffer, sizeof (buffer)));
if (UNLIKELY (ret < 0))
return crun_make_error (err, errno, "read from the exec fifo");
}
while (ret == 0);