maps,programs: avoid path UTF-8 assumptions

reviewable/pr742/r2
Tamir Duberstein 2 years ago
parent 5bc922af23
commit 09bee10e79
No known key found for this signature in database

@ -1642,15 +1642,12 @@ mod tests {
let prog_foo = obj.programs.get("foo").unwrap();
assert_matches!(
prog_foo,
Program {
assert_matches!(prog_foo, Program {
license,
kernel_version: None,
section: ProgramSection::KProbe { .. },
..
} if license.to_str().unwrap() == "GPL"
);
} => assert_eq!(license.to_str().unwrap(), "GPL"));
assert_matches!(
obj.functions.get(&prog_foo.function_key()),
@ -1704,14 +1701,12 @@ mod tests {
let prog_bar = obj.programs.get("bar").unwrap();
let function_bar = obj.functions.get(&prog_bar.function_key()).unwrap();
assert_matches!(prog_foo,
Program {
assert_matches!(prog_foo, Program {
license,
kernel_version: None,
section: ProgramSection::KProbe { .. },
..
} if license.to_string_lossy() == "GPL"
);
} => assert_eq!(license.to_str().unwrap(), "GPL"));
assert_matches!(
function_foo,
Function {
@ -1724,14 +1719,12 @@ mod tests {
} if name == "foo" && instructions.len() == 1
);
assert_matches!(prog_bar,
Program {
assert_matches!(prog_bar, Program {
license,
kernel_version: None,
section: ProgramSection::KProbe { .. },
..
} if license.to_string_lossy() == "GPL"
);
} => assert_eq!(license.to_str().unwrap(), "GPL"));
assert_matches!(
function_bar,
Function {

@ -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 {
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 {
@ -587,6 +587,8 @@ 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() });
}
@ -594,7 +596,7 @@ impl MapData {
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(),
}

@ -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.

@ -146,8 +146,9 @@ 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| {
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(),
}
@ -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",

@ -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,
@ -514,6 +515,8 @@ 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
@ -521,7 +524,7 @@ fn pin_program<T: Link, P: AsRef<Path>>(data: &ProgramData<T>, path: P) -> Resul
.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(),
}

@ -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,
@ -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)
}
@ -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> {
@ -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,
@ -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> {
@ -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::*;
@ -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!(

@ -4,7 +4,7 @@ use object::{Object, ObjectSection, ObjectSymbol};
use std::{
borrow::Cow,
error::Error,
ffi::CStr,
ffi::{CStr, OsStr, OsString},
fs,
io::{self, BufRead, Cursor, Read},
mem,
@ -49,6 +49,39 @@ pub struct UProbe {
pub(crate) kind: ProbeKind,
}
trait OsStringExt {
fn starts_with(&self, needle: &OsStr) -> bool;
fn ends_with(&self, needle: &OsStr) -> bool;
fn strip_prefix(&self, prefix: &OsStr) -> Option<&OsStr>;
fn strip_suffix(&self, suffix: &OsStr) -> Option<&OsStr>;
}
impl OsStringExt for OsStr {
fn starts_with(&self, needle: &OsStr) -> bool {
use std::os::unix::ffi::OsStrExt as _;
self.as_bytes().starts_with(needle.as_bytes())
}
fn ends_with(&self, needle: &OsStr) -> bool {
use std::os::unix::ffi::OsStrExt as _;
self.as_bytes().ends_with(needle.as_bytes())
}
fn strip_prefix(&self, prefix: &OsStr) -> Option<&OsStr> {
use std::os::unix::ffi::OsStrExt as _;
self.as_bytes()
.strip_prefix(prefix.as_bytes())
.map(OsStr::from_bytes)
}
fn strip_suffix(&self, suffix: &OsStr) -> Option<&OsStr> {
use std::os::unix::ffi::OsStrExt as _;
self.as_bytes()
.strip_suffix(suffix.as_bytes())
.map(OsStr::from_bytes)
}
}
impl UProbe {
/// Loads the program inside the kernel.
pub fn load(&mut self) -> Result<(), ProgramError> {
@ -127,16 +160,15 @@ impl UProbe {
fn resolve_attach_path<T: AsRef<Path>>(
target: &T,
pid: Option<pid_t>,
) -> Result<Cow<'_, str>, UProbeError> {
) -> Result<Cow<'_, Path>, UProbeError> {
// Look up the path for the target. If it there is a pid, and the target is a library name
// that is in the process's memory map, use the path of that library. Otherwise, use the target as-is.
let target = target.as_ref();
let invalid_target = || UProbeError::InvalidTarget {
path: target.to_owned(),
};
let target_str = target.to_str().ok_or_else(invalid_target)?;
pid.and_then(|pid| {
find_lib_in_proc_maps(pid, target_str)
find_lib_in_proc_maps(pid, target)
.map_err(|io_error| UProbeError::FileError {
filename: format!("/proc/{pid}/maps"),
io_error,
@ -144,14 +176,14 @@ fn resolve_attach_path<T: AsRef<Path>>(
.map(|v| v.map(Cow::Owned))
.transpose()
})
.or_else(|| target.is_absolute().then(|| Ok(Cow::Borrowed(target_str))))
.or_else(|| target.is_absolute().then(|| Ok(Cow::Borrowed(target))))
.or_else(|| {
LD_SO_CACHE
.as_ref()
.map_err(|error| UProbeError::InvalidLdSoCache {
io_error: error.clone(),
})
.map(|cache| cache.resolve(target_str).map(Cow::Borrowed))
.map(|cache| cache.resolve(target).map(Cow::Borrowed))
.transpose()
})
.unwrap_or_else(|| Err(invalid_target()))
@ -172,6 +204,7 @@ fn test_resolve_attach_path() {
// Now let's resolve the path to libc. It should exist in the current process's memory map and
// then in the ld.so.cache.
let libc_path = resolve_attach_path(&"libc", Some(pid)).unwrap();
let libc_path = libc_path.to_str().unwrap();
// Make sure we got a path that contains libc.
assert!(libc_path.contains("libc"), "libc_path: {}", libc_path);
@ -249,45 +282,55 @@ pub enum UProbeError {
},
}
fn proc_maps_libs(pid: pid_t) -> Result<Vec<(String, String)>, io::Error> {
fn proc_maps_libs(pid: pid_t) -> Result<Vec<(OsString, PathBuf)>, io::Error> {
use std::os::unix::ffi::OsStrExt as _;
let maps_file = format!("/proc/{pid}/maps");
let data = fs::read_to_string(maps_file)?;
Ok(data
.lines()
.filter_map(|line| {
let line = line.split_whitespace().last()?;
if line.starts_with('/') {
let path = PathBuf::from(line);
let key = path.file_name().unwrap().to_string_lossy().into_owned();
Some((key, path.to_string_lossy().to_string()))
} else {
None
let data = fs::read(maps_file)?;
let libs = data
.split(|b| b == &b'\n')
.filter_map(|mut line| {
loop {
if let [stripped @ .., c] = line {
if c.is_ascii_whitespace() {
line = stripped;
continue;
}
}
break;
}
let path = line.split(|b| b.is_ascii_whitespace()).last()?;
let path = Path::new(OsStr::from_bytes(path));
path.is_absolute()
.then(|| {
path.file_name()
.map(|file_name| (file_name.to_owned(), path.to_owned()))
})
.collect())
.flatten()
})
.collect();
Ok(libs)
}
fn find_lib_in_proc_maps(pid: pid_t, lib: &str) -> Result<Option<String>, io::Error> {
fn find_lib_in_proc_maps(pid: pid_t, lib: &Path) -> Result<Option<PathBuf>, io::Error> {
let libs = proc_maps_libs(pid)?;
let ret = if lib.contains(".so") {
libs.into_iter().find(|(k, _)| k.as_str().starts_with(lib))
} else {
libs.into_iter().find(|(k, _)| {
k.strip_prefix(lib)
.map(|k| k.starts_with(".so") || k.starts_with('-'))
.unwrap_or_default()
})
};
let lib = lib.as_os_str();
let lib = lib.strip_suffix(OsStr::new(".so")).unwrap_or(lib);
Ok(ret.map(|(_, v)| v))
Ok(libs.into_iter().find_map(|(file_name, path)| {
file_name.strip_prefix(lib).and_then(|suffix| {
(suffix.starts_with(OsStr::new(".so")) || suffix.starts_with(OsStr::new("-")))
.then_some(path)
})
}))
}
#[derive(Debug)]
pub(crate) struct CacheEntry {
key: String,
value: String,
key: OsString,
value: OsString,
_flags: i32,
}
@ -368,11 +411,16 @@ impl LdSoCache {
}
let read_str = |pos| {
use std::os::unix::ffi::OsStrExt as _;
OsStr::from_bytes(
unsafe {
CStr::from_ptr(cursor.get_ref()[offset + pos..].as_ptr() as *const c_char)
CStr::from_ptr(
cursor.get_ref()[offset + pos..].as_ptr() as *const c_char
)
}
.to_string_lossy()
.into_owned()
.to_bytes(),
)
.to_owned()
};
let key = read_str(k_pos);
@ -389,16 +437,18 @@ impl LdSoCache {
Ok(LdSoCache { entries })
}
pub fn resolve(&self, lib: &str) -> Option<&str> {
let lib = if !lib.contains(".so") {
lib.to_string() + ".so"
} else {
lib.to_string()
};
pub fn resolve(&self, lib: &Path) -> Option<&Path> {
let lib = lib.as_os_str();
let lib = lib.strip_suffix(OsStr::new(".so")).unwrap_or(lib);
self.entries
.iter()
.find(|entry| entry.key.starts_with(&lib))
.map(|entry| entry.value.as_str())
.find_map(|CacheEntry { key, value, _flags }| {
key.strip_prefix(lib).and_then(|suffix| {
suffix
.starts_with(OsStr::new(".so"))
.then_some(Path::new(value.as_os_str()))
})
})
}
}
@ -420,7 +470,7 @@ enum ResolveSymbolError {
SectionFileRangeNone(String, Result<String, object::Error>),
}
fn resolve_symbol(path: &str, symbol: &str) -> Result<u64, ResolveSymbolError> {
fn resolve_symbol(path: &Path, symbol: &str) -> Result<u64, ResolveSymbolError> {
let data = fs::read(path)?;
let obj = object::read::File::parse(&*data)?;

@ -742,6 +742,6 @@ mod tests {
TCA_BPF_NAME as u16
);
let name = CStr::from_bytes_with_nul(inner.data).unwrap();
assert_eq!(name.to_string_lossy(), "foo");
assert_eq!(name.to_str().unwrap(), "foo");
}
}

@ -2,6 +2,7 @@ use std::{
ffi::{c_long, CString},
io, mem,
os::fd::{BorrowedFd, FromRawFd as _, OwnedFd},
path::Path,
};
use libc::{c_int, pid_t};
@ -62,17 +63,19 @@ pub(crate) fn perf_event_open_bpf(cpu: c_int) -> SysResult<OwnedFd> {
pub(crate) fn perf_event_open_probe(
ty: u32,
ret_bit: Option<u32>,
name: &str,
name: &Path,
offset: u64,
pid: Option<pid_t>,
) -> SysResult<OwnedFd> {
use std::os::unix::ffi::OsStrExt as _;
let mut attr = unsafe { mem::zeroed::<perf_event_attr>() };
if let Some(ret_bit) = ret_bit {
attr.config = 1 << ret_bit;
}
let c_name = CString::new(name).unwrap();
let c_name = CString::new(name.as_os_str().as_bytes()).unwrap();
attr.size = mem::size_of::<perf_event_attr>() as u32;
attr.type_ = ty;

Loading…
Cancel
Save