1 /* SPDX-License-Identifier: LGPL-2.1+ */
13 #include <linux/loop.h>
15 #include <netinet/in.h>
23 #include <sys/mount.h>
24 #include <sys/param.h>
25 #include <sys/prctl.h>
26 #include <sys/sendfile.h>
27 #include <sys/socket.h>
29 #include <sys/syscall.h>
30 #include <sys/sysmacros.h>
31 #include <sys/types.h>
32 #include <sys/utsname.h>
39 #include "cgroups/cgroup.h"
44 #include "confile_utils.h"
49 #include "lxcseccomp.h"
51 #include "memory_utils.h"
52 #include "mount_utils.h"
53 #include "namespace.h"
56 #include "process_utils.h"
59 #include "storage/storage.h"
60 #include "storage/overlay.h"
62 #include "syscall_wrappers.h"
68 #include <sys/mkdev.h>
72 #include <sys/statvfs.h>
82 #include <sys/capability.h>
90 #include "lxcmntent.h"
95 #if !defined(HAVE_PRLIMIT) && defined(HAVE_PRLIMIT64)
103 #ifndef HAVE_STRCHRNUL
104 #include "strchrnul.h"
107 lxc_log_define(conf
, lxc
);
110 * The lxc_conf of the container currently being worked on in an API call.
111 * This is used in the error calls.
113 thread_local
struct lxc_conf
*current_config
;
115 char *lxchook_names
[NUM_LXC_HOOKS
] = {
146 static struct mount_opt mount_opt
[] = {
147 { "atime", 1, false, MOUNT_ATTR_NOATIME
, MS_NOATIME
},
148 { "dev", 1, false, MOUNT_ATTR_NODEV
, MS_NODEV
},
149 { "diratime", 1, false, MOUNT_ATTR_NODIRATIME
, MS_NODIRATIME
},
150 { "exec", 1, false, MOUNT_ATTR_NOEXEC
, MS_NOEXEC
},
151 { "noatime", 0, false, MOUNT_ATTR_NOATIME
, MS_NOATIME
},
152 { "nodev", 0, false, MOUNT_ATTR_NODEV
, MS_NODEV
},
153 { "nodiratime", 0, false, MOUNT_ATTR_NODIRATIME
, MS_NODIRATIME
},
154 { "noexec", 0, false, MOUNT_ATTR_NOEXEC
, MS_NOEXEC
},
155 { "norelatime", 1, false, MOUNT_ATTR_RELATIME
, MS_RELATIME
},
156 { "nostrictatime", 1, false, MOUNT_ATTR_STRICTATIME
, MS_STRICTATIME
},
157 { "nosuid", 0, false, MOUNT_ATTR_NOSUID
, MS_NOSUID
},
158 { "relatime", 0, false, MOUNT_ATTR_RELATIME
, MS_RELATIME
},
159 { "ro", 0, false, MOUNT_ATTR_RDONLY
, MS_RDONLY
},
160 { "rw", 1, false, MOUNT_ATTR_RDONLY
, MS_RDONLY
},
161 { "strictatime", 0, false, MOUNT_ATTR_STRICTATIME
, MS_STRICTATIME
},
162 { "suid", 1, false, MOUNT_ATTR_NOSUID
, MS_NOSUID
},
164 { "bind", 0, false, 0, MS_BIND
},
165 { "defaults", 0, false, 0, 0 },
166 { "rbind", 0, true, 0, MS_BIND
| MS_REC
},
168 { "sync", 0, false, ~0, MS_SYNCHRONOUS
},
169 { "async", 1, false, ~0, MS_SYNCHRONOUS
},
170 { "dirsync", 0, false, ~0, MS_DIRSYNC
},
171 { "lazytime", 0, false, ~0, MS_LAZYTIME
},
172 { "mand", 0, false, ~0, MS_MANDLOCK
},
173 { "nomand", 1, false, ~0, MS_MANDLOCK
},
174 { "remount", 0, false, ~0, MS_REMOUNT
},
176 { NULL
, 0, false, ~0, ~0 },
179 static struct mount_opt propagation_opt
[] = {
180 { "private", 0, false, MS_PRIVATE
, MS_PRIVATE
},
181 { "shared", 0, false, MS_SHARED
, MS_SHARED
},
182 { "slave", 0, false, MS_SLAVE
, MS_SLAVE
},
183 { "unbindable", 0, false, MS_UNBINDABLE
, MS_UNBINDABLE
},
184 { "rprivate", 0, true, MS_PRIVATE
, MS_PRIVATE
| MS_REC
},
185 { "rshared", 0, true, MS_SHARED
, MS_SHARED
| MS_REC
},
186 { "rslave", 0, true, MS_SLAVE
, MS_SLAVE
| MS_REC
},
187 { "runbindable", 0, true, MS_UNBINDABLE
, MS_UNBINDABLE
| MS_REC
},
191 static struct caps_opt caps_opt
[] = {
193 { "chown", CAP_CHOWN
},
194 { "dac_override", CAP_DAC_OVERRIDE
},
195 { "dac_read_search", CAP_DAC_READ_SEARCH
},
196 { "fowner", CAP_FOWNER
},
197 { "fsetid", CAP_FSETID
},
198 { "kill", CAP_KILL
},
199 { "setgid", CAP_SETGID
},
200 { "setuid", CAP_SETUID
},
201 { "setpcap", CAP_SETPCAP
},
202 { "linux_immutable", CAP_LINUX_IMMUTABLE
},
203 { "net_bind_service", CAP_NET_BIND_SERVICE
},
204 { "net_broadcast", CAP_NET_BROADCAST
},
205 { "net_admin", CAP_NET_ADMIN
},
206 { "net_raw", CAP_NET_RAW
},
207 { "ipc_lock", CAP_IPC_LOCK
},
208 { "ipc_owner", CAP_IPC_OWNER
},
209 { "sys_module", CAP_SYS_MODULE
},
210 { "sys_rawio", CAP_SYS_RAWIO
},
211 { "sys_chroot", CAP_SYS_CHROOT
},
212 { "sys_ptrace", CAP_SYS_PTRACE
},
213 { "sys_pacct", CAP_SYS_PACCT
},
214 { "sys_admin", CAP_SYS_ADMIN
},
215 { "sys_boot", CAP_SYS_BOOT
},
216 { "sys_nice", CAP_SYS_NICE
},
217 { "sys_resource", CAP_SYS_RESOURCE
},
218 { "sys_time", CAP_SYS_TIME
},
219 { "sys_tty_config", CAP_SYS_TTY_CONFIG
},
220 { "mknod", CAP_MKNOD
},
221 { "lease", CAP_LEASE
},
222 { "audit_write", CAP_AUDIT_WRITE
},
223 { "audit_control", CAP_AUDIT_CONTROL
},
224 { "setfcap", CAP_SETFCAP
},
225 { "mac_override", CAP_MAC_OVERRIDE
},
226 { "mac_admin", CAP_MAC_ADMIN
},
227 { "syslog", CAP_SYSLOG
},
228 { "wake_alarm", CAP_WAKE_ALARM
},
229 { "block_suspend", CAP_BLOCK_SUSPEND
},
230 { "audit_read", CAP_AUDIT_READ
},
231 { "perfmon", CAP_PERFMON
},
233 { "checkpoint_restore", CAP_CHECKPOINT_RESTORE
},
237 static struct limit_opt limit_opt
[] = {
242 { "core", RLIMIT_CORE
},
245 { "cpu", RLIMIT_CPU
},
248 { "data", RLIMIT_DATA
},
251 { "fsize", RLIMIT_FSIZE
},
254 { "locks", RLIMIT_LOCKS
},
256 #ifdef RLIMIT_MEMLOCK
257 { "memlock", RLIMIT_MEMLOCK
},
259 #ifdef RLIMIT_MSGQUEUE
260 { "msgqueue", RLIMIT_MSGQUEUE
},
263 { "nice", RLIMIT_NICE
},
266 { "nofile", RLIMIT_NOFILE
},
269 { "nproc", RLIMIT_NPROC
},
272 { "rss", RLIMIT_RSS
},
275 { "rtprio", RLIMIT_RTPRIO
},
278 { "rttime", RLIMIT_RTTIME
},
280 #ifdef RLIMIT_SIGPENDING
281 { "sigpending", RLIMIT_SIGPENDING
},
284 { "stack", RLIMIT_STACK
},
288 static int run_buffer(char *buffer
)
290 __do_free
char *output
= NULL
;
291 __do_lxc_pclose
struct lxc_popen_FILE
*f
= NULL
;
294 f
= lxc_popen(buffer
);
296 return log_error_errno(-1, errno
, "Failed to popen() %s", buffer
);
298 output
= zalloc(LXC_LOG_BUFFER_SIZE
);
300 return log_error_errno(-1, ENOMEM
, "Failed to allocate memory for %s", buffer
);
304 return log_error_errno(-1, errno
, "Failed to retrieve underlying file descriptor");
306 for (int i
= 0; i
< 10; i
++) {
309 bytes_read
= lxc_read_nointr(fd
, output
, LXC_LOG_BUFFER_SIZE
- 1);
310 if (bytes_read
> 0) {
311 output
[bytes_read
] = '\0';
312 DEBUG("Script %s produced output: %s", buffer
, output
);
319 ret
= lxc_pclose(move_ptr(f
));
321 return log_error_errno(-1, errno
, "Script exited with error");
322 else if (WIFEXITED(ret
) && WEXITSTATUS(ret
) != 0)
323 return log_error(-1, "Script exited with status %d", WEXITSTATUS(ret
));
324 else if (WIFSIGNALED(ret
))
325 return log_error(-1, "Script terminated by signal %d", WTERMSIG(ret
));
330 int run_script_argv(const char *name
, unsigned int hook_version
,
331 const char *section
, const char *script
,
332 const char *hookname
, char **argv
)
334 __do_free
char *buffer
= NULL
;
338 if (hook_version
== 0)
339 INFO("Executing script \"%s\" for container \"%s\", config section \"%s\"",
340 script
, name
, section
);
342 INFO("Executing script \"%s\" for container \"%s\"", script
, name
);
344 for (i
= 0; argv
&& argv
[i
]; i
++)
345 size
+= strlen(argv
[i
]) + 1;
347 size
+= STRLITERALLEN("exec");
349 size
+= strlen(script
);
355 if (hook_version
== 0) {
356 size
+= strlen(hookname
);
359 size
+= strlen(name
);
362 size
+= strlen(section
);
369 buffer
= zalloc(size
);
373 if (hook_version
== 0)
374 buf_pos
= strnprintf(buffer
, size
, "exec %s %s %s %s", script
, name
, section
, hookname
);
376 buf_pos
= strnprintf(buffer
, size
, "exec %s", script
);
378 return log_error_errno(-1, errno
, "Failed to create command line for script \"%s\"", script
);
380 if (hook_version
== 1) {
381 ret
= setenv("LXC_HOOK_TYPE", hookname
, 1);
383 return log_error_errno(-1, errno
, "Failed to set environment variable: LXC_HOOK_TYPE=%s", hookname
);
385 TRACE("Set environment variable: LXC_HOOK_TYPE=%s", hookname
);
387 ret
= setenv("LXC_HOOK_SECTION", section
, 1);
389 return log_error_errno(-1, errno
, "Failed to set environment variable: LXC_HOOK_SECTION=%s", section
);
390 TRACE("Set environment variable: LXC_HOOK_SECTION=%s", section
);
392 if (strequal(section
, "net")) {
395 if (!argv
|| !argv
[0])
398 ret
= setenv("LXC_NET_TYPE", argv
[0], 1);
400 return log_error_errno(-1, errno
, "Failed to set environment variable: LXC_NET_TYPE=%s", argv
[0]);
401 TRACE("Set environment variable: LXC_NET_TYPE=%s", argv
[0]);
403 parent
= argv
[1] ? argv
[1] : "";
405 if (strequal(argv
[0], "macvlan")) {
406 ret
= setenv("LXC_NET_PARENT", parent
, 1);
408 return log_error_errno(-1, errno
, "Failed to set environment variable: LXC_NET_PARENT=%s", parent
);
409 TRACE("Set environment variable: LXC_NET_PARENT=%s", parent
);
410 } else if (strequal(argv
[0], "phys")) {
411 ret
= setenv("LXC_NET_PARENT", parent
, 1);
413 return log_error_errno(-1, errno
, "Failed to set environment variable: LXC_NET_PARENT=%s", parent
);
414 TRACE("Set environment variable: LXC_NET_PARENT=%s", parent
);
415 } else if (strequal(argv
[0], "veth")) {
416 char *peer
= argv
[2] ? argv
[2] : "";
418 ret
= setenv("LXC_NET_PEER", peer
, 1);
420 return log_error_errno(-1, errno
, "Failed to set environment variable: LXC_NET_PEER=%s", peer
);
421 TRACE("Set environment variable: LXC_NET_PEER=%s", peer
);
423 ret
= setenv("LXC_NET_PARENT", parent
, 1);
425 return log_error_errno(-1, errno
, "Failed to set environment variable: LXC_NET_PARENT=%s", parent
);
426 TRACE("Set environment variable: LXC_NET_PARENT=%s", parent
);
431 for (i
= 0; argv
&& argv
[i
]; i
++) {
432 size_t len
= size
- buf_pos
;
434 ret
= strnprintf(buffer
+ buf_pos
, len
, " %s", argv
[i
]);
436 return log_error_errno(-1, errno
, "Failed to create command line for script \"%s\"", script
);
440 return run_buffer(buffer
);
443 int run_script(const char *name
, const char *section
, const char *script
, ...)
445 __do_free
char *buffer
= NULL
;
451 INFO("Executing script \"%s\" for container \"%s\", config section \"%s\"",
452 script
, name
, section
);
454 va_start(ap
, script
);
455 while ((p
= va_arg(ap
, char *)))
456 size
+= strlen(p
) + 1;
459 size
+= STRLITERALLEN("exec");
460 size
+= strlen(script
);
461 size
+= strlen(name
);
462 size
+= strlen(section
);
468 buffer
= must_realloc(NULL
, size
);
469 ret
= strnprintf(buffer
, size
, "exec %s %s %s", script
, name
, section
);
473 va_start(ap
, script
);
474 while ((p
= va_arg(ap
, char *))) {
475 int len
= size
- ret
;
477 rc
= strnprintf(buffer
+ ret
, len
, " %s", p
);
486 return run_buffer(buffer
);
489 int lxc_storage_prepare(struct lxc_conf
*conf
)
492 struct lxc_rootfs
*rootfs
= &conf
->rootfs
;
495 ret
= mount("", "/", NULL
, MS_SLAVE
| MS_REC
, 0);
497 return log_error_errno(-1, errno
, "Failed to recursively turn root mount tree into dependent mount");
499 rootfs
->dfd_mnt
= open_at(-EBADF
, "/", PROTECT_OPATH_DIRECTORY
, PROTECT_LOOKUP_ABSOLUTE
, 0);
500 if (rootfs
->dfd_mnt
< 0)
506 ret
= access(rootfs
->mount
, F_OK
);
508 return log_error_errno(-1, errno
, "Failed to access to \"%s\". Check it is present",
511 rootfs
->storage
= storage_init(conf
);
512 if (!rootfs
->storage
)
513 return log_error(-1, "Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\"",
514 rootfs
->path
, rootfs
->mount
,
515 rootfs
->mnt_opts
.raw_options
? rootfs
->mnt_opts
.raw_options
: "(null)");
520 void lxc_storage_put(struct lxc_conf
*conf
)
522 storage_put(conf
->rootfs
.storage
);
523 conf
->rootfs
.storage
= NULL
;
526 /* lxc_rootfs_prepare
527 * if rootfs is a directory, then open ${rootfs}/.lxc-keep for writing for
528 * the duration of the container run, to prevent the container from marking
529 * the underlying fs readonly on shutdown. unlink the file immediately so
530 * no name pollution is happens.
531 * don't unlink on NFS to avoid random named stale handles.
533 int lxc_rootfs_init(struct lxc_conf
*conf
, bool userns
)
535 __do_close
int dfd_path
= -EBADF
, fd_pin
= -EBADF
;
539 struct lxc_rootfs
*rootfs
= &conf
->rootfs
;
541 ret
= lxc_storage_prepare(conf
);
543 return syserror_set(-EINVAL
, "Failed to prepare rootfs storage");
545 if (!is_empty_string(rootfs
->mnt_opts
.userns_path
)) {
547 return syserror_set(-EINVAL
, "Idmapped rootfs currently only supported with separate rootfs for container");
549 if (rootfs
->bdev_type
&& !strequal(rootfs
->bdev_type
, "dir"))
550 return syserror_set(-EINVAL
, "Idmapped rootfs currently only supports the \"dir\" storage driver");
554 return log_trace(0, "Not pinning because container does not have a rootfs");
557 return log_trace(0, "Not pinning because container runs in user namespace");
559 if (rootfs
->bdev_type
) {
560 if (strequal(rootfs
->bdev_type
, "overlay") ||
561 strequal(rootfs
->bdev_type
, "overlayfs"))
562 return log_trace_errno(0, EINVAL
, "Not pinning on stacking filesystem");
564 if (strequal(rootfs
->bdev_type
, "zfs"))
565 return log_trace_errno(0, EINVAL
, "Not pinning on ZFS filesystem");
568 dfd_path
= open_at(-EBADF
, rootfs
->path
, PROTECT_OPATH_FILE
, 0, 0);
570 return syserror("Failed to open \"%s\"", rootfs
->path
);
572 ret
= fstat(dfd_path
, &st
);
574 return log_trace_errno(-errno
, errno
, "Failed to retrieve file status");
576 if (!S_ISDIR(st
.st_mode
))
577 return log_trace_errno(0, ENOTDIR
, "Not pinning because file descriptor is not a directory");
579 fd_pin
= open_at(dfd_path
, ".lxc_keep",
580 PROTECT_OPEN
| O_CREAT
,
581 PROTECT_LOOKUP_BENEATH
,
584 if (errno
== EROFS
) {
585 return log_trace_errno(0, EROFS
, "Not pinning on read-only filesystem");
587 return syserror("Failed to pin rootfs");
590 TRACE("Pinned rootfs %d(.lxc_keep)", fd_pin
);
592 ret
= fstatfs(fd_pin
, &stfs
);
594 SYSWARN("Failed to retrieve filesystem status");
598 if (stfs
.f_type
== NFS_SUPER_MAGIC
) {
599 DEBUG("Not unlinking pinned file on NFS");
603 if (unlinkat(dfd_path
, ".lxc_keep", 0))
604 SYSTRACE("Failed to unlink rootfs pinning file %d(.lxc_keep)", dfd_path
);
606 TRACE("Unlinked pinned file %d(.lxc_keep)", dfd_path
);
609 rootfs
->fd_path_pin
= move_fd(fd_pin
);
613 int lxc_rootfs_prepare_parent(struct lxc_handler
*handler
)
615 __do_close
int dfd_idmapped
= -EBADF
, fd_userns
= -EBADF
;
616 struct lxc_rootfs
*rootfs
= &handler
->conf
->rootfs
;
617 struct lxc_storage
*storage
= rootfs
->storage
;
618 const struct lxc_mount_options
*mnt_opts
= &rootfs
->mnt_opts
;
620 const char *path_source
;
622 if (list_empty(&handler
->conf
->id_map
))
625 if (is_empty_string(rootfs
->mnt_opts
.userns_path
))
628 if (handler
->conf
->rootfs_setup
)
631 if (rootfs_is_blockdev(handler
->conf
))
632 return syserror_set(-EOPNOTSUPP
, "Idmapped mounts on block-backed storage not yet supported");
634 if (!can_use_bind_mounts())
635 return syserror_set(-EOPNOTSUPP
, "Kernel does not support the new mount api");
637 if (strequal(rootfs
->mnt_opts
.userns_path
, "container"))
638 fd_userns
= dup_cloexec(handler
->nsfd
[LXC_NS_USER
]);
640 fd_userns
= open_at(-EBADF
, rootfs
->mnt_opts
.userns_path
,
641 PROTECT_OPEN_WITH_TRAILING_SYMLINKS
, 0, 0);
643 return syserror("Failed to open user namespace");
645 path_source
= lxc_storage_get_path(storage
->src
, storage
->type
);
647 dfd_idmapped
= create_detached_idmapped_mount(path_source
, fd_userns
, true,
648 mnt_opts
->attr
.attr_set
,
649 mnt_opts
->attr
.attr_clr
);
650 if (dfd_idmapped
< 0)
651 return syserror("Failed to create detached idmapped mount");
653 ret
= lxc_abstract_unix_send_fds(handler
->data_sock
[0], &dfd_idmapped
, 1, NULL
, 0);
655 return syserror("Failed to send detached idmapped mount fd");
657 TRACE("Created detached idmapped mount %d", dfd_idmapped
);
661 static int add_shmount_to_list(struct lxc_conf
*conf
)
663 char new_mount
[PATH_MAX
];
664 /* Offset for the leading '/' since the path_cont
665 * is absolute inside the container.
667 int offset
= 1, ret
= -1;
669 ret
= strnprintf(new_mount
, sizeof(new_mount
),
670 "%s %s none bind,create=dir 0 0", conf
->shmount
.path_host
,
671 conf
->shmount
.path_cont
+ offset
);
675 return add_elem_to_mount_list(new_mount
, conf
);
678 static int lxc_mount_auto_mounts(struct lxc_handler
*handler
, int flags
)
685 const char *destination
;
689 bool requires_cap_net_admin
;
690 } default_mounts
[] = {
691 /* Read-only bind-mounting... In older kernels, doing that
692 * required to do one MS_BIND mount and then
693 * MS_REMOUNT|MS_RDONLY the same one. According to mount(2)
694 * manpage, MS_BIND honors MS_RDONLY from kernel 2.6.26
695 * onwards. However, this apparently does not work on kernel
696 * 3.8. Unfortunately, on that very same kernel, doing the same
697 * trick as above doesn't seem to work either, there one needs
698 * to ALSO specify MS_BIND for the remount, otherwise the
699 * entire fs is remounted read-only or the mount fails because
700 * it's busy... MS_REMOUNT|MS_BIND|MS_RDONLY seems to work for
701 * kernels as low as 2.6.32...
703 { LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
, "proc", "%r/proc", "proc", MS_NODEV
|MS_NOEXEC
|MS_NOSUID
, NULL
, false },
704 /* proc/tty is used as a temporary placeholder for proc/sys/net which we'll move back in a few steps */
705 { LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
, "%r/proc/sys/net", "%r/proc/tty", NULL
, MS_BIND
, NULL
, true, },
706 { LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
, "%r/proc/sys", "%r/proc/sys", NULL
, MS_BIND
, NULL
, false },
707 { LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
, NULL
, "%r/proc/sys", NULL
, MS_REMOUNT
|MS_BIND
|MS_RDONLY
, NULL
, false },
708 { LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
, "%r/proc/tty", "%r/proc/sys/net", NULL
, MS_MOVE
, NULL
, true },
709 { LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
, "%r/proc/sysrq-trigger", "%r/proc/sysrq-trigger", NULL
, MS_BIND
, NULL
, false },
710 { LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
, NULL
, "%r/proc/sysrq-trigger", NULL
, MS_REMOUNT
|MS_BIND
|MS_RDONLY
, NULL
, false },
711 { LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
, "proc", "%r/proc", "proc", MS_NODEV
|MS_NOEXEC
|MS_NOSUID
, NULL
, false },
712 { LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
, "sysfs", "%r/sys", "sysfs", 0, NULL
, false },
713 { LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
, "sysfs", "%r/sys", "sysfs", MS_RDONLY
, NULL
, false },
714 { LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
, "sysfs", "%r/sys", "sysfs", MS_RDONLY
|MS_NOSUID
|MS_NODEV
|MS_NOEXEC
, NULL
, false },
715 { LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
, "%r/sys/devices/virtual/net", "%r/sys/devices/virtual/net", NULL
, MS_BIND
, NULL
, false },
716 { LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
, NULL
, "%r/sys/devices/virtual/net", NULL
, MS_REMOUNT
|MS_NOSUID
|MS_NODEV
|MS_NOEXEC
, NULL
, false },
717 { 0, 0, NULL
, NULL
, NULL
, 0, NULL
, false }
719 struct lxc_conf
*conf
= handler
->conf
;
720 struct lxc_rootfs
*rootfs
= &conf
->rootfs
;
721 bool has_cap_net_admin
;
723 if (flags
& LXC_AUTO_PROC_MASK
) {
726 * Only unmount procfs if we have a separate rootfs so
727 * we can still access it in safe_mount() below.
729 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "%s/proc",
730 rootfs
->path
? rootfs
->mount
: "");
732 return ret_errno(EIO
);
734 ret
= umount2(rootfs
->buf
, MNT_DETACH
);
736 SYSDEBUG("Tried to ensure procfs is unmounted");
739 ret
= mkdirat(rootfs
->dfd_mnt
, "proc" , S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
740 if (ret
< 0 && errno
!= EEXIST
)
741 return syserror("Failed to create procfs mountpoint under %d", rootfs
->dfd_mnt
);
743 TRACE("Created procfs mountpoint under %d", rootfs
->dfd_mnt
);
746 if (flags
& LXC_AUTO_SYS_MASK
) {
749 * Only unmount sysfs if we have a separate rootfs so
750 * we can still access it in safe_mount() below.
752 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "%s/sys",
753 rootfs
->path
? rootfs
->mount
: "");
755 return ret_errno(EIO
);
757 ret
= umount2(rootfs
->buf
, MNT_DETACH
);
759 SYSDEBUG("Tried to ensure sysfs is unmounted");
762 ret
= mkdirat(rootfs
->dfd_mnt
, "sys" , S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
763 if (ret
< 0 && errno
!= EEXIST
)
764 return syserror("Failed to create sysfs mountpoint under %d", rootfs
->dfd_mnt
);
766 TRACE("Created sysfs mountpoint under %d", rootfs
->dfd_mnt
);
769 has_cap_net_admin
= lxc_wants_cap(CAP_NET_ADMIN
, conf
);
770 for (i
= 0; default_mounts
[i
].match_mask
; i
++) {
771 __do_free
char *destination
= NULL
, *source
= NULL
;
772 unsigned long mflags
= default_mounts
[i
].flags
;
774 if ((flags
& default_mounts
[i
].match_mask
) != default_mounts
[i
].match_flag
)
777 if (default_mounts
[i
].source
) {
778 /* will act like strdup if %r is not present */
779 source
= lxc_string_replace("%r", rootfs
->path
? rootfs
->mount
: "", default_mounts
[i
].source
);
781 return syserror_set(-ENOMEM
, "Failed to create source path");
784 if (!default_mounts
[i
].destination
)
785 return syserror_set(-EINVAL
, "BUG: auto mounts destination %d was NULL", i
);
787 if (!has_cap_net_admin
&& default_mounts
[i
].requires_cap_net_admin
) {
788 TRACE("Container does not have CAP_NET_ADMIN. Skipping \"%s\" mount", default_mounts
[i
].source
?: "(null)");
792 /* will act like strdup if %r is not present */
793 destination
= lxc_string_replace("%r", rootfs
->path
? rootfs
->mount
: "", default_mounts
[i
].destination
);
795 return syserror_set(-ENOMEM
, "Failed to create target path");
797 ret
= safe_mount(source
, destination
,
798 default_mounts
[i
].fstype
,
800 default_mounts
[i
].options
,
801 rootfs
->path
? rootfs
->mount
: NULL
);
804 return syserror("Failed to mount \"%s\" on \"%s\" with flags %lu", source
, destination
, mflags
);
806 INFO("Mount source or target for \"%s\" on \"%s\" does not exist. Skipping", source
, destination
);
810 if (mflags
& MS_REMOUNT
)
811 TRACE("Remounted automount \"%s\" on \"%s\" %s with flags %lu", source
, destination
, (mflags
& MS_RDONLY
) ? "read-only" : "read-write", mflags
);
813 TRACE("Mounted automount \"%s\" on \"%s\" %s with flags %lu", source
, destination
, (mflags
& MS_RDONLY
) ? "read-only" : "read-write", mflags
);
816 if (flags
& LXC_AUTO_CGROUP_MASK
) {
819 cg_flags
= flags
& (LXC_AUTO_CGROUP_MASK
& ~LXC_AUTO_CGROUP_FORCE
);
820 /* If the type of cgroup mount was not specified, it depends on
821 * the container's capabilities as to what makes sense: if we
822 * have CAP_SYS_ADMIN, the read-only part can be remounted
823 * read-write anyway, so we may as well default to read-write;
824 * then the admin will not be given a false sense of security.
825 * (And if they really want mixed r/o r/w, then they can
826 * explicitly specify :mixed.) OTOH, if the container lacks
827 * CAP_SYS_ADMIN, do only default to :mixed, because then the
828 * container can't remount it read-write.
830 if ((cg_flags
== LXC_AUTO_CGROUP_NOSPEC
) || (cg_flags
== LXC_AUTO_CGROUP_FULL_NOSPEC
)) {
831 if (cg_flags
== LXC_AUTO_CGROUP_NOSPEC
)
832 cg_flags
= has_cap(CAP_SYS_ADMIN
, conf
)
834 : LXC_AUTO_CGROUP_MIXED
;
836 cg_flags
= has_cap(CAP_SYS_ADMIN
, conf
)
837 ? LXC_AUTO_CGROUP_FULL_RW
838 : LXC_AUTO_CGROUP_FULL_MIXED
;
841 if (flags
& LXC_AUTO_CGROUP_FORCE
)
842 cg_flags
|= LXC_AUTO_CGROUP_FORCE
;
844 if (!handler
->cgroup_ops
->mount(handler
->cgroup_ops
, handler
, cg_flags
))
845 return log_error_errno(-1, errno
, "Failed to mount \"/sys/fs/cgroup\"");
848 if (flags
& LXC_AUTO_SHMOUNTS_MASK
) {
849 ret
= add_shmount_to_list(conf
);
851 return log_error(-1, "Failed to add shmount entry to container config");
857 static int setup_utsname(struct utsname
*utsname
)
864 ret
= sethostname(utsname
->nodename
, strlen(utsname
->nodename
));
866 return log_error_errno(-1, errno
, "Failed to set the hostname to \"%s\"",
869 INFO("Set hostname to \"%s\"", utsname
->nodename
);
874 struct dev_symlinks
{
879 static const struct dev_symlinks dev_symlinks
[] = {
880 { "/proc/self/fd", "fd" },
881 { "/proc/self/fd/0", "stdin" },
882 { "/proc/self/fd/1", "stdout" },
883 { "/proc/self/fd/2", "stderr" },
886 static int lxc_setup_dev_symlinks(const struct lxc_rootfs
*rootfs
)
888 for (int i
= 0; i
< sizeof(dev_symlinks
) / sizeof(dev_symlinks
[0]); i
++) {
891 const struct dev_symlinks
*d
= &dev_symlinks
[i
];
894 * Stat the path first. If we don't get an error accept it as
895 * is and don't try to create it
897 ret
= fstatat(rootfs
->dfd_dev
, d
->name
, &s
, 0);
901 ret
= symlinkat(d
->oldpath
, rootfs
->dfd_dev
, d
->name
);
905 WARN("Failed to create \"%s\" on read-only filesystem", d
->name
);
910 return log_error_errno(-errno
, errno
, "Failed to create \"%s\"", d
->name
);
918 /* Build a space-separate list of ptys to pass to systemd. */
919 static bool append_ttyname(char **pp
, char *name
)
925 *pp
= zalloc(strlen(name
) + strlen("container_ttys=") + 1);
929 sprintf(*pp
, "container_ttys=%s", name
);
933 size
= strlen(*pp
) + strlen(name
) + 2;
934 p
= realloc(*pp
, size
);
939 (void)strlcat(p
, " ", size
);
940 (void)strlcat(p
, name
, size
);
945 static int open_ttymnt_at(int dfd
, const char *path
)
949 fd
= open_at(dfd
, path
,
950 PROTECT_OPEN
| O_CREAT
| O_EXCL
,
951 PROTECT_LOOKUP_BENEATH
,
954 if (!IN_SET(errno
, ENXIO
, EEXIST
))
955 return syserror("Failed to create \"%d/\%s\"", dfd
, path
);
957 SYSINFO("Failed to create \"%d/\%s\"", dfd
, path
);
958 fd
= open_at(dfd
, path
,
960 PROTECT_LOOKUP_BENEATH
,
967 static int lxc_setup_ttys(struct lxc_conf
*conf
)
970 struct lxc_rootfs
*rootfs
= &conf
->rootfs
;
971 const struct lxc_tty_info
*ttys
= &conf
->ttys
;
972 char *ttydir
= ttys
->dir
;
974 if (!conf
->rootfs
.path
)
977 for (int i
= 0; i
< ttys
->max
; i
++) {
978 __do_close
int fd_to
= -EBADF
;
979 struct lxc_terminal_info
*tty
= &ttys
->tty
[i
];
982 char *tty_name
, *tty_path
;
984 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
),
985 "/dev/%s/tty%d", ttydir
, i
+ 1);
987 return ret_errno(-EIO
);
989 tty_path
= &rootfs
->buf
[STRLITERALLEN("/dev/")];
990 tty_name
= tty_path
+ strlen(ttydir
) + 1;
992 /* create bind-mount target */
993 fd_to
= open_ttymnt_at(rootfs
->dfd_dev
, tty_path
);
995 return log_error_errno(-errno
, errno
,
996 "Failed to create tty mount target %d(%s)",
997 rootfs
->dfd_dev
, tty_path
);
999 ret
= unlinkat(rootfs
->dfd_dev
, tty_name
, 0);
1000 if (ret
< 0 && errno
!= ENOENT
)
1001 return log_error_errno(-errno
, errno
,
1002 "Failed to unlink %d(%s)",
1003 rootfs
->dfd_dev
, tty_name
);
1005 if (can_use_mount_api())
1006 ret
= fd_bind_mount(tty
->pty
, "",
1008 PROTECT_LOOKUP_BENEATH_XDEV
,
1011 PROTECT_LOOKUP_BENEATH_XDEV
,
1017 ret
= mount_fd(tty
->pty
, fd_to
, "none", MS_BIND
, 0);
1019 return log_error_errno(-errno
, errno
,
1020 "Failed to bind mount \"%s\" onto \"%s\"",
1021 tty
->name
, rootfs
->buf
);
1022 DEBUG("Bind mounted \"%s\" onto \"%s\"", tty
->name
, rootfs
->buf
);
1024 ret
= symlinkat(tty_path
, rootfs
->dfd_dev
, tty_name
);
1026 return log_error_errno(-errno
, errno
,
1027 "Failed to create symlink \"%d(%s)\" -> \"%d(%s)\"",
1028 rootfs
->dfd_dev
, tty_name
,
1029 rootfs
->dfd_dev
, tty_path
);
1031 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "tty%d", i
+ 1);
1033 return ret_errno(-EIO
);
1035 /* If we populated /dev, then we need to create /dev/tty<idx>. */
1036 fd_to
= open_ttymnt_at(rootfs
->dfd_dev
, rootfs
->buf
);
1038 return log_error_errno(-errno
, errno
,
1039 "Failed to create tty mount target %d(%s)",
1040 rootfs
->dfd_dev
, rootfs
->buf
);
1042 if (can_use_mount_api())
1043 ret
= fd_bind_mount(tty
->pty
, "",
1045 PROTECT_LOOKUP_BENEATH_XDEV
,
1048 PROTECT_LOOKUP_BENEATH
,
1054 ret
= mount_fd(tty
->pty
, fd_to
, "none", MS_BIND
, 0);
1056 return log_error_errno(-errno
, errno
,
1057 "Failed to bind mount \"%s\" onto \"%s\"",
1058 tty
->name
, rootfs
->buf
);
1059 DEBUG("Bind mounted \"%s\" onto \"%s\"", tty
->name
, rootfs
->buf
);
1062 if (!append_ttyname(&conf
->ttys
.tty_names
, tty
->name
))
1063 return log_error(-1, "Error setting up container_ttys string");
1066 INFO("Finished setting up %zu /dev/tty<N> device(s)", ttys
->max
);
1070 define_cleanup_function(struct lxc_tty_info
*, lxc_delete_tty
);
1072 static int lxc_allocate_ttys(struct lxc_conf
*conf
)
1074 call_cleaner(lxc_delete_tty
) struct lxc_tty_info
*ttys
= &conf
->ttys
;
1077 /* no tty in the configuration */
1081 ttys
->tty
= zalloc(sizeof(struct lxc_terminal_info
) * ttys
->max
);
1085 for (size_t i
= 0; i
< conf
->ttys
.max
; i
++) {
1087 struct lxc_terminal_info
*tty
= &ttys
->tty
[i
];
1089 ret
= lxc_devpts_terminal(conf
->devpts_fd
, &tty
->ptx
,
1090 &tty
->pty
, &pty_nr
, false);
1093 return syserror_set(-ENOTTY
, "Failed to create tty %zu", i
);
1095 DEBUG("Created tty with ptx fd %d and pty fd %d and index %d",
1096 tty
->ptx
, tty
->pty
, pty_nr
);
1100 INFO("Finished creating %zu tty devices", ttys
->max
);
1105 void lxc_delete_tty(struct lxc_tty_info
*ttys
)
1107 if (!ttys
|| !ttys
->tty
)
1110 for (int i
= 0; i
< ttys
->max
; i
++) {
1111 struct lxc_terminal_info
*tty
= &ttys
->tty
[i
];
1112 close_prot_errno_disarm(tty
->ptx
);
1113 close_prot_errno_disarm(tty
->pty
);
1116 free_disarm(ttys
->tty
);
1119 static int __lxc_send_ttys_to_parent(struct lxc_handler
*handler
)
1123 struct lxc_conf
*conf
= handler
->conf
;
1124 struct lxc_tty_info
*ttys
= &conf
->ttys
;
1125 int sock
= handler
->data_sock
[0];
1130 for (i
= 0; i
< ttys
->max
; i
++) {
1132 struct lxc_terminal_info
*tty
= &ttys
->tty
[i
];
1134 ttyfds
[0] = tty
->ptx
;
1135 ttyfds
[1] = tty
->pty
;
1137 ret
= lxc_abstract_unix_send_fds(sock
, ttyfds
, 2, NULL
, 0);
1141 TRACE("Sent tty \"%s\" with ptx fd %d and pty fd %d to parent",
1142 tty
->name
, tty
->ptx
, tty
->pty
);
1146 SYSERROR("Failed to send %zu ttys to parent", ttys
->max
);
1148 TRACE("Sent %zu ttys to parent", ttys
->max
);
1153 static int lxc_create_ttys(struct lxc_handler
*handler
)
1156 struct lxc_conf
*conf
= handler
->conf
;
1158 ret
= lxc_allocate_ttys(conf
);
1160 ERROR("Failed to allocate ttys");
1164 if (!conf
->is_execute
) {
1165 ret
= lxc_setup_ttys(conf
);
1167 ERROR("Failed to setup ttys");
1172 if (conf
->ttys
.tty_names
) {
1173 ret
= setenv("container_ttys", conf
->ttys
.tty_names
, 1);
1175 SYSERROR("Failed to set \"container_ttys=%s\"", conf
->ttys
.tty_names
);
1183 lxc_delete_tty(&conf
->ttys
);
1188 static int lxc_send_ttys_to_parent(struct lxc_handler
*handler
)
1192 ret
= __lxc_send_ttys_to_parent(handler
);
1193 lxc_delete_tty(&handler
->conf
->ttys
);
1197 /* Just create a path for /dev under $lxcpath/$name and in rootfs If we hit an
1198 * error, log it but don't fail yet.
1200 static int mount_autodev(const char *name
, const struct lxc_rootfs
*rootfs
,
1201 int autodevtmpfssize
, const char *lxcpath
)
1203 __do_close
int fd_fs
= -EBADF
;
1204 const char *path
= rootfs
->path
? rootfs
->mount
: NULL
;
1205 size_t tmpfs_size
= (autodevtmpfssize
!= 0) ? autodevtmpfssize
: 500000;
1208 char mount_options
[128];
1210 INFO("Preparing \"/dev\"");
1212 cur_mask
= umask(S_IXUSR
| S_IXGRP
| S_IXOTH
);
1213 ret
= mkdirat(rootfs
->dfd_mnt
, "dev" , S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
1214 if (ret
< 0 && errno
!= EEXIST
) {
1215 SYSERROR("Failed to create \"/dev\" directory");
1220 if (can_use_mount_api()) {
1221 fd_fs
= fs_prepare("tmpfs", -EBADF
, "", 0, 0);
1223 return log_error_errno(-errno
, errno
, "Failed to prepare filesystem context for tmpfs");
1225 sprintf(mount_options
, "%zu", tmpfs_size
);
1227 ret
= fs_set_property(fd_fs
, "mode", "0755");
1229 return log_error_errno(-errno
, errno
, "Failed to mount tmpfs onto %d(dev)", fd_fs
);
1231 ret
= fs_set_property(fd_fs
, "size", mount_options
);
1233 return log_error_errno(-errno
, errno
, "Failed to mount tmpfs onto %d(dev)", fd_fs
);
1235 ret
= fs_attach(fd_fs
, rootfs
->dfd_mnt
, "dev",
1236 PROTECT_OPATH_DIRECTORY
,
1237 PROTECT_LOOKUP_BENEATH_XDEV
, 0);
1239 __do_free
char *fallback_path
= NULL
;
1241 sprintf(mount_options
, "size=%zu,mode=755", tmpfs_size
);
1242 DEBUG("Using mount options: %s", mount_options
);
1245 fallback_path
= must_make_path(path
, "/dev", NULL
);
1246 ret
= safe_mount("none", fallback_path
, "tmpfs", 0, mount_options
, path
);
1248 ret
= safe_mount("none", "dev", "tmpfs", 0, mount_options
, NULL
);
1252 SYSERROR("Failed to mount tmpfs on \"%s\"", path
);
1256 /* If we are running on a devtmpfs mapping, dev/pts may already exist.
1257 * If not, then create it and exit if that fails...
1259 ret
= mkdirat(rootfs
->dfd_mnt
, "dev/pts", S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
1260 if (ret
< 0 && errno
!= EEXIST
) {
1261 SYSERROR("Failed to create directory \"dev/pts\"");
1269 (void)umask(cur_mask
);
1271 INFO("Prepared \"/dev\"");
1275 struct lxc_device_node
{
1282 static const struct lxc_device_node lxc_devices
[] = {
1283 { "full", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 7 },
1284 { "null", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 3 },
1285 { "random", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 8 },
1286 { "tty", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 5, 0 },
1287 { "urandom", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 9 },
1288 { "zero", S_IFCHR
| S_IRWXU
| S_IRWXG
| S_IRWXO
, 1, 5 },
1295 LXC_DEVNODE_PARTIAL
,
1299 static int lxc_fill_autodev(struct lxc_rootfs
*rootfs
)
1303 int use_mknod
= LXC_DEVNODE_MKNOD
;
1305 if (rootfs
->dfd_dev
< 0)
1306 return log_info(0, "No /dev directory found, skipping setup");
1308 INFO("Populating \"/dev\"");
1310 cmask
= umask(S_IXUSR
| S_IXGRP
| S_IXOTH
);
1311 for (i
= 0; i
< sizeof(lxc_devices
) / sizeof(lxc_devices
[0]); i
++) {
1312 const struct lxc_device_node
*device
= &lxc_devices
[i
];
1314 if (use_mknod
>= LXC_DEVNODE_MKNOD
) {
1315 ret
= mknodat(rootfs
->dfd_dev
, device
->name
, device
->mode
, makedev(device
->maj
, device
->min
));
1316 if (ret
== 0 || (ret
< 0 && errno
== EEXIST
)) {
1317 DEBUG("Created device node \"%s\"", device
->name
);
1318 } else if (ret
< 0) {
1320 return log_error_errno(-1, errno
, "Failed to create device node \"%s\"", device
->name
);
1322 use_mknod
= LXC_DEVNODE_BIND
;
1325 /* Device nodes are fully useable. */
1326 if (use_mknod
== LXC_DEVNODE_OPEN
)
1329 if (use_mknod
== LXC_DEVNODE_MKNOD
) {
1330 __do_close
int fd
= -EBADF
;
1332 * - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=55956b59df336f6738da916dbb520b6e37df9fbd
1333 * - https://lists.linuxfoundation.org/pipermail/containers/2018-June/039176.html
1335 fd
= open_at(rootfs
->dfd_dev
, device
->name
, PROTECT_OPEN
, PROTECT_LOOKUP_BENEATH
, 0);
1337 /* Device nodes are fully useable. */
1338 use_mknod
= LXC_DEVNODE_OPEN
;
1342 SYSTRACE("Failed to open \"%s\" device", device
->name
);
1343 /* Device nodes are only partially useable. */
1344 use_mknod
= LXC_DEVNODE_PARTIAL
;
1348 if (use_mknod
!= LXC_DEVNODE_PARTIAL
) {
1349 /* If we are dealing with partially functional device
1350 * nodes the prio mknod() call will have created the
1351 * device node so we can use it as a bind-mount target.
1353 ret
= mknodat(rootfs
->dfd_dev
, device
->name
, S_IFREG
| 0000, 0);
1354 if (ret
< 0 && errno
!= EEXIST
)
1355 return log_error_errno(-1, errno
, "Failed to create file \"%s\"", device
->name
);
1358 /* Fallback to bind-mounting the device from the host. */
1359 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "dev/%s", device
->name
);
1361 return ret_errno(EIO
);
1363 if (can_use_mount_api()) {
1364 ret
= fd_bind_mount(rootfs
->dfd_host
, rootfs
->buf
,
1366 PROTECT_LOOKUP_BENEATH_XDEV
,
1367 rootfs
->dfd_dev
, device
->name
,
1369 PROTECT_LOOKUP_BENEATH
,
1375 char path
[PATH_MAX
];
1377 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "/dev/%s", device
->name
);
1379 return ret_errno(EIO
);
1381 ret
= strnprintf(path
, sizeof(path
), "%s/dev/%s", get_rootfs_mnt(rootfs
), device
->name
);
1383 return log_error(-1, "Failed to create device path for %s", device
->name
);
1385 ret
= safe_mount(rootfs
->buf
, path
, 0, MS_BIND
, NULL
, get_rootfs_mnt(rootfs
));
1387 return log_error_errno(-1, errno
, "Failed to bind mount host device node \"%s\" to \"%s\"", rootfs
->buf
, path
);
1389 DEBUG("Bind mounted host device node \"%s\" to \"%s\"", rootfs
->buf
, path
);
1392 DEBUG("Bind mounted host device %d(%s) to %d(%s)", rootfs
->dfd_host
, rootfs
->buf
, rootfs
->dfd_dev
, device
->name
);
1396 INFO("Populated \"/dev\"");
1400 static int lxc_mount_rootfs(struct lxc_rootfs
*rootfs
)
1404 if (!rootfs
->path
) {
1405 ret
= mount("", "/", NULL
, MS_SLAVE
| MS_REC
, 0);
1407 return log_error_errno(-1, errno
, "Failed to recursively turn root mount tree into dependent mount");
1409 rootfs
->dfd_mnt
= open_at(-EBADF
, "/", PROTECT_OPATH_DIRECTORY
, PROTECT_LOOKUP_ABSOLUTE
, 0);
1410 if (rootfs
->dfd_mnt
< 0)
1413 return log_trace(0, "Container doesn't use separate rootfs. Opened host's rootfs");
1416 ret
= access(rootfs
->mount
, F_OK
);
1418 return log_error_errno(-1, errno
, "Failed to access to \"%s\". Check it is present",
1421 ret
= rootfs
->storage
->ops
->mount(rootfs
->storage
);
1423 return log_error(-1, "Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\"",
1424 rootfs
->path
, rootfs
->mount
,
1425 rootfs
->mnt_opts
.raw_options
? rootfs
->mnt_opts
.raw_options
: "(null)");
1427 DEBUG("Mounted rootfs \"%s\" onto \"%s\" with options \"%s\"",
1428 rootfs
->path
, rootfs
->mount
,
1429 rootfs
->mnt_opts
.raw_options
? rootfs
->mnt_opts
.raw_options
: "(null)");
1431 rootfs
->dfd_mnt
= open_at(-EBADF
, rootfs
->mount
, PROTECT_OPATH_DIRECTORY
, PROTECT_LOOKUP_ABSOLUTE_XDEV
, 0);
1432 if (rootfs
->dfd_mnt
< 0)
1435 return log_trace(0, "Container uses separate rootfs. Opened container's rootfs");
1438 static int lxc_chroot(const struct lxc_rootfs
*rootfs
)
1440 __do_free
char *nroot
= NULL
;
1442 char *root
= rootfs
->mount
;
1444 nroot
= realpath(root
, NULL
);
1446 return log_error_errno(-1, errno
, "Failed to resolve \"%s\"", root
);
1452 /* We could use here MS_MOVE, but in userns this mount is locked and
1455 ret
= mount(nroot
, "/", NULL
, MS_REC
| MS_BIND
, NULL
);
1457 return log_error_errno(-1, errno
, "Failed to mount \"%s\" onto \"/\" as MS_REC | MS_BIND", nroot
);
1459 ret
= mount(NULL
, "/", NULL
, MS_REC
| MS_PRIVATE
, NULL
);
1461 return log_error_errno(-1, errno
, "Failed to remount \"/\"");
1463 /* The following code cleans up inherited mounts which are not required
1466 * The mountinfo file shows not all mounts, if a few points have been
1467 * unmounted between read operations from the mountinfo. So we need to
1468 * read mountinfo a few times.
1470 * This loop can be skipped if a container uses userns, because all
1471 * inherited mounts are locked and we should live with all this trash.
1474 __do_fclose
FILE *f
= NULL
;
1475 __do_free
char *line
= NULL
;
1476 char *slider1
, *slider2
;
1480 f
= fopen("./proc/self/mountinfo", "re");
1482 return log_error_errno(-1, errno
, "Failed to open \"/proc/self/mountinfo\"");
1484 while (getline(&line
, &len
, f
) > 0) {
1485 for (slider1
= line
, i
= 0; slider1
&& i
< 4; i
++)
1486 slider1
= strchr(slider1
+ 1, ' ');
1491 slider2
= strchr(slider1
+ 1, ' ');
1498 if (strequal(slider1
+ 1, "/"))
1501 if (strequal(slider1
+ 1, "/proc"))
1504 ret
= umount2(slider1
, MNT_DETACH
);
1513 /* This also can be skipped if a container uses userns. */
1514 (void)umount2("./proc", MNT_DETACH
);
1516 /* It is weird, but chdir("..") moves us in a new root */
1519 return log_error_errno(-1, errno
, "Failed to chdir(\"..\")");
1523 return log_error_errno(-1, errno
, "Failed to chroot(\".\")");
1528 /* (The following explanation is copied verbatim from the kernel.)
1530 * pivot_root Semantics:
1531 * Moves the root file system of the current process to the directory put_old,
1532 * makes new_root as the new root file system of the current process, and sets
1533 * root/cwd of all processes which had them on the current root to new_root.
1536 * The new_root and put_old must be directories, and must not be on the
1537 * same file system as the current process root. The put_old must be
1538 * underneath new_root, i.e. adding a non-zero number of /.. to the string
1539 * pointed to by put_old must yield the same directory as new_root. No other
1540 * file system may be mounted on put_old. After all, new_root is a mountpoint.
1542 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
1543 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
1544 * in this situation.
1547 * - we don't move root/cwd if they are not at the root (reason: if something
1548 * cared enough to change them, it's probably wrong to force them elsewhere)
1549 * - it's okay to pick a root that isn't the root of a file system, e.g.
1550 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1551 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1554 static int lxc_pivot_root(const struct lxc_rootfs
*rootfs
)
1556 __do_close
int fd_oldroot
= -EBADF
;
1559 fd_oldroot
= open_at(-EBADF
, "/", PROTECT_OPATH_DIRECTORY
, PROTECT_LOOKUP_ABSOLUTE
, 0);
1561 return log_error_errno(-1, errno
, "Failed to open old root directory");
1563 /* change into new root fs */
1564 ret
= fchdir(rootfs
->dfd_mnt
);
1566 return log_error_errno(-errno
, errno
, "Failed to change into new root directory \"%s\"", rootfs
->mount
);
1568 /* pivot_root into our new root fs */
1569 ret
= pivot_root(".", ".");
1571 return log_error_errno(-errno
, errno
, "Failed to pivot into new root directory \"%s\"", rootfs
->mount
);
1573 /* At this point the old-root is mounted on top of our new-root. To
1574 * unmounted it we must not be chdir'd into it, so escape back to
1577 ret
= fchdir(fd_oldroot
);
1579 return log_error_errno(-errno
, errno
, "Failed to enter old root directory");
1582 * Make fd_oldroot a depedent mount to make sure our umounts don't
1583 * propagate to the host.
1585 ret
= mount("", ".", "", MS_SLAVE
| MS_REC
, NULL
);
1587 return log_error_errno(-errno
, errno
, "Failed to recursively turn old root mount tree into dependent mount");
1589 ret
= umount2(".", MNT_DETACH
);
1591 return log_error_errno(-errno
, errno
, "Failed to detach old root directory");
1593 ret
= fchdir(rootfs
->dfd_mnt
);
1595 return log_error_errno(-errno
, errno
, "Failed to re-enter new root directory \"%s\"", rootfs
->mount
);
1597 TRACE("Changed into new rootfs \"%s\"", rootfs
->mount
);
1601 static int lxc_setup_rootfs_switch_root(const struct lxc_rootfs
*rootfs
)
1604 return log_debug(0, "Container does not have a rootfs");
1606 if (detect_ramfs_rootfs())
1607 return lxc_chroot(rootfs
);
1609 return lxc_pivot_root(rootfs
);
1612 static const struct id_map
*find_mapped_nsid_entry(const struct lxc_conf
*conf
,
1617 struct id_map
*retmap
= NULL
;
1619 /* Shortcut for container's root mappings. */
1621 if (idtype
== ID_TYPE_UID
)
1622 return conf
->root_nsuid_map
;
1624 if (idtype
== ID_TYPE_GID
)
1625 return conf
->root_nsgid_map
;
1628 list_for_each_entry(map
, &conf
->id_map
, head
) {
1629 if (map
->idtype
!= idtype
)
1632 if (id
>= map
->nsid
&& id
< map
->nsid
+ map
->range
) {
1641 static int lxc_recv_devpts_from_child(struct lxc_handler
*handler
)
1645 if (handler
->conf
->pty_max
<= 0)
1648 ret
= lxc_abstract_unix_recv_one_fd(handler
->data_sock
[1],
1649 &handler
->conf
->devpts_fd
,
1650 &handler
->conf
->devpts_fd
,
1651 sizeof(handler
->conf
->devpts_fd
));
1653 return log_error_errno(-1, errno
, "Failed to receive devpts fd from child");
1655 TRACE("Received devpts file descriptor %d from child", handler
->conf
->devpts_fd
);
1659 static int lxc_setup_devpts_child(struct lxc_handler
*handler
)
1661 __do_close
int devpts_fd
= -EBADF
, fd_fs
= -EBADF
;
1662 struct lxc_conf
*conf
= handler
->conf
;
1663 struct lxc_rootfs
*rootfs
= &conf
->rootfs
;
1664 size_t pty_max
= conf
->pty_max
;
1667 pty_max
+= conf
->ttys
.max
;
1669 return log_debug(0, "No new devpts instance will be mounted since no pts devices are required");
1671 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
),
1672 "/proc/self/fd/%d/pts", rootfs
->dfd_dev
);
1674 return syserror("Failed to create path");
1676 (void)umount2(rootfs
->buf
, MNT_DETACH
);
1678 /* Create mountpoint for devpts instance. */
1679 ret
= mkdirat(rootfs
->dfd_dev
, "pts", 0755);
1680 if (ret
< 0 && errno
!= EEXIST
)
1681 return log_error_errno(-1, errno
, "Failed to create \"/dev/pts\" directory");
1683 if (can_use_mount_api()) {
1684 fd_fs
= fs_prepare("devpts", -EBADF
, "", 0, 0);
1686 return syserror("Failed to prepare filesystem context for devpts");
1688 ret
= fs_set_property(fd_fs
, "source", "devpts");
1690 SYSTRACE("Failed to set \"source=devpts\" on devpts filesystem context %d", fd_fs
);
1692 ret
= fs_set_property(fd_fs
, "gid", "5");
1694 SYSTRACE("Failed to set \"gid=5\" on devpts filesystem context %d", fd_fs
);
1696 ret
= fs_set_flag(fd_fs
, "newinstance");
1698 return syserror("Failed to set \"newinstance\" property on devpts filesystem context %d", fd_fs
);
1700 ret
= fs_set_property(fd_fs
, "ptmxmode", "0666");
1702 return syserror("Failed to set \"ptmxmode=0666\" property on devpts filesystem context %d", fd_fs
);
1704 ret
= fs_set_property(fd_fs
, "mode", "0620");
1706 return syserror("Failed to set \"mode=0620\" property on devpts filesystem context %d", fd_fs
);
1708 ret
= fs_set_property(fd_fs
, "max", fdstr(pty_max
));
1710 return syserror("Failed to set \"max=%zu\" property on devpts filesystem context %d", conf
->pty_max
, fd_fs
);
1712 ret
= fsconfig(fd_fs
, FSCONFIG_CMD_CREATE
, NULL
, NULL
, 0);
1714 return syserror("Failed to finalize filesystem context %d", fd_fs
);
1716 devpts_fd
= fsmount(fd_fs
, FSMOUNT_CLOEXEC
, MOUNT_ATTR_NOSUID
| MOUNT_ATTR_NOEXEC
);
1718 return syserror("Failed to create new mount for filesystem context %d", fd_fs
);
1719 TRACE("Created detached devpts mount %d", devpts_fd
);
1721 ret
= move_mount(devpts_fd
, "", rootfs
->dfd_dev
, "pts", MOVE_MOUNT_F_EMPTY_PATH
);
1723 return syserror("Failed to attach devpts mount %d to %d/pts", conf
->devpts_fd
, rootfs
->dfd_dev
);
1725 DEBUG("Attached detached devpts mount %d to %d/pts", devpts_fd
, rootfs
->dfd_dev
);
1728 char devpts_mntopts
[256];
1729 char *mntopt_sets
[5];
1730 char default_devpts_mntopts
[256] = "gid=5,newinstance,ptmxmode=0666,mode=0620";
1733 * Fallback codepath in case the new mount API can't be used to
1734 * create detached mounts.
1737 ret
= strnprintf(devpts_mntopts
, sizeof(devpts_mntopts
), "%s,max=%zu",
1738 default_devpts_mntopts
, pty_max
);
1742 /* Create mountpoint for devpts instance. */
1743 ret
= mkdirat(rootfs
->dfd_dev
, "pts", 0755);
1744 if (ret
< 0 && errno
!= EEXIST
)
1745 return log_error_errno(-1, errno
, "Failed to create \"/dev/pts\" directory");
1748 mntopt_sets
[0] = devpts_mntopts
;
1750 /* !gid=5 && max= */
1751 mntopt_sets
[1] = devpts_mntopts
+ STRLITERALLEN("gid=5") + 1;
1753 /* gid=5 && !max= */
1754 mntopt_sets
[2] = default_devpts_mntopts
;
1756 /* !gid=5 && !max= */
1757 mntopt_sets
[3] = default_devpts_mntopts
+ STRLITERALLEN("gid=5") + 1;
1760 mntopt_sets
[4] = NULL
;
1762 for (ret
= -1, opts
= mntopt_sets
; opts
&& *opts
; opts
++) {
1763 /* mount new devpts instance */
1764 ret
= mount_at(rootfs
->dfd_dev
, "", 0,
1765 rootfs
->dfd_dev
, "pts", PROTECT_LOOKUP_BENEATH
,
1766 "devpts", MS_NOSUID
| MS_NOEXEC
, *opts
);
1771 return log_error_errno(-1, errno
, "Failed to mount new devpts instance");
1773 devpts_fd
= open_at(rootfs
->dfd_dev
, "pts", PROTECT_OPATH_DIRECTORY
, PROTECT_LOOKUP_BENEATH_XDEV
, 0);
1774 if (devpts_fd
< 0) {
1776 TRACE("Failed to create detached devpts mount");
1779 DEBUG("Mounted new devpts instance with options \"%s\"", *opts
);
1782 handler
->conf
->devpts_fd
= move_fd(devpts_fd
);
1785 * In order to allocate terminal devices the devpts filesystem will
1786 * have to be attached to the filesystem at least ones in the new mount
1787 * api. The reason is lengthy but the gist is that until the new mount
1788 * has been attached to the filesystem it is a detached mount with an
1789 * anonymous mount mamespace attached to it for which the kernel
1790 * refuses certain operations.
1791 * We end up here if the user has requested to allocate tty devices
1792 * while not requestig pty devices be made available to the container.
1793 * We only need the devpts_fd to allocate tty devices.
1795 if (conf
->pty_max
<= 0)
1798 /* Remove any pre-existing /dev/ptmx file. */
1799 ret
= unlinkat(rootfs
->dfd_dev
, "ptmx", 0);
1801 if (errno
!= ENOENT
)
1802 return log_error_errno(-1, errno
, "Failed to remove existing \"/dev/ptmx\" file");
1804 DEBUG("Removed existing \"/dev/ptmx\" file");
1807 /* Create placeholder /dev/ptmx file as bind mountpoint for /dev/pts/ptmx. */
1808 ret
= mknodat(rootfs
->dfd_dev
, "ptmx", S_IFREG
| 0000, 0);
1809 if (ret
< 0 && errno
!= EEXIST
)
1810 return log_error_errno(-1, errno
, "Failed to create \"/dev/ptmx\" file as bind mount target");
1811 DEBUG("Created \"/dev/ptmx\" file as bind mount target");
1813 /* Main option: use a bind-mount to please AppArmor */
1814 ret
= mount_at(rootfs
->dfd_dev
, "pts/ptmx", (PROTECT_LOOKUP_BENEATH_WITH_SYMLINKS
& ~RESOLVE_NO_XDEV
),
1815 rootfs
->dfd_dev
, "ptmx", (PROTECT_LOOKUP_BENEATH_WITH_SYMLINKS
& ~RESOLVE_NO_XDEV
),
1816 NULL
, MS_BIND
, NULL
);
1818 return log_debug(0, "Bind mounted \"/dev/pts/ptmx\" to \"/dev/ptmx\"");
1820 /* Fallthrough and try to create a symlink. */
1821 ERROR("Failed to bind mount \"/dev/pts/ptmx\" to \"/dev/ptmx\"");
1823 /* Remove the placeholder /dev/ptmx file we created above. */
1824 ret
= unlinkat(rootfs
->dfd_dev
, "ptmx", 0);
1826 return log_error_errno(-1, errno
, "Failed to remove existing \"/dev/ptmx\"");
1828 /* Fallback option: Create symlink /dev/ptmx -> /dev/pts/ptmx. */
1829 ret
= symlinkat("/dev/pts/ptmx", rootfs
->dfd_dev
, "dev/ptmx");
1831 return log_error_errno(-1, errno
, "Failed to create symlink from \"/dev/ptmx\" to \"/dev/pts/ptmx\"");
1833 DEBUG("Created symlink from \"/dev/ptmx\" to \"/dev/pts/ptmx\"");
1837 static int lxc_finish_devpts_child(struct lxc_handler
*handler
)
1839 struct lxc_conf
*conf
= handler
->conf
;
1840 struct lxc_rootfs
*rootfs
= &conf
->rootfs
;
1843 if (conf
->pty_max
> 0)
1847 * We end up here if the user has requested to allocate tty devices
1848 * while not requestig pty devices be made available to the container.
1849 * This means we can unmount the devpts instance. We only need the
1850 * devpts_fd to allocate tty devices.
1852 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
),
1853 "/proc/self/fd/%d/pts", rootfs
->dfd_dev
);
1855 return syserror("Failed to create path");
1857 close_prot_errno_disarm(conf
->devpts_fd
);
1858 return umount2(rootfs
->buf
, MNT_DETACH
);
1861 static int lxc_send_devpts_to_parent(struct lxc_handler
*handler
)
1865 if (handler
->conf
->pty_max
<= 0)
1866 return log_debug(0, "No devpts file descriptor will be sent since no pts devices are requested");
1868 ret
= lxc_abstract_unix_send_fds(handler
->data_sock
[0], &handler
->conf
->devpts_fd
, 1, NULL
, 0);
1870 SYSERROR("Failed to send devpts file descriptor %d to parent", handler
->conf
->devpts_fd
);
1872 TRACE("Sent devpts file descriptor %d to parent", handler
->conf
->devpts_fd
);
1874 close_prot_errno_disarm(handler
->conf
->devpts_fd
);
1879 static int setup_personality(personality_t persona
)
1883 if (persona
== LXC_ARCH_UNCHANGED
)
1884 return log_debug(0, "Retaining original personality");
1886 ret
= lxc_personality(persona
);
1888 return syserror("Failed to set personality to \"0lx%lx\"", persona
);
1890 INFO("Set personality to \"0lx%lx\"", persona
);
1894 static int bind_mount_console(int fd_devpts
, struct lxc_rootfs
*rootfs
,
1895 struct lxc_terminal
*console
, int fd_to
)
1897 __do_close
int fd_pty
= -EBADF
;
1899 if (is_empty_string(console
->name
))
1900 return ret_errno(EINVAL
);
1903 * When the pty fd stashed in console->pty has been retrieved via the
1904 * TIOCGPTPEER ioctl() to avoid dangerous path-based lookups when
1905 * allocating new pty devices we can't reopen it through openat2() or
1906 * created a detached mount through open_tree() from it. This means we
1907 * would need to mount using the path stased in console->name which is
1908 * unsafe. We could be mounting a device that isn't identical to the
1909 * one we've already safely opened and stashed in console->pty.
1910 * So, what we do is we open an O_PATH file descriptor for
1911 * console->name and verify that the opened fd and the fd we stashed in
1912 * console->pty refer to the same device. If they do we can go on and
1913 * created a detached mount based on the newly opened O_PATH file
1914 * descriptor and then safely mount.
1916 fd_pty
= open_at_same(console
->pty
, fd_devpts
, fdstr(console
->pty_nr
),
1917 PROTECT_OPATH_FILE
, PROTECT_LOOKUP_ABSOLUTE_XDEV
, 0);
1919 return syserror("Failed to open \"%s\"", console
->name
);
1922 * Note, there are intentionally no open or lookup restrictions since
1923 * we're operating directly on the fd.
1925 if (can_use_mount_api())
1926 return fd_bind_mount(fd_pty
, "", 0, 0, fd_to
, "", 0, 0, 0, 0, 0, false);
1928 return mount_fd(fd_pty
, fd_to
, "none", MS_BIND
, 0);
1931 static int lxc_setup_dev_console(int fd_devpts
, struct lxc_rootfs
*rootfs
,
1932 struct lxc_terminal
*console
)
1934 __do_close
int fd_console
= -EBADF
;
1938 * When we are asked to setup a console we remove any previous
1939 * /dev/console bind-mounts.
1941 if (exists_file_at(rootfs
->dfd_dev
, "console")) {
1942 char *rootfs_path
= rootfs
->path
? rootfs
->mount
: "";
1944 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "%s/dev/console", rootfs_path
);
1948 ret
= lxc_unstack_mountpoint(rootfs
->buf
, false);
1950 return log_error_errno(-ret
, errno
, "Failed to unmount \"%s\"", rootfs
->buf
);
1952 DEBUG("Cleared all (%d) mounts from \"%s\"", ret
, rootfs
->buf
);
1956 * For unprivileged containers autodev or automounts will already have
1957 * taken care of creating /dev/console.
1959 fd_console
= open_at(rootfs
->dfd_dev
,
1961 PROTECT_OPEN
| O_CREAT
,
1962 PROTECT_LOOKUP_BENEATH
,
1965 return syserror("Failed to create \"%d/console\"", rootfs
->dfd_dev
);
1967 ret
= fchmod(console
->pty
, 0620);
1969 return syserror("Failed to change console mode");
1971 ret
= bind_mount_console(fd_devpts
, rootfs
, console
, fd_console
);
1973 return syserror("Failed to mount \"%d(%s)\" on \"%d\"",
1974 console
->pty
, console
->name
, fd_console
);
1976 TRACE("Setup console \"%s\"", console
->name
);
1980 static int lxc_setup_ttydir_console(int fd_devpts
, struct lxc_rootfs
*rootfs
,
1981 struct lxc_terminal
*console
,
1984 __do_close
int fd_ttydir
= -EBADF
, fd_dev_console
= -EBADF
,
1985 fd_reg_console
= -EBADF
, fd_reg_ttydir_console
= -EBADF
;
1988 /* create dev/<ttydir> */
1989 ret
= mkdirat(rootfs
->dfd_dev
, ttydir
, 0755);
1990 if (ret
< 0 && errno
!= EEXIST
)
1991 return syserror("Failed to create \"%d/%s\"", rootfs
->dfd_dev
, ttydir
);
1993 fd_ttydir
= open_at(rootfs
->dfd_dev
,
1995 PROTECT_OPATH_DIRECTORY
,
1996 PROTECT_LOOKUP_BENEATH
,
1999 return syserror("Failed to open \"%d/%s\"", rootfs
->dfd_dev
, ttydir
);
2001 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "%s/console", ttydir
);
2005 /* create dev/<ttydir>/console */
2006 fd_reg_ttydir_console
= open_at(fd_ttydir
,
2008 PROTECT_OPEN
| O_CREAT
,
2009 PROTECT_LOOKUP_BENEATH
,
2011 if (fd_reg_ttydir_console
< 0)
2012 return syserror("Failed to create \"%d/console\"", fd_ttydir
);
2014 if (file_exists(rootfs
->buf
)) {
2015 char *rootfs_path
= rootfs
->path
? rootfs
->mount
: "";
2017 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "%s/dev/console", rootfs_path
);
2021 ret
= lxc_unstack_mountpoint(rootfs
->buf
, false);
2023 return log_error_errno(-ret
, errno
, "Failed to unmount \"%s\"", rootfs
->buf
);
2025 DEBUG("Cleared all (%d) mounts from \"%s\"", ret
, rootfs
->buf
);
2028 /* create dev/console */
2029 fd_reg_console
= open_at(rootfs
->dfd_dev
,
2031 PROTECT_OPEN
| O_CREAT
,
2032 PROTECT_LOOKUP_BENEATH
,
2034 if (fd_reg_console
< 0)
2035 return syserror("Failed to create \"%d/console\"", rootfs
->dfd_dev
);
2037 ret
= fchmod(console
->pty
, 0620);
2039 return syserror("Failed to change console mode");
2041 /* bind mount console to '/dev/<ttydir>/console' */
2042 ret
= bind_mount_console(fd_devpts
, rootfs
, console
, fd_reg_ttydir_console
);
2044 return syserror("Failed to mount \"%d(%s)\" on \"%d\"",
2045 console
->pty
, console
->name
, fd_reg_ttydir_console
);
2047 fd_dev_console
= open_at_same(console
->pty
,
2051 PROTECT_LOOKUP_BENEATH_XDEV
,
2053 if (fd_dev_console
< 0)
2054 return syserror("Failed to open \"%d/console\"", fd_ttydir
);
2056 /* bind mount '/dev/<ttydir>/console' to '/dev/console' */
2057 if (can_use_mount_api())
2058 ret
= fd_bind_mount(fd_dev_console
,
2061 PROTECT_LOOKUP_BENEATH_XDEV
,
2065 PROTECT_LOOKUP_BENEATH
,
2071 ret
= mount_fd(fd_dev_console
, fd_reg_console
, "none", MS_BIND
, 0);
2073 return syserror("Failed to mount \"%d\" on \"%d\"",
2074 fd_dev_console
, fd_reg_console
);
2076 TRACE("Setup console \"%s\"", console
->name
);
2080 static int lxc_setup_console(const struct lxc_handler
*handler
,
2081 struct lxc_rootfs
*rootfs
,
2082 struct lxc_terminal
*console
, char *ttydir
)
2084 __do_close
int fd_devpts_host
= -EBADF
;
2085 int fd_devpts
= handler
->conf
->devpts_fd
;
2088 if (!wants_console(console
))
2089 return log_trace(0, "Skipping console setup");
2091 if (console
->pty
< 0) {
2093 * Allocate a console from the container's devpts instance. We
2094 * have checked on the host that we have enough pty devices
2097 ret
= lxc_devpts_terminal(handler
->conf
->devpts_fd
, &console
->ptx
,
2098 &console
->pty
, &console
->pty_nr
, false);
2100 return syserror("Failed to allocate console from container's devpts instance");
2102 ret
= strnprintf(console
->name
, sizeof(console
->name
),
2103 "/dev/pts/%d", console
->pty_nr
);
2105 return syserror("Failed to create console path");
2108 * We're using a console from the host's devpts instance. Open
2109 * it again so we can later verify that the console we're
2110 * supposed to use is still the same as the one we opened on
2113 fd_devpts_host
= open_at(rootfs
->dfd_host
,
2115 PROTECT_OPATH_DIRECTORY
,
2116 PROTECT_LOOKUP_BENEATH_XDEV
,
2118 if (fd_devpts_host
< 0)
2119 return syserror("Failed to open host devpts");
2121 fd_devpts
= fd_devpts_host
;
2125 ret
= lxc_setup_ttydir_console(fd_devpts
, rootfs
, console
, ttydir
);
2127 ret
= lxc_setup_dev_console(fd_devpts
, rootfs
, console
);
2129 return syserror("Failed to setup console");
2132 * Some init's such as busybox will set sane tty settings on stdin,
2133 * stdout, stderr which it thinks is the console. We already set them
2134 * the way we wanted on the real terminal, and we want init to do its
2135 * setup on its console ie. the pty allocated in lxc_terminal_setup() so
2136 * make sure that that pty is stdin,stdout,stderr.
2138 if (console
->pty
>= 0) {
2139 if (handler
->daemonize
|| !handler
->conf
->is_execute
)
2140 ret
= set_stdfds(console
->pty
);
2142 ret
= lxc_terminal_set_stdfds(console
->pty
);
2144 return syserror("Failed to redirect std{in,out,err} to pty file descriptor %d", console
->pty
);
2147 * If the console has been allocated from the host's devpts
2148 * we're done and we don't need to send fds to the parent.
2150 if (fd_devpts_host
>= 0)
2151 lxc_terminal_delete(console
);
2157 static int parse_mntopt(char *opt
, unsigned long *flags
, char **data
, size_t size
)
2161 /* If '=' is contained in opt, the option must go into data. */
2162 if (!strchr(opt
, '=')) {
2164 * If opt is found in mount_opt, set or clear flags.
2165 * Otherwise append it to data.
2167 size_t opt_len
= strlen(opt
);
2168 for (struct mount_opt
*mo
= &mount_opt
[0]; mo
->name
!= NULL
; mo
++) {
2169 size_t mo_name_len
= strlen(mo
->name
);
2171 if (opt_len
== mo_name_len
&& strnequal(opt
, mo
->name
, mo_name_len
)) {
2173 *flags
&= ~mo
->legacy_flag
;
2175 *flags
|= mo
->legacy_flag
;
2181 if (strlen(*data
)) {
2182 ret
= strlcat(*data
, ",", size
);
2184 return log_error_errno(ret
, errno
, "Failed to append \",\" to %s", *data
);
2187 ret
= strlcat(*data
, opt
, size
);
2189 return log_error_errno(ret
, errno
, "Failed to append \"%s\" to %s", opt
, *data
);
2194 int parse_mntopts_legacy(const char *mntopts
, unsigned long *mntflags
, char **mntdata
)
2196 __do_free
char *mntopts_new
= NULL
, *mntopts_dup
= NULL
;
2197 char *mntopt_cur
= NULL
;
2200 if (*mntdata
|| *mntflags
)
2201 return ret_errno(EINVAL
);
2206 mntopts_dup
= strdup(mntopts
);
2208 return ret_errno(ENOMEM
);
2210 size
= strlen(mntopts_dup
) + 1;
2211 mntopts_new
= zalloc(size
);
2213 return ret_errno(ENOMEM
);
2215 lxc_iterate_parts(mntopt_cur
, mntopts_dup
, ",")
2216 if (parse_mntopt(mntopt_cur
, mntflags
, &mntopts_new
, size
) < 0)
2217 return ret_errno(EINVAL
);
2220 *mntdata
= move_ptr(mntopts_new
);
2225 static int parse_vfs_attr(struct lxc_mount_options
*opts
, char *opt
, size_t size
)
2228 * If opt is found in mount_opt, set or clear flags.
2229 * Otherwise append it to data.
2231 for (struct mount_opt
*mo
= &mount_opt
[0]; mo
->name
!= NULL
; mo
++) {
2232 if (!strnequal(opt
, mo
->name
, strlen(mo
->name
)))
2235 /* This is a recursive bind-mount. */
2236 if (strequal(mo
->name
, "rbind")) {
2237 opts
->bind_recursively
= 1;
2239 opts
->mnt_flags
|= mo
->legacy_flag
; /* MS_BIND | MS_REC */
2243 /* This is a bind-mount. */
2244 if (strequal(mo
->name
, "bind")) {
2246 opts
->mnt_flags
|= mo
->legacy_flag
; /* MS_BIND */
2251 return log_info(0, "Ignoring %s mount option", mo
->name
);
2254 opts
->attr
.attr_clr
|= mo
->flag
;
2255 opts
->mnt_flags
&= ~mo
->legacy_flag
;
2256 TRACE("Lowering %s", mo
->name
);
2258 opts
->attr
.attr_set
|= mo
->flag
;
2259 opts
->mnt_flags
|= mo
->legacy_flag
;
2260 TRACE("Raising %s", mo
->name
);
2266 for (struct mount_opt
*mo
= &propagation_opt
[0]; mo
->name
!= NULL
; mo
++) {
2267 if (!strnequal(opt
, mo
->name
, strlen(mo
->name
)))
2270 if (strequal(mo
->name
, "rslave") ||
2271 strequal(mo
->name
, "rshared") ||
2272 strequal(mo
->name
, "runbindable") ||
2273 strequal(mo
->name
, "rprivate"))
2274 opts
->propagate_recursively
= 1;
2276 opts
->attr
.propagation
= mo
->flag
;
2277 opts
->prop_flags
|= mo
->legacy_flag
;
2284 int parse_mount_attrs(struct lxc_mount_options
*opts
, const char *mntopts
)
2286 __do_free
char *mntopts_new
= NULL
, *mntopts_dup
= NULL
;
2287 char *end
= NULL
, *mntopt_cur
= NULL
;
2292 return ret_errno(EINVAL
);
2297 mntopts_dup
= strdup(mntopts
);
2299 return ret_errno(ENOMEM
);
2301 size
= strlen(mntopts_dup
) + 1;
2302 mntopts_new
= zalloc(size
);
2304 return ret_errno(ENOMEM
);
2306 lxc_iterate_parts(mntopt_cur
, mntopts_dup
, ",") {
2307 /* This is a filesystem specific option. */
2308 if (strchr(mntopt_cur
, '=')) {
2310 end
= stpcpy(mntopts_new
, mntopt_cur
);
2312 end
= stpcpy(end
, ",");
2313 end
= stpcpy(end
, mntopt_cur
);
2319 /* This is a generic vfs option. */
2320 ret
= parse_vfs_attr(opts
, mntopt_cur
, size
);
2322 return syserror("Failed to parse mount attributes: \"%s\"", mntopt_cur
);
2326 opts
->data
= move_ptr(mntopts_new
);
2331 static void null_endofword(char *word
)
2333 while (*word
&& *word
!= ' ' && *word
!= '\t')
2338 /* skip @nfields spaces in @src */
2339 static char *get_field(char *src
, int nfields
)
2344 for (i
= 0; i
< nfields
; i
++) {
2345 while (*p
&& *p
!= ' ' && *p
!= '\t')
2357 static int mount_entry(const char *fsname
, const char *target
,
2358 const char *fstype
, unsigned long mountflags
,
2359 unsigned long pflags
, const char *data
, bool optional
,
2360 bool dev
, bool relative
, const char *rootfs
)
2363 char srcbuf
[PATH_MAX
];
2364 const char *srcpath
= fsname
;
2370 ret
= strnprintf(srcbuf
, sizeof(srcbuf
), "%s/%s", rootfs
? rootfs
: "/", fsname
? fsname
: "");
2372 return log_error_errno(-1, errno
, "source path is too long");
2376 ret
= safe_mount(srcpath
, target
, fstype
, mountflags
& ~MS_REMOUNT
, data
,
2380 return log_info_errno(0, errno
, "Failed to mount \"%s\" on \"%s\" (optional)",
2381 srcpath
? srcpath
: "(null)", target
);
2383 return log_error_errno(-1, errno
, "Failed to mount \"%s\" on \"%s\"",
2384 srcpath
? srcpath
: "(null)", target
);
2387 if ((mountflags
& MS_REMOUNT
) || (mountflags
& MS_BIND
)) {
2389 DEBUG("Remounting \"%s\" on \"%s\" to respect bind or remount options",
2390 srcpath
? srcpath
: "(none)", target
? target
: "(none)");
2393 if (srcpath
&& statvfs(srcpath
, &sb
) == 0) {
2394 unsigned long required_flags
= 0;
2396 if (sb
.f_flag
& MS_NOSUID
)
2397 required_flags
|= MS_NOSUID
;
2399 if (sb
.f_flag
& MS_NODEV
&& !dev
)
2400 required_flags
|= MS_NODEV
;
2402 if (sb
.f_flag
& MS_RDONLY
)
2403 required_flags
|= MS_RDONLY
;
2405 if (sb
.f_flag
& MS_NOEXEC
)
2406 required_flags
|= MS_NOEXEC
;
2408 DEBUG("Flags for \"%s\" were %lu, required extra flags are %lu",
2409 srcpath
, sb
.f_flag
, required_flags
);
2411 /* If this was a bind mount request, and required_flags
2412 * does not have any flags which are not already in
2413 * mountflags, then skip the remount.
2415 if (!(mountflags
& MS_REMOUNT
) &&
2416 (!(required_flags
& ~mountflags
) && !(mountflags
& MS_RDONLY
))) {
2417 DEBUG("Mountflags already were %lu, skipping remount", mountflags
);
2421 mountflags
|= required_flags
;
2425 ret
= mount(srcpath
, target
, fstype
, mountflags
| MS_REMOUNT
, data
);
2428 return log_info_errno(0, errno
, "Failed to mount \"%s\" on \"%s\" (optional)",
2429 srcpath
? srcpath
: "(null)",
2432 return log_error_errno(-1, errno
, "Failed to mount \"%s\" on \"%s\"",
2433 srcpath
? srcpath
: "(null)",
2442 ret
= mount(NULL
, target
, NULL
, pflags
, NULL
);
2445 return log_info_errno(0, errno
, "Failed to change mount propagation for \"%s\" (optional)", target
);
2447 return log_error_errno(-1, errno
, "Failed to change mount propagation for \"%s\" (optional)", target
);
2449 DEBUG("Changed mount propagation for \"%s\"", target
);
2452 DEBUG("Mounted \"%s\" on \"%s\" with filesystem type \"%s\"",
2453 srcpath
? srcpath
: "(null)", target
, fstype
);
2458 const char *lxc_mount_options_info
[LXC_MOUNT_MAX
] = {
2466 /* Remove "optional", "create=dir", and "create=file" from mntopt */
2467 int parse_lxc_mount_attrs(struct lxc_mount_options
*opts
, char *mnt_opts
)
2469 for (size_t i
= LXC_MOUNT_CREATE_DIR
; i
< LXC_MOUNT_MAX
; i
++) {
2470 __do_close
int fd_userns
= -EBADF
;
2471 const char *opt_name
= lxc_mount_options_info
[i
];
2473 char *idmap_path
, *opt
, *opt_next
;
2475 opt
= strstr(mnt_opts
, opt_name
);
2480 case LXC_MOUNT_CREATE_DIR
:
2481 opts
->create_dir
= 1;
2483 case LXC_MOUNT_CREATE_FILE
:
2484 opts
->create_file
= 1;
2486 case LXC_MOUNT_OPTIONAL
:
2489 case LXC_MOUNT_RELATIVE
:
2492 case LXC_MOUNT_IDMAP
:
2494 opt_next
+= STRLITERALLEN("idmap=");
2495 idmap_path
= strchrnul(opt_next
, ',');
2496 len
= idmap_path
- opt_next
+ 1;
2498 if (len
>= sizeof(opts
->userns_path
))
2499 return syserror_set(-EIO
, "Excessive idmap path length for \"idmap=<path>\" LXC specific mount option");
2501 strlcpy(opts
->userns_path
, opt_next
, len
);
2503 if (is_empty_string(opts
->userns_path
))
2504 return syserror_set(-EINVAL
, "Missing idmap path for \"idmap=<path>\" LXC specific mount option");
2506 if (!strequal(opts
->userns_path
, "container")) {
2507 fd_userns
= open(opts
->userns_path
, O_RDONLY
| O_NOCTTY
| O_CLOEXEC
);
2509 return syserror("Failed to open user namespace %s", opts
->userns_path
);
2512 TRACE("Parse LXC specific mount option %d->\"idmap=%s\"", fd_userns
, opts
->userns_path
);
2515 return syserror_set(-EINVAL
, "Unknown LXC specific mount option");
2518 opt_next
= strchr(opt
, ',');
2520 *opt
= '\0'; /* no more mntopts, so just chop it here */
2522 memmove(opt
, opt_next
+ 1, strlen(opt_next
+ 1) + 1);
2528 static int mount_entry_create_dir_file(const struct mntent
*mntent
,
2530 const struct lxc_rootfs
*rootfs
,
2531 const char *lxc_name
, const char *lxc_path
)
2533 __do_free
char *p1
= NULL
;
2537 if (strnequal(mntent
->mnt_type
, "overlay", 7)) {
2538 ret
= ovl_mkdir(mntent
, rootfs
, lxc_name
, lxc_path
);
2543 if (hasmntopt(mntent
, "create=dir")) {
2544 ret
= mkdir_p(path
, 0755);
2545 if (ret
< 0 && errno
!= EEXIST
)
2546 return log_error_errno(-1, errno
, "Failed to create directory \"%s\"", path
);
2549 if (!hasmntopt(mntent
, "create=file"))
2552 ret
= access(path
, F_OK
);
2562 ret
= mkdir_p(p2
, 0755);
2563 if (ret
< 0 && errno
!= EEXIST
)
2564 return log_error_errno(-1, errno
, "Failed to create directory \"%s\"", path
);
2566 ret
= mknod(path
, S_IFREG
| 0000, 0);
2567 if (ret
< 0 && errno
!= EEXIST
)
2573 /* rootfs, lxc_name, and lxc_path can be NULL when the container is created
2574 * without a rootfs. */
2575 static inline int mount_entry_on_generic(struct mntent
*mntent
,
2577 const struct lxc_rootfs
*rootfs
,
2578 const char *lxc_name
,
2579 const char *lxc_path
)
2581 __do_free
char *mntdata
= NULL
;
2582 char *rootfs_path
= NULL
;
2584 bool dev
, optional
, relative
;
2585 struct lxc_mount_options opts
= {};
2587 optional
= hasmntopt(mntent
, "optional") != NULL
;
2588 dev
= hasmntopt(mntent
, "dev") != NULL
;
2589 relative
= hasmntopt(mntent
, "relative") != NULL
;
2591 if (rootfs
&& rootfs
->path
)
2592 rootfs_path
= rootfs
->mount
;
2594 ret
= mount_entry_create_dir_file(mntent
, path
, rootfs
, lxc_name
,
2603 ret
= parse_lxc_mount_attrs(&opts
, mntent
->mnt_opts
);
2608 * Idmapped mount entries will be setup by the parent for us. Note that
2609 * we rely on mount_entry_create_dir_file() above to have already
2610 * created the target path for us. So the parent can just open the
2611 * target and send us the target fd.
2614 if (!is_empty_string(opts
.userns_path
))
2615 return systrace_ret(0, "Skipping idmapped mount entry");
2617 ret
= parse_mount_attrs(&opts
, mntent
->mnt_opts
);
2621 ret
= mount_entry(mntent
->mnt_fsname
,
2635 static inline int mount_entry_on_systemfs(struct lxc_rootfs
*rootfs
,
2636 struct mntent
*mntent
)
2640 /* For containers created without a rootfs all mounts are treated as
2641 * absolute paths starting at / on the host.
2643 if (mntent
->mnt_dir
[0] != '/')
2644 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "/%s", mntent
->mnt_dir
);
2646 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "%s", mntent
->mnt_dir
);
2650 return mount_entry_on_generic(mntent
, rootfs
->buf
, NULL
, NULL
, NULL
);
2653 static int mount_entry_on_absolute_rootfs(struct mntent
*mntent
,
2654 struct lxc_rootfs
*rootfs
,
2655 const char *lxc_name
,
2656 const char *lxc_path
)
2660 const char *lxcpath
;
2663 lxcpath
= lxc_global_config_value("lxc.lxcpath");
2667 /* If rootfs->path is a blockdev path, allow container fstab to use
2668 * <lxcpath>/<name>/rootfs" as the target prefix.
2670 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "%s/%s/rootfs", lxcpath
, lxc_name
);
2674 aux
= strstr(mntent
->mnt_dir
, rootfs
->buf
);
2676 offset
= strlen(rootfs
->buf
);
2681 aux
= strstr(mntent
->mnt_dir
, rootfs
->path
);
2683 return log_warn(ret
, "Ignoring mount point \"%s\"", mntent
->mnt_dir
);
2684 offset
= strlen(rootfs
->path
);
2687 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "%s/%s", rootfs
->mount
, aux
+ offset
);
2691 return mount_entry_on_generic(mntent
, rootfs
->buf
, rootfs
, lxc_name
, lxc_path
);
2694 static int mount_entry_on_relative_rootfs(struct mntent
*mntent
,
2695 struct lxc_rootfs
*rootfs
,
2696 const char *lxc_name
,
2697 const char *lxc_path
)
2701 /* relative to root mount point */
2702 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "%s/%s", rootfs
->mount
, mntent
->mnt_dir
);
2706 return mount_entry_on_generic(mntent
, rootfs
->buf
, rootfs
, lxc_name
, lxc_path
);
2709 static int mount_file_entries(struct lxc_rootfs
*rootfs
, FILE *file
,
2710 const char *lxc_name
, const char *lxc_path
)
2713 struct mntent mntent
;
2715 while (getmntent_r(file
, &mntent
, buf
, sizeof(buf
))) {
2719 ret
= mount_entry_on_systemfs(rootfs
, &mntent
);
2720 else if (mntent
.mnt_dir
[0] != '/')
2721 ret
= mount_entry_on_relative_rootfs(&mntent
, rootfs
,
2722 lxc_name
, lxc_path
);
2724 ret
= mount_entry_on_absolute_rootfs(&mntent
, rootfs
,
2725 lxc_name
, lxc_path
);
2730 if (!feof(file
) || ferror(file
))
2731 return log_error(-1, "Failed to parse mount entries");
2736 static inline void __auto_endmntent__(FILE **f
)
2742 #define __do_endmntent __attribute__((__cleanup__(__auto_endmntent__)))
2744 static int setup_mount_fstab(struct lxc_rootfs
*rootfs
, const char *fstab
,
2745 const char *lxc_name
, const char *lxc_path
)
2747 __do_endmntent
FILE *f
= NULL
;
2753 f
= setmntent(fstab
, "re");
2755 return log_error_errno(-1, errno
, "Failed to open \"%s\"", fstab
);
2757 ret
= mount_file_entries(rootfs
, f
, lxc_name
, lxc_path
);
2759 ERROR("Failed to set up mount entries");
2765 * In order for nested containers to be able to mount /proc and /sys they need
2766 * to see a "pure" proc and sysfs mount points with nothing mounted on top
2768 * For this we provide proc and sysfs in /dev/.lxc/{proc,sys} while using an
2769 * apparmor rule to deny access to them. This is mostly for convenience: The
2770 * container's root user can mount them anyway and thus has access to the two
2771 * file systems. But a non-root user in the container should not be allowed to
2772 * access them as a side effect without explicitly allowing it.
2774 static const char nesting_helpers
[] =
2775 "proc dev/.lxc/proc proc create=dir,optional 0 0\n"
2776 "sys dev/.lxc/sys sysfs create=dir,optional 0 0\n";
2778 FILE *make_anonymous_mount_file(const struct list_head
*mount_entries
,
2779 bool include_nesting_helpers
)
2781 __do_close
int fd
= -EBADF
;
2784 struct string_entry
*entry
;
2786 fd
= memfd_create(".lxc_mount_file", MFD_CLOEXEC
);
2788 char template[] = P_tmpdir
"/.lxc_mount_file_XXXXXX";
2790 if (errno
!= ENOSYS
)
2793 fd
= lxc_make_tmpfile(template, true);
2795 return log_error_errno(NULL
, errno
, "Could not create temporary mount file");
2797 TRACE("Created temporary mount file");
2800 list_for_each_entry(entry
, mount_entries
, head
) {
2803 len
= strlen(entry
->val
);
2805 ret
= lxc_write_nointr(fd
, entry
->val
, len
);
2809 ret
= lxc_write_nointr(fd
, "\n", 1);
2814 if (include_nesting_helpers
) {
2815 ret
= lxc_write_nointr(fd
, nesting_helpers
,
2816 STRARRAYLEN(nesting_helpers
));
2817 if (ret
!= STRARRAYLEN(nesting_helpers
))
2821 ret
= lseek(fd
, 0, SEEK_SET
);
2825 f
= fdopen(fd
, "re+");
2827 move_fd(fd
); /* Transfer ownership of fd. */
2831 static int setup_mount_entries(const struct lxc_conf
*conf
,
2832 struct lxc_rootfs
*rootfs
,
2833 const char *lxc_name
, const char *lxc_path
)
2835 __do_fclose
FILE *f
= NULL
;
2837 f
= make_anonymous_mount_file(&conf
->mount_entries
, conf
->lsm_aa_allow_nesting
);
2841 return mount_file_entries(rootfs
, f
, lxc_name
, lxc_path
);
2844 static int __lxc_idmapped_mounts_child(struct lxc_handler
*handler
, FILE *f
)
2846 struct lxc_conf
*conf
= handler
->conf
;
2847 struct lxc_rootfs
*rootfs
= &conf
->rootfs
;
2851 struct mntent mntent
;
2853 while (getmntent_r(f
, &mntent
, buf
, sizeof(buf
))) {
2854 __do_close
int fd_from
= -EBADF
, fd_to
= -EBADF
,
2856 __do_free
char *__data
= NULL
;
2857 int cur_mnt_seq
= -1;
2858 struct lxc_mount_options opts
= {};
2860 const char *source_relative
, *target_relative
;
2861 struct lxc_mount_attr attr
= {};
2863 ret
= parse_lxc_mount_attrs(&opts
, mntent
.mnt_opts
);
2865 return syserror("Failed to parse LXC specific mount options");
2868 ret
= parse_mount_attrs(&opts
, mntent
.mnt_opts
);
2870 return syserror("Failed to parse mount options");
2872 /* No idmapped mount entry so skip it. */
2873 if (is_empty_string(opts
.userns_path
))
2876 if (!can_use_bind_mounts())
2877 return syserror_set(-EINVAL
, "Kernel does not support idmapped mounts");
2880 return syserror_set(-EINVAL
, "Only bind mounts can currently be idmapped");
2882 /* We don't support new filesystem mounts yet. */
2883 if (!is_empty_string(mntent
.mnt_type
) &&
2884 !strequal(mntent
.mnt_type
, "none"))
2885 return syserror_set(-EINVAL
, "Only bind mounts can currently be idmapped");
2887 /* Someone specified additional mount options for a bind-mount. */
2888 if (!is_empty_string(opts
.data
))
2889 return syserror_set(-EINVAL
, "Bind mounts don't support non-generic mount options");
2892 * The source path is supposed to be taken relative to the
2893 * container's rootfs mount or - if the container does not have
2894 * a separate rootfs - to the host's /.
2896 source_relative
= deabs(mntent
.mnt_fsname
);
2897 if (opts
.relative
|| !rootfs
->path
)
2898 dfd_from
= rootfs
->dfd_mnt
;
2900 dfd_from
= rootfs
->dfd_host
;
2901 fd_from
= open_tree(dfd_from
, source_relative
,
2902 OPEN_TREE_CLONE
| OPEN_TREE_CLOEXEC
|
2903 (opts
.bind_recursively
? AT_RECURSIVE
: 0));
2905 return syserror("Failed to create detached %smount of %d/%s",
2906 opts
.bind_recursively
? "recursive " : "",
2907 dfd_from
, source_relative
);
2909 if (strequal(opts
.userns_path
, "container"))
2910 fd_userns
= openat(dfd_from
, "proc/self/ns/user", O_RDONLY
| O_CLOEXEC
);
2912 fd_userns
= open_at(-EBADF
, opts
.userns_path
,
2913 PROTECT_OPEN_WITH_TRAILING_SYMLINKS
, 0, 0);
2914 if (fd_userns
< 0) {
2915 if (opts
.optional
) {
2916 TRACE("Skipping optional idmapped mount");
2920 return syserror("Failed to open user namespace \"%s\" for detached %smount of %d/%s",
2921 opts
.userns_path
, opts
.bind_recursively
? "recursive " : "",
2922 dfd_from
, source_relative
);
2925 ret
= __lxc_abstract_unix_send_two_fds(handler
->data_sock
[0],
2927 &opts
, sizeof(opts
));
2929 if (opts
.optional
) {
2930 TRACE("Skipping optional idmapped mount");
2934 return syserror("Failed to send file descriptor %d for detached %smount of %d/%s and file descriptor %d of user namespace \"%s\" to parent",
2935 fd_from
, opts
.bind_recursively
? "recursive " : "",
2936 dfd_from
, source_relative
, fd_userns
,
2940 ret
= lxc_abstract_unix_rcv_credential(handler
->data_sock
[0],
2942 sizeof(cur_mnt_seq
));
2944 if (opts
.optional
) {
2945 TRACE("Skipping optional idmapped mount");
2949 return syserror("Failed to receive notification that parent idmapped detached %smount %d/%s to user namespace %d",
2950 opts
.bind_recursively
? "recursive " : "",
2951 dfd_from
, source_relative
, fd_userns
);
2954 if (mnt_seq
!= cur_mnt_seq
)
2955 return syserror("Expected mount sequence number and mount sequence number from parent mismatch: %d != %d",
2956 mnt_seq
, cur_mnt_seq
);
2959 /* Set regular mount options. */
2961 attr
.propagation
= 0;
2962 ret
= mount_setattr(fd_from
,
2965 (opts
.bind_recursively
? AT_RECURSIVE
: 0),
2969 if (opts
.optional
) {
2970 TRACE("Skipping optional idmapped mount");
2974 return syserror("Failed to set %smount options on detached %d/%s",
2975 opts
.bind_recursively
? "recursive " : "",
2976 dfd_from
, source_relative
);
2979 /* Set propagation mount options. */
2980 if (opts
.attr
.propagation
) {
2981 attr
= (struct lxc_mount_attr
) {
2982 attr
.propagation
= opts
.attr
.propagation
,
2985 ret
= mount_setattr(fd_from
,
2988 (opts
.propagate_recursively
? AT_RECURSIVE
: 0),
2992 if (opts
.optional
) {
2993 TRACE("Skipping optional idmapped mount");
2997 return syserror("Failed to set %spropagation mount options on detached %d/%s",
2998 opts
.bind_recursively
? "recursive " : "",
2999 dfd_from
, source_relative
);
3005 * In contrast to the legacy mount codepath we will simplify
3006 * our lifes and just always treat the target mountpoint to be
3007 * relative to the container's rootfs mountpoint or - if the
3008 * container does not have a separate rootfs - to the host's /.
3011 target_relative
= deabs(mntent
.mnt_dir
);
3013 dfd_from
= rootfs
->dfd_mnt
;
3015 dfd_from
= rootfs
->dfd_host
;
3016 fd_to
= open_at(dfd_from
, target_relative
, PROTECT_OPATH_FILE
, PROTECT_LOOKUP_BENEATH_WITH_SYMLINKS
, 0);
3018 if (opts
.optional
) {
3019 TRACE("Skipping optional idmapped mount");
3023 return syserror("Failed to open target mountpoint %d/%s for detached idmapped %smount %d:%d/%s",
3024 dfd_from
, target_relative
,
3025 opts
.bind_recursively
? "recursive " : "",
3026 fd_userns
, dfd_from
, source_relative
);
3029 ret
= move_detached_mount(fd_from
, fd_to
, "", 0, 0);
3031 if (opts
.optional
) {
3032 TRACE("Skipping optional idmapped mount");
3036 return syserror("Failed to attach detached idmapped %smount %d:%d/%s to target mountpoint %d/%s",
3037 opts
.bind_recursively
? "recursive " : "",
3038 fd_userns
, dfd_from
, source_relative
, dfd_from
, target_relative
);
3041 TRACE("Attached detached idmapped %smount %d:%d/%s to target mountpoint %d/%s",
3042 opts
.bind_recursively
? "recursive " : "", fd_userns
, dfd_from
,
3043 source_relative
, dfd_from
, target_relative
);
3046 if (!feof(f
) || ferror(f
))
3047 return syserror_set(-EINVAL
, "Failed to parse mount entries");
3052 static int lxc_idmapped_mounts_child(struct lxc_handler
*handler
)
3054 __do_fclose
FILE *f_entries
= NULL
;
3056 struct lxc_conf
*conf
= handler
->conf
;
3057 const char *fstab
= conf
->fstab
;
3060 f_entries
= make_anonymous_mount_file(&conf
->mount_entries
,
3061 conf
->lsm_aa_allow_nesting
);
3063 SYSERROR("Failed to create anonymous mount file");
3067 ret
= __lxc_idmapped_mounts_child(handler
, f_entries
);
3069 SYSERROR("Failed to setup idmapped mount entries");
3073 TRACE("Finished setting up idmapped mounts");
3076 __do_endmntent
FILE *f_fstab
= NULL
;
3078 f_fstab
= setmntent(fstab
, "re");
3080 SYSERROR("Failed to open fstab format file \"%s\"", fstab
);
3084 ret
= __lxc_idmapped_mounts_child(handler
, f_fstab
);
3086 SYSERROR("Failed to setup idmapped mount entries specified in fstab");
3090 TRACE("Finished setting up idmapped mounts specified in fstab");
3096 ret
= lxc_abstract_unix_send_credential(handler
->data_sock
[0], NULL
, 0);
3098 return syserror("Failed to inform parent that we are done setting up mounts");
3103 int parse_cap(const char *cap
)
3107 size_t end
= sizeof(caps_opt
) / sizeof(caps_opt
[0]);
3110 if (strequal(cap
, "none"))
3113 for (i
= 0; i
< end
; i
++) {
3114 if (!strequal(cap
, caps_opt
[i
].name
))
3117 capid
= caps_opt
[i
].value
;
3122 /* Try to see if it's numeric, so the user may specify
3123 * capabilities that the running kernel knows about but we
3127 capid
= strtol(cap
, &ptr
, 10);
3128 if (!ptr
|| *ptr
!= '\0' || errno
!= 0)
3129 /* not a valid number */
3131 else if (capid
> lxc_caps_last_cap())
3132 /* we have a number but it's not a valid
3140 bool has_cap(int cap
, struct lxc_conf
*conf
)
3142 bool cap_in_list
= false;
3143 struct cap_entry
*cap_entry
;
3145 list_for_each_entry(cap_entry
, &conf
->caps
.list
, head
) {
3146 if (cap_entry
->cap
!= cap
)
3152 /* The capability is kept. */
3153 if (conf
->caps
.keep
)
3156 /* The capability is not dropped. */
3157 return !cap_in_list
;
3160 static int setup_caps(struct lxc_conf
*conf
)
3162 struct cap_entry
*cap
;
3164 list_for_each_entry(cap
, &conf
->caps
.list
, head
) {
3167 ret
= prctl(PR_CAPBSET_DROP
, prctl_arg(cap
->cap
), prctl_arg(0),
3168 prctl_arg(0), prctl_arg(0));
3170 return log_error_errno(-1, errno
, "Failed to remove %s capability", cap
->cap_name
);
3172 DEBUG("Dropped %s (%d) capability", cap
->cap_name
, cap
->cap
);
3175 DEBUG("Capabilities have been setup");
3179 static int dropcaps_except(struct lxc_conf
*conf
)
3182 struct cap_entry
*cap
;
3184 numcaps
= lxc_caps_last_cap() + 1;
3185 if (numcaps
<= 0 || numcaps
> 200)
3186 return ret_errno(EINVAL
);
3188 TRACE("Found %d capabilities", numcaps
);
3190 list_for_each_entry(cap
, &conf
->caps
.list
, head
) {
3193 if (cap
->cap
>= numcaps
)
3196 ret
= prctl(PR_CAPBSET_DROP
, prctl_arg(cap
->cap
), prctl_arg(0),
3197 prctl_arg(0), prctl_arg(0));
3199 return log_error_errno(-1, errno
,
3200 "Failed to remove capability %s (%d)",
3201 cap
->cap_name
, cap
->cap
);
3203 DEBUG("Keep capability %s (%d)", cap
->cap_name
, cap
->cap
);
3206 DEBUG("Capabilities have been setup");
3210 static int parse_resource(const char *res
)
3216 for (i
= 0; i
< sizeof(limit_opt
) / sizeof(limit_opt
[0]); ++i
)
3217 if (strequal(res
, limit_opt
[i
].name
))
3218 return limit_opt
[i
].value
;
3220 /* Try to see if it's numeric, so the user may specify
3221 * resources that the running kernel knows about but
3224 ret
= lxc_safe_int(res
, &resid
);
3231 int setup_resource_limits(struct lxc_conf
*conf
, pid_t pid
)
3234 struct lxc_limit
*lim
;
3236 if (list_empty(&conf
->limits
))
3239 list_for_each_entry(lim
, &conf
->limits
, head
) {
3240 resid
= parse_resource(lim
->resource
);
3242 return log_error(-1, "Unknown resource %s", lim
->resource
);
3244 #if HAVE_PRLIMIT || HAVE_PRLIMIT64
3245 if (prlimit(pid
, resid
, &lim
->limit
, NULL
) != 0)
3246 return log_error_errno(-1, errno
, "Failed to set limit %s", lim
->resource
);
3248 TRACE("Setup \"%s\" limit", lim
->resource
);
3250 return log_error(-1, "Cannot set limit \"%s\" as prlimit is missing", lim
->resource
);
3254 TRACE("Setup resource limits");
3258 int setup_sysctl_parameters(struct lxc_conf
*conf
)
3260 __do_free
char *tmp
= NULL
;
3262 char filename
[PATH_MAX
] = {0};
3263 struct lxc_sysctl
*sysctl
, *nsysctl
;
3265 if (!list_empty(&conf
->sysctls
))
3268 list_for_each_entry_safe(sysctl
, nsysctl
, &conf
->sysctls
, head
) {
3269 tmp
= lxc_string_replace(".", "/", sysctl
->key
);
3271 return log_error(-1, "Failed to replace key %s", sysctl
->key
);
3273 ret
= strnprintf(filename
, sizeof(filename
), "/proc/sys/%s", tmp
);
3275 return log_error(-1, "Error setting up sysctl parameters path");
3277 ret
= lxc_write_to_file(filename
, sysctl
->value
,
3278 strlen(sysctl
->value
), false, 0666);
3280 return log_error_errno(-1, errno
, "Failed to setup sysctl parameters %s to %s",
3281 sysctl
->key
, sysctl
->value
);
3287 int setup_proc_filesystem(struct lxc_conf
*conf
, pid_t pid
)
3289 __do_free
char *tmp
= NULL
;
3291 char filename
[PATH_MAX
] = {0};
3292 struct lxc_proc
*proc
;
3294 if (!list_empty(&conf
->procs
))
3297 list_for_each_entry(proc
, &conf
->procs
, head
) {
3298 tmp
= lxc_string_replace(".", "/", proc
->filename
);
3300 return log_error(-1, "Failed to replace key %s", proc
->filename
);
3302 ret
= strnprintf(filename
, sizeof(filename
), "/proc/%d/%s", pid
, tmp
);
3304 return log_error(-1, "Error setting up proc filesystem path");
3306 ret
= lxc_write_to_file(filename
, proc
->value
,
3307 strlen(proc
->value
), false, 0666);
3309 return log_error_errno(-1, errno
, "Failed to setup proc filesystem %s to %s",
3310 proc
->filename
, proc
->value
);
3313 TRACE("Setup /proc/%d settings", pid
);
3317 static char *default_rootfs_mount
= LXCROOTFSMOUNT
;
3319 struct lxc_conf
*lxc_conf_init(void)
3322 struct lxc_conf
*new;
3324 new = zalloc(sizeof(*new));
3328 new->loglevel
= LXC_LOG_LEVEL_NOTSET
;
3329 new->personality
= LXC_ARCH_UNCHANGED
;
3331 new->console
.buffer_size
= 0;
3332 new->console
.log_path
= NULL
;
3333 new->console
.log_fd
= -1;
3334 new->console
.log_size
= 0;
3335 new->console
.path
= NULL
;
3336 new->console
.peer
= -1;
3337 new->console
.proxy
.busy
= -1;
3338 new->console
.proxy
.ptx
= -1;
3339 new->console
.proxy
.pty
= -1;
3340 new->console
.ptx
= -EBADF
;
3341 new->console
.pty
= -EBADF
;
3342 new->console
.pty_nr
= -1;
3343 new->console
.name
[0] = '\0';
3344 new->devpts_fd
= -EBADF
;
3345 memset(&new->console
.ringbuf
, 0, sizeof(struct lxc_ringbuf
));
3346 new->maincmd_fd
= -1;
3347 new->monitor_signal_pdeath
= SIGKILL
;
3349 new->rootfs
.mount
= strdup(default_rootfs_mount
);
3350 if (!new->rootfs
.mount
) {
3354 new->rootfs
.managed
= true;
3355 new->rootfs
.dfd_mnt
= -EBADF
;
3356 new->rootfs
.dfd_dev
= -EBADF
;
3357 new->rootfs
.dfd_host
= -EBADF
;
3358 new->rootfs
.fd_path_pin
= -EBADF
;
3359 new->rootfs
.dfd_idmapped
= -EBADF
;
3361 INIT_LIST_HEAD(&new->cgroup
);
3362 INIT_LIST_HEAD(&new->cgroup2
);
3363 /* Block ("allowlist") all devices by default. */
3364 new->bpf_devices
.list_type
= LXC_BPF_DEVICE_CGROUP_ALLOWLIST
;
3365 INIT_LIST_HEAD(&(new->bpf_devices
).devices
);
3366 INIT_LIST_HEAD(&new->mount_entries
);
3367 INIT_LIST_HEAD(&new->caps
.list
);
3368 INIT_LIST_HEAD(&new->id_map
);
3369 new->root_nsuid_map
= NULL
;
3370 new->root_nsgid_map
= NULL
;
3371 INIT_LIST_HEAD(&new->environment
);
3372 INIT_LIST_HEAD(&new->limits
);
3373 INIT_LIST_HEAD(&new->sysctls
);
3374 INIT_LIST_HEAD(&new->procs
);
3375 new->hooks_version
= 0;
3376 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++)
3377 INIT_LIST_HEAD(&new->hooks
[i
]);
3378 INIT_LIST_HEAD(&new->groups
);
3379 INIT_LIST_HEAD(&new->state_clients
);
3380 new->lsm_aa_profile
= NULL
;
3381 INIT_LIST_HEAD(&new->lsm_aa_raw
);
3382 new->lsm_se_context
= NULL
;
3383 new->lsm_se_keyring_context
= NULL
;
3384 new->keyring_disable_session
= false;
3385 new->transient_procfs_mnt
= false;
3386 new->shmount
.path_host
= NULL
;
3387 new->shmount
.path_cont
= NULL
;
3389 /* if running in a new user namespace, init and COMMAND
3390 * default to running as UID/GID 0 when using lxc-execute */
3393 memset(&new->init_groups
, 0, sizeof(lxc_groups_t
));
3394 memset(&new->cgroup_meta
, 0, sizeof(struct lxc_cgroup
));
3395 memset(&new->ns_share
, 0, sizeof(char *) * LXC_NS_MAX
);
3396 memset(&new->timens
, 0, sizeof(struct timens_offsets
));
3397 seccomp_conf_init(new);
3399 INIT_LIST_HEAD(&new->netdevs
);
3404 int write_id_mapping(enum idtype idtype
, pid_t pid
, const char *buf
,
3407 __do_close
int fd
= -EBADF
;
3409 char path
[PATH_MAX
];
3411 if (geteuid() != 0 && idtype
== ID_TYPE_GID
) {
3412 __do_close
int setgroups_fd
= -EBADF
;
3414 ret
= strnprintf(path
, sizeof(path
), "/proc/%d/setgroups", pid
);
3418 setgroups_fd
= open(path
, O_WRONLY
);
3419 if (setgroups_fd
< 0 && errno
!= ENOENT
)
3420 return log_error_errno(-1, errno
, "Failed to open \"%s\"", path
);
3422 if (setgroups_fd
>= 0) {
3423 ret
= lxc_write_nointr(setgroups_fd
, "deny\n",
3424 STRLITERALLEN("deny\n"));
3425 if (ret
!= STRLITERALLEN("deny\n"))
3426 return log_error_errno(-1, errno
, "Failed to write \"deny\" to \"/proc/%d/setgroups\"", pid
);
3427 TRACE("Wrote \"deny\" to \"/proc/%d/setgroups\"", pid
);
3431 ret
= strnprintf(path
, sizeof(path
), "/proc/%d/%cid_map", pid
,
3432 idtype
== ID_TYPE_UID
? 'u' : 'g');
3436 fd
= open(path
, O_WRONLY
| O_CLOEXEC
);
3438 return log_error_errno(-1, errno
, "Failed to open \"%s\"", path
);
3440 ret
= lxc_write_nointr(fd
, buf
, buf_size
);
3441 if (ret
!= buf_size
)
3442 return log_error_errno(-1, errno
, "Failed to write %cid mapping to \"%s\"",
3443 idtype
== ID_TYPE_UID
? 'u' : 'g', path
);
3448 /* Check whether a binary exist and has either CAP_SETUID, CAP_SETGID or both.
3450 * @return 1 if functional binary was found
3451 * @return 0 if binary exists but is lacking privilege
3452 * @return -ENOENT if binary does not exist
3453 * @return -EINVAL if cap to check is neither CAP_SETUID nor CAP_SETGID
3455 static int idmaptool_on_path_and_privileged(const char *binary
, cap_value_t cap
)
3457 __do_free
char *path
= NULL
;
3461 if (cap
!= CAP_SETUID
&& cap
!= CAP_SETGID
)
3462 return ret_errno(EINVAL
);
3464 path
= on_path(binary
, NULL
);
3466 return ret_errno(ENOENT
);
3468 ret
= stat(path
, &st
);
3472 /* Check if the binary is setuid. */
3473 if (st
.st_mode
& S_ISUID
)
3474 return log_debug(1, "The binary \"%s\" does have the setuid bit set", path
);
3476 #if HAVE_LIBCAP && LIBCAP_SUPPORTS_FILE_CAPABILITIES
3477 /* Check if it has the CAP_SETUID capability. */
3478 if ((cap
& CAP_SETUID
) &&
3479 lxc_file_cap_is_set(path
, CAP_SETUID
, CAP_EFFECTIVE
) &&
3480 lxc_file_cap_is_set(path
, CAP_SETUID
, CAP_PERMITTED
))
3481 return log_debug(1, "The binary \"%s\" has CAP_SETUID in its CAP_EFFECTIVE and CAP_PERMITTED sets", path
);
3483 /* Check if it has the CAP_SETGID capability. */
3484 if ((cap
& CAP_SETGID
) &&
3485 lxc_file_cap_is_set(path
, CAP_SETGID
, CAP_EFFECTIVE
) &&
3486 lxc_file_cap_is_set(path
, CAP_SETGID
, CAP_PERMITTED
))
3487 return log_debug(1, "The binary \"%s\" has CAP_SETGID in its CAP_EFFECTIVE and CAP_PERMITTED sets", path
);
3492 * If we cannot check for file capabilities we need to give the benefit
3493 * of the doubt. Otherwise we might fail even though all the necessary
3494 * file capabilities are set.
3496 DEBUG("Cannot check for file capabilities as full capability support is missing. Manual intervention needed");
3501 static int lxc_map_ids_exec_wrapper(void *args
)
3503 execl("/bin/sh", "sh", "-c", (char *)args
, (char *)NULL
);
3507 static struct id_map
*find_mapped_hostid_entry(const struct list_head
*idmap
,
3508 unsigned id
, enum idtype idtype
);
3510 int lxc_map_ids(struct list_head
*idmap
, pid_t pid
)
3512 int hostuid
, hostgid
, fill
, left
;
3515 char cmd_output
[PATH_MAX
];
3518 int ret
= 0, gidmap
= 0, uidmap
= 0;
3519 char mapbuf
[STRLITERALLEN("new@idmap") + STRLITERALLEN(" ") +
3520 INTTYPE_TO_STRLEN(pid_t
) + STRLITERALLEN(" ") +
3521 LXC_IDMAPLEN
] = {0};
3522 bool had_entry
= false, maps_host_root
= false, use_shadow
= false;
3524 hostuid
= geteuid();
3525 hostgid
= getegid();
3528 * Check whether caller wants to map host root.
3529 * Due to a security fix newer kernels require CAP_SETFCAP when mapping
3530 * host root into the child userns as you would be able to write fscaps
3531 * that would be valid in the ancestor userns. Mapping host root should
3532 * rarely be the case but LXC is being clever in a bunch of cases.
3534 if (find_mapped_hostid_entry(idmap
, 0, ID_TYPE_UID
))
3535 maps_host_root
= true;
3537 /* If new{g,u}idmap exists, that is, if shadow is handing out subuid
3538 * ranges, then insist that root also reserve ranges in subuid. This
3539 * will protected it by preventing another user from being handed the
3542 uidmap
= idmaptool_on_path_and_privileged("newuidmap", CAP_SETUID
);
3543 if (uidmap
== -ENOENT
)
3544 WARN("newuidmap binary is missing");
3546 WARN("newuidmap is lacking necessary privileges");
3548 gidmap
= idmaptool_on_path_and_privileged("newgidmap", CAP_SETGID
);
3549 if (gidmap
== -ENOENT
)
3550 WARN("newgidmap binary is missing");
3552 WARN("newgidmap is lacking necessary privileges");
3554 if (maps_host_root
) {
3555 INFO("Caller maps host root. Writing mapping directly");
3556 } else if (uidmap
> 0 && gidmap
> 0) {
3557 DEBUG("Functional newuidmap and newgidmap binary found");
3560 /* In case unprivileged users run application containers via
3561 * execute() or a start*() there are valid cases where they may
3562 * only want to map their own {g,u}id. Let's not block them from
3563 * doing so by requiring geteuid() == 0.
3565 DEBUG("No newuidmap and newgidmap binary found. Trying to "
3566 "write directly with euid %d", hostuid
);
3569 /* Check if we really need to use newuidmap and newgidmap.
3570 * If the user is only remapping their own {g,u}id, we don't need it.
3572 if (use_shadow
&& list_len(map
, idmap
, head
) == 2) {
3574 list_for_each_entry(map
, idmap
, head
) {
3575 if (map
->idtype
== ID_TYPE_UID
&& map
->range
== 1 &&
3576 map
->nsid
== hostuid
&& map
->hostid
== hostuid
)
3578 if (map
->idtype
== ID_TYPE_GID
&& map
->range
== 1 &&
3579 map
->nsid
== hostgid
&& map
->hostid
== hostgid
)
3586 for (type
= ID_TYPE_UID
, u_or_g
= 'u'; type
<= ID_TYPE_GID
;
3587 type
++, u_or_g
= 'g') {
3591 pos
+= sprintf(mapbuf
, "new%cidmap %d", u_or_g
, pid
);
3593 list_for_each_entry(map
, idmap
, head
) {
3594 if (map
->idtype
!= type
)
3599 left
= LXC_IDMAPLEN
- (pos
- mapbuf
);
3600 fill
= strnprintf(pos
, left
, "%s%lu %lu %lu%s",
3601 use_shadow
? " " : "", map
->nsid
,
3602 map
->hostid
, map
->range
,
3603 use_shadow
? "" : "\n");
3605 * The kernel only takes <= 4k for writes to
3606 * /proc/<pid>/{g,u}id_map
3609 return log_error_errno(-1, errno
, "Too many %cid mappings defined", u_or_g
);
3616 /* Try to catch the output of new{g,u}idmap to make debugging
3620 ret
= run_command(cmd_output
, sizeof(cmd_output
),
3621 lxc_map_ids_exec_wrapper
,
3624 return log_error(-1, "new%cidmap failed to write mapping \"%s\": %s", u_or_g
, cmd_output
, mapbuf
);
3625 TRACE("new%cidmap wrote mapping \"%s\"", u_or_g
, mapbuf
);
3627 ret
= write_id_mapping(type
, pid
, mapbuf
, pos
- mapbuf
);
3629 return log_error(-1, "Failed to write mapping: %s", mapbuf
);
3630 TRACE("Wrote mapping \"%s\"", mapbuf
);
3633 memset(mapbuf
, 0, sizeof(mapbuf
));
3640 * Return the host uid/gid to which the container root is mapped in val.
3641 * Return true if id was found, false otherwise.
3643 static id_t
get_mapped_rootid(const struct lxc_conf
*conf
, enum idtype idtype
)
3648 if (idtype
== ID_TYPE_UID
)
3649 nsid
= (conf
->root_nsuid_map
!= NULL
) ? 0 : conf
->init_uid
;
3651 nsid
= (conf
->root_nsgid_map
!= NULL
) ? 0 : conf
->init_gid
;
3653 list_for_each_entry (map
, &conf
->id_map
, head
) {
3654 if (map
->idtype
!= idtype
)
3656 if (map
->nsid
!= nsid
)
3661 if (idtype
== ID_TYPE_UID
)
3662 return LXC_INVALID_UID
;
3664 return LXC_INVALID_GID
;
3667 int mapped_hostid(unsigned id
, const struct lxc_conf
*conf
, enum idtype idtype
)
3671 list_for_each_entry(map
, &conf
->id_map
, head
) {
3672 if (map
->idtype
!= idtype
)
3675 if (id
>= map
->hostid
&& id
< map
->hostid
+ map
->range
)
3676 return (id
- map
->hostid
) + map
->nsid
;
3682 int find_unmapped_nsid(const struct lxc_conf
*conf
, enum idtype idtype
)
3685 unsigned int freeid
= 0;
3688 list_for_each_entry(map
, &conf
->id_map
, head
) {
3689 if (map
->idtype
!= idtype
)
3692 if (freeid
>= map
->nsid
&& freeid
< map
->nsid
+ map
->range
) {
3693 freeid
= map
->nsid
+ map
->range
;
3702 * Mount a proc under @rootfs if proc self points to a pid other than
3703 * my own. This is needed to have a known-good proc mount for setting
3704 * up LSMs both at container startup and attach.
3706 * NOTE: not to be called from inside the container namespace!
3708 static int lxc_transient_proc(struct lxc_rootfs
*rootfs
)
3710 __do_close
int fd_proc
= -EBADF
;
3711 int link_to_pid
, link_len
, pid_self
, ret
;
3712 char link
[INTTYPE_TO_STRLEN(pid_t
) + 1];
3714 link_len
= readlinkat(rootfs
->dfd_mnt
, "proc/self", link
, sizeof(link
));
3716 ret
= mkdirat(rootfs
->dfd_mnt
, "proc", 0000);
3717 if (ret
< 0 && errno
!= EEXIST
)
3718 return log_error_errno(-errno
, errno
, "Failed to create %d(proc)", rootfs
->dfd_mnt
);
3721 } else if (link_len
>= sizeof(link
)) {
3722 return log_error_errno(-EIO
, EIO
, "Truncated link target");
3724 link
[link_len
] = '\0';
3726 pid_self
= lxc_raw_getpid();
3727 INFO("Caller's PID is %d; /proc/self points to %s", pid_self
, link
);
3729 ret
= lxc_safe_int(link
, &link_to_pid
);
3731 return log_error_errno(-ret
, ret
, "Failed to parse %s", link
);
3733 /* Correct procfs is already mounted. */
3734 if (link_to_pid
== pid_self
)
3735 return log_trace(0, "Correct procfs instance mounted");
3737 fd_proc
= open_at(rootfs
->dfd_mnt
, "proc", PROTECT_OPATH_DIRECTORY
,
3738 PROTECT_LOOKUP_BENEATH_XDEV
, 0);
3740 return log_error_errno(-errno
, errno
, "Failed to open transient procfs mountpoint");
3742 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "/proc/self/fd/%d", fd_proc
);
3744 return ret_errno(EIO
);
3746 ret
= umount2(rootfs
->buf
, MNT_DETACH
);
3748 SYSWARN("Failed to umount \"%s\" with MNT_DETACH", rootfs
->buf
);
3751 /* rootfs is NULL */
3752 if (!rootfs
->path
) {
3753 ret
= mount("proc", rootfs
->buf
, "proc", 0, NULL
);
3755 ret
= safe_mount_beneath_at(rootfs
->dfd_mnt
, "none", "proc", "proc", 0, NULL
);
3757 ret
= strnprintf(rootfs
->buf
, sizeof(rootfs
->buf
), "%s/proc", rootfs
->path
? rootfs
->mount
: "");
3759 return ret_errno(EIO
);
3761 ret
= safe_mount("proc", rootfs
->buf
, "proc", 0, NULL
, rootfs
->mount
);
3765 return log_error_errno(-1, errno
, "Failed to mount temporary procfs");
3767 INFO("Created transient procfs mount");
3771 /* NOTE: Must not be called from inside the container namespace! */
3772 static int lxc_create_tmp_proc_mount(struct lxc_conf
*conf
)
3776 mounted
= lxc_transient_proc(&conf
->rootfs
);
3777 if (mounted
== -1) {
3778 /* continue only if there is no rootfs */
3779 if (conf
->rootfs
.path
)
3780 return log_error_errno(-EPERM
, EPERM
, "Failed to create transient procfs mount");
3781 } else if (mounted
== 1) {
3782 conf
->transient_procfs_mnt
= true;
3788 void tmp_proc_unmount(struct lxc_conf
*lxc_conf
)
3790 if (lxc_conf
->transient_procfs_mnt
) {
3791 (void)umount2("/proc", MNT_DETACH
);
3792 lxc_conf
->transient_procfs_mnt
= false;
3796 /* Walk /proc/mounts and change any shared entries to dependent mounts. */
3797 static void turn_into_dependent_mounts(const struct lxc_rootfs
*rootfs
)
3799 __do_free
char *line
= NULL
;
3800 __do_fclose
FILE *f
= NULL
;
3801 __do_close
int memfd
= -EBADF
, mntinfo_fd
= -EBADF
;
3806 mntinfo_fd
= open_at(rootfs
->dfd_host
, "proc/self/mountinfo", PROTECT_OPEN
,
3807 (PROTECT_LOOKUP_BENEATH_XDEV
& ~RESOLVE_NO_SYMLINKS
), 0);
3808 if (mntinfo_fd
< 0) {
3809 SYSERROR("Failed to open %d/proc/self/mountinfo", rootfs
->dfd_host
);
3813 memfd
= memfd_create(".lxc_mountinfo", MFD_CLOEXEC
);
3815 char template[] = P_tmpdir
"/.lxc_mountinfo_XXXXXX";
3817 if (errno
!= ENOSYS
) {
3818 SYSERROR("Failed to create temporary in-memory file");
3822 memfd
= lxc_make_tmpfile(template, true);
3824 WARN("Failed to create temporary file");
3829 copied
= fd_to_fd(mntinfo_fd
, memfd
);
3831 SYSERROR("Failed to copy \"/proc/self/mountinfo\"");
3835 ret
= lseek(memfd
, 0, SEEK_SET
);
3837 SYSERROR("Failed to reset file descriptor offset");
3841 f
= fdopen(memfd
, "re");
3843 SYSERROR("Failed to open copy of \"/proc/self/mountinfo\" to mark all shared. Continuing");
3848 * After a successful fdopen() memfd will be closed when calling
3849 * fclose(f). Calling close(memfd) afterwards is undefined.
3853 while (getline(&line
, &len
, f
) != -1) {
3854 char *opts
, *target
;
3856 target
= get_field(line
, 4);
3860 opts
= get_field(target
, 2);
3864 null_endofword(opts
);
3865 if (!strstr(opts
, "shared"))
3868 null_endofword(target
);
3869 ret
= mount(NULL
, target
, NULL
, MS_SLAVE
, NULL
);
3871 SYSERROR("Failed to recursively turn old root mount tree into dependent mount. Continuing...");
3875 TRACE("Turned all mount table entries into dependent mount");
3878 /* This does the work of remounting / if it is shared, calling the container
3879 * pre-mount hooks, and mounting the rootfs.
3881 int lxc_setup_rootfs_prepare_root(struct lxc_conf
*conf
, const char *name
,
3882 const char *lxcpath
)
3886 conf
->rootfs
.dfd_host
= open_at(-EBADF
, "/", PROTECT_OPATH_DIRECTORY
, PROTECT_LOOKUP_ABSOLUTE
, 0);
3887 if (conf
->rootfs
.dfd_host
< 0)
3888 return log_error_errno(-errno
, errno
, "Failed to open \"/\"");
3890 turn_into_dependent_mounts(&conf
->rootfs
);
3892 if (conf
->rootfs_setup
) {
3893 const char *path
= conf
->rootfs
.mount
;
3896 * The rootfs was set up in another namespace. bind-mount it to
3897 * give us a mount in our own ns so we can pivot_root to it
3899 ret
= mount(path
, path
, "rootfs", MS_BIND
, NULL
);
3901 return log_error(-1, "Failed to bind mount container / onto itself");
3903 conf
->rootfs
.dfd_mnt
= openat(-EBADF
, path
, O_RDONLY
| O_CLOEXEC
| O_DIRECTORY
| O_PATH
| O_NOCTTY
);
3904 if (conf
->rootfs
.dfd_mnt
< 0)
3905 return log_error_errno(-errno
, errno
, "Failed to open file descriptor for container rootfs");
3907 return log_trace(0, "Bind mounted container / onto itself");
3910 ret
= run_lxc_hooks(name
, "pre-mount", conf
, NULL
);
3912 return log_error(-1, "Failed to run pre-mount hooks");
3914 ret
= lxc_mount_rootfs(&conf
->rootfs
);
3916 return log_error(-1, "Failed to setup rootfs for");
3918 conf
->rootfs_setup
= true;
3922 static bool verify_start_hooks(struct lxc_conf
*conf
)
3924 char path
[PATH_MAX
];
3925 struct string_entry
*hook
;
3927 list_for_each_entry(hook
, &conf
->hooks
[LXCHOOK_START
], head
) {
3929 char *hookname
= hook
->val
;
3931 ret
= strnprintf(path
, sizeof(path
), "%s%s",
3932 conf
->rootfs
.path
? conf
->rootfs
.mount
: "",
3937 ret
= access(path
, X_OK
);
3939 return log_error_errno(false, errno
, "Start hook \"%s\" not found in container", hookname
);
3947 static int lxc_setup_boot_id(void)
3950 const char *boot_id_path
= "/proc/sys/kernel/random/boot_id";
3951 const char *mock_boot_id_path
= "/dev/.lxc-boot-id";
3954 if (access(boot_id_path
, F_OK
))
3957 memset(&n
, 0, sizeof(n
));
3958 if (lxc_id128_randomize(&n
)) {
3959 SYSERROR("Failed to generate random data for uuid");
3963 ret
= lxc_id128_write(mock_boot_id_path
, n
);
3965 SYSERROR("Failed to write uuid to %s", mock_boot_id_path
);
3969 ret
= chmod(mock_boot_id_path
, 0444);
3971 SYSERROR("Failed to chown %s", mock_boot_id_path
);
3972 (void)unlink(mock_boot_id_path
);
3976 ret
= mount(mock_boot_id_path
, boot_id_path
, NULL
, MS_BIND
, NULL
);
3978 SYSERROR("Failed to mount %s to %s", mock_boot_id_path
,
3980 (void)unlink(mock_boot_id_path
);
3984 ret
= mount(NULL
, boot_id_path
, NULL
,
3985 (MS_BIND
| MS_REMOUNT
| MS_RDONLY
| MS_NOSUID
| MS_NOEXEC
|
3989 SYSERROR("Failed to remount %s read-only", boot_id_path
);
3990 (void)unlink(mock_boot_id_path
);
3997 static int lxc_setup_keyring(struct lsm_ops
*lsm_ops
, const struct lxc_conf
*conf
)
3999 key_serial_t keyring
;
4002 if (conf
->lsm_se_keyring_context
)
4003 ret
= lsm_ops
->keyring_label_set(lsm_ops
, conf
->lsm_se_keyring_context
);
4004 else if (conf
->lsm_se_context
)
4005 ret
= lsm_ops
->keyring_label_set(lsm_ops
, conf
->lsm_se_context
);
4007 return syserror("Failed to set keyring context");
4010 * Try to allocate a new session keyring for the container to prevent
4011 * information leaks.
4013 keyring
= keyctl(KEYCTL_JOIN_SESSION_KEYRING
, prctl_arg(0),
4014 prctl_arg(0), prctl_arg(0), prctl_arg(0));
4018 DEBUG("The keyctl() syscall is not supported or blocked");
4023 DEBUG("Failed to access kernel keyring. Continuing...");
4026 SYSWARN("Failed to create kernel keyring");
4034 static int lxc_rootfs_prepare_child(struct lxc_handler
*handler
)
4036 struct lxc_rootfs
*rootfs
= &handler
->conf
->rootfs
;
4037 int dfd_idmapped
= -EBADF
;
4040 if (list_empty(&handler
->conf
->id_map
))
4043 if (is_empty_string(rootfs
->mnt_opts
.userns_path
))
4046 if (handler
->conf
->rootfs_setup
)
4049 ret
= lxc_abstract_unix_recv_one_fd(handler
->data_sock
[1], &dfd_idmapped
, NULL
, 0);
4051 return syserror("Failed to receive idmapped mount fd");
4053 rootfs
->dfd_idmapped
= dfd_idmapped
;
4054 TRACE("Received detached idmapped mount %d", rootfs
->dfd_idmapped
);
4058 int lxc_idmapped_mounts_parent(struct lxc_handler
*handler
)
4063 __do_close
int fd_from
= -EBADF
, fd_userns
= -EBADF
;
4064 struct lxc_mount_attr attr
= {};
4065 struct lxc_mount_options opts
= {};
4068 ret
= __lxc_abstract_unix_recv_two_fds(handler
->data_sock
[1],
4069 &fd_from
, &fd_userns
,
4070 &opts
, sizeof(opts
));
4072 return syserror("Failed to receive idmapped mount file descriptors from child");
4074 if (fd_from
< 0 || fd_userns
< 0)
4075 return log_trace(0, "Finished receiving idmapped mount file descriptors from child");
4077 attr
.attr_set
= MOUNT_ATTR_IDMAP
;
4078 attr
.userns_fd
= fd_userns
;
4079 ret
= mount_setattr(fd_from
, "",
4081 (opts
.bind_recursively
? AT_RECURSIVE
: 0),
4082 &attr
, sizeof(attr
));
4084 return syserror("Failed to idmap detached %smount %d to %d",
4085 opts
.bind_recursively
? "recursive " : "",
4086 fd_from
, fd_userns
);
4088 ret
= lxc_abstract_unix_send_credential(handler
->data_sock
[1],
4092 return syserror("Parent failed to notify child that detached %smount %d was idmapped to user namespace %d",
4093 opts
.bind_recursively
? "recursive " : "",
4094 fd_from
, fd_userns
);
4096 TRACE("Parent idmapped detached %smount %d to user namespace %d",
4097 opts
.bind_recursively
? "recursive " : "", fd_from
, fd_userns
);
4102 static int lxc_recv_ttys_from_child(struct lxc_handler
*handler
)
4104 call_cleaner(lxc_delete_tty
) struct lxc_tty_info
*info_new
= &(struct lxc_tty_info
){};
4105 int sock
= handler
->data_sock
[1];
4106 struct lxc_conf
*conf
= handler
->conf
;
4107 struct lxc_tty_info
*tty_info
= &conf
->ttys
;
4108 size_t ttys_max
= tty_info
->max
;
4109 struct lxc_terminal_info
*terminal_info
;
4115 info_new
->tty
= malloc(sizeof(*(info_new
->tty
)) * ttys_max
);
4117 return ret_errno(ENOMEM
);
4119 for (int i
= 0; i
< ttys_max
; i
++) {
4120 terminal_info
= &info_new
->tty
[i
];
4121 terminal_info
->busy
= -1;
4122 terminal_info
->ptx
= -EBADF
;
4123 terminal_info
->pty
= -EBADF
;
4126 for (int i
= 0; i
< ttys_max
; i
++) {
4127 int ptx
= -EBADF
, pty
= -EBADF
;
4129 ret
= lxc_abstract_unix_recv_two_fds(sock
, &ptx
, &pty
);
4131 return syserror("Failed to receive %zu ttys from child", ttys_max
);
4133 terminal_info
= &info_new
->tty
[i
];
4134 terminal_info
->ptx
= ptx
;
4135 terminal_info
->pty
= pty
;
4136 TRACE("Received pty with ptx fd %d and pty fd %d from child",
4137 terminal_info
->ptx
, terminal_info
->pty
);
4140 tty_info
->tty
= move_ptr(info_new
->tty
);
4141 TRACE("Received %zu ttys from child", ttys_max
);
4145 static int lxc_send_console_to_parent(struct lxc_handler
*handler
)
4147 struct lxc_terminal
*console
= &handler
->conf
->console
;
4150 if (!wants_console(console
))
4153 /* We've already allocated a console from the host's devpts instance. */
4154 if (console
->pty
< 0)
4157 ret
= __lxc_abstract_unix_send_two_fds(handler
->data_sock
[0],
4158 console
->ptx
, console
->pty
,
4160 sizeof(struct lxc_terminal
));
4162 return syserror("Fail to send console to parent");
4164 TRACE("Sent console to parent");
4168 static int lxc_recv_console_from_child(struct lxc_handler
*handler
)
4170 __do_close
int fd_ptx
= -EBADF
, fd_pty
= -EBADF
;
4171 struct lxc_terminal
*console
= &handler
->conf
->console
;
4174 if (!wants_console(console
))
4177 /* We've already allocated a console from the host's devpts instance. */
4178 if (console
->pty
>= 0)
4181 ret
= __lxc_abstract_unix_recv_two_fds(handler
->data_sock
[1],
4184 sizeof(struct lxc_terminal
));
4186 return syserror("Fail to receive console from child");
4188 console
->ptx
= move_fd(fd_ptx
);
4189 console
->pty
= move_fd(fd_pty
);
4191 TRACE("Received console from child");
4195 int lxc_sync_fds_parent(struct lxc_handler
*handler
)
4199 ret
= lxc_seccomp_recv_notifier_fd(&handler
->conf
->seccomp
, handler
->data_sock
[1]);
4201 return syserror_ret(ret
, "Failed to receive seccomp notify fd from child");
4203 ret
= lxc_recv_devpts_from_child(handler
);
4205 return syserror_ret(ret
, "Failed to receive devpts fd from child");
4207 /* Read tty fds allocated by child. */
4208 ret
= lxc_recv_ttys_from_child(handler
);
4210 return syserror_ret(ret
, "Failed to receive tty info from child process");
4212 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
4213 ret
= lxc_network_recv_name_and_ifindex_from_child(handler
);
4215 return syserror_ret(ret
, "Failed to receive names and ifindices for network devices from child");
4218 ret
= lxc_recv_console_from_child(handler
);
4220 return syserror_ret(ret
, "Failed to receive console from child");
4222 TRACE("Finished syncing file descriptors with child");
4226 int lxc_sync_fds_child(struct lxc_handler
*handler
)
4230 ret
= lxc_seccomp_send_notifier_fd(&handler
->conf
->seccomp
, handler
->data_sock
[0]);
4232 return syserror_ret(ret
, "Failed to send seccomp notify fd to parent");
4234 ret
= lxc_send_devpts_to_parent(handler
);
4236 return syserror_ret(ret
, "Failed to send seccomp devpts fd to parent");
4238 ret
= lxc_send_ttys_to_parent(handler
);
4240 return syserror_ret(ret
, "Failed to send tty file descriptors to parent");
4242 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
4243 ret
= lxc_network_send_name_and_ifindex_to_parent(handler
);
4245 return syserror_ret(ret
, "Failed to send network device names and ifindices to parent");
4248 ret
= lxc_send_console_to_parent(handler
);
4250 return syserror_ret(ret
, "Failed to send console to parent");
4252 TRACE("Finished syncing file descriptors with parent");
4256 static int setcup_capabilities(struct lxc_conf
*conf
)
4260 if (conf
->caps
.keep
)
4261 ret
= dropcaps_except(conf
);
4263 ret
= setup_caps(conf
);
4265 return log_error(-1, "Failed to %s capabilities", conf
->caps
.keep
? "keep" : "drop");
4270 int lxc_setup(struct lxc_handler
*handler
)
4273 const char *lxcpath
= handler
->lxcpath
, *name
= handler
->name
;
4274 struct lxc_conf
*lxc_conf
= handler
->conf
;
4276 ret
= lxc_rootfs_prepare_child(handler
);
4278 return syserror("Failed to prepare rootfs");
4280 ret
= lxc_setup_rootfs_prepare_root(lxc_conf
, name
, lxcpath
);
4282 return log_error(-1, "Failed to setup rootfs");
4284 if (handler
->nsfd
[LXC_NS_UTS
] == -EBADF
) {
4285 ret
= setup_utsname(lxc_conf
->utsname
);
4287 return log_error(-1, "Failed to setup the utsname %s", name
);
4290 if (!lxc_conf
->keyring_disable_session
) {
4291 ret
= lxc_setup_keyring(handler
->lsm_ops
, lxc_conf
);
4293 return log_error(-1, "Failed to setup container keyring");
4296 if (handler
->ns_clone_flags
& CLONE_NEWNET
) {
4297 ret
= lxc_network_recv_from_parent(handler
);
4299 return log_error(-1, "Failed to receive veth names from parent");
4301 ret
= lxc_setup_network_in_child_namespaces(lxc_conf
);
4303 return log_error(-1, "Failed to setup network");
4306 if (lxc_conf
->autodev
> 0) {
4307 ret
= mount_autodev(name
, &lxc_conf
->rootfs
, lxc_conf
->autodevtmpfssize
, lxcpath
);
4309 return log_error(-1, "Failed to mount \"/dev\"");
4312 /* Do automatic mounts (mainly /proc and /sys), but exclude those that
4313 * need to wait until other stuff has finished.
4315 ret
= lxc_mount_auto_mounts(handler
, lxc_conf
->auto_mounts
& ~LXC_AUTO_CGROUP_MASK
);
4317 return log_error(-1, "Failed to setup first automatic mounts");
4319 ret
= setup_mount_fstab(&lxc_conf
->rootfs
, lxc_conf
->fstab
, name
, lxcpath
);
4321 return log_error(-1, "Failed to setup mounts");
4323 if (!list_empty(&lxc_conf
->mount_entries
)) {
4324 ret
= setup_mount_entries(lxc_conf
, &lxc_conf
->rootfs
, name
, lxcpath
);
4326 return log_error(-1, "Failed to setup mount entries");
4329 if (!lxc_sync_wake_parent(handler
, START_SYNC_IDMAPPED_MOUNTS
))
4332 ret
= lxc_idmapped_mounts_child(handler
);
4334 return syserror("Failed to attached detached idmapped mounts");
4336 lxc_conf
->rootfs
.dfd_dev
= open_at(lxc_conf
->rootfs
.dfd_mnt
, "dev",
4337 PROTECT_OPATH_DIRECTORY
, PROTECT_LOOKUP_BENEATH_XDEV
, 0);
4338 if (lxc_conf
->rootfs
.dfd_dev
< 0 && errno
!= ENOENT
)
4339 return log_error_errno(-errno
, errno
, "Failed to open \"/dev\"");
4341 /* Now mount only cgroups, if wanted. Before, /sys could not have been
4342 * mounted. It is guaranteed to be mounted now either through
4343 * automatically or via fstab entries.
4345 ret
= lxc_mount_auto_mounts(handler
, lxc_conf
->auto_mounts
& LXC_AUTO_CGROUP_MASK
);
4347 return log_error(-1, "Failed to setup remaining automatic mounts");
4349 ret
= run_lxc_hooks(name
, "mount", lxc_conf
, NULL
);
4351 return log_error(-1, "Failed to run mount hooks");
4353 if (lxc_conf
->autodev
> 0) {
4354 ret
= run_lxc_hooks(name
, "autodev", lxc_conf
, NULL
);
4356 return log_error(-1, "Failed to run autodev hooks");
4358 ret
= lxc_fill_autodev(&lxc_conf
->rootfs
);
4360 return log_error(-1, "Failed to populate \"/dev\"");
4363 /* Make sure any start hooks are in the container */
4364 if (!verify_start_hooks(lxc_conf
))
4365 return log_error(-1, "Failed to verify start hooks");
4367 ret
= lxc_create_tmp_proc_mount(lxc_conf
);
4369 return log_error(-1, "Failed to mount transient procfs instance for LSMs");
4371 ret
= lxc_setup_devpts_child(handler
);
4373 return log_error(-1, "Failed to prepare new devpts instance");
4375 ret
= lxc_finish_devpts_child(handler
);
4377 return log_error(-1, "Failed to finish devpts setup");
4379 ret
= lxc_setup_console(handler
, &lxc_conf
->rootfs
, &lxc_conf
->console
,
4380 lxc_conf
->ttys
.dir
);
4382 return log_error(-1, "Failed to setup console");
4384 ret
= lxc_create_ttys(handler
);
4386 return log_error(-1, "Failed to create ttys");
4388 ret
= lxc_setup_dev_symlinks(&lxc_conf
->rootfs
);
4390 return log_error(-1, "Failed to setup \"/dev\" symlinks");
4392 ret
= lxc_setup_rootfs_switch_root(&lxc_conf
->rootfs
);
4394 return log_error(-1, "Failed to pivot root into rootfs");
4396 /* Setting the boot-id is best-effort for now. */
4397 if (lxc_conf
->autodev
> 0)
4398 (void)lxc_setup_boot_id();
4400 ret
= setup_personality(lxc_conf
->personality
);
4402 return syserror("Failed to set personality");
4404 /* Set sysctl value to a path under /proc/sys as determined from the
4405 * key. For e.g. net.ipv4.ip_forward translated to
4406 * /proc/sys/net/ipv4/ip_forward.
4408 ret
= setup_sysctl_parameters(lxc_conf
);
4410 return log_error(-1, "Failed to setup sysctl parameters");
4412 ret
= setcup_capabilities(lxc_conf
);
4414 return log_error(-1, "Failed to setup capabilities");
4416 put_lxc_rootfs(&handler
->conf
->rootfs
, true);
4417 NOTICE("The container \"%s\" is set up", name
);
4422 int run_lxc_hooks(const char *name
, char *hookname
, struct lxc_conf
*conf
,
4426 struct string_entry
*entry
;
4428 for (which
= 0; which
< NUM_LXC_HOOKS
; which
++) {
4429 if (strequal(hookname
, lxchook_names
[which
]))
4433 if (which
>= NUM_LXC_HOOKS
)
4436 list_for_each_entry(entry
, &conf
->hooks
[which
], head
) {
4438 char *hook
= entry
->val
;
4440 ret
= run_script_argv(name
, conf
->hooks_version
, "lxc", hook
,
4449 int lxc_clear_config_caps(struct lxc_conf
*c
)
4451 struct cap_entry
*cap
, *ncap
;
4453 list_for_each_entry_safe(cap
, ncap
, &c
->caps
.list
, head
) {
4454 list_del(&cap
->head
);
4455 free(cap
->cap_name
);
4459 c
->caps
.keep
= false;
4460 INIT_LIST_HEAD(&c
->caps
.list
);
4464 static int lxc_free_idmap(struct list_head
*id_map
)
4466 struct id_map
*map
, *nmap
;
4468 list_for_each_entry_safe(map
, nmap
, id_map
, head
) {
4469 list_del(&map
->head
);
4473 INIT_LIST_HEAD(id_map
);
4477 static int __lxc_free_idmap(struct list_head
*id_map
)
4479 lxc_free_idmap(id_map
);
4482 define_cleanup_function(struct list_head
*, __lxc_free_idmap
);
4484 int lxc_clear_idmaps(struct lxc_conf
*c
)
4486 return lxc_free_idmap(&c
->id_map
);
4489 int lxc_clear_namespace(struct lxc_conf
*c
)
4491 for (int i
= 0; i
< LXC_NS_MAX
; i
++)
4492 free_disarm(c
->ns_share
[i
]);
4497 int lxc_clear_cgroups(struct lxc_conf
*c
, const char *key
, int version
)
4499 const char *k
= key
;
4501 char *global_token
, *namespaced_token
;
4502 size_t namespaced_token_len
;
4503 struct list_head
*list
;
4504 struct lxc_cgroup
*cgroup
, *ncgroup
;
4506 if (version
== CGROUP2_SUPER_MAGIC
) {
4507 global_token
= "lxc.cgroup2";
4508 namespaced_token
= "lxc.cgroup2.";
4509 namespaced_token_len
= STRLITERALLEN("lxc.cgroup2.");
4511 } else if (version
== CGROUP_SUPER_MAGIC
) {
4512 global_token
= "lxc.cgroup";
4513 namespaced_token
= "lxc.cgroup.";
4514 namespaced_token_len
= STRLITERALLEN("lxc.cgroup.");
4517 return ret_errno(EINVAL
);
4520 if (strequal(key
, global_token
))
4522 else if (strnequal(key
, namespaced_token
, namespaced_token_len
))
4523 k
+= namespaced_token_len
;
4525 return ret_errno(EINVAL
);
4527 list_for_each_entry_safe(cgroup
, ncgroup
, list
, head
) {
4528 if (!all
&& !strequal(cgroup
->subsystem
, k
))
4531 list_del(&cgroup
->head
);
4532 free(cgroup
->subsystem
);
4533 free(cgroup
->value
);
4538 INIT_LIST_HEAD(list
);
4543 static inline void lxc_clear_cgroups_devices(struct lxc_conf
*conf
)
4545 lxc_clear_cgroup2_devices(&conf
->bpf_devices
);
4548 int lxc_clear_limits(struct lxc_conf
*c
, const char *key
)
4550 const char *k
= NULL
;
4552 struct lxc_limit
*lim
, *nlim
;
4554 if (strequal(key
, "lxc.limit") || strequal(key
, "lxc.prlimit"))
4556 else if (strnequal(key
, "lxc.limit.", STRLITERALLEN("lxc.limit.")))
4557 k
= key
+ STRLITERALLEN("lxc.limit.");
4558 else if (strnequal(key
, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")))
4559 k
= key
+ STRLITERALLEN("lxc.prlimit.");
4561 return ret_errno(EINVAL
);
4563 list_for_each_entry_safe(lim
, nlim
, &c
->limits
, head
) {
4564 if (!all
&& !strequal(lim
->resource
, k
))
4567 list_del(&lim
->head
);
4568 free_disarm(lim
->resource
);
4573 INIT_LIST_HEAD(&c
->limits
);
4578 int lxc_clear_sysctls(struct lxc_conf
*c
, const char *key
)
4580 const char *k
= NULL
;
4582 struct lxc_sysctl
*sysctl
, *nsysctl
;
4584 if (strequal(key
, "lxc.sysctl"))
4586 else if (strnequal(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")))
4587 k
= key
+ STRLITERALLEN("lxc.sysctl.");
4591 list_for_each_entry_safe(sysctl
, nsysctl
, &c
->sysctls
, head
) {
4592 if (!all
&& !strequal(sysctl
->key
, k
))
4595 list_del(&sysctl
->head
);
4597 free(sysctl
->value
);
4602 INIT_LIST_HEAD(&c
->sysctls
);
4607 int lxc_clear_procs(struct lxc_conf
*c
, const char *key
)
4609 const char *k
= NULL
;
4611 struct lxc_proc
*proc
, *nproc
;
4613 if (strequal(key
, "lxc.proc"))
4615 else if (strnequal(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")))
4616 k
= key
+ STRLITERALLEN("lxc.proc.");
4620 list_for_each_entry_safe(proc
, nproc
, &c
->procs
, head
) {
4621 if (!all
&& !strequal(proc
->filename
, k
))
4624 list_del(&proc
->head
);
4625 free(proc
->filename
);
4631 INIT_LIST_HEAD(&c
->procs
);
4636 int lxc_clear_groups(struct lxc_conf
*c
)
4638 struct string_entry
*entry
, *nentry
;
4640 list_for_each_entry_safe(entry
, nentry
, &c
->groups
, head
) {
4641 list_del(&entry
->head
);
4646 INIT_LIST_HEAD(&c
->groups
);
4650 int lxc_clear_environment(struct lxc_conf
*c
)
4652 struct environment_entry
*env
, *nenv
;
4654 list_for_each_entry_safe(env
, nenv
, &c
->environment
, head
) {
4655 list_del(&env
->head
);
4661 INIT_LIST_HEAD(&c
->environment
);
4665 int lxc_clear_mount_entries(struct lxc_conf
*c
)
4667 struct string_entry
*entry
, *nentry
;
4669 list_for_each_entry_safe(entry
, nentry
, &c
->mount_entries
, head
) {
4670 list_del(&entry
->head
);
4675 INIT_LIST_HEAD(&c
->mount_entries
);
4679 int lxc_clear_automounts(struct lxc_conf
*c
)
4685 int lxc_clear_hooks(struct lxc_conf
*c
, const char *key
)
4687 const char *k
= NULL
;
4688 bool all
= false, done
= false;
4689 struct string_entry
*entry
, *nentry
;
4691 if (strequal(key
, "lxc.hook"))
4693 else if (strnequal(key
, "lxc.hook.", STRLITERALLEN("lxc.hook.")))
4694 k
= key
+ STRLITERALLEN("lxc.hook.");
4698 for (int i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
4699 if (all
|| strequal(k
, lxchook_names
[i
])) {
4700 list_for_each_entry_safe(entry
, nentry
, &c
->hooks
[i
], head
) {
4701 list_del(&entry
->head
);
4705 INIT_LIST_HEAD(&c
->hooks
[i
]);
4711 return log_error(-1, "Invalid hook key: %s", key
);
4716 int lxc_clear_apparmor_raw(struct lxc_conf
*c
)
4718 struct string_entry
*entry
, *nentry
;
4720 list_for_each_entry_safe(entry
, nentry
, &c
->lsm_aa_raw
, head
) {
4721 list_del(&entry
->head
);
4726 INIT_LIST_HEAD(&c
->lsm_aa_raw
);
4730 void lxc_conf_free(struct lxc_conf
*conf
)
4735 if (current_config
== conf
)
4736 current_config
= NULL
;
4737 lxc_terminal_conf_free(&conf
->console
);
4738 free(conf
->rootfs
.mount
);
4739 free(conf
->rootfs
.bdev_type
);
4740 free(conf
->rootfs
.path
);
4741 put_lxc_rootfs(&conf
->rootfs
, true);
4742 free(conf
->logfile
);
4743 if (conf
->logfd
!= -1)
4745 free(conf
->utsname
);
4746 free(conf
->ttys
.dir
);
4747 free(conf
->ttys
.tty_names
);
4750 free(conf
->execute_cmd
);
4751 free(conf
->init_cmd
);
4752 free(conf
->init_groups
.list
);
4753 free(conf
->init_cwd
);
4754 free(conf
->unexpanded_config
);
4756 lxc_free_networks(conf
);
4757 free(conf
->lsm_aa_profile
);
4758 free(conf
->lsm_aa_profile_computed
);
4759 free(conf
->lsm_se_context
);
4760 free(conf
->lsm_se_keyring_context
);
4761 lxc_seccomp_free(&conf
->seccomp
);
4762 lxc_clear_config_caps(conf
);
4763 lxc_clear_cgroups(conf
, "lxc.cgroup", CGROUP_SUPER_MAGIC
);
4764 lxc_clear_cgroups(conf
, "lxc.cgroup2", CGROUP2_SUPER_MAGIC
);
4765 lxc_clear_cgroups_devices(conf
);
4766 lxc_clear_hooks(conf
, "lxc.hook");
4767 lxc_clear_mount_entries(conf
);
4768 lxc_clear_idmaps(conf
);
4769 lxc_clear_groups(conf
);
4770 lxc_clear_environment(conf
);
4771 lxc_clear_limits(conf
, "lxc.prlimit");
4772 lxc_clear_sysctls(conf
, "lxc.sysctl");
4773 lxc_clear_procs(conf
, "lxc.proc");
4774 lxc_clear_apparmor_raw(conf
);
4775 lxc_clear_namespace(conf
);
4776 free(conf
->cgroup_meta
.dir
);
4777 free(conf
->cgroup_meta
.monitor_dir
);
4778 free(conf
->cgroup_meta
.monitor_pivot_dir
);
4779 free(conf
->cgroup_meta
.container_dir
);
4780 free(conf
->cgroup_meta
.namespace_dir
);
4781 free(conf
->cgroup_meta
.controllers
);
4782 free(conf
->shmount
.path_host
);
4783 free(conf
->shmount
.path_cont
);
4787 struct userns_fn_data
{
4789 const char *fn_name
;
4794 static int run_userns_fn(void *data
)
4796 struct userns_fn_data
*d
= data
;
4800 close_prot_errno_disarm(d
->p
[1]);
4803 * Wait for parent to finish establishing a new mapping in the user
4804 * namespace we are executing in.
4806 ret
= lxc_read_nointr(d
->p
[0], &c
, 1);
4807 close_prot_errno_disarm(d
->p
[0]);
4812 TRACE("Calling function \"%s\"", d
->fn_name
);
4814 /* Call function to run. */
4815 return d
->fn(d
->arg
);
4818 static struct id_map
*mapped_nsid_add(const struct lxc_conf
*conf
, unsigned id
,
4821 const struct id_map
*map
;
4822 struct id_map
*retmap
;
4824 map
= find_mapped_nsid_entry(conf
, id
, idtype
);
4828 retmap
= zalloc(sizeof(*retmap
));
4832 memcpy(retmap
, map
, sizeof(*retmap
));
4836 static struct id_map
*find_mapped_hostid_entry(const struct list_head
*idmap
,
4837 unsigned id
, enum idtype idtype
)
4839 struct id_map
*retmap
= NULL
;
4842 list_for_each_entry(map
, idmap
, head
) {
4843 if (map
->idtype
!= idtype
)
4846 if (id
>= map
->hostid
&& id
< map
->hostid
+ map
->range
) {
4855 /* Allocate a new {g,u}id mapping for the given {g,u}id. Re-use an already
4856 * existing one or establish a new one.
4858 static struct id_map
*mapped_hostid_add(const struct lxc_conf
*conf
, uid_t id
,
4861 __do_free
struct id_map
*entry
= NULL
;
4863 struct id_map
*tmp
= NULL
;
4865 entry
= zalloc(sizeof(*entry
));
4869 /* Reuse existing mapping. */
4870 tmp
= find_mapped_hostid_entry(&conf
->id_map
, id
, type
);
4872 memcpy(entry
, tmp
, sizeof(*entry
));
4874 /* Find new mapping. */
4875 hostid_mapped
= find_unmapped_nsid(conf
, type
);
4876 if (hostid_mapped
< 0)
4877 return log_debug(NULL
, "Failed to find free mapping for id %d", id
);
4879 entry
->idtype
= type
;
4880 entry
->nsid
= hostid_mapped
;
4881 entry
->hostid
= (unsigned long)id
;
4885 return move_ptr(entry
);
4888 static int get_minimal_idmap(const struct lxc_conf
*conf
, uid_t
*resuid
,
4889 gid_t
*resgid
, struct list_head
*head_ret
)
4891 __do_free
struct id_map
*container_root_uid
= NULL
,
4892 *container_root_gid
= NULL
,
4893 *host_uid_map
= NULL
, *host_gid_map
= NULL
;
4895 uid_t nsuid
= (conf
->root_nsuid_map
!= NULL
) ? 0 : conf
->init_uid
;
4896 gid_t nsgid
= (conf
->root_nsgid_map
!= NULL
) ? 0 : conf
->init_gid
;
4898 /* Find container root mappings. */
4899 container_root_uid
= mapped_nsid_add(conf
, nsuid
, ID_TYPE_UID
);
4900 if (!container_root_uid
)
4901 return sysdebug("Failed to find mapping for namespace uid %d", 0);
4903 if (euid
>= container_root_uid
->hostid
&&
4904 euid
< (container_root_uid
->hostid
+ container_root_uid
->range
))
4905 host_uid_map
= move_ptr(container_root_uid
);
4907 container_root_gid
= mapped_nsid_add(conf
, nsgid
, ID_TYPE_GID
);
4908 if (!container_root_gid
)
4909 return sysdebug("Failed to find mapping for namespace gid %d", 0);
4911 if (egid
>= container_root_gid
->hostid
&&
4912 egid
< (container_root_gid
->hostid
+ container_root_gid
->range
))
4913 host_gid_map
= move_ptr(container_root_gid
);
4915 /* Check whether the {g,u}id of the user has a mapping. */
4917 host_uid_map
= mapped_hostid_add(conf
, euid
, ID_TYPE_UID
);
4919 return sysdebug("Failed to find mapping for uid %d", euid
);
4922 host_gid_map
= mapped_hostid_add(conf
, egid
, ID_TYPE_GID
);
4924 return sysdebug("Failed to find mapping for gid %d", egid
);
4926 /* idmap will now keep track of that memory. */
4927 list_add_tail(&host_uid_map
->head
, head_ret
);
4928 move_ptr(host_uid_map
);
4930 if (container_root_uid
) {
4931 /* idmap will now keep track of that memory. */
4932 list_add_tail(&container_root_uid
->head
, head_ret
);
4933 move_ptr(container_root_uid
);
4936 /* idmap will now keep track of that memory. */
4937 list_add_tail(&host_gid_map
->head
, head_ret
);
4938 move_ptr(host_gid_map
);
4940 if (container_root_gid
) {
4941 /* idmap will now keep track of that memory. */
4942 list_add_tail(&container_root_gid
->head
, head_ret
);
4943 move_ptr(container_root_gid
);
4946 TRACE("Allocated minimal idmapping for ns uid %d and ns gid %d", nsuid
, nsgid
);
4957 * Run a function in a new user namespace.
4958 * The caller's euid/egid will be mapped if it is not already.
4959 * Afaict, userns_exec_1() is only used to operate based on privileges for the
4960 * user's own {g,u}id on the host and for the container root's unmapped {g,u}id.
4961 * This means we require only to establish a mapping from:
4962 * - the container root {g,u}id as seen from the host > user's host {g,u}id
4963 * - the container root -> some sub{g,u}id
4964 * The former we add, if the user did not specify a mapping. The latter we
4965 * retrieve from the container's configured {g,u}id mappings as it must have been
4966 * there to start the container in the first place.
4968 int userns_exec_1(const struct lxc_conf
*conf
, int (*fn
)(void *), void *data
,
4969 const char *fn_name
)
4971 LIST_HEAD(minimal_idmap
);
4972 call_cleaner(__lxc_free_idmap
) struct list_head
*idmap
= &minimal_idmap
;
4973 int ret
= -1, status
= -1;
4975 struct userns_fn_data d
= {
4986 ret
= get_minimal_idmap(conf
, NULL
, NULL
, idmap
);
4988 return ret_errno(ENOENT
);
4990 ret
= pipe2(pipe_fds
, O_CLOEXEC
);
4994 d
.p
[0] = pipe_fds
[0];
4995 d
.p
[1] = pipe_fds
[1];
4997 /* Clone child in new user namespace. */
4998 pid
= lxc_raw_clone_cb(run_userns_fn
, &d
, CLONE_NEWUSER
, NULL
);
5000 ERROR("Failed to clone process in new user namespace");
5004 close_prot_errno_disarm(pipe_fds
[0]);
5006 if (lxc_log_trace()) {
5009 list_for_each_entry(map
, idmap
, head
)
5010 TRACE("Establishing %cid mapping for \"%d\" in new user namespace: nsuid %lu - hostid %lu - range %lu",
5011 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g', pid
, map
->nsid
, map
->hostid
, map
->range
);
5014 /* Set up {g,u}id mapping for user namespace of child process. */
5015 ret
= lxc_map_ids(idmap
, pid
);
5017 ERROR("Error setting up {g,u}id mappings for child process \"%d\"", pid
);
5021 /* Tell child to proceed. */
5022 if (lxc_write_nointr(pipe_fds
[1], &c
, 1) != 1) {
5023 SYSERROR("Failed telling child process \"%d\" to proceed", pid
);
5028 close_prot_errno_disarm(pipe_fds
[0]);
5029 close_prot_errno_disarm(pipe_fds
[1]);
5031 /* Wait for child to finish. */
5033 status
= wait_for_pid(pid
);
5041 int userns_exec_minimal(const struct lxc_conf
*conf
,
5042 int (*fn_parent
)(void *), void *fn_parent_data
,
5043 int (*fn_child
)(void *), void *fn_child_data
)
5045 LIST_HEAD(minimal_idmap
);
5046 call_cleaner(__lxc_free_idmap
) struct list_head
*idmap
= &minimal_idmap
;
5047 uid_t resuid
= LXC_INVALID_UID
;
5048 gid_t resgid
= LXC_INVALID_GID
;
5054 if (!conf
|| !fn_child
)
5055 return ret_errno(EINVAL
);
5057 ret
= get_minimal_idmap(conf
, &resuid
, &resgid
, idmap
);
5059 return ret_errno(ENOENT
);
5061 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
| SOCK_CLOEXEC
, 0, sock_fds
);
5067 SYSERROR("Failed to create new process");
5072 close_prot_errno_disarm(sock_fds
[1]);
5074 ret
= unshare(CLONE_NEWUSER
);
5076 SYSERROR("Failed to unshare new user namespace");
5077 _exit(EXIT_FAILURE
);
5080 ret
= lxc_write_nointr(sock_fds
[0], &c
, 1);
5082 _exit(EXIT_FAILURE
);
5084 ret
= lxc_read_nointr(sock_fds
[0], &c
, 1);
5086 _exit(EXIT_FAILURE
);
5088 close_prot_errno_disarm(sock_fds
[0]);
5090 if (!lxc_drop_groups() && errno
!= EPERM
)
5091 _exit(EXIT_FAILURE
);
5093 ret
= setresgid(resgid
, resgid
, resgid
);
5095 SYSERROR("Failed to setresgid(%d, %d, %d)",
5096 resgid
, resgid
, resgid
);
5097 _exit(EXIT_FAILURE
);
5100 ret
= setresuid(resuid
, resuid
, resuid
);
5102 SYSERROR("Failed to setresuid(%d, %d, %d)",
5103 resuid
, resuid
, resuid
);
5104 _exit(EXIT_FAILURE
);
5107 ret
= fn_child(fn_child_data
);
5109 SYSERROR("Running function in new user namespace failed");
5110 _exit(EXIT_FAILURE
);
5113 _exit(EXIT_SUCCESS
);
5116 close_prot_errno_disarm(sock_fds
[0]);
5118 if (lxc_log_trace()) {
5121 list_for_each_entry(map
, idmap
, head
)
5122 TRACE("Establishing %cid mapping for \"%d\" in new user namespace: nsuid %lu - hostid %lu - range %lu",
5123 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g', pid
, map
->nsid
, map
->hostid
, map
->range
);
5126 ret
= lxc_read_nointr(sock_fds
[1], &c
, 1);
5128 SYSERROR("Failed waiting for child process %d\" to tell us to proceed", pid
);
5132 /* Set up {g,u}id mapping for user namespace of child process. */
5133 ret
= lxc_map_ids(idmap
, pid
);
5135 ERROR("Error setting up {g,u}id mappings for child process \"%d\"", pid
);
5139 /* Tell child to proceed. */
5140 ret
= lxc_write_nointr(sock_fds
[1], &c
, 1);
5142 SYSERROR("Failed telling child process \"%d\" to proceed", pid
);
5146 if (fn_parent
&& fn_parent(fn_parent_data
)) {
5147 SYSERROR("Running parent function failed");
5148 _exit(EXIT_FAILURE
);
5152 close_prot_errno_disarm(sock_fds
[0]);
5153 close_prot_errno_disarm(sock_fds
[1]);
5155 /* Wait for child to finish. */
5159 return wait_for_pid(pid
);
5162 int userns_exec_full(struct lxc_conf
*conf
, int (*fn
)(void *), void *data
,
5163 const char *fn_name
)
5165 LIST_HEAD(full_idmap
);
5168 struct id_map
*container_root_uid
= NULL
, *container_root_gid
= NULL
,
5169 *host_uid_map
= NULL
, *host_gid_map
= NULL
;
5174 struct userns_fn_data d
;
5179 ret
= pipe2(p
, O_CLOEXEC
);
5184 d
.fn_name
= fn_name
;
5189 /* Clone child in new user namespace. */
5190 pid
= lxc_clone(run_userns_fn
, &d
, CLONE_NEWUSER
, NULL
);
5192 ERROR("Failed to clone process in new user namespace");
5202 /* Find container root. */
5203 list_for_each_entry(map
, &conf
->id_map
, head
) {
5204 __do_free
struct id_map
*dup_map
= NULL
;
5206 dup_map
= memdup(map
, sizeof(struct id_map
));
5210 list_add_tail(&dup_map
->head
, &full_idmap
);
5213 if (map
->idtype
== ID_TYPE_UID
)
5214 if (euid
>= map
->hostid
&& euid
< map
->hostid
+ map
->range
)
5217 if (map
->idtype
== ID_TYPE_GID
)
5218 if (egid
>= map
->hostid
&& egid
< map
->hostid
+ map
->range
)
5224 if (map
->idtype
== ID_TYPE_UID
)
5225 if (container_root_uid
== NULL
)
5226 container_root_uid
= map
;
5228 if (map
->idtype
== ID_TYPE_GID
)
5229 if (container_root_gid
== NULL
)
5230 container_root_gid
= map
;
5233 if (!container_root_uid
|| !container_root_gid
) {
5234 ERROR("No mapping for container root found");
5238 /* Check whether the {g,u}id of the user has a mapping. */
5240 host_uid_map
= mapped_hostid_add(conf
, euid
, ID_TYPE_UID
);
5242 host_uid_map
= container_root_uid
;
5245 host_gid_map
= mapped_hostid_add(conf
, egid
, ID_TYPE_GID
);
5247 host_gid_map
= container_root_gid
;
5249 if (!host_uid_map
) {
5250 DEBUG("Failed to find mapping for uid %d", euid
);
5254 if (!host_gid_map
) {
5255 DEBUG("Failed to find mapping for gid %d", egid
);
5259 if (host_uid_map
&& (host_uid_map
!= container_root_uid
)) {
5260 /* idmap will now keep track of that memory. */
5261 list_add_tail(&host_uid_map
->head
, &full_idmap
);
5262 move_ptr(host_uid_map
);
5265 if (host_gid_map
&& (host_gid_map
!= container_root_gid
)) {
5266 /* idmap will now keep track of that memory. */
5267 list_add_tail(&host_gid_map
->head
, &full_idmap
);
5268 move_ptr(host_gid_map
);
5271 if (lxc_log_trace()) {
5272 list_for_each_entry(map
, &full_idmap
, head
) {
5273 TRACE("establishing %cid mapping for \"%d\" in new user namespace: nsuid %lu - hostid %lu - range %lu",
5274 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g', pid
,
5275 map
->nsid
, map
->hostid
, map
->range
);
5279 /* Set up {g,u}id mapping for user namespace of child process. */
5280 ret
= lxc_map_ids(&full_idmap
, pid
);
5282 ERROR("error setting up {g,u}id mappings for child process \"%d\"", pid
);
5286 /* Tell child to proceed. */
5287 if (lxc_write_nointr(p
[1], &c
, 1) != 1) {
5288 SYSERROR("Failed telling child process \"%d\" to proceed", pid
);
5297 /* Wait for child to finish. */
5299 ret
= wait_for_pid(pid
);
5301 __lxc_free_idmap(&full_idmap
);
5303 if (host_uid_map
&& (host_uid_map
!= container_root_uid
))
5305 if (host_gid_map
&& (host_gid_map
!= container_root_gid
))
5311 static int add_idmap_entry(struct list_head
*idmap_list
, enum idtype idtype
,
5312 unsigned long nsid
, unsigned long hostid
,
5313 unsigned long range
)
5315 __do_free
struct id_map
*new_idmap
= NULL
;
5317 new_idmap
= zalloc(sizeof(*new_idmap
));
5319 return ret_errno(ENOMEM
);
5321 new_idmap
->idtype
= idtype
;
5322 new_idmap
->hostid
= hostid
;
5323 new_idmap
->nsid
= nsid
;
5324 new_idmap
->range
= range
;
5326 list_add_tail(&new_idmap
->head
, idmap_list
);
5327 move_ptr(new_idmap
);
5329 INFO("Adding id map: type %c nsid %lu hostid %lu range %lu",
5330 idtype
== ID_TYPE_UID
? 'u' : 'g', nsid
, hostid
, range
);
5334 int userns_exec_mapped_root(const char *path
, int path_fd
,
5335 const struct lxc_conf
*conf
)
5337 LIST_HEAD(idmap_list
);
5338 call_cleaner(__lxc_free_idmap
) struct list_head
*idmap
= &idmap_list
;
5339 __do_close
int fd
= -EBADF
;
5340 int target_fd
= -EBADF
;
5345 uid_t container_host_uid
, hostuid
;
5346 gid_t container_host_gid
, hostgid
;
5349 if (!conf
|| (!path
&& path_fd
< 0))
5350 return ret_errno(EINVAL
);
5355 container_host_uid
= get_mapped_rootid(conf
, ID_TYPE_UID
);
5356 if (!uid_valid(container_host_uid
))
5357 return log_error(-1, "No uid mapping for container root");
5359 container_host_gid
= get_mapped_rootid(conf
, ID_TYPE_GID
);
5360 if (!gid_valid(container_host_gid
))
5361 return log_error(-1, "No gid mapping for container root");
5364 fd
= open(path
, O_CLOEXEC
| O_NOCTTY
);
5366 return log_error_errno(-errno
, errno
, "Failed to open \"%s\"", path
);
5369 target_fd
= path_fd
;
5372 hostuid
= geteuid();
5373 /* We are root so chown directly. */
5375 ret
= fchown(target_fd
, container_host_uid
, container_host_gid
);
5377 return log_error_errno(-errno
, errno
,
5378 "Failed to fchown(%d(%s), %d, %d)",
5379 target_fd
, path
, container_host_uid
,
5380 container_host_gid
);
5381 return log_trace(0, "Chowned %d(%s) to uid %d and %d", target_fd
, path
,
5382 container_host_uid
, container_host_gid
);
5385 /* The container's root host id matches */
5386 if (container_host_uid
== hostuid
)
5387 return log_info(0, "Container root id is mapped to our uid");
5389 /* Get the current ids of our target. */
5390 ret
= fstat(target_fd
, &st
);
5392 return log_error_errno(-errno
, errno
, "Failed to stat \"%s\"", path
);
5394 hostgid
= getegid();
5395 if (st
.st_uid
== hostuid
&& mapped_hostid(st
.st_gid
, conf
, ID_TYPE_GID
) < 0) {
5396 ret
= fchown(target_fd
, -1, hostgid
);
5398 return log_error_errno(-errno
, errno
,
5399 "Failed to fchown(%d(%s), -1, %d)",
5400 target_fd
, path
, hostgid
);
5401 TRACE("Chowned %d(%s) to -1:%d", target_fd
, path
, hostgid
);
5404 /* "u:0:rootuid:1" */
5405 ret
= add_idmap_entry(idmap
, ID_TYPE_UID
, 0, container_host_uid
, 1);
5407 return log_error_errno(ret
, -ret
, "Failed to add idmap entry");
5409 /* "u:hostuid:hostuid:1" */
5410 ret
= add_idmap_entry(idmap
, ID_TYPE_UID
, hostuid
, hostuid
, 1);
5412 return log_error_errno(ret
, -ret
, "Failed to add idmap entry");
5414 /* "g:0:rootgid:1" */
5415 ret
= add_idmap_entry(idmap
, ID_TYPE_GID
, 0, container_host_gid
, 1);
5417 return log_error_errno(ret
, -ret
, "Failed to add idmap entry");
5419 /* "g:hostgid:hostgid:1" */
5420 ret
= add_idmap_entry(idmap
, ID_TYPE_GID
, hostgid
, hostgid
, 1);
5422 return log_error_errno(ret
, -ret
, "Failed to add idmap entry");
5424 if (hostgid
!= st
.st_gid
) {
5425 /* "g:pathgid:rootgid+pathgid:1" */
5426 ret
= add_idmap_entry(idmap
, ID_TYPE_GID
, st
.st_gid
,
5427 container_host_gid
+ (gid_t
)st
.st_gid
, 1);
5429 return log_error_errno(ret
, -ret
, "Failed to add idmap entry");
5432 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
| SOCK_CLOEXEC
, 0, sock_fds
);
5438 SYSERROR("Failed to create new process");
5443 close_prot_errno_disarm(sock_fds
[1]);
5445 ret
= unshare(CLONE_NEWUSER
);
5447 SYSERROR("Failed to unshare new user namespace");
5448 _exit(EXIT_FAILURE
);
5451 ret
= lxc_write_nointr(sock_fds
[0], &c
, 1);
5453 _exit(EXIT_FAILURE
);
5455 ret
= lxc_read_nointr(sock_fds
[0], &c
, 1);
5457 _exit(EXIT_FAILURE
);
5459 close_prot_errno_disarm(sock_fds
[0]);
5461 if (!lxc_switch_uid_gid(0, 0))
5462 _exit(EXIT_FAILURE
);
5464 if (!lxc_drop_groups())
5465 _exit(EXIT_FAILURE
);
5467 ret
= fchown(target_fd
, 0, st
.st_gid
);
5469 SYSERROR("Failed to chown %d(%s) to 0:%d", target_fd
, path
, st
.st_gid
);
5470 _exit(EXIT_FAILURE
);
5473 TRACE("Chowned %d(%s) to 0:%d", target_fd
, path
, st
.st_gid
);
5474 _exit(EXIT_SUCCESS
);
5477 close_prot_errno_disarm(sock_fds
[0]);
5479 if (lxc_log_trace()) {
5482 list_for_each_entry(map
, idmap
, head
)
5483 TRACE("Establishing %cid mapping for \"%d\" in new user namespace: nsuid %lu - hostid %lu - range %lu",
5484 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g', pid
, map
->nsid
, map
->hostid
, map
->range
);
5487 ret
= lxc_read_nointr(sock_fds
[1], &c
, 1);
5489 SYSERROR("Failed waiting for child process %d\" to tell us to proceed", pid
);
5493 /* Set up {g,u}id mapping for user namespace of child process. */
5494 ret
= lxc_map_ids(idmap
, pid
);
5496 ERROR("Error setting up {g,u}id mappings for child process \"%d\"", pid
);
5500 /* Tell child to proceed. */
5501 ret
= lxc_write_nointr(sock_fds
[1], &c
, 1);
5503 SYSERROR("Failed telling child process \"%d\" to proceed", pid
);
5508 close_prot_errno_disarm(sock_fds
[0]);
5509 close_prot_errno_disarm(sock_fds
[1]);
5511 /* Wait for child to finish. */
5515 return wait_for_pid(pid
);
5518 /* not thread-safe, do not use from api without first forking */
5519 static char *getuname(void)
5521 __do_free
char *buf
= NULL
;
5522 struct passwd pwent
;
5523 struct passwd
*pwentp
= NULL
;
5527 bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
);
5531 buf
= zalloc(bufsize
);
5535 ret
= getpwuid_r(geteuid(), &pwent
, buf
, bufsize
, &pwentp
);
5538 WARN("Could not find matched password record.");
5540 return log_error(NULL
, "Failed to get password record - %u", geteuid());
5543 return strdup(pwent
.pw_name
);
5546 /* not thread-safe, do not use from api without first forking */
5547 static char *getgname(void)
5549 __do_free
char *buf
= NULL
;
5551 struct group
*grentp
= NULL
;
5555 bufsize
= sysconf(_SC_GETGR_R_SIZE_MAX
);
5559 buf
= zalloc(bufsize
);
5563 ret
= getgrgid_r(getegid(), &grent
, buf
, bufsize
, &grentp
);
5566 WARN("Could not find matched group record");
5568 return log_error(NULL
, "Failed to get group record - %u", getegid());
5571 return strdup(grent
.gr_name
);
5574 /* not thread-safe, do not use from api without first forking */
5575 void suggest_default_idmap(void)
5577 __do_free
char *gname
= NULL
, *line
= NULL
, *uname
= NULL
;
5578 __do_fclose
FILE *subuid_f
= NULL
, *subgid_f
= NULL
;
5579 unsigned int uid
= 0, urange
= 0, gid
= 0, grange
= 0;
5590 subuid_f
= fopen(subuidfile
, "re");
5592 ERROR("Your system is not configured with subuids");
5596 while (getline(&line
, &len
, subuid_f
) != -1) {
5598 size_t no_newline
= 0;
5600 p
= strchr(line
, ':');
5608 if (!strequal(line
, uname
))
5611 p2
= strchr(p
, ':');
5618 no_newline
= strcspn(p2
, "\n");
5619 p2
[no_newline
] = '\0';
5621 if (lxc_safe_uint(p
, &uid
) < 0)
5622 WARN("Could not parse UID");
5623 if (lxc_safe_uint(p2
, &urange
) < 0)
5624 WARN("Could not parse UID range");
5627 subgid_f
= fopen(subgidfile
, "re");
5629 ERROR("Your system is not configured with subgids");
5633 while (getline(&line
, &len
, subgid_f
) != -1) {
5635 size_t no_newline
= 0;
5637 p
= strchr(line
, ':');
5645 if (!strequal(line
, uname
))
5648 p2
= strchr(p
, ':');
5655 no_newline
= strcspn(p2
, "\n");
5656 p2
[no_newline
] = '\0';
5658 if (lxc_safe_uint(p
, &gid
) < 0)
5659 WARN("Could not parse GID");
5660 if (lxc_safe_uint(p2
, &grange
) < 0)
5661 WARN("Could not parse GID range");
5664 if (!urange
|| !grange
) {
5665 ERROR("You do not have subuids or subgids allocated");
5666 ERROR("Unprivileged containers require subuids and subgids");
5670 ERROR("You must either run as root, or define uid mappings");
5671 ERROR("To pass uid mappings to lxc-create, you could create");
5672 ERROR("~/.config/lxc/default.conf:");
5673 ERROR("lxc.include = %s", LXC_DEFAULT_CONFIG
);
5674 ERROR("lxc.idmap = u 0 %u %u", uid
, urange
);
5675 ERROR("lxc.idmap = g 0 %u %u", gid
, grange
);
5678 int lxc_set_environment(const struct lxc_conf
*conf
)
5680 struct environment_entry
*env
;
5682 list_for_each_entry(env
, &conf
->environment
, head
) {
5685 ret
= setenv(env
->key
, env
->val
, 1);
5687 return syserror("Failed to set environment variable: %s=%s",
5688 env
->key
, env
->val
);
5689 TRACE("Set environment variable: %s=%s", env
->key
, env
->val
);