@@ -117,7 +117,8 @@ fn read(settings: &Settings, now: i64) -> Snapshot {
117117 if !( 1 ..=300 ) . contains ( & settings. max_age_seconds ) {
118118 return Snapshot :: UNAVAILABLE ;
119119 }
120- let Some ( value) = observation ( path) else {
120+ // The observation must come from root or this process's effective user.
121+ let Some ( value) = observation ( path, unsafe { libc:: geteuid ( ) } ) else {
121122 return Snapshot :: UNAVAILABLE ;
122123 } ;
123124 let state = match value. get ( "state" ) . and_then ( Value :: as_str) {
@@ -146,16 +147,19 @@ fn read(settings: &Settings, now: i64) -> Snapshot {
146147 }
147148}
148149
149- /// A bounded regular file, never a FIFO, device or symlink, nor one another
150- /// user could replace. The final path component is opened without following
150+ /// A bounded regular file owned by root or the reader, never a FIFO, device
151+ /// or symlink, nor writable by another user. Trusted parent directories remain
152+ /// an operator requirement. The final path component is opened without following
151153/// a symlink and without blocking on a FIFO, and every check is made on that
152154/// same descriptor, so nothing can be swapped in between a check and the read.
153- fn observation ( path : & str ) -> Option < Value > {
155+ fn observation ( path : & str , reader_uid : u32 ) -> Option < Value > {
154156 use std:: os:: unix:: fs:: OpenOptionsExt ;
155157 let mut file = std:: fs:: OpenOptions :: new ( ) . read ( true )
156158 . custom_flags ( libc:: O_NOFOLLOW | libc:: O_NONBLOCK | libc:: O_CLOEXEC ) . open ( path) . ok ( ) ?;
157159 let opened = file. metadata ( ) . ok ( ) ?;
158- if !opened. file_type ( ) . is_file ( ) || opened. len ( ) > MAX_BYTES || opened. mode ( ) & 0o022 != 0 {
160+ if !opened. file_type ( ) . is_file ( ) || opened. len ( ) > MAX_BYTES || opened. mode ( ) & 0o022 != 0
161+ || ( opened. uid ( ) != 0 && opened. uid ( ) != reader_uid)
162+ {
159163 return None ;
160164 }
161165 let mut bytes = Vec :: with_capacity ( MAX_BYTES as usize ) ;
@@ -338,6 +342,40 @@ mod tests {
338342 unavailable ( & Settings { max_age_seconds : 301 , ..base. clone ( ) } ) ;
339343 }
340344
345+ #[ test]
346+ fn observation_owner_must_be_root_or_the_reader ( ) {
347+ let directory = tempfile:: tempdir ( ) . unwrap ( ) ;
348+ let path = directory. path ( ) . join ( "pressure.json" ) ;
349+ let path_text = path. to_str ( ) . unwrap ( ) ;
350+ let reader_uid = unsafe { libc:: geteuid ( ) } ;
351+ write ( & path, & record ( "normal" , NOW , "{}" ) ) ;
352+ assert ! ( read( & settings( & path) , NOW ) . accepts_new_work( ) ) ;
353+
354+ if reader_uid == 0 {
355+ // In the privileged container, exercise a real foreign-owned
356+ // descriptor through the production snapshot path as well.
357+ std:: os:: unix:: fs:: chown ( & path, Some ( 65534 ) , None ) . unwrap ( ) ;
358+ assert_eq ! ( read( & settings( & path) , NOW ) , Snapshot :: UNAVAILABLE ) ;
359+ }
360+ let owner_uid = std:: fs:: metadata ( & path) . unwrap ( ) . uid ( ) ;
361+ assert_ne ! ( owner_uid, 0 ) ;
362+ // This also exercises the descriptor policy on unprivileged runners,
363+ // without requiring permission to change an inode's owner.
364+ let other_uid = if owner_uid == 1 { 2 } else { 1 } ;
365+ assert ! ( observation( path_text, owner_uid) . is_some( ) ) ;
366+ assert ! ( observation( path_text, other_uid) . is_none( ) ) ;
367+
368+ if reader_uid == 0 {
369+ std:: os:: unix:: fs:: chown ( & path, Some ( 0 ) , None ) . unwrap ( ) ;
370+ // A root-owned observation is trusted by a non-root reader too.
371+ assert ! ( observation( path_text, other_uid) . is_some( ) ) ;
372+ assert ! ( read( & settings( & path) , NOW ) . accepts_new_work( ) ) ;
373+ }
374+ // Atomic producer replacement restores the actual reader's ownership.
375+ write ( & path, & record ( "draining" , NOW , "{}" ) ) ;
376+ assert_eq ! ( read( & settings( & path) , NOW ) . state, State :: Draining ) ;
377+ }
378+
341379 #[ test]
342380 fn routes_follow_php_s_admission_classes ( ) {
343381 for ( method, path, expected) in [
0 commit comments