The console onboarding wrapper needs 0755 to be started by agetty; the manifest check hardcoded 644 for everything except the CLI binary.
60 lines
3.2 KiB
Rust
60 lines
3.2 KiB
Rust
use std::{fs, os::unix::fs::MetadataExt, process::Command};
|
|
|
|
// OSTree does not retain symlink mtimes. Accept only that diagnostic for
|
|
// these package-owned aliases, after independently verifying each link.
|
|
fn ostree_link_times(stderr: &str) -> bool {
|
|
let links = [
|
|
("/usr/bin/archipelagod", "archipelago"),
|
|
("/usr/lib/systemd/system/archipelago-daemon.service", "archipelagod.service"),
|
|
];
|
|
let lines: Vec<_> = stderr.lines().collect();
|
|
!lines.is_empty() && lines.iter().all(|line| links.iter().any(|(path, target)| {
|
|
*line == format!("warning: archipelago: {path} (Modification time mismatch)")
|
|
&& fs::symlink_metadata(path).is_ok_and(|m| m.is_symlink()
|
|
&& m.uid() == 0 && m.gid() == 0 && m.mode() & 0o7777 == 0o777)
|
|
&& fs::read_link(path).is_ok_and(|p| p == std::path::Path::new(target))
|
|
}))
|
|
}
|
|
|
|
pub fn check() -> Result<(), String> {
|
|
let owner = Command::new("pacman").args(["-Qoq", "/usr/bin/archipelago"])
|
|
.output().map_err(|e| e.to_string())?;
|
|
if !owner.status.success() || owner.stdout != b"archipelago\n" {
|
|
return Err("binary is not owned by the archipelago package".into());
|
|
}
|
|
let result = Command::new("pacman").env("LC_ALL", "C").args(["-Qkk", "archipelago"])
|
|
.output().map_err(|e| e.to_string())?;
|
|
print!("{}", String::from_utf8_lossy(&result.stdout));
|
|
eprint!("{}", String::from_utf8_lossy(&result.stderr));
|
|
if !result.status.success() {
|
|
if result.status.code() != Some(1) || !std::path::Path::new("/run/ostree-booted").exists()
|
|
|| !ostree_link_times(&String::from_utf8_lossy(&result.stderr)) {
|
|
return Err("package integrity check failed".into());
|
|
}
|
|
println!("OSTree symlink timestamps differ; alias targets, types, owners and modes verified");
|
|
}
|
|
let manifest = fs::read_to_string("/usr/share/archipelago/permissions.tsv")
|
|
.map_err(|e| e.to_string())?;
|
|
let mut binary_seen = false;
|
|
for line in manifest.lines() {
|
|
let fields: Vec<_> = line.split_whitespace().collect();
|
|
if fields.len() != 4 || !fields[3].starts_with("usr/") || fields[3].split('/').any(|p| p == "..") {
|
|
return Err(format!("invalid permissions entry: {line}"));
|
|
}
|
|
let path = format!("/{}", fields[3]);
|
|
let metadata = fs::symlink_metadata(&path).map_err(|e| format!("{path}: {e}"))?;
|
|
if path == "/usr/bin/archipelago" { binary_seen = true; }
|
|
// Executables live in usr/bin; everything else is data and must not be
|
|
// executable. The console onboarding wrapper is run by agetty, so it is
|
|
// a packaged executable like the CLI itself.
|
|
let expected = if fields[3].starts_with("usr/bin/") { "755" } else { "644" };
|
|
if fields[0] != expected || fields[1] != "0" || fields[2] != "0"
|
|
|| !metadata.is_file() || metadata.uid() != 0 || metadata.gid() != 0
|
|
|| format!("{:o}", metadata.mode() & 0o7777) != expected {
|
|
return Err(format!("incorrect type, ownership or permissions: {path}"));
|
|
}
|
|
}
|
|
if !binary_seen { return Err("permissions manifest omits binary".into()); }
|
|
println!("ARCHIPELAGO_SELF_CHECK_OK: package integrity, root:root ownership and permissions");
|
|
Ok(())
|
|
}
|