forked from canonical/lxd
/
main_forkmount.go
501 lines (411 loc) · 11.5 KB
/
main_forkmount.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package main
import (
"fmt"
"github.com/spf13/cobra"
// Used by cgo
_ "github.com/lxc/lxd/lxd/include"
)
/*
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <lxc/lxccontainer.h>
#include <lxc/version.h>
#include <sched.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "include/memory_utils.h"
#include "include/mount_utils.h"
#include "include/syscall_numbers.h"
#include "include/syscall_wrappers.h"
#define VERSION_AT_LEAST(major, minor, micro) \
((LXC_DEVEL == 1) || (!(major > LXC_VERSION_MAJOR || \
major == LXC_VERSION_MAJOR && minor > LXC_VERSION_MINOR || \
major == LXC_VERSION_MAJOR && minor == LXC_VERSION_MINOR && micro > LXC_VERSION_MICRO)))
extern char* advance_arg(bool required);
extern void error(char *msg);
extern void attach_userns_fd(int ns_fd);
extern int pidfd_nsfd(int pidfd, pid_t pid);
extern int preserve_ns(pid_t pid, int ns_fd, const char *ns);
extern bool change_namespaces(int pidfd, int nsfd, unsigned int flags);
extern int mount_detach_idmap(const char *path, int fd_userns);
int mkdir_p(const char *dir, mode_t mode)
{
const char *tmp = dir;
const char *orig = dir;
do {
__do_free char *makeme = NULL;
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': %s\n", makeme, strerror(errno));
return -1;
}
}
} while(tmp != dir);
return 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)
{
__do_close int fd = -EBADF;
struct stat sb;
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);
}
}
void create(char *src, char *dest)
{
__do_free char *dirdup = NULL;
char *destdirname;
struct stat sb;
if (stat(src, &sb) < 0) {
fprintf(stderr, "source %s does not exist\n", src);
_exit(1);
}
dirdup = strdup(dest);
if (!dirdup)
_exit(1);
destdirname = dirname(dirdup);
if (mkdir_p(destdirname, 0755) < 0) {
fprintf(stderr, "failed to create path: %s\n", destdirname);
_exit(1);
}
switch (sb.st_mode & S_IFMT) {
case S_IFDIR:
ensure_dir(dest);
return;
default:
ensure_file(dest);
return;
}
}
static int lxc_safe_ulong(const char *numstr, unsigned long *converted)
{
char *err = NULL;
unsigned long int uli;
while (isspace(*numstr))
numstr++;
if (*numstr == '-')
return -EINVAL;
errno = 0;
uli = strtoul(numstr, &err, 0);
if (errno == ERANGE && uli == ULONG_MAX)
return -ERANGE;
if (err == numstr || *err != '\0')
return -EINVAL;
*converted = uli;
return 0;
}
static void do_lxd_forkmount(int pidfd, int ns_fd)
{
unsigned long mntflags = 0;
int fd_tree = -EBADF;
int ret;
char *src, *dest, *idmapType, *flags;
src = advance_arg(true);
dest = advance_arg(true);
idmapType = advance_arg(true);
flags = advance_arg(true);
if (strcmp(idmapType, "idmapped") == 0) {
int fd_mntns, fd_userns;
fd_userns = preserve_ns(-ESRCH, ns_fd, "user");
if (fd_userns < 0) {
fprintf(stderr, "Failed to open user namespace of container: %s\n", strerror(errno));
_exit(1);
}
fd_mntns = preserve_ns(getpid(), -EBADF, "mnt");
if (fd_mntns < 0) {
fprintf(stderr, "Failed to open mount namespace of container: %s\n", strerror(errno));
_exit(1);
}
if (!change_namespaces(pidfd, ns_fd, CLONE_NEWNS)) {
fprintf(stderr, "Failed setns to container mount namespace: %s\n", strerror(errno));
_exit(1);
}
fd_tree = mount_detach_idmap(src, fd_userns);
if (fd_tree < 0) {
fprintf(stderr, "Failed to create detached idmapped mount \"%s\": %s\n", src, strerror(errno));
_exit(1);
}
ret = setns(fd_mntns, CLONE_NEWNS);
if (ret) {
fprintf(stderr, "Failed to switch to original mount namespace: %s\n", strerror(errno));
_exit(1);
}
close_prot_errno_disarm(fd_userns);
close_prot_errno_disarm(fd_mntns);
}
attach_userns_fd(ns_fd);
if (!change_namespaces(pidfd, ns_fd, CLONE_NEWNS)) {
fprintf(stderr, "Failed setns to container mount namespace: %s\n", strerror(errno));
_exit(1);
}
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);
}
if (fd_tree >= 0) {
ret = move_mount(fd_tree, "", -EBADF, dest, MOVE_MOUNT_F_EMPTY_PATH);
if (ret) {
fprintf(stderr, "Failed to move detached mount to target from %d to %s: %s\n", fd_tree, dest, strerror(errno));
_exit(1);
}
close_prot_errno_disarm(fd_tree);
_exit(0);
}
ret = lxc_safe_ulong(flags, &mntflags);
if (ret < 0)
_exit(1);
if (strcmp(idmapType, "shiftfs") == 0) {
// Setup shiftfs inside the container
if (mount(src, src, "shiftfs", mntflags, "passthrough=3") < 0) {
fprintf(stderr, "Failed shiftfs setup for %s: %s\n", src, 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) {
// If using shiftfs, undo the shiftfs mount
if (strcmp(idmapType, "shiftfs") == 0) {
umount2(src, MNT_DETACH);
}
fprintf(stderr, "Failed mounting %s onto %s: %s\n", src, dest, strerror(errno));
_exit(1);
}
if (strcmp(idmapType, "shiftfs") == 0) {
// Clear source mount as target is now in place
if (umount2(src, MNT_DETACH) < 0) {
fprintf(stderr, "Failed shiftfs source unmount for %s: %s\n", src, strerror(errno));
_exit(1);
}
}
_exit(0);
}
void do_lxd_forkumount(int pidfd, int ns_fd)
{
int ret;
char *path = NULL;
if (!change_namespaces(pidfd, ns_fd, CLONE_NEWNS)) {
fprintf(stderr, "Failed to setns to container mount namespace: %s\n", strerror(errno));
_exit(1);
}
path = advance_arg(true);
ret = umount2(path, MNT_DETACH);
if (ret < 0) {
// - ENOENT: The user must have unmounted and removed the path.
// - EINVAL: The user must have unmounted. Other explanations
// for EINVAL do not apply.
if (errno == ENOENT || errno == EINVAL)
_exit(0);
fprintf(stderr, "Error unmounting %s: %s\n", path, strerror(errno));
_exit(1);
}
_exit(0);
}
static void do_lxc_forkmount(void)
{
#if VERSION_AT_LEAST(3, 1, 0)
int ret;
char *config, *flags, *fstype, *lxcpath, *name, *source, *target;
struct lxc_container *c;
struct lxc_mount mnt = {0};
unsigned long mntflags = 0;
name = advance_arg(true);
lxcpath = advance_arg(true);
config = advance_arg(true);
source = advance_arg(true);
target = advance_arg(true);
fstype = advance_arg(true);
flags = advance_arg(true);
c = lxc_container_new(name, lxcpath);
if (!c)
_exit(1);
c->clear_config(c);
if (!c->load_config(c, config)) {
lxc_container_put(c);
_exit(1);
}
ret = lxc_safe_ulong(flags, &mntflags);
if (ret < 0) {
lxc_container_put(c);
_exit(1);
}
ret = c->mount(c, source, target, fstype, mntflags, NULL, &mnt);
lxc_container_put(c);
if (ret < 0)
_exit(1);
_exit(0);
#else
fprintf(stderr, "error: Called lxc_forkmount when missing LXC support\n");
_exit(1);
#endif
}
static void do_lxc_forkumount(void)
{
#if VERSION_AT_LEAST(3, 1, 0)
int ret;
char *config, *lxcpath, *name, *target;
struct lxc_container *c;
struct lxc_mount mnt = {0};
name = advance_arg(true);
lxcpath = advance_arg(true);
config = advance_arg(true);
target = advance_arg(true);
c = lxc_container_new(name, lxcpath);
if (!c)
_exit(1);
c->clear_config(c);
if (!c->load_config(c, config)) {
lxc_container_put(c);
_exit(1);
}
ret = c->umount(c, target, MNT_DETACH, &mnt);
lxc_container_put(c);
if (ret < 0)
_exit(1);
_exit(0);
#else
fprintf(stderr, "error: Called lxc_forkumount when missing LXC support\n");
_exit(1);
#endif
}
void forkmount(void)
{
char *command = NULL, *cur = NULL;
int ns_fd = -EBADF, pidfd = -EBADF;
pid_t pid = 0;
// Get the subcommand
command = advance_arg(false);
if (command == NULL || (strcmp(command, "--help") == 0 || strcmp(command, "--version") == 0 || strcmp(command, "-h") == 0))
return;
// Check that we're root
if (geteuid() != 0) {
fprintf(stderr, "Error: forkmount requires root privileges\n");
_exit(1);
}
// skip "--"
advance_arg(true);
// Call the subcommands
if (strcmp(command, "lxd-mount") == 0) {
// Get the pid
cur = advance_arg(false);
if (cur == NULL || (strcmp(cur, "--help") == 0 || strcmp(cur, "--version") == 0 || strcmp(cur, "-h") == 0))
return;
pid = atoi(cur);
if (pid <= 0)
_exit(EXIT_FAILURE);
pidfd = atoi(advance_arg(true));
ns_fd = pidfd_nsfd(pidfd, pid);
if (ns_fd < 0)
_exit(EXIT_FAILURE);
do_lxd_forkmount(pidfd, ns_fd);
} else if (strcmp(command, "lxc-mount") == 0) {
do_lxc_forkmount();
} else if (strcmp(command, "lxd-umount") == 0) {
// Get the pid
cur = advance_arg(false);
if (cur == NULL || (strcmp(cur, "--help") == 0 || strcmp(cur, "--version") == 0 || strcmp(cur, "-h") == 0))
return;
pid = atoi(cur);
if (pid <= 0)
_exit(EXIT_FAILURE);
pidfd = atoi(advance_arg(true));
ns_fd = pidfd_nsfd(pidfd, pid);
if (ns_fd < 0)
_exit(EXIT_FAILURE);
do_lxd_forkumount(pidfd, ns_fd);
} else if (strcmp(command, "lxc-umount") == 0) {
do_lxc_forkumount();
}
}
*/
import "C"
type cmdForkmount struct {
global *cmdGlobal
}
func (c *cmdForkmount) Command() *cobra.Command {
// Main subcommand
cmd := &cobra.Command{}
cmd.Use = "forkmount"
cmd.Short = "Perform mount operations"
cmd.Long = `Description:
Perform mount operations
This set of internal commands are used for all container mount
operations.
`
cmd.Hidden = true
// mount
cmdLXCMount := &cobra.Command{}
cmdLXCMount.Use = "lxc-mount <name> <lxcpath> <configpath> <source> <destination> <fstype> <flags>"
cmdLXCMount.Args = cobra.ExactArgs(7)
cmdLXCMount.RunE = c.Run
cmd.AddCommand(cmdLXCMount)
cmdLXDMount := &cobra.Command{}
cmdLXDMount.Use = "lxd-mount <PID> <PidFd> <source> <destination> <idmapType> <flags>"
cmdLXDMount.Args = cobra.ExactArgs(6)
cmdLXDMount.RunE = c.Run
cmd.AddCommand(cmdLXDMount)
// umount
cmdLXCUmount := &cobra.Command{}
cmdLXCUmount.Use = "lxc-umount <name> <lxcpath> <configpath> <path>"
cmdLXCUmount.Args = cobra.ExactArgs(4)
cmdLXCUmount.RunE = c.Run
cmd.AddCommand(cmdLXCUmount)
cmdLXDUmount := &cobra.Command{}
cmdLXDUmount.Use = "lxd-umount <PID> <PidFd> <path>"
cmdLXDUmount.Args = cobra.ExactArgs(3)
cmdLXDUmount.RunE = c.Run
cmd.AddCommand(cmdLXDUmount)
// Workaround for subcommand usage errors. See: https://github.com/spf13/cobra/issues/706
cmd.Args = cobra.NoArgs
cmd.Run = func(cmd *cobra.Command, args []string) { cmd.Usage() }
return cmd
}
func (c *cmdForkmount) Run(cmd *cobra.Command, args []string) error {
return fmt.Errorf("This command should have been intercepted in cgo")
}