-
Notifications
You must be signed in to change notification settings - Fork 64
/
bindfs.c
2975 lines (2596 loc) · 88.2 KB
/
bindfs.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
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2006,2007,2008,2009,2010,2012,2019 Martin Pärtel <martin.partel@gmail.com>
This file is part of bindfs.
bindfs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
bindfs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with bindfs. If not, see <http://www.gnu.org/licenses/>.
This file is based on fusexmp_fh.c from FUSE 2.5.3,
which had the following notice:
---
FUSE: Filesystem in Userspace
Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
---
*/
#include <config.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#include <sys/time.h>
#include <sys/statvfs.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <getopt.h>
#include <assert.h>
#include <pwd.h>
#include <grp.h>
#include <limits.h>
#include <signal.h>
#ifdef HAVE_SETXATTR
#include <sys/xattr.h>
#endif
#ifdef HAVE_FUSE_3
#ifndef __NR_renameat2
#include <libgen.h> // For dirname(), basename()
#endif
#endif
#ifdef __linux__
#include <sys/mman.h>
#include <fcntl.h>
#include <linux/fs.h> // For BLKGETSIZE64
#ifndef O_DIRECT
#define O_DIRECT 00040000 /* direct disk access hint */
#endif
#endif
#include <fuse.h>
#include <fuse_opt.h>
#include "arena.h"
#include "debug.h"
#include "misc.h"
#include "permchain.h"
#include "rate_limiter.h"
#include "userinfo.h"
#include "usermap.h"
/* Socket file support for MacOS and FreeBSD */
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/socket.h>
#include <sys/un.h>
#endif
/* Apple Structs */
#ifdef __APPLE__
#include <sys/param.h>
#define G_PREFIX "org"
#define G_KAUTH_FILESEC_XATTR G_PREFIX ".apple.system.Security"
#define A_PREFIX "com"
#define A_KAUTH_FILESEC_XATTR A_PREFIX ".apple.system.Security"
#define XATTR_APPLE_PREFIX "com.apple."
// Yes, Apple asks us to copy/paste these -.-
#define LOCK_SH 1 /* shared lock */
#define LOCK_EX 2 /* exclusive lock */
#define LOCK_NB 4 /* don't block when locking */
#define LOCK_UN 8 /* unlock */
int flock(int fd, int operation);
#endif
/* We pessimistically assume signed uid_t and gid_t in our overflow checks,
mostly because supporting both cases would require a bunch more code. */
static const int64_t UID_T_MAX = ((1LL << (sizeof(uid_t)*8-1)) - 1);
static const int64_t GID_T_MAX = ((1LL << (sizeof(gid_t)*8-1)) - 1);
static const int UID_GID_OVERFLOW_ERRNO = EIO;
/* SETTINGS */
static struct Settings {
const char *progname;
struct permchain *permchain; /* permission bit rules. see permchain.h */
uid_t new_uid; /* user-specified uid */
gid_t new_gid; /* user-specified gid */
uid_t create_for_uid;
gid_t create_for_gid;
char *mntsrc;
char *mntdest;
int mntdest_len; /* caches strlen(mntdest) */
int mntsrc_fd;
char *original_working_dir;
mode_t original_umask;
UserMap *usermap; /* From the --map option. */
UserMap *usermap_reverse;
RateLimiter *read_limiter;
RateLimiter *write_limiter;
enum CreatePolicy {
CREATE_AS_USER,
CREATE_AS_MOUNTER
} create_policy;
struct permchain *create_permchain; /* the --create-with-perms option */
enum ChownPolicy {
CHOWN_NORMAL,
CHOWN_IGNORE,
CHOWN_DENY
} chown_policy;
enum ChgrpPolicy {
CHGRP_NORMAL,
CHGRP_IGNORE,
CHGRP_DENY
} chgrp_policy;
enum ChmodPolicy {
CHMOD_NORMAL,
CHMOD_IGNORE,
CHMOD_DENY
} chmod_policy;
int chmod_allow_x;
struct permchain *chmod_permchain; /* the --chmod-filter option */
enum XAttrPolicy {
XATTR_UNIMPLEMENTED,
XATTR_READ_ONLY,
XATTR_READ_WRITE
} xattr_policy;
int delete_deny;
int rename_deny;
int mirrored_users_only;
uid_t *mirrored_users;
int num_mirrored_users;
gid_t *mirrored_members;
int num_mirrored_members;
int hide_hard_links;
int resolve_symlinks;
int block_devices_as_files;
enum ResolvedSymlinkDeletion {
RESOLVED_SYMLINK_DELETION_DENY,
RESOLVED_SYMLINK_DELETION_SYMLINK_ONLY,
RESOLVED_SYMLINK_DELETION_SYMLINK_FIRST,
RESOLVED_SYMLINK_DELETION_TARGET_FIRST
} resolved_symlink_deletion_policy;
int realistic_permissions;
int ctime_from_mtime;
int enable_lock_forwarding;
int enable_ioctl;
#ifdef __linux__
int forward_odirect;
size_t odirect_alignment;
bool direct_io;
#endif
int64_t uid_offset;
int64_t gid_offset;
} settings;
static bool bindfs_init_failed = false;
/* PROTOTYPES */
static int is_mirroring_enabled(void);
/* Checks whether the uid is to be the mirrored owner of all files. */
static int is_mirrored_user(uid_t uid);
/* Processes the virtual path to a real path. Always free() the result. */
static char *process_path(const char *path, bool resolve_symlinks);
/* The common parts of getattr and fgetattr. */
static int getattr_common(const char *path, struct stat *stbuf);
/* Chowns a new file if necessary. */
static int chown_new_file(const char *path, struct fuse_context *fc, int (*chown_func)(const char*, uid_t, gid_t));
/* Unified implementation of unlink and rmdir. */
static int delete_file(const char *path, int (*target_delete_func)(const char *));
/* Apply offsets with overflow checking. */
static bool apply_uid_offset(uid_t *uid);
static bool apply_gid_offset(gid_t *gid);
static bool unapply_uid_offset(uid_t *uid);
static bool unapply_gid_offset(gid_t *gid);
static bool bounded_add(int64_t* a, int64_t b, int64_t max);
#ifdef __linux__
static size_t round_up_buffer_size_for_direct_io(size_t size);
#endif
/* FUSE callbacks */
#ifdef HAVE_FUSE_3
static void *bindfs_init(struct fuse_conn_info *conn, struct fuse_config *cfg);
#else
static void *bindfs_init(struct fuse_conn_info *conn);
#endif
static void bindfs_destroy(void *private_data);
#ifdef HAVE_FUSE_3
static int bindfs_getattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi);
#else
static int bindfs_getattr(const char *path, struct stat *stbuf);
static int bindfs_fgetattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi);
#endif
static int bindfs_readlink(const char *path, char *buf, size_t size);
#ifdef HAVE_FUSE_3
static int bindfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi, enum fuse_readdir_flags flags);
#else
static int bindfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi);
#endif
static int bindfs_mknod(const char *path, mode_t mode, dev_t rdev);
static int bindfs_mkdir(const char *path, mode_t mode);
static int bindfs_unlink(const char *path);
static int bindfs_rmdir(const char *path);
static int bindfs_symlink(const char *from, const char *to);
#ifdef HAVE_FUSE_3
static int bindfs_rename(const char *from, const char *to, unsigned int flags);
#else
static int bindfs_rename(const char *from, const char *to);
#endif
static int bindfs_link(const char *from, const char *to);
#ifdef HAVE_FUSE_3
static int bindfs_chmod(const char *path, mode_t mode, struct fuse_file_info *fi);
#else
static int bindfs_chmod(const char *path, mode_t mode);
#endif
#ifdef HAVE_FUSE_3
static int bindfs_chown(const char *path, uid_t uid, gid_t gid, struct fuse_file_info *fi);
#else
static int bindfs_chown(const char *path, uid_t uid, gid_t gid);
#endif
#ifdef HAVE_FUSE_3
static int bindfs_truncate(const char *path, off_t size,
struct fuse_file_info *fi);
#else
static int bindfs_truncate(const char *path, off_t size);
static int bindfs_ftruncate(const char *path, off_t size,
struct fuse_file_info *fi);
#endif
#ifdef HAVE_FUSE_3
static int bindfs_utimens(const char *path, const struct timespec tv[2], struct fuse_file_info *fi);
#else
static int bindfs_utimens(const char *path, const struct timespec tv[2]);
#endif
static int bindfs_create(const char *path, mode_t mode, struct fuse_file_info *fi);
static int bindfs_open(const char *path, struct fuse_file_info *fi);
static int bindfs_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi);
static int bindfs_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi);
#if defined(HAVE_FUSE_29) || defined(HAVE_FUSE_3)
static int bindfs_lock(const char *path, struct fuse_file_info *fi, int cmd,
struct flock *lock);
static int bindfs_flock(const char *path, struct fuse_file_info *fi, int op);
#endif
#ifdef HAVE_FUSE_3
static int bindfs_ioctl(const char *path, int cmd, void *arg,
struct fuse_file_info *fi, unsigned int flags,
void *data);
#else
static int bindfs_ioctl(const char *path, int cmd, void *arg,
struct fuse_file_info *fi, unsigned int flags,
void *data);
#endif
static int bindfs_statfs(const char *path, struct statvfs *stbuf);
#if __APPLE__
static int bindfs_statfs_x(const char *path, struct statfs *stbuf);
#endif
static int bindfs_release(const char *path, struct fuse_file_info *fi);
static int bindfs_fsync(const char *path, int isdatasync,
struct fuse_file_info *fi);
static void print_usage(const char *progname);
static int process_option(void *data, const char *arg, int key,
struct fuse_args *outargs);
static int parse_mirrored_users(char* mirror);
static int parse_user_map(UserMap *map, UserMap *reverse_map, char *spec);
static char *get_working_dir(void);
static void maybe_stdout_stderr_to_file(void);
/* Sets up handling of SIGUSR1. */
static void setup_signal_handling(void);
static void signal_handler(int sig);
static void atexit_func(void);
/*
Ignore some options (starting with -o)
like "mount.fuse" would do in "libfuse/util/mount.fuse.c"
Why? Because some of those options like "nofail"
are special ones interpreted by systemd in /etc/fstab
*/
struct fuse_args filter_special_opts(struct fuse_args *args);
static bool keep_option(const char* opt);
static int is_mirroring_enabled(void)
{
return settings.num_mirrored_users + settings.num_mirrored_members > 0;
}
static int is_mirrored_user(uid_t uid)
{
int i;
for (i = 0; i < settings.num_mirrored_users; ++i) {
if (settings.mirrored_users[i] == uid) {
return 1;
}
}
for (i = 0; i < settings.num_mirrored_members; ++i) {
if (user_belongs_to_group(uid, settings.mirrored_members[i])) {
return 1;
}
}
return 0;
}
static char *process_path(const char *path, bool resolve_symlinks)
{
if (path == NULL) { /* possible? */
errno = EINVAL;
return NULL;
}
while (*path == '/')
++path;
if (*path == '\0')
path = ".";
if (resolve_symlinks && settings.resolve_symlinks) {
char* result = realpath(path, NULL);
if (result == NULL) {
if (errno == ENOENT) {
/* Broken symlink (or missing file). Don't return null because
we want to be able to operate on broken symlinks. */
return strdup(path);
}
} else if (path_starts_with(result, settings.mntdest, settings.mntdest_len)) {
/* Recursive call. We cannot handle this without deadlocking,
especially in single-threaded mode. */
DPRINTF("Denying recursive access to mountpoint \"%s\" at \"%s\"", settings.mntdest, result);
free(result);
errno = EPERM;
return NULL;
}
return result;
} else {
return strdup(path);
}
}
static int getattr_common(const char *procpath, struct stat *stbuf)
{
struct fuse_context *fc = fuse_get_context();
/* Copy mtime (file content modification time)
to ctime (inode/status change time)
if the user asked for that */
if (settings.ctime_from_mtime) {
#ifdef HAVE_STAT_NANOSEC
// TODO: does this work on OS X?
stbuf->st_ctim = stbuf->st_mtim;
#else
stbuf->st_ctime = stbuf->st_mtime;
#endif
}
/* Possibly map user/group */
stbuf->st_uid = usermap_get_uid_or_default(settings.usermap, stbuf->st_uid, stbuf->st_uid);
stbuf->st_gid = usermap_get_gid_or_default(settings.usermap, stbuf->st_gid, stbuf->st_gid);
if (!apply_uid_offset(&stbuf->st_uid)) {
return -UID_GID_OVERFLOW_ERRNO;
}
if (!apply_gid_offset(&stbuf->st_gid)) {
return -UID_GID_OVERFLOW_ERRNO;
}
/* Report user-defined owner/group if specified */
if (settings.new_uid != (uid_t)-1)
stbuf->st_uid = settings.new_uid;
if (settings.new_gid != (gid_t)-1)
stbuf->st_gid = settings.new_gid;
/* Mirrored user? */
if (is_mirroring_enabled() && is_mirrored_user(fc->uid)) {
stbuf->st_uid = fc->uid;
} else if (settings.mirrored_users_only && fc->uid != 0) {
stbuf->st_mode &= ~0777; /* Deny all access if mirror-only and not root */
return 0;
}
/* Hide hard links */
if (settings.hide_hard_links) {
stbuf->st_nlink = 1;
}
/* Block files as regular files. */
if (settings.block_devices_as_files && S_ISBLK(stbuf->st_mode)) {
stbuf->st_mode ^= S_IFBLK | S_IFREG; // Flip both bits
int fd = open(procpath, O_RDONLY);
if (fd == -1) {
return -errno;
}
#ifdef __linux__
uint64_t size;
ioctl(fd, BLKGETSIZE64, &size);
stbuf->st_size = (off_t)size;
if (stbuf->st_size < 0) { // Underflow
close(fd);
return -EOVERFLOW;
}
#else
off_t size = lseek(fd, 0, SEEK_END);
if (size == (off_t)-1) {
close(fd);
return -errno;
}
stbuf->st_size = size;
#endif
close(fd);
}
/* Then permission bits. Symlink permissions don't matter, though. */
if ((stbuf->st_mode & S_IFLNK) != S_IFLNK) {
/* Apply user-defined permission bit modifications */
stbuf->st_mode = permchain_apply(settings.permchain, stbuf->st_mode);
/* Check that we can really do what we promise if --realistic-permissions was given */
if (settings.realistic_permissions) {
if (access(procpath, R_OK) == -1)
stbuf->st_mode &= ~0444;
if (access(procpath, W_OK) == -1)
stbuf->st_mode &= ~0222;
if (access(procpath, X_OK) == -1)
stbuf->st_mode &= ~0111;
}
}
return 0;
}
/* FIXME: another thread may race to see the old owner before the chown is done.
Is there a scenario where this compromises security? Or application correctness? */
static int chown_new_file(const char *path, struct fuse_context *fc, int (*chown_func)(const char*, uid_t, gid_t))
{
uid_t file_owner;
gid_t file_group;
if (settings.create_policy == CREATE_AS_USER) {
char *path_copy;
const char *dir_path;
struct stat stbuf;
file_owner = fc->uid;
file_group = fc->gid;
path_copy = strdup(path);
dir_path = my_dirname(path_copy);
if (lstat(dir_path, &stbuf) != -1 && stbuf.st_mode & S_ISGID)
file_group = -1;
free(path_copy);
} else {
file_owner = -1;
file_group = -1;
}
file_owner = usermap_get_uid_or_default(settings.usermap_reverse, fc->uid, file_owner);
file_group = usermap_get_gid_or_default(settings.usermap_reverse, fc->gid, file_group);
if (file_owner != (uid_t)-1) {
if (!unapply_uid_offset(&file_owner)) {
return -UID_GID_OVERFLOW_ERRNO;
}
}
if (file_group != (gid_t)-1) {
if (!unapply_gid_offset(&file_group)) {
return -UID_GID_OVERFLOW_ERRNO;
}
}
if (settings.create_for_uid != (uid_t)-1)
file_owner = settings.create_for_uid;
if (settings.create_for_gid != (gid_t)-1)
file_group = settings.create_for_gid;
if ((file_owner != (uid_t)-1) || (file_group != (gid_t)-1)) {
if (chown_func(path, file_owner, file_group) == -1) {
DPRINTF("Failed to chown new file or directory (%d)", errno);
}
}
return 0;
}
static int delete_file(const char *path, int (*target_delete_func)(const char *)) {
int res;
char *real_path;
struct stat st;
char *also_try_delete = NULL;
char *unlink_first = NULL;
int (*main_delete_func)(const char*) = target_delete_func;
if (settings.delete_deny)
return -EPERM;
real_path = process_path(path, false);
if (real_path == NULL)
return -errno;
if (settings.resolve_symlinks) {
if (lstat(real_path, &st) == -1) {
free(real_path);
return -errno;
}
if (S_ISLNK(st.st_mode)) {
switch(settings.resolved_symlink_deletion_policy) {
case RESOLVED_SYMLINK_DELETION_DENY:
free(real_path);
return -EPERM;
case RESOLVED_SYMLINK_DELETION_SYMLINK_ONLY:
main_delete_func = &unlink;
break;
case RESOLVED_SYMLINK_DELETION_SYMLINK_FIRST:
main_delete_func = &unlink;
also_try_delete = realpath(real_path, NULL);
if (also_try_delete == NULL && errno != ENOENT) {
free(real_path);
return -errno;
}
break;
case RESOLVED_SYMLINK_DELETION_TARGET_FIRST:
unlink_first = realpath(real_path, NULL);
if (unlink_first == NULL && errno != ENOENT) {
free(real_path);
return -errno;
}
if (unlink_first != NULL) {
res = unlink(unlink_first);
free(unlink_first);
if (res == -1) {
free(real_path);
return -errno;
}
}
break;
}
}
}
res = main_delete_func(real_path);
free(real_path);
if (res == -1) {
free(also_try_delete);
return -errno;
}
if (also_try_delete != NULL) {
(void)target_delete_func(also_try_delete);
free(also_try_delete);
}
return 0;
}
static bool apply_uid_offset(uid_t *uid) {
int64_t value = (int64_t)*uid;
if (bounded_add(&value, settings.uid_offset, UID_T_MAX)) {
*uid = (uid_t)value;
return true;
} else {
DPRINTF("UID %ld out of bounds after applying offset", value);
return false;
}
}
static bool apply_gid_offset(gid_t *gid) {
int64_t value = (int64_t)*gid;
if (bounded_add(&value, settings.gid_offset, GID_T_MAX)) {
*gid = (uid_t)value;
return true;
} else {
DPRINTF("GID %ld out of bounds after applying offset", value);
return false;
}
}
static bool unapply_uid_offset(uid_t *uid) {
int64_t value = (int64_t)*uid;
if (bounded_add(&value, -settings.uid_offset, UID_T_MAX)) {
*uid = (uid_t)value;
return true;
} else {
DPRINTF("UID %ld out of bounds after unapplying offset", value);
return false;
}
}
static bool unapply_gid_offset(gid_t *gid) {
int64_t value = (int64_t)*gid;
if (bounded_add(&value, -settings.gid_offset, GID_T_MAX)) {
*gid = (uid_t)value;
return true;
} else {
DPRINTF("GID %ld out of bounds after unapplying offset", value);
return false;
}
}
static bool bounded_add(int64_t* a, int64_t b, int64_t max) {
int64_t result;
if (!__builtin_add_overflow(*a, b, &result) && 0 <= result && result <= max) {
*a = result;
return true;
}
return false;
}
#ifdef __linux__
static size_t round_up_buffer_size_for_direct_io(size_t size)
{
// `man 2 open` says this should be block-size aligned and that there's no
// general way to determine this. Empirically the page size should be good enough.
// See also: Pull Request #74
size_t alignment = settings.odirect_alignment;
size_t rem = size % alignment;
if (rem == 0) {
return size;
}
return size - rem + alignment;
}
#endif
#ifdef HAVE_FUSE_3
static void *bindfs_init(struct fuse_conn_info *conn, struct fuse_config *cfg)
#else
static void *bindfs_init(struct fuse_conn_info *conn)
#endif
{
(void) conn;
#ifdef HAVE_FUSE_3
cfg->use_ino = 1;
// Disable caches so changes in base FS are visible immediately.
// Especially the attribute cache must be disabled when different users
// might see different file attributes, such as when mirroring users.
cfg->entry_timeout = 0;
cfg->attr_timeout = 0;
cfg->negative_timeout = 0;
#ifdef __linux__
cfg->direct_io = settings.direct_io;
#endif
#endif
assert(settings.permchain != NULL);
assert(settings.mntsrc_fd > 0);
maybe_stdout_stderr_to_file();
if (fchdir(settings.mntsrc_fd) != 0) {
fprintf(
stderr,
"Could not change working directory to '%s': %s\n",
settings.mntsrc,
strerror(errno)
);
bindfs_init_failed = true;
#ifdef __OpenBSD__
exit(1);
#else
fuse_exit(fuse_get_context()->fuse);
#endif
}
return NULL;
}
static void bindfs_destroy(void *private_data)
{
(void)private_data;
}
#ifdef HAVE_FUSE_3
static int bindfs_getattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi)
#else
static int bindfs_getattr(const char *path, struct stat *stbuf)
#endif
{
int res;
char *real_path;
#ifdef HAVE_FUSE_3
(void)fi;
#endif
real_path = process_path(path, true);
if (real_path == NULL)
return -errno;
if (lstat(real_path, stbuf) == -1) {
free(real_path);
return -errno;
}
res = getattr_common(real_path, stbuf);
free(real_path);
return res;
}
#ifndef HAVE_FUSE_3
static int bindfs_fgetattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi)
{
int res;
char *real_path;
real_path = process_path(path, true);
if (real_path == NULL)
return -errno;
if (fstat(fi->fh, stbuf) == -1) {
free(real_path);
return -errno;
}
res = getattr_common(real_path, stbuf);
free(real_path);
return res;
}
#endif
static int bindfs_readlink(const char *path, char *buf, size_t size)
{
int res;
char *real_path;
real_path = process_path(path, true);
if (real_path == NULL)
return -errno;
/* No need to check for access to the link itself, since symlink
permissions don't matter. Access to the path components of the symlink
are automatically queried by FUSE. */
res = readlink(real_path, buf, size - 1);
free(real_path);
if (res == -1)
return -errno;
buf[res] = '\0';
return 0;
}
#ifdef HAVE_FUSE_3
static int bindfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi, enum fuse_readdir_flags flags)
#else
static int bindfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
#endif
{
(void)offset;
(void)fi;
#ifdef HAVE_FUSE_3
(void)flags;
#endif
char *real_path = process_path(path, true);
if (real_path == NULL) {
return -errno;
}
DIR *dp = opendir(real_path);
if (dp == NULL) {
free(real_path);
return -errno;
}
long pc_ret = pathconf(real_path, _PC_NAME_MAX);
if (pc_ret < 0) {
DPRINTF("pathconf failed: %s (%d)", strerror(errno), errno);
pc_ret = NAME_MAX;
} else if (pc_ret == 0) {
// Workaround for some source filesystems erroneously returning 0
// (see issue #54).
pc_ret = NAME_MAX;
}
// Path buffer for resolving symlinks
struct memory_block resolve_buf = MEMORY_BLOCK_INITIALIZER;
if (settings.resolve_symlinks) {
int len = strlen(real_path);
append_to_memory_block(&resolve_buf, real_path, len + 1);
resolve_buf.ptr[len] = '/';
}
free(real_path);
real_path = NULL;
int result = 0;
while (1) {
errno = 0;
struct dirent *de = readdir(dp);
if (de == NULL) {
if (errno != 0) {
result = -errno;
}
break;
}
struct stat st;
memset(&st, 0, sizeof(st));
st.st_ino = de->d_ino;
st.st_mode = de->d_type << 12;
if (settings.resolve_symlinks && (st.st_mode & S_IFLNK) == S_IFLNK) {
int file_len = strlen(de->d_name) + 1; // (include null terminator)
append_to_memory_block(&resolve_buf, de->d_name, file_len);
char *resolved = realpath(resolve_buf.ptr, NULL);
resolve_buf.size -= file_len;
if (resolved) {
if (lstat(resolved, &st) == -1) {
result = -errno;
break;
}
free(resolved);
}
}
// See issue #28 for why we pass a 0 offset to `filler` and ignore
// `offset`.
//
// Given a 0 offset, `filler` should never return non-zero, so we
// consider it an error if it does. It is undocumented whether it sets
// errno in that case, so we zero it first and set it ourself if it
// doesn't.
#ifdef HAVE_FUSE_3
// TODO: FUSE_FILL_DIR_PLUS doesn't work with offset==0: https://github.com/libfuse/libfuse/issues/583
// Work around this by implementing the offset!=1 mode. Be careful - it's quite error-prone!
if (filler(buf, de->d_name, &st, 0, FUSE_FILL_DIR_PLUS) != 0) {
#else
if (filler(buf, de->d_name, &st, 0) != 0) {
#endif
result = errno != 0 ? -errno : -EIO;
break;
}
}
if (settings.resolve_symlinks) {
free_memory_block(&resolve_buf);
}
closedir(dp);
return result;
}
static int bindfs_mknod(const char *path, mode_t mode, dev_t rdev)
{
int res;
struct fuse_context *fc;
char *real_path;
real_path = process_path(path, true);
if (real_path == NULL)
return -errno;
mode = permchain_apply(settings.create_permchain, mode);
if (S_ISFIFO(mode)) {
res = mkfifo(real_path, mode);
#if defined(__APPLE__) || defined(__FreeBSD__)
} else if (S_ISSOCK(mode)) {
struct sockaddr_un su;
int fd;
if (strlen(real_path) >= sizeof(su.sun_path)) {
errno = ENAMETOOLONG;
return -1;
}
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd >= 0) {
/*
* We must bind the socket to the underlying file
* system to create the socket file, even though
* we'll never listen on this socket.
*/
su.sun_family = AF_UNIX;
strncpy(su.sun_path, real_path, sizeof(su.sun_path));
res = bind(fd, (struct sockaddr*)&su, sizeof(su));
close(fd);
} else {
res = -1;
}
#endif
} else {
res = mknod(real_path, mode, rdev);
}
if (res == -1) {
free(real_path);
return -errno;
}
fc = fuse_get_context();
res = chown_new_file(real_path, fc, &chown);
free(real_path);
return res;
}
static int bindfs_mkdir(const char *path, mode_t mode)
{
int res;
struct fuse_context *fc;
char *real_path;
real_path = process_path(path, true);
if (real_path == NULL)
return -errno;
mode |= S_IFDIR; /* tell permchain_apply this is a directory */
mode = permchain_apply(settings.create_permchain, mode);
res = mkdir(real_path, mode & 0777);
if (res == -1) {
free(real_path);