]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/attach.c
Fix typo
[mirror_lxc.git] / src / lxc / attach.c
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 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #define _GNU_SOURCE
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <grp.h>
28 #include <pwd.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <linux/unistd.h>
35 #include <sys/mount.h>
36 #include <sys/param.h>
37 #include <sys/prctl.h>
38 #include <sys/socket.h>
39 #include <sys/syscall.h>
40 #include <sys/wait.h>
41
42 #include <lxc/lxccontainer.h>
43
44 #ifndef HAVE_DECL_PR_CAPBSET_DROP
45 #define PR_CAPBSET_DROP 24
46 #endif
47
48 #ifndef HAVE_DECL_PR_SET_NO_NEW_PRIVS
49 #define PR_SET_NO_NEW_PRIVS 38
50 #endif
51
52 #ifndef HAVE_DECL_PR_GET_NO_NEW_PRIVS
53 #define PR_GET_NO_NEW_PRIVS 39
54 #endif
55
56 #include "af_unix.h"
57 #include "attach.h"
58 #include "caps.h"
59 #include "cgroup.h"
60 #include "commands.h"
61 #include "conf.h"
62 #include "config.h"
63 #include "confile.h"
64 #include "log.h"
65 #include "lsm/lsm.h"
66 #include "lxclock.h"
67 #include "lxcseccomp.h"
68 #include "mainloop.h"
69 #include "namespace.h"
70 #include "terminal.h"
71 #include "utils.h"
72
73 #if HAVE_SYS_PERSONALITY_H
74 #include <sys/personality.h>
75 #endif
76
77 #ifndef SOCK_CLOEXEC
78 #define SOCK_CLOEXEC 02000000
79 #endif
80
81 #ifndef MS_REC
82 #define MS_REC 16384
83 #endif
84
85 #ifndef MS_SLAVE
86 #define MS_SLAVE (1 << 19)
87 #endif
88
89 lxc_log_define(lxc_attach, lxc);
90
91 /* /proc/pid-to-str/status\0 = (5 + 21 + 7 + 1) */
92 #define __PROC_STATUS_LEN (5 + (LXC_NUMSTRLEN64) + 7 + 1)
93 static struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
94 {
95 int ret;
96 bool found;
97 FILE *proc_file;
98 char proc_fn[__PROC_STATUS_LEN];
99 size_t line_bufsz = 0;
100 char *line = NULL;
101 struct lxc_proc_context_info *info = NULL;
102
103 /* Read capabilities. */
104 ret = snprintf(proc_fn, __PROC_STATUS_LEN, "/proc/%d/status", pid);
105 if (ret < 0 || ret >= __PROC_STATUS_LEN)
106 goto on_error;
107
108 proc_file = fopen(proc_fn, "r");
109 if (!proc_file) {
110 SYSERROR("Could not open %s.", proc_fn);
111 goto on_error;
112 }
113
114 info = calloc(1, sizeof(*info));
115 if (!info) {
116 SYSERROR("Could not allocate memory.");
117 fclose(proc_file);
118 return NULL;
119 }
120
121 found = false;
122 while (getline(&line, &line_bufsz, proc_file) != -1) {
123 ret = sscanf(line, "CapBnd: %llx", &info->capability_mask);
124 if (ret != EOF && ret == 1) {
125 found = true;
126 break;
127 }
128 }
129
130 free(line);
131 fclose(proc_file);
132
133 if (!found) {
134 SYSERROR("Could not read capability bounding set from %s.",
135 proc_fn);
136 errno = ENOENT;
137 goto on_error;
138 }
139
140 info->lsm_label = lsm_process_label_get(pid);
141 info->ns_inherited = 0;
142 memset(info->ns_fd, -1, sizeof(int) * LXC_NS_MAX);
143
144 return info;
145
146 on_error:
147 free(info);
148 return NULL;
149 }
150
151 static inline void lxc_proc_close_ns_fd(struct lxc_proc_context_info *ctx)
152 {
153 int i;
154
155 for (i = 0; i < LXC_NS_MAX; i++) {
156 if (ctx->ns_fd[i] < 0)
157 continue;
158 close(ctx->ns_fd[i]);
159 ctx->ns_fd[i] = -EBADF;
160 }
161 }
162
163 static void lxc_proc_put_context_info(struct lxc_proc_context_info *ctx)
164 {
165 free(ctx->lsm_label);
166 ctx->lsm_label = NULL;
167
168 if (ctx->container) {
169 lxc_container_put(ctx->container);
170 ctx->container = NULL;
171 }
172
173 lxc_proc_close_ns_fd(ctx);
174 free(ctx);
175 ctx = NULL;
176 }
177
178 /**
179 * in_same_namespace - Check whether two processes are in the same namespace.
180 * @pid1 - PID of the first process.
181 * @pid2 - PID of the second process.
182 * @ns - Name of the namespace to check. Must correspond to one of the names
183 * for the namespaces as shown in /proc/<pid/ns/
184 *
185 * If the two processes are not in the same namespace returns an fd to the
186 * namespace of the second process identified by @pid2. If the two processes are
187 * in the same namespace returns -EINVAL, -1 if an error occurred.
188 */
189 static int in_same_namespace(pid_t pid1, pid_t pid2, const char *ns)
190 {
191 int ns_fd1 = -1, ns_fd2 = -1, ret = -1;
192 struct stat ns_st1, ns_st2;
193
194 ns_fd1 = lxc_preserve_ns(pid1, ns);
195 if (ns_fd1 < 0) {
196 /* The kernel does not support this namespace. This is not an
197 * error.
198 */
199 if (errno == ENOENT)
200 return -EINVAL;
201
202 goto out;
203 }
204
205 ns_fd2 = lxc_preserve_ns(pid2, ns);
206 if (ns_fd2 < 0)
207 goto out;
208
209 ret = fstat(ns_fd1, &ns_st1);
210 if (ret < 0)
211 goto out;
212
213 ret = fstat(ns_fd2, &ns_st2);
214 if (ret < 0)
215 goto out;
216
217 /* processes are in the same namespace */
218 ret = -EINVAL;
219 if ((ns_st1.st_dev == ns_st2.st_dev ) && (ns_st1.st_ino == ns_st2.st_ino))
220 goto out;
221
222 /* processes are in different namespaces */
223 ret = ns_fd2;
224 ns_fd2 = -1;
225
226 out:
227
228 if (ns_fd1 >= 0)
229 close(ns_fd1);
230 if (ns_fd2 >= 0)
231 close(ns_fd2);
232
233 return ret;
234 }
235
236 static int lxc_attach_to_ns(pid_t pid, struct lxc_proc_context_info *ctx)
237 {
238 int i, ret;
239
240 for (i = 0; i < LXC_NS_MAX; i++) {
241 if (ctx->ns_fd[i] < 0)
242 continue;
243
244 ret = setns(ctx->ns_fd[i], ns_info[i].clone_flag);
245 if (ret < 0) {
246 SYSERROR("Failed to attach to %s namespace of %d",
247 ns_info[i].proc_name, pid);
248 return -1;
249 }
250
251 DEBUG("Attached to %s namespace of %d", ns_info[i].proc_name, pid);
252 }
253
254 return 0;
255 }
256
257 static int lxc_attach_remount_sys_proc(void)
258 {
259 int ret;
260
261 ret = unshare(CLONE_NEWNS);
262 if (ret < 0) {
263 SYSERROR("Failed to unshare mount namespace.");
264 return -1;
265 }
266
267 if (detect_shared_rootfs()) {
268 if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL)) {
269 SYSERROR("Failed to make / rslave.");
270 ERROR("Continuing...");
271 }
272 }
273
274 /* Assume /proc is always mounted, so remount it. */
275 ret = umount2("/proc", MNT_DETACH);
276 if (ret < 0) {
277 SYSERROR("Failed to unmount /proc.");
278 return -1;
279 }
280
281 ret = mount("none", "/proc", "proc", 0, NULL);
282 if (ret < 0) {
283 SYSERROR("Failed to remount /proc.");
284 return -1;
285 }
286
287 /* Try to umount /sys. If it's not a mount point, we'll get EINVAL, then
288 * we ignore it because it may not have been mounted in the first place.
289 */
290 ret = umount2("/sys", MNT_DETACH);
291 if (ret < 0 && errno != EINVAL) {
292 SYSERROR("Failed to unmount /sys.");
293 return -1;
294 } else if (ret == 0) {
295 /* Remount it. */
296 ret = mount("none", "/sys", "sysfs", 0, NULL);
297 if (ret < 0) {
298 SYSERROR("Failed to remount /sys.");
299 return -1;
300 }
301 }
302
303 return 0;
304 }
305
306 static int lxc_attach_drop_privs(struct lxc_proc_context_info *ctx)
307 {
308 int cap, last_cap;
309
310 last_cap = lxc_caps_last_cap();
311 for (cap = 0; cap <= last_cap; cap++) {
312 if (ctx->capability_mask & (1LL << cap))
313 continue;
314
315 if (prctl(PR_CAPBSET_DROP, cap, 0, 0, 0)) {
316 SYSERROR("Failed to drop capability %d", cap);
317 return -1;
318 }
319 TRACE("Dropped capability %d", cap);
320 }
321
322 return 0;
323 }
324
325 static int lxc_attach_set_environment(struct lxc_proc_context_info *init_ctx,
326 enum lxc_attach_env_policy_t policy,
327 char **extra_env, char **extra_keep)
328 {
329 int ret;
330 struct lxc_list *iterator;
331
332 if (policy == LXC_ATTACH_CLEAR_ENV) {
333 int path_kept = 0;
334 char **extra_keep_store = NULL;
335
336 if (extra_keep) {
337 size_t count, i;
338
339 for (count = 0; extra_keep[count]; count++)
340 ;
341
342 extra_keep_store = calloc(count, sizeof(char *));
343 if (!extra_keep_store)
344 return -1;
345
346 for (i = 0; i < count; i++) {
347 char *v = getenv(extra_keep[i]);
348 if (v) {
349 extra_keep_store[i] = strdup(v);
350 if (!extra_keep_store[i]) {
351 while (i > 0)
352 free(extra_keep_store[--i]);
353 free(extra_keep_store);
354 return -1;
355 }
356
357 if (strcmp(extra_keep[i], "PATH") == 0)
358 path_kept = 1;
359 }
360 }
361 }
362
363 if (clearenv()) {
364 if (extra_keep_store) {
365 char **p;
366
367 for (p = extra_keep_store; *p; p++)
368 free(*p);
369
370 free(extra_keep_store);
371 }
372
373 SYSERROR("Failed to clear environment");
374 return -1;
375 }
376
377 if (extra_keep_store) {
378 size_t i;
379
380 for (i = 0; extra_keep[i]; i++) {
381 if (extra_keep_store[i]) {
382 ret = setenv(extra_keep[i], extra_keep_store[i], 1);
383 if (ret < 0)
384 WARN("%s - Failed to set environment variable", strerror(errno));
385 }
386 free(extra_keep_store[i]);
387 }
388 free(extra_keep_store);
389 }
390
391 /* Always set a default path; shells and execlp tend to be fine
392 * without it, but there is a disturbing number of C programs
393 * out there that just assume that getenv("PATH") is never NULL
394 * and then die a painful segfault death.
395 */
396 if (!path_kept) {
397 ret = setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 1);
398 if (ret < 0)
399 WARN("%s - Failed to set environment variable", strerror(errno));
400 }
401 }
402
403 ret = putenv("container=lxc");
404 if (ret < 0) {
405 WARN("%s - Failed to set environment variable", strerror(errno));
406 return -1;
407 }
408
409 /* Set container environment variables.*/
410 if (init_ctx && init_ctx->container && init_ctx->container->lxc_conf) {
411 lxc_list_for_each(iterator, &init_ctx->container->lxc_conf->environment) {
412 char *env_tmp;
413
414 env_tmp = strdup((char *)iterator->elem);
415 if (!env_tmp)
416 return -1;
417
418 ret = putenv(env_tmp);
419 if (ret < 0) {
420 SYSERROR("Failed to set environment variable: %s", (char *)iterator->elem);
421 return -1;
422 }
423 }
424 }
425
426 /* Set extra environment variables. */
427 if (extra_env) {
428 for (; *extra_env; extra_env++) {
429 char *p;
430 /* We just assume the user knows what they are doing, so
431 * we don't do any checks.
432 */
433 p = strdup(*extra_env);
434 if (!p)
435 return -1;
436
437 ret = putenv(p);
438 if (ret < 0)
439 WARN("%s - Failed to set environment variable", strerror(errno));
440 }
441 }
442
443 return 0;
444 }
445
446 static char *lxc_attach_getpwshell(uid_t uid)
447 {
448 int fd, ret;
449 pid_t pid;
450 int pipes[2];
451 char *result = NULL;
452
453 /* We need to fork off a process that runs the getent program, and we
454 * need to capture its output, so we use a pipe for that purpose.
455 */
456 ret = pipe(pipes);
457 if (ret < 0)
458 return NULL;
459
460 pid = fork();
461 if (pid < 0) {
462 close(pipes[0]);
463 close(pipes[1]);
464 return NULL;
465 }
466
467 if (pid) {
468 int status;
469 FILE *pipe_f;
470 int found = 0;
471 size_t line_bufsz = 0;
472 char *line = NULL;
473
474 close(pipes[1]);
475
476 pipe_f = fdopen(pipes[0], "r");
477 while (getline(&line, &line_bufsz, pipe_f) != -1) {
478 int i;
479 long value;
480 char *token;
481 char *endptr = NULL, *saveptr = NULL;
482
483 /* If we already found something, just continue to read
484 * until the pipe doesn't deliver any more data, but
485 * don't modify the existing data structure.
486 */
487 if (found)
488 continue;
489
490 /* Trim line on the right hand side. */
491 for (i = strlen(line); i > 0 && (line[i - 1] == '\n' || line[i - 1] == '\r'); --i)
492 line[i - 1] = '\0';
493
494 /* Split into tokens: first: user name. */
495 token = strtok_r(line, ":", &saveptr);
496 if (!token)
497 continue;
498 /* next: dummy password field */
499 token = strtok_r(NULL, ":", &saveptr);
500 if (!token)
501 continue;
502 /* next: user id */
503 token = strtok_r(NULL, ":", &saveptr);
504 value = token ? strtol(token, &endptr, 10) : 0;
505 if (!token || !endptr || *endptr || value == LONG_MIN || value == LONG_MAX)
506 continue;
507 /* dummy sanity check: user id matches */
508 if ((uid_t) value != uid)
509 continue;
510 /* skip fields: gid, gecos, dir, go to next field 'shell' */
511 for (i = 0; i < 4; i++) {
512 token = strtok_r(NULL, ":", &saveptr);
513 if (!token)
514 break;
515 }
516 if (!token)
517 continue;
518 free(result);
519 result = strdup(token);
520
521 /* Sanity check that there are no fields after that. */
522 token = strtok_r(NULL, ":", &saveptr);
523 if (token)
524 continue;
525
526 found = 1;
527 }
528
529 free(line);
530 fclose(pipe_f);
531 again:
532 if (waitpid(pid, &status, 0) < 0) {
533 if (errno == EINTR)
534 goto again;
535 free(result);
536 return NULL;
537 }
538
539 /* Some sanity checks. If anything even hinted at going wrong,
540 * we can't be sure we have a valid result, so we assume we
541 * don't.
542 */
543
544 if (!WIFEXITED(status)) {
545 free(result);
546 return NULL;
547 }
548
549 if (WEXITSTATUS(status) != 0) {
550 free(result);
551 return NULL;
552 }
553
554 if (!found) {
555 free(result);
556 return NULL;
557 }
558
559 return result;
560 } else {
561 char uid_buf[32];
562 char *arguments[] = {
563 "getent",
564 "passwd",
565 uid_buf,
566 NULL
567 };
568
569 close(pipes[0]);
570
571 /* We want to capture stdout. */
572 dup2(pipes[1], 1);
573 close(pipes[1]);
574
575 /* Get rid of stdin/stderr, so we try to associate it with
576 * /dev/null.
577 */
578 fd = open("/dev/null", O_RDWR);
579 if (fd < 0) {
580 close(0);
581 close(2);
582 } else {
583 dup2(fd, 0);
584 dup2(fd, 2);
585 close(fd);
586 }
587
588 /* Finish argument list. */
589 ret = snprintf(uid_buf, sizeof(uid_buf), "%ld", (long) uid);
590 if (ret <= 0)
591 exit(-1);
592
593 /* Try to run getent program. */
594 (void) execvp("getent", arguments);
595 exit(-1);
596 }
597 }
598
599 static void lxc_attach_get_init_uidgid(uid_t *init_uid, gid_t *init_gid)
600 {
601 FILE *proc_file;
602 char proc_fn[__PROC_STATUS_LEN];
603 int ret;
604 char *line = NULL;
605 size_t line_bufsz = 0;
606 long value = -1;
607 uid_t uid = (uid_t)-1;
608 gid_t gid = (gid_t)-1;
609
610 /* Read capabilities. */
611 snprintf(proc_fn, __PROC_STATUS_LEN, "/proc/%d/status", 1);
612
613 proc_file = fopen(proc_fn, "r");
614 if (!proc_file)
615 return;
616
617 while (getline(&line, &line_bufsz, proc_file) != -1) {
618 /* Format is: real, effective, saved set user, fs we only care
619 * about real uid.
620 */
621 ret = sscanf(line, "Uid: %ld", &value);
622 if (ret != EOF && ret == 1) {
623 uid = (uid_t)value;
624 } else {
625 ret = sscanf(line, "Gid: %ld", &value);
626 if (ret != EOF && ret == 1)
627 gid = (gid_t)value;
628 }
629 if (uid != (uid_t)-1 && gid != (gid_t)-1)
630 break;
631 }
632
633 fclose(proc_file);
634 free(line);
635
636 /* Only override arguments if we found something. */
637 if (uid != (uid_t)-1)
638 *init_uid = uid;
639 if (gid != (gid_t)-1)
640 *init_gid = gid;
641
642 /* TODO: we should also parse supplementary groups and use
643 * setgroups() to set them.
644 */
645 }
646
647 /* Help the optimizer along if it doesn't know that exit always exits. */
648 #define rexit(c) \
649 do { \
650 int __c = (c); \
651 _exit(__c); \
652 return __c; \
653 } while (0)
654
655 /* Define default options if no options are supplied by the user. */
656 static lxc_attach_options_t attach_static_default_options = LXC_ATTACH_OPTIONS_DEFAULT;
657
658 static bool fetch_seccomp(struct lxc_container *c,
659 lxc_attach_options_t *options)
660 {
661 char *path;
662
663 if (!(options->namespaces & CLONE_NEWNS) ||
664 !(options->attach_flags & LXC_ATTACH_LSM)) {
665 free(c->lxc_conf->seccomp);
666 c->lxc_conf->seccomp = NULL;
667 return true;
668 }
669
670 /* Remove current setting. */
671 if (!c->set_config_item(c, "lxc.seccomp", "") &&
672 !c->set_config_item(c, "lxc.seccomp.profile", "")) {
673 return false;
674 }
675
676 /* Fetch the current profile path over the cmd interface. */
677 path = c->get_running_config_item(c, "lxc.seccomp.profile");
678 if (!path) {
679 INFO("Failed to get running config item for lxc.seccomp.profile");
680 path = c->get_running_config_item(c, "lxc.seccomp");
681 }
682 if (!path) {
683 INFO("Failed to get running config item for lxc.seccomp");
684 return true;
685 }
686
687 /* Copy the value into the new lxc_conf. */
688 if (!c->set_config_item(c, "lxc.seccomp.profile", path)) {
689 free(path);
690 return false;
691 }
692 free(path);
693
694 /* Attempt to parse the resulting config. */
695 if (lxc_read_seccomp_config(c->lxc_conf) < 0) {
696 ERROR("Error reading seccomp policy.");
697 return false;
698 }
699
700 INFO("Retrieved seccomp policy.");
701 return true;
702 }
703
704 static bool no_new_privs(struct lxc_container *c, lxc_attach_options_t *options)
705 {
706 char *val;
707
708 /* Remove current setting. */
709 if (!c->set_config_item(c, "lxc.no_new_privs", ""))
710 return false;
711
712 /* Retrieve currently active setting. */
713 val = c->get_running_config_item(c, "lxc.no_new_privs");
714 if (!val) {
715 INFO("Failed to get running config item for lxc.no_new_privs.");
716 return false;
717 }
718
719 /* Set currently active setting. */
720 if (!c->set_config_item(c, "lxc.no_new_privs", val)) {
721 free(val);
722 return false;
723 }
724 free(val);
725
726 return true;
727 }
728
729 static signed long get_personality(const char *name, const char *lxcpath)
730 {
731 char *p;
732 signed long ret;
733
734 p = lxc_cmd_get_config_item(name, "lxc.arch", lxcpath);
735 if (!p)
736 return -1;
737
738 ret = lxc_config_parse_arch(p);
739 free(p);
740
741 return ret;
742 }
743
744 struct attach_clone_payload {
745 int ipc_socket;
746 int pty_fd;
747 lxc_attach_options_t *options;
748 struct lxc_proc_context_info *init_ctx;
749 lxc_attach_exec_t exec_function;
750 void *exec_payload;
751 };
752
753 static void lxc_put_attach_clone_payload(struct attach_clone_payload *p)
754 {
755 if (p->ipc_socket >= 0) {
756 shutdown(p->ipc_socket, SHUT_RDWR);
757 close(p->ipc_socket);
758 p->ipc_socket = -EBADF;
759 }
760
761 if (p->pty_fd >= 0) {
762 close(p->pty_fd);
763 p->pty_fd = -EBADF;
764 }
765
766 if (p->init_ctx) {
767 lxc_proc_put_context_info(p->init_ctx);
768 p->init_ctx = NULL;
769 }
770 }
771
772 static int attach_child_main(struct attach_clone_payload *payload)
773 {
774 int fd, lsm_fd, ret;
775 uid_t new_uid;
776 gid_t new_gid;
777 lxc_attach_options_t* options = payload->options;
778 struct lxc_proc_context_info* init_ctx = payload->init_ctx;
779 bool needs_lsm = (options->namespaces & CLONE_NEWNS) &&
780 (options->attach_flags & LXC_ATTACH_LSM) &&
781 init_ctx->lsm_label;
782
783 /* A description of the purpose of this functionality is provided in the
784 * lxc-attach(1) manual page. We have to remount here and not in the
785 * parent process, otherwise /proc may not properly reflect the new pid
786 * namespace.
787 */
788 if (!(options->namespaces & CLONE_NEWNS) &&
789 (options->attach_flags & LXC_ATTACH_REMOUNT_PROC_SYS)) {
790 ret = lxc_attach_remount_sys_proc();
791 if (ret < 0)
792 goto on_error;
793 TRACE("Remounted \"/proc\" and \"/sys\"");
794 }
795
796 /* Now perform additional attachments. */
797 #if HAVE_SYS_PERSONALITY_H
798 if (options->attach_flags & LXC_ATTACH_SET_PERSONALITY) {
799 long new_personality;
800
801 if (options->personality < 0)
802 new_personality = init_ctx->personality;
803 else
804 new_personality = options->personality;
805 ret = personality(new_personality);
806 if (ret < 0)
807 goto on_error;
808 TRACE("Set new personality");
809 }
810 #endif
811
812 if (options->attach_flags & LXC_ATTACH_DROP_CAPABILITIES) {
813 ret = lxc_attach_drop_privs(init_ctx);
814 if (ret < 0)
815 goto on_error;
816 TRACE("Dropped capabilities");
817 }
818
819 /* Always set the environment (specify (LXC_ATTACH_KEEP_ENV, NULL, NULL)
820 * if you want this to be a no-op).
821 */
822 ret = lxc_attach_set_environment(init_ctx,
823 options->env_policy,
824 options->extra_env_vars,
825 options->extra_keep_env);
826 if (ret < 0)
827 goto on_error;
828 TRACE("Set up environment");
829
830 /* This remark only affects fully unprivileged containers:
831 * Receive fd for LSM security module before we set{g,u}id(). The reason
832 * is that on set{g,u}id() the kernel will a) make us undumpable and b)
833 * we will change our effective uid. This means our effective uid will
834 * be different from the effective uid of the process that created us
835 * which means that this processs no longer has capabilities in our
836 * namespace including CAP_SYS_PTRACE. This means we will not be able to
837 * read and /proc/<pid> files for the process anymore when /proc is
838 * mounted with hidepid={1,2}. So let's get the lsm label fd before the
839 * set{g,u}id().
840 */
841 if (needs_lsm) {
842 ret = lxc_abstract_unix_recv_fds(payload->ipc_socket, &lsm_fd, 1, NULL, 0);
843 if (ret <= 0)
844 goto on_error;
845 TRACE("Received LSM label file descriptor %d from parent", lsm_fd);
846 }
847
848 if (options->stdin_fd > 0 && isatty(options->stdin_fd)) {
849 ret = lxc_make_controlling_terminal(options->stdin_fd);
850 if (ret < 0)
851 goto on_error;
852 }
853
854 /* Set {u,g}id. */
855 new_uid = 0;
856 new_gid = 0;
857 /* Ignore errors, we will fall back to root in that case (/proc was not
858 * mounted etc.).
859 */
860 if (options->namespaces & CLONE_NEWUSER)
861 lxc_attach_get_init_uidgid(&new_uid, &new_gid);
862
863 if (options->uid != (uid_t)-1)
864 new_uid = options->uid;
865 if (options->gid != (gid_t)-1)
866 new_gid = options->gid;
867
868 /* Try to set the {u,g}id combination. */
869 if (new_uid != 0 || new_gid != 0 || options->namespaces & CLONE_NEWUSER) {
870 ret = lxc_switch_uid_gid(new_uid, new_gid);
871 if (ret < 0)
872 goto on_error;
873
874 ret = lxc_setgroups(0, NULL);
875 if (ret < 0)
876 goto on_error;
877 }
878
879 if ((init_ctx->container && init_ctx->container->lxc_conf &&
880 init_ctx->container->lxc_conf->no_new_privs) ||
881 (options->attach_flags & LXC_ATTACH_NO_NEW_PRIVS)) {
882 ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
883 if (ret < 0)
884 goto on_error;
885 TRACE("Set PR_SET_NO_NEW_PRIVS");
886 }
887
888 if (needs_lsm) {
889 bool on_exec;
890
891 /* Change into our new LSM profile. */
892 on_exec = options->attach_flags & LXC_ATTACH_LSM_EXEC ? true : false;
893 ret = lsm_process_label_set_at(lsm_fd, init_ctx->lsm_label, on_exec);
894 close(lsm_fd);
895 if (ret < 0)
896 goto on_error;
897 TRACE("Set %s LSM label to \"%s\"", lsm_name(), init_ctx->lsm_label);
898 }
899
900 if (init_ctx->container && init_ctx->container->lxc_conf &&
901 init_ctx->container->lxc_conf->seccomp) {
902 ret = lxc_seccomp_load(init_ctx->container->lxc_conf);
903 if (ret < 0)
904 goto on_error;
905 TRACE("Loaded seccomp profile");
906 }
907 shutdown(payload->ipc_socket, SHUT_RDWR);
908 close(payload->ipc_socket);
909 payload->ipc_socket = -EBADF;
910 lxc_proc_put_context_info(init_ctx);
911
912 /* The following is done after the communication socket is shut down.
913 * That way, all errors that might (though unlikely) occur up until this
914 * point will have their messages printed to the original stderr (if
915 * logging is so configured) and not the fd the user supplied, if any.
916 */
917
918 /* Fd handling for stdin, stdout and stderr; ignore errors here, user
919 * may want to make sure the fds are closed, for example.
920 */
921 if (options->stdin_fd >= 0 && options->stdin_fd != STDIN_FILENO)
922 dup2(options->stdin_fd, STDIN_FILENO);
923
924 if (options->stdout_fd >= 0 && options->stdout_fd != STDOUT_FILENO)
925 dup2(options->stdout_fd, STDOUT_FILENO);
926
927 if (options->stderr_fd >= 0 && options->stderr_fd != STDERR_FILENO)
928 dup2(options->stderr_fd, STDERR_FILENO);
929
930 /* close the old fds */
931 if (options->stdin_fd > STDERR_FILENO)
932 close(options->stdin_fd);
933
934 if (options->stdout_fd > STDERR_FILENO)
935 close(options->stdout_fd);
936
937 if (options->stderr_fd > STDERR_FILENO)
938 close(options->stderr_fd);
939
940 /* Try to remove FD_CLOEXEC flag from stdin/stdout/stderr, but also
941 * here, ignore errors.
942 */
943 for (fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++) {
944 int flags;
945
946 flags = fcntl(fd, F_GETFL);
947 if (flags < 0)
948 continue;
949
950 if ((flags & FD_CLOEXEC) == 0)
951 continue;
952
953 ret = fcntl(fd, F_SETFL, flags & ~FD_CLOEXEC);
954 if (ret < 0) {
955 SYSERROR("Failed to clear FD_CLOEXEC from file descriptor %d", fd);
956 goto on_error;
957 }
958 }
959
960 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY) {
961 ret = lxc_terminal_prepare_login(payload->pty_fd);
962 if (ret < 0) {
963 SYSERROR("Failed to prepare pty file descriptor %d", payload->pty_fd);
964 goto on_error;
965 }
966 TRACE("Prepared pty file descriptor %d", payload->pty_fd);
967 }
968
969 /* We're done, so we can now do whatever the user intended us to do. */
970 rexit(payload->exec_function(payload->exec_payload));
971
972 on_error:
973 lxc_put_attach_clone_payload(payload);
974 rexit(EXIT_FAILURE);
975 }
976
977 static int lxc_attach_pty(struct lxc_conf *conf, struct lxc_terminal *pty)
978 {
979 int ret;
980
981 lxc_terminal_init(pty);
982
983 ret = lxc_terminal_create(pty);
984 if (ret < 0) {
985 SYSERROR("Failed to create pty");
986 return -1;
987 }
988
989 /* Shift ttys to container. */
990 ret = lxc_terminal_map_ids(conf, pty);
991 if (ret < 0) {
992 ERROR("Failed to shift pty");
993 goto on_error;
994 }
995
996 return 0;
997
998 on_error:
999 lxc_terminal_delete(pty);
1000 lxc_terminal_conf_free(pty);
1001 return -1;
1002 }
1003
1004 static int lxc_attach_pty_mainloop_init(struct lxc_terminal *pty,
1005 struct lxc_epoll_descr *descr)
1006 {
1007 int ret;
1008
1009 ret = lxc_mainloop_open(descr);
1010 if (ret < 0) {
1011 ERROR("Failed to create mainloop");
1012 return -1;
1013 }
1014
1015 ret = lxc_terminal_mainloop_add(descr, pty);
1016 if (ret < 0) {
1017 ERROR("Failed to add handlers to mainloop");
1018 lxc_mainloop_close(descr);
1019 return -1;
1020 }
1021
1022 return 0;
1023 }
1024
1025 static inline void lxc_attach_pty_close_master(struct lxc_terminal *pty)
1026 {
1027 if (pty->master < 0)
1028 return;
1029
1030 close(pty->master);
1031 pty->master = -EBADF;
1032 }
1033
1034 static inline void lxc_attach_pty_close_slave(struct lxc_terminal *pty)
1035 {
1036 if (pty->slave < 0)
1037 return;
1038
1039 close(pty->slave);
1040 pty->slave = -EBADF;
1041 }
1042
1043 static inline void lxc_attach_pty_close_peer(struct lxc_terminal *pty)
1044 {
1045 if (pty->peer < 0)
1046 return;
1047
1048 close(pty->peer);
1049 pty->peer = -EBADF;
1050 }
1051
1052 static inline void lxc_attach_pty_close_log(struct lxc_terminal *pty)
1053 {
1054 if (pty->log_fd < 0)
1055 return;
1056
1057 close(pty->log_fd);
1058 pty->log_fd = -EBADF;
1059 }
1060
1061 int lxc_attach(const char *name, const char *lxcpath,
1062 lxc_attach_exec_t exec_function, void *exec_payload,
1063 lxc_attach_options_t *options, pid_t *attached_process)
1064 {
1065 int i, ret, status;
1066 int ipc_sockets[2];
1067 char *cwd, *new_cwd;
1068 signed long personality;
1069 pid_t attached_pid, init_pid, pid;
1070 struct lxc_proc_context_info *init_ctx;
1071 struct lxc_terminal pty;
1072 struct lxc_conf *conf;
1073 struct attach_clone_payload payload = {0};
1074
1075 ret = access("/proc/self/ns", X_OK);
1076 if (ret) {
1077 ERROR("Does this kernel version support namespaces?");
1078 return -1;
1079 }
1080
1081 if (!options)
1082 options = &attach_static_default_options;
1083
1084 init_pid = lxc_cmd_get_init_pid(name, lxcpath);
1085 if (init_pid < 0) {
1086 ERROR("Failed to get init pid.");
1087 return -1;
1088 }
1089
1090 init_ctx = lxc_proc_get_context_info(init_pid);
1091 if (!init_ctx) {
1092 ERROR("Failed to get context of init process: %ld", (long)init_pid);
1093 return -1;
1094 }
1095
1096 personality = get_personality(name, lxcpath);
1097 if (init_ctx->personality < 0) {
1098 ERROR("Failed to get personality of the container");
1099 lxc_proc_put_context_info(init_ctx);
1100 return -1;
1101 }
1102 init_ctx->personality = personality;
1103
1104 init_ctx->container = lxc_container_new(name, lxcpath);
1105 if (!init_ctx->container) {
1106 lxc_proc_put_context_info(init_ctx);
1107 return -1;
1108 }
1109
1110 if (!init_ctx->container->lxc_conf) {
1111 init_ctx->container->lxc_conf = lxc_conf_init();
1112 if (!init_ctx->container->lxc_conf) {
1113 lxc_proc_put_context_info(init_ctx);
1114 return -ENOMEM;
1115 }
1116 }
1117 conf = init_ctx->container->lxc_conf;
1118
1119 if (!fetch_seccomp(init_ctx->container, options))
1120 WARN("Failed to get seccomp policy.");
1121
1122 if (!no_new_privs(init_ctx->container, options))
1123 WARN("Could not determine whether PR_SET_NO_NEW_PRIVS is set.");
1124
1125 cwd = getcwd(NULL, 0);
1126
1127 /* Determine which namespaces the container was created with
1128 * by asking lxc-start, if necessary.
1129 */
1130 if (options->namespaces == -1) {
1131 options->namespaces = lxc_cmd_get_clone_flags(name, lxcpath);
1132 /* call failed */
1133 if (options->namespaces == -1) {
1134 ERROR("Failed to automatically determine the "
1135 "namespaces which the container uses");
1136 free(cwd);
1137 lxc_proc_put_context_info(init_ctx);
1138 return -1;
1139 }
1140
1141 for (i = 0; i < LXC_NS_MAX; i++) {
1142 if (ns_info[i].clone_flag & CLONE_NEWCGROUP)
1143 if (!(options->attach_flags & LXC_ATTACH_MOVE_TO_CGROUP) ||
1144 !cgns_supported())
1145 continue;
1146
1147 if (ns_info[i].clone_flag & options->namespaces)
1148 continue;
1149
1150 init_ctx->ns_inherited |= ns_info[i].clone_flag;
1151 }
1152 }
1153
1154 pid = lxc_raw_getpid();
1155 for (i = 0; i < LXC_NS_MAX; i++) {
1156 int j, saved_errno;
1157
1158 if (options->namespaces & ns_info[i].clone_flag)
1159 init_ctx->ns_fd[i] = lxc_preserve_ns(init_pid, ns_info[i].proc_name);
1160 else if (init_ctx->ns_inherited & ns_info[i].clone_flag)
1161 init_ctx->ns_fd[i] = in_same_namespace(pid, init_pid, ns_info[i].proc_name);
1162 else
1163 continue;
1164 if (init_ctx->ns_fd[i] >= 0)
1165 continue;
1166
1167 if (init_ctx->ns_fd[i] == -EINVAL) {
1168 DEBUG("Inheriting %s namespace from %d",
1169 ns_info[i].proc_name, pid);
1170 init_ctx->ns_inherited &= ~ns_info[i].clone_flag;
1171 continue;
1172 }
1173
1174 /* We failed to preserve the namespace. */
1175 saved_errno = errno;
1176 /* Close all already opened file descriptors before we return an
1177 * error, so we don't leak them.
1178 */
1179 for (j = 0; j < i; j++)
1180 close(init_ctx->ns_fd[j]);
1181
1182 errno = saved_errno;
1183 SYSERROR("Failed to attach to %s namespace of %d",
1184 ns_info[i].proc_name, pid);
1185 free(cwd);
1186 lxc_proc_put_context_info(init_ctx);
1187 return -1;
1188 }
1189
1190 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY) {
1191 ret = lxc_attach_pty(conf, &pty);
1192 if (ret < 0) {
1193 ERROR("Failed to allocate pty");
1194 free(cwd);
1195 lxc_proc_put_context_info(init_ctx);
1196 return -1;
1197 }
1198
1199 pty.log_fd = options->log_fd;
1200 } else {
1201 lxc_terminal_init(&pty);
1202 }
1203
1204 /* Create a socket pair for IPC communication; set SOCK_CLOEXEC in order
1205 * to make sure we don't irritate other threads that want to fork+exec
1206 * away
1207 *
1208 * IMPORTANT: if the initial process is multithreaded and another call
1209 * just fork()s away without exec'ing directly after, the socket fd will
1210 * exist in the forked process from the other thread and any close() in
1211 * our own child process will not really cause the socket to close
1212 * properly, potentiall causing the parent to hang.
1213 *
1214 * For this reason, while IPC is still active, we have to use shutdown()
1215 * if the child exits prematurely in order to signal that the socket is
1216 * closed and cannot assume that the child exiting will automatically do
1217 * that.
1218 *
1219 * IPC mechanism: (X is receiver)
1220 * initial process intermediate attached
1221 * X <--- send pid of
1222 * attached proc,
1223 * then exit
1224 * send 0 ------------------------------------> X
1225 * [do initialization]
1226 * X <------------------------------------ send 1
1227 * [add to cgroup, ...]
1228 * send 2 ------------------------------------> X
1229 * [set LXC_ATTACH_NO_NEW_PRIVS]
1230 * X <------------------------------------ send 3
1231 * [open LSM label fd]
1232 * send 4 ------------------------------------> X
1233 * [set LSM label]
1234 * close socket close socket
1235 * run program
1236 */
1237 ret = socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
1238 if (ret < 0) {
1239 SYSERROR("Could not set up required IPC mechanism for attaching.");
1240 free(cwd);
1241 lxc_proc_put_context_info(init_ctx);
1242 return -1;
1243 }
1244
1245 /* Create intermediate subprocess, two reasons:
1246 * 1. We can't setns() in the child itself, since we want to make
1247 * sure we are properly attached to the pidns.
1248 * 2. Also, the initial thread has to put the attached process
1249 * into the cgroup, which we can only do if we didn't already
1250 * setns() (otherwise, user namespaces will hate us).
1251 */
1252 pid = fork();
1253 if (pid < 0) {
1254 SYSERROR("Failed to create first subprocess.");
1255 free(cwd);
1256 lxc_proc_put_context_info(init_ctx);
1257 return -1;
1258 }
1259
1260 if (pid) {
1261 int ret_parent = -1;
1262 pid_t to_cleanup_pid = pid;
1263 struct lxc_epoll_descr descr = {0};
1264
1265 /* close unneeded file descriptors */
1266 close(ipc_sockets[1]);
1267 free(cwd);
1268 lxc_proc_close_ns_fd(init_ctx);
1269 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY)
1270 lxc_attach_pty_close_slave(&pty);
1271
1272 /* Attach to cgroup, if requested. */
1273 if (options->attach_flags & LXC_ATTACH_MOVE_TO_CGROUP) {
1274 if (!cgroup_attach(name, lxcpath, pid))
1275 goto on_error;
1276 TRACE("Moved intermediate process %d into container's "
1277 "cgroups", pid);
1278 }
1279
1280 /* Setup /proc limits */
1281 if (!lxc_list_empty(&conf->procs)) {
1282 ret = setup_proc_filesystem(&conf->procs, pid);
1283 if (ret < 0)
1284 goto on_error;
1285 }
1286
1287 /* Setup resource limits */
1288 if (!lxc_list_empty(&conf->limits)) {
1289 ret = setup_resource_limits(&conf->limits, pid);
1290 if (ret < 0)
1291 goto on_error;
1292 }
1293
1294 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY) {
1295 ret = lxc_attach_pty_mainloop_init(&pty, &descr);
1296 if (ret < 0)
1297 goto on_error;
1298 TRACE("Initialized pty mainloop");
1299 }
1300
1301 /* Let the child process know to go ahead. */
1302 status = 0;
1303 ret = lxc_write_nointr(ipc_sockets[0], &status, sizeof(status));
1304 if (ret != sizeof(status))
1305 goto close_mainloop;
1306 TRACE("Told intermediate process to start initializing");
1307
1308 /* Get pid of attached process from intermediate process. */
1309 ret = lxc_read_nointr(ipc_sockets[0], &attached_pid, sizeof(attached_pid));
1310 if (ret != sizeof(attached_pid))
1311 goto close_mainloop;
1312 TRACE("Received pid %d of attached process in parent pid namespace", attached_pid);
1313
1314 /* Ignore SIGKILL (CTRL-C) and SIGQUIT (CTRL-\) - issue #313. */
1315 if (options->stdin_fd == 0) {
1316 signal(SIGINT, SIG_IGN);
1317 signal(SIGQUIT, SIG_IGN);
1318 }
1319
1320 /* Reap intermediate process. */
1321 ret = wait_for_pid(pid);
1322 if (ret < 0)
1323 goto close_mainloop;
1324 TRACE("Intermediate process %d exited", pid);
1325
1326 /* We will always have to reap the attached process now. */
1327 to_cleanup_pid = attached_pid;
1328
1329 /* Open LSM fd and send it to child. */
1330 if ((options->namespaces & CLONE_NEWNS) &&
1331 (options->attach_flags & LXC_ATTACH_LSM) &&
1332 init_ctx->lsm_label) {
1333 int ret = -1;
1334 int labelfd;
1335 bool on_exec;
1336
1337 on_exec = options->attach_flags & LXC_ATTACH_LSM_EXEC ? true : false;
1338 labelfd = lsm_process_label_fd_get(attached_pid, on_exec);
1339 if (labelfd < 0)
1340 goto close_mainloop;
1341 TRACE("Opened LSM label file descriptor %d", labelfd);
1342
1343 /* Send child fd of the LSM security module to write to. */
1344 ret = lxc_abstract_unix_send_fds(ipc_sockets[0], &labelfd, 1, NULL, 0);
1345 close(labelfd);
1346 if (ret <= 0) {
1347 SYSERROR("%d", (int)ret);
1348 goto close_mainloop;
1349 }
1350 TRACE("Sent LSM label file descriptor %d to child", labelfd);
1351 }
1352
1353 /* We're done, the child process should now execute whatever it
1354 * is that the user requested. The parent can now track it with
1355 * waitpid() or similar.
1356 */
1357
1358 *attached_process = attached_pid;
1359
1360 /* Now shut down communication with child, we're done. */
1361 shutdown(ipc_sockets[0], SHUT_RDWR);
1362 close(ipc_sockets[0]);
1363 ipc_sockets[0] = -1;
1364
1365 ret_parent = 0;
1366 to_cleanup_pid = -1;
1367 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY) {
1368 ret = lxc_mainloop(&descr, -1);
1369 if (ret < 0) {
1370 ret_parent = -1;
1371 to_cleanup_pid = attached_pid;
1372 }
1373 }
1374
1375 close_mainloop:
1376 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY)
1377 lxc_mainloop_close(&descr);
1378
1379 on_error:
1380 if (ipc_sockets[0] >= 0) {
1381 shutdown(ipc_sockets[0], SHUT_RDWR);
1382 close(ipc_sockets[0]);
1383 }
1384
1385 if (to_cleanup_pid > 0)
1386 (void)wait_for_pid(to_cleanup_pid);
1387
1388 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY) {
1389 lxc_terminal_delete(&pty);
1390 lxc_terminal_conf_free(&pty);
1391 }
1392 lxc_proc_put_context_info(init_ctx);
1393 return ret_parent;
1394 }
1395
1396 /* close unneeded file descriptors */
1397 close(ipc_sockets[0]);
1398 ipc_sockets[0] = -EBADF;
1399 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY) {
1400 lxc_attach_pty_close_master(&pty);
1401 lxc_attach_pty_close_peer(&pty);
1402 lxc_attach_pty_close_log(&pty);
1403 }
1404
1405 /* Wait for the parent to have setup cgroups. */
1406 ret = lxc_read_nointr(ipc_sockets[1], &status, sizeof(status));
1407 if (ret != sizeof(status)) {
1408 shutdown(ipc_sockets[1], SHUT_RDWR);
1409 lxc_proc_put_context_info(init_ctx);
1410 rexit(-1);
1411 }
1412 TRACE("Intermediate process starting to initialize");
1413
1414 /* Attach now, create another subprocess later, since pid namespaces
1415 * only really affect the children of the current process.
1416 */
1417 ret = lxc_attach_to_ns(init_pid, init_ctx);
1418 if (ret < 0) {
1419 ERROR("Failed to enter namespaces");
1420 shutdown(ipc_sockets[1], SHUT_RDWR);
1421 lxc_proc_put_context_info(init_ctx);
1422 rexit(-1);
1423 }
1424 /* close namespace file descriptors */
1425 lxc_proc_close_ns_fd(init_ctx);
1426
1427 /* Attach succeeded, try to cwd. */
1428 if (options->initial_cwd)
1429 new_cwd = options->initial_cwd;
1430 else
1431 new_cwd = cwd;
1432 if (new_cwd) {
1433 ret = chdir(new_cwd);
1434 if (ret < 0)
1435 WARN("Could not change directory to \"%s\"", new_cwd);
1436 }
1437 free(cwd);
1438
1439 /* Create attached process. */
1440 payload.ipc_socket = ipc_sockets[1];
1441 payload.options = options;
1442 payload.init_ctx = init_ctx;
1443 payload.pty_fd = pty.slave;
1444 payload.exec_function = exec_function;
1445 payload.exec_payload = exec_payload;
1446
1447 pid = lxc_raw_clone(CLONE_PARENT);
1448 if (pid < 0) {
1449 SYSERROR("Failed to clone attached process");
1450 shutdown(ipc_sockets[1], SHUT_RDWR);
1451 lxc_proc_put_context_info(init_ctx);
1452 rexit(-1);
1453 }
1454
1455 if (pid == 0) {
1456 ret = attach_child_main(&payload);
1457 if (ret < 0)
1458 ERROR("Failed to exec");
1459 _exit(EXIT_FAILURE);
1460 }
1461 if (options->attach_flags & LXC_ATTACH_ALLOCATE_PTY)
1462 lxc_attach_pty_close_slave(&pty);
1463
1464 /* Tell grandparent the pid of the pid of the newly created child. */
1465 ret = lxc_write_nointr(ipc_sockets[1], &pid, sizeof(pid));
1466 if (ret != sizeof(pid)) {
1467 /* If this really happens here, this is very unfortunate, since
1468 * the parent will not know the pid of the attached process and
1469 * will not be able to wait for it (and we won't either due to
1470 * CLONE_PARENT) so the parent won't be able to reap it and the
1471 * attached process will remain a zombie.
1472 */
1473 shutdown(ipc_sockets[1], SHUT_RDWR);
1474 lxc_proc_put_context_info(init_ctx);
1475 rexit(-1);
1476 }
1477 TRACE("Sending pid %d of attached process", pid);
1478
1479 /* The rest is in the hands of the initial and the attached process. */
1480 lxc_proc_put_context_info(init_ctx);
1481 rexit(0);
1482 }
1483
1484 int lxc_attach_run_command(void* payload)
1485 {
1486 lxc_attach_command_t* cmd = (lxc_attach_command_t*)payload;
1487
1488 execvp(cmd->program, cmd->argv);
1489 SYSERROR("Failed to exec \"%s\".", cmd->program);
1490 return -1;
1491 }
1492
1493 int lxc_attach_run_shell(void* payload)
1494 {
1495 uid_t uid;
1496 struct passwd *passwd;
1497 char *user_shell;
1498
1499 /* Ignore payload parameter. */
1500 (void)payload;
1501
1502 uid = getuid();
1503 passwd = getpwuid(uid);
1504
1505 /* This probably happens because of incompatible nss implementations in
1506 * host and container (remember, this code is still using the host's
1507 * glibc but our mount namespace is in the container) we may try to get
1508 * the information by spawning a [getent passwd uid] process and parsing
1509 * the result.
1510 */
1511 if (!passwd)
1512 user_shell = lxc_attach_getpwshell(uid);
1513 else
1514 user_shell = passwd->pw_shell;
1515 if (user_shell)
1516 execlp(user_shell, user_shell, (char *)NULL);
1517
1518 /* Executed if either no passwd entry or execvp fails, we will fall back
1519 * on /bin/sh as a default shell.
1520 */
1521 execlp("/bin/sh", "/bin/sh", (char *)NULL);
1522 SYSERROR("Failed to execute shell");
1523 if (!passwd)
1524 free(user_shell);
1525 return -1;
1526 }