Implement puppeting

This commit is contained in:
Michael Mikovsky
2025-08-23 11:02:01 -06:00
parent 0e36538e84
commit 5f38d85e6c
15 changed files with 644 additions and 115 deletions
+3 -3
View File
@@ -923,9 +923,9 @@ impl Syscall {
// rqtp: args[2] as ConstPtr<libc::timespec>,
// rmtp: args[3] as Ptr<libc::timespec>,
// },
libc::SYS_exit_group => Syscall::ExitGroup {
error_code: args[0] as libc::c_int,
},
// libc::SYS_exit_group => Syscall::ExitGroup {
// error_code: args[0] as libc::c_int,
// },
// libc::SYS_epoll_wait => Syscall::EpollWait {
// epfd: args[0] as libc::c_int,
// events: args[1] as Ptr<libc::epoll_event>,
+104 -64
View File
@@ -1,30 +1,30 @@
use crate::{AsPtr, Syscall};
use syscaller::{syscall0, syscall1, syscall2, syscall3, syscall4, syscall5, syscall6};
// use syscaller::{syscall0, syscall1, syscall2, syscall3, syscall4, syscall5, syscall6};
// fn syscall0(number: isize) -> isize {
// unsafe { syscaller::syscall0(number as usize) }
// unsafe { syscaller::syscall0(number as libc::c_ulong) }
// }
// fn syscall1(number: isize, arg0: isize) -> isize {
// unsafe { syscaller::syscall1(number as usize, arg0 as usize) }
// unsafe { syscaller::syscall1(number as libc::c_ulong, arg0 as libc::c_ulong) }
// }
// fn syscall2(number: isize, arg0: isize, arg1: isize) -> isize {
// unsafe { syscaller::syscall2(number as usize, arg0 as usize, arg1 as usize) }
// unsafe { syscaller::syscall2(number as libc::c_ulong, arg0 as libc::c_ulong, arg1 as libc::c_ulong) }
// }
// fn syscall3(number: isize, arg0: isize, arg1: isize, arg2: isize) -> isize {
// unsafe { syscaller::syscall3(number as usize, arg0 as usize, arg1 as usize, arg2 as usize) }
// unsafe { syscaller::syscall3(number as libc::c_ulong, arg0 as libc::c_ulong, arg1 as libc::c_ulong, arg2 as libc::c_ulong) }
// }
// fn syscall4(number: isize, arg0: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
// unsafe {
// syscaller::syscall4(
// number as usize,
// arg0 as usize,
// arg1 as usize,
// arg2 as usize,
// arg3 as usize,
// number as libc::c_ulong,
// arg0 as libc::c_ulong,
// arg1 as libc::c_ulong,
// arg2 as libc::c_ulong,
// arg3 as libc::c_ulong,
// )
// }
// }
@@ -39,12 +39,12 @@ use syscaller::{syscall0, syscall1, syscall2, syscall3, syscall4, syscall5, sysc
// ) -> isize {
// unsafe {
// syscaller::syscall5(
// number as usize,
// arg0 as usize,
// arg1 as usize,
// arg2 as usize,
// arg3 as usize,
// arg4 as usize,
// number as libc::c_ulong,
// arg0 as libc::c_ulong,
// arg1 as libc::c_ulong,
// arg2 as libc::c_ulong,
// arg3 as libc::c_ulong,
// arg4 as libc::c_ulong,
// )
// }
// }
@@ -60,47 +60,79 @@ use syscaller::{syscall0, syscall1, syscall2, syscall3, syscall4, syscall5, sysc
// ) -> isize {
// unsafe {
// syscaller::syscall6(
// number as usize,
// arg0 as usize,
// arg1 as usize,
// arg2 as usize,
// arg3 as usize,
// arg4 as usize,
// arg5 as usize,
// number as libc::c_ulong,
// arg0 as libc::c_ulong,
// arg1 as libc::c_ulong,
// arg2 as libc::c_ulong,
// arg3 as libc::c_ulong,
// arg4 as libc::c_ulong,
// arg5 as libc::c_ulong,
// )
// }
// }
pub type Args = [libc::c_ulong; 7];
// The Syscall enum and type aliases from the previous response are assumed to be present here.
#[allow(unused_unsafe, unsafe_op_in_unsafe_fn)]
// #[allow(unused_unsafe, unsafe_op_in_unsafe_fn)]
impl Syscall {
pub unsafe fn execute_syscall(&self) -> isize {
pub fn to_syscall_args(&self) -> Args {
match self {
Syscall::Read { fd, buf, len } => syscall3(
libc::SYS_read as usize,
*fd as usize,
Syscall::Read { fd, buf, len } => [
libc::SYS_read as libc::c_ulong,
*fd as libc::c_ulong,
buf.as_ptr(),
*len as usize,
),
Syscall::Write { fd, buf, len } => syscall3(
libc::SYS_write as usize,
*fd as usize,
*len as libc::c_ulong,
0,
0,
0,
],
Syscall::Write { fd, buf, len } => [
libc::SYS_write as libc::c_ulong,
*fd as libc::c_ulong,
buf.as_ptr(),
*len as usize,
),
Syscall::Open { path, flags, mode } => syscall3(
libc::SYS_read as usize,
*len as libc::c_ulong,
0,
0,
0,
],
Syscall::Open { path, flags, mode } => [
libc::SYS_read as libc::c_ulong,
path.as_ptr(),
*flags as usize,
*mode as usize,
),
Syscall::Close { fd } => syscall1(libc::SYS_read as usize, *fd as usize),
Syscall::Stat { path, statbuf } => {
syscall2(libc::SYS_stat as usize, path.as_ptr(), statbuf.as_ptr())
}
Syscall::Fstat { fd, statbuf } => {
syscall2(libc::SYS_stat as usize, *fd as usize, statbuf.as_ptr())
}
*flags as libc::c_ulong,
*mode as libc::c_ulong,
0,
0,
0,
],
Syscall::Close { fd } => [
libc::SYS_read as libc::c_ulong,
*fd as libc::c_ulong,
0,
0,
0,
0,
0,
],
Syscall::Stat { path, statbuf } => [
libc::SYS_stat as libc::c_ulong,
path.as_ptr(),
statbuf.as_ptr(),
0,
0,
0,
0,
],
Syscall::Fstat { fd, statbuf } => [
libc::SYS_stat as libc::c_ulong,
*fd as libc::c_ulong,
statbuf.as_ptr(),
0,
0,
0,
0,
],
// Syscall::Fstat(arg0, arg1) => unsafe { syscall2(5, arg0, arg1) },
// Syscall::Lstat(arg0, arg1) => unsafe { syscall2(6, arg0, arg1) },
// Syscall::Poll(arg0, arg1, arg2) => unsafe { syscall3(7, arg0, arg1, arg2) },
@@ -112,15 +144,15 @@ impl Syscall {
flags,
fd,
offset,
} => syscall6(
libc::SYS_mmap as usize,
} => [
libc::SYS_mmap as libc::c_ulong,
addr.as_ptr(),
*len as usize,
*prot as usize,
*flags as usize,
*fd as usize,
*offset as usize,
),
*len as libc::c_ulong,
*prot as libc::c_ulong,
*flags as libc::c_ulong,
*fd as libc::c_ulong,
*offset as libc::c_ulong,
],
// Syscall::Mmap(arg0, arg1, arg2, arg3, arg4, arg5) => unsafe {
// syscall6(9, arg0, arg1, arg2, arg3, arg4, arg5)
// },
@@ -409,9 +441,15 @@ impl Syscall {
// Syscall::ClockNanosleep(arg0, arg1, arg2, arg3) => unsafe {
// syscall4(230, arg0, arg1, arg2, arg3)
// },
Syscall::ExitGroup { error_code } => unsafe {
syscall1(libc::SYS_exit_group as usize, *error_code as usize)
},
Syscall::ExitGroup { error_code } => [
libc::SYS_exit_group as libc::c_ulong,
*error_code as libc::c_ulong,
0,
0,
0,
0,
0,
],
// Syscall::EpollWait(arg0, arg1, arg2, arg3) => unsafe {
// syscall4(232, arg0, arg1, arg2, arg3)
// },
@@ -462,13 +500,15 @@ impl Syscall {
filename,
flags,
mode,
} => syscall4(
libc::SYS_openat as usize,
*dfd as usize,
} => [
libc::SYS_openat as libc::c_ulong,
*dfd as libc::c_ulong,
filename.as_ptr(),
*flags as usize,
*mode as usize,
),
*flags as libc::c_ulong,
*mode as libc::c_ulong,
0,
0,
],
// Syscall::Openat(arg0, arg1, arg2, arg3) => unsafe { syscall4(257, arg0, arg1, arg2, arg3) },
// Syscall::Mkdirat(arg0, arg1, arg2) => unsafe { syscall3(258, arg0, arg1, arg2) },
// Syscall::Mknodat(arg0, arg1, arg2, arg3) => unsafe {
+2 -2
View File
@@ -33,7 +33,7 @@ impl<T: Debug> Debug for Buf<T> {
}
impl<T: Debug> AsPtr for Buf<T> {
fn as_ptr(&self) -> usize {
self.0 as usize
fn as_ptr(&self) -> libc::c_ulong {
self.0 as libc::c_ulong
}
}
+1 -1
View File
@@ -16,5 +16,5 @@ pub use x86_64::*;
const MAX_STR_LEN: usize = 30;
pub trait AsPtr {
fn as_ptr(&self) -> usize;
fn as_ptr(&self) -> libc::c_ulong;
}
+2 -2
View File
@@ -28,8 +28,8 @@ impl<T: Debug> Debug for Ptr<T> {
}
impl<T: Debug> AsPtr for Ptr<T> {
fn as_ptr(&self) -> usize {
self.0 as usize
fn as_ptr(&self) -> libc::c_ulong {
self.0 as libc::c_ulong
}
}
+2 -2
View File
@@ -20,7 +20,7 @@ impl Debug for PtrVoid {
}
impl AsPtr for PtrVoid {
fn as_ptr(&self) -> usize {
self.0 as usize
fn as_ptr(&self) -> libc::c_ulong {
self.0 as libc::c_ulong
}
}
+2 -2
View File
@@ -39,7 +39,7 @@ impl Debug for StrRef {
}
impl AsPtr for StrRef {
fn as_ptr(&self) -> usize {
self.0 as usize
fn as_ptr(&self) -> libc::c_ulong {
self.0 as libc::c_ulong
}
}