forked from arachsys/containers
/
contain.c
131 lines (113 loc) · 3.05 KB
/
contain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#define _GNU_SOURCE
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <grp.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include "contain.h"
void usage(char *progname) {
fprintf(stderr, "\
Usage: %s [OPTIONS] DIR [CMD [ARG]...]\n\
Options:\n\
-c disable console emulation in the container\n\
-g MAP set the container-to-host GID map\n\
-i CMD run a helper child inside the new namespaces\n\
-n share the host network unprivileged in the container\n\
-o CMD run a helper child outside the new namespaces\n\
-u MAP set the container-to-host UID map\n\
GID and UID maps are specified as START:LOWER:COUNT[,START:LOWER:COUNT]...\n\
", progname);
exit(EX_USAGE);
}
int main(int argc, char **argv) {
char *gidmap = NULL, *inside = NULL, *outside = NULL, *uidmap = NULL;
int hostnet = 0, master, option, stdio = 0;
pid_t child, parent;
while ((option = getopt(argc, argv, "+:cg:i:no:u:")) > 0)
switch (option) {
case 'c':
stdio++;
break;
case 'g':
gidmap = optarg;
break;
case 'i':
inside = optarg;
break;
case 'n':
hostnet++;
break;
case 'o':
outside = optarg;
break;
case 'u':
uidmap = optarg;
break;
default:
usage(argv[0]);
}
if (argc <= optind)
usage(argv[0]);
parent = getpid();
switch (child = fork()) {
case -1:
error(1, errno, "fork");
case 0:
raise(SIGSTOP);
if (geteuid() != 0)
denysetgroups(parent);
writemap(parent, GID, gidmap);
writemap(parent, UID, uidmap);
if (outside) {
if (setgid(getgid()) < 0 || setuid(getuid()) < 0)
error(1, 0, "Failed to drop privileges");
execlp(SHELL, SHELL, "-c", outside, NULL);
error(1, errno, "exec %s", outside);
}
exit(EXIT_SUCCESS);
}
if (setgid(getgid()) < 0 || setuid(getuid()) < 0)
error(1, 0, "Failed to drop privileges");
if (unshare(CLONE_NEWIPC | CLONE_NEWNS | CLONE_NEWUSER | CLONE_NEWUTS) < 0)
error(1, 0, "Failed to unshare namespaces");
if (!hostnet && unshare(CLONE_NEWNET) < 0)
error(1, 0, "Failed to unshare network namespace");
waitforstop(child);
kill(child, SIGCONT);
waitforexit(child);
setgid(0);
setgroups(0, NULL);
setuid(0);
master = stdio ? -1 : getconsole();
createroot(argv[optind], master, inside);
unshare(CLONE_NEWPID);
switch (child = fork()) {
case -1:
error(1, errno, "fork");
case 0:
mountproc();
if (!hostnet)
mountsys();
enterroot();
if (master >= 0) {
close(master);
setconsole("/dev/console");
}
clearenv();
putenv("container=contain");
if (argv[optind + 1])
execv(argv[optind + 1], argv + optind + 1);
else
execl(SHELL, SHELL, NULL);
error(1, errno, "exec");
}
return supervise(child, master);
}