]>
Commit | Line | Data |
---|---|---|
0ad19a3f | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
9afe19d6 | 7 | * Daniel Lezcano <daniel.lezcano at free.fr> |
0ad19a3f | 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 | |
250b1eec | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0ad19a3f | 22 | */ |
1d52bdf7 CB |
23 | |
24 | #define _GNU_SOURCE | |
d06245b8 NC |
25 | #include "config.h" |
26 | ||
8f3e280e CB |
27 | #include <dirent.h> |
28 | #include <errno.h> | |
29 | #include <fcntl.h> | |
30 | #include <grp.h> | |
31 | #include <inttypes.h> | |
32 | #include <libgen.h> | |
33 | #include <pwd.h> | |
34 | #include <stdarg.h> | |
0ad19a3f | 35 | #include <stdio.h> |
0ad19a3f | 36 | #include <stdlib.h> |
0ad19a3f | 37 | #include <string.h> |
8f3e280e | 38 | #include <time.h> |
0ad19a3f | 39 | #include <unistd.h> |
8f3e280e CB |
40 | #include <arpa/inet.h> |
41 | #include <linux/loop.h> | |
8f3e280e CB |
42 | #include <net/if.h> |
43 | #include <netinet/in.h> | |
44 | #include <sys/mman.h> | |
45 | #include <sys/mount.h> | |
46 | #include <sys/param.h> | |
47 | #include <sys/prctl.h> | |
48 | #include <sys/stat.h> | |
49 | #include <sys/socket.h> | |
ce831b3b | 50 | #include <sys/sysmacros.h> |
2d76d1d7 | 51 | #include <sys/syscall.h> |
97e9cfa0 | 52 | #include <sys/types.h> |
8f3e280e CB |
53 | #include <sys/utsname.h> |
54 | #include <sys/wait.h> | |
1d52bdf7 | 55 | |
af6824fc ST |
56 | /* makedev() */ |
57 | #ifdef MAJOR_IN_MKDEV | |
58 | # include <sys/mkdev.h> | |
59 | #endif | |
af6824fc | 60 | |
614305f3 | 61 | #ifdef HAVE_STATVFS |
2938f7c8 | 62 | #include <sys/statvfs.h> |
614305f3 | 63 | #endif |
e827ff7e SG |
64 | |
65 | #if HAVE_PTY_H | |
b0a33c1e | 66 | #include <pty.h> |
e827ff7e SG |
67 | #else |
68 | #include <../include/openpty.h> | |
69 | #endif | |
0ad19a3f | 70 | |
e8bd4e43 | 71 | #include "af_unix.h" |
8f3e280e CB |
72 | #include "caps.h" /* for lxc_caps_last_cap() */ |
73 | #include "cgroup.h" | |
1b09f2c0 | 74 | #include "conf.h" |
1ed6ba91 | 75 | #include "confile_utils.h" |
8f3e280e | 76 | #include "error.h" |
1b09f2c0 | 77 | #include "log.h" |
0ed9b1bc | 78 | #include "lsm/lsm.h" |
025ed0f3 | 79 | #include "lxclock.h" |
8f3e280e | 80 | #include "lxcseccomp.h" |
4355ab5f | 81 | #include "namespace.h" |
8f3e280e CB |
82 | #include "network.h" |
83 | #include "parse.h" | |
732375f5 | 84 | #include "ringbuf.h" |
28d832c4 CB |
85 | #include "storage.h" |
86 | #include "storage/aufs.h" | |
87 | #include "storage/overlay.h" | |
0ed9b1bc | 88 | #include "terminal.h" |
8f3e280e | 89 | #include "utils.h" |
d0a36f2c | 90 | |
e37dda71 | 91 | #if HAVE_LIBCAP |
495d2046 SG |
92 | #include <sys/capability.h> |
93 | #endif | |
94 | ||
6ff05e18 SG |
95 | #if HAVE_SYS_PERSONALITY_H |
96 | #include <sys/personality.h> | |
97 | #endif | |
98 | ||
edaf8b1b SG |
99 | #if IS_BIONIC |
100 | #include <../include/lxcmntent.h> | |
101 | #else | |
102 | #include <mntent.h> | |
103 | #endif | |
104 | ||
f48b5fd8 FF |
105 | #if !defined(HAVE_PRLIMIT) && defined(HAVE_PRLIMIT64) |
106 | #include <../include/prlimit.h> | |
107 | #endif | |
108 | ||
36eb9bde | 109 | lxc_log_define(lxc_conf, lxc); |
e5bda9ee | 110 | |
2d76d1d7 SG |
111 | /* Define pivot_root() if missing from the C library */ |
112 | #ifndef HAVE_PIVOT_ROOT | |
113 | static int pivot_root(const char * new_root, const char * put_old) | |
114 | { | |
115 | #ifdef __NR_pivot_root | |
8f3e280e | 116 | return syscall(__NR_pivot_root, new_root, put_old); |
2d76d1d7 | 117 | #else |
8f3e280e CB |
118 | errno = ENOSYS; |
119 | return -1; | |
2d76d1d7 SG |
120 | #endif |
121 | } | |
122 | #else | |
123 | extern int pivot_root(const char * new_root, const char * put_old); | |
124 | #endif | |
125 | ||
ecec0126 SG |
126 | #ifndef MS_PRIVATE |
127 | #define MS_PRIVATE (1<<18) | |
128 | #endif | |
129 | ||
8912711c CB |
130 | #ifndef MS_LAZYTIME |
131 | #define MS_LAZYTIME (1<<25) | |
132 | #endif | |
133 | ||
2b9ae35a CB |
134 | char *lxchook_names[NUM_LXC_HOOKS] = {"pre-start", "pre-mount", "mount", |
135 | "autodev", "start", "stop", | |
08dd2805 SH |
136 | "post-stop", "clone", "destroy", |
137 | "start-host"}; | |
72d0e1cb | 138 | |
998ac676 RT |
139 | struct mount_opt { |
140 | char *name; | |
141 | int clear; | |
142 | int flag; | |
143 | }; | |
144 | ||
81810dd1 DL |
145 | struct caps_opt { |
146 | char *name; | |
147 | int value; | |
148 | }; | |
149 | ||
c6d09e15 WB |
150 | struct limit_opt { |
151 | char *name; | |
152 | int value; | |
153 | }; | |
154 | ||
858377e4 SH |
155 | /* |
156 | * The lxc_conf of the container currently being worked on in an | |
157 | * API call | |
158 | * This is used in the error calls | |
159 | */ | |
160 | #ifdef HAVE_TLS | |
161 | __thread struct lxc_conf *current_config; | |
162 | #else | |
163 | struct lxc_conf *current_config; | |
164 | #endif | |
165 | ||
998ac676 | 166 | static struct mount_opt mount_opt[] = { |
470b359b CB |
167 | { "async", 1, MS_SYNCHRONOUS }, |
168 | { "atime", 1, MS_NOATIME }, | |
169 | { "bind", 0, MS_BIND }, | |
88d413d5 | 170 | { "defaults", 0, 0 }, |
88d413d5 | 171 | { "dev", 1, MS_NODEV }, |
470b359b | 172 | { "diratime", 1, MS_NODIRATIME }, |
88d413d5 | 173 | { "dirsync", 0, MS_DIRSYNC }, |
470b359b | 174 | { "exec", 1, MS_NOEXEC }, |
8912711c | 175 | { "lazytime", 0, MS_LAZYTIME }, |
88d413d5 | 176 | { "mand", 0, MS_MANDLOCK }, |
88d413d5 | 177 | { "noatime", 0, MS_NOATIME }, |
470b359b | 178 | { "nodev", 0, MS_NODEV }, |
88d413d5 | 179 | { "nodiratime", 0, MS_NODIRATIME }, |
470b359b CB |
180 | { "noexec", 0, MS_NOEXEC }, |
181 | { "nomand", 1, MS_MANDLOCK }, | |
182 | { "norelatime", 1, MS_RELATIME }, | |
183 | { "nostrictatime", 1, MS_STRICTATIME }, | |
184 | { "nosuid", 0, MS_NOSUID }, | |
88d413d5 SW |
185 | { "rbind", 0, MS_BIND|MS_REC }, |
186 | { "relatime", 0, MS_RELATIME }, | |
470b359b CB |
187 | { "remount", 0, MS_REMOUNT }, |
188 | { "ro", 0, MS_RDONLY }, | |
189 | { "rw", 1, MS_RDONLY }, | |
88d413d5 | 190 | { "strictatime", 0, MS_STRICTATIME }, |
470b359b CB |
191 | { "suid", 1, MS_NOSUID }, |
192 | { "sync", 0, MS_SYNCHRONOUS }, | |
88d413d5 | 193 | { NULL, 0, 0 }, |
998ac676 RT |
194 | }; |
195 | ||
d840039e YT |
196 | static struct mount_opt propagation_opt[] = { |
197 | { "private", 0, MS_PRIVATE }, | |
198 | { "shared", 0, MS_SHARED }, | |
199 | { "slave", 0, MS_SLAVE }, | |
200 | { "unbindable", 0, MS_UNBINDABLE }, | |
201 | { "rprivate", 0, MS_PRIVATE|MS_REC }, | |
202 | { "rshared", 0, MS_SHARED|MS_REC }, | |
203 | { "rslave", 0, MS_SLAVE|MS_REC }, | |
204 | { "runbindable", 0, MS_UNBINDABLE|MS_REC }, | |
205 | { NULL, 0, 0 }, | |
206 | }; | |
207 | ||
e37dda71 | 208 | #if HAVE_LIBCAP |
81810dd1 | 209 | static struct caps_opt caps_opt[] = { |
a6afdde9 | 210 | { "chown", CAP_CHOWN }, |
1e11be34 DL |
211 | { "dac_override", CAP_DAC_OVERRIDE }, |
212 | { "dac_read_search", CAP_DAC_READ_SEARCH }, | |
213 | { "fowner", CAP_FOWNER }, | |
214 | { "fsetid", CAP_FSETID }, | |
81810dd1 DL |
215 | { "kill", CAP_KILL }, |
216 | { "setgid", CAP_SETGID }, | |
217 | { "setuid", CAP_SETUID }, | |
218 | { "setpcap", CAP_SETPCAP }, | |
219 | { "linux_immutable", CAP_LINUX_IMMUTABLE }, | |
220 | { "net_bind_service", CAP_NET_BIND_SERVICE }, | |
221 | { "net_broadcast", CAP_NET_BROADCAST }, | |
222 | { "net_admin", CAP_NET_ADMIN }, | |
223 | { "net_raw", CAP_NET_RAW }, | |
224 | { "ipc_lock", CAP_IPC_LOCK }, | |
225 | { "ipc_owner", CAP_IPC_OWNER }, | |
226 | { "sys_module", CAP_SYS_MODULE }, | |
227 | { "sys_rawio", CAP_SYS_RAWIO }, | |
228 | { "sys_chroot", CAP_SYS_CHROOT }, | |
229 | { "sys_ptrace", CAP_SYS_PTRACE }, | |
230 | { "sys_pacct", CAP_SYS_PACCT }, | |
231 | { "sys_admin", CAP_SYS_ADMIN }, | |
232 | { "sys_boot", CAP_SYS_BOOT }, | |
233 | { "sys_nice", CAP_SYS_NICE }, | |
234 | { "sys_resource", CAP_SYS_RESOURCE }, | |
235 | { "sys_time", CAP_SYS_TIME }, | |
236 | { "sys_tty_config", CAP_SYS_TTY_CONFIG }, | |
237 | { "mknod", CAP_MKNOD }, | |
238 | { "lease", CAP_LEASE }, | |
57b837e2 CB |
239 | #ifdef CAP_AUDIT_READ |
240 | { "audit_read", CAP_AUDIT_READ }, | |
241 | #endif | |
9527e566 | 242 | #ifdef CAP_AUDIT_WRITE |
81810dd1 | 243 | { "audit_write", CAP_AUDIT_WRITE }, |
9527e566 FW |
244 | #endif |
245 | #ifdef CAP_AUDIT_CONTROL | |
81810dd1 | 246 | { "audit_control", CAP_AUDIT_CONTROL }, |
9527e566 | 247 | #endif |
81810dd1 DL |
248 | { "setfcap", CAP_SETFCAP }, |
249 | { "mac_override", CAP_MAC_OVERRIDE }, | |
250 | { "mac_admin", CAP_MAC_ADMIN }, | |
5170c716 CS |
251 | #ifdef CAP_SYSLOG |
252 | { "syslog", CAP_SYSLOG }, | |
253 | #endif | |
254 | #ifdef CAP_WAKE_ALARM | |
255 | { "wake_alarm", CAP_WAKE_ALARM }, | |
256 | #endif | |
2b54359b CB |
257 | #ifdef CAP_BLOCK_SUSPEND |
258 | { "block_suspend", CAP_BLOCK_SUSPEND }, | |
259 | #endif | |
81810dd1 | 260 | }; |
495d2046 SG |
261 | #else |
262 | static struct caps_opt caps_opt[] = {}; | |
263 | #endif | |
81810dd1 | 264 | |
c6d09e15 WB |
265 | static struct limit_opt limit_opt[] = { |
266 | #ifdef RLIMIT_AS | |
267 | { "as", RLIMIT_AS }, | |
268 | #endif | |
269 | #ifdef RLIMIT_CORE | |
270 | { "core", RLIMIT_CORE }, | |
271 | #endif | |
272 | #ifdef RLIMIT_CPU | |
273 | { "cpu", RLIMIT_CPU }, | |
274 | #endif | |
275 | #ifdef RLIMIT_DATA | |
276 | { "data", RLIMIT_DATA }, | |
277 | #endif | |
278 | #ifdef RLIMIT_FSIZE | |
279 | { "fsize", RLIMIT_FSIZE }, | |
280 | #endif | |
281 | #ifdef RLIMIT_LOCKS | |
282 | { "locks", RLIMIT_LOCKS }, | |
283 | #endif | |
284 | #ifdef RLIMIT_MEMLOCK | |
285 | { "memlock", RLIMIT_MEMLOCK }, | |
286 | #endif | |
287 | #ifdef RLIMIT_MSGQUEUE | |
288 | { "msgqueue", RLIMIT_MSGQUEUE }, | |
289 | #endif | |
290 | #ifdef RLIMIT_NICE | |
291 | { "nice", RLIMIT_NICE }, | |
292 | #endif | |
293 | #ifdef RLIMIT_NOFILE | |
294 | { "nofile", RLIMIT_NOFILE }, | |
295 | #endif | |
296 | #ifdef RLIMIT_NPROC | |
297 | { "nproc", RLIMIT_NPROC }, | |
298 | #endif | |
299 | #ifdef RLIMIT_RSS | |
300 | { "rss", RLIMIT_RSS }, | |
301 | #endif | |
302 | #ifdef RLIMIT_RTPRIO | |
303 | { "rtprio", RLIMIT_RTPRIO }, | |
304 | #endif | |
305 | #ifdef RLIMIT_RTTIME | |
306 | { "rttime", RLIMIT_RTTIME }, | |
307 | #endif | |
308 | #ifdef RLIMIT_SIGPENDING | |
309 | { "sigpending", RLIMIT_SIGPENDING }, | |
310 | #endif | |
311 | #ifdef RLIMIT_STACK | |
312 | { "stack", RLIMIT_STACK }, | |
313 | #endif | |
314 | }; | |
315 | ||
91c3830e SH |
316 | static int run_buffer(char *buffer) |
317 | { | |
ebec9176 | 318 | struct lxc_popen_FILE *f; |
91c3830e | 319 | char *output; |
8e7da691 | 320 | int ret; |
91c3830e | 321 | |
ebec9176 | 322 | f = lxc_popen(buffer); |
91c3830e | 323 | if (!f) { |
3f60c2f7 | 324 | SYSERROR("Failed to popen() %s", buffer); |
91c3830e SH |
325 | return -1; |
326 | } | |
327 | ||
328 | output = malloc(LXC_LOG_BUFFER_SIZE); | |
329 | if (!output) { | |
3f60c2f7 | 330 | ERROR("Failed to allocate memory for %s", buffer); |
ebec9176 | 331 | lxc_pclose(f); |
91c3830e SH |
332 | return -1; |
333 | } | |
334 | ||
062b72c6 | 335 | while (fgets(output, LXC_LOG_BUFFER_SIZE, f->f)) |
3f60c2f7 | 336 | DEBUG("Script %s with output: %s", buffer, output); |
91c3830e SH |
337 | |
338 | free(output); | |
339 | ||
ebec9176 | 340 | ret = lxc_pclose(f); |
8e7da691 | 341 | if (ret == -1) { |
3f60c2f7 | 342 | SYSERROR("Script exited with error"); |
91c3830e | 343 | return -1; |
8e7da691 | 344 | } else if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0) { |
3f60c2f7 | 345 | ERROR("Script exited with status %d", WEXITSTATUS(ret)); |
8e7da691 DE |
346 | return -1; |
347 | } else if (WIFSIGNALED(ret)) { | |
3f60c2f7 | 348 | ERROR("Script terminated by signal %d", WTERMSIG(ret)); |
8e7da691 | 349 | return -1; |
91c3830e SH |
350 | } |
351 | ||
352 | return 0; | |
353 | } | |
354 | ||
14a7b0f9 CB |
355 | int run_script_argv(const char *name, unsigned int hook_version, |
356 | const char *section, const char *script, | |
586b1ce7 | 357 | const char *hookname, char **argv) |
148e91f5 | 358 | { |
3f60c2f7 | 359 | int buf_pos, i, ret; |
148e91f5 | 360 | char *buffer; |
6f8d00d2 | 361 | int fret = -1; |
d08e5708 | 362 | size_t size = 0; |
148e91f5 | 363 | |
3f60c2f7 CB |
364 | if (hook_version == 0) |
365 | INFO("Executing script \"%s\" for container \"%s\", config " | |
366 | "section \"%s\"", script, name, section); | |
367 | else | |
368 | INFO("Executing script \"%s\" for container \"%s\"", script, name); | |
148e91f5 | 369 | |
586b1ce7 CB |
370 | for (i = 0; argv && argv[i]; i++) |
371 | size += strlen(argv[i]) + 1; | |
148e91f5 | 372 | |
3f60c2f7 | 373 | size += sizeof("exec"); |
148e91f5 | 374 | size += strlen(script); |
3f60c2f7 CB |
375 | size++; |
376 | ||
148e91f5 | 377 | if (size > INT_MAX) |
3f60c2f7 | 378 | return -EFBIG; |
148e91f5 | 379 | |
3f60c2f7 | 380 | if (hook_version == 0) { |
d08e5708 CB |
381 | size += strlen(hookname); |
382 | size++; | |
383 | ||
384 | size += strlen(name); | |
385 | size++; | |
386 | ||
387 | size += strlen(section); | |
388 | size++; | |
389 | ||
390 | if (size > INT_MAX) | |
391 | return -EFBIG; | |
327cce76 | 392 | } |
3f60c2f7 | 393 | |
6f8d00d2 CB |
394 | buffer = malloc(size); |
395 | if (!buffer) | |
396 | return -ENOMEM; | |
397 | ||
327cce76 | 398 | if (hook_version == 0) |
3f60c2f7 | 399 | buf_pos = snprintf(buffer, size, "exec %s %s %s %s", script, name, section, hookname); |
327cce76 | 400 | else |
3f60c2f7 | 401 | buf_pos = snprintf(buffer, size, "exec %s", script); |
327cce76 CB |
402 | if (buf_pos < 0 || (size_t)buf_pos >= size) { |
403 | ERROR("Failed to create command line for script \"%s\"", script); | |
6f8d00d2 | 404 | goto on_error; |
327cce76 | 405 | } |
3f60c2f7 | 406 | |
327cce76 | 407 | if (hook_version == 1) { |
3f60c2f7 CB |
408 | ret = setenv("LXC_HOOK_TYPE", hookname, 1); |
409 | if (ret < 0) { | |
410 | SYSERROR("Failed to set environment variable: " | |
411 | "LXC_HOOK_TYPE=%s", hookname); | |
6f8d00d2 | 412 | goto on_error; |
3f60c2f7 | 413 | } |
90f20466 | 414 | TRACE("Set environment variable: LXC_HOOK_TYPE=%s", hookname); |
3f60c2f7 CB |
415 | |
416 | ret = setenv("LXC_HOOK_SECTION", section, 1); | |
417 | if (ret < 0) { | |
418 | SYSERROR("Failed to set environment variable: " | |
419 | "LXC_HOOK_SECTION=%s", section); | |
6f8d00d2 | 420 | goto on_error; |
3f60c2f7 CB |
421 | } |
422 | TRACE("Set environment variable: LXC_HOOK_SECTION=%s", section); | |
14a7b0f9 CB |
423 | |
424 | if (strcmp(section, "net") == 0) { | |
425 | char *parent; | |
426 | ||
586b1ce7 | 427 | if (!argv || !argv[0]) |
6f8d00d2 | 428 | goto on_error; |
14a7b0f9 | 429 | |
586b1ce7 | 430 | ret = setenv("LXC_NET_TYPE", argv[0], 1); |
14a7b0f9 CB |
431 | if (ret < 0) { |
432 | SYSERROR("Failed to set environment variable: " | |
586b1ce7 | 433 | "LXC_NET_TYPE=%s", argv[0]); |
6f8d00d2 | 434 | goto on_error; |
14a7b0f9 | 435 | } |
586b1ce7 | 436 | TRACE("Set environment variable: LXC_NET_TYPE=%s", argv[0]); |
14a7b0f9 | 437 | |
586b1ce7 | 438 | parent = argv[1] ? argv[1] : ""; |
14a7b0f9 | 439 | |
586b1ce7 | 440 | if (strcmp(argv[0], "macvlan")) { |
14a7b0f9 CB |
441 | ret = setenv("LXC_NET_PARENT", parent, 1); |
442 | if (ret < 0) { | |
443 | SYSERROR("Failed to set environment " | |
444 | "variable: LXC_NET_PARENT=%s", parent); | |
6f8d00d2 | 445 | goto on_error; |
14a7b0f9 CB |
446 | } |
447 | TRACE("Set environment variable: LXC_NET_PARENT=%s", parent); | |
586b1ce7 | 448 | } else if (strcmp(argv[0], "phys")) { |
14a7b0f9 CB |
449 | ret = setenv("LXC_NET_PARENT", parent, 1); |
450 | if (ret < 0) { | |
451 | SYSERROR("Failed to set environment " | |
452 | "variable: LXC_NET_PARENT=%s", parent); | |
6f8d00d2 | 453 | goto on_error; |
14a7b0f9 CB |
454 | } |
455 | TRACE("Set environment variable: LXC_NET_PARENT=%s", parent); | |
586b1ce7 CB |
456 | } else if (strcmp(argv[0], "veth")) { |
457 | char *peer = argv[2] ? argv[2] : ""; | |
14a7b0f9 CB |
458 | |
459 | ret = setenv("LXC_NET_PEER", peer, 1); | |
460 | if (ret < 0) { | |
461 | SYSERROR("Failed to set environment " | |
462 | "variable: LXC_NET_PEER=%s", peer); | |
6f8d00d2 | 463 | goto on_error; |
14a7b0f9 CB |
464 | } |
465 | TRACE("Set environment variable: LXC_NET_PEER=%s", peer); | |
466 | ||
467 | ret = setenv("LXC_NET_PARENT", parent, 1); | |
468 | if (ret < 0) { | |
469 | SYSERROR("Failed to set environment " | |
470 | "variable: LXC_NET_PARENT=%s", parent); | |
6f8d00d2 | 471 | goto on_error; |
14a7b0f9 CB |
472 | } |
473 | TRACE("Set environment variable: LXC_NET_PARENT=%s", parent); | |
474 | } | |
475 | } | |
148e91f5 SH |
476 | } |
477 | ||
586b1ce7 | 478 | for (i = 0; argv && argv[i]; i++) { |
3f60c2f7 CB |
479 | size_t len = size - buf_pos; |
480 | ||
586b1ce7 | 481 | ret = snprintf(buffer + buf_pos, len, " %s", argv[i]); |
3f60c2f7 CB |
482 | if (ret < 0 || (size_t)ret >= len) { |
483 | ERROR("Failed to create command line for script \"%s\"", script); | |
6f8d00d2 | 484 | goto on_error; |
148e91f5 | 485 | } |
3f60c2f7 | 486 | buf_pos += ret; |
148e91f5 SH |
487 | } |
488 | ||
6f8d00d2 CB |
489 | fret = run_buffer(buffer); |
490 | ||
491 | on_error: | |
492 | free(buffer); | |
493 | return fret; | |
148e91f5 SH |
494 | } |
495 | ||
811ef482 | 496 | int run_script(const char *name, const char *section, const char *script, ...) |
e3b4c4c4 | 497 | { |
abbfd20b | 498 | int ret; |
91c3830e | 499 | char *buffer, *p; |
abbfd20b DL |
500 | size_t size = 0; |
501 | va_list ap; | |
751d9dcd | 502 | |
062b72c6 | 503 | INFO("Executing script \"%s\" for container \"%s\", config section \"%s\".", |
751d9dcd | 504 | script, name, section); |
e3b4c4c4 | 505 | |
abbfd20b DL |
506 | va_start(ap, script); |
507 | while ((p = va_arg(ap, char *))) | |
95642a10 | 508 | size += strlen(p) + 1; |
abbfd20b DL |
509 | va_end(ap); |
510 | ||
6d1a5f93 | 511 | size += strlen("exec"); |
abbfd20b DL |
512 | size += strlen(script); |
513 | size += strlen(name); | |
514 | size += strlen(section); | |
6d1a5f93 | 515 | size += 4; |
abbfd20b | 516 | |
95642a10 MS |
517 | if (size > INT_MAX) |
518 | return -1; | |
519 | ||
520 | buffer = alloca(size); | |
abbfd20b | 521 | if (!buffer) { |
062b72c6 | 522 | ERROR("Failed to allocate memory."); |
751d9dcd DL |
523 | return -1; |
524 | } | |
525 | ||
6d1a5f93 | 526 | ret = snprintf(buffer, size, "exec %s %s %s", script, name, section); |
9ba8130c | 527 | if (ret < 0 || ret >= size) { |
062b72c6 | 528 | ERROR("Script name too long."); |
9ba8130c SH |
529 | return -1; |
530 | } | |
751d9dcd | 531 | |
abbfd20b | 532 | va_start(ap, script); |
9ba8130c | 533 | while ((p = va_arg(ap, char *))) { |
062b72c6 | 534 | int len = size - ret; |
9ba8130c SH |
535 | int rc; |
536 | rc = snprintf(buffer + ret, len, " %s", p); | |
537 | if (rc < 0 || rc >= len) { | |
062b72c6 | 538 | ERROR("Script args too long."); |
9ba8130c SH |
539 | return -1; |
540 | } | |
541 | ret += rc; | |
542 | } | |
abbfd20b | 543 | va_end(ap); |
751d9dcd | 544 | |
91c3830e | 545 | return run_buffer(buffer); |
e3b4c4c4 ST |
546 | } |
547 | ||
0c547523 SH |
548 | /* |
549 | * pin_rootfs | |
b7ed4bf0 CS |
550 | * if rootfs is a directory, then open ${rootfs}/lxc.hold for writing for |
551 | * the duration of the container run, to prevent the container from marking | |
552 | * the underlying fs readonly on shutdown. unlink the file immediately so | |
553 | * no name pollution is happens | |
0c547523 SH |
554 | * return -1 on error. |
555 | * return -2 if nothing needed to be pinned. | |
556 | * return an open fd (>=0) if we pinned it. | |
557 | */ | |
558 | int pin_rootfs(const char *rootfs) | |
559 | { | |
560 | char absrootfs[MAXPATHLEN]; | |
561 | char absrootfspin[MAXPATHLEN]; | |
562 | struct stat s; | |
563 | int ret, fd; | |
564 | ||
e99ee0de | 565 | if (rootfs == NULL || strlen(rootfs) == 0) |
0d03360a | 566 | return -2; |
e99ee0de | 567 | |
00ec333b | 568 | if (!realpath(rootfs, absrootfs)) |
9be53773 | 569 | return -2; |
0c547523 | 570 | |
00ec333b | 571 | if (access(absrootfs, F_OK)) |
0c547523 | 572 | return -1; |
0c547523 | 573 | |
00ec333b | 574 | if (stat(absrootfs, &s)) |
0c547523 | 575 | return -1; |
0c547523 | 576 | |
72f919c4 | 577 | if (!S_ISDIR(s.st_mode)) |
0c547523 SH |
578 | return -2; |
579 | ||
b7ed4bf0 | 580 | ret = snprintf(absrootfspin, MAXPATHLEN, "%s/lxc.hold", absrootfs); |
00ec333b | 581 | if (ret >= MAXPATHLEN) |
0c547523 | 582 | return -1; |
0c547523 SH |
583 | |
584 | fd = open(absrootfspin, O_CREAT | O_RDWR, S_IWUSR|S_IRUSR); | |
b7ed4bf0 CS |
585 | if (fd < 0) |
586 | return fd; | |
587 | (void)unlink(absrootfspin); | |
0c547523 SH |
588 | return fd; |
589 | } | |
590 | ||
e2a7e8dc SH |
591 | /* |
592 | * If we are asking to remount something, make sure that any | |
593 | * NOEXEC etc are honored. | |
594 | */ | |
5ae72b98 | 595 | unsigned long add_required_remount_flags(const char *s, const char *d, |
5285689c | 596 | unsigned long flags) |
e2a7e8dc | 597 | { |
614305f3 | 598 | #ifdef HAVE_STATVFS |
e2a7e8dc SH |
599 | struct statvfs sb; |
600 | unsigned long required_flags = 0; | |
601 | ||
602 | if (!(flags & MS_REMOUNT)) | |
603 | return flags; | |
604 | ||
605 | if (!s) | |
606 | s = d; | |
607 | ||
608 | if (!s) | |
609 | return flags; | |
610 | if (statvfs(s, &sb) < 0) | |
611 | return flags; | |
612 | ||
613 | if (sb.f_flag & MS_NOSUID) | |
614 | required_flags |= MS_NOSUID; | |
615 | if (sb.f_flag & MS_NODEV) | |
616 | required_flags |= MS_NODEV; | |
617 | if (sb.f_flag & MS_RDONLY) | |
618 | required_flags |= MS_RDONLY; | |
619 | if (sb.f_flag & MS_NOEXEC) | |
620 | required_flags |= MS_NOEXEC; | |
621 | ||
622 | return flags | required_flags; | |
614305f3 SH |
623 | #else |
624 | return flags; | |
625 | #endif | |
e2a7e8dc SH |
626 | } |
627 | ||
4fb3cba5 | 628 | static int lxc_mount_auto_mounts(struct lxc_conf *conf, int flags, struct lxc_handler *handler) |
368bbc02 | 629 | { |
368bbc02 | 630 | int r; |
80e80c40 | 631 | int i; |
b06b8511 CS |
632 | static struct { |
633 | int match_mask; | |
634 | int match_flag; | |
635 | const char *source; | |
636 | const char *destination; | |
637 | const char *fstype; | |
638 | unsigned long flags; | |
639 | const char *options; | |
640 | } default_mounts[] = { | |
641 | /* Read-only bind-mounting... In older kernels, doing that required | |
642 | * to do one MS_BIND mount and then MS_REMOUNT|MS_RDONLY the same | |
643 | * one. According to mount(2) manpage, MS_BIND honors MS_RDONLY from | |
644 | * kernel 2.6.26 onwards. However, this apparently does not work on | |
645 | * kernel 3.8. Unfortunately, on that very same kernel, doing the | |
646 | * same trick as above doesn't seem to work either, there one needs | |
647 | * to ALSO specify MS_BIND for the remount, otherwise the entire | |
648 | * fs is remounted read-only or the mount fails because it's busy... | |
649 | * MS_REMOUNT|MS_BIND|MS_RDONLY seems to work for kernels as low as | |
650 | * 2.6.32... | |
368bbc02 | 651 | */ |
f24a52d5 | 652 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "proc", "%r/proc", "proc", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL }, |
592fd47a SH |
653 | /* proc/tty is used as a temporary placeholder for proc/sys/net which we'll move back in a few steps */ |
654 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys/net", "%r/proc/tty", NULL, MS_BIND, NULL }, | |
f24a52d5 SG |
655 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys", "%r/proc/sys", NULL, MS_BIND, NULL }, |
656 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL, "%r/proc/sys", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL }, | |
592fd47a | 657 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/tty", "%r/proc/sys/net", NULL, MS_MOVE, NULL }, |
f24a52d5 SG |
658 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sysrq-trigger", "%r/proc/sysrq-trigger", NULL, MS_BIND, NULL }, |
659 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL, "%r/proc/sysrq-trigger", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL }, | |
660 | { LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW, "proc", "%r/proc", "proc", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL }, | |
661 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW, "sysfs", "%r/sys", "sysfs", 0, NULL }, | |
662 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO, "sysfs", "%r/sys", "sysfs", MS_RDONLY, NULL }, | |
663 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "sysfs", "%r/sys", "sysfs", MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL }, | |
664 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "%r/sys", "%r/sys", NULL, MS_BIND, NULL }, | |
665 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, NULL, "%r/sys", NULL, MS_REMOUNT|MS_BIND|MS_RDONLY, NULL }, | |
666 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "sysfs", "%r/sys/devices/virtual/net", "sysfs", 0, NULL }, | |
667 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, "%r/sys/devices/virtual/net/devices/virtual/net", "%r/sys/devices/virtual/net", NULL, MS_BIND, NULL }, | |
668 | { LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED, NULL, "%r/sys/devices/virtual/net", NULL, MS_REMOUNT|MS_BIND|MS_NOSUID|MS_NODEV|MS_NOEXEC, NULL }, | |
669 | { 0, 0, NULL, NULL, NULL, 0, NULL } | |
b06b8511 | 670 | }; |
368bbc02 | 671 | |
b06b8511 CS |
672 | for (i = 0; default_mounts[i].match_mask; i++) { |
673 | if ((flags & default_mounts[i].match_mask) == default_mounts[i].match_flag) { | |
674 | char *source = NULL; | |
675 | char *destination = NULL; | |
676 | int saved_errno; | |
e2a7e8dc | 677 | unsigned long mflags; |
b06b8511 CS |
678 | |
679 | if (default_mounts[i].source) { | |
680 | /* will act like strdup if %r is not present */ | |
8ede5f4c | 681 | source = lxc_string_replace("%r", conf->rootfs.path ? conf->rootfs.mount : "", default_mounts[i].source); |
b06b8511 CS |
682 | if (!source) { |
683 | SYSERROR("memory allocation error"); | |
684 | return -1; | |
685 | } | |
686 | } | |
cc4fd506 SH |
687 | if (!default_mounts[i].destination) { |
688 | ERROR("BUG: auto mounts destination %d was NULL", i); | |
b2f44b4d | 689 | free(source); |
cc4fd506 SH |
690 | return -1; |
691 | } | |
692 | /* will act like strdup if %r is not present */ | |
693 | destination = lxc_string_replace("%r", conf->rootfs.path ? conf->rootfs.mount : "", default_mounts[i].destination); | |
694 | if (!destination) { | |
695 | saved_errno = errno; | |
696 | SYSERROR("memory allocation error"); | |
697 | free(source); | |
698 | errno = saved_errno; | |
699 | return -1; | |
b06b8511 | 700 | } |
e2a7e8dc SH |
701 | mflags = add_required_remount_flags(source, destination, |
702 | default_mounts[i].flags); | |
592fd47a | 703 | r = safe_mount(source, destination, default_mounts[i].fstype, mflags, default_mounts[i].options, conf->rootfs.path ? conf->rootfs.mount : NULL); |
b06b8511 | 704 | saved_errno = errno; |
b88ff9a0 SG |
705 | if (r < 0 && errno == ENOENT) { |
706 | INFO("Mount source or target for %s on %s doesn't exist. Skipping.", source, destination); | |
707 | r = 0; | |
708 | } | |
709 | else if (r < 0) | |
e2a7e8dc | 710 | SYSERROR("error mounting %s on %s flags %lu", source, destination, mflags); |
f24a52d5 | 711 | |
b06b8511 CS |
712 | free(source); |
713 | free(destination); | |
714 | if (r < 0) { | |
b06b8511 CS |
715 | errno = saved_errno; |
716 | return -1; | |
717 | } | |
368bbc02 | 718 | } |
368bbc02 CS |
719 | } |
720 | ||
b06b8511 | 721 | if (flags & LXC_AUTO_CGROUP_MASK) { |
0769b82a CS |
722 | int cg_flags; |
723 | ||
3f69fb12 | 724 | cg_flags = flags & (LXC_AUTO_CGROUP_MASK & ~LXC_AUTO_CGROUP_FORCE); |
0769b82a CS |
725 | /* If the type of cgroup mount was not specified, it depends on the |
726 | * container's capabilities as to what makes sense: if we have | |
727 | * CAP_SYS_ADMIN, the read-only part can be remounted read-write | |
728 | * anyway, so we may as well default to read-write; then the admin | |
729 | * will not be given a false sense of security. (And if they really | |
730 | * want mixed r/o r/w, then they can explicitly specify :mixed.) | |
731 | * OTOH, if the container lacks CAP_SYS_ADMIN, do only default to | |
732 | * :mixed, because then the container can't remount it read-write. */ | |
733 | if (cg_flags == LXC_AUTO_CGROUP_NOSPEC || cg_flags == LXC_AUTO_CGROUP_FULL_NOSPEC) { | |
734 | int has_sys_admin = 0; | |
b0ee5983 CB |
735 | |
736 | if (!lxc_list_empty(&conf->keepcaps)) | |
0769b82a | 737 | has_sys_admin = in_caplist(CAP_SYS_ADMIN, &conf->keepcaps); |
b0ee5983 | 738 | else |
0769b82a | 739 | has_sys_admin = !in_caplist(CAP_SYS_ADMIN, &conf->caps); |
b0ee5983 CB |
740 | |
741 | if (cg_flags == LXC_AUTO_CGROUP_NOSPEC) | |
0769b82a | 742 | cg_flags = has_sys_admin ? LXC_AUTO_CGROUP_RW : LXC_AUTO_CGROUP_MIXED; |
b0ee5983 | 743 | else |
0769b82a | 744 | cg_flags = has_sys_admin ? LXC_AUTO_CGROUP_FULL_RW : LXC_AUTO_CGROUP_FULL_MIXED; |
0769b82a | 745 | } |
3f69fb12 SY |
746 | if (flags & LXC_AUTO_CGROUP_FORCE) |
747 | cg_flags |= LXC_AUTO_CGROUP_FORCE; | |
8ede5f4c | 748 | if (!cgroup_mount(conf->rootfs.path ? conf->rootfs.mount : "", handler, cg_flags)) { |
368bbc02 | 749 | SYSERROR("error mounting /sys/fs/cgroup"); |
b06b8511 | 750 | return -1; |
368bbc02 CS |
751 | } |
752 | } | |
753 | ||
368bbc02 | 754 | return 0; |
368bbc02 CS |
755 | } |
756 | ||
4e5440c6 | 757 | static int setup_utsname(struct utsname *utsname) |
0ad19a3f | 758 | { |
4e5440c6 DL |
759 | if (!utsname) |
760 | return 0; | |
0ad19a3f | 761 | |
4e5440c6 DL |
762 | if (sethostname(utsname->nodename, strlen(utsname->nodename))) { |
763 | SYSERROR("failed to set the hostname to '%s'", utsname->nodename); | |
0ad19a3f | 764 | return -1; |
765 | } | |
766 | ||
4e5440c6 | 767 | INFO("'%s' hostname has been setup", utsname->nodename); |
cd54d859 | 768 | |
0ad19a3f | 769 | return 0; |
770 | } | |
771 | ||
69aa6655 DE |
772 | struct dev_symlinks { |
773 | const char *oldpath; | |
774 | const char *name; | |
775 | }; | |
776 | ||
777 | static const struct dev_symlinks dev_symlinks[] = { | |
778 | {"/proc/self/fd", "fd"}, | |
779 | {"/proc/self/fd/0", "stdin"}, | |
780 | {"/proc/self/fd/1", "stdout"}, | |
781 | {"/proc/self/fd/2", "stderr"}, | |
782 | }; | |
783 | ||
ed8704d0 | 784 | static int lxc_setup_dev_symlinks(const struct lxc_rootfs *rootfs) |
69aa6655 DE |
785 | { |
786 | char path[MAXPATHLEN]; | |
787 | int ret,i; | |
09227be2 | 788 | struct stat s; |
69aa6655 DE |
789 | |
790 | ||
791 | for (i = 0; i < sizeof(dev_symlinks) / sizeof(dev_symlinks[0]); i++) { | |
792 | const struct dev_symlinks *d = &dev_symlinks[i]; | |
ec50007f | 793 | ret = snprintf(path, sizeof(path), "%s/dev/%s", rootfs->path ? rootfs->mount : "", d->name); |
69aa6655 DE |
794 | if (ret < 0 || ret >= MAXPATHLEN) |
795 | return -1; | |
09227be2 MW |
796 | |
797 | /* | |
798 | * Stat the path first. If we don't get an error | |
799 | * accept it as is and don't try to create it | |
800 | */ | |
801 | if (!stat(path, &s)) { | |
802 | continue; | |
803 | } | |
804 | ||
69aa6655 | 805 | ret = symlink(d->oldpath, path); |
09227be2 | 806 | |
69aa6655 | 807 | if (ret && errno != EEXIST) { |
09227be2 MW |
808 | if ( errno == EROFS ) { |
809 | WARN("Warning: Read Only file system while creating %s", path); | |
810 | } else { | |
811 | SYSERROR("Error creating %s", path); | |
812 | return -1; | |
813 | } | |
69aa6655 DE |
814 | } |
815 | } | |
816 | return 0; | |
817 | } | |
818 | ||
2187efd3 | 819 | /* Build a space-separate list of ptys to pass to systemd. */ |
393903d1 | 820 | static bool append_ptyname(char **pp, char *name) |
b0a33c1e | 821 | { |
393903d1 SH |
822 | char *p; |
823 | ||
824 | if (!*pp) { | |
825 | *pp = malloc(strlen(name) + strlen("container_ttys=") + 1); | |
826 | if (!*pp) | |
827 | return false; | |
828 | sprintf(*pp, "container_ttys=%s", name); | |
829 | return true; | |
830 | } | |
831 | p = realloc(*pp, strlen(*pp) + strlen(name) + 2); | |
832 | if (!p) | |
833 | return false; | |
834 | *pp = p; | |
835 | strcat(p, " "); | |
836 | strcat(p, name); | |
837 | return true; | |
838 | } | |
839 | ||
2187efd3 | 840 | static int lxc_setup_ttys(struct lxc_conf *conf) |
393903d1 | 841 | { |
9e1045e3 | 842 | int i, ret; |
0e4be3cf | 843 | const struct lxc_tty_info *ttys = &conf->ttys; |
393903d1 | 844 | char *ttydir = conf->ttydir; |
7c6ef2a2 | 845 | char path[MAXPATHLEN], lxcpath[MAXPATHLEN]; |
b0a33c1e | 846 | |
e8bd4e43 | 847 | if (!conf->rootfs.path) |
bc9bd0e3 DL |
848 | return 0; |
849 | ||
0e4be3cf CB |
850 | for (i = 0; i < ttys->nbtty; i++) { |
851 | struct lxc_terminal_info *tty = &ttys->tty[i]; | |
b0a33c1e | 852 | |
e8bd4e43 | 853 | ret = snprintf(path, sizeof(path), "/dev/tty%d", i + 1); |
73363c61 | 854 | if (ret < 0 || (size_t)ret >= sizeof(path)) |
7c6ef2a2 | 855 | return -1; |
9e1045e3 | 856 | |
7c6ef2a2 SH |
857 | if (ttydir) { |
858 | /* create dev/lxc/tty%d" */ | |
9e1045e3 CB |
859 | ret = snprintf(lxcpath, sizeof(lxcpath), |
860 | "/dev/%s/tty%d", ttydir, i + 1); | |
73363c61 | 861 | if (ret < 0 || (size_t)ret >= sizeof(lxcpath)) |
7c6ef2a2 | 862 | return -1; |
9e1045e3 | 863 | |
7c6ef2a2 | 864 | ret = creat(lxcpath, 0660); |
9e1045e3 | 865 | if (ret < 0 && errno != EEXIST) { |
73363c61 | 866 | SYSERROR("Failed to create \"%s\"", lxcpath); |
7c6ef2a2 SH |
867 | return -1; |
868 | } | |
4d44e274 SH |
869 | if (ret >= 0) |
870 | close(ret); | |
9e1045e3 | 871 | |
7c6ef2a2 | 872 | ret = unlink(path); |
9e1045e3 | 873 | if (ret < 0 && errno != ENOENT) { |
73363c61 | 874 | SYSERROR("Failed to unlink \"%s\"", path); |
7c6ef2a2 SH |
875 | return -1; |
876 | } | |
b0a33c1e | 877 | |
2520facd | 878 | ret = mount(tty->name, lxcpath, "none", MS_BIND, 0); |
9e1045e3 | 879 | if (ret < 0) { |
73363c61 | 880 | WARN("Failed to bind mount \"%s\" onto \"%s\"", |
2520facd | 881 | tty->name, path); |
7c6ef2a2 SH |
882 | continue; |
883 | } | |
2520facd | 884 | DEBUG("bind mounted \"%s\" onto \"%s\"", tty->name, |
9e1045e3 | 885 | path); |
13954cce | 886 | |
9e1045e3 CB |
887 | ret = snprintf(lxcpath, sizeof(lxcpath), "%s/tty%d", |
888 | ttydir, i + 1); | |
73363c61 | 889 | if (ret < 0 || (size_t)ret >= sizeof(lxcpath)) |
9ba8130c | 890 | return -1; |
9e1045e3 | 891 | |
7c6ef2a2 | 892 | ret = symlink(lxcpath, path); |
9e1045e3 | 893 | if (ret < 0) { |
73363c61 | 894 | SYSERROR("Failed to create symlink \"%s\" -> \"%s\"", |
9e1045e3 | 895 | path, lxcpath); |
7c6ef2a2 SH |
896 | return -1; |
897 | } | |
898 | } else { | |
9e1045e3 CB |
899 | /* If we populated /dev, then we need to create |
900 | * /dev/ttyN | |
901 | */ | |
902 | ret = access(path, F_OK); | |
903 | if (ret < 0) { | |
c6883f38 | 904 | ret = creat(path, 0660); |
9e1045e3 | 905 | if (ret < 0) { |
73363c61 | 906 | SYSERROR("Failed to create \"%s\"", path); |
c6883f38 | 907 | /* this isn't fatal, continue */ |
025ed0f3 | 908 | } else { |
c6883f38 | 909 | close(ret); |
025ed0f3 | 910 | } |
c6883f38 | 911 | } |
9e1045e3 | 912 | |
2520facd | 913 | ret = mount(tty->name, path, "none", MS_BIND, 0); |
9e1045e3 | 914 | if (ret < 0) { |
2520facd | 915 | SYSERROR("Failed to mount '%s'->'%s'", tty->name, path); |
7c6ef2a2 SH |
916 | continue; |
917 | } | |
9e1045e3 | 918 | |
2520facd | 919 | DEBUG("Bind mounted \"%s\" onto \"%s\"", tty->name, |
9e1045e3 | 920 | path); |
393903d1 | 921 | } |
9e1045e3 | 922 | |
2520facd | 923 | if (!append_ptyname(&conf->pty_names, tty->name)) { |
393903d1 SH |
924 | ERROR("Error setting up container_ttys string"); |
925 | return -1; | |
b0a33c1e | 926 | } |
927 | } | |
928 | ||
0e4be3cf | 929 | INFO("Finished setting up %d /dev/tty<N> device(s)", ttys->nbtty); |
b0a33c1e | 930 | return 0; |
931 | } | |
932 | ||
2187efd3 CB |
933 | int lxc_allocate_ttys(const char *name, struct lxc_conf *conf) |
934 | { | |
0e4be3cf | 935 | struct lxc_tty_info *ttys = &conf->ttys; |
2187efd3 CB |
936 | int i, ret; |
937 | ||
938 | /* no tty in the configuration */ | |
939 | if (!conf->tty) | |
940 | return 0; | |
941 | ||
0e4be3cf CB |
942 | ttys->tty = malloc(sizeof(*ttys->tty) * conf->tty); |
943 | if (!ttys->tty) | |
2187efd3 | 944 | return -ENOMEM; |
2187efd3 CB |
945 | |
946 | for (i = 0; i < conf->tty; i++) { | |
0e4be3cf | 947 | struct lxc_terminal_info *tty = &ttys->tty[i]; |
2187efd3 | 948 | |
2520facd CB |
949 | ret = openpty(&tty->master, &tty->slave, |
950 | tty->name, NULL, NULL); | |
2187efd3 CB |
951 | if (ret) { |
952 | SYSERROR("failed to create pty device number %d", i); | |
0e4be3cf CB |
953 | ttys->nbtty = i; |
954 | lxc_delete_tty(ttys); | |
2187efd3 CB |
955 | return -ENOTTY; |
956 | } | |
957 | ||
958 | DEBUG("allocated pty \"%s\" with master fd %d and slave fd %d", | |
2520facd | 959 | tty->name, tty->master, tty->slave); |
2187efd3 CB |
960 | |
961 | /* Prevent leaking the file descriptors to the container */ | |
2520facd | 962 | ret = fcntl(tty->master, F_SETFD, FD_CLOEXEC); |
2187efd3 CB |
963 | if (ret < 0) |
964 | WARN("failed to set FD_CLOEXEC flag on master fd %d of " | |
965 | "pty device \"%s\": %s", | |
2520facd | 966 | tty->master, tty->name, strerror(errno)); |
2187efd3 | 967 | |
2520facd | 968 | ret = fcntl(tty->slave, F_SETFD, FD_CLOEXEC); |
2187efd3 CB |
969 | if (ret < 0) |
970 | WARN("failed to set FD_CLOEXEC flag on slave fd %d of " | |
971 | "pty device \"%s\": %s", | |
2520facd | 972 | tty->slave, tty->name, strerror(errno)); |
2187efd3 | 973 | |
2520facd | 974 | tty->busy = 0; |
2187efd3 CB |
975 | } |
976 | ||
0e4be3cf | 977 | ttys->nbtty = conf->tty; |
2187efd3 CB |
978 | |
979 | INFO("finished allocating %d pts devices", conf->tty); | |
980 | return 0; | |
981 | } | |
982 | ||
0e4be3cf | 983 | void lxc_delete_tty(struct lxc_tty_info *ttys) |
2187efd3 CB |
984 | { |
985 | int i; | |
986 | ||
0e4be3cf CB |
987 | for (i = 0; i < ttys->nbtty; i++) { |
988 | struct lxc_terminal_info *tty = &ttys->tty[i]; | |
2187efd3 | 989 | |
2520facd CB |
990 | close(tty->master); |
991 | close(tty->slave); | |
2187efd3 CB |
992 | } |
993 | ||
0e4be3cf CB |
994 | free(ttys->tty); |
995 | ttys->tty = NULL; | |
996 | ttys->nbtty = 0; | |
2187efd3 CB |
997 | } |
998 | ||
999 | static int lxc_send_ttys_to_parent(struct lxc_handler *handler) | |
1000 | { | |
1001 | int i; | |
1002 | struct lxc_conf *conf = handler->conf; | |
0e4be3cf | 1003 | struct lxc_tty_info *ttys = &conf->ttys; |
2187efd3 CB |
1004 | int sock = handler->data_sock[0]; |
1005 | int ret = -1; | |
1006 | ||
1007 | if (!conf->tty) | |
1008 | return 0; | |
1009 | ||
1010 | for (i = 0; i < conf->tty; i++) { | |
1011 | int ttyfds[2]; | |
0e4be3cf | 1012 | struct lxc_terminal_info *tty = &ttys->tty[i]; |
2187efd3 | 1013 | |
2520facd CB |
1014 | ttyfds[0] = tty->master; |
1015 | ttyfds[1] = tty->slave; | |
2187efd3 CB |
1016 | |
1017 | ret = lxc_abstract_unix_send_fds(sock, ttyfds, 2, NULL, 0); | |
1018 | if (ret < 0) | |
1019 | break; | |
1020 | ||
1021 | TRACE("Send pty \"%s\" with master fd %d and slave fd %d to " | |
2520facd | 1022 | "parent", tty->name, tty->master, tty->slave); |
2187efd3 CB |
1023 | } |
1024 | ||
1025 | if (ret < 0) | |
1026 | ERROR("Failed to send %d ttys to parent: %s", conf->tty, | |
1027 | strerror(errno)); | |
1028 | else | |
1029 | TRACE("Sent %d ttys to parent", conf->tty); | |
1030 | ||
1031 | return ret; | |
1032 | } | |
1033 | ||
1034 | static int lxc_create_ttys(struct lxc_handler *handler) | |
1035 | { | |
1036 | int ret = -1; | |
1037 | struct lxc_conf *conf = handler->conf; | |
1038 | ||
1039 | ret = lxc_allocate_ttys(handler->name, conf); | |
1040 | if (ret < 0) { | |
1041 | ERROR("Failed to allocate ttys"); | |
1042 | goto on_error; | |
1043 | } | |
1044 | ||
1045 | ret = lxc_send_ttys_to_parent(handler); | |
1046 | if (ret < 0) { | |
1047 | ERROR("Failed to send ttys to parent"); | |
1048 | goto on_error; | |
1049 | } | |
1050 | ||
1051 | if (!conf->is_execute) { | |
1052 | ret = lxc_setup_ttys(conf); | |
1053 | if (ret < 0) { | |
1054 | ERROR("Failed to setup ttys"); | |
1055 | goto on_error; | |
1056 | } | |
1057 | } | |
1058 | ||
1059 | if (conf->pty_names) { | |
1060 | ret = setenv("container_ttys", conf->pty_names, 1); | |
1061 | if (ret < 0) | |
1062 | SYSERROR("Failed to set \"container_ttys=%s\"", conf->pty_names); | |
1063 | } | |
1064 | ||
1065 | ret = 0; | |
1066 | ||
1067 | on_error: | |
0e4be3cf | 1068 | lxc_delete_tty(&conf->ttys); |
2187efd3 CB |
1069 | |
1070 | return ret; | |
1071 | } | |
1072 | ||
59bb8698 | 1073 | static int setup_rootfs_pivot_root(const char *rootfs) |
bf601689 | 1074 | { |
2d489f9e | 1075 | int oldroot = -1, newroot = -1; |
bf601689 | 1076 | |
2d489f9e SH |
1077 | oldroot = open("/", O_DIRECTORY | O_RDONLY); |
1078 | if (oldroot < 0) { | |
1079 | SYSERROR("Error opening old-/ for fchdir"); | |
9ba8130c SH |
1080 | return -1; |
1081 | } | |
2d489f9e SH |
1082 | newroot = open(rootfs, O_DIRECTORY | O_RDONLY); |
1083 | if (newroot < 0) { | |
1084 | SYSERROR("Error opening new-/ for fchdir"); | |
1085 | goto fail; | |
c08556c6 | 1086 | } |
bf601689 | 1087 | |
cc6f6dd7 | 1088 | /* change into new root fs */ |
2d489f9e | 1089 | if (fchdir(newroot)) { |
cc6f6dd7 | 1090 | SYSERROR("can't chdir to new rootfs '%s'", rootfs); |
2d489f9e | 1091 | goto fail; |
cc6f6dd7 DL |
1092 | } |
1093 | ||
cc6f6dd7 | 1094 | /* pivot_root into our new root fs */ |
2d489f9e | 1095 | if (pivot_root(".", ".")) { |
cc6f6dd7 | 1096 | SYSERROR("pivot_root syscall failed"); |
2d489f9e | 1097 | goto fail; |
bf601689 | 1098 | } |
cc6f6dd7 | 1099 | |
2d489f9e SH |
1100 | /* |
1101 | * at this point the old-root is mounted on top of our new-root | |
1102 | * To unmounted it we must not be chdir'd into it, so escape back | |
1103 | * to old-root | |
1104 | */ | |
1105 | if (fchdir(oldroot) < 0) { | |
1106 | SYSERROR("Error entering oldroot"); | |
1107 | goto fail; | |
1108 | } | |
7981ea46 | 1109 | if (umount2(".", MNT_DETACH) < 0) { |
2d489f9e SH |
1110 | SYSERROR("Error detaching old root"); |
1111 | goto fail; | |
cc6f6dd7 DL |
1112 | } |
1113 | ||
2d489f9e SH |
1114 | if (fchdir(newroot) < 0) { |
1115 | SYSERROR("Error re-entering newroot"); | |
1116 | goto fail; | |
1117 | } | |
cc6f6dd7 | 1118 | |
2d489f9e SH |
1119 | close(oldroot); |
1120 | close(newroot); | |
bf601689 | 1121 | |
2d489f9e | 1122 | DEBUG("pivot_root syscall to '%s' successful", rootfs); |
bf601689 | 1123 | |
bf601689 | 1124 | return 0; |
2d489f9e SH |
1125 | |
1126 | fail: | |
1127 | if (oldroot != -1) | |
1128 | close(oldroot); | |
1129 | if (newroot != -1) | |
1130 | close(newroot); | |
1131 | return -1; | |
bf601689 MH |
1132 | } |
1133 | ||
7133b912 CB |
1134 | /* Just create a path for /dev under $lxcpath/$name and in rootfs If we hit an |
1135 | * error, log it but don't fail yet. | |
91c3830e | 1136 | */ |
7133b912 CB |
1137 | static int mount_autodev(const char *name, const struct lxc_rootfs *rootfs, |
1138 | const char *lxcpath) | |
91c3830e SH |
1139 | { |
1140 | int ret; | |
87da4ec3 SH |
1141 | size_t clen; |
1142 | char *path; | |
91c3830e | 1143 | |
7133b912 | 1144 | INFO("Preparing \"/dev\""); |
bc6928ff | 1145 | |
14221cbb | 1146 | /* $(rootfs->mount) + "/dev/pts" + '\0' */ |
ec50007f | 1147 | clen = (rootfs->path ? strlen(rootfs->mount) : 0) + 9; |
87da4ec3 | 1148 | path = alloca(clen); |
bc6928ff | 1149 | |
ec50007f | 1150 | ret = snprintf(path, clen, "%s/dev", rootfs->path ? rootfs->mount : ""); |
7133b912 | 1151 | if (ret < 0 || (size_t)ret >= clen) |
91c3830e | 1152 | return -1; |
bc6928ff | 1153 | |
87da4ec3 | 1154 | if (!dir_exists(path)) { |
7133b912 CB |
1155 | WARN("\"/dev\" directory does not exist. Proceeding without " |
1156 | "autodev being set up"); | |
87da4ec3 | 1157 | return 0; |
bc6928ff | 1158 | } |
87da4ec3 | 1159 | |
1ec0e8e3 | 1160 | ret = safe_mount("none", path, "tmpfs", 0, "size=500000,mode=755", |
7133b912 CB |
1161 | rootfs->path ? rootfs->mount : NULL); |
1162 | if (ret < 0) { | |
1163 | SYSERROR("Failed to mount tmpfs on \"%s\"", path); | |
1ec0e8e3 | 1164 | return -1; |
91c3830e | 1165 | } |
7133b912 | 1166 | INFO("Mounted tmpfs on \"%s\"", path); |
87da4ec3 | 1167 | |
ec50007f | 1168 | ret = snprintf(path, clen, "%s/dev/pts", rootfs->path ? rootfs->mount : ""); |
7133b912 | 1169 | if (ret < 0 || (size_t)ret >= clen) |
91c3830e | 1170 | return -1; |
87da4ec3 | 1171 | |
7133b912 | 1172 | /* If we are running on a devtmpfs mapping, dev/pts may already exist. |
bc6928ff MW |
1173 | * If not, then create it and exit if that fails... |
1174 | */ | |
87da4ec3 | 1175 | if (!dir_exists(path)) { |
bc6928ff | 1176 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
7133b912 CB |
1177 | if (ret < 0) { |
1178 | SYSERROR("Failed to create directory \"%s\"", path); | |
bc6928ff MW |
1179 | return -1; |
1180 | } | |
91c3830e SH |
1181 | } |
1182 | ||
7133b912 | 1183 | INFO("Prepared \"/dev\""); |
91c3830e SH |
1184 | return 0; |
1185 | } | |
1186 | ||
c6883f38 | 1187 | struct lxc_devs { |
74a3920a | 1188 | const char *name; |
c6883f38 SH |
1189 | mode_t mode; |
1190 | int maj; | |
1191 | int min; | |
1192 | }; | |
1193 | ||
74a3920a | 1194 | static const struct lxc_devs lxc_devs[] = { |
06749971 CB |
1195 | { "null", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 3 }, |
1196 | { "zero", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 5 }, | |
1197 | { "full", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 7 }, | |
1198 | { "urandom", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 9 }, | |
1199 | { "random", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 8 }, | |
1200 | { "tty", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 5, 0 }, | |
c6883f38 SH |
1201 | }; |
1202 | ||
27245ff7 | 1203 | static int lxc_fill_autodev(const struct lxc_rootfs *rootfs) |
c6883f38 SH |
1204 | { |
1205 | int ret; | |
c6883f38 SH |
1206 | char path[MAXPATHLEN]; |
1207 | int i; | |
3a32201c | 1208 | mode_t cmask; |
c6883f38 | 1209 | |
3999be0a CB |
1210 | ret = snprintf(path, MAXPATHLEN, "%s/dev", |
1211 | rootfs->path ? rootfs->mount : ""); | |
1212 | if (ret < 0 || ret >= MAXPATHLEN) | |
c6883f38 | 1213 | return -1; |
91c3830e | 1214 | |
0bbf8572 CB |
1215 | /* ignore, just don't try to fill in */ |
1216 | if (!dir_exists(path)) | |
9cb4d183 SH |
1217 | return 0; |
1218 | ||
3999be0a CB |
1219 | INFO("Populating \"/dev\""); |
1220 | ||
3a32201c | 1221 | cmask = umask(S_IXUSR | S_IXGRP | S_IXOTH); |
c6883f38 | 1222 | for (i = 0; i < sizeof(lxc_devs) / sizeof(lxc_devs[0]); i++) { |
74a3920a | 1223 | const struct lxc_devs *d = &lxc_devs[i]; |
0728ebf4 | 1224 | |
3999be0a CB |
1225 | ret = snprintf(path, MAXPATHLEN, "%s/dev/%s", |
1226 | rootfs->path ? rootfs->mount : "", d->name); | |
c6883f38 SH |
1227 | if (ret < 0 || ret >= MAXPATHLEN) |
1228 | return -1; | |
0bbf8572 | 1229 | |
c6883f38 | 1230 | ret = mknod(path, d->mode, makedev(d->maj, d->min)); |
0bbf8572 | 1231 | if (ret < 0) { |
9cb4d183 | 1232 | FILE *pathfile; |
3999be0a | 1233 | char hostpath[MAXPATHLEN]; |
9cb4d183 | 1234 | |
0bbf8572 CB |
1235 | if (errno == EEXIST) { |
1236 | DEBUG("\"%s\" device already existed", path); | |
1237 | continue; | |
1238 | } | |
1239 | ||
1240 | /* Unprivileged containers cannot create devices, so | |
1241 | * bind mount the device from the host. | |
1242 | */ | |
9cb4d183 SH |
1243 | ret = snprintf(hostpath, MAXPATHLEN, "/dev/%s", d->name); |
1244 | if (ret < 0 || ret >= MAXPATHLEN) | |
1245 | return -1; | |
3999be0a | 1246 | |
9cb4d183 SH |
1247 | pathfile = fopen(path, "wb"); |
1248 | if (!pathfile) { | |
3999be0a | 1249 | SYSERROR("Failed to create file \"%s\"", path); |
9cb4d183 SH |
1250 | return -1; |
1251 | } | |
1252 | fclose(pathfile); | |
3999be0a CB |
1253 | |
1254 | ret = safe_mount(hostpath, path, 0, MS_BIND, NULL, | |
1255 | rootfs->path ? rootfs->mount : NULL); | |
1256 | if (ret < 0) { | |
1257 | SYSERROR("Failed to bind mount \"%s\" from " | |
1258 | "host into container", | |
1259 | d->name); | |
9cb4d183 SH |
1260 | return -1; |
1261 | } | |
3999be0a CB |
1262 | DEBUG("Bind mounted \"%s\" onto \"%s\"", hostpath, |
1263 | path); | |
0bbf8572 | 1264 | } else { |
3999be0a | 1265 | DEBUG("Created device node \"%s\"", path); |
c6883f38 SH |
1266 | } |
1267 | } | |
3a32201c | 1268 | umask(cmask); |
c6883f38 | 1269 | |
3999be0a | 1270 | INFO("Populated \"/dev\""); |
c6883f38 SH |
1271 | return 0; |
1272 | } | |
1273 | ||
9aa76a17 | 1274 | static int lxc_setup_rootfs(struct lxc_conf *conf) |
0ad19a3f | 1275 | { |
9aa76a17 | 1276 | int ret; |
10bc1861 | 1277 | struct lxc_storage *bdev; |
91c3e281 | 1278 | const struct lxc_rootfs *rootfs; |
cc28d0b0 | 1279 | |
91c3e281 | 1280 | rootfs = &conf->rootfs; |
a0f379bf | 1281 | if (!rootfs->path) { |
91c3e281 CB |
1282 | if (mount("", "/", NULL, MS_SLAVE | MS_REC, 0)) { |
1283 | SYSERROR("Failed to make / rslave."); | |
a0f379bf DW |
1284 | return -1; |
1285 | } | |
c69bd12f | 1286 | return 0; |
a0f379bf | 1287 | } |
0ad19a3f | 1288 | |
12297168 | 1289 | if (access(rootfs->mount, F_OK)) { |
91c3e281 | 1290 | SYSERROR("Failed to access to \"%s\". Check it is present.", |
12297168 | 1291 | rootfs->mount); |
b1789442 DL |
1292 | return -1; |
1293 | } | |
1294 | ||
8a388ed4 | 1295 | bdev = storage_init(conf); |
9aa76a17 CB |
1296 | if (!bdev) { |
1297 | ERROR("Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\".", | |
91c3e281 CB |
1298 | rootfs->path, rootfs->mount, |
1299 | rootfs->options ? rootfs->options : "(null)"); | |
9aa76a17 | 1300 | return -1; |
9be53773 | 1301 | } |
9aa76a17 CB |
1302 | |
1303 | ret = bdev->ops->mount(bdev); | |
10bc1861 | 1304 | storage_put(bdev); |
9aa76a17 | 1305 | if (ret < 0) { |
91c3e281 CB |
1306 | ERROR("Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\".", |
1307 | rootfs->path, rootfs->mount, | |
1308 | rootfs->options ? rootfs->options : "(null)"); | |
c3f0a28c | 1309 | return -1; |
1310 | } | |
0ad19a3f | 1311 | |
91c3e281 CB |
1312 | DEBUG("Mounted rootfs \"%s\" onto \"%s\" with options \"%s\".", |
1313 | rootfs->path, rootfs->mount, | |
1314 | rootfs->options ? rootfs->options : "(null)"); | |
9aa76a17 | 1315 | |
ac778708 DL |
1316 | return 0; |
1317 | } | |
1318 | ||
91e93c71 AV |
1319 | int prepare_ramfs_root(char *root) |
1320 | { | |
eab15c1e | 1321 | char buf[LXC_LINELEN], *p; |
91e93c71 AV |
1322 | char nroot[PATH_MAX]; |
1323 | FILE *f; | |
1324 | int i; | |
1325 | char *p2; | |
1326 | ||
1327 | if (realpath(root, nroot) == NULL) | |
39c7b795 | 1328 | return -errno; |
91e93c71 AV |
1329 | |
1330 | if (chdir("/") == -1) | |
39c7b795 | 1331 | return -errno; |
91e93c71 AV |
1332 | |
1333 | /* | |
1334 | * We could use here MS_MOVE, but in userns this mount is | |
1335 | * locked and can't be moved. | |
1336 | */ | |
39c7b795 | 1337 | if (mount(root, "/", NULL, MS_REC | MS_BIND, NULL) < 0) { |
91e93c71 | 1338 | SYSERROR("Failed to move %s into /", root); |
39c7b795 | 1339 | return -errno; |
91e93c71 AV |
1340 | } |
1341 | ||
39c7b795 | 1342 | if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL) < 0) { |
91e93c71 | 1343 | SYSERROR("Failed to make . rprivate"); |
39c7b795 | 1344 | return -errno; |
91e93c71 AV |
1345 | } |
1346 | ||
1347 | /* | |
1348 | * The following code cleans up inhereted mounts which are not | |
1349 | * required for CT. | |
1350 | * | |
1351 | * The mountinfo file shows not all mounts, if a few points have been | |
1352 | * unmounted between read operations from the mountinfo. So we need to | |
1353 | * read mountinfo a few times. | |
1354 | * | |
1355 | * This loop can be skipped if a container uses unserns, because all | |
1356 | * inherited mounts are locked and we should live with all this trash. | |
1357 | */ | |
1358 | while (1) { | |
1359 | int progress = 0; | |
1360 | ||
1361 | f = fopen("./proc/self/mountinfo", "r"); | |
1362 | if (!f) { | |
1363 | SYSERROR("Unable to open /proc/self/mountinfo"); | |
1364 | return -1; | |
1365 | } | |
eab15c1e | 1366 | while (fgets(buf, LXC_LINELEN, f)) { |
91e93c71 AV |
1367 | for (p = buf, i=0; p && i < 4; i++) |
1368 | p = strchr(p+1, ' '); | |
1369 | if (!p) | |
1370 | continue; | |
1371 | p2 = strchr(p+1, ' '); | |
1372 | if (!p2) | |
1373 | continue; | |
1374 | ||
1375 | *p2 = '\0'; | |
1376 | *p = '.'; | |
1377 | ||
1378 | if (strcmp(p + 1, "/") == 0) | |
1379 | continue; | |
1380 | if (strcmp(p + 1, "/proc") == 0) | |
1381 | continue; | |
1382 | ||
1383 | if (umount2(p, MNT_DETACH) == 0) | |
1384 | progress++; | |
1385 | } | |
1386 | fclose(f); | |
1387 | if (!progress) | |
1388 | break; | |
1389 | } | |
1390 | ||
8bea9fae PR |
1391 | /* This also can be skipped if a container uses unserns */ |
1392 | umount2("./proc", MNT_DETACH); | |
91e93c71 AV |
1393 | |
1394 | /* It is weird, but chdir("..") moves us in a new root */ | |
1395 | if (chdir("..") == -1) { | |
1396 | SYSERROR("Unable to change working directory"); | |
1397 | return -1; | |
1398 | } | |
1399 | ||
1400 | if (chroot(".") == -1) { | |
1401 | SYSERROR("Unable to chroot"); | |
1402 | return -1; | |
1403 | } | |
1404 | ||
1405 | return 0; | |
1406 | } | |
1407 | ||
74a3920a | 1408 | static int setup_pivot_root(const struct lxc_rootfs *rootfs) |
ac778708 | 1409 | { |
39c7b795 CB |
1410 | if (!rootfs->path) { |
1411 | DEBUG("container does not have a rootfs, so not doing pivot root"); | |
ac778708 | 1412 | return 0; |
39c7b795 | 1413 | } |
ac778708 | 1414 | |
91e93c71 | 1415 | if (detect_ramfs_rootfs()) { |
39c7b795 CB |
1416 | DEBUG("detected that container is on ramfs"); |
1417 | if (prepare_ramfs_root(rootfs->mount)) { | |
1418 | ERROR("failed to prepare minimal ramfs root"); | |
91e93c71 | 1419 | return -1; |
39c7b795 CB |
1420 | } |
1421 | ||
1422 | DEBUG("prepared ramfs root for container"); | |
1423 | return 0; | |
1424 | } | |
1425 | ||
1426 | if (setup_rootfs_pivot_root(rootfs->mount) < 0) { | |
1427 | ERROR("failed to pivot root"); | |
25368b52 | 1428 | return -1; |
c69bd12f DL |
1429 | } |
1430 | ||
39c7b795 | 1431 | DEBUG("finished pivot root"); |
25368b52 | 1432 | return 0; |
0ad19a3f | 1433 | } |
1434 | ||
f4900711 CB |
1435 | static struct id_map *find_mapped_nsid_entry(struct lxc_conf *conf, unsigned id, |
1436 | enum idtype idtype) | |
1437 | { | |
1438 | struct lxc_list *it; | |
1439 | struct id_map *map; | |
1440 | struct id_map *retmap = NULL; | |
1441 | ||
dcf0ffdf CB |
1442 | /* Shortcut for container's root mappings. */ |
1443 | if (id == 0) { | |
1444 | if (idtype == ID_TYPE_UID) | |
1445 | return conf->root_nsuid_map; | |
1446 | ||
1447 | if (idtype == ID_TYPE_GID) | |
1448 | return conf->root_nsgid_map; | |
1449 | } | |
1450 | ||
f4900711 CB |
1451 | lxc_list_for_each(it, &conf->id_map) { |
1452 | map = it->elem; | |
1453 | if (map->idtype != idtype) | |
1454 | continue; | |
1455 | ||
1456 | if (id >= map->nsid && id < map->nsid + map->range) { | |
1457 | retmap = map; | |
1458 | break; | |
1459 | } | |
1460 | } | |
1461 | ||
1462 | return retmap; | |
1463 | } | |
1464 | ||
1465 | static int lxc_setup_devpts(struct lxc_conf *conf) | |
3c26f34e | 1466 | { |
70761e5e | 1467 | int ret; |
f4900711 | 1468 | const char *default_devpts_mntopts; |
9d28c4f9 | 1469 | char devpts_mntopts[256]; |
77890c6d | 1470 | |
f4900711 | 1471 | if (conf->pts <= 0) { |
70761e5e CB |
1472 | DEBUG("no new devpts instance will be mounted since no pts " |
1473 | "devices are requested"); | |
d852c78c | 1474 | return 0; |
3c26f34e | 1475 | } |
1476 | ||
f4900711 CB |
1477 | if (!find_mapped_nsid_entry(conf, 5, ID_TYPE_GID)) |
1478 | default_devpts_mntopts = "newinstance,ptmxmode=0666,mode=0620"; | |
1479 | else | |
1480 | default_devpts_mntopts = "newinstance,ptmxmode=0666,mode=0620,gid=5"; | |
1481 | ||
9d28c4f9 | 1482 | ret = snprintf(devpts_mntopts, sizeof(devpts_mntopts), "%s,max=%d", |
f4900711 | 1483 | default_devpts_mntopts, conf->pts); |
9d28c4f9 CB |
1484 | if (ret < 0 || (size_t)ret >= sizeof(devpts_mntopts)) |
1485 | return -1; | |
1486 | ||
d5cb35d6 | 1487 | /* Unmount old devpts instance. */ |
70761e5e CB |
1488 | ret = access("/dev/pts/ptmx", F_OK); |
1489 | if (!ret) { | |
70761e5e CB |
1490 | ret = umount("/dev/pts"); |
1491 | if (ret < 0) { | |
1492 | SYSERROR("failed to unmount old devpts instance"); | |
1493 | return -1; | |
7e40254a | 1494 | } |
70761e5e | 1495 | DEBUG("unmounted old /dev/pts instance"); |
7e40254a JTLB |
1496 | } |
1497 | ||
70761e5e CB |
1498 | /* Create mountpoint for devpts instance. */ |
1499 | ret = mkdir("/dev/pts", 0755); | |
1500 | if (ret < 0 && errno != EEXIST) { | |
1501 | SYSERROR("failed to create the \"/dev/pts\" directory"); | |
3c26f34e | 1502 | return -1; |
1503 | } | |
1504 | ||
70761e5e | 1505 | /* Mount new devpts instance. */ |
f4900711 | 1506 | ret = mount("devpts", "/dev/pts", "devpts", MS_NOSUID | MS_NOEXEC, devpts_mntopts); |
70761e5e CB |
1507 | if (ret < 0) { |
1508 | SYSERROR("failed to mount new devpts instance"); | |
1509 | return -1; | |
1510 | } | |
f4f52cb5 | 1511 | DEBUG("mount new devpts instance with options \"%s\"", devpts_mntopts); |
70761e5e | 1512 | |
d5cb35d6 | 1513 | /* Remove any pre-existing /dev/ptmx file. */ |
70761e5e | 1514 | ret = access("/dev/ptmx", F_OK); |
d5cb35d6 CB |
1515 | if (!ret) { |
1516 | ret = remove("/dev/ptmx"); | |
1517 | if (ret < 0) { | |
1518 | SYSERROR("failed to remove existing \"/dev/ptmx\""); | |
1519 | return -1; | |
70761e5e | 1520 | } |
d5cb35d6 | 1521 | DEBUG("removed existing \"/dev/ptmx\""); |
3c26f34e | 1522 | } |
1523 | ||
d5cb35d6 CB |
1524 | /* Create dummy /dev/ptmx file as bind mountpoint for /dev/pts/ptmx. */ |
1525 | ret = open("/dev/ptmx", O_CREAT, 0666); | |
1526 | if (ret < 0) { | |
1527 | SYSERROR("failed to create dummy \"/dev/ptmx\" file as bind mount target"); | |
1528 | return -1; | |
1529 | } | |
e87bd19c | 1530 | close(ret); |
d5cb35d6 | 1531 | DEBUG("created dummy \"/dev/ptmx\" file as bind mount target"); |
77890c6d | 1532 | |
d5cb35d6 | 1533 | /* Fallback option: create symlink /dev/ptmx -> /dev/pts/ptmx */ |
e87bd19c | 1534 | ret = mount("/dev/pts/ptmx", "/dev/ptmx", NULL, MS_BIND, NULL); |
d5cb35d6 CB |
1535 | if (!ret) { |
1536 | DEBUG("bind mounted \"/dev/pts/ptmx\" to \"/dev/ptmx\""); | |
1537 | return 0; | |
1538 | } else { | |
1539 | /* Fallthrough and try to create a symlink. */ | |
1540 | ERROR("failed to bind mount \"/dev/pts/ptmx\" to \"/dev/ptmx\""); | |
1541 | } | |
1542 | ||
1543 | /* Remove the dummy /dev/ptmx file we created above. */ | |
1544 | ret = remove("/dev/ptmx"); | |
70761e5e | 1545 | if (ret < 0) { |
d5cb35d6 CB |
1546 | SYSERROR("failed to remove existing \"/dev/ptmx\""); |
1547 | return -1; | |
1548 | } | |
1549 | ||
1550 | /* Fallback option: Create symlink /dev/ptmx -> /dev/pts/ptmx. */ | |
1551 | ret = symlink("/dev/pts/ptmx", "/dev/ptmx"); | |
1552 | if (ret < 0) { | |
1553 | SYSERROR("failed to create symlink \"/dev/ptmx\" -> \"/dev/pts/ptmx\""); | |
3c26f34e | 1554 | return -1; |
1555 | } | |
d5cb35d6 | 1556 | DEBUG("created symlink \"/dev/ptmx\" -> \"/dev/pts/ptmx\""); |
cd54d859 | 1557 | |
3c26f34e | 1558 | return 0; |
1559 | } | |
1560 | ||
cccc74b5 DL |
1561 | static int setup_personality(int persona) |
1562 | { | |
6ff05e18 | 1563 | #if HAVE_SYS_PERSONALITY_H |
cccc74b5 DL |
1564 | if (persona == -1) |
1565 | return 0; | |
1566 | ||
1567 | if (personality(persona) < 0) { | |
1568 | SYSERROR("failed to set personality to '0x%x'", persona); | |
1569 | return -1; | |
1570 | } | |
1571 | ||
1572 | INFO("set personality to '0x%x'", persona); | |
6ff05e18 | 1573 | #endif |
cccc74b5 DL |
1574 | |
1575 | return 0; | |
1576 | } | |
1577 | ||
3d7d929a | 1578 | static int lxc_setup_dev_console(const struct lxc_rootfs *rootfs, |
dcad02f8 | 1579 | const struct lxc_terminal *console) |
6e590161 | 1580 | { |
63376d7d | 1581 | char path[MAXPATHLEN]; |
0728ebf4 | 1582 | int ret, fd; |
86530b0a | 1583 | char *rootfs_path = rootfs->path ? rootfs->mount : ""; |
52e35957 | 1584 | |
8b1b1210 CB |
1585 | if (console->path && !strcmp(console->path, "none")) |
1586 | return 0; | |
1587 | ||
86530b0a | 1588 | ret = snprintf(path, sizeof(path), "%s/dev/console", rootfs_path); |
3d7d929a | 1589 | if (ret < 0 || (size_t)ret >= sizeof(path)) |
7c6ef2a2 | 1590 | return -1; |
52e35957 | 1591 | |
8b1b1210 CB |
1592 | /* When we are asked to setup a console we remove any previous |
1593 | * /dev/console bind-mounts. | |
1594 | */ | |
a7ba3c7f CB |
1595 | if (file_exists(path)) { |
1596 | ret = lxc_unstack_mountpoint(path, false); | |
1597 | if (ret < 0) { | |
86530b0a | 1598 | ERROR("Failed to unmount \"%s\": %s", path, strerror(errno)); |
a7ba3c7f CB |
1599 | return -ret; |
1600 | } else { | |
86530b0a | 1601 | DEBUG("Cleared all (%d) mounts from \"%s\"", ret, path); |
a7ba3c7f | 1602 | } |
8b1b1210 CB |
1603 | } |
1604 | ||
1605 | /* For unprivileged containers autodev or automounts will already have | |
1606 | * taken care of creating /dev/console. | |
1607 | */ | |
0728ebf4 TA |
1608 | fd = open(path, O_CREAT | O_EXCL, S_IXUSR | S_IXGRP | S_IXOTH); |
1609 | if (fd < 0) { | |
1610 | if (errno != EEXIST) { | |
86530b0a | 1611 | SYSERROR("Failed to create console"); |
3d7d929a | 1612 | return -errno; |
0728ebf4 TA |
1613 | } |
1614 | } else { | |
1615 | close(fd); | |
52e35957 DL |
1616 | } |
1617 | ||
86530b0a L |
1618 | ret = chmod(console->name, S_IXUSR | S_IXGRP | S_IXOTH); |
1619 | if (ret < 0) { | |
1620 | SYSERROR("Failed to set mode '0%o' to '%s'", S_IXUSR | S_IXGRP | S_IXOTH, console->name); | |
3d7d929a | 1621 | return -errno; |
63376d7d | 1622 | } |
13954cce | 1623 | |
86530b0a L |
1624 | ret = safe_mount(console->name, path, "none", MS_BIND, 0, rootfs_path); |
1625 | if (ret < 0) { | |
1626 | ERROR("Failed to mount '%s' on '%s'", console->name, path); | |
6e590161 | 1627 | return -1; |
1628 | } | |
1629 | ||
86530b0a | 1630 | DEBUG("Mounted pts device \"%s\" onto \"%s\"", console->name, path); |
7c6ef2a2 SH |
1631 | return 0; |
1632 | } | |
1633 | ||
3d7d929a | 1634 | static int lxc_setup_ttydir_console(const struct lxc_rootfs *rootfs, |
dcad02f8 | 1635 | const struct lxc_terminal *console, |
3d7d929a | 1636 | char *ttydir) |
7c6ef2a2 | 1637 | { |
3dc035f1 | 1638 | int ret, fd; |
3d7d929a | 1639 | char path[MAXPATHLEN], lxcpath[MAXPATHLEN]; |
86530b0a | 1640 | char *rootfs_path = rootfs->path ? rootfs->mount : ""; |
7c6ef2a2 | 1641 | |
3dc035f1 L |
1642 | if (console->path && !strcmp(console->path, "none")) |
1643 | return 0; | |
1644 | ||
7c6ef2a2 | 1645 | /* create rootfs/dev/<ttydir> directory */ |
86530b0a | 1646 | ret = snprintf(path, sizeof(path), "%s/dev/%s", rootfs_path, ttydir); |
3d7d929a | 1647 | if (ret < 0 || (size_t)ret >= sizeof(path)) |
7c6ef2a2 | 1648 | return -1; |
3d7d929a | 1649 | |
7c6ef2a2 SH |
1650 | ret = mkdir(path, 0755); |
1651 | if (ret && errno != EEXIST) { | |
86530b0a | 1652 | SYSERROR("Failed with errno %d to create %s", errno, path); |
3d7d929a | 1653 | return -errno; |
7c6ef2a2 | 1654 | } |
4742cd9a | 1655 | DEBUG("Created directory for console and tty devices at \"%s\"", path); |
7c6ef2a2 | 1656 | |
86530b0a | 1657 | ret = snprintf(lxcpath, sizeof(lxcpath), "%s/dev/%s/console", rootfs_path, ttydir); |
3d7d929a CB |
1658 | if (ret < 0 || (size_t)ret >= sizeof(lxcpath)) |
1659 | return -1; | |
1660 | ||
7c6ef2a2 | 1661 | ret = creat(lxcpath, 0660); |
3d7d929a | 1662 | if (ret == -1 && errno != EEXIST) { |
86530b0a | 1663 | SYSERROR("Error %d creating %s", errno, lxcpath); |
3d7d929a | 1664 | return -errno; |
7c6ef2a2 | 1665 | } |
4d44e274 SH |
1666 | if (ret >= 0) |
1667 | close(ret); | |
7c6ef2a2 | 1668 | |
86530b0a | 1669 | ret = snprintf(path, sizeof(path), "%s/dev/console", rootfs_path); |
3dc035f1 | 1670 | if (ret < 0 || (size_t)ret >= sizeof(path)) |
7c6ef2a2 | 1671 | return -1; |
2a12fefd | 1672 | |
3dc035f1 | 1673 | if (file_exists(path)) { |
a7ba3c7f | 1674 | ret = lxc_unstack_mountpoint(path, false); |
2a12fefd | 1675 | if (ret < 0) { |
86530b0a | 1676 | ERROR("Failed to unmount \"%s\": %s", path, strerror(errno)); |
a7ba3c7f CB |
1677 | return -ret; |
1678 | } else { | |
86530b0a | 1679 | DEBUG("Cleared all (%d) mounts from \"%s\"", ret, path); |
a7ba3c7f | 1680 | } |
3dc035f1 | 1681 | } |
2a12fefd | 1682 | |
3dc035f1 L |
1683 | fd = open(path, O_CREAT | O_EXCL, S_IXUSR | S_IXGRP | S_IXOTH); |
1684 | if (fd < 0) { | |
1685 | if (errno != EEXIST) { | |
86530b0a | 1686 | SYSERROR("Failed to create console"); |
3dc035f1 | 1687 | return -errno; |
2a12fefd | 1688 | } |
3dc035f1 L |
1689 | } else { |
1690 | close(fd); | |
7c6ef2a2 SH |
1691 | } |
1692 | ||
86530b0a L |
1693 | ret = chmod(console->name, S_IXUSR | S_IXGRP | S_IXOTH); |
1694 | if (ret < 0) { | |
1695 | SYSERROR("Failed to set mode '0%o' to '%s'", S_IXUSR | S_IXGRP | S_IXOTH, console->name); | |
2a12fefd CB |
1696 | return -errno; |
1697 | } | |
1698 | ||
3dc035f1 | 1699 | /* bind mount console->name to '/dev/<ttydir>/console' */ |
86530b0a L |
1700 | ret = safe_mount(console->name, lxcpath, "none", MS_BIND, 0, rootfs_path); |
1701 | if (ret < 0) { | |
1702 | ERROR("Failed to mount '%s' on '%s'", console->name, lxcpath); | |
7c6ef2a2 SH |
1703 | return -1; |
1704 | } | |
86530b0a | 1705 | DEBUG("Mounted \"%s\" onto \"%s\"", console->name, lxcpath); |
3dc035f1 L |
1706 | |
1707 | /* bind mount '/dev/<ttydir>/console' to '/dev/console' */ | |
86530b0a L |
1708 | ret = safe_mount(lxcpath, path, "none", MS_BIND, 0, rootfs_path); |
1709 | if (ret < 0) { | |
1710 | ERROR("Failed to mount '%s' on '%s'", console->name, lxcpath); | |
3dc035f1 L |
1711 | return -1; |
1712 | } | |
86530b0a | 1713 | DEBUG("Mounted \"%s\" onto \"%s\"", console->name, lxcpath); |
3dc035f1 | 1714 | |
86530b0a | 1715 | DEBUG("Console has been setup under \"%s\" and mounted to \"%s\"", lxcpath, path); |
7c6ef2a2 | 1716 | |
6e590161 | 1717 | return 0; |
1718 | } | |
1719 | ||
3d7d929a | 1720 | static int lxc_setup_console(const struct lxc_rootfs *rootfs, |
dcad02f8 | 1721 | const struct lxc_terminal *console, char *ttydir) |
7c6ef2a2 | 1722 | { |
3d7d929a | 1723 | |
7c6ef2a2 | 1724 | if (!ttydir) |
3d7d929a | 1725 | return lxc_setup_dev_console(rootfs, console); |
7c6ef2a2 | 1726 | |
3d7d929a | 1727 | return lxc_setup_ttydir_console(rootfs, console, ttydir); |
7c6ef2a2 SH |
1728 | } |
1729 | ||
998ac676 RT |
1730 | static void parse_mntopt(char *opt, unsigned long *flags, char **data) |
1731 | { | |
1732 | struct mount_opt *mo; | |
1733 | ||
1734 | /* If opt is found in mount_opt, set or clear flags. | |
1735 | * Otherwise append it to data. */ | |
1736 | ||
1737 | for (mo = &mount_opt[0]; mo->name != NULL; mo++) { | |
1738 | if (!strncmp(opt, mo->name, strlen(mo->name))) { | |
1739 | if (mo->clear) | |
1740 | *flags &= ~mo->flag; | |
1741 | else | |
1742 | *flags |= mo->flag; | |
1743 | return; | |
1744 | } | |
1745 | } | |
1746 | ||
1747 | if (strlen(*data)) | |
1748 | strcat(*data, ","); | |
1749 | strcat(*data, opt); | |
1750 | } | |
1751 | ||
a17b1e65 | 1752 | int parse_mntopts(const char *mntopts, unsigned long *mntflags, |
998ac676 RT |
1753 | char **mntdata) |
1754 | { | |
1755 | char *s, *data; | |
1756 | char *p, *saveptr = NULL; | |
1757 | ||
911324ef | 1758 | *mntdata = NULL; |
91656ce5 | 1759 | *mntflags = 0L; |
911324ef DL |
1760 | |
1761 | if (!mntopts) | |
998ac676 RT |
1762 | return 0; |
1763 | ||
911324ef | 1764 | s = strdup(mntopts); |
998ac676 | 1765 | if (!s) { |
36eb9bde | 1766 | SYSERROR("failed to allocate memory"); |
998ac676 RT |
1767 | return -1; |
1768 | } | |
1769 | ||
1770 | data = malloc(strlen(s) + 1); | |
1771 | if (!data) { | |
36eb9bde | 1772 | SYSERROR("failed to allocate memory"); |
998ac676 RT |
1773 | free(s); |
1774 | return -1; | |
1775 | } | |
1776 | *data = 0; | |
1777 | ||
1778 | for (p = strtok_r(s, ",", &saveptr); p != NULL; | |
1779 | p = strtok_r(NULL, ",", &saveptr)) | |
1780 | parse_mntopt(p, mntflags, &data); | |
1781 | ||
1782 | if (*data) | |
1783 | *mntdata = data; | |
1784 | else | |
1785 | free(data); | |
1786 | free(s); | |
1787 | ||
1788 | return 0; | |
1789 | } | |
1790 | ||
d840039e YT |
1791 | static void parse_propagationopt(char *opt, unsigned long *flags) |
1792 | { | |
1793 | struct mount_opt *mo; | |
1794 | ||
1795 | /* If opt is found in propagation_opt, set or clear flags. */ | |
1796 | ||
1797 | for (mo = &propagation_opt[0]; mo->name != NULL; mo++) { | |
1798 | if (strncmp(opt, mo->name, strlen(mo->name)) == 0) { | |
1799 | if (mo->clear) | |
1800 | *flags &= ~mo->flag; | |
1801 | else | |
1802 | *flags |= mo->flag; | |
1803 | return; | |
1804 | } | |
1805 | } | |
1806 | } | |
1807 | ||
1808 | static int parse_propagationopts(const char *mntopts, unsigned long *pflags) | |
1809 | { | |
1810 | char *s; | |
1811 | char *p, *saveptr = NULL; | |
1812 | *pflags = 0L; | |
1813 | ||
1814 | if (!mntopts) | |
1815 | return 0; | |
1816 | ||
1817 | s = strdup(mntopts); | |
1818 | if (!s) { | |
1819 | SYSERROR("Failed to allocate memory"); | |
1820 | return -ENOMEM; | |
1821 | } | |
1822 | ||
1823 | for (p = strtok_r(s, ",", &saveptr); p != NULL; | |
1824 | p = strtok_r(NULL, ",", &saveptr)) | |
1825 | parse_propagationopt(p, pflags); | |
1826 | ||
1827 | free(s); | |
1828 | return 0; | |
1829 | } | |
1830 | ||
6fd5e769 SH |
1831 | static void null_endofword(char *word) |
1832 | { | |
1833 | while (*word && *word != ' ' && *word != '\t') | |
1834 | word++; | |
1835 | *word = '\0'; | |
1836 | } | |
1837 | ||
1838 | /* | |
1839 | * skip @nfields spaces in @src | |
1840 | */ | |
1841 | static char *get_field(char *src, int nfields) | |
1842 | { | |
1843 | char *p = src; | |
1844 | int i; | |
1845 | ||
1846 | for (i = 0; i < nfields; i++) { | |
1847 | while (*p && *p != ' ' && *p != '\t') | |
1848 | p++; | |
1849 | if (!*p) | |
1850 | break; | |
1851 | p++; | |
1852 | } | |
1853 | return p; | |
1854 | } | |
1855 | ||
911324ef DL |
1856 | static int mount_entry(const char *fsname, const char *target, |
1857 | const char *fstype, unsigned long mountflags, | |
d840039e YT |
1858 | unsigned long pflags, const char *data, bool optional, |
1859 | bool dev, bool relative, const char *rootfs) | |
911324ef | 1860 | { |
0ac4b28a | 1861 | int ret; |
181437fd YT |
1862 | char srcbuf[MAXPATHLEN]; |
1863 | const char *srcpath = fsname; | |
614305f3 | 1864 | #ifdef HAVE_STATVFS |
2938f7c8 | 1865 | struct statvfs sb; |
614305f3 | 1866 | #endif |
2938f7c8 | 1867 | |
181437fd YT |
1868 | if (relative) { |
1869 | ret = snprintf(srcbuf, MAXPATHLEN, "%s/%s", rootfs ? rootfs : "/", fsname ? fsname : ""); | |
1870 | if (ret < 0 || ret >= MAXPATHLEN) { | |
1871 | ERROR("source path is too long"); | |
1872 | return -1; | |
1873 | } | |
1874 | srcpath = srcbuf; | |
1875 | } | |
1876 | ||
1877 | ret = safe_mount(srcpath, target, fstype, mountflags & ~MS_REMOUNT, data, | |
0ac4b28a CB |
1878 | rootfs); |
1879 | if (ret < 0) { | |
1fc64d22 | 1880 | if (optional) { |
0ac4b28a | 1881 | INFO("Failed to mount \"%s\" on \"%s\" (optional): %s", |
181437fd | 1882 | srcpath ? srcpath : "(null)", target, strerror(errno)); |
1fc64d22 SG |
1883 | return 0; |
1884 | } | |
0ac4b28a | 1885 | |
0103eb53 | 1886 | SYSERROR("Failed to mount \"%s\" on \"%s\"", |
181437fd | 1887 | srcpath ? srcpath : "(null)", target); |
0ac4b28a | 1888 | return -1; |
911324ef DL |
1889 | } |
1890 | ||
1891 | if ((mountflags & MS_REMOUNT) || (mountflags & MS_BIND)) { | |
7c5b6e7c | 1892 | unsigned long rqd_flags = 0; |
0ac4b28a CB |
1893 | |
1894 | DEBUG("Remounting \"%s\" on \"%s\" to respect bind or remount " | |
181437fd | 1895 | "options", srcpath ? srcpath : "(none)", target ? target : "(none)"); |
0ac4b28a | 1896 | |
7c5b6e7c AS |
1897 | if (mountflags & MS_RDONLY) |
1898 | rqd_flags |= MS_RDONLY; | |
614305f3 | 1899 | #ifdef HAVE_STATVFS |
181437fd | 1900 | if (srcpath && statvfs(srcpath, &sb) == 0) { |
7c5b6e7c | 1901 | unsigned long required_flags = rqd_flags; |
0ac4b28a | 1902 | |
2938f7c8 SH |
1903 | if (sb.f_flag & MS_NOSUID) |
1904 | required_flags |= MS_NOSUID; | |
0ac4b28a | 1905 | |
ae7a770e | 1906 | if (sb.f_flag & MS_NODEV && !dev) |
2938f7c8 | 1907 | required_flags |= MS_NODEV; |
0ac4b28a | 1908 | |
2938f7c8 SH |
1909 | if (sb.f_flag & MS_RDONLY) |
1910 | required_flags |= MS_RDONLY; | |
0ac4b28a | 1911 | |
2938f7c8 SH |
1912 | if (sb.f_flag & MS_NOEXEC) |
1913 | required_flags |= MS_NOEXEC; | |
0ac4b28a CB |
1914 | |
1915 | DEBUG("Flags for \"%s\" were %lu, required extra flags " | |
181437fd | 1916 | "are %lu", srcpath, sb.f_flag, required_flags); |
0ac4b28a CB |
1917 | |
1918 | /* If this was a bind mount request, and required_flags | |
2938f7c8 | 1919 | * does not have any flags which are not already in |
0ac4b28a | 1920 | * mountflags, then skip the remount. |
2938f7c8 SH |
1921 | */ |
1922 | if (!(mountflags & MS_REMOUNT)) { | |
0ac4b28a CB |
1923 | if (!(required_flags & ~mountflags) && |
1924 | rqd_flags == 0) { | |
1925 | DEBUG("Mountflags already were %lu, " | |
1926 | "skipping remount", mountflags); | |
2938f7c8 SH |
1927 | goto skipremount; |
1928 | } | |
1929 | } | |
0ac4b28a | 1930 | |
2938f7c8 | 1931 | mountflags |= required_flags; |
6fd5e769 | 1932 | } |
614305f3 | 1933 | #endif |
911324ef | 1934 | |
181437fd | 1935 | ret = mount(srcpath, target, fstype, mountflags | MS_REMOUNT, data); |
0ac4b28a | 1936 | if (ret < 0) { |
1fc64d22 | 1937 | if (optional) { |
0ac4b28a | 1938 | INFO("Failed to mount \"%s\" on \"%s\" " |
0103eb53 | 1939 | "(optional): %s", |
181437fd | 1940 | srcpath ? srcpath : "(null)", target, |
0ac4b28a | 1941 | strerror(errno)); |
1fc64d22 SG |
1942 | return 0; |
1943 | } | |
0ac4b28a | 1944 | |
0103eb53 | 1945 | SYSERROR("Failed to mount \"%s\" on \"%s\"", |
181437fd | 1946 | srcpath ? srcpath : "(null)", target); |
0ac4b28a | 1947 | return -1; |
911324ef DL |
1948 | } |
1949 | } | |
1950 | ||
d840039e YT |
1951 | if (pflags) { |
1952 | ret = mount(NULL, target, NULL, pflags, NULL); | |
1953 | if (ret < 0) { | |
1954 | if (optional) { | |
1955 | INFO("%s - Failed to change mount propagation " | |
1956 | "for \"%s\" (optional)", strerror(errno), target); | |
1957 | return 0; | |
1958 | } else { | |
1959 | SYSERROR("Failed to change mount propagation " | |
1960 | "for \"%s\" (optional)", target); | |
1961 | return -1; | |
1962 | } | |
1963 | } | |
1964 | DEBUG("Changed mount propagation for \"%s\"", target); | |
1965 | } | |
1966 | ||
1967 | ||
614305f3 | 1968 | #ifdef HAVE_STATVFS |
6fd5e769 | 1969 | skipremount: |
614305f3 | 1970 | #endif |
0103eb53 | 1971 | DEBUG("Mounted \"%s\" on \"%s\" with filesystem type \"%s\"", |
181437fd | 1972 | srcpath ? srcpath : "(null)", target, fstype); |
911324ef DL |
1973 | |
1974 | return 0; | |
1975 | } | |
1976 | ||
c5e30de4 | 1977 | /* Remove "optional", "create=dir", and "create=file" from mntopt */ |
4e4ca161 SH |
1978 | static void cull_mntent_opt(struct mntent *mntent) |
1979 | { | |
1980 | int i; | |
181437fd | 1981 | char *list[] = {"create=dir", "create=file", "optional", "relative", NULL}; |
c5e30de4 CB |
1982 | |
1983 | for (i = 0; list[i]; i++) { | |
1984 | char *p, *p2; | |
1985 | ||
1986 | p = strstr(mntent->mnt_opts, list[i]); | |
1987 | if (!p) | |
4e4ca161 | 1988 | continue; |
c5e30de4 | 1989 | |
4e4ca161 SH |
1990 | p2 = strchr(p, ','); |
1991 | if (!p2) { | |
1992 | /* no more mntopts, so just chop it here */ | |
1993 | *p = '\0'; | |
1994 | continue; | |
1995 | } | |
c5e30de4 CB |
1996 | |
1997 | memmove(p, p2 + 1, strlen(p2 + 1) + 1); | |
4e4ca161 SH |
1998 | } |
1999 | } | |
2000 | ||
4d5b72a1 | 2001 | static int mount_entry_create_dir_file(const struct mntent *mntent, |
749f98d9 CB |
2002 | const char *path, |
2003 | const struct lxc_rootfs *rootfs, | |
2004 | const char *lxc_name, | |
2005 | const char *lxc_path) | |
0ad19a3f | 2006 | { |
608e3567 | 2007 | int ret = 0; |
911324ef | 2008 | |
749f98d9 CB |
2009 | if (!strncmp(mntent->mnt_type, "overlay", 7)) |
2010 | ret = ovl_mkdir(mntent, rootfs, lxc_name, lxc_path); | |
2011 | else if (!strncmp(mntent->mnt_type, "aufs", 4)) | |
2012 | ret = aufs_mkdir(mntent, rootfs, lxc_name, lxc_path); | |
2013 | if (ret < 0) | |
2014 | return -1; | |
6e46cc0d | 2015 | |
34cfffb3 | 2016 | if (hasmntopt(mntent, "create=dir")) { |
749f98d9 CB |
2017 | ret = mkdir_p(path, 0755); |
2018 | if (ret < 0 && errno != EEXIST) { | |
2019 | SYSERROR("Failed to create directory \"%s\"", path); | |
2020 | return -1; | |
34cfffb3 SG |
2021 | } |
2022 | } | |
2023 | ||
4d5b72a1 | 2024 | if (hasmntopt(mntent, "create=file") && access(path, F_OK)) { |
749f98d9 CB |
2025 | int fd; |
2026 | char *p1, *p2; | |
2027 | ||
2028 | p1 = strdup(path); | |
2029 | if (!p1) | |
2030 | return -1; | |
2031 | ||
2032 | p2 = dirname(p1); | |
2033 | ||
2034 | ret = mkdir_p(p2, 0755); | |
2035 | free(p1); | |
2036 | if (ret < 0 && errno != EEXIST) { | |
2037 | SYSERROR("Failed to create directory \"%s\"", path); | |
2038 | return -1; | |
6e46cc0d | 2039 | } |
749f98d9 CB |
2040 | |
2041 | fd = open(path, O_CREAT, 0644); | |
2042 | if (fd < 0) | |
2043 | return -1; | |
2044 | close(fd); | |
34cfffb3 | 2045 | } |
749f98d9 CB |
2046 | |
2047 | return 0; | |
4d5b72a1 NC |
2048 | } |
2049 | ||
ec50007f CB |
2050 | /* rootfs, lxc_name, and lxc_path can be NULL when the container is created |
2051 | * without a rootfs. */ | |
db4aba38 | 2052 | static inline int mount_entry_on_generic(struct mntent *mntent, |
d8b712bc CB |
2053 | const char *path, |
2054 | const struct lxc_rootfs *rootfs, | |
2055 | const char *lxc_name, | |
2056 | const char *lxc_path) | |
4d5b72a1 | 2057 | { |
d8b712bc | 2058 | int ret; |
d840039e | 2059 | unsigned long mntflags, pflags; |
4d5b72a1 | 2060 | char *mntdata; |
181437fd | 2061 | bool dev, optional, relative; |
ec50007f | 2062 | char *rootfs_path = NULL; |
d8b712bc CB |
2063 | |
2064 | optional = hasmntopt(mntent, "optional") != NULL; | |
2065 | dev = hasmntopt(mntent, "dev") != NULL; | |
181437fd | 2066 | relative = hasmntopt(mntent, "relative") != NULL; |
d8b712bc | 2067 | |
ec50007f CB |
2068 | if (rootfs && rootfs->path) |
2069 | rootfs_path = rootfs->mount; | |
2070 | ||
d8b712bc CB |
2071 | ret = mount_entry_create_dir_file(mntent, path, rootfs, lxc_name, |
2072 | lxc_path); | |
2073 | if (ret < 0) { | |
2074 | if (optional) | |
2075 | return 0; | |
608e3567 | 2076 | |
d8b712bc CB |
2077 | return -1; |
2078 | } | |
4e4ca161 SH |
2079 | cull_mntent_opt(mntent); |
2080 | ||
d840039e YT |
2081 | ret = parse_propagationopts(mntent->mnt_opts, &pflags); |
2082 | if (ret < 0) | |
2083 | return -1; | |
2084 | ||
d8b712bc CB |
2085 | ret = parse_mntopts(mntent->mnt_opts, &mntflags, &mntdata); |
2086 | if (ret < 0) | |
a17b1e65 | 2087 | return -1; |
a17b1e65 | 2088 | |
6e46cc0d | 2089 | ret = mount_entry(mntent->mnt_fsname, path, mntent->mnt_type, mntflags, |
d840039e | 2090 | pflags, mntdata, optional, dev, relative, rootfs_path); |
68c152ef | 2091 | |
911324ef | 2092 | free(mntdata); |
911324ef DL |
2093 | return ret; |
2094 | } | |
2095 | ||
db4aba38 NC |
2096 | static inline int mount_entry_on_systemfs(struct mntent *mntent) |
2097 | { | |
1433c9f9 | 2098 | int ret; |
07667a6a | 2099 | char path[MAXPATHLEN]; |
1433c9f9 CB |
2100 | |
2101 | /* For containers created without a rootfs all mounts are treated as | |
07667a6a CB |
2102 | * absolute paths starting at / on the host. |
2103 | */ | |
1433c9f9 CB |
2104 | if (mntent->mnt_dir[0] != '/') |
2105 | ret = snprintf(path, sizeof(path), "/%s", mntent->mnt_dir); | |
2106 | else | |
2107 | ret = snprintf(path, sizeof(path), "%s", mntent->mnt_dir); | |
07667a6a | 2108 | if (ret < 0 || ret >= sizeof(path)) |
1433c9f9 | 2109 | return -1; |
1433c9f9 CB |
2110 | |
2111 | return mount_entry_on_generic(mntent, path, NULL, NULL, NULL); | |
db4aba38 NC |
2112 | } |
2113 | ||
4e4ca161 | 2114 | static int mount_entry_on_absolute_rootfs(struct mntent *mntent, |
80a881b2 | 2115 | const struct lxc_rootfs *rootfs, |
0a2dddd4 CB |
2116 | const char *lxc_name, |
2117 | const char *lxc_path) | |
911324ef | 2118 | { |
bdd2b34c | 2119 | int offset; |
013bd428 | 2120 | char *aux; |
67e571de | 2121 | const char *lxcpath; |
bdd2b34c CB |
2122 | char path[MAXPATHLEN]; |
2123 | int ret = 0; | |
0ad19a3f | 2124 | |
593e8478 | 2125 | lxcpath = lxc_global_config_value("lxc.lxcpath"); |
bdd2b34c | 2126 | if (!lxcpath) |
2a59a681 | 2127 | return -1; |
2a59a681 | 2128 | |
bdd2b34c CB |
2129 | /* If rootfs->path is a blockdev path, allow container fstab to use |
2130 | * <lxcpath>/<name>/rootfs" as the target prefix. | |
2131 | */ | |
2132 | ret = snprintf(path, MAXPATHLEN, "%s/%s/rootfs", lxcpath, lxc_name); | |
2133 | if (ret < 0 || ret >= MAXPATHLEN) | |
80a881b2 SH |
2134 | goto skipvarlib; |
2135 | ||
2136 | aux = strstr(mntent->mnt_dir, path); | |
2137 | if (aux) { | |
2138 | offset = strlen(path); | |
2139 | goto skipabs; | |
2140 | } | |
2141 | ||
2142 | skipvarlib: | |
013bd428 DL |
2143 | aux = strstr(mntent->mnt_dir, rootfs->path); |
2144 | if (!aux) { | |
bdd2b34c | 2145 | WARN("Ignoring mount point \"%s\"", mntent->mnt_dir); |
db4aba38 | 2146 | return ret; |
013bd428 | 2147 | } |
80a881b2 SH |
2148 | offset = strlen(rootfs->path); |
2149 | ||
2150 | skipabs: | |
bdd2b34c CB |
2151 | ret = snprintf(path, MAXPATHLEN, "%s/%s", rootfs->mount, aux + offset); |
2152 | if (ret < 0 || ret >= MAXPATHLEN) | |
a17b1e65 | 2153 | return -1; |
a17b1e65 | 2154 | |
0a2dddd4 | 2155 | return mount_entry_on_generic(mntent, path, rootfs, lxc_name, lxc_path); |
911324ef | 2156 | } |
d330fe7b | 2157 | |
4e4ca161 | 2158 | static int mount_entry_on_relative_rootfs(struct mntent *mntent, |
0a2dddd4 CB |
2159 | const struct lxc_rootfs *rootfs, |
2160 | const char *lxc_name, | |
2161 | const char *lxc_path) | |
911324ef DL |
2162 | { |
2163 | char path[MAXPATHLEN]; | |
911324ef | 2164 | int ret; |
d330fe7b | 2165 | |
34cfffb3 | 2166 | /* relative to root mount point */ |
6e46cc0d | 2167 | ret = snprintf(path, sizeof(path), "%s/%s", rootfs->mount, mntent->mnt_dir); |
1433c9f9 | 2168 | if (ret < 0 || ret >= sizeof(path)) { |
9ba8130c SH |
2169 | ERROR("path name too long"); |
2170 | return -1; | |
2171 | } | |
911324ef | 2172 | |
0a2dddd4 | 2173 | return mount_entry_on_generic(mntent, path, rootfs, lxc_name, lxc_path); |
911324ef DL |
2174 | } |
2175 | ||
06749971 CB |
2176 | /* This logs a NOTICE() when a user specifies mounts that would conflict with |
2177 | * devices liblxc sets up automatically. | |
2178 | */ | |
2179 | static void log_notice_on_conflict(const struct lxc_conf *conf, const char *src, | |
2180 | const char *dest) | |
2181 | { | |
2182 | char *clean_mnt_fsname, *clean_mnt_dir, *tmp; | |
2183 | bool needs_warning = false; | |
2184 | ||
2185 | clean_mnt_fsname = lxc_deslashify(src); | |
2186 | if (!clean_mnt_fsname) | |
2187 | return; | |
2188 | ||
2189 | clean_mnt_dir = lxc_deslashify(dest); | |
2190 | if (!clean_mnt_dir) { | |
2191 | free(clean_mnt_fsname); | |
2192 | return; | |
2193 | } | |
2194 | ||
2195 | tmp = clean_mnt_dir; | |
2196 | if (*tmp == '/') | |
2197 | tmp++; | |
2198 | ||
2199 | if (strncmp(src, "/dev", 4) || strncmp(tmp, "dev", 3)) { | |
2200 | free(clean_mnt_dir); | |
2201 | free(clean_mnt_fsname); | |
2202 | return; | |
2203 | } | |
2204 | ||
2205 | if (!conf->autodev && !conf->pts && !conf->tty && | |
2206 | (!conf->console.path || !strcmp(conf->console.path, "none"))) { | |
2207 | free(clean_mnt_dir); | |
2208 | free(clean_mnt_fsname); | |
2209 | return; | |
2210 | } | |
2211 | ||
2212 | if (!strcmp(tmp, "dev") && conf->autodev > 0) | |
2213 | needs_warning = true; | |
2214 | else if (!strcmp(tmp, "dev/pts") && (conf->autodev > 0 || conf->pts > 0)) | |
2215 | needs_warning = true; | |
2216 | else if (!strcmp(tmp, "dev/ptmx") && (conf->autodev > 0 || conf->pts > 0)) | |
2217 | needs_warning = true; | |
2218 | else if (!strcmp(tmp, "dev/pts/ptmx") && (conf->autodev > 0 || conf->pts > 0)) | |
2219 | needs_warning = true; | |
2220 | else if (!strcmp(tmp, "dev/null") && conf->autodev > 0) | |
2221 | needs_warning = true; | |
2222 | else if (!strcmp(tmp, "dev/zero") && conf->autodev > 0) | |
2223 | needs_warning = true; | |
2224 | else if (!strcmp(tmp, "dev/full") && conf->autodev > 0) | |
2225 | needs_warning = true; | |
2226 | else if (!strcmp(tmp, "dev/urandom") && conf->autodev > 0) | |
2227 | needs_warning = true; | |
2228 | else if (!strcmp(tmp, "dev/random") && conf->autodev > 0) | |
2229 | needs_warning = true; | |
2230 | else if (!strcmp(tmp, "dev/tty") && conf->autodev > 0) | |
2231 | needs_warning = true; | |
2232 | else if (!strncmp(tmp, "dev/tty", 7) && (conf->autodev > 0 || conf->tty > 0)) | |
2233 | needs_warning = true; | |
2234 | ||
2235 | if (needs_warning) | |
2236 | NOTICE("Requesting to mount \"%s\" on \"%s\" while requesting " | |
2237 | "automatic device setup under \"/dev\"", | |
2238 | clean_mnt_fsname, clean_mnt_dir); | |
2239 | ||
2240 | free(clean_mnt_dir); | |
2241 | free(clean_mnt_fsname); | |
2242 | } | |
2243 | ||
2244 | static int mount_file_entries(const struct lxc_conf *conf, | |
2245 | const struct lxc_rootfs *rootfs, FILE *file, | |
1ae3c19f | 2246 | const char *lxc_name, const char *lxc_path) |
911324ef | 2247 | { |
aaf901be AM |
2248 | struct mntent mntent; |
2249 | char buf[4096]; | |
911324ef | 2250 | int ret = -1; |
e76b8764 | 2251 | |
aaf901be | 2252 | while (getmntent_r(file, &mntent, buf, sizeof(buf))) { |
06749971 CB |
2253 | log_notice_on_conflict(conf, mntent.mnt_fsname, mntent.mnt_dir); |
2254 | ||
1ae3c19f CB |
2255 | if (!rootfs->path) |
2256 | ret = mount_entry_on_systemfs(&mntent); | |
2257 | else if (mntent.mnt_dir[0] != '/') | |
2258 | ret = mount_entry_on_relative_rootfs(&mntent, rootfs, | |
2259 | lxc_name, lxc_path); | |
2260 | else | |
2261 | ret = mount_entry_on_absolute_rootfs(&mntent, rootfs, | |
2262 | lxc_name, lxc_path); | |
2263 | if (ret < 0) | |
2264 | return -1; | |
0ad19a3f | 2265 | } |
2266 | ret = 0; | |
cd54d859 | 2267 | |
1ae3c19f | 2268 | INFO("Set up mount entries"); |
e7938e9e MN |
2269 | return ret; |
2270 | } | |
2271 | ||
06749971 CB |
2272 | static int setup_mount(const struct lxc_conf *conf, |
2273 | const struct lxc_rootfs *rootfs, const char *fstab, | |
42dff448 | 2274 | const char *lxc_name, const char *lxc_path) |
e7938e9e | 2275 | { |
42dff448 | 2276 | FILE *f; |
e7938e9e MN |
2277 | int ret; |
2278 | ||
2279 | if (!fstab) | |
2280 | return 0; | |
2281 | ||
42dff448 CB |
2282 | f = setmntent(fstab, "r"); |
2283 | if (!f) { | |
2284 | SYSERROR("Failed to open \"%s\"", fstab); | |
e7938e9e MN |
2285 | return -1; |
2286 | } | |
2287 | ||
06749971 | 2288 | ret = mount_file_entries(conf, rootfs, f, lxc_name, lxc_path); |
42dff448 CB |
2289 | if (ret < 0) |
2290 | ERROR("Failed to set up mount entries"); | |
e7938e9e | 2291 | |
42dff448 | 2292 | endmntent(f); |
0ad19a3f | 2293 | return ret; |
2294 | } | |
2295 | ||
5ef5c9a3 | 2296 | FILE *make_anonymous_mount_file(struct lxc_list *mount) |
e7938e9e | 2297 | { |
5ef5c9a3 | 2298 | int ret; |
e7938e9e | 2299 | char *mount_entry; |
5ef5c9a3 | 2300 | struct lxc_list *iterator; |
6bd04140 | 2301 | FILE *f; |
5ef5c9a3 CB |
2302 | int fd = -1; |
2303 | ||
2304 | fd = memfd_create("lxc_mount_file", MFD_CLOEXEC); | |
2305 | if (fd < 0) { | |
2306 | if (errno != ENOSYS) | |
2307 | return NULL; | |
6bd04140 CB |
2308 | f = tmpfile(); |
2309 | TRACE("Created temporary mount file"); | |
5ef5c9a3 | 2310 | } else { |
6bd04140 CB |
2311 | f = fdopen(fd, "r+"); |
2312 | TRACE("Created anonymous mount file"); | |
5ef5c9a3 | 2313 | } |
e7938e9e | 2314 | |
6bd04140 CB |
2315 | if (!f) { |
2316 | SYSERROR("Could not create mount file"); | |
5ef5c9a3 CB |
2317 | if (fd != -1) |
2318 | close(fd); | |
9fc7f8c0 | 2319 | return NULL; |
e7938e9e MN |
2320 | } |
2321 | ||
2322 | lxc_list_for_each(iterator, mount) { | |
2323 | mount_entry = iterator->elem; | |
6bd04140 | 2324 | ret = fprintf(f, "%s\n", mount_entry); |
5ef5c9a3 | 2325 | if (ret < strlen(mount_entry)) |
6bd04140 | 2326 | WARN("Could not write mount entry to mount file"); |
5ef5c9a3 CB |
2327 | } |
2328 | ||
6bd04140 CB |
2329 | ret = fseek(f, 0, SEEK_SET); |
2330 | if (ret < 0) { | |
2331 | SYSERROR("Failed to seek mount file"); | |
2332 | fclose(f); | |
5ef5c9a3 | 2333 | return NULL; |
e7938e9e MN |
2334 | } |
2335 | ||
6bd04140 | 2336 | return f; |
9fc7f8c0 TA |
2337 | } |
2338 | ||
06749971 CB |
2339 | static int setup_mount_entries(const struct lxc_conf *conf, |
2340 | const struct lxc_rootfs *rootfs, | |
5ef5c9a3 CB |
2341 | struct lxc_list *mount, const char *lxc_name, |
2342 | const char *lxc_path) | |
9fc7f8c0 | 2343 | { |
19b5d755 | 2344 | FILE *f; |
9fc7f8c0 TA |
2345 | int ret; |
2346 | ||
19b5d755 CB |
2347 | f = make_anonymous_mount_file(mount); |
2348 | if (!f) | |
9fc7f8c0 | 2349 | return -1; |
e7938e9e | 2350 | |
06749971 | 2351 | ret = mount_file_entries(conf, rootfs, f, lxc_name, lxc_path); |
e7938e9e | 2352 | |
19b5d755 | 2353 | fclose(f); |
e7938e9e MN |
2354 | return ret; |
2355 | } | |
2356 | ||
bab88e68 CS |
2357 | static int parse_cap(const char *cap) |
2358 | { | |
2359 | char *ptr = NULL; | |
84760c11 | 2360 | size_t i; |
2361 | int capid = -1; | |
bab88e68 | 2362 | |
7035407c DE |
2363 | if (!strcmp(cap, "none")) |
2364 | return -2; | |
2365 | ||
bab88e68 CS |
2366 | for (i = 0; i < sizeof(caps_opt)/sizeof(caps_opt[0]); i++) { |
2367 | ||
2368 | if (strcmp(cap, caps_opt[i].name)) | |
2369 | continue; | |
2370 | ||
2371 | capid = caps_opt[i].value; | |
2372 | break; | |
2373 | } | |
2374 | ||
2375 | if (capid < 0) { | |
2376 | /* try to see if it's numeric, so the user may specify | |
2377 | * capabilities that the running kernel knows about but | |
2378 | * we don't */ | |
2379 | errno = 0; | |
2380 | capid = strtol(cap, &ptr, 10); | |
2381 | if (!ptr || *ptr != '\0' || errno != 0) | |
2382 | /* not a valid number */ | |
2383 | capid = -1; | |
2384 | else if (capid > lxc_caps_last_cap()) | |
2385 | /* we have a number but it's not a valid | |
2386 | * capability */ | |
2387 | capid = -1; | |
2388 | } | |
2389 | ||
2390 | return capid; | |
2391 | } | |
2392 | ||
0769b82a CS |
2393 | int in_caplist(int cap, struct lxc_list *caps) |
2394 | { | |
2395 | struct lxc_list *iterator; | |
2396 | int capid; | |
2397 | ||
2398 | lxc_list_for_each(iterator, caps) { | |
2399 | capid = parse_cap(iterator->elem); | |
2400 | if (capid == cap) | |
2401 | return 1; | |
2402 | } | |
2403 | ||
2404 | return 0; | |
2405 | } | |
2406 | ||
81810dd1 DL |
2407 | static int setup_caps(struct lxc_list *caps) |
2408 | { | |
2409 | struct lxc_list *iterator; | |
2410 | char *drop_entry; | |
bab88e68 | 2411 | int capid; |
81810dd1 DL |
2412 | |
2413 | lxc_list_for_each(iterator, caps) { | |
2414 | ||
2415 | drop_entry = iterator->elem; | |
2416 | ||
bab88e68 | 2417 | capid = parse_cap(drop_entry); |
d55bc1ad | 2418 | |
81810dd1 | 2419 | if (capid < 0) { |
1e11be34 DL |
2420 | ERROR("unknown capability %s", drop_entry); |
2421 | return -1; | |
81810dd1 DL |
2422 | } |
2423 | ||
2424 | DEBUG("drop capability '%s' (%d)", drop_entry, capid); | |
2425 | ||
2426 | if (prctl(PR_CAPBSET_DROP, capid, 0, 0, 0)) { | |
3ec1648d SH |
2427 | SYSERROR("failed to remove %s capability", drop_entry); |
2428 | return -1; | |
2429 | } | |
81810dd1 DL |
2430 | |
2431 | } | |
2432 | ||
1fb86a7c SH |
2433 | DEBUG("capabilities have been setup"); |
2434 | ||
2435 | return 0; | |
2436 | } | |
2437 | ||
2438 | static int dropcaps_except(struct lxc_list *caps) | |
2439 | { | |
2440 | struct lxc_list *iterator; | |
2441 | char *keep_entry; | |
1fb86a7c SH |
2442 | int i, capid; |
2443 | int numcaps = lxc_caps_last_cap() + 1; | |
959aee9c | 2444 | INFO("found %d capabilities", numcaps); |
1fb86a7c | 2445 | |
2caf9a97 SH |
2446 | if (numcaps <= 0 || numcaps > 200) |
2447 | return -1; | |
2448 | ||
1a0e70ac | 2449 | /* caplist[i] is 1 if we keep capability i */ |
1fb86a7c SH |
2450 | int *caplist = alloca(numcaps * sizeof(int)); |
2451 | memset(caplist, 0, numcaps * sizeof(int)); | |
2452 | ||
2453 | lxc_list_for_each(iterator, caps) { | |
2454 | ||
2455 | keep_entry = iterator->elem; | |
2456 | ||
bab88e68 | 2457 | capid = parse_cap(keep_entry); |
1fb86a7c | 2458 | |
7035407c DE |
2459 | if (capid == -2) |
2460 | continue; | |
2461 | ||
1fb86a7c SH |
2462 | if (capid < 0) { |
2463 | ERROR("unknown capability %s", keep_entry); | |
2464 | return -1; | |
2465 | } | |
2466 | ||
8255688a | 2467 | DEBUG("keep capability '%s' (%d)", keep_entry, capid); |
1fb86a7c SH |
2468 | |
2469 | caplist[capid] = 1; | |
2470 | } | |
2471 | for (i=0; i<numcaps; i++) { | |
2472 | if (caplist[i]) | |
2473 | continue; | |
2474 | if (prctl(PR_CAPBSET_DROP, i, 0, 0, 0)) { | |
3ec1648d SH |
2475 | SYSERROR("failed to remove capability %d", i); |
2476 | return -1; | |
2477 | } | |
1fb86a7c SH |
2478 | } |
2479 | ||
2480 | DEBUG("capabilities have been setup"); | |
81810dd1 DL |
2481 | |
2482 | return 0; | |
2483 | } | |
2484 | ||
c6d09e15 WB |
2485 | static int parse_resource(const char *res) { |
2486 | size_t i; | |
2487 | int resid = -1; | |
2488 | ||
2489 | for (i = 0; i < sizeof(limit_opt)/sizeof(limit_opt[0]); ++i) { | |
2490 | if (strcmp(res, limit_opt[i].name) == 0) | |
2491 | return limit_opt[i].value; | |
2492 | } | |
2493 | ||
2494 | /* try to see if it's numeric, so the user may specify | |
2495 | * resources that the running kernel knows about but | |
2496 | * we don't */ | |
2497 | if (lxc_safe_int(res, &resid) == 0) | |
2498 | return resid; | |
2499 | return -1; | |
2500 | } | |
2501 | ||
2502 | int setup_resource_limits(struct lxc_list *limits, pid_t pid) { | |
2503 | struct lxc_list *it; | |
2504 | struct lxc_limit *lim; | |
2505 | int resid; | |
2506 | ||
2507 | lxc_list_for_each(it, limits) { | |
2508 | lim = it->elem; | |
2509 | ||
2510 | resid = parse_resource(lim->resource); | |
2511 | if (resid < 0) { | |
2512 | ERROR("unknown resource %s", lim->resource); | |
2513 | return -1; | |
2514 | } | |
2515 | ||
f48b5fd8 | 2516 | #if HAVE_PRLIMIT || HAVE_PRLIMIT64 |
c6d09e15 WB |
2517 | if (prlimit(pid, resid, &lim->limit, NULL) != 0) { |
2518 | ERROR("failed to set limit %s: %s", lim->resource, strerror(errno)); | |
2519 | return -1; | |
2520 | } | |
f48b5fd8 FF |
2521 | #else |
2522 | ERROR("Cannot set limit %s as prlimit is missing", lim->resource); | |
2523 | return -1; | |
2524 | #endif | |
c6d09e15 WB |
2525 | } |
2526 | return 0; | |
2527 | } | |
2528 | ||
7edd0540 L |
2529 | int setup_sysctl_parameters(struct lxc_list *sysctls) |
2530 | { | |
2531 | struct lxc_list *it; | |
2532 | struct lxc_sysctl *elem; | |
2533 | char *tmp = NULL; | |
2534 | char filename[MAXPATHLEN] = {0}; | |
2535 | int ret = 0; | |
2536 | ||
2537 | lxc_list_for_each(it, sysctls) { | |
2538 | elem = it->elem; | |
2539 | tmp = lxc_string_replace(".", "/", elem->key); | |
2540 | if (!tmp) { | |
2541 | ERROR("Failed to replace key %s", elem->key); | |
2542 | return -1; | |
2543 | } | |
2544 | ||
2545 | ret = snprintf(filename, sizeof(filename), "/proc/sys/%s", tmp); | |
2546 | free(tmp); | |
2547 | if (ret < 0 || (size_t)ret >= sizeof(filename)) { | |
2548 | ERROR("Error setting up sysctl parameters path"); | |
2549 | return -1; | |
2550 | } | |
2551 | ||
2552 | ret = lxc_write_to_file(filename, elem->value, strlen(elem->value), false); | |
2553 | if (ret < 0) { | |
2554 | ERROR("Failed to setup sysctl parameters %s to %s", elem->key, elem->value); | |
2555 | return -1; | |
2556 | } | |
2557 | } | |
2558 | return 0; | |
2559 | } | |
2560 | ||
61d7a733 YT |
2561 | int setup_proc_filesystem(struct lxc_list *procs, pid_t pid) |
2562 | { | |
2563 | struct lxc_list *it; | |
2564 | struct lxc_proc *elem; | |
2565 | char *tmp = NULL; | |
2566 | char filename[MAXPATHLEN] = {0}; | |
2567 | int ret = 0; | |
2568 | ||
2569 | lxc_list_for_each(it, procs) { | |
2570 | elem = it->elem; | |
2571 | tmp = lxc_string_replace(".", "/", elem->filename); | |
2572 | if (!tmp) { | |
2573 | ERROR("Failed to replace key %s", elem->filename); | |
2574 | return -1; | |
2575 | } | |
2576 | ||
2577 | ret = snprintf(filename, sizeof(filename), "/proc/%d/%s", pid, tmp); | |
2578 | free(tmp); | |
2579 | if (ret < 0 || (size_t)ret >= sizeof(filename)) { | |
2580 | ERROR("Error setting up proc filesystem path"); | |
2581 | return -1; | |
2582 | } | |
2583 | ||
2584 | ret = lxc_write_to_file(filename, elem->value, strlen(elem->value), false); | |
2585 | if (ret < 0) { | |
2586 | ERROR("Failed to setup proc filesystem %s to %s", elem->filename, elem->value); | |
2587 | return -1; | |
2588 | } | |
2589 | } | |
2590 | return 0; | |
2591 | } | |
2592 | ||
ae9242c8 SH |
2593 | static char *default_rootfs_mount = LXCROOTFSMOUNT; |
2594 | ||
7b379ab3 | 2595 | struct lxc_conf *lxc_conf_init(void) |
089cd8b8 | 2596 | { |
7b379ab3 | 2597 | struct lxc_conf *new; |
26ddeedd | 2598 | int i; |
7b379ab3 | 2599 | |
13277ec4 | 2600 | new = malloc(sizeof(*new)); |
7b379ab3 | 2601 | if (!new) { |
13277ec4 | 2602 | ERROR("lxc_conf_init : %s", strerror(errno)); |
7b379ab3 MN |
2603 | return NULL; |
2604 | } | |
2605 | memset(new, 0, sizeof(*new)); | |
2606 | ||
4b73005c | 2607 | new->loglevel = LXC_LOG_LEVEL_NOTSET; |
cccc74b5 | 2608 | new->personality = -1; |
124fa0a8 | 2609 | new->autodev = 1; |
3a784510 | 2610 | new->console.buffer_size = 0; |
596a818d DE |
2611 | new->console.log_path = NULL; |
2612 | new->console.log_fd = -1; | |
861813e5 | 2613 | new->console.log_size = 0; |
28a4b0e5 | 2614 | new->console.path = NULL; |
63376d7d | 2615 | new->console.peer = -1; |
b5159817 DE |
2616 | new->console.peerpty.busy = -1; |
2617 | new->console.peerpty.master = -1; | |
2618 | new->console.peerpty.slave = -1; | |
63376d7d DL |
2619 | new->console.master = -1; |
2620 | new->console.slave = -1; | |
2621 | new->console.name[0] = '\0'; | |
732375f5 | 2622 | memset(&new->console.ringbuf, 0, sizeof(struct lxc_ringbuf)); |
d2e30e99 | 2623 | new->maincmd_fd = -1; |
76a26f55 | 2624 | new->nbd_idx = -1; |
54c30e29 | 2625 | new->rootfs.mount = strdup(default_rootfs_mount); |
53f3f048 | 2626 | if (!new->rootfs.mount) { |
13277ec4 | 2627 | ERROR("lxc_conf_init : %s", strerror(errno)); |
53f3f048 SH |
2628 | free(new); |
2629 | return NULL; | |
2630 | } | |
858377e4 | 2631 | new->logfd = -1; |
7b379ab3 | 2632 | lxc_list_init(&new->cgroup); |
54860ed0 | 2633 | lxc_list_init(&new->cgroup2); |
7b379ab3 MN |
2634 | lxc_list_init(&new->network); |
2635 | lxc_list_init(&new->mount_list); | |
81810dd1 | 2636 | lxc_list_init(&new->caps); |
1fb86a7c | 2637 | lxc_list_init(&new->keepcaps); |
f6d3e3e4 | 2638 | lxc_list_init(&new->id_map); |
46ad64ab CB |
2639 | new->root_nsuid_map = NULL; |
2640 | new->root_nsgid_map = NULL; | |
f979ac15 | 2641 | lxc_list_init(&new->includes); |
4184c3e1 | 2642 | lxc_list_init(&new->aliens); |
7c661726 | 2643 | lxc_list_init(&new->environment); |
c6d09e15 | 2644 | lxc_list_init(&new->limits); |
7edd0540 | 2645 | lxc_list_init(&new->sysctls); |
61d7a733 | 2646 | lxc_list_init(&new->procs); |
44ae0fb6 | 2647 | new->hooks_version = 0; |
28d9e29e | 2648 | for (i = 0; i < NUM_LXC_HOOKS; i++) |
26ddeedd | 2649 | lxc_list_init(&new->hooks[i]); |
ee1e7aa0 | 2650 | lxc_list_init(&new->groups); |
d39b10eb | 2651 | lxc_list_init(&new->state_clients); |
fe4de9a6 DE |
2652 | new->lsm_aa_profile = NULL; |
2653 | new->lsm_se_context = NULL; | |
5112cd70 | 2654 | new->tmp_umount_proc = 0; |
7b379ab3 | 2655 | |
72bb04e4 PT |
2656 | /* if running in a new user namespace, init and COMMAND |
2657 | * default to running as UID/GID 0 when using lxc-execute */ | |
2658 | new->init_uid = 0; | |
2659 | new->init_gid = 0; | |
43654d34 | 2660 | memset(&new->cgroup_meta, 0, sizeof(struct lxc_cgroup)); |
b074bbf1 | 2661 | memset(&new->ns_share, 0, sizeof(char *) * LXC_NS_MAX); |
72bb04e4 | 2662 | |
7b379ab3 | 2663 | return new; |
089cd8b8 DL |
2664 | } |
2665 | ||
344c9d81 | 2666 | int write_id_mapping(enum idtype idtype, pid_t pid, const char *buf, |
a19b974f | 2667 | size_t buf_size) |
f6d3e3e4 | 2668 | { |
29053180 CB |
2669 | char path[MAXPATHLEN]; |
2670 | int fd, ret; | |
f6d3e3e4 | 2671 | |
a19b974f CB |
2672 | if (geteuid() != 0 && idtype == ID_TYPE_GID) { |
2673 | size_t buflen; | |
2674 | ||
2675 | ret = snprintf(path, MAXPATHLEN, "/proc/%d/setgroups", pid); | |
2676 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2677 | ERROR("Failed to create string"); | |
2678 | return -E2BIG; | |
2679 | } | |
2680 | ||
2681 | fd = open(path, O_WRONLY); | |
2682 | if (fd < 0 && errno != ENOENT) { | |
2683 | SYSERROR("Failed to open \"%s\"", path); | |
2684 | return -1; | |
2685 | } | |
2686 | ||
2388737b CB |
2687 | if (fd >= 0) { |
2688 | buflen = sizeof("deny\n") - 1; | |
2689 | errno = 0; | |
2690 | ret = lxc_write_nointr(fd, "deny\n", buflen); | |
2691 | if (ret != buflen) { | |
2692 | SYSERROR("Failed to write \"deny\" to \"/proc/%d/setgroups\"", pid); | |
2693 | close(fd); | |
2694 | return -1; | |
2695 | } | |
a19b974f | 2696 | close(fd); |
a19b974f | 2697 | } |
a19b974f CB |
2698 | } |
2699 | ||
29053180 CB |
2700 | ret = snprintf(path, MAXPATHLEN, "/proc/%d/%cid_map", pid, |
2701 | idtype == ID_TYPE_UID ? 'u' : 'g'); | |
2702 | if (ret < 0 || ret >= MAXPATHLEN) { | |
a19b974f | 2703 | ERROR("Failed to create string"); |
f6d3e3e4 SH |
2704 | return -E2BIG; |
2705 | } | |
29053180 CB |
2706 | |
2707 | fd = open(path, O_WRONLY); | |
2708 | if (fd < 0) { | |
a19b974f | 2709 | SYSERROR("Failed to open \"%s\"", path); |
29053180 | 2710 | return -1; |
f6d3e3e4 | 2711 | } |
29053180 CB |
2712 | |
2713 | errno = 0; | |
2714 | ret = lxc_write_nointr(fd, buf, buf_size); | |
2715 | if (ret != buf_size) { | |
a19b974f | 2716 | SYSERROR("Failed to write %cid mapping to \"%s\"", |
29053180 CB |
2717 | idtype == ID_TYPE_UID ? 'u' : 'g', path); |
2718 | close(fd); | |
2719 | return -1; | |
2720 | } | |
2721 | close(fd); | |
2722 | ||
2723 | return 0; | |
f6d3e3e4 SH |
2724 | } |
2725 | ||
6e50e704 CB |
2726 | /* Check whether a binary exist and has either CAP_SETUID, CAP_SETGID or both. |
2727 | * | |
2728 | * @return 1 if functional binary was found | |
2729 | * @return 0 if binary exists but is lacking privilege | |
2730 | * @return -ENOENT if binary does not exist | |
2731 | * @return -EINVAL if cap to check is neither CAP_SETUID nor CAP_SETGID | |
2732 | * | |
2733 | */ | |
df6a2945 CB |
2734 | static int idmaptool_on_path_and_privileged(const char *binary, cap_value_t cap) |
2735 | { | |
2736 | char *path; | |
2737 | int ret; | |
2738 | struct stat st; | |
2739 | int fret = 0; | |
2740 | ||
6e50e704 CB |
2741 | if (cap != CAP_SETUID && cap != CAP_SETGID) |
2742 | return -EINVAL; | |
2743 | ||
df6a2945 CB |
2744 | path = on_path(binary, NULL); |
2745 | if (!path) | |
2746 | return -ENOENT; | |
2747 | ||
2748 | ret = stat(path, &st); | |
2749 | if (ret < 0) { | |
2750 | fret = -errno; | |
2751 | goto cleanup; | |
2752 | } | |
2753 | ||
2754 | /* Check if the binary is setuid. */ | |
2755 | if (st.st_mode & S_ISUID) { | |
2756 | DEBUG("The binary \"%s\" does have the setuid bit set.", path); | |
2757 | fret = 1; | |
2758 | goto cleanup; | |
2759 | } | |
2760 | ||
69924fff | 2761 | #if HAVE_LIBCAP && LIBCAP_SUPPORTS_FILE_CAPABILITIES |
df6a2945 CB |
2762 | /* Check if it has the CAP_SETUID capability. */ |
2763 | if ((cap & CAP_SETUID) && | |
2764 | lxc_file_cap_is_set(path, CAP_SETUID, CAP_EFFECTIVE) && | |
2765 | lxc_file_cap_is_set(path, CAP_SETUID, CAP_PERMITTED)) { | |
2766 | DEBUG("The binary \"%s\" has CAP_SETUID in its CAP_EFFECTIVE " | |
2767 | "and CAP_PERMITTED sets.", path); | |
2768 | fret = 1; | |
2769 | goto cleanup; | |
2770 | } | |
2771 | ||
2772 | /* Check if it has the CAP_SETGID capability. */ | |
2773 | if ((cap & CAP_SETGID) && | |
2774 | lxc_file_cap_is_set(path, CAP_SETGID, CAP_EFFECTIVE) && | |
2775 | lxc_file_cap_is_set(path, CAP_SETGID, CAP_PERMITTED)) { | |
2776 | DEBUG("The binary \"%s\" has CAP_SETGID in its CAP_EFFECTIVE " | |
2777 | "and CAP_PERMITTED sets.", path); | |
2778 | fret = 1; | |
2779 | goto cleanup; | |
2780 | } | |
d6018f88 | 2781 | #else |
69924fff CB |
2782 | /* If we cannot check for file capabilities we need to give the benefit |
2783 | * of the doubt. Otherwise we might fail even though all the necessary | |
2784 | * file capabilities are set. | |
2785 | */ | |
d6018f88 CB |
2786 | DEBUG("Cannot check for file capabilites as full capability support is " |
2787 | "missing. Manual intervention needed."); | |
2788 | fret = 1; | |
df6a2945 CB |
2789 | #endif |
2790 | ||
2791 | cleanup: | |
2792 | free(path); | |
2793 | return fret; | |
2794 | } | |
2795 | ||
986ef930 CB |
2796 | int lxc_map_ids_exec_wrapper(void *args) |
2797 | { | |
2798 | execl("/bin/sh", "sh", "-c", (char *)args, (char *)NULL); | |
2799 | return -1; | |
2800 | } | |
2801 | ||
f6d3e3e4 SH |
2802 | int lxc_map_ids(struct lxc_list *idmap, pid_t pid) |
2803 | { | |
f6d3e3e4 | 2804 | struct id_map *map; |
4bc3b759 | 2805 | struct lxc_list *iterator; |
251d0d2a | 2806 | enum idtype type; |
986ef930 | 2807 | char u_or_g; |
4bc3b759 | 2808 | char *pos; |
99d43365 | 2809 | int fill, left; |
986ef930 CB |
2810 | char cmd_output[MAXPATHLEN]; |
2811 | /* strlen("new@idmap") = 9 | |
2812 | * + | |
2813 | * strlen(" ") = 1 | |
2814 | * + | |
2815 | * LXC_NUMSTRLEN64 | |
2816 | * + | |
2817 | * strlen(" ") = 1 | |
2818 | * | |
2819 | * We add some additional space to make sure that we really have | |
2820 | * LXC_IDMAPLEN bytes available for our the {g,u]id mapping. | |
2821 | */ | |
2822 | char mapbuf[9 + 1 + LXC_NUMSTRLEN64 + 1 + LXC_IDMAPLEN] = {0}; | |
2823 | int ret = 0, uidmap = 0, gidmap = 0; | |
2824 | bool use_shadow = false, had_entry = false; | |
df6a2945 CB |
2825 | |
2826 | /* If new{g,u}idmap exists, that is, if shadow is handing out subuid | |
2827 | * ranges, then insist that root also reserve ranges in subuid. This | |
22038de5 SH |
2828 | * will protected it by preventing another user from being handed the |
2829 | * range by shadow. | |
2830 | */ | |
df6a2945 | 2831 | uidmap = idmaptool_on_path_and_privileged("newuidmap", CAP_SETUID); |
6e50e704 CB |
2832 | if (uidmap == -ENOENT) |
2833 | WARN("newuidmap binary is missing"); | |
2834 | else if (!uidmap) | |
2835 | WARN("newuidmap is lacking necessary privileges"); | |
2836 | ||
df6a2945 | 2837 | gidmap = idmaptool_on_path_and_privileged("newgidmap", CAP_SETGID); |
6e50e704 CB |
2838 | if (gidmap == -ENOENT) |
2839 | WARN("newgidmap binary is missing"); | |
2840 | else if (!gidmap) | |
2841 | WARN("newgidmap is lacking necessary privileges"); | |
2842 | ||
df6a2945 CB |
2843 | if (uidmap > 0 && gidmap > 0) { |
2844 | DEBUG("Functional newuidmap and newgidmap binary found."); | |
4bc3b759 | 2845 | use_shadow = true; |
df6a2945 | 2846 | } else { |
99d43365 CB |
2847 | /* In case unprivileged users run application containers via |
2848 | * execute() or a start*() there are valid cases where they may | |
2849 | * only want to map their own {g,u}id. Let's not block them from | |
2850 | * doing so by requiring geteuid() == 0. | |
2851 | */ | |
2852 | DEBUG("No newuidmap and newgidmap binary found. Trying to " | |
2853 | "write directly with euid %d.", geteuid()); | |
0e6e3a41 | 2854 | } |
251d0d2a | 2855 | |
986ef930 CB |
2856 | for (type = ID_TYPE_UID, u_or_g = 'u'; type <= ID_TYPE_GID; |
2857 | type++, u_or_g = 'g') { | |
2858 | pos = mapbuf; | |
2859 | ||
0e6e3a41 | 2860 | if (use_shadow) |
986ef930 | 2861 | pos += sprintf(mapbuf, "new%cidmap %d", u_or_g, pid); |
4f7521b4 | 2862 | |
cf3ef16d | 2863 | lxc_list_for_each(iterator, idmap) { |
251d0d2a | 2864 | map = iterator->elem; |
cf3ef16d SH |
2865 | if (map->idtype != type) |
2866 | continue; | |
2867 | ||
4bc3b759 CB |
2868 | had_entry = true; |
2869 | ||
986ef930 | 2870 | left = LXC_IDMAPLEN - (pos - mapbuf); |
d1838f34 | 2871 | fill = snprintf(pos, left, "%s%lu %lu %lu%s", |
4bc3b759 CB |
2872 | use_shadow ? " " : "", map->nsid, |
2873 | map->hostid, map->range, | |
0e6e3a41 | 2874 | use_shadow ? "" : "\n"); |
a427e268 CB |
2875 | if (fill <= 0 || fill >= left) { |
2876 | /* The kernel only takes <= 4k for writes to | |
2877 | * /proc/<pid>/{g,u}id_map | |
2878 | */ | |
2879 | SYSERROR("Too many %cid mappings defined", u_or_g); | |
2880 | return -1; | |
2881 | } | |
4bc3b759 | 2882 | |
cf3ef16d | 2883 | pos += fill; |
251d0d2a | 2884 | } |
cf3ef16d | 2885 | if (!had_entry) |
4f7521b4 | 2886 | continue; |
cf3ef16d | 2887 | |
986ef930 CB |
2888 | /* Try to catch the ouput of new{g,u}idmap to make debugging |
2889 | * easier. | |
2890 | */ | |
2891 | if (use_shadow) { | |
2892 | ret = run_command(cmd_output, sizeof(cmd_output), | |
2893 | lxc_map_ids_exec_wrapper, | |
2894 | (void *)mapbuf); | |
2895 | if (ret < 0) { | |
54fbbeb5 CB |
2896 | ERROR("new%cidmap failed to write mapping \"%s\": %s", |
2897 | u_or_g, cmd_output, mapbuf); | |
986ef930 CB |
2898 | return -1; |
2899 | } | |
54fbbeb5 | 2900 | TRACE("new%cidmap wrote mapping \"%s\"", u_or_g, mapbuf); |
d1838f34 | 2901 | } else { |
986ef930 | 2902 | ret = write_id_mapping(type, pid, mapbuf, pos - mapbuf); |
54fbbeb5 | 2903 | if (ret < 0) { |
da0f9977 | 2904 | ERROR("Failed to write mapping: %s", mapbuf); |
986ef930 | 2905 | return -1; |
54fbbeb5 CB |
2906 | } |
2907 | TRACE("Wrote mapping \"%s\"", mapbuf); | |
d1838f34 | 2908 | } |
986ef930 CB |
2909 | |
2910 | memset(mapbuf, 0, sizeof(mapbuf)); | |
f6d3e3e4 | 2911 | } |
251d0d2a | 2912 | |
986ef930 | 2913 | return 0; |
f6d3e3e4 SH |
2914 | } |
2915 | ||
cf3ef16d | 2916 | /* |
7b50c609 TS |
2917 | * return the host uid/gid to which the container root is mapped in |
2918 | * *val. | |
0b3a6504 | 2919 | * Return true if id was found, false otherwise. |
cf3ef16d | 2920 | */ |
2a9a80cb | 2921 | bool get_mapped_rootid(struct lxc_conf *conf, enum idtype idtype, |
4160c3a0 | 2922 | unsigned long *val) |
cf3ef16d SH |
2923 | { |
2924 | struct lxc_list *it; | |
2925 | struct id_map *map; | |
4160c3a0 CB |
2926 | unsigned nsid; |
2927 | ||
2928 | if (idtype == ID_TYPE_UID) | |
2929 | nsid = (conf->root_nsuid_map != NULL) ? 0 : conf->init_uid; | |
2930 | else | |
2931 | nsid = (conf->root_nsgid_map != NULL) ? 0 : conf->init_gid; | |
cf3ef16d SH |
2932 | |
2933 | lxc_list_for_each(it, &conf->id_map) { | |
2934 | map = it->elem; | |
7b50c609 | 2935 | if (map->idtype != idtype) |
cf3ef16d | 2936 | continue; |
4160c3a0 | 2937 | if (map->nsid != nsid) |
cf3ef16d | 2938 | continue; |
2a9a80cb SH |
2939 | *val = map->hostid; |
2940 | return true; | |
cf3ef16d | 2941 | } |
4160c3a0 | 2942 | |
2a9a80cb | 2943 | return false; |
cf3ef16d SH |
2944 | } |
2945 | ||
2133f58c | 2946 | int mapped_hostid(unsigned id, struct lxc_conf *conf, enum idtype idtype) |
cf3ef16d SH |
2947 | { |
2948 | struct lxc_list *it; | |
2949 | struct id_map *map; | |
2950 | lxc_list_for_each(it, &conf->id_map) { | |
2951 | map = it->elem; | |
2133f58c | 2952 | if (map->idtype != idtype) |
cf3ef16d SH |
2953 | continue; |
2954 | if (id >= map->hostid && id < map->hostid + map->range) | |
57d116ab | 2955 | return (id - map->hostid) + map->nsid; |
cf3ef16d | 2956 | } |
57d116ab | 2957 | return -1; |
cf3ef16d SH |
2958 | } |
2959 | ||
339efad9 | 2960 | int find_unmapped_nsid(struct lxc_conf *conf, enum idtype idtype) |
cf3ef16d SH |
2961 | { |
2962 | struct lxc_list *it; | |
2963 | struct id_map *map; | |
2133f58c | 2964 | unsigned int freeid = 0; |
cf3ef16d SH |
2965 | again: |
2966 | lxc_list_for_each(it, &conf->id_map) { | |
2967 | map = it->elem; | |
2133f58c | 2968 | if (map->idtype != idtype) |
cf3ef16d SH |
2969 | continue; |
2970 | if (freeid >= map->nsid && freeid < map->nsid + map->range) { | |
2971 | freeid = map->nsid + map->range; | |
2972 | goto again; | |
2973 | } | |
2974 | } | |
2975 | return freeid; | |
2976 | } | |
2977 | ||
f4f52cb5 CB |
2978 | int chown_mapped_root_exec_wrapper(void *args) |
2979 | { | |
2980 | execvp("lxc-usernsexec", args); | |
2981 | return -1; | |
2982 | } | |
2983 | ||
f6d3e3e4 | 2984 | /* |
7b50c609 TS |
2985 | * chown_mapped_root: for an unprivileged user with uid/gid X to |
2986 | * chown a dir to subuid/subgid Y, he needs to run chown as root | |
2987 | * in a userns where nsid 0 is mapped to hostuid/hostgid Y, and | |
2988 | * nsid Y is mapped to hostuid/hostgid X. That way, the container | |
2989 | * root is privileged with respect to hostuid/hostgid X, allowing | |
2990 | * him to do the chown. | |
f6d3e3e4 | 2991 | */ |
41dc7155 | 2992 | int chown_mapped_root(const char *path, struct lxc_conf *conf) |
f6d3e3e4 | 2993 | { |
f4f52cb5 | 2994 | uid_t rootuid, rootgid; |
2a9a80cb | 2995 | unsigned long val; |
f4f52cb5 CB |
2996 | int hostuid, hostgid, ret; |
2997 | struct stat sb; | |
2998 | char map1[100], map2[100], map3[100], map4[100], map5[100]; | |
2999 | char ugid[100]; | |
41dc7155 | 3000 | const char *args1[] = {"lxc-usernsexec", |
f4f52cb5 CB |
3001 | "-m", map1, |
3002 | "-m", map2, | |
3003 | "-m", map3, | |
3004 | "-m", map5, | |
3005 | "--", "chown", ugid, path, | |
3006 | NULL}; | |
41dc7155 | 3007 | const char *args2[] = {"lxc-usernsexec", |
f4f52cb5 CB |
3008 | "-m", map1, |
3009 | "-m", map2, | |
3010 | "-m", map3, | |
3011 | "-m", map4, | |
3012 | "-m", map5, | |
3013 | "--", "chown", ugid, path, | |
3014 | NULL}; | |
3015 | char cmd_output[MAXPATHLEN]; | |
3016 | ||
3017 | hostuid = geteuid(); | |
3018 | hostgid = getegid(); | |
f6d3e3e4 | 3019 | |
2a9a80cb | 3020 | if (!get_mapped_rootid(conf, ID_TYPE_UID, &val)) { |
bc80f098 | 3021 | ERROR("No uid mapping for container root"); |
c4d10a05 | 3022 | return -1; |
f6d3e3e4 | 3023 | } |
f4f52cb5 | 3024 | rootuid = (uid_t)val; |
7b50c609 | 3025 | if (!get_mapped_rootid(conf, ID_TYPE_GID, &val)) { |
bc80f098 | 3026 | ERROR("No gid mapping for container root"); |
7b50c609 TS |
3027 | return -1; |
3028 | } | |
f4f52cb5 | 3029 | rootgid = (gid_t)val; |
2a9a80cb | 3030 | |
f4f52cb5 | 3031 | if (hostuid == 0) { |
7b50c609 | 3032 | if (chown(path, rootuid, rootgid) < 0) { |
c4d10a05 SH |
3033 | ERROR("Error chowning %s", path); |
3034 | return -1; | |
3035 | } | |
3036 | return 0; | |
3037 | } | |
f3d7e4ca | 3038 | |
f4f52cb5 | 3039 | if (rootuid == hostuid) { |
1a0e70ac | 3040 | /* nothing to do */ |
b103ceac | 3041 | INFO("Container root is our uid; no need to chown"); |
f3d7e4ca SH |
3042 | return 0; |
3043 | } | |
3044 | ||
bbdbf8f0 | 3045 | /* save the current gid of "path" */ |
f4f52cb5 CB |
3046 | if (stat(path, &sb) < 0) { |
3047 | ERROR("Error stat %s", path); | |
f6d3e3e4 SH |
3048 | return -1; |
3049 | } | |
7b50c609 | 3050 | |
bbdbf8f0 CB |
3051 | /* Update the path argument in case this was overlayfs. */ |
3052 | args1[sizeof(args1) / sizeof(args1[0]) - 2] = path; | |
3053 | args2[sizeof(args2) / sizeof(args2[0]) - 2] = path; | |
3054 | ||
f4f52cb5 CB |
3055 | /* |
3056 | * A file has to be group-owned by a gid mapped into the | |
3057 | * container, or the container won't be privileged over it. | |
3058 | */ | |
3059 | DEBUG("trying to chown \"%s\" to %d", path, hostgid); | |
3060 | if (sb.st_uid == hostuid && | |
3061 | mapped_hostid(sb.st_gid, conf, ID_TYPE_GID) < 0 && | |
3062 | chown(path, -1, hostgid) < 0) { | |
3063 | ERROR("Failed chgrping %s", path); | |
3064 | return -1; | |
3065 | } | |
f6d3e3e4 | 3066 | |
1a0e70ac | 3067 | /* "u:0:rootuid:1" */ |
f4f52cb5 CB |
3068 | ret = snprintf(map1, 100, "u:0:%d:1", rootuid); |
3069 | if (ret < 0 || ret >= 100) { | |
3070 | ERROR("Error uid printing map string"); | |
3071 | return -1; | |
3072 | } | |
7b50c609 | 3073 | |
1a0e70ac | 3074 | /* "u:hostuid:hostuid:1" */ |
f4f52cb5 CB |
3075 | ret = snprintf(map2, 100, "u:%d:%d:1", hostuid, hostuid); |
3076 | if (ret < 0 || ret >= 100) { | |
3077 | ERROR("Error uid printing map string"); | |
3078 | return -1; | |
3079 | } | |
c4d10a05 | 3080 | |
1a0e70ac | 3081 | /* "g:0:rootgid:1" */ |
f4f52cb5 CB |
3082 | ret = snprintf(map3, 100, "g:0:%d:1", rootgid); |
3083 | if (ret < 0 || ret >= 100) { | |
3084 | ERROR("Error gid printing map string"); | |
3085 | return -1; | |
3086 | } | |
98e5ba51 | 3087 | |
1a0e70ac | 3088 | /* "g:pathgid:rootgid+pathgid:1" */ |
f4f52cb5 CB |
3089 | ret = snprintf(map4, 100, "g:%d:%d:1", (gid_t)sb.st_gid, |
3090 | rootgid + (gid_t)sb.st_gid); | |
3091 | if (ret < 0 || ret >= 100) { | |
3092 | ERROR("Error gid printing map string"); | |
3093 | return -1; | |
3094 | } | |
c4d10a05 | 3095 | |
1a0e70ac | 3096 | /* "g:hostgid:hostgid:1" */ |
f4f52cb5 CB |
3097 | ret = snprintf(map5, 100, "g:%d:%d:1", hostgid, hostgid); |
3098 | if (ret < 0 || ret >= 100) { | |
3099 | ERROR("Error gid printing map string"); | |
3100 | return -1; | |
3101 | } | |
7b50c609 | 3102 | |
1a0e70ac | 3103 | /* "0:pathgid" (chown) */ |
f4f52cb5 CB |
3104 | ret = snprintf(ugid, 100, "0:%d", (gid_t)sb.st_gid); |
3105 | if (ret < 0 || ret >= 100) { | |
3106 | ERROR("Error owner printing format string for chown"); | |
3107 | return -1; | |
3108 | } | |
7b50c609 | 3109 | |
f4f52cb5 CB |
3110 | if (hostgid == sb.st_gid) |
3111 | ret = run_command(cmd_output, sizeof(cmd_output), | |
3112 | chown_mapped_root_exec_wrapper, | |
3113 | (void *)args1); | |
3114 | else | |
3115 | ret = run_command(cmd_output, sizeof(cmd_output), | |
3116 | chown_mapped_root_exec_wrapper, | |
3117 | (void *)args2); | |
3118 | if (ret < 0) | |
3119 | ERROR("lxc-usernsexec failed: %s", cmd_output); | |
7b50c609 | 3120 | |
f4f52cb5 | 3121 | return ret; |
f6d3e3e4 SH |
3122 | } |
3123 | ||
943144d9 CB |
3124 | /* NOTE: Must not be called from inside the container namespace! */ |
3125 | int lxc_create_tmp_proc_mount(struct lxc_conf *conf) | |
5112cd70 SH |
3126 | { |
3127 | int mounted; | |
3128 | ||
943144d9 | 3129 | mounted = lxc_mount_proc_if_needed(conf->rootfs.path ? conf->rootfs.mount : ""); |
5112cd70 | 3130 | if (mounted == -1) { |
943144d9 | 3131 | SYSERROR("failed to mount /proc in the container"); |
01958b1f | 3132 | /* continue only if there is no rootfs */ |
943144d9 | 3133 | if (conf->rootfs.path) |
01958b1f | 3134 | return -1; |
5112cd70 | 3135 | } else if (mounted == 1) { |
943144d9 | 3136 | conf->tmp_umount_proc = 1; |
5112cd70 | 3137 | } |
943144d9 | 3138 | |
5112cd70 SH |
3139 | return 0; |
3140 | } | |
3141 | ||
3142 | void tmp_proc_unmount(struct lxc_conf *lxc_conf) | |
3143 | { | |
3144 | if (lxc_conf->tmp_umount_proc == 1) { | |
3145 | umount("/proc"); | |
3146 | lxc_conf->tmp_umount_proc = 0; | |
3147 | } | |
3148 | } | |
3149 | ||
6a0c909a | 3150 | void remount_all_slave(void) |
e995d7a2 SH |
3151 | { |
3152 | /* walk /proc/mounts and change any shared entries to slave */ | |
3153 | FILE *f = fopen("/proc/self/mountinfo", "r"); | |
3154 | char *line = NULL; | |
3155 | size_t len = 0; | |
3156 | ||
3157 | if (!f) { | |
3158 | SYSERROR("Failed to open /proc/self/mountinfo to mark all shared"); | |
3159 | ERROR("Continuing container startup..."); | |
3160 | return; | |
3161 | } | |
3162 | ||
3163 | while (getline(&line, &len, f) != -1) { | |
3164 | char *target, *opts; | |
3165 | target = get_field(line, 4); | |
3166 | if (!target) | |
3167 | continue; | |
3168 | opts = get_field(target, 2); | |
3169 | if (!opts) | |
3170 | continue; | |
3171 | null_endofword(opts); | |
3172 | if (!strstr(opts, "shared")) | |
3173 | continue; | |
3174 | null_endofword(target); | |
3175 | if (mount(NULL, target, NULL, MS_SLAVE, NULL)) { | |
3176 | SYSERROR("Failed to make %s rslave", target); | |
3177 | ERROR("Continuing..."); | |
3178 | } | |
3179 | } | |
3180 | fclose(f); | |
f10fad2f | 3181 | free(line); |
e995d7a2 SH |
3182 | } |
3183 | ||
8353b4c9 | 3184 | static int lxc_execute_bind_init(struct lxc_conf *conf) |
2322903b SH |
3185 | { |
3186 | int ret; | |
9d9c111c SH |
3187 | char path[PATH_MAX], destpath[PATH_MAX], *p; |
3188 | ||
3189 | /* If init exists in the container, don't bind mount a static one */ | |
3190 | p = choose_init(conf->rootfs.mount); | |
3191 | if (p) { | |
3192 | free(p); | |
8353b4c9 | 3193 | return 0; |
9d9c111c | 3194 | } |
2322903b SH |
3195 | |
3196 | ret = snprintf(path, PATH_MAX, SBINDIR "/init.lxc.static"); | |
3197 | if (ret < 0 || ret >= PATH_MAX) { | |
8353b4c9 CB |
3198 | ERROR("Path name too long searching for lxc.init.static"); |
3199 | return -1; | |
2322903b SH |
3200 | } |
3201 | ||
3202 | if (!file_exists(path)) { | |
8353b4c9 CB |
3203 | ERROR("%s does not exist on host", path); |
3204 | return -1; | |
2322903b SH |
3205 | } |
3206 | ||
3207 | ret = snprintf(destpath, PATH_MAX, "%s%s", conf->rootfs.mount, "/init.lxc.static"); | |
3208 | if (ret < 0 || ret >= PATH_MAX) { | |
8353b4c9 CB |
3209 | ERROR("Path name too long for container's lxc.init.static"); |
3210 | return -1; | |
2322903b SH |
3211 | } |
3212 | ||
3213 | if (!file_exists(destpath)) { | |
8353b4c9 | 3214 | FILE *pathfile = fopen(destpath, "wb"); |
2322903b | 3215 | if (!pathfile) { |
8353b4c9 CB |
3216 | SYSERROR("Failed to create mount target \"%s\"", destpath); |
3217 | return -1; | |
2322903b | 3218 | } |
8353b4c9 | 3219 | |
2322903b SH |
3220 | fclose(pathfile); |
3221 | } | |
3222 | ||
592fd47a | 3223 | ret = safe_mount(path, destpath, "none", MS_BIND, NULL, conf->rootfs.mount); |
8353b4c9 | 3224 | if (ret < 0) { |
2322903b | 3225 | SYSERROR("Failed to bind lxc.init.static into container"); |
8353b4c9 CB |
3226 | return -1; |
3227 | } | |
3228 | ||
3229 | INFO("Bind mounted lxc.init.static into container at \"%s\"", path); | |
3230 | return 0; | |
2322903b SH |
3231 | } |
3232 | ||
35120d9c SH |
3233 | /* |
3234 | * This does the work of remounting / if it is shared, calling the | |
3235 | * container pre-mount hooks, and mounting the rootfs. | |
3236 | */ | |
3237 | int do_rootfs_setup(struct lxc_conf *conf, const char *name, const char *lxcpath) | |
0ad19a3f | 3238 | { |
35120d9c SH |
3239 | if (conf->rootfs_setup) { |
3240 | /* | |
3241 | * rootfs was set up in another namespace. bind-mount it | |
3242 | * to give us a mount in our own ns so we can pivot_root to it | |
3243 | */ | |
3244 | const char *path = conf->rootfs.mount; | |
3245 | if (mount(path, path, "rootfs", MS_BIND, NULL) < 0) { | |
3246 | ERROR("Failed to bind-mount container / onto itself"); | |
145832ba | 3247 | return -1; |
35120d9c | 3248 | } |
145832ba | 3249 | return 0; |
35120d9c | 3250 | } |
d4ef7c50 | 3251 | |
e995d7a2 SH |
3252 | remount_all_slave(); |
3253 | ||
14a7b0f9 | 3254 | if (run_lxc_hooks(name, "pre-mount", conf, NULL)) { |
35120d9c SH |
3255 | ERROR("failed to run pre-mount hooks for container '%s'.", name); |
3256 | return -1; | |
3257 | } | |
3258 | ||
9aa76a17 | 3259 | if (lxc_setup_rootfs(conf)) { |
35120d9c SH |
3260 | ERROR("failed to setup rootfs for '%s'", name); |
3261 | return -1; | |
3262 | } | |
3263 | ||
3264 | conf->rootfs_setup = true; | |
3265 | return 0; | |
3266 | } | |
3267 | ||
1c1c7051 SH |
3268 | static bool verify_start_hooks(struct lxc_conf *conf) |
3269 | { | |
3270 | struct lxc_list *it; | |
3271 | char path[MAXPATHLEN]; | |
3272 | lxc_list_for_each(it, &conf->hooks[LXCHOOK_START]) { | |
3273 | char *hookname = it->elem; | |
3274 | struct stat st; | |
3275 | int ret; | |
3276 | ||
3277 | ret = snprintf(path, MAXPATHLEN, "%s%s", | |
7b6753e7 | 3278 | conf->rootfs.path ? conf->rootfs.mount : "", hookname); |
1c1c7051 SH |
3279 | if (ret < 0 || ret >= MAXPATHLEN) |
3280 | return false; | |
3281 | ret = stat(path, &st); | |
3282 | if (ret) { | |
7b6753e7 | 3283 | SYSERROR("Start hook %s not found in container", |
1c1c7051 SH |
3284 | hookname); |
3285 | return false; | |
3286 | } | |
6a0c909a | 3287 | return true; |
1c1c7051 SH |
3288 | } |
3289 | ||
3290 | return true; | |
3291 | } | |
3292 | ||
3b988b33 | 3293 | int lxc_setup(struct lxc_handler *handler) |
35120d9c | 3294 | { |
2187efd3 | 3295 | int ret; |
35120d9c SH |
3296 | const char *name = handler->name; |
3297 | struct lxc_conf *lxc_conf = handler->conf; | |
3298 | const char *lxcpath = handler->lxcpath; | |
35120d9c | 3299 | |
8353b4c9 CB |
3300 | ret = do_rootfs_setup(lxc_conf, name, lxcpath); |
3301 | if (ret < 0) { | |
3302 | ERROR("Failed to setup rootfs"); | |
35120d9c SH |
3303 | return -1; |
3304 | } | |
3305 | ||
28d9e29e | 3306 | if (handler->nsfd[LXC_NS_UTS] == -1) { |
8353b4c9 CB |
3307 | ret = setup_utsname(lxc_conf->utsname); |
3308 | if (ret < 0) { | |
6c544cb3 MM |
3309 | ERROR("failed to setup the utsname for '%s'", name); |
3310 | return -1; | |
3311 | } | |
0ad19a3f | 3312 | } |
3313 | ||
8353b4c9 CB |
3314 | ret = lxc_setup_network_in_child_namespaces(lxc_conf, &lxc_conf->network); |
3315 | if (ret < 0) { | |
3316 | ERROR("Failed to setup network"); | |
95b5ffaf | 3317 | return -1; |
0ad19a3f | 3318 | } |
3319 | ||
8353b4c9 CB |
3320 | ret = lxc_network_send_name_and_ifindex_to_parent(handler); |
3321 | if (ret < 0) { | |
3322 | ERROR("Failed to send network device names and ifindices to parent"); | |
790255cf CB |
3323 | return -1; |
3324 | } | |
3325 | ||
bc6928ff | 3326 | if (lxc_conf->autodev > 0) { |
8353b4c9 CB |
3327 | ret = mount_autodev(name, &lxc_conf->rootfs, lxcpath); |
3328 | if (ret < 0) { | |
3329 | ERROR("Failed to mount \"/dev\""); | |
c6883f38 SH |
3330 | return -1; |
3331 | } | |
3332 | } | |
3333 | ||
8353b4c9 CB |
3334 | /* Do automatic mounts (mainly /proc and /sys), but exclude those that |
3335 | * need to wait until other stuff has finished. | |
368bbc02 | 3336 | */ |
8353b4c9 CB |
3337 | ret = lxc_mount_auto_mounts(lxc_conf, lxc_conf->auto_mounts & ~LXC_AUTO_CGROUP_MASK, handler); |
3338 | if (ret < 0) { | |
3339 | ERROR("Failed to setup first automatic mounts"); | |
368bbc02 CS |
3340 | return -1; |
3341 | } | |
3342 | ||
8353b4c9 CB |
3343 | ret = setup_mount(lxc_conf, &lxc_conf->rootfs, lxc_conf->fstab, name, lxcpath); |
3344 | if (ret < 0) { | |
3345 | ERROR("Failed to setup mounts"); | |
95b5ffaf | 3346 | return -1; |
576f946d | 3347 | } |
3348 | ||
7b6753e7 | 3349 | /* Make sure any start hooks are in the container */ |
1c1c7051 SH |
3350 | if (!verify_start_hooks(lxc_conf)) |
3351 | return -1; | |
3352 | ||
8353b4c9 CB |
3353 | if (lxc_conf->is_execute) { |
3354 | ret = lxc_execute_bind_init(lxc_conf); | |
3355 | if (ret < 0) { | |
3356 | ERROR("Failed to bind-mount the lxc init system"); | |
3357 | return -1; | |
3358 | } | |
3359 | } | |
2322903b | 3360 | |
8353b4c9 CB |
3361 | /* Now mount only cgroups, if wanted. Before, /sys could not have been |
3362 | * mounted. It is guaranteed to be mounted now either through | |
3363 | * automatically or via fstab entries. | |
368bbc02 | 3364 | */ |
8353b4c9 CB |
3365 | ret = lxc_mount_auto_mounts(lxc_conf, lxc_conf->auto_mounts & LXC_AUTO_CGROUP_MASK, handler); |
3366 | if (ret < 0) { | |
3367 | ERROR("Failed to setup remaining automatic mounts"); | |
368bbc02 CS |
3368 | return -1; |
3369 | } | |
3370 | ||
8353b4c9 | 3371 | ret = run_lxc_hooks(name, "mount", lxc_conf, NULL); |
14a7b0f9 | 3372 | if (run_lxc_hooks(name, "mount", lxc_conf, NULL)) { |
8353b4c9 | 3373 | ERROR("Failed to run mount hooks"); |
773fb9ca SH |
3374 | return -1; |
3375 | } | |
3376 | ||
bc6928ff | 3377 | if (lxc_conf->autodev > 0) { |
8353b4c9 CB |
3378 | ret = run_lxc_hooks(name, "autodev", lxc_conf, NULL); |
3379 | if (ret < 0) { | |
3380 | ERROR("Failed to run autodev hooks"); | |
f7bee6c6 MW |
3381 | return -1; |
3382 | } | |
06749971 | 3383 | |
8353b4c9 CB |
3384 | ret = lxc_fill_autodev(&lxc_conf->rootfs); |
3385 | if (ret < 0) { | |
3386 | ERROR("Failed to populate \"/dev\""); | |
91c3830e SH |
3387 | return -1; |
3388 | } | |
3389 | } | |
368bbc02 | 3390 | |
8353b4c9 CB |
3391 | if (!lxc_list_empty(&lxc_conf->mount_list)) { |
3392 | ret = setup_mount_entries(lxc_conf, &lxc_conf->rootfs, | |
3393 | &lxc_conf->mount_list, name, lxcpath); | |
3394 | if (ret < 0) { | |
3395 | ERROR("Failed to setup mount entries"); | |
3396 | return -1; | |
3397 | } | |
181437fd YT |
3398 | } |
3399 | ||
ed8704d0 CB |
3400 | ret = lxc_setup_console(&lxc_conf->rootfs, &lxc_conf->console, |
3401 | lxc_conf->ttydir); | |
3402 | if (ret < 0) { | |
3403 | ERROR("Failed to setup console"); | |
95b5ffaf | 3404 | return -1; |
6e590161 | 3405 | } |
3406 | ||
ed8704d0 CB |
3407 | ret = lxc_setup_dev_symlinks(&lxc_conf->rootfs); |
3408 | if (ret < 0) { | |
8353b4c9 | 3409 | ERROR("Failed to setup \"/dev\" symlinks"); |
69aa6655 DE |
3410 | return -1; |
3411 | } | |
3412 | ||
8353b4c9 CB |
3413 | ret = lxc_create_tmp_proc_mount(lxc_conf); |
3414 | if (ret < 0) { | |
3415 | ERROR("Failed to \"/proc\" LSMs"); | |
e075f5d9 | 3416 | return -1; |
e075f5d9 | 3417 | } |
e075f5d9 | 3418 | |
8353b4c9 CB |
3419 | ret = setup_pivot_root(&lxc_conf->rootfs); |
3420 | if (ret < 0) { | |
3421 | ERROR("Failed to pivot root into rootfs"); | |
95b5ffaf | 3422 | return -1; |
ed502555 | 3423 | } |
3424 | ||
8353b4c9 CB |
3425 | ret = lxc_setup_devpts(lxc_conf); |
3426 | if (ret < 0) { | |
3427 | ERROR("Failed to setup new devpts instance"); | |
95b5ffaf | 3428 | return -1; |
3c26f34e | 3429 | } |
3430 | ||
2187efd3 CB |
3431 | ret = lxc_create_ttys(handler); |
3432 | if (ret < 0) | |
e8bd4e43 | 3433 | return -1; |
e8bd4e43 | 3434 | |
8353b4c9 CB |
3435 | ret = setup_personality(lxc_conf->personality); |
3436 | if (ret < 0) { | |
3437 | ERROR("Failed to set personality"); | |
cccc74b5 DL |
3438 | return -1; |
3439 | } | |
3440 | ||
8353b4c9 CB |
3441 | /* Set sysctl value to a path under /proc/sys as determined from the |
3442 | * key. For e.g. net.ipv4.ip_forward translated to | |
3443 | * /proc/sys/net/ipv4/ip_forward. | |
7edd0540 L |
3444 | */ |
3445 | if (!lxc_list_empty(&lxc_conf->sysctls)) { | |
3446 | ret = setup_sysctl_parameters(&lxc_conf->sysctls); | |
8353b4c9 CB |
3447 | if (ret < 0) { |
3448 | ERROR("Failed to setup sysctl parameters"); | |
7edd0540 | 3449 | return -1; |
8353b4c9 | 3450 | } |
7edd0540 L |
3451 | } |
3452 | ||
97a8f74f SG |
3453 | if (!lxc_list_empty(&lxc_conf->keepcaps)) { |
3454 | if (!lxc_list_empty(&lxc_conf->caps)) { | |
8353b4c9 CB |
3455 | ERROR("Container requests lxc.cap.drop and " |
3456 | "lxc.cap.keep: either use lxc.cap.drop or " | |
3457 | "lxc.cap.keep, not both"); | |
f6d3e3e4 SH |
3458 | return -1; |
3459 | } | |
8353b4c9 | 3460 | |
97a8f74f | 3461 | if (dropcaps_except(&lxc_conf->keepcaps)) { |
8353b4c9 | 3462 | ERROR("Failed to keep capabilities"); |
97a8f74f SG |
3463 | return -1; |
3464 | } | |
3465 | } else if (setup_caps(&lxc_conf->caps)) { | |
8353b4c9 | 3466 | ERROR("Failed to drop capabilities"); |
97a8f74f | 3467 | return -1; |
81810dd1 DL |
3468 | } |
3469 | ||
8353b4c9 | 3470 | NOTICE("The container \"%s\" is set up", name); |
cd54d859 | 3471 | |
0ad19a3f | 3472 | return 0; |
3473 | } | |
26ddeedd | 3474 | |
3f60c2f7 | 3475 | int run_lxc_hooks(const char *name, char *hookname, struct lxc_conf *conf, |
14a7b0f9 | 3476 | char *argv[]) |
26ddeedd | 3477 | { |
26ddeedd | 3478 | struct lxc_list *it; |
3f60c2f7 | 3479 | int which = -1; |
26ddeedd | 3480 | |
3f60c2f7 | 3481 | if (strcmp(hookname, "pre-start") == 0) |
26ddeedd | 3482 | which = LXCHOOK_PRESTART; |
3f60c2f7 | 3483 | else if (strcmp(hookname, "start-host") == 0) |
08dd2805 | 3484 | which = LXCHOOK_START_HOST; |
3f60c2f7 | 3485 | else if (strcmp(hookname, "pre-mount") == 0) |
5ea6163a | 3486 | which = LXCHOOK_PREMOUNT; |
3f60c2f7 | 3487 | else if (strcmp(hookname, "mount") == 0) |
26ddeedd | 3488 | which = LXCHOOK_MOUNT; |
3f60c2f7 | 3489 | else if (strcmp(hookname, "autodev") == 0) |
f7bee6c6 | 3490 | which = LXCHOOK_AUTODEV; |
3f60c2f7 | 3491 | else if (strcmp(hookname, "start") == 0) |
26ddeedd | 3492 | which = LXCHOOK_START; |
3f60c2f7 | 3493 | else if (strcmp(hookname, "stop") == 0) |
52492063 | 3494 | which = LXCHOOK_STOP; |
3f60c2f7 | 3495 | else if (strcmp(hookname, "post-stop") == 0) |
26ddeedd | 3496 | which = LXCHOOK_POSTSTOP; |
3f60c2f7 | 3497 | else if (strcmp(hookname, "clone") == 0) |
148e91f5 | 3498 | which = LXCHOOK_CLONE; |
3f60c2f7 | 3499 | else if (strcmp(hookname, "destroy") == 0) |
37cf711b | 3500 | which = LXCHOOK_DESTROY; |
26ddeedd SH |
3501 | else |
3502 | return -1; | |
3f60c2f7 | 3503 | |
26ddeedd SH |
3504 | lxc_list_for_each(it, &conf->hooks[which]) { |
3505 | int ret; | |
3f60c2f7 CB |
3506 | char *hook = it->elem; |
3507 | ||
3508 | ret = run_script_argv(name, conf->hooks_version, "lxc", hook, | |
14a7b0f9 | 3509 | hookname, argv); |
3f60c2f7 CB |
3510 | if (ret < 0) |
3511 | return -1; | |
26ddeedd | 3512 | } |
3f60c2f7 | 3513 | |
26ddeedd SH |
3514 | return 0; |
3515 | } | |
72d0e1cb | 3516 | |
72d0e1cb SG |
3517 | int lxc_clear_config_caps(struct lxc_conf *c) |
3518 | { | |
1a0e70ac | 3519 | struct lxc_list *it, *next; |
72d0e1cb | 3520 | |
9ebb03ad | 3521 | lxc_list_for_each_safe(it, &c->caps, next) { |
72d0e1cb SG |
3522 | lxc_list_del(it); |
3523 | free(it->elem); | |
3524 | free(it); | |
3525 | } | |
3526 | return 0; | |
3527 | } | |
3528 | ||
c7e345ae CB |
3529 | static int lxc_free_idmap(struct lxc_list *id_map) |
3530 | { | |
27c27d73 SH |
3531 | struct lxc_list *it, *next; |
3532 | ||
4355ab5f | 3533 | lxc_list_for_each_safe(it, id_map, next) { |
27c27d73 SH |
3534 | lxc_list_del(it); |
3535 | free(it->elem); | |
3536 | free(it); | |
3537 | } | |
c7e345ae | 3538 | |
27c27d73 SH |
3539 | return 0; |
3540 | } | |
3541 | ||
4355ab5f SH |
3542 | int lxc_clear_idmaps(struct lxc_conf *c) |
3543 | { | |
3544 | return lxc_free_idmap(&c->id_map); | |
3545 | } | |
3546 | ||
1fb86a7c SH |
3547 | int lxc_clear_config_keepcaps(struct lxc_conf *c) |
3548 | { | |
3549 | struct lxc_list *it,*next; | |
3550 | ||
3551 | lxc_list_for_each_safe(it, &c->keepcaps, next) { | |
3552 | lxc_list_del(it); | |
3553 | free(it->elem); | |
3554 | free(it); | |
3555 | } | |
3556 | return 0; | |
3557 | } | |
3558 | ||
54860ed0 | 3559 | int lxc_clear_cgroups(struct lxc_conf *c, const char *key, int version) |
72d0e1cb | 3560 | { |
54860ed0 | 3561 | char *global_token, *namespaced_token; |
ab1a6cac | 3562 | size_t namespaced_token_len; |
54860ed0 | 3563 | struct lxc_list *it, *next, *list; |
ab1a6cac | 3564 | const char *k = key; |
54860ed0 | 3565 | bool all = false; |
72d0e1cb | 3566 | |
54860ed0 CB |
3567 | if (version == CGROUP2_SUPER_MAGIC) { |
3568 | global_token = "lxc.cgroup2"; | |
3569 | namespaced_token = "lxc.cgroup2."; | |
ab1a6cac | 3570 | namespaced_token_len = sizeof("lxc.cgroup2.") - 1;; |
54860ed0 CB |
3571 | list = &c->cgroup2; |
3572 | } else if (version == CGROUP_SUPER_MAGIC) { | |
3573 | global_token = "lxc.cgroup"; | |
3574 | namespaced_token = "lxc.cgroup."; | |
ab1a6cac | 3575 | namespaced_token_len = sizeof("lxc.cgroup.") - 1;; |
54860ed0 CB |
3576 | list = &c->cgroup; |
3577 | } else { | |
ab1a6cac | 3578 | return -EINVAL; |
54860ed0 CB |
3579 | } |
3580 | ||
3581 | if (strcmp(key, global_token) == 0) | |
72d0e1cb | 3582 | all = true; |
54860ed0 | 3583 | else if (strncmp(key, namespaced_token, sizeof(namespaced_token) - 1) == 0) |
ab1a6cac | 3584 | k += namespaced_token_len; |
a6390f01 | 3585 | else |
ab1a6cac | 3586 | return -EINVAL; |
72d0e1cb | 3587 | |
54860ed0 | 3588 | lxc_list_for_each_safe(it, list, next) { |
72d0e1cb | 3589 | struct lxc_cgroup *cg = it->elem; |
54860ed0 | 3590 | |
72d0e1cb SG |
3591 | if (!all && strcmp(cg->subsystem, k) != 0) |
3592 | continue; | |
54860ed0 | 3593 | |
72d0e1cb SG |
3594 | lxc_list_del(it); |
3595 | free(cg->subsystem); | |
3596 | free(cg->value); | |
3597 | free(cg); | |
3598 | free(it); | |
3599 | } | |
e409b214 | 3600 | |
72d0e1cb SG |
3601 | return 0; |
3602 | } | |
3603 | ||
c6d09e15 WB |
3604 | int lxc_clear_limits(struct lxc_conf *c, const char *key) |
3605 | { | |
3606 | struct lxc_list *it, *next; | |
3607 | bool all = false; | |
3608 | const char *k = NULL; | |
3609 | ||
b668653c | 3610 | if (strcmp(key, "lxc.limit") == 0 || strcmp(key, "lxc.prlimit") == 0) |
c6d09e15 | 3611 | all = true; |
b668653c CB |
3612 | else if (strncmp(key, "lxc.limit.", sizeof("lxc.limit.") - 1) == 0) |
3613 | k = key + sizeof("lxc.limit.") - 1; | |
3614 | else if (strncmp(key, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) == 0) | |
3615 | k = key + sizeof("lxc.prlimit.") - 1; | |
c6d09e15 WB |
3616 | else |
3617 | return -1; | |
3618 | ||
3619 | lxc_list_for_each_safe(it, &c->limits, next) { | |
3620 | struct lxc_limit *lim = it->elem; | |
3621 | if (!all && strcmp(lim->resource, k) != 0) | |
3622 | continue; | |
3623 | lxc_list_del(it); | |
3624 | free(lim->resource); | |
3625 | free(lim); | |
3626 | free(it); | |
3627 | } | |
b668653c | 3628 | |
c6d09e15 WB |
3629 | return 0; |
3630 | } | |
3631 | ||
7edd0540 L |
3632 | int lxc_clear_sysctls(struct lxc_conf *c, const char *key) |
3633 | { | |
3634 | struct lxc_list *it, *next; | |
3635 | bool all = false; | |
3636 | const char *k = NULL; | |
3637 | ||
3638 | if (strcmp(key, "lxc.sysctl") == 0) | |
3639 | all = true; | |
3640 | else if (strncmp(key, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0) | |
3641 | k = key + sizeof("lxc.sysctl.") - 1; | |
3642 | else | |
3643 | return -1; | |
3644 | ||
3645 | lxc_list_for_each_safe(it, &c->sysctls, next) { | |
3646 | struct lxc_sysctl *elem = it->elem; | |
3647 | if (!all && strcmp(elem->key, k) != 0) | |
3648 | continue; | |
3649 | lxc_list_del(it); | |
3650 | free(elem->key); | |
3651 | free(elem->value); | |
3652 | free(elem); | |
3653 | free(it); | |
3654 | } | |
3655 | return 0; | |
3656 | } | |
3657 | ||
61d7a733 YT |
3658 | int lxc_clear_procs(struct lxc_conf *c, const char *key) |
3659 | { | |
3660 | struct lxc_list *it,*next; | |
3661 | bool all = false; | |
3662 | const char *k = NULL; | |
3663 | ||
3664 | if (strcmp(key, "lxc.proc") == 0) | |
3665 | all = true; | |
3666 | else if (strncmp(key, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0) | |
3667 | k = key + sizeof("lxc.proc.") - 1; | |
3668 | else | |
3669 | return -1; | |
3670 | ||
3671 | lxc_list_for_each_safe(it, &c->procs, next) { | |
3672 | struct lxc_proc *proc = it->elem; | |
3673 | if (!all && strcmp(proc->filename, k) != 0) | |
3674 | continue; | |
3675 | lxc_list_del(it); | |
3676 | free(proc->filename); | |
3677 | free(proc->value); | |
3678 | free(proc); | |
3679 | free(it); | |
3680 | } | |
3681 | ||
3682 | return 0; | |
3683 | } | |
3684 | ||
ee1e7aa0 SG |
3685 | int lxc_clear_groups(struct lxc_conf *c) |
3686 | { | |
3687 | struct lxc_list *it,*next; | |
3688 | ||
3689 | lxc_list_for_each_safe(it, &c->groups, next) { | |
3690 | lxc_list_del(it); | |
3691 | free(it->elem); | |
3692 | free(it); | |
3693 | } | |
3694 | return 0; | |
3695 | } | |
3696 | ||
ab799c0b SG |
3697 | int lxc_clear_environment(struct lxc_conf *c) |
3698 | { | |
3699 | struct lxc_list *it,*next; | |
3700 | ||
3701 | lxc_list_for_each_safe(it, &c->environment, next) { | |
3702 | lxc_list_del(it); | |
3703 | free(it->elem); | |
3704 | free(it); | |
3705 | } | |
3706 | return 0; | |
3707 | } | |
3708 | ||
72d0e1cb SG |
3709 | int lxc_clear_mount_entries(struct lxc_conf *c) |
3710 | { | |
9ebb03ad | 3711 | struct lxc_list *it,*next; |
72d0e1cb | 3712 | |
9ebb03ad | 3713 | lxc_list_for_each_safe(it, &c->mount_list, next) { |
72d0e1cb SG |
3714 | lxc_list_del(it); |
3715 | free(it->elem); | |
3716 | free(it); | |
3717 | } | |
3718 | return 0; | |
3719 | } | |
3720 | ||
b099e9e9 SH |
3721 | int lxc_clear_automounts(struct lxc_conf *c) |
3722 | { | |
3723 | c->auto_mounts = 0; | |
3724 | return 0; | |
3725 | } | |
3726 | ||
12a50cc6 | 3727 | int lxc_clear_hooks(struct lxc_conf *c, const char *key) |
72d0e1cb | 3728 | { |
9ebb03ad | 3729 | struct lxc_list *it,*next; |
17ed13a3 | 3730 | bool all = false, done = false; |
a6390f01 | 3731 | const char *k = NULL; |
72d0e1cb SG |
3732 | int i; |
3733 | ||
17ed13a3 SH |
3734 | if (strcmp(key, "lxc.hook") == 0) |
3735 | all = true; | |
a6390f01 WB |
3736 | else if (strncmp(key, "lxc.hook.", sizeof("lxc.hook.")-1) == 0) |
3737 | k = key + sizeof("lxc.hook.")-1; | |
3738 | else | |
3739 | return -1; | |
17ed13a3 | 3740 | |
72d0e1cb | 3741 | for (i=0; i<NUM_LXC_HOOKS; i++) { |
17ed13a3 | 3742 | if (all || strcmp(k, lxchook_names[i]) == 0) { |
9ebb03ad | 3743 | lxc_list_for_each_safe(it, &c->hooks[i], next) { |
17ed13a3 SH |
3744 | lxc_list_del(it); |
3745 | free(it->elem); | |
3746 | free(it); | |
3747 | } | |
3748 | done = true; | |
72d0e1cb SG |
3749 | } |
3750 | } | |
17ed13a3 SH |
3751 | |
3752 | if (!done) { | |
3753 | ERROR("Invalid hook key: %s", key); | |
3754 | return -1; | |
3755 | } | |
72d0e1cb SG |
3756 | return 0; |
3757 | } | |
8eb5694b | 3758 | |
4184c3e1 SH |
3759 | static inline void lxc_clear_aliens(struct lxc_conf *conf) |
3760 | { | |
3761 | struct lxc_list *it,*next; | |
3762 | ||
3763 | lxc_list_for_each_safe(it, &conf->aliens, next) { | |
3764 | lxc_list_del(it); | |
3765 | free(it->elem); | |
3766 | free(it); | |
3767 | } | |
3768 | } | |
3769 | ||
c7b15d1e | 3770 | void lxc_clear_includes(struct lxc_conf *conf) |
f979ac15 SH |
3771 | { |
3772 | struct lxc_list *it,*next; | |
3773 | ||
3774 | lxc_list_for_each_safe(it, &conf->includes, next) { | |
3775 | lxc_list_del(it); | |
3776 | free(it->elem); | |
3777 | free(it); | |
3778 | } | |
3779 | } | |
3780 | ||
8eb5694b SH |
3781 | void lxc_conf_free(struct lxc_conf *conf) |
3782 | { | |
3783 | if (!conf) | |
3784 | return; | |
858377e4 SH |
3785 | if (current_config == conf) |
3786 | current_config = NULL; | |
aed105d5 | 3787 | lxc_terminal_conf_free(&conf->console); |
f10fad2f | 3788 | free(conf->rootfs.mount); |
b3b8c97f | 3789 | free(conf->rootfs.bdev_type); |
f10fad2f ME |
3790 | free(conf->rootfs.options); |
3791 | free(conf->rootfs.path); | |
f10fad2f | 3792 | free(conf->logfile); |
858377e4 SH |
3793 | if (conf->logfd != -1) |
3794 | close(conf->logfd); | |
f10fad2f ME |
3795 | free(conf->utsname); |
3796 | free(conf->ttydir); | |
3797 | free(conf->fstab); | |
3798 | free(conf->rcfile); | |
5cda27c1 | 3799 | free(conf->execute_cmd); |
f10fad2f | 3800 | free(conf->init_cmd); |
3c491553 | 3801 | free(conf->init_cwd); |
6b0d5538 | 3802 | free(conf->unexpanded_config); |
393903d1 | 3803 | free(conf->pty_names); |
76d0127f | 3804 | free(conf->syslog); |
c302b476 | 3805 | lxc_free_networks(&conf->network); |
f10fad2f ME |
3806 | free(conf->lsm_aa_profile); |
3807 | free(conf->lsm_se_context); | |
769872f9 | 3808 | lxc_seccomp_free(conf); |
8eb5694b | 3809 | lxc_clear_config_caps(conf); |
1fb86a7c | 3810 | lxc_clear_config_keepcaps(conf); |
54860ed0 CB |
3811 | lxc_clear_cgroups(conf, "lxc.cgroup", CGROUP_SUPER_MAGIC); |
3812 | lxc_clear_cgroups(conf, "lxc.cgroup2", CGROUP2_SUPER_MAGIC); | |
17ed13a3 | 3813 | lxc_clear_hooks(conf, "lxc.hook"); |
8eb5694b | 3814 | lxc_clear_mount_entries(conf); |
27c27d73 | 3815 | lxc_clear_idmaps(conf); |
ee1e7aa0 | 3816 | lxc_clear_groups(conf); |
f979ac15 | 3817 | lxc_clear_includes(conf); |
761d81ca | 3818 | lxc_clear_aliens(conf); |
ab799c0b | 3819 | lxc_clear_environment(conf); |
240d4b74 | 3820 | lxc_clear_limits(conf, "lxc.prlimit"); |
7edd0540 | 3821 | lxc_clear_sysctls(conf, "lxc.sysctl"); |
61d7a733 | 3822 | lxc_clear_procs(conf, "lxc.proc"); |
43654d34 CB |
3823 | free(conf->cgroup_meta.dir); |
3824 | free(conf->cgroup_meta.controllers); | |
8eb5694b SH |
3825 | free(conf); |
3826 | } | |
4355ab5f SH |
3827 | |
3828 | struct userns_fn_data { | |
3829 | int (*fn)(void *); | |
c9b7c33e | 3830 | const char *fn_name; |
4355ab5f SH |
3831 | void *arg; |
3832 | int p[2]; | |
3833 | }; | |
3834 | ||
3835 | static int run_userns_fn(void *data) | |
3836 | { | |
3837 | struct userns_fn_data *d = data; | |
3838 | char c; | |
4355ab5f | 3839 | |
f8aa4bf3 | 3840 | /* Close write end of the pipe. */ |
4355ab5f | 3841 | close(d->p[1]); |
f8aa4bf3 CB |
3842 | |
3843 | /* Wait for parent to finish establishing a new mapping in the user | |
3844 | * namespace we are executing in. | |
3845 | */ | |
4355ab5f SH |
3846 | if (read(d->p[0], &c, 1) != 1) |
3847 | return -1; | |
f8aa4bf3 CB |
3848 | |
3849 | /* Close read end of the pipe. */ | |
4355ab5f | 3850 | close(d->p[0]); |
f8aa4bf3 | 3851 | |
c9b7c33e CB |
3852 | if (d->fn_name) |
3853 | TRACE("calling function \"%s\"", d->fn_name); | |
f8aa4bf3 | 3854 | /* Call function to run. */ |
4355ab5f SH |
3855 | return d->fn(d->arg); |
3856 | } | |
3857 | ||
db7cfe23 CB |
3858 | static struct id_map *mapped_nsid_add(struct lxc_conf *conf, unsigned id, |
3859 | enum idtype idtype) | |
3860 | { | |
3861 | struct id_map *map, *retmap; | |
3862 | ||
3863 | map = find_mapped_nsid_entry(conf, id, idtype); | |
3864 | if (!map) | |
3865 | return NULL; | |
3866 | ||
3867 | retmap = malloc(sizeof(*retmap)); | |
3868 | if (!retmap) | |
3869 | return NULL; | |
3870 | ||
3871 | memcpy(retmap, map, sizeof(*retmap)); | |
3872 | return retmap; | |
3873 | } | |
3874 | ||
c4333195 CB |
3875 | static struct id_map *find_mapped_hostid_entry(struct lxc_conf *conf, |
3876 | unsigned id, enum idtype idtype) | |
f8aa4bf3 CB |
3877 | { |
3878 | struct lxc_list *it; | |
3879 | struct id_map *map; | |
3880 | struct id_map *retmap = NULL; | |
3881 | ||
3882 | lxc_list_for_each(it, &conf->id_map) { | |
3883 | map = it->elem; | |
3884 | if (map->idtype != idtype) | |
3885 | continue; | |
3886 | ||
3887 | if (id >= map->hostid && id < map->hostid + map->range) { | |
3888 | retmap = map; | |
3889 | break; | |
3890 | } | |
3891 | } | |
3892 | ||
f8aa4bf3 CB |
3893 | return retmap; |
3894 | } | |
3895 | ||
4355ab5f | 3896 | /* |
f8aa4bf3 CB |
3897 | * Allocate a new {g,u}id mapping for the given {g,u}id. Re-use an already |
3898 | * existing one or establish a new one. | |
4355ab5f | 3899 | */ |
c4333195 | 3900 | static struct id_map *mapped_hostid_add(struct lxc_conf *conf, uid_t id, enum idtype type) |
4355ab5f | 3901 | { |
28a2d9e7 | 3902 | int hostid_mapped; |
c4333195 CB |
3903 | struct id_map *entry = NULL, *tmp = NULL; |
3904 | ||
3905 | entry = malloc(sizeof(*entry)); | |
3906 | if (!entry) | |
3907 | return NULL; | |
f8aa4bf3 | 3908 | |
28a2d9e7 | 3909 | /* Reuse existing mapping. */ |
c4333195 CB |
3910 | tmp = find_mapped_hostid_entry(conf, id, type); |
3911 | if (tmp) | |
3912 | return memcpy(entry, tmp, sizeof(*entry)); | |
f8aa4bf3 | 3913 | |
28a2d9e7 CB |
3914 | /* Find new mapping. */ |
3915 | hostid_mapped = find_unmapped_nsid(conf, type); | |
3916 | if (hostid_mapped < 0) { | |
c4333195 CB |
3917 | DEBUG("Failed to find free mapping for id %d", id); |
3918 | free(entry); | |
28a2d9e7 | 3919 | return NULL; |
f8aa4bf3 | 3920 | } |
f8aa4bf3 | 3921 | |
28a2d9e7 CB |
3922 | entry->idtype = type; |
3923 | entry->nsid = hostid_mapped; | |
3924 | entry->hostid = (unsigned long)id; | |
3925 | entry->range = 1; | |
4355ab5f | 3926 | |
28a2d9e7 | 3927 | return entry; |
4355ab5f SH |
3928 | } |
3929 | ||
dcf0ffdf | 3930 | struct lxc_list *get_minimal_idmap(struct lxc_conf *conf) |
4355ab5f | 3931 | { |
f8aa4bf3 | 3932 | uid_t euid, egid; |
4160c3a0 CB |
3933 | uid_t nsuid = (conf->root_nsuid_map != NULL) ? 0 : conf->init_uid; |
3934 | gid_t nsgid = (conf->root_nsgid_map != NULL) ? 0 : conf->init_gid; | |
f8aa4bf3 | 3935 | struct lxc_list *idmap = NULL, *tmplist = NULL; |
28a2d9e7 CB |
3936 | struct id_map *container_root_uid = NULL, *container_root_gid = NULL, |
3937 | *host_uid_map = NULL, *host_gid_map = NULL; | |
4355ab5f | 3938 | |
db7cfe23 | 3939 | /* Find container root mappings. */ |
4160c3a0 | 3940 | container_root_uid = mapped_nsid_add(conf, nsuid, ID_TYPE_UID); |
db7cfe23 | 3941 | if (!container_root_uid) { |
dcf0ffdf | 3942 | DEBUG("Failed to find mapping for namespace uid %d", 0); |
db7cfe23 | 3943 | goto on_error; |
f8aa4bf3 | 3944 | } |
dcf0ffdf CB |
3945 | euid = geteuid(); |
3946 | if (euid >= container_root_uid->hostid && | |
3947 | euid < (container_root_uid->hostid + container_root_uid->range)) | |
db7cfe23 | 3948 | host_uid_map = container_root_uid; |
f8aa4bf3 | 3949 | |
4160c3a0 | 3950 | container_root_gid = mapped_nsid_add(conf, nsgid, ID_TYPE_GID); |
db7cfe23 | 3951 | if (!container_root_gid) { |
dcf0ffdf | 3952 | DEBUG("Failed to find mapping for namespace gid %d", 0); |
f8aa4bf3 CB |
3953 | goto on_error; |
3954 | } | |
dcf0ffdf CB |
3955 | egid = getegid(); |
3956 | if (egid >= container_root_gid->hostid && | |
3957 | egid < (container_root_gid->hostid + container_root_gid->range)) | |
db7cfe23 | 3958 | host_gid_map = container_root_gid; |
f8aa4bf3 CB |
3959 | |
3960 | /* Check whether the {g,u}id of the user has a mapping. */ | |
954b7d9b | 3961 | if (!host_uid_map) |
c4333195 | 3962 | host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID); |
28a2d9e7 | 3963 | if (!host_uid_map) { |
db7cfe23 | 3964 | DEBUG("Failed to find mapping for uid %d", euid); |
f8aa4bf3 CB |
3965 | goto on_error; |
3966 | } | |
3967 | ||
dcf0ffdf CB |
3968 | if (!host_gid_map) |
3969 | host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID); | |
28a2d9e7 | 3970 | if (!host_gid_map) { |
db7cfe23 | 3971 | DEBUG("Failed to find mapping for gid %d", egid); |
28a2d9e7 CB |
3972 | goto on_error; |
3973 | } | |
3974 | ||
3975 | /* Allocate new {g,u}id map list. */ | |
3976 | idmap = malloc(sizeof(*idmap)); | |
3977 | if (!idmap) | |
3978 | goto on_error; | |
3979 | lxc_list_init(idmap); | |
3980 | ||
f8aa4bf3 CB |
3981 | /* Add container root to the map. */ |
3982 | tmplist = malloc(sizeof(*tmplist)); | |
3983 | if (!tmplist) | |
3984 | goto on_error; | |
3985 | lxc_list_add_elem(tmplist, container_root_uid); | |
3986 | lxc_list_add_tail(idmap, tmplist); | |
28a2d9e7 | 3987 | |
1d90e064 | 3988 | if (host_uid_map && (host_uid_map != container_root_uid)) { |
28a2d9e7 CB |
3989 | /* idmap will now keep track of that memory. */ |
3990 | container_root_uid = NULL; | |
3991 | ||
3992 | /* Add container root to the map. */ | |
3993 | tmplist = malloc(sizeof(*tmplist)); | |
3994 | if (!tmplist) | |
3995 | goto on_error; | |
3996 | lxc_list_add_elem(tmplist, host_uid_map); | |
3997 | lxc_list_add_tail(idmap, tmplist); | |
28a2d9e7 | 3998 | } |
1d90e064 CB |
3999 | /* idmap will now keep track of that memory. */ |
4000 | container_root_uid = NULL; | |
4001 | /* idmap will now keep track of that memory. */ | |
4002 | host_uid_map = NULL; | |
f8aa4bf3 CB |
4003 | |
4004 | tmplist = malloc(sizeof(*tmplist)); | |
4005 | if (!tmplist) | |
4006 | goto on_error; | |
4007 | lxc_list_add_elem(tmplist, container_root_gid); | |
4008 | lxc_list_add_tail(idmap, tmplist); | |
28a2d9e7 | 4009 | |
1d90e064 | 4010 | if (host_gid_map && (host_gid_map != container_root_gid)) { |
28a2d9e7 CB |
4011 | /* idmap will now keep track of that memory. */ |
4012 | container_root_gid = NULL; | |
4013 | ||
4014 | tmplist = malloc(sizeof(*tmplist)); | |
4015 | if (!tmplist) | |
4016 | goto on_error; | |
4017 | lxc_list_add_elem(tmplist, host_gid_map); | |
4018 | lxc_list_add_tail(idmap, tmplist); | |
28a2d9e7 | 4019 | } |
1d90e064 CB |
4020 | /* idmap will now keep track of that memory. */ |
4021 | container_root_gid = NULL; | |
4022 | /* idmap will now keep track of that memory. */ | |
4023 | host_gid_map = NULL; | |
f8aa4bf3 | 4024 | |
dcf0ffdf CB |
4025 | TRACE("Allocated minimal idmapping"); |
4026 | return idmap; | |
4027 | ||
4028 | on_error: | |
4029 | if (idmap) | |
4030 | lxc_free_idmap(idmap); | |
4031 | if (container_root_uid) | |
4032 | free(container_root_uid); | |
4033 | if (container_root_gid) | |
4034 | free(container_root_gid); | |
4035 | if (host_uid_map && (host_uid_map != container_root_uid)) | |
4036 | free(host_uid_map); | |
4037 | if (host_gid_map && (host_gid_map != container_root_gid)) | |
4038 | free(host_gid_map); | |
4039 | ||
4040 | return NULL; | |
4041 | } | |
4042 | ||
4043 | /* Run a function in a new user namespace. | |
4044 | * The caller's euid/egid will be mapped if it is not already. | |
4045 | * Afaict, userns_exec_1() is only used to operate based on privileges for the | |
4046 | * user's own {g,u}id on the host and for the container root's unmapped {g,u}id. | |
4047 | * This means we require only to establish a mapping from: | |
4048 | * - the container root {g,u}id as seen from the host > user's host {g,u}id | |
4049 | * - the container root -> some sub{g,u}id | |
4050 | * The former we add, if the user did not specifiy a mapping. The latter we | |
4051 | * retrieve from the ontainer's configured {g,u}id mappings as it must have been | |
4052 | * there to start the container in the first place. | |
4053 | */ | |
4054 | int userns_exec_1(struct lxc_conf *conf, int (*fn)(void *), void *data, | |
4055 | const char *fn_name) | |
4056 | { | |
4057 | pid_t pid; | |
4058 | struct userns_fn_data d; | |
4059 | int p[2]; | |
4060 | char c = '1'; | |
4061 | int ret = -1, status = -1; | |
4062 | struct lxc_list *idmap; | |
4063 | ||
4064 | idmap = get_minimal_idmap(conf); | |
4065 | if (!idmap) | |
4066 | return -1; | |
4067 | ||
4068 | ret = pipe(p); | |
4069 | if (ret < 0) { | |
4070 | SYSERROR("Failed to create pipe"); | |
4071 | return -1; | |
4072 | } | |
4073 | d.fn = fn; | |
4074 | d.fn_name = fn_name; | |
4075 | d.arg = data; | |
4076 | d.p[0] = p[0]; | |
4077 | d.p[1] = p[1]; | |
4078 | ||
4079 | /* Clone child in new user namespace. */ | |
4080 | pid = lxc_raw_clone_cb(run_userns_fn, &d, CLONE_NEWUSER); | |
4081 | if (pid < 0) { | |
4082 | ERROR("failed to clone child process in new user namespace"); | |
4083 | goto on_error; | |
4084 | } | |
4085 | ||
4086 | close(p[0]); | |
4087 | p[0] = -1; | |
4088 | ||
4b73005c CB |
4089 | if (lxc_log_get_level() == LXC_LOG_LEVEL_TRACE || |
4090 | conf->loglevel == LXC_LOG_LEVEL_TRACE) { | |
dcf0ffdf CB |
4091 | struct lxc_list *it; |
4092 | struct id_map *map; | |
4093 | ||
f8aa4bf3 CB |
4094 | lxc_list_for_each(it, idmap) { |
4095 | map = it->elem; | |
dcf0ffdf | 4096 | TRACE("Establishing %cid mapping for \"%d\" in new " |
f8aa4bf3 | 4097 | "user namespace: nsuid %lu - hostid %lu - range " |
dcf0ffdf CB |
4098 | "%lu", (map->idtype == ID_TYPE_UID) ? 'u' : 'g', |
4099 | pid, map->nsid, map->hostid, map->range); | |
f8aa4bf3 | 4100 | } |
4355ab5f SH |
4101 | } |
4102 | ||
f8aa4bf3 | 4103 | /* Set up {g,u}id mapping for user namespace of child process. */ |
4355ab5f | 4104 | ret = lxc_map_ids(idmap, pid); |
f8aa4bf3 | 4105 | if (ret < 0) { |
dcf0ffdf | 4106 | ERROR("Error setting up {g,u}id mappings for child process " |
415a8851 | 4107 | "\"%d\"", pid); |
f8aa4bf3 | 4108 | goto on_error; |
4355ab5f SH |
4109 | } |
4110 | ||
f8aa4bf3 | 4111 | /* Tell child to proceed. */ |
4355ab5f | 4112 | if (write(p[1], &c, 1) != 1) { |
dcf0ffdf | 4113 | SYSERROR("Failed telling child process \"%d\" to proceed", pid); |
f8aa4bf3 | 4114 | goto on_error; |
4355ab5f SH |
4115 | } |
4116 | ||
686dd5d1 | 4117 | on_error: |
4355ab5f SH |
4118 | if (p[0] != -1) |
4119 | close(p[0]); | |
4120 | close(p[1]); | |
f8aa4bf3 | 4121 | |
ee1b16bc TA |
4122 | /* Wait for child to finish. */ |
4123 | if (pid > 0) | |
4124 | status = wait_for_pid(pid); | |
4125 | ||
686dd5d1 CB |
4126 | if (status < 0) |
4127 | ret = -1; | |
4128 | ||
f8aa4bf3 | 4129 | return ret; |
4355ab5f | 4130 | } |
97e9cfa0 | 4131 | |
415a8851 CB |
4132 | int userns_exec_full(struct lxc_conf *conf, int (*fn)(void *), void *data, |
4133 | const char *fn_name) | |
4134 | { | |
4135 | pid_t pid; | |
4136 | uid_t euid, egid; | |
4137 | struct userns_fn_data d; | |
4138 | int p[2]; | |
4139 | struct id_map *map; | |
4140 | struct lxc_list *cur; | |
4141 | char c = '1'; | |
4142 | int ret = -1; | |
4143 | struct lxc_list *idmap = NULL, *tmplist = NULL; | |
4144 | struct id_map *container_root_uid = NULL, *container_root_gid = NULL, | |
4145 | *host_uid_map = NULL, *host_gid_map = NULL; | |
4146 | ||
4147 | ret = pipe(p); | |
4148 | if (ret < 0) { | |
4149 | SYSERROR("opening pipe"); | |
4150 | return -1; | |
4151 | } | |
4152 | d.fn = fn; | |
4153 | d.fn_name = fn_name; | |
4154 | d.arg = data; | |
4155 | d.p[0] = p[0]; | |
4156 | d.p[1] = p[1]; | |
4157 | ||
4158 | /* Clone child in new user namespace. */ | |
4159 | pid = lxc_clone(run_userns_fn, &d, CLONE_NEWUSER); | |
4160 | if (pid < 0) { | |
4161 | ERROR("failed to clone child process in new user namespace"); | |
4162 | goto on_error; | |
4163 | } | |
4164 | ||
4165 | close(p[0]); | |
4166 | p[0] = -1; | |
4167 | ||
4168 | euid = geteuid(); | |
4169 | egid = getegid(); | |
4170 | ||
4171 | /* Allocate new {g,u}id map list. */ | |
4172 | idmap = malloc(sizeof(*idmap)); | |
4173 | if (!idmap) | |
4174 | goto on_error; | |
4175 | lxc_list_init(idmap); | |
4176 | ||
4177 | /* Find container root. */ | |
4178 | lxc_list_for_each(cur, &conf->id_map) { | |
4179 | struct id_map *tmpmap; | |
4180 | ||
4181 | tmplist = malloc(sizeof(*tmplist)); | |
4182 | if (!tmplist) | |
4183 | goto on_error; | |
4184 | ||
4185 | tmpmap = malloc(sizeof(*tmpmap)); | |
4186 | if (!tmpmap) { | |
4187 | free(tmplist); | |
4188 | goto on_error; | |
4189 | } | |
4190 | ||
4191 | memset(tmpmap, 0, sizeof(*tmpmap)); | |
4192 | memcpy(tmpmap, cur->elem, sizeof(*tmpmap)); | |
4193 | tmplist->elem = tmpmap; | |
4194 | ||
4195 | lxc_list_add_tail(idmap, tmplist); | |
4196 | ||
4197 | map = cur->elem; | |
4198 | ||
4199 | if (map->idtype == ID_TYPE_UID) | |
4200 | if (euid >= map->hostid && euid < map->hostid + map->range) | |
4201 | host_uid_map = map; | |
4202 | ||
4203 | if (map->idtype == ID_TYPE_GID) | |
4204 | if (egid >= map->hostid && egid < map->hostid + map->range) | |
4205 | host_gid_map = map; | |
4206 | ||
4207 | if (map->nsid != 0) | |
4208 | continue; | |
4209 | ||
4210 | if (map->idtype == ID_TYPE_UID) | |
4211 | if (container_root_uid == NULL) | |
4212 | container_root_uid = map; | |
4213 | ||
4214 | if (map->idtype == ID_TYPE_GID) | |
4215 | if (container_root_gid == NULL) | |
4216 | container_root_gid = map; | |
4217 | } | |
4218 | ||
4219 | if (!container_root_uid || !container_root_gid) { | |
4220 | ERROR("No mapping for container root found"); | |
4221 | goto on_error; | |
4222 | } | |
4223 | ||
4224 | /* Check whether the {g,u}id of the user has a mapping. */ | |
4225 | if (!host_uid_map) | |
c4333195 | 4226 | host_uid_map = mapped_hostid_add(conf, euid, ID_TYPE_UID); |
415a8851 CB |
4227 | else |
4228 | host_uid_map = container_root_uid; | |
4229 | ||
4230 | if (!host_gid_map) | |
c4333195 | 4231 | host_gid_map = mapped_hostid_add(conf, egid, ID_TYPE_GID); |
415a8851 CB |
4232 | else |
4233 | host_gid_map = container_root_gid; | |
4234 | ||
4235 | if (!host_uid_map) { | |
4236 | DEBUG("Failed to find mapping for uid %d", euid); | |
4237 | goto on_error; | |
4238 | } | |
4239 | ||
4240 | if (!host_gid_map) { | |
4241 | DEBUG("Failed to find mapping for gid %d", egid); | |
4242 | goto on_error; | |
4243 | } | |
4244 | ||
4245 | if (host_uid_map && (host_uid_map != container_root_uid)) { | |
4246 | /* Add container root to the map. */ | |
4247 | tmplist = malloc(sizeof(*tmplist)); | |
4248 | if (!tmplist) | |
4249 | goto on_error; | |
4250 | lxc_list_add_elem(tmplist, host_uid_map); | |
4251 | lxc_list_add_tail(idmap, tmplist); | |
4252 | } | |
4253 | /* idmap will now keep track of that memory. */ | |
4254 | host_uid_map = NULL; | |
4255 | ||
4256 | if (host_gid_map && (host_gid_map != container_root_gid)) { | |
4257 | tmplist = malloc(sizeof(*tmplist)); | |
4258 | if (!tmplist) | |
4259 | goto on_error; | |
4260 | lxc_list_add_elem(tmplist, host_gid_map); | |
4261 | lxc_list_add_tail(idmap, tmplist); | |
4262 | } | |
4263 | /* idmap will now keep track of that memory. */ | |
4264 | host_gid_map = NULL; | |
4265 | ||
4266 | if (lxc_log_get_level() == LXC_LOG_LEVEL_TRACE || | |
4267 | conf->loglevel == LXC_LOG_LEVEL_TRACE) { | |
4268 | lxc_list_for_each(cur, idmap) { | |
4269 | map = cur->elem; | |
4270 | TRACE("establishing %cid mapping for \"%d\" in new " | |
4271 | "user namespace: nsuid %lu - hostid %lu - range " | |
4272 | "%lu", | |
4273 | (map->idtype == ID_TYPE_UID) ? 'u' : 'g', pid, | |
4274 | map->nsid, map->hostid, map->range); | |
4275 | } | |
4276 | } | |
4277 | ||
4278 | /* Set up {g,u}id mapping for user namespace of child process. */ | |
4279 | ret = lxc_map_ids(idmap, pid); | |
4280 | if (ret < 0) { | |
4281 | ERROR("error setting up {g,u}id mappings for child process " | |
4282 | "\"%d\"", pid); | |
4283 | goto on_error; | |
4284 | } | |
4285 | ||
4286 | /* Tell child to proceed. */ | |
4287 | if (write(p[1], &c, 1) != 1) { | |
4288 | SYSERROR("failed telling child process \"%d\" to proceed", pid); | |
4289 | goto on_error; | |
4290 | } | |
4291 | ||
686dd5d1 | 4292 | on_error: |
ee1b16bc TA |
4293 | if (p[0] != -1) |
4294 | close(p[0]); | |
4295 | close(p[1]); | |
4296 | ||
415a8851 | 4297 | /* Wait for child to finish. */ |
686dd5d1 CB |
4298 | if (pid > 0) |
4299 | ret = wait_for_pid(pid); | |
415a8851 | 4300 | |
415a8851 CB |
4301 | if (idmap) |
4302 | lxc_free_idmap(idmap); | |
4303 | if (host_uid_map && (host_uid_map != container_root_uid)) | |
4304 | free(host_uid_map); | |
4305 | if (host_gid_map && (host_gid_map != container_root_gid)) | |
4306 | free(host_gid_map); | |
4307 | ||
415a8851 CB |
4308 | return ret; |
4309 | } | |
4310 | ||
a96a8e8c | 4311 | /* not thread-safe, do not use from api without first forking */ |
97e9cfa0 SH |
4312 | static char* getuname(void) |
4313 | { | |
a96a8e8c | 4314 | struct passwd *result; |
97e9cfa0 | 4315 | |
a96a8e8c SH |
4316 | result = getpwuid(geteuid()); |
4317 | if (!result) | |
97e9cfa0 SH |
4318 | return NULL; |
4319 | ||
a96a8e8c | 4320 | return strdup(result->pw_name); |
97e9cfa0 SH |
4321 | } |
4322 | ||
a96a8e8c | 4323 | /* not thread-safe, do not use from api without first forking */ |
97e9cfa0 SH |
4324 | static char *getgname(void) |
4325 | { | |
a96a8e8c | 4326 | struct group *result; |
97e9cfa0 | 4327 | |
a96a8e8c SH |
4328 | result = getgrgid(getegid()); |
4329 | if (!result) | |
97e9cfa0 SH |
4330 | return NULL; |
4331 | ||
a96a8e8c | 4332 | return strdup(result->gr_name); |
97e9cfa0 SH |
4333 | } |
4334 | ||
a96a8e8c | 4335 | /* not thread-safe, do not use from api without first forking */ |
97e9cfa0 SH |
4336 | void suggest_default_idmap(void) |
4337 | { | |
4338 | FILE *f; | |
4339 | unsigned int uid = 0, urange = 0, gid = 0, grange = 0; | |
4340 | char *line = NULL; | |
4341 | char *uname, *gname; | |
4342 | size_t len = 0; | |
4343 | ||
4344 | if (!(uname = getuname())) | |
4345 | return; | |
4346 | ||
4347 | if (!(gname = getgname())) { | |
4348 | free(uname); | |
4349 | return; | |
4350 | } | |
4351 | ||
4352 | f = fopen(subuidfile, "r"); | |
4353 | if (!f) { | |
4354 | ERROR("Your system is not configured with subuids"); | |
4355 | free(gname); | |
4356 | free(uname); | |
4357 | return; | |
4358 | } | |
4359 | while (getline(&line, &len, f) != -1) { | |
b7930180 | 4360 | size_t no_newline = 0; |
97e9cfa0 SH |
4361 | char *p = strchr(line, ':'), *p2; |
4362 | if (*line == '#') | |
4363 | continue; | |
4364 | if (!p) | |
4365 | continue; | |
4366 | *p = '\0'; | |
4367 | p++; | |
4368 | if (strcmp(line, uname)) | |
4369 | continue; | |
4370 | p2 = strchr(p, ':'); | |
4371 | if (!p2) | |
4372 | continue; | |
4373 | *p2 = '\0'; | |
4374 | p2++; | |
4375 | if (!*p2) | |
4376 | continue; | |
b7930180 CB |
4377 | no_newline = strcspn(p2, "\n"); |
4378 | p2[no_newline] = '\0'; | |
4379 | ||
b7b2fde4 CB |
4380 | if (lxc_safe_uint(p, &uid) < 0) |
4381 | WARN("Could not parse UID."); | |
4382 | if (lxc_safe_uint(p2, &urange) < 0) | |
4383 | WARN("Could not parse UID range."); | |
97e9cfa0 SH |
4384 | } |
4385 | fclose(f); | |
4386 | ||
6be7389a | 4387 | f = fopen(subgidfile, "r"); |
97e9cfa0 SH |
4388 | if (!f) { |
4389 | ERROR("Your system is not configured with subgids"); | |
4390 | free(gname); | |
4391 | free(uname); | |
4392 | return; | |
4393 | } | |
4394 | while (getline(&line, &len, f) != -1) { | |
b7930180 | 4395 | size_t no_newline = 0; |
97e9cfa0 SH |
4396 | char *p = strchr(line, ':'), *p2; |
4397 | if (*line == '#') | |
4398 | continue; | |
4399 | if (!p) | |
4400 | continue; | |
4401 | *p = '\0'; | |
4402 | p++; | |
4403 | if (strcmp(line, uname)) | |
4404 | continue; | |
4405 | p2 = strchr(p, ':'); | |
4406 | if (!p2) | |
4407 | continue; | |
4408 | *p2 = '\0'; | |
4409 | p2++; | |
4410 | if (!*p2) | |
4411 | continue; | |
b7930180 CB |
4412 | no_newline = strcspn(p2, "\n"); |
4413 | p2[no_newline] = '\0'; | |
4414 | ||
b7b2fde4 CB |
4415 | if (lxc_safe_uint(p, &gid) < 0) |
4416 | WARN("Could not parse GID."); | |
4417 | if (lxc_safe_uint(p2, &grange) < 0) | |
4418 | WARN("Could not parse GID range."); | |
97e9cfa0 SH |
4419 | } |
4420 | fclose(f); | |
4421 | ||
f10fad2f | 4422 | free(line); |
97e9cfa0 SH |
4423 | |
4424 | if (!urange || !grange) { | |
4425 | ERROR("You do not have subuids or subgids allocated"); | |
4426 | ERROR("Unprivileged containers require subuids and subgids"); | |
4427 | return; | |
4428 | } | |
4429 | ||
4430 | ERROR("You must either run as root, or define uid mappings"); | |
4431 | ERROR("To pass uid mappings to lxc-create, you could create"); | |
4432 | ERROR("~/.config/lxc/default.conf:"); | |
4433 | ERROR("lxc.include = %s", LXC_DEFAULT_CONFIG); | |
bdcbb6b3 CB |
4434 | ERROR("lxc.idmap = u 0 %u %u", uid, urange); |
4435 | ERROR("lxc.idmap = g 0 %u %u", gid, grange); | |
97e9cfa0 SH |
4436 | |
4437 | free(gname); | |
4438 | free(uname); | |
4439 | } | |
aaf26830 | 4440 | |
a7307747 SH |
4441 | static void free_cgroup_settings(struct lxc_list *result) |
4442 | { | |
4443 | struct lxc_list *iterator, *next; | |
4444 | ||
4445 | lxc_list_for_each_safe(iterator, result, next) { | |
4446 | lxc_list_del(iterator); | |
4447 | free(iterator); | |
4448 | } | |
4449 | free(result); | |
4450 | } | |
4451 | ||
aaf26830 KT |
4452 | /* |
4453 | * Return the list of cgroup_settings sorted according to the following rules | |
4454 | * 1. Put memory.limit_in_bytes before memory.memsw.limit_in_bytes | |
4455 | */ | |
4456 | struct lxc_list *sort_cgroup_settings(struct lxc_list* cgroup_settings) | |
4457 | { | |
4458 | struct lxc_list *result; | |
4459 | struct lxc_list *memsw_limit = NULL; | |
4460 | struct lxc_list *it = NULL; | |
4461 | struct lxc_cgroup *cg = NULL; | |
4462 | struct lxc_list *item = NULL; | |
4463 | ||
4464 | result = malloc(sizeof(*result)); | |
fac7c663 KT |
4465 | if (!result) { |
4466 | ERROR("failed to allocate memory to sort cgroup settings"); | |
4467 | return NULL; | |
4468 | } | |
aaf26830 KT |
4469 | lxc_list_init(result); |
4470 | ||
4471 | /*Iterate over the cgroup settings and copy them to the output list*/ | |
4472 | lxc_list_for_each(it, cgroup_settings) { | |
4473 | item = malloc(sizeof(*item)); | |
fac7c663 KT |
4474 | if (!item) { |
4475 | ERROR("failed to allocate memory to sort cgroup settings"); | |
a7307747 | 4476 | free_cgroup_settings(result); |
fac7c663 KT |
4477 | return NULL; |
4478 | } | |
aaf26830 KT |
4479 | item->elem = it->elem; |
4480 | cg = it->elem; | |
4481 | if (strcmp(cg->subsystem, "memory.memsw.limit_in_bytes") == 0) { | |
4482 | /* Store the memsw_limit location */ | |
4483 | memsw_limit = item; | |
4484 | } else if (strcmp(cg->subsystem, "memory.limit_in_bytes") == 0 && memsw_limit != NULL) { | |
4d5b72a1 | 4485 | /* lxc.cgroup.memory.memsw.limit_in_bytes is found before |
aaf26830 KT |
4486 | * lxc.cgroup.memory.limit_in_bytes, swap these two items */ |
4487 | item->elem = memsw_limit->elem; | |
4488 | memsw_limit->elem = it->elem; | |
4489 | } | |
4490 | lxc_list_add_tail(result, item); | |
4491 | } | |
4492 | ||
4493 | return result; | |
a7307747 | 4494 | } |