@@ -561,6 +561,13 @@ bool cbm_mkdir_p(const char *path, int mode) {
561561 return ok ;
562562}
563563
564+ bool cbm_mkdir_p_ex (const char * path , int mode , unsigned int policy ) {
565+ /* The Windows walk has its own reparse-point policy (see
566+ * cbm_windows_mkdir_component); the POSIX symlink policy does not apply. */
567+ (void )policy ;
568+ return cbm_mkdir_p (path , mode );
569+ }
570+
564571int cbm_unlink (const char * path ) {
565572 wchar_t * wpath = cbm_path_to_wide (path );
566573 if (!wpath ) {
@@ -841,24 +848,131 @@ FILE *cbm_fopen(const char *path, const char *mode) {
841848 return fopen (path , mode );
842849}
843850
844- static int cbm_open_directory_component (int parent , const char * component , int flags ) {
851+ /* Symlink policy for the parent-chain walk. Every component is opened with
852+ * O_NOFOLLOW; a symlink is followed only when its OWNER is trusted, never by
853+ * default. Root-owned links are trusted everywhere (distro /home indirection,
854+ * macOS /tmp: only root can create those, so they are outside the attacker
855+ * model). A link owned by the invoking account is trusted only where the
856+ * caller opted in with CBM_MKDIR_FOLLOW_OWNED: for a path rooted in the user's
857+ * own configuration such a link is the user's own arrangement (a dotfile
858+ * manager, ~/.config/opencode -> /mnt/...), and refusing it made every
859+ * agent-config write under such a root fail with an opaque agent_config
860+ * error. It is not trusted for a path derived from a repository, where git
861+ * creates symlinks owned by whoever cloned, so "user-owned" says nothing about
862+ * "user-intended". The rule is the one the Linux kernel's
863+ * fs.protected_symlinks applies, and the ancestor policy the activation
864+ * transaction already uses. A link owned by any OTHER account (planted in a
865+ * group- or world-writable ancestor) stays refused, and a privileged walk
866+ * (euid 0) still refuses user-owned links.
867+ *
868+ * Inspecting the link and following it are two steps, so what the follow
869+ * lands on is checked as well: the opened target must be a directory owned by
870+ * root or the invoking user, and not world-writable unless sticky. An account
871+ * that can write the parent cannot steer the walk into a directory it
872+ * controls or into one where anyone can pre-plant entries, and because the
873+ * judgement and the follow are bound to one inode (cbm_read_trusted_link) it
874+ * cannot substitute a link of its own between them either. */
875+ static bool cbm_walk_link_trusted (uid_t owner , bool follow_owned ) {
876+ return owner == 0U || (follow_owned && owner == geteuid ());
877+ }
878+
879+ static bool cbm_walk_target_trusted (const struct stat * target ) {
880+ bool trusted_owner = target -> st_uid == 0U || target -> st_uid == geteuid ();
881+ bool world_writable = (target -> st_mode & S_IWOTH ) != 0 ;
882+ bool sticky = (target -> st_mode & S_ISVTX ) != 0 ;
883+ return S_ISDIR (target -> st_mode ) && trusted_owner && (!world_writable || sticky );
884+ }
885+
886+ /* Read the target text of the symlink at `component`, but only if the link's
887+ * owner is trusted -- and read it from the SAME inode the judgement was made
888+ * on. Judging by name and then opening by name is a check-then-use pair: an
889+ * account that can write the parent could swap the entry in between, so the
890+ * link that gets followed is never the one that was judged (demonstrated
891+ * against an earlier head with an LD_PRELOAD shim). The link is therefore
892+ * never opened by name after the judgement: on Linux an O_PATH|O_NOFOLLOW
893+ * descriptor pins the inode, and both the fstat and the readlinkat operate on
894+ * it; elsewhere the link is stat'ed by name before and after the readlinkat
895+ * and both must be the same inode with the same owner. What is followed
896+ * afterwards is the text this function returns, resolved from the parent. */
897+ static bool cbm_read_trusted_link (int parent , const char * component , bool follow_owned , char * text ,
898+ size_t text_size ) {
899+ ssize_t length = 0 ; /* nothing read yet: refused below unless a read succeeds */
900+ #if defined(__linux__ ) && defined(O_PATH )
901+ int link = openat (parent , component , O_PATH | O_NOFOLLOW | O_CLOEXEC );
902+ if (link < 0 ) {
903+ return false;
904+ }
905+ struct stat state ;
906+ if (fstat (link , & state ) == 0 && S_ISLNK (state .st_mode ) &&
907+ cbm_walk_link_trusted (state .st_uid , follow_owned )) {
908+ /* An empty path names the link the descriptor itself refers to. */
909+ length = readlinkat (link , "" , text , text_size );
910+ }
911+ (void )close (link );
912+ #else
913+ struct stat before ;
914+ struct stat after ;
915+ if (fstatat (parent , component , & before , AT_SYMLINK_NOFOLLOW ) != 0 || !S_ISLNK (before .st_mode ) ||
916+ !cbm_walk_link_trusted (before .st_uid , follow_owned )) {
917+ return false;
918+ }
919+ length = readlinkat (parent , component , text , text_size );
920+ if (fstatat (parent , component , & after , AT_SYMLINK_NOFOLLOW ) != 0 || !S_ISLNK (after .st_mode ) ||
921+ after .st_dev != before .st_dev || after .st_ino != before .st_ino ||
922+ after .st_uid != before .st_uid ) {
923+ return false;
924+ }
925+ #endif
926+ /* Empty or truncated text is refused rather than guessed at. */
927+ if (length <= 0 || (size_t )length >= text_size ) {
928+ return false;
929+ }
930+ text [length ] = '\0' ;
931+ return true;
932+ }
933+
934+ static int cbm_open_directory_component (int parent , const char * component , int flags ,
935+ bool follow_owned ) {
845936 int descriptor = openat (parent , component , flags );
846937#if defined(O_NOFOLLOW ) && defined(AT_SYMLINK_NOFOLLOW )
847938 if (descriptor < 0 ) {
848- struct stat state ;
849- if (fstatat (parent , component , & state , AT_SYMLINK_NOFOLLOW ) == 0 &&
850- S_ISLNK (state .st_mode ) && state .st_uid == 0U ) {
851- descriptor = openat (parent , component , flags & ~O_NOFOLLOW );
939+ /* The caller decides on errno from the FIRST open (ENOENT means
940+ * "create it"); a refused link must not leak a later call's errno. */
941+ int open_errno = errno ;
942+ char text [CBM_SZ_4K ];
943+ if (cbm_read_trusted_link (parent , component , follow_owned , text , sizeof (text ))) {
944+ /* The judged link's own text, resolved from the parent exactly as
945+ * the kernel would resolve it (relative texts against the link's
946+ * directory). Links inside the text are resolved by the kernel as
947+ * before; the target check bounds where the walk lands. */
948+ int followed = openat (parent , text , flags & ~O_NOFOLLOW );
949+ struct stat target ;
950+ if (followed >= 0 && fstat (followed , & target ) == 0 &&
951+ cbm_walk_target_trusted (& target )) {
952+ descriptor = followed ;
953+ } else if (followed >= 0 ) {
954+ (void )close (followed );
955+ }
956+ }
957+ if (descriptor < 0 ) {
958+ errno = open_errno ;
852959 }
853960 }
961+ #else
962+ (void )follow_owned ;
854963#endif
855964 return descriptor ;
856965}
857966
858967bool cbm_mkdir_p (const char * path , int mode ) {
968+ return cbm_mkdir_p_ex (path , mode , 0U );
969+ }
970+
971+ bool cbm_mkdir_p_ex (const char * path , int mode , unsigned int policy ) {
859972 if (!path || path [0 ] == '\0' ) {
860973 return false;
861974 }
975+ bool follow_owned = (policy & CBM_MKDIR_FOLLOW_OWNED ) != 0U ;
862976 char * tmp = strdup (path );
863977 if (!tmp ) {
864978 return false;
@@ -891,12 +1005,12 @@ bool cbm_mkdir_p(const char *path, int mode) {
8911005 * separator = '\0' ;
8921006 }
8931007 if (cursor [0 ] != '\0' && strcmp (cursor , "." ) != 0 ) {
894- int next = cbm_open_directory_component (directory , cursor , flags );
1008+ int next = cbm_open_directory_component (directory , cursor , flags , follow_owned );
8951009 if (next < 0 && errno == ENOENT ) {
8961010 if (mkdirat (directory , cursor , (mode_t )mode ) != 0 && errno != EEXIST ) {
8971011 ok = false;
8981012 } else {
899- next = cbm_open_directory_component (directory , cursor , flags );
1013+ next = cbm_open_directory_component (directory , cursor , flags , follow_owned );
9001014 }
9011015 }
9021016 if (ok && next < 0 ) {
0 commit comments