mirror of https://github.com/aya-rs/aya
lsm-cgroup: attachment type support
parent
f34d355d7d
commit
3787b4be44
@ -0,0 +1,21 @@
|
|||||||
|
//! XDP programs.
|
||||||
|
|
||||||
|
use crate::generated::bpf_attach_type;
|
||||||
|
|
||||||
|
/// Defines where to attach an `XDP` program.
|
||||||
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
pub enum LsmAttachType {
|
||||||
|
/// Cgroup based LSM program
|
||||||
|
Cgroup,
|
||||||
|
/// MAC based LSM program
|
||||||
|
Mac,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<LsmAttachType> for bpf_attach_type {
|
||||||
|
fn from(value: LsmAttachType) -> Self {
|
||||||
|
match value {
|
||||||
|
LsmAttachType::Cgroup => bpf_attach_type::BPF_LSM_CGROUP,
|
||||||
|
LsmAttachType::Mac => bpf_attach_type::BPF_LSM_MAC,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
use std::{fs::File, io::{ErrorKind, Write}, path::Path};
|
||||||
|
use aya::{programs::Lsm, util::KernelVersion, Btf, Ebpf};
|
||||||
|
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpListener};
|
||||||
|
use nix::{
|
||||||
|
sys::wait::waitpid,
|
||||||
|
unistd::{fork, getpid, ForkResult},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lsm_cgroup() {
|
||||||
|
let kernel_version = KernelVersion::current().unwrap();
|
||||||
|
if kernel_version < KernelVersion::new(6, 0, 0) {
|
||||||
|
eprintln!("skipping lsm_cgroup test on kernel {kernel_version:?}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut bpf: Ebpf = Ebpf::load(crate::TEST).unwrap();
|
||||||
|
let prog: &mut Lsm = bpf.program_mut("test_lsmcgroup").unwrap().try_into().unwrap();
|
||||||
|
let btf = Btf::from_sys_fs().expect("could not get btf from sys");
|
||||||
|
if let Err(err) = prog.load("socket_bind", &btf) {
|
||||||
|
panic!("{err}");
|
||||||
|
}
|
||||||
|
|
||||||
|
let cgroup_path = Path::new(".").join("/sys/fs/cgroup/").join("lsm_cgroup_test");
|
||||||
|
|
||||||
|
let _ = std::fs::create_dir_all( cgroup_path.clone()).expect("could not create the cgroup dir");
|
||||||
|
|
||||||
|
let p = prog.attach(
|
||||||
|
Some(File::open(cgroup_path.clone()).unwrap()),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
match fork().expect("Failed to fork process") {
|
||||||
|
ForkResult::Parent { child } => {
|
||||||
|
waitpid(Some(child), None).unwrap();
|
||||||
|
|
||||||
|
let pid = getpid();
|
||||||
|
|
||||||
|
let mut f = File::create(cgroup_path.join("cgroup.procs")).expect("could not open cgroup procs");
|
||||||
|
f.write_fmt(format_args!("{}",pid.as_raw() as u64)).expect("could not write into procs file");
|
||||||
|
|
||||||
|
assert_matches::assert_matches!(TcpListener::bind("127.0.0.1:12345"), Err(e) => assert_eq!(
|
||||||
|
e.kind(), ErrorKind::PermissionDenied)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
ForkResult::Child => {
|
||||||
|
assert_matches::assert_matches!(TcpListener::bind("127.0.0.1:12345"), Ok(listener) => assert_eq!(
|
||||||
|
listener.local_addr().unwrap(), SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 12345)))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue