address review: open regular files with O_NONBLOCK (per-OS custom_flags) and validate the descriptor; text reads error past the limit

This commit is contained in:
Dylan M. Taylor
2026-11-11 04:49:05 +00:00
parent 781b1640a0
commit 85b0b69271
6 changed files with 99 additions and 37 deletions
+10 -7
View File
@@ -347,15 +347,18 @@ const MAX_TEXT_FILE_BYTES: u64 = 256 << 20;
/// [`MAX_TEXT_FILE_BYTES`] (`/dev/zero` or a FIFO would never end).
pub(crate) fn read_text_file(path: &Path) -> std::io::Result<String> {
use std::io::Read;
let meta = std::fs::metadata(path)?;
if !meta.is_file() || meta.len() > MAX_TEXT_FILE_BYTES {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!("{} is not a text file of at most {} MiB", path.display(), MAX_TEXT_FILE_BYTES >> 20),
));
let too_big =
|| std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{} is not a text file of at most {} MiB", path.display(), MAX_TEXT_FILE_BYTES >> 20));
let (file, meta) = effectcraft_project::open_regular_file(path)?;
if meta.len() > MAX_TEXT_FILE_BYTES {
return Err(too_big());
}
// One byte past the limit: a file that grew after the metadata check is an error, not a prefix.
let mut text = String::new();
std::fs::File::open(path)?.take(MAX_TEXT_FILE_BYTES).read_to_string(&mut text)?;
file.take(MAX_TEXT_FILE_BYTES + 1).read_to_string(&mut text)?;
if text.len() as u64 > MAX_TEXT_FILE_BYTES {
return Err(too_big());
}
Ok(text)
}
+1 -10
View File
@@ -54,16 +54,7 @@ impl Services for FsServices {
/// those never ends (#2134).
fn read_file(&self, path: &str) -> std::io::Result<Vec<u8>> {
use std::io::Read;
// Checked before opening too: opening a FIFO with no writer blocks, so the check after the
// open would never run. shortcut: a FIFO swapped in between still blocks; O_NONBLOCK would close it.
if !std::fs::metadata(path)?.is_file() {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{path} is not a file")));
}
let mut file = std::fs::File::open(path)?;
let meta = file.metadata()?;
if !meta.is_file() {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{path} is not a file")));
}
let (mut file, meta) = effectcraft_project::open_regular_file(std::path::Path::new(path))?;
let mut bytes = Vec::new();
// A sparse file can claim far more than memory: an error, not an abort.
bytes.try_reserve_exact(usize::try_from(meta.len()).unwrap_or(usize::MAX)).map_err(|e| std::io::Error::new(std::io::ErrorKind::OutOfMemory, e))?;
+1 -10
View File
@@ -84,16 +84,7 @@ pub fn is_importable(path: &std::path::Path) -> bool {
/// `/dev/zero` or a FIFO as footage, and reading those never ends (#2134).
pub(crate) fn read_regular_file(path: &std::path::Path) -> std::io::Result<Vec<u8>> {
use std::io::Read;
// Checked before opening too: opening a FIFO with no writer blocks, so the check after the
// open would never run. shortcut: a FIFO swapped in between still blocks; O_NONBLOCK would close it.
if !std::fs::metadata(path)?.is_file() {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{} is not a file", path.display())));
}
let mut file = std::fs::File::open(path)?;
let meta = file.metadata()?;
if !meta.is_file() {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{} is not a file", path.display())));
}
let (mut file, meta) = effectcraft_project::open_regular_file(path)?;
let mut bytes = Vec::new();
// A sparse file can claim far more than memory: an error, not an abort.
bytes.try_reserve_exact(usize::try_from(meta.len()).unwrap_or(usize::MAX)).map_err(|e| std::io::Error::new(std::io::ErrorKind::OutOfMemory, e))?;
+1 -10
View File
@@ -24,16 +24,7 @@ pub(crate) struct FileReader {
#[cfg(any(unix, windows))]
impl FileReader {
pub(crate) fn open(path: &Path) -> std::io::Result<Self> {
// Checked before opening too: opening a FIFO with no writer blocks, so the check after the
// open would never run. shortcut: a FIFO swapped in between still blocks; O_NONBLOCK would close it.
if !std::fs::metadata(path)?.is_file() {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{} is not a file", path.display())));
}
let file = std::fs::File::open(path)?;
let m = file.metadata()?;
if !m.is_file() {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{} is not a file", path.display())));
}
let (file, m) = effectcraft_project::open_regular_file(path)?;
Ok(Self { file, len: m.len() })
}
}
+2
View File
@@ -11,6 +11,8 @@ pub mod essential;
mod file_check;
pub mod limits;
pub mod props;
mod regular_file;
pub use regular_file::open_regular_file;
pub mod render_queue;
pub mod render_templates;
pub mod sequence;
+84
View File
@@ -0,0 +1,84 @@
//! Opening a file that must be a regular file, without ever blocking on a FIFO.
use std::fs::File;
use std::io::{Error, ErrorKind};
use std::path::Path;
/// `O_NONBLOCK` per OS: opening a FIFO read-only with it returns at once instead of waiting for a writer.
/// shortcut: other Unixes keep a blocking open, the `metadata` check before it still covers the common case.
#[cfg(any(target_os = "linux", target_os = "android"))]
const O_NONBLOCK: i32 = 0o4000;
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd", target_os = "dragonfly"))]
const O_NONBLOCK: i32 = 0x4;
/// Opens `path` for reading and returns the file with its metadata, only when it is a regular file.
/// A path swapped for a FIFO between a check and the open cannot hang the caller: the open does not
/// block, and the descriptor is validated before anything reads it.
pub fn open_regular_file(path: &Path) -> std::io::Result<(File, std::fs::Metadata)> {
let not_file = || Error::new(ErrorKind::InvalidInput, format!("{} is not a file", path.display()));
if !std::fs::metadata(path)?.is_file() {
return Err(not_file());
}
let mut options = std::fs::OpenOptions::new();
options.read(true);
#[cfg(all(
unix,
any(
target_os = "linux",
target_os = "android",
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "dragonfly"
)
))]
{
use std::os::unix::fs::OpenOptionsExt;
options.custom_flags(O_NONBLOCK);
}
let file = options.open(path)?;
let meta = file.metadata()?;
if !meta.is_file() {
return Err(not_file());
}
Ok((file, meta))
}
#[cfg(all(test, unix))]
mod tests {
use super::*;
#[test]
fn a_fifo_is_refused_without_blocking_and_a_file_opens() {
let dir = std::env::temp_dir().join(format!("ec-regfile-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let fifo = dir.join("pipe");
let made = std::process::Command::new("mkfifo").arg(&fifo).status().map(|s| s.success()).unwrap_or(false);
let plain = dir.join("plain");
std::fs::write(&plain, b"abc").unwrap();
assert_eq!(open_regular_file(&plain).unwrap().1.len(), 3);
if made {
// metadata already says FIFO; the open itself must not block either.
assert!(open_regular_file(&fifo).is_err());
let mut o = std::fs::OpenOptions::new();
use std::os::unix::fs::OpenOptionsExt;
#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "dragonfly"
))]
{
let f = o.read(true).custom_flags(O_NONBLOCK).open(&fifo).unwrap();
assert!(!f.metadata().unwrap().is_file());
}
}
std::fs::remove_dir_all(&dir).ok();
}
}