From 2478f0bbb0a8dcb07f0d35b9a6229928859e9888 Mon Sep 17 00:00:00 2001 From: svartalf Date: Sun, 1 Mar 2020 00:53:45 +0300 Subject: [PATCH 1/7] Boilerplate code for Process::user method --- heim-host/src/os/windows.rs | 12 ++++++++++- heim-host/src/sys/windows/users.rs | 4 ++++ heim-process/Cargo.toml | 3 +-- heim-process/src/process/mod.rs | 6 ++++++ heim-process/src/sys/linux/process/mod.rs | 9 +++++++++ heim-process/src/sys/macos/process/mod.rs | 7 +++++++ heim-process/src/sys/windows/bindings/mod.rs | 1 + .../src/sys/windows/bindings/token.rs | 20 +++++++++++++++++++ heim-process/src/sys/windows/process/mod.rs | 8 ++++++++ 9 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 heim-process/src/sys/windows/bindings/token.rs diff --git a/heim-host/src/os/windows.rs b/heim-host/src/os/windows.rs index 7486c533..81cd7ab2 100644 --- a/heim-host/src/os/windows.rs +++ b/heim-host/src/os/windows.rs @@ -2,10 +2,16 @@ use std::net::IpAddr; +use heim_common::Result; + /// Extension for [User] struct. /// /// [User]: ../../struct.User.html -pub trait UserExt { +pub trait UserExt: Sized { + // TODO: Proper type instead of `i32` + #[doc(hidden)] + fn try_from_sid(sid: i32) -> Result; + /// Domain name that the user belongs to. fn domain(&self) -> &str; @@ -26,6 +32,10 @@ pub trait UserExt { #[cfg(target_os = "windows")] impl UserExt for crate::User { + fn try_from_sid(sid: i32) -> Result { + crate::sys::User::try_from_sid(sid).map(crate::User::from) + } + fn domain(&self) -> &str { self.as_ref().domain() } diff --git a/heim-host/src/sys/windows/users.rs b/heim-host/src/sys/windows/users.rs index 52677658..afd67109 100644 --- a/heim-host/src/sys/windows/users.rs +++ b/heim-host/src/sys/windows/users.rs @@ -27,6 +27,10 @@ impl User { })) } + pub fn try_from_sid(_sid: i32) -> Result { + unimplemented!() + } + pub fn domain(&self) -> &str { self.domain.as_str() } diff --git a/heim-process/Cargo.toml b/heim-process/Cargo.toml index e28959cb..412fc325 100644 --- a/heim-process/Cargo.toml +++ b/heim-process/Cargo.toml @@ -18,6 +18,7 @@ github-actions = { repository = "heim-rs/heim", workflow = "Tier 1 CI" } heim-common = { version = "0.1.0-alpha.1", path = "../heim-common" } heim-runtime = { version = "0.1.0-alpha.1", path = "../heim-runtime", default-features = false } heim-cpu = { version = "0.1.0-alpha.1", path = "../heim-cpu", default-features = false } +heim-host = { version = "0.1.0-alpha.1", path = "../heim-host", default-features = false } cfg-if = "~0.1" libc = "~0.2" lazy_static = "1.3.0" @@ -27,10 +28,8 @@ async-trait = "~0.1" [target.'cfg(target_os = "linux")'.dependencies] heim-net = { version = "0.1.0-alpha.1", path = "../heim-net", default-features = false } -heim-host = { version = "0.1.0-alpha.1", path = "../heim-host", default-features = false } [target.'cfg(target_os = "windows")'.dependencies] -heim-host = { version = "0.1.0-alpha.1", path = "../heim-host", default-features = false } ntapi = "0.3.3" [target.'cfg(target_os = "windows")'.dependencies.winapi] diff --git a/heim-process/src/process/mod.rs b/heim-process/src/process/mod.rs index 8fd267e5..155542c2 100644 --- a/heim-process/src/process/mod.rs +++ b/heim-process/src/process/mod.rs @@ -4,6 +4,7 @@ use std::time::Instant; use heim_common::prelude::*; use heim_common::units::Time; +use heim_host::User; use crate::{sys, Pid, ProcessResult}; @@ -165,6 +166,11 @@ impl Process { self.as_ref().memory().await.map(Into::into) } + /// Returns user who owns this process. + pub async fn user(&self) -> ProcessResult { + self.as_ref().user().await.map(Into::into) + } + /// Checks if this `Process` is still running. pub async fn is_running(&self) -> ProcessResult { self.as_ref().is_running().await diff --git a/heim-process/src/sys/linux/process/mod.rs b/heim-process/src/sys/linux/process/mod.rs index cbeeddc1..7c96a828 100644 --- a/heim-process/src/sys/linux/process/mod.rs +++ b/heim-process/src/sys/linux/process/mod.rs @@ -5,6 +5,7 @@ use std::path::{Path, PathBuf}; use heim_common::prelude::*; use heim_common::units::Time; +use heim_host::User; use heim_runtime as rt; use super::{pid_exists, pids}; @@ -122,6 +123,14 @@ impl Process { procfs::stat_memory(self.pid).await } + pub async fn user(&self) -> ProcessResult { + // TODO: implement + // 1. Read `/proc/{pid}/stat` or smth else + // 2. parse uid and gid + // 3. Construct `heim_host::User` from them + unimplemented!("https://github.com/heim-rs/heim/issues/194") + } + pub async fn is_running(&self) -> ProcessResult { let other = get(self.pid).await?; diff --git a/heim-process/src/sys/macos/process/mod.rs b/heim-process/src/sys/macos/process/mod.rs index 7f8ffdef..bcfc315c 100644 --- a/heim-process/src/sys/macos/process/mod.rs +++ b/heim-process/src/sys/macos/process/mod.rs @@ -10,6 +10,7 @@ use futures::future::BoxFuture; use heim_common::prelude::*; use heim_common::sys::IntoTime; use heim_common::units::Time; +use heim_host::User; use super::{bindings, pids, utils::catch_zombie}; use crate::os::unix::Signal; @@ -114,6 +115,12 @@ impl Process { } } + pub async fn user(&self) -> ProcessResult { + // Fetch user infomation with `darwin_libproc` help, + // construct user from this information somehow. + unimplemented!("https://github.com/heim-rs/heim/issues/194") + } + pub async fn is_running(&self) -> ProcessResult { let other = get(self.pid).await?; diff --git a/heim-process/src/sys/windows/bindings/mod.rs b/heim-process/src/sys/windows/bindings/mod.rs index cfdbfde7..2de8f566 100644 --- a/heim-process/src/sys/windows/bindings/mod.rs +++ b/heim-process/src/sys/windows/bindings/mod.rs @@ -8,6 +8,7 @@ use heim_common::{Error, Result}; pub mod handle; pub mod processes; pub mod snapshot; +pub mod token; pub use self::handle::ProcessHandle; diff --git a/heim-process/src/sys/windows/bindings/token.rs b/heim-process/src/sys/windows/bindings/token.rs new file mode 100644 index 00000000..c4b80389 --- /dev/null +++ b/heim-process/src/sys/windows/bindings/token.rs @@ -0,0 +1,20 @@ +use heim_common::sys::windows::Handle; +use heim_common::Result; +use heim_host::User; +//use heim_host::os::windows::UserExt; + +pub struct Token(Handle); + +impl Token { + pub fn open(process: &Handle) -> Result { + unimplemented!(); + } + + pub fn user(&self) -> Result { + // let sid = self.get_sid_somehow(); + // + // Really should call it `try_from_sid`: + // UserExt::try_from_sid(sid) + unimplemented!() + } +} diff --git a/heim-process/src/sys/windows/process/mod.rs b/heim-process/src/sys/windows/process/mod.rs index ecb42c02..683ba9f9 100644 --- a/heim-process/src/sys/windows/process/mod.rs +++ b/heim-process/src/sys/windows/process/mod.rs @@ -6,6 +6,7 @@ use std::path::PathBuf; use heim_common::prelude::*; use heim_common::units::Time; +use heim_host::User; use winapi::um::processthreadsapi; use super::{bindings, pid_exists, pids}; @@ -141,6 +142,13 @@ impl Process { handle.memory().map(Memory::from) } } + pub async fn user(&self) -> ProcessResult { + // TODO: implement + // Get the process handle + // Acquire access token from it + // Fetch user from this token + unimplemented!("https://github.com/heim-rs/heim/issues/194") + } pub async fn is_running(&self) -> ProcessResult { let other = get(self.pid).await?; From 495ea3487f82e029f8f615f5b4725575d0226fbc Mon Sep 17 00:00:00 2001 From: Sandorex Date: Sun, 1 Mar 2020 19:08:26 +0100 Subject: [PATCH 2/7] Working code for windows --- examples/process.rs | 1 + heim-host/src/os/windows.rs | 6 +- heim-host/src/sys/windows/users.rs | 42 +++++++++++- heim-process/Cargo.toml | 3 +- .../windows/bindings/handle/limited_info.rs | 10 +++ .../src/sys/windows/bindings/token.rs | 64 ++++++++++++++++--- heim-process/src/sys/windows/process/mod.rs | 13 ++-- heim-process/tests/smoke.rs | 1 + 8 files changed, 120 insertions(+), 20 deletions(-) diff --git a/examples/process.rs b/examples/process.rs index 72057225..38046ca0 100644 --- a/examples/process.rs +++ b/examples/process.rs @@ -43,6 +43,7 @@ async fn flip_the_table(p: process::Process) -> process::ProcessResult Result; + fn try_from_sid(sid: PSID) -> Result; /// Domain name that the user belongs to. fn domain(&self) -> &str; @@ -32,7 +32,7 @@ pub trait UserExt: Sized { #[cfg(target_os = "windows")] impl UserExt for crate::User { - fn try_from_sid(sid: i32) -> Result { + fn try_from_sid(sid: PSID) -> Result { crate::sys::User::try_from_sid(sid).map(crate::User::from) } diff --git a/heim-host/src/sys/windows/users.rs b/heim-host/src/sys/windows/users.rs index afd67109..a8d276f4 100644 --- a/heim-host/src/sys/windows/users.rs +++ b/heim-host/src/sys/windows/users.rs @@ -1,4 +1,8 @@ use std::net::IpAddr; +use std::ptr; +use winapi::shared::minwindef::DWORD; +use winapi::um::winbase::LookupAccountSidW; +use winapi::um::winnt::{PSID, PSID_NAME_USE, SID_NAME_USE, WCHAR}; use super::wrappers::{Session, Sessions}; use heim_common::prelude::*; @@ -27,8 +31,42 @@ impl User { })) } - pub fn try_from_sid(_sid: i32) -> Result { - unimplemented!() + pub fn try_from_sid(sid: PSID) -> Result { + // name and domain cannot be longer than 256 + let mut name_cch: DWORD = 256; + let mut name: Vec = Vec::with_capacity(name_cch as usize); + let mut domain_cch: DWORD = 256; + let mut domain: Vec = Vec::with_capacity(domain_cch as usize); + + // winapi does not have SID_NAME_USE enum + let mut account_type: SID_NAME_USE = 0; + + let result = unsafe { + LookupAccountSidW( + ptr::null(), + sid, + name.as_mut_ptr(), + &mut name_cch, + domain.as_mut_ptr(), + &mut domain_cch, + &mut account_type as PSID_NAME_USE, + ) + }; + + if result == 0 { + return Err(Error::last_os_error()); + } + + unsafe { + name.set_len(name_cch as usize); + domain.set_len(domain_cch as usize); + } + + Ok(Self { + domain: String::from_utf16(domain.as_slice()).unwrap(), + username: String::from_utf16(name.as_slice()).unwrap(), + address: None, + }) } pub fn domain(&self) -> &str { diff --git a/heim-process/Cargo.toml b/heim-process/Cargo.toml index 412fc325..4bb5e670 100644 --- a/heim-process/Cargo.toml +++ b/heim-process/Cargo.toml @@ -44,7 +44,8 @@ features = [ "psapi", "processthreadsapi", "winerror", - "tlhelp32" + "tlhelp32", + "securitybaseapi" ] [target.'cfg(target_os = "macos")'.dependencies] diff --git a/heim-process/src/sys/windows/bindings/handle/limited_info.rs b/heim-process/src/sys/windows/bindings/handle/limited_info.rs index 98916cb2..024fff61 100644 --- a/heim-process/src/sys/windows/bindings/handle/limited_info.rs +++ b/heim-process/src/sys/windows/bindings/handle/limited_info.rs @@ -15,7 +15,9 @@ use winapi::um::{processthreadsapi, psapi, winbase, winnt}; use heim_common::sys::IntoTime; use heim_common::units::{time, Time}; use heim_common::Error; +use heim_host::User; +use super::super::token::Token; use super::{ProcessHandle, ProcessHandlePermissions}; use crate::sys::windows::process::CpuTime; use crate::{Pid, ProcessError, ProcessResult}; @@ -139,4 +141,12 @@ impl ProcessHandle { Ok((creation, exit, kernel, user)) } } + + pub fn owner(&self) -> ProcessResult { + // TODO clean this up? + match Token::open(&self.handle)?.user() { + Ok(x) => Ok(x), + Err(e) => Err(e.into()), + } + } } diff --git a/heim-process/src/sys/windows/bindings/token.rs b/heim-process/src/sys/windows/bindings/token.rs index c4b80389..4e0cb17f 100644 --- a/heim-process/src/sys/windows/bindings/token.rs +++ b/heim-process/src/sys/windows/bindings/token.rs @@ -1,20 +1,66 @@ +use std::ptr; +use winapi::shared::minwindef::{DWORD, LPVOID}; +use winapi::um::handleapi::CloseHandle; +use winapi::um::processthreadsapi::OpenProcessToken; +use winapi::um::securitybaseapi::GetTokenInformation; +use winapi::um::winnt::{TokenUser, HANDLE, TOKEN_QUERY, TOKEN_USER}; + +use heim_common::prelude::*; use heim_common::sys::windows::Handle; use heim_common::Result; +use heim_host::os::windows::UserExt; use heim_host::User; -//use heim_host::os::windows::UserExt; -pub struct Token(Handle); +pub struct Token(HANDLE); impl Token { - pub fn open(process: &Handle) -> Result { - unimplemented!(); + pub fn open(process_handle: &Handle) -> Result { + let mut token_handle: HANDLE = ptr::null_mut(); + + let result = unsafe { OpenProcessToken(**process_handle, TOKEN_QUERY, &mut token_handle) }; + + if result == 0 { + return Err(Error::last_os_error().with_ffi("OpenProcessToken")); + } + + Ok(Self(token_handle)) } pub fn user(&self) -> Result { - // let sid = self.get_sid_somehow(); - // - // Really should call it `try_from_sid`: - // UserExt::try_from_sid(sid) - unimplemented!() + // data should always be 44 bytes + let mut data: Vec = Vec::with_capacity(64); + let mut length: DWORD = 0; + + let result = unsafe { + GetTokenInformation( + self.0, + TokenUser, + data.as_mut_ptr() as LPVOID, + data.capacity() as DWORD, + &mut length, + ) + }; + + if result == 0 { + return Err(Error::last_os_error().with_ffi("GetTokenInformation")); + } + + unsafe { data.set_len(length as usize) }; + + let token_user = unsafe { ptr::read(data.as_ptr() as *const TOKEN_USER) }; + + User::try_from_sid(token_user.User.Sid) + } +} + +impl Drop for Token { + fn drop(&mut self) { + let result = unsafe { CloseHandle(self.0) }; + + assert!( + result != 0, + "{:?}", + Error::last_os_error().with_ffi("CloseHandle") + ); } } diff --git a/heim-process/src/sys/windows/process/mod.rs b/heim-process/src/sys/windows/process/mod.rs index 683ba9f9..0f211bf6 100644 --- a/heim-process/src/sys/windows/process/mod.rs +++ b/heim-process/src/sys/windows/process/mod.rs @@ -142,12 +142,15 @@ impl Process { handle.memory().map(Memory::from) } } + pub async fn user(&self) -> ProcessResult { - // TODO: implement - // Get the process handle - // Acquire access token from it - // Fetch user from this token - unimplemented!("https://github.com/heim-rs/heim/issues/194") + if self.pid == 0 || self.pid == 4 { + Err(ProcessError::AccessDenied(self.pid)) + } else { + let handle = bindings::ProcessHandle::query_limited_info(self.pid)?; + + handle.owner() + } } pub async fn is_running(&self) -> ProcessResult { diff --git a/heim-process/tests/smoke.rs b/heim-process/tests/smoke.rs index a55e1976..445bac9b 100644 --- a/heim-process/tests/smoke.rs +++ b/heim-process/tests/smoke.rs @@ -54,6 +54,7 @@ async fn smoke_processes() { let _ = process.pid(); try_method!(process.parent_pid()); try_method!(process.name()); + try_method!(process.user()); try_method!(process.command()); try_method!(process.exe()); #[cfg(not(target_os = "windows"))] // Not implemented yet From 97027c8e9e7a57a61929c327e59f4eb849c1d2ec Mon Sep 17 00:00:00 2001 From: Sandorex Date: Mon, 2 Mar 2020 19:27:44 +0100 Subject: [PATCH 3/7] Applying changes --- examples/process.rs | 1 + heim-common/src/errors.rs | 6 ++++++ heim-host/src/sys/windows/users.rs | 14 ++++++-------- .../windows/bindings/handle/limited_info.rs | 6 +----- .../src/sys/windows/bindings/token.rs | 19 +++---------------- 5 files changed, 17 insertions(+), 29 deletions(-) diff --git a/examples/process.rs b/examples/process.rs index 2b401f7f..b748be85 100644 --- a/examples/process.rs +++ b/examples/process.rs @@ -43,6 +43,7 @@ async fn flip_the_table(p: process::Process) -> process::ProcessResult for Error { } } +impl From for Error { + fn from(e: std::string::FromUtf16Error) -> Self { + Error::from(io::Error::new(io::ErrorKind::InvalidData, e)) + } +} + #[cfg(unix)] impl From for Error { fn from(e: nix::Error) -> Self { diff --git a/heim-host/src/sys/windows/users.rs b/heim-host/src/sys/windows/users.rs index a8d276f4..786817b3 100644 --- a/heim-host/src/sys/windows/users.rs +++ b/heim-host/src/sys/windows/users.rs @@ -2,7 +2,7 @@ use std::net::IpAddr; use std::ptr; use winapi::shared::minwindef::DWORD; use winapi::um::winbase::LookupAccountSidW; -use winapi::um::winnt::{PSID, PSID_NAME_USE, SID_NAME_USE, WCHAR}; +use winapi::um::winnt::{SidTypeUser, PSID, SID_NAME_USE, WCHAR}; use super::wrappers::{Session, Sessions}; use heim_common::prelude::*; @@ -37,8 +37,6 @@ impl User { let mut name: Vec = Vec::with_capacity(name_cch as usize); let mut domain_cch: DWORD = 256; let mut domain: Vec = Vec::with_capacity(domain_cch as usize); - - // winapi does not have SID_NAME_USE enum let mut account_type: SID_NAME_USE = 0; let result = unsafe { @@ -49,12 +47,12 @@ impl User { &mut name_cch, domain.as_mut_ptr(), &mut domain_cch, - &mut account_type as PSID_NAME_USE, + &mut account_type, ) }; - if result == 0 { - return Err(Error::last_os_error()); + if result == 0 || account_type != SidTypeUser { + return Err(Error::last_os_error().with_ffi("LookupAccountSidW")); } unsafe { @@ -63,8 +61,8 @@ impl User { } Ok(Self { - domain: String::from_utf16(domain.as_slice()).unwrap(), - username: String::from_utf16(name.as_slice()).unwrap(), + domain: String::from_utf16(domain.as_slice())?, + username: String::from_utf16(name.as_slice())?, address: None, }) } diff --git a/heim-process/src/sys/windows/bindings/handle/limited_info.rs b/heim-process/src/sys/windows/bindings/handle/limited_info.rs index 024fff61..0c7af9e2 100644 --- a/heim-process/src/sys/windows/bindings/handle/limited_info.rs +++ b/heim-process/src/sys/windows/bindings/handle/limited_info.rs @@ -143,10 +143,6 @@ impl ProcessHandle { } pub fn owner(&self) -> ProcessResult { - // TODO clean this up? - match Token::open(&self.handle)?.user() { - Ok(x) => Ok(x), - Err(e) => Err(e.into()), - } + Token::open(&self.handle)?.user().map_err(Into::into) } } diff --git a/heim-process/src/sys/windows/bindings/token.rs b/heim-process/src/sys/windows/bindings/token.rs index 4e0cb17f..cdf71edd 100644 --- a/heim-process/src/sys/windows/bindings/token.rs +++ b/heim-process/src/sys/windows/bindings/token.rs @@ -1,6 +1,5 @@ use std::ptr; use winapi::shared::minwindef::{DWORD, LPVOID}; -use winapi::um::handleapi::CloseHandle; use winapi::um::processthreadsapi::OpenProcessToken; use winapi::um::securitybaseapi::GetTokenInformation; use winapi::um::winnt::{TokenUser, HANDLE, TOKEN_QUERY, TOKEN_USER}; @@ -11,7 +10,7 @@ use heim_common::Result; use heim_host::os::windows::UserExt; use heim_host::User; -pub struct Token(HANDLE); +pub struct Token(Handle); impl Token { pub fn open(process_handle: &Handle) -> Result { @@ -23,7 +22,7 @@ impl Token { return Err(Error::last_os_error().with_ffi("OpenProcessToken")); } - Ok(Self(token_handle)) + Ok(Self(Handle::new(token_handle))) } pub fn user(&self) -> Result { @@ -33,7 +32,7 @@ impl Token { let result = unsafe { GetTokenInformation( - self.0, + *self.0, TokenUser, data.as_mut_ptr() as LPVOID, data.capacity() as DWORD, @@ -52,15 +51,3 @@ impl Token { User::try_from_sid(token_user.User.Sid) } } - -impl Drop for Token { - fn drop(&mut self) { - let result = unsafe { CloseHandle(self.0) }; - - assert!( - result != 0, - "{:?}", - Error::last_os_error().with_ffi("CloseHandle") - ); - } -} From d6979fa4750f9e9eca6a21edfb9f38a6939fab1d Mon Sep 17 00:00:00 2001 From: Sandorex Date: Mon, 2 Mar 2020 19:34:57 +0100 Subject: [PATCH 4/7] Removing raw pointer PSID --- heim-host/src/os/windows.rs | 6 +++--- heim-host/src/sys/windows/users.rs | 6 +++--- heim-process/src/sys/windows/bindings/token.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/heim-host/src/os/windows.rs b/heim-host/src/os/windows.rs index 499284a5..132635c6 100644 --- a/heim-host/src/os/windows.rs +++ b/heim-host/src/os/windows.rs @@ -1,7 +1,7 @@ //! Windows-specific extensions. use std::net::IpAddr; -use winapi::um::winnt::PSID; +use winapi::um::winnt::SID_AND_ATTRIBUTES; use heim_common::Result; @@ -10,7 +10,7 @@ use heim_common::Result; /// [User]: ../../struct.User.html pub trait UserExt: Sized { #[doc(hidden)] - fn try_from_sid(sid: PSID) -> Result; + fn try_from_sid(sid: &SID_AND_ATTRIBUTES) -> Result; /// Domain name that the user belongs to. fn domain(&self) -> &str; @@ -32,7 +32,7 @@ pub trait UserExt: Sized { #[cfg(target_os = "windows")] impl UserExt for crate::User { - fn try_from_sid(sid: PSID) -> Result { + fn try_from_sid(sid: &SID_AND_ATTRIBUTES) -> Result { crate::sys::User::try_from_sid(sid).map(crate::User::from) } diff --git a/heim-host/src/sys/windows/users.rs b/heim-host/src/sys/windows/users.rs index 786817b3..491349cd 100644 --- a/heim-host/src/sys/windows/users.rs +++ b/heim-host/src/sys/windows/users.rs @@ -2,7 +2,7 @@ use std::net::IpAddr; use std::ptr; use winapi::shared::minwindef::DWORD; use winapi::um::winbase::LookupAccountSidW; -use winapi::um::winnt::{SidTypeUser, PSID, SID_NAME_USE, WCHAR}; +use winapi::um::winnt::{SidTypeUser, SID_AND_ATTRIBUTES, SID_NAME_USE, WCHAR}; use super::wrappers::{Session, Sessions}; use heim_common::prelude::*; @@ -31,7 +31,7 @@ impl User { })) } - pub fn try_from_sid(sid: PSID) -> Result { + pub fn try_from_sid(sid: &SID_AND_ATTRIBUTES) -> Result { // name and domain cannot be longer than 256 let mut name_cch: DWORD = 256; let mut name: Vec = Vec::with_capacity(name_cch as usize); @@ -42,7 +42,7 @@ impl User { let result = unsafe { LookupAccountSidW( ptr::null(), - sid, + sid.Sid, name.as_mut_ptr(), &mut name_cch, domain.as_mut_ptr(), diff --git a/heim-process/src/sys/windows/bindings/token.rs b/heim-process/src/sys/windows/bindings/token.rs index cdf71edd..a826dfcd 100644 --- a/heim-process/src/sys/windows/bindings/token.rs +++ b/heim-process/src/sys/windows/bindings/token.rs @@ -48,6 +48,6 @@ impl Token { let token_user = unsafe { ptr::read(data.as_ptr() as *const TOKEN_USER) }; - User::try_from_sid(token_user.User.Sid) + User::try_from_sid(&token_user.User) } } From a7589063f38611cf595950e8a695177851db2fba Mon Sep 17 00:00:00 2001 From: Sandorex Date: Mon, 2 Mar 2020 19:42:28 +0100 Subject: [PATCH 5/7] Reverting last commit --- heim-host/src/os/windows.rs | 6 +++--- heim-host/src/sys/windows/users.rs | 6 +++--- heim-process/src/sys/windows/bindings/token.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/heim-host/src/os/windows.rs b/heim-host/src/os/windows.rs index 132635c6..499284a5 100644 --- a/heim-host/src/os/windows.rs +++ b/heim-host/src/os/windows.rs @@ -1,7 +1,7 @@ //! Windows-specific extensions. use std::net::IpAddr; -use winapi::um::winnt::SID_AND_ATTRIBUTES; +use winapi::um::winnt::PSID; use heim_common::Result; @@ -10,7 +10,7 @@ use heim_common::Result; /// [User]: ../../struct.User.html pub trait UserExt: Sized { #[doc(hidden)] - fn try_from_sid(sid: &SID_AND_ATTRIBUTES) -> Result; + fn try_from_sid(sid: PSID) -> Result; /// Domain name that the user belongs to. fn domain(&self) -> &str; @@ -32,7 +32,7 @@ pub trait UserExt: Sized { #[cfg(target_os = "windows")] impl UserExt for crate::User { - fn try_from_sid(sid: &SID_AND_ATTRIBUTES) -> Result { + fn try_from_sid(sid: PSID) -> Result { crate::sys::User::try_from_sid(sid).map(crate::User::from) } diff --git a/heim-host/src/sys/windows/users.rs b/heim-host/src/sys/windows/users.rs index 491349cd..786817b3 100644 --- a/heim-host/src/sys/windows/users.rs +++ b/heim-host/src/sys/windows/users.rs @@ -2,7 +2,7 @@ use std::net::IpAddr; use std::ptr; use winapi::shared::minwindef::DWORD; use winapi::um::winbase::LookupAccountSidW; -use winapi::um::winnt::{SidTypeUser, SID_AND_ATTRIBUTES, SID_NAME_USE, WCHAR}; +use winapi::um::winnt::{SidTypeUser, PSID, SID_NAME_USE, WCHAR}; use super::wrappers::{Session, Sessions}; use heim_common::prelude::*; @@ -31,7 +31,7 @@ impl User { })) } - pub fn try_from_sid(sid: &SID_AND_ATTRIBUTES) -> Result { + pub fn try_from_sid(sid: PSID) -> Result { // name and domain cannot be longer than 256 let mut name_cch: DWORD = 256; let mut name: Vec = Vec::with_capacity(name_cch as usize); @@ -42,7 +42,7 @@ impl User { let result = unsafe { LookupAccountSidW( ptr::null(), - sid.Sid, + sid, name.as_mut_ptr(), &mut name_cch, domain.as_mut_ptr(), diff --git a/heim-process/src/sys/windows/bindings/token.rs b/heim-process/src/sys/windows/bindings/token.rs index a826dfcd..cdf71edd 100644 --- a/heim-process/src/sys/windows/bindings/token.rs +++ b/heim-process/src/sys/windows/bindings/token.rs @@ -48,6 +48,6 @@ impl Token { let token_user = unsafe { ptr::read(data.as_ptr() as *const TOKEN_USER) }; - User::try_from_sid(&token_user.User) + User::try_from_sid(token_user.User.Sid) } } From f6a1b73f3074bf59435670f8ad81819cf2c95347 Mon Sep 17 00:00:00 2001 From: Sandorex Date: Mon, 2 Mar 2020 19:42:45 +0100 Subject: [PATCH 6/7] Fixing process example --- examples/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/process.rs b/examples/process.rs index b748be85..21412ecd 100644 --- a/examples/process.rs +++ b/examples/process.rs @@ -43,7 +43,7 @@ async fn flip_the_table(p: process::Process) -> process::ProcessResult Date: Mon, 2 Mar 2020 19:54:41 +0100 Subject: [PATCH 7/7] Switched to MaybeUninit instead of a Vec --- heim-process/src/sys/windows/bindings/token.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/heim-process/src/sys/windows/bindings/token.rs b/heim-process/src/sys/windows/bindings/token.rs index cdf71edd..704ab497 100644 --- a/heim-process/src/sys/windows/bindings/token.rs +++ b/heim-process/src/sys/windows/bindings/token.rs @@ -1,3 +1,4 @@ +use std::mem; use std::ptr; use winapi::shared::minwindef::{DWORD, LPVOID}; use winapi::um::processthreadsapi::OpenProcessToken; @@ -26,8 +27,7 @@ impl Token { } pub fn user(&self) -> Result { - // data should always be 44 bytes - let mut data: Vec = Vec::with_capacity(64); + let mut data = mem::MaybeUninit::::uninit(); let mut length: DWORD = 0; let result = unsafe { @@ -35,7 +35,8 @@ impl Token { *self.0, TokenUser, data.as_mut_ptr() as LPVOID, - data.capacity() as DWORD, + // data should always be 44 bytes + 44, &mut length, ) }; @@ -44,9 +45,7 @@ impl Token { return Err(Error::last_os_error().with_ffi("GetTokenInformation")); } - unsafe { data.set_len(length as usize) }; - - let token_user = unsafe { ptr::read(data.as_ptr() as *const TOKEN_USER) }; + let token_user = unsafe { data.assume_init() }; User::try_from_sid(token_user.User.Sid) }