Skip to main content

Crate rustnet_sandbox

Crate rustnet_sandbox 

Source
Expand description

Post-initialization sandboxing and privilege dropping for rustnet.

One apply_sandbox entry point with a platform backend behind it:

  • Linux: PR_SET_NO_NEW_PRIVS, capability drops (CAP_NET_RAW, CAP_BPF, CAP_PERFMON), the root uid drop, and Landlock filesystem/network/scope restrictions (behind the landlock feature).
  • macOS: the root uid drop, then a Seatbelt profile restricting outbound network, credential reads, and writes under user home directories (behind the macos-sandbox feature).
  • Windows: dangerous token privileges removed and a job object that blocks child process creation.
  • FreeBSD: the root uid drop (Capsicum is planned).

§Application order contract

Sandboxing is a post-initialization step. Callers must apply it only after every privileged operation is done, in this order:

  1. Open capture handles (raw sockets, BPF/PKTAP devices, Npcap): the descriptors stay valid across the sandbox and uid drop.
  2. Load eBPF programs (needs CAP_BPF/CAP_PERFMON, which the sandbox drops).
  3. Securely open output files (logs, PCAP exports) and, when dropping root, hand ownership to the drop target (privdrop::chown_to_target). Retain these descriptors for writing after the drop instead of reopening paths or granting pathname write exceptions.
  4. Call apply_sandbox on the main thread.
  5. Only then spawn worker threads that should inherit the restrictions: Landlock domains and capability sets are per-thread state that new threads inherit from their spawner; threads started before step 4 keep the unrestricted state.

Long-lived workers should not start before the sandbox. Capture handles and eBPF maps opened during initialization remain usable through their existing descriptors after capability and identity reduction. Workers created after step 4 inherit the reduced capability set and sandbox restrictions.

Modules§

privdrop
Root privilege drop (setuid) for Linux, macOS, and FreeBSD

Structs§

PrivilegeDropError
A requested root UID/GID transition failed.
SandboxConfig
Configuration for the sandbox
SandboxReport
Result of sandbox application

Enums§

SandboxMode
Sandbox enforcement mode
SandboxStatus
Status of sandbox application

Functions§

apply_sandbox
Apply the sandbox with the given configuration.
apply_sandbox_allowing_dns
Apply the sandbox with its narrow platform DNS exception enabled.