Skip to content

Commit 2e08b36

Browse files
d-e-s-odanielocfb
authored andcommitted
Rid program.rs of create_bpf_entity_checked()
Get rid of any traces of create_bpf_entity_checked() usage in program.rs. Favor more explicit error checking via validate_bpf_ret() (as introduced earlier) and make sure to add proper context. Signed-off-by: Daniel Müller <[email protected]>
1 parent 52f44ed commit 2e08b36

File tree

1 file changed

+111
-124
lines changed

1 file changed

+111
-124
lines changed

libbpf-rs/src/program.rs

Lines changed: 111 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ use std::slice;
2525
use libbpf_sys::bpf_func_id;
2626

2727
use crate::util;
28+
use crate::util::validate_bpf_ret;
2829
use crate::AsRawLibbpf;
2930
use crate::Error;
31+
use crate::ErrorExt as _;
3032
use crate::Link;
3133
use crate::Mut;
3234
use crate::Result;
@@ -692,36 +694,30 @@ impl<'obj> ProgramMut<'obj> {
692694

693695
/// Auto-attach based on prog section
694696
pub fn attach(&mut self) -> Result<Link> {
695-
util::create_bpf_entity_checked(|| unsafe {
696-
libbpf_sys::bpf_program__attach(self.ptr.as_ptr())
697-
})
698-
.map(|ptr| unsafe {
699-
// SAFETY: the pointer came from libbpf and has been checked for errors
700-
Link::new(ptr)
701-
})
697+
let ptr = unsafe { libbpf_sys::bpf_program__attach(self.ptr.as_ptr()) };
698+
let ptr = validate_bpf_ret(ptr).context("failed to attach BPF program")?;
699+
// SAFETY: the pointer came from libbpf and has been checked for errors.
700+
let link = unsafe { Link::new(ptr) };
701+
Ok(link)
702702
}
703703

704704
/// Attach this program to a
705705
/// [cgroup](https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html).
706706
pub fn attach_cgroup(&mut self, cgroup_fd: i32) -> Result<Link> {
707-
util::create_bpf_entity_checked(|| unsafe {
708-
libbpf_sys::bpf_program__attach_cgroup(self.ptr.as_ptr(), cgroup_fd)
709-
})
710-
.map(|ptr| unsafe {
711-
// SAFETY: the pointer came from libbpf and has been checked for errors
712-
Link::new(ptr)
713-
})
707+
let ptr = unsafe { libbpf_sys::bpf_program__attach_cgroup(self.ptr.as_ptr(), cgroup_fd) };
708+
let ptr = validate_bpf_ret(ptr).context("failed to attach cgroup")?;
709+
// SAFETY: the pointer came from libbpf and has been checked for errors.
710+
let link = unsafe { Link::new(ptr) };
711+
Ok(link)
714712
}
715713

716714
/// Attach this program to a [perf event](https://linux.die.net/man/2/perf_event_open).
717715
pub fn attach_perf_event(&mut self, pfd: i32) -> Result<Link> {
718-
util::create_bpf_entity_checked(|| unsafe {
719-
libbpf_sys::bpf_program__attach_perf_event(self.ptr.as_ptr(), pfd)
720-
})
721-
.map(|ptr| unsafe {
722-
// SAFETY: the pointer came from libbpf and has been checked for errors
723-
Link::new(ptr)
724-
})
716+
let ptr = unsafe { libbpf_sys::bpf_program__attach_perf_event(self.ptr.as_ptr(), pfd) };
717+
let ptr = validate_bpf_ret(ptr).context("failed to attach perf event")?;
718+
// SAFETY: the pointer came from libbpf and has been checked for errors.
719+
let link = unsafe { Link::new(ptr) };
720+
Ok(link)
725721
}
726722

727723
/// Attach this program to a [userspace
@@ -735,19 +731,19 @@ impl<'obj> ProgramMut<'obj> {
735731
) -> Result<Link> {
736732
let path = util::path_to_cstring(binary_path)?;
737733
let path_ptr = path.as_ptr();
738-
util::create_bpf_entity_checked(|| unsafe {
734+
let ptr = unsafe {
739735
libbpf_sys::bpf_program__attach_uprobe(
740736
self.ptr.as_ptr(),
741737
retprobe,
742738
pid,
743739
path_ptr,
744740
func_offset as libbpf_sys::size_t,
745741
)
746-
})
747-
.map(|ptr| unsafe {
748-
// SAFETY: the pointer came from libbpf and has been checked for errors
749-
Link::new(ptr)
750-
})
742+
};
743+
let ptr = validate_bpf_ret(ptr).context("failed to attach uprobe")?;
744+
// SAFETY: the pointer came from libbpf and has been checked for errors.
745+
let link = unsafe { Link::new(ptr) };
746+
Ok(link)
751747
}
752748

753749
/// Attach this program to a [userspace
@@ -780,33 +776,33 @@ impl<'obj> ProgramMut<'obj> {
780776
..Default::default()
781777
};
782778

783-
util::create_bpf_entity_checked(|| unsafe {
779+
let ptr = unsafe {
784780
libbpf_sys::bpf_program__attach_uprobe_opts(
785781
self.ptr.as_ptr(),
786782
pid,
787783
path_ptr,
788784
func_offset as libbpf_sys::size_t,
789785
&opts as *const _,
790786
)
791-
})
792-
.map(|ptr| unsafe {
793-
// SAFETY: the pointer came from libbpf and has been checked for errors
794-
Link::new(ptr)
795-
})
787+
};
788+
let ptr = validate_bpf_ret(ptr).context("failed to attach uprobe")?;
789+
// SAFETY: the pointer came from libbpf and has been checked for errors.
790+
let link = unsafe { Link::new(ptr) };
791+
Ok(link)
796792
}
797793

798794
/// Attach this program to a [kernel
799795
/// probe](https://www.kernel.org/doc/html/latest/trace/kprobetrace.html).
800796
pub fn attach_kprobe<T: AsRef<str>>(&mut self, retprobe: bool, func_name: T) -> Result<Link> {
801797
let func_name = util::str_to_cstring(func_name.as_ref())?;
802798
let func_name_ptr = func_name.as_ptr();
803-
util::create_bpf_entity_checked(|| unsafe {
799+
let ptr = unsafe {
804800
libbpf_sys::bpf_program__attach_kprobe(self.ptr.as_ptr(), retprobe, func_name_ptr)
805-
})
806-
.map(|ptr| unsafe {
807-
// SAFETY: the pointer came from libbpf and has been checked for errors
808-
Link::new(ptr)
809-
})
801+
};
802+
let ptr = validate_bpf_ret(ptr).context("failed to attach kprobe")?;
803+
// SAFETY: the pointer came from libbpf and has been checked for errors.
804+
let link = unsafe { Link::new(ptr) };
805+
Ok(link)
810806
}
811807

812808
/// Attach this program to the specified syscall
@@ -823,13 +819,13 @@ impl<'obj> ProgramMut<'obj> {
823819

824820
let syscall_name = util::str_to_cstring(syscall_name.as_ref())?;
825821
let syscall_name_ptr = syscall_name.as_ptr();
826-
util::create_bpf_entity_checked(|| unsafe {
822+
let ptr = unsafe {
827823
libbpf_sys::bpf_program__attach_ksyscall(self.ptr.as_ptr(), syscall_name_ptr, &opts)
828-
})
829-
.map(|ptr| unsafe {
830-
// SAFETY: the pointer came from libbpf and has been checked for errors
831-
Link::new(ptr)
832-
})
824+
};
825+
let ptr = validate_bpf_ret(ptr).context("failed to attach ksyscall")?;
826+
// SAFETY: the pointer came from libbpf and has been checked for errors.
827+
let link = unsafe { Link::new(ptr) };
828+
Ok(link)
833829
}
834830

835831
fn attach_tracepoint_impl(
@@ -843,31 +839,30 @@ impl<'obj> ProgramMut<'obj> {
843839
let tp_name = util::str_to_cstring(tp_name)?;
844840
let tp_name_ptr = tp_name.as_ptr();
845841

846-
util::create_bpf_entity_checked(|| {
847-
if let Some(tp_opts) = tp_opts {
848-
let tp_opts = libbpf_sys::bpf_tracepoint_opts::from(tp_opts);
849-
unsafe {
850-
libbpf_sys::bpf_program__attach_tracepoint_opts(
851-
self.ptr.as_ptr(),
852-
tp_category_ptr,
853-
tp_name_ptr,
854-
&tp_opts as *const _,
855-
)
856-
}
857-
} else {
858-
unsafe {
859-
libbpf_sys::bpf_program__attach_tracepoint(
860-
self.ptr.as_ptr(),
861-
tp_category_ptr,
862-
tp_name_ptr,
863-
)
864-
}
842+
let ptr = if let Some(tp_opts) = tp_opts {
843+
let tp_opts = libbpf_sys::bpf_tracepoint_opts::from(tp_opts);
844+
unsafe {
845+
libbpf_sys::bpf_program__attach_tracepoint_opts(
846+
self.ptr.as_ptr(),
847+
tp_category_ptr,
848+
tp_name_ptr,
849+
&tp_opts as *const _,
850+
)
865851
}
866-
})
867-
.map(|ptr| unsafe {
868-
// SAFETY: the pointer came from libbpf and has been checked for errors
869-
Link::new(ptr)
870-
})
852+
} else {
853+
unsafe {
854+
libbpf_sys::bpf_program__attach_tracepoint(
855+
self.ptr.as_ptr(),
856+
tp_category_ptr,
857+
tp_name_ptr,
858+
)
859+
}
860+
};
861+
862+
let ptr = validate_bpf_ret(ptr).context("failed to attach tracepoint")?;
863+
// SAFETY: the pointer came from libbpf and has been checked for errors.
864+
let link = unsafe { Link::new(ptr) };
865+
Ok(link)
871866
}
872867

873868
/// Attach this program to a [kernel
@@ -897,35 +892,31 @@ impl<'obj> ProgramMut<'obj> {
897892
pub fn attach_raw_tracepoint<T: AsRef<str>>(&mut self, tp_name: T) -> Result<Link> {
898893
let tp_name = util::str_to_cstring(tp_name.as_ref())?;
899894
let tp_name_ptr = tp_name.as_ptr();
900-
util::create_bpf_entity_checked(|| unsafe {
895+
let ptr = unsafe {
901896
libbpf_sys::bpf_program__attach_raw_tracepoint(self.ptr.as_ptr(), tp_name_ptr)
902-
})
903-
.map(|ptr| unsafe {
904-
// SAFETY: the pointer came from libbpf and has been checked for errors
905-
Link::new(ptr)
906-
})
897+
};
898+
let ptr = validate_bpf_ret(ptr).context("failed to attach raw tracepoint")?;
899+
// SAFETY: the pointer came from libbpf and has been checked for errors.
900+
let link = unsafe { Link::new(ptr) };
901+
Ok(link)
907902
}
908903

909904
/// Attach to an [LSM](https://en.wikipedia.org/wiki/Linux_Security_Modules) hook
910905
pub fn attach_lsm(&mut self) -> Result<Link> {
911-
util::create_bpf_entity_checked(|| unsafe {
912-
libbpf_sys::bpf_program__attach_lsm(self.ptr.as_ptr())
913-
})
914-
.map(|ptr| unsafe {
915-
// SAFETY: the pointer came from libbpf and has been checked for errors
916-
Link::new(ptr)
917-
})
906+
let ptr = unsafe { libbpf_sys::bpf_program__attach_lsm(self.ptr.as_ptr()) };
907+
let ptr = validate_bpf_ret(ptr).context("failed to attach LSM")?;
908+
// SAFETY: the pointer came from libbpf and has been checked for errors.
909+
let link = unsafe { Link::new(ptr) };
910+
Ok(link)
918911
}
919912

920913
/// Attach to a [fentry/fexit kernel probe](https://lwn.net/Articles/801479/)
921914
pub fn attach_trace(&mut self) -> Result<Link> {
922-
util::create_bpf_entity_checked(|| unsafe {
923-
libbpf_sys::bpf_program__attach_trace(self.ptr.as_ptr())
924-
})
925-
.map(|ptr| unsafe {
926-
// SAFETY: the pointer came from libbpf and has been checked for errors
927-
Link::new(ptr)
928-
})
915+
let ptr = unsafe { libbpf_sys::bpf_program__attach_trace(self.ptr.as_ptr()) };
916+
let ptr = validate_bpf_ret(ptr).context("failed to attach fentry/fexit kernel probe")?;
917+
// SAFETY: the pointer came from libbpf and has been checked for errors.
918+
let link = unsafe { Link::new(ptr) };
919+
Ok(link)
929920
}
930921

931922
/// Attach a verdict/parser to a [sockmap/sockhash](https://lwn.net/Articles/731133/)
@@ -943,24 +934,20 @@ impl<'obj> ProgramMut<'obj> {
943934

944935
/// Attach this program to [XDP](https://lwn.net/Articles/825998/)
945936
pub fn attach_xdp(&mut self, ifindex: i32) -> Result<Link> {
946-
util::create_bpf_entity_checked(|| unsafe {
947-
libbpf_sys::bpf_program__attach_xdp(self.ptr.as_ptr(), ifindex)
948-
})
949-
.map(|ptr| unsafe {
950-
// SAFETY: the pointer came from libbpf and has been checked for errors
951-
Link::new(ptr)
952-
})
937+
let ptr = unsafe { libbpf_sys::bpf_program__attach_xdp(self.ptr.as_ptr(), ifindex) };
938+
let ptr = validate_bpf_ret(ptr).context("failed to attach XDP program")?;
939+
// SAFETY: the pointer came from libbpf and has been checked for errors.
940+
let link = unsafe { Link::new(ptr) };
941+
Ok(link)
953942
}
954943

955944
/// Attach this program to [netns-based programs](https://lwn.net/Articles/819618/)
956945
pub fn attach_netns(&mut self, netns_fd: i32) -> Result<Link> {
957-
util::create_bpf_entity_checked(|| unsafe {
958-
libbpf_sys::bpf_program__attach_netns(self.ptr.as_ptr(), netns_fd)
959-
})
960-
.map(|ptr| unsafe {
961-
// SAFETY: the pointer came from libbpf and has been checked for errors
962-
Link::new(ptr)
963-
})
946+
let ptr = unsafe { libbpf_sys::bpf_program__attach_netns(self.ptr.as_ptr(), netns_fd) };
947+
let ptr = validate_bpf_ret(ptr).context("failed to attach network namespace program")?;
948+
// SAFETY: the pointer came from libbpf and has been checked for errors.
949+
let link = unsafe { Link::new(ptr) };
950+
Ok(link)
964951
}
965952

966953
fn attach_usdt_impl(
@@ -983,7 +970,7 @@ impl<'obj> ProgramMut<'obj> {
983970
.map(|opts| opts as *const _)
984971
.unwrap_or_else(ptr::null);
985972

986-
util::create_bpf_entity_checked(|| unsafe {
973+
let ptr = unsafe {
987974
libbpf_sys::bpf_program__attach_usdt(
988975
self.ptr.as_ptr(),
989976
pid,
@@ -992,11 +979,11 @@ impl<'obj> ProgramMut<'obj> {
992979
usdt_name_ptr,
993980
usdt_opts_ptr,
994981
)
995-
})
996-
.map(|ptr| unsafe {
997-
// SAFETY: the pointer came from libbpf and has been checked for errors
998-
Link::new(ptr)
999-
})
982+
};
983+
let ptr = validate_bpf_ret(ptr).context("failed to attach USDT")?;
984+
// SAFETY: the pointer came from libbpf and has been checked for errors.
985+
let link = unsafe { Link::new(ptr) };
986+
Ok(link)
1000987
}
1001988

1002989
/// Attach this program to a [USDT](https://lwn.net/Articles/753601/) probe
@@ -1042,25 +1029,25 @@ impl<'obj> ProgramMut<'obj> {
10421029
/// [BPF Iterator](https://www.kernel.org/doc/html/latest/bpf/bpf_iterators.html).
10431030
/// The entry point of the program must be defined with `SEC("iter")` or `SEC("iter.s")`.
10441031
pub fn attach_iter(&mut self, map_fd: BorrowedFd<'_>) -> Result<Link> {
1045-
util::create_bpf_entity_checked(|| unsafe {
1046-
let mut linkinfo = libbpf_sys::bpf_iter_link_info::default();
1047-
linkinfo.map.map_fd = map_fd.as_raw_fd() as _;
1048-
let attach_opt = libbpf_sys::bpf_iter_attach_opts {
1049-
link_info: &mut linkinfo as *mut libbpf_sys::bpf_iter_link_info,
1050-
link_info_len: size_of::<libbpf_sys::bpf_iter_link_info>() as _,
1051-
sz: size_of::<libbpf_sys::bpf_iter_attach_opts>() as _,
1052-
..Default::default()
1053-
};
1054-
1032+
let mut linkinfo = libbpf_sys::bpf_iter_link_info::default();
1033+
linkinfo.map.map_fd = map_fd.as_raw_fd() as _;
1034+
let attach_opt = libbpf_sys::bpf_iter_attach_opts {
1035+
link_info: &mut linkinfo as *mut libbpf_sys::bpf_iter_link_info,
1036+
link_info_len: size_of::<libbpf_sys::bpf_iter_link_info>() as _,
1037+
sz: size_of::<libbpf_sys::bpf_iter_attach_opts>() as _,
1038+
..Default::default()
1039+
};
1040+
let ptr = unsafe {
10551041
libbpf_sys::bpf_program__attach_iter(
10561042
self.ptr.as_ptr(),
10571043
&attach_opt as *const libbpf_sys::bpf_iter_attach_opts,
10581044
)
1059-
})
1060-
.map(|ptr| unsafe {
1061-
// SAFETY: the pointer came from libbpf and has been checked for errors
1062-
Link::new(ptr)
1063-
})
1045+
};
1046+
1047+
let ptr = validate_bpf_ret(ptr).context("failed to attach iterator")?;
1048+
// SAFETY: the pointer came from libbpf and has been checked for errors.
1049+
let link = unsafe { Link::new(ptr) };
1050+
Ok(link)
10641051
}
10651052

10661053
/// Test run the program with the given input data.

0 commit comments

Comments
 (0)