Compare commits

..
Author SHA1 Message Date
rustdeskandClaude Opus 5.5 86e56334bd fix(linux): let gtk sudo retry after sudo-rs rejects a password
sudo-rs reports a rejected password with "sudo: Authentication failed,
try again." and then asks again. The line starts with "sudo:", so
ui_parent treated it as fatal: it showed it in an error dialog and
exited instead of offering a retry, as it does after classic sudo's
"Sorry, try again." (the second log in #16647).

Exempt exactly that line from the fatal check. It then goes through
the ordinary non-prompt branch, so the held-password drop rule still
applies. sudo-rs's final "maximum N incorrect authentication attempts"
stays fatal, and so does every other sudo:/su: line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 18:14:10 +08:00
rustdeskandClaude Opus 5.5 9c8da0e6ea fix(linux): no false "Sorry, try again." in gtk sudo after a PAM info line
The gtk sudo dialog takes the password before sudo starts and drops it
when sudo prints a non-prompt line first, such as pam_fprintd's "Place
your finger on the fingerprint reader". The real password prompt then
came up labelled as a retry, "Sorry, try again.", although no password
had been sent, so users took it as a rejected password.

Treat the next prompt as a first prompt when the typed password is
dropped. The dialog comes back prefilled and without the error.

Keeping the password and sending it to the next prompt instead is not
safe: when sudo authenticates without a prompt (fingerprint, NOPASSWD),
use_pty, the default since sudo 1.9.14, holds the tty in raw mode while
the command runs, so any output line ending in ':' would receive it.

Fixes #16647

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 18:14:09 +08:00
17 changed files with 364 additions and 1151 deletions
+1 -6
View File
@@ -2,12 +2,7 @@
fn build_windows() {
let file = "src/platform/windows.cc";
let file2 = "src/platform/windows_delete_test_cert.cc";
let mut build = cc::Build::new();
if !cfg!(feature = "flutter") {
build.cpp(true).std("c++17").define("RUSTDESK_SCITER", None);
println!("cargo:rerun-if-changed=flutter/windows/runner/service_control.h");
}
build.file(file).file(file2).compile("windows");
cc::Build::new().file(file).file(file2).compile("windows");
println!("cargo:rustc-link-lib=WtsApi32");
println!("cargo:rerun-if-changed={}", file);
println!("cargo:rerun-if-changed={}", file2);
@@ -50,15 +50,6 @@ const val WHEEL_BUTTON_UP = 34
const val WHEEL_DOWN = 523331
const val WHEEL_UP = 963
// src/common.rs MOUSE_TYPE_WHEEL: the controller sends a wheel tick as a
// MouseEvent with these type bits and the scroll delta in y (y < 0 = down).
const val MOUSE_TYPE_WHEEL = 3
// Wheel-tick travel for MOUSE_TYPE_WHEEL events. Three quarters of the legacy
// WHEEL_STEP; tuned on real devices during review (the full 120px felt too
// far per tick).
const val MOUSE_WHEEL_STEP = 90
const val TOUCH_SCALE_START = 1
const val TOUCH_SCALE = 2
const val TOUCH_SCALE_END = 3
@@ -111,12 +102,6 @@ class InputService : AccessibilityService() {
private val volumeController: VolumeController by lazy { VolumeController(applicationContext.getSystemService(AUDIO_SERVICE) as AudioManager) }
/**
* Handles a remote mouse event on the gesture path. [mask] is the
* button/phase mask of the mouse protocol ([MOUSE_TYPE_WHEEL] ticks carry
* the scroll delta in [_y] instead of a position), x/y the screen-space
* position. Requires API 24 for the gesture APIs.
*/
@RequiresApi(Build.VERSION_CODES.N)
fun onMouseInput(mask: Int, _x: Int, _y: Int) {
val x = max(0, _x)
@@ -199,38 +184,6 @@ class InputService : AccessibilityService() {
return
}
// The controller sends a wheel tick as a MouseEvent with the type bits
// MOUSE_TYPE_WHEEL (src/common.rs) and the scroll delta in _y
// (y < 0 = wheel down); the WHEEL_DOWN/WHEEL_UP masks below are legacy
// values that current controllers never send. Gated to API 24+ like
// the rest of the gesture path - below that, legacy devices take the
// Shizuku backend or a no-op, not this class.
if (mask == MOUSE_TYPE_WHEEL && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// A horizontal tick (the controller sends y = 0 when the
// horizontal delta dominates) or a delta truncated to zero is not
// a vertical swipe.
if (_y == 0) {
return
}
if (mouseY < MOUSE_WHEEL_STEP) {
return
}
val path = Path()
path.moveTo(mouseX.toFloat(), mouseY.toFloat())
val deltaY = if (_y < 0) -MOUSE_WHEEL_STEP else MOUSE_WHEEL_STEP
path.lineTo(mouseX.toFloat(), (mouseY + deltaY).toFloat())
val stroke = GestureDescription.StrokeDescription(
path,
0,
WHEEL_DURATION
)
val builder = GestureDescription.Builder()
builder.addStroke(stroke)
wheelActionsQueue.offer(builder.build())
consumeWheelActions()
return
}
if (mask == WHEEL_DOWN) {
if (mouseY < WHEEL_STEP) {
return
-4
View File
@@ -10,7 +10,6 @@
#include "win32_desktop.h"
#include "flutter_window.h"
#include "utils.h"
#include "service_control.h"
typedef char** (*FUNC_RUSTDESK_CORE_MAIN)(int*);
typedef void (*FUNC_RUSTDESK_FREE_ARGS)( char**, int);
@@ -24,9 +23,6 @@ const wchar_t* getWindowClassName();
int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
_In_ wchar_t *command_line, _In_ int show_command)
{
if (auto result = service_control::HandleCommand(command_line)) {
return static_cast<int>(*result);
}
HINSTANCE hInstance = LoadLibraryA("librustdesk.dll");
if (!hInstance)
{
-183
View File
@@ -1,183 +0,0 @@
#ifndef RUSTDESK_RUNNER_SERVICE_CONTROL_H_
#define RUSTDESK_RUNNER_SERVICE_CONTROL_H_
#include <windows.h>
#include <shellapi.h>
#include <winsvc.h>
#include <cstdio>
#include <cwchar>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>
namespace service_control {
using ServiceHandle = std::unique_ptr<std::remove_pointer_t<SC_HANDLE>,
decltype(&CloseServiceHandle)>;
constexpr DWORD kStopTimeoutMilliseconds = 30000;
constexpr DWORD kPollIntervalMilliseconds = 100;
inline DWORD StopService(SC_HANDLE service) {
SERVICE_STATUS_PROCESS status{};
DWORD bytes_needed = 0;
if (!QueryServiceStatusEx(service, SC_STATUS_PROCESS_INFO,
reinterpret_cast<BYTE*>(&status), sizeof(status),
&bytes_needed)) {
return GetLastError();
}
if (status.dwCurrentState == SERVICE_STOPPED) {
return ERROR_SUCCESS;
}
if (status.dwCurrentState != SERVICE_STOP_PENDING) {
SERVICE_STATUS stopped{};
if (!ControlService(service, SERVICE_CONTROL_STOP, &stopped)) {
const DWORD error = GetLastError();
return error == ERROR_SERVICE_NOT_ACTIVE ? ERROR_SUCCESS : error;
}
}
// The installer may remove files next; accepting STOP alone does not mean
// the service has reached SERVICE_STOPPED.
const ULONGLONG started = GetTickCount64();
while (status.dwCurrentState != SERVICE_STOPPED) {
if (GetTickCount64() - started >= kStopTimeoutMilliseconds) {
return ERROR_TIMEOUT;
}
Sleep(kPollIntervalMilliseconds);
if (!QueryServiceStatusEx(service, SC_STATUS_PROCESS_INFO,
reinterpret_cast<BYTE*>(&status), sizeof(status),
&bytes_needed)) {
return GetLastError();
}
}
return ERROR_SUCCESS;
}
inline DWORD RemoveService(const wchar_t* name) {
ServiceHandle manager(OpenSCManagerW(nullptr, nullptr, SC_MANAGER_CONNECT),
CloseServiceHandle);
if (!manager) {
return GetLastError();
}
ServiceHandle service(
OpenServiceW(manager.get(), name,
SERVICE_STOP | SERVICE_QUERY_STATUS | DELETE),
CloseServiceHandle);
if (!service) {
const DWORD error = GetLastError();
return error == ERROR_SERVICE_DOES_NOT_EXIST ? ERROR_SUCCESS : error;
}
const DWORD stopped = StopService(service.get());
if (stopped != ERROR_SUCCESS) {
// The uninstall script must still reach its taskkill cleanup.
std::fwprintf(stderr,
L"Service did not stop cleanly: Windows error %lu\n", stopped);
}
if (DeleteService(service.get())) {
return ERROR_SUCCESS;
}
const DWORD error = GetLastError();
return error == ERROR_SERVICE_MARKED_FOR_DELETE ? ERROR_SUCCESS : error;
}
inline DWORD InstallService(const wchar_t* name, const wchar_t* executable,
bool rollback_on_failure) {
if (!*executable || wcschr(executable, L'"')) {
return ERROR_INVALID_PARAMETER;
}
const std::wstring binary_path =
L"\"" + std::wstring(executable) + L"\" --service";
const std::wstring display_name = std::wstring(name) + L" Service";
ServiceHandle manager(
OpenSCManagerW(nullptr, nullptr,
SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE),
CloseServiceHandle);
if (!manager) {
return GetLastError();
}
ServiceHandle service(
CreateServiceW(manager.get(), name, display_name.c_str(),
SERVICE_START | DELETE, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
binary_path.c_str(), nullptr,
nullptr, nullptr, nullptr, nullptr),
CloseServiceHandle);
const bool created = service != nullptr;
// --after-install can reuse a registration. Preserve its executable and
// account, as the previous create-then-start script did.
if (!service && GetLastError() == ERROR_SERVICE_EXISTS) {
service.reset(OpenServiceW(manager.get(), name, SERVICE_START));
}
if (!service) {
return GetLastError();
}
if (StartServiceW(service.get(), 0, nullptr)) {
return ERROR_SUCCESS;
}
const DWORD error = GetLastError();
// Only manual Start Service restores stop-service=Y on failure. Other
// installation paths retain the registration for a later startup attempt.
if (rollback_on_failure && created && error != ERROR_SERVICE_ALREADY_RUNNING &&
!DeleteService(service.get())) {
const DWORD rollback_error = GetLastError();
std::fwprintf(stderr,
L"Failed to roll back newly created service: Windows error %lu\n",
rollback_error);
}
return error == ERROR_SERVICE_ALREADY_RUNNING ? ERROR_SUCCESS : error;
}
inline DWORD RunCommand(int count, wchar_t** args) {
constexpr int kRemoveArgumentCount = 4;
constexpr int kInstallArgumentCount = 5;
if (count < kRemoveArgumentCount) {
return ERROR_BAD_ARGUMENTS;
}
if (!*args[3]) {
return ERROR_INVALID_PARAMETER;
}
const std::wstring_view operation(args[2]);
const bool rollback_on_failure = operation == L"create-service-with-rollback";
if ((operation == L"create-service" || rollback_on_failure) &&
count == kInstallArgumentCount) {
return InstallService(args[3], args[4], rollback_on_failure);
}
if (operation == L"remove" && count == kRemoveArgumentCount) {
return RemoveService(args[3]);
}
return ERROR_BAD_ARGUMENTS;
}
// Keep these commands in the existing runner so installation/removal can control
// services before loading librustdesk.dll or initializing the UI. A Rust-side
// handler would first require that DLL to load. The header-only implementation
// keeps main.cpp as a dispatch hook without adding a CMake source entry; it is
// compiled into the existing EXE, not a separately packaged helper.
inline std::optional<DWORD> HandleCommand(const wchar_t* command_line) {
constexpr std::wstring_view option = L"--service-control";
const std::wstring_view arguments(command_line);
if (arguments.compare(0, option.size(), option) != 0 ||
(arguments.size() > option.size() && arguments[option.size()] != L' ' &&
arguments[option.size()] != L'\t')) {
return std::nullopt;
}
int count = 0;
wchar_t** args = CommandLineToArgvW(GetCommandLineW(), &count);
if (!args) {
return GetLastError();
}
const DWORD result = RunCommand(count, args);
LocalFree(args);
if (result != ERROR_SUCCESS) {
std::fwprintf(stderr,
L"RustDesk service control failed with Windows error %lu\n",
result);
}
return result;
}
} // namespace service_control
#endif // RUSTDESK_RUNNER_SERVICE_CONTROL_H_
-1
View File
@@ -311,7 +311,6 @@ pub const KEYS_SETTINGS: &[&str] = &[
OPTION_API_SERVER,
OPTION_KEY,
OPTION_ALLOW_WEBSOCKET,
OPTION_WEBSOCKET_PORT,
OPTION_PRESET_ADDRESS_BOOK_NAME,
OPTION_PRESET_ADDRESS_BOOK_TAG,
OPTION_PRESET_ADDRESS_BOOK_ALIAS,
-4
View File
@@ -18,10 +18,6 @@ fn main() {
#[cfg(not(any(target_os = "android", target_os = "ios", feature = "flutter")))]
fn main() -> std::process::ExitCode {
#[cfg(windows)]
if let Some(exit_code) = platform::handle_service_control() {
return exit_code;
}
#[cfg(all(windows, not(feature = "inline")))]
unsafe {
winapi::um::shellscalingapi::SetProcessDpiAwareness(2);
+5 -1
View File
@@ -351,7 +351,10 @@ fn ui_parent(
let last_line = buf.lines().last().unwrap_or(&buf).trim().to_string();
log::info!("read from child: {}", buf);
if last_line.starts_with("sudo:") || last_line.starts_with("su:") {
// sudo-rs's notice for a rejected password, not an error: it asks again.
if (last_line.starts_with("sudo:") || last_line.starts_with("su:"))
&& last_line != "sudo: Authentication failed, try again."
{
if let Err(e) = tx_to_ui.send(Message::ErrorDialog(last_line)) {
log::error!("Channel error: {e:?}");
kill_child(child);
@@ -419,6 +422,7 @@ fn ui_parent(
if !last_line.is_empty() && initial_password.is_some() {
log::error!("received not empty line: {last_line}, clear initial password");
initial_password = None;
first = true;
}
}
}
-17
View File
@@ -15,23 +15,6 @@
#include <sddl.h>
#include <memory>
#ifdef RUSTDESK_SCITER
// Share the runner's service operations without loading either UI framework.
#include "../../flutter/windows/runner/service_control.h"
extern "C" DWORD run_service_control()
{
int count = 0;
wchar_t** args = CommandLineToArgvW(GetCommandLineW(), &count);
if (!args) {
return GetLastError();
}
const DWORD result = service_control::RunCommand(count, args);
LocalFree(args);
return result;
}
#endif
extern "C" uint32_t get_session_user_info(PWSTR bufin, uint32_t nin, uint32_t id);
void flog(char const *fmt, ...)
+35 -138
View File
@@ -97,7 +97,6 @@ use windows_service::{
use winreg::{enums::*, RegKey};
mod acl;
pub(crate) mod installer_files;
mod installer_handoff;
mod installer_shell;
mod verified_update;
@@ -108,10 +107,10 @@ pub use acl::{
set_path_permission_for_portable_service_shmem_file,
validate_path_for_portable_service_shmem_dir,
};
use installer_handoff::{run_cmds, InstallCommands};
use installer_handoff::run_cmds;
use installer_shell::{
escape_nested_cmd_ampersands, shortcut_bytes, staged_shortcut_commands,
staged_tray_shortcut_commands, validate_install_value,
embedded_shortcut_commands, embedded_tray_shortcut_commands, escape_nested_cmd_ampersands,
shortcut_bytes, validate_install_value,
};
pub use verified_update::{
copy_and_verify_update_file_sha256, schedule_current_verified_update_file_cleanup,
@@ -1676,16 +1675,18 @@ pub fn install_me(options: &str, path: String, silent: bool, debug: bool) -> Res
// The elevated runner expands this to `%~f0.dir`, beside its protected copy.
// Do not stage privileged shortcut artifacts in the user-writable `%TEMP%`.
let tmp_path = "%RUSTDESK_OUTPUT_DIR%".to_owned();
let (mk_shortcut_commands, mk_shortcut_file) = staged_shortcut_commands(
let mk_shortcut_commands = embedded_shortcut_commands(
shortcut_bytes(&exe, None, shortcut_icon_location.as_deref())?,
&format!("{app_name}.lnk"),
)?;
let (uninstall_shortcut_commands, uninstall_shortcut_file) = staged_shortcut_commands(
"mk_shortcut",
);
let uninstall_shortcut_commands = embedded_shortcut_commands(
shortcut_bytes(&exe, Some("--uninstall"), Some("msiexec.exe"))?,
&format!("Uninstall {app_name}.lnk"),
)?;
let (tray_shortcut_commands, tray_shortcut_file) =
staged_tray_shortcut_commands(&app_name, &exe, shortcut_icon_location.as_deref())?;
"uninstall_shortcut",
);
let tray_shortcut_commands =
embedded_tray_shortcut_commands(&app_name, &exe, shortcut_icon_location.as_deref())?;
let mut reg_value_desktop_shortcuts = "0".to_owned();
let mut reg_value_start_menu_shortcuts = "0".to_owned();
let mut reg_value_printer = "0".to_owned();
@@ -1759,17 +1760,6 @@ copy /Y \"{tmp_path}\\{app_name} Tray.lnk\" \"%PROGRAMDATA%\\Microsoft\\Windows\
"".to_owned()
};
let after_install = get_after_install(
&exe,
Some(reg_value_start_menu_shortcuts),
Some(reg_value_desktop_shortcuts),
Some(reg_value_printer),
);
let after_install = native_after_install_commands(
format!("{after_install}\r\n{install_remote_printer}"),
&exe,
true,
)?;
// Remember to check if `update_me` need to be changed if changing the `cmds`.
// No need to merge the existing dup code, because the code in these two functions are too critical.
// New code should be written in a common function.
@@ -1801,25 +1791,25 @@ copy /Y \"{tmp_path}\\Uninstall {app_name}.lnk\" \"{path}\\\"
{dels}
{import_config}
{after_install}
{install_remote_printer}
{sleep}
",
display_icon = shortcut_icon_location.as_deref().unwrap_or(exe.as_str()),
nested_exe = escape_nested_cmd_ampersands(&exe),
version = crate::VERSION.replace("-", "."),
build_date = crate::BUILD_DATE,
after_install = get_after_install(
&exe,
Some(reg_value_start_menu_shortcuts),
Some(reg_value_desktop_shortcuts),
Some(reg_value_printer)
),
sleep = if debug { "timeout 300" } else { "" },
dels = if debug { "" } else { &dels },
copy_exe = copy_exe_cmd(&src_exe, &exe, &path)?,
import_config = get_import_config(&exe),
);
run_cmds(
InstallCommands {
script: cmds,
files: vec![mk_shortcut_file, uninstall_shortcut_file, tray_shortcut_file],
},
debug,
"install",
)?;
run_cmds(cmds, debug, "install")?;
run_after_run_cmds(silent);
Ok(())
}
@@ -1827,30 +1817,29 @@ copy /Y \"{tmp_path}\\Uninstall {app_name}.lnk\" \"{path}\\\"
pub fn run_after_install() -> ResultType<()> {
let (_, _, _, exe) = get_install_info();
run_cmds(
native_after_install_commands(get_after_install(&exe, None, None, None), &exe, false)?,
get_after_install(&exe, None, None, None),
true,
"after_install",
)
}
pub fn run_before_uninstall() -> ResultType<()> {
run_cmds(get_before_uninstall(true)?, true, "before_install")
run_cmds(get_before_uninstall(true), true, "before_install")
}
fn get_before_uninstall(kill_self: bool) -> ResultType<String> {
fn get_before_uninstall(kill_self: bool) -> String {
let app_name = crate::get_app_name();
let (_, _, _, exe) = get_install_info();
let remove_service = service_control_command("remove", &exe, false)?;
let ext = app_name.to_lowercase();
let filter = if kill_self {
"".to_string()
} else {
format!(" /FI \"PID ne {}\"", get_current_pid())
};
Ok(format!(
format!(
"
chcp 65001
{remove_service}
sc stop {app_name}
sc delete {app_name}
taskkill /F /IM {broker_exe}
taskkill /F /IM {app_name}.exe{filter}
reg delete HKEY_CLASSES_ROOT\\.{ext} /f
@@ -1858,7 +1847,7 @@ fn get_before_uninstall(kill_self: bool) -> ResultType<String> {
netsh advfirewall firewall delete rule name=\"{app_name} Service\"
",
broker_exe = WIN_TOPMOST_INJECTED_PROCESS_EXE,
))
)
}
/// Constructs the uninstall command string for the application.
@@ -1905,7 +1894,7 @@ fn get_uninstall(kill_self: bool, uninstall_printer: bool) -> ResultType<String>
if exist \"%PUBLIC%\\Desktop\\{app_name}.lnk\" del /f /q \"%PUBLIC%\\Desktop\\{app_name}.lnk\"
if exist \"%PROGRAMDATA%\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\{app_name} Tray.lnk\" del /f /q \"%PROGRAMDATA%\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\{app_name} Tray.lnk\"
",
before_uninstall=get_before_uninstall(kill_self)?,
before_uninstall=get_before_uninstall(kill_self),
uninstall_amyuni_idd=get_uninstall_amyuni_idd(),
app_name = crate::get_app_name(),
))
@@ -3334,24 +3323,15 @@ pub fn try_lock_tray_single_instance() -> bool {
}
}
/// Returns `true` on failure or cancellation so the caller keeps running.
/// Successful removal exits this process instead of returning.
pub fn uninstall_service(show_new_window: bool, _: bool) -> bool {
log::info!("Uninstalling service...");
let filter = format!(" /FI \"PID ne {}\"", get_current_pid());
let (_, _, _, exe) = get_install_info();
let remove_service = match service_control_command("remove", &exe, false) {
Ok(commands) => commands,
Err(err) => {
log::error!("Failed to prepare service removal: {err}");
return true;
}
};
Config::set_option("stop-service".into(), "Y".into());
let cmds = format!(
"
chcp 65001
{remove_service}
sc stop {app_name}
sc delete {app_name}
if exist \"%PROGRAMDATA%\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\{app_name} Tray.lnk\" del /f /q \"%PROGRAMDATA%\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\{app_name} Tray.lnk\"
taskkill /F /IM {broker_exe}
taskkill /F /IM {app_name}.exe{filter}
@@ -3368,7 +3348,7 @@ pub fn uninstall_service(show_new_window: bool, _: bool) -> bool {
std::process::exit(0);
}
fn get_install_service_commands(path: &str, exe: &str) -> ResultType<InstallCommands> {
fn get_install_service_commands(path: &str, exe: &str) -> ResultType<String> {
let app_name = crate::get_app_name();
for value in [path, exe] {
validate_install_value(value)?;
@@ -3383,10 +3363,10 @@ fn get_install_service_commands(path: &str, exe: &str) -> ResultType<InstallComm
if let Some(icon) = shortcut_icon_location.as_deref() {
validate_install_value(icon)?;
}
let (tray_shortcut_commands, tray_shortcut_file) =
staged_tray_shortcut_commands(&app_name, exe, shortcut_icon_location.as_deref())?;
let tray_shortcut_commands =
embedded_tray_shortcut_commands(&app_name, exe, shortcut_icon_location.as_deref())?;
let filter = format!(" /FI \"PID ne {}\"", get_current_pid());
let script = format!(
Ok(format!(
"
chcp 65001
taskkill /F /IM {app_name}.exe{filter}
@@ -3396,12 +3376,8 @@ copy /Y \"%RUSTDESK_OUTPUT_DIR%\\{app_name} Tray.lnk\" \"%PROGRAMDATA%\\Microsof
{create_service}
",
import_config = get_import_config(exe),
create_service = service_control_command("create-service-with-rollback", exe, false)?,
);
Ok(InstallCommands {
script,
files: vec![tray_shortcut_file],
})
create_service = get_create_service(exe),
))
}
pub fn install_service() -> bool {
@@ -3952,86 +3928,7 @@ pub fn update_me_msi(msi: &str, expected_sha256: &str, quiet: bool) -> ResultTyp
verified_update::install_verified_msi(msi, expected_sha256, quiet)
}
fn native_after_install_commands(
commands: String,
exe: &str,
installed_now: bool,
) -> ResultType<String> {
let service = get_create_service(exe);
if service.is_empty() {
return Ok(commands);
}
let native_service = service_control_command("create-service", exe, installed_now)?;
if native_service == service {
return Ok(commands);
}
// Complete independent setup before service-start errors can exit the BAT.
Ok(format!(
"{}\r\n{native_service}",
commands.replacen(&service, "", 1)
))
}
#[cfg(not(feature = "flutter"))]
pub fn handle_service_control() -> Option<std::process::ExitCode> {
if std::env::args_os().nth(1).as_deref() != Some(std::ffi::OsStr::new("--service-control")) {
return None;
}
extern "C" {
fn run_service_control() -> u32;
}
let error = unsafe { run_service_control() };
Some(if error == ERROR_SUCCESS {
std::process::ExitCode::SUCCESS
} else {
eprintln!(
"RustDesk service control failed: {}",
std::io::Error::from_raw_os_error(error as i32)
);
std::process::ExitCode::FAILURE
})
}
fn service_control_command(operation: &str, exe: &str, installed_now: bool) -> ResultType<String> {
let app_name = crate::get_app_name();
let is_create = matches!(operation, "create-service" | "create-service-with-rollback");
let legacy = match operation {
"create-service" | "create-service-with-rollback" => get_create_service(exe),
"remove" => format!("sc stop {app_name}\r\nsc delete {app_name}"),
_ => bail!("Unsupported service operation: {operation}"),
};
if legacy.is_empty() || (is_create && Config::get_option("stop-service") == "Y") {
return Ok(legacy);
}
// A fresh installation has just copied this runner. Otherwise only use the
// new command when the caller is the installed executable; other versions
// may not recognize it.
if !installed_now {
let current = std::env::current_exe()?;
let current = current
.to_str()
.ok_or_else(|| anyhow!("Current executable path is not valid Unicode"))?;
if !current.eq_ignore_ascii_case(exe) {
log::debug!("Keeping legacy service commands for a different installed executable");
return Ok(legacy);
}
}
validate_install_value(exe)?;
let executable_argument = if is_create {
format!(" \"{exe}\"")
} else {
String::new()
};
Ok(format!(
"start \"\" /b /wait \"{exe}\" --service-control {operation} \"{app_name}\"{executable_argument}\r\n\
if not \"%errorlevel%\"==\"0\" exit /b %errorlevel%"
))
}
fn get_import_config(exe: &str) -> String {
// --import-config does not register a service dispatcher, so SCM can report
// a start failure even when import runs. Preserve this legacy contract for
// older installed executables.
if config::is_outgoing_only() {
return "".to_string();
}
-373
View File
@@ -1,373 +0,0 @@
use super::{installer_handoff::directory, wide_string};
use hbb_common::{anyhow::anyhow, log, ResultType};
use std::{
fs::{self, File},
io::{self, Read},
os::windows::io::{AsRawHandle, FromRawHandle},
path::{Path, PathBuf},
};
use windows::{
core::{w, Owned, PCWSTR},
Win32::{
Foundation::{DuplicateHandle, GENERIC_READ, GENERIC_WRITE, HANDLE, HLOCAL},
Security::{
Authorization::{
ConvertStringSecurityDescriptorToSecurityDescriptorW, SDDL_REVISION_1,
},
PSECURITY_DESCRIPTOR, SECURITY_ATTRIBUTES,
},
Storage::FileSystem::{
CreateFileW, FileDispositionInfo, SetFileInformationByHandle, CREATE_NEW, DELETE,
FILE_ATTRIBUTE_NORMAL, FILE_DISPOSITION_INFO, FILE_SHARE_READ,
},
System::Threading::GetCurrentProcess,
},
};
// Stage data before elevation instead of relaunching a verifier from a portable
// directory whose executable or DLLs could be replaced while UAC is pending.
pub(super) struct ProtectedInstallFile {
path: PathBuf,
cleanup_handle: Option<Owned<HANDLE>>,
}
impl ProtectedInstallFile {
pub(super) fn new(path: &Path, bytes: &[u8]) -> ResultType<Self> {
Self::create(path, &mut io::Cursor::new(bytes), None)
}
pub(super) fn copy_verified(source: &Path, expected_sha256: &str) -> ResultType<Self> {
let mut source = File::open(source)?;
let path = directory::path()?.with_extension("tmp");
Self::create(&path, &mut source, Some(expected_sha256))
}
fn create(
path: &Path,
source: &mut impl Read,
expected_sha256: Option<&str>,
) -> ResultType<Self> {
let mut file = create_protected_file(path)?;
let result = (|| -> ResultType<Owned<HANDLE>> {
io::copy(source, &mut file)?;
file.sync_all()?;
if let Some(expected) = expected_sha256 {
crate::update_hash::verify_sha256_reader(&mut file, expected).map_err(|error| {
anyhow!(
"Failed to verify staged installer file {}: {error:?}",
path.display()
)
})?;
}
duplicate_cleanup_handle(&file)
})();
let cleanup_handle = result.map_err(|error| {
// The initial handle can remove an incomplete file without reopening its protected path.
if let Err(cleanup_error) = delete_staged_file(HANDLE(file.as_raw_handle())) {
log::warn!(
"Failed to remove incomplete installer file {}: {cleanup_error}",
path.display()
);
}
error
})?;
Ok(Self {
path: path.to_owned(),
cleanup_handle: Some(cleanup_handle),
})
}
pub(super) fn path(&self) -> &Path {
&self.path
}
pub(super) fn release(&mut self) {
// cmd.exe cannot read/copy these files while a DELETE handle is open.
// The handoff gate keeps it waiting until every staged handle is closed.
drop(self.cleanup_handle.take());
}
}
impl Drop for ProtectedInstallFile {
fn drop(&mut self) {
// Cancellation cannot reopen the protected path with DELETE permission.
let result = match &self.cleanup_handle {
Some(handle) => delete_staged_file(**handle),
None => match fs::remove_file(&self.path) {
Err(error) if error.kind() != io::ErrorKind::NotFound => Err(error.into()),
_ => Ok(()),
},
};
if let Err(error) = result {
log::warn!(
"Protected installer file may remain at {}: {error}",
self.path.display()
);
}
}
}
fn create_protected_file(path: &Path) -> ResultType<File> {
let mut descriptor = PSECURITY_DESCRIPTOR::default();
// Restrict OWNER RIGHTS at creation so the unelevated creator cannot reopen
// the file for writing or grant itself that permission after staging.
unsafe {
ConvertStringSecurityDescriptorToSecurityDescriptorW(
w!("D:P(A;;FA;;;SY)(A;;FA;;;BA)(A;;FRFX;;;OW)"),
SDDL_REVISION_1,
&mut descriptor,
None,
)?;
}
let _descriptor = unsafe { Owned::new(HLOCAL(descriptor.0)) };
let attributes = SECURITY_ATTRIBUTES {
nLength: std::mem::size_of::<SECURITY_ATTRIBUTES>() as u32,
lpSecurityDescriptor: descriptor.0,
bInheritHandle: false.into(),
};
let path_string = path
.to_str()
.ok_or_else(|| anyhow!("Invalid protected installer path"))?;
let path_string = wide_string(path_string);
let handle = unsafe {
CreateFileW(
PCWSTR(path_string.as_ptr()),
GENERIC_READ.0 | GENERIC_WRITE.0 | DELETE.0,
FILE_SHARE_READ,
Some(&attributes),
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
None,
)
}
.map_err(|error| {
anyhow!(
"Failed to create protected installer file {}: {error}",
path.display()
)
})?;
Ok(unsafe { File::from_raw_handle(handle.0) })
}
fn duplicate_cleanup_handle(file: &File) -> ResultType<Owned<HANDLE>> {
let mut handle = HANDLE::default();
// Retain deletion capability, not a writable handle, while waiting for UAC.
unsafe {
let process = GetCurrentProcess();
DuplicateHandle(
process,
HANDLE(file.as_raw_handle()),
process,
&mut handle,
DELETE.0,
false,
Default::default(),
)?;
Ok(Owned::new(handle))
}
}
fn delete_staged_file(handle: HANDLE) -> ResultType<()> {
let disposition = FILE_DISPOSITION_INFO {
DeleteFile: true.into(),
};
unsafe {
SetFileInformationByHandle(
handle,
FileDispositionInfo,
&disposition as *const _ as *const _,
std::mem::size_of_val(&disposition) as u32,
)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use windows::{
core::BOOL,
Win32::{
Foundation::HANDLE,
Security::{
AccessCheck,
Authorization::{GetNamedSecurityInfoW, SE_FILE_OBJECT},
CreateRestrictedToken, CreateWellKnownSid, DuplicateToken, SecurityImpersonation,
WinBuiltinAdministratorsSid, DACL_SECURITY_INFORMATION, DISABLE_MAX_PRIVILEGE,
GENERIC_MAPPING, GROUP_SECURITY_INFORMATION, OWNER_SECURITY_INFORMATION,
PRIVILEGE_SET, PSID, SECURITY_MAX_SID_SIZE, SID_AND_ATTRIBUTES, TOKEN_DUPLICATE,
TOKEN_QUERY,
},
Storage::FileSystem::{
FILE_ALL_ACCESS, FILE_GENERIC_EXECUTE, FILE_GENERIC_READ, FILE_GENERIC_WRITE,
},
System::Threading::{GetCurrentProcess, OpenProcessToken},
},
};
#[test]
fn cancelled_staging_removes_files_without_parent_delete_permission() {
use windows::Win32::Security::{ImpersonateLoggedOnUser, RevertToSelf};
let root = std::env::temp_dir().join(format!("rustdesk-cancel-{}", uuid::Uuid::new_v4()));
fs::create_dir(&root).unwrap();
let path = root.join("package.tmp");
let file = ProtectedInstallFile::new(&path, b"cancelled update").unwrap();
// Omit DELETE_CHILD as in Windows Temp, but retain fixture ACL control.
set_parent_acl(&root, w!("D:P(A;;0x1600af;;;OW)"));
unsafe {
let token = ordinary_owner_token();
ImpersonateLoggedOnUser(*token).unwrap();
drop(file);
RevertToSelf().unwrap();
}
set_parent_acl(&root, w!("D:P(A;;FA;;;OW)"));
let remaining = fs::symlink_metadata(&path);
fs::remove_dir_all(&root).unwrap();
assert!(
matches!(remaining, Err(error) if error.kind() == io::ErrorKind::NotFound),
"Cancelled staging must remove the protected package"
);
}
fn set_parent_acl(path: &Path, sddl: PCWSTR) {
use windows::Win32::Security::{SetFileSecurityW, PROTECTED_DACL_SECURITY_INFORMATION};
let path = wide_string(path.to_str().unwrap());
let mut descriptor = PSECURITY_DESCRIPTOR::default();
unsafe {
ConvertStringSecurityDescriptorToSecurityDescriptorW(
sddl,
SDDL_REVISION_1,
&mut descriptor,
None,
)
.unwrap();
let _descriptor = Owned::new(HLOCAL(descriptor.0));
assert!(SetFileSecurityW(
PCWSTR(path.as_ptr()),
DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
descriptor
)
.as_bool());
}
}
#[test]
fn staged_files_protect_contents_and_remove_unverified_data() {
const HASH: &str = "304ca1638c5effa6832e0e15b958a8f74847efe4df9c3f3187216e921c168fed";
let root = std::env::temp_dir().join(format!("rustdesk-staging-{}", uuid::Uuid::new_v4()));
fs::create_dir(&root).unwrap();
let path = root.join("input.tmp");
let file =
ProtectedInstallFile::create(&path, &mut io::Cursor::new(b"rustdesk"), Some(HASH))
.unwrap();
assert_eq!(
ordinary_owner_access(file.path()),
(FILE_GENERIC_READ | FILE_GENERIC_EXECUTE).0
);
assert_eq!(fs::read(file.path()).unwrap(), b"rustdesk");
assert!(ProtectedInstallFile::new(&path, b"replacement").is_err());
let rejected = root.join("rejected.tmp");
assert!(ProtectedInstallFile::create(
&rejected,
&mut io::Cursor::new(b"changed"),
Some(HASH)
)
.is_err());
assert!(!rejected.exists());
drop(file);
fs::remove_dir(root).unwrap();
}
fn ordinary_owner_access(path: &Path) -> u32 {
let path = wide_string(path.to_str().unwrap());
let mut descriptor = PSECURITY_DESCRIPTOR::default();
unsafe {
GetNamedSecurityInfoW(
PCWSTR(path.as_ptr()),
SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
None,
None,
None,
None,
&mut descriptor,
)
.ok()
.unwrap();
}
let _descriptor = unsafe { Owned::new(HLOCAL(descriptor.0)) };
let token = ordinary_owner_token();
let mapping = GENERIC_MAPPING {
GenericRead: FILE_GENERIC_READ.0,
GenericWrite: FILE_GENERIC_WRITE.0,
GenericExecute: FILE_GENERIC_EXECUTE.0,
GenericAll: FILE_ALL_ACCESS.0,
};
let mut privileges = PRIVILEGE_SET::default();
let mut size = std::mem::size_of::<PRIVILEGE_SET>() as u32;
let mut granted = 0;
let mut allowed = BOOL::default();
unsafe {
AccessCheck(
descriptor,
*token,
winapi::um::winnt::MAXIMUM_ALLOWED,
&mapping,
Some(&mut privileges),
&mut size,
&mut granted,
&mut allowed,
)
.unwrap();
}
assert!(allowed.as_bool());
granted
}
fn ordinary_owner_token() -> Owned<HANDLE> {
let mut original = HANDLE::default();
unsafe {
OpenProcessToken(
GetCurrentProcess(),
TOKEN_QUERY | TOKEN_DUPLICATE,
&mut original,
)
.unwrap();
}
let original = unsafe { Owned::new(original) };
let mut sid = [0; SECURITY_MAX_SID_SIZE as usize];
let sid_pointer = PSID(sid.as_mut_ptr().cast());
let mut size = sid.len() as u32;
unsafe {
CreateWellKnownSid(
WinBuiltinAdministratorsSid,
None,
Some(sid_pointer),
&mut size,
)
.unwrap();
}
let disabled = [SID_AND_ATTRIBUTES {
Sid: sid_pointer,
Attributes: 0,
}];
let mut restricted = HANDLE::default();
unsafe {
CreateRestrictedToken(
*original,
DISABLE_MAX_PRIVILEGE,
Some(&disabled),
None,
None,
&mut restricted,
)
.unwrap();
}
let restricted = unsafe { Owned::new(restricted) };
let mut impersonation = HANDLE::default();
unsafe {
DuplicateToken(*restricted, SecurityImpersonation, &mut impersonation).unwrap();
}
unsafe { Owned::new(impersonation) }
}
}
+237 -164
View File
@@ -1,95 +1,147 @@
pub(super) mod directory;
mod gate;
mod directory;
use super::{
installer_files::ProtectedInstallFile,
installer_shell::{
get_system_executable, launch_elevated, path_for_cmd_environment,
trusted_install_environment, BATCH_SHORTCUT_COPY_FAILURE_EXIT_CODE, CMD_RELATIVE_PATH,
get_system_executable, path_for_cmd_assignment, path_for_cmd_environment,
run_elevated_and_wait, trusted_install_environment,
BATCH_SHORTCUT_DECODE_FAILURE_EXIT_CODE, CMD_RELATIVE_PATH,
WIN7_SHELL_EXECUTE_MAX_PARAMETER_CHARS,
},
validate_install_app_name, ResultType,
};
use gate::InstallGate;
use hbb_common::{bail, log};
use std::{fs, io, path::Path};
use hbb_common::{
anyhow::anyhow,
bail, log,
sha2::{Digest, Sha256},
};
use std::{
fs,
io::{self, Write},
path::{Path, PathBuf},
};
const CERTUTIL_RELATIVE_PATH: &str = "certutil.exe";
const CHCP_RELATIVE_PATH: &str = "chcp.com";
const FINDSTR_RELATIVE_PATH: &str = "findstr.exe";
const UTF8_CODE_PAGE: u32 = 65001;
const INSTALL_HANDOFF_RUNNER_EXISTS_EXIT_CODE: u32 = 0x5253_0001;
const INSTALL_HANDOFF_COPY_FAILURE_EXIT_CODE: u32 = 0x5253_0002;
const INSTALL_HANDOFF_HASH_FAILURE_EXIT_CODE: u32 = 0x5253_0003;
const INSTALL_HANDOFF_HASH_MISMATCH_EXIT_CODE: u32 = 0x5253_0004;
const BATCH_CODE_PAGE_FAILURE_EXIT_CODE: u32 = 0x5253_0005;
const BATCH_OUTPUT_DIRECTORY_EXISTS_EXIT_CODE: u32 = 0x5253_0006;
const BATCH_OUTPUT_DIRECTORY_CREATE_FAILURE_EXIT_CODE: u32 = 0x5253_0007;
const SHA256_HASH_LENGTH: usize = 32;
pub(super) struct InstallCommands {
pub script: String,
pub files: Vec<ProtectedInstallFile>,
}
impl From<String> for InstallCommands {
fn from(script: String) -> Self {
Self {
script,
files: Vec::new(),
}
}
}
type BatchHash = [u8; SHA256_HASH_LENGTH];
struct InstallCommandScript {
file: ProtectedInstallFile,
_files: Vec<ProtectedInstallFile>,
path: PathBuf,
expected_hash: BatchHash,
}
impl InstallCommandScript {
fn release_files(&mut self) {
self.file.release();
for file in &mut self._files {
file.release();
impl Drop for InstallCommandScript {
fn drop(&mut self) {
if let Err(err) = fs::remove_file(&self.path) {
if err.kind() != io::ErrorKind::NotFound {
log::warn!(
"Failed to remove temporary installer file {:?}: {err}",
self.path
);
}
}
}
}
fn prepare_install_commands(cmds: &InstallCommands) -> ResultType<String> {
let commands = cmds.script.replace("\r\n", "\n").replace('\n', "\r\n");
fn prepare_install_commands(commands: &str) -> ResultType<String> {
let commands = commands.replace("\r\n", "\n").replace('\n', "\r\n");
let chcp_path = get_system_executable(CHCP_RELATIVE_PATH)?;
let chcp = path_for_cmd_environment(&chcp_path)?;
let files = cmds
.files
.iter()
.map(|file| path_for_cmd_environment(file.path()).map(|path| format!("\"{path}\"")))
.collect::<ResultType<Vec<_>>>()?
.join(" ");
// Call a fixed label so early EXIT /B still reaches cleanup, without reparsing a path.
// Exit the dedicated cmd after self-deletion; EXIT /B would reopen the missing BAT.
Ok(format!(
"@echo off\r\nsetlocal EnableExtensions DisableDelayedExpansion\r\n\
set \"ERRORLEVEL=\"\r\n\
call :rustdesk_install\r\n\
set \"RUSTDESK_INSTALL_EXIT_CODE=%ERRORLEVEL%\"\r\n\
rd /s /q \"%~f0.dir\" > nul 2>&1\r\n\
(del /q \"%~f0\" {files} > nul 2>&1 & exit %RUSTDESK_INSTALL_EXIT_CODE%)\r\n\
:rustdesk_install\r\n\
\"{chcp}\" {UTF8_CODE_PAGE} > nul || exit /b \
{BATCH_CODE_PAGE_FAILURE_EXIT_CODE}\r\n\
{}\r\n\
if exist \"%~f0.dir\" exit /b {BATCH_OUTPUT_DIRECTORY_EXISTS_EXIT_CODE}\r\n\
md \"%~f0.dir\" || exit /b {BATCH_OUTPUT_DIRECTORY_CREATE_FAILURE_EXIT_CODE}\r\n\
set \"RUSTDESK_OUTPUT_DIR=%~f0.dir\"\r\n{commands}\r\nexit /b 0\r\n",
trusted_install_environment()?
))
}
fn write_install_script(cmds: InstallCommands, path: &Path) -> ResultType<InstallCommandScript> {
fn write_install_script(cmds: String) -> ResultType<InstallCommandScript> {
let directory = std::env::temp_dir();
path_for_cmd_environment(&directory)?;
let commands = prepare_install_commands(&cmds)?;
Ok(InstallCommandScript {
file: ProtectedInstallFile::new(path, commands.as_bytes())?,
_files: cmds.files,
})
let expected_hash = Sha256::digest(commands.as_bytes()).into();
let path = directory.join(format!(
"rustdesk_install_{}.bat",
uuid::Uuid::new_v4().simple()
));
let mut file = fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&path)?;
let script = InstallCommandScript {
path,
expected_hash,
};
file.write_all(commands.as_bytes())?;
file.sync_all()?;
Ok(script)
}
fn protected_install_parameters(
fn install_hash_pattern(hash: &BatchHash) -> String {
hash.iter()
.map(|byte| format!("{byte:02x}"))
.collect::<Vec<_>>()
.join(" *")
}
fn verified_install_bootstrap(
script: &InstallCommandScript,
gate: &InstallGate,
runner_directory: &Path,
) -> ResultType<String> {
let path = path_for_cmd_environment(script.file.path())?;
let pipe = gate.name();
let parameters =
format!("/D /E:ON /V:OFF /C \"set /p RUSTDESK_HANDOFF=<\"{pipe}\" >nul && \"{path}\"\"");
let source = path_for_cmd_assignment(&script.path)?;
let directory = path_for_cmd_assignment(runner_directory)?;
let runner = runner_directory.join("install.bat");
let runner = path_for_cmd_assignment(&runner)?;
let cmd_path = get_system_executable(CMD_RELATIVE_PATH)?;
let certutil_path = get_system_executable(CERTUTIL_RELATIVE_PATH)?;
let findstr_path = get_system_executable(FINDSTR_RELATIVE_PATH)?;
let cmd = path_for_cmd_assignment(&cmd_path)?;
let certutil = path_for_cmd_assignment(&certutil_path)?;
let findstr = path_for_cmd_assignment(&findstr_path)?;
// Short names preserve headroom under the Windows 7 ShellExecuteExW 2,048
// UTF-16-character parameter limit. Paths are stored with delayed expansion
// disabled, then expanded indirectly so literal `!` survives: S=source,
// R=runner, Q=cmd.exe, H=certutil.exe, F=findstr.exe, D=directory, E=exit code.
Ok(format!(
"setlocal DisableDelayedExpansion & set \"S={source}\" & set \"R={runner}\" & \
set \"Q={cmd}\" & set \"H={certutil}\" & set \"F={findstr}\" & \
set \"ERRORLEVEL=\" & set \"D={directory}\" & set \"E=0\" & setlocal EnableDelayedExpansion & \
if exist \"!R!\" (set \"E={INSTALL_HANDOFF_RUNNER_EXISTS_EXIT_CODE}\") else (\
copy /Y \"!S!\" \"!R!\" > nul || \
(set \"E={INSTALL_HANDOFF_COPY_FAILURE_EXIT_CODE}\") & \
if \"!E!\"==\"0\" (\"!H!\" -hashfile \"!R!\" SHA256 > \"!R!.hash\" || \
set \"E={INSTALL_HANDOFF_HASH_FAILURE_EXIT_CODE}\") & \
if \"!E!\"==\"0\" (\"!F!\" /R /I /X /C:\"{}\" \"!R!.hash\" > nul || \
set \"E={INSTALL_HANDOFF_HASH_MISMATCH_EXIT_CODE}\") & \
if \"!E!\"==\"0\" (\"!Q!\" /D /E:ON /V:OFF /C \"\"!R!\"\" & \
set \"E=!errorlevel!\")) & \
rd /s /q \"!D!\" > nul 2>&1 & exit /b !E!",
install_hash_pattern(&script.expected_hash),
))
}
fn verified_install_parameters(
script: &InstallCommandScript,
directory: &Path,
) -> ResultType<String> {
let parameters = format!(
"/D /E:ON /V:ON /C {}",
verified_install_bootstrap(script, directory)?
);
let length = parameters.encode_utf16().count();
if length >= WIN7_SHELL_EXECUTE_MAX_PARAMETER_CHARS {
bail!("Elevated command is too long: {length} UTF-16 characters");
@@ -97,28 +149,19 @@ fn protected_install_parameters(
Ok(parameters)
}
pub(super) fn run_cmds(cmds: impl Into<InstallCommands>, show: bool, tip: &str) -> ResultType<()> {
pub(super) fn run_cmds(cmds: String, show: bool, tip: &str) -> ResultType<()> {
validate_install_app_name(&crate::get_app_name())?;
let path = directory::path()?.with_extension("bat");
let directory = path.with_extension("bat.dir");
let mut script = write_install_script(cmds.into(), &path)?;
let script = write_install_script(cmds)?;
let cmd_path = get_system_executable(CMD_RELATIVE_PATH)?;
let gate = InstallGate::new()?;
let parameters = protected_install_parameters(&script, &gate)?;
let directory = directory::path()?;
let parameters = verified_install_parameters(&script, &directory)?;
directory::create(&directory)?;
let process = launch_elevated(&cmd_path, &parameters, show).map_err(|err| {
log::debug!("Failed to launch elevated installer: {err:#}");
err.context(format!(
"Installer handoff failed; protected directory may remain at {}",
let exit_code = run_elevated_and_wait(&cmd_path, &parameters, show).map_err(|err| {
anyhow!(
"Installer handoff failed; protected directory may remain at {}: {err}",
directory.display()
))
)
})?;
gate.wait_for_client(process.handle())?;
// Keep DELETE-capable handles through UAC cancellation, but close them before
// cmd opens the BAT or copies its inputs. The protected DACL stays unchanged.
script.release_files();
gate.release(process.handle())?;
let exit_code = process.wait()?;
// Cleanup must not change the result of an installation that already ran.
match fs::symlink_metadata(&directory) {
Ok(_) => log::warn!(
@@ -142,111 +185,141 @@ pub(super) fn run_cmds(cmds: impl Into<InstallCommands>, show: bool, tip: &str)
fn elevated_install_failure_reason(exit_code: u32) -> &'static str {
match exit_code {
INSTALL_HANDOFF_RUNNER_EXISTS_EXIT_CODE => "protected runner already exists",
INSTALL_HANDOFF_COPY_FAILURE_EXIT_CODE => "failed to copy protected runner",
INSTALL_HANDOFF_HASH_FAILURE_EXIT_CODE => "failed to hash protected runner",
INSTALL_HANDOFF_HASH_MISMATCH_EXIT_CODE => "protected runner hash mismatch",
BATCH_CODE_PAGE_FAILURE_EXIT_CODE => "failed to set the installer code page",
BATCH_SHORTCUT_COPY_FAILURE_EXIT_CODE => "failed to copy a prepared shortcut",
BATCH_OUTPUT_DIRECTORY_EXISTS_EXIT_CODE => "installer output directory already exists",
BATCH_OUTPUT_DIRECTORY_CREATE_FAILURE_EXIT_CODE => {
"failed to create the installer output directory"
}
BATCH_SHORTCUT_DECODE_FAILURE_EXIT_CODE => "failed to decode an embedded shortcut",
_ => "installer command failed",
}
}
#[cfg(test)]
mod tests {
use super::super::installer_shell::shortcut_bytes;
use super::*;
use std::{
os::windows::{io::AsRawHandle, process::CommandExt},
process::Stdio,
use super::super::installer_shell::{
embedded_shortcut_commands, shortcut_bytes, WIN7_SHELL_EXECUTE_MAX_PARAMETER_CHARS,
};
use windows::Win32::Foundation::HANDLE;
use super::*;
use ::windows::Win32::System::Threading;
use std::os::windows::process::CommandExt;
#[test]
fn protected_handoff_preserves_shortcuts_failures_and_cleanup() {
const COMMAND_FAILURE: u32 = 77;
for exit_code in [0, COMMAND_FAILURE] {
let root =
std::env::temp_dir().join(format!("rustdesk-handoff-{}", uuid::Uuid::new_v4()));
let source = root.join("source !&^@().bat");
let runner = source.with_extension("bat.dir");
fs::create_dir_all(&runner).unwrap();
let bytes = shortcut_bytes(r"C:\RustDesk.exe", Some("--tray"), None).unwrap();
let shortcut =
ProtectedInstallFile::new(&root.join("shortcut !&^@().tmp"), &bytes).unwrap();
let marker = root.join("copied.lnk");
let commands = InstallCommands {
script: format!(
"copy /B /Y \"{}\" \"{}\" > nul\r\nexit /b {exit_code}",
shortcut.path().display(),
marker.display()
),
files: vec![shortcut],
};
let mut script = write_install_script(commands, &source).unwrap();
if !super::super::is_elevated(None).unwrap() {
assert!(fs::write(script.file.path(), "exit /b 0\r\n").is_err());
}
let output = run_script(&mut script, true);
let copied = fs::read(&marker).unwrap();
let cleaned = !runner.exists()
&& !script.file.path().exists()
&& script._files.iter().all(|file| !file.path().exists());
fs::remove_dir_all(&root).unwrap();
assert_eq!(
output.status.code().map(|code| code as u32),
Some(exit_code),
"{output:?}"
);
assert_eq!(copied, bytes);
assert!(cleaned, "The runner and protected inputs must be removed");
}
fn native_install_handoff_verifies_before_execution() {
let marker = std::env::temp_dir().join(format!(
"rustdesk_install_marker_{}",
uuid::Uuid::new_v4().simple()
));
let runner_dir = std::env::temp_dir().join(format!(
"rustdesk_install_!RUSTDESK_HANDOFF_EXPAND!&^@()runner_{}",
uuid::Uuid::new_v4().simple()
));
std::fs::create_dir(&runner_dir).expect("runner directory should be created");
let shortcut_commands = embedded_shortcut_commands(
shortcut_bytes(r"C:\RustDesk.exe", None, None)
.expect("native shortcut should be generated"),
"test.lnk",
"test",
);
let script = write_install_script(format!(
"if \"%PROGRAMDATA%\"==\"rustdesk_untrusted\" exit /b 77\r\n\
if \"%PUBLIC%\"==\"rustdesk_untrusted\" exit /b 77\r\n\
{shortcut_commands}\r\n\
> \"{}\" echo verified",
marker.display()
))
.expect("install script should be created");
let bootstrap = verified_install_bootstrap(&script, &runner_dir)
.expect("native verifier bootstrap should be generated");
assert_native_handoff_structure(&script, &shortcut_commands, &runner_dir);
let output = run_install_bootstrap_for_test(&bootstrap);
assert!(
output.status.success(),
"unchanged script failed: {}",
String::from_utf8_lossy(&output.stderr)
);
assert!(marker.exists(), "verified install script must execute");
std::fs::remove_file(&marker).expect("test marker should be removed");
assert!(
!runner_dir.exists(),
"protected runner directory must be removed"
);
std::fs::create_dir(&runner_dir).expect("runner directory should be created");
assert_replaced_install_script_is_rejected(&script, &runner_dir, &marker);
assert!(
!runner_dir.exists(),
"failed handoff directory must be removed"
);
}
#[test]
fn interrupted_handoff_does_not_execute_the_batch() {
let root =
std::env::temp_dir().join(format!("rustdesk-interrupted-{}", uuid::Uuid::new_v4()));
fs::create_dir(&root).unwrap();
let source = root.join("source.bat");
let marker = root.join("executed");
let commands = format!("echo unexpected > \"{}\"", marker.display());
let mut script = write_install_script(commands.into(), &source).unwrap();
let output = run_script(&mut script, false);
assert!(!output.status.success(), "{output:?}");
assert!(!marker.exists());
drop(script);
assert!(!source.exists());
fs::remove_dir(&root).unwrap();
// A child exiting without connecting must also retire the pending pipe I/O.
let gate = InstallGate::new().unwrap();
let mut child =
std::process::Command::new(get_system_executable(CMD_RELATIVE_PATH).unwrap())
.args(["/D", "/C", "exit", "0"])
.creation_flags(::windows::Win32::System::Threading::CREATE_NO_WINDOW.0)
.spawn()
.unwrap();
assert!(gate.wait_for_client(HANDLE(child.as_raw_handle())).is_err());
assert!(child.wait().unwrap().success());
fn assert_native_handoff_structure(
script: &InstallCommandScript,
shortcut_commands: &str,
runner_dir: &Path,
) {
let bootstrap = verified_install_bootstrap(script, runner_dir)
.expect("native verifier bootstrap should be generated");
assert!(shortcut_commands.contains("certutil"));
assert!(shortcut_commands.contains("-decode"));
assert!(!shortcut_commands.to_ascii_lowercase().contains("cscript"));
assert!(!shortcut_commands
.to_ascii_lowercase()
.contains("powershell"));
let win7_hash_pattern = script
.expected_hash
.iter()
.map(|byte| format!("{byte:02x}"))
.collect::<Vec<_>>()
.join(" *");
assert!(bootstrap.contains(&format!("/R /I /X /C:\"{win7_hash_pattern}\"")));
let parameters = verified_install_parameters(script, runner_dir)
.expect("elevated parameters should be generated");
assert!(bootstrap.contains("certutil.exe"));
assert!(bootstrap.contains("findstr.exe"));
assert!(!bootstrap.to_ascii_lowercase().contains("powershell"));
assert!(parameters.encode_utf16().count() < WIN7_SHELL_EXECUTE_MAX_PARAMETER_CHARS);
}
fn run_script(script: &mut InstallCommandScript, release: bool) -> std::process::Output {
let gate = InstallGate::new().unwrap();
let parameters = protected_install_parameters(script, &gate).unwrap();
let mut child =
std::process::Command::new(get_system_executable(CMD_RELATIVE_PATH).unwrap())
.raw_arg(parameters)
.creation_flags(::windows::Win32::System::Threading::CREATE_NO_WINDOW.0)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
let process = HANDLE(child.as_raw_handle());
gate.wait_for_client(process).unwrap();
assert!(child.try_wait().unwrap().is_none());
if release {
script.release_files();
gate.release(process).unwrap();
} else {
drop(gate);
}
child.wait_with_output().unwrap()
fn assert_replaced_install_script_is_rejected(
script: &InstallCommandScript,
runner_dir: &Path,
marker: &Path,
) {
std::fs::write(
&script.path,
format!("> \"{}\" echo hijacked\r\n", marker.display()),
)
.expect("install script should be replaceable");
let replaced = verified_install_bootstrap(&script, &runner_dir)
.expect("replacement verifier should be generated");
let output = run_install_bootstrap_for_test(&replaced);
assert!(
!output.status.success(),
"replaced script unexpectedly passed verification"
);
assert_eq!(
output.status.code(),
Some(INSTALL_HANDOFF_HASH_MISMATCH_EXIT_CODE as i32)
);
assert!(!marker.exists(), "replaced script must not execute");
}
fn run_install_bootstrap_for_test(bootstrap: &str) -> std::process::Output {
let cmd = get_system_executable(CMD_RELATIVE_PATH).expect("system cmd.exe should resolve");
let mut command = std::process::Command::new(cmd);
command
.env("PROGRAMDATA", "rustdesk_untrusted")
.env("PUBLIC", "rustdesk_untrusted")
.env("RUSTDESK_HANDOFF_EXPAND", "expanded");
command.raw_arg(format!("/D /E:ON /V:ON /C {bootstrap}"));
command
.creation_flags(Threading::CREATE_NO_WINDOW.0)
.output()
.expect("native verifier should run")
}
}
@@ -22,7 +22,7 @@ use windows::{
},
};
pub(in crate::platform::windows) fn path() -> ResultType<PathBuf> {
pub(super) fn path() -> ResultType<PathBuf> {
let system = get_system_executable("")?;
let windows = system
.parent()
@@ -1,135 +0,0 @@
use super::super::wide_string;
use hbb_common::{bail, log, ResultType};
use std::io;
use windows::{
core::{Owned, PCWSTR},
Win32::{
Foundation::{
ERROR_IO_PENDING, ERROR_NOT_FOUND, ERROR_PIPE_CONNECTED, HANDLE, WAIT_OBJECT_0,
},
Storage::FileSystem::{
WriteFile, FILE_FLAG_FIRST_PIPE_INSTANCE, FILE_FLAG_OVERLAPPED, PIPE_ACCESS_OUTBOUND,
},
System::{
Pipes::{
ConnectNamedPipe, CreateNamedPipeW, GetNamedPipeClientProcessId,
PIPE_REJECT_REMOTE_CLIENTS,
},
Threading::{CreateEventW, GetProcessId, WaitForMultipleObjects, INFINITE},
IO::{CancelIoEx, GetOverlappedResult, OVERLAPPED},
},
},
};
const READY: &[u8] = b"1\r\n";
const PIPE_INSTANCES: u32 = 1;
pub(super) struct InstallGate {
pipe: Owned<HANDLE>,
name: String,
}
impl InstallGate {
pub(super) fn new() -> ResultType<Self> {
let name = format!(
r"\\.\pipe\rustdesk-install-{}",
uuid::Uuid::new_v4().simple()
);
let wide_name = wide_string(&name);
let pipe = unsafe {
CreateNamedPipeW(
PCWSTR(wide_name.as_ptr()),
PIPE_ACCESS_OUTBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE | FILE_FLAG_OVERLAPPED,
PIPE_REJECT_REMOTE_CLIENTS,
PIPE_INSTANCES,
READY.len() as u32,
0,
0,
None,
)
};
if pipe.is_invalid() {
return Err(io::Error::last_os_error().into());
}
Ok(Self {
pipe: unsafe { Owned::new(pipe) },
name,
})
}
pub(super) fn name(&self) -> &str {
&self.name
}
pub(super) fn wait_for_client(&self, process: HANDLE) -> ResultType<()> {
let event = unsafe { Owned::new(CreateEventW(None, true, false, None)?) };
let mut overlapped = OVERLAPPED {
hEvent: *event,
..Default::default()
};
if let Err(error) = unsafe { ConnectNamedPipe(*self.pipe, Some(&mut overlapped)) } {
if error.code() == ERROR_IO_PENDING.to_hresult() {
self.wait_for_io(&overlapped, process)?;
} else if error.code() != ERROR_PIPE_CONNECTED.to_hresult() {
return Err(error.into());
}
}
let mut client = 0;
unsafe { GetNamedPipeClientProcessId(*self.pipe, &mut client)? };
if client != unsafe { GetProcessId(process) } {
bail!("Unexpected installer handoff pipe client");
}
Ok(())
}
// cmd's SET /P must read a line successfully before it opens the BAT. Closing
// the pipe without this signal fails the command instead of running the BAT.
pub(super) fn release(&self, process: HANDLE) -> ResultType<()> {
let event = unsafe { Owned::new(CreateEventW(None, true, false, None)?) };
let mut overlapped = OVERLAPPED {
hEvent: *event,
..Default::default()
};
// Windows 7 requires a byte-count pointer even for overlapped writes.
let mut written = 0;
if let Err(error) = unsafe {
WriteFile(
*self.pipe,
Some(READY),
Some(&mut written),
Some(&mut overlapped),
)
} {
if error.code() != ERROR_IO_PENDING.to_hresult() {
return Err(error.into());
}
self.wait_for_io(&overlapped, process)?;
}
// Keep the server handle alive until cmd exits so the signal stays buffered.
Ok(())
}
fn wait_for_io(&self, overlapped: &OVERLAPPED, process: HANDLE) -> ResultType<()> {
let wait =
unsafe { WaitForMultipleObjects(&[overlapped.hEvent, process], false, INFINITE) };
let mut transferred = 0;
if wait == WAIT_OBJECT_0 {
unsafe { GetOverlappedResult(*self.pipe, overlapped, &mut transferred, false)? };
return Ok(());
}
let error = io::Error::last_os_error();
// Retire pending I/O before the stack-allocated OVERLAPPED and event go away.
if let Err(error) = unsafe { CancelIoEx(*self.pipe, Some(overlapped)) } {
if error.code() != ERROR_NOT_FOUND.to_hresult() {
log::warn!("Failed to cancel installer handoff I/O: {error}");
}
}
// Cancellation normally completes with ERROR_OPERATION_ABORTED; preserve
// the original wait failure after observing that completion.
let _ = unsafe { GetOverlappedResult(*self.pipe, overlapped, &mut transferred, true) };
if wait.0 == WAIT_OBJECT_0.0 + 1 {
bail!("Elevated installer exited before the file handoff completed");
}
bail!("Installer handoff wait failed ({}): {error}", wait.0)
}
}
+41 -41
View File
@@ -1,7 +1,10 @@
use super::{
installer_files::ProtectedInstallFile, installer_handoff::directory, wide_string, ResultType,
use super::{wide_string, ResultType};
use hbb_common::{
anyhow::anyhow,
bail,
base64::{engine::general_purpose::STANDARD, Engine as _},
log,
};
use hbb_common::{anyhow::anyhow, bail, log};
use std::{
ffi::OsString,
io, mem,
@@ -23,7 +26,7 @@ use windows::{
};
pub(super) const CMD_RELATIVE_PATH: &str = "cmd.exe";
pub(super) const BATCH_SHORTCUT_COPY_FAILURE_EXIT_CODE: u32 = 0x5253_0008;
pub(super) const BATCH_SHORTCUT_DECODE_FAILURE_EXIT_CODE: u32 = 0x5253_0008;
pub(super) const WIN7_SHELL_EXECUTE_MAX_PARAMETER_CHARS: usize = 2048;
const SHORTCUT_ICON_INDEX: i32 = 0;
@@ -68,29 +71,26 @@ pub(super) fn shortcut_bytes(
Ok(bytes)
}
pub(super) fn staged_shortcut_commands(
bytes: Vec<u8>,
filename: &str,
) -> ResultType<(String, ProtectedInstallFile)> {
validate_install_value(filename)?;
let file = ProtectedInstallFile::new(&directory::path()?.with_extension("lnk"), &bytes)?;
let source = path_for_cmd_environment(file.path())?;
let commands = format!(
"copy /B /Y \"{source}\" \"%RUSTDESK_OUTPUT_DIR%\\{filename}\" > nul || exit /b {BATCH_SHORTCUT_COPY_FAILURE_EXIT_CODE}"
);
Ok((commands, file))
pub(super) fn embedded_shortcut_commands(bytes: Vec<u8>, filename: &str, name: &str) -> String {
let encoded = STANDARD.encode(bytes);
let encoded_path = format!("%~f0.{name}.b64");
format!(
"> \"{encoded_path}\" echo {encoded}\r\n\
certutil -f -decode \"{encoded_path}\" \"%RUSTDESK_OUTPUT_DIR%\\{filename}\" > nul || exit /b {BATCH_SHORTCUT_DECODE_FAILURE_EXIT_CODE}"
)
}
pub(super) fn staged_tray_shortcut_commands(
pub(super) fn embedded_tray_shortcut_commands(
app_name: &str,
exe: &str,
icon_location: Option<&str>,
) -> ResultType<(String, ProtectedInstallFile)> {
) -> ResultType<String> {
let filename = format!("{app_name} Tray.lnk");
staged_shortcut_commands(
Ok(embedded_shortcut_commands(
shortcut_bytes(exe, Some("--tray"), icon_location)?,
&filename,
)
"tray_shortcut",
))
}
pub(super) fn validate_install_value(value: &str) -> ResultType<()> {
@@ -135,6 +135,14 @@ pub(super) fn path_for_cmd_environment(path: &Path) -> ResultType<&str> {
Ok(value)
}
// Bootstrap assignments are parsed before DisableDelayedExpansion takes effect.
// Escape carets first so `^!` preserves each literal exclamation mark.
pub(super) fn path_for_cmd_assignment(path: &Path) -> ResultType<String> {
Ok(path_for_cmd_environment(path)?
.replace('^', "^^")
.replace('!', "^!"))
}
pub(super) fn trusted_install_environment() -> ResultType<String> {
let system = get_system_executable("")?;
let program_data = get_known_folder(&FOLDERID_ProgramData)?;
@@ -193,25 +201,7 @@ fn initialize_shell_com() -> ResultType<Option<ShellComGuard>> {
Ok(Some(ShellComGuard))
}
pub(super) struct ProcessHandle(HANDLE);
impl ProcessHandle {
pub(super) fn handle(&self) -> HANDLE {
self.0
}
pub(super) fn wait(self) -> ResultType<u32> {
let wait_result = unsafe { Threading::WaitForSingleObject(self.0, Threading::INFINITE) };
if wait_result == Foundation::WAIT_FAILED {
return Err(io::Error::last_os_error().into());
}
if wait_result != Foundation::WAIT_OBJECT_0 {
bail!("Unexpected elevated process wait result: {}", wait_result.0);
}
let mut exit_code = 0;
unsafe { Threading::GetExitCodeProcess(self.0, &mut exit_code) }?;
Ok(exit_code)
}
}
struct ProcessHandle(HANDLE);
impl Drop for ProcessHandle {
fn drop(&mut self) {
@@ -227,11 +217,11 @@ fn elevated_working_directory(executable: &Path) -> ResultType<&Path> {
.ok_or_else(|| anyhow!("Elevated executable has no parent directory"))
}
pub(super) fn launch_elevated(
pub(super) fn run_elevated_and_wait(
executable: &Path,
parameters: &str,
show: bool,
) -> ResultType<ProcessHandle> {
) -> ResultType<u32> {
let parameter_chars = parameters.encode_utf16().count();
if parameter_chars >= WIN7_SHELL_EXECUTE_MAX_PARAMETER_CHARS {
bail!("Elevated command is too long: {parameter_chars} UTF-16 characters");
@@ -259,7 +249,17 @@ pub(super) fn launch_elevated(
if info.hProcess.0.is_null() {
bail!("Windows did not return an elevated process handle");
}
Ok(ProcessHandle(info.hProcess))
let process = ProcessHandle(info.hProcess);
let wait_result = unsafe { Threading::WaitForSingleObject(process.0, Threading::INFINITE) };
if wait_result == Foundation::WAIT_FAILED {
return Err(io::Error::last_os_error().into());
}
if wait_result != Foundation::WAIT_OBJECT_0 {
bail!("Unexpected elevated process wait result: {}", wait_result.0);
}
let mut exit_code = 0;
unsafe { Threading::GetExitCodeProcess(process.0, &mut exit_code) }?;
Ok(exit_code)
}
// Escape `^` before using it to escape `&` so both survive nested cmd.exe parsing.
+40 -23
View File
@@ -1,11 +1,10 @@
mod file;
use super::{
get_custom_client_staging_dir, get_reg_of_hkcr, handle_custom_client_staging_dir_before_update,
installer_files::ProtectedInstallFile,
installer_handoff::{run_cmds, InstallCommands},
installer_shell::path_for_cmd_environment,
remove_custom_client_staging_dir, ResultType, REG_NAME_INSTALL_PRINTER,
get_custom_client_staging_dir, get_reg_of_hkcr,
handle_custom_client_staging_dir_before_update, installer_handoff::run_cmds,
installer_shell::path_for_cmd_environment, remove_custom_client_staging_dir, ResultType,
REG_NAME_INSTALL_PRINTER,
};
use hbb_common::{allow_err, anyhow::anyhow, bail, log};
use std::path::Path;
@@ -16,7 +15,10 @@ const VERIFIED_UPDATE_FILE_PREFIX: &str = "rustdesk-verified-";
const VERIFIED_EXE_FILENAME: &str = "update.exe";
const VERIFIED_MSI_FILENAME: &str = "update.msi";
const VERIFIED_UPDATE_COPY_FAILURE_EXIT_CODE: u32 = 0x5253_0009;
const VERIFIED_UPDATE_HASH_FAILURE_EXIT_CODE: u32 = 0x5253_000A;
const VERIFIED_UPDATE_HASH_MISMATCH_EXIT_CODE: u32 = 0x5253_000B;
const VERIFIED_EXE_LAUNCH_FAILURE_EXIT_CODE: u32 = 0x5253_000C;
const SHA256_HASH_LENGTH: usize = 32;
pub fn update_to_verified(file: &str, expected_sha256: &str, expected_size: u64) -> ResultType<()> {
let extension = file::update_file_extension(file).unwrap_or_default();
@@ -75,33 +77,50 @@ fn launch_verified_update(
Ok(())
}
fn verified_update_hash_pattern(expected_sha256: &str) -> ResultType<String> {
let hash = hex::decode(expected_sha256.trim())
.map_err(|err| anyhow!("Invalid expected update SHA-256: {err}"))?;
if hash.len() != SHA256_HASH_LENGTH {
bail!("Invalid expected update SHA-256 length");
}
// FINDSTR /R /I /X /C matches the complete certutil hash line; ` *` allows
// optional spaces between byte pairs.
Ok(hash
.iter()
.map(|byte| format!("{byte:02x}"))
.collect::<Vec<_>>()
.join(" *"))
}
fn launch_verified_exe(exe: &str, expected_sha256: &str) -> ResultType<()> {
let update = ProtectedInstallFile::copy_verified(Path::new(exe), expected_sha256)?;
let source = path_for_cmd_environment(update.path())?;
let source = path_for_cmd_environment(Path::new(exe))?;
let hash_pattern = verified_update_hash_pattern(expected_sha256)?;
// Keep the hash outside the EXE directory because update_me() copies that
// entire directory into the installation.
let commands = format!(
"set \"RUSTDESK_VERIFIED_EXE_SOURCE={source}\"\r\n\
set \"RUSTDESK_VERIFIED_EXE=%RUSTDESK_OUTPUT_DIR%\\{VERIFIED_EXE_FILENAME}\"\r\n\
copy /B /Y \"%RUSTDESK_VERIFIED_EXE_SOURCE%\" \"%RUSTDESK_VERIFIED_EXE%\" > nul || exit /b {VERIFIED_UPDATE_COPY_FAILURE_EXIT_CODE}\r\n\
certutil.exe -hashfile \"%RUSTDESK_VERIFIED_EXE%\" SHA256 > \"%~f0.exe.sha256\" || exit /b {VERIFIED_UPDATE_HASH_FAILURE_EXIT_CODE}\r\n\
findstr.exe /R /I /X /C:\"{hash_pattern}\" \"%~f0.exe.sha256\" > nul || exit /b {VERIFIED_UPDATE_HASH_MISMATCH_EXIT_CODE}\r\n\
start \"\" /wait \"%RUSTDESK_VERIFIED_EXE%\" --update || exit /b {VERIFIED_EXE_LAUNCH_FAILURE_EXIT_CODE}"
);
run_cmds(
InstallCommands {
script: commands,
files: vec![update],
},
false,
"update-exe",
)
run_cmds(commands, false, "update-exe")
}
fn verified_msi_install_commands(msi: &Path, quiet: bool) -> ResultType<String> {
fn verified_msi_install_commands(
msi: &Path,
expected_sha256: &str,
quiet: bool,
) -> ResultType<String> {
use windows::Win32::Foundation::{
ERROR_SUCCESS, ERROR_SUCCESS_REBOOT_INITIATED, ERROR_SUCCESS_REBOOT_REQUIRED,
};
// The input is already verified and protected, including its parent path.
// Execute the copy from the protected runner to exclude adjacent untrusted files.
// The open source handle prevents replacing that file, but not retargeting an
// ancestor junction. Copy and reverify it in the protected runner directory.
let source = path_for_cmd_environment(msi)?;
let hash_pattern = verified_update_hash_pattern(expected_sha256)?;
let quiet_args = if quiet { " /qn LAUNCH_TRAY_APP=N" } else { "" };
let app_name = crate::get_app_name();
let subkey = format!(".{}", app_name.to_lowercase());
@@ -122,6 +141,8 @@ fn verified_msi_install_commands(msi: &Path, quiet: bool) -> ResultType<String>
"set \"RUSTDESK_VERIFIED_MSI_SOURCE={source}\"\r\n\
set \"RUSTDESK_VERIFIED_MSI=%RUSTDESK_OUTPUT_DIR%\\{VERIFIED_MSI_FILENAME}\"\r\n\
copy /B /Y \"%RUSTDESK_VERIFIED_MSI_SOURCE%\" \"%RUSTDESK_VERIFIED_MSI%\" > nul || exit /b {VERIFIED_UPDATE_COPY_FAILURE_EXIT_CODE}\r\n\
certutil.exe -hashfile \"%RUSTDESK_VERIFIED_MSI%\" SHA256 > \"%RUSTDESK_VERIFIED_MSI%.sha256\" || exit /b {VERIFIED_UPDATE_HASH_FAILURE_EXIT_CODE}\r\n\
findstr.exe /R /I /X /C:\"{hash_pattern}\" \"%RUSTDESK_VERIFIED_MSI%.sha256\" > nul || exit /b {VERIFIED_UPDATE_HASH_MISMATCH_EXIT_CODE}\r\n\
msiexec.exe /i \"%RUSTDESK_VERIFIED_MSI%\"{quiet_args}{printer_args} REBOOT=ReallySuppress /norestart\r\n\
set \"RUSTDESK_MSI_EXIT_CODE=%ERRORLEVEL%\"\r\n\
if \"%RUSTDESK_MSI_EXIT_CODE%\"==\"{reboot_initiated}\" exit /b {success}\r\n\
@@ -138,12 +159,8 @@ pub(super) fn install_verified_msi(
expected_sha256: &str,
quiet: bool,
) -> ResultType<()> {
let update = ProtectedInstallFile::copy_verified(Path::new(msi), expected_sha256)?;
run_cmds(
InstallCommands {
script: verified_msi_install_commands(update.path(), quiet)?,
files: vec![update],
},
verified_msi_install_commands(Path::new(msi), expected_sha256, quiet)?,
false,
"update-msi",
)
+3 -12
View File
@@ -107,18 +107,9 @@ pub fn goto_install() {
pub fn install_me(_options: String, _path: String, _silent: bool, _debug: bool) {
#[cfg(windows)]
std::thread::spawn(move || {
if let Err(err) = crate::platform::windows::install_me(&_options, _path, _silent, _debug) {
log::error!("Installation failed: {err:#}");
if !_silent
&& !err
.downcast_ref::<::windows::core::Error>()
.is_some_and(|err| {
err.code() == ::windows::Win32::Foundation::ERROR_CANCELLED.to_hresult()
})
{
crate::platform::windows::message_box(&format!("Installation failed:\n{err:#}!"));
}
}
allow_err!(crate::platform::windows::install_me(
&_options, _path, _silent, _debug
));
std::process::exit(0);
});
}