]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/attach.c
lxc-attach: User namespaces: Use init's user & group id when attaching
[mirror_lxc.git] / src / lxc / attach.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #define _GNU_SOURCE
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <sys/param.h>
32 #include <sys/prctl.h>
33 #include <sys/mount.h>
34 #include <sys/syscall.h>
35 #include <sys/wait.h>
36 #include <linux/unistd.h>
37 #include <pwd.h>
38
39 #if !HAVE_DECL_PR_CAPBSET_DROP
40 #define PR_CAPBSET_DROP 24
41 #endif
42
43 #include "namespace.h"
44 #include "log.h"
45 #include "attach.h"
46 #include "caps.h"
47 #include "config.h"
48 #include "apparmor.h"
49
50 lxc_log_define(lxc_attach, lxc);
51
52 /* Define setns() if missing from the C library */
53 #ifndef HAVE_SETNS
54 static int setns(int fd, int nstype)
55 {
56 #ifdef __NR_setns
57 return syscall(__NR_setns, fd, nstype);
58 #else
59 errno = ENOSYS;
60 return -1;
61 #endif
62 }
63 #endif
64
65 /* Define unshare() if missing from the C library */
66 #ifndef HAVE_UNSHARE
67 static int unshare(int flags)
68 {
69 #ifdef __NR_unshare
70 return syscall(__NR_unshare, flags);
71 #else
72 errno = ENOSYS;
73 return -1;
74 #endif
75 }
76 #endif
77
78 /* Define getline() if missing from the C library */
79 #ifndef HAVE_GETLINE
80 #ifdef HAVE_FGETLN
81 #include <../include/getline.h>
82 #endif
83 #endif
84
85 struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
86 {
87 struct lxc_proc_context_info *info = calloc(1, sizeof(*info));
88 FILE *proc_file;
89 char proc_fn[MAXPATHLEN];
90 char *line = NULL;
91 size_t line_bufsz = 0;
92 int ret, found;
93
94 if (!info) {
95 SYSERROR("Could not allocate memory.");
96 return NULL;
97 }
98
99 /* read capabilities */
100 snprintf(proc_fn, MAXPATHLEN, "/proc/%d/status", pid);
101
102 proc_file = fopen(proc_fn, "r");
103 if (!proc_file) {
104 SYSERROR("Could not open %s", proc_fn);
105 goto out_error;
106 }
107
108 found = 0;
109 while (getline(&line, &line_bufsz, proc_file) != -1) {
110 ret = sscanf(line, "CapBnd: %llx", &info->capability_mask);
111 if (ret != EOF && ret > 0) {
112 found = 1;
113 break;
114 }
115 }
116
117 fclose(proc_file);
118
119 if (!found) {
120 SYSERROR("Could not read capability bounding set from %s", proc_fn);
121 errno = ENOENT;
122 goto out_error;
123 }
124
125 /* read personality */
126 snprintf(proc_fn, MAXPATHLEN, "/proc/%d/personality", pid);
127
128 proc_file = fopen(proc_fn, "r");
129 if (!proc_file) {
130 SYSERROR("Could not open %s", proc_fn);
131 goto out_error;
132 }
133
134 ret = fscanf(proc_file, "%lx", &info->personality);
135 fclose(proc_file);
136
137 if (ret == EOF || ret == 0) {
138 SYSERROR("Could not read personality from %s", proc_fn);
139 errno = ENOENT;
140 goto out_error;
141 }
142 info->aa_profile = aa_get_profile(pid);
143
144 return info;
145
146 out_error:
147 free(info);
148 free(line);
149 return NULL;
150 }
151
152 int lxc_attach_to_ns(pid_t pid, int which)
153 {
154 char path[MAXPATHLEN];
155 /* according to <http://article.gmane.org/gmane.linux.kernel.containers.lxc.devel/1429>,
156 * the file for user namepsaces in /proc/$pid/ns will be called
157 * 'user' once the kernel supports it
158 */
159 static char *ns[] = { "mnt", "pid", "uts", "ipc", "user", "net" };
160 static int flags[] = {
161 CLONE_NEWNS, CLONE_NEWPID, CLONE_NEWUTS, CLONE_NEWIPC,
162 CLONE_NEWUSER, CLONE_NEWNET
163 };
164 static const int size = sizeof(ns) / sizeof(char *);
165 int fd[size];
166 int i, j, saved_errno;
167
168
169 snprintf(path, MAXPATHLEN, "/proc/%d/ns", pid);
170 if (access(path, X_OK)) {
171 ERROR("Does this kernel version support 'attach' ?");
172 return -1;
173 }
174
175 for (i = 0; i < size; i++) {
176 /* ignore if we are not supposed to attach to that
177 * namespace
178 */
179 if (which != -1 && !(which & flags[i])) {
180 fd[i] = -1;
181 continue;
182 }
183
184 snprintf(path, MAXPATHLEN, "/proc/%d/ns/%s", pid, ns[i]);
185 fd[i] = open(path, O_RDONLY);
186 if (fd[i] < 0) {
187 saved_errno = errno;
188
189 /* close all already opened file descriptors before
190 * we return an error, so we don't leak them
191 */
192 for (j = 0; j < i; j++)
193 close(fd[j]);
194
195 errno = saved_errno;
196 SYSERROR("failed to open '%s'", path);
197 return -1;
198 }
199 }
200
201 for (i = 0; i < size; i++) {
202 if (fd[i] >= 0 && setns(fd[i], 0) != 0) {
203 saved_errno = errno;
204
205 for (j = i; j < size; j++)
206 close(fd[j]);
207
208 errno = saved_errno;
209 SYSERROR("failed to set namespace '%s'", ns[i]);
210 return -1;
211 }
212
213 close(fd[i]);
214 }
215
216 return 0;
217 }
218
219 int lxc_attach_remount_sys_proc()
220 {
221 int ret;
222
223 ret = unshare(CLONE_NEWNS);
224 if (ret < 0) {
225 SYSERROR("failed to unshare mount namespace");
226 return -1;
227 }
228
229 /* assume /proc is always mounted, so remount it */
230 ret = umount2("/proc", MNT_DETACH);
231 if (ret < 0) {
232 SYSERROR("failed to unmount /proc");
233 return -1;
234 }
235
236 ret = mount("none", "/proc", "proc", 0, NULL);
237 if (ret < 0) {
238 SYSERROR("failed to remount /proc");
239 return -1;
240 }
241
242 /* try to umount /sys - if it's not a mount point,
243 * we'll get EINVAL, then we ignore it because it
244 * may not have been mounted in the first place
245 */
246 ret = umount2("/sys", MNT_DETACH);
247 if (ret < 0 && errno != EINVAL) {
248 SYSERROR("failed to unmount /sys");
249 return -1;
250 } else if (ret == 0) {
251 /* remount it */
252 ret = mount("none", "/sys", "sysfs", 0, NULL);
253 if (ret < 0) {
254 SYSERROR("failed to remount /sys");
255 return -1;
256 }
257 }
258
259 return 0;
260 }
261
262 int lxc_attach_drop_privs(struct lxc_proc_context_info *ctx)
263 {
264 int last_cap = lxc_caps_last_cap();
265 int cap;
266
267 for (cap = 0; cap <= last_cap; cap++) {
268 if (ctx->capability_mask & (1LL << cap))
269 continue;
270
271 if (prctl(PR_CAPBSET_DROP, cap, 0, 0, 0)) {
272 SYSERROR("failed to remove capability id %d", cap);
273 return -1;
274 }
275 }
276
277 return 0;
278 }
279
280 char *lxc_attach_getpwshell(uid_t uid)
281 {
282 /* local variables */
283 pid_t pid;
284 int pipes[2];
285 int ret;
286 int fd;
287 char *result = NULL;
288
289 /* we need to fork off a process that runs the
290 * getent program, and we need to capture its
291 * output, so we use a pipe for that purpose
292 */
293 ret = pipe(pipes);
294 if (ret < 0)
295 return NULL;
296
297 pid = fork();
298 if (pid < 0) {
299 close(pipes[0]);
300 close(pipes[1]);
301 return NULL;
302 }
303
304 if (pid) {
305 /* parent process */
306 FILE *pipe_f;
307 char *line = NULL;
308 size_t line_bufsz = 0;
309 int found = 0;
310 int status;
311
312 close(pipes[1]);
313
314 pipe_f = fdopen(pipes[0], "r");
315 while (getline(&line, &line_bufsz, pipe_f) != -1) {
316 char *token;
317 char *saveptr = NULL;
318 long value;
319 char *endptr = NULL;
320 int i;
321
322 /* if we already found something, just continue
323 * to read until the pipe doesn't deliver any more
324 * data, but don't modify the existing data
325 * structure
326 */
327 if (found)
328 continue;
329
330 /* trim line on the right hand side */
331 for (i = strlen(line); line && i > 0 && (line[i - 1] == '\n' || line[i - 1] == '\r'); --i)
332 line[i - 1] = '\0';
333
334 /* split into tokens: first user name */
335 token = strtok_r(line, ":", &saveptr);
336 if (!token)
337 continue;
338 /* next: dummy password field */
339 token = strtok_r(NULL, ":", &saveptr);
340 if (!token)
341 continue;
342 /* next: user id */
343 token = strtok_r(NULL, ":", &saveptr);
344 value = token ? strtol(token, &endptr, 10) : 0;
345 if (!token || !endptr || *endptr || value == LONG_MIN || value == LONG_MAX)
346 continue;
347 /* dummy sanity check: user id matches */
348 if ((uid_t) value != uid)
349 continue;
350 /* skip fields: gid, gecos, dir, go to next field 'shell' */
351 for (i = 0; i < 4; i++) {
352 token = strtok_r(NULL, ":", &saveptr);
353 if (!token)
354 break;
355 }
356 if (!token)
357 continue;
358 result = strdup(token);
359
360 /* sanity check that there are no fields after that */
361 token = strtok_r(NULL, ":", &saveptr);
362 if (token)
363 continue;
364
365 found = 1;
366 }
367
368 free(line);
369 fclose(pipe_f);
370 again:
371 if (waitpid(pid, &status, 0) < 0) {
372 if (errno == EINTR)
373 goto again;
374 return NULL;
375 }
376
377 /* some sanity checks: if anything even hinted at going
378 * wrong: we can't be sure we have a valid result, so
379 * we assume we don't
380 */
381
382 if (!WIFEXITED(status))
383 return NULL;
384
385 if (WEXITSTATUS(status) != 0)
386 return NULL;
387
388 if (!found)
389 return NULL;
390
391 return result;
392 } else {
393 /* child process */
394 char uid_buf[32];
395 char *arguments[] = {
396 "getent",
397 "passwd",
398 uid_buf,
399 NULL
400 };
401
402 close(pipes[0]);
403
404 /* we want to capture stdout */
405 dup2(pipes[1], 1);
406 close(pipes[1]);
407
408 /* get rid of stdin/stderr, so we try to associate it
409 * with /dev/null
410 */
411 fd = open("/dev/null", O_RDWR);
412 if (fd < 0) {
413 close(0);
414 close(2);
415 } else {
416 dup2(fd, 0);
417 dup2(fd, 2);
418 close(fd);
419 }
420
421 /* finish argument list */
422 ret = snprintf(uid_buf, sizeof(uid_buf), "%ld", (long) uid);
423 if (ret <= 0)
424 exit(-1);
425
426 /* try to run getent program */
427 (void) execvp("getent", arguments);
428 exit(-1);
429 }
430 }
431
432 void lxc_attach_get_init_uidgid(uid_t* init_uid, gid_t* init_gid)
433 {
434 FILE *proc_file;
435 char proc_fn[MAXPATHLEN];
436 char *line = NULL;
437 size_t line_bufsz = 0;
438 int ret;
439 long value = -1;
440 uid_t uid = (uid_t)-1;
441 gid_t gid = (gid_t)-1;
442
443 /* read capabilities */
444 snprintf(proc_fn, MAXPATHLEN, "/proc/%d/status", 1);
445
446 proc_file = fopen(proc_fn, "r");
447 if (!proc_file)
448 return;
449
450 while (getline(&line, &line_bufsz, proc_file) != -1) {
451 /* format is: real, effective, saved set user, fs
452 * we only care about real uid
453 */
454 ret = sscanf(line, "Uid: %ld", &value);
455 if (ret != EOF && ret > 0) {
456 uid = (uid_t) value;
457 } else {
458 ret = sscanf(line, "Gid: %ld", &value);
459 if (ret != EOF && ret > 0)
460 gid = (gid_t) value;
461 }
462 if (uid != (uid_t)-1 && gid != (gid_t)-1)
463 break;
464 }
465
466 fclose(proc_file);
467 free(line);
468
469 /* only override arguments if we found something */
470 if (uid != (uid_t)-1)
471 *init_uid = uid;
472 if (gid != (gid_t)-1)
473 *init_gid = gid;
474
475 /* TODO: we should also parse supplementary groups and use
476 * setgroups() to set them */
477 }