Skip to content

Commit d7a7653

Browse files
committed
fix(sandbox): bound control admission and retire stale mediation
Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent 1754f8d commit d7a7653

13 files changed

Lines changed: 561 additions & 6837 deletions

File tree

‎architecture/sandbox.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ TCP Service, or VM vsock channel. Independent bidirectional `Exchange` RPCs
2727
carry lifecycle, exec, TCP, and forwarding traffic, while one persistent
2828
bidirectional `Mediate` RPC carries multiplexed DNS traffic. General application
2929
UDP is unsupported; UDP DNS remains mediated by the supervisor.
30+
The sandbox probes HTTP/2 connection liveness every five seconds and closes
31+
connections that miss a ten-second acknowledgement deadline. Closing a
32+
connection cancels its stream bridges before releasing the exclusive DNS
33+
mediation lease; an authenticated replacement waits for release instead of
34+
preempting a live supervisor. Idle healthy connections remain usable.
35+
Unauthenticated TLS handshakes have a separate bounded asynchronous pool and
36+
five-second deadline, never consuming authenticated control slots or threads.
37+
The socket broker reserves the TCP control-listener port against workload
38+
connections, including loopback aliases. Unix control listeners reject workload
39+
descendants using kernel peer credentials and process ancestry, while ordinary
40+
workload loopback and Unix services remain available.
3041
NetworkPolicy is an outer reachability fence, not a confidentiality boundary.
3142
Each sandbox generation receives a fresh CA and distinct server/client leaves;
3243
both endpoints bind the same workload identity and immutable driver resource

‎crates/openshell-sandbox/src/boundary_server.rs‎

Lines changed: 463 additions & 72 deletions
Large diffs are not rendered by default.

‎crates/openshell-sandbox/src/network_broker.rs‎

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ fn register_dns_socket(
187187

188188
#[derive(Clone)]
189189
struct 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

209210
impl 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+
619645
fn 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));

‎crates/openshell-sandbox/src/sandbox/linux/landlock.rs‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -434,17 +434,6 @@ pub fn enforce(prepared: PreparedRuleset) -> Result<()> {
434434
Ok(())
435435
}
436436

437-
/// Legacy single-phase apply. Kept for non-Linux platforms and tests.
438-
/// On Linux, callers should use [`prepare`] + [`enforce`] for correct
439-
/// privilege ordering.
440-
#[allow(dead_code)] // Retained for backward compat; live callers use prepare+enforce.
441-
pub fn apply(policy: &SandboxPolicy, workdir: Option<&str>) -> Result<()> {
442-
if let Some(prepared) = prepare(policy, workdir)? {
443-
enforce(prepared)?;
444-
}
445-
Ok(())
446-
}
447-
448437
/// Tailor a rule's access mask to the inode referenced by its already-open FD.
449438
///
450439
/// Landlock directory-only rights such as `ReadDir` are invalid for regular

‎crates/openshell-sandbox/src/sandbox/linux/mod.rs‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,6 @@ pub fn apply_supervisor_prelude() -> Result<()> {
102102
seccomp::apply_supervisor_prelude()
103103
}
104104

105-
/// Legacy single-phase apply. Kept for backward compatibility.
106-
/// New callers should use [`prepare`] + [`enforce`] for correct privilege ordering.
107-
#[allow(dead_code)] // Retained for backward compat; live callers use prepare+enforce.
108-
pub fn apply(policy: &SandboxPolicy, workdir: Option<&str>) -> Result<()> {
109-
landlock::apply(policy, workdir)?;
110-
seccomp::apply(policy)?;
111-
Ok(())
112-
}
113-
114105
/// Probe Landlock availability and emit OCSF logs from the parent process.
115106
///
116107
/// This must be called **before** `pre_exec` / `fork()` so that the OCSF events

‎crates/openshell-sandbox/src/sandbox/mod.rs‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! Platform sandboxing implementation.
55
66
use miette::Result;
7+
#[cfg(not(target_os = "linux"))]
78
use openshell_core::policy::SandboxPolicy;
89

910
#[cfg(target_os = "linux")]
@@ -16,15 +17,9 @@ pub mod linux;
1617
/// Returns an error if the sandbox cannot be applied.
1718
// On Linux the spawn path uses `prepare`+`enforce` directly; this single-phase
1819
// apply is only invoked from the non-Linux spawn_impl.
19-
#[cfg_attr(target_os = "linux", allow(dead_code))]
20-
#[cfg_attr(not(target_os = "linux"), allow(clippy::unnecessary_wraps))]
20+
#[cfg(not(target_os = "linux"))]
21+
#[allow(clippy::unnecessary_wraps)]
2122
pub fn apply(policy: &SandboxPolicy, workdir: Option<&str>) -> Result<()> {
22-
#[cfg(target_os = "linux")]
23-
{
24-
linux::apply(policy, workdir)
25-
}
26-
27-
#[cfg(not(target_os = "linux"))]
2823
{
2924
let _ = (policy, workdir);
3025
openshell_ocsf::ocsf_emit!(

‎crates/openshell-supervisor-process/src/child_env.rs‎

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)