|
|
923a60 |
From a221a65ad0563d1bfe8770e928b221efc6ba8c88 Mon Sep 17 00:00:00 2001
|
|
|
923a60 |
From: David Tardon <dtardon@redhat.com>
|
|
|
923a60 |
Date: Thu, 3 Jan 2019 13:09:43 +0100
|
|
|
923a60 |
Subject: [PATCH] backport chase_symlinks
|
|
|
923a60 |
|
|
|
923a60 |
Related: #1663143
|
|
|
923a60 |
---
|
|
|
923a60 |
src/shared/util.c | 233 +++++++++++++++++++++++++++++++++++++++++++
|
|
|
923a60 |
src/shared/util.h | 8 ++
|
|
|
923a60 |
src/test/test-util.c | 208 ++++++++++++++++++++++++++++++++++++++
|
|
|
923a60 |
3 files changed, 449 insertions(+)
|
|
|
923a60 |
|
|
|
923a60 |
diff --git a/src/shared/util.c b/src/shared/util.c
|
|
|
923a60 |
index 2838d50f6f..385551f2b3 100644
|
|
|
923a60 |
--- a/src/shared/util.c
|
|
|
923a60 |
+++ b/src/shared/util.c
|
|
|
923a60 |
@@ -9202,3 +9202,236 @@ int fd_is_fs_type(int fd, statfs_f_type_t magic_value) {
|
|
|
923a60 |
|
|
|
923a60 |
return is_fs_type(&s, magic_value);
|
|
|
923a60 |
}
|
|
|
923a60 |
+
|
|
|
923a60 |
+int chase_symlinks(const char *path, const char *original_root, unsigned flags, char **ret) {
|
|
|
923a60 |
+ _cleanup_free_ char *buffer = NULL, *done = NULL, *root = NULL;
|
|
|
923a60 |
+ _cleanup_close_ int fd = -1;
|
|
|
923a60 |
+ unsigned max_follow = 32; /* how many symlinks to follow before giving up and returning ELOOP */
|
|
|
923a60 |
+ bool exists = true;
|
|
|
923a60 |
+ char *todo;
|
|
|
923a60 |
+ int r;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ assert(path);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* This is a lot like canonicalize_file_name(), but takes an additional "root" parameter, that allows following
|
|
|
923a60 |
+ * symlinks relative to a root directory, instead of the root of the host.
|
|
|
923a60 |
+ *
|
|
|
923a60 |
+ * Note that "root" primarily matters if we encounter an absolute symlink. It is also used when following
|
|
|
923a60 |
+ * relative symlinks to ensure they cannot be used to "escape" the root directory. The path parameter passed is
|
|
|
923a60 |
+ * assumed to be already prefixed by it, except if the CHASE_PREFIX_ROOT flag is set, in which case it is first
|
|
|
923a60 |
+ * prefixed accordingly.
|
|
|
923a60 |
+ *
|
|
|
923a60 |
+ * Algorithmically this operates on two path buffers: "done" are the components of the path we already
|
|
|
923a60 |
+ * processed and resolved symlinks, "." and ".." of. "todo" are the components of the path we still need to
|
|
|
923a60 |
+ * process. On each iteration, we move one component from "todo" to "done", processing it's special meaning
|
|
|
923a60 |
+ * each time. The "todo" path always starts with at least one slash, the "done" path always ends in no
|
|
|
923a60 |
+ * slash. We always keep an O_PATH fd to the component we are currently processing, thus keeping lookup races
|
|
|
923a60 |
+ * at a minimum.
|
|
|
923a60 |
+ *
|
|
|
923a60 |
+ * Suggested usage: whenever you want to canonicalize a path, use this function. Pass the absolute path you got
|
|
|
923a60 |
+ * as-is: fully qualified and relative to your host's root. Optionally, specify the root parameter to tell this
|
|
|
923a60 |
+ * function what to do when encountering a symlink with an absolute path as directory: prefix it by the
|
|
|
923a60 |
+ * specified path. */
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (original_root) {
|
|
|
923a60 |
+ root = path_make_absolute_cwd(original_root);
|
|
|
923a60 |
+ if (root == NULL)
|
|
|
923a60 |
+ return -ENOENT;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (flags & CHASE_PREFIX_ROOT)
|
|
|
923a60 |
+ path = prefix_roota(root, path);
|
|
|
923a60 |
+ }
|
|
|
923a60 |
+
|
|
|
923a60 |
+ buffer = path_make_absolute_cwd(path);
|
|
|
923a60 |
+ if (buffer == NULL)
|
|
|
923a60 |
+ return -ENOENT;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ fd = open("/", O_CLOEXEC|O_NOFOLLOW|O_PATH);
|
|
|
923a60 |
+ if (fd < 0)
|
|
|
923a60 |
+ return -errno;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ todo = buffer;
|
|
|
923a60 |
+ for (;;) {
|
|
|
923a60 |
+ _cleanup_free_ char *first = NULL;
|
|
|
923a60 |
+ _cleanup_close_ int child = -1;
|
|
|
923a60 |
+ struct stat st;
|
|
|
923a60 |
+ size_t n, m;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Determine length of first component in the path */
|
|
|
923a60 |
+ n = strspn(todo, "/"); /* The slashes */
|
|
|
923a60 |
+ m = n + strcspn(todo + n, "/"); /* The entire length of the component */
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Extract the first component. */
|
|
|
923a60 |
+ first = strndup(todo, m);
|
|
|
923a60 |
+ if (!first)
|
|
|
923a60 |
+ return -ENOMEM;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ todo += m;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Empty? Then we reached the end. */
|
|
|
923a60 |
+ if (isempty(first))
|
|
|
923a60 |
+ break;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Just a single slash? Then we reached the end. */
|
|
|
923a60 |
+ if (path_equal(first, "/")) {
|
|
|
923a60 |
+ /* Preserve the trailing slash */
|
|
|
923a60 |
+ if (!strextend(&done, "/", NULL))
|
|
|
923a60 |
+ return -ENOMEM;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ break;
|
|
|
923a60 |
+ }
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Just a dot? Then let's eat this up. */
|
|
|
923a60 |
+ if (path_equal(first, "/."))
|
|
|
923a60 |
+ continue;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Two dots? Then chop off the last bit of what we already found out. */
|
|
|
923a60 |
+ if (path_equal(first, "/..")) {
|
|
|
923a60 |
+ _cleanup_free_ char *parent = NULL;
|
|
|
923a60 |
+ int fd_parent = -1;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* If we already are at the top, then going up will not change anything. This is in-line with
|
|
|
923a60 |
+ * how the kernel handles this. */
|
|
|
923a60 |
+ if (isempty(done) || path_equal(done, "/"))
|
|
|
923a60 |
+ continue;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ parent = dirname_malloc(done);
|
|
|
923a60 |
+ if (!parent)
|
|
|
923a60 |
+ return -ENOMEM;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Don't allow this to leave the root dir. */
|
|
|
923a60 |
+ if (root &&
|
|
|
923a60 |
+ path_startswith(done, root) &&
|
|
|
923a60 |
+ !path_startswith(parent, root))
|
|
|
923a60 |
+ continue;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ free(done);
|
|
|
923a60 |
+ done = parent;
|
|
|
923a60 |
+ parent = NULL;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ fd_parent = openat(fd, "..", O_CLOEXEC|O_NOFOLLOW|O_PATH);
|
|
|
923a60 |
+ if (fd_parent < 0)
|
|
|
923a60 |
+ return -errno;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ safe_close(fd);
|
|
|
923a60 |
+ fd = fd_parent;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ continue;
|
|
|
923a60 |
+ }
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Otherwise let's see what this is. */
|
|
|
923a60 |
+ child = openat(fd, first + n, O_CLOEXEC|O_NOFOLLOW|O_PATH);
|
|
|
923a60 |
+ if (child < 0) {
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (errno == ENOENT &&
|
|
|
923a60 |
+ (flags & CHASE_NONEXISTENT) &&
|
|
|
923a60 |
+ (isempty(todo) || path_is_safe(todo))) {
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* If CHASE_NONEXISTENT is set, and the path does not exist, then that's OK, return
|
|
|
923a60 |
+ * what we got so far. But don't allow this if the remaining path contains "../ or "./"
|
|
|
923a60 |
+ * or something else weird. */
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* If done is "/", as first also contains slash at the head, then remove this redundant slash. */
|
|
|
923a60 |
+ if (streq_ptr(done, "/"))
|
|
|
923a60 |
+ *done = '\0';
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (!strextend(&done, first, todo, NULL))
|
|
|
923a60 |
+ return -ENOMEM;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ exists = false;
|
|
|
923a60 |
+ break;
|
|
|
923a60 |
+ }
|
|
|
923a60 |
+
|
|
|
923a60 |
+ return -errno;
|
|
|
923a60 |
+ }
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (fstat(child, &st) < 0)
|
|
|
923a60 |
+ return -errno;
|
|
|
923a60 |
+ if ((flags & CHASE_NO_AUTOFS) &&
|
|
|
923a60 |
+ fd_is_fs_type(child, AUTOFS_SUPER_MAGIC) > 0)
|
|
|
923a60 |
+ return -EREMOTE;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (S_ISLNK(st.st_mode)) {
|
|
|
923a60 |
+ char *joined;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ _cleanup_free_ char *destination = NULL;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* This is a symlink, in this case read the destination. But let's make sure we don't follow
|
|
|
923a60 |
+ * symlinks without bounds. */
|
|
|
923a60 |
+ if (--max_follow <= 0)
|
|
|
923a60 |
+ return -ELOOP;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = readlinkat_malloc(fd, first + n, &destination);
|
|
|
923a60 |
+ if (r < 0)
|
|
|
923a60 |
+ return r;
|
|
|
923a60 |
+ if (isempty(destination))
|
|
|
923a60 |
+ return -EINVAL;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (path_is_absolute(destination)) {
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* An absolute destination. Start the loop from the beginning, but use the root
|
|
|
923a60 |
+ * directory as base. */
|
|
|
923a60 |
+
|
|
|
923a60 |
+ safe_close(fd);
|
|
|
923a60 |
+ fd = open(root ?: "/", O_CLOEXEC|O_NOFOLLOW|O_PATH);
|
|
|
923a60 |
+ if (fd < 0)
|
|
|
923a60 |
+ return -errno;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ free(done);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Note that we do not revalidate the root, we take it as is. */
|
|
|
923a60 |
+ if (isempty(root))
|
|
|
923a60 |
+ done = NULL;
|
|
|
923a60 |
+ else {
|
|
|
923a60 |
+ done = strdup(root);
|
|
|
923a60 |
+ if (!done)
|
|
|
923a60 |
+ return -ENOMEM;
|
|
|
923a60 |
+ }
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Prefix what's left to do with what we just read, and start the loop again, but
|
|
|
923a60 |
+ * remain in the current directory. */
|
|
|
923a60 |
+ joined = strjoin(destination, todo, NULL);
|
|
|
923a60 |
+ } else
|
|
|
923a60 |
+ joined = strjoin("/", destination, todo, NULL);
|
|
|
923a60 |
+ if (!joined)
|
|
|
923a60 |
+ return -ENOMEM;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ free(buffer);
|
|
|
923a60 |
+ todo = buffer = joined;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ continue;
|
|
|
923a60 |
+ }
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* If this is not a symlink, then let's just add the name we read to what we already verified. */
|
|
|
923a60 |
+ if (!done) {
|
|
|
923a60 |
+ done = first;
|
|
|
923a60 |
+ first = NULL;
|
|
|
923a60 |
+ } else {
|
|
|
923a60 |
+ /* If done is "/", as first also contains slash at the head, then remove this redundant slash. */
|
|
|
923a60 |
+ if (streq(done, "/"))
|
|
|
923a60 |
+ *done = '\0';
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (!strextend(&done, first, NULL))
|
|
|
923a60 |
+ return -ENOMEM;
|
|
|
923a60 |
+ }
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* And iterate again, but go one directory further down. */
|
|
|
923a60 |
+ safe_close(fd);
|
|
|
923a60 |
+ fd = child;
|
|
|
923a60 |
+ child = -1;
|
|
|
923a60 |
+ }
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (!done) {
|
|
|
923a60 |
+ /* Special case, turn the empty string into "/", to indicate the root directory. */
|
|
|
923a60 |
+ done = strdup("/");
|
|
|
923a60 |
+ if (!done)
|
|
|
923a60 |
+ return -ENOMEM;
|
|
|
923a60 |
+ }
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (ret) {
|
|
|
923a60 |
+ *ret = done;
|
|
|
923a60 |
+ done = NULL;
|
|
|
923a60 |
+ }
|
|
|
923a60 |
+
|
|
|
923a60 |
+ return exists;
|
|
|
923a60 |
+}
|
|
|
923a60 |
diff --git a/src/shared/util.h b/src/shared/util.h
|
|
|
923a60 |
index f768936ab1..915c7439e8 100644
|
|
|
923a60 |
--- a/src/shared/util.h
|
|
|
923a60 |
+++ b/src/shared/util.h
|
|
|
923a60 |
@@ -1155,3 +1155,11 @@ typedef typeof(((struct statfs*)NULL)->f_type) statfs_f_type_t;
|
|
|
923a60 |
|
|
|
923a60 |
bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) _pure_;
|
|
|
923a60 |
int fd_is_fs_type(int fd, statfs_f_type_t magic_value);
|
|
|
923a60 |
+
|
|
|
923a60 |
+enum {
|
|
|
923a60 |
+ CHASE_PREFIX_ROOT = 1, /* If set, the specified path will be prefixed by the specified root before beginning the iteration */
|
|
|
923a60 |
+ CHASE_NONEXISTENT = 2, /* If set, it's OK if the path doesn't actually exist. */
|
|
|
923a60 |
+ CHASE_NO_AUTOFS = 4, /* If set, return -EREMOTE if autofs mount point found */
|
|
|
923a60 |
+};
|
|
|
923a60 |
+
|
|
|
923a60 |
+int chase_symlinks(const char *path_with_prefix, const char *root, unsigned flags, char **ret);
|
|
|
923a60 |
diff --git a/src/test/test-util.c b/src/test/test-util.c
|
|
|
923a60 |
index efb02ff530..397c45a9f4 100644
|
|
|
923a60 |
--- a/src/test/test-util.c
|
|
|
923a60 |
+++ b/src/test/test-util.c
|
|
|
923a60 |
@@ -36,6 +36,7 @@
|
|
|
923a60 |
#include "fileio.h"
|
|
|
923a60 |
#include "conf-parser.h"
|
|
|
923a60 |
#include "virt.h"
|
|
|
923a60 |
+#include "path-util.h"
|
|
|
923a60 |
|
|
|
923a60 |
static void test_streq_ptr(void) {
|
|
|
923a60 |
assert_se(streq_ptr(NULL, NULL));
|
|
|
923a60 |
@@ -1909,6 +1910,212 @@ static void test_acquire_data_fd(void) {
|
|
|
923a60 |
test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE|ACQUIRE_NO_TMPFILE);
|
|
|
923a60 |
}
|
|
|
923a60 |
|
|
|
923a60 |
+static void test_chase_symlinks(void) {
|
|
|
923a60 |
+ _cleanup_free_ char *result = NULL;
|
|
|
923a60 |
+ char temp[] = "/tmp/test-chase.XXXXXX";
|
|
|
923a60 |
+ const char *top, *p, *pslash, *q, *qslash;
|
|
|
923a60 |
+ int r;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ assert_se(mkdtemp(temp));
|
|
|
923a60 |
+
|
|
|
923a60 |
+ top = strjoina(temp, "/top");
|
|
|
923a60 |
+ assert_se(mkdir(top, 0700) >= 0);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(top, "/dot");
|
|
|
923a60 |
+ assert_se(symlink(".", p) >= 0);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(top, "/dotdot");
|
|
|
923a60 |
+ assert_se(symlink("..", p) >= 0);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(top, "/dotdota");
|
|
|
923a60 |
+ assert_se(symlink("../a", p) >= 0);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(temp, "/a");
|
|
|
923a60 |
+ assert_se(symlink("b", p) >= 0);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(temp, "/b");
|
|
|
923a60 |
+ assert_se(symlink("/usr", p) >= 0);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(temp, "/start");
|
|
|
923a60 |
+ assert_se(symlink("top/dot/dotdota", p) >= 0);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Paths that use symlinks underneath the "root" */
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(p, NULL, 0, &result);
|
|
|
923a60 |
+ assert_se(r > 0);
|
|
|
923a60 |
+ assert_se(path_equal(result, "/usr"));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ pslash = strjoina(p, "/");
|
|
|
923a60 |
+ r = chase_symlinks(pslash, NULL, 0, &result);
|
|
|
923a60 |
+ assert_se(r > 0);
|
|
|
923a60 |
+ assert_se(path_equal(result, "/usr/"));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(p, temp, 0, &result);
|
|
|
923a60 |
+ assert_se(r == -ENOENT);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(pslash, temp, 0, &result);
|
|
|
923a60 |
+ assert_se(r == -ENOENT);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ q = strjoina(temp, "/usr");
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(p, temp, CHASE_NONEXISTENT, &result);
|
|
|
923a60 |
+ assert_se(r == 0);
|
|
|
923a60 |
+ assert_se(path_equal(result, q));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ qslash = strjoina(q, "/");
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(pslash, temp, CHASE_NONEXISTENT, &result);
|
|
|
923a60 |
+ assert_se(r == 0);
|
|
|
923a60 |
+ assert_se(path_equal(result, qslash));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ assert_se(mkdir(q, 0700) >= 0);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(p, temp, 0, &result);
|
|
|
923a60 |
+ assert_se(r > 0);
|
|
|
923a60 |
+ assert_se(path_equal(result, q));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(pslash, temp, 0, &result);
|
|
|
923a60 |
+ assert_se(r > 0);
|
|
|
923a60 |
+ assert_se(path_equal(result, qslash));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(temp, "/slash");
|
|
|
923a60 |
+ assert_se(symlink("/", p) >= 0);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(p, NULL, 0, &result);
|
|
|
923a60 |
+ assert_se(r > 0);
|
|
|
923a60 |
+ assert_se(path_equal(result, "/"));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(p, temp, 0, &result);
|
|
|
923a60 |
+ assert_se(r > 0);
|
|
|
923a60 |
+ assert_se(path_equal(result, temp));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Paths that would "escape" outside of the "root" */
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(temp, "/6dots");
|
|
|
923a60 |
+ assert_se(symlink("../../..", p) >= 0);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(p, temp, 0, &result);
|
|
|
923a60 |
+ assert_se(r > 0 && path_equal(result, temp));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(temp, "/6dotsusr");
|
|
|
923a60 |
+ assert_se(symlink("../../../usr", p) >= 0);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(p, temp, 0, &result);
|
|
|
923a60 |
+ assert_se(r > 0 && path_equal(result, q));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(temp, "/top/8dotsusr");
|
|
|
923a60 |
+ assert_se(symlink("../../../../usr", p) >= 0);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(p, temp, 0, &result);
|
|
|
923a60 |
+ assert_se(r > 0 && path_equal(result, q));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Paths that contain repeated slashes */
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(temp, "/slashslash");
|
|
|
923a60 |
+ assert_se(symlink("///usr///", p) >= 0);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(p, NULL, 0, &result);
|
|
|
923a60 |
+ assert_se(r > 0);
|
|
|
923a60 |
+ assert_se(path_equal(result, "/usr"));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(p, temp, 0, &result);
|
|
|
923a60 |
+ assert_se(r > 0);
|
|
|
923a60 |
+ assert_se(path_equal(result, q));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Paths using . */
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks("/etc/./.././", NULL, 0, &result);
|
|
|
923a60 |
+ assert_se(r > 0);
|
|
|
923a60 |
+ assert_se(path_equal(result, "/"));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks("/etc/./.././", "/etc", 0, &result);
|
|
|
923a60 |
+ assert_se(r > 0 && path_equal(result, "/etc"));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks("/../.././//../../etc", NULL, 0, &result);
|
|
|
923a60 |
+ assert_se(r > 0);
|
|
|
923a60 |
+ assert_se(streq(result, "/etc"));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks("/../.././//../../test-chase.fsldajfl", NULL, CHASE_NONEXISTENT, &result);
|
|
|
923a60 |
+ assert_se(r == 0);
|
|
|
923a60 |
+ assert_se(streq(result, "/test-chase.fsldajfl"));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks("/../.././//../../etc", "/", CHASE_PREFIX_ROOT, &result);
|
|
|
923a60 |
+ assert_se(r > 0);
|
|
|
923a60 |
+ assert_se(streq(result, "/etc"));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks("/../.././//../../test-chase.fsldajfl", "/", CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &result);
|
|
|
923a60 |
+ assert_se(r == 0);
|
|
|
923a60 |
+ assert_se(streq(result, "/test-chase.fsldajfl"));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks("/etc/machine-id/foo", NULL, 0, &result);
|
|
|
923a60 |
+ assert_se(r == -ENOTDIR);
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Path that loops back to self */
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(temp, "/recursive-symlink");
|
|
|
923a60 |
+ assert_se(symlink("recursive-symlink", p) >= 0);
|
|
|
923a60 |
+ r = chase_symlinks(p, NULL, 0, &result);
|
|
|
923a60 |
+ assert_se(r == -ELOOP);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Path which doesn't exist */
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(temp, "/idontexist");
|
|
|
923a60 |
+ r = chase_symlinks(p, NULL, 0, &result);
|
|
|
923a60 |
+ assert_se(r == -ENOENT);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result);
|
|
|
923a60 |
+ assert_se(r == 0);
|
|
|
923a60 |
+ assert_se(path_equal(result, p));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(temp, "/idontexist/meneither");
|
|
|
923a60 |
+ r = chase_symlinks(p, NULL, 0, &result);
|
|
|
923a60 |
+ assert_se(r == -ENOENT);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result);
|
|
|
923a60 |
+ assert_se(r == 0);
|
|
|
923a60 |
+ assert_se(path_equal(result, p));
|
|
|
923a60 |
+ result = mfree(result);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* Path which doesn't exist, but contains weird stuff */
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(temp, "/idontexist/..");
|
|
|
923a60 |
+ r = chase_symlinks(p, NULL, 0, &result);
|
|
|
923a60 |
+ assert_se(r == -ENOENT);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result);
|
|
|
923a60 |
+ assert_se(r == -ENOENT);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ p = strjoina(temp, "/target");
|
|
|
923a60 |
+ q = strjoina(temp, "/top");
|
|
|
923a60 |
+ assert_se(symlink(q, p) >= 0);
|
|
|
923a60 |
+ p = strjoina(temp, "/target/idontexist");
|
|
|
923a60 |
+ r = chase_symlinks(p, NULL, 0, &result);
|
|
|
923a60 |
+ assert_se(r == -ENOENT);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ assert_se(rm_rf_dangerous(temp, false, true, false) >= 0);
|
|
|
923a60 |
+}
|
|
|
923a60 |
+
|
|
|
923a60 |
int main(int argc, char *argv[]) {
|
|
|
923a60 |
log_parse_environment();
|
|
|
923a60 |
log_open();
|
|
|
923a60 |
@@ -1992,6 +2199,7 @@ int main(int argc, char *argv[]) {
|
|
|
923a60 |
test_system_tasks_max();
|
|
|
923a60 |
test_system_tasks_max_scale();
|
|
|
923a60 |
test_acquire_data_fd();
|
|
|
923a60 |
+ test_chase_symlinks();
|
|
|
923a60 |
|
|
|
923a60 |
return 0;
|
|
|
923a60 |
}
|