From 74e699f376daaf3cd0904a3925071dfc657ee672 Mon Sep 17 00:00:00 2001 From: saviourmk Date: Fri, 27 Nov 2026 13:14:12 +0900 Subject: [PATCH] netlink: honor SOCK_NONBLOCK and SOCK_CLOEXEC on the virtual socket --- crates/sandlock-core/src/netlink/handlers.rs | 14 +++++-- .../tests/integration/test_netlink_virt.rs | 40 +++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/crates/sandlock-core/src/netlink/handlers.rs b/crates/sandlock-core/src/netlink/handlers.rs index 50073fab1..f3f4b4d2f 100644 --- a/crates/sandlock-core/src/netlink/handlers.rs +++ b/crates/sandlock-core/src/netlink/handlers.rs @@ -101,6 +101,7 @@ fn bind_kernel_like_address(fd: RawFd) -> std::io::Result<()> { /// else (and non-NETLINK_ROUTE netlink protocols) returns EAFNOSUPPORT. pub async fn handle_socket(notif: &SeccompNotif, state: &Arc) -> NotifAction { let domain = notif.data.args[0]; + let sock_type = notif.data.args[1] as i32; let protocol = notif.data.args[2]; if !family_allowed(domain) { @@ -129,12 +130,15 @@ pub async fn handle_socket(notif: &SeccompNotif, state: &Arc) -> N // fds[1] → child side (injected) // // The supervisor end is driven by a tokio task via AsyncFd, so it - // must be non-blocking. The child end stays blocking (glibc's - // netlink code expects blocking semantics). + // must be non-blocking. The child end takes the SOCK_NONBLOCK the + // child passed to socket(2); glibc's netlink code passes none and + // expects blocking semantics. let flags = unsafe { libc::fcntl(fds[0], libc::F_GETFL) }; if flags < 0 || unsafe { libc::fcntl(fds[0], libc::F_SETFL, flags | libc::O_NONBLOCK) } < 0 || bind_kernel_like_address(fds[0]).is_err() + || (sock_type & libc::SOCK_NONBLOCK != 0 + && unsafe { libc::fcntl(fds[1], libc::F_SETFL, libc::O_NONBLOCK) } < 0) { unsafe { libc::close(fds[0]); @@ -155,7 +159,11 @@ pub async fn handle_socket(notif: &SeccompNotif, state: &Arc) -> N proxy::spawn_responder(responder_fd, port_id, registration); NotifAction::InjectFdSend { srcfd: child_fd, - newfd_flags: libc::O_CLOEXEC as u32, + newfd_flags: if sock_type & libc::SOCK_CLOEXEC != 0 { + libc::O_CLOEXEC as u32 + } else { + 0 + }, } } diff --git a/crates/sandlock-core/tests/integration/test_netlink_virt.rs b/crates/sandlock-core/tests/integration/test_netlink_virt.rs index 0edac137d..3ea4dd3e7 100644 --- a/crates/sandlock-core/tests/integration/test_netlink_virt.rs +++ b/crates/sandlock-core/tests/integration/test_netlink_virt.rs @@ -730,3 +730,43 @@ s = original ) .await; } + +/// socket(2) applies SOCK_NONBLOCK and SOCK_CLOEXEC to the descriptor it +/// returns, and the virtual NETLINK_ROUTE socket has to honor them too. It +/// came back blocking and close-on-exec whatever the caller asked for, so a +/// non-blocking reader such as Python with SOCK_NONBLOCK, which trusts the +/// flag and never calls fcntl, hung in recv once the replies ran out. +#[tokio::test] +async fn netlink_socket_honors_creation_flags() { + let script = r#" +import ctypes, fcntl, os, socket +libc = ctypes.CDLL(None, use_errno=True) +out = [] +for extra in (0, socket.SOCK_NONBLOCK, socket.SOCK_CLOEXEC): + fd = libc.socket(socket.AF_NETLINK, socket.SOCK_RAW | extra, socket.NETLINK_ROUTE) + assert fd >= 0, ctypes.get_errno() + nonblock = fcntl.fcntl(fd, fcntl.F_GETFL) & os.O_NONBLOCK != 0 + cloexec = fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC != 0 + out.append('%d%d' % (nonblock, cloexec)) + os.close(fd) +s = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW | socket.SOCK_NONBLOCK, socket.NETLINK_ROUTE) +try: + s.recv(4096) + out.append('data') +except BlockingIOError: + out.append('EAGAIN') +print(' '.join(out)) +"#; + let result = tokio::time::timeout( + std::time::Duration::from_secs(30), + base_policy() + .build() + .unwrap() + .run(&["python3", "-c", script]), + ) + .await + .expect("recv on a SOCK_NONBLOCK netlink socket blocked") + .unwrap(); + assert!(result.success(), "{}", result.stderr_str().unwrap_or("")); + assert_eq!(result.stdout_str().unwrap_or("").trim(), "00 10 01 EAGAIN"); +}