]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/attach.c
attach: fix error checking for dup2()
[mirror_lxc.git] / src / lxc / attach.c
CommitLineData
cc73685d 1/* SPDX-License-Identifier: LGPL-2.1+ */
e0732705 2
d38dd64a
CB
3#ifndef _GNU_SOURCE
4#define _GNU_SOURCE 1
5#endif
e0732705
CS
6#include <errno.h>
7#include <fcntl.h>
c476bdce 8#include <grp.h>
604ca1c0 9#include <linux/unistd.h>
6f4f1937 10#include <pwd.h>
0bece477 11#include <pthread.h>
6f4f1937
CB
12#include <signal.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
6f4f1937 16#include <sys/mount.h>
e0732705
CS
17#include <sys/param.h>
18#include <sys/prctl.h>
5ec27989 19#include <sys/socket.h>
1ba0013f 20#include <sys/syscall.h>
905022f7 21#include <sys/wait.h>
604ca1c0
CB
22#include <termios.h>
23#include <unistd.h>
6f4f1937
CB
24
25#include <lxc/lxccontainer.h>
e0732705 26
81f466d0 27#include "af_unix.h"
e0732705
CS
28#include "attach.h"
29#include "caps.h"
9c4693b8 30#include "cgroup.h"
6f4f1937 31#include "commands.h"
2c4ea790 32#include "conf.h"
6f4f1937 33#include "config.h"
9b8e3c96 34#include "confile.h"
6f4f1937
CB
35#include "log.h"
36#include "lsm/lsm.h"
37#include "lxclock.h"
38#include "lxcseccomp.h"
604ca1c0 39#include "macro.h"
ba2be1a8 40#include "mainloop.h"
cd8f5663 41#include "memory_utils.h"
657256e0 42#include "mount_utils.h"
6f4f1937 43#include "namespace.h"
f40988c7 44#include "process_utils.h"
a9f0cecf 45#include "sync.h"
59524108 46#include "syscall_wrappers.h"
0ed9b1bc 47#include "terminal.h"
6f4f1937 48#include "utils.h"
9c4693b8
CS
49
50#if HAVE_SYS_PERSONALITY_H
51#include <sys/personality.h>
52#endif
e0732705 53
ac2cecc4 54lxc_log_define(attach, lxc);
e0732705 55
ef05d368
CB
56/* Define default options if no options are supplied by the user. */
57static lxc_attach_options_t attach_static_default_options = LXC_ATTACH_OPTIONS_DEFAULT;
58
20718e39
CB
59/*
60 * The context used to attach to the container.
61 * @attach_flags : the attach flags specified in lxc_attach_options_t
62 * @init_pid : the PID of the container's init process
63 * @dfd_init_pid : file descriptor to /proc/@init_pid
64 * __Must be closed in attach_context_security_barrier()__!
65 * @dfd_self_pid : file descriptor to /proc/self
66 * __Must be closed in attach_context_security_barrier()__!
67 * @setup_uid : if CLONE_NEWUSER is specified will contain the uid used
68 * during attach setup.
69 * @setup_gid : if CLONE_NEWUSER is specified will contain the gid used
70 * during attach setup.
71 * @target_uid : if CLONE_NEWUSER is specified the uid that the final program
72 * will be run with.
73 * @target_gid : if CLONE_NEWUSER is specified the gid that the final program
74 * will be run with.
75 * @lsm_label : LSM label to be used for the attaching process
76 * @container : the container we're attaching o
77 * @personality : the personality to use for the final program
78 * @capability : the capability mask of the @init_pid
79 * @ns_inherited : flags of namespaces that the final program will inherit from
80 * @init_pid
81 * @ns_fd : file descriptors to @init_pid's namespaces
82 */
ab919e5f 83struct attach_context {
afc691a0 84 unsigned int attach_flags;
500ed813 85 int init_pid;
25c659d5
CB
86 int dfd_init_pid;
87 int dfd_self_pid;
4475fabb
CB
88 uid_t setup_uid;
89 gid_t setup_gid;
90 uid_t target_uid;
91 gid_t target_gid;
0e304baa
CB
92 char *lsm_label;
93 struct lxc_container *container;
94 signed long personality;
95 unsigned long long capability_mask;
96 int ns_inherited;
97 int ns_fd[LXC_NS_MAX];
98 struct lsm_ops *lsm_ops;
99};
100
d8764025
CB
101static pid_t pidfd_get_pid(int pidfd)
102{
103 __do_free char *line = NULL;
104 __do_fclose FILE *f = NULL;
105 size_t len = 0;
106 char path[STRLITERALLEN("/proc/self/fdinfo/") +
107 INTTYPE_TO_STRLEN(int) + 1 ] = "/proc/self/fdinfo/";
108 int ret;
109
110 if (pidfd < 0)
111 return -EBADF;
112
113 ret = snprintf(path + STRLITERALLEN("/proc/self/fdinfo/"),
114 INTTYPE_TO_STRLEN(int), "%d", pidfd);
115 if (ret < 0 || ret > (size_t)INTTYPE_TO_STRLEN(int))
116 return ret_errno(EIO);
117
118 f = fopen_cloexec(path, "re");
119 if (!f)
120 return -errno;
121
122 while (getline(&line, &len, f) != -1) {
123 const char *prefix = "Pid:\t";
124 const size_t prefix_len = STRLITERALLEN("Pid:\t");
125 int pid = -ESRCH;
126 char *slider = line;
127
128 if (strncmp(slider, prefix, prefix_len))
129 continue;
130
131 slider += prefix_len;
132 slider = lxc_trim_whitespace_in_place(slider);
133
134 ret = lxc_safe_int(slider, &pid);
135 if (ret)
136 return -ret;
137
138 return pid;
139 }
140
141 return ret_errno(ENOENT);
142}
143
6e36c297
CB
144static inline bool sync_wake_pid(int fd, pid_t pid)
145{
146 return lxc_write_nointr(fd, &pid, sizeof(pid_t)) == sizeof(pid_t);
147}
148
149static inline bool sync_wait_pid(int fd, pid_t *pid)
150{
151 return lxc_read_nointr(fd, pid, sizeof(pid_t)) == sizeof(pid_t);
152}
153
154static inline bool sync_wake_fd(int fd, int fd_send)
155{
156 return lxc_abstract_unix_send_fds(fd, &fd_send, 1, NULL, 0) > 0;
157}
158
159static inline bool sync_wait_fd(int fd, int *fd_recv)
160{
161 return lxc_abstract_unix_recv_fds(fd, fd_recv, 1, NULL, 0) > 0;
162}
163
afc691a0
CB
164static bool attach_lsm(lxc_attach_options_t *options)
165{
166 return (options->namespaces & CLONE_NEWNS) &&
167 (options->attach_flags & (LXC_ATTACH_LSM | LXC_ATTACH_LSM_LABEL));
168}
169
9745eb8a
CB
170static struct attach_context *alloc_attach_context(void)
171{
581b849a
CB
172 struct attach_context *ctx;
173
174 ctx = zalloc(sizeof(struct attach_context));
175 if (!ctx)
176 return ret_set_errno(NULL, ENOMEM);
177
178 ctx->dfd_self_pid = -EBADF;
179 ctx->dfd_init_pid = -EBADF;
4475fabb
CB
180 ctx->init_pid = -ESRCH;
181 ctx->setup_uid = LXC_INVALID_UID;
182 ctx->setup_gid = LXC_INVALID_GID;
183 ctx->target_uid = LXC_INVALID_UID;
184 ctx->target_gid = LXC_INVALID_GID;
581b849a
CB
185
186 for (int i = 0; i < LXC_NS_MAX; i++)
187 ctx->ns_fd[i] = -EBADF;
188
189 return ctx;
9745eb8a
CB
190}
191
ee142207
CB
192static int get_personality(const char *name, const char *lxcpath,
193 signed long *personality)
d92c8e40
CB
194{
195 __do_free char *p = NULL;
ee142207 196 signed long per;
d92c8e40
CB
197
198 p = lxc_cmd_get_config_item(name, "lxc.arch", lxcpath);
ee142207
CB
199 if (!p) {
200 *personality = LXC_ARCH_UNCHANGED;
201 return 0;
202 }
d92c8e40 203
ee142207
CB
204 per = lxc_config_parse_arch(p);
205 if (per == LXC_ARCH_UNCHANGED)
206 return ret_errno(EINVAL);
207
208 *personality = per;
209 return 0;
d92c8e40
CB
210}
211
4475fabb
CB
212static int userns_setup_ids(struct attach_context *ctx,
213 lxc_attach_options_t *options,
214 uid_t init_hostuid, gid_t init_hostgid)
215{
216 __do_free char *line = NULL;
217 __do_fclose FILE *f_gidmap = NULL, *f_uidmap = NULL;
218 size_t len = 0;
219 uid_t init_ns_uid = LXC_INVALID_UID;
220 gid_t init_ns_gid = LXC_INVALID_GID;
221 uid_t nsuid, hostuid, range_uid;
222 gid_t nsgid, hostgid, range_gid;
223
224 if (!(options->namespaces & CLONE_NEWUSER))
225 return 0;
226
227 f_uidmap = fdopenat(ctx->dfd_init_pid, "uid_map", "re");
228 if (!f_uidmap)
229 return log_error_errno(-errno, errno, "Failed to open uid_map");
230
231 while (getline(&line, &len, f_uidmap) != -1) {
232 if (sscanf(line, "%u %u %u", &nsuid, &hostuid, &range_uid) != 3)
233 continue;
234
235 if (0 >= nsuid && 0 < nsuid + range_uid) {
236 ctx->setup_uid = 0;
237 TRACE("Container has mapping for uid 0");
238 break;
239 }
240
241 if (init_hostuid >= hostuid && init_hostuid < hostuid + range_uid) {
242 init_ns_uid = (init_hostuid - hostuid) + nsuid;
243 TRACE("Container runs with uid %d", init_ns_uid);
244 }
245 }
246
247 f_gidmap = fdopenat(ctx->dfd_init_pid, "gid_map", "re");
248 if (!f_gidmap)
249 return log_error_errno(-errno, errno, "Failed to open gid_map");
250
251 while (getline(&line, &len, f_gidmap) != -1) {
252 if (sscanf(line, "%u %u %u", &nsgid, &hostgid, &range_gid) != 3)
253 continue;
254
255 if (0 >= nsgid && 0 < nsgid + range_gid) {
256 ctx->setup_gid = 0;
257 TRACE("Container has mapping for gid 0");
258 break;
259 }
260
261 if (init_hostgid >= hostgid && init_hostgid < hostgid + range_gid) {
262 init_ns_gid = (init_hostgid - hostgid) + nsgid;
263 TRACE("Container runs with gid %d", init_ns_gid);
264 }
265 }
266
267 if (ctx->setup_uid == LXC_INVALID_UID)
268 ctx->setup_uid = init_ns_uid;
269
270 if (ctx->setup_gid == LXC_INVALID_UID)
271 ctx->setup_gid = init_ns_gid;
272
273 /*
274 * TODO: we should also parse supplementary groups and use
275 * setgroups() to set them.
276 */
277
278 return 0;
279}
280
281static void userns_target_ids(struct attach_context *ctx, lxc_attach_options_t *options)
282{
283 if (options->uid != LXC_INVALID_UID)
284 ctx->target_uid = options->uid;
285 else if (options->namespaces & CLONE_NEWUSER)
286 ctx->target_uid = ctx->setup_uid;
287 else
288 ctx->target_uid = 0;
289
290 if (ctx->target_uid == LXC_INVALID_UID)
291 WARN("Invalid uid specified");
292
293 if (options->gid != LXC_INVALID_GID)
294 ctx->target_gid = options->gid;
295 else if (options->namespaces & CLONE_NEWUSER)
296 ctx->target_gid = ctx->setup_gid;
297 else
298 ctx->target_uid = 0;
299
300 if (ctx->target_gid == LXC_INVALID_GID)
301 WARN("Invalid gid specified");
302}
303
9680e7b0
CB
304static int parse_init_status(struct attach_context *ctx, lxc_attach_options_t *options)
305{
306 __do_free char *line = NULL;
307 __do_fclose FILE *f = NULL;
308 size_t len = 0;
309 bool caps_found = false;
4475fabb
CB
310 int ret;
311 uid_t init_host_uid = LXC_INVALID_UID;
312 gid_t init_host_gid = LXC_INVALID_GID;
313
9680e7b0
CB
314
315 f = fdopenat(ctx->dfd_init_pid, "status", "re");
316 if (!f)
4475fabb 317 return log_error_errno(-errno, errno, "Failed to open status file");
9680e7b0
CB
318
319 while (getline(&line, &len, f) != -1) {
320 signed long value = -1;
9680e7b0 321
4475fabb
CB
322 /*
323 * Format is: real, effective, saved set user, fs we only care
324 * about real uid.
325 */
326 ret = sscanf(line, "Uid: %ld", &value);
327 if (ret != EOF && ret == 1) {
328 init_host_uid = (uid_t)value;
329 TRACE("Container's init process runs with hostuid %d", init_host_uid);
330 goto next;
331 }
9680e7b0 332
4475fabb
CB
333 ret = sscanf(line, "Gid: %ld", &value);
334 if (ret != EOF && ret == 1) {
335 init_host_gid = (gid_t)value;
336 TRACE("Container's init process runs with hostgid %d", init_host_gid);
337 goto next;
9680e7b0
CB
338 }
339
340 ret = sscanf(line, "CapBnd: %llx", &ctx->capability_mask);
341 if (ret != EOF && ret == 1) {
342 caps_found = true;
343 goto next;
344 }
345
346 next:
4475fabb
CB
347 if (init_host_uid != LXC_INVALID_UID &&
348 init_host_gid != LXC_INVALID_GID &&
349 caps_found)
9680e7b0
CB
350 break;
351
352 }
353
4475fabb
CB
354 ret = userns_setup_ids(ctx, options, init_host_uid, init_host_gid);
355 if (ret)
356 return log_error_errno(ret, errno, "Failed to get setup ids");
357 userns_target_ids(ctx, options);
358
9680e7b0
CB
359 /*
360 * TODO: we should also parse supplementary groups and use
361 * setgroups() to set them.
362 */
363
364 return 0;
365}
366
500ed813 367static int get_attach_context(struct attach_context *ctx,
afc691a0
CB
368 struct lxc_container *container,
369 lxc_attach_options_t *options)
e0732705 370{
9680e7b0
CB
371 __do_close int init_pidfd = -EBADF;
372 __do_free char *lsm_label = NULL;
6f4f1937 373 int ret;
c538837d 374 char path[LXC_PROC_PID_LEN];
e0732705 375
500ed813 376 ctx->container = container;
afc691a0 377 ctx->attach_flags = options->attach_flags;
500ed813 378
d8764025
CB
379 init_pidfd = lxc_cmd_get_init_pidfd(container->name, container->config_path);
380 if (init_pidfd >= 0)
381 ctx->init_pid = pidfd_get_pid(init_pidfd);
382 else
383 ctx->init_pid = lxc_cmd_get_init_pid(container->name, container->config_path);
384
500ed813
CB
385 if (ctx->init_pid < 0)
386 return log_error(-1, "Failed to get init pid");
387
595798bb 388 ctx->dfd_self_pid = openat(-EBADF, "/proc/self", O_CLOEXEC | O_NOCTTY | O_PATH | O_DIRECTORY);
9680e7b0 389 if (ctx->dfd_self_pid < 0)
4475fabb 390 return log_error_errno(-errno, errno, "Failed to open /proc/self");
25c659d5 391
c538837d
CB
392 ret = snprintf(path, sizeof(path), "/proc/%d", ctx->init_pid);
393 if (ret < 0 || ret >= sizeof(path))
394 return ret_errno(EIO);
e0732705 395
9680e7b0
CB
396 ctx->dfd_init_pid = openat(-EBADF, path, O_CLOEXEC | O_NOCTTY | O_NOFOLLOW | O_PATH | O_DIRECTORY);
397 if (ctx->dfd_init_pid < 0)
4475fabb 398 return log_error_errno(-errno, errno, "Failed to open /proc/%d", ctx->init_pid);
c538837d 399
d8764025
CB
400 if (init_pidfd >= 0) {
401 ret = lxc_raw_pidfd_send_signal(init_pidfd, 0, NULL, 0);
402 if (ret)
403 return log_error_errno(-errno, errno, "Container process exited or PID has been recycled");
404 else
405 TRACE("Container process still running and PID was not recycled");
406 }
407
4475fabb
CB
408 /* Determine which namespaces the container was created with. */
409 if (options->namespaces == -1) {
410 options->namespaces = lxc_cmd_get_clone_flags(container->name, container->config_path);
411 if (options->namespaces == -1)
412 return log_error_errno(-EINVAL, EINVAL, "Failed to automatically determine the namespaces which the container uses");
413
414 for (int i = 0; i < LXC_NS_MAX; i++) {
415 if (ns_info[i].clone_flag & CLONE_NEWCGROUP)
416 if (!(options->attach_flags & LXC_ATTACH_MOVE_TO_CGROUP) ||
417 !cgns_supported())
418 continue;
419
420 if (ns_info[i].clone_flag & options->namespaces)
421 continue;
422
423 ctx->ns_inherited |= ns_info[i].clone_flag;
424 }
425 }
426
9680e7b0
CB
427 ret = parse_init_status(ctx, options);
428 if (ret)
4475fabb 429 return log_error_errno(-errno, errno, "Failed to open parse file");
e0732705 430
4eb19ac0 431 ctx->lsm_ops = lsm_init_static();
d701d729 432
afc691a0
CB
433 if (attach_lsm(options)) {
434 if (ctx->attach_flags & LXC_ATTACH_LSM_LABEL)
435 lsm_label = options->lsm_label;
436 else
9680e7b0 437 lsm_label = ctx->lsm_ops->process_label_get_at(ctx->lsm_ops, ctx->dfd_init_pid);
afc691a0
CB
438 if (!lsm_label)
439 WARN("No security context received");
440 else
441 INFO("Retrieved security context %s", lsm_label);
442 }
9745eb8a 443 ctx->ns_inherited = 0;
e0732705 444
ee142207
CB
445 ret = get_personality(container->name, container->config_path, &ctx->personality);
446 if (ret)
447 return log_error_errno(ret, errno, "Failed to get personality of the container");
d92c8e40 448
1874ef74
CB
449 if (!ctx->container->lxc_conf) {
450 ctx->container->lxc_conf = lxc_conf_init();
451 if (!ctx->container->lxc_conf)
452 return log_error_errno(-ENOMEM, ENOMEM, "Failed to allocate new lxc config");
453 }
454
afc691a0 455 ctx->lsm_label = move_ptr(lsm_label);
9745eb8a 456 return 0;
e0732705
CS
457}
458
25c659d5 459static int in_same_namespace(int ns_fd_pid1, int ns_fd_pid2, const char *ns_path)
299d1198 460{
644e7393 461 __do_close int ns_fd1 = -EBADF, ns_fd2 = -EBADF;
3cc629fe 462 int ret = -1;
299d1198
CB
463 struct stat ns_st1, ns_st2;
464
25c659d5 465 ns_fd1 = openat(ns_fd_pid1, ns_path, O_CLOEXEC | O_NOCTTY | O_RDONLY);
134284c3 466 if (ns_fd1 < 0) {
25c659d5 467 /* The kernel does not support this namespace. This is not an error. */
134284c3
CB
468 if (errno == ENOENT)
469 return -EINVAL;
470
3cc629fe 471 return -1;
134284c3 472 }
299d1198 473
25c659d5 474 ns_fd2 = openat(ns_fd_pid2, ns_path, O_CLOEXEC | O_NOCTTY | O_RDONLY);
21d0acc2 475 if (ns_fd2 < 0)
3cc629fe 476 return -1;
299d1198
CB
477
478 ret = fstat(ns_fd1, &ns_st1);
21d0acc2 479 if (ret < 0)
3cc629fe 480 return -1;
299d1198
CB
481
482 ret = fstat(ns_fd2, &ns_st2);
21d0acc2 483 if (ret < 0)
3cc629fe 484 return -1;
299d1198
CB
485
486 /* processes are in the same namespace */
25c659d5
CB
487 if ((ns_st1.st_dev == ns_st2.st_dev) &&
488 (ns_st1.st_ino == ns_st2.st_ino))
3cc629fe 489 return -EINVAL;
299d1198
CB
490
491 /* processes are in different namespaces */
3cc629fe 492 return move_fd(ns_fd2);
299d1198
CB
493}
494
b7873c95
CB
495static int get_attach_context_nsfds(struct attach_context *ctx,
496 lxc_attach_options_t *options)
497{
b7873c95
CB
498 for (int i = 0; i < LXC_NS_MAX; i++) {
499 int j;
500
501 if (options->namespaces & ns_info[i].clone_flag)
25c659d5 502 ctx->ns_fd[i] = openat(ctx->dfd_init_pid, ns_info[i].proc_path, O_CLOEXEC | O_NOCTTY | O_RDONLY);
b7873c95 503 else if (ctx->ns_inherited & ns_info[i].clone_flag)
25c659d5 504 ctx->ns_fd[i] = in_same_namespace(ctx->dfd_self_pid, ctx->dfd_init_pid, ns_info[i].proc_path);
b7873c95
CB
505 else
506 continue;
507
508 if (ctx->ns_fd[i] >= 0)
509 continue;
510
511 if (ctx->ns_fd[i] == -EINVAL) {
25c659d5 512 DEBUG("Inheriting %s namespace", ns_info[i].proc_name);
b7873c95
CB
513 ctx->ns_inherited &= ~ns_info[i].clone_flag;
514 continue;
515 }
516
517 /* We failed to preserve the namespace. */
25c659d5 518 SYSERROR("Failed to preserve %s namespace of %d", ns_info[i].proc_name, ctx->init_pid);
b7873c95
CB
519
520 /* Close all already opened file descriptors before we return an
521 * error, so we don't leak them.
522 */
523 for (j = 0; j < i; j++)
524 close_prot_errno_disarm(ctx->ns_fd[j]);
525
526 return -1;
527 }
528
529 return 0;
530}
531
7e995801 532static inline void close_nsfds(struct attach_context *ctx)
b7873c95
CB
533{
534 for (int i = 0; i < LXC_NS_MAX; i++)
535 close_prot_errno_disarm(ctx->ns_fd[i]);
536}
537
538static void put_attach_context(struct attach_context *ctx)
539{
c538837d 540 if (ctx) {
afc691a0
CB
541 if (!(ctx->attach_flags & LXC_ATTACH_LSM_LABEL))
542 free_disarm(ctx->lsm_label);
25c659d5 543 close_prot_errno_disarm(ctx->dfd_init_pid);
b7873c95 544
c538837d
CB
545 if (ctx->container) {
546 lxc_container_put(ctx->container);
547 ctx->container = NULL;
548 }
b7873c95 549
c538837d
CB
550 close_nsfds(ctx);
551 free(ctx);
552 }
b7873c95
CB
553}
554
c538837d 555static int attach_context_container(struct attach_context *ctx)
99d50954 556{
ffeeed8b
CB
557 for (int i = 0; i < LXC_NS_MAX; i++) {
558 int ret;
99d50954 559
877f3a04 560 if (ctx->ns_fd[i] < 0)
26818618
CB
561 continue;
562
21d0acc2 563 ret = setns(ctx->ns_fd[i], ns_info[i].clone_flag);
ffeeed8b 564 if (ret < 0)
39b3b69b
CB
565 return log_error_errno(-1, errno,
566 "Failed to attach to %s namespace of %d",
567 ns_info[i].proc_name, ctx->init_pid);
99d50954 568
39b3b69b
CB
569 DEBUG("Attached to %s namespace of %d",
570 ns_info[i].proc_name, ctx->init_pid);
99d50954
CS
571 }
572
573 return 0;
574}
575
c538837d
CB
576/*
577 * Place anything in here that needs to be get rid of before we move into the
578 * container's context and fail hard if we can't.
579 */
580static bool attach_context_security_barrier(struct attach_context *ctx)
581{
582 if (ctx) {
25c659d5
CB
583 if (close(ctx->dfd_self_pid))
584 return false;
585 ctx->dfd_self_pid = -EBADF;
586
587 if (close(ctx->dfd_init_pid))
c538837d 588 return false;
25c659d5 589 ctx->dfd_init_pid = -EBADF;
c538837d
CB
590 }
591
592 return true;
593}
594
e4103cf6 595int lxc_attach_remount_sys_proc(void)
7a0b0b56
CS
596{
597 int ret;
598
599 ret = unshare(CLONE_NEWNS);
ffeeed8b
CB
600 if (ret < 0)
601 return log_error_errno(-1, errno, "Failed to unshare mount namespace");
7a0b0b56 602
9e61fb1f
CB
603 if (detect_shared_rootfs() && mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL))
604 SYSERROR("Failed to recursively turn root mount tree into dependent mount. Continuing...");
2c6f3fc9 605
8ce83369 606 /* Assume /proc is always mounted, so remount it. */
7a0b0b56 607 ret = umount2("/proc", MNT_DETACH);
ffeeed8b
CB
608 if (ret < 0)
609 return log_error_errno(-1, errno, "Failed to unmount /proc");
7a0b0b56 610
657256e0 611 ret = mount_filesystem("proc", "/proc", 0);
ffeeed8b
CB
612 if (ret < 0)
613 return log_error_errno(-1, errno, "Failed to remount /proc");
7a0b0b56 614
ffeeed8b
CB
615 /*
616 * Try to umount /sys. If it's not a mount point, we'll get EINVAL, then
8ce83369 617 * we ignore it because it may not have been mounted in the first place.
7a0b0b56
CS
618 */
619 ret = umount2("/sys", MNT_DETACH);
ffeeed8b
CB
620 if (ret < 0 && errno != EINVAL)
621 return log_error_errno(-1, errno, "Failed to unmount /sys");
622
623 /* Remount it. */
657256e0 624 if (ret == 0 && mount_filesystem("sysfs", "/sys", 0))
ffeeed8b 625 return log_error_errno(-1, errno, "Failed to remount /sys");
7a0b0b56
CS
626
627 return 0;
628}
629
677e1d27 630static int drop_capabilities(struct attach_context *ctx)
e0732705 631{
ffeeed8b 632 int last_cap;
e0732705 633
6f4f1937 634 last_cap = lxc_caps_last_cap();
ffeeed8b 635 for (int cap = 0; cap <= last_cap; cap++) {
e0732705
CS
636 if (ctx->capability_mask & (1LL << cap))
637 continue;
638
b81689a1 639 if (prctl(PR_CAPBSET_DROP, prctl_arg(cap), prctl_arg(0),
ffeeed8b
CB
640 prctl_arg(0), prctl_arg(0)))
641 return log_error_errno(-1, errno, "Failed to drop capability %d", cap);
ea918412 642
94ac256f 643 TRACE("Dropped capability %d", cap);
e0732705
CS
644 }
645
646 return 0;
647}
905022f7 648
ab919e5f 649static int lxc_attach_set_environment(struct attach_context *ctx,
7385273f 650 enum lxc_attach_env_policy_t policy,
6f4f1937 651 char **extra_env, char **extra_keep)
b3a39ba6 652{
3d55242a 653 int ret;
7385273f 654 struct lxc_list *iterator;
655
799f96fd 656 if (policy == LXC_ATTACH_CLEAR_ENV) {
3d5e9f48 657 int path_kept = 0;
6f4f1937 658 char **extra_keep_store = NULL;
3d5e9f48
CS
659
660 if (extra_keep) {
661 size_t count, i;
662
3d55242a
CB
663 for (count = 0; extra_keep[count]; count++)
664 ;
3d5e9f48 665
89b7bfe3 666 extra_keep_store = zalloc(count * sizeof(char *));
3d55242a 667 if (!extra_keep_store)
3d5e9f48 668 return -1;
3d55242a 669
3d5e9f48
CS
670 for (i = 0; i < count; i++) {
671 char *v = getenv(extra_keep[i]);
672 if (v) {
673 extra_keep_store[i] = strdup(v);
674 if (!extra_keep_store[i]) {
3d5e9f48
CS
675 while (i > 0)
676 free(extra_keep_store[--i]);
ea918412 677
3d5e9f48
CS
678 free(extra_keep_store);
679 return -1;
680 }
3d55242a 681
3d5e9f48
CS
682 if (strcmp(extra_keep[i], "PATH") == 0)
683 path_kept = 1;
684 }
3d5e9f48
CS
685 }
686 }
687
799f96fd 688 if (clearenv()) {
a9cab7e3 689 if (extra_keep_store) {
3d55242a
CB
690 char **p;
691
a9cab7e3
CS
692 for (p = extra_keep_store; *p; p++)
693 free(*p);
3d55242a 694
a9cab7e3
CS
695 free(extra_keep_store);
696 }
3d55242a 697
ffeeed8b 698 return log_error(-1, "Failed to clear environment");
3d5e9f48
CS
699 }
700
701 if (extra_keep_store) {
702 size_t i;
6f4f1937 703
3d5e9f48 704 for (i = 0; extra_keep[i]; i++) {
acd4922e 705 if (extra_keep_store[i]) {
3d55242a
CB
706 ret = setenv(extra_keep[i], extra_keep_store[i], 1);
707 if (ret < 0)
a24c5678 708 SYSWARN("Failed to set environment variable");
acd4922e 709 }
ea918412 710
3d5e9f48
CS
711 free(extra_keep_store[i]);
712 }
ea918412 713
3d5e9f48
CS
714 free(extra_keep_store);
715 }
716
8ce83369
CB
717 /* Always set a default path; shells and execlp tend to be fine
718 * without it, but there is a disturbing number of C programs
719 * out there that just assume that getenv("PATH") is never NULL
720 * and then die a painful segfault death.
721 */
3d55242a
CB
722 if (!path_kept) {
723 ret = setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 1);
724 if (ret < 0)
a24c5678 725 SYSWARN("Failed to set environment variable");
3d55242a 726 }
b3a39ba6
DW
727 }
728
3d55242a 729 ret = putenv("container=lxc");
ffeeed8b 730 if (ret < 0)
818a57fc 731 return log_warn(-1, "Failed to set environment variable");
b3a39ba6 732
7385273f 733 /* Set container environment variables.*/
640952e5 734 if (ctx->container->lxc_conf) {
ab919e5f 735 lxc_list_for_each(iterator, &ctx->container->lxc_conf->environment) {
3d55242a
CB
736 char *env_tmp;
737
738 env_tmp = strdup((char *)iterator->elem);
739 if (!env_tmp)
7385273f 740 return -1;
7385273f 741
3d55242a 742 ret = putenv(env_tmp);
ffeeed8b
CB
743 if (ret < 0)
744 return log_error_errno(-1, errno, "Failed to set environment variable: %s", (char *)iterator->elem);
7385273f 745 }
746 }
747
8ce83369 748 /* Set extra environment variables. */
3d5e9f48
CS
749 if (extra_env) {
750 for (; *extra_env; extra_env++) {
3d55242a 751 char *p;
ea918412 752
8ce83369
CB
753 /* We just assume the user knows what they are doing, so
754 * we don't do any checks.
755 */
3d55242a
CB
756 p = strdup(*extra_env);
757 if (!p)
3d5e9f48 758 return -1;
3d55242a
CB
759
760 ret = putenv(p);
761 if (ret < 0)
a24c5678 762 SYSWARN("Failed to set environment variable");
3d5e9f48
CS
763 }
764 }
765
b3a39ba6
DW
766 return 0;
767}
768
74a3920a 769static char *lxc_attach_getpwshell(uid_t uid)
905022f7 770{
1b9c9f5b 771 __do_free char *line = NULL, *result = NULL;
cd8f5663 772 __do_fclose FILE *pipe_f = NULL;
6f4f1937 773 int fd, ret;
905022f7
CS
774 pid_t pid;
775 int pipes[2];
3fa23ac3
CB
776 bool found = false;
777 size_t line_bufsz = 0;
905022f7 778
8ce83369
CB
779 /* We need to fork off a process that runs the getent program, and we
780 * need to capture its output, so we use a pipe for that purpose.
905022f7 781 */
3fa23ac3 782 ret = pipe2(pipes, O_CLOEXEC);
905022f7
CS
783 if (ret < 0)
784 return NULL;
785
786 pid = fork();
787 if (pid < 0) {
788 close(pipes[0]);
789 close(pipes[1]);
790 return NULL;
791 }
792
3fa23ac3 793 if (!pid) {
905022f7
CS
794 char uid_buf[32];
795 char *arguments[] = {
796 "getent",
797 "passwd",
798 uid_buf,
799 NULL
800 };
801
802 close(pipes[0]);
803
8ce83369 804 /* We want to capture stdout. */
3fa23ac3 805 ret = dup2(pipes[1], STDOUT_FILENO);
905022f7 806 close(pipes[1]);
3fa23ac3 807 if (ret < 0)
ea918412 808 _exit(EXIT_FAILURE);
905022f7 809
8ce83369
CB
810 /* Get rid of stdin/stderr, so we try to associate it with
811 * /dev/null.
905022f7 812 */
3fa23ac3 813 fd = open_devnull();
905022f7 814 if (fd < 0) {
3fa23ac3
CB
815 close(STDIN_FILENO);
816 close(STDERR_FILENO);
905022f7 817 } else {
3fa23ac3 818 (void)dup3(fd, STDIN_FILENO, O_CLOEXEC);
59f0e209 819 (void)dup3(fd, STDERR_FILENO, O_CLOEXEC);
905022f7
CS
820 close(fd);
821 }
822
8ce83369 823 /* Finish argument list. */
3fa23ac3
CB
824 ret = snprintf(uid_buf, sizeof(uid_buf), "%ld", (long)uid);
825 if (ret <= 0 || ret >= sizeof(uid_buf))
ea918412 826 _exit(EXIT_FAILURE);
905022f7 827
8ce83369 828 /* Try to run getent program. */
3fa23ac3 829 (void)execvp("getent", arguments);
ea918412 830 _exit(EXIT_FAILURE);
905022f7 831 }
3fa23ac3
CB
832
833 close(pipes[1]);
834
4110345b 835 pipe_f = fdopen(pipes[0], "re");
cf4026f1
CB
836 if (!pipe_f) {
837 close(pipes[0]);
838 goto reap_child;
839 }
840 /* Transfer ownership of pipes[0] to pipe_f. */
841 move_fd(pipes[0]);
842
3fa23ac3
CB
843 while (getline(&line, &line_bufsz, pipe_f) != -1) {
844 int i;
845 long value;
846 char *token;
847 char *endptr = NULL, *saveptr = NULL;
848
849 /* If we already found something, just continue to read
850 * until the pipe doesn't deliver any more data, but
851 * don't modify the existing data structure.
852 */
853 if (found)
854 continue;
855
18d4ffde 856 if (!line)
857 continue;
858
3fa23ac3
CB
859 /* Trim line on the right hand side. */
860 for (i = strlen(line); i > 0 && (line[i - 1] == '\n' || line[i - 1] == '\r'); --i)
861 line[i - 1] = '\0';
862
863 /* Split into tokens: first: user name. */
864 token = strtok_r(line, ":", &saveptr);
865 if (!token)
866 continue;
867
868 /* next: dummy password field */
869 token = strtok_r(NULL, ":", &saveptr);
870 if (!token)
871 continue;
872
873 /* next: user id */
874 token = strtok_r(NULL, ":", &saveptr);
875 value = token ? strtol(token, &endptr, 10) : 0;
876 if (!token || !endptr || *endptr || value == LONG_MIN ||
ea918412 877 value == LONG_MAX)
3fa23ac3
CB
878 continue;
879
880 /* dummy sanity check: user id matches */
881 if ((uid_t)value != uid)
882 continue;
883
884 /* skip fields: gid, gecos, dir, go to next field 'shell' */
885 for (i = 0; i < 4; i++) {
886 token = strtok_r(NULL, ":", &saveptr);
887 if (!token)
888 continue;
889 }
ea918412 890
3fa23ac3
CB
891 if (!token)
892 continue;
ea918412 893
1b9c9f5b 894 free_disarm(result);
3fa23ac3
CB
895 result = strdup(token);
896
897 /* Sanity check that there are no fields after that. */
898 token = strtok_r(NULL, ":", &saveptr);
899 if (token)
900 continue;
901
902 found = true;
903 }
ea918412 904
cf4026f1 905reap_child:
3fa23ac3 906 ret = wait_for_pid(pid);
1b9c9f5b 907 if (ret < 0)
3fa23ac3 908 return NULL;
3fa23ac3 909
1b9c9f5b 910 if (!found)
3fa23ac3 911 return NULL;
3fa23ac3 912
1b9c9f5b 913 return move_ptr(result);
905022f7 914}
cb3e61fa 915
d4db3d14 916static bool fetch_seccomp(struct lxc_container *c, lxc_attach_options_t *options)
2c4ea790 917{
cd8f5663 918 __do_free char *path = NULL;
d4db3d14
CB
919 int ret;
920 bool bret;
2eef2bda 921
afc691a0 922 if (!attach_lsm(options)) {
cca66e06 923 free_disarm(c->lxc_conf->seccomp.seccomp);
2c4ea790 924 return true;
bd4307f0 925 }
bd7b4e28 926
afc691a0 927 /* Remove current setting. */
d4db3d14 928 if (!c->set_config_item(c, "lxc.seccomp.profile", "") &&
ea918412 929 !c->set_config_item(c, "lxc.seccomp", ""))
2c4ea790 930 return false;
bd7b4e28 931
8ce83369 932 /* Fetch the current profile path over the cmd interface. */
0b427da0 933 path = c->get_running_config_item(c, "lxc.seccomp.profile");
bd7b4e28 934 if (!path) {
d4db3d14 935 INFO("Failed to retrieve lxc.seccomp.profile");
ea918412 936
0b427da0 937 path = c->get_running_config_item(c, "lxc.seccomp");
cca66e06
CB
938 if (!path)
939 return log_info(true, "Failed to retrieve lxc.seccomp");
bd7b4e28
SG
940 }
941
8ce83369 942 /* Copy the value into the new lxc_conf. */
d4db3d14 943 bret = c->set_config_item(c, "lxc.seccomp.profile", path);
d4db3d14
CB
944 if (!bret)
945 return false;
bd7b4e28 946
8ce83369 947 /* Attempt to parse the resulting config. */
d4db3d14 948 ret = lxc_read_seccomp_config(c->lxc_conf);
cca66e06
CB
949 if (ret < 0)
950 return log_error(false, "Failed to retrieve seccomp policy");
2c4ea790 951
cca66e06 952 return log_info(true, "Retrieved seccomp policy");
2e812c16
CB
953}
954
6f4f1937 955static bool no_new_privs(struct lxc_container *c, lxc_attach_options_t *options)
2e812c16 956{
cd8f5663 957 __do_free char *val = NULL;
2e812c16 958
2e812c16 959 /* Remove current setting. */
02d3b72b
CB
960 if (!c->set_config_item(c, "lxc.no_new_privs", ""))
961 return log_info(false, "Failed to unset lxc.no_new_privs");
2e812c16
CB
962
963 /* Retrieve currently active setting. */
964 val = c->get_running_config_item(c, "lxc.no_new_privs");
02d3b72b
CB
965 if (!val)
966 return log_info(false, "Failed to retrieve lxc.no_new_privs");
2e812c16
CB
967
968 /* Set currently active setting. */
cd8f5663 969 return c->set_config_item(c, "lxc.no_new_privs", val);
2c4ea790
SH
970}
971
338b230f 972struct attach_payload {
a998454a 973 int ipc_socket;
cecf3e83 974 int terminal_pts_fd;
a998454a 975 lxc_attach_options_t *options;
ab919e5f 976 struct attach_context *ctx;
a998454a
CB
977 lxc_attach_exec_t exec_function;
978 void *exec_payload;
979};
980
338b230f 981static void put_attach_payload(struct attach_payload *p)
ba2be1a8 982{
afc691a0
CB
983 if (p) {
984 close_prot_errno_disarm(p->ipc_socket);
985 close_prot_errno_disarm(p->terminal_pts_fd);
dd53c8af 986 put_attach_context(p->ctx);
ab919e5f 987 p->ctx = NULL;
b21da190 988 }
ba2be1a8
CB
989}
990
338b230f 991__noreturn static void do_attach(struct attach_payload *ap)
a998454a 992{
afc691a0
CB
993 lxc_attach_exec_t attach_function = move_ptr(ap->exec_function);
994 void *attach_function_args = move_ptr(ap->exec_payload);
427a8067 995 int lsm_fd, ret;
338b230f
CB
996 lxc_attach_options_t* options = ap->options;
997 struct attach_context *ctx = ap->ctx;
ab919e5f 998 struct lxc_conf *conf = ctx->container->lxc_conf;
a998454a
CB
999
1000 /* A description of the purpose of this functionality is provided in the
1001 * lxc-attach(1) manual page. We have to remount here and not in the
1002 * parent process, otherwise /proc may not properly reflect the new pid
1003 * namespace.
1004 */
1005 if (!(options->namespaces & CLONE_NEWNS) &&
1006 (options->attach_flags & LXC_ATTACH_REMOUNT_PROC_SYS)) {
1007 ret = lxc_attach_remount_sys_proc();
b75c344c
CB
1008 if (ret < 0)
1009 goto on_error;
ea918412 1010
b75c344c 1011 TRACE("Remounted \"/proc\" and \"/sys\"");
a998454a
CB
1012 }
1013
5b514ce3 1014 /* Now perform additional attachments. */
a998454a 1015#if HAVE_SYS_PERSONALITY_H
a998454a 1016 if (options->attach_flags & LXC_ATTACH_SET_PERSONALITY) {
b75c344c
CB
1017 long new_personality;
1018
1019 if (options->personality < 0)
ab919e5f 1020 new_personality = ctx->personality;
b75c344c
CB
1021 else
1022 new_personality = options->personality;
ea918412 1023
ee142207
CB
1024 if (new_personality != LXC_ARCH_UNCHANGED) {
1025 ret = personality(new_personality);
1026 if (ret < 0)
1027 goto on_error;
ea918412 1028
ee142207
CB
1029 TRACE("Set new personality");
1030 }
a998454a
CB
1031 }
1032#endif
1033
1034 if (options->attach_flags & LXC_ATTACH_DROP_CAPABILITIES) {
677e1d27 1035 ret = drop_capabilities(ctx);
b75c344c
CB
1036 if (ret < 0)
1037 goto on_error;
ea918412 1038
b75c344c 1039 TRACE("Dropped capabilities");
a998454a
CB
1040 }
1041
1042 /* Always set the environment (specify (LXC_ATTACH_KEEP_ENV, NULL, NULL)
1043 * if you want this to be a no-op).
1044 */
ab919e5f 1045 ret = lxc_attach_set_environment(ctx,
7385273f 1046 options->env_policy,
a998454a
CB
1047 options->extra_env_vars,
1048 options->extra_keep_env);
b75c344c
CB
1049 if (ret < 0)
1050 goto on_error;
ea918412 1051
b75c344c 1052 TRACE("Set up environment");
a998454a 1053
afc691a0
CB
1054 /*
1055 * This remark only affects fully unprivileged containers:
57de839f
CB
1056 * Receive fd for LSM security module before we set{g,u}id(). The reason
1057 * is that on set{g,u}id() the kernel will a) make us undumpable and b)
1058 * we will change our effective uid. This means our effective uid will
1059 * be different from the effective uid of the process that created us
1060 * which means that this processs no longer has capabilities in our
1061 * namespace including CAP_SYS_PTRACE. This means we will not be able to
1062 * read and /proc/<pid> files for the process anymore when /proc is
1063 * mounted with hidepid={1,2}. So let's get the lsm label fd before the
1064 * set{g,u}id().
1065 */
afc691a0 1066 if (attach_lsm(options) && ctx->lsm_label) {
f8e88e94 1067 if (!sync_wait_fd(ap->ipc_socket, ATTACH_SYNC_LSM(&lsm_fd))) {
6e36c297 1068 SYSERROR("Failed to receive lsm label fd");
b75c344c 1069 goto on_error;
9044b79e 1070 }
1071
57de839f
CB
1072 TRACE("Received LSM label file descriptor %d from parent", lsm_fd);
1073 }
1074
08ea9270 1075 if (options->stdin_fd > 0 && isatty(options->stdin_fd)) {
cd0a2b2f 1076 ret = lxc_make_controlling_terminal(options->stdin_fd);
08ea9270
CB
1077 if (ret < 0)
1078 goto on_error;
1079 }
1080
b58214ac
CB
1081 if (!lxc_setgroups(0, NULL) && errno != EPERM)
1082 goto on_error;
1083
4475fabb
CB
1084 if (options->namespaces & CLONE_NEWUSER)
1085 if (!lxc_switch_uid_gid(ctx->setup_uid, ctx->setup_gid))
b75c344c 1086 goto on_error;
936efc72 1087
afc691a0 1088 if (attach_lsm(options) && ctx->lsm_label) {
d3ba7c98 1089 bool on_exec;
a998454a
CB
1090
1091 /* Change into our new LSM profile. */
d3ba7c98 1092 on_exec = options->attach_flags & LXC_ATTACH_LSM_EXEC ? true : false;
afc691a0 1093 ret = ctx->lsm_ops->process_label_set_at(ctx->lsm_ops, lsm_fd, ctx->lsm_label, on_exec);
cb2420df 1094 close_prot_errno_disarm(lsm_fd);
b75c344c
CB
1095 if (ret < 0)
1096 goto on_error;
ea918412 1097
ab919e5f 1098 TRACE("Set %s LSM label to \"%s\"", ctx->lsm_ops->name, ctx->lsm_label);
a998454a
CB
1099 }
1100
640952e5 1101 if (conf->no_new_privs || (options->attach_flags & LXC_ATTACH_NO_NEW_PRIVS)) {
6ce8e678
AL
1102 ret = prctl(PR_SET_NO_NEW_PRIVS, prctl_arg(1), prctl_arg(0),
1103 prctl_arg(0), prctl_arg(0));
1104 if (ret < 0)
1105 goto on_error;
1106
1107 TRACE("Set PR_SET_NO_NEW_PRIVS");
1108 }
1109
640952e5 1110 if (conf->seccomp.seccomp) {
cdb2a47f 1111 ret = lxc_seccomp_load(conf);
b75c344c
CB
1112 if (ret < 0)
1113 goto on_error;
ea918412 1114
b75c344c 1115 TRACE("Loaded seccomp profile");
cdb2a47f 1116
338b230f 1117 ret = lxc_seccomp_send_notifier_fd(&conf->seccomp, ap->ipc_socket);
c3e3c21a
CB
1118 if (ret < 0)
1119 goto on_error;
a998454a 1120 }
ea918412 1121
a998454a
CB
1122 /* The following is done after the communication socket is shut down.
1123 * That way, all errors that might (though unlikely) occur up until this
1124 * point will have their messages printed to the original stderr (if
1125 * logging is so configured) and not the fd the user supplied, if any.
1126 */
1127
1128 /* Fd handling for stdin, stdout and stderr; ignore errors here, user
1129 * may want to make sure the fds are closed, for example.
1130 */
08ea9270 1131 if (options->stdin_fd >= 0 && options->stdin_fd != STDIN_FILENO)
40301d48 1132 if (dup2(options->stdin_fd, STDIN_FILENO) < 0)
a7563434 1133 SYSDEBUG("Failed to replace stdin with %d", options->stdin_fd);
08ea9270
CB
1134
1135 if (options->stdout_fd >= 0 && options->stdout_fd != STDOUT_FILENO)
40301d48 1136 if (dup2(options->stdout_fd, STDOUT_FILENO) < 0)
93b9960a 1137 SYSDEBUG("Failed to replace stdout with %d", options->stdout_fd);
08ea9270
CB
1138
1139 if (options->stderr_fd >= 0 && options->stderr_fd != STDERR_FILENO)
40301d48 1140 if (dup2(options->stderr_fd, STDERR_FILENO) < 0)
93b9960a 1141 SYSDEBUG("Failed to replace stderr with %d", options->stderr_fd);
a998454a
CB
1142
1143 /* close the old fds */
08ea9270 1144 if (options->stdin_fd > STDERR_FILENO)
a998454a 1145 close(options->stdin_fd);
08ea9270
CB
1146
1147 if (options->stdout_fd > STDERR_FILENO)
a998454a 1148 close(options->stdout_fd);
08ea9270
CB
1149
1150 if (options->stderr_fd > STDERR_FILENO)
a998454a
CB
1151 close(options->stderr_fd);
1152
427a8067
CB
1153 /*
1154 * Try to remove FD_CLOEXEC flag from stdin/stdout/stderr, but also
a998454a
CB
1155 * here, ignore errors.
1156 */
427a8067 1157 for (int fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++) {
3f62938a 1158 ret = fd_cloexec(fd, false);
b75c344c
CB
1159 if (ret < 0) {
1160 SYSERROR("Failed to clear FD_CLOEXEC from file descriptor %d", fd);
1161 goto on_error;
1162 }
a998454a
CB
1163 }
1164
9e84479f 1165 if (options->attach_flags & LXC_ATTACH_TERMINAL) {
338b230f 1166 ret = lxc_terminal_prepare_login(ap->terminal_pts_fd);
ba2be1a8 1167 if (ret < 0) {
338b230f 1168 SYSERROR("Failed to prepare terminal file descriptor %d", ap->terminal_pts_fd);
ba2be1a8
CB
1169 goto on_error;
1170 }
ea918412 1171
338b230f 1172 TRACE("Prepared terminal file descriptor %d", ap->terminal_pts_fd);
ba2be1a8
CB
1173 }
1174
afc691a0
CB
1175 put_attach_payload(ap);
1176
6aff5157 1177 /* Make sure that the processes STDIO is correctly owned by the user that we are switching to */
4475fabb 1178 ret = fix_stdio_permissions(ctx->target_uid);
c353b0b9 1179 if (ret)
a2c26bef 1180 INFO("Failed to adjust stdio permissions");
936efc72 1181
4475fabb
CB
1182 /* Avoid unnecessary syscalls. */
1183 if (ctx->setup_uid == ctx->target_uid)
1184 ctx->target_uid = LXC_INVALID_UID;
1185
1186 if (ctx->setup_gid == ctx->target_gid)
1187 ctx->target_gid = LXC_INVALID_GID;
1188
1189 if (!lxc_switch_uid_gid(ctx->target_uid, ctx->target_gid))
936efc72
CB
1190 goto on_error;
1191
a998454a 1192 /* We're done, so we can now do whatever the user intended us to do. */
afc691a0 1193 _exit(attach_function(attach_function_args));
b75c344c
CB
1194
1195on_error:
dab02267 1196 ERROR("Failed to attach to container");
c7ac2e1c 1197 _exit(EXIT_FAILURE);
a998454a
CB
1198}
1199
f797f05e 1200static int lxc_attach_terminal(const char *name, const char *lxcpath, struct lxc_conf *conf,
9e84479f 1201 struct lxc_terminal *terminal)
ba2be1a8
CB
1202{
1203 int ret;
1204
9e84479f 1205 lxc_terminal_init(terminal);
ba2be1a8 1206
8ea93a0f 1207 ret = lxc_terminal_create(name, lxcpath, conf, terminal);
c2af3a15
CB
1208 if (ret < 0)
1209 return log_error(-1, "Failed to create terminal");
ba2be1a8 1210
ba2be1a8 1211 return 0;
ba2be1a8
CB
1212}
1213
9e84479f
CB
1214static int lxc_attach_terminal_mainloop_init(struct lxc_terminal *terminal,
1215 struct lxc_epoll_descr *descr)
ba2be1a8
CB
1216{
1217 int ret;
1218
1219 ret = lxc_mainloop_open(descr);
c2af3a15
CB
1220 if (ret < 0)
1221 return log_error(-1, "Failed to create mainloop");
ba2be1a8 1222
9e84479f 1223 ret = lxc_terminal_mainloop_add(descr, terminal);
ba2be1a8 1224 if (ret < 0) {
ba2be1a8 1225 lxc_mainloop_close(descr);
c2af3a15 1226 return log_error(-1, "Failed to add handlers to mainloop");
ba2be1a8
CB
1227 }
1228
1229 return 0;
1230}
1231
36a94ce8 1232static inline void lxc_attach_terminal_close_ptx(struct lxc_terminal *terminal)
ba2be1a8 1233{
36a94ce8 1234 close_prot_errno_disarm(terminal->ptx);
ba2be1a8
CB
1235}
1236
cecf3e83 1237static inline void lxc_attach_terminal_close_pts(struct lxc_terminal *terminal)
ba2be1a8 1238{
41808e20 1239 close_prot_errno_disarm(terminal->pty);
ba2be1a8
CB
1240}
1241
9e84479f 1242static inline void lxc_attach_terminal_close_peer(struct lxc_terminal *terminal)
ba2be1a8 1243{
19a3e906 1244 close_prot_errno_disarm(terminal->peer);
ba2be1a8
CB
1245}
1246
9e84479f 1247static inline void lxc_attach_terminal_close_log(struct lxc_terminal *terminal)
ba2be1a8 1248{
19a3e906 1249 close_prot_errno_disarm(terminal->log_fd);
ba2be1a8
CB
1250}
1251
908fbc1a
CB
1252int lxc_attach(struct lxc_container *container, lxc_attach_exec_t exec_function,
1253 void *exec_payload, lxc_attach_options_t *options,
1254 pid_t *attached_process)
9c4693b8 1255{
6f9fe5d0 1256 int ret_parent = -1;
6f9fe5d0 1257 struct lxc_epoll_descr descr = {};
a9f0cecf 1258 int ret;
26abd7ea 1259 char *name, *lxcpath;
9c4693b8 1260 int ipc_sockets[2];
500ed813 1261 pid_t attached_pid, pid, to_cleanup_pid;
ab919e5f 1262 struct attach_context *ctx;
9e84479f 1263 struct lxc_terminal terminal;
1cce35e6 1264 struct lxc_conf *conf;
9c4693b8 1265
908fbc1a 1266 if (!container)
540a2f70 1267 return ret_set_errno(-1, EINVAL);
908fbc1a
CB
1268
1269 if (!lxc_container_get(container))
540a2f70 1270 return ret_set_errno(-1, EINVAL);
908fbc1a
CB
1271
1272 name = container->name;
1273 lxcpath = container->config_path;
1274
afc691a0 1275 if (!options) {
9c4693b8 1276 options = &attach_static_default_options;
afc691a0
CB
1277 options->lsm_label = NULL;
1278 }
9c4693b8 1279
9745eb8a 1280 ctx = alloc_attach_context();
ab919e5f 1281 if (!ctx) {
9745eb8a
CB
1282 lxc_container_put(container);
1283 return log_error_errno(-ENOMEM, ENOMEM, "Failed to allocate attach context");
1284 }
1285
afc691a0 1286 ret = get_attach_context(ctx, container, options);
9745eb8a 1287 if (ret) {
7e995801 1288 put_attach_context(ctx);
74ce42b5 1289 return log_error(-1, "Failed to get attach context");
9c4693b8
CS
1290 }
1291
ab919e5f 1292 conf = ctx->container->lxc_conf;
ba773996 1293
ab919e5f 1294 if (!fetch_seccomp(ctx->container, options))
ae026f55 1295 WARN("Failed to get seccomp policy");
2c4ea790 1296
ab919e5f 1297 if (!no_new_privs(ctx->container, options))
ae026f55 1298 WARN("Could not determine whether PR_SET_NO_NEW_PRIVS is set");
2e812c16 1299
b7873c95
CB
1300 ret = get_attach_context_nsfds(ctx, options);
1301 if (ret) {
b7873c95 1302 lxc_container_put(container);
74ce42b5 1303 return log_error(-1, "Failed to get namespace file descriptors");
9c4693b8
CS
1304 }
1305
9e84479f 1306 if (options->attach_flags & LXC_ATTACH_TERMINAL) {
f797f05e 1307 ret = lxc_attach_terminal(name, lxcpath, conf, &terminal);
ba2be1a8 1308 if (ret < 0) {
dd53c8af 1309 put_attach_context(ctx);
74ce42b5 1310 return log_error(-1, "Failed to setup new terminal");
ba2be1a8
CB
1311 }
1312
9e84479f 1313 terminal.log_fd = options->log_fd;
c948657b 1314 } else {
9e84479f 1315 lxc_terminal_init(&terminal);
ba2be1a8
CB
1316 }
1317
8ce83369
CB
1318 /* Create a socket pair for IPC communication; set SOCK_CLOEXEC in order
1319 * to make sure we don't irritate other threads that want to fork+exec
1320 * away
9c4693b8
CS
1321 *
1322 * IMPORTANT: if the initial process is multithreaded and another call
1323 * just fork()s away without exec'ing directly after, the socket fd will
1324 * exist in the forked process from the other thread and any close() in
8ce83369 1325 * our own child process will not really cause the socket to close
1d801260 1326 * properly, potentially causing the parent to hang.
9c4693b8
CS
1327 *
1328 * For this reason, while IPC is still active, we have to use shutdown()
8ce83369
CB
1329 * if the child exits prematurely in order to signal that the socket is
1330 * closed and cannot assume that the child exiting will automatically do
1331 * that.
9c4693b8
CS
1332 *
1333 * IPC mechanism: (X is receiver)
1334 * initial process intermediate attached
1335 * X <--- send pid of
1336 * attached proc,
1337 * then exit
1338 * send 0 ------------------------------------> X
1339 * [do initialization]
1340 * X <------------------------------------ send 1
1341 * [add to cgroup, ...]
1342 * send 2 ------------------------------------> X
81f466d0
CB
1343 * [set LXC_ATTACH_NO_NEW_PRIVS]
1344 * X <------------------------------------ send 3
1345 * [open LSM label fd]
1346 * send 4 ------------------------------------> X
1347 * [set LSM label]
9c4693b8
CS
1348 * close socket close socket
1349 * run program
1350 */
1351 ret = socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
1352 if (ret < 0) {
dd53c8af 1353 put_attach_context(ctx);
74ce42b5 1354 return log_error_errno(-1, errno, "Could not set up required IPC mechanism for attaching");
9c4693b8
CS
1355 }
1356
e3f0e436
CB
1357 /* Create intermediate subprocess, two reasons:
1358 * 1. We can't setns() in the child itself, since we want to make
8ce83369 1359 * sure we are properly attached to the pidns.
e3f0e436 1360 * 2. Also, the initial thread has to put the attached process
8ce83369
CB
1361 * into the cgroup, which we can only do if we didn't already
1362 * setns() (otherwise, user namespaces will hate us).
9c4693b8
CS
1363 */
1364 pid = fork();
9c4693b8 1365 if (pid < 0) {
dd53c8af 1366 put_attach_context(ctx);
74ce42b5 1367 return log_error_errno(-1, errno, "Failed to create first subprocess");
9c4693b8
CS
1368 }
1369
4f25e72f 1370 if (pid == 0) {
26abd7ea 1371 char *cwd, *new_cwd;
a588a482 1372
ba2be1a8 1373 /* close unneeded file descriptors */
4f25e72f 1374 close_prot_errno_disarm(ipc_sockets[0]);
2202afc9 1375
4f25e72f
CB
1376 if (options->attach_flags & LXC_ATTACH_TERMINAL) {
1377 lxc_attach_terminal_close_ptx(&terminal);
1378 lxc_attach_terminal_close_peer(&terminal);
1379 lxc_attach_terminal_close_log(&terminal);
f4364484
SG
1380 }
1381
4f25e72f 1382 /* Wait for the parent to have setup cgroups. */
6e48e7c5 1383 if (!sync_wait(ipc_sockets[1], ATTACH_SYNC_CGROUP)) {
4f25e72f 1384 shutdown(ipc_sockets[1], SHUT_RDWR);
dd53c8af 1385 put_attach_context(ctx);
4f25e72f 1386 _exit(EXIT_FAILURE);
bb2ada6f
CB
1387 }
1388
c538837d
CB
1389 if (!attach_context_security_barrier(ctx)) {
1390 shutdown(ipc_sockets[1], SHUT_RDWR);
1391 put_attach_context(ctx);
1392 _exit(EXIT_FAILURE);
1393 }
1394
4f25e72f
CB
1395 TRACE("Intermediate process starting to initialize");
1396
a588a482
CB
1397 cwd = getcwd(NULL, 0);
1398
c538837d
CB
1399 /*
1400 * Attach now, create another subprocess later, since pid
1401 * namespaces only really affect the children of the current
1402 * process.
1403 *
1404 * Note that this is a crucial barrier. We're no moving into
1405 * the container's context so we need to make sure to not leak
1406 * anything sensitive. That especially means things such as
1407 * open file descriptors!
4f25e72f 1408 */
c538837d 1409 ret = attach_context_container(ctx);
4f25e72f
CB
1410 if (ret < 0) {
1411 ERROR("Failed to enter namespaces");
1412 shutdown(ipc_sockets[1], SHUT_RDWR);
dd53c8af 1413 put_attach_context(ctx);
4f25e72f 1414 _exit(EXIT_FAILURE);
ba2be1a8
CB
1415 }
1416
4f25e72f 1417 /* close namespace file descriptors */
7e995801 1418 close_nsfds(ctx);
ea918412 1419
4f25e72f
CB
1420 /* Attach succeeded, try to cwd. */
1421 if (options->initial_cwd)
1422 new_cwd = options->initial_cwd;
1423 else
1424 new_cwd = cwd;
1425 if (new_cwd) {
1426 ret = chdir(new_cwd);
1427 if (ret < 0)
1428 WARN("Could not change directory to \"%s\"", new_cwd);
ba2be1a8 1429 }
a588a482 1430 free_disarm(cwd);
c6d09e15 1431
4f25e72f 1432 /* Create attached process. */
4f25e72f
CB
1433 pid = lxc_raw_clone(CLONE_PARENT, NULL);
1434 if (pid < 0) {
1435 SYSERROR("Failed to clone attached process");
1436 shutdown(ipc_sockets[1], SHUT_RDWR);
dd53c8af 1437 put_attach_context(ctx);
4f25e72f
CB
1438 _exit(EXIT_FAILURE);
1439 }
f4364484 1440
4f25e72f 1441 if (pid == 0) {
338b230f 1442 struct attach_payload ap = {
a64902ab
CB
1443 .ipc_socket = ipc_sockets[1],
1444 .options = options,
1445 .ctx = ctx,
1446 .terminal_pts_fd = terminal.pty,
1447 .exec_function = exec_function,
1448 .exec_payload = exec_payload,
1449 };
1450
4f25e72f
CB
1451 if (options->attach_flags & LXC_ATTACH_TERMINAL) {
1452 ret = lxc_terminal_signal_sigmask_safe_blocked(&terminal);
1453 if (ret < 0) {
1454 SYSERROR("Failed to reset signal mask");
1455 _exit(EXIT_FAILURE);
1456 }
1457 }
ea918412 1458
a64902ab 1459 /* Does not return. */
338b230f 1460 do_attach(&ap);
62183f1a 1461 }
2eef2bda 1462
4f25e72f
CB
1463 if (options->attach_flags & LXC_ATTACH_TERMINAL)
1464 lxc_attach_terminal_close_pts(&terminal);
ea918412 1465
4f25e72f 1466 /* Tell grandparent the pid of the pid of the newly created child. */
f8e88e94 1467 if (!sync_wake_pid(ipc_sockets[1], ATTACH_SYNC_PID(pid))) {
4f25e72f
CB
1468 /* If this really happens here, this is very unfortunate, since
1469 * the parent will not know the pid of the attached process and
1470 * will not be able to wait for it (and we won't either due to
1471 * CLONE_PARENT) so the parent won't be able to reap it and the
1472 * attached process will remain a zombie.
1473 */
1474 shutdown(ipc_sockets[1], SHUT_RDWR);
dd53c8af 1475 put_attach_context(ctx);
4f25e72f
CB
1476 _exit(EXIT_FAILURE);
1477 }
9c4693b8 1478
4f25e72f 1479 TRACE("Sending pid %d of attached process", pid);
9c4693b8 1480
4f25e72f 1481 /* The rest is in the hands of the initial and the attached process. */
dd53c8af 1482 put_attach_context(ctx);
4f25e72f
CB
1483 _exit(EXIT_SUCCESS);
1484 }
6f4f1937 1485
4f25e72f 1486 to_cleanup_pid = pid;
ea918412 1487
4f25e72f 1488 /* close unneeded file descriptors */
cb2420df 1489 close_prot_errno_disarm(ipc_sockets[1]);
7e995801 1490 close_nsfds(ctx);
4f25e72f
CB
1491 if (options->attach_flags & LXC_ATTACH_TERMINAL)
1492 lxc_attach_terminal_close_pts(&terminal);
81f466d0 1493
4f25e72f
CB
1494 /* Attach to cgroup, if requested. */
1495 if (options->attach_flags & LXC_ATTACH_MOVE_TO_CGROUP) {
1496 /*
1497 * If this is the unified hierarchy cgroup_attach() is
1498 * enough.
1499 */
1500 ret = cgroup_attach(conf, name, lxcpath, pid);
1501 if (ret) {
1502 call_cleaner(cgroup_exit) struct cgroup_ops *cgroup_ops = NULL;
9044b79e 1503
4f25e72f
CB
1504 cgroup_ops = cgroup_init(conf);
1505 if (!cgroup_ops)
1506 goto on_error;
9044b79e 1507
4f25e72f
CB
1508 if (!cgroup_ops->attach(cgroup_ops, conf, name, lxcpath, pid))
1509 goto on_error;
81f466d0 1510 }
4f25e72f
CB
1511 TRACE("Moved intermediate process %d into container's cgroups", pid);
1512 }
81f466d0 1513
4f25e72f
CB
1514 /* Setup /proc limits */
1515 if (!lxc_list_empty(&conf->procs)) {
1516 ret = setup_proc_filesystem(&conf->procs, pid);
1517 if (ret < 0)
1518 goto on_error;
4f3b6a85
CB
1519
1520 TRACE("Setup /proc/%d settings", pid);
4f25e72f 1521 }
cdb2a47f 1522
4f25e72f
CB
1523 /* Setup resource limits */
1524 if (!lxc_list_empty(&conf->limits)) {
1525 ret = setup_resource_limits(&conf->limits, pid);
1526 if (ret < 0)
1527 goto on_error;
4f3b6a85
CB
1528
1529 TRACE("Setup resource limits");
4f25e72f 1530 }
cdb2a47f 1531
4f25e72f
CB
1532 if (options->attach_flags & LXC_ATTACH_TERMINAL) {
1533 ret = lxc_attach_terminal_mainloop_init(&terminal, &descr);
1534 if (ret < 0)
1535 goto on_error;
9c4693b8 1536
4f25e72f
CB
1537 TRACE("Initialized terminal mainloop");
1538 }
9c4693b8 1539
4f25e72f 1540 /* Let the child process know to go ahead. */
6e48e7c5 1541 if (!sync_wake(ipc_sockets[0], ATTACH_SYNC_CGROUP))
4f25e72f 1542 goto close_mainloop;
ba2be1a8 1543
4f25e72f 1544 TRACE("Told intermediate process to start initializing");
ea918412 1545
4f25e72f 1546 /* Get pid of attached process from intermediate process. */
f8e88e94 1547 if (!sync_wait_pid(ipc_sockets[0], ATTACH_SYNC_PID(&attached_pid)))
4f25e72f 1548 goto close_mainloop;
ba2be1a8 1549
4f25e72f 1550 TRACE("Received pid %d of attached process in parent pid namespace", attached_pid);
ba2be1a8 1551
4f25e72f 1552 /* Ignore SIGKILL (CTRL-C) and SIGQUIT (CTRL-\) - issue #313. */
5d2b46fb 1553 if (options->stdin_fd == STDIN_FILENO) {
4f25e72f
CB
1554 signal(SIGINT, SIG_IGN);
1555 signal(SIGQUIT, SIG_IGN);
1556 }
ba2be1a8 1557
4f25e72f
CB
1558 /* Reap intermediate process. */
1559 ret = wait_for_pid(pid);
1560 if (ret < 0)
1561 goto close_mainloop;
ba2be1a8 1562
4f25e72f 1563 TRACE("Intermediate process %d exited", pid);
ea918412 1564
4f25e72f
CB
1565 /* We will always have to reap the attached process now. */
1566 to_cleanup_pid = attached_pid;
9c4693b8 1567
4f25e72f 1568 /* Open LSM fd and send it to child. */
afc691a0 1569 if (attach_lsm(options) && ctx->lsm_label) {
ad001fb6 1570 __do_close int labelfd = -EBADF;
4f25e72f 1571 bool on_exec;
ea918412 1572
4f25e72f 1573 on_exec = options->attach_flags & LXC_ATTACH_LSM_EXEC ? true : false;
afc691a0 1574 labelfd = ctx->lsm_ops->process_label_fd_get(ctx->lsm_ops, attached_pid, on_exec);
4f25e72f
CB
1575 if (labelfd < 0)
1576 goto close_mainloop;
9c4693b8 1577
4f25e72f 1578 TRACE("Opened LSM label file descriptor %d", labelfd);
ea918412 1579
4f25e72f 1580 /* Send child fd of the LSM security module to write to. */
f8e88e94 1581 if (!sync_wake_fd(ipc_sockets[0], ATTACH_SYNC_LSM(labelfd))) {
6e36c297 1582 SYSERROR("Failed to send lsm label fd");
4f25e72f
CB
1583 goto close_mainloop;
1584 }
1585
4f25e72f 1586 TRACE("Sent LSM label file descriptor %d to child", labelfd);
9c4693b8 1587 }
ea918412 1588
4f25e72f
CB
1589 if (conf->seccomp.seccomp) {
1590 ret = lxc_seccomp_recv_notifier_fd(&conf->seccomp, ipc_sockets[0]);
1591 if (ret < 0)
1592 goto close_mainloop;
9c4693b8 1593
4f25e72f 1594 ret = lxc_seccomp_add_notifier(name, lxcpath, &conf->seccomp);
d6d979bc 1595 if (ret < 0)
4f25e72f 1596 goto close_mainloop;
d6d979bc 1597 }
9c4693b8 1598
4f25e72f
CB
1599 /* We're done, the child process should now execute whatever it
1600 * is that the user requested. The parent can now track it with
1601 * waitpid() or similar.
1602 */
9c4693b8 1603
4f25e72f 1604 *attached_process = attached_pid;
a998454a 1605
4f25e72f
CB
1606 /* Now shut down communication with child, we're done. */
1607 shutdown(ipc_sockets[0], SHUT_RDWR);
cb2420df 1608 close_prot_errno_disarm(ipc_sockets[0]);
f157b056 1609
4f25e72f
CB
1610 ret_parent = 0;
1611 to_cleanup_pid = -1;
ea918412 1612
4f25e72f
CB
1613 if (options->attach_flags & LXC_ATTACH_TERMINAL) {
1614 ret = lxc_mainloop(&descr, -1);
1615 if (ret < 0) {
1616 ret_parent = -1;
1617 to_cleanup_pid = attached_pid;
1618 }
a998454a 1619 }
ea918412 1620
4f25e72f 1621close_mainloop:
9e84479f 1622 if (options->attach_flags & LXC_ATTACH_TERMINAL)
4f25e72f 1623 lxc_mainloop_close(&descr);
9c4693b8 1624
4f25e72f
CB
1625on_error:
1626 if (ipc_sockets[0] >= 0) {
1627 shutdown(ipc_sockets[0], SHUT_RDWR);
cb2420df 1628 close_prot_errno_disarm(ipc_sockets[0]);
9c4693b8 1629 }
ea918412 1630
4f25e72f
CB
1631 if (to_cleanup_pid > 0)
1632 (void)wait_for_pid(to_cleanup_pid);
1633
1634 if (options->attach_flags & LXC_ATTACH_TERMINAL) {
1635 lxc_terminal_delete(&terminal);
1636 lxc_terminal_conf_free(&terminal);
1637 }
9c4693b8 1638
dd53c8af 1639 put_attach_context(ctx);
4f25e72f 1640 return ret_parent;
9c4693b8
CS
1641}
1642
06346bb0 1643int lxc_attach_run_command(void *payload)
9c4693b8 1644{
06346bb0
CB
1645 int ret = -1;
1646 lxc_attach_command_t *cmd = payload;
9c4693b8 1647
06346bb0
CB
1648 ret = execvp(cmd->program, cmd->argv);
1649 if (ret < 0) {
1650 switch (errno) {
1651 case ENOEXEC:
1652 ret = 126;
cf0fd972 1653 break;
06346bb0
CB
1654 case ENOENT:
1655 ret = 127;
cf0fd972 1656 break;
06346bb0
CB
1657 }
1658 }
ea918412 1659
c2af3a15 1660 return log_error_errno(ret, errno, "Failed to exec \"%s\"", cmd->program);
9c4693b8
CS
1661}
1662
1663int lxc_attach_run_shell(void* payload)
1664{
cd8f5663 1665 __do_free char *buf = NULL;
9c4693b8 1666 uid_t uid;
cb7aa5e8
DJ
1667 struct passwd pwent;
1668 struct passwd *pwentp = NULL;
9c4693b8 1669 char *user_shell;
cb7aa5e8
DJ
1670 size_t bufsize;
1671 int ret;
9c4693b8 1672
8ce83369 1673 /* Ignore payload parameter. */
9c4693b8
CS
1674 (void)payload;
1675
1676 uid = getuid();
cb7aa5e8
DJ
1677
1678 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
1679 if (bufsize == -1)
1680 bufsize = 1024;
1681
1682 buf = malloc(bufsize);
1683 if (buf) {
1684 ret = getpwuid_r(uid, &pwent, buf, bufsize, &pwentp);
1685 if (!pwentp) {
1686 if (ret == 0)
ea918412 1687 WARN("Could not find matched password record");
cb7aa5e8
DJ
1688
1689 WARN("Failed to get password record - %u", uid);
1690 }
1691 }
9c4693b8 1692
8ce83369
CB
1693 /* This probably happens because of incompatible nss implementations in
1694 * host and container (remember, this code is still using the host's
1695 * glibc but our mount namespace is in the container) we may try to get
1696 * the information by spawning a [getent passwd uid] process and parsing
1697 * the result.
9c4693b8 1698 */
cb7aa5e8 1699 if (!pwentp)
9c4693b8
CS
1700 user_shell = lxc_attach_getpwshell(uid);
1701 else
cb7aa5e8 1702 user_shell = pwent.pw_shell;
ea918412 1703
9c4693b8 1704 if (user_shell)
acf47e1b 1705 execlp(user_shell, user_shell, (char *)NULL);
9c4693b8 1706
8ce83369
CB
1707 /* Executed if either no passwd entry or execvp fails, we will fall back
1708 * on /bin/sh as a default shell.
9c4693b8 1709 */
acf47e1b 1710 execlp("/bin/sh", "/bin/sh", (char *)NULL);
ea918412 1711
edeb1836 1712 SYSERROR("Failed to execute shell");
cb7aa5e8 1713 if (!pwentp)
edeb1836 1714 free(user_shell);
ea918412 1715
9c4693b8
CS
1716 return -1;
1717}