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