@@ -187,6 +187,7 @@ fn register_dns_socket(
187187
188188#[ derive( Clone ) ]
189189struct NotificationQueues {
190+ protected_control_port : Option < u16 > ,
190191 accept_registrar : crate :: accept_interrupt:: AcceptRegistrar ,
191192 identity_resolver : ProcfsIdentityResolver ,
192193 pending : mpsc:: Sender < PendingTcpOpen > ,
@@ -207,28 +208,39 @@ pub struct NetworkBroker {
207208}
208209
209210impl NetworkBroker {
210- pub ( crate ) fn start ( listener : NotificationListener ) -> io:: Result < Self > {
211- Self :: start_with_dns_address ( listener, DNS_RELAY_ADDRESS )
211+ pub ( crate ) fn start (
212+ listener : NotificationListener ,
213+ protected_control_port : Option < u16 > ,
214+ ) -> io:: Result < Self > {
215+ Self :: start_with_dns_address ( listener, DNS_RELAY_ADDRESS , protected_control_port)
212216 }
213217
214218 #[ cfg( any( test, feature = "perf-harness" ) ) ]
215219 pub ( crate ) fn start_for_test ( listener : NotificationListener ) -> io:: Result < Self > {
216220 Self :: start_with_dns_address (
217221 listener,
218222 SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: LOCALHOST ) , 0 ) ,
223+ None ,
219224 )
220225 }
221226
222227 fn start_with_dns_address (
223228 listener : NotificationListener ,
224229 dns_address : SocketAddr ,
230+ protected_control_port : Option < u16 > ,
225231 ) -> io:: Result < Self > {
226- Self :: start_with_decision_timeout ( listener, dns_address, NETWORK_DECISION_TIMEOUT )
232+ Self :: start_with_decision_timeout (
233+ listener,
234+ dns_address,
235+ protected_control_port,
236+ NETWORK_DECISION_TIMEOUT ,
237+ )
227238 }
228239
229240 fn start_with_decision_timeout (
230241 listener : NotificationListener ,
231242 dns_address : SocketAddr ,
243+ protected_control_port : Option < u16 > ,
232244 decision_timeout : Duration ,
233245 ) -> io:: Result < Self > {
234246 let listener = Arc :: new ( listener) ;
@@ -244,6 +256,7 @@ impl NetworkBroker {
244256 let dns_relay = start_dns_relay ( dns_address, pending_dns_tx) ?;
245257 let dns_address = dns_relay. address ;
246258 let queues = NotificationQueues {
259+ protected_control_port,
247260 accept_registrar : accept_monitor. registrar ( ) ,
248261 identity_resolver : ProcfsIdentityResolver :: for_pid_namespace ( ) ,
249262 pending : pending_tx,
@@ -616,6 +629,19 @@ fn create_socket(
616629 Ok ( ( ) )
617630}
618631
632+ fn reject_protected_control_destination (
633+ destination : SocketAddr ,
634+ protected_port : Option < u16 > ,
635+ ) -> io:: Result < ( ) > {
636+ // Reserve the listener's port across loopback aliases, IPv4-mapped IPv6,
637+ // and wildcard Pod listeners. A workload must never reach its control
638+ // endpoint, including through a supervisor-authorized external relay.
639+ if protected_port == Some ( destination. port ( ) ) {
640+ return Err ( io:: Error :: from_raw_os_error ( libc:: EACCES ) ) ;
641+ }
642+ Ok ( ( ) )
643+ }
644+
619645fn connect_socket (
620646 registry : Arc < Mutex < SocketRegistry > > ,
621647 listener : Arc < NotificationListener > ,
@@ -628,6 +654,7 @@ fn connect_socket(
628654 active_opens,
629655 identity_resolver,
630656 decision_timeout,
657+ protected_control_port,
631658 ..
632659 } = queues;
633660 let notification_started = Instant :: now ( ) ;
@@ -661,6 +688,7 @@ fn connect_socket(
661688 }
662689 let destination =
663690 read_socket_addr ( notification. tid , notification. args [ 1 ] , notification. args [ 2 ] ) ?;
691+ reject_protected_control_destination ( destination, protected_control_port) ?;
664692 let ( kind, socket_identity, nonblocking) = {
665693 let registry = lock ( & registry) ;
666694 let entry = registry. resolve ( notification. tid , fd) ?;
@@ -1771,6 +1799,7 @@ mod tests {
17711799 let broker = NetworkBroker :: start_with_decision_timeout (
17721800 listener,
17731801 "127.0.0.1:0" . parse ( ) . unwrap ( ) ,
1802+ None ,
17741803 Duration :: from_millis ( 50 ) ,
17751804 )
17761805 . unwrap ( ) ;
@@ -1875,6 +1904,58 @@ mod tests {
18751904 }
18761905 }
18771906
1907+ #[ test]
1908+ fn protected_control_port_rejects_loopback_aliases_and_pod_addresses ( ) {
1909+ for address in [
1910+ "127.0.0.1:7443" ,
1911+ "127.0.0.2:7443" ,
1912+ "[::1]:7443" ,
1913+ "[::ffff:127.0.0.1]:7443" ,
1914+ "10.42.0.8:7443" ,
1915+ ] {
1916+ assert_eq ! (
1917+ reject_protected_control_destination( address. parse( ) . unwrap( ) , Some ( 7443 ) )
1918+ . unwrap_err( )
1919+ . raw_os_error( ) ,
1920+ Some ( libc:: EACCES )
1921+ ) ;
1922+ }
1923+ assert ! (
1924+ reject_protected_control_destination( "127.0.0.1:8080" . parse( ) . unwrap( ) , Some ( 7443 ) )
1925+ . is_ok( )
1926+ ) ;
1927+ }
1928+
1929+ #[ test]
1930+ fn workload_cannot_fill_control_listener_with_loopback_connections ( ) {
1931+ let control = TcpListener :: bind ( "127.0.0.1:0" ) . unwrap ( ) ;
1932+ control. set_nonblocking ( true ) . unwrap ( ) ;
1933+ let address = control. local_addr ( ) . unwrap ( ) ;
1934+ let ( launcher, listener) =
1935+ openshell_isolation_interface:: linux:: workload_launcher:: start ( ) . unwrap ( ) ;
1936+ let _broker = NetworkBroker :: start_with_dns_address (
1937+ listener,
1938+ "127.0.0.1:0" . parse ( ) . unwrap ( ) ,
1939+ Some ( address. port ( ) ) ,
1940+ )
1941+ . unwrap ( ) ;
1942+ launcher
1943+ . execute ( move || -> io:: Result < ( ) > {
1944+ for _ in 0 ..160 {
1945+ let error =
1946+ TcpStream :: connect ( address) . expect_err ( "control port must be unreachable" ) ;
1947+ assert_eq ! ( error. raw_os_error( ) , Some ( libc:: EACCES ) ) ;
1948+ }
1949+ Ok ( ( ) )
1950+ } )
1951+ . unwrap ( )
1952+ . unwrap ( ) ;
1953+ assert_eq ! (
1954+ control. accept( ) . unwrap_err( ) . kind( ) ,
1955+ io:: ErrorKind :: WouldBlock
1956+ ) ;
1957+ }
1958+
18781959 #[ test]
18791960 fn pending_external_open_slots_are_bounded_and_reusable ( ) {
18801961 let active = Arc :: new ( AtomicUsize :: new ( OPEN_QUEUE_CAPACITY - 1 ) ) ;
0 commit comments