-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create output tempfile in a secure way and in same directory as original
This prevents a number of issues: - remove(new) should not be used prior to rename(old, new) in POSIX C. If it fails, the original file was be lost. And a very common way for rename() to fail is when tempfile and original file are on different mountpoints / drives - current dir (or more likely, executable dir) can be read-only, so tempfile must not be created there. This is certainly true for POSIX/Linux installs. - tempfile must have a secure (ie, random and unique) name, and must be opened atomically to avoid race conditions. And add some benefits: - Each original file has its own tempfile, in the same dir, with a matching name This is very convenient to the user as if any error aborts processing, he still can access the result. - Original files (and their directories) must be writable anyway, so it is a sensible choice. - For Linux, all file operations are atomic and race-safe Also, in POSIX, try to preserve input file's owner, group and permissions after applying gain. Since all output is first written to a temporary file, which may be created with different fstat values than the original, these values must be saved and then restored after tempfile is copied over the original.
- Loading branch information
1 parent
fe1e169
commit 6c49eaa
Showing
3 changed files
with
72 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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