Skip to content

LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.

Imager desktop: re-check the drive before writing the first-run file and ejecting; Windows disk numbers parsed strictly before going into PowerShell

Federico Pereira56849d5055e5
diff --git a/desktop/imager/core/src/drives.rs b/desktop/imager/core/src/drives.rs
index 863a858..98e96de 100644
--- a/desktop/imager/core/src/drives.rs
+++ b/desktop/imager/core/src/drives.rs
@@ -359,6 +359,12 @@ mod win {
use windows_sys::Win32::System::Ioctl::*;
use windows_sys::Win32::System::IO::DeviceIoControl;
+ /// The disk number, parsed strictly: it is the only value put into a PowerShell command.
+ fn num(d: &Drive) -> Result<u32> {
+ if d.id.is_empty() || d.id.len() > 4 || !d.id.bytes().all(|b| b.is_ascii_digit()) { return Err(Error::new("admin.invalidArgument", "")); }
+ d.id.parse().map_err(|_| Error::new("admin.invalidArgument", ""))
+ }
+
fn ps(script: &str) -> Result<String> {
run("powershell.exe", &["-NoProfile", "-NonInteractive", "-Command", script])
}
@@ -390,7 +396,7 @@ mod win {
static LOCKS: std::sync::Mutex<Vec<isize>> = std::sync::Mutex::new(Vec::new());
pub fn unmount(d: &Drive) -> Result<()> {
- let letters = ps(&format!("(Get-Partition -DiskNumber {} | Where-Object DriveLetter | ForEach-Object {{ $_.DriveLetter }}) -join ','", d.id))?;
+ let letters = ps(&format!("(Get-Partition -DiskNumber {} | Where-Object DriveLetter | ForEach-Object {{ $_.DriveLetter }}) -join ','", num(d)?))?;
for l in letters.trim().split(',').filter(|s| s.len() == 1) {
let path: Vec<u16> = format!(r"\\.\{l}:").encode_utf16().chain([0]).collect();
let h = unsafe { CreateFileW(path.as_ptr(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, std::ptr::null(), OPEN_EXISTING, 0, std::ptr::null_mut()) };
@@ -417,14 +423,15 @@ mod win {
pub fn eject(d: &Drive) -> Result<()> {
release_locks();
- ps(&format!("$s = New-Object -ComObject Shell.Application; Get-Partition -DiskNumber {} | Where-Object DriveLetter | ForEach-Object {{ $s.Namespace(17).ParseName(\"$($_.DriveLetter):\").InvokeVerb('Eject') }}", d.id)).map(|_| ())
+ ps(&format!("$s = New-Object -ComObject Shell.Application; Get-Partition -DiskNumber {} | Where-Object DriveLetter | ForEach-Object {{ $s.Namespace(17).ParseName(\"$($_.DriveLetter):\").InvokeVerb('Eject') }}", num(d)?)).map(|_| ())
}
pub fn mount_boot(d: &Drive) -> Result<std::path::PathBuf> {
release_locks();
- let _ = ps(&format!("Update-Disk -Number {}", d.id));
+ let n = num(d)?;
+ let _ = ps(&format!("Update-Disk -Number {n}"));
for _ in 0..30 {
- let l = ps(&format!("$p = Get-Partition -DiskNumber {} -PartitionNumber 1 -ErrorAction SilentlyContinue; if ($p -and -not $p.DriveLetter) {{ $p | Add-PartitionAccessPath -AssignDriveLetter | Out-Null; $p = Get-Partition -DiskNumber {} -PartitionNumber 1 }}; $p.DriveLetter", d.id, d.id)).unwrap_or_default();
+ let l = ps(&format!("$p = Get-Partition -DiskNumber {} -PartitionNumber 1 -ErrorAction SilentlyContinue; if ($p -and -not $p.DriveLetter) {{ $p | Add-PartitionAccessPath -AssignDriveLetter | Out-Null; $p = Get-Partition -DiskNumber {} -PartitionNumber 1 }}; $p.DriveLetter", n, n)).unwrap_or_default();
let l = l.trim();
if l.len() == 1 { return Ok(format!(r"{l}:\").into()); }
std::thread::sleep(std::time::Duration::from_millis(500));
diff --git a/desktop/imager/src-tauri/src/main.rs b/desktop/imager/src-tauri/src/main.rs
index 2d3244c..65d4b49 100644
--- a/desktop/imager/src-tauri/src/main.rs
+++ b/desktop/imager/src-tauri/src/main.rs
@@ -252,16 +252,18 @@ async fn write_image(app: AppHandle, st: State<'_, App>, drive: drives::Drive, p
async fn write_firstrun(drive: drives::Drive, text: String, name: String) -> Res<()> {
if name != "lordos-firstrun.txt" || text.len() > 65536 { return Err(code("admin.invalidArgument", "")); }
tauri::async_runtime::spawn_blocking(move || {
- let dir = drives::mount_boot(&drive).map_err(err)?;
+ // the drive as listed now, never the fields the page sent
+ let d = find_drive(&drive)?;
+ let dir = drives::mount_boot(&d).map_err(err)?;
let r = fs::write(dir.join(&name), text.as_bytes()).and_then(|_| fs::File::open(dir.join(&name))?.sync_all());
- drives::unmount_boot(&drive);
+ drives::unmount_boot(&d);
r.map_err(|e| code("fs.writeFailed", e.to_string()))
}).await.map_err(|e| code("fs.writeFailed", e.to_string()))?
}
#[tauri::command]
async fn eject(drive: drives::Drive) -> Res<()> {
- tauri::async_runtime::spawn_blocking(move || drives::eject(&drive).map_err(err)).await.map_err(|e| code("imager.ejectFailed", e.to_string()))?
+ tauri::async_runtime::spawn_blocking(move || { let d = find_drive(&drive)?; drives::eject(&d).map_err(err) }).await.map_err(|e| code("imager.ejectFailed", e.to_string()))?
}
// ---- options, profiles (never secrets), cache ----