From 7c64f6a086bf73658838d7cdb0ec636da2823005 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Sat, 27 May 2023 18:44:43 +0200 Subject: [PATCH] Use xopen and xdup in Scheme::handle. --- src/scheme/generate.sh | 1 + src/scheme/mod.rs | 31 ++++++++++++++++++++++++++++++- src/scheme/scheme.rs | 7 ++++--- src/scheme/scheme_block.rs | 7 ++++--- src/scheme/scheme_block_mut.rs | 7 ++++--- src/scheme/scheme_mut.rs | 7 ++++--- 6 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/scheme/generate.sh b/src/scheme/generate.sh index 3c2415bf97..be851f57b1 100755 --- a/src/scheme/generate.sh +++ b/src/scheme/generate.sh @@ -16,6 +16,7 @@ sed 's/trait Scheme/trait SchemeBlock/' scheme.rs \ | sed 's/Ok(0)/Ok(Some(0))/g' \ | sed 's/Result<\([^>]\+\)>/Result>/g' \ | sed 's/convert_to_this_scheme/convert_to_this_scheme_block/g' \ +| sed 's/convert_in_scheme_handle/convert_in_scheme_handle_block/g' \ > scheme_block.rs echo "Generating SchemeBlockMut from SchemeBlock" diff --git a/src/scheme/mod.rs b/src/scheme/mod.rs index 386229188b..054817a463 100644 --- a/src/scheme/mod.rs +++ b/src/scheme/mod.rs @@ -1,6 +1,6 @@ use core::{slice, str}; -use crate::Result; +use crate::{Error, Result, EOPNOTSUPP, ESKMSG, Packet}; pub use self::scheme::Scheme; pub use self::scheme_mut::SchemeMut; @@ -30,9 +30,38 @@ pub enum OpenResult { OtherScheme { fd: usize }, } +// TODO: Find a better solution than generate.sh pub(crate) fn convert_to_this_scheme(r: Result) -> Result { r.map(|number| OpenResult::ThisScheme { number }) } pub(crate) fn convert_to_this_scheme_block(r: Result>) -> Result> { r.map(|o| o.map(|number| OpenResult::ThisScheme { number })) } +pub(crate) fn convert_in_scheme_handle_block(_: &Packet, result: Result>) -> Result> { + match result { + Ok(Some(OpenResult::ThisScheme { number })) => Ok(Some(number)), + Ok(Some(OpenResult::OtherScheme { .. })) => Err(Error::new(EOPNOTSUPP)), + Ok(None) => Ok(None), + Err(err) => Err(err), + } +} +pub(crate) fn convert_in_scheme_handle(packet: &mut Packet, result: Result) -> Result { + match result { + Ok(OpenResult::ThisScheme { number }) => Ok(number), + Ok(OpenResult::OtherScheme { fd }) => { + packet.b = fd; + Err(Error::new(ESKMSG)) + } + Err(err) => Err(err), + } +} + +impl CallerCtx { + pub fn from_packet(packet: &Packet) -> Self { + Self { + pid: packet.pid, + uid: packet.uid, + gid: packet.gid, + } + } +} diff --git a/src/scheme/scheme.rs b/src/scheme/scheme.rs index a48126a0b7..46e3abefac 100644 --- a/src/scheme/scheme.rs +++ b/src/scheme/scheme.rs @@ -12,8 +12,9 @@ pub trait Scheme { fn handle(&self, packet: &mut Packet) { let res = match packet.a { SYS_OPEN => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } { - self.open(path, packet.d, packet.uid, packet.gid) - } else { + convert_in_scheme_handle(packet, self.xopen(path, packet.d, &CallerCtx::from_packet(&packet))) + } + else { Err(Error::new(EINVAL)) }, SYS_RMDIR => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } { @@ -27,7 +28,7 @@ pub trait Scheme { Err(Error::new(EINVAL)) }, - SYS_DUP => self.dup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }), + SYS_DUP => convert_in_scheme_handle(packet, self.xdup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }, &CallerCtx::from_packet(&packet))), SYS_READ => self.read(packet.b, unsafe { slice::from_raw_parts_mut(packet.c as *mut u8, packet.d) }), SYS_WRITE => self.write(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }), SYS_LSEEK => self.seek(packet.b, packet.c as isize, packet.d).map(|o| o as usize), diff --git a/src/scheme/scheme_block.rs b/src/scheme/scheme_block.rs index bf40c102aa..2db6c361ae 100644 --- a/src/scheme/scheme_block.rs +++ b/src/scheme/scheme_block.rs @@ -12,8 +12,9 @@ pub trait SchemeBlock { fn handle(&self, packet: &Packet) -> Option { let res = match packet.a { SYS_OPEN => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } { - self.open(path, packet.d, packet.uid, packet.gid) - } else { + convert_in_scheme_handle_block(packet, self.xopen(path, packet.d, &CallerCtx::from_packet(&packet))) + } + else { Err(Error::new(EINVAL)) }, SYS_RMDIR => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } { @@ -27,7 +28,7 @@ pub trait SchemeBlock { Err(Error::new(EINVAL)) }, - SYS_DUP => self.dup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }), + SYS_DUP => convert_in_scheme_handle_block(packet, self.xdup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }, &CallerCtx::from_packet(&packet))), SYS_READ => self.read(packet.b, unsafe { slice::from_raw_parts_mut(packet.c as *mut u8, packet.d) }), SYS_WRITE => self.write(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }), SYS_LSEEK => self.seek(packet.b, packet.c as isize, packet.d).map(|o| o.map(|o| o as usize)), diff --git a/src/scheme/scheme_block_mut.rs b/src/scheme/scheme_block_mut.rs index da9be6e590..62302ea2c9 100644 --- a/src/scheme/scheme_block_mut.rs +++ b/src/scheme/scheme_block_mut.rs @@ -12,8 +12,9 @@ pub trait SchemeBlockMut { fn handle(&mut self, packet: &Packet) -> Option { let res = match packet.a { SYS_OPEN => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } { - self.open(path, packet.d, packet.uid, packet.gid) - } else { + convert_in_scheme_handle_block(packet, self.xopen(path, packet.d, &CallerCtx::from_packet(&packet))) + } + else { Err(Error::new(EINVAL)) }, SYS_RMDIR => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } { @@ -27,7 +28,7 @@ pub trait SchemeBlockMut { Err(Error::new(EINVAL)) }, - SYS_DUP => self.dup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }), + SYS_DUP => convert_in_scheme_handle_block(packet, self.xdup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }, &CallerCtx::from_packet(&packet))), SYS_READ => self.read(packet.b, unsafe { slice::from_raw_parts_mut(packet.c as *mut u8, packet.d) }), SYS_WRITE => self.write(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }), SYS_LSEEK => self.seek(packet.b, packet.c as isize, packet.d).map(|o| o.map(|o| o as usize)), diff --git a/src/scheme/scheme_mut.rs b/src/scheme/scheme_mut.rs index 294bdac276..229a5c866b 100644 --- a/src/scheme/scheme_mut.rs +++ b/src/scheme/scheme_mut.rs @@ -12,8 +12,9 @@ pub trait SchemeMut { fn handle(&mut self, packet: &mut Packet) { let res = match packet.a { SYS_OPEN => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } { - self.open(path, packet.d, packet.uid, packet.gid) - } else { + convert_in_scheme_handle(packet, self.xopen(path, packet.d, &CallerCtx::from_packet(&packet))) + } + else { Err(Error::new(EINVAL)) }, SYS_RMDIR => if let Some(path) = unsafe { str_from_raw_parts(packet.b as *const u8, packet.c) } { @@ -27,7 +28,7 @@ pub trait SchemeMut { Err(Error::new(EINVAL)) }, - SYS_DUP => self.dup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }), + SYS_DUP => convert_in_scheme_handle(packet, self.xdup(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }, &CallerCtx::from_packet(&packet))), SYS_READ => self.read(packet.b, unsafe { slice::from_raw_parts_mut(packet.c as *mut u8, packet.d) }), SYS_WRITE => self.write(packet.b, unsafe { slice::from_raw_parts(packet.c as *const u8, packet.d) }), SYS_LSEEK => self.seek(packet.b, packet.c as isize, packet.d).map(|o| o as usize),