From 53b29d8a47326a4613d772fd8d175c626f5d345c Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 18 Jun 2026 06:20:57 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Sentinel:=20[CRITICAL]?= =?UTF-8?q?=20Fix=20TOCTOU=20symlink=20attack=20in=20permission=20hardenin?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: acebytes <2820910+acebytes@users.noreply.github.com> --- Sources/Cacheout/Headless/DaemonMode.swift | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Sources/Cacheout/Headless/DaemonMode.swift b/Sources/Cacheout/Headless/DaemonMode.swift index 23b86f7..92a8d6f 100644 --- a/Sources/Cacheout/Headless/DaemonMode.swift +++ b/Sources/Cacheout/Headless/DaemonMode.swift @@ -295,10 +295,17 @@ public actor DaemonMode: StatusSocket.DataSource { withIntermediateDirectories: true, attributes: [.posixPermissions: 0o700] ) - try FileManager.default.setAttributes( - [.posixPermissions: 0o700], - ofItemAtPath: config.stateDir.path - ) + let fd = config.stateDir.withUnsafeFileSystemRepresentation { pathPtr -> Int32 in + guard let pathPtr = pathPtr else { return -1 } + return open(pathPtr, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC) + } + guard fd >= 0 else { + throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno), userInfo: nil) + } + defer { close(fd) } + guard fchmod(fd, 0o700) == 0 else { + throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno), userInfo: nil) + } } catch { logger.error("Failed to create/secure state directory: \(error.localizedDescription, privacy: .public)") Foundation.exit(1)