@@ -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+
902957static 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 );
0 commit comments