Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@
**Vulnerability:** External shell command executed in `listLocalSnapshots()` triggered a deadlock when `tmutil` output exceeded 64KB, because stdout and stderr were read synchronously inside the process termination handler.
**Learning:** In Swift, reading from a process pipe synchronously inside a `terminationHandler` can result in a permanent deadlock if the child blocks writing to a full pipe, preventing it from exiting.
**Prevention:** Asynchronously drain pipes continuously while the process is running using background queues.
## 2024-05-24 - TOCTOU symlink attack in chmod
**Vulnerability:** A Time-of-Check to Time-of-Use (TOCTOU) symlink attack when applying `chmod` directly to a file path.
**Learning:** `chmod()` operates on the path and will follow symlinks, which allows attackers to switch the file to a symlink and modify permissions of arbitrary files on the system before `chmod` executes.
**Prevention:** Open the file safely using `open()` with `O_NOFOLLOW`, then use `fchmod()` on the resulting file descriptor.
10 changes: 8 additions & 2 deletions Sources/Cacheout/Headless/DaemonMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,14 @@ public actor DaemonMode: StatusSocket.DataSource {
return
}

// Enforce 0600 permissions
chmod(path, 0o600)
// Enforce 0600 permissions safely to avoid TOCTOU symlink attacks
let fd = open(path, O_RDONLY | O_NOFOLLOW)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Open config with O_NONBLOCK to prevent FIFO hangs

Using open(path, O_RDONLY | O_NOFOLLOW) can block indefinitely when the path is a FIFO with no writer, because read-only open() on FIFOs blocks unless O_NONBLOCK is set. Since fileExists(atPath:) is true for FIFOs, a malicious or accidental replacement of the config path with a named pipe can stall this reload path and prevent config processing.

Useful? React with πŸ‘Β / πŸ‘Ž.

if fd >= 0 {
fchmod(fd, 0o600)
Comment on lines +970 to +972
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid O_RDONLY when opening file for permission repair

Opening the config with open(..., O_RDONLY | O_NOFOLLOW) requires read permission before fchmod can run, so a config owned by the daemon but currently mode 0000/write-only can no longer be repaired to 0600. In that case open fails, permissions remain unchanged, and the subsequent read fails, which is a functional regression from the previous chmod(path, 0o600) behavior that could recover mis-mode files.

Useful? React with πŸ‘Β / πŸ‘Ž.

close(fd)
} else {
logger.error("Failed to safely open config for permissions check at \(path, privacy: .public)")
}

// Read file
Comment on lines +974 to 978
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Stop processing when secure open for chmod fails

If open(..., O_NOFOLLOW) fails (notably on symlinks), this branch only logs an error and then continues to FileManager.default.contents(atPath:), which still reads the path target. That means the code proceeds with config loading even though the permission-enforcement step did not run, so symlinked or otherwise unfixable files bypass the intended 0600 enforcement path.

Useful? React with πŸ‘Β / πŸ‘Ž.

guard let data = FileManager.default.contents(atPath: path) else {
Expand Down
Loading