From 789c1957ed709c0aa5edb5a9420682ed67958a74 Mon Sep 17 00:00:00 2001 From: Graham Brereton Date: Wed, 25 Oct 2023 16:57:23 -0400 Subject: [PATCH 1/3] chore: Use stable channel Full relro is the default behaviour on Linux and x64, so the experimental build flag is not required. Work around lack of `Vec::into_raw_parts`. Use a standard `Vec` for node paths. It should provide the same performance or better than using a `LinkedList`, and it has `retain` defined which facilitates the `remove` functionality. Signed-off-by: Graham Brereton --- .cargo/config.toml | 2 -- Dockerfile | 3 +-- rust-toolchain | 2 +- src/hookfs/mod.rs | 10 +++++----- src/injector/multi_injector.rs | 8 ++++---- src/lib.rs | 5 ----- src/main.rs | 5 ----- src/mount_injector.rs | 4 ++-- src/replacer/fd_replacer.rs | 6 ++++-- src/replacer/mmap_replacer.rs | 6 ++++-- 10 files changed, 21 insertions(+), 30 deletions(-) delete mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index 731b547..0000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[build] -rustflags = ["-Z", "relro-level=full"] \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 3baa75a..0673bd2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,7 @@ ENV https_proxy $HTTPS_PROXY RUN apt-get update && apt-get install build-essential curl git pkg-config libfuse-dev fuse -y && rm -rf /var/lib/apt/lists/* -RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain nightly-2021-12-23 -y +RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain stable -y ENV PATH "/root/.cargo/bin:${PATH}" RUN if [ -n "$HTTP_PROXY" ]; then echo "[http]\n\ @@ -24,7 +24,6 @@ COPY . /toda-build WORKDIR /toda-build -ENV RUSTFLAGS "-Z relro-level=full" RUN --mount=type=cache,target=/toda-build/target \ --mount=type=cache,target=/root/.cargo/registry \ cargo build --release diff --git a/rust-toolchain b/rust-toolchain index 9ea09ab..2bf5ad0 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2021-12-23 \ No newline at end of file +stable diff --git a/src/hookfs/mod.rs b/src/hookfs/mod.rs index d72fbae..ffe01a2 100644 --- a/src/hookfs/mod.rs +++ b/src/hookfs/mod.rs @@ -4,7 +4,7 @@ mod reply; pub mod runtime; mod utils; -use std::collections::{HashMap, LinkedList}; +use std::collections::HashMap; use std::ffi::{CString, OsStr, OsString}; use std::os::unix::ffi::OsStrExt; use std::os::unix::io::RawFd; @@ -159,12 +159,12 @@ pub struct HookFs { struct Node { pub ref_count: u64, // TODO: optimize paths with a combination data structure - paths: LinkedList, + paths: Vec, } impl Node { fn get_path(&self) -> Option<&Path> { - self.paths.back().map(|item| item.as_path()) + self.paths.last().map(|item| item.as_path()) } fn insert(&mut self, path: PathBuf) { @@ -174,11 +174,11 @@ impl Node { } } - self.paths.push_back(path); + self.paths.push(path); } fn remove(&mut self, path: &Path) { - self.paths.drain_filter(|x| x == path); + self.paths.retain(|x| x != path); } } diff --git a/src/injector/multi_injector.rs b/src/injector/multi_injector.rs index 92c4723..a6a9772 100644 --- a/src/injector/multi_injector.rs +++ b/src/injector/multi_injector.rs @@ -25,16 +25,16 @@ impl MultiInjector { for injector in conf.into_iter() { let injector = match injector { InjectorConfig::Fault(faults) => { - (box FaultInjector::build(faults)?) as Box + (Box::new(FaultInjector::build(faults)?)) as Box } InjectorConfig::Latency(latency) => { - (box LatencyInjector::build(latency)?) as Box + (Box::new(LatencyInjector::build(latency)?)) as Box } InjectorConfig::AttrOverride(attr_override) => { - (box AttrOverrideInjector::build(attr_override)?) as Box + (Box::new(AttrOverrideInjector::build(attr_override)?)) as Box } InjectorConfig::Mistake(mistakes) => { - (box MistakeInjector::build(mistakes)?) as Box + (Box::new(MistakeInjector::build(mistakes)?)) as Box } }; injectors.push(injector) diff --git a/src/lib.rs b/src/lib.rs index ae8b02a..280aaeb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,11 +11,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#![feature(box_syntax)] -#![feature(async_closure)] -#![feature(vec_into_raw_parts)] -#![feature(atomic_mut_ptr)] -#![feature(drain_filter)] #![allow(clippy::or_fun_call)] #![allow(clippy::too_many_arguments)] diff --git a/src/main.rs b/src/main.rs index d860e90..affe648 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,11 +11,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#![feature(box_syntax)] -#![feature(async_closure)] -#![feature(vec_into_raw_parts)] -#![feature(atomic_mut_ptr)] -#![feature(drain_filter)] #![allow(clippy::or_fun_call)] #![allow(clippy::too_many_arguments)] diff --git a/src/mount_injector.rs b/src/mount_injector.rs index 2ac35f9..dca7493 100644 --- a/src/mount_injector.rs +++ b/src/mount_injector.rs @@ -125,7 +125,7 @@ impl MountInjector { let cloned_hookfs = hookfs.clone(); let (before_mount_waiter, before_mount_guard) = stop::lock(); - let handler = std::thread::spawn(box move || { + let handler = std::thread::spawn(Box::new(move || { let fs = hookfs::AsyncFileSystem::from(cloned_hookfs); std::fs::create_dir_all(new_path.as_path())?; @@ -144,7 +144,7 @@ impl MountInjector { drop(hookfs::runtime::RUNTIME.write().unwrap().take().unwrap()); Ok(()) - }); + })); // TODO: remove this. But wait for FUSE gets up // Related Issue: https://github.com/zargony/fuse-rs/issues/9 before_mount_waiter.wait(); diff --git a/src/replacer/fd_replacer.rs b/src/replacer/fd_replacer.rs index aca42dc..59b9a50 100644 --- a/src/replacer/fd_replacer.rs +++ b/src/replacer/fd_replacer.rs @@ -105,9 +105,11 @@ impl ProcessAccessor { let mut new_paths = Vec::new(); self.new_paths.read_to_end(&mut new_paths)?; - let (cases_ptr, length, _) = self.cases.clone().into_raw_parts(); + let cases = &mut *self.cases.clone(); + let length = cases.len(); + let cases_ptr = &mut cases[0] as *mut ReplaceCase as *mut u8; let size = length * std::mem::size_of::(); - let cases = unsafe { std::slice::from_raw_parts(cases_ptr as *mut u8, size) }; + let cases = unsafe { std::slice::from_raw_parts(cases_ptr, size) }; self.process.run_codes(|addr| { let mut vec_rt = diff --git a/src/replacer/mmap_replacer.rs b/src/replacer/mmap_replacer.rs index 276a602..15e262a 100644 --- a/src/replacer/mmap_replacer.rs +++ b/src/replacer/mmap_replacer.rs @@ -154,9 +154,11 @@ impl ProcessAccessor { let mut new_paths = Vec::new(); self.new_paths.read_to_end(&mut new_paths)?; - let (cases_ptr, length, _) = self.cases.clone().into_raw_parts(); + let cases = &mut *self.cases.clone(); + let length = cases.len(); + let cases_ptr = &mut cases[0] as *mut RawReplaceCase as *mut u8; let size = length * std::mem::size_of::(); - let cases = unsafe { std::slice::from_raw_parts(cases_ptr as *mut u8, size) }; + let cases = unsafe { std::slice::from_raw_parts(cases_ptr, size) }; self.process.run_codes(|addr| { let mut vec_rt = From 777f93dd6ae497973097b42ee49383247aedfe08 Mon Sep 17 00:00:00 2001 From: Graham Brereton Date: Wed, 25 Oct 2023 17:41:16 -0400 Subject: [PATCH 2/3] deps: update nix to 0.27.1 Addresses CVE-2021-45707 Signed-off-by: Graham Brereton --- Cargo.lock | 37 ++++++++++++++++++------------------- Cargo.toml | 2 +- src/fuse_device.rs | 8 +++----- src/hookfs/errors.rs | 11 ++--------- src/ptrace/mod.rs | 20 +++++++------------- 5 files changed, 31 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fe39508..1aec3fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -75,6 +75,12 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + [[package]] name = "byteorder" version = "1.4.2" @@ -93,12 +99,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" -[[package]] -name = "cc" -version = "1.0.66" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c0496836a84f8d0495758516b8621a622beb77c0fed418570e50764093ced48" - [[package]] name = "cfg-if" version = "0.1.10" @@ -132,7 +132,7 @@ checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" dependencies = [ "ansi_term 0.11.0", "atty", - "bitflags", + "bitflags 1.2.1", "strsim", "textwrap", "unicode-width", @@ -165,7 +165,7 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7d1242462849390bb2ad38aeed769499f1afc7383affa2ab0c1baa894c0200" dependencies = [ - "bitflags", + "bitflags 1.2.1", "byteorder", "lazy_static", "proc-macro-error", @@ -216,7 +216,7 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" dependencies = [ - "bitflags", + "bitflags 1.2.1", "fuchsia-zircon-sys", ] @@ -539,9 +539,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.82" +version = "0.2.149" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929" +checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" [[package]] name = "libflate" @@ -686,13 +686,12 @@ dependencies = [ [[package]] name = "nix" -version = "0.18.0" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83450fe6a6142ddd95fb064b746083fc4ef1705fe81f64a64e1d4b39f54a1055" +checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" dependencies = [ - "bitflags", - "cc", - "cfg-if 0.1.10", + "bitflags 2.4.1", + "cfg-if 1.0.0", "libc", ] @@ -872,7 +871,7 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4a336c8310f4955f343935b9c11a30254d1ad8fad98ec257a4407a061a6fd49" dependencies = [ - "bitflags", + "bitflags 1.2.1", "byteorder", "chrono", "hex", @@ -937,7 +936,7 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94341e4e44e24f6b591b59e47a8a027df12e008d73fd5672dbea9cc22f4507d9" dependencies = [ - "bitflags", + "bitflags 1.2.1", ] [[package]] @@ -1187,7 +1186,7 @@ version = "0.2.3" dependencies = [ "anyhow", "async-trait", - "bitflags", + "bitflags 1.2.1", "derive_more", "dynasmrt", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 642e255..7bba0f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ edition = "2018" [dependencies] structopt = "0.3" -nix = "0.18" +nix = {version = "0.27.1", features = ["dir", "fs", "mman", "mount", "process", "ptrace", "signal", "uio", "user"]} anyhow = "1.0" fuser = {version = "0.6", features = ["abi-7-19"]} time = "0.1" diff --git a/src/fuse_device.rs b/src/fuse_device.rs index 437fc84..b2b723c 100644 --- a/src/fuse_device.rs +++ b/src/fuse_device.rs @@ -1,18 +1,16 @@ use nix::sys::stat::{makedev, mknod, Mode, SFlag}; -use nix::Error as NixError; pub fn mkfuse_node() -> anyhow::Result<()> { - let mode = unsafe { Mode::from_bits_unchecked(0o666) }; + let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IWGRP | Mode::S_IRGRP | Mode::S_IWOTH | Mode::S_IROTH; let dev = makedev(10, 229); match mknod("/dev/fuse", SFlag::S_IFCHR, mode, dev) { Ok(()) => Ok(()), - Err(NixError::Sys(errno)) => { + Err(errno) => { if errno == nix::errno::Errno::EEXIST { Ok(()) } else { - Err(NixError::from_errno(errno).into()) + Err(errno.into()) } } - Err(err) => Err(err.into()), } } diff --git a/src/hookfs/errors.rs b/src/hookfs/errors.rs index d3a95b0..d548ad7 100644 --- a/src/hookfs/errors.rs +++ b/src/hookfs/errors.rs @@ -36,15 +36,8 @@ impl HookFsError { } impl From for HookFsError { - fn from(err: Error) -> HookFsError { - // TODO: match more error types - match err { - Error::Sys(errno) => HookFsError::Sys(errno), - _ => { - error!("unknown error {:?}", err); - HookFsError::UnknownError - } - } + fn from(errno: Error) -> HookFsError { + HookFsError::Sys(errno) } } diff --git a/src/ptrace/mod.rs b/src/ptrace/mod.rs index bcf6475..8cb7616 100644 --- a/src/ptrace/mod.rs +++ b/src/ptrace/mod.rs @@ -1,6 +1,7 @@ use std::cell::RefCell; use std::collections::{HashMap, HashSet}; use std::ffi::CString; +use std::io::IoSlice; use std::os::unix::ffi::OsStrExt; use std::path::Path; @@ -8,17 +9,15 @@ use anyhow::{anyhow, Result}; use nix::errno::Errno; use nix::sys::mman::{MapFlags, ProtFlags}; use nix::sys::signal::Signal; -use nix::sys::uio::{process_vm_writev, IoVec, RemoteIoVec}; +use nix::sys::uio::{process_vm_writev, RemoteIoVec}; use nix::sys::{ptrace, wait}; use nix::unistd::Pid; -use nix::Error::Sys; use procfs::process::Task; use procfs::ProcError; use retry::delay::Fixed; use retry::Error::{self, Operation}; use retry::OperationResult; use tracing::{error, info, instrument, trace, warn}; -use Error::Internal; // There should be only one PtraceManager in one thread. But as we don't implement TLS // , we cannot use thread-local variables safely. @@ -47,16 +46,12 @@ fn attach_task(task: &Task) -> Result<()> { trace!("attach task: {}", task.tid); match ptrace::attach(pid) { - Err(Sys(errno)) + Err(errno) if errno == Errno::ESRCH || (errno == Errno::EPERM && thread_is_gone(process.stat.state)) => { info!("task {} doesn't exist, maybe has stopped", task.tid) } - Err(err) => { - warn!("attach error: {:?}", err); - return Err(err.into()); - } _ => {} } info!("attach task: {} successfully", task.tid); @@ -145,7 +140,7 @@ impl PtraceManager { Ok(()) => { info!("successfully detached task: {}", task.tid); } - Err(Sys(Errno::ESRCH)) => trace!( + Err(Errno::ESRCH) => trace!( "task {} doesn't exist, maybe has stopped or not traced", task.tid ), @@ -168,7 +163,7 @@ impl PtraceManager { total_delay: _, tries: _, } => return Err(e), - Internal(err) => error!("internal error: {:?}", err), + Error::Internal(err) => error!("internal error: {:?}", err), } }; } @@ -325,7 +320,7 @@ impl TracedProcess { process_vm_writev( pid, - &[IoVec::from_slice(content)], + &[IoSlice::new(content)], &[RemoteIoVec { base: addr as usize, len: content.len(), @@ -366,12 +361,11 @@ impl TracedProcess { let status = wait::waitpid(pid, None)?; info!("wait status: {:?}", status); - use nix::sys::signal::SIGTRAP; let regs = ptrace::getregs(pid)?; info!("current registers: {:?}", regs); match status { - wait::WaitStatus::Stopped(_, SIGTRAP) => { + wait::WaitStatus::Stopped(_, Signal::SIGTRAP) => { break; } _ => info!("continue running replacers"), From f64664c2077014ca459fd1218ad06ca40a1a94ad Mon Sep 17 00:00:00 2001 From: Graham Brereton Date: Wed, 25 Oct 2023 17:56:06 -0400 Subject: [PATCH 3/3] chore: address cargo clippy diagnostics Signed-off-by: Graham Brereton --- src/hookfs/mod.rs | 24 +++++++++--------------- src/mount.rs | 2 +- src/replacer/fd_replacer.rs | 5 ++--- src/replacer/mmap_replacer.rs | 5 ++--- 4 files changed, 14 insertions(+), 22 deletions(-) diff --git a/src/hookfs/mod.rs b/src/hookfs/mod.rs index ffe01a2..633fc73 100644 --- a/src/hookfs/mod.rs +++ b/src/hookfs/mod.rs @@ -728,7 +728,7 @@ impl AsyncFileSystemImpl for HookFs { // filter out append. The kernel layer will translate the // offsets for us appropriately. let filtered_flags = flags & (!libc::O_APPEND) & (!libc::O_DIRECT); - let filtered_flags = OFlag::from_bits_truncate(filtered_flags as i32); + let filtered_flags = OFlag::from_bits_truncate(filtered_flags); let inode_map = self.inode_map.read().await; let path = inode_map.get_path(ino)?; @@ -849,7 +849,7 @@ impl AsyncFileSystemImpl for HookFs { let inode_map = self.inode_map.read().await; let path = { inode_map.get_path(ino)?.to_owned() }; let filtered_flags = flags & (!libc::O_APPEND); - let filtered_flags = OFlag::from_bits_truncate(filtered_flags as i32); + let filtered_flags = OFlag::from_bits_truncate(filtered_flags); let path_clone = path.clone(); let dir = spawn_blocking(move || { @@ -889,7 +889,7 @@ impl AsyncFileSystemImpl for HookFs { trace!("empty reply"); return Ok(()); } - for (index, entry) in all_entries.iter().enumerate().skip(offset as usize) { + for (index, entry) in all_entries.iter().enumerate().skip(offset) { let entry = (*entry)?; let name = entry.file_name(); @@ -990,9 +990,6 @@ impl AsyncFileSystemImpl for HookFs { let cpath = CString::new(path.as_os_str().as_bytes())?; let name = CString::new(name.as_bytes())?; - let mut buf = Vec::new(); - buf.resize(size as usize, 0u8); - let data = async_getxattr(cpath, name, size as usize).await?; let mut reply = if size == 0 { @@ -1016,8 +1013,7 @@ impl AsyncFileSystemImpl for HookFs { let path = inode_map.get_path(ino)?.to_owned(); let cpath = CString::new(path.as_os_str().as_bytes())?; - let mut buf = Vec::new(); - buf.resize(size as usize, 0u8); + let buf = vec![0u8; size as usize]; let shared_buf = std::sync::Arc::new(buf); let buf_clone = shared_buf.clone(); @@ -1073,7 +1069,7 @@ impl AsyncFileSystemImpl for HookFs { let inode_map = self.inode_map.read().await; let path = inode_map.get_path(ino)?.to_owned(); - let mask = AccessFlags::from_bits_truncate(mask as i32); + let mask = AccessFlags::from_bits_truncate(mask); let path_clone = path.to_path_buf(); spawn_blocking(move || nix::unistd::access(&path_clone, mask)).await??; @@ -1102,7 +1098,7 @@ impl AsyncFileSystemImpl for HookFs { }; let filtered_flags = flags & (!libc::O_APPEND); - let filtered_flags = OFlag::from_bits_truncate(filtered_flags as i32); + let filtered_flags = OFlag::from_bits_truncate(filtered_flags); let mode = stat::Mode::from_bits_truncate(mode); trace!("create with flags: {:?}, mode: {:?}", filtered_flags, mode); @@ -1180,14 +1176,13 @@ async fn async_setxattr(path: CString, name: CString, data: Vec, flags: i32) async fn async_getxattr(path: CString, name: CString, size: usize) -> Result> { spawn_blocking(move || { - let mut buf = Vec::new(); - buf.resize(size, 0); + let mut buf = vec![0; size]; let path_ptr = &path.as_bytes_with_nul()[0] as *const u8 as *const libc::c_char; let name_ptr = &name.as_bytes_with_nul()[0] as *const u8 as *const libc::c_char; let buf_ptr = buf.as_slice() as *const [u8] as *mut [u8] as *mut libc::c_void; - let ret = unsafe { lgetxattr(path_ptr, name_ptr, buf_ptr, size as usize) }; + let ret = unsafe { lgetxattr(path_ptr, name_ptr, buf_ptr, size) }; if ret == -1 { Err(Error::last()) } else { @@ -1200,8 +1195,7 @@ async fn async_getxattr(path: CString, name: CString, size: usize) -> Result Result> { spawn_blocking(move || unsafe { - let mut buf = Vec::new(); - buf.resize(count, 0); + let mut buf = vec![0; count]; let ret = libc::pread(fd, buf.as_ptr() as *mut c_void, count, offset); if ret == -1 { Err(Error::last()) diff --git a/src/mount.rs b/src/mount.rs index 286fd06..0741f21 100644 --- a/src/mount.rs +++ b/src/mount.rs @@ -21,7 +21,7 @@ impl MountsInfo { pub fn non_root>(&self, path: P) -> Result { let mount_points = self.mounts.iter().map(|item| &item.mount_point); for mount_point in mount_points { - if path.as_ref().starts_with(&mount_point) { + if path.as_ref().starts_with(mount_point) { // The relationship is "contain" because if we want to inject /a/b, and /a is a mount point, we can still // use this method. return Ok(true); diff --git a/src/replacer/fd_replacer.rs b/src/replacer/fd_replacer.rs index 59b9a50..b4b8235 100644 --- a/src/replacer/fd_replacer.rs +++ b/src/replacer/fd_replacer.rs @@ -106,9 +106,8 @@ impl ProcessAccessor { self.new_paths.read_to_end(&mut new_paths)?; let cases = &mut *self.cases.clone(); - let length = cases.len(); let cases_ptr = &mut cases[0] as *mut ReplaceCase as *mut u8; - let size = length * std::mem::size_of::(); + let size = std::mem::size_of_val(cases); let cases = unsafe { std::slice::from_raw_parts(cases_ptr, size) }; self.process.run_codes(|addr| { @@ -229,7 +228,7 @@ impl FdReplacer { .filter(|(_, path)| path.starts_with(detect_path)) .filter_map(move |(fd, path)| { trace!("replace fd({}): {}", fd, path.display()); - let stripped_path = path.strip_prefix(&detect_path).ok()?; + let stripped_path = path.strip_prefix(detect_path).ok()?; Some((process.clone(), (fd, new_path.join(stripped_path)))) }) }) diff --git a/src/replacer/mmap_replacer.rs b/src/replacer/mmap_replacer.rs index 15e262a..eb6ea76 100644 --- a/src/replacer/mmap_replacer.rs +++ b/src/replacer/mmap_replacer.rs @@ -155,9 +155,8 @@ impl ProcessAccessor { self.new_paths.read_to_end(&mut new_paths)?; let cases = &mut *self.cases.clone(); - let length = cases.len(); let cases_ptr = &mut cases[0] as *mut RawReplaceCase as *mut u8; - let size = length * std::mem::size_of::(); + let size = std::mem::size_of_val(cases); let cases = unsafe { std::slice::from_raw_parts(cases_ptr, size) }; self.process.run_codes(|addr| { @@ -315,7 +314,7 @@ impl MmapReplacer { }) .filter(|(_, case)| case.path.starts_with(detect_path)) .filter_map(|(process, mut case)| { - let stripped_path = case.path.strip_prefix(&detect_path).ok()?; + let stripped_path = case.path.strip_prefix(detect_path).ok()?; case.path = new_path.join(stripped_path); Some((process, case)) })