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:
parent
d68b45e2b8
commit
a4cf78e475
21 changed files with 431 additions and 54 deletions
76
tests/blacklist_port.rs
Normal file
76
tests/blacklist_port.rs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
use rampart::filter::blacklist::Blacklist;
|
||||
use std::net::IpAddr;
|
||||
use std::time::Duration;
|
||||
|
||||
fn ip(text: &str) -> IpAddr {
|
||||
text.parse().expect("valid test address")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn port_ban_blocks_only_that_port() {
|
||||
let blacklist = Blacklist::new();
|
||||
let attacker = ip("203.0.113.9");
|
||||
assert!(blacklist.add_port(attacker, 25565, Duration::from_secs(60)));
|
||||
|
||||
assert!(blacklist.is_blocked_on(attacker, 25565));
|
||||
assert!(!blacklist.is_blocked_on(attacker, 22), "SSH port must stay reachable");
|
||||
assert!(
|
||||
!blacklist.is_blocked(attacker),
|
||||
"an address-wide lookup is not affected"
|
||||
);
|
||||
assert!(
|
||||
!blacklist.is_blocked_on(ip("203.0.113.10"), 25565),
|
||||
"other addresses are not affected"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn address_ban_covers_every_port() {
|
||||
let blacklist = Blacklist::new();
|
||||
let attacker = ip("203.0.113.9");
|
||||
blacklist.add(attacker, Duration::from_secs(60), "test");
|
||||
assert!(blacklist.is_blocked_on(attacker, 22));
|
||||
assert!(blacklist.is_blocked_on(attacker, 25565));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn port_ban_expires_and_can_be_removed() {
|
||||
let blacklist = Blacklist::new();
|
||||
let client = ip("198.51.100.4");
|
||||
assert!(blacklist.add_port(client, 80, Duration::from_millis(30)));
|
||||
assert!(blacklist.is_blocked_on(client, 80));
|
||||
std::thread::sleep(Duration::from_millis(60));
|
||||
assert!(
|
||||
!blacklist.is_blocked_on(client, 80),
|
||||
"an expired port ban must stop applying"
|
||||
);
|
||||
|
||||
assert!(blacklist.add_port(client, 80, Duration::from_secs(60)));
|
||||
blacklist.remove_port(client, 80);
|
||||
assert!(!blacklist.is_blocked_on(client, 80));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn port_zero_and_mapped_addresses() {
|
||||
let blacklist = Blacklist::new();
|
||||
assert!(
|
||||
!blacklist.add_port(ip("192.0.2.1"), 0, Duration::from_secs(60)),
|
||||
"port 0 means any and is rejected"
|
||||
);
|
||||
assert!(blacklist.add_port(ip("::ffff:192.0.2.7"), 443, Duration::from_secs(60)));
|
||||
assert!(
|
||||
blacklist.is_blocked_on(ip("192.0.2.7"), 443),
|
||||
"mapped and plain IPv4 share one key"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn port_bans_are_counted_and_swept() {
|
||||
let blacklist = Blacklist::new();
|
||||
assert!(blacklist.is_empty());
|
||||
blacklist.add_port(ip("192.0.2.1"), 25565, Duration::from_millis(20));
|
||||
assert_eq!(blacklist.len(), 1);
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
blacklist.clear_expired();
|
||||
assert!(blacklist.is_empty());
|
||||
}
|
||||
|
|
@ -188,3 +188,39 @@ fn failure_hint_covers_privileges_and_btf() {
|
|||
no_btf.files.remove("/sys/kernel/btf/vmlinux");
|
||||
assert!(no_btf.collect().failure_hint().contains("BTF"));
|
||||
}
|
||||
|
||||
fn with_cap_eff(probe: &mut MockProbe, mask: &str) {
|
||||
probe.files.insert(
|
||||
"/proc/self/status".to_owned(),
|
||||
format!("Uid:\t0\t0\t0\t0\nCapEff:\t{mask}\n"),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn root_without_bpf_capabilities_is_not_privileged() {
|
||||
// Docker's default capability set: uid 0, but no CAP_NET_ADMIN / CAP_BPF.
|
||||
let mut probe = MockProbe::linux("virtio_net", "6.8.0");
|
||||
with_cap_eff(&mut probe, "00000000a80425fb");
|
||||
let report = probe.collect();
|
||||
assert!(!report.privileged, "uid 0 without CAP_BPF must not count as privileged");
|
||||
assert!(report.warnings.join("\n").contains("CAP_BPF"));
|
||||
assert!(report.failure_hint().contains("--cap-add"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_xdp_capability_set_is_privileged() {
|
||||
// CAP_NET_ADMIN (12) + CAP_PERFMON (38) + CAP_BPF (39).
|
||||
let mut modern = MockProbe::linux("virtio_net", "6.8.0");
|
||||
with_cap_eff(&mut modern, "000000c000001000");
|
||||
assert!(modern.collect().privileged);
|
||||
|
||||
// Legacy: CAP_NET_ADMIN (12) + CAP_SYS_ADMIN (21).
|
||||
let mut legacy = MockProbe::linux("virtio_net", "5.15.0");
|
||||
with_cap_eff(&mut legacy, "0000000000201000");
|
||||
assert!(legacy.collect().privileged);
|
||||
|
||||
// CAP_BPF alone, without CAP_NET_ADMIN, cannot attach.
|
||||
let mut partial = MockProbe::linux("virtio_net", "6.8.0");
|
||||
with_cap_eff(&mut partial, "000000c000000000");
|
||||
assert!(!partial.collect().privileged);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue