aya: programs: fix detaching programs attached with bpf_prog_attach

pull/1/head
Alessandro Decina 3 years ago
parent 665802594c
commit fb3e2f7f9d

@ -56,7 +56,7 @@ mod trace_point;
mod uprobe; mod uprobe;
mod xdp; mod xdp;
use libc::{close, ENOSPC}; use libc::{close, dup, ENOSPC};
use std::{cell::RefCell, cmp, convert::TryFrom, ffi::CStr, io, os::unix::io::RawFd, rc::Rc}; use std::{cell::RefCell, cmp, convert::TryFrom, ffi::CStr, io, os::unix::io::RawFd, rc::Rc};
use thiserror::Error; use thiserror::Error;
@ -397,10 +397,26 @@ struct ProgAttachLink {
attach_type: bpf_attach_type, attach_type: bpf_attach_type,
} }
impl ProgAttachLink {
pub(crate) fn new(
prog_fd: RawFd,
target_fd: RawFd,
attach_type: bpf_attach_type,
) -> ProgAttachLink {
ProgAttachLink {
prog_fd: Some(prog_fd),
target_fd: Some(unsafe { dup(target_fd) }),
attach_type,
}
}
}
impl Link for ProgAttachLink { impl Link for ProgAttachLink {
fn detach(&mut self) -> Result<(), ProgramError> { fn detach(&mut self) -> Result<(), ProgramError> {
if let Some(prog_fd) = self.prog_fd.take() { if let Some(prog_fd) = self.prog_fd.take() {
let _ = bpf_prog_detach(prog_fd, self.target_fd.take().unwrap(), self.attach_type); let target_fd = self.target_fd.take().unwrap();
let _ = bpf_prog_detach(prog_fd, target_fd, self.attach_type);
unsafe { close(target_fd) };
Ok(()) Ok(())
} else { } else {
Err(ProgramError::AlreadyDetached) Err(ProgramError::AlreadyDetached)

@ -42,10 +42,8 @@ impl SkMsg {
io_error, io_error,
} }
})?; })?;
Ok(self.data.link(ProgAttachLink { Ok(self
prog_fd: Some(prog_fd), .data
target_fd: Some(map_fd), .link(ProgAttachLink::new(prog_fd, map_fd, BPF_SK_MSG_VERDICT)))
attach_type: BPF_SK_MSG_VERDICT,
}))
} }
} }

@ -48,10 +48,8 @@ impl SkSkb {
io_error, io_error,
} }
})?; })?;
Ok(self.data.link(ProgAttachLink { Ok(self
prog_fd: Some(prog_fd), .data
target_fd: Some(map_fd), .link(ProgAttachLink::new(prog_fd, map_fd, attach_type)))
attach_type,
}))
} }
} }

@ -35,10 +35,8 @@ impl SockOps {
io_error, io_error,
} }
})?; })?;
Ok(self.data.link(ProgAttachLink { Ok(self
prog_fd: Some(prog_fd), .data
target_fd: Some(cgroup_fd), .link(ProgAttachLink::new(prog_fd, cgroup_fd, BPF_CGROUP_SOCK_OPS)))
attach_type: BPF_CGROUP_SOCK_OPS,
}))
} }
} }

@ -238,13 +238,13 @@ pub(crate) fn bpf_link_create(
pub(crate) fn bpf_prog_attach( pub(crate) fn bpf_prog_attach(
prog_fd: RawFd, prog_fd: RawFd,
map_fd: RawFd, target_fd: RawFd,
attach_type: bpf_attach_type, attach_type: bpf_attach_type,
) -> SysResult { ) -> SysResult {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
attr.__bindgen_anon_5.attach_bpf_fd = prog_fd as u32; attr.__bindgen_anon_5.attach_bpf_fd = prog_fd as u32;
attr.__bindgen_anon_5.target_fd = map_fd as u32; attr.__bindgen_anon_5.target_fd = target_fd as u32;
attr.__bindgen_anon_5.attach_type = attach_type as u32; attr.__bindgen_anon_5.attach_type = attach_type as u32;
sys_bpf(bpf_cmd::BPF_PROG_ATTACH, &attr) sys_bpf(bpf_cmd::BPF_PROG_ATTACH, &attr)
@ -261,7 +261,7 @@ pub(crate) fn bpf_prog_detach(
attr.__bindgen_anon_5.target_fd = map_fd as u32; attr.__bindgen_anon_5.target_fd = map_fd as u32;
attr.__bindgen_anon_5.attach_type = attach_type as u32; attr.__bindgen_anon_5.attach_type = attach_type as u32;
sys_bpf(bpf_cmd::BPF_PROG_ATTACH, &attr) sys_bpf(bpf_cmd::BPF_PROG_DETACH, &attr)
} }
fn sys_bpf<'a>(cmd: bpf_cmd, attr: &'a bpf_attr) -> SysResult { fn sys_bpf<'a>(cmd: bpf_cmd, attr: &'a bpf_attr) -> SysResult {

Loading…
Cancel
Save