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
47 changes: 40 additions & 7 deletions src/daemon/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 == '/') {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(&current_fd) || fchmod(current_fd, 0700) != 0)) {
ipc_validation_detail_set("%s: chmod 0700 failed (errno %d)", directory_path, errno);
ok = false;
}
Expand Down
97 changes: 97 additions & 0 deletions tests/test_daemon_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
#include <unistd.h>
#ifdef __linux__
#include <sched.h> /* #1830 userns real smoke: unshare(CLONE_NEWUSER). */
#if defined(__has_include) && __has_include(<linux/landlock.h>)
#include <linux/landlock.h> /* #3014 Landlock real smoke. */
#include <sys/prctl.h>
#include <sys/syscall.h>
#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 <membership.h>
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down
Loading