Skip to content

Commit 635f8e3

Browse files
authored
Merge pull request DeusData#2309 from BumaldaOverTheWater94/fix/home-user-symlink-alias
fix(cli): trust root-owned per-user /home entries during activation walk
2 parents 2796460 + 7716153 commit 635f8e3

4 files changed

Lines changed: 276 additions & 20 deletions

File tree

‎scripts/memory-core-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal/cbm/lsp/perl_lsp.c 21
1313
internal/cbm/macro_table.c 1
1414
internal/cbm/service_patterns.c 2
1515
internal/cbm/sqlite_writer.c 74
16-
src/cli/activation_transaction.c 80
16+
src/cli/activation_transaction.c 78
1717
src/cli/agent_clients.c 73
1818
src/cli/agent_profiles.c 7
1919
src/cli/cli.c 166

‎src/cli/activation_transaction.c‎

Lines changed: 89 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -899,14 +899,69 @@ static bool activation_posix_acl_empty(int descriptor) {
899899
return cbm_macos_extended_acl_fd_is_empty(descriptor);
900900
}
901901

902+
#ifdef __linux__
903+
/* Root of the per-user home tree and the account trusted to own it. Root and
904+
* "/home" in production; the test seam substitutes a fixture tree and the test
905+
* account, because the real layout needs root-owned entries to reproduce. */
906+
static const char *g_activation_home_root = "/home";
907+
static uid_t g_activation_home_owner = 0;
908+
909+
#ifdef CBM_ENABLE_TEST_SEAMS
910+
void cbm_activation_transaction_set_home_root_for_testing(const char *home_root,
911+
unsigned long owner_uid) {
912+
g_activation_home_root = home_root ? home_root : "/home";
913+
g_activation_home_owner = home_root ? (uid_t)owner_uid : 0;
914+
}
915+
#endif
916+
#endif
917+
918+
#if defined(__APPLE__) || defined(__linux__)
919+
/* Map a trusted symlink `link` (already lstat'd) onto its resolved target and
920+
* append `rest`. The link and the target must both belong to `owner`; with
921+
* accept_own_target the target may instead belong to the current account.
922+
* Returns NULL when either check fails or the mapped path does not fit. */
923+
static char *activation_alias_map(const char *link, const struct stat *link_status, uid_t owner,
924+
bool accept_own_target, const char *rest) {
925+
char resolved[4096];
926+
struct stat resolved_status;
927+
if (link_status->st_uid != owner || !realpath(link, resolved) ||
928+
lstat(resolved, &resolved_status) != 0 || !S_ISDIR(resolved_status.st_mode)) {
929+
return NULL;
930+
}
931+
if (resolved_status.st_uid != owner &&
932+
!(accept_own_target && resolved_status.st_uid == geteuid())) {
933+
return NULL;
934+
}
935+
char mapped[4096];
936+
int written = snprintf(mapped, sizeof(mapped), "%s%s", resolved, rest);
937+
if (written <= 0 || (size_t)written >= sizeof(mapped)) {
938+
return NULL;
939+
}
940+
return activation_string_copy(mapped);
941+
}
942+
943+
#ifdef CBM_ENABLE_TEST_SEAMS
944+
char *cbm_activation_transaction_map_alias_for_testing(const char *link,
945+
unsigned long link_owner_uid,
946+
unsigned long trusted_owner_uid,
947+
bool accept_own_target, const char *rest) {
948+
/* Only link ownership is synthetic; the shared production helper resolves
949+
* and checks the real target so distinct-owner cases need no chown/root. */
950+
const struct stat link_status = {.st_uid = (uid_t)link_owner_uid};
951+
return activation_alias_map(link, &link_status, (uid_t)trusted_owner_uid, accept_own_target,
952+
rest);
953+
}
954+
#endif
955+
#endif
956+
902957
static char *activation_posix_walk_path(const char *directory) {
903958
#if defined(__APPLE__) || defined(__linux__)
904959
/* macOS and immutable Linux layouts can expose writable trees through
905960
* root-owned aliases (for example /tmp and /home on Atomic systems).
906961
* Resolve only these trusted system aliases; arbitrary user symlinks must
907962
* still fail the O_NOFOLLOW walk below. */
908963
#ifdef __linux__
909-
static const char *const aliases[] = {"/tmp", "/var", "/home"};
964+
const char *const aliases[] = {"/tmp", "/var", g_activation_home_root};
910965
#else
911966
static const char *const aliases[] = {"/tmp", "/var"};
912967
#endif
@@ -917,33 +972,48 @@ static char *activation_posix_walk_path(const char *directory) {
917972
(directory[alias_length] != '\0' && directory[alias_length] != '/')) {
918973
continue;
919974
}
975+
uid_t owner = 0;
976+
#ifdef __linux__
977+
bool is_home = alias == g_activation_home_root;
978+
if (is_home) {
979+
owner = g_activation_home_owner;
980+
}
981+
#endif
920982
struct stat alias_status;
921-
char resolved[4096];
922983
if (lstat(alias, &alias_status) != 0) {
923984
continue;
924985
}
925-
if (!S_ISLNK(alias_status.st_mode)) {
926-
continue;
927-
}
928-
if (alias_status.st_uid != 0 || !realpath(alias, resolved)) {
929-
return NULL;
986+
if (S_ISLNK(alias_status.st_mode)) {
987+
return activation_alias_map(alias, &alias_status, owner, false,
988+
directory + alias_length);
930989
}
931-
struct stat resolved_status;
932-
if (lstat(resolved, &resolved_status) != 0 || !S_ISDIR(resolved_status.st_mode) ||
933-
resolved_status.st_uid != 0) {
934-
return NULL;
990+
#ifdef __linux__
991+
/* Managed Linux hosts often keep /home a real directory and point the
992+
* per-user entry elsewhere (/home/alice -> /local/home/alice, #2306).
993+
* Trust that entry only inside a root-owned /home nobody else can
994+
* write, and only when root owns the entry itself. Unlike the whole-
995+
* alias case above, the target may belong to the current account:
996+
* that is the normal shape of a relocated home, and an account can
997+
* already write its own home, so it gains nothing it did not have. */
998+
const char *name = directory + alias_length;
999+
size_t name_length = *name == '/' ? strcspn(name + 1, "/") : 0;
1000+
if (!is_home || !S_ISDIR(alias_status.st_mode) || alias_status.st_uid != owner ||
1001+
(alias_status.st_mode & 0022) != 0 || name_length == 0) {
1002+
continue;
9351003
}
936-
size_t needed = strlen(resolved) + strlen(directory + alias_length) + 1U;
937-
char *mapped = malloc(needed);
938-
if (!mapped) {
1004+
char entry[4096];
1005+
size_t entry_length = alias_length + 1U + name_length;
1006+
if (entry_length >= sizeof(entry)) {
9391007
return NULL;
9401008
}
941-
int written = snprintf(mapped, needed, "%s%s", resolved, directory + alias_length);
942-
if (written <= 0 || (size_t)written >= needed) {
943-
free(mapped);
944-
return NULL;
1009+
memcpy(entry, directory, entry_length);
1010+
entry[entry_length] = '\0';
1011+
struct stat entry_status;
1012+
if (lstat(entry, &entry_status) == 0 && S_ISLNK(entry_status.st_mode)) {
1013+
return activation_alias_map(entry, &entry_status, owner, true,
1014+
directory + entry_length);
9451015
}
946-
return mapped;
1016+
#endif
9471017
}
9481018
#endif
9491019
return activation_string_copy(directory);

‎src/cli/activation_transaction.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ const char *cbm_activation_transaction_refusal_note(void);
104104
#ifdef CBM_ENABLE_TEST_SEAMS
105105
void cbm_activation_transaction_note_refusal_for_testing(const char *predicate,
106106
unsigned long os_error);
107+
/* Linux only: stand home_root in for /home and owner_uid in for root when
108+
* deciding whether /home or a /home/<name> entry is a trusted alias. NULL
109+
* restores the production values. Inert elsewhere. */
110+
void cbm_activation_transaction_set_home_root_for_testing(const char *home_root,
111+
unsigned long owner_uid);
112+
113+
#if defined(__APPLE__) || defined(__linux__)
114+
/* Exercise the shared alias checks with synthetic link ownership and a real
115+
* target. The caller frees the mapped path; NULL means the alias was refused. */
116+
char *cbm_activation_transaction_map_alias_for_testing(const char *link,
117+
unsigned long link_owner_uid,
118+
unsigned long trusted_owner_uid,
119+
bool accept_own_target, const char *rest);
120+
#endif
121+
107122
void cbm_activation_transaction_write_failures_set_for_testing(unsigned int count, int os_error);
108123
#endif
109124

‎tests/test_activation_transaction.c‎

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,172 @@ TEST(activation_transaction_rejects_symlink_candidate_target_and_parent) {
764764
PASS();
765765
}
766766

767+
#if (defined(__APPLE__) || defined(__linux__)) && defined(CBM_ENABLE_TEST_SEAMS)
768+
/* These tests reach the actual shared alias helper independently of the home
769+
* root check. Only the link UID is substituted: resolving and inspecting the
770+
* target still use the filesystem, without requiring privileged chown calls. */
771+
TEST(activation_transaction_alias_rejects_untrusted_link_owner) {
772+
char root[ACTIVATION_TEST_PATH_CAP];
773+
char target[ACTIVATION_TEST_PATH_CAP];
774+
char link[ACTIVATION_TEST_PATH_CAP];
775+
char resolved[4096];
776+
char expected[4096];
777+
ASSERT_TRUE(activation_test_fixture(root));
778+
ASSERT_TRUE(activation_test_path(target, root, "target"));
779+
ASSERT_TRUE(activation_test_path(link, root, "alias"));
780+
ASSERT_TRUE(cbm_mkdir_p(target, 0700));
781+
ASSERT_EQ(symlink(target, link), 0);
782+
ASSERT_NOT_NULL(realpath(target, resolved));
783+
ASSERT_TRUE(activation_test_path(expected, resolved, "bin/cbm"));
784+
unsigned long self = (unsigned long)geteuid();
785+
unsigned long other = self == 0 ? 1U : 0U;
786+
787+
char *trusted =
788+
cbm_activation_transaction_map_alias_for_testing(link, self, self, false, "/bin/cbm");
789+
char *untrusted =
790+
cbm_activation_transaction_map_alias_for_testing(link, other, self, false, "/bin/cbm");
791+
bool trusted_maps_target = trusted && strcmp(trusted, expected) == 0;
792+
bool untrusted_refused = untrusted == NULL;
793+
free(trusted);
794+
free(untrusted);
795+
int unlink_status = unlink(link);
796+
int cleanup_status = th_rmtree(root);
797+
798+
ASSERT_TRUE(trusted_maps_target);
799+
ASSERT_TRUE(untrusted_refused);
800+
ASSERT_EQ(unlink_status, 0);
801+
ASSERT_EQ(cleanup_status, 0);
802+
PASS();
803+
}
804+
805+
TEST(activation_transaction_alias_allows_own_target_only_when_enabled) {
806+
char root[ACTIVATION_TEST_PATH_CAP];
807+
char target[ACTIVATION_TEST_PATH_CAP];
808+
char link[ACTIVATION_TEST_PATH_CAP];
809+
char resolved[4096];
810+
char expected[4096];
811+
ASSERT_TRUE(activation_test_fixture(root));
812+
ASSERT_TRUE(activation_test_path(target, root, "target"));
813+
ASSERT_TRUE(activation_test_path(link, root, "alias"));
814+
ASSERT_TRUE(cbm_mkdir_p(target, 0700));
815+
ASSERT_EQ(symlink(target, link), 0);
816+
ASSERT_NOT_NULL(realpath(target, resolved));
817+
ASSERT_TRUE(activation_test_path(expected, resolved, "bin/cbm"));
818+
unsigned long self = (unsigned long)geteuid();
819+
unsigned long trusted_owner = self == 0 ? 1U : 0U;
820+
struct stat target_status;
821+
ASSERT_EQ(lstat(resolved, &target_status), 0);
822+
ASSERT_EQ((unsigned long)target_status.st_uid, self);
823+
ASSERT_TRUE((unsigned long)target_status.st_uid != trusted_owner);
824+
825+
char *strict = cbm_activation_transaction_map_alias_for_testing(
826+
link, trusted_owner, trusted_owner, false, "/bin/cbm");
827+
char *own = cbm_activation_transaction_map_alias_for_testing(link, trusted_owner, trusted_owner,
828+
true, "/bin/cbm");
829+
bool strict_refused = strict == NULL;
830+
bool own_maps_target = own && strcmp(own, expected) == 0;
831+
free(strict);
832+
free(own);
833+
int unlink_status = unlink(link);
834+
int cleanup_status = th_rmtree(root);
835+
836+
ASSERT_TRUE(strict_refused);
837+
ASSERT_TRUE(own_maps_target);
838+
ASSERT_EQ(unlink_status, 0);
839+
ASSERT_EQ(cleanup_status, 0);
840+
PASS();
841+
}
842+
#endif
843+
844+
/* #2306: managed Linux hosts keep /home a real directory and point the
845+
* per-user entry elsewhere (/home/alice -> /local/home/alice). Root owns that
846+
* entry, so it is trusted like the #2175 /home alias; the seam stands a
847+
* fixture tree in for /home and the test account in for root. Every status is
848+
* captured before the seam is reset so a failure cannot leak into the
849+
* following tests. */
850+
TEST(activation_transaction_follows_trusted_per_user_home_entry) {
851+
#if defined(__linux__) && defined(CBM_ENABLE_TEST_SEAMS)
852+
char root[ACTIVATION_TEST_PATH_CAP];
853+
char home[ACTIVATION_TEST_PATH_CAP];
854+
char entry[ACTIVATION_TEST_PATH_CAP];
855+
char relocated[ACTIVATION_TEST_PATH_CAP];
856+
char relocated_bin[ACTIVATION_TEST_PATH_CAP];
857+
char relocated_target[ACTIVATION_TEST_PATH_CAP];
858+
char target[ACTIVATION_TEST_PATH_CAP];
859+
ASSERT_TRUE(activation_test_fixture(root));
860+
ASSERT_TRUE(activation_test_path(home, root, "home"));
861+
ASSERT_TRUE(activation_test_path(entry, home, "alice"));
862+
ASSERT_TRUE(activation_test_path(relocated, root, "local-alice"));
863+
ASSERT_TRUE(activation_test_path(relocated_bin, relocated, "bin"));
864+
ASSERT_TRUE(activation_test_path(relocated_target, relocated_bin, "cbm"));
865+
ASSERT_TRUE(activation_test_path(target, entry, "bin/cbm"));
866+
ASSERT_TRUE(cbm_mkdir_p(home, 0755));
867+
ASSERT_EQ(chmod(home, 0755), 0);
868+
ASSERT_TRUE(cbm_mkdir_p(relocated_bin, 0700));
869+
ASSERT_EQ(symlink(relocated, entry), 0);
870+
unsigned long self = (unsigned long)geteuid();
871+
872+
/* Without the seam the entry is an ordinary user symlink: refused. */
873+
cbm_activation_transaction_t *transaction = NULL;
874+
cbm_activation_transaction_status_t untrusted_status = cbm_activation_transaction_stage_bytes(
875+
target, "candidate", strlen("candidate"), &transaction);
876+
bool untrusted_created = transaction != NULL;
877+
(void)cbm_activation_transaction_close(&transaction);
878+
879+
/* A home root belonging to another owner is refused before entry checks. */
880+
cbm_activation_transaction_set_home_root_for_testing(home, self + 1U);
881+
cbm_activation_transaction_status_t foreign_owner_status =
882+
cbm_activation_transaction_stage_bytes(target, "candidate", strlen("candidate"),
883+
&transaction);
884+
bool foreign_owner_created = transaction != NULL;
885+
(void)cbm_activation_transaction_close(&transaction);
886+
887+
/* A home root others can write could have its entry swapped: refused. */
888+
cbm_activation_transaction_set_home_root_for_testing(home, self);
889+
(void)chmod(home, 0775);
890+
cbm_activation_transaction_status_t writable_home_status =
891+
cbm_activation_transaction_stage_bytes(target, "candidate", strlen("candidate"),
892+
&transaction);
893+
bool writable_home_created = transaction != NULL;
894+
(void)cbm_activation_transaction_close(&transaction);
895+
(void)chmod(home, 0755);
896+
897+
/* Trusted root, trusted entry: staged and committed into the target. */
898+
cbm_activation_transaction_status_t trusted_status = cbm_activation_transaction_stage_bytes(
899+
target, "candidate", strlen("candidate"), &transaction);
900+
cbm_activation_transaction_status_t commit_status =
901+
transaction ? cbm_activation_transaction_commit(transaction, NULL, NULL)
902+
: CBM_ACTIVATION_TRANSACTION_INVALID_STATE;
903+
cbm_activation_transaction_status_t finalize_status =
904+
transaction ? cbm_activation_transaction_finalize(transaction)
905+
: CBM_ACTIVATION_TRANSACTION_INVALID_STATE;
906+
cbm_activation_transaction_status_t close_status =
907+
cbm_activation_transaction_close(&transaction);
908+
cbm_activation_transaction_set_home_root_for_testing(NULL, 0);
909+
910+
char contents[ACTIVATION_TEST_CONTENT_CAP] = "";
911+
bool published = activation_test_read(relocated_target, contents);
912+
int unlink_status = unlink(entry);
913+
int cleanup_status = th_rmtree(root);
914+
915+
ASSERT_EQ(untrusted_status, CBM_ACTIVATION_TRANSACTION_IO);
916+
ASSERT_FALSE(untrusted_created);
917+
ASSERT_EQ(foreign_owner_status, CBM_ACTIVATION_TRANSACTION_IO);
918+
ASSERT_FALSE(foreign_owner_created);
919+
ASSERT_EQ(writable_home_status, CBM_ACTIVATION_TRANSACTION_IO);
920+
ASSERT_FALSE(writable_home_created);
921+
ASSERT_EQ(trusted_status, CBM_ACTIVATION_TRANSACTION_OK);
922+
ASSERT_EQ(commit_status, CBM_ACTIVATION_TRANSACTION_OK);
923+
ASSERT_EQ(finalize_status, CBM_ACTIVATION_TRANSACTION_OK);
924+
ASSERT_EQ(close_status, CBM_ACTIVATION_TRANSACTION_OK);
925+
ASSERT_TRUE(published);
926+
ASSERT_STR_EQ(contents, "candidate");
927+
ASSERT_EQ(unlink_status, 0);
928+
ASSERT_EQ(cleanup_status, 0);
929+
#endif
930+
PASS();
931+
}
932+
767933
TEST(activation_transaction_fails_closed_if_target_directory_is_replaced) {
768934
char root[ACTIVATION_TEST_PATH_CAP];
769935
char active[ACTIVATION_TEST_PATH_CAP];
@@ -1089,6 +1255,11 @@ SUITE(activation_transaction) {
10891255
RUN_TEST(activation_transaction_admits_group_writable_ancestor);
10901256
RUN_TEST(activation_transaction_rejects_windows_callback_allow_directory_ace);
10911257
RUN_TEST(activation_transaction_rejects_symlink_candidate_target_and_parent);
1258+
#if (defined(__APPLE__) || defined(__linux__)) && defined(CBM_ENABLE_TEST_SEAMS)
1259+
RUN_TEST(activation_transaction_alias_rejects_untrusted_link_owner);
1260+
RUN_TEST(activation_transaction_alias_allows_own_target_only_when_enabled);
1261+
#endif
1262+
RUN_TEST(activation_transaction_follows_trusted_per_user_home_entry);
10921263
RUN_TEST(activation_transaction_fails_closed_if_target_directory_is_replaced);
10931264
RUN_TEST(activation_transaction_does_not_replace_target_created_at_publish_boundary);
10941265
RUN_TEST(activation_transaction_rejects_macos_mutating_extended_acl);

0 commit comments

Comments
 (0)