@@ -15,6 +15,7 @@ use std::mem::size_of;
1515use std:: os:: fd:: { AsRawFd , FromRawFd , OwnedFd , RawFd } ;
1616use std:: sync:: mpsc;
1717use std:: thread;
18+ use std:: time:: Duration ;
1819
1920const SECCOMP_SET_MODE_FILTER : libc:: c_uint = 1 ;
2021const SECCOMP_GET_NOTIF_SIZES : libc:: c_uint = 3 ;
@@ -44,6 +45,7 @@ const SECCOMP_USER_NOTIF_FLAG_CONTINUE: u32 = 1;
4445const CONNECTED_SEND_FLAGS : u32 =
4546 ( libc:: MSG_DONTWAIT | libc:: MSG_EOR | libc:: MSG_MORE | libc:: MSG_NOSIGNAL | libc:: MSG_OOB )
4647 as u32 ;
48+ const PROBE_NOTIFICATION_TIMEOUT : Duration = Duration :: from_secs ( 5 ) ;
4749
4850const IOC_NRBITS : u32 = 8 ;
4951const IOC_TYPEBITS : u32 = 8 ;
@@ -393,7 +395,13 @@ fn probe_scalar_round_trip() -> io::Result<bool> {
393395 let ( listener, wait_killable) = receiver
394396 . recv ( )
395397 . map_err ( |_| io:: Error :: other ( "notification launcher disappeared" ) ) ?;
396- let notification = listener. receive ( ) ?;
398+ let notification = match receive_probe_notification ( & listener) {
399+ Ok ( notification) => notification,
400+ Err ( error) => {
401+ let _ = launcher. join ( ) ;
402+ return Err ( error) ;
403+ }
404+ } ;
397405 if i64:: from ( notification. syscall ) != libc:: SYS_getppid {
398406 return Err ( io:: Error :: other ( "unexpected scalar probe syscall" ) ) ;
399407 }
@@ -429,8 +437,10 @@ fn probe_addfd_send() -> io::Result<()> {
429437 }
430438 let injected = RawFd :: try_from ( injected)
431439 . map_err ( |_| io:: Error :: other ( "injected descriptor does not fit RawFd" ) ) ?;
440+ // SAFETY: ADDFD-SEND returned one newly owned descriptor to this task.
441+ let injected = unsafe { OwnedFd :: from_raw_fd ( injected) } ;
432442 // SAFETY: `injected` was returned as an open descriptor by the kernel.
433- let descriptor_flags = unsafe { libc:: fcntl ( injected, libc:: F_GETFD ) } ;
443+ let descriptor_flags = unsafe { libc:: fcntl ( injected. as_raw_fd ( ) , libc:: F_GETFD ) } ;
434444 if descriptor_flags < 0 {
435445 return Err ( io:: Error :: last_os_error ( ) ) ;
436446 }
@@ -441,13 +451,11 @@ fn probe_addfd_send() -> io::Result<()> {
441451 // SAFETY: eventfd reads exactly one u64 into a valid aligned pointer.
442452 let read = unsafe {
443453 libc:: read (
444- injected,
454+ injected. as_raw_fd ( ) ,
445455 std:: ptr:: addr_of_mut!( value) . cast ( ) ,
446456 size_of :: < u64 > ( ) ,
447457 )
448458 } ;
449- // SAFETY: close consumes the descriptor returned by the kernel.
450- unsafe { libc:: close ( injected) } ;
451459 let word_size = isize:: try_from ( size_of :: < u64 > ( ) ) . expect ( "u64 size fits isize" ) ;
452460 if read != word_size || value != 7 {
453461 return Err ( io:: Error :: other ( "injected eventfd was not usable" ) ) ;
@@ -458,7 +466,13 @@ fn probe_addfd_send() -> io::Result<()> {
458466 let listener = receiver
459467 . recv ( )
460468 . map_err ( |_| io:: Error :: other ( "ADDFD launcher disappeared" ) ) ?;
461- let notification = listener. receive ( ) ?;
469+ let notification = match receive_probe_notification ( & listener) {
470+ Ok ( notification) => notification,
471+ Err ( error) => {
472+ let _ = launcher. join ( ) ;
473+ return Err ( error) ;
474+ }
475+ } ;
462476 if i64:: from ( notification. syscall ) != libc:: SYS_socket {
463477 return Err ( io:: Error :: other ( "unexpected ADDFD probe syscall" ) ) ;
464478 }
@@ -641,7 +655,13 @@ fn probe_connected_sendto_fast_path() -> io::Result<()> {
641655 let listener = receiver
642656 . recv ( )
643657 . map_err ( |_| io:: Error :: other ( "sendto launcher disappeared" ) ) ?;
644- let destination = listener. receive ( ) ?;
658+ let destination = match receive_probe_notification ( & listener) {
659+ Ok ( notification) => notification,
660+ Err ( error) => {
661+ let _ = launcher. join ( ) ;
662+ return Err ( error) ;
663+ }
664+ } ;
645665 if i64:: from ( destination. syscall ) != libc:: SYS_sendto
646666 || destination. args [ 4 ] == 0
647667 || destination. args [ 5 ] == 0
@@ -652,7 +672,13 @@ fn probe_connected_sendto_fast_path() -> io::Result<()> {
652672 }
653673 listener. respond_errno ( destination. id , libc:: EACCES ) ?;
654674
655- let fast_open = listener. receive ( ) ?;
675+ let fast_open = match receive_probe_notification ( & listener) {
676+ Ok ( notification) => notification,
677+ Err ( error) => {
678+ let _ = launcher. join ( ) ;
679+ return Err ( error) ;
680+ }
681+ } ;
656682 if i64:: from ( fast_open. syscall ) != libc:: SYS_sendto
657683 || fast_open. args [ 4 ] != 0
658684 || fast_open. args [ 5 ] != 0
@@ -670,6 +696,34 @@ fn probe_connected_sendto_fast_path() -> io::Result<()> {
670696 Ok ( ( ) )
671697}
672698
699+ fn receive_probe_notification ( listener : & NotificationListener ) -> io:: Result < Notification > {
700+ let mut descriptor = libc:: pollfd {
701+ fd : listener. as_raw_fd ( ) ,
702+ events : libc:: POLLIN | libc:: POLLHUP ,
703+ revents : 0 ,
704+ } ;
705+ let timeout = i32:: try_from ( PROBE_NOTIFICATION_TIMEOUT . as_millis ( ) )
706+ . expect ( "probe timeout fits poll milliseconds" ) ;
707+ // SAFETY: descriptor points to one live pollfd for the duration of poll.
708+ let ready = unsafe { libc:: poll ( & raw mut descriptor, 1 , timeout) } ;
709+ if ready < 0 {
710+ return Err ( io:: Error :: last_os_error ( ) ) ;
711+ }
712+ if ready == 0 {
713+ return Err ( io:: Error :: new (
714+ io:: ErrorKind :: TimedOut ,
715+ "seccomp notification probe timed out" ,
716+ ) ) ;
717+ }
718+ if descriptor. revents & libc:: POLLIN == 0 {
719+ return Err ( io:: Error :: new (
720+ io:: ErrorKind :: BrokenPipe ,
721+ "seccomp notification probe listener closed" ,
722+ ) ) ;
723+ }
724+ listener. receive ( )
725+ }
726+
673727fn install_listener_with_flags (
674728 syscalls : & [ i64 ] ,
675729 wait_killable_recv : bool ,
0 commit comments