@@ -421,7 +421,7 @@ static int ona_open_absolute_root(char *remaining, char *abspath, size_t abspath
421421/* Core walk. When out_abs is non-NULL and the path resolves to a directory
422422 * (O_DIRECTORY), the resolved absolute path is copied there -- owner_walk_parent
423423 * uses it to filter-check the (otherwise unchecked) leaf basename. */
424- static int ona_open (const char * path , int flags , mode_t mode , char * out_abs , size_t out_cap )
424+ static int ona_open (const char * path , int flags , mode_t mode , char * out_abs , size_t out_cap , int enforce_root )
425425{
426426#if defined AT_FDCWD && defined O_NOFOLLOW && defined O_DIRECTORY
427427 const int dir_traverse_flags = directory_traverse_flags () | O_CLOEXEC ;
@@ -446,7 +446,7 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz
446446 char abspath [MAXPATHLEN ] = {0 };
447447 if (am_daemon && module_dir && module_dir [0 ] == '/' )
448448 strlcpy (abspath , module_dir , sizeof abspath ); /* "/" for a path=/ module */
449- else if (confine_root ) {
449+ else if (enforce_root && confine_root ) {
450450 /* Unlike a daemon's, this cwd is not pinned to the root -- the receiver
451451 * chdir's into the destination -- so it has to be read, not assumed.
452452 * It must be the PHYSICAL cwd: curr_dir is the lexical name change_dir()
@@ -472,7 +472,7 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz
472472 * following the link restarts the walk at its absolute target, and every
473473 * component of THAT is checked, so a pin aimed outside is still refused. */
474474 const char * ptail = fd_pin_tail (path );
475- int pin_transit = !am_daemon && confine_root && ptail != NULL ;
475+ int pin_transit = enforce_root && !am_daemon && confine_root && ptail != NULL ;
476476
477477 /* Path-walk state. `remaining` is the unconsumed tail; we splice
478478 * symlink targets back into it as we go. Sized 2x MAXPATHLEN so a
@@ -534,7 +534,7 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz
534534 saved_errno = errno ;
535535 goto out ;
536536 }
537- if (!pin_transit && abspath_outside_confinement (abspath , 1 )) {
537+ if (enforce_root && !pin_transit && abspath_outside_confinement (abspath , 1 )) {
538538 saved_errno = ELOOP ;
539539 goto out ;
540540 }
@@ -664,7 +664,7 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz
664664 saved_errno = errno ;
665665 goto out ;
666666 }
667- if (!pin_transit && abspath_outside_confinement (abspath , 1 )) {
667+ if (enforce_root && !pin_transit && abspath_outside_confinement (abspath , 1 )) {
668668 saved_errno = ELOOP ;
669669 goto out ;
670670 }
@@ -688,7 +688,7 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz
688688 saved_errno = errno ;
689689 goto out ;
690690 }
691- if (!pin_transit && abspath_outside_confinement (abspath , 0 )) {
691+ if (enforce_root && !pin_transit && abspath_outside_confinement (abspath , 0 )) {
692692 saved_errno = ELOOP ;
693693 goto out ;
694694 }
@@ -716,7 +716,7 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz
716716 * an O_PATH fd is sufficient for traversal and fchdir but not operations
717717 * such as fchmod. */
718718 if (flags & O_DIRECTORY ) {
719- if (!pin_transit && abspath_outside_confinement (abspath , 1 )) {
719+ if (enforce_root && !pin_transit && abspath_outside_confinement (abspath , 1 )) {
720720 saved_errno = ELOOP ;
721721 goto out ;
722722 }
@@ -737,21 +737,28 @@ static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, siz
737737#else
738738 /* Pre-AT_FDCWD / no O_NOFOLLOW systems: best-effort fallback. */
739739 (void )out_abs ; (void )out_cap ;
740+ (void )enforce_root ;
740741 return open (path , flags , mode );
741742#endif
742743}
743744
744745int open_no_attacker_symlinks (const char * path , int flags , mode_t mode )
745746{
746- return ona_open (path , flags , mode , NULL , 0 );
747+ return ona_open (path , flags , mode , NULL , 0 , 1 );
748+ }
749+
750+ /* A log's ownership check must not inherit file-list confinement. */
751+ int open_log_file (const char * path , int flags , mode_t mode )
752+ {
753+ return ona_open (path , flags , mode , NULL , 0 , 0 );
747754}
748755
749756/* Open a directory for traversal or as *at()/fchdir() authority. Unlike an
750757 * O_RDONLY directory endpoint, this accepts a searchable but unreadable
751758 * directory on Linux. */
752759int open_no_attacker_symlinks_dirfd (const char * path )
753760{
754- return ona_open (path , directory_traverse_flags (), 0 , NULL , 0 );
761+ return ona_open (path , directory_traverse_flags (), 0 , NULL , 0 , 1 );
755762}
756763
757764/* When set, the do_*_at() wrappers resolve their path as an OPERATOR-supplied
@@ -780,7 +787,7 @@ int owner_walk_parent(const char *path, const char **bname)
780787 * bname = slash ? slash + 1 : path ;
781788 pabs [0 ] = '\0' ;
782789 if (!slash )
783- dfd = ona_open ("." , directory_traverse_flags (), 0 , pabs , sizeof pabs );
790+ dfd = ona_open ("." , directory_traverse_flags (), 0 , pabs , sizeof pabs , 1 );
784791 else {
785792 dlen = slash == path ? 1 : (size_t )(slash - path ); /* "/x" -> parent "/" */
786793 if (dlen >= sizeof dir ) {
@@ -789,7 +796,7 @@ int owner_walk_parent(const char *path, const char **bname)
789796 }
790797 memcpy (dir , path , dlen );
791798 dir [dlen ] = '\0' ;
792- dfd = ona_open (dir , directory_traverse_flags (), 0 , pabs , sizeof pabs );
799+ dfd = ona_open (dir , directory_traverse_flags (), 0 , pabs , sizeof pabs , 1 );
793800 }
794801 if (dfd < 0 )
795802 return -1 ;
0 commit comments