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