]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/namespace.c
lxc_clone: pass non-stack allocated stack to clone
[mirror_lxc.git] / src / lxc / namespace.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2009
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE 1
26 #endif
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <sched.h>
30 #include <signal.h>
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #include <sys/syscall.h>
34 #include <sys/types.h>
35 #include <unistd.h>
36
37 #include "config.h"
38 #include "log.h"
39 #include "memory_utils.h"
40 #include "namespace.h"
41 #include "utils.h"
42
43 lxc_log_define(namespace, lxc);
44
45 struct clone_arg {
46 int (*fn)(void *);
47 void *arg;
48 };
49
50 static int do_clone(void *arg)
51 {
52 struct clone_arg *clone_arg = arg;
53 return clone_arg->fn(clone_arg->arg);
54 }
55
56 #define __LXC_STACK_SIZE 4096
57 pid_t lxc_clone(int (*fn)(void *), void *arg, int flags, int *pidfd)
58 {
59 pid_t ret;
60 struct clone_arg clone_arg = {
61 .fn = fn,
62 .arg = arg,
63 };
64 void *stack;
65
66 stack = malloc(__LXC_STACK_SIZE);
67 if (!stack) {
68 SYSERROR("Failed to allocate clone stack");
69 return -ENOMEM;
70 }
71
72 #ifdef __ia64__
73 ret = __clone2(fn, stack, __LXC_STACK_SIZE, flags | SIGCHLD, &clone_arg, pidfd);
74 #else
75 ret = clone(fn, stack + __LXC_STACK_SIZE, flags | SIGCHLD, &clone_arg, pidfd);
76 #endif
77 if (ret < 0)
78 SYSERROR("Failed to clone (%#x)", flags);
79
80 return ret;
81 }
82
83 /* Leave the user namespace at the first position in the array of structs so
84 * that we always attach to it first when iterating over the struct and using
85 * setns() to switch namespaces. This especially affects lxc_attach(): Suppose
86 * you cloned a new user namespace and mount namespace as an unprivileged user
87 * on the host and want to setns() to the mount namespace. This requires you to
88 * attach to the user namespace first otherwise the kernel will fail this check:
89 *
90 * if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
91 * !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
92 * !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
93 * return -EPERM;
94 *
95 * in
96 *
97 * linux/fs/namespace.c:mntns_install().
98 */
99 const struct ns_info ns_info[LXC_NS_MAX] = {
100 [LXC_NS_USER] = { "user", CLONE_NEWUSER, "CLONE_NEWUSER", "LXC_USER_NS" },
101 [LXC_NS_MNT] = { "mnt", CLONE_NEWNS, "CLONE_NEWNS", "LXC_MNT_NS" },
102 [LXC_NS_PID] = { "pid", CLONE_NEWPID, "CLONE_NEWPID", "LXC_PID_NS" },
103 [LXC_NS_UTS] = { "uts", CLONE_NEWUTS, "CLONE_NEWUTS", "LXC_UTS_NS" },
104 [LXC_NS_IPC] = { "ipc", CLONE_NEWIPC, "CLONE_NEWIPC", "LXC_IPC_NS" },
105 [LXC_NS_NET] = { "net", CLONE_NEWNET, "CLONE_NEWNET", "LXC_NET_NS" },
106 [LXC_NS_CGROUP] = { "cgroup", CLONE_NEWCGROUP, "CLONE_NEWCGROUP", "LXC_CGROUP_NS" }
107 };
108
109 int lxc_namespace_2_cloneflag(const char *namespace)
110 {
111 int i;
112
113 for (i = 0; i < LXC_NS_MAX; i++)
114 if (!strcasecmp(ns_info[i].proc_name, namespace))
115 return ns_info[i].clone_flag;
116
117 ERROR("Invalid namespace name \"%s\"", namespace);
118 return -EINVAL;
119 }
120
121 int lxc_namespace_2_ns_idx(const char *namespace)
122 {
123 int i;
124
125 for (i = 0; i < LXC_NS_MAX; i++)
126 if (!strcmp(ns_info[i].proc_name, namespace))
127 return i;
128
129 ERROR("Invalid namespace name \"%s\"", namespace);
130 return -EINVAL;
131 }
132
133 extern int lxc_namespace_2_std_identifiers(char *namespaces)
134 {
135 char **it;
136 char *del;
137
138 /* The identifiers for namespaces used with lxc-attach and lxc-unshare
139 * as given on the manpage do not align with the standard identifiers.
140 * This affects network, mount, and uts namespaces. The standard identifiers
141 * are: "mnt", "uts", and "net" whereas lxc-attach and lxc-unshare uses
142 * "MOUNT", "UTSNAME", and "NETWORK". So let's use some cheap memmove()s
143 * to replace them by their standard identifiers.
144 * Let's illustrate this with an example:
145 * Assume the string:
146 *
147 * "IPC|MOUNT|PID"
148 *
149 * then we memmove()
150 *
151 * dest: del + 1 == OUNT|PID
152 * src: del + 3 == NT|PID
153 */
154 if (!namespaces)
155 return -1;
156
157 while ((del = strstr(namespaces, "MOUNT")))
158 memmove(del + 1, del + 3, strlen(del) - 2);
159
160 for (it = (char *[]){"NETWORK", "UTSNAME", NULL}; it && *it; it++)
161 while ((del = strstr(namespaces, *it)))
162 memmove(del + 3, del + 7, strlen(del) - 6);
163
164 return 0;
165 }
166
167 int lxc_fill_namespace_flags(char *flaglist, int *flags)
168 {
169 char *token;
170 int aflag;
171
172 if (!flaglist) {
173 ERROR("At least one namespace is needed.");
174 return -1;
175 }
176
177 lxc_iterate_parts(token, flaglist, "|") {
178 aflag = lxc_namespace_2_cloneflag(token);
179 if (aflag < 0)
180 return -1;
181
182 *flags |= aflag;
183 }
184
185 return 0;
186 }