]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/namespace.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/param.h>
12 #include <sys/syscall.h>
13 #include <sys/types.h>
18 #include "memory_utils.h"
19 #include "namespace.h"
22 lxc_log_define(namespace, lxc
);
25 * Let's use the "standard stack limit" (i.e. glibc thread size default) for
28 #define __LXC_STACK_SIZE (8 * 1024 * 1024)
29 pid_t
lxc_clone(int (*fn
)(void *), void *arg
, int flags
, int *pidfd
)
34 stack
= malloc(__LXC_STACK_SIZE
);
36 SYSERROR("Failed to allocate clone stack");
41 ret
= __clone2(fn
, stack
, __LXC_STACK_SIZE
, flags
| SIGCHLD
, arg
, pidfd
);
43 ret
= clone(fn
, stack
+ __LXC_STACK_SIZE
, flags
| SIGCHLD
, arg
, pidfd
);
46 SYSERROR("Failed to clone (%#x)", flags
);
51 /* Leave the user namespace at the first position in the array of structs so
52 * that we always attach to it first when iterating over the struct and using
53 * setns() to switch namespaces. This especially affects lxc_attach(): Suppose
54 * you cloned a new user namespace and mount namespace as an unprivileged user
55 * on the host and want to setns() to the mount namespace. This requires you to
56 * attach to the user namespace first otherwise the kernel will fail this check:
58 * if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
59 * !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
60 * !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
65 * linux/fs/namespace.c:mntns_install().
67 const struct ns_info ns_info
[LXC_NS_MAX
] = {
68 [LXC_NS_USER
] = { "user", CLONE_NEWUSER
, "CLONE_NEWUSER", "LXC_USER_NS" },
69 [LXC_NS_MNT
] = { "mnt", CLONE_NEWNS
, "CLONE_NEWNS", "LXC_MNT_NS" },
70 [LXC_NS_PID
] = { "pid", CLONE_NEWPID
, "CLONE_NEWPID", "LXC_PID_NS" },
71 [LXC_NS_UTS
] = { "uts", CLONE_NEWUTS
, "CLONE_NEWUTS", "LXC_UTS_NS" },
72 [LXC_NS_IPC
] = { "ipc", CLONE_NEWIPC
, "CLONE_NEWIPC", "LXC_IPC_NS" },
73 [LXC_NS_NET
] = { "net", CLONE_NEWNET
, "CLONE_NEWNET", "LXC_NET_NS" },
74 [LXC_NS_CGROUP
] = { "cgroup", CLONE_NEWCGROUP
, "CLONE_NEWCGROUP", "LXC_CGROUP_NS" }
77 int lxc_namespace_2_cloneflag(const char *namespace)
81 for (i
= 0; i
< LXC_NS_MAX
; i
++)
82 if (!strcasecmp(ns_info
[i
].proc_name
, namespace))
83 return ns_info
[i
].clone_flag
;
85 ERROR("Invalid namespace name \"%s\"", namespace);
89 int lxc_namespace_2_ns_idx(const char *namespace)
93 for (i
= 0; i
< LXC_NS_MAX
; i
++)
94 if (!strcmp(ns_info
[i
].proc_name
, namespace))
97 ERROR("Invalid namespace name \"%s\"", namespace);
101 extern int lxc_namespace_2_std_identifiers(char *namespaces
)
106 /* The identifiers for namespaces used with lxc-attach and lxc-unshare
107 * as given on the manpage do not align with the standard identifiers.
108 * This affects network, mount, and uts namespaces. The standard identifiers
109 * are: "mnt", "uts", and "net" whereas lxc-attach and lxc-unshare uses
110 * "MOUNT", "UTSNAME", and "NETWORK". So let's use some cheap memmove()s
111 * to replace them by their standard identifiers.
112 * Let's illustrate this with an example:
119 * dest: del + 1 == OUNT|PID
120 * src: del + 3 == NT|PID
125 while ((del
= strstr(namespaces
, "MOUNT")))
126 memmove(del
+ 1, del
+ 3, strlen(del
) - 2);
128 for (it
= (char *[]){"NETWORK", "UTSNAME", NULL
}; it
&& *it
; it
++)
129 while ((del
= strstr(namespaces
, *it
)))
130 memmove(del
+ 3, del
+ 7, strlen(del
) - 6);
135 int lxc_fill_namespace_flags(char *flaglist
, int *flags
)
141 ERROR("At least one namespace is needed.");
145 lxc_iterate_parts(token
, flaglist
, "|") {
146 aflag
= lxc_namespace_2_cloneflag(token
);