-
Notifications
You must be signed in to change notification settings - Fork 4
π‘οΈ Sentinel: [CRITICAL] Fix predictable temp file symlink attack in atomic file writes #69
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
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-24 - Predictable Temp File Creation Symlink Attack | ||
| **Vulnerability:** Found uses of predictable temporary file names in `os.TempDir()` (e.g., `tmpPath := path + ".tmp"`) followed by `os.WriteFile` in `internal/burnrate/burnrate.go` and `internal/sparkline/sparkline.go`. This allows for local symlink attacks where an attacker can overwrite arbitrary files by creating a symlink at the predictable path before the application writes to it. | ||
| **Learning:** These existed because predictable `.tmp` extensions were concatenated onto predictable file paths for intermediate atomic writes instead of generating cryptographically random filenames. | ||
| **Prevention:** Use `os.CreateTemp` to safely create unpredictable temporary files. Always use `filepath.Dir(path)` as the first argument to ensure the temporary file is created on the same filesystem/device as the target file, which avoids cross-device rename errors. Also, be sure to `Chmod` appropriately and `Close` the file descriptor before calling `os.Rename`. |
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,21 @@ func Save(sessionID, metric string, b *Buffer) { | |
| if err != nil { | ||
| return | ||
| } | ||
| tmp := path + ".tmp" | ||
| if err := os.WriteFile(tmp, data, 0644); err != nil { | ||
| f, err := os.CreateTemp(filepath.Dir(path), "prism-spark-*.tmp") | ||
| if err != nil { | ||
| return | ||
| } | ||
| tmp := f.Name() | ||
| if _, err := f.Write(data); err != nil { | ||
| f.Close() | ||
| os.Remove(tmp) | ||
| return | ||
| } | ||
| os.Rename(tmp, path) | ||
| f.Chmod(0644) | ||
| f.Close() | ||
|
Comment on lines
+146
to
+152
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. Consider adding if _, err := f.Write(data); err != nil {
f.Close()
os.Remove(tmp)
return
}
if err := f.Sync(); err != nil {
f.Close()
os.Remove(tmp)
return
}
if err := f.Chmod(0644); err != nil {
f.Close()
os.Remove(tmp)
return
}
if err := f.Close(); err != nil {
os.Remove(tmp)
return
} |
||
| if err := os.Rename(tmp, path); err != nil { | ||
| os.Remove(tmp) | ||
| } | ||
| } | ||
|
|
||
| // 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.
To ensure the atomic write is durable and complete, it is recommended to call
f.Sync()before closing the file, and to check the error returned byf.Close(). Iff.Close()fails, it may indicate that the data was not successfully flushed to disk, and the subsequentos.Renamecould result in a corrupted or incomplete file.