Skip to content
Closed
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
14 changes: 10 additions & 4 deletions Sources/Cacheout/Headless/DaemonMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,22 @@ public actor DaemonMode: StatusSocket.DataSource {
// Ensure state directory exists with 0700 permissions.
// createDirectory only sets attributes on newly created dirs, so we
// explicitly chmod afterward to harden pre-existing directories.
// Use O_NOFOLLOW | O_DIRECTORY + fchmod on the resulting fd so a
// concurrent symlink swap at `stateDir` can't redirect the chmod target.
do {
try FileManager.default.createDirectory(
at: config.stateDir,
withIntermediateDirectories: true,
attributes: [.posixPermissions: 0o700]
)
try FileManager.default.setAttributes(
[.posixPermissions: 0o700],
ofItemAtPath: config.stateDir.path
)
let dirFd = open(config.stateDir.path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC)
guard dirFd >= 0 else {
throw NSError(domain: "DaemonMode", code: Int(errno))
}
defer { close(dirFd) }
guard fchmod(dirFd, 0o700) == 0 else {
throw NSError(domain: "DaemonMode", code: Int(errno))
}
} catch {
logger.error("Failed to create/secure state directory: \(error.localizedDescription, privacy: .public)")
Foundation.exit(1)
Expand Down
Loading