Skip to content

Commit 1f9fc5b

Browse files
committed
fix(sandbox): open proxy-auth file non-blocking to reject FIFOs promptly
read_upstream_proxy_credential_file opened the path with a blocking File::open before the regular-file check, so a configured FIFO with no writer would block open() indefinitely — hanging sandbox creation on the driver and supervisor startup. Open with O_NONBLOCK on Unix so the open returns immediately, then reject the non-regular file as before; O_NONBLOCK has no effect on the later read of a regular file. Adds a mkfifo regression asserting the reader returns promptly with a non-regular-file error instead of hanging. Signed-off-by: Philippe Martin <[email protected]>
1 parent 4524ac9 commit 1f9fc5b

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/openshell-core/Cargo.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ base64 = { workspace = true }
2828
chrono = { version = "0.4", default-features = false, features = ["clock", "std"], optional = true }
2929
reqwest = { workspace = true, features = ["blocking", "rustls-tls-webpki-roots"], optional = true }
3030

31+
[target.'cfg(unix)'.dependencies]
32+
nix = { workspace = true }
33+
3134
[features]
3235
default = ["telemetry"]
3336
## Compile in anonymous telemetry emission support. On by default; disable with

‎crates/openshell-core/src/driver_utils.rs‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,21 @@ pub const MAX_UPSTREAM_PROXY_CREDENTIAL_BYTES: u64 = 4096;
320320
pub fn read_upstream_proxy_credential_file(path: &str) -> Result<String, String> {
321321
use std::io::Read as _;
322322

323-
let file = std::fs::File::open(path)
324-
.map_err(|e| format!("failed to open proxy auth file '{path}': {e}"))?;
323+
// On Unix, open non-blocking so a FIFO with no writer does not hang the
324+
// open() call indefinitely; the regular-file check below then rejects it.
325+
// O_NONBLOCK has no effect on the subsequent read of a regular file.
326+
#[cfg(unix)]
327+
let open_result = {
328+
use std::os::unix::fs::OpenOptionsExt as _;
329+
std::fs::OpenOptions::new()
330+
.read(true)
331+
.custom_flags(nix::libc::O_NONBLOCK)
332+
.open(path)
333+
};
334+
#[cfg(not(unix))]
335+
let open_result = std::fs::File::open(path);
336+
337+
let file = open_result.map_err(|e| format!("failed to open proxy auth file '{path}': {e}"))?;
325338
let metadata = file
326339
.metadata()
327340
.map_err(|e| format!("failed to stat proxy auth file '{path}': {e}"))?;
@@ -637,4 +650,23 @@ mod tests {
637650
let err = read_upstream_proxy_credential_file("/nonexistent/proxy-auth").unwrap_err();
638651
assert!(err.contains("open proxy auth file"), "{err}");
639652
}
653+
654+
#[cfg(unix)]
655+
#[test]
656+
fn credential_file_rejects_fifo_without_hanging() {
657+
// A FIFO with no writer would block a blocking open() forever. The
658+
// reader opens non-blocking and rejects the non-regular file, so it
659+
// must return promptly even though nothing ever opens the write end.
660+
let dir = tempfile::tempdir().unwrap();
661+
let fifo = dir.path().join("proxy-auth-fifo");
662+
nix::unistd::mkfifo(&fifo, nix::sys::stat::Mode::S_IRUSR).unwrap();
663+
664+
let start = std::time::Instant::now();
665+
let err = read_upstream_proxy_credential_file(fifo.to_str().unwrap()).unwrap_err();
666+
assert!(err.contains("regular file"), "{err}");
667+
assert!(
668+
start.elapsed() < std::time::Duration::from_secs(5),
669+
"reading a FIFO must not block"
670+
);
671+
}
640672
}

0 commit comments

Comments
 (0)