Skip to content
Open
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
8 changes: 8 additions & 0 deletions Sources/Cacheout/Cleaner/CacheCleaner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ actor CacheCleaner {
.appendingPathComponent(".cacheout")
try? FileManager.default.createDirectory(at: logDir, withIntermediateDirectories: true, attributes: [.posixPermissions: 0o700])

let dirFd = logDir.withUnsafeFileSystemRepresentation { pathPtr -> Int32 in
guard let pathPtr = pathPtr else { return -1 }
return open(pathPtr, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC)
}
guard dirFd >= 0 else { return }
defer { close(dirFd) }
guard fchmod(dirFd, 0o700) == 0 else { return }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Open the log through the hardened directory fd

When an untrusted local process can rename entries in the user's home directory, this fd is only used for fchmod; the later open(logDir.appendingPathComponent(...)) re-walks ~/.cacheout, so a swap after this line can redirect the log append despite the hardening. Keep using dirFd for cleanup.log (for example with openat) so the file creation is anchored to the directory you just verified.

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


let logFile = logDir.appendingPathComponent("cleanup.log")
let size = ByteCountFormatter.sharedFile.string(fromByteCount: bytesFreed)
let entry = "[\(ISO8601DateFormatter.shared.string(from: Date()))] Cleaned \(category): \(size)\n"
Expand Down
Loading