Skip to content
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

[TUF] Prevent executable from being overwritten #1678

Merged
merged 2 commits into from
Apr 10, 2024
Merged
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
10 changes: 7 additions & 3 deletions ee/tuf/library_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,17 @@ func (ulm *updateLibraryManager) stageAndVerifyUpdate(binary autoupdatableBinary
return stagedUpdatePath, fmt.Errorf("verification failed for target %s staged at %s: %w", targetFilename, stagedUpdatePath, err)
}

// Everything looks good: create the file and write it to disk
out, err := os.Create(stagedUpdatePath)
// Everything looks good: create the file and write it to disk.
// We create the file with 0655 permissions to prevent any other user from writing to this file
// before we can copy to it.
out, err := os.OpenFile(stagedUpdatePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0655)
if err != nil {
return "", fmt.Errorf("could not create file at %s: %w", stagedUpdatePath, err)
}
if _, err := io.Copy(out, &fileBuffer); err != nil {
out.Close()
if err := out.Close(); err != nil {
return stagedUpdatePath, fmt.Errorf("could not write downloaded target %s to file %s and could not close file: %w", targetFilename, stagedUpdatePath, err)
}
return stagedUpdatePath, fmt.Errorf("could not write downloaded target %s to file %s: %w", targetFilename, stagedUpdatePath, err)
}
if err := out.Close(); err != nil {
Expand Down
Loading