Skip to content
Closed
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.
## 2026-05-02 - TOCTOU Vulnerability via Default File Creation Permissions
**Vulnerability:** Files were created with default umask permissions and subsequently locked down using `FileManager.default.setAttributes`, exposing a brief window where unauthorized users could access or replace the file.
**Learning:** High-level Swift APIs like `Data.write(to:)` rely on global process umask, making them vulnerable to Time-of-Check to Time-of-Use (TOCTOU) attacks when handling sensitive data.
**Prevention:** Always use POSIX `open()` with `O_CREAT | O_WRONLY | O_EXCL | O_CLOEXEC` flags and explicitly set secure permissions (e.g., 0600) during file creation, then wrap the resulting file descriptor in a `FileHandle`.
27 changes: 20 additions & 7 deletions Sources/CacheoutHelperLib/SysctlJournal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,27 @@ public final class SysctlJournal {
do {
let data = try PropertyListEncoder().encode(state)

// Write to temp file (non-atomic β€” we control the rename ourselves).
try data.write(to: tmpURL)
// Preemptively remove stale temp files to avoid EEXIST with O_EXCL
try? FileManager.default.removeItem(at: tmpURL)

// Set permissions to 0600 (root-only) on temp file before rename.
try FileManager.default.setAttributes(
[.posixPermissions: 0o600],
ofItemAtPath: tmpURL.path
)
// Securely create temp file with 0600 permissions to prevent TOCTOU
let fd = tmpURL.withUnsafeFileSystemRepresentation { pathPtr -> Int32 in
guard let pathPtr = pathPtr else { return -1 }
return open(pathPtr, O_CREAT | O_WRONLY | O_EXCL | O_CLOEXEC, 0o600)
}

guard fd != -1 else {
throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno), userInfo: nil)
}

let handle = FileHandle(fileDescriptor: fd, closeOnDealloc: true)
if #available(macOS 10.15.4, *) {
try handle.write(contentsOf: data)
try handle.close()
} else {
handle.write(data)
handle.closeFile()
}

// Atomic rename(2) β€” atomicity on APFS/HFS+.
if rename(tmpURL.path, url.path) != 0 {
Expand Down
Loading