Skip to content

Commit

Permalink
0.3.1 large commit, path resolution fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sectordistrict committed Nov 27, 2024
1 parent 9573137 commit 358b0b3
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 244 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
308 changes: 110 additions & 198 deletions src/one_line_formatter.rs

Large diffs are not rendered by default.

133 changes: 100 additions & 33 deletions src/syscall_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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)]
Expand Down Expand Up @@ -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(());
}
Expand Down Expand Up @@ -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;
Expand All @@ -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 => (),
}

}
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1510,18 +1579,14 @@ impl SyscallObject {
}

pub(crate) fn read_affinity_from_child(addr: usize, child: Pid) -> Option<Vec<usize>> {
let mut addr = addr as *mut c_void;
const CPU_SET_USIZE: usize = CPU_SETSIZE as usize / (8 * std::mem::size_of::<usize>());
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) } {
Expand Down Expand Up @@ -1884,6 +1949,8 @@ impl SyscallObject {
range
}
}


impl Iterator for SyscallObject {
type Item = (u64, ([&'static str; 2], ArgContainer));
fn next(&mut self) -> Option<Self::Item> {
Expand Down
23 changes: 12 additions & 11 deletions src/syscalls_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ pub fn initialize_syscall_map() -> HashMap<Sysno, SysDetails> {

(["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)
Expand Down Expand Up @@ -463,17 +464,6 @@ pub fn initialize_syscall_map() -> HashMap<Sysno, SysDetails> {
(["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,
(
Expand Down Expand Up @@ -536,6 +526,17 @@ pub fn initialize_syscall_map() -> HashMap<Sysno, SysDetails> {
(["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);
Expand Down
12 changes: 12 additions & 0 deletions src/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ Options:
args.collect::<Vec<String>>()
}

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
}
Expand Down

0 comments on commit 358b0b3

Please sign in to comment.