-
Notifications
You must be signed in to change notification settings - Fork 4
π‘οΈ Sentinel: [CRITICAL] Fix predictable temporary filename vulnerability #73
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
Closed
himattm
wants to merge
1
commit into
main
from
sentinel-fix-symlink-atomic-writes-3689979196399062848
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ## 2025-05-18 - Prevent Predictable Temporary Filenames during Atomic File Writes | ||
| **Vulnerability:** Found predictable temporary filenames in atomic file writes (e.g., `path + ".tmp"`) using `os.WriteFile` in `internal/burnrate/burnrate.go` and `internal/sparkline/sparkline.go`. This opens up symlink attacks during the predictable write, leading to potential unintended file overrides and privilege escalation. | ||
| **Learning:** It existed because `os.WriteFile` + `os.Rename` is a common but incomplete pattern for atomic file writes. The initial write must go to a file with an unpredictable name to prevent an attacker from creating a symlink before the write happens. | ||
| **Prevention:** Always use `os.CreateTemp` to generate an unpredictable temporary file for the initial write. Ensure it is created in the same directory as the target path (`filepath.Dir(path)`), close the file handle, and perform the `os.Rename` with explicit permission handling (`Chmod`). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -138,11 +138,32 @@ func Save(sessionID, metric string, b *Buffer) { | |
| if err != nil { | ||
| return | ||
| } | ||
| tmp := path + ".tmp" | ||
| if err := os.WriteFile(tmp, data, 0644); err != nil { | ||
|
|
||
| dir := filepath.Dir(path) | ||
| tmpFile, err := os.CreateTemp(dir, "prism-spark-*") | ||
| if err != nil { | ||
| return | ||
| } | ||
| tmpPath := tmpFile.Name() | ||
|
|
||
| if _, err := tmpFile.Write(data); err != nil { | ||
| tmpFile.Close() | ||
| os.Remove(tmpPath) | ||
| return | ||
| } | ||
| os.Rename(tmp, path) | ||
| if err := tmpFile.Chmod(0644); err != nil { | ||
| tmpFile.Close() | ||
| os.Remove(tmpPath) | ||
| return | ||
| } | ||
| if err := tmpFile.Close(); err != nil { | ||
| os.Remove(tmpPath) | ||
| return | ||
| } | ||
|
|
||
| if err := os.Rename(tmpPath, path); err != nil { | ||
| os.Remove(tmpPath) | ||
| } | ||
|
Comment on lines
+142
to
+166
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. Similar to the implementation in dir := filepath.Dir(path)
tmpFile, err := os.CreateTemp(dir, "prism-spark-*")
if err != nil {
return
}
tmpPath := tmpFile.Name()
defer os.Remove(tmpPath)
defer tmpFile.Close()
if _, err := tmpFile.Write(data); err != nil {
return
}
if err := tmpFile.Chmod(0644); err != nil {
return
}
if err := tmpFile.Close(); err != nil {
return
}
if err := os.Rename(tmpPath, path); err != nil {
return
} |
||
| } | ||
|
|
||
| // PushAndSave is a convenience that loads, pushes, saves, and returns the buffer | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The manual cleanup of the temporary file and its handle on every error path is repetitive and error-prone. Using
deferfor cleanup is the idiomatic Go approach; it ensures that the temporary file is removed and the file handle is closed regardless of how the function exits (including panics). Sinceos.Renamemoves the file, the deferredos.Removewill simply become a no-op on success.