/
jchroot.c
304 lines (277 loc) · 7.96 KB
/
jchroot.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright (c) 2011 Vincent Bernat <bernat@luffy.cx>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#define _GNU_SOURCE
#define _BSD_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <alloca.h>
#include <errno.h>
#include <sched.h>
#include <pwd.h>
#include <grp.h>
#include <mntent.h>
#include <sys/mount.h>
struct config {
uid_t user;
gid_t group;
char *fstab;
char *hostname;
char *target;
char *const *command;
};
const char *progname;
static void usage() {
fprintf(stderr,
"Usage: %s [OPTIONS] TARGET [--] COMMAND\n"
"\n"
"Available options:\n"
" -u USER | --user=USER Specify user to use after chroot\n"
" -g USER | --group=USER Specify group to use after chroot\n"
" -f FSTAB | --fstab=FSTAB Specify a fstab(5) file\n"
" -n NAME | --hostname=NAME Specify a hostname\n",
progname);
exit(EXIT_FAILURE);
}
/* Step 5: Execute command */
static int step6(struct config *config) {
if (execvp(config->command[0], config->command) == -1) {
int i = 1;
fprintf(stderr, "unable to execute '%s", config->command[0]);
while (config->command[i]) fprintf(stderr, " %s", config->command[i++]);
fprintf(stderr, "': %m\n");
return errno;
}
return 0; /* No real return... */
}
/* Step 4: Drop privileges */
static int step5(struct config *config) {
if (config->group != (gid_t) -1 && setgid(config->group)) {
fprintf(stderr, "unable to change to GID %d: %m\n", config->group);
return EXIT_FAILURE;
}
if (setgroups(0, NULL)) {
fprintf(stderr, "unable to drop additional groups: %m\n");
return EXIT_FAILURE;
}
if (config->user != (uid_t) -1 && setuid(config->user)) {
fprintf(stderr, "unable to change to UID %d: %m\n", config->user);
return EXIT_FAILURE;
}
return step6(config);
}
/* Step 3: Chroot */
static int step4(struct config *config) {
if (chroot(config->target)) {
fprintf(stderr, "unable to chroot to %s: %m\n", config->target);
return EXIT_FAILURE;
}
if (chdir("/")) {
fprintf(stderr, "unable to go into chroot: %m\n");
return EXIT_FAILURE;
}
return step5(config);
}
/* Step 3: Set hostname */
static int step3(struct config *config) {
if (config->hostname &&
sethostname(config->hostname, strlen(config->hostname))) {
fprintf(stderr, "unable to change hostname to '%s': %m\n",
config->hostname);
}
return step4(config);
}
struct mount_opt {
char *name;
int clear;
int flag;
};
static struct mount_opt mount_opt[] = {
{ "defaults", 0, 0 },
{ "ro", 0, MS_RDONLY },
{ "rw", 1, MS_RDONLY },
{ "suid", 1, MS_NOSUID },
{ "nosuid", 0, MS_NOSUID },
{ "dev", 1, MS_NODEV },
{ "nodev", 0, MS_NODEV },
{ "exec", 1, MS_NOEXEC },
{ "noexec", 0, MS_NOEXEC },
{ "sync", 0, MS_SYNCHRONOUS },
{ "async", 1, MS_SYNCHRONOUS },
{ "atime", 1, MS_NOATIME },
{ "noatime", 0, MS_NOATIME },
{ "diratime", 1, MS_NODIRATIME },
{ "nodiratime", 0, MS_NODIRATIME },
{ "bind", 0, MS_BIND },
{ NULL, 0, 0 },
};
/* Step 2: Mount anything needed */
static int step2(void *arg) {
struct config *config = arg;
if (config->fstab) {
struct mntent *mntent;
char path[256];
FILE *file;
file = setmntent(config->fstab, "r");
if (!file) {
fprintf(stderr, "unable to open '%s': %m\n", config->fstab);
return EXIT_FAILURE;
}
while ((mntent = getmntent(file))) {
/* We need to parse mnt_opts */
unsigned long mntflags = 0;
char *mntopts = strdup(mntent->mnt_opts);
char *mntdata = malloc(strlen(mntent->mnt_opts) + 1);
if (!mntdata || !mntopts) {
fprintf(stderr, "unable to allocate memory\n");
free(mntopts);
free(mntdata);
return EXIT_FAILURE;
}
*mntdata = 0;
char *opt = NULL;
struct mount_opt *mo;
for (opt = strtok(mntopts, ","); opt != NULL;
opt = strtok(NULL, ",")) {
/* Is `opt` a known option? */
for (mo = &mount_opt[0];
mo->name != NULL; mo++) {
if (!strncmp(opt, mo->name, strlen(mo->name))) {
if (mo->clear)
mntflags &= ~mo->flag;
else
mntflags |= mo->flag;
break;
}
}
if (!mo->name) {
/* `opt` is not know, append it to `mntdata` */
if (strlen(mntdata)) strcat(mntdata, ",");
strcat(mntdata, opt);
}
}
free(mntopts);
/* Mount! */
if (snprintf(path, sizeof(path), "%s%s",
config->target, mntent->mnt_dir) >= sizeof(path)) {
fprintf(stderr, "path too long: %s\n", mntent->mnt_dir);
free(mntdata);
return EXIT_FAILURE;
}
if (mount(mntent->mnt_fsname, path, mntent->mnt_type,
mntflags, mntdata)) {
fprintf(stderr, "unable to mount '%s' on '%s': %m\n",
mntent->mnt_fsname, mntent->mnt_dir);
free(mntdata);
return EXIT_FAILURE;
}
free(mntdata);
}
}
return step3(config);
}
/* Step 1: create a new PID/IPC/NS/UTS namespace */
static int step1(struct config *config) {
int ret;
pid_t pid;
long stack_size = sysconf(_SC_PAGESIZE);
void *stack = alloca(stack_size) + stack_size;
int flags = CLONE_NEWPID | CLONE_NEWIPC | CLONE_NEWNS;
if (config->hostname) flags |= CLONE_NEWUTS;
pid = clone(step2,
stack,
SIGCHLD | flags | CLONE_FILES,
config);
if (pid < 0) {
fprintf(stderr, "failed to clone: %m\n");
return EXIT_FAILURE;
}
while (waitpid(pid, &ret, 0) < 0 && errno == EINTR)
continue;
return WIFEXITED(ret)?WEXITSTATUS(ret):EXIT_FAILURE;
}
int main(int argc, char * argv[]) {
struct config config;
memset(&config, 0, sizeof(struct config));
config.user = config.group = -1;
progname = argv[0];
int c;
while (1) {
int option_index = 0;
static struct option long_options[] = {
{ "user", required_argument, 0, 'u' },
{ "group", required_argument, 0, 'g' },
{ "fstab", required_argument, 0, 'f' },
{ "hostname", required_argument, 0, 'n' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};
c = getopt_long(argc, argv, "hu:g:f:n:",
long_options, &option_index);
if (c == -1) break;
switch (c) {
case 'u':
if (!optarg) usage();
struct passwd *passwd;
passwd = getpwnam(optarg);
if (!passwd) {
config.user = strtoul(optarg, NULL, 10);
if (errno) {
fprintf(stderr, "'%s' is not a valid user\n", optarg);
usage();
}
} else {
config.user = passwd->pw_uid;
if (config.group == (gid_t) -1)
config.group = passwd->pw_gid;
}
break;
case 'g':
if (!optarg) usage();
struct group *group;
group = getgrnam(optarg);
if (!group) {
config.group = strtoul(optarg, NULL, 10);
if (errno) {
fprintf(stderr, "'%s' is not a valid group\n", optarg);
usage();
}
} else {
config.group = group->gr_gid;
}
break;
case 'f':
if (!optarg) usage();
config.fstab = optarg;
break;
case 'n':
if (!optarg) usage();
config.hostname = optarg;
break;
default:
usage();
}
}
if (optind == argc) usage();
config.target = argv[optind++];
if (optind == argc) usage();
config.command = argv + optind;
exit(step1(&config));
}