use anyhow::{Context, Result};
use caps::{CapSet, Capability};
const ACTIVE_SETS: [CapSet; 2] = [CapSet::Effective, CapSet::Permitted];
fn has_cap_in_active_sets(capability: Capability) -> Result<bool> {
for set in ACTIVE_SETS {
if caps::has_cap(None, set, capability)
.with_context(|| format!("Failed to check {capability:?} in {set:?} set"))?
{
return Ok(true);
}
}
Ok(false)
}
fn drop_and_verify(capability: Capability) -> Result<bool> {
let mut dropped = false;
for set in ACTIVE_SETS {
if caps::has_cap(None, set, capability)
.with_context(|| format!("Failed to check {capability:?} in {set:?} set"))?
{
caps::drop(None, set, capability)
.with_context(|| format!("Failed to drop {capability:?} from {set:?} set"))?;
dropped = true;
}
}
if has_cap_in_active_sets(capability)? {
anyhow::bail!("{capability:?} remained in an effective or permitted capability set");
}
Ok(dropped)
}
pub(crate) fn drop_cap_net_raw() -> Result<bool> {
drop_and_verify(Capability::CAP_NET_RAW)
}
#[cfg(test)]
pub(crate) fn has_cap_net_raw() -> Result<bool> {
has_cap_in_active_sets(Capability::CAP_NET_RAW)
}
pub(crate) fn drop_ebpf_caps() -> Result<u32> {
let mut dropped = 0;
for cap in [Capability::CAP_BPF, Capability::CAP_PERFMON] {
if drop_and_verify(cap)? {
log::debug!("Dropped {:?} from effective and permitted sets", cap);
dropped += 1;
}
}
Ok(dropped)
}
pub fn drop_thread_cap_net_raw(thread: &str) {
match drop_cap_net_raw() {
Ok(_) => log::debug!("Dropped CAP_NET_RAW in {thread}"),
Err(e) => log::warn!("Failed to drop CAP_NET_RAW in {thread}: {e}"),
}
}
pub fn drop_unused_thread_caps(thread: &str) {
drop_thread_cap_net_raw(thread);
match drop_ebpf_caps() {
Ok(_) => log::debug!("Dropped eBPF capabilities in {thread}"),
Err(e) => log::warn!("Failed to drop eBPF capabilities in {thread}: {e}"),
}
}
pub(crate) fn clear_ambient_caps() -> Result<()> {
caps::clear(None, CapSet::Ambient).context("Failed to clear ambient capability set")?;
log::debug!("Cleared ambient capability set");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_has_cap_net_raw_does_not_panic() {
assert!(has_cap_net_raw().is_ok());
}
#[test]
fn test_drop_cap_net_raw_without_capability() {
let result = drop_cap_net_raw();
assert!(result.is_ok());
}
#[test]
fn test_drop_ebpf_caps_does_not_panic() {
let result = drop_ebpf_caps();
assert!(result.is_ok());
}
#[test]
#[ignore = "requires root with capture and identity-change capabilities in a dedicated process"]
fn capture_capability_drops_preserve_uid_and_gid_changes() {
assert_eq!(unsafe { libc::geteuid() }, 0);
let required = [
Capability::CAP_SETUID,
Capability::CAP_SETGID,
Capability::CAP_NET_RAW,
Capability::CAP_BPF,
Capability::CAP_PERFMON,
];
let mut expected = ACTIVE_SETS.map(|set| {
let held = caps::read(None, set).unwrap();
for capability in required {
assert!(
held.contains(&capability),
"{capability:?} missing from {set:?}"
);
}
held
});
super::super::set_no_new_privs().unwrap();
clear_ambient_caps().unwrap();
for capability in [
Capability::CAP_NET_RAW,
Capability::CAP_BPF,
Capability::CAP_PERFMON,
] {
assert!(drop_and_verify(capability).unwrap());
for (set, held) in ACTIVE_SETS.into_iter().zip(&mut expected) {
held.remove(&capability);
assert_eq!(caps::read(None, set).unwrap(), *held);
assert!(held.contains(&Capability::CAP_SETUID));
assert!(held.contains(&Capability::CAP_SETGID));
}
}
crate::privdrop::drop_to(crate::privdrop::DropTarget {
uid: 65534,
gid: 65534,
})
.unwrap();
assert_eq!(unsafe { libc::geteuid() }, 65534);
assert_eq!(unsafe { libc::getegid() }, 65534);
}
}