H: never open a token path that is not a regular file
Check is_file before opening and open with O_NONBLOCK, so a FIFO token file cannot park a blocking-pool thread on every check and proxy call.
This commit is contained in:
1 parent
f9911f6c70
commit
a78303c123
1 file changed
+41
-4
+41
-4
@@ -5,6 +5,7 @@
|
||||
//! and nothing here keeps the bytes.
|
||||
|
||||
use std::io::Read;
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::peers::error::PeerError;
|
||||
@@ -41,14 +42,27 @@ pub fn load_token_at(
|
||||
credentials_dir: Option<&Path>,
|
||||
) -> Result<Secret, PeerError> {
|
||||
let path = &peer.token_file;
|
||||
let file = std::fs::File::open(path)
|
||||
.map_err(|err| PeerError::TokenFile(format!("not readable: {err}")))?;
|
||||
let meta = file
|
||||
.metadata()
|
||||
// Checked before opening: opening a FIFO waits for a writer, which would
|
||||
// park a blocking-pool thread on every check and proxy call. `metadata`
|
||||
// follows symlinks, so the mode check and the exemption below look at the
|
||||
// file that is actually read.
|
||||
let meta = std::fs::metadata(path)
|
||||
.map_err(|err| PeerError::TokenFile(format!("not readable: {err}")))?;
|
||||
if !meta.is_file() {
|
||||
return Err(PeerError::TokenFile("not a file".into()));
|
||||
}
|
||||
// O_NONBLOCK closes the window between the check above and the open: a
|
||||
// path swapped for a FIFO yields an unreadable file instead of a wait.
|
||||
let file = std::fs::OpenOptions::new()
|
||||
.read(true)
|
||||
.custom_flags(libc::O_NONBLOCK)
|
||||
.open(path)
|
||||
.map_err(|err| PeerError::TokenFile(format!("not readable: {err}")))?;
|
||||
// The open descriptor's metadata, so the mode that is checked is the one
|
||||
// on the file that is read.
|
||||
let meta = file
|
||||
.metadata()
|
||||
.map_err(|err| PeerError::TokenFile(format!("not readable: {err}")))?;
|
||||
if !under_credentials(path, credentials_dir) {
|
||||
reject_group_or_world_accessible(&meta)?;
|
||||
}
|
||||
@@ -255,6 +269,29 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fifo_is_refused_without_waiting_for_a_writer() {
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
|
||||
let dir = TempDir::new("peer-secret");
|
||||
let fifo = dir.path().join("fifo");
|
||||
let c_path = std::ffi::CString::new(fifo.as_os_str().as_bytes()).unwrap();
|
||||
// SAFETY: mkfifo only reads the path string; the mode is a literal.
|
||||
assert_eq!(unsafe { libc::mkfifo(c_path.as_ptr(), 0o600) }, 0);
|
||||
|
||||
// Opening a FIFO for reading blocks until a writer appears, which
|
||||
// would park a blocking-pool thread on every check and proxy call.
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let peer = peer(fifo);
|
||||
std::thread::spawn(move || {
|
||||
let _ = tx.send(token_err(load_token_at(&peer, None).unwrap_err()));
|
||||
});
|
||||
let err = rx
|
||||
.recv_timeout(std::time::Duration::from_secs(5))
|
||||
.expect("the token loader must not wait on a fifo");
|
||||
assert_eq!(err, "not a file");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_symlink_cannot_borrow_the_credentials_directory_exemption() {
|
||||
let dir = TempDir::new("peer-secret");
|
||||
|
||||
Reference in new issue
Block a user