From 1660e541d9360e4b1eb6ed0744dd0c2ff9c446b5 Mon Sep 17 00:00:00 2001 From: Putta Khunchalee Date: Sat, 23 Dec 2023 18:44:44 +0700 Subject: [PATCH] Implements syscall 602 (#532) --- src/kernel/src/main.rs | 2 +- src/kernel/src/process/mod.rs | 45 +++++++++++++++++++++++++++++--- src/kernel/src/syscalls/input.rs | 18 ++++++++----- 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/kernel/src/main.rs b/src/kernel/src/main.rs index 29a06a421..e5166f2b5 100644 --- a/src/kernel/src/main.rs +++ b/src/kernel/src/main.rs @@ -183,7 +183,7 @@ fn main() -> ExitCode { let arnd = Arnd::new(); let llvm = Llvm::new(); let mut syscalls = Syscalls::new(); - let vp = match VProc::new(auth, &mut syscalls) { + let vp = match VProc::new(auth, "QXuNNl0Zhn", &mut syscalls) { Ok(v) => v, Err(e) => { error!(e, "Virtual process initialization failed"); diff --git a/src/kernel/src/process/mod.rs b/src/kernel/src/process/mod.rs index ab625fd1d..51dd0744e 100644 --- a/src/kernel/src/process/mod.rs +++ b/src/kernel/src/process/mod.rs @@ -19,6 +19,8 @@ use crate::ucred::{AuthInfo, Privilege, Ucred}; use gmtx::{Gutex, GutexGroup, GutexReadGuard, GutexWriteGuard}; use llt::{SpawnError, Thread}; use std::any::Any; +use std::cmp::min; +use std::ffi::c_char; use std::mem::zeroed; use std::num::NonZeroI32; use std::ptr::null_mut; @@ -49,6 +51,7 @@ pub struct VProc { group: Gutex>, // p_pgrp sigacts: Gutex, // p_sigacts files: FileDesc, // p_fd + system_path: String, // p_randomized_path limits: [ResourceLimit; ResourceLimit::NLIMITS], // p_limit comm: Gutex>, // p_comm objects: Gutex>>, @@ -61,7 +64,11 @@ pub struct VProc { } impl VProc { - pub fn new(auth: AuthInfo, sys: &mut Syscalls) -> Result, VProcError> { + pub fn new>( + auth: AuthInfo, + system_path: S, + sys: &mut Syscalls, + ) -> Result, VProcError> { // TODO: Check how ucred is constructed for a process. let gg = GutexGroup::new(); let limits = Self::load_limits()?; @@ -72,6 +79,7 @@ impl VProc { group: gg.spawn(None), sigacts: gg.spawn(SignalActs::new()), files: FileDesc::new(&gg), + system_path: system_path.into(), objects: gg.spawn(Idt::new(0x1000)), dmem_container: gg.spawn(0), // TODO: Check the initial value on the PS4. budget: gg.spawn(None), @@ -95,6 +103,7 @@ impl VProc { sys.register(557, &vp, Self::sys_namedobj_create); sys.register(585, &vp, Self::sys_is_in_sandbox); sys.register(587, &vp, Self::sys_get_authinfo); + sys.register(602, &vp, Self::sys_randomized_path); Ok(vp) } @@ -572,9 +581,8 @@ impl VProc { } fn sys_is_in_sandbox(self: &Arc, _: &SysIn) -> Result { - // TODO: Get the actual value from the PS4. - info!("Returning is_in_sandbox as 0."); - Ok(0.into()) + // TODO: Implement this once FS rework has been usable. + Ok(1.into()) } fn sys_get_authinfo(self: &Arc, i: &SysIn) -> Result { @@ -615,6 +623,35 @@ impl VProc { Ok(SysOut::ZERO) } + fn sys_randomized_path(self: &Arc, i: &SysIn) -> Result { + let set = i.args[0]; + let get: *mut c_char = i.args[1].into(); + let len: *mut usize = i.args[2].into(); + + // Get the value. + let len = if get.is_null() || len.is_null() { + 0 + } else { + let v = unsafe { *len }; + unsafe { *len = self.system_path.len() }; + v + }; + + if len > 0 && !self.system_path.is_empty() { + let len = min(len - 1, self.system_path.len()); + + unsafe { get.copy_from_nonoverlapping(self.system_path.as_ptr().cast(), len) }; + unsafe { *get.add(len) = 0 }; + } + + // Set the value. + if set != 0 { + todo!("sys_randomized_path with non-null set"); + } + + Ok(SysOut::ZERO) + } + fn new_id() -> NonZeroI32 { let id = NEXT_ID.fetch_add(1, Ordering::Relaxed); diff --git a/src/kernel/src/syscalls/input.rs b/src/kernel/src/syscalls/input.rs index bf03284bc..8deaf44b1 100644 --- a/src/kernel/src/syscalls/input.rs +++ b/src/kernel/src/syscalls/input.rs @@ -19,12 +19,6 @@ pub struct SysIn<'a> { #[derive(Clone, Copy)] pub struct SysArg(usize); -impl LowerHex for SysArg { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{:#x}", self.0) - } -} - impl SysArg { pub unsafe fn to_path<'a>(self) -> Result, SysErr> { if self.0 == 0 { @@ -126,3 +120,15 @@ impl TryFrom for u8 { v.0.try_into() } } + +impl PartialEq for SysArg { + fn eq(&self, other: &usize) -> bool { + self.0 == *other + } +} + +impl LowerHex for SysArg { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + LowerHex::fmt(&self.0, f) + } +}