Skip to content
Open
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
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/boot_transaction.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -923,13 +927,17 @@ fn run_privileged_script(script: &str) -> Result<InstallReport, InstallError> {
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 },
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/install_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
32 changes: 31 additions & 1 deletion src/tui/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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<Vec<u8>> {
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)
}
15 changes: 15 additions & 0 deletions src/tui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}