-
-
Notifications
You must be signed in to change notification settings - Fork 225
/
main_forkmount.go
691 lines (556 loc) · 15.2 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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
package main
/*
#include "config.h"
#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 "incus.h"
#include "memory_utils.h"
#include "mount_utils.h"
#include "syscall_numbers.h"
#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)))
static 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;
}
static 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);
}
}
static 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);
}
}
static void create(int fd_src, char *src, char *dest)
{
__do_free char *dirdup = NULL;
char *destdirname;
struct stat sb;
if (src) {
if (stat(src, &sb) < 0)
die("source %s does not exist", src);
} else {
if (fstat(fd_src, &sb) < 0)
die("source %s does not exist", src);
}
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_incus_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(-EBADF, 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 = incus_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);
// 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);
}
static int lxc_safe_uint(const char *numstr, unsigned int *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;
if (uli > UINT_MAX)
return -ERANGE;
*converted = (unsigned int)uli;
return 0;
}
static int mnt_attributes_new(unsigned int old_flags, unsigned int *new_flags)
{
unsigned int flags = 0;
if (old_flags & MS_RDONLY) {
flags |= MOUNT_ATTR_RDONLY;
old_flags &= ~MS_RDONLY;
}
if (old_flags & MS_NOSUID) {
flags |= MOUNT_ATTR_NOSUID;
old_flags &= ~MS_NOSUID;
}
if (old_flags & MS_NODEV) {
flags |= MOUNT_ATTR_NODEV;
old_flags &= ~MS_NODEV;
}
if (old_flags & MS_NOEXEC) {
flags |= MOUNT_ATTR_NOEXEC;
old_flags &= ~MS_NOEXEC;
}
if (old_flags & MS_RELATIME) {
flags |= MOUNT_ATTR_RELATIME;
old_flags &= ~MS_RELATIME;
}
if (old_flags & MS_NOATIME) {
flags |= MOUNT_ATTR_NOATIME;
old_flags &= ~MS_NOATIME;
}
if (old_flags & MS_STRICTATIME) {
flags |= MOUNT_ATTR_STRICTATIME;
old_flags &= ~MS_STRICTATIME;
}
if (old_flags & MS_NODIRATIME) {
flags |= MOUNT_ATTR_NODIRATIME;
old_flags &= ~MS_NODIRATIME;
}
*new_flags |= flags;
return old_flags;
}
static int make_final_open(struct stat *st_src, const char *dest)
{
int ret;
int flags = O_CLOEXEC | O_NOFOLLOW | O_NOCTTY | O_CLOEXEC;
struct stat st_dest;
ret = stat(dest, &st_dest);
if (ret == 0) {
if ((st_dest.st_mode & S_IFMT) == (st_src->st_mode & S_IFMT))
goto out_open;
ret = remove(dest);
if (ret)
return -1;
}
if ((st_src->st_mode & S_IFMT) == S_IFDIR)
ret = mkdir(dest, 0000);
else
ret = mknod(dest, S_IFREG | 0000, 0);
if (ret)
return -1;
out_open:
return open(dest, flags | O_PATH);
}
static int make_dest_open(int fd_src, const char *dest)
{
__do_free char *dirdup = NULL;
int ret;
char *destdirname;
struct stat st_src;
ret = fstat(fd_src, &st_src);
if (ret)
return -1;
dirdup = strdup(dest);
if (!dirdup)
return -1;
destdirname = dirname(dirdup);
ret = mkdir_p(destdirname, 0755);
if (ret)
return -1;
return make_final_open(&st_src, dest);
}
static void do_move_forkmount(int pidfd, int ns_fd)
{
__do_close int fs_fd = -EBADF, mnt_fd = -EBADF, fd_userns = -EBADF,
dest_fd = -EBADF;
int ret;
char *fstype, *src, *dest, *idmapType, *flags;
unsigned int old_mntflags = 0, new_mntflags = 0;
fstype = advance_arg(true);
src = advance_arg(true);
dest = advance_arg(true);
idmapType = advance_arg(true);
flags = advance_arg(true);
ret = lxc_safe_uint(flags, &old_mntflags);
if (ret < 0)
die("parse mount flags");
mnt_attributes_new(old_mntflags, &new_mntflags);
if (strcmp(fstype, "") && strcmp(fstype, "none")) {
fs_fd = incus_fsopen(fstype, FSOPEN_CLOEXEC);
if (fs_fd < 0)
die("fsopen: %s", fstype);
ret = incus_fsconfig(fs_fd, FSCONFIG_SET_STRING, "source", src, 0);
if (ret < 0)
die("fsconfig: source");
ret = incus_fsconfig(fs_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0);
if (ret < 0)
die("fsconfig: create");
mnt_fd = incus_fsmount(fs_fd, FSMOUNT_CLOEXEC, new_mntflags);
if (mnt_fd < 0)
die("fsmount");
} else {
mnt_fd = incus_open_tree(-EBADF, src, OPEN_TREE_CLOEXEC | OPEN_TREE_CLONE);
if (mnt_fd < 0)
die("open_tree");
}
fd_userns = preserve_ns(-ESRCH, ns_fd, "user");
if (fd_userns < 0)
die("preserve userns");
if (strcmp(idmapType, "idmapped") == 0) {
struct lxc_mount_attr attr = {
.attr_set = MOUNT_ATTR_IDMAP,
.userns_fd = fd_userns,
};
ret = incus_mount_setattr(mnt_fd, "", AT_EMPTY_PATH, &attr, sizeof(attr));
if (ret)
die("idmap mount");
}
attach_userns_fd(ns_fd);
if (!change_namespaces(pidfd, ns_fd, CLONE_NEWNS))
die("Failed setns to container mount namespace");
dest_fd = make_dest_open(mnt_fd, dest);
if (dest_fd < 0)
die("Failed to create destination mount point");
ret = incus_move_mount(mnt_fd, "", dest_fd, "",
MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_T_EMPTY_PATH);
if (ret)
die("Failed to move detached mount to target from %d to %s", mnt_fd, dest);
_exit(EXIT_SUCCESS);
}
static void do_incus_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, "go-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_incus_forkmount(pidfd, ns_fd);
} else if (strcmp(command, "lxc-mount") == 0) {
do_lxc_forkmount();
} else if (strcmp(command, "move-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_move_forkmount(pidfd, ns_fd);
} else if (strcmp(command, "go-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_incus_forkumount(pidfd, ns_fd);
} else if (strcmp(command, "lxc-umount") == 0) {
do_lxc_forkumount();
}
}
*/
import "C"
import (
"fmt"
"github.com/spf13/cobra"
// Used by cgo
_ "github.com/lxc/incus/shared/cgo"
)
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)
cmdGoMount := &cobra.Command{}
cmdGoMount.Use = "go-mount <PID> <PidFd> <source> <destination> <idmapType> <flags>"
cmdGoMount.Args = cobra.ExactArgs(6)
cmdGoMount.RunE = c.Run
cmd.AddCommand(cmdGoMount)
// umount
cmdLXCUmount := &cobra.Command{}
cmdLXCUmount.Use = "lxc-umount <name> <lxcpath> <configpath> <path>"
cmdLXCUmount.Args = cobra.ExactArgs(4)
cmdLXCUmount.RunE = c.Run
cmd.AddCommand(cmdLXCUmount)
cmdGoUmount := &cobra.Command{}
cmdGoUmount.Use = "go-umount <PID> <PidFd> <path>"
cmdGoUmount.Args = cobra.ExactArgs(3)
cmdGoUmount.RunE = c.Run
cmd.AddCommand(cmdGoUmount)
// 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")
}