Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions crates/sandlock-core/src/netlink/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NetlinkState>) -> 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) {
Expand Down Expand Up @@ -129,12 +130,15 @@ pub async fn handle_socket(notif: &SeccompNotif, state: &Arc<NetlinkState>) -> 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]);
Expand All @@ -155,7 +159,11 @@ pub async fn handle_socket(notif: &SeccompNotif, state: &Arc<NetlinkState>) -> 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
},
}
}

Expand Down
40 changes: 40 additions & 0 deletions crates/sandlock-core/tests/integration/test_netlink_virt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Loading