Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions cf-serverd/server_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,48 @@ size_t ShortcutsExpand(char *path, size_t path_size,
return (size_t) -1;
}

/**
* Get the canonical path of a dangling symlink.
*
* realpath() fails if the symlink target does not exist. Instead, this
* resolves the parent directory with realpath() and appends the link name.
* The link itself is not followed.
*
* Example: "/srv/admitted/../dangling" becomes "/srv/dangling".
*
* The caller must check that #path is a symlink.
*
* @return true on success, with the result written to #dst.
*/
static bool ResolveDanglingSymlink(const char *path, char *dst, size_t dst_size)
{
const char *link_name = ReadLastNode(path);
if (link_name[0] == '\0')
{
return false;
}

char *parent = GetParentDirectoryCopy(path);
if (parent == NULL)
{
return false;
}

char resolved[PATH_MAX];
const char *ok = realpath(parent, resolved);
free(parent);
if (ok == NULL)
{
return false;
}

size_t resolved_len = strlen(resolved);
bool need_separator = !IsFileSep(resolved[resolved_len - 1]);
int ret = snprintf(dst, dst_size, "%s%s%s", resolved,
need_separator ? FILE_SEPARATOR_STR : "", link_name);
return (ret >= 0 && (size_t) ret < dst_size);
}

/**
* Canonicalize a path, ensure it is absolute, and resolve all symlinks.
* In detail:
Expand Down Expand Up @@ -1414,11 +1456,13 @@ size_t PreprocessRequestPath(char *reqpath, size_t reqpath_size)
/* { */

/* } */
/* Cap at PATH_MAX like realpath(), since callers such as
* acl_CheckPath() copy the result into PATH_MAX sized buffers */
struct stat statbuf;
if ((lstat(reqpath, &statbuf) == 0) && S_ISLNK(statbuf.st_mode))
if ((lstat(reqpath, &statbuf) == 0) && S_ISLNK(statbuf.st_mode) &&
ResolveDanglingSymlink(reqpath, dst, MIN(sizeof(dst), PATH_MAX)))
{
Log(LOG_LEVEL_VERBOSE, "Requested file is a dead symbolic link (filename: %s)", reqpath);
strlcpy(dst, reqpath, CF_BUFSIZE);
}
else
{
Expand Down
77 changes: 76 additions & 1 deletion tests/unit/protocol_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <connection_info.h>
#include <net.h>
#include <eval_context.h>
#include <misc_lib.h> /* xsnprintf */

#include <sys/socket.h>
#include <unistd.h>
Expand Down Expand Up @@ -501,14 +502,88 @@ static void test_get_server_literal_sendbuffer_bound(void)
free(conn);
}

/*
* realpath() fails on a dangling symlink. PreprocessRequestPath() must still
* return a canonical path for it. Otherwise '..' components and symlinked
* directories reach the prefix-based path ACL check unresolved.
*
* Layout:
* <dir>/admitted/
* <dir>/admitted/escape -> ../outside
* <dir>/outside/
* <dir>/outside/dangling -> /nonexistent/protocol_test
*/
static char DANGLING_DIR[PATH_MAX];
static char DANGLING_REAL_DIR[PATH_MAX];

static void dangling_setup(void)
{
strlcpy(DANGLING_DIR, "/tmp/protocol_test.XXXXXX", sizeof(DANGLING_DIR));
assert_true(mkdtemp(DANGLING_DIR) != NULL);
assert_true(realpath(DANGLING_DIR, DANGLING_REAL_DIR) != NULL);

char path[PATH_MAX];
xsnprintf(path, sizeof(path), "%s/admitted", DANGLING_DIR);
assert_int_equal(mkdir(path, 0700), 0);
xsnprintf(path, sizeof(path), "%s/outside", DANGLING_DIR);
assert_int_equal(mkdir(path, 0700), 0);
xsnprintf(path, sizeof(path), "%s/outside/dangling", DANGLING_DIR);
assert_int_equal(symlink("/nonexistent/protocol_test", path), 0);
xsnprintf(path, sizeof(path), "%s/admitted/escape", DANGLING_DIR);
assert_int_equal(symlink("../outside", path), 0);
}

static void dangling_teardown(void)
{
char path[PATH_MAX];
xsnprintf(path, sizeof(path), "%s/admitted/escape", DANGLING_DIR);
unlink(path);
xsnprintf(path, sizeof(path), "%s/outside/dangling", DANGLING_DIR);
unlink(path);
xsnprintf(path, sizeof(path), "%s/outside", DANGLING_DIR);
rmdir(path);
xsnprintf(path, sizeof(path), "%s/admitted", DANGLING_DIR);
rmdir(path);
rmdir(DANGLING_DIR);
}

/* Request <dir>/<request> must resolve to <real dir>/outside/dangling */
static void check_resolves_outside(const char *request)
{
char reqpath[CF_BUFSIZE];
xsnprintf(reqpath, sizeof(reqpath), "%s/%s", DANGLING_DIR, request);

char expected[PATH_MAX];
xsnprintf(expected, sizeof(expected), "%s/outside/dangling",
DANGLING_REAL_DIR);

size_t len = PreprocessRequestPath(reqpath, sizeof(reqpath));
assert_string_equal(reqpath, expected);
assert_int_equal(len, strlen(expected));
}

static void test_preprocess_request_path_dangling_dotdot(void)
{
check_resolves_outside("admitted/../outside/dangling");
}

static void test_preprocess_request_path_dangling_symlinked_dir(void)
{
check_resolves_outside("admitted/escape/dangling");
}

int main()
{
PRINT_TEST_BANNER();
const UnitTest tests[] =
{
unit_test(test_command_parser),
unit_test(test_user_name),
unit_test(test_get_server_literal_sendbuffer_bound)
unit_test(test_get_server_literal_sendbuffer_bound),
unit_test_setup_teardown(test_preprocess_request_path_dangling_dotdot,
dangling_setup, dangling_teardown),
unit_test_setup_teardown(test_preprocess_request_path_dangling_symlinked_dir,
dangling_setup, dangling_teardown)
};

return run_tests(tests);
Expand Down
Loading