sandlock icon indicating copy to clipboard operation
sandlock copied to clipboard

Uncapped buffer length in sendto_on_behalf + missing null checks in dry-run FFI

Open dzerik opened this issue 7 months ago • 0 comments

Part 1: sendto_on_behalf and sendmsg_on_behalf — uncapped buffer allocation

File: crates/sandlock-core/src/network.rs:310, 462

The supervisor reads data from child memory to perform sendto() on behalf of the sandboxed process. The buffer length comes directly from the child's syscall arguments without any upper bound:

// network.rs:310
let buf_len = args[2] as usize;
let data = read_child_mem(notif_fd, notif.id, notif.pid, buf_addr, buf_len)?;

read_child_mem allocates vec![0u8; buf_len] internally. A sandboxed process can pass buf_len = usize::MAX via a single sendto() call, causing the supervisor to attempt an ~18 exabyte allocation → OOM.

The same pattern exists in sendmsg_on_behalf at line 462:

// network.rs:462
let iov_len = u64::from_ne_bytes(iov_bytes[off + 8..off + 16].try_into().unwrap()) as usize;
let buf = match read_child_mem(notif_fd, notif.id, notif.pid, iov_base, iov_len) {

Impact

One syscall from any sandbox → supervisor OOM → all concurrent sandboxes fail. The supervisor is a long-lived process, so this is a reliable DoS. No special setup needed — just sendto(fd, buf, SIZE_MAX, 0, NULL, 0).

Suggested fix

const MAX_SEND_BUF: usize = 64 << 20; // 64 MiB

// network.rs:310 — sendto_on_behalf
let buf_len = args[2] as usize;
if buf_len > MAX_SEND_BUF {
    return NotifAction::Errno(libc::EMSGSIZE);
}

// network.rs:462 — sendmsg_on_behalf iov_len
let iov_len = /* ... */ as usize;
if iov_len > MAX_SEND_BUF {
    return NotifAction::Errno(libc::EMSGSIZE);
}

64 MiB is well above any realistic sendto — net.core.wmem_max defaults to ~208 KB on most systems.

Test

#[tokio::test]
async fn test_sendto_large_buffer_rejected() {
    let policy = Policy::builder()
        .fs_read("/")
        .net_connect(&[80])
        .build()
        .unwrap();

    let result = Sandbox::run(&policy, &[
        "python3", "-c",
        "import socket; s = socket.socket(); s.sendto(b'x', 0, ('127.0.0.1', 80))"
        // Normal sendto works; the cap only blocks unreasonably large buf_len
    ]).await.unwrap();
    // Just verify the supervisor didn't crash
    assert!(result.code().is_some());
}

Part 2: Dry-run FFI result accessors — missing null pointer checks

File: crates/sandlock-ffi/src/lib.rs:1021-1107

Seven sandlock_dry_run_result_* functions dereference the result pointer without checking for null. The regular sandlock_result_* functions at lines 882-932 correctly include if r.is_null() { return <default>; } guards.

// lib.rs:882 — regular result (correct pattern)
pub unsafe extern "C" fn sandlock_result_exit_code(r: *const sandlock_result_t) -> c_int {
    if r.is_null() { return -1; }
    (*r)._private.code().unwrap_or(-1) as c_int
}

// lib.rs:1021 — dry-run result (missing guard)
pub unsafe extern "C" fn sandlock_dry_run_result_exit_code(r: *const sandlock_dry_run_result_t) -> c_int {
    (*r)._private.run_result.code().unwrap_or(-1) as c_int
    // ^^ null pointer dereference if r is NULL
}

Affected functions

Function Line Return on null
sandlock_dry_run_result_exit_code 1021 -1
sandlock_dry_run_result_success 1030 false
sandlock_dry_run_result_stdout_bytes 1039 null, *len = 0
sandlock_dry_run_result_stderr_bytes 1053 null, *len = 0
sandlock_dry_run_result_changes_len 1067 0
sandlock_dry_run_result_change_kind 1076 0
sandlock_dry_run_result_change_path 1092 null

change_kind and change_path also have no bounds check on the index i — if i >= changes_len, Rust panics in debug or UB in release.

Current mitigation

The Python SDK checks if not result_p before calling these (_sdk.py:1060), so Python users are protected. But C API consumers are not, and the inconsistency with sandlock_result_* makes the API surprising.

Suggested fix

Copy the existing guard pattern from sandlock_result_*:

pub unsafe extern "C" fn sandlock_dry_run_result_exit_code(
    r: *const sandlock_dry_run_result_t,
) -> c_int {
    if r.is_null() { return -1; }
    (*r)._private.run_result.code().unwrap_or(-1) as c_int
}

// For indexed accessors, also add bounds check:
pub unsafe extern "C" fn sandlock_dry_run_result_change_kind(
    r: *const sandlock_dry_run_result_t, i: usize,
) -> c_char {
    if r.is_null() { return 0; }
    let changes = &(*r)._private.changes;
    if i >= changes.len() { return 0; }
    match changes[i].kind {
        ChangeKind::Added => b'A' as c_char,
        ChangeKind::Modified => b'M' as c_char,
        ChangeKind::Deleted => b'D' as c_char,
    }
}

Summary

Part Severity Fix Lines changed
sendto buf_len High (DoS) Add 2 cap checks + EMSGSIZE ~6 lines
dry-run null checks Medium (segfault for C consumers) Add 7 null guards + 2 bounds checks ~14 lines

dzerik avatar Apr 06 '26 08:04 dzerik