-
Notifications
You must be signed in to change notification settings - Fork 0
π‘οΈ Sentinel: [CRITICAL/HIGH] Fix TOCTOU vulnerability in file creation #381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ | |
|
|
||
| import Foundation | ||
| import AppKit | ||
| import Darwin | ||
|
|
||
| actor CacheCleaner { | ||
| private let fileManager = FileManager.default | ||
|
|
@@ -255,18 +256,27 @@ actor CacheCleaner { | |
| private func logCleanup(category: String, bytesFreed: Int64) { | ||
| let logDir = FileManager.default.homeDirectoryForCurrentUser | ||
| .appendingPathComponent(".cacheout") | ||
| try? FileManager.default.createDirectory(at: logDir, withIntermediateDirectories: true) | ||
| try? FileManager.default.createDirectory(at: logDir, withIntermediateDirectories: true, attributes: [.posixPermissions: 0o700]) | ||
|
|
||
| let logFile = logDir.appendingPathComponent("cleanup.log") | ||
| let size = ByteCountFormatter.sharedFile.string(fromByteCount: bytesFreed) | ||
| let entry = "[\(ISO8601DateFormatter.shared.string(from: Date()))] Cleaned \(category): \(size)\n" | ||
|
|
||
| if let handle = try? FileHandle(forWritingTo: logFile) { | ||
| handle.seekToEndOfFile() | ||
| handle.write(entry.data(using: .utf8) ?? Data()) | ||
| handle.closeFile() | ||
| } else { | ||
| try? entry.write(to: logFile, atomically: true, encoding: .utf8) | ||
| _ = logFile.withUnsafeFileSystemRepresentation { pathPtr -> Int32 in | ||
| guard let pathPtr = pathPtr else { return -1 } | ||
| let fd = open(pathPtr, O_CREAT | O_WRONLY | O_APPEND | O_NOFOLLOW | O_CLOEXEC, 0o600) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If Useful? React with πΒ / π. |
||
| if fd != -1 { | ||
| let handle = FileHandle(fileDescriptor: fd, closeOnDealloc: true) | ||
| let data = entry.data(using: .utf8) ?? Data() | ||
| if #available(macOS 10.15.4, *) { | ||
| try? handle.write(contentsOf: data) | ||
| try? handle.close() | ||
| } else { | ||
| handle.write(data) | ||
| handle.closeFile() | ||
| } | ||
| } | ||
| return fd | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On upgrades where
~/.cacheoutalready exists from the previous implementation, this call does not change its mode;createDirectoryonly appliesattributesto newly-created directories (the same repo handles this explicitly inDaemonMode.setup). As a result, users with a pre-existing 0755 state directory continue exposingcleanup.logmetadata to other local users even after this security fix, so this path should chmod the directory after creation and similarly handle any existing log file mode.Useful? React with πΒ / π.