diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e34e845..8a97426 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,6 +23,10 @@ jobs: - name: Install Rust run: rustup toolchain install stable --profile minimal --component rustfmt,clippy - run: cargo +stable fmt --check + - name: Shellcheck privileged scripts + # SC2154/SC1090: variables and the Limine library come from the + # prologue that install.rs generates. + run: shellcheck -s bash -S warning -e SC2154,SC1090 src/*.sh tests/fixtures/*.sh - run: cargo +stable clippy --all-targets -- -D warnings - run: cargo +stable test --locked diff --git a/src/boot_transaction.sh b/src/boot_transaction.sh index b19ca97..86b0673 100644 --- a/src/boot_transaction.sh +++ b/src/boot_transaction.sh @@ -60,6 +60,8 @@ finish() { fi if [ "$PACKAGE_LOCK_OWNED" -eq 1 ] && [ "$(cat "$PACKAGE_LOCK" 2>/dev/null)" = "drmcru-$$" ]; then rm -f -- "$PACKAGE_LOCK"; fi if [ -n "$TX" ] && [ "$failed" -eq 0 ]; then rm -rf -- "$TX"; fi + # pkexec reserves 126 (dialog dismissed) and 127 (not authorized). + case "$status" in 126|127) status=1 ;; esac exit "$status" } trap finish EXIT diff --git a/src/install.rs b/src/install.rs index 95472a2..6d13615 100644 --- a/src/install.rs +++ b/src/install.rs @@ -378,6 +378,10 @@ pub enum InstallError { ScriptFailed { code: i32, output: String }, #[error("privileged script was cancelled by the user")] Cancelled, + #[error( + "authorization failed; check the password and that a polkit authentication agent is running: {0}" + )] + NotAuthorized(String), #[error("unsupported automatic Apply environment: {0}")] UnsupportedSystem(String), } @@ -923,13 +927,17 @@ fn run_privileged_script(script: &str) -> Result { output, }) } else { - let code = result.status.code().unwrap_or(-1); - // pkexec returns 126 when the user dismisses the auth dialog - if code == 126 { - Err(InstallError::Cancelled) - } else { - Err(InstallError::ScriptFailed { code, output }) - } + Err(classify_failure(result.status.code().unwrap_or(-1), output)) + } +} + +/// pkexec exits 126 when the dialog is dismissed and 127 when authorization +/// fails; the privileged script maps those codes to 1 so they stay unambiguous. +fn classify_failure(code: i32, output: String) -> InstallError { + match code { + 126 => InstallError::Cancelled, + 127 => InstallError::NotAuthorized(output), + _ => InstallError::ScriptFailed { code, output }, } } diff --git a/src/install_tests.rs b/src/install_tests.rs index 0a3870d..a57e21f 100644 --- a/src/install_tests.rs +++ b/src/install_tests.rs @@ -1094,3 +1094,36 @@ fn preview_summary_has_all_targets() { assert!(lines[7].contains("drm.edid_firmware")); assert!(lines[8].contains("verify")); } + +#[test] +fn pkexec_exit_codes_are_classified() { + assert!(matches!( + classify_failure(126, String::new()), + InstallError::Cancelled + )); + assert!(matches!( + classify_failure(127, String::new()), + InstallError::NotAuthorized(_) + )); + assert!(matches!( + classify_failure(1, String::new()), + InstallError::ScriptFailed { code: 1, .. } + )); +} + +#[test] +fn script_failures_never_use_pkexec_reserved_exit_codes() { + for code in [126, 127] { + let fixture = ScriptFixture::new(); + let before = fixture.boot(); + fixture.command("mkinitcpio", &format!("exit {code}")); + let output = fixture.run(false); + assert_eq!( + output.status.code(), + Some(1), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + assert_eq!(fixture.boot(), before); + } +} diff --git a/src/tui/actions.rs b/src/tui/actions.rs index 23aa61f..46705e6 100644 --- a/src/tui/actions.rs +++ b/src/tui/actions.rs @@ -937,7 +937,7 @@ impl App { return; } - let raw = match fs::read(&path) { + let raw = match read_import_file(&path) { Ok(raw) => raw, Err(error) => { self.status = format!("Import failed: could not read {}: {error}", path.display()); @@ -2187,3 +2187,33 @@ fn polarity_label(positive: bool) -> &'static str { fn checksum_label(valid: bool) -> &'static str { if valid { "ok" } else { "bad" } } + +/// Largest valid EDID: a base block plus 255 extension blocks. +const MAX_IMPORT_LEN: u64 = 256 * 128; + +/// Read an import candidate without blocking on FIFOs or devices and without +/// loading arbitrarily large files into memory. +pub(super) fn read_import_file(path: &Path) -> std::io::Result> { + use std::io::{Error, ErrorKind, Read}; + let metadata = fs::metadata(path)?; + if !metadata.is_file() { + return Err(Error::new(ErrorKind::InvalidInput, "not a regular file")); + } + if metadata.len() > MAX_IMPORT_LEN { + return Err(Error::new( + ErrorKind::InvalidData, + format!("larger than the {MAX_IMPORT_LEN}-byte EDID maximum"), + )); + } + let mut raw = Vec::new(); + fs::File::open(path)? + .take(MAX_IMPORT_LEN + 1) + .read_to_end(&mut raw)?; + if raw.len() as u64 > MAX_IMPORT_LEN { + return Err(Error::new( + ErrorKind::InvalidData, + "file grew while reading", + )); + } + Ok(raw) +} diff --git a/src/tui/mod.rs b/src/tui/mod.rs index ec9e14a..5eda83c 100644 --- a/src/tui/mod.rs +++ b/src/tui/mod.rs @@ -1089,4 +1089,19 @@ mod tests { ); assert!(app.status.contains("blocked")); } + + #[test] + fn import_reads_only_bounded_regular_files() { + let dir = std::env::temp_dir().join(format!("drmcru-import-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let edid = dir.join("edid.bin"); + std::fs::write(&edid, [0u8; 256]).unwrap(); + assert_eq!(actions::read_import_file(&edid).unwrap().len(), 256); + let large = dir.join("large.bin"); + std::fs::write(&large, vec![0u8; 256 * 128 + 1]).unwrap(); + assert!(actions::read_import_file(&large).is_err()); + assert!(actions::read_import_file(&dir).is_err()); + assert!(actions::read_import_file(std::path::Path::new("/dev/zero")).is_err()); + let _ = std::fs::remove_dir_all(&dir); + } }