From 5c7fc2856a6186e197e43b644e956d3fcb1b2117 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Fri, 21 Aug 2020 16:52:26 +0200 Subject: [PATCH] direct: move override_mode to utils Signed-off-by: Giuseppe Scrivano --- direct.c | 61 -------------------------------------------------------- utils.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 5 +++++ 3 files changed, 63 insertions(+), 61 deletions(-) diff --git a/direct.c b/direct.c index 89569a2..2fc2efc 100644 --- a/direct.c +++ b/direct.c @@ -34,9 +34,6 @@ #include "utils.h" -#define XATTR_OVERRIDE_STAT "user.fuseoverlayfs.override_stat" -#define XATTR_PRIVILEGED_OVERRIDE_STAT "security.fuseoverlayfs.override_stat" - static int direct_file_exists (struct ovl_layer *l, const char *pathname) { @@ -79,64 +76,6 @@ direct_getxattr (struct ovl_layer *l, const char *path, const char *name, char * return lgetxattr (full_path, name, buf, size); } -static int -override_mode (struct ovl_layer *l, int fd, 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; - - if (l->has_stat_override == 0 && l->has_privileged_stat_override == 0) - return 0; - - xattr_name = l->has_privileged_stat_override ? XATTR_PRIVILEGED_OVERRIDE_STAT : XATTR_OVERRIDE_STAT; - - if (fd >= 0) - { - ret = fgetxattr (fd, xattr_name, buf, sizeof (buf) - 1); - if (ret < 0) - return ret; - } - 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 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; -} - - static int direct_fstat (struct ovl_layer *l, int fd, const char *path, unsigned int mask, struct stat *st) { diff --git a/utils.c b/utils.c index 3d0e0aa..2aa7560 100644 --- a/utils.c +++ b/utils.c @@ -29,6 +29,7 @@ #include #include #include +#include #ifndef TEMP_FAILURE_RETRY #define TEMP_FAILURE_RETRY(expression) \ @@ -222,3 +223,60 @@ open_fd_or_get_path (struct ovl_layer *l, const char *path, char *out, int *fd, return *fd; } + +int +override_mode (struct ovl_layer *l, int fd, 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; + + if (l->has_stat_override == 0 && l->has_privileged_stat_override == 0) + return 0; + + xattr_name = l->has_privileged_stat_override ? XATTR_PRIVILEGED_OVERRIDE_STAT : XATTR_OVERRIDE_STAT; + + if (fd >= 0) + { + ret = fgetxattr (fd, xattr_name, buf, sizeof (buf) - 1); + if (ret < 0) + return ret; + } + 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 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; +} diff --git a/utils.h b/utils.h index b447df9..0d4a54e 100644 --- a/utils.h +++ b/utils.h @@ -31,6 +31,9 @@ # include # include "fuse-overlayfs.h" +# define XATTR_OVERRIDE_STAT "user.fuseoverlayfs.override_stat" +# define XATTR_PRIVILEGED_OVERRIDE_STAT "security.fuseoverlayfs.override_stat" + void cleanup_freep (void *p); void cleanup_filep (FILE **f); void cleanup_closep (void *p); @@ -55,4 +58,6 @@ void statx_to_stat (struct statx *stx, struct stat *st); int safe_openat (int dirfd, const char *pathname, int flags, mode_t mode); +int override_mode (struct ovl_layer *l, int fd, const char *path, struct stat *st); + #endif