]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/namespace.c
Merge pull request #3235 from xinhua9569/master
[mirror_lxc.git] / src / lxc / namespace.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef _GNU_SOURCE
4 #define _GNU_SOURCE 1
5 #endif
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <sched.h>
9 #include <signal.h>
10 #include <sys/param.h>
11 #include <sys/stat.h>
12 #include <sys/syscall.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15
16 #include "config.h"
17 #include "log.h"
18 #include "memory_utils.h"
19 #include "namespace.h"
20 #include "utils.h"
21
22 lxc_log_define(namespace, lxc);
23
24 /*
25 * Let's use the "standard stack limit" (i.e. glibc thread size default) for
26 * stack sizes: 8MB.
27 */
28 #define __LXC_STACK_SIZE (8 * 1024 * 1024)
29 pid_t lxc_clone(int (*fn)(void *), void *arg, int flags, int *pidfd)
30 {
31 pid_t ret;
32 void *stack;
33
34 stack = malloc(__LXC_STACK_SIZE);
35 if (!stack) {
36 SYSERROR("Failed to allocate clone stack");
37 return -ENOMEM;
38 }
39
40 #ifdef __ia64__
41 ret = __clone2(fn, stack, __LXC_STACK_SIZE, flags | SIGCHLD, arg, pidfd);
42 #else
43 ret = clone(fn, stack + __LXC_STACK_SIZE, flags | SIGCHLD, arg, pidfd);
44 #endif
45 if (ret < 0)
46 SYSERROR("Failed to clone (%#x)", flags);
47
48 return ret;
49 }
50
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:
57 *
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))
61 * return -EPERM;
62 *
63 * in
64 *
65 * linux/fs/namespace.c:mntns_install().
66 */
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" }
75 };
76
77 int lxc_namespace_2_cloneflag(const char *namespace)
78 {
79 int i;
80
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;
84
85 ERROR("Invalid namespace name \"%s\"", namespace);
86 return -EINVAL;
87 }
88
89 int lxc_namespace_2_ns_idx(const char *namespace)
90 {
91 int i;
92
93 for (i = 0; i < LXC_NS_MAX; i++)
94 if (!strcmp(ns_info[i].proc_name, namespace))
95 return i;
96
97 ERROR("Invalid namespace name \"%s\"", namespace);
98 return -EINVAL;
99 }
100
101 extern int lxc_namespace_2_std_identifiers(char *namespaces)
102 {
103 char **it;
104 char *del;
105
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:
113 * Assume the string:
114 *
115 * "IPC|MOUNT|PID"
116 *
117 * then we memmove()
118 *
119 * dest: del + 1 == OUNT|PID
120 * src: del + 3 == NT|PID
121 */
122 if (!namespaces)
123 return -1;
124
125 while ((del = strstr(namespaces, "MOUNT")))
126 memmove(del + 1, del + 3, strlen(del) - 2);
127
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);
131
132 return 0;
133 }
134
135 int lxc_fill_namespace_flags(char *flaglist, int *flags)
136 {
137 char *token;
138 int aflag;
139
140 if (!flaglist) {
141 ERROR("At least one namespace is needed.");
142 return -1;
143 }
144
145 lxc_iterate_parts(token, flaglist, "|") {
146 aflag = lxc_namespace_2_cloneflag(token);
147 if (aflag < 0)
148 return -1;
149
150 *flags |= aflag;
151 }
152
153 return 0;
154 }