Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

Merge main and strengthen home alias ownership regression coverage

Martin Vogel631d24c790f6
diff --cc src/cli/activation_transaction.c
index f526b2c9c,18ed6dd14..f8af41530
--- a/src/cli/activation_transaction.c
+++ b/src/cli/activation_transaction.c
@@@ -889,48 -899,6 +899,61 @@@ static bool activation_posix_acl_empty(
return cbm_macos_extended_acl_fd_is_empty(descriptor);
}
+#ifdef __linux__
+/* Root of the per-user home tree and the account trusted to own it. Root and
+ * "/home" in production; the test seam substitutes a fixture tree and the test
+ * account, because the real layout needs root-owned entries to reproduce. */
+static const char *g_activation_home_root = "/home";
+static uid_t g_activation_home_owner = 0;
+
+#ifdef CBM_ENABLE_TEST_SEAMS
+void cbm_activation_transaction_set_home_root_for_testing(const char *home_root,
+ unsigned long owner_uid) {
+ g_activation_home_root = home_root ? home_root : "/home";
+ g_activation_home_owner = home_root ? (uid_t)owner_uid : 0;
+}
+#endif
+#endif
+
+#if defined(__APPLE__) || defined(__linux__)
+/* Map a trusted symlink `link` (already lstat'd) onto its resolved target and
+ * append `rest`. The link and the target must both belong to `owner`; with
+ * accept_own_target the target may instead belong to the current account.
+ * Returns NULL when either check fails or the mapped path does not fit. */
+static char *activation_alias_map(const char *link, const struct stat *link_status, uid_t owner,
+ bool accept_own_target, const char *rest) {
+ char resolved[4096];
+ struct stat resolved_status;
+ if (link_status->st_uid != owner || !realpath(link, resolved) ||
+ lstat(resolved, &resolved_status) != 0 || !S_ISDIR(resolved_status.st_mode)) {
+ return NULL;
+ }
+ if (resolved_status.st_uid != owner &&
+ !(accept_own_target && resolved_status.st_uid == geteuid())) {
+ return NULL;
+ }
+ char mapped[4096];
+ int written = snprintf(mapped, sizeof(mapped), "%s%s", resolved, rest);
+ if (written <= 0 || (size_t)written >= sizeof(mapped)) {
+ return NULL;
+ }
+ return activation_string_copy(mapped);
+}
++
++#ifdef CBM_ENABLE_TEST_SEAMS
++char *cbm_activation_transaction_map_alias_for_testing(const char *link,
++ unsigned long link_owner_uid,
++ unsigned long trusted_owner_uid,
++ bool accept_own_target, const char *rest) {
++ /* Only link ownership is synthetic; the shared production helper resolves
++ * and checks the real target so distinct-owner cases need no chown/root. */
++ const struct stat link_status = {.st_uid = (uid_t)link_owner_uid};
++ return activation_alias_map(link, &link_status, (uid_t)trusted_owner_uid, accept_own_target,
++ rest);
++}
++#endif
+#endif
+
static char *activation_posix_walk_path(const char *directory) {
#if defined(__APPLE__) || defined(__linux__)
/* macOS and immutable Linux layouts can expose writable trees through
diff --cc src/cli/activation_transaction.h
index 591f655fb,3aa37e1e4..d52ba76de
--- a/src/cli/activation_transaction.h
+++ b/src/cli/activation_transaction.h
@@@ -100,11 -104,7 +104,22 @@@ const char *cbm_activation_transaction_
#ifdef CBM_ENABLE_TEST_SEAMS
void cbm_activation_transaction_note_refusal_for_testing(const char *predicate,
unsigned long os_error);
+/* Linux only: stand home_root in for /home and owner_uid in for root when
+ * deciding whether /home or a /home/<name> entry is a trusted alias. NULL
+ * restores the production values. Inert elsewhere. */
+void cbm_activation_transaction_set_home_root_for_testing(const char *home_root,
+ unsigned long owner_uid);
++
++#if defined(__APPLE__) || defined(__linux__)
++/* Exercise the shared alias checks with synthetic link ownership and a real
++ * target. The caller frees the mapped path; NULL means the alias was refused. */
++char *cbm_activation_transaction_map_alias_for_testing(const char *link,
++ unsigned long link_owner_uid,
++ unsigned long trusted_owner_uid,
++ bool accept_own_target, const char *rest);
++#endif
++
+ void cbm_activation_transaction_write_failures_set_for_testing(unsigned int count, int os_error);
#endif
#endif /* CBM_ACTIVATION_TRANSACTION_H */
diff --cc tests/test_activation_transaction.c
index 28f185f56,b984145f5..3079551f0
--- a/tests/test_activation_transaction.c
+++ b/tests/test_activation_transaction.c
@@@ -764,95 -764,6 +764,172 @@@ TEST(activation_transaction_rejects_sym
PASS();
}
++#if (defined(__APPLE__) || defined(__linux__)) && defined(CBM_ENABLE_TEST_SEAMS)
++/* These tests reach the actual shared alias helper independently of the home
++ * root check. Only the link UID is substituted: resolving and inspecting the
++ * target still use the filesystem, without requiring privileged chown calls. */
++TEST(activation_transaction_alias_rejects_untrusted_link_owner) {
++ char root[ACTIVATION_TEST_PATH_CAP];
++ char target[ACTIVATION_TEST_PATH_CAP];
++ char link[ACTIVATION_TEST_PATH_CAP];
++ char resolved[4096];
++ char expected[4096];
++ ASSERT_TRUE(activation_test_fixture(root));
++ ASSERT_TRUE(activation_test_path(target, root, "target"));
++ ASSERT_TRUE(activation_test_path(link, root, "alias"));
++ ASSERT_TRUE(cbm_mkdir_p(target, 0700));
++ ASSERT_EQ(symlink(target, link), 0);
++ ASSERT_NOT_NULL(realpath(target, resolved));
++ ASSERT_TRUE(activation_test_path(expected, resolved, "bin/cbm"));
++ unsigned long self = (unsigned long)geteuid();
++ unsigned long other = self == 0 ? 1U : 0U;
++
++ char *trusted =
++ cbm_activation_transaction_map_alias_for_testing(link, self, self, false, "/bin/cbm");
++ char *untrusted =
++ cbm_activation_transaction_map_alias_for_testing(link, other, self, false, "/bin/cbm");
++ bool trusted_maps_target = trusted && strcmp(trusted, expected) == 0;
++ bool untrusted_refused = untrusted == NULL;
++ free(trusted);
++ free(untrusted);
++ int unlink_status = unlink(link);
++ int cleanup_status = th_rmtree(root);
++
++ ASSERT_TRUE(trusted_maps_target);
++ ASSERT_TRUE(untrusted_refused);
++ ASSERT_EQ(unlink_status, 0);
++ ASSERT_EQ(cleanup_status, 0);
++ PASS();
++}
++
++TEST(activation_transaction_alias_allows_own_target_only_when_enabled) {
++ char root[ACTIVATION_TEST_PATH_CAP];
++ char target[ACTIVATION_TEST_PATH_CAP];
++ char link[ACTIVATION_TEST_PATH_CAP];
++ char resolved[4096];
++ char expected[4096];
++ ASSERT_TRUE(activation_test_fixture(root));
++ ASSERT_TRUE(activation_test_path(target, root, "target"));
++ ASSERT_TRUE(activation_test_path(link, root, "alias"));
++ ASSERT_TRUE(cbm_mkdir_p(target, 0700));
++ ASSERT_EQ(symlink(target, link), 0);
++ ASSERT_NOT_NULL(realpath(target, resolved));
++ ASSERT_TRUE(activation_test_path(expected, resolved, "bin/cbm"));
++ unsigned long self = (unsigned long)geteuid();
++ unsigned long trusted_owner = self == 0 ? 1U : 0U;
++ struct stat target_status;
++ ASSERT_EQ(lstat(resolved, &target_status), 0);
++ ASSERT_EQ((unsigned long)target_status.st_uid, self);
++ ASSERT_TRUE((unsigned long)target_status.st_uid != trusted_owner);
++
++ char *strict = cbm_activation_transaction_map_alias_for_testing(
++ link, trusted_owner, trusted_owner, false, "/bin/cbm");
++ char *own = cbm_activation_transaction_map_alias_for_testing(link, trusted_owner, trusted_owner,
++ true, "/bin/cbm");
++ bool strict_refused = strict == NULL;
++ bool own_maps_target = own && strcmp(own, expected) == 0;
++ free(strict);
++ free(own);
++ int unlink_status = unlink(link);
++ int cleanup_status = th_rmtree(root);
++
++ ASSERT_TRUE(strict_refused);
++ ASSERT_TRUE(own_maps_target);
++ ASSERT_EQ(unlink_status, 0);
++ ASSERT_EQ(cleanup_status, 0);
++ PASS();
++}
++#endif
++
+/* #2306: managed Linux hosts keep /home a real directory and point the
+ * per-user entry elsewhere (/home/alice -> /local/home/alice). Root owns that
+ * entry, so it is trusted like the #2175 /home alias; the seam stands a
+ * fixture tree in for /home and the test account in for root. Every status is
+ * captured before the seam is reset so a failure cannot leak into the
+ * following tests. */
+TEST(activation_transaction_follows_trusted_per_user_home_entry) {
+#if defined(__linux__) && defined(CBM_ENABLE_TEST_SEAMS)
+ char root[ACTIVATION_TEST_PATH_CAP];
+ char home[ACTIVATION_TEST_PATH_CAP];
+ char entry[ACTIVATION_TEST_PATH_CAP];
+ char relocated[ACTIVATION_TEST_PATH_CAP];
+ char relocated_bin[ACTIVATION_TEST_PATH_CAP];
+ char relocated_target[ACTIVATION_TEST_PATH_CAP];
+ char target[ACTIVATION_TEST_PATH_CAP];
+ ASSERT_TRUE(activation_test_fixture(root));
+ ASSERT_TRUE(activation_test_path(home, root, "home"));
+ ASSERT_TRUE(activation_test_path(entry, home, "alice"));
+ ASSERT_TRUE(activation_test_path(relocated, root, "local-alice"));
+ ASSERT_TRUE(activation_test_path(relocated_bin, relocated, "bin"));
+ ASSERT_TRUE(activation_test_path(relocated_target, relocated_bin, "cbm"));
+ ASSERT_TRUE(activation_test_path(target, entry, "bin/cbm"));
+ ASSERT_TRUE(cbm_mkdir_p(home, 0755));
+ ASSERT_EQ(chmod(home, 0755), 0);
+ ASSERT_TRUE(cbm_mkdir_p(relocated_bin, 0700));
+ ASSERT_EQ(symlink(relocated, entry), 0);
+ unsigned long self = (unsigned long)geteuid();
+
+ /* Without the seam the entry is an ordinary user symlink: refused. */
+ cbm_activation_transaction_t *transaction = NULL;
+ cbm_activation_transaction_status_t untrusted_status = cbm_activation_transaction_stage_bytes(
+ target, "candidate", strlen("candidate"), &transaction);
+ bool untrusted_created = transaction != NULL;
+ (void)cbm_activation_transaction_close(&transaction);
+
- /* The home root and the entry must belong to the trusted owner. */
++ /* A home root belonging to another owner is refused before entry checks. */
+ cbm_activation_transaction_set_home_root_for_testing(home, self + 1U);
+ cbm_activation_transaction_status_t foreign_owner_status =
+ cbm_activation_transaction_stage_bytes(target, "candidate", strlen("candidate"),
+ &transaction);
+ bool foreign_owner_created = transaction != NULL;
+ (void)cbm_activation_transaction_close(&transaction);
+
+ /* A home root others can write could have its entry swapped: refused. */
+ cbm_activation_transaction_set_home_root_for_testing(home, self);
+ (void)chmod(home, 0775);
+ cbm_activation_transaction_status_t writable_home_status =
+ cbm_activation_transaction_stage_bytes(target, "candidate", strlen("candidate"),
+ &transaction);
+ bool writable_home_created = transaction != NULL;
+ (void)cbm_activation_transaction_close(&transaction);
+ (void)chmod(home, 0755);
+
+ /* Trusted root, trusted entry: staged and committed into the target. */
+ cbm_activation_transaction_status_t trusted_status = cbm_activation_transaction_stage_bytes(
+ target, "candidate", strlen("candidate"), &transaction);
+ cbm_activation_transaction_status_t commit_status =
+ transaction ? cbm_activation_transaction_commit(transaction, NULL, NULL)
+ : CBM_ACTIVATION_TRANSACTION_INVALID_STATE;
+ cbm_activation_transaction_status_t finalize_status =
+ transaction ? cbm_activation_transaction_finalize(transaction)
+ : CBM_ACTIVATION_TRANSACTION_INVALID_STATE;
+ cbm_activation_transaction_status_t close_status =
+ cbm_activation_transaction_close(&transaction);
+ cbm_activation_transaction_set_home_root_for_testing(NULL, 0);
+
+ char contents[ACTIVATION_TEST_CONTENT_CAP] = "";
+ bool published = activation_test_read(relocated_target, contents);
+ int unlink_status = unlink(entry);
+ int cleanup_status = th_rmtree(root);
+
+ ASSERT_EQ(untrusted_status, CBM_ACTIVATION_TRANSACTION_IO);
+ ASSERT_FALSE(untrusted_created);
+ ASSERT_EQ(foreign_owner_status, CBM_ACTIVATION_TRANSACTION_IO);
+ ASSERT_FALSE(foreign_owner_created);
+ ASSERT_EQ(writable_home_status, CBM_ACTIVATION_TRANSACTION_IO);
+ ASSERT_FALSE(writable_home_created);
+ ASSERT_EQ(trusted_status, CBM_ACTIVATION_TRANSACTION_OK);
+ ASSERT_EQ(commit_status, CBM_ACTIVATION_TRANSACTION_OK);
+ ASSERT_EQ(finalize_status, CBM_ACTIVATION_TRANSACTION_OK);
+ ASSERT_EQ(close_status, CBM_ACTIVATION_TRANSACTION_OK);
+ ASSERT_TRUE(published);
+ ASSERT_STR_EQ(contents, "candidate");
+ ASSERT_EQ(unlink_status, 0);
+ ASSERT_EQ(cleanup_status, 0);
+#endif
+ PASS();
+}
+
TEST(activation_transaction_fails_closed_if_target_directory_is_replaced) {
char root[ACTIVATION_TEST_PATH_CAP];
char active[ACTIVATION_TEST_PATH_CAP];
@@@ -1178,7 -1089,6 +1255,11 @@@ SUITE(activation_transaction)
RUN_TEST(activation_transaction_admits_group_writable_ancestor);
RUN_TEST(activation_transaction_rejects_windows_callback_allow_directory_ace);
RUN_TEST(activation_transaction_rejects_symlink_candidate_target_and_parent);
++#if (defined(__APPLE__) || defined(__linux__)) && defined(CBM_ENABLE_TEST_SEAMS)
++ RUN_TEST(activation_transaction_alias_rejects_untrusted_link_owner);
++ RUN_TEST(activation_transaction_alias_allows_own_target_only_when_enabled);
++#endif
+ RUN_TEST(activation_transaction_follows_trusted_per_user_home_entry);
RUN_TEST(activation_transaction_fails_closed_if_target_directory_is_replaced);
RUN_TEST(activation_transaction_does_not_replace_target_created_at_publish_boundary);
RUN_TEST(activation_transaction_rejects_macos_mutating_extended_acl);