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
12 changes: 6 additions & 6 deletions cf-serverd/server_classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,16 @@ static bool AccessControl(EvalContext *ctx, const char *req_path, ServerConnecti
{
Log(LOG_LEVEL_VERBOSE, "Filename %s is resolved to %s", translated_req_path, transrequest);
}
else if ((lstat(translated_req_path, &statbuf) == -1) && !S_ISLNK(statbuf.st_mode))
else if ((lstat(translated_req_path, &statbuf) == 0) && S_ISLNK(statbuf.st_mode) &&
ResolveDanglingSymlink(translated_req_path, transrequest, sizeof(transrequest)))
{
Log(LOG_LEVEL_INFO, "Couldn't resolve (realpath: %s) filename: %s",
GetErrorStr(), translated_req_path);
return false; /* can't continue without transrequest */
Log(LOG_LEVEL_VERBOSE, "Requested file is a dead symbolic link (filename: %s)", translated_req_path);
}
else
{
Log(LOG_LEVEL_VERBOSE, "Requested file is a dead symbolic link (filename: %s)", translated_req_path);
strlcpy(transrequest, translated_req_path, CF_BUFSIZE);
Log(LOG_LEVEL_INFO, "Couldn't resolve (realpath: %s) filename: %s",
GetErrorStr(), translated_req_path);
return false; /* can't continue without transrequest */
}

if (lstat(transrequest, &statbuf) == -1)
Expand Down
51 changes: 49 additions & 2 deletions cf-serverd/server_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,53 @@ size_t ShortcutsExpand(char *path, size_t path_size,
return (size_t) -1;
}

/**
* realpath() fails on a dangling symlink, so canonicalise the parent
* directory of #path instead and append the link name. The result can be
* matched against the path ACLs like any other resolved path, unlike #path
* itself which may still contain '..' components or symlinked directories.
*
* @return true on success, with the result written in #dst.
*/
bool ResolveDanglingSymlink(const char *path, char *dst, size_t dst_size)
{
const char *link_name = strrchr(path, FILE_SEPARATOR);
if (link_name == NULL || link_name[1] == '\0')
{
return false;
}

char parent[PATH_MAX];
size_t parent_len = link_name - path;
if (parent_len >= sizeof(parent))
{
return false;
}
if (parent_len == 0) /* link is in / */
{
parent[0] = FILE_SEPARATOR;
parent[1] = '\0';
}
else
{
memcpy(parent, path, parent_len);
parent[parent_len] = '\0';
}
link_name++;

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

size_t resolved_len = strlen(resolved);
bool need_separator = (resolved[resolved_len - 1] != FILE_SEPARATOR);
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 @@ -1415,10 +1462,10 @@ size_t PreprocessRequestPath(char *reqpath, size_t reqpath_size)

/* } */
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, sizeof(dst)))
{
Log(LOG_LEVEL_VERBOSE, "Requested file is a dead symbolic link (filename: %s)", reqpath);
strlcpy(dst, reqpath, CF_BUFSIZE);
}
else
{
Expand Down
1 change: 1 addition & 0 deletions cf-serverd/server_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ size_t ShortcutsExpand(char *path, size_t path_size,
const StringMap *shortcuts,
const char *ipaddr, const char *hostname,
const char *key);
bool ResolveDanglingSymlink(const char *path, char *dst, size_t dst_size);
size_t PreprocessRequestPath(char *reqpath, size_t reqpath_size);
void SetConnIdentity(ServerConnectionState *conn, const char *username);
bool DoExec2(const EvalContext *ctx,
Expand Down
36 changes: 35 additions & 1 deletion tests/unit/protocol_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,48 @@ 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 of the request
* reach the prefix-based path ACL check unresolved.
*/
static void test_preprocess_request_path_dangling_symlink(void)
{
char tmpl[] = "/tmp/protocol_test.XXXXXX";
char *dir = mkdtemp(tmpl);
assert_true(dir != NULL);

char admitted[PATH_MAX], link_path[PATH_MAX];
snprintf(admitted, sizeof(admitted), "%s/admitted", dir);
snprintf(link_path, sizeof(link_path), "%s/dangling", dir);
assert_int_equal(mkdir(admitted, 0700), 0);
assert_int_equal(symlink("/nonexistent/protocol_test", link_path), 0);

char real_dir[PATH_MAX];
assert_true(realpath(dir, real_dir) != NULL);
char expected[PATH_MAX];
snprintf(expected, sizeof(expected), "%s/dangling", real_dir);

char reqpath[CF_BUFSIZE];
snprintf(reqpath, sizeof(reqpath), "%s/../dangling", admitted);
size_t len = PreprocessRequestPath(reqpath, sizeof(reqpath));
assert_string_equal(reqpath, expected);
assert_int_equal(len, strlen(expected));

unlink(link_path);
rmdir(admitted);
rmdir(dir);
}

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(test_preprocess_request_path_dangling_symlink)
};

return run_tests(tests);
Expand Down
Loading