diff --git a/cf-serverd/server_classic.c b/cf-serverd/server_classic.c index 3f05f32552a..57aba6409ba 100644 --- a/cf-serverd/server_classic.c +++ b/cf-serverd/server_classic.c @@ -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) diff --git a/cf-serverd/server_common.c b/cf-serverd/server_common.c index fb4b0b28b22..0c16d816c07 100644 --- a/cf-serverd/server_common.c +++ b/cf-serverd/server_common.c @@ -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: @@ -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 { diff --git a/cf-serverd/server_common.h b/cf-serverd/server_common.h index ab764ef95d8..16e4f1493c7 100644 --- a/cf-serverd/server_common.h +++ b/cf-serverd/server_common.h @@ -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, diff --git a/tests/unit/protocol_test.c b/tests/unit/protocol_test.c index 39829e3a694..1f68782cad4 100644 --- a/tests/unit/protocol_test.c +++ b/tests/unit/protocol_test.c @@ -501,6 +501,39 @@ 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(); @@ -508,7 +541,8 @@ int main() { 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);