From d18c3160127acb36eadc18f57aa791dc5bb3390a Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 8 Jan 2024 14:43:11 -0700 Subject: [PATCH] Add session ID --- src/context/context.rs | 3 +++ src/scheme/proc.rs | 30 +++++++++++++++++++++++++++++- src/scheme/sys/context.rs | 6 ++++-- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/context/context.rs b/src/context/context.rs index e19ac753bd..b98eb68f2d 100644 --- a/src/context/context.rs +++ b/src/context/context.rs @@ -138,6 +138,8 @@ pub struct Context { pub pgid: ContextId, /// The ID of the parent context pub ppid: ContextId, + /// The ID of the session + pub session_id: ContextId, /// The real user id pub ruid: u32, /// The real group id @@ -232,6 +234,7 @@ impl Context { id, pgid: id, ppid: ContextId::from(0), + session_id: ContextId::from(0), ruid: 0, rgid: 0, rns: SchemeNamespace::from(0), diff --git a/src/scheme/proc.rs b/src/scheme/proc.rs index 0286d6bc44..096154d72f 100644 --- a/src/scheme/proc.rs +++ b/src/scheme/proc.rs @@ -110,6 +110,7 @@ enum Operation { Trace, Static(&'static str), Name, + SessionId, Sigstack, Attr(Attr), Filetable { filetable: Arc>>> }, @@ -148,7 +149,7 @@ enum Attr { } impl Operation { fn needs_child_process(&self) -> bool { - matches!(self, Self::Regs(_) | Self::Trace | Self::Filetable { .. } | Self::AddrSpace { .. } | Self::CurrentAddrSpace | Self::CurrentFiletable | Self::Sigactions(_) | Self::CurrentSigactions | Self::AwaitingSigactionsChange(_)) + matches!(self, Self::Regs(_) | Self::Trace | Self::SessionId | Self::Filetable { .. } | Self::AddrSpace { .. } | Self::CurrentAddrSpace | Self::CurrentFiletable | Self::Sigactions(_) | Self::CurrentSigactions | Self::AwaitingSigactionsChange(_)) } fn needs_root(&self) -> bool { matches!(self, Self::Attr(_)) @@ -258,6 +259,7 @@ impl ProcScheme { Some("trace") => Operation::Trace, Some("exe") => Operation::Static("exe"), Some("name") => Operation::Name, + Some("session_id") => Operation::SessionId, Some("sigstack") => Operation::Sigstack, Some("uid") => Operation::Attr(Attr::Uid), Some("gid") => Operation::Attr(Attr::Gid), @@ -771,6 +773,7 @@ impl KernelScheme for ProcScheme { Ok(grants_read * mem::size_of::()) } Operation::Name => read_from(buf, context::contexts().get(info.pid).ok_or(Error::new(ESRCH))?.read().name.as_bytes(), &mut 0), + Operation::SessionId => read_from(buf, &context::contexts().get(info.pid).ok_or(Error::new(ESRCH))?.read().session_id.get().to_ne_bytes(), &mut 0), Operation::Sigstack => read_from(buf, &context::contexts().get(info.pid).ok_or(Error::new(ESRCH))?.read().sigstack.unwrap_or(!0).to_ne_bytes(), &mut 0), Operation::Attr(attr) => { let src_buf = match (attr, &*Arc::clone(context::contexts().get(info.pid).ok_or(Error::new(ESRCH))?).read()) { @@ -947,6 +950,30 @@ impl KernelScheme for ProcScheme { context::contexts().get(info.pid).ok_or(Error::new(ESRCH))?.write().name = utf8.into(); Ok(buf.len()) } + Operation::SessionId => { + let session_id = ContextId::new(buf.read_usize()?); + + if session_id != info.pid { + // Session ID can only be set to this process's ID + return Err(Error::new(EPERM)); + } + + for (_id, context_lock) in context::contexts().iter() { + if session_id == context_lock.read().pgid { + // The session ID cannot match the PGID of any process + return Err(Error::new(EPERM)); + } + } + + let context_lock = Arc::clone(context::contexts().get(info.pid).ok_or(Error::new(ESRCH))?); + { + let mut context = context_lock.write(); + context.pgid = session_id; + context.session_id = session_id; + } + + Ok(buf.len()) + } Operation::Sigstack => { let sigstack = buf.read_usize()?; context::contexts().get(info.pid).ok_or(Error::new(ESRCH))?.write().sigstack = (sigstack != !0).then(|| sigstack); @@ -1204,6 +1231,7 @@ fn inherit_context() -> Result { new_context.rns = current_context.rns; new_context.ppid = current_context.id; new_context.pgid = current_context.pgid; + new_context.session_id = current_context.session_id; new_context.umask = current_context.umask; // TODO: Force userspace to copy sigmask. Start with "all signals blocked". diff --git a/src/scheme/sys/context.rs b/src/scheme/sys/context.rs index 533470fc31..d75d42c1d9 100644 --- a/src/scheme/sys/context.rs +++ b/src/scheme/sys/context.rs @@ -6,10 +6,11 @@ use crate::paging::PAGE_SIZE; use crate::syscall::error::Result; pub fn resource() -> Result> { - let mut string = format!("{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<12}{:<8}{}\n", + let mut string = format!("{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<12}{:<8}{}\n", "PID", "PGID", "PPID", + "SID", "RUID", "RGID", "RNS", @@ -99,10 +100,11 @@ pub fn resource() -> Result> { format!("{} B", memory) }; - string.push_str(&format!("{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<12}{:<8}{}\n", + string.push_str(&format!("{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<12}{:<8}{}\n", context.id.get(), context.pgid.get(), context.ppid.get(), + context.session_id.get(), context.ruid, context.rgid, context.rns.get(),