From d2bcd0ba7582552a3813b21f6c37926bc3ec1562 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 18 Apr 2018 16:19:46 +0200 Subject: [PATCH] path-lookup: properly chase paths when reducing with root dir (#8750) Let's make this correct. --- src/basic/fs-util.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/basic/fs-util.h | 1 + src/shared/path-lookup.c | 20 +++++++------------- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index 8ce4571d2b..c6708a4c58 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -951,6 +951,46 @@ int chase_symlinks_and_opendir( return 0; } +int chase_symlinks_and_stat( + const char *path, + const char *root, + unsigned chase_flags, + char **ret_path, + struct stat *ret_stat) { + + _cleanup_close_ int path_fd = -1; + _cleanup_free_ char *p = NULL; + + assert(path); + assert(ret_stat); + + if (chase_flags & CHASE_NONEXISTENT) + return -EINVAL; + + if (empty_or_root(root) && !ret_path && (chase_flags & (CHASE_NO_AUTOFS|CHASE_SAFE)) == 0) { + /* Shortcut this call if none of the special features of this call are requested */ + if (stat(path, ret_stat) < 0) + return -errno; + + return 1; + } + + path_fd = chase_symlinks(path, root, chase_flags|CHASE_OPEN, ret_path ? &p : NULL); + if (path_fd < 0) + return path_fd; + + if (fstat(path_fd, ret_stat) < 0) + return -errno; + + if (ret_path) + *ret_path = TAKE_PTR(p); + + if (chase_flags & CHASE_OPEN) + return TAKE_FD(path_fd); + + return 1; +} + int access_fd(int fd, int mode) { char p[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(fd) + 1]; int r; diff --git a/src/basic/fs-util.h b/src/basic/fs-util.h index d50ac7f972..4760961c0d 100644 --- a/src/basic/fs-util.h +++ b/src/basic/fs-util.h @@ -81,6 +81,7 @@ int chase_symlinks(const char *path_with_prefix, const char *root, unsigned flag int chase_symlinks_and_open(const char *path, const char *root, unsigned chase_flags, int open_flags, char **ret_path); int chase_symlinks_and_opendir(const char *path, const char *root, unsigned chase_flags, char **ret_path, DIR **ret_dir); +int chase_symlinks_and_stat(const char *path, const char *root, unsigned chase_flags, char **ret_path, struct stat *ret_stat); /* Useful for usage with _cleanup_(), removes a directory and frees the pointer */ static inline void rmdir_and_free(char *p) { diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c index 33e38f8a71..b816076713 100644 --- a/src/shared/path-lookup.c +++ b/src/shared/path-lookup.c @@ -723,14 +723,13 @@ int lookup_paths_reduce(LookupPaths *p) { assert(p); /* Drop duplicates and non-existing directories from the search path. We figure out whether two directories are - * the same by comparing their device and inode numbers. Note one special tweak: when we have a root path set, - * we do not follow symlinks when retrieving them, because the kernel wouldn't take the root prefix into - * account when following symlinks. When we have no root path set this restriction does not apply however. */ + * the same by comparing their device and inode numbers. */ if (!p->search_path) return 0; while (p->search_path[c]) { + _cleanup_free_ char *chased = NULL; struct stat st; unsigned k; @@ -742,25 +741,20 @@ int lookup_paths_reduce(LookupPaths *p) { continue; } - if (p->root_dir) - r = lstat(p->search_path[c], &st); - else - r = stat(p->search_path[c], &st); + r = chase_symlinks_and_stat(p->search_path[c], p->root_dir, 0, NULL, &st); + if (r == -ENOENT) + goto remove_item; if (r < 0) { - if (errno == ENOENT) - goto remove_item; - /* If something we don't grok happened, let's better leave it in. */ - log_debug_errno(errno, "Failed to stat %s: %m", p->search_path[c]); + log_debug_errno(r, "Failed to chase and stat %s: %m", p->search_path[c]); c++; continue; } - for (k = 0; k < n_stats; k++) { + for (k = 0; k < n_stats; k++) if (stats[k].st_dev == st.st_dev && stats[k].st_ino == st.st_ino) break; - } if (k < n_stats) /* Is there already an entry with the same device/inode? */ goto remove_item;