-
Notifications
You must be signed in to change notification settings - Fork 4.6k
edmWriteConfigs: don't use unsecure tmpnam(...) #44314
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
Changes from all commits
7531a10
a6a68a1
9338b20
bcc7dae
a97d49f
b2d6d37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -54,6 +54,8 @@ | |||||||||||||||||||||||||||||||||||||||||
| #include <sstream> | ||||||||||||||||||||||||||||||||||||||||||
| #include <fstream> | ||||||||||||||||||||||||||||||||||||||||||
| #include <filesystem> | ||||||||||||||||||||||||||||||||||||||||||
| #include <cstdlib> | ||||||||||||||||||||||||||||||||||||||||||
| #include <unistd.h> | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| static char const* const kHelpOpt = "help"; | ||||||||||||||||||||||||||||||||||||||||||
| static char const* const kHelpCommandOpt = "help,h"; | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -136,18 +138,33 @@ namespace { | |||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| void writeModulesFile() { | ||||||||||||||||||||||||||||||||||||||||||
| bool open_temp(std::string& path, std::ofstream& f) { | ||||||||||||||||||||||||||||||||||||||||||
| path += "/XXXXXX"; | ||||||||||||||||||||||||||||||||||||||||||
| path += '\0'; // ensure that the string is null-terminated | ||||||||||||||||||||||||||||||||||||||||||
| int fd = mkstemp(path.data()); | ||||||||||||||||||||||||||||||||||||||||||
| if (fd != -1) { | ||||||||||||||||||||||||||||||||||||||||||
| f.open(path.c_str(), std::ios_base::trunc | std::ios_base::out); | ||||||||||||||||||||||||||||||||||||||||||
| close(fd); | ||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+141
to
+151
Contributor
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. It would be better to separate the generation of a unique temporary file name and opening of the file. It would also be better to use the exceptions for error handling (since the caller of Also, from C++11 onwards the Please change along
Suggested change
Contributor
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. This approach (using a call to generate the file name, and a separate call to open the file) is not safe: that's the whole point of not using
Contributor
Author
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. Can we use low-level function
Contributor
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.
Why not?
so the file name should be unique among other processes (which is better than the behavior of
). I agree it is possible that something else could tamper with the created file between the
Contributor
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. No, this is exactly the behaviour of With
With
So replacing
Contributor
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. I believe there can be a difference between Written that, given that the best solution would be to move the generation of
Contributor
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. Ah... yes, I see what you mean. |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| bool writeModulesFile() { | ||||||||||||||||||||||||||||||||||||||||||
|
Contributor
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. With exceptions the function signature can return to
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| if (std::filesystem::exists(std::filesystem::current_path() / "modules.py")) | ||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||
| std::array<char, L_tmpnam> buffer; | ||||||||||||||||||||||||||||||||||||||||||
| std::tmpnam(buffer.data()); | ||||||||||||||||||||||||||||||||||||||||||
| std::ofstream file{buffer.data()}; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| file << "from FWCore.ParameterSet.ModulesProxy import _setupProxies\n" | ||||||||||||||||||||||||||||||||||||||||||
| "locals().update(_setupProxies(__file__))\n"; | ||||||||||||||||||||||||||||||||||||||||||
| file.close(); | ||||||||||||||||||||||||||||||||||||||||||
| std::filesystem::copy(buffer.data(), "modules.py"); | ||||||||||||||||||||||||||||||||||||||||||
| std::filesystem::remove(buffer.data()); | ||||||||||||||||||||||||||||||||||||||||||
| std::ofstream file; | ||||||||||||||||||||||||||||||||||||||||||
| std::string path(std::filesystem::current_path()); | ||||||||||||||||||||||||||||||||||||||||||
| bool res = open_temp(path, file); | ||||||||||||||||||||||||||||||||||||||||||
| if (res) { | ||||||||||||||||||||||||||||||||||||||||||
| file << "from FWCore.ParameterSet.ModulesProxy import _setupProxies\n" | ||||||||||||||||||||||||||||||||||||||||||
| "locals().update(_setupProxies(__file__))\n"; | ||||||||||||||||||||||||||||||||||||||||||
| file.close(); | ||||||||||||||||||||||||||||||||||||||||||
| std::filesystem::copy(path.data(), "modules.py"); | ||||||||||||||||||||||||||||||||||||||||||
| std::filesystem::remove(path.data()); | ||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+156
to
+167
Contributor
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. Now this could become
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } // namespace | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -304,7 +321,11 @@ int main(int argc, char** argv) try { | |||||||||||||||||||||||||||||||||||||||||
| std::set<std::string> usedCfiFileNames; | ||||||||||||||||||||||||||||||||||||||||||
| edm::for_all(pluginNames, std::bind(&writeCfisForPlugin, _1, factory, std::ref(usedCfiFileNames))); | ||||||||||||||||||||||||||||||||||||||||||
| if (not pluginNames.empty()) { | ||||||||||||||||||||||||||||||||||||||||||
| writeModulesFile(); | ||||||||||||||||||||||||||||||||||||||||||
| bool res = writeModulesFile(); | ||||||||||||||||||||||||||||||||||||||||||
| if (!res) { | ||||||||||||||||||||||||||||||||||||||||||
| std::cerr << "writeModulesFile() failed" << std::endl; | ||||||||||||||||||||||||||||||||||||||||||
| return 1; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+324
to
+328
Contributor
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. Now this would go back to
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
| } catch (cms::Exception& iException) { | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.