[pkg-d-commits] [ldc] 26/149: cache: Add files atomically to the cache (copy to temp file + rename), to prevent cache poisoning upon program abort during copying the file into the cache.

Matthias Klumpp mak at moszumanska.debian.org
Sun Apr 23 22:36:54 UTC 2017


This is an automated email from the git hooks/post-receive script.

mak pushed a commit to annotated tag v1.2.0
in repository ldc.

commit c72d976f84a7a63286b6b04a6f8224d44172d921
Author: Johan Engelen <jbc.engelen at gmail.com>
Date:   Sat Jan 7 16:21:47 2017 +0100

    cache: Add files atomically to the cache (copy to temp file + rename), to prevent cache poisoning upon program abort during copying the file into the cache.
---
 driver/cache.cpp | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/driver/cache.cpp b/driver/cache.cpp
index 2e014e9..110f99b 100644
--- a/driver/cache.cpp
+++ b/driver/cache.cpp
@@ -374,14 +374,32 @@ void cacheObjectFile(llvm::StringRef objectFile,
     fatal();
   }
 
+  // To prevent bad cache files, add files to the cache atomically: first copy
+  // to a temporary file and then rename that temp file to the cache entry
+  // filename (rename is atomic).
+
   llvm::SmallString<128> cacheFile;
   storeCacheFileName(cacheObjectHash, cacheFile);
 
-  IF_LOG Logger::println("Copy object file to cache: %s to %s",
-                         objectFile.str().c_str(), cacheFile.c_str());
-  if (llvm::sys::fs::copy_file(objectFile, cacheFile.c_str())) {
+  llvm::SmallString<128> tempFile;
+  if (llvm::sys::fs::createUniqueFile(llvm::Twine(cacheFile) + ".tmp%%%%%%%",
+                                      tempFile)) {
+    error(Loc(), "Could not create name of temporary file in the cache.");
+    fatal();
+  }
+
+  IF_LOG Logger::println("Copy object file to temp file: %s to %s",
+                         objectFile.str().c_str(), tempFile.c_str());
+  if (llvm::sys::fs::copy_file(objectFile, tempFile.c_str())) {
     error(Loc(), "Failed to copy object file to cache: %s to %s",
-          objectFile.str().c_str(), cacheFile.c_str());
+          objectFile.str().c_str(), tempFile.c_str());
+    fatal();
+  }
+  IF_LOG Logger::println("Rename temp file to cache file: %s to %s",
+                         tempFile.c_str(), cacheFile.c_str());
+  if (llvm::sys::fs::rename(tempFile.c_str(), cacheFile.c_str())) {
+    error(Loc(), "Failed to rename temp file to cache file: %s to %s",
+          tempFile.c_str(), cacheFile.c_str());
     fatal();
   }
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-d/ldc.git



More information about the pkg-d-commits mailing list