From 358b0b31f33da1ba8c846aab66f7a34b6d99b30b Mon Sep 17 00:00:00 2001 From: sectordistrict <157201659+sectordistrict@users.noreply.github.com> Date: Wed, 27 Nov 2024 19:05:23 +0200 Subject: [PATCH] 0.3.1 large commit, path resolution fixes --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/one_line_formatter.rs | 308 ++++++++++++++------------------------ src/syscall_object.rs | 133 ++++++++++++---- src/syscalls_map.rs | 23 +-- src/utilities.rs | 12 ++ 6 files changed, 236 insertions(+), 244 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 954b3f1..95ef564 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -240,7 +240,7 @@ dependencies = [ [[package]] name = "intentrace" -version = "0.2.6" +version = "0.3.1" dependencies = [ "clone3", "colored", diff --git a/Cargo.toml b/Cargo.toml index e3961e4..bd70652 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "intentrace" -version = "0.2.6" +version = "0.3.1" description = "intentrace is strace with intent, it goes all the way for you instead of half the way." edition = "2021" license = "MIT" diff --git a/src/one_line_formatter.rs b/src/one_line_formatter.rs index 81e6103..847f04b 100644 --- a/src/one_line_formatter.rs +++ b/src/one_line_formatter.rs @@ -35,7 +35,7 @@ use nix::{ MAP_SYNC, MCL_CURRENT, MCL_FUTURE, MCL_ONFAULT, O_APPEND, O_ASYNC, O_CLOEXEC, O_CREAT, O_DIRECT, O_DIRECTORY, O_DSYNC, O_EXCL, O_LARGEFILE, O_NDELAY, O_NOATIME, O_NOCTTY, O_NOFOLLOW, O_NONBLOCK, O_PATH, O_SYNC, O_TMPFILE, O_TRUNC, PRIO_PGRP, PRIO_PROCESS, - PRIO_USER, P_ALL, P_PGID, P_PID, P_PIDFD, + PRIO_USER, P_ALL, P_PGID, P_PID, P_PIDFD, AT_REMOVEDIR, RENAME_EXCHANGE, RENAME_NOREPLACE, RENAME_WHITEOUT }, sys::{ eventfd, @@ -219,7 +219,7 @@ impl SyscallObject { // TODO! also fix file mode granularity // self.one_line.push("open the file ".white()); - self.one_line.push(filename.yellow()); + handle_path_file(filename, &mut self.one_line); } Exiting => { let eph_return = self.get_syscall_return(); @@ -234,10 +234,8 @@ impl SyscallObject { } } Sysno::openat => { - // - let anchor = self.pavfol(0); + let dirfd = self.args[0] as i32; let filename = self.pavfol(1); - let filename_path = Path::new(&filename); let flags_num = self.args[2] as i32; let flags = self.pavfol(2); match self.state { @@ -253,31 +251,12 @@ impl SyscallObject { // when the last file descriptor is closed, unless the file is given a name. if (flags_num & O_TMPFILE) > 0 { self.one_line - .push("create an unnamed temporary file in the path ".white()); + .push("create an unnamed temporary file in the path: ".white()); } else { - self.one_line.push("open the file ".white()); + self.one_line.push("open the file: ".white()); } + self.possible_dirfd_file(dirfd, filename); - if filename_path.is_absolute() { - handle_path_file(filename, &mut self.one_line); - // self.one_line.push(filename.yellow()) - } else { - if self.args[0] as i32 == AT_FDCWD { - let pwd = current_dir().unwrap(); - let joined_path = pwd.join(filename_path); - self.one_line.push("open the file ".white()); - let filename = joined_path.to_str().unwrap().to_owned(); - handle_path_file(filename, &mut self.one_line); - // self.one_line.push(joined_path.as_str().unwrap().yellow()); - } else { - let anchor_path = Path::new(&anchor); - let joined_path = anchor_path.join(filename_path); - self.one_line.push("open the file ".white()); - let filename = joined_path.to_str().unwrap().to_owned(); - handle_path_file(filename, &mut self.one_line); - // self.one_line.push(joined_path.as_str().unwrap().yellow()); - } - } let mut directives = vec![]; if (flags_num & O_APPEND) == O_APPEND { directives.push("open the file in append mode".yellow()); @@ -377,7 +356,7 @@ impl SyscallObject { match self.state { Entering => { self.one_line.push("get the stats of the file: ".white()); - handle_path_file(filename, &mut self.one_line); + self.one_line.push(filename.yellow()); } Exiting => { let eph_return = self.get_syscall_return(); @@ -438,7 +417,7 @@ impl SyscallObject { Entering => { self.one_line .push("get stats for the filesystem that contains the file: ".white()); - handle_path_file(filename, &mut self.one_line); + self.one_line.push(filename.yellow()); } Exiting => { let eph_return = self.get_syscall_return(); @@ -453,15 +432,14 @@ impl SyscallObject { } } Sysno::newfstatat => { - let dirfd = self.args[0] as i32; - let dirfd_parsed = self.pavfol(0); + let dirfd: i32 = self.args[0] as i32; let filename: String = self.pavfol(1); let flags: rustix::fs::AtFlags = unsafe { std::mem::transmute(self.args[3] as u32) }; match self.state { Entering => { self.one_line.push("get the stats of the file: ".white()); - handle_path_file(filename, &mut self.one_line); + self.possible_dirfd_file(dirfd, filename); let mut flag_directive = vec![]; if flags.contains(rustix::fs::AtFlags::SYMLINK_NOFOLLOW) { @@ -637,8 +615,8 @@ impl SyscallObject { let name = user.name(); let owner = name.as_str().unwrap(); - self.one_line.push("change the owner of ".white()); - handle_path_file(filename, &mut self.one_line); + self.one_line.push("change the owner of the file: ".white()); + self.one_line.push(filename.yellow()); self.one_line.push(" to ".white()); self.one_line.push(owner.green()); if group_given != -1 { @@ -654,7 +632,7 @@ impl SyscallObject { let user = get_user_by_uid.unwrap(); let group = user.name().to_str().unwrap(); self.one_line.push("change the owner of the file: ".white()); - handle_path_file(filename, &mut self.one_line); + self.one_line.push(filename.yellow()); self.one_line.push("to ".white()); self.one_line.push(group.green()); @@ -725,9 +703,10 @@ impl SyscallObject { Sysno::fchownat => { use uzers::{Groups, Users, UsersCache}; let mut cache = UsersCache::new(); + let dirfd = self.args[0] as i32; + let filename = self.pavfol(1); let owner_given = self.args[2] as i32; let group_given = self.args[3] as i32; - let filename = self.pavfol(1); match self.state { Entering => { if owner_given != -1 { @@ -737,7 +716,8 @@ impl SyscallObject { let owner = name.as_str().unwrap(); self.one_line.push("change the owner of ".white()); - handle_path_file(filename, &mut self.one_line); + self.possible_dirfd_file(dirfd, filename); + self.one_line.push(" to ".white()); self.one_line.push(owner.green()); if group_given != -1 { @@ -2043,16 +2023,57 @@ impl SyscallObject { } } Sysno::renameat => { - let old_dirfd = self.pavfol(0); - let old_path = self.pavfol(1); - let new_dirfd = self.pavfol(2); - let new_path = self.pavfol(3); + let old_dirfd = self.args[0] as i32; + let old_filename = self.pavfol(1); + let new_dirfd = self.args[2] as i32; + let new_filename = self.pavfol(3); match self.state { Entering => { self.one_line.push("move the file: ".white()); - self.one_line.push(old_path.yellow()); + self.possible_dirfd_file(old_dirfd, old_filename); + self.one_line.push(" to: ".white()); - self.one_line.push(new_path.yellow()); + self.possible_dirfd_file(new_dirfd, new_filename); + + } + Exiting => { + let eph_return = self.get_syscall_return(); + if eph_return.is_ok() { + // TODO! granular + self.one_line.push(" |=> ".white()); + self.one_line.push("file moved".green()); + } else { + // TODO! granular + one_line_error(eph_return, &mut self.one_line, &self.errno); + } + } + } + } + Sysno::renameat2 => { + let old_dirfd = self.args[0] as i32; + let old_filename = self.pavfol(1); + let new_dirfd = self.args[2] as i32; + let new_filename = self.pavfol(3); + let flags = self.args[2] as u32; + match self.state { + Entering => { + self.one_line.push("move the file: ".white()); + self.possible_dirfd_file(old_dirfd, old_filename); + + self.one_line.push(" to: ".white()); + self.possible_dirfd_file(new_dirfd, new_filename); + + let mut directives = vec![]; + if (flags & RENAME_EXCHANGE) > 0 { + directives.push("exchange the paths atomically".yellow()) + } + if (flags & RENAME_NOREPLACE) > 0 { + directives.push("error if the new path exists".yellow()); + } + if (flags & RENAME_WHITEOUT) > 0 { + directives.push("white-out the original file".yellow()); + } + directives_handler(directives, &mut self.one_line); } Exiting => { let eph_return = self.get_syscall_return(); @@ -2099,31 +2120,21 @@ impl SyscallObject { } Sysno::mkdirat => { let dirfd = self.args[0] as i32; - let dirfd_parsed = self.pavfol(0); - let path: String = self.pavfol(1); - let path_rust = PathBuf::from(path); + let filename: String = self.pavfol(1); + match self.state { Entering => { - if dirfd == AT_FDCWD { - match path_rust.canonicalize() { - Ok(abs_path) => { - let canon_path = abs_path.canonicalize().unwrap(); - self.one_line.push("create a new directory ".white()); - self.one_line.push( - canon_path.file_name().unwrap().to_string_lossy().yellow(), - ); - self.one_line.push(" inside: ".white()); - self.one_line.push( - canon_path.parent().unwrap().to_string_lossy().yellow(), - ); - } - Err(_) => { - self.one_line.push("[intentrace Error: path error]".blink()); - } - } - } else { - panic!("dirfd not handled yet") - } + let path = self.possible_dirfd_file_output(dirfd, filename); + let path_rust = PathBuf::from(path); + + self.one_line.push("create a new directory ".white()); + self.one_line.push( + path_rust.file_name().unwrap().to_string_lossy().to_owned().blue(), + ); + self.one_line.push(" inside: ".white()); + self.one_line.push( + path_rust.parent().unwrap().to_string_lossy().to_owned().yellow(), + ); } Exiting => { let eph_return = self.get_syscall_return(); @@ -2162,25 +2173,10 @@ impl SyscallObject { match self.state { Entering => { self.one_line.push("create the symlink: ".white()); - self.one_line.push(symlink.yellow()); - let symlink_buf = PathBuf::from(symlink); - match symlink_buf.canonicalize() { - Ok(sl) => { - self.one_line.pop(); - self.one_line.push(sl.to_string_lossy().yellow()); - } - Err(_) => {} - } + handle_path_file(symlink, &mut self.one_line); + self.one_line.push(" and link it with: ".white()); - self.one_line.push(target.yellow()); - let target_buf = PathBuf::from(target); - match target_buf.canonicalize() { - Ok(tg) => { - self.one_line.pop(); - self.one_line.push(tg.to_string_lossy().yellow()); - } - Err(_) => {} - } + handle_path_file(target, &mut self.one_line); } Exiting => { let eph_return = self.get_syscall_return(); @@ -2196,46 +2192,15 @@ impl SyscallObject { } Sysno::symlinkat => { let target = self.pavfol(0); - let symlink = self.pavfol(1); - let dirfd = self.args[0] as i32; - let dirfd_parsed = self.pavfol(0); + let dirfd = self.args[1] as i32; + let symlink = self.pavfol(2); match self.state { Entering => { self.one_line.push("create the symlink: ".white()); - self.one_line.push(symlink.yellow()); - let symlink_buf = PathBuf::from(symlink); - if dirfd == AT_FDCWD { - match symlink_buf.canonicalize() { - Ok(abs_path) => { - self.one_line.pop(); - let file_name = abs_path.file_name().unwrap().to_string_lossy(); - self.one_line.push(file_name.yellow()); - } - Err(_) => { - // TODO! - } - } - } else { - panic!("dirfd not handled yet") - } - match symlink_buf.canonicalize() { - Ok(sl) => { - self.one_line.pop(); - self.one_line.push(sl.to_string_lossy().yellow()); - } - Err(_) => {} - } + self.possible_dirfd_file(dirfd, symlink); self.one_line.push(" and link it with: ".white()); self.one_line.push(target.yellow()); - let target_buf = PathBuf::from(target); - match target_buf.canonicalize() { - Ok(tg) => { - self.one_line.pop(); - self.one_line.push(tg.to_string_lossy().yellow()); - } - Err(_) => {} - } } Exiting => { let eph_return = self.get_syscall_return(); @@ -2251,23 +2216,11 @@ impl SyscallObject { } Sysno::unlink => { let path = self.pavfol(0); - let path_rust = PathBuf::from(path); match self.state { Entering => { self.one_line .push("unlink and possibly delete the file: ".white()); - match path_rust.canonicalize() { - Ok(abs_path) => { - let canon_path = abs_path.canonicalize().unwrap(); - self.one_line.push( - canon_path.file_name().unwrap().to_string_lossy().yellow(), - ); - } - Err(_) => { - self.one_line.push(path_rust.to_string_lossy().yellow()); - // self.one_line.push("[intentrace Error: path error]".blink()); - } - } + handle_path_file(path, &mut self.one_line); } Exiting => { let eph_return = self.get_syscall_return(); @@ -2283,27 +2236,18 @@ impl SyscallObject { } Sysno::unlinkat => { let dirfd = self.args[0] as i32; - let dirfd_parsed = self.pavfol(0); let path = self.pavfol(1); + let flag = self.args[2] as i32; match self.state { Entering => { - self.one_line - .push("unlink and possibly delete the file: ".white()); - self.one_line.push(path.yellow()); - let path_rust = PathBuf::from(path); - if dirfd == AT_FDCWD { - match path_rust.canonicalize() { - Ok(abs_path) => { - self.one_line.pop(); - let file_name = abs_path.file_name().unwrap().to_string_lossy(); - self.one_line.push(file_name.yellow()); - } - Err(_) => { - // self.one_line.push("[intentrace Error: path error]".blink()); - } - } - } else { - // handle dirfd + self.one_line.push("unlink and possibly delete the file: ".white()); + self.possible_dirfd_file(dirfd, path); + + if (flag & AT_REMOVEDIR) > 0 { + self.one_line.push(" (".white()); + self.one_line.push("perform the same operation as ".yellow()); + self.one_line.push("`rmdir`".blue()); + self.one_line.push(")".white()); } } Exiting => { @@ -2344,8 +2288,8 @@ impl SyscallObject { self.one_line .push("check if the process is allowed to ".white()); vanilla_commas_handler(checks, &mut self.one_line); - self.one_line.push(" the file ".white()); - self.one_line.push(filename.yellow()); + self.one_line.push(" the file: ".white()); + handle_path_file(filename, &mut self.one_line); } } } @@ -2363,7 +2307,6 @@ impl SyscallObject { } Sysno::faccessat => { let dirfd = self.args[0] as i32; - let dirfd_parsed = self.pavfol(0); let filename = self.pavfol(1); let access_mode: nix::unistd::AccessFlags = unsafe { std::mem::transmute(self.args[2] as u32) }; @@ -2373,8 +2316,9 @@ impl SyscallObject { match self.state { Entering => { if access_mode.contains(nix::unistd::AccessFlags::F_OK) { - self.one_line.push("check if the file : ".white()); - self.one_line.push(filename.yellow()); + self.one_line.push("check if the file: ".white()); + self.possible_dirfd_file(dirfd, filename); + self.one_line.push(" ".white()); self.one_line.push("exists".yellow()); } else { @@ -2392,8 +2336,8 @@ impl SyscallObject { self.one_line .push("check if the process is allowed to ".white()); vanilla_commas_handler(checks, &mut self.one_line); - self.one_line.push(" the file ".white()); - self.one_line.push(filename.yellow()); + self.one_line.push(" the file: ".white()); + self.possible_dirfd_file(dirfd, filename); } } let mut flag_directive = vec![]; @@ -2446,7 +2390,7 @@ impl SyscallObject { Entering => { if access_mode.contains(nix::unistd::AccessFlags::F_OK) { self.one_line.push("check if the file: ".white()); - self.one_line.push(filename.yellow()); + self.possible_dirfd_file(dirfd, filename); self.one_line.push(" ".white()); self.one_line.push("exists".yellow()); } else { @@ -2466,7 +2410,7 @@ impl SyscallObject { .push("check if the process is allowed to ".white()); vanilla_commas_handler(checks, &mut self.one_line); self.one_line.push(" the file ".white()); - self.one_line.push(filename.yellow()); + self.possible_dirfd_file(dirfd, filename); } } let mut flag_directive = vec![]; @@ -2512,7 +2456,7 @@ impl SyscallObject { Entering => { self.one_line .push("get the target path of the symbolic link: ".white()); - self.one_line.push(filename.yellow()); + handle_path_file(filename, &mut self.one_line); } Exiting => { let eph_return = self.get_syscall_return(); @@ -2530,35 +2474,18 @@ impl SyscallObject { } Sysno::readlinkat => { let dirfd = self.args[0] as i32; - let dirfd_parsed = self.pavfol(0); - let filename: String = self.pavfol(1); + let filename = self.pavfol(1); match self.state { Entering => { - self.one_line - .push("get the target path of the symbolic link: ".white()); - self.one_line.push(filename.yellow()); - let file_path_buf = PathBuf::from(filename); - if dirfd == AT_FDCWD { - match file_path_buf.canonicalize() { - Ok(abs_path) => { - self.one_line.pop(); - let file_name = abs_path.file_name().unwrap().to_string_lossy(); - self.one_line.push(file_name.yellow()); - } - Err(_) => { - // self.one_line.push("[intentrace Error: path error]".blink()); - } - } - } else { - panic!("dirfd not handled yet") - } - } + self.one_line.push("get the target path of the symbolic link: ".white()); + self.possible_dirfd_file(dirfd, filename) + } Exiting => { let eph_return = self.get_syscall_return(); if eph_return.is_ok() { self.one_line.push(" |=> ".white()); self.one_line.push("target retrieved: ".green()); - let target = self.pavfol(2); + let target = self.pavfol(1); self.one_line.push(target.yellow()); } else { // TODO! granular @@ -2573,7 +2500,7 @@ impl SyscallObject { match self.state { Entering => { self.one_line.push("change the mode of the file: ".white()); - self.one_line.push(filename.yellow()); + handle_path_file(filename, &mut self.one_line); mode_matcher(mode, &mut self.one_line); } Exiting => { @@ -2618,22 +2545,7 @@ impl SyscallObject { match self.state { Entering => { self.one_line.push("change the mode of the file: ".white()); - self.one_line.push(filename.yellow()); - let file_path_buf = PathBuf::from(filename); - if dirfd == AT_FDCWD { - match file_path_buf.canonicalize() { - Ok(abs_path) => { - self.one_line.pop(); - let file_name = abs_path.file_name().unwrap().to_string_lossy(); - self.one_line.push(file_name.yellow()); - } - Err(_) => { - // self.one_line.push("[intentrace Error: path error]".blink()); - } - } - } else { - panic!("dirfd not handled yet") - } + self.possible_dirfd_file(dirfd, filename); mode_matcher(mode, &mut self.one_line); self.one_line.push("and ".white()); match flag { diff --git a/src/syscall_object.rs b/src/syscall_object.rs index dabb5bb..770b8e6 100644 --- a/src/syscall_object.rs +++ b/src/syscall_object.rs @@ -5,7 +5,7 @@ use crate::{ mlock2, Annotation, ArgContainer, Bytes, BytesPagesRelevant, Category, Flag, LandlockCreateFlags, LandlockRuleTypeFlags, SysArg, SysReturn, }, - utilities::{FOLLOW_FORKS, INTENT, SYSCALL_MAP, UNSUPPORTED}, + utilities::{FOLLOW_FORKS, INTENT, SYSCALL_MAP, UNSUPPORTED, lose_relativity_on_path}, }; use colored::{ColoredString, Colorize}; @@ -29,21 +29,16 @@ use nix::{ uio::{process_vm_readv, RemoteIoVec}, wait::WaitPidFlag, }, - unistd::{AccessFlags, Pid, Whence}, + unistd::{AccessFlags, Pid, Whence}, NixPath, }; -use procfs::Current; use rustix::{ fs::StatxFlags, io::ReadWriteFlags, path::Arg, rand::GetRandomFlags, thread::FutexFlags, }; use std::{ - fmt::Display, - io::IoSliceMut, - mem::{self, transmute, zeroed}, - os::{fd::RawFd, raw::c_void}, - ptr::null, sync::atomic::Ordering, + fmt::Display, io::IoSliceMut, mem::{self, transmute, zeroed}, os::{fd::RawFd, raw::c_void}, path::PathBuf, ptr::null, sync::atomic::Ordering }; #[derive(Clone, Debug, PartialEq)] @@ -683,7 +678,7 @@ impl SyscallObject { Length_Of_Bytes_Specific_Or_Errno => { let bytes = register_value as isize; - if self.sysno == Sysno::readlink { + if self.sysno == Sysno::readlink || self.sysno == Sysno::readlinkat { if self.errno.is_some() { return Err(()); } @@ -828,7 +823,7 @@ impl SyscallObject { Pointer_To_Text(ref mut text) => { // TODO! fix this - if self.sysno == Sysno::readlink { + if self.sysno == Sysno::readlink || self.sysno == Sysno::readlinkat { // let a = nix::errno::Errno::EINVAL if self.errno.is_some() { continue; @@ -839,17 +834,33 @@ impl SyscallObject { let size = -1 * (size as i32); let error = nix::errno::Errno::from_raw(size); } else { - match SyscallObject::read_string_specific_length( - self.args[index] as usize, - self.child, - size as usize, - ) { - Some(styled_fd) => { - self.rich_args[1].1 = ArgContainer::Normal( - Pointer_To_Text(styled_fd.leak()), - ); + if self.sysno == Sysno::readlink && index == 1 { + match SyscallObject::read_string_specific_length( + self.args[index] as usize, + self.child, + size as usize, + ) { + Some(styled_fd) => { + self.rich_args[1].1 = ArgContainer::Normal( + Pointer_To_Text(styled_fd.leak()), + ); + } + None => (), } - None => (), + } else if self.sysno == Sysno::readlinkat && index == 2 { + match SyscallObject::read_string_specific_length( + self.args[index] as usize, + self.child, + size as usize, + ) { + Some(styled_fd) => { + self.rich_args[1].1 = ArgContainer::Normal( + Pointer_To_Text(styled_fd.leak()), + ); + } + None => (), + } + } } } @@ -1188,7 +1199,6 @@ impl SyscallObject { } else if fd == 2 { string.push("2 -> StdErr".bright_blue()); } else { - let file_descriptor_location = format!("/proc/{child}/fd/{fd}"); let file_info = procfs::process::FDInfo::from_raw_fd(child.into(), fd); match file_info { Ok(file) => match file.target { @@ -1320,6 +1330,65 @@ impl SyscallObject { bytes.to_string() } + pub(crate) fn possible_dirfd_file(&mut self, dirfd: i32, filename: String){ + let file_path_buf = PathBuf::from(filename); + if file_path_buf.is_relative() { + if dirfd == AT_FDCWD { + let cwd = procfs::process::Process::new(self.child.into()).unwrap().cwd().unwrap(); + self.one_line.push(cwd.as_path().to_string_lossy().yellow()); + self.one_line.push("/".yellow()); + let path_without_leading_relativeness = lose_relativity_on_path(file_path_buf.as_path().to_string_lossy().to_owned()); + self.one_line.push(path_without_leading_relativeness.blue()); + } else { + let file_info = procfs::process::FDInfo::from_raw_fd(self.child.into(), dirfd).unwrap(); + match file_info.target { + procfs::process::FDTarget::Path(path) => { + self.one_line.push(path.as_path().to_string_lossy().yellow()); + if !path.is_absolute() || path.len() != 1 { + self.one_line.push("/".yellow()); + } + let path_without_leading_relativeness = lose_relativity_on_path(file_path_buf.as_path().to_string_lossy().to_owned()); + self.one_line.push(path_without_leading_relativeness.blue()); + } + _ => unreachable!() + } + } + } else { + handle_path_file(file_path_buf.as_path().to_string_lossy().into_owned(), &mut self.one_line); + } + } + + + pub(crate) fn possible_dirfd_file_output(&mut self, dirfd: i32, filename: String) -> String { + let mut string = String::new(); + let file_path_buf = PathBuf::from(filename); + if file_path_buf.is_relative() { + if dirfd == AT_FDCWD { + let cwd = procfs::process::Process::new(10).unwrap().cwd().unwrap(); + string.push_str(&cwd.as_path().to_string_lossy()); + string.push_str("/"); + let path_without_leading_relativeness = lose_relativity_on_path(file_path_buf.as_path().to_string_lossy().to_owned()); + string.push_str(&path_without_leading_relativeness); + } else { + let file_info = procfs::process::FDInfo::from_raw_fd(self.child.into(), dirfd).unwrap(); + match file_info.target { + procfs::process::FDTarget::Path(path) => { + self.one_line.push(path.as_path().to_string_lossy().yellow()); + if !path.is_absolute() || path.len() != 1 { + self.one_line.push("/".yellow()); + } + let path_without_leading_relativeness = lose_relativity_on_path(file_path_buf.as_path().to_string_lossy().to_owned()); + self.one_line.push(path_without_leading_relativeness.blue()); + } + _ => unreachable!() + } + } + } else { + string.push_str(&file_path_buf.as_path().to_string_lossy().to_owned()); + } + string + } + // Use process_vm_readv(2) fn string_from_pointer(address: u64, child: Pid) -> String { // TODO! execve multi-threaded fails here for some reason @@ -1510,18 +1579,14 @@ impl SyscallObject { } pub(crate) fn read_affinity_from_child(addr: usize, child: Pid) -> Option> { - let mut addr = addr as *mut c_void; - const CPU_SET_USIZE: usize = CPU_SETSIZE as usize / (8 * std::mem::size_of::()); - let mut cpu_mask: [usize; CPU_SET_USIZE] = unsafe { zeroed() }; - for i in 0..CPU_SET_USIZE { - match ptrace::read(child, addr.wrapping_add(i as usize)) { - Ok(sixty_four_cpus) => cpu_mask[i] = sixty_four_cpus as usize, - Err(res) => { - return None; - } - } - } - let cpu_set: cpu_set_t = unsafe { transmute(cpu_mask) }; + const CPU_SET_USIZE: usize = (CPU_SETSIZE / 8) as usize; + + let cpu_mask = SyscallObject::read_bytes_specific_length(addr, child, CPU_SET_USIZE)?; + + let a: [u8; CPU_SET_USIZE] = cpu_mask.try_into().ok()?; + + let cpu_set: cpu_set_t = unsafe { transmute(a) }; + let mut vec = Vec::new(); for cpu_number in 0..num_cpus::get() as usize { if unsafe { CPU_ISSET(cpu_number, &cpu_set) } { @@ -1884,6 +1949,8 @@ impl SyscallObject { range } } + + impl Iterator for SyscallObject { type Item = (u64, ([&'static str; 2], ArgContainer)); fn next(&mut self) -> Option { diff --git a/src/syscalls_map.rs b/src/syscalls_map.rs index 08a8e17..17c48a1 100644 --- a/src/syscalls_map.rs +++ b/src/syscalls_map.rs @@ -433,6 +433,7 @@ pub fn initialize_syscall_map() -> HashMap { (["newdirfd", "file descriptor of a path to use as anchor if newpath is relative"], Normal(File_Descriptor(""))), (["newpath", "new path of the file"], Normal(Pointer_To_Text(""))), + (["flags", "renaming and replacement behaviour falgs"], Normal(General_Flag(FileRenameFlags))), ], (["return value", "0 success. -1 for error and errno modified"], Numeric_Or_Errno) @@ -463,17 +464,6 @@ pub fn initialize_syscall_map() -> HashMap { (["return value", "0 success. -1 for error and errno modified"], Numeric_Or_Errno) ) ), - ( - Sysno::rmdir, - ( - FileOp, - "delete a specific directory", - &[ - (["pathname", "path of the directory to remove"], Normal(Pointer_To_Text(""))), - ], - (["return value", "0 success. -1 for error and errno modified"], Numeric_Or_Errno) - ) - ), ( Sysno::link, ( @@ -536,6 +526,17 @@ pub fn initialize_syscall_map() -> HashMap { (["return value", "0 success. -1 for error and errno modified"], Numeric_Or_Errno) ) ), + ( + Sysno::rmdir, + ( + FileOp, + "delete a specific directory", + &[ + (["pathname", "path of the directory to remove"], Normal(Pointer_To_Text(""))), + ], + (["return value", "0 success. -1 for error and errno modified"], Numeric_Or_Errno) + ) + ), ( // A symbolic link (also known as a soft link) may becomoe dangling // (point to a nonexistent file); diff --git a/src/utilities.rs b/src/utilities.rs index 35db06f..562abc0 100644 --- a/src/utilities.rs +++ b/src/utilities.rs @@ -164,6 +164,18 @@ Options: args.collect::>() } +pub fn lose_relativity_on_path(string: std::borrow::Cow<'_, str>) -> String { + let mut chars = string.chars().peekable(); + while let Some(&chara) = chars.peek() { + if chara == '.' || chara == '/'{ + let _ = chars.next().unwrap(); + continue; + } + break; + } + chars.collect() +} + pub fn get_mem_difference_from_previous(post_call_brk: usize) -> isize { post_call_brk as isize - PRE_CALL_PROGRAM_BREAK_POINT.get() as isize }