]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Daniel Lezcano <daniel.lezcano at free.fr> | |
8 | * Serge Hallyn <serge@hallyn.com> | |
9 | * Christian Brauner <christian.brauner@ubuntu.com> | |
10 | * | |
11 | * This library is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU Lesser General Public | |
13 | * License as published by the Free Software Foundation; either | |
14 | * version 2.1 of the License, or (at your option) any later version. | |
15 | * | |
16 | * This library is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * Lesser General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU Lesser General Public | |
22 | * License along with this library; if not, write to the Free Software | |
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
24 | */ | |
25 | ||
26 | #define _GNU_SOURCE | |
27 | #include "config.h" | |
28 | ||
29 | #include <alloca.h> | |
30 | #include <dirent.h> | |
31 | #include <errno.h> | |
32 | #include <fcntl.h> | |
33 | #include <grp.h> | |
34 | #include <poll.h> | |
35 | #include <pthread.h> | |
36 | #include <signal.h> | |
37 | #include <stdio.h> | |
38 | #include <stdlib.h> | |
39 | #include <string.h> | |
40 | #include <unistd.h> | |
41 | #include <sys/file.h> | |
42 | #include <sys/mount.h> | |
43 | #include <sys/param.h> | |
44 | #include <sys/prctl.h> | |
45 | #include <sys/socket.h> | |
46 | #include <sys/stat.h> | |
47 | #include <sys/syscall.h> | |
48 | #include <sys/types.h> | |
49 | #include <sys/un.h> | |
50 | #include <sys/wait.h> | |
51 | ||
52 | #if HAVE_LIBCAP | |
53 | #include <sys/capability.h> | |
54 | #endif | |
55 | ||
56 | #if !HAVE_DECL_PR_CAPBSET_DROP | |
57 | #define PR_CAPBSET_DROP 24 | |
58 | #endif | |
59 | ||
60 | #if !HAVE_DECL_PR_SET_NO_NEW_PRIVS | |
61 | #define PR_SET_NO_NEW_PRIVS 38 | |
62 | #endif | |
63 | ||
64 | #if !HAVE_DECL_PR_GET_NO_NEW_PRIVS | |
65 | #define PR_GET_NO_NEW_PRIVS 39 | |
66 | #endif | |
67 | ||
68 | #include "af_unix.h" | |
69 | #include "caps.h" | |
70 | #include "cgroup.h" | |
71 | #include "commands.h" | |
72 | #include "commands_utils.h" | |
73 | #include "conf.h" | |
74 | #include "confile_utils.h" | |
75 | #include "error.h" | |
76 | #include "list.h" | |
77 | #include "lsm/lsm.h" | |
78 | #include "log.h" | |
79 | #include "lxccontainer.h" | |
80 | #include "lxclock.h" | |
81 | #include "lxcseccomp.h" | |
82 | #include "mainloop.h" | |
83 | #include "monitor.h" | |
84 | #include "namespace.h" | |
85 | #include "network.h" | |
86 | #include "start.h" | |
87 | #include "storage/storage.h" | |
88 | #include "storage/storage_utils.h" | |
89 | #include "sync.h" | |
90 | #include "terminal.h" | |
91 | #include "utils.h" | |
92 | ||
93 | #ifndef HAVE_STRLCPY | |
94 | #include "include/strlcpy.h" | |
95 | #endif | |
96 | ||
97 | lxc_log_define(start, lxc); | |
98 | ||
99 | extern void mod_all_rdeps(struct lxc_container *c, bool inc); | |
100 | static bool do_destroy_container(struct lxc_handler *handler); | |
101 | static int lxc_rmdir_onedev_wrapper(void *data); | |
102 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
103 | const char *name); | |
104 | ||
105 | static void print_top_failing_dir(const char *path) | |
106 | { | |
107 | int ret; | |
108 | size_t len; | |
109 | char *copy, *e, *p, saved; | |
110 | ||
111 | len = strlen(path); | |
112 | copy = alloca(len + 1); | |
113 | (void)strlcpy(copy, path, len + 1); | |
114 | ||
115 | p = copy; | |
116 | e = copy + len; | |
117 | ||
118 | while (p < e) { | |
119 | while (p < e && *p == '/') | |
120 | p++; | |
121 | ||
122 | while (p < e && *p != '/') | |
123 | p++; | |
124 | ||
125 | saved = *p; | |
126 | *p = '\0'; | |
127 | ||
128 | ret = access(copy, X_OK); | |
129 | if (ret != 0) { | |
130 | SYSERROR("Could not access %s. Please grant it x " | |
131 | "access, or add an ACL for the container " | |
132 | "root", copy); | |
133 | return; | |
134 | } | |
135 | *p = saved; | |
136 | } | |
137 | } | |
138 | ||
139 | static void lxc_put_nsfds(struct lxc_handler *handler) | |
140 | { | |
141 | int i; | |
142 | ||
143 | for (i = 0; i < LXC_NS_MAX; i++) { | |
144 | if (handler->nsfd[i] < 0) | |
145 | continue; | |
146 | ||
147 | close(handler->nsfd[i]); | |
148 | handler->nsfd[i] = -EBADF; | |
149 | } | |
150 | } | |
151 | ||
152 | static int lxc_try_preserve_ns(const int pid, const char *ns) | |
153 | { | |
154 | int fd; | |
155 | ||
156 | fd = lxc_preserve_ns(pid, ns); | |
157 | if (fd < 0) { | |
158 | if (errno != ENOENT) { | |
159 | SYSERROR("Failed to preserve %s namespace", ns); | |
160 | return -EINVAL; | |
161 | } | |
162 | ||
163 | SYSWARN("Kernel does not support preserving %s namespaces", ns); | |
164 | return -EOPNOTSUPP; | |
165 | } | |
166 | ||
167 | return fd; | |
168 | } | |
169 | ||
170 | /* lxc_try_preserve_namespaces: open /proc/@pid/ns/@ns for each namespace | |
171 | * specified in ns_clone_flags. | |
172 | * Return true on success, false on failure. | |
173 | */ | |
174 | static bool lxc_try_preserve_namespaces(struct lxc_handler *handler, | |
175 | int ns_clone_flags, pid_t pid) | |
176 | { | |
177 | int i; | |
178 | ||
179 | for (i = 0; i < LXC_NS_MAX; i++) | |
180 | handler->nsfd[i] = -EBADF; | |
181 | ||
182 | for (i = 0; i < LXC_NS_MAX; i++) { | |
183 | int fd; | |
184 | ||
185 | if ((ns_clone_flags & ns_info[i].clone_flag) == 0) | |
186 | continue; | |
187 | ||
188 | fd = lxc_try_preserve_ns(pid, ns_info[i].proc_name); | |
189 | if (fd < 0) { | |
190 | handler->nsfd[i] = -EBADF; | |
191 | ||
192 | /* Do not fail to start container on kernels that do | |
193 | * not support interacting with namespaces through | |
194 | * /proc. | |
195 | */ | |
196 | if (fd == -EOPNOTSUPP) | |
197 | continue; | |
198 | ||
199 | lxc_put_nsfds(handler); | |
200 | return false; | |
201 | } | |
202 | ||
203 | handler->nsfd[i] = fd; | |
204 | DEBUG("Preserved %s namespace via fd %d", ns_info[i].proc_name, | |
205 | handler->nsfd[i]); | |
206 | } | |
207 | ||
208 | return true; | |
209 | } | |
210 | ||
211 | static inline bool match_stdfds(int fd) | |
212 | { | |
213 | return (fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO); | |
214 | } | |
215 | ||
216 | int lxc_check_inherited(struct lxc_conf *conf, bool closeall, | |
217 | int *fds_to_ignore, size_t len_fds) | |
218 | { | |
219 | int fd, fddir; | |
220 | size_t i; | |
221 | DIR *dir; | |
222 | struct dirent *direntp; | |
223 | ||
224 | if (conf && conf->close_all_fds) | |
225 | closeall = true; | |
226 | ||
227 | restart: | |
228 | dir = opendir("/proc/self/fd"); | |
229 | if (!dir) { | |
230 | SYSWARN("Failed to open directory"); | |
231 | return -1; | |
232 | } | |
233 | ||
234 | fddir = dirfd(dir); | |
235 | ||
236 | while ((direntp = readdir(dir))) { | |
237 | int ret; | |
238 | struct lxc_list *cur; | |
239 | bool matched = false; | |
240 | ||
241 | if (strcmp(direntp->d_name, ".") == 0) | |
242 | continue; | |
243 | ||
244 | if (strcmp(direntp->d_name, "..") == 0) | |
245 | continue; | |
246 | ||
247 | ret = lxc_safe_int(direntp->d_name, &fd); | |
248 | if (ret < 0) { | |
249 | INFO("Could not parse file descriptor for \"%s\"", direntp->d_name); | |
250 | continue; | |
251 | } | |
252 | ||
253 | for (i = 0; i < len_fds; i++) | |
254 | if (fds_to_ignore[i] == fd) | |
255 | break; | |
256 | ||
257 | if (fd == fddir || fd == lxc_log_fd || | |
258 | (i < len_fds && fd == fds_to_ignore[i])) | |
259 | continue; | |
260 | ||
261 | /* Keep state clients that wait on reboots. */ | |
262 | if (conf) { | |
263 | lxc_list_for_each(cur, &conf->state_clients) { | |
264 | struct lxc_state_client *client = cur->elem; | |
265 | ||
266 | if (client->clientfd != fd) | |
267 | continue; | |
268 | ||
269 | matched = true; | |
270 | break; | |
271 | } | |
272 | } | |
273 | ||
274 | if (matched) | |
275 | continue; | |
276 | ||
277 | if (current_config && fd == current_config->logfd) | |
278 | continue; | |
279 | ||
280 | if (match_stdfds(fd)) | |
281 | continue; | |
282 | ||
283 | if (closeall) { | |
284 | close(fd); | |
285 | closedir(dir); | |
286 | INFO("Closed inherited fd %d", fd); | |
287 | goto restart; | |
288 | } | |
289 | WARN("Inherited fd %d", fd); | |
290 | } | |
291 | ||
292 | /* Only enable syslog at this point to avoid the above logging function | |
293 | * to open a new fd and make the check_inherited function enter an | |
294 | * infinite loop. | |
295 | */ | |
296 | lxc_log_enable_syslog(); | |
297 | ||
298 | closedir(dir); /* cannot fail */ | |
299 | return 0; | |
300 | } | |
301 | ||
302 | static int setup_signal_fd(sigset_t *oldmask) | |
303 | { | |
304 | int ret; | |
305 | int sig; | |
306 | sigset_t mask; | |
307 | const int signals[] = {SIGBUS, SIGILL, SIGSEGV, SIGWINCH}; | |
308 | ||
309 | /* Block everything except serious error signals. */ | |
310 | ret = sigfillset(&mask); | |
311 | if (ret < 0) | |
312 | return -EBADF; | |
313 | ||
314 | for (sig = 0; sig < (sizeof(signals) / sizeof(signals[0])); sig++) { | |
315 | ret = sigdelset(&mask, signals[sig]); | |
316 | if (ret < 0) | |
317 | return -EBADF; | |
318 | } | |
319 | ||
320 | ret = pthread_sigmask(SIG_BLOCK, &mask, oldmask); | |
321 | if (ret < 0) { | |
322 | SYSERROR("Failed to set signal mask"); | |
323 | return -EBADF; | |
324 | } | |
325 | ||
326 | ret = signalfd(-1, &mask, SFD_CLOEXEC); | |
327 | if (ret < 0) { | |
328 | SYSERROR("Failed to create signal file descriptor"); | |
329 | return -EBADF; | |
330 | } | |
331 | ||
332 | TRACE("Created signal file descriptor %d", ret); | |
333 | ||
334 | return ret; | |
335 | } | |
336 | ||
337 | static int signal_handler(int fd, uint32_t events, void *data, | |
338 | struct lxc_epoll_descr *descr) | |
339 | { | |
340 | int ret; | |
341 | siginfo_t info; | |
342 | struct signalfd_siginfo siginfo; | |
343 | struct lxc_handler *hdlr = data; | |
344 | ||
345 | ret = lxc_read_nointr(fd, &siginfo, sizeof(siginfo)); | |
346 | if (ret < 0) { | |
347 | ERROR("Failed to read signal info from signal file descriptor %d", fd); | |
348 | return LXC_MAINLOOP_ERROR; | |
349 | } | |
350 | ||
351 | if (ret != sizeof(siginfo)) { | |
352 | ERROR("Unexpected size for struct signalfd_siginfo"); | |
353 | return -EINVAL; | |
354 | } | |
355 | ||
356 | /* Check whether init is running. */ | |
357 | info.si_pid = 0; | |
358 | ret = waitid(P_PID, hdlr->pid, &info, WEXITED | WNOWAIT | WNOHANG); | |
359 | if (ret == 0 && info.si_pid == hdlr->pid) | |
360 | hdlr->init_died = true; | |
361 | ||
362 | /* Try to figure out a reasonable exit status to report. */ | |
363 | if (hdlr->init_died) { | |
364 | switch (info.si_code) { | |
365 | case CLD_EXITED: | |
366 | hdlr->exit_status = info.si_status << 8; | |
367 | break; | |
368 | case CLD_KILLED: | |
369 | case CLD_DUMPED: | |
370 | case CLD_STOPPED: | |
371 | hdlr->exit_status = info.si_status << 8 | 0x7f; | |
372 | break; | |
373 | case CLD_CONTINUED: | |
374 | /* Huh? The waitid() told us it's dead *and* continued? */ | |
375 | WARN("Init %d dead and continued?", hdlr->pid); | |
376 | hdlr->exit_status = 1; | |
377 | break; | |
378 | default: | |
379 | ERROR("Unknown si_code: %d", info.si_code); | |
380 | hdlr->exit_status = 1; | |
381 | } | |
382 | } | |
383 | ||
384 | if (siginfo.ssi_signo == SIGHUP) { | |
385 | kill(hdlr->pid, SIGTERM); | |
386 | INFO("Killing %d since terminal hung up", hdlr->pid); | |
387 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE : LXC_MAINLOOP_CONTINUE; | |
388 | } | |
389 | ||
390 | if (siginfo.ssi_signo != SIGCHLD) { | |
391 | kill(hdlr->pid, siginfo.ssi_signo); | |
392 | INFO("Forwarded signal %d to pid %d", siginfo.ssi_signo, hdlr->pid); | |
393 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE : LXC_MAINLOOP_CONTINUE; | |
394 | } | |
395 | ||
396 | /* More robustness, protect ourself from a SIGCHLD sent | |
397 | * by a process different from the container init. | |
398 | */ | |
399 | if (siginfo.ssi_pid != hdlr->pid) { | |
400 | NOTICE("Received %d from pid %d instead of container init %d", | |
401 | siginfo.ssi_signo, siginfo.ssi_pid, hdlr->pid); | |
402 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE : LXC_MAINLOOP_CONTINUE; | |
403 | } | |
404 | ||
405 | if (siginfo.ssi_code == CLD_STOPPED) { | |
406 | INFO("Container init process was stopped"); | |
407 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE : LXC_MAINLOOP_CONTINUE; | |
408 | } else if (siginfo.ssi_code == CLD_CONTINUED) { | |
409 | INFO("Container init process was continued"); | |
410 | return hdlr->init_died ? LXC_MAINLOOP_CLOSE : LXC_MAINLOOP_CONTINUE; | |
411 | } | |
412 | ||
413 | DEBUG("Container init process %d exited", hdlr->pid); | |
414 | return LXC_MAINLOOP_CLOSE; | |
415 | } | |
416 | ||
417 | int lxc_serve_state_clients(const char *name, struct lxc_handler *handler, | |
418 | lxc_state_t state) | |
419 | { | |
420 | size_t retlen; | |
421 | ssize_t ret; | |
422 | struct lxc_list *cur, *next; | |
423 | struct lxc_state_client *client; | |
424 | struct lxc_msg msg = {.type = lxc_msg_state, .value = state}; | |
425 | ||
426 | if (state == THAWED) | |
427 | handler->state = RUNNING; | |
428 | else | |
429 | handler->state = state; | |
430 | ||
431 | TRACE("Set container state to %s", lxc_state2str(state)); | |
432 | ||
433 | if (lxc_list_empty(&handler->conf->state_clients)) { | |
434 | TRACE("No state clients registered"); | |
435 | return 0; | |
436 | } | |
437 | ||
438 | retlen = strlcpy(msg.name, name, sizeof(msg.name)); | |
439 | if (retlen >= sizeof(msg.name)) | |
440 | return -E2BIG; | |
441 | ||
442 | lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) { | |
443 | client = cur->elem; | |
444 | ||
445 | if (client->states[state] == 0) { | |
446 | TRACE("State %s not registered for state client %d", | |
447 | lxc_state2str(state), client->clientfd); | |
448 | continue; | |
449 | } | |
450 | ||
451 | TRACE("Sending state %s to state client %d", | |
452 | lxc_state2str(state), client->clientfd); | |
453 | ||
454 | again: | |
455 | ret = send(client->clientfd, &msg, sizeof(msg), MSG_NOSIGNAL); | |
456 | if (ret <= 0) { | |
457 | if (errno == EINTR) { | |
458 | TRACE("Caught EINTR; retrying"); | |
459 | goto again; | |
460 | } | |
461 | ||
462 | SYSERROR("Failed to send message to client"); | |
463 | } | |
464 | ||
465 | /* kick client from list */ | |
466 | lxc_list_del(cur); | |
467 | close(client->clientfd); | |
468 | free(cur->elem); | |
469 | free(cur); | |
470 | } | |
471 | ||
472 | return 0; | |
473 | } | |
474 | ||
475 | static int lxc_serve_state_socket_pair(const char *name, | |
476 | struct lxc_handler *handler, | |
477 | lxc_state_t state) | |
478 | { | |
479 | ssize_t ret; | |
480 | ||
481 | if (!handler->backgrounded || | |
482 | handler->state_socket_pair[1] < 0 || | |
483 | state == STARTING) | |
484 | return 0; | |
485 | ||
486 | /* Close read end of the socket pair. */ | |
487 | close(handler->state_socket_pair[0]); | |
488 | handler->state_socket_pair[0] = -1; | |
489 | ||
490 | again: | |
491 | ret = lxc_abstract_unix_send_credential(handler->state_socket_pair[1], | |
492 | &(int){state}, sizeof(int)); | |
493 | if (ret < 0) { | |
494 | SYSERROR("Failed to send state to %d", handler->state_socket_pair[1]); | |
495 | ||
496 | if (errno == EINTR) | |
497 | goto again; | |
498 | ||
499 | return -1; | |
500 | } | |
501 | ||
502 | if (ret != sizeof(int)) { | |
503 | ERROR("Message too long : %d", handler->state_socket_pair[1]); | |
504 | return -1; | |
505 | } | |
506 | ||
507 | TRACE("Sent container state \"%s\" to %d", lxc_state2str(state), | |
508 | handler->state_socket_pair[1]); | |
509 | ||
510 | /* Close write end of the socket pair. */ | |
511 | close(handler->state_socket_pair[1]); | |
512 | handler->state_socket_pair[1] = -1; | |
513 | ||
514 | return 0; | |
515 | } | |
516 | ||
517 | int lxc_set_state(const char *name, struct lxc_handler *handler, | |
518 | lxc_state_t state) | |
519 | { | |
520 | int ret; | |
521 | ||
522 | ret = lxc_serve_state_socket_pair(name, handler, state); | |
523 | if (ret < 0) { | |
524 | ERROR("Failed to synchronize via anonymous pair of unix sockets"); | |
525 | return -1; | |
526 | } | |
527 | ||
528 | ret = lxc_serve_state_clients(name, handler, state); | |
529 | if (ret < 0) | |
530 | return -1; | |
531 | ||
532 | /* This function will try to connect to the legacy lxc-monitord state | |
533 | * server and only exists for backwards compatibility. | |
534 | */ | |
535 | lxc_monitor_send_state(name, state, handler->lxcpath); | |
536 | ||
537 | return 0; | |
538 | } | |
539 | ||
540 | int lxc_poll(const char *name, struct lxc_handler *handler) | |
541 | { | |
542 | int ret; | |
543 | bool has_console = true; | |
544 | struct lxc_epoll_descr descr, descr_console; | |
545 | ||
546 | if (handler->conf->console.path && | |
547 | strcmp(handler->conf->console.path, "none") == 0) | |
548 | has_console = false; | |
549 | ||
550 | ret = lxc_mainloop_open(&descr); | |
551 | if (ret < 0) { | |
552 | ERROR("Failed to create mainloop"); | |
553 | goto out_sigfd; | |
554 | } | |
555 | ||
556 | if (has_console) { | |
557 | ret = lxc_mainloop_open(&descr_console); | |
558 | if (ret < 0) { | |
559 | ERROR("Failed to create console mainloop"); | |
560 | goto out_mainloop; | |
561 | } | |
562 | } | |
563 | ||
564 | ret = lxc_mainloop_add_handler(&descr, handler->sigfd, signal_handler, handler); | |
565 | if (ret < 0) { | |
566 | ERROR("Failed to add signal handler for %d to mainloop", handler->sigfd); | |
567 | goto out_mainloop_console; | |
568 | } | |
569 | ||
570 | if (has_console) { | |
571 | struct lxc_terminal *console = &handler->conf->console; | |
572 | ||
573 | ret = lxc_terminal_mainloop_add(&descr, console); | |
574 | if (ret < 0) { | |
575 | ERROR("Failed to add console handlers to mainloop"); | |
576 | goto out_mainloop_console; | |
577 | } | |
578 | ||
579 | ret = lxc_terminal_mainloop_add(&descr_console, console); | |
580 | if (ret < 0) { | |
581 | ERROR("Failed to add console handlers to console mainloop"); | |
582 | goto out_mainloop_console; | |
583 | } | |
584 | ||
585 | handler->conf->console.descr = &descr; | |
586 | } | |
587 | ||
588 | ret = lxc_cmd_mainloop_add(name, &descr, handler); | |
589 | if (ret < 0) { | |
590 | ERROR("Failed to add command handler to mainloop"); | |
591 | goto out_mainloop_console; | |
592 | } | |
593 | ||
594 | TRACE("Mainloop is ready"); | |
595 | ||
596 | ret = lxc_mainloop(&descr, -1); | |
597 | close(descr.epfd); | |
598 | descr.epfd = -EBADF; | |
599 | if (ret < 0 || !handler->init_died) | |
600 | goto out_mainloop_console; | |
601 | ||
602 | if (has_console) | |
603 | ret = lxc_mainloop(&descr_console, 0); | |
604 | ||
605 | out_mainloop_console: | |
606 | if (has_console) { | |
607 | lxc_mainloop_close(&descr_console); | |
608 | TRACE("Closed console mainloop"); | |
609 | } | |
610 | ||
611 | out_mainloop: | |
612 | lxc_mainloop_close(&descr); | |
613 | TRACE("Closed mainloop"); | |
614 | ||
615 | out_sigfd: | |
616 | close(handler->sigfd); | |
617 | TRACE("Closed signal file descriptor %d", handler->sigfd); | |
618 | handler->sigfd = -EBADF; | |
619 | ||
620 | return ret; | |
621 | } | |
622 | ||
623 | void lxc_zero_handler(struct lxc_handler *handler) | |
624 | { | |
625 | int i; | |
626 | ||
627 | memset(handler, 0, sizeof(struct lxc_handler)); | |
628 | ||
629 | handler->pinfd = -1; | |
630 | ||
631 | handler->sigfd = -1; | |
632 | ||
633 | for (i = 0; i < LXC_NS_MAX; i++) | |
634 | handler->nsfd[i] = -1; | |
635 | ||
636 | handler->data_sock[0] = -1; | |
637 | handler->data_sock[1] = -1; | |
638 | ||
639 | handler->state_socket_pair[0] = -1; | |
640 | handler->state_socket_pair[1] = -1; | |
641 | ||
642 | handler->sync_sock[0] = -1; | |
643 | handler->sync_sock[1] = -1; | |
644 | } | |
645 | ||
646 | void lxc_free_handler(struct lxc_handler *handler) | |
647 | { | |
648 | if (handler->pinfd >= 0) | |
649 | close(handler->pinfd); | |
650 | ||
651 | if (handler->sigfd >= 0) | |
652 | close(handler->sigfd); | |
653 | ||
654 | lxc_put_nsfds(handler); | |
655 | ||
656 | if (handler->conf && handler->conf->reboot == REBOOT_NONE) | |
657 | if (handler->conf->maincmd_fd >= 0) | |
658 | lxc_abstract_unix_close(handler->conf->maincmd_fd); | |
659 | ||
660 | if (handler->state_socket_pair[0] >= 0) | |
661 | close(handler->state_socket_pair[0]); | |
662 | ||
663 | if (handler->state_socket_pair[1] >= 0) | |
664 | close(handler->state_socket_pair[1]); | |
665 | ||
666 | handler->conf = NULL; | |
667 | free(handler); | |
668 | handler = NULL; | |
669 | } | |
670 | ||
671 | struct lxc_handler *lxc_init_handler(const char *name, struct lxc_conf *conf, | |
672 | const char *lxcpath, bool daemonize) | |
673 | { | |
674 | int i, ret; | |
675 | struct lxc_handler *handler; | |
676 | ||
677 | handler = malloc(sizeof(*handler)); | |
678 | if (!handler) | |
679 | return NULL; | |
680 | ||
681 | memset(handler, 0, sizeof(*handler)); | |
682 | ||
683 | /* Note that am_guest_unpriv() checks the effective uid. We | |
684 | * probably don't care if we are real root only if we are running | |
685 | * as root so this should be fine. | |
686 | */ | |
687 | handler->am_root = !am_guest_unpriv(); | |
688 | handler->data_sock[0] = handler->data_sock[1] = -1; | |
689 | handler->conf = conf; | |
690 | handler->lxcpath = lxcpath; | |
691 | handler->pinfd = -1; | |
692 | handler->sigfd = -EBADF; | |
693 | handler->init_died = false; | |
694 | handler->state_socket_pair[0] = handler->state_socket_pair[1] = -1; | |
695 | if (handler->conf->reboot == REBOOT_NONE) | |
696 | lxc_list_init(&handler->conf->state_clients); | |
697 | ||
698 | for (i = 0; i < LXC_NS_MAX; i++) | |
699 | handler->nsfd[i] = -1; | |
700 | ||
701 | handler->name = name; | |
702 | ||
703 | if (daemonize && handler->conf->reboot == REBOOT_NONE) { | |
704 | /* Create socketpair() to synchronize on daemonized startup. | |
705 | * When the container reboots we don't need to synchronize | |
706 | * again currently so don't open another socketpair(). | |
707 | */ | |
708 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, | |
709 | handler->state_socket_pair); | |
710 | if (ret < 0) { | |
711 | ERROR("Failed to create anonymous pair of unix sockets"); | |
712 | goto on_error; | |
713 | } | |
714 | ||
715 | TRACE("Created anonymous pair {%d,%d} of unix sockets", | |
716 | handler->state_socket_pair[0], | |
717 | handler->state_socket_pair[1]); | |
718 | } | |
719 | ||
720 | if (handler->conf->reboot == REBOOT_NONE) { | |
721 | handler->conf->maincmd_fd = lxc_cmd_init(name, lxcpath, "command"); | |
722 | if (handler->conf->maincmd_fd < 0) { | |
723 | ERROR("Failed to set up command socket"); | |
724 | goto on_error; | |
725 | } | |
726 | } | |
727 | ||
728 | TRACE("Unix domain socket %d for command server is ready", | |
729 | handler->conf->maincmd_fd); | |
730 | ||
731 | return handler; | |
732 | ||
733 | on_error: | |
734 | lxc_free_handler(handler); | |
735 | ||
736 | return NULL; | |
737 | } | |
738 | ||
739 | int lxc_init(const char *name, struct lxc_handler *handler) | |
740 | { | |
741 | int ret; | |
742 | const char *loglevel; | |
743 | struct lxc_conf *conf = handler->conf; | |
744 | ||
745 | lsm_init(); | |
746 | TRACE("Initialized LSM"); | |
747 | ||
748 | ret = lxc_read_seccomp_config(conf); | |
749 | if (ret < 0) { | |
750 | ERROR("Failed loading seccomp policy"); | |
751 | goto out_close_maincmd_fd; | |
752 | } | |
753 | TRACE("Read seccomp policy"); | |
754 | ||
755 | /* Begin by setting the state to STARTING. */ | |
756 | ret = lxc_set_state(name, handler, STARTING); | |
757 | if (ret < 0) { | |
758 | ERROR("Failed to set state to \"%s\"", lxc_state2str(STARTING)); | |
759 | goto out_close_maincmd_fd; | |
760 | } | |
761 | TRACE("Set container state to \"STARTING\""); | |
762 | ||
763 | /* Start of environment variable setup for hooks. */ | |
764 | ret = setenv("LXC_NAME", name, 1); | |
765 | if (ret < 0) | |
766 | SYSERROR("Failed to set environment variable: LXC_NAME=%s", name); | |
767 | ||
768 | if (conf->rcfile) { | |
769 | ret = setenv("LXC_CONFIG_FILE", conf->rcfile, 1); | |
770 | if (ret < 0) | |
771 | SYSERROR("Failed to set environment variable: " | |
772 | "LXC_CONFIG_FILE=%s", conf->rcfile); | |
773 | } | |
774 | ||
775 | if (conf->rootfs.mount) { | |
776 | ret = setenv("LXC_ROOTFS_MOUNT", conf->rootfs.mount, 1); | |
777 | if (ret < 0) | |
778 | SYSERROR("Failed to set environment variable: " | |
779 | "LXC_ROOTFS_MOUNT=%s", conf->rootfs.mount); | |
780 | } | |
781 | ||
782 | if (conf->rootfs.path) { | |
783 | ret = setenv("LXC_ROOTFS_PATH", conf->rootfs.path, 1); | |
784 | if (ret < 0) | |
785 | SYSERROR("Failed to set environment variable: " | |
786 | "LXC_ROOTFS_PATH=%s", conf->rootfs.path); | |
787 | } | |
788 | ||
789 | if (conf->console.path) { | |
790 | ret = setenv("LXC_CONSOLE", conf->console.path, 1); | |
791 | if (ret < 0) | |
792 | SYSERROR("Failed to set environment variable: " | |
793 | "LXC_CONSOLE=%s", conf->console.path); | |
794 | } | |
795 | ||
796 | if (conf->console.log_path) { | |
797 | ret = setenv("LXC_CONSOLE_LOGPATH", conf->console.log_path, 1); | |
798 | if (ret < 0) | |
799 | SYSERROR("Failed to set environment variable: " | |
800 | "LXC_CONSOLE_LOGPATH=%s", conf->console.log_path); | |
801 | } | |
802 | ||
803 | if (cgns_supported()) { | |
804 | ret = setenv("LXC_CGNS_AWARE", "1", 1); | |
805 | if (ret < 0) | |
806 | SYSERROR("Failed to set environment variable " | |
807 | "LXC_CGNS_AWARE=1"); | |
808 | } | |
809 | ||
810 | loglevel = lxc_log_priority_to_string(lxc_log_get_level()); | |
811 | ret = setenv("LXC_LOG_LEVEL", loglevel, 1); | |
812 | if (ret < 0) | |
813 | SYSERROR("Set environment variable LXC_LOG_LEVEL=%s", | |
814 | loglevel); | |
815 | ||
816 | if (conf->hooks_version == 0) | |
817 | ret = setenv("LXC_HOOK_VERSION", "0", 1); | |
818 | else | |
819 | ret = setenv("LXC_HOOK_VERSION", "1", 1); | |
820 | if (ret < 0) | |
821 | SYSERROR("Failed to set environment variable LXC_HOOK_VERSION=%u", conf->hooks_version); | |
822 | /* End of environment variable setup for hooks. */ | |
823 | ||
824 | TRACE("Set environment variables"); | |
825 | ||
826 | ret = run_lxc_hooks(name, "pre-start", conf, NULL); | |
827 | if (ret < 0) { | |
828 | ERROR("Failed to run lxc.hook.pre-start for container \"%s\"", name); | |
829 | goto out_aborting; | |
830 | } | |
831 | TRACE("Ran pre-start hooks"); | |
832 | ||
833 | /* The signal fd has to be created before forking otherwise if the child | |
834 | * process exits before we setup the signal fd, the event will be lost | |
835 | * and the command will be stuck. | |
836 | */ | |
837 | handler->sigfd = setup_signal_fd(&handler->oldmask); | |
838 | if (handler->sigfd < 0) { | |
839 | ERROR("Failed to setup SIGCHLD fd handler."); | |
840 | goto out_delete_tty; | |
841 | } | |
842 | TRACE("Set up signal fd"); | |
843 | ||
844 | /* Do this after setting up signals since it might unblock SIGWINCH. */ | |
845 | ret = lxc_terminal_setup(conf); | |
846 | if (ret < 0) { | |
847 | ERROR("Failed to create console"); | |
848 | goto out_restore_sigmask; | |
849 | } | |
850 | TRACE("Created console"); | |
851 | ||
852 | ret = lxc_terminal_map_ids(conf, &conf->console); | |
853 | if (ret < 0) { | |
854 | ERROR("Failed to chown console"); | |
855 | goto out_delete_terminal; | |
856 | } | |
857 | TRACE("Chowned console"); | |
858 | ||
859 | handler->cgroup_ops = cgroup_init(handler); | |
860 | if (!handler->cgroup_ops) { | |
861 | ERROR("Failed to initialize cgroup driver"); | |
862 | goto out_delete_terminal; | |
863 | } | |
864 | TRACE("Initialized cgroup driver"); | |
865 | ||
866 | ret = lsm_process_prepare(conf, handler->lxcpath); | |
867 | if (ret < 0) { | |
868 | ERROR("Failed to initialize LSM"); | |
869 | goto out_destroy_cgroups; | |
870 | } | |
871 | TRACE("Initialized LSM"); | |
872 | ||
873 | INFO("Container \"%s\" is initialized", name); | |
874 | return 0; | |
875 | ||
876 | out_destroy_cgroups: | |
877 | handler->cgroup_ops->destroy(handler->cgroup_ops, handler); | |
878 | ||
879 | out_delete_terminal: | |
880 | lxc_terminal_delete(&handler->conf->console); | |
881 | ||
882 | out_restore_sigmask: | |
883 | (void)pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL); | |
884 | ||
885 | out_delete_tty: | |
886 | lxc_delete_tty(&conf->ttys); | |
887 | ||
888 | out_aborting: | |
889 | (void)lxc_set_state(name, handler, ABORTING); | |
890 | ||
891 | out_close_maincmd_fd: | |
892 | lxc_abstract_unix_close(conf->maincmd_fd); | |
893 | conf->maincmd_fd = -1; | |
894 | return -1; | |
895 | } | |
896 | ||
897 | void lxc_fini(const char *name, struct lxc_handler *handler) | |
898 | { | |
899 | int i, ret; | |
900 | pid_t self; | |
901 | struct lxc_list *cur, *next; | |
902 | char *namespaces[LXC_NS_MAX + 1]; | |
903 | size_t namespace_count = 0; | |
904 | struct cgroup_ops *cgroup_ops = handler->cgroup_ops; | |
905 | ||
906 | /* The STOPPING state is there for future cleanup code which can take | |
907 | * awhile. | |
908 | */ | |
909 | lxc_set_state(name, handler, STOPPING); | |
910 | ||
911 | self = lxc_raw_getpid(); | |
912 | for (i = 0; i < LXC_NS_MAX; i++) { | |
913 | if (handler->nsfd[i] < 0) | |
914 | continue; | |
915 | ||
916 | if (handler->conf->hooks_version == 0) | |
917 | ret = asprintf(&namespaces[namespace_count], | |
918 | "%s:/proc/%d/fd/%d", ns_info[i].proc_name, | |
919 | self, handler->nsfd[i]); | |
920 | else | |
921 | ret = asprintf(&namespaces[namespace_count], | |
922 | "/proc/%d/fd/%d", self, handler->nsfd[i]); | |
923 | if (ret == -1) { | |
924 | SYSERROR("Failed to allocate memory"); | |
925 | break; | |
926 | } | |
927 | ||
928 | if (handler->conf->hooks_version == 0) { | |
929 | namespace_count++; | |
930 | continue; | |
931 | } | |
932 | ||
933 | ret = setenv(ns_info[i].env_name, namespaces[namespace_count], 1); | |
934 | if (ret < 0) | |
935 | SYSERROR("Failed to set environment variable %s=%s", | |
936 | ns_info[i].env_name, namespaces[namespace_count]); | |
937 | else | |
938 | TRACE("Set environment variable %s=%s", | |
939 | ns_info[i].env_name, namespaces[namespace_count]); | |
940 | ||
941 | namespace_count++; | |
942 | } | |
943 | namespaces[namespace_count] = NULL; | |
944 | ||
945 | if (handler->conf->reboot > REBOOT_NONE) { | |
946 | ret = setenv("LXC_TARGET", "reboot", 1); | |
947 | if (ret < 0) | |
948 | SYSERROR("Failed to set environment variable: " | |
949 | "LXC_TARGET=reboot"); | |
950 | } | |
951 | ||
952 | if (handler->conf->reboot == REBOOT_NONE) { | |
953 | ret = setenv("LXC_TARGET", "stop", 1); | |
954 | if (ret < 0) | |
955 | SYSERROR("Failed to set environment variable: " | |
956 | "LXC_TARGET=stop"); | |
957 | } | |
958 | ||
959 | if (handler->conf->hooks_version == 0) | |
960 | ret = run_lxc_hooks(name, "stop", handler->conf, namespaces); | |
961 | else | |
962 | ret = run_lxc_hooks(name, "stop", handler->conf, NULL); | |
963 | if (ret < 0) | |
964 | ERROR("Failed to run \"lxc.hook.stop\" hook"); | |
965 | ||
966 | while (namespace_count--) | |
967 | free(namespaces[namespace_count]); | |
968 | ||
969 | lsm_process_cleanup(handler->conf, handler->lxcpath); | |
970 | ||
971 | cgroup_ops->destroy(cgroup_ops, handler); | |
972 | cgroup_exit(cgroup_ops); | |
973 | ||
974 | if (handler->conf->reboot == REBOOT_NONE) { | |
975 | /* For all new state clients simply close the command socket. | |
976 | * This will inform all state clients that the container is | |
977 | * STOPPED and also prevents a race between a open()/close() on | |
978 | * the command socket causing a new process to get ECONNREFUSED | |
979 | * because we haven't yet closed the command socket. | |
980 | */ | |
981 | lxc_abstract_unix_close(handler->conf->maincmd_fd); | |
982 | handler->conf->maincmd_fd = -1; | |
983 | TRACE("Closed command socket"); | |
984 | ||
985 | /* This function will try to connect to the legacy lxc-monitord | |
986 | * state server and only exists for backwards compatibility. | |
987 | */ | |
988 | lxc_monitor_send_state(name, STOPPED, handler->lxcpath); | |
989 | ||
990 | /* The command socket is closed so no one can acces the command | |
991 | * socket anymore so there's no need to lock it. | |
992 | */ | |
993 | handler->state = STOPPED; | |
994 | TRACE("Set container state to \"STOPPED\""); | |
995 | } else { | |
996 | lxc_set_state(name, handler, STOPPED); | |
997 | } | |
998 | ||
999 | ret = run_lxc_hooks(name, "post-stop", handler->conf, NULL); | |
1000 | if (ret < 0) { | |
1001 | ERROR("Failed to run lxc.hook.post-stop for container \"%s\"", name); | |
1002 | if (handler->conf->reboot > REBOOT_NONE) { | |
1003 | WARN("Container will be stopped instead of rebooted"); | |
1004 | handler->conf->reboot = REBOOT_NONE; | |
1005 | ||
1006 | ret = setenv("LXC_TARGET", "stop", 1); | |
1007 | if (ret < 0) | |
1008 | WARN("Failed to set environment variable: " | |
1009 | "LXC_TARGET=stop"); | |
1010 | } | |
1011 | } | |
1012 | ||
1013 | /* Reset mask set by setup_signal_fd. */ | |
1014 | ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL); | |
1015 | if (ret < 0) | |
1016 | SYSWARN("Failed to restore signal mask"); | |
1017 | ||
1018 | lxc_terminal_delete(&handler->conf->console); | |
1019 | lxc_delete_tty(&handler->conf->ttys); | |
1020 | ||
1021 | /* The command socket is now closed, no more state clients can register | |
1022 | * themselves from now on. So free the list of state clients. | |
1023 | */ | |
1024 | lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) { | |
1025 | struct lxc_state_client *client = cur->elem; | |
1026 | ||
1027 | /* Keep state clients that want to be notified about reboots. */ | |
1028 | if ((handler->conf->reboot > REBOOT_NONE) && | |
1029 | (client->states[RUNNING] == 2)) | |
1030 | continue; | |
1031 | ||
1032 | /* close state client socket */ | |
1033 | lxc_list_del(cur); | |
1034 | close(client->clientfd); | |
1035 | free(cur->elem); | |
1036 | free(cur); | |
1037 | } | |
1038 | ||
1039 | if (handler->conf->ephemeral == 1 && handler->conf->reboot != REBOOT_REQ) | |
1040 | lxc_destroy_container_on_signal(handler, name); | |
1041 | ||
1042 | lxc_free_handler(handler); | |
1043 | } | |
1044 | ||
1045 | void lxc_abort(const char *name, struct lxc_handler *handler) | |
1046 | { | |
1047 | int ret, status; | |
1048 | ||
1049 | lxc_set_state(name, handler, ABORTING); | |
1050 | ||
1051 | if (handler->pid > 0) { | |
1052 | ret = kill(handler->pid, SIGKILL); | |
1053 | if (ret < 0) | |
1054 | SYSERROR("Failed to send SIGKILL to %d", handler->pid); | |
1055 | } | |
1056 | ||
1057 | while ((ret = waitpid(-1, &status, 0)) > 0) { | |
1058 | ; | |
1059 | } | |
1060 | } | |
1061 | ||
1062 | static int do_start(void *data) | |
1063 | { | |
1064 | int ret; | |
1065 | char path[PATH_MAX]; | |
1066 | bool have_cap_setgid; | |
1067 | uid_t new_uid; | |
1068 | gid_t new_gid; | |
1069 | struct lxc_list *iterator; | |
1070 | int devnull_fd = -1; | |
1071 | struct lxc_handler *handler = data; | |
1072 | ||
1073 | lxc_sync_fini_parent(handler); | |
1074 | ||
1075 | /* This prctl must be before the synchro, so if the parent dies before | |
1076 | * we set the parent death signal, we will detect its death with the | |
1077 | * synchro right after, otherwise we have a window where the parent can | |
1078 | * exit before we set the pdeath signal leading to a unsupervized | |
1079 | * container. | |
1080 | */ | |
1081 | ret = lxc_set_death_signal(SIGKILL); | |
1082 | if (ret < 0) { | |
1083 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL"); | |
1084 | goto out_warn_father; | |
1085 | } | |
1086 | ||
1087 | ret = lxc_ambient_caps_up(); | |
1088 | if (ret < 0) { | |
1089 | ERROR("Failed to raise ambient capabilities"); | |
1090 | goto out_warn_father; | |
1091 | } | |
1092 | ||
1093 | ret = pthread_sigmask(SIG_SETMASK, &handler->oldmask, NULL); | |
1094 | if (ret < 0) { | |
1095 | SYSERROR("Failed to set signal mask"); | |
1096 | goto out_warn_father; | |
1097 | } | |
1098 | ||
1099 | /* Don't leak the pinfd to the container. */ | |
1100 | if (handler->pinfd >= 0) | |
1101 | close(handler->pinfd); | |
1102 | ||
1103 | ret = lxc_sync_wait_parent(handler, LXC_SYNC_STARTUP); | |
1104 | if (ret < 0) | |
1105 | goto out_warn_father; | |
1106 | ||
1107 | /* Unshare CLONE_NEWNET after CLONE_NEWUSER. See | |
1108 | * https://github.com/lxc/lxd/issues/1978. | |
1109 | */ | |
1110 | if ((handler->ns_clone_flags & (CLONE_NEWNET | CLONE_NEWUSER)) == | |
1111 | (CLONE_NEWNET | CLONE_NEWUSER)) { | |
1112 | ret = unshare(CLONE_NEWNET); | |
1113 | if (ret < 0) { | |
1114 | SYSERROR("Failed to unshare CLONE_NEWNET"); | |
1115 | goto out_warn_father; | |
1116 | } | |
1117 | INFO("Unshared CLONE_NEWNET"); | |
1118 | } | |
1119 | ||
1120 | /* Tell the parent task it can begin to configure the container and wait | |
1121 | * for it to finish. | |
1122 | */ | |
1123 | ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CONFIGURE); | |
1124 | if (ret < 0) | |
1125 | goto out_error; | |
1126 | ||
1127 | ret = lxc_network_recv_veth_names_from_parent(handler); | |
1128 | if (ret < 0) { | |
1129 | ERROR("Failed to receive veth names from parent"); | |
1130 | goto out_warn_father; | |
1131 | } | |
1132 | ||
1133 | /* If we are in a new user namespace, become root there to have | |
1134 | * privilege over our namespace. | |
1135 | */ | |
1136 | if (!lxc_list_empty(&handler->conf->id_map)) { | |
1137 | uid_t nsuid = (handler->conf->root_nsuid_map != NULL) | |
1138 | ? 0 | |
1139 | : handler->conf->init_uid; | |
1140 | gid_t nsgid = (handler->conf->root_nsgid_map != NULL) | |
1141 | ? 0 | |
1142 | : handler->conf->init_gid; | |
1143 | ||
1144 | ret = lxc_switch_uid_gid(nsuid, nsgid); | |
1145 | if (ret < 0) | |
1146 | goto out_warn_father; | |
1147 | ||
1148 | /* Drop groups only after we switched to a valid gid in the new | |
1149 | * user namespace. | |
1150 | */ | |
1151 | ret = lxc_setgroups(0, NULL); | |
1152 | if (ret < 0 && (handler->am_root || errno != EPERM)) | |
1153 | goto out_warn_father; | |
1154 | ||
1155 | ret = prctl(PR_SET_DUMPABLE, prctl_arg(1), prctl_arg(0), | |
1156 | prctl_arg(0), prctl_arg(0)); | |
1157 | if (ret < 0) | |
1158 | goto out_warn_father; | |
1159 | ||
1160 | /* set{g,u}id() clears deathsignal */ | |
1161 | ret = lxc_set_death_signal(SIGKILL); | |
1162 | if (ret < 0) { | |
1163 | SYSERROR("Failed to set PR_SET_PDEATHSIG to SIGKILL"); | |
1164 | goto out_warn_father; | |
1165 | } | |
1166 | } | |
1167 | ||
1168 | ret = access(handler->lxcpath, X_OK); | |
1169 | if (ret != 0) { | |
1170 | print_top_failing_dir(handler->lxcpath); | |
1171 | goto out_warn_father; | |
1172 | } | |
1173 | ||
1174 | ret = snprintf(path, sizeof(path), "%s/dev/null", | |
1175 | handler->conf->rootfs.mount); | |
1176 | if (ret < 0 || ret >= sizeof(path)) | |
1177 | goto out_warn_father; | |
1178 | ||
1179 | /* In order to checkpoint restore, we need to have everything in the | |
1180 | * same mount namespace. However, some containers may not have a | |
1181 | * reasonable /dev (in particular, they may not have /dev/null), so we | |
1182 | * can't set init's std fds to /dev/null by opening it from inside the | |
1183 | * container. | |
1184 | * | |
1185 | * If that's the case, fall back to using the host's /dev/null. This | |
1186 | * means that migration won't work, but at least we won't spew output | |
1187 | * where it isn't wanted. | |
1188 | */ | |
1189 | if (handler->backgrounded && !handler->conf->autodev) { | |
1190 | ret = access(path, F_OK); | |
1191 | if (ret != 0) { | |
1192 | devnull_fd = open_devnull(); | |
1193 | ||
1194 | if (devnull_fd < 0) | |
1195 | goto out_warn_father; | |
1196 | WARN("Using /dev/null from the host for container " | |
1197 | "init's standard file descriptors. Migration will " | |
1198 | "not work"); | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | /* Ask father to setup cgroups and wait for him to finish. */ | |
1203 | ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP); | |
1204 | if (ret < 0) | |
1205 | goto out_error; | |
1206 | ||
1207 | /* Unshare cgroup namespace after we have setup our cgroups. If we do it | |
1208 | * earlier we end up with a wrong view of /proc/self/cgroup. For | |
1209 | * example, assume we unshare(CLONE_NEWCGROUP) first, and then create | |
1210 | * the cgroup for the container, say /sys/fs/cgroup/cpuset/lxc/c, then | |
1211 | * /proc/self/cgroup would show us: | |
1212 | * | |
1213 | * 8:cpuset:/lxc/c | |
1214 | * | |
1215 | * whereas it should actually show | |
1216 | * | |
1217 | * 8:cpuset:/ | |
1218 | */ | |
1219 | if (handler->ns_clone_flags & CLONE_NEWCGROUP) { | |
1220 | ret = unshare(CLONE_NEWCGROUP); | |
1221 | if (ret < 0) { | |
1222 | INFO("Failed to unshare CLONE_NEWCGROUP"); | |
1223 | goto out_warn_father; | |
1224 | } | |
1225 | INFO("Unshared CLONE_NEWCGROUP"); | |
1226 | } | |
1227 | ||
1228 | /* Add the requested environment variables to the current environment to | |
1229 | * allow them to be used by the various hooks, such as the start hook | |
1230 | * above. | |
1231 | */ | |
1232 | lxc_list_for_each(iterator, &handler->conf->environment) { | |
1233 | ret = putenv((char *)iterator->elem); | |
1234 | if (ret < 0) { | |
1235 | SYSERROR("Failed to set environment variable: %s", | |
1236 | (char *)iterator->elem); | |
1237 | goto out_warn_father; | |
1238 | } | |
1239 | } | |
1240 | ||
1241 | /* Setup the container, ip, names, utsname, ... */ | |
1242 | ret = lxc_setup(handler); | |
1243 | close(handler->data_sock[1]); | |
1244 | close(handler->data_sock[0]); | |
1245 | if (ret < 0) { | |
1246 | ERROR("Failed to setup container \"%s\"", handler->name); | |
1247 | goto out_warn_father; | |
1248 | } | |
1249 | ||
1250 | /* Set the label to change to when we exec(2) the container's init. */ | |
1251 | ret = lsm_process_label_set(NULL, handler->conf, true); | |
1252 | if (ret < 0) | |
1253 | goto out_warn_father; | |
1254 | ||
1255 | /* Set PR_SET_NO_NEW_PRIVS after we changed the lsm label. If we do it | |
1256 | * before we aren't allowed anymore. | |
1257 | */ | |
1258 | if (handler->conf->no_new_privs) { | |
1259 | ret = prctl(PR_SET_NO_NEW_PRIVS, prctl_arg(1), prctl_arg(0), | |
1260 | prctl_arg(0), prctl_arg(0)); | |
1261 | if (ret < 0) { | |
1262 | SYSERROR("Could not set PR_SET_NO_NEW_PRIVS to block " | |
1263 | "execve() gainable privileges"); | |
1264 | goto out_warn_father; | |
1265 | } | |
1266 | DEBUG("Set PR_SET_NO_NEW_PRIVS to block execve() gainable " | |
1267 | "privileges"); | |
1268 | } | |
1269 | ||
1270 | /* Some init's such as busybox will set sane tty settings on stdin, | |
1271 | * stdout, stderr which it thinks is the console. We already set them | |
1272 | * the way we wanted on the real terminal, and we want init to do its | |
1273 | * setup on its console ie. the pty allocated in lxc_terminal_setup() so | |
1274 | * make sure that that pty is stdin,stdout,stderr. | |
1275 | */ | |
1276 | if (handler->conf->console.slave >= 0) { | |
1277 | if (handler->backgrounded || !handler->conf->is_execute) | |
1278 | ret = set_stdfds(handler->conf->console.slave); | |
1279 | else | |
1280 | ret = lxc_terminal_set_stdfds(handler->conf->console.slave); | |
1281 | if (ret < 0) { | |
1282 | ERROR("Failed to redirect std{in,out,err} to pty file " | |
1283 | "descriptor %d", handler->conf->console.slave); | |
1284 | goto out_warn_father; | |
1285 | } | |
1286 | } | |
1287 | ||
1288 | /* If we mounted a temporary proc, then unmount it now. */ | |
1289 | tmp_proc_unmount(handler->conf); | |
1290 | ||
1291 | ret = lxc_seccomp_load(handler->conf); | |
1292 | if (ret < 0) | |
1293 | goto out_warn_father; | |
1294 | ||
1295 | ret = run_lxc_hooks(handler->name, "start", handler->conf, NULL); | |
1296 | if (ret < 0) { | |
1297 | ERROR("Failed to run lxc.hook.start for container \"%s\"", | |
1298 | handler->name); | |
1299 | goto out_warn_father; | |
1300 | } | |
1301 | ||
1302 | close(handler->sigfd); | |
1303 | ||
1304 | if (handler->conf->console.slave < 0 && handler->backgrounded) { | |
1305 | if (devnull_fd < 0) { | |
1306 | devnull_fd = open_devnull(); | |
1307 | if (devnull_fd < 0) | |
1308 | goto out_warn_father; | |
1309 | } | |
1310 | ||
1311 | ret = set_stdfds(devnull_fd); | |
1312 | if (ret < 0) { | |
1313 | ERROR("Failed to redirect std{in,out,err} to \"/dev/null\""); | |
1314 | goto out_warn_father; | |
1315 | } | |
1316 | } | |
1317 | ||
1318 | if (devnull_fd >= 0) { | |
1319 | close(devnull_fd); | |
1320 | devnull_fd = -1; | |
1321 | } | |
1322 | ||
1323 | setsid(); | |
1324 | ||
1325 | if (handler->conf->init_cwd) { | |
1326 | ret = chdir(handler->conf->init_cwd); | |
1327 | if (ret < 0) { | |
1328 | SYSERROR("Could not change directory to \"%s\"", | |
1329 | handler->conf->init_cwd); | |
1330 | goto out_warn_father; | |
1331 | } | |
1332 | } | |
1333 | ||
1334 | ret = lxc_sync_barrier_parent(handler, LXC_SYNC_CGROUP_LIMITS); | |
1335 | if (ret < 0) | |
1336 | goto out_warn_father; | |
1337 | ||
1338 | /* Reset the environment variables the user requested in a clear | |
1339 | * environment. | |
1340 | */ | |
1341 | ret = clearenv(); | |
1342 | /* Don't error out though. */ | |
1343 | if (ret < 0) | |
1344 | SYSERROR("Failed to clear environment."); | |
1345 | ||
1346 | lxc_list_for_each(iterator, &handler->conf->environment) { | |
1347 | ret = putenv((char *)iterator->elem); | |
1348 | if (ret < 0) { | |
1349 | SYSERROR("Failed to set environment variable: %s", | |
1350 | (char *)iterator->elem); | |
1351 | goto out_warn_father; | |
1352 | } | |
1353 | } | |
1354 | ||
1355 | ret = putenv("container=lxc"); | |
1356 | if (ret < 0) { | |
1357 | SYSERROR("Failed to set environment variable: container=lxc"); | |
1358 | goto out_warn_father; | |
1359 | } | |
1360 | ||
1361 | if (handler->conf->ttys.tty_names) { | |
1362 | ret = putenv(handler->conf->ttys.tty_names); | |
1363 | if (ret < 0) { | |
1364 | SYSERROR("Failed to set environment variable for container ptys"); | |
1365 | goto out_warn_father; | |
1366 | } | |
1367 | } | |
1368 | ||
1369 | /* The container has been setup. We can now switch to an unprivileged | |
1370 | * uid/gid. | |
1371 | */ | |
1372 | new_uid = handler->conf->init_uid; | |
1373 | new_gid = handler->conf->init_gid; | |
1374 | ||
1375 | /* If we are in a new user namespace we already dropped all groups when | |
1376 | * we switched to root in the new user namespace further above. Only | |
1377 | * drop groups if we can, so ensure that we have necessary privilege. | |
1378 | */ | |
1379 | #if HAVE_LIBCAP | |
1380 | have_cap_setgid = lxc_proc_cap_is_set(CAP_SETGID, CAP_EFFECTIVE); | |
1381 | #else | |
1382 | have_cap_setgid = false; | |
1383 | #endif | |
1384 | if (lxc_list_empty(&handler->conf->id_map) && have_cap_setgid) { | |
1385 | ret = lxc_setgroups(0, NULL); | |
1386 | if (ret < 0) | |
1387 | goto out_warn_father; | |
1388 | } | |
1389 | ||
1390 | ret = lxc_switch_uid_gid(new_uid, new_gid); | |
1391 | if (ret < 0) | |
1392 | goto out_warn_father; | |
1393 | ||
1394 | ret = lxc_ambient_caps_down(); | |
1395 | if (ret < 0) { | |
1396 | ERROR("Failed to clear ambient capabilities"); | |
1397 | goto out_warn_father; | |
1398 | } | |
1399 | ||
1400 | if (handler->conf->monitor_signal_pdeath != SIGKILL) { | |
1401 | ret = lxc_set_death_signal(handler->conf->monitor_signal_pdeath); | |
1402 | if (ret < 0) { | |
1403 | SYSERROR("Failed to set PR_SET_PDEATHSIG to %d", | |
1404 | handler->conf->monitor_signal_pdeath); | |
1405 | goto out_warn_father; | |
1406 | } | |
1407 | } | |
1408 | ||
1409 | /* After this call, we are in error because this ops should not return | |
1410 | * as it execs. | |
1411 | */ | |
1412 | handler->ops->start(handler, handler->data); | |
1413 | ||
1414 | out_warn_father: | |
1415 | /* We want the parent to know something went wrong, so we return a | |
1416 | * special error code. | |
1417 | */ | |
1418 | lxc_sync_wake_parent(handler, LXC_SYNC_ERROR); | |
1419 | ||
1420 | out_error: | |
1421 | if (devnull_fd >= 0) | |
1422 | close(devnull_fd); | |
1423 | ||
1424 | return -1; | |
1425 | } | |
1426 | ||
1427 | static int lxc_recv_ttys_from_child(struct lxc_handler *handler) | |
1428 | { | |
1429 | int i; | |
1430 | struct lxc_terminal_info *tty; | |
1431 | int ret = -1; | |
1432 | int sock = handler->data_sock[1]; | |
1433 | struct lxc_conf *conf = handler->conf; | |
1434 | struct lxc_tty_info *ttys = &conf->ttys; | |
1435 | ||
1436 | if (!conf->ttys.max) | |
1437 | return 0; | |
1438 | ||
1439 | ttys->tty = malloc(sizeof(*ttys->tty) * ttys->max); | |
1440 | if (!ttys->tty) | |
1441 | return -1; | |
1442 | ||
1443 | for (i = 0; i < conf->ttys.max; i++) { | |
1444 | int ttyfds[2]; | |
1445 | ||
1446 | ret = lxc_abstract_unix_recv_fds(sock, ttyfds, 2, NULL, 0); | |
1447 | if (ret < 0) | |
1448 | break; | |
1449 | ||
1450 | tty = &ttys->tty[i]; | |
1451 | tty->busy = 0; | |
1452 | tty->master = ttyfds[0]; | |
1453 | tty->slave = ttyfds[1]; | |
1454 | TRACE("Received pty with master fd %d and slave fd %d from " | |
1455 | "parent", tty->master, tty->slave); | |
1456 | } | |
1457 | ||
1458 | if (ret < 0) | |
1459 | SYSERROR("Failed to receive %zu ttys from child", ttys->max); | |
1460 | else | |
1461 | TRACE("Received %zu ttys from child", ttys->max); | |
1462 | ||
1463 | return ret; | |
1464 | } | |
1465 | ||
1466 | int resolve_clone_flags(struct lxc_handler *handler) | |
1467 | { | |
1468 | int i; | |
1469 | struct lxc_conf *conf = handler->conf; | |
1470 | ||
1471 | for (i = 0; i < LXC_NS_MAX; i++) { | |
1472 | if (conf->ns_keep != 0) { | |
1473 | if ((conf->ns_keep & ns_info[i].clone_flag) == 0) | |
1474 | handler->ns_clone_flags |= ns_info[i].clone_flag; | |
1475 | } else if (conf->ns_clone != 0) { | |
1476 | if ((conf->ns_clone & ns_info[i].clone_flag) > 0) | |
1477 | handler->ns_clone_flags |= ns_info[i].clone_flag; | |
1478 | } else { | |
1479 | if (i == LXC_NS_USER && lxc_list_empty(&handler->conf->id_map)) | |
1480 | continue; | |
1481 | ||
1482 | if (i == LXC_NS_NET && lxc_requests_empty_network(handler)) | |
1483 | continue; | |
1484 | ||
1485 | if (i == LXC_NS_CGROUP && !cgns_supported()) | |
1486 | continue; | |
1487 | ||
1488 | handler->ns_clone_flags |= ns_info[i].clone_flag; | |
1489 | } | |
1490 | ||
1491 | if (!conf->ns_share[i]) | |
1492 | continue; | |
1493 | ||
1494 | handler->ns_clone_flags &= ~ns_info[i].clone_flag; | |
1495 | TRACE("Sharing %s namespace", ns_info[i].proc_name); | |
1496 | } | |
1497 | ||
1498 | return 0; | |
1499 | } | |
1500 | ||
1501 | /* Note that this function is used with clone(CLONE_VM). Some glibc versions | |
1502 | * used to reset the pid/tid to -1 when CLONE_VM was used without CLONE_THREAD. | |
1503 | * But since the memory between parent and child is shared on CLONE_VM this | |
1504 | * would invalidate the getpid() cache that glibc used to maintain and so | |
1505 | * getpid() in the child would return the parent's pid. This is all fixed in | |
1506 | * newer glibc versions where the getpid() cache is removed and the pid/tid is | |
1507 | * not reset anymore. | |
1508 | * However, if for whatever reason you - dear commiter - somehow need to get the | |
1509 | * pid of the dummy intermediate process for do_share_ns() you need to call | |
1510 | * lxc_raw_getpid(). The next lxc_raw_clone() call does not employ CLONE_VM and | |
1511 | * will be fine. | |
1512 | */ | |
1513 | static inline int do_share_ns(void *arg) | |
1514 | { | |
1515 | int i, flags, ret; | |
1516 | struct lxc_handler *handler = arg; | |
1517 | ||
1518 | for (i = 0; i < LXC_NS_MAX; i++) { | |
1519 | if (handler->nsfd[i] < 0) | |
1520 | continue; | |
1521 | ||
1522 | ret = setns(handler->nsfd[i], 0); | |
1523 | if (ret < 0) { | |
1524 | /* | |
1525 | * Note that joining a user and/or mount namespace | |
1526 | * requires the process is not multithreaded otherwise | |
1527 | * setns() will fail here. | |
1528 | */ | |
1529 | SYSERROR("Failed to inherit %s namespace", | |
1530 | ns_info[i].proc_name); | |
1531 | return -1; | |
1532 | } | |
1533 | ||
1534 | DEBUG("Inherited %s namespace", ns_info[i].proc_name); | |
1535 | } | |
1536 | ||
1537 | flags = handler->ns_on_clone_flags; | |
1538 | flags |= CLONE_PARENT; | |
1539 | handler->pid = lxc_raw_clone_cb(do_start, handler, flags); | |
1540 | if (handler->pid < 0) | |
1541 | return -1; | |
1542 | ||
1543 | return 0; | |
1544 | } | |
1545 | ||
1546 | static int lxc_setup_shmount(struct lxc_conf *conf) | |
1547 | { | |
1548 | size_t len_cont; | |
1549 | char *full_cont_path; | |
1550 | int ret = -1; | |
1551 | ||
1552 | /* Construct the shmount path under the container root. */ | |
1553 | len_cont = strlen(conf->rootfs.mount) + 1 + strlen(conf->shmount.path_cont); | |
1554 | /* +1 for the terminating '\0' */ | |
1555 | full_cont_path = malloc(len_cont + 1); | |
1556 | if (!full_cont_path) { | |
1557 | SYSERROR("Not enough memory"); | |
1558 | return -ENOMEM; | |
1559 | } | |
1560 | ||
1561 | ret = snprintf(full_cont_path, len_cont + 1, "%s/%s", | |
1562 | conf->rootfs.mount, conf->shmount.path_cont); | |
1563 | if (ret < 0 || ret >= len_cont + 1) { | |
1564 | SYSERROR("Failed to create filename"); | |
1565 | free(full_cont_path); | |
1566 | return -1; | |
1567 | } | |
1568 | ||
1569 | /* Check if shmount point is already set up. */ | |
1570 | if (is_shared_mountpoint(conf->shmount.path_host)) { | |
1571 | INFO("Path \"%s\" is already MS_SHARED. Reusing", | |
1572 | conf->shmount.path_host); | |
1573 | free(full_cont_path); | |
1574 | return 0; | |
1575 | } | |
1576 | ||
1577 | /* Create host and cont mount paths */ | |
1578 | ret = mkdir_p(conf->shmount.path_host, 0711); | |
1579 | if (ret < 0 && errno != EEXIST) { | |
1580 | SYSERROR("Failed to create directory \"%s\"", | |
1581 | conf->shmount.path_host); | |
1582 | free(full_cont_path); | |
1583 | return ret; | |
1584 | } | |
1585 | ||
1586 | ret = mkdir_p(full_cont_path, 0711); | |
1587 | if (ret < 0 && errno != EEXIST) { | |
1588 | SYSERROR("Failed to create directory \"%s\"", full_cont_path); | |
1589 | free(full_cont_path); | |
1590 | return ret; | |
1591 | } | |
1592 | ||
1593 | /* Prepare host mountpoint */ | |
1594 | ret = mount("tmpfs", conf->shmount.path_host, "tmpfs", 0, | |
1595 | "size=100k,mode=0711"); | |
1596 | if (ret < 0) { | |
1597 | SYSERROR("Failed to mount \"%s\"", conf->shmount.path_host); | |
1598 | free(full_cont_path); | |
1599 | return ret; | |
1600 | } | |
1601 | ||
1602 | ret = mount(conf->shmount.path_host, conf->shmount.path_host, "none", | |
1603 | MS_REC | MS_SHARED, ""); | |
1604 | if (ret < 0) { | |
1605 | SYSERROR("Failed to make shared \"%s\"", conf->shmount.path_host); | |
1606 | free(full_cont_path); | |
1607 | return ret; | |
1608 | } | |
1609 | ||
1610 | INFO("Setup shared mount point \"%s\"", conf->shmount.path_host); | |
1611 | free(full_cont_path); | |
1612 | return 0; | |
1613 | } | |
1614 | ||
1615 | /* lxc_spawn() performs crucial setup tasks and clone()s the new process which | |
1616 | * exec()s the requested container binary. | |
1617 | * Note that lxc_spawn() runs in the parent namespaces. Any operations performed | |
1618 | * right here should be double checked if they'd pose a security risk. (For | |
1619 | * example, any {u}mount() operations performed here will be reflected on the | |
1620 | * host!) | |
1621 | */ | |
1622 | static int lxc_spawn(struct lxc_handler *handler) | |
1623 | { | |
1624 | int i, ret; | |
1625 | char pidstr[20]; | |
1626 | bool wants_to_map_ids; | |
1627 | struct lxc_list *id_map; | |
1628 | const char *name = handler->name; | |
1629 | const char *lxcpath = handler->lxcpath; | |
1630 | bool share_ns = false; | |
1631 | struct lxc_conf *conf = handler->conf; | |
1632 | struct cgroup_ops *cgroup_ops = handler->cgroup_ops; | |
1633 | ||
1634 | id_map = &conf->id_map; | |
1635 | wants_to_map_ids = !lxc_list_empty(id_map); | |
1636 | ||
1637 | for (i = 0; i < LXC_NS_MAX; i++) { | |
1638 | if (!conf->ns_share[i]) | |
1639 | continue; | |
1640 | ||
1641 | handler->nsfd[i] = lxc_inherit_namespace(conf->ns_share[i], lxcpath, ns_info[i].proc_name); | |
1642 | if (handler->nsfd[i] < 0) | |
1643 | return -1; | |
1644 | ||
1645 | share_ns = true; | |
1646 | } | |
1647 | ||
1648 | ret = lxc_sync_init(handler); | |
1649 | if (ret < 0) | |
1650 | return -1; | |
1651 | ||
1652 | ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, | |
1653 | handler->data_sock); | |
1654 | if (ret < 0) { | |
1655 | lxc_sync_fini(handler); | |
1656 | return -1; | |
1657 | } | |
1658 | ||
1659 | ret = resolve_clone_flags(handler); | |
1660 | if (ret < 0) { | |
1661 | lxc_sync_fini(handler); | |
1662 | return -1; | |
1663 | } | |
1664 | ||
1665 | if (conf->shmount.path_host) { | |
1666 | if (!conf->shmount.path_cont) { | |
1667 | lxc_sync_fini(handler); | |
1668 | return -1; | |
1669 | } | |
1670 | ||
1671 | ret = lxc_setup_shmount(conf); | |
1672 | if (ret < 0) { | |
1673 | ERROR("Failed to setup shared mount point"); | |
1674 | lxc_sync_fini(handler); | |
1675 | return -1; | |
1676 | } | |
1677 | } | |
1678 | ||
1679 | if (handler->ns_clone_flags & CLONE_NEWNET) { | |
1680 | if (!lxc_list_empty(&conf->network)) { | |
1681 | ||
1682 | /* Find gateway addresses from the link device, which is | |
1683 | * no longer accessible inside the container. Do this | |
1684 | * before creating network interfaces, since goto | |
1685 | * out_delete_net does not work before lxc_clone. | |
1686 | */ | |
1687 | ret = lxc_find_gateway_addresses(handler); | |
1688 | if (ret < 0) { | |
1689 | ERROR("Failed to find gateway addresses"); | |
1690 | lxc_sync_fini(handler); | |
1691 | return -1; | |
1692 | } | |
1693 | ||
1694 | /* That should be done before the clone because we will | |
1695 | * fill the netdev index and use them in the child. | |
1696 | */ | |
1697 | ret = lxc_create_network_priv(handler); | |
1698 | if (ret < 0) { | |
1699 | ERROR("Failed to create the network"); | |
1700 | goto out_delete_net; | |
1701 | } | |
1702 | } | |
1703 | } | |
1704 | ||
1705 | if (!cgroup_init(handler)) { | |
1706 | ERROR("Failed initializing cgroup support"); | |
1707 | goto out_delete_net; | |
1708 | } | |
1709 | ||
1710 | if (!cgroup_ops->create(cgroup_ops, handler)) { | |
1711 | ERROR("Failed creating cgroups"); | |
1712 | goto out_delete_net; | |
1713 | } | |
1714 | ||
1715 | /* If the rootfs is not a blockdev, prevent the container from marking | |
1716 | * it readonly. | |
1717 | * If the container is unprivileged then skip rootfs pinning. | |
1718 | */ | |
1719 | if (!wants_to_map_ids) { | |
1720 | handler->pinfd = pin_rootfs(conf->rootfs.path); | |
1721 | if (handler->pinfd == -1) | |
1722 | INFO("Failed to pin the rootfs for container \"%s\"", handler->name); | |
1723 | } | |
1724 | ||
1725 | /* Create a process in a new set of namespaces. */ | |
1726 | handler->ns_on_clone_flags = handler->ns_clone_flags; | |
1727 | if (handler->ns_clone_flags & CLONE_NEWUSER) { | |
1728 | /* If CLONE_NEWUSER and CLONE_NEWNET was requested, we need to | |
1729 | * clone a new user namespace first and only later unshare our | |
1730 | * network namespace to ensure that network devices ownership is | |
1731 | * set up correctly. | |
1732 | */ | |
1733 | handler->ns_on_clone_flags &= ~CLONE_NEWNET; | |
1734 | } | |
1735 | /* The cgroup namespace gets unshare()ed not clone()ed. */ | |
1736 | handler->ns_on_clone_flags &= ~CLONE_NEWCGROUP; | |
1737 | ||
1738 | if (share_ns) { | |
1739 | pid_t attacher_pid; | |
1740 | ||
1741 | attacher_pid = lxc_clone(do_share_ns, handler, | |
1742 | CLONE_VFORK | CLONE_VM | CLONE_FILES); | |
1743 | if (attacher_pid < 0) { | |
1744 | SYSERROR(LXC_CLONE_ERROR); | |
1745 | goto out_delete_net; | |
1746 | } | |
1747 | ||
1748 | ret = wait_for_pid(attacher_pid); | |
1749 | if (ret < 0) { | |
1750 | SYSERROR("Intermediate process failed"); | |
1751 | goto out_delete_net; | |
1752 | } | |
1753 | } else { | |
1754 | handler->pid = lxc_raw_clone_cb(do_start, handler, | |
1755 | handler->ns_on_clone_flags); | |
1756 | } | |
1757 | if (handler->pid < 0) { | |
1758 | SYSERROR(LXC_CLONE_ERROR); | |
1759 | goto out_delete_net; | |
1760 | } | |
1761 | TRACE("Cloned child process %d", handler->pid); | |
1762 | ||
1763 | for (i = 0; i < LXC_NS_MAX; i++) | |
1764 | if (handler->ns_on_clone_flags & ns_info[i].clone_flag) | |
1765 | INFO("Cloned %s", ns_info[i].flag_name); | |
1766 | ||
1767 | if (!lxc_try_preserve_namespaces(handler, handler->ns_on_clone_flags, handler->pid)) { | |
1768 | ERROR("Failed to preserve cloned namespaces for lxc.hook.stop"); | |
1769 | goto out_delete_net; | |
1770 | } | |
1771 | ||
1772 | lxc_sync_fini_child(handler); | |
1773 | ||
1774 | /* Map the container uids. The container became an invalid userid the | |
1775 | * moment it was cloned with CLONE_NEWUSER. This call doesn't change | |
1776 | * anything immediately, but allows the container to setuid(0) (0 being | |
1777 | * mapped to something else on the host.) later to become a valid uid | |
1778 | * again. | |
1779 | */ | |
1780 | if (wants_to_map_ids) { | |
1781 | if (!handler->conf->ns_share[LXC_NS_USER] && | |
1782 | (handler->conf->ns_keep & CLONE_NEWUSER) == 0) { | |
1783 | ret = lxc_map_ids(id_map, handler->pid); | |
1784 | if (ret < 0) { | |
1785 | ERROR("Failed to set up id mapping."); | |
1786 | goto out_delete_net; | |
1787 | } | |
1788 | } | |
1789 | } | |
1790 | ||
1791 | ret = lxc_sync_wake_child(handler, LXC_SYNC_STARTUP); | |
1792 | if (ret < 0) | |
1793 | goto out_delete_net; | |
1794 | ||
1795 | ret = lxc_sync_wait_child(handler, LXC_SYNC_CONFIGURE); | |
1796 | if (ret < 0) | |
1797 | goto out_delete_net; | |
1798 | ||
1799 | if (!cgroup_ops->setup_limits(cgroup_ops, handler->conf, false)) { | |
1800 | ERROR("Failed to setup cgroup limits for container \"%s\"", name); | |
1801 | goto out_delete_net; | |
1802 | } | |
1803 | ||
1804 | if (!cgroup_ops->enter(cgroup_ops, handler->pid)) | |
1805 | goto out_delete_net; | |
1806 | ||
1807 | if (!cgroup_ops->chown(cgroup_ops, handler->conf)) | |
1808 | goto out_delete_net; | |
1809 | ||
1810 | /* Now we're ready to preserve the network namespace */ | |
1811 | ret = lxc_try_preserve_ns(handler->pid, "net"); | |
1812 | if (ret < 0) { | |
1813 | if (ret != -EOPNOTSUPP) { | |
1814 | SYSERROR("Failed to preserve net namespace"); | |
1815 | goto out_delete_net; | |
1816 | } | |
1817 | } else { | |
1818 | handler->nsfd[LXC_NS_NET] = ret; | |
1819 | DEBUG("Preserved net namespace via fd %d", ret); | |
1820 | ||
1821 | ret = lxc_netns_set_nsid(handler->nsfd[LXC_NS_NET]); | |
1822 | if (ret < 0) { | |
1823 | errno = -ret; | |
1824 | SYSERROR("Failed to allocate new network namespace id"); | |
1825 | } else { | |
1826 | TRACE("Allocated new network namespace id"); | |
1827 | } | |
1828 | } | |
1829 | ||
1830 | /* Create the network configuration. */ | |
1831 | if (handler->ns_clone_flags & CLONE_NEWNET) { | |
1832 | ret = lxc_network_move_created_netdev_priv(handler->lxcpath, | |
1833 | handler->name, | |
1834 | &conf->network, | |
1835 | handler->pid); | |
1836 | if (ret < 0) { | |
1837 | ERROR("Failed to create the configured network"); | |
1838 | goto out_delete_net; | |
1839 | } | |
1840 | ||
1841 | ret = lxc_create_network_unpriv(handler->lxcpath, handler->name, | |
1842 | &conf->network, handler->pid, conf->hooks_version); | |
1843 | if (ret < 0) { | |
1844 | ERROR("Failed to create the configured network"); | |
1845 | goto out_delete_net; | |
1846 | } | |
1847 | } | |
1848 | ||
1849 | ret = lxc_network_send_veth_names_to_child(handler); | |
1850 | if (ret < 0) { | |
1851 | ERROR("Failed to send veth names to child"); | |
1852 | goto out_delete_net; | |
1853 | } | |
1854 | ||
1855 | if (!lxc_list_empty(&conf->procs)) { | |
1856 | ret = setup_proc_filesystem(&conf->procs, handler->pid); | |
1857 | if (ret < 0) | |
1858 | goto out_delete_net; | |
1859 | } | |
1860 | ||
1861 | /* Tell the child to continue its initialization. We'll get | |
1862 | * LXC_SYNC_CGROUP when it is ready for us to setup cgroups. | |
1863 | */ | |
1864 | ret = lxc_sync_barrier_child(handler, LXC_SYNC_POST_CONFIGURE); | |
1865 | if (ret < 0) | |
1866 | goto out_delete_net; | |
1867 | ||
1868 | if (!lxc_list_empty(&conf->limits)) { | |
1869 | ret = setup_resource_limits(&conf->limits, handler->pid); | |
1870 | if (ret < 0) { | |
1871 | ERROR("Failed to setup resource limits"); | |
1872 | goto out_delete_net; | |
1873 | } | |
1874 | } | |
1875 | ||
1876 | ret = lxc_sync_barrier_child(handler, LXC_SYNC_CGROUP_UNSHARE); | |
1877 | if (ret < 0) | |
1878 | goto out_delete_net; | |
1879 | ||
1880 | if (!cgroup_ops->setup_limits(cgroup_ops, handler->conf, true)) { | |
1881 | ERROR("Failed to setup legacy device cgroup controller limits"); | |
1882 | goto out_delete_net; | |
1883 | } | |
1884 | TRACE("Set up legacy device cgroup controller limits"); | |
1885 | ||
1886 | if (handler->ns_clone_flags & CLONE_NEWCGROUP) { | |
1887 | /* Now we're ready to preserve the cgroup namespace */ | |
1888 | ret = lxc_try_preserve_ns(handler->pid, "cgroup"); | |
1889 | if (ret < 0) { | |
1890 | if (ret != -EOPNOTSUPP) { | |
1891 | SYSERROR("Failed to preserve cgroup namespace"); | |
1892 | goto out_delete_net; | |
1893 | } | |
1894 | } else { | |
1895 | handler->nsfd[LXC_NS_CGROUP] = ret; | |
1896 | DEBUG("Preserved cgroup namespace via fd %d", ret); | |
1897 | } | |
1898 | } | |
1899 | ||
1900 | ret = snprintf(pidstr, 20, "%d", handler->pid); | |
1901 | if (ret < 0 || ret >= 20) | |
1902 | goto out_delete_net; | |
1903 | ||
1904 | ret = setenv("LXC_PID", pidstr, 1); | |
1905 | if (ret < 0) | |
1906 | SYSERROR("Failed to set environment variable: LXC_PID=%s", pidstr); | |
1907 | ||
1908 | /* Run any host-side start hooks */ | |
1909 | ret = run_lxc_hooks(name, "start-host", conf, NULL); | |
1910 | if (ret < 0) { | |
1911 | ERROR("Failed to run lxc.hook.start-host"); | |
1912 | goto out_delete_net; | |
1913 | } | |
1914 | ||
1915 | /* Tell the child to complete its initialization and wait for it to exec | |
1916 | * or return an error. (The child will never return | |
1917 | * LXC_SYNC_READY_START+1. It will either close the sync pipe, causing | |
1918 | * lxc_sync_barrier_child to return success, or return a different | |
1919 | * value, causing us to error out). | |
1920 | */ | |
1921 | ret = lxc_sync_barrier_child(handler, LXC_SYNC_READY_START); | |
1922 | if (ret < 0) | |
1923 | goto out_delete_net; | |
1924 | ||
1925 | ret = lxc_network_recv_name_and_ifindex_from_child(handler); | |
1926 | if (ret < 0) { | |
1927 | ERROR("Failed to receive names and ifindices for network " | |
1928 | "devices from child"); | |
1929 | goto out_delete_net; | |
1930 | } | |
1931 | ||
1932 | /* Now all networks are created, network devices are moved into place, | |
1933 | * and the correct names and ifindeces in the respective namespaces have | |
1934 | * been recorded. The corresponding structs have now all been filled. So | |
1935 | * log them for debugging purposes. | |
1936 | */ | |
1937 | lxc_log_configured_netdevs(conf); | |
1938 | ||
1939 | /* Read tty fds allocated by child. */ | |
1940 | ret = lxc_recv_ttys_from_child(handler); | |
1941 | if (ret < 0) { | |
1942 | ERROR("Failed to receive tty info from child process"); | |
1943 | goto out_delete_net; | |
1944 | } | |
1945 | ||
1946 | ret = handler->ops->post_start(handler, handler->data); | |
1947 | if (ret < 0) | |
1948 | goto out_abort; | |
1949 | ||
1950 | ret = lxc_set_state(name, handler, RUNNING); | |
1951 | if (ret < 0) { | |
1952 | ERROR("Failed to set state to \"%s\"", lxc_state2str(RUNNING)); | |
1953 | goto out_abort; | |
1954 | } | |
1955 | ||
1956 | lxc_sync_fini(handler); | |
1957 | ||
1958 | return 0; | |
1959 | ||
1960 | out_delete_net: | |
1961 | if (handler->ns_clone_flags & CLONE_NEWNET) | |
1962 | lxc_delete_network(handler); | |
1963 | ||
1964 | out_abort: | |
1965 | lxc_abort(name, handler); | |
1966 | lxc_sync_fini(handler); | |
1967 | if (handler->pinfd >= 0) { | |
1968 | close(handler->pinfd); | |
1969 | handler->pinfd = -1; | |
1970 | } | |
1971 | ||
1972 | return -1; | |
1973 | } | |
1974 | ||
1975 | int __lxc_start(const char *name, struct lxc_handler *handler, | |
1976 | struct lxc_operations* ops, void *data, const char *lxcpath, | |
1977 | bool backgrounded, int *error_num) | |
1978 | { | |
1979 | int ret, status; | |
1980 | struct lxc_conf *conf = handler->conf; | |
1981 | ||
1982 | ret = lxc_init(name, handler); | |
1983 | if (ret < 0) { | |
1984 | ERROR("Failed to initialize container \"%s\"", name); | |
1985 | return -1; | |
1986 | } | |
1987 | handler->ops = ops; | |
1988 | handler->data = data; | |
1989 | handler->backgrounded = backgrounded; | |
1990 | ||
1991 | if (!attach_block_device(handler->conf)) { | |
1992 | ERROR("Failed to attach block device"); | |
1993 | goto out_fini_nonet; | |
1994 | } | |
1995 | ||
1996 | if (geteuid() == 0 && !lxc_list_empty(&conf->id_map)) { | |
1997 | /* If the backing store is a device, mount it here and now. */ | |
1998 | if (rootfs_is_blockdev(conf)) { | |
1999 | ret = unshare(CLONE_NEWNS); | |
2000 | if (ret < 0) { | |
2001 | ERROR("Failed to unshare CLONE_NEWNS"); | |
2002 | goto out_fini_nonet; | |
2003 | } | |
2004 | INFO("Unshared CLONE_NEWNS"); | |
2005 | ||
2006 | remount_all_slave(); | |
2007 | ret = lxc_setup_rootfs_prepare_root(conf, name, lxcpath); | |
2008 | if (ret < 0) { | |
2009 | ERROR("Error setting up rootfs mount as root before spawn"); | |
2010 | goto out_fini_nonet; | |
2011 | } | |
2012 | INFO("Set up container rootfs as host root"); | |
2013 | } | |
2014 | } | |
2015 | ||
2016 | ret = lxc_spawn(handler); | |
2017 | if (ret < 0) { | |
2018 | ERROR("Failed to spawn container \"%s\"", name); | |
2019 | goto out_detach_blockdev; | |
2020 | } | |
2021 | /* close parent side of data socket */ | |
2022 | close(handler->data_sock[0]); | |
2023 | handler->data_sock[0] = -1; | |
2024 | close(handler->data_sock[1]); | |
2025 | handler->data_sock[1] = -1; | |
2026 | ||
2027 | handler->conf->reboot = REBOOT_NONE; | |
2028 | ||
2029 | ret = lxc_poll(name, handler); | |
2030 | if (ret) { | |
2031 | ERROR("LXC mainloop exited with error: %d", ret); | |
2032 | goto out_abort; | |
2033 | } | |
2034 | ||
2035 | if (!handler->init_died && handler->pid > 0) { | |
2036 | ERROR("Child process is not killed"); | |
2037 | goto out_abort; | |
2038 | } | |
2039 | ||
2040 | status = lxc_wait_for_pid_status(handler->pid); | |
2041 | if (status < 0) | |
2042 | SYSERROR("Failed to retrieve status for %d", handler->pid); | |
2043 | ||
2044 | /* If the child process exited but was not signaled, it didn't call | |
2045 | * reboot. This should mean it was an lxc-execute which simply exited. | |
2046 | * In any case, treat it as a 'halt'. | |
2047 | */ | |
2048 | if (WIFSIGNALED(status)) { | |
2049 | switch(WTERMSIG(status)) { | |
2050 | case SIGINT: /* halt */ | |
2051 | DEBUG("Container \"%s\" is halting", name); | |
2052 | break; | |
2053 | case SIGHUP: /* reboot */ | |
2054 | DEBUG("Container \"%s\" is rebooting", name); | |
2055 | handler->conf->reboot = REBOOT_REQ; | |
2056 | break; | |
2057 | case SIGSYS: /* seccomp */ | |
2058 | DEBUG("Container \"%s\" violated its seccomp policy", name); | |
2059 | break; | |
2060 | default: | |
2061 | DEBUG("Unknown exit status for container \"%s\" init %d", name, WTERMSIG(status)); | |
2062 | break; | |
2063 | } | |
2064 | } | |
2065 | ||
2066 | ret = lxc_restore_phys_nics_to_netns(handler); | |
2067 | if (ret < 0) | |
2068 | ERROR("Failed to move physical network devices back to parent " | |
2069 | "network namespace"); | |
2070 | ||
2071 | if (handler->pinfd >= 0) { | |
2072 | close(handler->pinfd); | |
2073 | handler->pinfd = -1; | |
2074 | } | |
2075 | ||
2076 | lxc_monitor_send_exit_code(name, status, handler->lxcpath); | |
2077 | lxc_error_set_and_log(handler->pid, status); | |
2078 | if (error_num) | |
2079 | *error_num = handler->exit_status; | |
2080 | ||
2081 | out_fini: | |
2082 | lxc_delete_network(handler); | |
2083 | ||
2084 | out_detach_blockdev: | |
2085 | detach_block_device(handler->conf); | |
2086 | ||
2087 | out_fini_nonet: | |
2088 | lxc_fini(name, handler); | |
2089 | return ret; | |
2090 | ||
2091 | out_abort: | |
2092 | lxc_abort(name, handler); | |
2093 | goto out_fini; | |
2094 | } | |
2095 | ||
2096 | struct start_args { | |
2097 | char *const *argv; | |
2098 | }; | |
2099 | ||
2100 | static int start(struct lxc_handler *handler, void* data) | |
2101 | { | |
2102 | struct start_args *arg = data; | |
2103 | ||
2104 | NOTICE("Exec'ing \"%s\"", arg->argv[0]); | |
2105 | ||
2106 | execvp(arg->argv[0], arg->argv); | |
2107 | SYSERROR("Failed to exec \"%s\"", arg->argv[0]); | |
2108 | return 0; | |
2109 | } | |
2110 | ||
2111 | static int post_start(struct lxc_handler *handler, void* data) | |
2112 | { | |
2113 | struct start_args *arg = data; | |
2114 | ||
2115 | NOTICE("Started \"%s\" with pid \"%d\"", arg->argv[0], handler->pid); | |
2116 | return 0; | |
2117 | } | |
2118 | ||
2119 | static struct lxc_operations start_ops = { | |
2120 | .start = start, | |
2121 | .post_start = post_start | |
2122 | }; | |
2123 | ||
2124 | int lxc_start(const char *name, char *const argv[], struct lxc_handler *handler, | |
2125 | const char *lxcpath, bool backgrounded, int *error_num) | |
2126 | { | |
2127 | struct start_args start_arg = { | |
2128 | .argv = argv, | |
2129 | }; | |
2130 | ||
2131 | TRACE("Doing lxc_start"); | |
2132 | return __lxc_start(name, handler, &start_ops, &start_arg, lxcpath, backgrounded, error_num); | |
2133 | } | |
2134 | ||
2135 | static void lxc_destroy_container_on_signal(struct lxc_handler *handler, | |
2136 | const char *name) | |
2137 | { | |
2138 | char destroy[MAXPATHLEN]; | |
2139 | struct lxc_container *c; | |
2140 | int ret = 0; | |
2141 | bool bret = true; | |
2142 | ||
2143 | if (handler->conf->rootfs.path && handler->conf->rootfs.mount) { | |
2144 | bret = do_destroy_container(handler); | |
2145 | if (!bret) { | |
2146 | ERROR("Error destroying rootfs for container \"%s\"", name); | |
2147 | return; | |
2148 | } | |
2149 | } | |
2150 | INFO("Destroyed rootfs for container \"%s\"", name); | |
2151 | ||
2152 | ret = snprintf(destroy, MAXPATHLEN, "%s/%s", handler->lxcpath, name); | |
2153 | if (ret < 0 || ret >= MAXPATHLEN) { | |
2154 | ERROR("Error destroying directory for container \"%s\"", name); | |
2155 | return; | |
2156 | } | |
2157 | ||
2158 | c = lxc_container_new(name, handler->lxcpath); | |
2159 | if (c) { | |
2160 | if (container_disk_lock(c)) { | |
2161 | INFO("Could not update lxc_snapshots file"); | |
2162 | lxc_container_put(c); | |
2163 | } else { | |
2164 | mod_all_rdeps(c, false); | |
2165 | container_disk_unlock(c); | |
2166 | lxc_container_put(c); | |
2167 | } | |
2168 | } | |
2169 | ||
2170 | if (!handler->am_root) | |
2171 | ret = userns_exec_full(handler->conf, lxc_rmdir_onedev_wrapper, | |
2172 | destroy, "lxc_rmdir_onedev_wrapper"); | |
2173 | else | |
2174 | ret = lxc_rmdir_onedev(destroy, NULL); | |
2175 | ||
2176 | if (ret < 0) { | |
2177 | ERROR("Error destroying directory for container \"%s\"", name); | |
2178 | return; | |
2179 | } | |
2180 | INFO("Destroyed directory for container \"%s\"", name); | |
2181 | } | |
2182 | ||
2183 | static int lxc_rmdir_onedev_wrapper(void *data) | |
2184 | { | |
2185 | char *arg = (char *) data; | |
2186 | return lxc_rmdir_onedev(arg, NULL); | |
2187 | } | |
2188 | ||
2189 | static bool do_destroy_container(struct lxc_handler *handler) | |
2190 | { | |
2191 | int ret; | |
2192 | ||
2193 | if (!handler->am_root) { | |
2194 | ret = userns_exec_full(handler->conf, storage_destroy_wrapper, | |
2195 | handler->conf, "storage_destroy_wrapper"); | |
2196 | if (ret < 0) | |
2197 | return false; | |
2198 | ||
2199 | return true; | |
2200 | } | |
2201 | ||
2202 | return storage_destroy(handler->conf); | |
2203 | } |