-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclone.c
More file actions
247 lines (226 loc) · 6.91 KB
/
clone.c
File metadata and controls
247 lines (226 loc) · 6.91 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#define _GNU_SOURCE 1
#include "contain.h"
#include "cgroup.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#include <argp.h>
#include <err.h>
#include <grp.h>
#include <pwd.h>
#include <sched.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static bool new_ipc = false;
static bool new_net = false;
static bool new_ns = false;
static bool new_pid = false;
static bool new_uts = false;
static bool new_user = false;
static char **argv = NULL;
static int argc = 0;
static struct passwd *user = NULL;
static struct group *group = NULL;
static char *hostname = NULL;
static bool detach = false;
/* If:
* !user && !group => nothing
* user && !group => change to user, initgroups with gid
* user && group => change to user, change to gid (no initgroups)
* !user && group => invalid
*/
static void change_user(void)
{
if (group) {
if (setgid(group->gr_gid) == -1)
err(1, "setgid(%d)", group->gr_gid);
} else
if (user) {
if (initgroups(user->pw_name, user->pw_gid) == -1)
err(1, "initgroups(%s, %d)", user->pw_name, user->pw_gid);
if (setgid(user->pw_gid) == -1)
err(1, "setgid(%d)", user->pw_gid);
}
if (user) {
if (setuid(user->pw_uid) == -1)
err(1, "setuid(%d)", user->pw_uid);
}
}
static int child_start(void *dummy)
{
(void)dummy;
if (hostname)
sethostname(hostname, strlen(hostname));
if (do_nice() == -1)
err(1, "nice");
if (do_ioprio() == -1)
err(1, "ioprio");
if (do_cgroup() == -1)
err(1, "cgroup");
if (do_chroot() == -1)
err(1, "chroot");
if (do_selinux() == -1)
err(1, "selinux");
if (do_prctl() == -1)
err(1, "prctl");
if (do_capabilities() == -1)
err(1, "capabilities");
change_user();
execvp(argv[0], argv);
err(1, "execlv(%s)", argv[0]);
}
static struct argp_option clone_options[] = {
{"newipc", 'I', 0, 0, "Create a new IPC namespace", 0},
{"newnet", 'N', 0, 0, "Create a new networking namespace", 0},
{"newmount", 'M', 0, 0, "Create a new filesystem namespace", 0},
{"newpid", 'P', 0, 0, "Create a new pid namespace", 0},
{"newuname", 'U', 0, 0, "Create a new uname namespace", 0},
{"newuser", 1052, 0, 0, "Create a new uid/gid namespace", 0},
{"hostname",1050, "hostname", 0, "Set the hostname", 0},
{"uid", 'u', "user", 0, "User ID to change to", 0},
{"gid", 'g', "group", 0, "Group ID to change to", 0},
{"detach", 1051, 0, 0, "Detach child process", 0},
{NULL, 0, 0, 0, NULL, 0 },
};
static error_t
parse_clone_opt(int key, char *arg, struct argp_state *state)
{
switch(key) {
case 'I': new_ipc = true; break;
case 'N': new_net = true; break;
case 'M': new_ns = true; break;
case 'P': new_pid = true; break;
case 'U': new_uts = true; break;
case 'u':
user = getpwnam(arg);
if (!user)
argp_failure(state, 1, errno, "Unknown user %s", arg);
break;
case 'g':
group = getgrnam(arg);
if (!group)
argp_failure(state, 1, errno, "Unknown group %s", arg);
break;
case 1050:
if (hostname)
argp_failure(state, 1, errno, "Cannot specify hostname twice");
else
hostname = strdup(arg);
break;
case 1051:
detach = true;
break;
case 1052:
new_user = true; break;
case ARGP_KEY_ARGS:
argv = state->argv + state->next;
argc = state->argc - state->next;
break;
case ARGP_KEY_END:
if(!argv)
argp_usage(state);
if(group && !user)
argp_failure(state, 1, 0,
"If you specify a group, you must specify a user");
if (hostname && !new_uts)
argp_failure(state, 1, 0,
"You must create a new uts namespace with hostname");
if (new_user && !user) {
user = getpwuid(getuid());
if (!user)
argp_failure(state, 1, 0,
"User not specified, and you don't exist");
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
struct argp clone_argp = { clone_options, parse_clone_opt, "", "Clone flags", 0, 0, 0 };
#define UNSHARE_SUPPORTED_FLAGS \
(CLONE_NEWIPC|CLONE_NEWNET|CLONE_NEWNS|CLONE_NEWUTS|CLONE_FILES \
|CLONE_NEWUSER)
int setup_clone(void)
{
#define STACKSIZE (32*1024)
int flags = 0;
int status;
flags |= CLONE_FILES; /* fork() normally shares fd's */
flags |= SIGCHLD; /* Signal to send to the parent process. */
if (new_ipc)
flags |= CLONE_NEWIPC;
if (new_net)
flags |= CLONE_NEWNET;
if (new_ns)
flags |= CLONE_NEWNS;
if (new_pid)
flags |= CLONE_NEWPID;
if (new_uts)
flags |= CLONE_NEWUTS;
if (new_user)
flags |= CLONE_NEWUSER;
if (detach)
daemon(true, true);
/* We have to remap the users independently, because you can't mix
* NEW_USER with other namespaces safely.
*/
if ((uidmap_head || gidmap_head) && (flags & CLONE_NEWUSER)) {
/* We need another process that still has permissions to take care
* of the rewriting of the uid table.
*/
int pipes[2]; /* Pipe used for IPC with helper */
int pid;
if (pipe(pipes) == -1)
err(1, "pipe");
int ppid;
switch (pid = fork()) {
case -1:
err(1, "fork");
case 0: /* Child */
/* The child retains it's permissions so it can update the
* parents environment.
*/
close(pipes[1]);
/* Wait until the unshare has completed. */
assert(sizeof(ppid) < PIPE_BUF);
if (read(pipes[0], &ppid, sizeof(ppid)) != sizeof(ppid))
err(1, "unexpected read on pipe");
close(pipes[0]);
_exit(do_idmap(ppid));
default: /* Parent process... */
close(pipes[0]);
if (unshare(CLONE_NEWUSER) == -1)
err(1, "unshare(CLONE_NEWUSER)");
/* Signal to the helper that we're ready for it to do its
* thing.
*/
ppid = getpid();
if (write(pipes[1], &ppid, sizeof(ppid)) != sizeof(ppid))
err(1, "unexpected write on pipe");
close(pipes[1]);
/* Wait for the child to have finished creating our
* uid/gid mapping. */
if (waitpid(pid, NULL, 0) == -1)
err(1, "waitpid(helper[%d])", pid);
flags &= ~CLONE_NEWUSER; /* No longer needed. */
}
}
if (flags & ~(UNSHARE_SUPPORTED_FLAGS|SIGCHLD)) {
char *stack = malloc(STACKSIZE);
pid_t pid = clone(child_start, stack+STACKSIZE, flags, NULL);
if (pid == -1)
err(1, "clone()");
if (!detach && waitpid(pid, &status, 0) == -1)
warn("waitpid(%d)", pid);
}
else {
if (unshare(flags & ~(SIGCHLD)) == -1)
err(1, "unshare");
child_start(NULL);
}
return status;
}