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