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