Skip to content

Commit

Permalink
maps,programs: avoid path UTF-8 assumptions
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Aug 10, 2023
1 parent 68bf881 commit e0bb296
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 78 deletions.
4 changes: 2 additions & 2 deletions aya-obj/src/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,7 @@ mod tests {
kernel_version: None,
section: ProgramSection::KProbe { .. },
..
} if license.to_string_lossy() == "GPL"
} if license.to_str().unwrap() == "GPL"
);
assert_matches!(
function_foo,
Expand All @@ -1730,7 +1730,7 @@ mod tests {
kernel_version: None,
section: ProgramSection::KProbe { .. },
..
} if license.to_string_lossy() == "GPL"
} if license.to_str().unwrap() == "GPL"
);
assert_matches!(
function_bar,
Expand Down
18 changes: 10 additions & 8 deletions aya/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,14 +541,14 @@ impl MapData {

/// Loads a map from a pinned path in bpffs.
pub fn from_pin<P: AsRef<Path>>(path: P) -> Result<MapData, MapError> {
use std::os::unix::ffi::OsStrExt as _;

let path_string =
CString::new(path.as_ref().to_string_lossy().into_owned()).map_err(|e| {
MapError::PinError {
name: None,
error: PinError::InvalidPinPath {
error: e.to_string(),
},
}
CString::new(path.as_ref().as_os_str().as_bytes()).map_err(|e| MapError::PinError {
name: None,
error: PinError::InvalidPinPath {
error: e.to_string(),
},
})?;

let fd = bpf_get_object(&path_string).map_err(|(_, io_error)| SyscallError {
Expand Down Expand Up @@ -587,14 +587,16 @@ impl MapData {
}

pub(crate) fn pin<P: AsRef<Path>>(&mut self, name: &str, path: P) -> Result<(), PinError> {
use std::os::unix::ffi::OsStrExt as _;

if self.pinned {
return Err(PinError::AlreadyPinned { name: name.into() });
}
let map_path = path.as_ref().join(name);
let fd = self.fd.ok_or(PinError::NoFd {
name: name.to_string(),
})?;
let path_string = CString::new(map_path.to_string_lossy().into_owned()).map_err(|e| {
let path_string = CString::new(map_path.as_os_str().as_bytes()).map_err(|e| {
PinError::InvalidPinPath {
error: e.to_string(),
}
Expand Down
2 changes: 1 addition & 1 deletion aya/src/programs/kprobe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl KProbe {
///
/// The returned value can be used to detach from the given function, see [KProbe::detach].
pub fn attach(&mut self, fn_name: &str, offset: u64) -> Result<KProbeLinkId, ProgramError> {
attach(&mut self.data, self.kind, fn_name, offset, None)
attach(&mut self.data, self.kind, Path::new(fn_name), offset, None)
}

/// Detaches the program.
Expand Down
17 changes: 10 additions & 7 deletions aya/src/programs/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ impl FdLink {
/// # Ok::<(), Error>(())
/// ```
pub fn pin<P: AsRef<Path>>(self, path: P) -> Result<PinnedLink, PinError> {
let path_string =
CString::new(path.as_ref().to_string_lossy().into_owned()).map_err(|e| {
PinError::InvalidPinPath {
error: e.to_string(),
}
})?;
use std::os::unix::ffi::OsStrExt as _;

let path_string = CString::new(path.as_ref().as_os_str().as_bytes()).map_err(|e| {
PinError::InvalidPinPath {
error: e.to_string(),
}
})?;
bpf_pin_object(self.fd.as_raw_fd(), &path_string).map_err(|(_, io_error)| {
SyscallError {
call: "BPF_OBJ_PIN",
Expand Down Expand Up @@ -203,7 +204,9 @@ impl PinnedLink {

/// Creates a [`crate::programs::links::PinnedLink`] from a valid path on bpffs.
pub fn from_pin<P: AsRef<Path>>(path: P) -> Result<Self, LinkError> {
let path_string = CString::new(path.as_ref().to_string_lossy().to_string()).unwrap();
use std::os::unix::ffi::OsStrExt as _;

let path_string = CString::new(path.as_ref().as_os_str().as_bytes()).unwrap();
let fd = bpf_get_object(&path_string).map_err(|(_, io_error)| {
LinkError::SyscallError(SyscallError {
call: "BPF_OBJ_GET",
Expand Down
9 changes: 6 additions & 3 deletions aya/src/programs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,9 @@ impl<T: Link> ProgramData<T> {
path: P,
verifier_log_level: VerifierLogLevel,
) -> Result<ProgramData<T>, ProgramError> {
let path_string =
CString::new(path.as_ref().as_os_str().to_string_lossy().as_bytes()).unwrap();
use std::os::unix::ffi::OsStrExt as _;

let path_string = CString::new(path.as_ref().as_os_str().as_bytes()).unwrap();
let fd = bpf_get_object(&path_string).map_err(|(_, io_error)| SyscallError {
call: "bpf_obj_get",
io_error,
Expand Down Expand Up @@ -514,14 +515,16 @@ fn unload_program<T: Link>(data: &mut ProgramData<T>) -> Result<(), ProgramError
}

fn pin_program<T: Link, P: AsRef<Path>>(data: &ProgramData<T>, path: P) -> Result<(), PinError> {
use std::os::unix::ffi::OsStrExt as _;

let fd = data.fd.ok_or(PinError::NoFd {
name: data
.name
.as_deref()
.unwrap_or("<unknown program>")
.to_string(),
})?;
let path_string = CString::new(path.as_ref().to_string_lossy().into_owned()).map_err(|e| {
let path_string = CString::new(path.as_ref().as_os_str().as_bytes()).map_err(|e| {
PinError::InvalidPinPath {
error: e.to_string(),
}
Expand Down
15 changes: 8 additions & 7 deletions aya/src/programs/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ pub(crate) struct ProbeEvent {
pub(crate) fn attach<T: Link + From<PerfLinkInner>>(
program_data: &mut ProgramData<T>,
kind: ProbeKind,
fn_name: &str,
name: &Path,
offset: u64,
pid: Option<pid_t>,
) -> Result<T::Id, ProgramError> {
// https://github.com/torvalds/linux/commit/e12f03d7031a977356e3d7b75a68c2185ff8d155
// Use debugfs to create probe
if KernelVersion::current().unwrap() < KernelVersion::new(4, 17, 0) {
let (fd, event_alias) = create_as_trace_point(kind, fn_name, offset, pid)?;
let (fd, event_alias) = create_as_trace_point(kind, name, offset, pid)?;
let link = T::from(perf_attach_debugfs(
program_data.fd_or_err()?,
fd,
Expand All @@ -67,7 +67,7 @@ pub(crate) fn attach<T: Link + From<PerfLinkInner>>(
return program_data.links.insert(link);
};

let fd = create_as_probe(kind, fn_name, offset, pid)?;
let fd = create_as_probe(kind, name, offset, pid)?;
let link = T::from(perf_attach(program_data.fd_or_err()?, fd)?);
program_data.links.insert(link)
}
Expand All @@ -92,7 +92,7 @@ pub(crate) fn detach_debug_fs(event: ProbeEvent) -> Result<(), ProgramError> {

fn create_as_probe(
kind: ProbeKind,
fn_name: &str,
name: &Path,
offset: u64,
pid: Option<pid_t>,
) -> Result<OwnedFd, ProgramError> {
Expand All @@ -117,7 +117,7 @@ fn create_as_probe(
_ => None,
};

perf_event_open_probe(perf_ty, ret_bit, fn_name, offset, pid).map_err(|(_code, io_error)| {
perf_event_open_probe(perf_ty, ret_bit, name, offset, pid).map_err(|(_code, io_error)| {
SyscallError {
call: "perf_event_open",
io_error,
Expand All @@ -128,7 +128,7 @@ fn create_as_probe(

fn create_as_trace_point(
kind: ProbeKind,
name: &str,
name: &Path,
offset: u64,
pid: Option<pid_t>,
) -> Result<(OwnedFd, String), ProgramError> {
Expand Down Expand Up @@ -156,7 +156,7 @@ fn create_as_trace_point(
fn create_probe_event(
tracefs: &Path,
kind: ProbeKind,
fn_name: &str,
fn_name: &Path,
offset: u64,
) -> Result<String, (String, io::Error)> {
use ProbeKind::*;
Expand All @@ -167,6 +167,7 @@ fn create_probe_event(
KRetProbe | URetProbe => 'r',
};

let fn_name = fn_name.to_string_lossy();
let fixed_fn_name = fn_name.replace(['.', '/', '-'], "_");

let event_alias = format!(
Expand Down
Loading

0 comments on commit e0bb296

Please sign in to comment.