Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions OpenBitFun-Installer/src-tauri/src/installer/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,34 @@ fn maintenance_requires_elevation(path: &Path, all_users: bool) -> Result<bool,
Err(error) if error.ends_with("not_writable") => true,
Err(error) => return Err(error),
};
let protected_lock = match super::transaction::probe_lock_access(path) {
Ok(()) => false,
Err(error) if error.kind() == std::io::ErrorKind::PermissionDenied => true,
Err(error) => return Err(format!("Cannot probe maintenance lock: {error}")),
};
Ok(!super::elevation::is_elevated()
let elevated = super::elevation::is_elevated();
let protected_lock =
lock_probe_requires_elevation(super::transaction::probe_lock_access(path), elevated)?;
Ok(!elevated
&& (all_users || scope.requires_machine_access() || protected_target || protected_lock))
}

#[cfg(windows)]
fn lock_probe_requires_elevation(
probe: anyhow::Result<()>,
elevated: bool,
) -> Result<bool, String> {
const ERROR_PRIVILEGE_NOT_HELD: i32 = 1314;

let Err(error) = probe else {
return Ok(false);
};
if !elevated
&& error.downcast_ref::<std::io::Error>().is_some_and(|error| {
error.kind() == std::io::ErrorKind::PermissionDenied
|| error.raw_os_error() == Some(ERROR_PRIVILEGE_NOT_HELD)
})
{
return Ok(true);
}
Err(format!("Cannot probe maintenance lock: {error:#}"))
}

#[cfg(windows)]
fn wait_for_legacy_update_parent(executable: &Path) -> Result<(), String> {
let powershell = system_powershell()?;
Expand Down Expand Up @@ -2632,6 +2651,34 @@ mod tests {
use std::io::{Cursor, Write};
use zip::write::SimpleFileOptions;

#[cfg(windows)]
#[test]
fn lock_probe_permission_errors_request_elevation_only_for_unelevated_processes() {
for code in [5, 1314] {
let probe = || {
Err(anyhow::Error::new(std::io::Error::from_raw_os_error(code))
.context("Cannot open maintenance lock D:\\Apps\\.OpenBitFun.maintenance.lock"))
};
assert!(super::lock_probe_requires_elevation(probe(), false).unwrap());
let message = super::lock_probe_requires_elevation(probe(), true).unwrap_err();
assert!(message.contains("D:\\Apps\\.OpenBitFun.maintenance.lock"));
assert!(message.contains(&format!("os error {code}")), "{message}");
}
}

#[cfg(windows)]
#[test]
fn lock_probe_other_errors_keep_their_diagnostics_without_requesting_elevation() {
for code in [32, 112, 123, 4321] {
let probe = Err(anyhow::Error::new(std::io::Error::from_raw_os_error(code))
.context("Cannot open maintenance lock D:\\Apps\\.OpenBitFun.maintenance.lock"));
let message = super::lock_probe_requires_elevation(probe, false).unwrap_err();
assert!(message.contains("D:\\Apps\\.OpenBitFun.maintenance.lock"));
assert!(message.contains(&format!("os error {code}")), "{message}");
}
assert!(!super::lock_probe_requires_elevation(Ok(()), false).unwrap());
}

#[test]
fn upgrades_accept_repair_and_prerelease_transitions_but_refuse_downgrades() {
assert!(super::validate_upgrade_version("1.0.9", "1.0.10").is_ok());
Expand Down
76 changes: 67 additions & 9 deletions OpenBitFun-Installer/src-tauri/src/installer/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Same-volume, journaled file changes for the standalone maintenance host.
//! Backups are never owned by an automatically deleted temporary directory.

#[cfg(windows)]
use anyhow::Context;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::fs::{self, File, OpenOptions};
Expand All @@ -14,24 +16,44 @@ pub(super) const INVENTORY: &str = ".openbitfun-installed.json";
/// Probe the same sibling lock used by maintenance, before an unelevated host
/// promises it can write. Existing installations can have a writable root but
/// a protected parent; opening an existing lock still requires write access.
pub(super) fn probe_lock_access(root: &Path) -> std::io::Result<()> {
if !root.exists() {
return Ok(());
}
let root = root.canonicalize()?;
#[cfg(windows)]
pub(super) fn probe_lock_access(root: &Path) -> anyhow::Result<()> {
const ERROR_SHARING_VIOLATION: i32 = 32;

let root = match root.canonicalize() {
Ok(path) => path,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
Err(error) => {
return Err(error).with_context(|| {
format!("Cannot resolve installation directory {}", root.display())
});
}
};
let name = root
.file_name()
.ok_or_else(|| std::io::Error::other("Installation has no directory name"))?
.with_context(|| format!("Installation has no directory name: {}", root.display()))?
.to_string_lossy();
let parent = root
.parent()
.ok_or_else(|| std::io::Error::other("Installation has no parent"))?;
.with_context(|| format!("Installation has no parent directory: {}", root.display()))?;
let lock_path = parent.join(format!(".{name}.maintenance.lock"));
let _file = OpenOptions::new()
.create(true)
.truncate(false)
.read(true)
.write(true)
.open(parent.join(format!(".{name}.maintenance.lock")))?;
.open(&lock_path)
.map_err(|error| {
let context = if error.raw_os_error() == Some(ERROR_SHARING_VIOLATION) {
format!(
"Cannot open maintenance lock {} because another process is using it. Close other installers and retry",
lock_path.display()
)
} else {
format!("Cannot open maintenance lock {}", lock_path.display())
};
anyhow::Error::new(error).context(context)
})?;
Ok(())
}

Expand Down Expand Up @@ -486,13 +508,49 @@ Set-Acl -LiteralPath $env:OPENBITFUN_LOCK_PARENT -AclObject $acl
String::from_utf8_lossy(&restored.stderr)
);
assert!(target_writable);
let error = lock.unwrap_err();
assert_eq!(
lock.unwrap_err().kind(),
error.downcast_ref::<std::io::Error>().unwrap().kind(),
std::io::ErrorKind::PermissionDenied
);
assert!(error.to_string().contains(".OpenBitFun.maintenance.lock"));
probe_lock_access(&root).unwrap();
}

#[cfg(windows)]
#[test]
fn lock_probe_reports_a_sharing_conflict_with_the_lock_path() {
use std::os::windows::fs::OpenOptionsExt;

let temp = tempfile::tempdir().unwrap();
let root = temp.path().join("OpenBitFun");
fs::create_dir(&root).unwrap();
let _held = OpenOptions::new()
.create_new(true)
.write(true)
.share_mode(0)
.open(temp.path().join(".OpenBitFun.maintenance.lock"))
.unwrap();

let error = probe_lock_access(&root).unwrap_err();
assert_eq!(
error
.downcast_ref::<std::io::Error>()
.unwrap()
.raw_os_error(),
Some(32)
);
let message = error.to_string();
assert!(
message.contains(".OpenBitFun.maintenance.lock"),
"{message}"
);
assert!(
message.contains("Close other installers and retry"),
"{message}"
);
}

#[cfg(windows)]
#[test]
fn cleanup_failure_keeps_committed_journal_until_retry_finishes() {
Expand Down
Loading