GITv

Restore the keys file's SELinux label after add-key

Browse files

add-key publishes the keys file by rename, so the new file took its directory's default label instead of the one the policy names for it (ssh_home_t for /etc/gitv/authorized_keys on the server). Where SELinux is on and restorecon is installed, add-key now relabels the file, and reports the command to run if that fails. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Author
Um9i vulgarmessiah@gmail.com
Date
22 Nov 2026, 16:40
Commit
dd59c34fb098
Parent
44b30d1

1 file changed +48−1

@@ -477,7 +477,27 @@ fn add_key(owner: &str, file: &Path) -> Result<(), String> {
}
// The key counts as added only once the rename itself survives a crash.
gitv_core::sync_directory_of(&keys)
.map_err(|e| format!("added the key, but couldn't sync {}: {e}", directory.display()))
.map_err(|e| format!("added the key, but couldn't sync {}: {e}", directory.display()))?;
relabel(&keys, Path::new("/sys/fs/selinux"), Path::new("/usr/sbin/restorecon"))
}
/// Give a file the SELinux label the system's policy names for its path. A file published by
/// rename carries the label its directory gives new files, not the one the policy names (sshd
/// reads authorized keys labelled `ssh_home_t`), so without this every added key would leave
/// the keys file mislabelled. Does nothing where SELinux is off or restorecon isn't installed.
fn relabel(path: &Path, selinuxfs: &Path, restorecon: &Path) -> Result<(), String> {
if !selinuxfs.join("enforce").exists() || !restorecon.is_file() {
return Ok(());
}
let status = Command::new(restorecon).arg(path).stdin(Stdio::null()).status();
match status {
Ok(status) if status.success() => Ok(()),
outcome => Err(format!(
"added the key, but couldn't give {} its SELinux label ({}); run: restorecon {0}",
path.display(),
outcome.map_or_else(|e| e.to_string(), |s| s.to_string())
)),
}
}
/// The decoded Ed25519 key in an authorized_keys line, wherever it sits: after options, and
@@ -826,6 +846,33 @@ mod tests {
open(path, OFlag::O_DIRECTORY | OFlag::O_CLOEXEC, Mode::empty()).unwrap()
}
#[test]
fn published_files_are_relabelled_only_where_selinux_is_on() {
let dir = std::env::temp_dir().join(format!("gitv-relabel-{}", std::process::id()));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(dir.join("selinuxfs")).unwrap();
let keys = dir.join("authorized_keys");
fs::write(&keys, "").unwrap();
// A stand-in restorecon that records what it was asked to relabel.
let record = dir.join("relabelled");
let fake = dir.join("restorecon");
fs::write(&fake, format!("#!/bin/sh\necho \"$1\" >> '{}'\n", record.display())).unwrap();
fs::set_permissions(&fake, fs::Permissions::from_mode(0o755)).unwrap();
relabel(&keys, &dir.join("selinuxfs"), &fake).unwrap();
assert!(!record.exists(), "SELinux is off (no enforce file): nothing to do");
relabel(&keys, &dir.join("selinuxfs"), &dir.join("missing")).unwrap();
fs::write(dir.join("selinuxfs/enforce"), "1").unwrap();
relabel(&keys, &dir.join("selinuxfs"), &fake).unwrap();
assert_eq!(fs::read_to_string(&record).unwrap().trim(), keys.to_str().unwrap());
fs::write(&fake, "#!/bin/sh\nexit 1\n").unwrap();
let error = relabel(&keys, &dir.join("selinuxfs"), &fake).unwrap_err();
assert!(error.contains("run: restorecon"), "{error}");
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn creation_errors_keep_their_leftovers() {
let leftover = || "couldn't remove the staging directory /x".to_string();