#[cfg(feature = "landlock")]
pub mod capabilities;
#[cfg(feature = "landlock")]
mod landlock;
use crate::{SandboxConfig, SandboxMode, SandboxReport, SandboxStatus, privdrop};
fn set_no_new_privs() -> std::io::Result<()> {
let rc = unsafe { libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) };
if rc == 0 {
Ok(())
} else {
Err(std::io::Error::last_os_error())
}
}
#[cfg(feature = "landlock")]
pub(crate) fn apply(config: &SandboxConfig) -> anyhow::Result<SandboxReport> {
use anyhow::Context;
let no_new_privs = match set_no_new_privs() {
Ok(()) => {
log::info!("PR_SET_NO_NEW_PRIVS set");
true
}
Err(e) => {
log::warn!("Failed to set PR_SET_NO_NEW_PRIVS: {}", e);
false
}
};
if !no_new_privs && config.mode == SandboxMode::Strict {
return Err(anyhow::anyhow!(
"Strict mode requires PR_SET_NO_NEW_PRIVS to be settable"
));
}
let landlock_available = landlock::is_available();
if config.mode == SandboxMode::Disabled {
log::info!("Sandbox disabled by configuration");
let message = if no_new_privs {
"Sandbox disabled by configuration (no-new-privs still set)"
} else {
"Sandbox disabled by configuration"
};
return Ok(SandboxReport {
message: message.to_string(),
landlock_available,
no_new_privs,
..SandboxReport::default()
});
}
let mut result = SandboxReport {
status: SandboxStatus::FullyEnforced,
landlock_available,
no_new_privs,
..SandboxReport::default()
};
let mut messages = Vec::new();
if no_new_privs {
messages.push("no-new-privs set".to_string());
} else {
messages.push("no-new-privs could not be set".to_string());
}
if let Err(e) = capabilities::clear_ambient_caps() {
log::debug!("Could not clear ambient capabilities: {}", e);
}
match capabilities::drop_cap_net_raw() {
Ok(dropped) => {
if dropped {
if capabilities::has_cap_net_raw() {
log::error!("CAP_NET_RAW drop reported success but capability still present!");
result.cap_net_raw_dropped = false;
messages.push("CAP_NET_RAW drop verification failed".to_string());
} else {
result.cap_net_raw_dropped = true;
messages.push("CAP_NET_RAW dropped".to_string());
log::info!("Dropped CAP_NET_RAW capability (verified)");
}
} else {
messages.push("CAP_NET_RAW was not held".to_string());
log::debug!("CAP_NET_RAW was not in effective set");
}
}
Err(e) => {
let msg = format!("Failed to drop CAP_NET_RAW: {}", e);
log::warn!("{}", msg);
messages.push(msg);
if config.mode == SandboxMode::Strict {
return Err(e).context("Strict mode requires CAP_NET_RAW to be droppable");
}
result.status = SandboxStatus::PartiallyEnforced;
}
}
match capabilities::drop_ebpf_caps() {
Ok(count) => {
if count > 0 {
result.ebpf_caps_dropped = true;
messages.push(format!("eBPF capabilities dropped ({})", count));
log::info!("Dropped {} eBPF capabilities", count);
} else {
log::debug!("No eBPF capabilities were held");
}
}
Err(e) => {
let msg = format!("Failed to drop eBPF capabilities: {}", e);
log::warn!("{}", msg);
messages.push(msg);
if config.mode == SandboxMode::Strict {
return Err(e).context("Strict mode requires eBPF capabilities to be droppable");
}
}
}
if let Some(target) = config.drop_uid {
match privdrop::drop_to(target) {
Ok(()) => {
result.uid_dropped = true;
messages.push(format!(
"root dropped to uid {} gid {}",
target.uid, target.gid
));
log::info!(
"Dropped root privileges to uid {} gid {} (verified); \
procfs process attribution is now limited to that user's \
processes (eBPF attribution unaffected)",
target.uid,
target.gid
);
}
Err(e) => {
let msg = format!("Failed to drop root uid/gid: {}", e);
log::warn!("{}", msg);
messages.push(msg);
if config.mode == SandboxMode::Strict {
return Err(e).context("Strict mode requires the root uid drop to succeed");
}
result.status = SandboxStatus::PartiallyEnforced;
}
}
}
match landlock::apply_landlock(config) {
Ok(ll_result) => {
result.fs_restricted = ll_result.fs_applied;
result.net_restricted = ll_result.net_applied;
result.scope_restricted = ll_result.scope_applied;
result.landlock_abi = ll_result.effective_abi;
if ll_result.fs_applied {
messages.push("Landlock filesystem restrictions applied".to_string());
}
if ll_result.net_applied {
messages.push("Landlock network restrictions applied".to_string());
}
if ll_result.scope_applied {
messages.push("Landlock scope restrictions applied".to_string());
}
if !ll_result.fs_applied && !ll_result.net_applied {
messages.push(format!("Landlock not applied: {}", ll_result.message));
if config.mode == SandboxMode::Strict {
return Err(anyhow::anyhow!(
"Strict mode requires Landlock support: {}",
ll_result.message
));
}
if result.status == SandboxStatus::FullyEnforced {
result.status = SandboxStatus::PartiallyEnforced;
}
} else if !ll_result.net_applied && config.block_network {
if result.status == SandboxStatus::FullyEnforced {
result.status = SandboxStatus::PartiallyEnforced;
}
}
}
Err(e) => {
let msg = format!("Landlock application failed: {}", e);
log::warn!("{}", msg);
messages.push(msg);
if config.mode == SandboxMode::Strict {
return Err(e).context("Strict mode requires Landlock");
}
result.status = SandboxStatus::PartiallyEnforced;
}
}
if !result.cap_net_raw_dropped
&& !result.uid_dropped
&& !result.fs_restricted
&& !result.net_restricted
{
result.status = SandboxStatus::NotApplied;
}
match result.status {
SandboxStatus::FullyEnforced => {
log::info!("Sandbox fully enforced: {}", messages.join("; "));
}
SandboxStatus::PartiallyEnforced => {
log::warn!("Sandbox partially enforced: {}", messages.join("; "));
}
_ => {
log::warn!("Sandbox not applied: {}", messages.join("; "));
}
}
result.message = messages.join("; ");
Ok(result)
}
#[cfg(not(feature = "landlock"))]
pub(crate) fn apply(config: &SandboxConfig) -> anyhow::Result<SandboxReport> {
let no_new_privs = match set_no_new_privs() {
Ok(()) => {
log::info!("PR_SET_NO_NEW_PRIVS set");
true
}
Err(e) => {
log::warn!("Failed to set PR_SET_NO_NEW_PRIVS: {}", e);
false
}
};
if !no_new_privs && config.mode == SandboxMode::Strict {
return Err(anyhow::anyhow!(
"Strict mode requires PR_SET_NO_NEW_PRIVS to be settable"
));
}
let mut uid_dropped = false;
let mut drop_message = String::new();
if config.mode != SandboxMode::Disabled
&& let Some(target) = config.drop_uid
{
match privdrop::drop_to(target) {
Ok(()) => {
uid_dropped = true;
drop_message = format!("; root dropped to uid {} gid {}", target.uid, target.gid);
log::info!(
"Dropped root privileges to uid {} gid {} (verified); \
procfs process attribution is now limited to that user's \
processes",
target.uid,
target.gid
);
}
Err(e) => {
log::warn!("Failed to drop root uid/gid: {}", e);
if config.mode == SandboxMode::Strict {
return Err(e.context("Strict mode requires the root uid drop to succeed"));
}
drop_message = format!("; root uid drop failed: {}", e);
}
}
}
log::warn!("Landlock feature not compiled in");
let message = if no_new_privs {
"Landlock feature not compiled in (no-new-privs still set)"
} else {
"Landlock feature not compiled in"
};
Ok(SandboxReport {
status: if uid_dropped {
SandboxStatus::PartiallyEnforced
} else {
SandboxStatus::NotApplied
},
message: format!("{}{}", message, drop_message),
uid_dropped,
no_new_privs,
..SandboxReport::default()
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_set_no_new_privs_is_idempotent_and_sticks() {
set_no_new_privs().expect("first set_no_new_privs call");
set_no_new_privs().expect("second set_no_new_privs call (idempotent)");
let value = unsafe { libc::prctl(libc::PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0) };
assert_eq!(value, 1, "NoNewPrivs should be set after set_no_new_privs");
}
}