@@ -10,11 +10,11 @@ use crate::failure_log;
1010use lithe_platform:: fs:: {
1111 restrict_directory_to_owner, OWNER_ONLY_DIRECTORY_MODE , OWNER_ONLY_FILE_MODE ,
1212} ;
13+ use lithe_platform:: socket:: { effective_uid, peer_uid} ;
1314use std:: ffi:: OsStr ;
1415use std:: fs:: { self , DirBuilder , Permissions } ;
1516use std:: io;
1617use std:: os:: unix:: fs:: { DirBuilderExt , MetadataExt , PermissionsExt } ;
17- use std:: os:: unix:: io:: { AsRawFd , RawFd } ;
1818use std:: os:: unix:: net:: { UnixListener , UnixStream } ;
1919use std:: path:: { Path , PathBuf } ;
2020
@@ -66,7 +66,7 @@ impl Listener {
6666 Err ( error) => return Err ( error) ,
6767 } ;
6868 // A peer whose account cannot be read is treated like another account.
69- if peer_uid ( stream. as_raw_fd ( ) ) . ok ( ) != Some ( effective_uid ( ) ) {
69+ if peer_uid ( & stream) . ok ( ) != Some ( effective_uid ( ) ) {
7070 return Ok ( Accept :: Rejected ( "peer-identity" ) ) ;
7171 }
7272 // Linux does not carry O_NONBLOCK from the listener to accepted sockets.
@@ -83,7 +83,7 @@ pub(super) fn connect(address: &OsStr) -> io::Result<UnixStream> {
8383 return Err ( io:: ErrorKind :: InvalidInput . into ( ) ) ;
8484 }
8585 let stream = UnixStream :: connect ( path) ?;
86- if peer_uid ( stream. as_raw_fd ( ) ) ? != effective_uid ( ) {
86+ if peer_uid ( & stream) ? != effective_uid ( ) {
8787 return Err ( io:: ErrorKind :: PermissionDenied . into ( ) ) ;
8888 }
8989 // The session's own prompt and write deadlines end first and close the
@@ -181,47 +181,3 @@ fn verify_private(directory: &Path) -> io::Result<()> {
181181 Err ( io:: ErrorKind :: PermissionDenied . into ( ) )
182182 }
183183}
184-
185- fn effective_uid ( ) -> u32 {
186- // SAFETY: geteuid has no preconditions and cannot fail.
187- unsafe { libc:: geteuid ( ) }
188- }
189-
190- /// Account of the process on the other end of a connected Unix socket. For a
191- /// client this is the account that created the listening socket.
192- #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
193- fn peer_uid ( socket : RawFd ) -> io:: Result < u32 > {
194- let mut credentials = libc:: ucred {
195- pid : 0 ,
196- uid : 0 ,
197- gid : 0 ,
198- } ;
199- let mut length = std:: mem:: size_of :: < libc:: ucred > ( ) as libc:: socklen_t ;
200- // SAFETY: the out-pointers reference live locals of the declared size.
201- let status = unsafe {
202- libc:: getsockopt (
203- socket,
204- libc:: SOL_SOCKET ,
205- libc:: SO_PEERCRED ,
206- ( & mut credentials as * mut libc:: ucred ) . cast ( ) ,
207- & mut length,
208- )
209- } ;
210- if status == -1 {
211- return Err ( io:: Error :: last_os_error ( ) ) ;
212- }
213- Ok ( credentials. uid )
214- }
215-
216- /// Account of the process on the other end of a connected Unix socket. For a
217- /// client this is the account that created the listening socket.
218- #[ cfg( not( any( target_os = "linux" , target_os = "android" ) ) ) ]
219- fn peer_uid ( socket : RawFd ) -> io:: Result < u32 > {
220- let mut uid: libc:: uid_t = 0 ;
221- let mut gid: libc:: gid_t = 0 ;
222- // SAFETY: the out-pointers reference live locals.
223- if unsafe { libc:: getpeereid ( socket, & mut uid, & mut gid) } == -1 {
224- return Err ( io:: Error :: last_os_error ( ) ) ;
225- }
226- Ok ( uid)
227- }
0 commit comments