/
nsexec.go
460 lines (383 loc) · 9.74 KB
/
nsexec.go
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
* This file is a bit funny. The goal here is to use setns() to manipulate
* files inside the container, so we don't have to reason about the paths to
* make sure they don't escape (we can simply rely on the kernel for
* correctness). Unfortunately, you can't setns() to a mount namespace with a
* multi-threaded program, which every golang binary is. However, by declaring
* our init as an initializer, we can capture process control before it is
* transferred to the golang runtime, so we can then setns() as we'd like
* before golang has a chance to set up any threads. So, we implement two new
* lxd fork* commands which are captured here, and take a file on the host fs
* and copy it into the container ns.
*
* An alternative to this would be to move this code into a separate binary,
* which of course has problems of its own when it comes to packaging (how do
* we find the binary, what do we do if someone does file push and it is
* missing, etc.). After some discussion, even though the embedded method is
* somewhat convoluted, it was preferred.
*/
package main
/*
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mount.h>
#include <sched.h>
#include <linux/sched.h>
#include <linux/limits.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <alloca.h>
#include <libgen.h>
#include <ifaddrs.h>
// This expects:
// ./lxd forkputfile /source/path <pid> /target/path
// or
// ./lxd forkgetfile /target/path <pid> /soruce/path <uid> <gid> <mode>
// i.e. 8 arguments, each which have a max length of PATH_MAX.
// Unfortunately, lseek() and fstat() both fail (EINVAL and 0 size) for
// procfs. Also, we can't mmap, because procfs doesn't support that, either.
//
#define CMDLINE_SIZE (8 * PATH_MAX)
void error(char *msg)
{
int old_errno = errno;
perror(msg);
fprintf(stderr, "errno: %d\n", old_errno);
}
int mkdir_p(const char *dir, mode_t mode)
{
const char *tmp = dir;
const char *orig = dir;
char *makeme;
do {
dir = tmp + strspn(tmp, "/");
tmp = dir + strcspn(dir, "/");
makeme = strndup(orig, dir - orig);
if (*makeme) {
if (mkdir(makeme, mode) && errno != EEXIST) {
fprintf(stderr, "failed to create directory '%s'", makeme);
free(makeme);
return -1;
}
}
free(makeme);
} while(tmp != dir);
return 0;
}
int copy(int target, int source)
{
ssize_t n;
char buf[1024];
if (ftruncate(target, 0) < 0) {
error("error: truncate");
return -1;
}
while ((n = read(source, buf, 1024)) > 0) {
if (write(target, buf, n) != n) {
error("error: write");
return -1;
}
}
if (n < 0) {
error("error: read");
return -1;
}
return 0;
}
int dosetns(int pid, char *nstype) {
int mntns;
char buf[PATH_MAX];
sprintf(buf, "/proc/%d/ns/%s", pid, nstype);
mntns = open(buf, O_RDONLY);
if (mntns < 0) {
error("error: open mntns");
return -1;
}
if (setns(mntns, 0) < 0) {
error("error: setns");
close(mntns);
return -1;
}
close(mntns);
return 0;
}
int manip_file_in_ns(char *rootfs, int pid, char *host, char *container, bool is_put, uid_t uid, gid_t gid, mode_t mode, uid_t defaultUid, gid_t defaultGid, mode_t defaultMode) {
int host_fd, container_fd;
int ret = -1;
int container_open_flags;
struct stat st;
int exists = 1;
host_fd = open(host, O_RDWR);
if (host_fd < 0) {
error("error: open");
return -1;
}
container_open_flags = O_RDWR;
if (is_put)
container_open_flags |= O_CREAT;
if (pid > 0) {
if (dosetns(pid, "mnt") < 0) {
error("error: setns");
goto close_host;
}
} else {
if (chroot(rootfs) < 0) {
error("error: chroot");
goto close_host;
}
if (chdir("/") < 0) {
error("error: chdir");
goto close_host;
}
}
if (is_put && stat(container, &st) < 0)
exists = 0;
umask(0);
container_fd = open(container, container_open_flags, 0);
if (container_fd < 0) {
error("error: open");
goto close_host;
}
if (is_put) {
if (!exists) {
if (mode == -1) {
mode = defaultMode;
}
if (uid == -1) {
uid = defaultUid;
}
if (gid == -1) {
gid = defaultGid;
}
}
if (copy(container_fd, host_fd) < 0) {
error("error: copy");
goto close_container;
}
if (mode != -1 && fchmod(container_fd, mode) < 0) {
error("error: chmod");
goto close_container;
}
if (fchown(container_fd, uid, gid) < 0) {
error("error: chown");
goto close_container;
}
ret = 0;
} else {
ret = copy(host_fd, container_fd);
if (fstat(container_fd, &st) < 0) {
error("error: stat");
goto close_container;
}
fprintf(stderr, "uid: %ld\n", (long)st.st_uid);
fprintf(stderr, "gid: %ld\n", (long)st.st_gid);
fprintf(stderr, "mode: %ld\n", (unsigned long)st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
}
close_container:
close(container_fd);
close_host:
close(host_fd);
return ret;
}
#define ADVANCE_ARG_REQUIRED() \
do { \
while (*cur != 0) \
cur++; \
cur++; \
if (size <= cur - buf) { \
fprintf(stderr, "not enough arguments\n"); \
_exit(1); \
} \
} while(0)
void ensure_dir(char *dest) {
struct stat sb;
if (stat(dest, &sb) == 0) {
if ((sb.st_mode & S_IFMT) == S_IFDIR)
return;
if (unlink(dest) < 0) {
fprintf(stderr, "Failed to remove old %s: %s\n", dest, strerror(errno));
_exit(1);
}
}
if (mkdir(dest, 0755) < 0) {
fprintf(stderr, "Failed to mkdir %s: %s\n", dest, strerror(errno));
_exit(1);
}
}
void ensure_file(char *dest) {
struct stat sb;
int fd;
if (stat(dest, &sb) == 0) {
if ((sb.st_mode & S_IFMT) != S_IFDIR)
return;
if (rmdir(dest) < 0) {
fprintf(stderr, "Failed to remove old %s: %s\n", dest, strerror(errno));
_exit(1);
}
}
fd = creat(dest, 0755);
if (fd < 0) {
fprintf(stderr, "Failed to mkdir %s: %s\n", dest, strerror(errno));
_exit(1);
}
close(fd);
}
void create(char *src, char *dest) {
char *destdirname;
struct stat sb;
if (stat(src, &sb) < 0) {
fprintf(stderr, "source %s does not exist\n", src);
_exit(1);
}
destdirname = strdup(dest);
destdirname = dirname(destdirname);
if (mkdir_p(destdirname, 0755) < 0) {
fprintf(stderr, "failed to create path: %s\n", destdirname);
free(destdirname);
_exit(1);
}
switch (sb.st_mode & S_IFMT) {
case S_IFDIR:
ensure_dir(dest);
return;
default:
ensure_file(dest);
return;
}
free(destdirname);
}
void forkmount(char *buf, char *cur, ssize_t size) {
char *src, *dest, *opts;
ADVANCE_ARG_REQUIRED();
int pid = atoi(cur);
if (dosetns(pid, "mnt") < 0) {
fprintf(stderr, "Failed setns to container mount namespace: %s\n", strerror(errno));
_exit(1);
}
ADVANCE_ARG_REQUIRED();
src = cur;
ADVANCE_ARG_REQUIRED();
dest = cur;
create(src, dest);
if (access(src, F_OK) < 0) {
fprintf(stderr, "Mount source doesn't exist: %s\n", strerror(errno));
_exit(1);
}
if (access(dest, F_OK) < 0) {
fprintf(stderr, "Mount destination doesn't exist: %s\n", strerror(errno));
_exit(1);
}
// Here, we always move recursively, because we sometimes allow
// recursive mounts. If the mount has no kids then it doesn't matter,
// but if it does, we want to move those too.
if (mount(src, dest, "none", MS_MOVE | MS_REC, NULL) < 0) {
fprintf(stderr, "Failed mounting %s onto %s: %s\n", src, dest, strerror(errno));
_exit(1);
}
_exit(0);
}
void forkumount(char *buf, char *cur, ssize_t size) {
ADVANCE_ARG_REQUIRED();
int pid = atoi(cur);
if (dosetns(pid, "mnt") < 0) {
fprintf(stderr, "Failed setns to container mount namespace: %s\n", strerror(errno));
_exit(1);
}
ADVANCE_ARG_REQUIRED();
if (access(cur, F_OK) < 0) {
fprintf(stderr, "Mount path doesn't exist: %s\n", strerror(errno));
_exit(1);
}
if (umount2(cur, MNT_DETACH) < 0) {
fprintf(stderr, "Error unmounting %s: %s\n", cur, strerror(errno));
_exit(1);
}
_exit(0);
}
void forkdofile(char *buf, char *cur, bool is_put, ssize_t size) {
uid_t uid = 0;
gid_t gid = 0;
mode_t mode = 0;
uid_t defaultUid = 0;
gid_t defaultGid = 0;
mode_t defaultMode = 0;
char *command = cur, *rootfs = NULL, *source = NULL, *target = NULL;
pid_t pid;
ADVANCE_ARG_REQUIRED();
rootfs = cur;
ADVANCE_ARG_REQUIRED();
pid = atoi(cur);
ADVANCE_ARG_REQUIRED();
source = cur;
ADVANCE_ARG_REQUIRED();
target = cur;
if (is_put) {
ADVANCE_ARG_REQUIRED();
uid = atoi(cur);
ADVANCE_ARG_REQUIRED();
gid = atoi(cur);
ADVANCE_ARG_REQUIRED();
mode = atoi(cur);
ADVANCE_ARG_REQUIRED();
defaultUid = atoi(cur);
ADVANCE_ARG_REQUIRED();
defaultGid = atoi(cur);
ADVANCE_ARG_REQUIRED();
defaultMode = atoi(cur);
}
_exit(manip_file_in_ns(rootfs, pid, source, target, is_put, uid, gid, mode, defaultUid, defaultGid, defaultMode));
}
void forkgetnet(char *buf, char *cur, ssize_t size) {
ADVANCE_ARG_REQUIRED();
int pid = atoi(cur);
if (dosetns(pid, "net") < 0) {
fprintf(stderr, "Failed setns to container network namespace: %s\n", strerror(errno));
_exit(1);
}
// The rest happens in Go
}
__attribute__((constructor)) void init(void) {
int cmdline;
char buf[CMDLINE_SIZE];
ssize_t size;
char *cur;
cmdline = open("/proc/self/cmdline", O_RDONLY);
if (cmdline < 0) {
error("error: open");
_exit(232);
}
memset(buf, 0, sizeof(buf));
if ((size = read(cmdline, buf, sizeof(buf)-1)) < 0) {
close(cmdline);
error("error: read");
_exit(232);
}
close(cmdline);
cur = buf;
// skip argv[0]
while (*cur != 0)
cur++;
cur++;
if (size <= cur - buf)
return;
if (strcmp(cur, "forkputfile") == 0) {
forkdofile(buf, cur, true, size);
} else if (strcmp(cur, "forkgetfile") == 0) {
forkdofile(buf, cur, false, size);
} else if (strcmp(cur, "forkmount") == 0) {
forkmount(buf, cur, size);
} else if (strcmp(cur, "forkumount") == 0) {
forkumount(buf, cur, size);
} else if (strcmp(cur, "forkgetnet") == 0) {
forkgetnet(buf, cur, size);
}
}
*/
import "C"