diff --git a/cf-serverd/server_common.c b/cf-serverd/server_common.c index fb4b0b28b22..bdac5c9cb30 100644 --- a/cf-serverd/server_common.c +++ b/cf-serverd/server_common.c @@ -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: @@ -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 { diff --git a/tests/unit/protocol_test.c b/tests/unit/protocol_test.c index 39829e3a694..b6caf92707d 100644 --- a/tests/unit/protocol_test.c +++ b/tests/unit/protocol_test.c @@ -11,6 +11,7 @@ #include #include #include +#include /* xsnprintf */ #include #include @@ -501,6 +502,76 @@ 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: + * /admitted/ + * /admitted/escape -> ../outside + * /outside/ + * /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 / must resolve to /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(); @@ -508,7 +579,11 @@ 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_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);