Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Amr Bashir <[email protected]>
  • Loading branch information
Legend-Master and amrbashir authored Jun 5, 2024
1 parent 19fd725 commit 08f20df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .changes/run-as-user.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
"nsis_process": "minor"
"nsis_tauri_utils: "minor"
---

Add a new function `RunAsUser` to run command as unelevated user
Add `RunAsUser` to run command as unelevated user
13 changes: 10 additions & 3 deletions crates/nsis-process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ unsafe fn belongs_to_user(user_sid: *mut c_void, pid: u32) -> bool {
fn kill(pid: u32) -> bool {
unsafe {
let handle = OwnedHandle::new(OpenProcess(PROCESS_TERMINATE, 0, pid));
let success = TerminateProcess(*handle, 1);
success != FALSE
TerminateProcess(*handle, 1) == TRUE
}
}

Expand All @@ -161,6 +160,7 @@ unsafe fn get_sid(pid: u32) -> Option<*mut c_void> {
if handle.is_invalid() {
return None;
}

let mut token_handle = OwnedHandle::new(HANDLE::default());
if OpenProcessToken(*handle, TOKEN_QUERY, &mut *token_handle) == FALSE {
return None;
Expand Down Expand Up @@ -229,25 +229,30 @@ unsafe fn run_as_user(command: &str, arguments: &str) -> bool {
if hwnd.is_invalid() {
return false;
}

let mut proccess_id = 0;
if GetWindowThreadProcessId(*hwnd, &mut proccess_id) == 0 {
return false;
}

let process = OwnedHandle::new(OpenProcess(PROCESS_CREATE_PROCESS, FALSE, proccess_id));
if process.is_invalid() {
return false;
}

let mut size = 0;
if !(InitializeProcThreadAttributeList(ptr::null_mut(), 1, 0, &mut size) == FALSE
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
return false;
}

let mut buffer = vec![0u8; size];
let attribute_list = buffer.as_mut_ptr() as LPPROC_THREAD_ATTRIBUTE_LIST;
if InitializeProcThreadAttributeList(attribute_list, 1, 0, &mut size) == FALSE {
return false;
}

if UpdateProcThreadAttribute(
attribute_list,
0,
Expand All @@ -260,19 +265,21 @@ unsafe fn run_as_user(command: &str, arguments: &str) -> bool {
{
return false;
}

let startup_info = STARTUPINFOEXW {
StartupInfo: STARTUPINFOW {
cb: mem::size_of::<STARTUPINFOEXW>() as _,
..mem::zeroed()
},
lpAttributeList: attribute_list,
};
let mut process_info = PROCESS_INFORMATION { ..mem::zeroed() };
let mut process_info: PROCESS_INFORMATION = mem::zeroed();
let mut command_line = command.to_owned();
if !arguments.is_empty() {
command_line.push(' ');
command_line.push_str(arguments);
}

if CreateProcessW(
encode_utf16(command).as_ptr(),
encode_utf16(&command_line).as_mut_ptr(),
Expand Down

0 comments on commit 08f20df

Please sign in to comment.