From edbd3994aeffc3720297fd795d6eaba8330f5961 Mon Sep 17 00:00:00 2001 From: VB <7825101+codr1@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:47:20 +0400 Subject: [PATCH] fix(daemon): validate private-directory ancestors through O_PATH handles private_directory_tree_open walks from "/" (or ".") down to the private directory and opened every component O_RDONLY|O_DIRECTORY. Landlock counts that open as listing the directory, so a process whose ruleset handles LANDLOCK_ACCESS_FS_READ_DIR without granting it on "/" failed at the first component, and the daemon exited with "could not load saved runtime settings". Where O_PATH exists the walk now holds each component by an O_PATH handle. Every check reads the handle (fstat, fstatfs), so the ownership, mode and symlink rules are unchanged. A handle is reopened through itself as an ordinary directory descriptor before it is chmod'ed or returned, which needs the read-directory right on the private directory only. Without O_PATH the walk opens as before. The new test runs the real path inside a Landlock domain that may list nothing above the test's own directory. It fails without this change. Fixes #3014 Signed-off-by: VB <7825101+codr1@users.noreply.github.com> --- src/daemon/ipc.c | 47 +++++++++++++++++--- tests/test_daemon_ipc.c | 97 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 7 deletions(-) diff --git a/src/daemon/ipc.c b/src/daemon/ipc.c index d7e075ebcf..29131e5198 100644 --- a/src/daemon/ipc.c +++ b/src/daemon/ipc.c @@ -2020,6 +2020,41 @@ static bool posix_directory_transition_secure(int parent_fd, int child_fd, const return true; } +/* #3014: where the system has O_PATH, the walk holds each directory by an + * O_PATH handle. Every check below reads the handle (fstat, fstatfs), so the + * ownership, mode and symlink rules are unchanged, and a process confined by + * Landlock can validate ancestors it is not allowed to list: an O_RDONLY + * directory open needs the read-directory right on "/" and on every ancestor, + * an O_PATH open needs none. A handle that is chmod'ed or handed to the caller + * is first reopened through itself, which cannot name another directory. */ +#if defined(__linux__) && !defined(O_PATH) +#error "O_PATH is not visible; the private-directory walk needs it on Linux" +#endif +#ifdef O_PATH +#define PRIVATE_DIRECTORY_WALK_OPEN (O_PATH | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW) +#else +#define PRIVATE_DIRECTORY_WALK_OPEN (O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW) +#endif + +/* Replace a walk handle with an ordinary directory descriptor for the same + * directory. False leaves *directory_fd as it was. */ +static bool private_directory_walk_reopen(int *directory_fd) { +#ifdef O_PATH + int opened = openat(*directory_fd, ".", O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW); + if (opened < 0 || !fd_set_cloexec(opened)) { + if (opened >= 0) { + (void)close(opened); + } + return false; + } + (void)close(*directory_fd); + *directory_fd = opened; +#else + (void)directory_fd; +#endif + return true; +} + static int private_directory_tree_open(const char *directory_path) { if (!directory_path || !directory_path[0] || O_DIRECTORY == 0 || O_NOFOLLOW == 0) { return -1; @@ -2029,7 +2064,7 @@ static int private_directory_tree_open(const char *directory_path) { return -1; } bool absolute = path[0] == '/'; - int current_fd = open(absolute ? "/" : ".", O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW); + int current_fd = open(absolute ? "/" : ".", PRIVATE_DIRECTORY_WALK_OPEN); bool ok = current_fd >= 0 && fd_set_cloexec(current_fd); char *cursor = path; while (ok && *cursor == '/') { @@ -2084,16 +2119,14 @@ static int private_directory_tree_open(const char *directory_path) { if (!created && errno != EEXIST) { ok = false; } - int next_fd = - ok ? openat(current_fd, component, O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW) - : -1; + int next_fd = ok ? openat(current_fd, component, PRIVATE_DIRECTORY_WALK_OPEN) : -1; struct stat status; ok = next_fd >= 0 && fd_set_cloexec(next_fd) && fstat(next_fd, &status) == 0 && S_ISDIR(status.st_mode) && posix_directory_transition_secure(current_fd, next_fd, path); if (ok && created) { - ok = status.st_uid == geteuid() && fchmod(next_fd, 0700) == 0 && - cbm_macos_extended_acl_fd_clear(next_fd) && + ok = status.st_uid == geteuid() && private_directory_walk_reopen(&next_fd) && + fchmod(next_fd, 0700) == 0 && cbm_macos_extended_acl_fd_clear(next_fd) && cbm_macos_extended_acl_fd_is_empty(next_fd); } if (ok) { @@ -2124,7 +2157,7 @@ static int private_directory_tree_open(const char *directory_path) { (long)final_status.st_uid, (long)geteuid()); ok = false; } - if (ok && fchmod(current_fd, 0700) != 0) { + if (ok && (!private_directory_walk_reopen(¤t_fd) || fchmod(current_fd, 0700) != 0)) { ipc_validation_detail_set("%s: chmod 0700 failed (errno %d)", directory_path, errno); ok = false; } diff --git a/tests/test_daemon_ipc.c b/tests/test_daemon_ipc.c index f19fbe08c2..ad8739862a 100644 --- a/tests/test_daemon_ipc.c +++ b/tests/test_daemon_ipc.c @@ -53,6 +53,15 @@ #include #ifdef __linux__ #include /* #1830 userns real smoke: unshare(CLONE_NEWUSER). */ +#if defined(__has_include) && __has_include() +#include /* #3014 Landlock real smoke. */ +#include +#include +#if defined(SYS_landlock_create_ruleset) && defined(SYS_landlock_add_rule) && \ + defined(SYS_landlock_restrict_self) +#define IPC_TEST_HAS_LANDLOCK 1 +#endif +#endif #endif #ifdef __APPLE__ #include @@ -5394,6 +5403,93 @@ TEST(daemon_ipc_posix_private_directory_rejects_world_writable_ancestor) { PASS(); } +/* #3014: a process confined by Landlock that may not list "/", or any other + * ancestor of its private directory, must still be able to secure that + * directory. The walk used to open every ancestor O_RDONLY|O_DIRECTORY, which + * Landlock counts as listing, so it failed at "/" in any sandbox that handles + * LANDLOCK_ACCESS_FS_READ_DIR without granting it on the root; the daemon then + * exited with "could not load saved runtime settings". + * + * The child handles READ_DIR only and is granted it beneath this test's own + * parent directory, then creates and secures a private directory below it. + * Exit protocol, as the userns smoke: 0 secured, 1 refused, 2 Landlock is not + * available in this kernel, 3 the harness could not build its sandbox. */ +TEST(daemon_ipc_linux_private_directory_secures_inside_landlock_issue3014) { +#ifdef IPC_TEST_HAS_LANDLOCK + char parent[TEST_PATH_CAP] = {0}; + char cache[TEST_PATH_CAP] = {0}; + bool paths_ok = false; + if (ipc_test_parent_new(parent, "private-cache-landlock")) { + int cache_written = snprintf(cache, sizeof(cache), "%s/cache", parent); + paths_ok = cache_written > 0 && cache_written < (int)sizeof(cache); + } + if (!paths_ok) { + ipc_test_remove_flat_dir(parent); + FAIL("could not create the Landlock smoke's parent directory"); + } + + pid_t child = fork(); + if (child == 0) { + struct landlock_ruleset_attr ruleset = {.handled_access_fs = LANDLOCK_ACCESS_FS_READ_DIR}; + int ruleset_fd = (int)syscall(SYS_landlock_create_ruleset, &ruleset, sizeof(ruleset), 0U); + if (ruleset_fd < 0) { + _exit(errno == ENOSYS || errno == EOPNOTSUPP ? 2 : 3); + } + struct landlock_path_beneath_attr beneath = { + .allowed_access = LANDLOCK_ACCESS_FS_READ_DIR, + .parent_fd = open(parent, O_PATH | O_CLOEXEC), + }; + if (beneath.parent_fd < 0 || + syscall(SYS_landlock_add_rule, ruleset_fd, LANDLOCK_RULE_PATH_BENEATH, &beneath, 0U) != + 0 || + prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0 || + syscall(SYS_landlock_restrict_self, ruleset_fd, 0U) != 0) { + _exit(3); + } + /* The sandbox must be real, or a pass below would prove nothing: + * listing "/" is refused from here on. */ + int root_fd = open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if (root_fd >= 0 || errno != EACCES) { + _exit(3); + } + _exit(cbm_daemon_ipc_private_directory_secure(cache) ? 0 : 1); + } + int status = 0; + if (child > 0) { + while (waitpid(child, &status, 0) < 0 && errno == EINTR) {} + } + struct stat secured = {0}; + bool hardened = lstat(cache, &secured) == 0 && S_ISDIR(secured.st_mode) && + secured.st_uid == geteuid() && (secured.st_mode & 07777) == 0700; + (void)rmdir(cache); + ipc_test_remove_flat_dir(parent); + + if (child < 0) { + FAIL("fork failed for the Landlock smoke"); + } + if (!WIFEXITED(status)) { + FAIL("Landlock probe did not exit normally (signalled) -- not a security verdict"); + } + if (WEXITSTATUS(status) == 2) { + SKIP_PLATFORM("Landlock is not available in this Linux kernel"); + } + if (WEXITSTATUS(status) == 3) { + FAIL("Landlock smoke could not build a sandbox that refuses listing /"); + } + if (WEXITSTATUS(status) != 0 && WEXITSTATUS(status) != 1) { + char unexpected[128]; + (void)snprintf(unexpected, sizeof(unexpected), + "Landlock probe exited %d -- not a security verdict", WEXITSTATUS(status)); + FAIL(unexpected); + } + ASSERT_EQ(0, WEXITSTATUS(status)); + ASSERT_TRUE(hardened); + PASS(); +#else + SKIP_PLATFORM("Landlock is Linux-only"); +#endif +} + TEST(daemon_ipc_posix_private_log_rejects_symlinks_and_is_owner_only) { char parent[TEST_PATH_CAP] = {0}; char logs[TEST_PATH_CAP] = {0}; @@ -6240,6 +6336,7 @@ SUITE(daemon_ipc) { RUN_TEST(daemon_ipc_posix_private_log_creates_first_ever_cache_tree_safely); RUN_TEST(daemon_ipc_posix_private_directory_hardens_existing_cache_root); RUN_TEST(daemon_ipc_posix_private_directory_rejects_world_writable_ancestor); + RUN_TEST(daemon_ipc_linux_private_directory_secures_inside_landlock_issue3014); RUN_TEST(daemon_ipc_posix_private_log_rejects_symlinks_and_is_owner_only); RUN_TEST(daemon_ipc_posix_rejects_non_socket_and_symlink_endpoints); #ifndef _WIN32