/
conf.c
5800 lines (4775 loc) · 160 KB
/
conf.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
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "config.h"
#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <inttypes.h>
#include <libgen.h>
#include <linux/loop.h>
#include <net/if.h>
#include <netinet/in.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/prctl.h>
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "conf.h"
#include "af_unix.h"
#include "caps.h"
#include "cgroups/cgroup.h"
#include "compiler.h"
#include "confile.h"
#include "confile_utils.h"
#include "error.h"
#include "log.h"
#include "lsm/lsm.h"
#include "lxclock.h"
#include "lxcseccomp.h"
#include "macro.h"
#include "memory_utils.h"
#include "mount_utils.h"
#include "namespace.h"
#include "network.h"
#include "open_utils.h"
#include "parse.h"
#include "process_utils.h"
#include "ringbuf.h"
#include "start.h"
#include "storage/storage.h"
#include "storage/overlay.h"
#include "sync.h"
#include "syscall_wrappers.h"
#include "terminal.h"
#include "utils.h"
#include "uuid.h"
#ifdef MAJOR_IN_MKDEV
#include <sys/mkdev.h>
#endif
#ifdef HAVE_STATVFS
#include <sys/statvfs.h>
#endif
#if HAVE_OPENPTY
#include <pty.h>
#else
#include "openpty.h"
#endif
#if HAVE_LIBCAP
#include <sys/capability.h>
#endif
#if !HAVE_STRLCAT
#include "strlcat.h"
#endif
#if IS_BIONIC
#include "lxcmntent.h"
#else
#include <mntent.h>
#endif
#if !HAVE_PRLIMIT && HAVE_PRLIMIT64
#include "prlimit.h"
#endif
#if !HAVE_STRLCPY
#include "strlcpy.h"
#endif
#if !HAVE_STRCHRNUL
#include "strchrnul.h"
#endif
lxc_log_define(conf, lxc);
/*
* The lxc_conf of the container currently being worked on in an API call.
* This is used in the error calls.
*/
thread_local struct lxc_conf *current_config;
char *lxchook_names[NUM_LXC_HOOKS] = {
"pre-start",
"pre-mount",
"mount",
"autodev",
"start",
"stop",
"post-stop",
"clone",
"destroy",
"start-host"
};
struct mount_opt {
char *name;
int clear;
bool recursive;
__u64 flag;
int legacy_flag;
};
struct caps_opt {
char *name;
__u32 value;
};
struct limit_opt {
char *name;
int value;
};
static struct mount_opt mount_opt[] = {
{ "atime", 1, false, MOUNT_ATTR_NOATIME, MS_NOATIME },
{ "dev", 1, false, MOUNT_ATTR_NODEV, MS_NODEV },
{ "diratime", 1, false, MOUNT_ATTR_NODIRATIME, MS_NODIRATIME },
{ "exec", 1, false, MOUNT_ATTR_NOEXEC, MS_NOEXEC },
{ "noatime", 0, false, MOUNT_ATTR_NOATIME, MS_NOATIME },
{ "nodev", 0, false, MOUNT_ATTR_NODEV, MS_NODEV },
{ "nodiratime", 0, false, MOUNT_ATTR_NODIRATIME, MS_NODIRATIME },
{ "noexec", 0, false, MOUNT_ATTR_NOEXEC, MS_NOEXEC },
{ "norelatime", 1, false, MOUNT_ATTR_RELATIME, MS_RELATIME },
{ "nostrictatime", 1, false, MOUNT_ATTR_STRICTATIME, MS_STRICTATIME },
{ "nosuid", 0, false, MOUNT_ATTR_NOSUID, MS_NOSUID },
{ "relatime", 0, false, MOUNT_ATTR_RELATIME, MS_RELATIME },
{ "ro", 0, false, MOUNT_ATTR_RDONLY, MS_RDONLY },
{ "rw", 1, false, MOUNT_ATTR_RDONLY, MS_RDONLY },
{ "strictatime", 0, false, MOUNT_ATTR_STRICTATIME, MS_STRICTATIME },
{ "suid", 1, false, MOUNT_ATTR_NOSUID, MS_NOSUID },
{ "bind", 0, false, 0, MS_BIND },
{ "defaults", 0, false, 0, 0 },
{ "rbind", 0, true, 0, MS_BIND | MS_REC },
{ "sync", 0, false, ~0, MS_SYNCHRONOUS },
{ "async", 1, false, ~0, MS_SYNCHRONOUS },
{ "dirsync", 0, false, ~0, MS_DIRSYNC },
{ "lazytime", 0, false, ~0, MS_LAZYTIME },
{ "mand", 0, false, ~0, MS_MANDLOCK },
{ "nomand", 1, false, ~0, MS_MANDLOCK },
{ "remount", 0, false, ~0, MS_REMOUNT },
{ NULL, 0, false, ~0, ~0 },
};
static struct mount_opt propagation_opt[] = {
{ "private", 0, false, MS_PRIVATE, MS_PRIVATE },
{ "shared", 0, false, MS_SHARED, MS_SHARED },
{ "slave", 0, false, MS_SLAVE, MS_SLAVE },
{ "unbindable", 0, false, MS_UNBINDABLE, MS_UNBINDABLE },
{ "rprivate", 0, true, MS_PRIVATE, MS_PRIVATE | MS_REC },
{ "rshared", 0, true, MS_SHARED, MS_SHARED | MS_REC },
{ "rslave", 0, true, MS_SLAVE, MS_SLAVE | MS_REC },
{ "runbindable", 0, true, MS_UNBINDABLE, MS_UNBINDABLE | MS_REC },
{ NULL, 0, false, 0, 0 },
};
static struct caps_opt caps_opt[] = {
#if HAVE_LIBCAP
{ "chown", CAP_CHOWN },
{ "dac_override", CAP_DAC_OVERRIDE },
{ "dac_read_search", CAP_DAC_READ_SEARCH },
{ "fowner", CAP_FOWNER },
{ "fsetid", CAP_FSETID },
{ "kill", CAP_KILL },
{ "setgid", CAP_SETGID },
{ "setuid", CAP_SETUID },
{ "setpcap", CAP_SETPCAP },
{ "linux_immutable", CAP_LINUX_IMMUTABLE },
{ "net_bind_service", CAP_NET_BIND_SERVICE },
{ "net_broadcast", CAP_NET_BROADCAST },
{ "net_admin", CAP_NET_ADMIN },
{ "net_raw", CAP_NET_RAW },
{ "ipc_lock", CAP_IPC_LOCK },
{ "ipc_owner", CAP_IPC_OWNER },
{ "sys_module", CAP_SYS_MODULE },
{ "sys_rawio", CAP_SYS_RAWIO },
{ "sys_chroot", CAP_SYS_CHROOT },
{ "sys_ptrace", CAP_SYS_PTRACE },
{ "sys_pacct", CAP_SYS_PACCT },
{ "sys_admin", CAP_SYS_ADMIN },
{ "sys_boot", CAP_SYS_BOOT },
{ "sys_nice", CAP_SYS_NICE },
{ "sys_resource", CAP_SYS_RESOURCE },
{ "sys_time", CAP_SYS_TIME },
{ "sys_tty_config", CAP_SYS_TTY_CONFIG },
{ "mknod", CAP_MKNOD },
{ "lease", CAP_LEASE },
{ "audit_write", CAP_AUDIT_WRITE },
{ "audit_control", CAP_AUDIT_CONTROL },
{ "setfcap", CAP_SETFCAP },
{ "mac_override", CAP_MAC_OVERRIDE },
{ "mac_admin", CAP_MAC_ADMIN },
{ "syslog", CAP_SYSLOG },
{ "wake_alarm", CAP_WAKE_ALARM },
{ "block_suspend", CAP_BLOCK_SUSPEND },
{ "audit_read", CAP_AUDIT_READ },
{ "perfmon", CAP_PERFMON },
{ "bpf", CAP_BPF },
{ "checkpoint_restore", CAP_CHECKPOINT_RESTORE },
#endif
};
static struct limit_opt limit_opt[] = {
#ifdef RLIMIT_AS
{ "as", RLIMIT_AS },
#endif
#ifdef RLIMIT_CORE
{ "core", RLIMIT_CORE },
#endif
#ifdef RLIMIT_CPU
{ "cpu", RLIMIT_CPU },
#endif
#ifdef RLIMIT_DATA
{ "data", RLIMIT_DATA },
#endif
#ifdef RLIMIT_FSIZE
{ "fsize", RLIMIT_FSIZE },
#endif
#ifdef RLIMIT_LOCKS
{ "locks", RLIMIT_LOCKS },
#endif
#ifdef RLIMIT_MEMLOCK
{ "memlock", RLIMIT_MEMLOCK },
#endif
#ifdef RLIMIT_MSGQUEUE
{ "msgqueue", RLIMIT_MSGQUEUE },
#endif
#ifdef RLIMIT_NICE
{ "nice", RLIMIT_NICE },
#endif
#ifdef RLIMIT_NOFILE
{ "nofile", RLIMIT_NOFILE },
#endif
#ifdef RLIMIT_NPROC
{ "nproc", RLIMIT_NPROC },
#endif
#ifdef RLIMIT_RSS
{ "rss", RLIMIT_RSS },
#endif
#ifdef RLIMIT_RTPRIO
{ "rtprio", RLIMIT_RTPRIO },
#endif
#ifdef RLIMIT_RTTIME
{ "rttime", RLIMIT_RTTIME },
#endif
#ifdef RLIMIT_SIGPENDING
{ "sigpending", RLIMIT_SIGPENDING },
#endif
#ifdef RLIMIT_STACK
{ "stack", RLIMIT_STACK },
#endif
};
static int run_buffer(char *buffer)
{
__do_free char *output = NULL;
__do_lxc_pclose struct lxc_popen_FILE *f = NULL;
int fd, ret;
f = lxc_popen(buffer);
if (!f)
return log_error_errno(-1, errno, "Failed to popen() %s", buffer);
output = zalloc(LXC_LOG_BUFFER_SIZE);
if (!output)
return log_error_errno(-1, ENOMEM, "Failed to allocate memory for %s", buffer);
fd = fileno(f->f);
if (fd < 0)
return log_error_errno(-1, errno, "Failed to retrieve underlying file descriptor");
for (int i = 0; i < 10; i++) {
ssize_t bytes_read;
bytes_read = lxc_read_nointr(fd, output, LXC_LOG_BUFFER_SIZE - 1);
if (bytes_read > 0) {
output[bytes_read] = '\0';
DEBUG("Script %s produced output: %s", buffer, output);
continue;
}
break;
}
ret = lxc_pclose(move_ptr(f));
if (ret == -1)
return log_error_errno(-1, errno, "Script exited with error");
else if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0)
return log_error(-1, "Script exited with status %d", WEXITSTATUS(ret));
else if (WIFSIGNALED(ret))
return log_error(-1, "Script terminated by signal %d", WTERMSIG(ret));
return 0;
}
int run_script_argv(const char *name, unsigned int hook_version,
const char *section, const char *script,
const char *hookname, char **argv)
{
__do_free char *buffer = NULL;
int buf_pos, i, ret;
size_t size = 0;
if (hook_version == 0)
INFO("Executing script \"%s\" for container \"%s\", config section \"%s\"",
script, name, section);
else
INFO("Executing script \"%s\" for container \"%s\"", script, name);
for (i = 0; argv && argv[i]; i++)
size += strlen(argv[i]) + 1;
size += STRLITERALLEN("exec");
size++;
size += strlen(script);
size++;
if (size > INT_MAX)
return -EFBIG;
if (hook_version == 0) {
size += strlen(hookname);
size++;
size += strlen(name);
size++;
size += strlen(section);
size++;
if (size > INT_MAX)
return -EFBIG;
}
buffer = zalloc(size);
if (!buffer)
return -ENOMEM;
if (hook_version == 0)
buf_pos = strnprintf(buffer, size, "exec %s %s %s %s", script, name, section, hookname);
else
buf_pos = strnprintf(buffer, size, "exec %s", script);
if (buf_pos < 0)
return log_error_errno(-1, errno, "Failed to create command line for script \"%s\"", script);
if (hook_version == 1) {
ret = setenv("LXC_HOOK_TYPE", hookname, 1);
if (ret < 0) {
return log_error_errno(-1, errno, "Failed to set environment variable: LXC_HOOK_TYPE=%s", hookname);
}
TRACE("Set environment variable: LXC_HOOK_TYPE=%s", hookname);
ret = setenv("LXC_HOOK_SECTION", section, 1);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to set environment variable: LXC_HOOK_SECTION=%s", section);
TRACE("Set environment variable: LXC_HOOK_SECTION=%s", section);
if (strequal(section, "net")) {
char *parent;
if (!argv || !argv[0])
return -1;
ret = setenv("LXC_NET_TYPE", argv[0], 1);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_TYPE=%s", argv[0]);
TRACE("Set environment variable: LXC_NET_TYPE=%s", argv[0]);
parent = argv[1] ? argv[1] : "";
if (strequal(argv[0], "macvlan")) {
ret = setenv("LXC_NET_PARENT", parent, 1);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_PARENT=%s", parent);
TRACE("Set environment variable: LXC_NET_PARENT=%s", parent);
} else if (strequal(argv[0], "phys")) {
ret = setenv("LXC_NET_PARENT", parent, 1);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_PARENT=%s", parent);
TRACE("Set environment variable: LXC_NET_PARENT=%s", parent);
} else if (strequal(argv[0], "veth")) {
char *peer = argv[2] ? argv[2] : "";
ret = setenv("LXC_NET_PEER", peer, 1);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_PEER=%s", peer);
TRACE("Set environment variable: LXC_NET_PEER=%s", peer);
ret = setenv("LXC_NET_PARENT", parent, 1);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to set environment variable: LXC_NET_PARENT=%s", parent);
TRACE("Set environment variable: LXC_NET_PARENT=%s", parent);
}
}
}
for (i = 0; argv && argv[i]; i++) {
size_t len = size - buf_pos;
ret = strnprintf(buffer + buf_pos, len, " %s", argv[i]);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to create command line for script \"%s\"", script);
buf_pos += ret;
}
return run_buffer(buffer);
}
int run_script(const char *name, const char *section, const char *script, ...)
{
__do_free char *buffer = NULL;
int ret;
char *p;
va_list ap;
size_t size = 0;
INFO("Executing script \"%s\" for container \"%s\", config section \"%s\"",
script, name, section);
va_start(ap, script);
while ((p = va_arg(ap, char *)))
size += strlen(p) + 1;
va_end(ap);
size += STRLITERALLEN("exec");
size += strlen(script);
size += strlen(name);
size += strlen(section);
size += 4;
if (size > INT_MAX)
return -1;
buffer = must_realloc(NULL, size);
ret = strnprintf(buffer, size, "exec %s %s %s", script, name, section);
if (ret < 0)
return -1;
va_start(ap, script);
while ((p = va_arg(ap, char *))) {
int len = size - ret;
int rc;
rc = strnprintf(buffer + ret, len, " %s", p);
if (rc < 0) {
va_end(ap);
return -1;
}
ret += rc;
}
va_end(ap);
return run_buffer(buffer);
}
int lxc_storage_prepare(struct lxc_conf *conf)
{
int ret;
struct lxc_rootfs *rootfs = &conf->rootfs;
if (!rootfs->path) {
ret = mount("", "/", NULL, MS_SLAVE | MS_REC, 0);
if (ret < 0)
return log_error_errno(-1, errno, "Failed to recursively turn root mount tree into dependent mount");
rootfs->dfd_mnt = open_at(-EBADF, "/", PROTECT_OPATH_DIRECTORY, PROTECT_LOOKUP_ABSOLUTE, 0);
if (rootfs->dfd_mnt < 0)
return -errno;
return 0;
}
ret = access(rootfs->mount, F_OK);
if (ret != 0)
return log_error_errno(-1, errno, "Failed to access to \"%s\". Check it is present",
rootfs->mount);
rootfs->storage = storage_init(conf);
if (!rootfs->storage)
return log_error(-1, "Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\"",
rootfs->path, rootfs->mount,
rootfs->mnt_opts.raw_options ? rootfs->mnt_opts.raw_options : "(null)");
return 0;
}
void lxc_storage_put(struct lxc_conf *conf)
{
storage_put(conf->rootfs.storage);
conf->rootfs.storage = NULL;
}
/* lxc_rootfs_prepare
* if rootfs is a directory, then open ${rootfs}/.lxc-keep for writing for
* the duration of the container run, to prevent the container from marking
* the underlying fs readonly on shutdown. unlink the file immediately so
* no name pollution is happens.
* don't unlink on NFS to avoid random named stale handles.
*/
int lxc_rootfs_init(struct lxc_conf *conf, bool userns)
{
__do_close int dfd_path = -EBADF, fd_pin = -EBADF;
int ret;
struct stat st;
struct statfs stfs;
struct lxc_rootfs *rootfs = &conf->rootfs;
ret = lxc_storage_prepare(conf);
if (ret)
return syserror_set(-EINVAL, "Failed to prepare rootfs storage");
if (!is_empty_string(rootfs->mnt_opts.userns_path)) {
if (!rootfs->path)
return syserror_set(-EINVAL, "Idmapped rootfs currently only supported with separate rootfs for container");
if (rootfs->bdev_type && !strequal(rootfs->bdev_type, "dir"))
return syserror_set(-EINVAL, "Idmapped rootfs currently only supports the \"dir\" storage driver");
}
if (!rootfs->path)
return log_trace(0, "Not pinning because container does not have a rootfs");
if (userns)
return log_trace(0, "Not pinning because container runs in user namespace");
if (rootfs->bdev_type) {
if (strequal(rootfs->bdev_type, "overlay") ||
strequal(rootfs->bdev_type, "overlayfs"))
return log_trace_errno(0, EINVAL, "Not pinning on stacking filesystem");
if (strequal(rootfs->bdev_type, "zfs"))
return log_trace_errno(0, EINVAL, "Not pinning on ZFS filesystem");
}
dfd_path = open_at(-EBADF, rootfs->path, PROTECT_OPATH_FILE, 0, 0);
if (dfd_path < 0)
return syserror("Failed to open \"%s\"", rootfs->path);
ret = fstat(dfd_path, &st);
if (ret < 0)
return log_trace_errno(-errno, errno, "Failed to retrieve file status");
if (!S_ISDIR(st.st_mode))
return log_trace_errno(0, ENOTDIR, "Not pinning because file descriptor is not a directory");
fd_pin = open_at(dfd_path, ".lxc_keep",
PROTECT_OPEN | O_CREAT,
PROTECT_LOOKUP_BENEATH,
S_IWUSR | S_IRUSR);
if (fd_pin < 0) {
if (errno == EROFS)
return log_trace_errno(0, EROFS, "Not pinning on read-only filesystem");
return syserror("Failed to pin rootfs");
}
TRACE("Pinned rootfs %d(.lxc_keep)", fd_pin);
ret = fstatfs(fd_pin, &stfs);
if (ret < 0) {
SYSWARN("Failed to retrieve filesystem status");
goto out;
}
if (stfs.f_type == NFS_SUPER_MAGIC) {
DEBUG("Not unlinking pinned file on NFS");
goto out;
}
if (unlinkat(dfd_path, ".lxc_keep", 0))
SYSTRACE("Failed to unlink rootfs pinning file %d(.lxc_keep)", dfd_path);
else
TRACE("Unlinked pinned file %d(.lxc_keep)", dfd_path);
out:
rootfs->fd_path_pin = move_fd(fd_pin);
return 0;
}
int lxc_rootfs_prepare_parent(struct lxc_handler *handler)
{
__do_close int dfd_idmapped = -EBADF, fd_userns = -EBADF;
struct lxc_rootfs *rootfs = &handler->conf->rootfs;
struct lxc_storage *storage = rootfs->storage;
const struct lxc_mount_options *mnt_opts = &rootfs->mnt_opts;
int ret;
const char *path_source;
if (list_empty(&handler->conf->id_map))
return 0;
if (is_empty_string(rootfs->mnt_opts.userns_path))
return 0;
if (handler->conf->rootfs_setup)
return 0;
if (rootfs_is_blockdev(handler->conf))
return syserror_set(-EOPNOTSUPP, "Idmapped mounts on block-backed storage not yet supported");
if (!can_use_bind_mounts())
return syserror_set(-EOPNOTSUPP, "Kernel does not support the new mount api");
if (strequal(rootfs->mnt_opts.userns_path, "container"))
fd_userns = dup_cloexec(handler->nsfd[LXC_NS_USER]);
else
fd_userns = open_at(-EBADF, rootfs->mnt_opts.userns_path,
PROTECT_OPEN_WITH_TRAILING_SYMLINKS, 0, 0);
if (fd_userns < 0)
return syserror("Failed to open user namespace");
path_source = lxc_storage_get_path(storage->src, storage->type);
dfd_idmapped = create_detached_idmapped_mount(path_source, fd_userns, true,
mnt_opts->attr.attr_set,
mnt_opts->attr.attr_clr);
if (dfd_idmapped < 0)
return syserror("Failed to create detached idmapped mount");
ret = lxc_abstract_unix_send_fds(handler->data_sock[0], &dfd_idmapped, 1, NULL, 0);
if (ret < 0)
return syserror("Failed to send detached idmapped mount fd");
TRACE("Created detached idmapped mount %d", dfd_idmapped);
return 0;
}
static int add_shmount_to_list(struct lxc_conf *conf)
{
char new_mount[PATH_MAX];
/* Offset for the leading '/' since the path_cont
* is absolute inside the container.
*/
int offset = 1, ret = -1;
ret = strnprintf(new_mount, sizeof(new_mount),
"%s %s none bind,create=dir 0 0", conf->shmount.path_host,
conf->shmount.path_cont + offset);
if (ret < 0)
return -1;
return add_elem_to_mount_list(new_mount, conf);
}
static int lxc_mount_auto_mounts(struct lxc_handler *handler, int flags)
{
int i, ret;
static struct {
int match_mask;
int match_flag;
const char *source;
const char *destination;
const char *fstype;
unsigned long flags;
const char *options;
bool requires_cap_net_admin;
} default_mounts[] = {
/* Read-only bind-mounting... In older kernels, doing that
* required to do one MS_BIND mount and then
* MS_REMOUNT|MS_RDONLY the same one. According to mount(2)
* manpage, MS_BIND honors MS_RDONLY from kernel 2.6.26
* onwards. However, this apparently does not work on kernel
* 3.8. Unfortunately, on that very same kernel, doing the same
* trick as above doesn't seem to work either, there one needs
* to ALSO specify MS_BIND for the remount, otherwise the
* entire fs is remounted read-only or the mount fails because
* it's busy... MS_REMOUNT|MS_BIND|MS_RDONLY seems to work for
* kernels as low as 2.6.32...
*/
{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "proc", "%r/proc", "proc", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL, false },
/* proc/tty is used as a temporary placeholder for proc/sys/net which we'll move back in a few steps */
{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys/net", "%r/proc/tty", NULL, MS_BIND, NULL, true, },
{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys", "%r/proc/sys", NULL, MS_BIND, NULL, false },
{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL, "%r/proc/sys", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL, false },
{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/tty", "%r/proc/sys/net", NULL, MS_MOVE, NULL, true },
{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sysrq-trigger", "%r/proc/sysrq-trigger", NULL, MS_BIND, NULL, false },
{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL, "%r/proc/sysrq-trigger", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL, false },
{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW, "proc", "%r/proc", "proc", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL, false },
{ LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW, "sysfs", "%r/sys", "sysfs", 0, NULL, false },
{ LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO, "sysfs", "%r/sys", "sysfs", MS_RDONLY, NULL, false },
/* /proc/sys is used as a temporary staging directory for the read-write sysfs mount and unmounted after binding net */
{ LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "sysfs", "%r/proc/sys", "sysfs", MS_NOSUID|MS_NODEV|MS_NOEXEC, NULL, false },
{ LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "sysfs", "%r/sys", "sysfs", MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC, NULL, false },
{ LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "%r/proc/sys/devices/virtual/net", "%r/sys/devices/virtual/net", NULL, MS_BIND, NULL, false },
{ LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "%r/proc/sys", NULL, NULL, 0, NULL, false },
{ 0, 0, NULL, NULL, NULL, 0, NULL, false }
};
struct lxc_conf *conf = handler->conf;
struct lxc_rootfs *rootfs = &conf->rootfs;
bool has_cap_net_admin;
if (flags & LXC_AUTO_PROC_MASK) {
if (rootfs->path) {
/*
* Only unmount procfs if we have a separate rootfs so
* we can still access it in safe_mount() below.
*/
ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/proc",
rootfs->path ? rootfs->mount : "");
if (ret < 0)
return ret_errno(EIO);
ret = umount2(rootfs->buf, MNT_DETACH);
if (ret)
SYSDEBUG("Tried to ensure procfs is unmounted");
}
ret = mkdirat(rootfs->dfd_mnt, "proc" , S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
if (ret < 0 && errno != EEXIST)
return syserror("Failed to create procfs mountpoint under %d", rootfs->dfd_mnt);
TRACE("Created procfs mountpoint under %d", rootfs->dfd_mnt);
}
if (flags & LXC_AUTO_SYS_MASK) {
if (rootfs->path) {
/*
* Only unmount sysfs if we have a separate rootfs so
* we can still access it in safe_mount() below.
*/
ret = strnprintf(rootfs->buf, sizeof(rootfs->buf), "%s/sys",
rootfs->path ? rootfs->mount : "");
if (ret < 0)
return ret_errno(EIO);
ret = umount2(rootfs->buf, MNT_DETACH);
if (ret)
SYSDEBUG("Tried to ensure sysfs is unmounted");
}
ret = mkdirat(rootfs->dfd_mnt, "sys" , S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
if (ret < 0 && errno != EEXIST)
return syserror("Failed to create sysfs mountpoint under %d", rootfs->dfd_mnt);
TRACE("Created sysfs mountpoint under %d", rootfs->dfd_mnt);
}
has_cap_net_admin = lxc_wants_cap(CAP_NET_ADMIN, conf);
for (i = 0; default_mounts[i].match_mask; i++) {
__do_free char *destination = NULL, *source = NULL;
unsigned long mflags = default_mounts[i].flags;
if ((flags & default_mounts[i].match_mask) != default_mounts[i].match_flag)
continue;
if (default_mounts[i].source) {
/* will act like strdup if %r is not present */
source = lxc_string_replace("%r", rootfs->path ? rootfs->mount : "", default_mounts[i].source);
if (!source)
return syserror_set(-ENOMEM, "Failed to create source path");
}
if (!has_cap_net_admin && default_mounts[i].requires_cap_net_admin) {
TRACE("Container does not have CAP_NET_ADMIN. Skipping \"%s\" mount", default_mounts[i].source ?: "(null)");
continue;
}
if (!default_mounts[i].destination) {
ret = umount2(source, MNT_DETACH);
if (ret < 0)
return log_error_errno(-1, errno,
"Failed to unmount \"%s\"",
source);
TRACE("Unmounted automount \"%s\"", source);
continue;
}
/* will act like strdup if %r is not present */
destination = lxc_string_replace("%r", rootfs->path ? rootfs->mount : "", default_mounts[i].destination);
if (!destination)
return syserror_set(-ENOMEM, "Failed to create target path");
ret = safe_mount(source, destination,
default_mounts[i].fstype,
mflags,
default_mounts[i].options,
rootfs->path ? rootfs->mount : NULL);
if (ret < 0) {
if (errno != ENOENT)
return syserror("Failed to mount \"%s\" on \"%s\" with flags %lu", source, destination, mflags);
INFO("Mount source or target for \"%s\" on \"%s\" does not exist. Skipping", source, destination);
continue;
}
if (mflags & MS_REMOUNT)
TRACE("Remounted automount \"%s\" on \"%s\" %s with flags %lu", source, destination, (mflags & MS_RDONLY) ? "read-only" : "read-write", mflags);
else
TRACE("Mounted automount \"%s\" on \"%s\" %s with flags %lu", source, destination, (mflags & MS_RDONLY) ? "read-only" : "read-write", mflags);
}
if (flags & LXC_AUTO_CGROUP_MASK) {
int cg_flags;
cg_flags = flags & (LXC_AUTO_CGROUP_MASK & ~LXC_AUTO_CGROUP_FORCE);
/* If the type of cgroup mount was not specified, it depends on
* the container's capabilities as to what makes sense: if we
* have CAP_SYS_ADMIN, the read-only part can be remounted
* read-write anyway, so we may as well default to read-write;
* then the admin will not be given a false sense of security.
* (And if they really want mixed r/o r/w, then they can
* explicitly specify :mixed.) OTOH, if the container lacks
* CAP_SYS_ADMIN, do only default to :mixed, because then the
* container can't remount it read-write.
*/
if ((cg_flags == LXC_AUTO_CGROUP_NOSPEC) || (cg_flags == LXC_AUTO_CGROUP_FULL_NOSPEC)) {
if (cg_flags == LXC_AUTO_CGROUP_NOSPEC)
cg_flags = has_cap(CAP_SYS_ADMIN, conf)
? LXC_AUTO_CGROUP_RW
: LXC_AUTO_CGROUP_MIXED;
else
cg_flags = has_cap(CAP_SYS_ADMIN, conf)
? LXC_AUTO_CGROUP_FULL_RW
: LXC_AUTO_CGROUP_FULL_MIXED;
}
if (flags & LXC_AUTO_CGROUP_FORCE)
cg_flags |= LXC_AUTO_CGROUP_FORCE;
if (!handler->cgroup_ops->mount(handler->cgroup_ops, handler, cg_flags))
return log_error_errno(-1, errno, "Failed to mount \"/sys/fs/cgroup\"");
}
if (flags & LXC_AUTO_SHMOUNTS_MASK) {
ret = add_shmount_to_list(conf);
if (ret < 0)
return log_error(-1, "Failed to add shmount entry to container config");
}
return 0;
}
static int setup_utsname(struct utsname *utsname)
{
int ret;
if (!utsname)
return 0;
ret = sethostname(utsname->nodename, strlen(utsname->nodename));
if (ret < 0)
return log_error_errno(-1, errno, "Failed to set the hostname to \"%s\"",
utsname->nodename);
INFO("Set hostname to \"%s\"", utsname->nodename);
return 0;
}
struct dev_symlinks {
const char *oldpath;
const char *name;
};
static const struct dev_symlinks dev_symlinks[] = {
{ "/proc/self/fd", "fd" },
{ "/proc/self/fd/0", "stdin" },
{ "/proc/self/fd/1", "stdout" },
{ "/proc/self/fd/2", "stderr" },
};
static int lxc_setup_dev_symlinks(const struct lxc_rootfs *rootfs)
{
for (size_t i = 0; i < sizeof(dev_symlinks) / sizeof(dev_symlinks[0]); i++) {
int ret;
struct stat s;
const struct dev_symlinks *d = &dev_symlinks[i];
/*
* Stat the path first. If we don't get an error accept it as
* is and don't try to create it
*/
ret = fstatat(rootfs->dfd_dev, d->name, &s, 0);
if (ret == 0)
continue;
ret = symlinkat(d->oldpath, rootfs->dfd_dev, d->name);
if (ret) {
switch (errno) {
case EROFS:
WARN("Failed to create \"%s\" on read-only filesystem", d->name);
__fallthrough;
case EEXIST:
break;
default:
return log_error_errno(-errno, errno, "Failed to create \"%s\"", d->name);
}
}
}
return 0;
}
/* Build a space-separate list of ptys to pass to systemd. */
static bool append_ttyname(struct lxc_tty_info *ttys, char *tty_name)
{
char *tty_names, *buf;
size_t size;
if (!tty_name)
return false;
size = strlen(tty_name) + 1;
if (ttys->tty_names)
size += strlen(ttys->tty_names) + 1;
buf = realloc(ttys->tty_names, size);
if (!buf)
return false;
tty_names = buf;
if (ttys->tty_names)
(void)strlcat(buf, " ", size);
else
buf[0] = '\0';
(void)strlcat(buf, tty_name, size);
ttys->tty_names = tty_names;
return true;
}
static int open_ttymnt_at(int dfd, const char *path)
{
int fd;
fd = open_at(dfd, path,
PROTECT_OPEN | O_CREAT | O_EXCL,
PROTECT_LOOKUP_BENEATH,
0);
if (fd < 0) {
if (errno != ENXIO && errno != EEXIST)
return syserror("Failed to create \"%d/\%s\"", dfd, path);
SYSINFO("Failed to create \"%d/\%s\"", dfd, path);
fd = open_at(dfd, path,
PROTECT_OPATH_FILE,
PROTECT_LOOKUP_BENEATH,
0);
}
return fd;
}
static int lxc_setup_ttys(struct lxc_conf *conf)
{
int ret;
struct lxc_rootfs *rootfs = &conf->rootfs;
const struct lxc_tty_info *ttys = &conf->ttys;
char *ttydir = ttys->dir;
if (!conf->rootfs.path)
return 0;
for (size_t i = 0; i < ttys->max; i++) {
__do_close int fd_to = -EBADF;
struct lxc_terminal_info *tty = &ttys->tty[i];
if (ttydir) {
char *tty_name, *tty_path;
ret = strnprintf(rootfs->buf, sizeof(rootfs->buf),
"/dev/%s/tty%zu", ttydir, i + 1);
if (ret < 0)
return ret_errno(-EIO);
tty_path = &rootfs->buf[STRLITERALLEN("/dev/")];
tty_name = tty_path + strlen(ttydir) + 1;
/* create bind-mount target */
fd_to = open_ttymnt_at(rootfs->dfd_dev, tty_path);