Skip to content

Commit

Permalink
Implements open for direct dumping
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed May 11, 2024
1 parent e5df6dc commit 33da54c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
58 changes: 58 additions & 0 deletions src/direct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::method::{OpenFlags, OwnedFd};
use crate::DumpMethod;
use core::ffi::{c_int, CStr};
use core::num::NonZeroI32;
use korbis::thread::Thread;
use korbis::uio::UioSeg;
use korbis::Kernel;

/// Implementation of [`DumpMethod`] using internal kernel functions.
///
/// This method require a first dump from syscall method for required function addresses.
pub struct DirectMethod<K> {
kernel: K,
}

impl<K> DirectMethod<K> {
pub fn new(kernel: K) -> Self {
Self { kernel }
}
}

impl<K: Kernel> DumpMethod for DirectMethod<K> {
fn open(
&self,
path: &CStr,
flags: OpenFlags,
mode: c_int,
) -> Result<OwnedFd<Self>, NonZeroI32> {
let td = Thread::current();
let errno = unsafe {
self.kernel.kern_openat(
td,
-100,
path.as_ptr(),
UioSeg::Kernel,
flags.bits() as _,
mode,
)
};

match NonZeroI32::new(errno) {
Some(v) => Err(v),
None => Ok(unsafe { OwnedFd::new(self, (*td).ret(0).try_into().unwrap()) }),
}
}

fn write(&self, fd: c_int, buf: *const u8, len: usize) -> Result<usize, NonZeroI32> {
Ok(len)
}

fn fsync(&self, fd: c_int) -> Result<(), NonZeroI32> {
Ok(())
}

fn close(&self, fd: c_int) -> Result<(), NonZeroI32> {
Ok(())
}
}
22 changes: 13 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use core::panic::PanicInfo;
use korbis::Kernel;
use x86_64::registers::model_specific::LStar;

#[cfg(method = "direct")]
mod direct;
mod method;
#[cfg(method = "syscall")]
mod syscall;
Expand Down Expand Up @@ -73,6 +75,8 @@ pub extern "C" fn main(_: *const u8) {
// Setup dumping method.
#[cfg(method = "syscall")]
let method = unsafe { crate::syscall::SyscallMethod::new(&kernel) };
#[cfg(method = "direct")]
let method = crate::direct::DirectMethod::new(kernel);

// Create dump file.
let out = match method.open(
Expand Down Expand Up @@ -108,17 +112,17 @@ pub extern "C" fn main(_: *const u8) {
return;
}

// Sync.
if method.fsync(fd).is_err() {
notify(
&method,
"Failed to synchronize changes to a /mnt/usb0/kernel.elf",
);
data = &data[written..];
}

return;
}
// Sync.
if method.fsync(out.as_raw_fd()).is_err() {
notify(
&method,
"Failed to synchronize changes to a /mnt/usb0/kernel.elf",
);

data = &data[written..];
return;
}

notify(&method, "Dump completed!");
Expand Down

0 comments on commit 33da54c

Please sign in to comment.