6
6
//! Endpoint Security framework.
7
7
8
8
use futures:: channel:: oneshot;
9
- use libc:: { proc_listallpids , proc_pidpath } ;
9
+ use libc:: pid_t ;
10
10
use serde:: Deserialize ;
11
11
use std:: {
12
12
collections:: { HashMap , HashSet } ,
13
- ffi:: c_void,
14
13
io,
15
14
path:: PathBuf ,
16
15
process:: Stdio ,
17
- ptr,
18
16
sync:: { Arc , LazyLock , Mutex } ,
19
17
time:: Duration ,
20
18
} ;
19
+ use talpid_macos:: process:: { list_pids, process_path} ;
21
20
use talpid_platform_metadata:: MacosVersion ;
22
21
use talpid_types:: tunnel:: ErrorStateCause ;
23
22
use tokio:: io:: { AsyncBufReadExt , BufReader } ;
@@ -52,7 +51,7 @@ pub enum Error {
52
51
InitializePids ( #[ source] io:: Error ) ,
53
52
/// Failed to find path for a process
54
53
#[ error( "Failed to find path for a process: {}" , _0) ]
55
- FindProcessPath ( #[ source] io:: Error , u32 ) ,
54
+ FindProcessPath ( #[ source] io:: Error , pid_t ) ,
56
55
}
57
56
58
57
impl From < & Error > for ErrorStateCause {
@@ -231,7 +230,7 @@ pub enum ExclusionStatus {
231
230
232
231
#[ derive( Debug ) ]
233
232
struct InnerProcessStates {
234
- processes : HashMap < u32 , ProcessInfo > ,
233
+ processes : HashMap < pid_t , ProcessInfo > ,
235
234
exclude_paths : HashSet < PathBuf > ,
236
235
}
237
236
@@ -277,7 +276,7 @@ impl ProcessStates {
277
276
inner. exclude_paths = paths;
278
277
}
279
278
280
- pub fn get_process_status ( & self , pid : u32 ) -> ExclusionStatus {
279
+ pub fn get_process_status ( & self , pid : pid_t ) -> ExclusionStatus {
281
280
let inner = self . inner . lock ( ) . unwrap ( ) ;
282
281
match inner. processes . get ( & pid) {
283
282
Some ( val) if val. is_excluded ( ) => ExclusionStatus :: Excluded ,
@@ -300,7 +299,7 @@ impl InnerProcessStates {
300
299
301
300
// For new processes, inherit all exclusion state from the parent, if there is one.
302
301
// Otherwise, look up excluded paths
303
- fn handle_fork ( & mut self , parent_pid : u32 , exec_path : PathBuf , msg : ESForkEvent ) {
302
+ fn handle_fork ( & mut self , parent_pid : pid_t , exec_path : PathBuf , msg : ESForkEvent ) {
304
303
let pid = msg. child . audit_token . pid ;
305
304
306
305
if self . processes . contains_key ( & pid) {
@@ -327,7 +326,7 @@ impl InnerProcessStates {
327
326
self . processes . insert ( pid, base_info) ;
328
327
}
329
328
330
- fn handle_exec ( & mut self , pid : u32 , msg : ESExecEvent ) {
329
+ fn handle_exec ( & mut self , pid : pid_t , msg : ESExecEvent ) {
331
330
let Some ( info) = self . processes . get_mut ( & pid) else {
332
331
log:: error!( "exec received for unknown pid {pid}" ) ;
333
332
return ;
@@ -354,54 +353,13 @@ impl InnerProcessStates {
354
353
}
355
354
}
356
355
357
- fn handle_exit ( & mut self , pid : u32 ) {
356
+ fn handle_exit ( & mut self , pid : pid_t ) {
358
357
if self . processes . remove ( & pid) . is_none ( ) {
359
358
log:: error!( "exit syscall for unknown pid {pid}" ) ;
360
359
}
361
360
}
362
361
}
363
362
364
- /// Obtain a list of all pids
365
- fn list_pids ( ) -> io:: Result < Vec < u32 > > {
366
- // SAFETY: Passing in null and 0 returns the number of processes
367
- let num_pids = unsafe { proc_listallpids ( ptr:: null_mut ( ) , 0 ) } ;
368
- if num_pids <= 0 {
369
- return Err ( io:: Error :: last_os_error ( ) ) ;
370
- }
371
- let num_pids = usize:: try_from ( num_pids) . unwrap ( ) ;
372
- let mut pids = vec ! [ 0u32 ; num_pids] ;
373
-
374
- let buf_sz = ( num_pids * std:: mem:: size_of :: < u32 > ( ) ) as i32 ;
375
- // SAFETY: 'pids' is large enough to contain 'num_pids' processes
376
- let num_pids = unsafe { proc_listallpids ( pids. as_mut_ptr ( ) as * mut c_void , buf_sz) } ;
377
- if num_pids == -1 {
378
- return Err ( io:: Error :: last_os_error ( ) ) ;
379
- }
380
-
381
- pids. resize ( usize:: try_from ( num_pids) . unwrap ( ) , 0 ) ;
382
-
383
- Ok ( pids)
384
- }
385
-
386
- fn process_path ( pid : u32 ) -> io:: Result < PathBuf > {
387
- let mut buffer = [ 0u8 ; libc:: MAXPATHLEN as usize ] ;
388
- // SAFETY: `proc_pidpath` returns at most `buffer.len()` bytes
389
- let buf_len = unsafe {
390
- proc_pidpath (
391
- pid as i32 ,
392
- buffer. as_mut_ptr ( ) as * mut c_void ,
393
- buffer. len ( ) as u32 ,
394
- )
395
- } ;
396
- if buf_len == -1 {
397
- return Err ( io:: Error :: last_os_error ( ) ) ;
398
- }
399
- Ok ( PathBuf :: from (
400
- std:: str:: from_utf8 ( & buffer[ 0 ..buf_len as usize ] )
401
- . map_err ( |_| io:: Error :: new ( io:: ErrorKind :: InvalidInput , "invalid process path" ) ) ?,
402
- ) )
403
- }
404
-
405
363
#[ derive( Debug , Clone ) ]
406
364
struct ProcessInfo {
407
365
exec_path : PathBuf ,
@@ -480,7 +438,7 @@ struct ESExecutable {
480
438
/// https://developer.apple.com/documentation/endpointsecurity/es_process_t/3228975-audit_token?language=objc
481
439
#[ derive( Debug , Deserialize ) ]
482
440
struct ESAuditToken {
483
- pid : u32 ,
441
+ pid : pid_t ,
484
442
}
485
443
486
444
/// Process information for the message returned by `eslogger`.
0 commit comments