-
Notifications
You must be signed in to change notification settings - Fork 751
UTL: unique file name per process #9143
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: master
Are you sure you want to change the base?
Conversation
Signed-off-by: osamahammad21 <[email protected]>
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.
Code Review
This pull request aims to make temporary filenames unique per process by embedding the process ID, which is a good step to prevent collisions in concurrent environments. However, the change introduces a critical issue by breaking existing unit tests that rely on the old filename format. Additionally, a pre-existing high-severity security vulnerability (TOCTOU) remains in the filename generation logic. I have provided detailed comments on both of these points.
| { | ||
| int counter = 1; | ||
| std::string filename; | ||
| const auto pid = static_cast<unsigned int>(::getpid()); | ||
| do { | ||
| filename = fmt::format("{}.{}", prefix, counter++); | ||
| filename = fmt::format("{}.{}.{}", prefix, pid, counter++); | ||
| } while (std::filesystem::exists(filename)); | ||
|
|
||
| return filename; |
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.
This change to the filename format will break existing tests. The tests stream_handler_temp_file_handling and file_handler_temp_file_handling in src/utl/test/cpp/TestCFileUtils.cpp construct an expected temporary filename (e.g., test_temp_file_handling.txt.1) which will no longer match the new format (test_temp_file_handling.txt.<pid>.1). The tests need to be updated to work with the new filename generation logic.
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.
Neat 😄
| { | ||
| int counter = 1; | ||
| std::string filename; | ||
| const auto pid = static_cast<unsigned int>(::getpid()); | ||
| do { | ||
| filename = fmt::format("{}.{}", prefix, counter++); | ||
| filename = fmt::format("{}.{}.{}", prefix, pid, counter++); | ||
| } while (std::filesystem::exists(filename)); | ||
|
|
||
| return filename; |
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.
While adding the process ID is a good improvement to prevent filename collisions between concurrent processes, this function is still vulnerable to a Time-of-check to time-of-use (TOCTOU) race condition. There is a time window between the check for file existence (std::filesystem::exists) and the file's actual creation by the caller. An attacker could exploit this by creating a symbolic link with the predicted filename, potentially causing the application to write to an unintended file. A more secure approach is to use mkstemp, which atomically creates a unique file, as is already done in the ScopedTemporaryFile class.
|
clang-tidy review says "All clean, LGTM! 👍" |
Signed-off-by: osamahammad21 <[email protected]>
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.
clang-tidy made some suggestions
| { | ||
| const char* filename = "test_temp_file_handling.txt"; | ||
| std::string tmp_filename = std::string(filename) + ".1"; | ||
| const auto pid = static_cast<unsigned int>(::getpid()); |
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.
warning: no header providing "getpid" is directly included [misc-include-cleaner]
src/utl/test/cpp/TestCFileUtils.cpp:3:
- #include <cstdint>
+ #include <unistd.h>
+ #include <cstdint>Signed-off-by: osamahammad21 <[email protected]>
|
clang-tidy review says "All clean, LGTM! 👍" |
No description provided.