use anyhow::{anyhow, Result};
use clap::{Parser, Subcommand};
use sandlock_core::profile;
use sandlock_core::sandbox::{parse_timestamp, BranchAction, ByteSize, SandboxBuilder};
use sandlock_core::Sandbox;
use std::path::PathBuf;
#[cfg(all(feature = "profiles", feature = "cow"))]
mod learn;
#[derive(Parser)]
#[command(name = "sandlock", about = "Lightweight process sandbox", version)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
Run(RunArgs),
Check,
Ps,
Inspect {
name: String,
#[arg(long)]
toml: bool,
},
Kill {
name: String,
},
Profile {
#[command(subcommand)]
action: ProfileAction,
},
Learn(LearnArgs),
}
#[derive(Clone, Debug)]
enum GpuSelection {
All,
Devices(Vec<u32>),
}
impl std::str::FromStr for GpuSelection {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.eq_ignore_ascii_case("all") {
return Ok(GpuSelection::All);
}
let devices = s
.split(',')
.map(|p| {
let p = p.trim();
p.parse::<u32>().map_err(|_| {
format!("invalid GPU index '{p}'; expected 'all' or indices like 0,2")
})
})
.collect::<Result<Vec<_>, _>>()?;
Ok(GpuSelection::Devices(devices))
}
}
#[derive(clap::Args)]
struct RunArgs {
#[clap(flatten)]
sandbox_builder: SandboxBuilder,
#[arg(short = 'm', long = "max-memory")]
max_memory: Option<String>,
#[arg(long = "max-disk")]
max_disk: Option<String>,
#[arg(long)]
time_start: Option<String>,
#[arg(long = "fs-mount", value_name = "VIRTUAL:HOST[:ro]")]
fs_mount: Vec<String>,
#[arg(long = "on-exit", value_name = "ACTION")]
on_exit: Option<String>,
#[arg(long = "on-error", value_name = "ACTION")]
on_error: Option<String>,
#[arg(long = "env", value_name = "KEY=VALUE")]
env_vars: Vec<String>,
#[arg(long = "uname", value_name = "FIELD=VALUE")]
uname: Vec<String>,
#[arg(long = "cpu-cores", value_delimiter = ',')]
cpu_cores: Vec<u32>,
#[arg(long = "gpu", value_name = "all|N,N,...")]
gpu: Option<GpuSelection>,
#[arg(short = 't', long)]
timeout: Option<u64>,
#[arg(short = 'p', long, conflicts_with = "profile_file")]
profile: Option<String>,
#[arg(long = "profile-file", value_name = "PATH", conflicts_with = "profile")]
profile_file: Option<PathBuf>,
#[arg(long = "status-fd", value_name = "FD")]
status_fd: Option<i32>,
#[arg(short = 'j', long = "wait-jobs")]
wait_jobs: bool,
#[arg(long)]
name: Option<String>,
#[arg(short = 'e', long = "exec-shell", value_name = "CMD")]
exec_shell: Option<String>,
#[arg(long, value_name = "IMAGE")]
image: Option<String>,
#[arg(long)]
dry_run: bool,
#[arg(long)]
no_supervisor: bool,
#[arg(long = "allow-degraded", value_name = "PROTECTION")]
allow_degraded: Vec<String>,
#[arg(long = "disable", value_name = "PROTECTION")]
disable: Vec<String>,
#[arg(last = true)]
cmd: Vec<String>,
}
fn parse_protection(s: &str) -> Result<sandlock_core::Protection, String> {
use sandlock_core::Protection;
match s.to_ascii_lowercase().as_str() {
"fs-refer" => Ok(Protection::FsRefer),
"fs-truncate" => Ok(Protection::FsTruncate),
"net-tcp" => Ok(Protection::NetTcp),
"fs-ioctl-dev" => Ok(Protection::FsIoctlDev),
"signal-scope" => Ok(Protection::SignalScope),
"abstract-unix-socket-scope" => Ok(Protection::AbstractUnixSocketScope),
other => Err(format!(
"unknown protection: {} (valid: fs-refer, fs-truncate, net-tcp, fs-ioctl-dev, signal-scope, abstract-unix-socket-scope)",
other,
)),
}
}
#[derive(Subcommand)]
enum ProfileAction {
List,
Show { name: String },
Delete { name: String },
}
#[derive(clap::Args)]
struct LearnArgs {
#[arg(short = 'o', long, value_name = "PATH")]
output: Option<PathBuf>,
#[arg(long, value_name = "PATH")]
merge: Option<PathBuf>,
#[arg(long, value_name = "SECS")]
timeout: Option<u64>,
#[arg(long, value_name = "N", default_missing_value = "4", num_args = 0..=1)]
collapse: Option<usize>,
#[arg(long, value_name = "PREFIX")]
collapse_prefix: Vec<PathBuf>,
#[arg(long, requires = "collapse_prefix")]
force_sensitive_collapse: bool,
#[arg(long, value_name = "PATH")]
http_inject_ca: Vec<PathBuf>,
#[arg(long, value_name = "PORT")]
http_port: Vec<u16>,
#[arg(long, value_name = "PATH")]
http_ca_out: Option<PathBuf>,
#[arg(long = "env", value_name = "KEY=VALUE")]
env_vars: Vec<String>,
#[arg(last = true, required = true)]
cmd: Vec<String>,
}
#[derive(serde::Serialize)]
struct SandboxStatus {
exit_code: i32,
#[serde(skip_serializing_if = "Option::is_none")]
signal: Option<i32>,
}
#[tokio::main(worker_threads = 2)]
async fn main() -> Result<()> {
sandlock_core::logging::log_to_stderr();
let cli = Cli::parse();
match cli.command {
Command::Run(args) => {
let code = run_command(args).await?;
std::process::exit(code);
}
Command::Ps => {
let names = match sandlock_core::control::list_sandboxes() {
Ok(n) => n,
Err(e) => {
eprintln!("sandlock: failed to list sandboxes: {}", e);
std::process::exit(1);
}
};
if names.is_empty() {
println!("No running sandboxes.");
} else {
println!("NAME PID UPTIME STATUS PORTS CMD");
for name in &names {
let pid = sandlock_core::control::sandbox_pids(name).map(|p| p.child);
let main_fd = pid.as_ref().ok().and_then(|&pid| pidfd_open(pid));
let (status, ports, pid, uptime, cmd) =
match (sandlock_core::control::sandbox_info(name), pid) {
(Ok(info), Ok(pid)) if !info.main_exited => {
let uptime = proc_uptime(pid);
let cmd = proc_cmdline(pid);
if main_fd.as_ref().is_some_and(|fd| !pidfd_exited(fd)) {
(
info.mode.unwrap_or_else(|| "running".to_string()),
query_ports(name),
pid.to_string(),
uptime.unwrap_or_else(|| "?".to_string()),
cmd.unwrap_or_else(|| "?".to_string()),
)
} else {
main_exited_row(name, info)
}
}
(Ok(info), _) if info.main_exited => main_exited_row(name, info),
(info, pid) => (
if info.is_ok() {
"running"
} else {
"unresponsive"
}
.to_string(),
"?".to_string(),
pid.map_or_else(|_| "?".to_string(), |p| p.to_string()),
"?".to_string(),
"?".to_string(),
),
};
println!(
"{:<32} {:>8} {:>12} {:<10} {:<24} {}",
name, pid, uptime, status, ports, cmd
);
}
}
}
Command::Inspect { name, toml } => {
if let Err(e) = validate_cli_name(&name) {
eprintln!("sandlock: {e}");
std::process::exit(1);
}
match sandlock_core::control::send_control_request(
&name,
"config",
serde_json::Value::Object(Default::default()),
) {
Ok(resp) if resp.ok => {
if let Some(data) = resp.data {
if toml {
#[cfg(not(feature = "profiles"))]
return Err(anyhow!("TOML output requires the profiles feature"));
#[cfg(feature = "profiles")]
{
let profile: sandlock_core::ProfileInput =
serde_json::from_value(data)
.map_err(|e| anyhow!("parse config response: {}", e))?;
let toml_str = toml::to_string_pretty(&profile)?;
println!("{}", toml_str);
}
} else {
let json_str = serde_json::to_string_pretty(&data)?;
println!("{}", json_str);
}
} else {
eprintln!("sandlock: empty response from sandbox");
std::process::exit(1);
}
}
Ok(resp) => {
eprintln!(
"sandlock: inspect error: {}",
resp.err.as_deref().unwrap_or("unknown")
);
std::process::exit(1);
}
Err(e) => {
eprintln!("sandlock: inspect failed: {}", e);
std::process::exit(1);
}
}
}
Command::Kill { name } => {
if let Err(e) = validate_cli_name(&name) {
eprintln!("sandlock: {e}");
std::process::exit(1);
}
if let Err(e) = sandlock_core::control::kill_sandbox(&name) {
eprintln!("sandlock: kill '{}': {}", name, e);
std::process::exit(1);
}
println!("Killed sandbox '{}'", name);
}
Command::Check => {
println!("Kernel feature support:");
match sandlock_core::landlock_abi_version() {
Ok(v) => {
println!(" Landlock: ABI v{}", v);
println!(
" Minimum required: ABI v{}",
sandlock_core::MIN_LANDLOCK_ABI
);
if v < sandlock_core::MIN_LANDLOCK_ABI {
println!(" Status: UNSUPPORTED (upgrade kernel)");
} else {
println!(" Status: OK");
}
println!(" Filesystem: supported (ABI v1+)");
println!(
" File truncate: {}",
if v >= 3 {
"supported (ABI v3+)"
} else {
"not supported"
}
);
println!(
" TCP ports: {}",
if v >= 4 {
"supported (ABI v4+)"
} else {
"not supported"
}
);
println!(
" Device ioctl: {}",
if v >= 5 {
"supported (ABI v5+)"
} else {
"not supported"
}
);
println!(
" IPC scoping: {}",
if v >= 6 {
"supported (ABI v6+)"
} else {
"not supported"
}
);
println!(
" Signal scoping: {}",
if v >= 6 {
"supported (ABI v6+)"
} else {
"not supported"
}
);
println!();
println!("Per-protection availability (host Landlock ABI v{}):", v);
for p in sandlock_core::Protection::all() {
let available = v >= p.min_abi();
let marker = if available {
"available"
} else {
"unavailable"
};
println!(
" {:<22} requires v{} — {}",
format!("{:?}", p),
p.min_abi(),
marker
);
}
}
Err(e) => {
println!(" Landlock: unavailable ({})", e);
println!(" Status: UNSUPPORTED");
}
}
println!(" Platform: {}", std::env::consts::ARCH);
}
Command::Learn(args) => {
#[cfg(all(feature = "profiles", feature = "cow"))]
learn::run(args).await?;
#[cfg(not(all(feature = "profiles", feature = "cow")))]
return Err(anyhow!(
"learning profiles requires the profiles and cow features"
));
}
Command::Profile { action } => match action {
ProfileAction::List => {
let profiles = profile::list_profiles()?;
if profiles.is_empty() {
println!("No profiles found in {}", profile::profile_dir()?.display());
} else {
for name in profiles {
println!(" {}", name);
}
}
}
ProfileAction::Show { name } => {
let path = profile::profile_dir()?.join(format!("{}.toml", name));
let content = std::fs::read_to_string(&path)?;
println!("{}", content);
}
ProfileAction::Delete { name } => {
let path = profile::profile_dir()?.join(format!("{}.toml", name));
std::fs::remove_file(&path)?;
println!("Deleted profile '{}'", name);
}
},
}
Ok(())
}
async fn run_command(args: RunArgs) -> Result<i32> {
let pb = &args.sandbox_builder;
if args.no_supervisor {
validate_no_supervisor(&args)?;
}
let (base_from_profile, profile_program_spec) = if let Some(ref name) = args.profile {
let (base, spec) = profile::load_profile(name)?;
(Some(base), Some(spec))
} else if let Some(ref path) = args.profile_file {
let content = std::fs::read_to_string(path)
.map_err(|e| anyhow!("failed to read profile file {}: {}", path.display(), e))?;
let (base, spec) = profile::parse_profile(&content)?;
(Some(base), Some(spec))
} else {
(None, None)
};
if args.no_supervisor {
if let Some(ref base) = base_from_profile {
validate_no_supervisor_profile(base, &profile_source(&args))?;
}
}
let profile_hint_cmd: Option<String> = if !args.cmd.is_empty() {
Some(args.cmd.join(" "))
} else {
profile_program_spec.as_ref().and_then(|spec| {
let exec = spec.exec.as_ref()?.display().to_string();
let args_str = spec.args.join(" ");
Some(if args_str.is_empty() {
exec
} else {
format!("{exec} {args_str}")
})
})
};
let mut builder = if let Some(base) = base_from_profile {
let mut b = Sandbox::builder();
b.fs_readable.extend(base.fs_readable.iter().cloned());
b.fs_writable.extend(base.fs_writable.iter().cloned());
for p in &base.fs_denied {
b = b.fs_deny(p);
}
for rule in &base.net_allow {
b = b.net_allow(sandlock_core::format_net_rule(rule));
}
for rule in &base.net_deny {
b = b.net_deny(sandlock_core::format_net_rule(rule));
}
match &base.net_allow_bind {
sandlock_core::BindPorts::All => b = b.net_allow_bind("*"),
sandlock_core::BindPorts::Ports(ports) => {
for p in ports {
b = b.net_allow_bind_port(*p);
}
}
}
for p in &base.net_deny_bind {
b = b.net_deny_bind_port(*p);
}
for rule in &base.http_allow {
let s = format!("{} {}{}", rule.method, rule.host, rule.path);
b = b.http_allow(&s);
}
for rule in &base.http_deny {
let s = format!("{} {}{}", rule.method, rule.host, rule.path);
b = b.http_deny(&s);
}
for port in &base.http_ports {
b = b.http_port(*port);
}
if let Some(mem) = base.max_memory {
b = b.max_memory(mem);
}
if let Some(n) = base.max_processes {
b = b.max_processes(n);
}
if let Some(cpu) = base.max_cpu {
b = b.max_cpu(cpu);
}
if let Some(seed) = base.random_seed {
b = b.random_seed(seed);
}
if let Some(n) = base.num_cpus {
b = b.num_cpus(n);
}
b.uname = base.uname.clone();
if let Some(n) = base.max_open_files {
b = b.max_open_files(n);
}
if let Some(disk) = base.max_disk {
b = b.max_disk(disk);
}
if !base.extra_deny_syscalls.is_empty() {
b = b.extra_deny_syscalls(base.extra_deny_syscalls.clone());
}
if !base.extra_allow_syscalls.is_empty() {
b = b.extra_allow_syscalls(base.extra_allow_syscalls.clone());
}
b = b.clean_env(base.clean_env);
for (k, v) in &base.env {
b = b.env_var(k, v);
}
if let Some(ref w) = base.workdir {
b = b.workdir(w);
}
if let Some(ref c) = base.cwd {
b = b.cwd(c);
}
if let Some(ref ca) = base.http_ca {
b = b.http_ca(ca);
}
if let Some(ref key) = base.http_key {
b = b.http_key(key);
}
for p in &base.http_inject_ca {
b = b.http_inject_ca(p);
}
if let Some(ref out) = base.http_ca_out {
b = b.http_ca_out(out);
}
if let Some(ref path) = base.chroot {
b = b.chroot(path);
}
if let Some(ref path) = base.fs_storage {
b = b.fs_storage(path);
}
for (virt, host) in &base.fs_mount {
if base.fs_mount_ro.iter().any(|d| d == virt) {
b = b.fs_mount_ro(virt, host);
} else {
b = b.fs_mount(virt, host);
}
}
b = b.on_exit(base.on_exit.clone());
b = b.on_error(base.on_error.clone());
b = b.deterministic_dirs(base.deterministic_dirs);
b = b.no_randomize_memory(base.no_randomize_memory);
b = b.no_huge_pages(base.no_huge_pages);
b = b.no_coredump(base.no_coredump);
if let Some(t) = base.time_start {
b = b.time_start(t);
}
b = b.port_remap(base.port_remap);
if let Some(user) = base.user {
b = b.user(user.uid, user.gid);
}
if let Some(ref devs) = base.gpu_devices {
b = b.gpu_devices(devs.clone());
}
if let Some(ref cores) = base.cpu_cores {
b = b.cpu_cores(cores.clone());
}
b
} else {
Sandbox::builder()
};
builder.fs_readable.extend(pb.fs_readable.iter().cloned());
builder.fs_writable.extend(pb.fs_writable.iter().cloned());
if let Some(n) = pb.max_processes {
builder = builder.max_processes(n);
}
for spec in &pb.net_allow {
builder = builder.net_allow(spec);
}
for spec in &pb.net_deny {
builder = builder.net_deny(spec);
}
for spec in &pb.net_allow_bind {
builder = builder.net_allow_bind(spec);
}
for spec in &pb.net_deny_bind {
builder = builder.net_deny_bind(spec);
}
if let Some(seed) = pb.random_seed {
builder = builder.random_seed(seed);
}
if pb.clean_env {
builder = builder.clean_env(true);
}
if let Some(n) = pb.num_cpus {
builder = builder.num_cpus(n);
}
if let Some(cpu) = pb.max_cpu {
builder = builder.max_cpu(cpu);
}
if let Some(n) = pb.max_open_files {
builder = builder.max_open_files(n);
}
for p in &pb.fs_denied {
builder = builder.fs_deny(p);
}
if let Some(ref path) = pb.chroot {
builder = builder.chroot(path);
}
if let Some(user) = pb.user {
builder = builder.user(user.uid, user.gid);
}
if let Some(ref path) = pb.workdir {
builder = builder.workdir(path);
}
if let Some(ref path) = pb.cwd {
builder = builder.cwd(path);
}
if let Some(ref path) = pb.fs_storage {
builder = builder.fs_storage(path);
}
if !pb.extra_allow_syscalls.is_empty() {
builder = builder.extra_allow_syscalls(pb.extra_allow_syscalls.clone());
}
if !pb.extra_deny_syscalls.is_empty() {
builder = builder.extra_deny_syscalls(pb.extra_deny_syscalls.clone());
}
for rule in &pb.http_allow {
builder = builder.http_allow(rule);
}
for rule in &pb.http_deny {
builder = builder.http_deny(rule);
}
for c in &pb.credentials {
builder = builder.credential_spec(c);
}
for rule in &pb.http_auth {
builder = builder.http_auth(rule);
}
for port in &pb.http_ports {
builder = builder.http_port(*port);
}
if let Some(ref ca) = pb.http_ca {
builder = builder.http_ca(ca);
}
if let Some(ref key) = pb.http_key {
builder = builder.http_key(key);
}
for p in &pb.http_inject_ca {
builder = builder.http_inject_ca(p);
}
if let Some(ref out) = pb.http_ca_out {
builder = builder.http_ca_out(out);
}
if pb.port_remap {
builder = builder.port_remap(true);
}
if pb.no_randomize_memory {
builder = builder.no_randomize_memory(true);
}
if pb.no_huge_pages {
builder = builder.no_huge_pages(true);
}
if pb.deterministic_dirs {
builder = builder.deterministic_dirs(true);
}
if pb.no_coredump {
builder = builder.no_coredump(true);
}
if let Some(ref m) = args.max_memory {
builder = builder.max_memory(ByteSize::parse(m)?);
}
if let Some(ref ts) = args.time_start {
builder = builder.time_start(parse_timestamp("--time-start", ts)?);
}
if let Some(ref s) = args.max_disk {
builder = builder.max_disk(ByteSize::parse(s)?);
}
if let Some(ref s) = args.on_exit {
builder = builder.on_exit(parse_branch_action("--on-exit", s)?);
}
if let Some(ref s) = args.on_error {
builder = builder.on_error(parse_branch_action("--on-error", s)?);
}
for spec in &args.fs_mount {
let (virt, host, read_only) = profile::parse_mount_spec(spec)?;
builder = if read_only {
builder.fs_mount_ro(virt, host)
} else {
builder.fs_mount(virt, host)
};
}
if !args.cpu_cores.is_empty() {
builder = builder.cpu_cores(args.cpu_cores.clone());
}
for spec in &args.uname {
builder = builder.uname_spec(spec);
}
match &args.gpu {
Some(GpuSelection::All) => builder = builder.gpu_devices(Vec::new()),
Some(GpuSelection::Devices(devs)) => builder = builder.gpu_devices(devs.clone()),
None => {}
}
for spec in &args.env_vars {
if let Some((k, v)) = spec.split_once('=') {
builder = builder.env_var(k, v);
} else {
return Err(anyhow!("--env requires KEY=VALUE, got: {}", spec));
}
}
let sandbox_name = args.name.clone();
let image_cmd: Option<Vec<String>>;
if let Some(ref img) = args.image {
#[cfg(not(feature = "image"))]
return Err(anyhow!(
"image support is disabled; enable the image feature"
));
#[cfg(feature = "image")]
{
let image = sandlock_core::image::pull(img, None).await?;
builder = builder.image(&image);
image_cmd = args.cmd.is_empty().then(|| image.config.default_cmd());
}
} else {
image_cmd = None;
}
let profile_cmd: Option<Vec<String>> =
if args.cmd.is_empty() && args.exec_shell.is_none() && image_cmd.is_none() {
if let Some(spec) = profile_program_spec {
if let Some(exec) = spec.exec {
let exec_str = exec
.into_os_string()
.into_string()
.map_err(|_| anyhow!("non-UTF-8 exec path in profile"))?;
let mut v = vec![exec_str];
v.extend(spec.args);
Some(v)
} else {
None
}
} else {
None
}
} else {
None
};
if args.exec_shell.is_none()
&& args.cmd.is_empty()
&& image_cmd.is_none()
&& profile_cmd.is_none()
{
return Err(anyhow!(
"no command specified (no trailing command and no [program].exec in profile)"
));
}
if args.no_supervisor {
builder = builder.no_supervisor(true);
}
for s in &args.allow_degraded {
builder = builder.allow_degraded(parse_protection(s).map_err(|e| anyhow!(e))?);
}
for s in &args.disable {
builder = builder.disable(parse_protection(s).map_err(|e| anyhow!(e))?);
}
let policy = builder.build()?;
let cmd_strs: Vec<&str> = if let Some(ref shell_cmd) = args.exec_shell {
vec!["/bin/sh", "-c", shell_cmd.as_str()]
} else if let Some(ref ic) = image_cmd {
ic.iter().map(|s| s.as_str()).collect()
} else if !args.cmd.is_empty() {
args.cmd.iter().map(|s| s.as_str()).collect()
} else if let Some(ref pc) = profile_cmd {
pc.iter().map(|s| s.as_str()).collect()
} else {
unreachable!("no command source available")
};
let mut policy = if let Some(ref name) = sandbox_name {
policy.with_name(name.clone())
} else {
policy
};
if args.dry_run {
if policy.workdir.is_none() {
return Err(anyhow!("--dry-run requires --workdir"));
}
policy.on_exit = BranchAction::Abort;
policy.on_error = BranchAction::Abort;
}
let run = async {
if args.wait_jobs {
policy.create_interactive(&cmd_strs).await?;
policy.start()?;
policy.join().await?;
policy.shutdown().await
} else {
policy.run_interactive(&cmd_strs).await
}
};
let result = if let Some(secs) = args.timeout {
match tokio::time::timeout(std::time::Duration::from_secs(secs), run).await {
Ok(r) => r?,
Err(_) => {
eprintln!("sandlock: timeout after {}s", secs);
return Ok(124);
}
}
} else {
run.await?
};
if args.dry_run {
if result.changes.is_empty() {
eprintln!("sandlock: dry-run: no filesystem changes");
} else {
eprintln!("sandlock: dry-run: filesystem changes:");
for change in &result.changes {
eprintln!("{change}");
}
}
}
if let Some(fd) = args.status_fd {
use sandlock_core::ExitStatus as SandlockExitStatus;
use std::io::Write as _;
use std::os::unix::io::FromRawFd;
let (code, signal) = match &result.exit_status {
SandlockExitStatus::Code(c) => (*c, None),
SandlockExitStatus::Signal(s) => (-1, Some(*s)),
SandlockExitStatus::Killed => (-1, None),
SandlockExitStatus::Timeout => (-1, None),
};
let status = SandboxStatus {
exit_code: code,
signal,
};
if let Ok(json) = serde_json::to_string(&status) {
let mut file = unsafe { std::fs::File::from_raw_fd(fd) };
let _ = writeln!(file, "{}", json);
std::mem::forget(file); }
}
let exit_code = result.code().unwrap_or(1);
if exit_code != 0 {
if let Some(ref path) = args.profile_file {
if let Some(ref cmd_str) = profile_hint_cmd {
eprintln!(
"hint: if a denial caused this, extend the profile with:\n \
sandlock learn --merge {} -- {cmd_str}",
path.display()
);
}
}
}
Ok(exit_code)
}
fn validate_no_supervisor(args: &RunArgs) -> Result<()> {
let pb = &args.sandbox_builder;
let mut bad = Vec::new();
if args.max_memory.is_some() {
bad.push("--max-memory");
}
if pb.max_processes.is_some() {
bad.push("--max-processes");
}
if pb.max_cpu.is_some() {
bad.push("--max-cpu");
}
if pb.max_open_files.is_some() {
bad.push("--max-open-files");
}
if args.timeout.is_some() {
bad.push("--timeout");
}
if !pb.net_allow.is_empty() {
bad.push("--net-allow");
}
if !pb.net_deny.is_empty() {
bad.push("--net-deny");
}
if !pb.net_allow_bind.is_empty() {
bad.push("--net-allow-bind");
}
if !pb.net_deny_bind.is_empty() {
bad.push("--net-deny-bind");
}
if !pb.http_allow.is_empty() {
bad.push("--http-allow");
}
if !pb.http_deny.is_empty() {
bad.push("--http-deny");
}
if !pb.http_ports.is_empty() {
bad.push("--http-port");
}
if pb.num_cpus.is_some() {
bad.push("--num-cpus");
}
if pb.random_seed.is_some() {
bad.push("--random-seed");
}
if args.time_start.is_some() {
bad.push("--time-start");
}
if pb.no_randomize_memory {
bad.push("--no-randomize-memory");
}
if pb.no_huge_pages {
bad.push("--no-huge-pages");
}
if pb.deterministic_dirs {
bad.push("--deterministic-dirs");
}
if pb.chroot.is_some() {
bad.push("--chroot");
}
if args.image.is_some() {
bad.push("--image");
}
if pb.user.is_some() {
bad.push("--user");
}
if pb.workdir.is_some() {
bad.push("--workdir");
}
if pb.cwd.is_some() {
bad.push("--cwd");
}
if pb.fs_storage.is_some() {
bad.push("--fs-storage");
}
if args.max_disk.is_some() {
bad.push("--max-disk");
}
if pb.port_remap {
bad.push("--port-remap");
}
if !args.cpu_cores.is_empty() {
bad.push("--cpu-cores");
}
if !args.uname.is_empty() {
bad.push("--uname");
}
if args.gpu.is_some() {
bad.push("--gpu");
}
if args.dry_run {
bad.push("--dry-run");
}
if args.status_fd.is_some() {
bad.push("--status-fd");
}
if !pb.fs_denied.is_empty() {
bad.push("--fs-deny");
}
if !args.fs_mount.is_empty() {
bad.push("--fs-mount");
}
if !bad.is_empty() {
return Err(anyhow!(
"--no-supervisor is incompatible with: {}",
bad.join(", ")
));
}
Ok(())
}
fn profile_source(args: &RunArgs) -> String {
args.profile
.as_deref()
.map(|n| format!("profile {n}"))
.unwrap_or_else(|| {
let path = args
.profile_file
.as_ref()
.expect("profile_source called without a loaded profile");
format!("profile file {}", path.display())
})
}
fn validate_no_supervisor_profile(profile: &Sandbox, source: &str) -> Result<()> {
let mut bad = Vec::new();
if !profile.fs_denied.is_empty() {
bad.push("[filesystem].deny");
}
if !profile.net_allow.is_empty() {
bad.push("[network].allow");
}
if !profile.net_deny.is_empty() {
bad.push("[network].deny");
}
if !profile.net_allow_bind.is_default() {
bad.push("[network].allow_bind");
}
if !profile.net_deny_bind.is_empty() {
bad.push("[network].deny_bind");
}
if profile.port_remap {
bad.push("[network].port_remap");
}
if !profile.http_allow.is_empty() {
bad.push("[http].allow");
}
if !profile.http_deny.is_empty() {
bad.push("[http].deny");
}
if !profile.http_ports.is_empty() {
bad.push("[http].ports");
}
if profile.http_ca.is_some() {
bad.push("[config].http_ca");
}
if profile.http_key.is_some() {
bad.push("[config].http_key");
}
if profile.max_memory.is_some() {
bad.push("[limits].memory");
}
if profile.max_processes.is_some() {
bad.push("[limits].processes");
}
if profile.max_open_files.is_some() {
bad.push("[limits].open_files");
}
if profile.max_cpu.is_some() {
bad.push("[limits].cpu");
}
if profile.max_disk.is_some() {
bad.push("[limits].disk");
}
if profile.gpu_devices.is_some() {
bad.push("[limits].gpu_devices");
}
if profile.cpu_cores.is_some() {
bad.push("[limits].cpu_cores");
}
if profile.num_cpus.is_some() {
bad.push("[limits].num_cpus");
}
if !profile.uname.is_empty() {
bad.push("[uname]");
}
if profile.random_seed.is_some() {
bad.push("[determinism].random_seed");
}
if profile.time_start.is_some() {
bad.push("[determinism].time_start");
}
if profile.deterministic_dirs {
bad.push("[determinism].deterministic_dirs");
}
if profile.no_randomize_memory {
bad.push("[determinism].no_randomize_memory");
}
if profile.no_huge_pages {
bad.push("[program].no_huge_pages");
}
if profile.no_coredump {
bad.push("[program].no_coredump");
}
if profile.workdir.is_some() {
bad.push("[config].workdir");
}
if profile.fs_storage.is_some() {
bad.push("[config].fs_storage");
}
if profile.cwd.is_some() {
bad.push("[program].cwd");
}
if profile.user.is_some() {
bad.push("[program].uid");
}
if profile.chroot.is_some() {
bad.push("[filesystem].chroot");
}
if !profile.fs_mount.is_empty() {
bad.push("[filesystem].mount");
}
if profile.on_exit != BranchAction::Commit {
bad.push("[filesystem].on_exit");
}
if profile.on_error != BranchAction::Abort {
bad.push("[filesystem].on_error");
}
if !bad.is_empty() {
return Err(anyhow!(
"--no-supervisor is incompatible with {} field(s): {}",
source,
bad.join(", ")
));
}
Ok(())
}
fn parse_branch_action(flag: &str, s: &str) -> Result<BranchAction> {
match s {
"commit" => Ok(BranchAction::Commit),
"abort" => Ok(BranchAction::Abort),
"keep" => Ok(BranchAction::Keep),
other => Err(anyhow!(
"invalid {} value '{}': expected commit | abort | keep",
flag,
other
)),
}
}
fn validate_cli_name(name: &str) -> Result<(), String> {
if name.is_empty() {
return Err("sandbox name must not be empty".into());
}
if name.len() > 64 {
return Err("sandbox name must be at most 64 bytes".into());
}
if name.contains('\0') {
return Err("sandbox name must not contain NUL bytes".into());
}
if name.bytes().any(|b| b <= 0x20 || b == 0x7f) {
return Err("sandbox name must not contain whitespace or control characters".into());
}
if name.contains('/') {
return Err("sandbox name must not contain '/'".into());
}
if name == "." || name == ".." {
return Err("sandbox name must not be '.' or '..'".into());
}
Ok(())
}
fn main_exited_row(
name: &str,
info: sandlock_core::control::SandboxInfo,
) -> (String, String, String, String, String) {
let cmd = match info.live_groups {
0 => "(main exited, idle)".to_string(),
1 => "(main exited, 1 group)".to_string(),
n => format!("(main exited, {n} groups)"),
};
(
info.mode.unwrap_or_else(|| "running".to_string()),
query_ports(name),
"-".to_string(),
"-".to_string(),
cmd,
)
}
fn pidfd_open(pid: i32) -> Option<std::os::fd::OwnedFd> {
use std::os::fd::FromRawFd;
let fd = unsafe { libc::syscall(libc::SYS_pidfd_open, pid, 0) };
(fd >= 0).then(|| unsafe { std::os::fd::OwnedFd::from_raw_fd(fd as i32) })
}
fn pidfd_exited(fd: &std::os::fd::OwnedFd) -> bool {
use std::os::fd::AsRawFd;
let mut poll = libc::pollfd {
fd: fd.as_raw_fd(),
events: libc::POLLIN,
revents: 0,
};
unsafe { libc::poll(&mut poll, 1, 0) != 0 }
}
fn query_ports(name: &str) -> String {
use sandlock_core::control::send_control_request;
match send_control_request(name, "ports", serde_json::Value::Object(Default::default())) {
Ok(resp) if resp.ok => {
if let Some(data) = resp.data {
let map: std::collections::HashMap<u16, u16> = match serde_json::from_value(data) {
Ok(m) => m,
Err(_) => return "-".to_string(),
};
if map.is_empty() {
return "-".to_string();
}
let mut entries: Vec<String> =
map.iter().map(|(v, r)| format!("{}→{}", v, r)).collect();
entries.sort();
entries.join(", ")
} else {
"-".to_string()
}
}
_ => "-".to_string(),
}
}
fn proc_uptime(pid: i32) -> Option<String> {
let stat = std::fs::read_to_string(format!("/proc/{}/stat", pid)).ok()?;
let starttime: u64 = stat
.split(')')
.nth(1)?
.split_whitespace()
.nth(19)?
.parse()
.ok()?;
let ticks_per_sec = unsafe { libc::sysconf(libc::_SC_CLK_TCK) } as u64;
let uptime_secs = if ticks_per_sec > 0 {
let boot_secs = proc_uptime_sys()?;
boot_secs.saturating_sub(starttime / ticks_per_sec)
} else {
return None;
};
if uptime_secs < 60 {
Some(format!("{}s", uptime_secs))
} else if uptime_secs < 3600 {
Some(format!("{}m", uptime_secs / 60))
} else if uptime_secs < 86400 {
Some(format!(
"{}h{}m",
uptime_secs / 3600,
(uptime_secs % 3600) / 60
))
} else {
Some(format!(
"{}d{}h",
uptime_secs / 86400,
(uptime_secs % 86400) / 3600
))
}
}
fn proc_uptime_sys() -> Option<u64> {
let s = std::fs::read_to_string("/proc/uptime").ok()?;
let secs: f64 = s.split_whitespace().next()?.parse().ok()?;
Some(secs as u64)
}
fn proc_cmdline(pid: i32) -> Option<String> {
let raw = std::fs::read(format!("/proc/{}/cmdline", pid)).ok()?;
if raw.is_empty() {
return Some("?".to_string());
}
let args: Vec<&str> = raw
.split(|b| *b == 0)
.filter_map(|chunk| std::str::from_utf8(chunk).ok())
.filter(|s| !s.is_empty())
.collect();
if args.is_empty() {
Some("?".to_string())
} else {
let joined = args.join(" ");
if joined.len() > 60 {
let end = joined.floor_char_boundary(57);
Some(format!("{}…", &joined[..end]))
} else {
Some(joined)
}
}
}
#[cfg(test)]
mod render_tests {
use sandlock_core::sandbox::NetRule;
#[test]
fn render_allow_drops_redundant_all_ports_star() {
let r = &NetRule::parse_allow("udp://*:*").unwrap()[0];
assert_eq!(sandlock_core::format_net_rule(r), "udp://*");
}
#[test]
fn render_allow_any_ip_all_ports_schemeless_pair() {
let rules = NetRule::parse_allow(":*").unwrap();
let rendered: Vec<String> = rules.iter().map(sandlock_core::format_net_rule).collect();
assert_eq!(rendered, vec!["tcp://*", "udp://*"]);
}
#[test]
fn render_allow_host_ports() {
let rules = NetRule::parse_allow("example.com:443").unwrap();
assert_eq!(
sandlock_core::format_net_rule(&rules[0]),
"tcp://example.com:443"
);
assert_eq!(
sandlock_core::format_net_rule(&rules[1]),
"udp://example.com:443"
);
}
#[test]
fn render_cidr_and_ipv6_round_trip() {
assert_eq!(
sandlock_core::format_net_rule(&NetRule::parse_allow("10.0.0.0/8:80").unwrap()[0]),
"tcp://10.0.0.0/8:80"
);
assert_eq!(
sandlock_core::format_net_rule(&NetRule::parse_deny("10.0.0.0/8").unwrap()[0]),
"tcp://10.0.0.0/8"
);
assert_eq!(
sandlock_core::format_net_rule(&NetRule::parse_allow("[::1]:443").unwrap()[0]),
"tcp://[::1]:443"
);
assert_eq!(
sandlock_core::format_net_rule(&NetRule::parse_allow("::1").unwrap()[0]),
"tcp://::1"
);
}
#[test]
fn render_roundtrips_through_parse() {
for spec in [
"example.com:443",
"udp://1.1.1.1:53",
"icmp://github.com",
"*",
"udp://*",
"tcp://*",
"10.0.0.0/8:80",
"[fc00::/7]:443",
"::1",
"1.2.3.4",
] {
for r in &NetRule::parse_allow(spec).unwrap() {
let rendered = sandlock_core::format_net_rule(r);
let reparsed = NetRule::parse_allow(&rendered).unwrap();
assert_eq!(
reparsed.len(),
1,
"rendered `{rendered}` from {spec} not single"
);
assert_eq!(
*r, reparsed[0],
"round-trip mismatch for {spec} via `{rendered}`"
);
}
}
}
}