Skip to content

Commit b91abca

Browse files
committed
fix(daemon): admit a group-writable ancestor, and stop inventing an errno (DeusData#1537)
The reporter came back with the output that named this: exact executable identity could not be verified (cache-private) - /Users/.../.cache/codebase-memory-mcp: ancestry component validation failed (errno 2) Two separate defects, and the second is why nobody could see the first. **The refusal.** The daemon's private-directory ancestry walk rejected any group-write bit on any ancestor — `(mode & 0022) == 0`. That is the identical rule DeusData#1535 removed on the activation side, in the sibling gate the consolidated strictness decision covers but that never got changed. A group-writable ~ or ~/.cache is ordinary: WSL2 ships 0775, so do several distro skeletons and any site with a shared primary group. So the daemon refused to run, permanently, with nothing the reader could close or restart. World-writable is still refused (any local user could swap a path component), and the private directory itself is still chmod'd to 0700 and verified after the walk — which is what makes admitting the ancestor safe: the thing that actually holds data stays owner-private either way. Group-writable ancestors are logged, so the weaker check is never silent. Both directions are tested; a fix that admitted everything would "fix" this by deleting the protection. **The invented errno.** When that check failed, the branch set no detail, so the caller fell back to printing `errno` — which nothing on the path sets. The reporter was handed ENOENT for a permission refusal and went looking for a missing file that was there all along; it cost a round trip to find out the number meant nothing. The branch now names the component and the rule. Reported by @listepo, who ran the diagnostics that turned this from a guess into a one-line root cause. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent a654acb commit b91abca

2 files changed

Lines changed: 111 additions & 4 deletions

File tree

‎src/daemon/ipc.c‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,8 +1383,26 @@ static bool posix_directory_parent_secure(int directory_fd) {
13831383
!cbm_macos_extended_acl_fd_is_deny_only(directory_fd)) {
13841384
return false;
13851385
}
1386-
return (status.st_mode & 0022) == 0 ||
1387-
(status.st_uid == (uid_t)0 && (status.st_mode & S_ISVTX) != 0);
1386+
/* #1537: this ANCESTOR check refused any group-write bit, which is the same
1387+
* rule #1535 removed on the activation side — and the sibling that decision
1388+
* covers but that never got changed. A group-writable ~ or ~/.cache is
1389+
* ordinary (WSL2 ships 0775, so do several distro skeletons and any site
1390+
* with a shared primary group), and refusing it here made the daemon
1391+
* unusable with no way for the reader to see why.
1392+
*
1393+
* WORLD-writable is still refused: any local user could swap a path
1394+
* component. Group-writable is admitted for ancestors only — the private
1395+
* directory itself is chmod'd to 0700 and verified after this walk, so the
1396+
* thing that actually holds data stays owner-private either way. */
1397+
if ((status.st_mode & 0002) != 0) {
1398+
return status.st_uid == (uid_t)0 && (status.st_mode & S_ISVTX) != 0;
1399+
}
1400+
if ((status.st_mode & 0020) != 0) {
1401+
char mode_text[16];
1402+
(void)snprintf(mode_text, sizeof(mode_text), "%04o", (unsigned)(status.st_mode & 07777));
1403+
cbm_log_warn("daemon.private_dir_group_writable_ancestor", "mode", mode_text);
1404+
}
1405+
return true;
13881406
}
13891407

13901408
/* Validate a path transition only through the two already-open directory
@@ -1435,6 +1453,17 @@ static int private_directory_tree_open(const char *directory_path) {
14351453
ok = false;
14361454
} else {
14371455
ok = posix_directory_parent_secure(current_fd);
1456+
if (!ok) {
1457+
/* #1537: this branch used to leave the detail empty, so the
1458+
* caller fell back to printing errno — which NOTHING here sets.
1459+
* A reporter was handed "errno 2" (ENOENT) for a permission
1460+
* refusal and went looking for a missing file that existed.
1461+
* An unset errno is not a diagnosis; name the component. */
1462+
ipc_validation_detail_set("%s: ancestor '%s' is not a usable private-directory "
1463+
"parent (must be owned by you, not world-writable, and "
1464+
"carry no allow-ACL)",
1465+
directory_path, component);
1466+
}
14381467
bool created = ok && mkdirat(current_fd, component, 0700) == 0;
14391468
if (!created && errno != EEXIST) {
14401469
ok = false;

‎tests/test_daemon_ipc.c‎

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,8 +1667,7 @@ TEST(daemon_ipc_endpoint_is_namespaced_by_instance_key) {
16671667
if (a_startup_status == 1 && !cbm_daemon_ipc_startup_lock_prepare_handoff(a_startup)) {
16681668
a_startup_status = -1;
16691669
}
1670-
if (other_startup_status == 1 &&
1671-
!cbm_daemon_ipc_startup_lock_prepare_handoff(other_startup)) {
1670+
if (other_startup_status == 1 && !cbm_daemon_ipc_startup_lock_prepare_handoff(other_startup)) {
16721671
other_startup_status = -1;
16731672
}
16741673
cbm_daemon_ipc_startup_lock_release(&a_startup);
@@ -4712,6 +4711,83 @@ TEST(daemon_ipc_posix_rejects_non_socket_and_symlink_endpoints) {
47124711

47134712
#endif /* !_WIN32 */
47144713

4714+
#ifndef _WIN32
4715+
/* #1537: the daemon's private-directory ancestry walk refused ANY group-write
4716+
* bit on an ancestor — the same rule #1535 removed on the activation side, and
4717+
* the sibling the consolidated strictness decision covers but that never got
4718+
* changed. A group-writable ~ or ~/.cache is ordinary (WSL2 ships 0775, so do
4719+
* several distro skeletons and any shared-primary-group site), so the daemon
4720+
* was unusable there. World-writable must still be refused: any local user
4721+
* could swap a path component. And the private directory itself must still end
4722+
* up owner-private, which is what makes admitting the ancestor safe. */
4723+
TEST(daemon_ipc_posix_group_writable_ancestor_is_admitted_issue1537) {
4724+
char parent[TEST_PATH_CAP];
4725+
char ancestor[TEST_PATH_CAP];
4726+
char cache[TEST_PATH_CAP];
4727+
bool paths_ok = false;
4728+
bool ancestor_ready = false;
4729+
bool secured = false;
4730+
bool leaf_owner_private = false;
4731+
4732+
if (ipc_test_parent_new(parent, "posix-group-writable-ancestor")) {
4733+
int a = snprintf(ancestor, sizeof(ancestor), "%s/group-writable", parent);
4734+
int c = snprintf(cache, sizeof(cache), "%s/cache", ancestor);
4735+
paths_ok = a > 0 && a < (int)sizeof(ancestor) && c > 0 && c < (int)sizeof(cache);
4736+
}
4737+
if (paths_ok) {
4738+
ancestor_ready = mkdir(ancestor, 0775) == 0 && chmod(ancestor, 0775) == 0;
4739+
}
4740+
if (ancestor_ready) {
4741+
secured = cbm_daemon_ipc_private_directory_secure(cache);
4742+
struct stat leaf;
4743+
leaf_owner_private =
4744+
stat(cache, &leaf) == 0 && S_ISDIR(leaf.st_mode) && (leaf.st_mode & 07777) == 0700;
4745+
}
4746+
4747+
(void)rmdir(cache);
4748+
(void)rmdir(ancestor);
4749+
ipc_test_remove_flat_dir(parent);
4750+
4751+
ASSERT_TRUE(paths_ok);
4752+
ASSERT_TRUE(ancestor_ready);
4753+
ASSERT_TRUE(secured);
4754+
ASSERT_TRUE(leaf_owner_private);
4755+
PASS();
4756+
}
4757+
4758+
/* The other half of the same decision: world-writable stays refused. A rule
4759+
* that admitted everything would "fix" #1537 by removing the protection. */
4760+
TEST(daemon_ipc_posix_world_writable_ancestor_still_refused_issue1537) {
4761+
char parent[TEST_PATH_CAP];
4762+
char ancestor[TEST_PATH_CAP];
4763+
char cache[TEST_PATH_CAP];
4764+
bool paths_ok = false;
4765+
bool ancestor_ready = false;
4766+
bool refused = false;
4767+
4768+
if (ipc_test_parent_new(parent, "posix-world-writable-ancestor")) {
4769+
int a = snprintf(ancestor, sizeof(ancestor), "%s/world-writable", parent);
4770+
int c = snprintf(cache, sizeof(cache), "%s/cache", ancestor);
4771+
paths_ok = a > 0 && a < (int)sizeof(ancestor) && c > 0 && c < (int)sizeof(cache);
4772+
}
4773+
if (paths_ok) {
4774+
ancestor_ready = mkdir(ancestor, 0777) == 0 && chmod(ancestor, 0777) == 0;
4775+
}
4776+
if (ancestor_ready) {
4777+
refused = !cbm_daemon_ipc_private_directory_secure(cache);
4778+
}
4779+
4780+
(void)rmdir(cache);
4781+
(void)rmdir(ancestor);
4782+
ipc_test_remove_flat_dir(parent);
4783+
4784+
ASSERT_TRUE(paths_ok);
4785+
ASSERT_TRUE(ancestor_ready);
4786+
ASSERT_TRUE(refused);
4787+
PASS();
4788+
}
4789+
#endif /* !_WIN32 */
4790+
47154791
SUITE(daemon_ipc) {
47164792
RUN_TEST(daemon_ipc_pending_timeout_race_returns_completed_io);
47174793
RUN_TEST(daemon_ipc_pending_wait_failure_cancels_and_drains);
@@ -4750,6 +4826,8 @@ SUITE(daemon_ipc) {
47504826
RUN_TEST(daemon_ipc_windows_startup_release_retains_retry_authority);
47514827
RUN_TEST(daemon_ipc_frame_timeout_poisons_connection);
47524828
#ifndef _WIN32
4829+
RUN_TEST(daemon_ipc_posix_group_writable_ancestor_is_admitted_issue1537);
4830+
RUN_TEST(daemon_ipc_posix_world_writable_ancestor_still_refused_issue1537);
47534831
RUN_TEST(daemon_ipc_posix_startup_lock_is_cross_process);
47544832
RUN_TEST(daemon_ipc_posix_lifetime_reservation_rejects_fork_inheritance);
47554833
RUN_TEST(daemon_ipc_posix_child_participant_handoff_retains_legacy_bridge);

0 commit comments

Comments
 (0)