From 24be89ebd8e9fbf97cb60c304b615ab26869c204 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 17 Apr 2023 05:19:07 +0900 Subject: [PATCH] chase: make the result absolute when a symlink is absolute As the path may be outside of the specified dir_fd. --- src/basic/chase.c | 5 +++++ src/test/test-chase.c | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/basic/chase.c b/src/basic/chase.c index a6a37978e8..2b8befc7f7 100644 --- a/src/basic/chase.c +++ b/src/basic/chase.c @@ -374,6 +374,11 @@ int chaseat(int dir_fd, const char *path, ChaseFlags flags, char **ret_path, int unsafe_transition(&st_child, &st)) return log_unsafe_transition(child, fd, path, flags); + /* When CHASE_AT_RESOLVE_IN_ROOT is not set, now the chased path may be + * outside of the specified dir_fd. Let's make the result absolute. */ + if (!FLAGS_SET(flags, CHASE_AT_RESOLVE_IN_ROOT)) + need_absolute = true; + r = free_and_strdup(&done, need_absolute ? "/" : NULL); if (r < 0) return r; diff --git a/src/test/test-chase.c b/src/test/test-chase.c index 52ea21a54c..c5fc08ca25 100644 --- a/src/test/test-chase.c +++ b/src/test/test-chase.c @@ -457,7 +457,16 @@ TEST(chaseat) { fd = safe_close(fd); - /* If the file descriptor does not point to the root directory, the result will be relative. */ + /* If the file descriptor does not point to the root directory, the result will be relative + * unless the result is outside of the specified file descriptor. */ + + assert_se(chaseat(tfd, "abc", 0, &result, NULL) >= 0); + assert_se(streq(result, "/usr")); + result = mfree(result); + + assert_se(chaseat(tfd, "/abc", 0, &result, NULL) >= 0); + assert_se(streq(result, "/usr")); + result = mfree(result); assert_se(chaseat(tfd, "abc", CHASE_AT_RESOLVE_IN_ROOT, NULL, NULL) == -ENOENT); assert_se(chaseat(tfd, "/abc", CHASE_AT_RESOLVE_IN_ROOT, NULL, NULL) == -ENOENT);