From 4f7cb87a91e5f0f8eb4214cae15823f62ecc3eb4 Mon Sep 17 00:00:00 2001 From: Dhanraj E Date: Sun, 15 Nov 2026 14:43:28 +0530 Subject: [PATCH 1/2] cf-serverd: Fixed path ACL bypass through dangling symlink requests PreprocessRequestPath() (TLS protocol) and AccessControl() (classic protocol) canonicalise the requested path with realpath() before matching it against the path ACL, and that match is a plain string prefix check. realpath() fails on a dangling symlink, and both functions then fell back to the path exactly as the client sent it, with '..' components and symlinked directories unresolved. A client admitted to /srv/admitted could request /srv/admitted/../../etc/some-dangling-link: lstat() resolves it outside the admitted tree, while the prefix match admits it (and deny rules miss it). STAT then returns the lstat() metadata and readlink() target of any dangling symlink on the server. For a dangling symlink, canonicalise its parent directory with realpath() and append the link name instead. The classic branch also no longer reads statbuf after a failed lstat(). Changelog: Title Signed-off-by: Dhanraj E --- cf-serverd/server_classic.c | 12 ++++----- cf-serverd/server_common.c | 51 +++++++++++++++++++++++++++++++++++-- cf-serverd/server_common.h | 1 + tests/unit/protocol_test.c | 36 +++++++++++++++++++++++++- 4 files changed, 91 insertions(+), 9 deletions(-) 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); From e1db2e48c93107682ae1c78b6fffbae3a97f2b67 Mon Sep 17 00:00:00 2001 From: Lars Erik Wik Date: Fri, 20 Nov 2026 13:55:46 +0100 Subject: [PATCH 2/2] Reworked dangling symlink fix in cf-serverd - Removed the classic protocol changes. - Capped the resolved path at PATH_MAX, as acl_CheckPath() expects. - Reused ReadLastNode() and GetParentDirectoryCopy(). - Added a test for a symlinked parent directory. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Lars Erik Wik --- cf-serverd/server_classic.c | 12 +++--- cf-serverd/server_common.c | 47 ++++++++++----------- cf-serverd/server_common.h | 1 - tests/unit/protocol_test.c | 81 ++++++++++++++++++++++++++++--------- 4 files changed, 89 insertions(+), 52 deletions(-) diff --git a/cf-serverd/server_classic.c b/cf-serverd/server_classic.c index 57aba6409ba..3f05f32552a 100644 --- a/cf-serverd/server_classic.c +++ b/cf-serverd/server_classic.c @@ -185,17 +185,17 @@ 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) == 0) && S_ISLNK(statbuf.st_mode) && - ResolveDanglingSymlink(translated_req_path, transrequest, sizeof(transrequest))) - { - Log(LOG_LEVEL_VERBOSE, "Requested file is a dead symbolic link (filename: %s)", translated_req_path); - } - else + else if ((lstat(translated_req_path, &statbuf) == -1) && !S_ISLNK(statbuf.st_mode)) { Log(LOG_LEVEL_INFO, "Couldn't resolve (realpath: %s) filename: %s", GetErrorStr(), translated_req_path); return false; /* can't continue without transrequest */ } + else + { + Log(LOG_LEVEL_VERBOSE, "Requested file is a dead symbolic link (filename: %s)", translated_req_path); + strlcpy(transrequest, translated_req_path, CF_BUFSIZE); + } if (lstat(transrequest, &statbuf) == -1) { diff --git a/cf-serverd/server_common.c b/cf-serverd/server_common.c index 0c16d816c07..bdac5c9cb30 100644 --- a/cf-serverd/server_common.c +++ b/cf-serverd/server_common.c @@ -1339,47 +1339,42 @@ size_t ShortcutsExpand(char *path, size_t path_size, } /** - * 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. + * Get the canonical path of a dangling symlink. * - * @return true on success, with the result written in #dst. + * 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. */ -bool ResolveDanglingSymlink(const char *path, char *dst, size_t dst_size) +static 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') + const char *link_name = ReadLastNode(path); + if (link_name[0] == '\0') { return false; } - char parent[PATH_MAX]; - size_t parent_len = link_name - path; - if (parent_len >= sizeof(parent)) + char *parent = GetParentDirectoryCopy(path); + if (parent == NULL) { 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) + const char *ok = realpath(parent, resolved); + free(parent); + if (ok == NULL) { return false; } size_t resolved_len = strlen(resolved); - bool need_separator = (resolved[resolved_len - 1] != FILE_SEPARATOR); + 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); @@ -1461,9 +1456,11 @@ 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) && - ResolveDanglingSymlink(reqpath, dst, sizeof(dst))) + ResolveDanglingSymlink(reqpath, dst, MIN(sizeof(dst), PATH_MAX))) { Log(LOG_LEVEL_VERBOSE, "Requested file is a dead symbolic link (filename: %s)", reqpath); } diff --git a/cf-serverd/server_common.h b/cf-serverd/server_common.h index 16e4f1493c7..ab764ef95d8 100644 --- a/cf-serverd/server_common.h +++ b/cf-serverd/server_common.h @@ -64,7 +64,6 @@ 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 1f68782cad4..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 @@ -503,35 +504,72 @@ static void test_get_server_literal_sendbuffer_bound(void) /* * 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. + * 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 void test_preprocess_request_path_dangling_symlink(void) +static char DANGLING_DIR[PATH_MAX]; +static char DANGLING_REAL_DIR[PATH_MAX]; + +static void dangling_setup(void) { - char tmpl[] = "/tmp/protocol_test.XXXXXX"; - char *dir = mkdtemp(tmpl); - assert_true(dir != NULL); + 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 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 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); +} - char real_dir[PATH_MAX]; - assert_true(realpath(dir, real_dir) != NULL); - char expected[PATH_MAX]; - snprintf(expected, sizeof(expected), "%s/dangling", real_dir); +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]; - snprintf(reqpath, sizeof(reqpath), "%s/../dangling", admitted); + 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)); +} - unlink(link_path); - rmdir(admitted); - rmdir(dir); +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() @@ -542,7 +580,10 @@ int main() unit_test(test_command_parser), unit_test(test_user_name), unit_test(test_get_server_literal_sendbuffer_bound), - unit_test(test_preprocess_request_path_dangling_symlink) + 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);