From: Benjamin Kramer Date: Thu, 10 Aug 2017 17:38:41 +0000 (+0000) Subject: [gold-plugin] Avoid race condition when creating temporary files. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=01823a62a4a150698e097483d1a465812c937279;p=llvm [gold-plugin] Avoid race condition when creating temporary files. This is both a potential security issue and a potential functionality issue because we create temporary files from multiple threads. Use the safe version of createTemporaryFile instead. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@310636 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/tools/gold/gold-plugin.cpp b/tools/gold/gold-plugin.cpp index 825fff1aa38..3123cde7950 100644 --- a/tools/gold/gold-plugin.cpp +++ b/tools/gold/gold-plugin.cpp @@ -699,11 +699,12 @@ static void recordFile(std::string Filename, bool TempOutFile) { /// Return the desired output filename given a base input name, a flag /// indicating whether a temp file should be generated, and an optional task id. /// The new filename generated is returned in \p NewFilename. -static void getOutputFileName(SmallString<128> InFilename, bool TempOutFile, - SmallString<128> &NewFilename, int TaskID) { +static int getOutputFileName(SmallString<128> InFilename, bool TempOutFile, + SmallString<128> &NewFilename, int TaskID) { + int FD = -1; if (TempOutFile) { std::error_code EC = - sys::fs::createTemporaryFile("lto-llvm", "o", NewFilename); + sys::fs::createTemporaryFile("lto-llvm", "o", FD, NewFilename); if (EC) message(LDPL_FATAL, "Could not create temporary file: %s", EC.message().c_str()); @@ -711,7 +712,13 @@ static void getOutputFileName(SmallString<128> InFilename, bool TempOutFile, NewFilename = InFilename; if (TaskID > 0) NewFilename += utostr(TaskID); + std::error_code EC = + sys::fs::openFileForWrite(NewFilename, FD, sys::fs::F_None); + if (EC) + message(LDPL_FATAL, "Could not open file %s: %s", NewFilename.c_str(), + EC.message().c_str()); } + return FD; } static CodeGenOpt::Level getCGOptLevel() { @@ -899,14 +906,8 @@ static ld_plugin_status allSymbolsReadHook() { auto AddStream = [&](size_t Task) -> std::unique_ptr { IsTemporary[Task] = !SaveTemps; - getOutputFileName(Filename, /*TempOutFile=*/!SaveTemps, Filenames[Task], - Task); - int FD; - std::error_code EC = - sys::fs::openFileForWrite(Filenames[Task], FD, sys::fs::F_None); - if (EC) - message(LDPL_FATAL, "Could not open file %s: %s", Filenames[Task].c_str(), - EC.message().c_str()); + int FD = getOutputFileName(Filename, /*TempOutFile=*/!SaveTemps, + Filenames[Task], Task); return llvm::make_unique( llvm::make_unique(FD, true)); };