Skip to content

Commit 27c2eee

Browse files
authored
Merge pull request #1206 from durable-workflow/fix/server-951-admission-owner
Check storage-admission observation ownership
2 parents 7d7ad9d + cd59a48 commit 27c2eee

2 files changed

Lines changed: 59 additions & 5 deletions

File tree

‎docs/storage-admission.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ history pruning, and payload cleanup do not start a new pass under pressure.
7070

7171
## Deployment Requirements
7272

73+
### Admission observation file
74+
75+
Publish the observation as a regular file owned by root or the Server's
76+
effective UID, with group and other write permissions disabled (for example,
77+
`0644` or `0600`). For containers, use the ownership as seen inside the Server
78+
container. Keep the collector's atomic replacement in the same trusted directory
79+
and preserve those owner and mode requirements on every replacement.
80+
81+
Every parent directory must be controlled by root or the Server's user and must
82+
not be writable by untrusted users. Avoid symlinked parent components. A private
83+
directory such as `/run/durable-workflow` is suitable when provisioned with that
84+
ownership and access policy. The native reader refuses a symlink in the final
85+
component and checks the opened file descriptor; it does not establish trust in
86+
the ancestor directories. File checks cannot protect an operator-selected path
87+
whose parent can be replaced by another user.
88+
7389
### External Completion Payloads
7490

7591
When discovery advertises `upload.completion_context`, a worker may retry a

‎rust/src/runtime/storage_admission.rs‎

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ fn read(settings: &Settings, now: i64) -> Snapshot {
117117
if !(1..=300).contains(&settings.max_age_seconds) {
118118
return Snapshot::UNAVAILABLE;
119119
}
120-
let Some(value) = observation(path) else {
120+
// The observation must come from root or this process's effective user.
121+
let Some(value) = observation(path, unsafe { libc::geteuid() }) else {
121122
return Snapshot::UNAVAILABLE;
122123
};
123124
let state = match value.get("state").and_then(Value::as_str) {
@@ -146,16 +147,19 @@ fn read(settings: &Settings, now: i64) -> Snapshot {
146147
}
147148
}
148149

149-
/// A bounded regular file, never a FIFO, device or symlink, nor one another
150-
/// user could replace. The final path component is opened without following
150+
/// A bounded regular file owned by root or the reader, never a FIFO, device
151+
/// or symlink, nor writable by another user. Trusted parent directories remain
152+
/// an operator requirement. The final path component is opened without following
151153
/// a symlink and without blocking on a FIFO, and every check is made on that
152154
/// same descriptor, so nothing can be swapped in between a check and the read.
153-
fn observation(path: &str) -> Option<Value> {
155+
fn observation(path: &str, reader_uid: u32) -> Option<Value> {
154156
use std::os::unix::fs::OpenOptionsExt;
155157
let mut file = std::fs::OpenOptions::new().read(true)
156158
.custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_CLOEXEC).open(path).ok()?;
157159
let opened = file.metadata().ok()?;
158-
if !opened.file_type().is_file() || opened.len() > MAX_BYTES || opened.mode() & 0o022 != 0 {
160+
if !opened.file_type().is_file() || opened.len() > MAX_BYTES || opened.mode() & 0o022 != 0
161+
|| (opened.uid() != 0 && opened.uid() != reader_uid)
162+
{
159163
return None;
160164
}
161165
let mut bytes = Vec::with_capacity(MAX_BYTES as usize);
@@ -338,6 +342,40 @@ mod tests {
338342
unavailable(&Settings { max_age_seconds: 301, ..base.clone() });
339343
}
340344

345+
#[test]
346+
fn observation_owner_must_be_root_or_the_reader() {
347+
let directory = tempfile::tempdir().unwrap();
348+
let path = directory.path().join("pressure.json");
349+
let path_text = path.to_str().unwrap();
350+
let reader_uid = unsafe { libc::geteuid() };
351+
write(&path, &record("normal", NOW, "{}"));
352+
assert!(read(&settings(&path), NOW).accepts_new_work());
353+
354+
if reader_uid == 0 {
355+
// In the privileged container, exercise a real foreign-owned
356+
// descriptor through the production snapshot path as well.
357+
std::os::unix::fs::chown(&path, Some(65534), None).unwrap();
358+
assert_eq!(read(&settings(&path), NOW), Snapshot::UNAVAILABLE);
359+
}
360+
let owner_uid = std::fs::metadata(&path).unwrap().uid();
361+
assert_ne!(owner_uid, 0);
362+
// This also exercises the descriptor policy on unprivileged runners,
363+
// without requiring permission to change an inode's owner.
364+
let other_uid = if owner_uid == 1 { 2 } else { 1 };
365+
assert!(observation(path_text, owner_uid).is_some());
366+
assert!(observation(path_text, other_uid).is_none());
367+
368+
if reader_uid == 0 {
369+
std::os::unix::fs::chown(&path, Some(0), None).unwrap();
370+
// A root-owned observation is trusted by a non-root reader too.
371+
assert!(observation(path_text, other_uid).is_some());
372+
assert!(read(&settings(&path), NOW).accepts_new_work());
373+
}
374+
// Atomic producer replacement restores the actual reader's ownership.
375+
write(&path, &record("draining", NOW, "{}"));
376+
assert_eq!(read(&settings(&path), NOW).state, State::Draining);
377+
}
378+
341379
#[test]
342380
fn routes_follow_php_s_admission_classes() {
343381
for (method, path, expected) in [

0 commit comments

Comments
 (0)