From 7b887f2284e2ada3e95494d5972d2f64f27fe81b Mon Sep 17 00:00:00 2001 From: Franck Bui Date: Thu, 12 Apr 2018 12:18:19 +0200 Subject: [PATCH 1/4] tmpfiles: remove spurious curly braces in create_item() --- src/tmpfiles/tmpfiles.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 9862e3402c..5755f22160 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -1451,8 +1451,7 @@ static int create_item(Item *i) { return r; break; - case COPY_FILES: { - + case COPY_FILES: RUN_WITH_UMASK(0000) (void) mkdir_parents_label(i->path, 0755); @@ -1625,7 +1624,6 @@ static int create_item(Item *i) { return r; break; - } case CREATE_SYMLINK: { RUN_WITH_UMASK(0000) From 4dfaa528d451aa7926be4f1b4cf8d0ffe338421d Mon Sep 17 00:00:00 2001 From: Franck Bui Date: Wed, 11 Apr 2018 16:58:49 +0200 Subject: [PATCH 2/4] fs-util: introduce fchmod_opath() fchmod(2) still doesn't take file descriptors opened with O_PATH. --- src/basic/fs-util.c | 15 +++++++++++++++ src/basic/fs-util.h | 1 + src/tmpfiles/tmpfiles.c | 10 ++-------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index 61aeb1fc8a..13dccfef54 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -241,6 +241,21 @@ int fchmod_umask(int fd, mode_t m) { return r; } +int fchmod_opath(int fd, mode_t m) { + char procfs_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; + + /* This function operates also on fd that might have been opened with + * O_PATH. Indeed fchmodat() doesn't have the AT_EMPTY_PATH flag like + * fchownat() does. */ + + xsprintf(procfs_path, "/proc/self/fd/%i", fd); + + if (chmod(procfs_path, m) < 0) + return -errno; + + return 0; +} + int fd_warn_permissions(const char *path, int fd) { struct stat st; diff --git a/src/basic/fs-util.h b/src/basic/fs-util.h index 2b1d2097fd..6157fe81bf 100644 --- a/src/basic/fs-util.h +++ b/src/basic/fs-util.h @@ -33,6 +33,7 @@ int readlink_and_make_absolute(const char *p, char **r); int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid); int fchmod_umask(int fd, mode_t mode); +int fchmod_opath(int fd, mode_t m); int fd_warn_permissions(const char *path, int fd); diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 5755f22160..4e437a57fd 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -808,15 +808,9 @@ static int fd_set_perms(Item *i, int fd, const struct stat *st) { if (m == (st->st_mode & 07777)) log_debug("\"%s\" has correct mode %o already.", path, st->st_mode); else { - char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; - log_debug("Changing \"%s\" to mode %o.", path, m); - - /* fchmodat() still doesn't have AT_EMPTY_PATH flag. */ - xsprintf(procfs_path, "/proc/self/fd/%i", fd); - - if (chmod(procfs_path, m) < 0) - return log_error_errno(errno, "chmod() of %s via %s failed: %m", path, procfs_path); + if (fchmod_opath(fd, m) < 0) + return log_error_errno(errno, "fchmod() of %s failed: %m", path); } } } From d460ba18e777128c19343b067574ecac399f3afc Mon Sep 17 00:00:00 2001 From: Franck Bui Date: Tue, 24 Apr 2018 12:23:29 +0200 Subject: [PATCH 3/4] tmpfiles: 'e' is supposed to operate on directory only --- src/tmpfiles/tmpfiles.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 4e437a57fd..d52af22d73 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -859,6 +859,9 @@ static int path_set_perms(Item *i, const char *path) { if (fstat(fd, &st) < 0) return log_error_errno(errno, "Failed to fstat() file %s: %m", path); + if (i->type == EMPTY_DIRECTORY && !S_ISDIR(st.st_mode)) + return log_error_errno(EEXIST, "'%s' already exists and is not a directory. ", path); + return fd_set_perms(i, fd, &st); } From 939ca2136363f50b5acdb9ca102b9c27389859f1 Mon Sep 17 00:00:00 2001 From: Franck Bui Date: Tue, 24 Apr 2018 12:24:38 +0200 Subject: [PATCH 4/4] tmpfiles: 'e' is supposed to accept shell-style globs --- src/tmpfiles/tmpfiles.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index d52af22d73..bc66299142 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -1570,7 +1570,7 @@ static int create_item(Item *i) { _fallthrough_; case EMPTY_DIRECTORY: - r = path_set_perms(i, i->path); + r = glob_item(i, path_set_perms); if (q < 0) return q; if (r < 0)