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
|
/* fuse-overlayfs: Overlay Filesystem in Userspace
Copyright (C) 2019 Red Hat Inc.
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 _FILE_OFFSET_BITS 64
#include <config.h>
#include "utils.h"
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/sysmacros.h>
#include <sys/syscall.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/xattr.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
#ifndef RESOLVE_IN_ROOT
# define RESOLVE_IN_ROOT 0x10
#endif
#ifndef __NR_openat2
# define __NR_openat2 437
#endif
/* uClibc and uClibc-ng don't provide O_TMPFILE */
#ifndef O_TMPFILE
# define O_TMPFILE (020000000 | O_DIRECTORY)
#endif
/* List of all valid flags for the open/openat flags argument: */
#define VALID_OPEN_FLAGS \
(O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | O_APPEND | O_NDELAY | O_NONBLOCK | O_NDELAY | O_SYNC | O_DSYNC | FASYNC | O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | O_NOATIME | O_CLOEXEC | O_PATH | O_TMPFILE)
static int
syscall_openat2 (int dirfd, const char *path, uint64_t flags, uint64_t mode, uint64_t resolve)
{
struct openat2_open_how
{
uint64_t flags;
uint64_t mode;
uint64_t resolve;
} how = {
.flags = flags & VALID_OPEN_FLAGS,
.mode = (flags & O_CREAT) ? (mode & 07777) : 0,
.resolve = resolve,
};
return (int) syscall (__NR_openat2, dirfd, path, &how, sizeof (how), 0);
}
int
safe_openat (int dirfd, const char *pathname, int flags, mode_t mode)
{
static bool openat2_supported = true;
if (openat2_supported)
{
int ret;
ret = syscall_openat2 (dirfd, pathname, flags, mode, RESOLVE_IN_ROOT);
if (ret < 0)
{
if (errno == ENOSYS)
openat2_supported = false;
if (errno == ENOSYS || errno == EINVAL)
goto fallback;
}
return ret;
}
fallback:
return openat (dirfd, pathname, flags, mode);
}
int
file_exists_at (int dirfd, const char *pathname)
{
int ret = faccessat (dirfd, pathname, F_OK, AT_SYMLINK_NOFOLLOW | AT_EACCESS);
if (ret < 0 && errno == EINVAL)
{
struct stat buf;
return fstatat (dirfd, pathname, &buf, AT_SYMLINK_NOFOLLOW);
}
return ret;
}
#ifdef HAVE_STATX
void
copy_statx_to_stat_time (struct statx_timestamp *stx, struct timespec *st)
{
st->tv_sec = stx->tv_sec;
st->tv_nsec = stx->tv_nsec;
}
void
statx_to_stat (struct statx *stx, struct stat *st)
{
st->st_dev = makedev (stx->stx_dev_major, stx->stx_dev_minor);
st->st_ino = stx->stx_ino;
st->st_mode = stx->stx_mode;
st->st_nlink = stx->stx_nlink;
st->st_uid = stx->stx_uid;
st->st_gid = stx->stx_gid;
st->st_rdev = makedev (stx->stx_rdev_major, stx->stx_rdev_minor);
st->st_size = stx->stx_size;
st->st_blksize = stx->stx_blksize;
st->st_blocks = stx->stx_blocks;
copy_statx_to_stat_time (&stx->stx_atime, &st->st_atim);
copy_statx_to_stat_time (&stx->stx_ctime, &st->st_ctim);
copy_statx_to_stat_time (&stx->stx_mtime, &st->st_mtim);
}
#endif
int
strconcat3 (char *dest, size_t size, const char *s1, const char *s2, const char *s3)
{
size_t t;
char *current = dest;
size--;
if (s1)
{
t = strlen (s1);
if (t > size)
t = size;
memcpy (current, s1, t);
current += t;
size -= t;
}
if (s2)
{
t = strlen (s2);
if (t > size)
t = size;
memcpy (current, s2, t);
current += t;
size -= t;
}
if (s3)
{
t = strlen (s3);
if (t > size)
t = size;
memcpy (current, s3, t);
current += t;
}
*current = '\0';
return current - dest;
}
void
cleanup_freep (void *p)
{
void **pp = (void **) p;
free (*pp);
}
void
cleanup_filep (FILE **f)
{
FILE *file = *f;
if (file)
(void) fclose (file);
}
void
cleanup_closep (void *p)
{
int *pp = p;
if (*pp >= 0)
TEMP_FAILURE_RETRY (close (*pp));
}
void
cleanup_dirp (DIR **p)
{
DIR *dir = *p;
if (dir)
closedir (dir);
}
int
open_fd_or_get_path (struct ovl_layer *l, const char *path, char *out, int *fd, int flags)
{
out[0] = '\0';
*fd = l->ds->openat (l, path, O_NONBLOCK | O_NOFOLLOW | flags, 0);
if (*fd < 0 && (errno == ELOOP || errno == EISDIR || errno == ENXIO))
{
strconcat3 (out, PATH_MAX, l->path, "/", path);
return 0;
}
return *fd;
}
int
override_mode (struct ovl_layer *l, int fd, const char *abs_path, const char *path, struct stat *st)
{
int ret;
uid_t uid;
gid_t gid;
mode_t mode;
char buf[64];
cleanup_close int cleanup_fd = -1;
const char *xattr_name;
switch (st->st_mode & S_IFMT)
{
case S_IFDIR:
case S_IFREG:
break;
default:
return 0;
}
switch (l->stat_override_mode)
{
case STAT_OVERRIDE_NONE:
return 0;
case STAT_OVERRIDE_USER:
xattr_name = XATTR_OVERRIDE_STAT;
break;
case STAT_OVERRIDE_PRIVILEGED:
xattr_name = XATTR_PRIVILEGED_OVERRIDE_STAT;
break;
case STAT_OVERRIDE_CONTAINERS:
xattr_name = XATTR_OVERRIDE_CONTAINERS_STAT;
break;
default:
errno = EINVAL;
return -1;
}
if (fd >= 0)
{
ret = fgetxattr (fd, xattr_name, buf, sizeof (buf) - 1);
}
else if (abs_path)
{
ret = lgetxattr (abs_path, xattr_name, buf, sizeof (buf) - 1);
}
else
{
char full_path[PATH_MAX];
full_path[0] = '\0';
ret = open_fd_or_get_path (l, path, full_path, &cleanup_fd, O_RDONLY);
if (ret < 0)
return ret;
fd = cleanup_fd;
if (fd >= 0)
ret = fgetxattr (fd, xattr_name, buf, sizeof (buf) - 1);
else
ret = lgetxattr (full_path, xattr_name, buf, sizeof (buf) - 1);
}
if (ret < 0)
return errno == ENODATA ? 0 : ret;
buf[ret] = '\0';
ret = sscanf (buf, "%d:%d:%o", &uid, &gid, &mode);
if (ret != 3)
{
errno = EINVAL;
return -1;
}
st->st_uid = uid;
st->st_gid = gid;
st->st_mode = (st->st_mode & S_IFMT) | mode;
return 0;
}
|