mirror of
https://github.com/systemd/systemd.git
synced 2024-11-01 17:51:22 +03:00
util-lib: Fix chase_symlinks() with absolute symlinks (#5185)
If chase_symlinks() encouters an absolute symlink, it resets the todo buffer to just the newly discovered symlink and discards any of the remaining previous symlink path. Regardless of whether or not the symlink is absolute or relative, we need to preserve the remainder of the path that has not yet been resolved.
This commit is contained in:
parent
ef1fd941f9
commit
877777d776
@ -723,6 +723,8 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
|
||||
return -errno;
|
||||
|
||||
if (S_ISLNK(st.st_mode)) {
|
||||
char *joined;
|
||||
|
||||
_cleanup_free_ char *destination = NULL;
|
||||
|
||||
/* This is a symlink, in this case read the destination. But let's make sure we don't follow
|
||||
@ -746,9 +748,6 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
|
||||
if (fd < 0)
|
||||
return -errno;
|
||||
|
||||
free_and_replace(buffer, destination);
|
||||
|
||||
todo = buffer;
|
||||
free(done);
|
||||
|
||||
/* Note that we do not revalidate the root, we take it as is. */
|
||||
@ -760,11 +759,10 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
} else {
|
||||
char *joined;
|
||||
}
|
||||
|
||||
/* A relative destination. If so, this is what we'll prefix what's left to do with what
|
||||
* we just read, and start the loop again, but remain in the current directory. */
|
||||
/* Prefix what's left to do with what we just read, and start the loop again,
|
||||
* but remain in the current directory. */
|
||||
|
||||
joined = strjoin("/", destination, todo);
|
||||
if (!joined)
|
||||
@ -772,7 +770,6 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
|
||||
|
||||
free(buffer);
|
||||
todo = buffer = joined;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
@ -186,6 +186,13 @@ static void test_chase_symlinks(void) {
|
||||
r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result);
|
||||
assert_se(r == -ENOENT);
|
||||
|
||||
p = strjoina(temp, "/target");
|
||||
q = strjoina(temp, "/top");
|
||||
assert_se(symlink(q, p) >= 0);
|
||||
p = strjoina(temp, "/target/idontexist");
|
||||
r = chase_symlinks(p, NULL, 0, &result);
|
||||
assert_se(r == -ENOENT);
|
||||
|
||||
assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user