This repository has been archived by the owner on Jul 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathinit.c
412 lines (361 loc) · 10.8 KB
/
init.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <net/if.h>
#include <netinet/ip.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "../vsockexec/vsock.h"
// musl-gcc doesn't use headers in /usr/include, so it can't find
// linux/random.h which is where RNDADDENTROPY is defined. We only need this
// single definition from linux/random.h, so we just duplicate it here as a
// workaround.
#define RNDADDENTROPY _IOW( 'R', 0x03, int [2] )
#define DEFAULT_PATH_ENV "PATH=/sbin:/usr/sbin:/bin:/usr/bin"
const char *const default_envp[] = {
DEFAULT_PATH_ENV,
NULL,
};
// When nothing is passed, default to the LCOWv1 behavior.
const char *const default_argv[] = { "/bin/gcs", "-loglevel", "debug", "-logfile=/run/gcs/gcs.log" };
const char *const default_shell = "/bin/sh";
struct Mount {
const char *source, *target, *type;
unsigned long flags;
const void *data;
};
struct Mkdir {
const char *path;
mode_t mode;
};
struct Mknod {
const char *path;
mode_t mode;
int major, minor;
};
struct Symlink {
const char *linkpath, *target;
};
enum OpType {
OpMount,
OpMkdir,
OpMknod,
OpSymlink,
};
struct InitOp {
enum OpType op;
union {
struct Mount mount;
struct Mkdir mkdir;
struct Mknod mknod;
struct Symlink symlink;
};
};
const struct InitOp ops[] = {
// mount /proc (which should already exist)
{ OpMount, .mount = { "proc", "/proc", "proc", MS_NODEV | MS_NOSUID | MS_NOEXEC } },
// add symlinks in /dev (which is already mounted)
{ OpSymlink, .symlink = { "/dev/fd", "/proc/self/fd" } },
{ OpSymlink, .symlink = { "/dev/stdin", "/proc/self/fd/0" } },
{ OpSymlink, .symlink = { "/dev/stdout", "/proc/self/fd/1" } },
{ OpSymlink, .symlink = { "/dev/stderr", "/proc/self/fd/2" } },
// mount tmpfs on /run and /tmp (which should already exist)
{ OpMount, .mount = { "tmpfs", "/run", "tmpfs", MS_NODEV | MS_NOSUID | MS_NOEXEC, "mode=0755" } },
{ OpMount, .mount = { "tmpfs", "/tmp", "tmpfs", MS_NODEV | MS_NOSUID | MS_NOEXEC } },
// mount shm and devpts
{ OpMkdir, .mkdir = { "/dev/shm", 0755 } },
{ OpMount, .mount = { "shm", "/dev/shm", "tmpfs", MS_NODEV | MS_NOSUID | MS_NOEXEC } },
{ OpMkdir, .mkdir = { "/dev/pts", 0755 } },
{ OpMount, .mount = { "devpts", "/dev/pts", "devpts", MS_NOSUID | MS_NOEXEC } },
// mount /sys (which should already exist)
{ OpMount, .mount = { "sysfs", "/sys", "sysfs", MS_NODEV | MS_NOSUID | MS_NOEXEC } },
{ OpMount, .mount = { "cgroup_root", "/sys/fs/cgroup", "tmpfs", MS_NODEV | MS_NOSUID | MS_NOEXEC, "mode=0755" } },
};
void warn(const char *msg) {
int error = errno;
perror(msg);
errno = error;
}
void warn2(const char *msg1, const char *msg2) {
int error = errno;
fputs(msg1, stderr);
fputs(": ", stderr);
errno = error;
warn(msg2);
}
_Noreturn void dien() {
exit(errno);
}
_Noreturn void die(const char *msg) {
warn(msg);
dien();
}
_Noreturn void die2(const char *msg1, const char *msg2) {
warn2(msg1, msg2);
dien();
}
void init_rlimit() {
// Set the hard limit for number of open fds much larger. The kernel sets
// a limit of 4096 for historical reasons, and this limit is too low for
// some software. According to the systemd developers, there is no downside
// to a large hard limit in modern Linux kernels.
//
// Retain the small soft limit of 1024 for appcompat.
struct rlimit rlim = {
.rlim_cur = 1024,
.rlim_max = 1024 * 1024,
};
if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
die("setrlimit(RLIMIT_NOFILE)");
}
}
void init_dev() {
if (mount("dev", "/dev", "devtmpfs", MS_NOSUID | MS_NOEXEC, NULL) < 0) {
warn2("mount", "/dev");
// /dev will be already mounted if devtmpfs.mount = 1 on the kernel
// command line or CONFIG_DEVTMPFS_MOUNT is set. Do not consider this
// an error.
if (errno != EBUSY) {
dien();
}
}
}
void init_fs(const struct InitOp *ops, size_t count) {
for (size_t i = 0; i < count; i++) {
switch (ops[i].op) {
case OpMount: {
const struct Mount *m = &ops[i].mount;
if (mount(m->source, m->target, m->type, m->flags, m->data) < 0) {
die2("mount", m->target);
}
break;
}
case OpMkdir: {
const struct Mkdir *m = &ops[i].mkdir;
if (mkdir(m->path, m->mode) < 0) {
warn2("mkdir", m->path);
if (errno != EEXIST) {
dien();
}
}
break;
}
case OpMknod: {
const struct Mknod *n = &ops[i].mknod;
if (mknod(n->path, n->mode, makedev(n->major, n->minor)) < 0) {
warn2("mknod", n->path);
if (errno != EEXIST) {
dien();
}
}
break;
}
case OpSymlink: {
const struct Symlink *sl = &ops[i].symlink;
if (symlink(sl->target, sl->linkpath) < 0) {
warn2("symlink", sl->linkpath);
if (errno != EEXIST) {
dien();
}
}
break;
}
}
}
}
void init_cgroups() {
const char *fpath = "/proc/cgroups";
FILE *f = fopen(fpath, "r");
if (f == NULL) {
die2("fopen", fpath);
}
// Skip the first line.
for (;;) {
char c = fgetc(f);
if (c == EOF || c == '\n') {
break;
}
}
for (;;) {
static const char base_path[] = "/sys/fs/cgroup/";
char path[sizeof(base_path) - 1 + 64];
char* name = path + sizeof(base_path) - 1;
int hier, groups, enabled;
int r = fscanf(f, "%64s %d %d %d\n", name, &hier, &groups, &enabled);
if (r == EOF) {
break;
}
if (r != 4) {
errno = errno ? : EINVAL;
die2("fscanf", fpath);
}
if (enabled) {
memcpy(path, base_path, sizeof(base_path) - 1);
if (mkdir(path, 0755) < 0) {
die2("mkdir", path);
}
if (mount(name, path, "cgroup", MS_NODEV | MS_NOSUID | MS_NOEXEC, name) < 0) {
die2("mount", path);
}
}
}
fclose(f);
}
void init_network(const char *iface, int domain) {
int s = socket(domain, SOCK_DGRAM, IPPROTO_IP);
if (s < 0) {
if (errno == EAFNOSUPPORT) {
return;
}
die("socket");
}
struct ifreq request = {0};
strncpy(request.ifr_name, iface, sizeof(request.ifr_name));
if (ioctl(s, SIOCGIFFLAGS, &request) < 0) {
die2("ioctl(SIOCGIFFLAGS)", iface);
}
request.ifr_flags |= IFF_UP | IFF_RUNNING;
if (ioctl(s, SIOCSIFFLAGS, &request) < 0) {
die2("ioctl(SIOCSIFFLAGS)", iface);
}
close(s);
}
// inject boot-time entropy after reading it from a vsock port
void init_entropy(int port) {
int s = openvsock(VMADDR_CID_HOST, port);
if (s < 0) {
die("openvsock entropy");
}
int e = open("/dev/random", O_RDWR);
if (e < 0) {
die("open /dev/random");
}
struct {
int entropy_count;
int buf_size;
char buf[4096];
} buf;
for (;;) {
ssize_t n = read(s, buf.buf, sizeof(buf.buf));
if (n < 0) {
die("read entropy");
}
if (n == 0) {
break;
}
buf.entropy_count = n * 8; // in bits
buf.buf_size = n; // in bytes
if (ioctl(e, RNDADDENTROPY, &buf) < 0) {
die("ioctl(RNDADDENTROPY)");
}
}
close(s);
close(e);
}
pid_t launch(int argc, char **argv) {
int pid = fork();
if (pid != 0) {
if (pid < 0) {
die("fork");
}
return pid;
}
// Unblock signals before execing.
sigset_t set;
sigfillset(&set);
sigprocmask(SIG_UNBLOCK, &set, 0);
// Create a session and process group.
setsid();
setpgid(0, 0);
// Terminate the arguments and exec.
char **argvn = alloca(sizeof(argv[0]) * (argc + 1));
memcpy(argvn, argv, sizeof(argv[0]) * argc);
argvn[argc] = NULL;
if (putenv(DEFAULT_PATH_ENV)) { // Specify the PATH used for execvpe
die("putenv");
}
execvpe(argvn[0], argvn, (char**)default_envp);
die2("execvpe", argvn[0]);
}
int reap_until(pid_t until_pid) {
for (;;) {
int status;
pid_t pid = wait(&status);
if (pid < 0) {
die("wait");
}
if (pid == until_pid) {
// The initial child process died. Pass through the exit status.
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) != 0) {
fputs("child exited with error\n", stderr);
}
return WEXITSTATUS(status);
}
fputs("child exited by signal: ", stderr);
fputs(strsignal(WTERMSIG(status)), stderr);
fputs("\n", stderr);
return 128 + WTERMSIG(status);
}
}
}
int main(int argc, char **argv) {
char *debug_shell = NULL;
int entropy_port = 0;
if (argc <= 1) {
argv = (char **)default_argv;
argc = sizeof(default_argv) / sizeof(default_argv[0]);
optind = 0;
debug_shell = (char*)default_shell;
} else {
for (int opt; (opt = getopt(argc, argv, "+d:e:")) >= 0; ) {
switch (opt) {
case 'd':
debug_shell = optarg;
break;
case 'e':
entropy_port = atoi(optarg);
if (entropy_port == 0) {
fputs("invalid entropy port\n", stderr);
exit(1);
}
break;
default:
exit(1);
}
}
}
char **child_argv = argv + optind;
int child_argc = argc - optind;
// Block all signals in init. SIGCHLD will still cause wait() to return.
sigset_t set;
sigfillset(&set);
sigprocmask(SIG_BLOCK, &set, 0);
init_rlimit();
init_dev();
init_fs(ops, sizeof(ops) / sizeof(ops[0]));
init_cgroups();
init_network("lo", AF_INET);
init_network("lo", AF_INET6);
if (entropy_port != 0) {
init_entropy(entropy_port);
}
pid_t pid = launch(child_argc, child_argv);
if (debug_shell != NULL) {
// The debug shell takes over as the primary child.
pid = launch(1, &debug_shell);
}
// Reap until the initial child process dies.
return reap_until(pid);
}