@@ -19,6 +19,9 @@ use tokio::net::TcpStream;
1919
2020use crate :: error:: CliError ;
2121
22+ mod ownership;
23+ use ownership:: { connect, current_uid} ;
24+
2225pub const PROVIDER_ENV : & str = "BUZZ_CREDENTIAL_PROVIDER" ;
2326
2427/// The protocol version this caller speaks.
@@ -28,8 +31,6 @@ const VERSION_HEADER: &str = "Buzz-Credential-Provider";
2831/// Methods the provider may hold open for a person's decision (up to 120s).
2932pub ( crate ) const HELD_TIMEOUT : Duration = Duration :: from_secs ( 150 ) ;
3033const QUICK_TIMEOUT : Duration = Duration :: from_secs ( 10 ) ;
31- /// `lsof` can take seconds on a busy Mac.
32- const SAME_USER_TIMEOUT : Duration = Duration :: from_secs ( 5 ) ;
3334/// Every result is built from a request's params, and the desktop reads at
3435/// most 2 MiB of request body, so no result is larger than that in bytes.
3536/// Escaping a result as a JSON string grows it at most sixfold: a decrypted
@@ -283,100 +284,6 @@ fn parse_response(raw: &[u8]) -> Option<(u16, Vec<u8>)> {
283284 }
284285}
285286
286- // ---------------------------------------------------------------------------
287- // Same-user check
288- // ---------------------------------------------------------------------------
289-
290- /// Connect to the provider port. With `check`, confirm by numeric user ID that
291- /// a process of this user holds the server side of this very connection, and
292- /// refuse to use the connection otherwise.
293- async fn connect ( port : u16 , check : bool ) -> Result < TcpStream , ProviderError > {
294- let stream = tokio:: time:: timeout ( QUICK_TIMEOUT , TcpStream :: connect ( ( "127.0.0.1" , port) ) )
295- . await
296- . map_err ( |_| ProviderError :: Transport ( "connection timed out" . into ( ) ) ) ?
297- . map_err ( |e| ProviderError :: Transport ( format ! ( "not reachable: {}" , e. kind( ) ) ) ) ?;
298- if check {
299- let local = stream
300- . local_addr ( )
301- . map_err ( |_| ProviderError :: Transport ( "no local address" . into ( ) ) ) ?
302- . port ( ) ;
303- #[ cfg( test) ]
304- if tests:: SAME_USER_HANGS . try_with ( |_| ( ) ) . is_ok ( ) {
305- return Err ( ProviderError :: Unconfirmed ) ;
306- }
307- confirm_same_user ( server_side_uid ( port, local) ) . await ?;
308- }
309- Ok ( stream)
310- }
311-
312- /// Refuse unless `owner` finds this user's UID. A lookup that runs out of
313- /// time decided nothing, so it is retryable; any other answer is final.
314- async fn confirm_same_user (
315- owner : impl std:: future:: Future < Output = Option < u32 > > ,
316- ) -> Result < ( ) , ProviderError > {
317- match tokio:: time:: timeout ( SAME_USER_TIMEOUT , owner) . await {
318- Ok ( Some ( uid) ) if uid == current_uid ( ) => Ok ( ( ) ) ,
319- Ok ( _) => Err ( ProviderError :: OtherUser ) ,
320- Err ( _) => Err ( ProviderError :: Unconfirmed ) ,
321- }
322- }
323-
324- fn current_uid ( ) -> u32 {
325- #[ cfg( test) ]
326- if let Ok ( uid) = tests:: CALLER_UID . try_with ( |uid| * uid) {
327- return uid;
328- }
329- // SAFETY: getuid has no preconditions and cannot fail.
330- unsafe { libc:: getuid ( ) }
331- }
332-
333- /// The owner UID of the socket `127.0.0.1:server -> 127.0.0.1:client`.
334- #[ cfg( target_os = "linux" ) ]
335- async fn server_side_uid ( server : u16 , client : u16 ) -> Option < u32 > {
336- let table = tokio:: fs:: read_to_string ( "/proc/net/tcp" ) . await . ok ( ) ?;
337- proc_net_tcp_uid ( & table, server, client)
338- }
339-
340- #[ cfg( any( target_os = "linux" , test) ) ]
341- fn proc_net_tcp_uid ( table : & str , server : u16 , client : u16 ) -> Option < u32 > {
342- let local = format ! ( "0100007F:{server:04X}" ) ;
343- let remote = format ! ( "0100007F:{client:04X}" ) ;
344- table. lines ( ) . skip ( 1 ) . find_map ( |line| {
345- let cols: Vec < & str > = line. split_whitespace ( ) . collect ( ) ;
346- ( cols. get ( 1 ) == Some ( & local. as_str ( ) ) && cols. get ( 2 ) == Some ( & remote. as_str ( ) ) )
347- . then ( || cols. get ( 7 ) ?. parse ( ) . ok ( ) )
348- . flatten ( )
349- } )
350- }
351-
352- /// macOS: `lsof` lists the socket's owning process with its UID.
353- #[ cfg( not( target_os = "linux" ) ) ]
354- async fn server_side_uid ( server : u16 , client : u16 ) -> Option < u32 > {
355- let output = tokio:: process:: Command :: new ( "/usr/sbin/lsof" )
356- . args ( [ "-nP" , "-F" , "un" , & format ! ( "[email protected] :{server}" ) ] ) 357- . kill_on_drop ( true )
358- . output ( )
359- . await
360- . ok ( ) ?;
361- lsof_uid ( & String :: from_utf8_lossy ( & output. stdout ) , server, client)
362- }
363-
364- #[ cfg( any( not( target_os = "linux" ) , test) ) ]
365- fn lsof_uid ( output : & str , server : u16 , client : u16 ) -> Option < u32 > {
366- let want = format ! ( "n127.0.0.1:{server}->127.0.0.1:{client}" ) ;
367- let mut uid = None ;
368- for line in output. lines ( ) {
369- if let Some ( value) = line. strip_prefix ( 'u' ) {
370- uid = value. parse ( ) . ok ( ) ;
371- } else if line. strip_prefix ( 'p' ) . is_some ( ) {
372- uid = None ;
373- } else if line == want {
374- return uid;
375- }
376- }
377- None
378- }
379-
380287// ---------------------------------------------------------------------------
381288// Provider client
382289// ---------------------------------------------------------------------------
@@ -1516,6 +1423,7 @@ fn unpair_with(
15161423
15171424#[ cfg( test) ]
15181425pub ( crate ) mod tests {
1426+ use super :: ownership:: { lsof_uid, proc_net_tcp_uid} ;
15191427 use super :: * ;
15201428 use crate :: cached_assertion:: unix_now;
15211429
@@ -1869,28 +1777,6 @@ pub(crate) mod tests {
18691777 assert ! ( seen. lock( ) . unwrap( ) . len( ) >= 5 ) ;
18701778 }
18711779
1872- /// A same-user lookup that runs out of time is retryable; a wrong or
1873- /// missing owner stays a refusal.
1874- #[ tokio:: test( start_paused = true ) ]
1875- async fn a_slow_same_user_check_is_retryable_and_a_mismatch_is_not ( ) {
1876- let slow = async {
1877- tokio:: time:: sleep ( Duration :: from_secs ( 4 ) ) . await ;
1878- Some ( current_uid ( ) )
1879- } ;
1880- assert ! (
1881- confirm_same_user( slow) . await . is_ok( ) ,
1882- "4 s is within the budget"
1883- ) ;
1884-
1885- let err = confirm_same_user ( std:: future:: pending ( ) ) . await . unwrap_err ( ) ;
1886- assert ! ( crate :: error:: is_retryable_error( & err. into_cli( "describe" ) ) ) ;
1887-
1888- for owner in [ Some ( current_uid ( ) + 1 ) , None ] {
1889- let err = confirm_same_user ( async { owner } ) . await . unwrap_err ( ) ;
1890- assert ! ( !crate :: error:: is_retryable_error( & err. into_cli( "describe" ) ) ) ;
1891- }
1892- }
1893-
18941780 #[ tokio:: test]
18951781 async fn rate_limited_is_waited_out_then_reported_as_retryable ( ) {
18961782 use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
0 commit comments