Browse Source

aya: programs: fix detaching programs attached with bpf_prog_attach

Alessandro Decina 3 years ago
parent
commit
fb3e2f7f9d

+ 18 - 2
aya/src/programs/mod.rs

@@ -56,7 +56,7 @@ mod trace_point;
 mod uprobe;
 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 thiserror::Error;
 
@@ -397,10 +397,26 @@ struct ProgAttachLink {
     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 {
     fn detach(&mut self) -> Result<(), ProgramError> {
         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(())
         } else {
             Err(ProgramError::AlreadyDetached)

+ 3 - 5
aya/src/programs/sk_msg.rs

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

+ 3 - 5
aya/src/programs/sk_skb.rs

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

+ 3 - 5
aya/src/programs/sock_ops.rs

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

+ 3 - 3
aya/src/sys/bpf.rs

@@ -238,13 +238,13 @@ pub(crate) fn bpf_link_create(
 
 pub(crate) fn bpf_prog_attach(
     prog_fd: RawFd,
-    map_fd: RawFd,
+    target_fd: RawFd,
     attach_type: bpf_attach_type,
 ) -> SysResult {
     let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
 
     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;
 
     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.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 {