-
Notifications
You must be signed in to change notification settings - Fork 85
/
main.c
6013 lines (5051 loc) · 143 KB
/
main.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
/* fuse-overlayfs: Overlay Filesystem in Userspace
Copyright (C) 2018 Giuseppe Scrivano <[email protected]>
Copyright (C) 2018-2020 Red Hat Inc.
Copyright (C) 2001-2007 Miklos Szeredi <[email protected]>
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#define FUSE_USE_VERSION 32
#define _FILE_OFFSET_BITS 64
#include <config.h>
#include "fuse-overlayfs.h"
#include <fuse_lowlevel.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>
#include <dirent.h>
#include <assert.h>
#include <errno.h>
#include <linux/magic.h>
#include <err.h>
#include <sys/vfs.h>
#include <sys/ioctl.h>
#ifdef HAVE_SYS_SENDFILE_H
# include <sys/sendfile.h>
#endif
#include <fuse_overlayfs_error.h>
#include <inttypes.h>
#include <fcntl.h>
#include <hash.h>
#include <sys/statvfs.h>
#include <sys/file.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <sys/xattr.h>
#include <linux/fs.h>
#include <linux/xattr.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <pthread.h>
#include <utils.h>
#include <plugin.h>
#ifndef TEMP_FAILURE_RETRY
# define TEMP_FAILURE_RETRY(expression) \
(__extension__ ({ long int __result; \
do __result = (long int) (expression); \
while (__result == -1L && errno == EINTR); \
__result; }))
#endif
static bool disable_locking;
static pthread_mutex_t lock;
static int
enter_big_lock ()
{
if (disable_locking)
return 0;
pthread_mutex_lock (&lock);
return 1;
}
static int
release_big_lock ()
{
if (disable_locking)
return 0;
pthread_mutex_unlock (&lock);
return 0;
}
static inline void
cleanup_lockp (int *l)
{
if (*l == 0)
return;
pthread_mutex_unlock (&lock);
*l = 0;
}
#define cleanup_lock __attribute__ ((cleanup (cleanup_lockp)))
#ifndef HAVE_OPEN_BY_HANDLE_AT
struct file_handle
{
unsigned int handle_bytes; /* Size of f_handle [in, out] */
int handle_type; /* Handle type [out] */
unsigned char f_handle[0]; /* File identifier (sized by
caller) [out] */
};
int
open_by_handle_at (int mount_fd, struct file_handle *handle, int flags)
{
return syscall (SYS_open_by_handle_at, mount_fd, handle, flags);
}
#endif
#ifndef RENAME_NOREPLACE
# define RENAME_NOREPLACE (1 << 0)
#endif
#ifndef RENAME_EXCHANGE
# define RENAME_EXCHANGE (1 << 1)
#endif
#ifndef RENAME_WHITEOUT
# define RENAME_WHITEOUT (1 << 2)
#endif
#define XATTR_PREFIX "user.fuseoverlayfs."
#define ORIGIN_XATTR "user.fuseoverlayfs.origin"
#define OPAQUE_XATTR "user.fuseoverlayfs.opaque"
#define XATTR_CONTAINERS_PREFIX "user.containers."
#define XATTR_CONTAINERS_OVERRIDE_PREFIX "user.containers.override_"
#define UNPRIVILEGED_XATTR_PREFIX "user.overlay."
#define UNPRIVILEGED_OPAQUE_XATTR "user.overlay.opaque"
#define PRIVILEGED_XATTR_PREFIX "trusted.overlay."
#define PRIVILEGED_OPAQUE_XATTR "trusted.overlay.opaque"
#define PRIVILEGED_ORIGIN_XATTR "trusted.overlay.origin"
#define OPAQUE_WHITEOUT ".wh..wh..opq"
#define WHITEOUT_MAX_LEN (sizeof (".wh.") - 1)
#if ! defined FICLONE && defined __linux__
# define FICLONE _IOW (0x94, 9, int)
#endif
#if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 6) && ! defined __cplusplus
_Static_assert (sizeof (fuse_ino_t) >= sizeof (uintptr_t),
"fuse_ino_t too small to hold uintptr_t values!");
#else
struct _uintptr_to_must_hold_fuse_ino_t_dummy_struct
{
unsigned _uintptr_to_must_hold_fuse_ino_t : ((sizeof (fuse_ino_t) >= sizeof (uintptr_t)) ? 1 : -1);
};
#endif
static uid_t overflow_uid;
static gid_t overflow_gid;
static struct ovl_ino dummy_ino;
struct stats_s
{
size_t nodes;
size_t inodes;
};
static volatile struct stats_s stats;
static void
print_stats (int sig)
{
char fmt[128];
int l = snprintf (fmt, sizeof (fmt) - 1, "# INODES: %zu\n# NODES: %zu\n", stats.inodes, stats.nodes);
fmt[l] = '\0';
(void) write (STDERR_FILENO, fmt, l + 1);
}
static double
get_timeout (struct ovl_data *lo)
{
return lo->timeout;
}
static const struct fuse_opt ovl_opts[] = {
{ "redirect_dir=%s",
offsetof (struct ovl_data, redirect_dir), 0 },
{ "context=%s",
offsetof (struct ovl_data, context), 0 },
{ "lowerdir=%s",
offsetof (struct ovl_data, lowerdir), 0 },
{ "upperdir=%s",
offsetof (struct ovl_data, upperdir), 0 },
{ "workdir=%s",
offsetof (struct ovl_data, workdir), 0 },
{ "uidmapping=%s",
offsetof (struct ovl_data, uid_str), 0 },
{ "gidmapping=%s",
offsetof (struct ovl_data, gid_str), 0 },
{ "timeout=%s",
offsetof (struct ovl_data, timeout_str), 0 },
{ "threaded=%d",
offsetof (struct ovl_data, threaded), 0 },
{ "fsync=%d",
offsetof (struct ovl_data, fsync), 1 },
{ "fast_ino=%d",
offsetof (struct ovl_data, fast_ino_check), 0 },
{ "writeback=%d",
offsetof (struct ovl_data, writeback), 1 },
{ "noxattrs=%d",
offsetof (struct ovl_data, disable_xattrs), 1 },
{ "plugins=%s",
offsetof (struct ovl_data, plugins), 0 },
{ "xattr_permissions=%d",
offsetof (struct ovl_data, xattr_permissions), 0 },
{ "squash_to_root",
offsetof (struct ovl_data, squash_to_root), 1 },
{ "squash_to_uid=%d",
offsetof (struct ovl_data, squash_to_uid), 1 },
{ "squash_to_gid=%d",
offsetof (struct ovl_data, squash_to_gid), 1 },
{ "static_nlink",
offsetof (struct ovl_data, static_nlink), 1 },
{ "volatile", /* native overlay supports "volatile" to mean fsync=0. */
offsetof (struct ovl_data, volatile_mode), 1 },
{ "noacl",
offsetof (struct ovl_data, noacl), 1 },
FUSE_OPT_END
};
/* The current process has enough privileges to use mknod. */
static bool can_mknod = true;
/* Kernel definitions. */
typedef unsigned char u8;
typedef unsigned char uuid_t[16];
/* The type returned by overlay exportfs ops when encoding an ovl_fh handle */
#define OVL_FILEID 0xfb
/* On-disk and in-memory format for redirect by file handle */
struct ovl_fh
{
u8 version; /* 0 */
u8 magic; /* 0xfb */
u8 len; /* size of this header + size of fid */
u8 flags; /* OVL_FH_FLAG_* */
u8 type; /* fid_type of fid */
uuid_t uuid; /* uuid of filesystem */
u8 fid[0]; /* file identifier */
} __packed;
static struct ovl_data *
ovl_data (fuse_req_t req)
{
return (struct ovl_data *) fuse_req_userdata (req);
}
static unsigned long
get_next_wd_counter ()
{
static unsigned long counter = 1;
return counter++;
}
static ino_t
node_to_inode (struct ovl_node *n)
{
return (ino_t) n->ino;
}
static struct ovl_ino *
lookup_inode (struct ovl_data *lo, ino_t n)
{
return (struct ovl_ino *) n;
}
static struct ovl_node *
inode_to_node (struct ovl_data *lo, ino_t n)
{
return lookup_inode (lo, n)->node;
}
static void
check_writeable_proc ()
{
struct statfs svfs;
int ret;
ret = statfs ("/proc", &svfs);
if (ret < 0)
{
fprintf (stderr, "error stating /proc: %m\n");
return;
}
if (svfs.f_type != PROC_SUPER_MAGIC)
{
fprintf (stderr, "invalid file system type found on /proc: %d, expected %d\n", svfs.f_fsid, PROC_SUPER_MAGIC);
return;
}
if (svfs.f_flags & ST_RDONLY)
{
fprintf (stderr, "/proc seems to be mounted as readonly, it can lead to unexpected failures");
return;
}
}
static void
check_can_mknod (struct ovl_data *lo)
{
int ret;
char path[PATH_MAX];
if (getenv ("FUSE_OVERLAYFS_DISABLE_OVL_WHITEOUT"))
{
can_mknod = false;
return;
}
sprintf (path, "%lu", get_next_wd_counter ());
ret = mknodat (lo->workdir_fd, path, S_IFCHR | 0700, makedev (0, 0));
if (ret == 0)
unlinkat (lo->workdir_fd, path, 0);
if (ret < 0 && errno == EPERM)
can_mknod = false;
}
static struct ovl_mapping *
read_mappings (const char *str)
{
char *buf = NULL, *saveptr = NULL, *it, *endptr;
struct ovl_mapping *tmp, *ret = NULL;
unsigned int a, b, c;
int state = 0;
buf = alloca (strlen (str) + 1);
strcpy (buf, str);
for (it = strtok_r (buf, ":", &saveptr); it; it = strtok_r (NULL, ":", &saveptr))
{
switch (state)
{
case 0:
a = strtol (it, &endptr, 10);
if (*endptr != 0)
error (EXIT_FAILURE, 0, "invalid mapping specified: %s", str);
state++;
break;
case 1:
b = strtol (it, &endptr, 10);
if (*endptr != 0)
error (EXIT_FAILURE, 0, "invalid mapping specified: %s", str);
state++;
break;
case 2:
c = strtol (it, &endptr, 10);
if (*endptr != 0)
error (EXIT_FAILURE, 0, "invalid mapping specified: %s", str);
state = 0;
tmp = malloc (sizeof (*tmp));
if (tmp == NULL)
return NULL;
tmp->next = ret;
tmp->host = a;
tmp->to = b;
tmp->len = c;
ret = tmp;
break;
}
}
if (state != 0)
error (EXIT_FAILURE, 0, "invalid mapping specified: %s", str);
return ret;
}
static void
free_mapping (struct ovl_mapping *it)
{
struct ovl_mapping *next = NULL;
for (; it; it = next)
{
next = it->next;
free (it);
}
}
/* Useful in a gdb session. */
void
dump_directory (struct ovl_node *node)
{
struct ovl_node *it;
if (node->children == NULL)
return;
for (it = hash_get_first (node->children); it; it = hash_get_next (node->children, it))
printf ("ENTRY: %s (%s)\n", it->name, it->path);
}
static long int
read_file_as_int (const char *file)
{
cleanup_close int fd = -1;
long int ret;
char buffer[256];
int r;
fd = open (file, O_RDONLY);
if (fd < 0)
error (EXIT_FAILURE, errno, "can't open %s", file);
r = read (fd, buffer, sizeof (buffer) - 1);
if (r < 0)
error (EXIT_FAILURE, errno, "can't read from %s", file);
buffer[r] = '\0';
ret = strtol (buffer, NULL, 10);
if (ret == 0)
error (EXIT_FAILURE, errno, "can't parse %s", file);
return ret;
}
static void
read_overflowids (void)
{
overflow_uid = read_file_as_int ("/proc/sys/kernel/overflowuid");
overflow_gid = read_file_as_int ("/proc/sys/kernel/overflowgid");
}
static bool
ovl_debug (fuse_req_t req)
{
return ovl_data (req)->debug != 0;
}
static void
ovl_init (void *userdata, struct fuse_conn_info *conn)
{
struct ovl_data *lo = (struct ovl_data *) userdata;
if ((conn->capable & FUSE_CAP_WRITEBACK_CACHE) == 0)
lo->writeback = 0;
if ((lo->noacl == 0) && (conn->capable & FUSE_CAP_POSIX_ACL))
conn->want |= FUSE_CAP_POSIX_ACL;
conn->want |= FUSE_CAP_DONT_MASK | FUSE_CAP_SPLICE_READ | FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
if (lo->writeback)
conn->want |= FUSE_CAP_WRITEBACK_CACHE;
}
static struct ovl_layer *
get_first_layer (struct ovl_data *lo)
{
return lo->layers;
}
static struct ovl_layer *
get_upper_layer (struct ovl_data *lo)
{
if (lo->upperdir == NULL)
return NULL;
return lo->layers;
}
static struct ovl_layer *
get_lower_layers (struct ovl_data *lo)
{
if (lo->upperdir == NULL)
return lo->layers;
return lo->layers->next;
}
static inline bool
node_dirp (struct ovl_node *n)
{
return n->children != NULL;
}
static int
node_dirfd (struct ovl_node *n)
{
if (n->hidden)
return n->hidden_dirfd;
return n->layer->fd;
}
static bool
has_prefix (const char *str, const char *pref)
{
while (1)
{
if (*pref == '\0')
return true;
if (*str == '\0')
return false;
if (*pref != *str)
return false;
str++;
pref++;
}
return false;
}
static bool
can_access_xattr (const struct ovl_layer *l, const char *name)
{
return ! (has_prefix (name, XATTR_PREFIX)
|| has_prefix (name, PRIVILEGED_XATTR_PREFIX)
|| has_prefix (name, UNPRIVILEGED_XATTR_PREFIX)
|| (l->stat_override_mode == STAT_OVERRIDE_CONTAINERS &&
has_prefix (name, XATTR_SECURITY_PREFIX)));
}
static bool encoded_xattr_name (const struct ovl_layer *l, const char *name)
{
return has_prefix (name, XATTR_CONTAINERS_OVERRIDE_PREFIX) &&
! can_access_xattr (l, name + sizeof(XATTR_CONTAINERS_OVERRIDE_PREFIX) - 1);
}
static const char *decode_xattr_name (const struct ovl_layer *l, const char *name)
{
if (encoded_xattr_name (l, name))
return name + sizeof(XATTR_CONTAINERS_OVERRIDE_PREFIX) - 1;
if (can_access_xattr (l, name))
return name;
return NULL;
}
static const char *encode_xattr_name (const struct ovl_layer *l, char *buf,
const char *name)
{
if (can_access_xattr (l, name))
return name;
if (l->stat_override_mode != STAT_OVERRIDE_CONTAINERS ||
strlen(name) > XATTR_NAME_MAX + 1 - sizeof(XATTR_CONTAINERS_OVERRIDE_PREFIX))
return NULL;
strcpy(buf, XATTR_CONTAINERS_OVERRIDE_PREFIX);
strcpy(buf + sizeof(XATTR_CONTAINERS_OVERRIDE_PREFIX) - 1, name);
return buf;
}
static ssize_t
write_permission_xattr (struct ovl_data *lo, int fd, const char *path, uid_t uid, gid_t gid, mode_t mode)
{
char buf[64];
size_t len;
int ret;
const char *name = NULL;
switch (get_upper_layer (lo)->stat_override_mode)
{
case STAT_OVERRIDE_NONE:
return 0;
case STAT_OVERRIDE_USER:
name = XATTR_OVERRIDE_STAT;
break;
case STAT_OVERRIDE_PRIVILEGED:
name = XATTR_PRIVILEGED_OVERRIDE_STAT;
break;
case STAT_OVERRIDE_CONTAINERS:
name = XATTR_OVERRIDE_CONTAINERS_STAT;
break;
default:
errno = EINVAL;
return -1;
}
if (path == NULL && fd < 0)
{
errno = EINVAL;
return -1;
}
len = sprintf (buf, "%d:%d:%o", uid, gid, mode);
if (fd >= 0)
return fsetxattr (fd, name, buf, len, 0);
ret = lsetxattr (path, name, buf, len, 0);
/* Ignore EPERM in unprivileged mode. */
if (ret < 0 && lo->xattr_permissions == 2 && errno == EPERM)
return 0;
return ret;
}
static int
do_fchown (struct ovl_data *lo, int fd, uid_t uid, gid_t gid, mode_t mode)
{
int ret;
if (lo->xattr_permissions)
ret = write_permission_xattr (lo, fd, NULL, uid, gid, mode);
else
ret = fchown (fd, uid, gid);
return (lo->squash_to_root || lo->squash_to_uid != -1 || lo->squash_to_gid != -1) ? 0 : ret;
}
/* Make sure it is not used anymore. */
#define fchown ERROR
static int
do_chown (struct ovl_data *lo, const char *path, uid_t uid, gid_t gid, mode_t mode)
{
int ret;
if (lo->xattr_permissions)
ret = write_permission_xattr (lo, -1, path, uid, gid, mode);
else
ret = chown (path, uid, gid);
return (lo->squash_to_root || lo->squash_to_uid != -1 || lo->squash_to_gid != -1) ? 0 : ret;
}
/* Make sure it is not used anymore. */
#define chown ERROR
static int
do_fchownat (struct ovl_data *lo, int dfd, const char *path, uid_t uid, gid_t gid, mode_t mode, int flags)
{
int ret;
if (lo->xattr_permissions)
{
char proc_path[32];
cleanup_close int fd = openat (dfd, path, O_NOFOLLOW | O_PATH);
if (fd < 0)
return fd;
sprintf (proc_path, "/proc/self/fd/%d", fd);
ret = write_permission_xattr (lo, -1, proc_path, uid, gid, mode);
}
else
ret = fchownat (dfd, path, uid, gid, flags);
return (lo->squash_to_root || lo->squash_to_uid != -1 || lo->squash_to_gid != -1) ? 0 : ret;
}
/* Make sure it is not used anymore. */
#define fchownat ERROR
static int
do_stat (struct ovl_node *node, int fd, const char *path, struct stat *st)
{
struct ovl_layer *l = node->layer;
if (fd >= 0)
return l->ds->fstat (l, fd, path, STATX_BASIC_STATS, st);
if (path != NULL)
return stat (path, st);
if (node->hidden)
return fstatat (node_dirfd (node), node->path, st, AT_SYMLINK_NOFOLLOW);
return l->ds->statat (l, node->path, st, AT_SYMLINK_NOFOLLOW, STATX_BASIC_STATS);
}
static int
do_fchmod (struct ovl_data *lo, struct ovl_node *node, int fd, mode_t mode)
{
if (lo->xattr_permissions)
{
struct stat st;
st.st_uid = 0;
st.st_gid = 0;
if (do_stat (node, fd, NULL, &st) < 0)
return -1;
return write_permission_xattr (lo, fd, NULL, st.st_uid, st.st_gid, mode);
}
return fchmod (fd, mode);
}
/* Make sure it is not used anymore. */
#define fchmod ERROR
static int
do_chmod (struct ovl_data *lo, struct ovl_node *node, const char *path, mode_t mode)
{
if (lo->xattr_permissions)
{
struct stat st;
st.st_uid = 0;
st.st_gid = 0;
if (do_stat (node, -1, path, &st) < 0)
return -1;
return write_permission_xattr (lo, -1, path, st.st_uid, st.st_gid, mode);
}
return chmod (path, mode);
}
/* Make sure it is not used anymore. */
#define chmod ERROR
static int
set_fd_origin (int fd, const char *origin)
{
cleanup_close int opq_whiteout_fd = -1;
size_t len = strlen (origin) + 1;
int ret;
ret = fsetxattr (fd, ORIGIN_XATTR, origin, len, 0);
if (ret < 0)
{
if (errno == ENOTSUP)
return 0;
}
return ret;
}
static int
set_fd_opaque (int fd)
{
cleanup_close int opq_whiteout_fd = -1;
int ret;
ret = fsetxattr (fd, PRIVILEGED_OPAQUE_XATTR, "y", 1, 0);
if (ret < 0)
{
if (errno == ENOTSUP)
goto create_opq_whiteout;
if (errno != EPERM || (fsetxattr (fd, OPAQUE_XATTR, "y", 1, 0) < 0 && errno != ENOTSUP))
return -1;
}
create_opq_whiteout:
opq_whiteout_fd = TEMP_FAILURE_RETRY (safe_openat (fd, OPAQUE_WHITEOUT, O_CREAT | O_WRONLY | O_NONBLOCK, 0700));
return (opq_whiteout_fd >= 0 || ret == 0) ? 0 : -1;
}
static int
is_directory_opaque (struct ovl_layer *l, const char *path)
{
char b[16];
ssize_t s;
s = l->ds->getxattr (l, path, PRIVILEGED_OPAQUE_XATTR, b, sizeof (b));
if (s < 0 && errno == ENODATA)
s = l->ds->getxattr (l, path, UNPRIVILEGED_OPAQUE_XATTR, b, sizeof (b));
if (s < 0 && errno == ENODATA)
s = l->ds->getxattr (l, path, OPAQUE_XATTR, b, sizeof (b));
if (s < 0)
{
if (errno == ENOTSUP || errno == ENODATA)
{
char whiteout_opq_path[PATH_MAX];
strconcat3 (whiteout_opq_path, PATH_MAX, path, "/" OPAQUE_WHITEOUT, NULL);
if (l->ds->file_exists (l, whiteout_opq_path) == 0)
return 1;
return (errno == ENOENT) ? 0 : -1;
}
return -1;
}
return b[0] == 'y' ? 1 : 0;
}
static int
create_whiteout (struct ovl_data *lo, struct ovl_node *parent, const char *name, bool skip_mknod, bool force_create)
{
char whiteout_wh_path[PATH_MAX];
cleanup_close int fd = -1;
int ret;
if (! force_create)
{
char path[PATH_MAX];
struct ovl_layer *l;
bool found = false;
strconcat3 (path, PATH_MAX, parent->path, "/", name);
for (l = get_lower_layers (lo); l; l = l->next)
{
ret = l->ds->file_exists (l, path);
if (ret < 0 && errno == ENOENT)
continue;
found = true;
break;
}
/* Not present in the lower layers, do not do anything. */
if (! found)
return 0;
}
if (! skip_mknod && can_mknod)
{
char whiteout_path[PATH_MAX];
strconcat3 (whiteout_path, PATH_MAX, parent->path, "/", name);
ret = mknodat (get_upper_layer (lo)->fd, whiteout_path, S_IFCHR | 0700, makedev (0, 0));
if (ret == 0)
return 0;
if (errno == EEXIST)
{
int saved_errno = errno;
struct stat st;
/* Check whether it is already a whiteout. */
if (TEMP_FAILURE_RETRY (fstatat (get_upper_layer (lo)->fd, whiteout_path, &st, AT_SYMLINK_NOFOLLOW)) == 0
&& (st.st_mode & S_IFMT) == S_IFCHR
&& major (st.st_rdev) == 0
&& minor (st.st_rdev) == 0)
return 0;
errno = saved_errno;
}
if (errno != EPERM && errno != ENOTSUP)
return -1;
/* if it fails with EPERM then do not attempt mknod again. */
can_mknod = false;
}
strconcat3 (whiteout_wh_path, PATH_MAX, parent->path, "/.wh.", name);
fd = get_upper_layer (lo)->ds->openat (get_upper_layer (lo), whiteout_wh_path, O_CREAT | O_WRONLY | O_NONBLOCK, 0700);
if (fd < 0 && errno != EEXIST)
return -1;
return 0;
}
static int
delete_whiteout (struct ovl_data *lo, int dirfd, struct ovl_node *parent, const char *name)
{
struct stat st;
if (can_mknod)
{
if (dirfd >= 0)
{
if (TEMP_FAILURE_RETRY (fstatat (dirfd, name, &st, AT_SYMLINK_NOFOLLOW)) == 0
&& (st.st_mode & S_IFMT) == S_IFCHR
&& major (st.st_rdev) == 0
&& minor (st.st_rdev) == 0)
{
if (unlinkat (dirfd, name, 0) < 0)
return -1;
}
}
else
{
char whiteout_path[PATH_MAX];
strconcat3 (whiteout_path, PATH_MAX, parent->path, "/", name);
if (get_upper_layer (lo)->ds->statat (get_upper_layer (lo), whiteout_path, &st, AT_SYMLINK_NOFOLLOW, STATX_MODE | STATX_TYPE) == 0
&& (st.st_mode & S_IFMT) == S_IFCHR
&& major (st.st_rdev) == 0
&& minor (st.st_rdev) == 0)
{
if (unlinkat (get_upper_layer (lo)->fd, whiteout_path, 0) < 0)
return -1;
}
}
}
/* Look for the .wh. alternative as well. */
if (dirfd >= 0)
{
char whiteout_path[PATH_MAX];
strconcat3 (whiteout_path, PATH_MAX, ".wh.", name, NULL);
if (unlinkat (dirfd, whiteout_path, 0) < 0 && errno != ENOENT)
return -1;
}
else
{
char whiteout_path[PATH_MAX];
strconcat3 (whiteout_path, PATH_MAX, parent->path, "/.wh.", name);
if (unlinkat (get_upper_layer (lo)->fd, whiteout_path, 0) < 0 && errno != ENOENT)
return -1;
}
return 0;
}
static unsigned int
find_mapping (unsigned int id, const struct ovl_data *data,
bool direct, bool uid)
{
const struct ovl_mapping *mapping = (uid ? data->uid_mappings
: data->gid_mappings);
if (direct && uid && data->squash_to_uid != -1)
return data->squash_to_uid;
if (direct && ! uid && data->squash_to_gid != -1)
return data->squash_to_gid;
if (direct && data->squash_to_root)
return 0;
if (mapping == NULL)
return id;
for (; mapping; mapping = mapping->next)
{
if (direct)
{
if (id >= mapping->host && id < mapping->host + mapping->len)
return mapping->to + (id - mapping->host);
}
else
{
if (id >= mapping->to && id < mapping->to + mapping->len)
return mapping->host + (id - mapping->to);
}
}
return uid ? overflow_uid : overflow_gid;
}
static uid_t
get_uid (struct ovl_data *data, uid_t id)
{
return find_mapping (id, data, false, true);
}
static uid_t
get_gid (struct ovl_data *data, gid_t id)
{
return find_mapping (id, data, false, false);
}
static int
rpl_stat (fuse_req_t req, struct ovl_node *node, int fd, const char *path, struct stat *st_in, struct stat *st)
{
int ret = 0;
struct ovl_layer *l = node->layer;
struct ovl_data *data = ovl_data (req);
if (st_in)
memcpy (st, st_in, sizeof (*st));
else
ret = do_stat (node, fd, path, st);
if (ret < 0)
return ret;
st->st_uid = find_mapping (st->st_uid, data, true, true);
st->st_gid = find_mapping (st->st_gid, data, true, false);
st->st_ino = node->tmp_ino;
st->st_dev = node->tmp_dev;
if (node->loaded && node->n_links > 0)
st->st_nlink = node->n_links;
else if (node_dirp (node))
{
if (data->static_nlink)
st->st_nlink = 1;
else
{
struct ovl_node *it;
st->st_nlink = 2;
for (it = hash_get_first (node->children); it; it = hash_get_next (node->children, it))
{
if (node_dirp (it))
st->st_nlink++;
}
}
node->n_links = st->st_nlink;
}