use std::fs::File;
use std::io::Read;
use std::os::unix::fs::MetadataExt;
use std::path::Path;
use zeroize::Zeroizing;
#[cfg(test)]
pub(crate) const REDACTED: &str = "Zeroizing { .. }";
pub(crate) const REFUSED_BITS: u32 = 0o027;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Refusal {
NotAFile,
Owner {
process: u32,
},
Mode {
mode: u32,
},
}
pub(crate) fn judge(is_file: bool, mode: u32, owner: u32, process: u32) -> Option<Refusal> {
if !is_file {
return Some(Refusal::NotAFile);
}
if owner != process && owner != 0 && process != 0 {
return Some(Refusal::Owner { process });
}
let mode = mode & 0o7777;
if mode & REFUSED_BITS != 0 {
return Some(Refusal::Mode { mode });
}
None
}
#[derive(Debug)]
pub(crate) enum Problem {
Io(std::io::Error),
Refused(Refusal),
}
pub(crate) fn check_open(path: &Path) -> Result<File, Problem> {
if !std::fs::metadata(path).map_err(Problem::Io)?.is_file() {
return Err(Problem::Refused(Refusal::NotAFile));
}
let file = File::open(path).map_err(Problem::Io)?;
let meta = file.metadata().map_err(Problem::Io)?;
let process = unsafe { libc::geteuid() };
match judge_metadata(&meta, process) {
Some(refusal) => Err(Problem::Refused(refusal)),
None => Ok(file),
}
}
pub(crate) fn judge_metadata(meta: &std::fs::Metadata, process: u32) -> Option<Refusal> {
judge(meta.is_file(), meta.mode(), meta.uid(), process)
}
pub(crate) fn describe(key: &str, path: &Path, refusal: Refusal) -> String {
let file = path.display();
match refusal {
Refusal::NotAFile => format!("{key} {file}: not a regular file"),
Refusal::Owner { process } => format!(
"{key} {file}: the file is owned by another user, not by the user this runs as \
(uid {process}) or root; run `chown {process} {file}` (or `chown root`)"
),
Refusal::Mode { mode } => format!(
"{key} {file}: other users may read or change the file (mode {mode:04o}); run \
`chmod 600 {file}`, or `chmod 640 {file}` for a group that only reads it"
),
}
}
pub(crate) fn open(path: &Path, key: &str) -> Result<File, String> {
check_open(path).map_err(|problem| match problem {
Problem::Io(e) => format!("{key} {}: {e}", path.display()),
Problem::Refused(refusal) => describe(key, path, refusal),
})
}
pub(crate) fn read(path: &Path, key: &str) -> Result<Zeroizing<Vec<u8>>, String> {
let mut file = open(path, key)?;
let size = file.metadata().map(|m| m.len()).unwrap_or(0);
let mut bytes = Zeroizing::new(Vec::with_capacity(
usize::try_from(size).unwrap_or(0).saturating_add(1),
));
file.read_to_end(&mut bytes)
.map_err(|e| format!("{key} {}: {e}", path.display()))?;
Ok(bytes)
}
#[cfg(test)]
mod tests {
use super::*;
use std::os::unix::fs::PermissionsExt;
const ME: u32 = 1000;
const OTHER: u32 = 1234;
const ROOT: u32 = 0;
fn mode(m: u32) -> Option<Refusal> {
judge(true, m, ME, ME)
}
#[test]
fn mode_0600_passes() {
assert_eq!(mode(0o600), None, "0600 gives no one else access");
}
#[test]
fn mode_0400_passes() {
assert_eq!(mode(0o400), None, "0400 gives no one else access");
}
#[test]
fn mode_0640_passes_for_a_group_that_reads_it() {
assert_eq!(mode(0o640), None, "0640: the group may only read");
}
#[test]
fn mode_0700_passes_since_owner_execute_gives_no_one_else_access() {
assert_eq!(mode(0o700), None, "owner execute gives no one else access");
}
#[test]
fn a_file_root_owns_passes_for_another_user() {
assert_eq!(
judge(true, 0o640, ROOT, ME),
None,
"root may own a secret another user reads"
);
}
#[test]
fn a_process_running_as_root_may_read_a_file_another_user_owns() {
assert_eq!(
judge(true, 0o600, OTHER, ROOT),
None,
"root may read a file any user owns"
);
}
#[test]
fn mode_0644_is_refused() {
assert_eq!(
mode(0o644),
Some(Refusal::Mode { mode: 0o644 }),
"others may read 0644"
);
}
#[test]
fn mode_0604_is_refused() {
assert_eq!(
mode(0o604),
Some(Refusal::Mode { mode: 0o604 }),
"others may read 0604"
);
}
#[test]
fn mode_0660_is_refused_since_the_group_could_replace_the_secret() {
assert_eq!(
mode(0o660),
Some(Refusal::Mode { mode: 0o660 }),
"the group may replace a 0660 file"
);
}
#[test]
fn mode_0620_is_refused_for_group_write_alone() {
assert_eq!(
mode(0o620),
Some(Refusal::Mode { mode: 0o620 }),
"the group may replace a 0620 file"
);
}
#[test]
fn mode_0602_is_refused_for_others_write_alone() {
assert_eq!(
mode(0o602),
Some(Refusal::Mode { mode: 0o602 }),
"others may write 0602"
);
}
#[test]
fn mode_0601_is_refused_for_others_execute_alone() {
assert_eq!(
mode(0o601),
Some(Refusal::Mode { mode: 0o601 }),
"others hold the execute bit of 0601"
);
}
#[test]
fn a_file_another_user_owns_is_refused() {
assert_eq!(
judge(true, 0o600, OTHER, ME),
Some(Refusal::Owner { process: ME }),
"another user could replace the file"
);
}
#[test]
fn a_file_that_is_not_a_regular_file_is_refused() {
assert_eq!(
judge(false, 0o600, ME, ME),
Some(Refusal::NotAFile),
"a directory, FIFO or device is not a file"
);
}
#[test]
fn group_execute_alone_passes() {
assert_eq!(
mode(0o610),
None,
"group execute alone gives no read or write"
);
}
#[test]
fn mode_0000_passes_the_rule_and_fails_at_the_read() {
assert_eq!(
mode(0o000),
None,
"no access for anyone else; the read fails later"
);
}
#[test]
fn a_setuid_bit_is_not_access_for_anyone_else() {
assert_eq!(mode(0o4600), None, "setuid grants no one else access");
}
#[test]
fn the_file_type_bits_of_a_full_st_mode_are_ignored() {
assert_eq!(
mode(0o100_600),
None,
"the file type bits of st_mode are not permission bits"
);
assert_eq!(
mode(0o100_644),
Some(Refusal::Mode { mode: 0o644 }),
"the file type bits are dropped and 0644 is still refused"
);
}
#[test]
fn not_a_file_is_reported_before_the_owner_and_the_mode() {
assert_eq!(
judge(false, 0o666, OTHER, ME),
Some(Refusal::NotAFile),
"a non-file is refused before its owner or mode is looked at"
);
}
#[test]
fn the_owner_is_reported_before_the_mode() {
assert_eq!(
judge(true, 0o644, OTHER, ME),
Some(Refusal::Owner { process: ME }),
"the owner is checked before the mode"
);
}
#[test]
fn a_mode_refusal_names_the_key_the_file_the_mode_and_the_chmod_to_run() {
let text = describe(
"master_key",
Path::new("/etc/vcon-server/master.key"),
Refusal::Mode { mode: 0o644 },
);
assert_eq!(
text,
"master_key /etc/vcon-server/master.key: other users may read or change the file \
(mode 0644); run `chmod 600 /etc/vcon-server/master.key`, or `chmod 640 \
/etc/vcon-server/master.key` for a group that only reads it",
"the text names the key, the file, the mode and both fixes"
);
}
#[test]
fn an_owner_refusal_names_the_process_user_and_the_chown_to_run() {
let text = describe(
"tls.key",
Path::new("/k.pem"),
Refusal::Owner { process: ME },
);
assert_eq!(
text,
"tls.key /k.pem: the file is owned by another user, not by the user this runs as \
(uid 1000) or root; run `chown 1000 /k.pem` (or `chown root`)",
"the text names the process's uid and the chown, not the owner's uid"
);
}
#[test]
fn a_not_a_file_refusal_names_the_key_and_the_file() {
assert_eq!(
describe("key_file", Path::new("/d"), Refusal::NotAFile),
"key_file /d: not a regular file",
"the text names the key and the file"
);
}
#[test]
fn a_setuid_mode_is_shown_with_its_special_bits() {
let text = describe("k", Path::new("/f"), Refusal::Mode { mode: 0o4644 });
assert!(
text.contains("(mode 4644)"),
"the mode is shown with its special bits"
);
}
fn file(dir: &tempfile::TempDir, name: &str, bytes: &[u8], m: u32) -> std::path::PathBuf {
let path = dir.path().join(name);
std::fs::write(&path, bytes).expect("write");
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(m)).expect("chmod");
path
}
#[test]
fn a_real_file_s_owner_is_read_from_its_metadata() {
let dir = tempfile::tempdir().expect("dir");
let path = file(&dir, "s", b"x", 0o600);
let meta = std::fs::metadata(&path).expect("metadata");
let owner = std::os::unix::fs::MetadataExt::uid(&meta);
let process = owner + 1;
assert_eq!(
judge_metadata(&meta, process),
Some(Refusal::Owner { process }),
"the file's own uid is read, not the process's"
);
assert_eq!(
judge_metadata(&meta, owner),
None,
"the same file passes for its owner"
);
}
#[test]
fn a_real_file_s_mode_is_read_from_its_metadata() {
let dir = tempfile::tempdir().expect("dir");
let path = file(&dir, "s", b"x", 0o604);
let meta = std::fs::metadata(&path).expect("metadata");
let owner = std::os::unix::fs::MetadataExt::uid(&meta);
assert_eq!(
judge_metadata(&meta, owner),
Some(Refusal::Mode { mode: 0o604 }),
"the file's own mode is read"
);
}
#[test]
fn a_0600_file_is_read_whole() {
let dir = tempfile::tempdir().expect("dir");
let path = file(&dir, "s", b"the secret\n", 0o600);
assert_eq!(
read(&path, "k").expect("read").as_slice(),
b"the secret\n",
"the file's bytes come back whole"
);
}
#[test]
fn a_0640_file_is_read() {
let dir = tempfile::tempdir().expect("dir");
let path = file(&dir, "s", b"x", 0o640);
assert!(read(&path, "k").is_ok(), "a group may read a 0640 file");
}
#[test]
fn a_0644_file_is_refused_with_its_key_and_path() {
let dir = tempfile::tempdir().expect("dir");
let path = file(&dir, "s", b"x", 0o644);
let err = read(&path, "storage.s3.secret_access_key_file").expect_err("refused");
assert!(
err.starts_with(&format!(
"storage.s3.secret_access_key_file {}: other users",
path.display()
)),
"{err}"
);
assert!(err.contains("(mode 0644)"), "{err}");
}
#[test]
fn a_refusal_never_holds_the_secret() {
let dir = tempfile::tempdir().expect("dir");
let path = file(&dir, "s", b"s3cr3t-value-0123", 0o644);
let err = read(&path, "k").expect_err("refused");
assert!(!err.contains("s3cr3t"), "{err}");
}
#[test]
fn a_symlink_to_a_0600_file_is_followed_and_read() {
let dir = tempfile::tempdir().expect("dir");
let target = file(&dir, "real", b"via link", 0o600);
let link = dir.path().join("link");
std::os::unix::fs::symlink(&target, &link).expect("symlink");
assert_eq!(
read(&link, "k").expect("read").as_slice(),
b"via link",
"the link's target is read"
);
}
#[test]
fn a_symlink_to_a_0644_file_is_refused_by_the_target_s_mode() {
let dir = tempfile::tempdir().expect("dir");
let target = file(&dir, "real", b"x", 0o644);
let link = dir.path().join("link");
std::os::unix::fs::symlink(&target, &link).expect("symlink");
let err = read(&link, "k").expect_err("refused");
assert!(
err.starts_with(&format!("k {}: other users", link.display())),
"{err}"
);
}
#[test]
fn a_dangling_symlink_is_refused_as_missing() {
let dir = tempfile::tempdir().expect("dir");
let link = dir.path().join("link");
std::os::unix::fs::symlink(dir.path().join("gone"), &link).expect("symlink");
let err = read(&link, "k").expect_err("refused");
assert!(err.contains("No such file or directory"), "{err}");
}
#[test]
fn a_missing_file_names_the_key_the_path_and_the_os_error() {
let err = read(Path::new("/nonexistent/vcon-secret"), "master_key").expect_err("refused");
assert_eq!(
err, "master_key /nonexistent/vcon-secret: No such file or directory (os error 2)",
"the text names the key, the path and the OS error"
);
}
#[test]
fn a_directory_is_refused_as_not_a_regular_file() {
let dir = tempfile::tempdir().expect("dir");
let err = read(dir.path(), "k").expect_err("refused");
assert_eq!(
err,
format!("k {}: not a regular file", dir.path().display()),
"a directory is refused by kind"
);
}
#[test]
fn a_fifo_is_refused_without_blocking() {
let dir = tempfile::tempdir().expect("dir");
let fifo = dir.path().join("fifo");
let made = std::process::Command::new("mkfifo")
.arg(&fifo)
.status()
.expect("mkfifo");
assert!(made.success(), "mkfifo made the FIFO");
let err = read(&fifo, "k").expect_err("refused");
assert!(err.ends_with("not a regular file"), "{err}");
}
#[test]
fn a_0000_file_is_refused_at_the_open() {
let dir = tempfile::tempdir().expect("dir");
let path = file(&dir, "s", b"x", 0o000);
if unsafe { libc::geteuid() } == 0 {
assert!(read(&path, "k").is_ok(), "root opens a 0000 file");
} else {
let err = read(&path, "k").expect_err("refused");
assert!(err.contains("Permission denied"), "{err}");
}
}
const SECRET: &[u8] = b"s3cr3t-0123456789";
fn shows_secret(text: &str) -> bool {
let decimal = SECRET
.iter()
.map(u8::to_string)
.collect::<Vec<_>>()
.join(", ");
text.contains("s3cr3t") || text.contains(&decimal[..12])
}
#[test]
fn the_redaction_text_is_pinned() {
assert_eq!(
REDACTED, "Zeroizing { .. }",
"zeroize's Debug of a held secret"
);
let dir = tempfile::tempdir().expect("dir");
let path = file(&dir, "s", SECRET, 0o600);
let bytes = read(&path, "k").expect("read");
assert!(
format!("{bytes:?}") == REDACTED,
"Debug of a secret file's bytes is the redaction text alone"
);
}
#[test]
fn debug_of_a_read_secret_holds_no_byte_of_it() {
let dir = tempfile::tempdir().expect("dir");
let path = file(&dir, "s", SECRET, 0o600);
let bytes = read(&path, "k").expect("read");
assert!(
!shows_secret(&format!("{bytes:?}")),
"Debug showed the secret"
);
assert!(&bytes[..] == SECRET, "the bytes are still read whole");
}
#[test]
fn an_expect_err_on_a_read_that_passed_does_not_log_the_secret() {
let dir = tempfile::tempdir().expect("dir");
let path = file(&dir, "s", SECRET, 0o600);
let panic = std::panic::catch_unwind(|| read(&path, "k").expect_err("refused"))
.expect_err("expect_err panics on a read that passed");
let message = panic
.downcast_ref::<String>()
.cloned()
.expect("the panic message is a String");
assert!(
message.starts_with("refused: "),
"the panic is expect_err's"
);
assert!(!shows_secret(&message), "the panic showed the secret");
}
#[test]
fn alternate_debug_of_a_read_secret_is_redacted_too() {
let dir = tempfile::tempdir().expect("dir");
let path = file(&dir, "s", SECRET, 0o600);
let bytes = read(&path, "k").expect("read");
let shown = format!("{bytes:#?}");
assert!(
shown.starts_with("Zeroizing {") && !shows_secret(&shown),
"{{:#?}} redacts too"
);
}
#[test]
fn debug_of_an_empty_secret_file_does_not_say_it_is_empty() {
let dir = tempfile::tempdir().expect("dir");
let path = file(&dir, "s", b"", 0o600);
let bytes = read(&path, "k").expect("read");
assert!(
format!("{bytes:?}") == REDACTED,
"an empty secret reads as any other: no length, no `[]`"
);
}
#[test]
fn an_owner_refusal_of_a_real_secret_file_holds_no_byte_of_it() {
let dir = tempfile::tempdir().expect("dir");
let path = file(&dir, "s", SECRET, 0o600);
let meta = std::fs::metadata(&path).expect("metadata");
let owner = std::os::unix::fs::MetadataExt::uid(&meta);
let refusal = judge_metadata(&meta, owner + 1).expect("another user's file is refused");
let text = describe("k", &path, refusal);
assert!(
!text.contains(&format!("uid {owner},")) && !text.contains(&format!("uid {owner} ")),
"the owner's uid is not in the text"
);
assert!(
text.contains(&format!("(uid {}) or root", owner + 1)),
"names the process's uid"
);
assert!(!shows_secret(&text), "the refusal showed the secret");
}
}