feat(v0.4): xdp_ci entry point, per-port bans, capability-based diagnostics, recovery check

- scripts/xdp_ci.sh with honest SKIP and RESULT lines, CI job xdp-kernel
- Blacklist::add_port/is_blocked_on, enforced by the gateway (userspace only)
- diagnostics read CapEff: root without CAP_BPF is not privileged
- smoke phase 9 verifies 'ip link set dev <if> xdp off' recovery
- module-size gate: lockout moved into a directory, detach_xdp into services
This commit is contained in:
Loki 2026-11-21 15:34:21 +01:00
parent d68b45e2b8
commit a4cf78e475
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
21 changed files with 431 additions and 54 deletions

View file

@ -1,8 +1,8 @@
use anyhow::{Result, bail};
use super::types::{
AttachMode, BTF_PATH, KernelVersion, MIN_KERNEL, OSRELEASE_PATH, SystemProbe, container_blocks_xdp,
detect_container, driver_from_link, driver_supports_native_xdp,
AttachMode, BTF_PATH, KernelVersion, MIN_KERNEL, OSRELEASE_PATH, STATUS_PATH, SystemProbe, caps_allow_xdp,
container_blocks_xdp, detect_container, driver_from_link, driver_supports_native_xdp, parse_cap_eff,
};
/// Structured report on how suitable the environment is for XDP.
@ -25,7 +25,10 @@ impl EnvironmentReport {
.ok()
.and_then(|r| KernelVersion::parse(r.trim()));
let btf_available = probe.path_exists(BTF_PATH);
let privileged = probe.effective_uid() == 0;
// Capabilities decide, not the uid: root in a container without CAP_BPF still
// cannot attach. Without a readable CapEff the uid heuristic remains.
let cap_eff = probe.read_file(STATUS_PATH).ok().and_then(|s| parse_cap_eff(&s));
let privileged = cap_eff.map_or_else(|| probe.effective_uid() == 0, caps_allow_xdp);
let driver_link = probe.symlink_target(&format!("/sys/class/net/{interface}/device/driver"));
let driver_name = driver_link.as_deref().and_then(driver_from_link);
@ -40,8 +43,8 @@ impl EnvironmentReport {
}
if !privileged {
warnings.push(
"процесс не от root: CAP_BPF/CAP_NET_ADMIN не подтверждены, \
attach скорее всего завершится EPERM"
"нет прав на XDP (нужны CAP_NET_ADMIN + CAP_BPF + CAP_PERFMON или root без урезанных \
capabilities): attach скорее всего завершится EPERM"
.to_owned(),
);
}
@ -90,7 +93,12 @@ impl EnvironmentReport {
),
(
"privileges",
if self.privileged { "root" } else { "non-root" }.to_owned(),
if self.privileged {
"xdp-capable"
} else {
"missing-xdp-caps"
}
.to_owned(),
),
(
"driver",
@ -118,7 +126,7 @@ impl EnvironmentReport {
return format!("контейнер '{kind}' не поддерживает XDP: нужна KVM-VDS или bare metal");
}
if !self.privileged {
return "запустите от root или выдайте CAP_BPF, CAP_NET_ADMIN, CAP_PERFMON".to_owned();
return "выдайте процессу CAP_NET_ADMIN, CAP_BPF и CAP_PERFMON (docker: --cap-add) или запустите от root без урезанных capabilities".to_owned();
}
if !self.btf_available {
return "ядро без BTF (/sys/kernel/btf/vmlinux): используйте ядро с CONFIG_DEBUG_INFO_BTF=y".to_owned();

View file

@ -5,7 +5,12 @@ pub const MIN_KERNEL: KernelVersion = KernelVersion::new(5, 15, 0);
pub(super) const OSRELEASE_PATH: &str = "/proc/sys/kernel/osrelease";
pub(super) const BTF_PATH: &str = "/sys/kernel/btf/vmlinux";
const STATUS_PATH: &str = "/proc/self/status";
pub(super) const STATUS_PATH: &str = "/proc/self/status";
const CAP_NET_ADMIN: u32 = 12;
const CAP_SYS_ADMIN: u32 = 21;
const CAP_PERFMON: u32 = 38;
const CAP_BPF: u32 = 39;
pub(super) const SYSTEMD_CONTAINER_PATH: &str = "/run/systemd/container";
const OPENVZ_PATH: &str = "/proc/vz";
@ -183,3 +188,17 @@ pub(super) fn driver_from_link(target: &str) -> Option<String> {
pub fn driver_supports_native_xdp(driver: &str) -> bool {
NATIVE_XDP_DRIVERS.contains(&driver)
}
/// Effective capability mask (`CapEff`) from `/proc/self/status`.
pub(super) fn parse_cap_eff(status: &str) -> Option<u64> {
status
.lines()
.find_map(|line| u64::from_str_radix(line.strip_prefix("CapEff:")?.trim(), 16).ok())
}
/// Whether the mask allows loading and attaching XDP: `CAP_NET_ADMIN` plus
/// `CAP_BPF`+`CAP_PERFMON` (kernel 5.8+) or the legacy `CAP_SYS_ADMIN`.
pub(super) fn caps_allow_xdp(mask: u64) -> bool {
let has = |bit: u32| mask & (1 << bit) != 0;
has(CAP_NET_ADMIN) && ((has(CAP_BPF) && has(CAP_PERFMON)) || has(CAP_SYS_ADMIN))
}