Skip to content

Commit f5c2bbc

Browse files
committed
fix(sandbox): accept local connections natively on loopback-confined sockets
On kernels without SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV (RHEL 9 / RHCOS 5.14), the broker cannot safely write an accepted peer address into workload memory, so accept/accept4 with a peer-address buffer failed with EOPNOTSUPP. Static binaries and Go servers, which issue the raw syscall, could not accept connections at all. Move local acceptance into the kernel and replace per-accept inspection with standing kernel confinement: - Bind every broker-created TCP/UDP socket to the loopback device before injection and verify the binding. Accepted sockets inherit it, so they can neither receive routed ingress nor emit routed egress. - Stop notifying accept/accept4 and remove the accept workers, the SIGUSR2 accept-interrupt monitor, and the 64-accept ceiling. - Continue getpeername natively for every descriptor except relayed connections. - Deny interface-selection socket options and MSG_FASTOPEN sends from scalar syscall arguments, so confinement does not depend on the capability state of the user namespace that owns the network namespace and covers unregistered descriptors. - Drop loopback-interface ingress on the non-loopback TCP control listener before it listens, so a workload cannot reach it by reconnecting a natively accepted socket. - Mark descriptors above stdio close-on-exec in every workload pre_exec path. - Require an active confinement probe at qualification and carry it as required authenticated audit evidence. Document OpenShift 4.19 as the minimum release: RHCOS kernels for 4.16 through 4.18 are built without Landlock. Closes #4058 Signed-off-by: Drew Newberry <[email protected]>
1 parent a48920a commit f5c2bbc

16 files changed

Lines changed: 1134 additions & 636 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/openshell-isolation-interface/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ libc = "0.2"
2525
rustix = { workspace = true, features = ["fs", "process"] }
2626

2727
[dev-dependencies]
28+
socket2 = { workspace = true }
2829
tokio = { workspace = true }
2930

3031
[lints]

‎crates/openshell-isolation-interface/src/linux/child_seccomp.rs‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const SECCOMP_DATA_ARGS_OFFSET: u32 = 16;
2929
const X32_SYSCALL_BIT: u32 = 0x4000_0000;
3030

3131
const CLOSE_RANGE_UNSHARE_FLAG: u32 = 1 << 1;
32+
const CLOSE_RANGE_CLOEXEC_FLAG: u32 = 1 << 2;
3233
const F_SETOWN_COMMAND: u32 = 8;
3334
const F_SETSIG_COMMAND: u32 = 10;
3435
const F_SETOWN_EX_COMMAND: u32 = 15;
@@ -53,6 +54,7 @@ impl ChildHardeningProgram {
5354
/// The caller must invoke this from the post-fork child after all
5455
/// sandbox-wide TSYNC work and the launcher's `NEW_LISTENER` filter.
5556
pub fn install(&mut self) -> io::Result<()> {
57+
mark_inherited_descriptors_close_on_exec()?;
5658
set_no_new_privileges()?;
5759
let len = u16::try_from(self.instructions.len()).map_err(|_| {
5860
io::Error::new(
@@ -88,6 +90,35 @@ impl ChildHardeningProgram {
8890
}
8991
}
9092

93+
/// Mark every descriptor above stdio close-on-exec in the post-fork child.
94+
///
95+
/// Workloads receive INET sockets only through broker injection, which binds
96+
/// them to loopback first. A descriptor the sandbox process inherited from its
97+
/// container runtime, or opened without `O_CLOEXEC`, must never cross `exec`
98+
/// as an unconfined socket. The command's stdio is already installed on 0-2
99+
/// when `pre_exec` hooks run. This is a single async-signal-safe syscall.
100+
///
101+
/// # Errors
102+
///
103+
/// Returns the kernel error; kernels without `CLOSE_RANGE_CLOEXEC` (before
104+
/// Linux 5.11) fail closed.
105+
pub fn mark_inherited_descriptors_close_on_exec() -> io::Result<()> {
106+
// SAFETY: close_range takes scalar arguments and only sets FD_CLOEXEC.
107+
let result = unsafe {
108+
libc::syscall(
109+
libc::SYS_close_range,
110+
3_u32,
111+
u32::MAX,
112+
CLOSE_RANGE_CLOEXEC_FLAG,
113+
)
114+
};
115+
if result < 0 {
116+
Err(io::Error::last_os_error())
117+
} else {
118+
Ok(())
119+
}
120+
}
121+
91122
/// Build the same-UID workload self-protection program before `fork`.
92123
///
93124
/// `sandbox_tgid` is the sandbox PID as visible from its workload namespace.
@@ -352,6 +383,36 @@ fn set_no_new_privileges() -> io::Result<()> {
352383
mod tests {
353384
use super::*;
354385

386+
#[test]
387+
#[allow(unsafe_code)]
388+
fn inherited_sockets_are_marked_close_on_exec_but_stdio_is_not() {
389+
// SAFETY: scalar socket arguments; deliberately inheritable.
390+
let socket = unsafe { libc::socket(libc::AF_INET, libc::SOCK_STREAM, 0) };
391+
assert!(socket > 2);
392+
// SAFETY: the child performs only async-signal-safe syscalls and exits.
393+
let pid = unsafe { libc::fork() };
394+
assert!(pid >= 0);
395+
if pid == 0 {
396+
let swept = mark_inherited_descriptors_close_on_exec().is_ok();
397+
// SAFETY: F_GETFD reads one descriptor flag word.
398+
let socket_flags = unsafe { libc::fcntl(socket, libc::F_GETFD) };
399+
// SAFETY: as above, for stderr.
400+
let stderr_flags = unsafe { libc::fcntl(libc::STDERR_FILENO, libc::F_GETFD) };
401+
let ok = swept
402+
&& socket_flags & libc::FD_CLOEXEC != 0
403+
&& stderr_flags >= 0
404+
&& stderr_flags & libc::FD_CLOEXEC == 0;
405+
// SAFETY: terminate the forked child without running destructors.
406+
unsafe { libc::_exit(i32::from(!ok)) };
407+
}
408+
let mut status = 0;
409+
// SAFETY: wait for the child created above.
410+
assert_eq!(unsafe { libc::waitpid(pid, &raw mut status, 0) }, pid);
411+
// SAFETY: close the test-owned socket.
412+
unsafe { libc::close(socket) };
413+
assert!(libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0);
414+
}
415+
355416
#[test]
356417
fn rejects_zero_sandbox_tgid() {
357418
assert_eq!(

‎crates/openshell-isolation-interface/src/linux/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod landlock;
1111
pub mod proc_fd;
1212
pub mod process_signal;
1313
pub mod seccomp_notify;
14+
pub mod socket_confinement;
1415
pub mod socket_registry;
1516
pub mod task_memory;
1617
pub mod workload_launcher;

‎crates/openshell-isolation-interface/src/linux/seccomp_notify.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,16 +430,16 @@ pub fn install_listener(syscalls: &[i64]) -> io::Result<NotificationListener> {
430430
///
431431
/// The filter mediates every syscall that can create, select, or materially
432432
/// reconfigure an INET endpoint. Connected `send()`/null-destination
433-
/// `sendto()` retains the audited cBPF fast path.
433+
/// `sendto()` retains the audited cBPF fast path. `accept`/`accept4` run
434+
/// natively: the broker binds every INET socket to loopback before injecting
435+
/// it, and accepted sockets inherit their listener's binding.
434436
pub fn install_workload_listener() -> io::Result<NotificationListener> {
435437
#[allow(unused_mut)] // SYS_open is unavailable on some architectures.
436438
let mut syscalls = vec![
437439
libc::SYS_socket,
438440
libc::SYS_connect,
439441
libc::SYS_bind,
440442
libc::SYS_listen,
441-
libc::SYS_accept,
442-
libc::SYS_accept4,
443443
libc::SYS_sendto,
444444
libc::SYS_sendmsg,
445445
libc::SYS_sendmmsg,

0 commit comments

Comments
 (0)