From 34ee5df55dc11dfc8afff60cf64cd479b639e5a8 Mon Sep 17 00:00:00 2001 From: "T.E.A de Souza" Date: Tue, 24 Nov 2015 14:33:27 +0800 Subject: [PATCH 1/2] Secure implementation of MakeTempDir --- include/caffe/util/io.hpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/include/caffe/util/io.hpp b/include/caffe/util/io.hpp index 6b7332548b0..f9f0f55a5d4 100644 --- a/include/caffe/util/io.hpp +++ b/include/caffe/util/io.hpp @@ -9,6 +9,10 @@ #include "caffe/common.hpp" #include "caffe/proto/caffe.pb.h" +#ifndef CAFFE_TMP_DIR_RETRIES +#define CAFFE_TMP_DIR_RETRIES 100 +#endif + namespace caffe { using ::google::protobuf::Message; @@ -23,12 +27,17 @@ inline void MakeTempFilename(string* temp_filename) { inline void MakeTempDir(string* temp_dirname) { temp_dirname->clear(); - const path& model = boost::filesystem::temp_directory_path() - /"caffe_test.%%%%%%"; - const path& dir = boost::filesystem::unique_path(model).string(); - bool directoryCreated = boost::filesystem::create_directory(dir); - CHECK(directoryCreated); - *temp_dirname = dir.string(); + const path& model = + boost::filesystem::temp_directory_path()/"caffe_test.%%%%-%%%%"; + for ( int i = 0; i < CAFFE_TMP_DIR_RETRIES; i++ ) { + const path& dir = boost::filesystem::unique_path(model).string(); + bool done = boost::filesystem::create_directory(dir); + if ( done ) { + *temp_dirname = dir.string(); + return; + } + } + LOG(FATAL) << "Failed to create a temporary directory."; } bool ReadProtoFromTextFile(const char* filename, Message* proto); From 33905d5a8023c3dbac514dac680060dc608145e8 Mon Sep 17 00:00:00 2001 From: Tea Date: Wed, 25 Nov 2015 11:43:45 +0800 Subject: [PATCH 2/2] Secure temporary file creation --- include/caffe/util/io.hpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/include/caffe/util/io.hpp b/include/caffe/util/io.hpp index f9f0f55a5d4..1a599883ca3 100644 --- a/include/caffe/util/io.hpp +++ b/include/caffe/util/io.hpp @@ -2,12 +2,15 @@ #define CAFFE_UTIL_IO_H_ #include +#include +#include // NOLINT(readability/streams) #include #include "google/protobuf/message.h" #include "caffe/common.hpp" #include "caffe/proto/caffe.pb.h" +#include "caffe/util/format.hpp" #ifndef CAFFE_TMP_DIR_RETRIES #define CAFFE_TMP_DIR_RETRIES 100 @@ -18,13 +21,6 @@ namespace caffe { using ::google::protobuf::Message; using ::boost::filesystem::path; -inline void MakeTempFilename(string* temp_filename) { - temp_filename->clear(); - const path& model = boost::filesystem::temp_directory_path() - /"caffe_test.%%%%%%"; - *temp_filename = boost::filesystem::unique_path(model).string(); -} - inline void MakeTempDir(string* temp_dirname) { temp_dirname->clear(); const path& model = @@ -40,6 +36,19 @@ inline void MakeTempDir(string* temp_dirname) { LOG(FATAL) << "Failed to create a temporary directory."; } +inline void MakeTempFilename(string* temp_filename) { + static path temp_files_subpath; + static uint64_t next_temp_file = 0; + temp_filename->clear(); + if ( temp_files_subpath.empty() ) { + string path_string=""; + MakeTempDir(&path_string); + temp_files_subpath = path_string; + } + *temp_filename = + (temp_files_subpath/caffe::format_int(next_temp_file++, 9)).string(); +} + bool ReadProtoFromTextFile(const char* filename, Message* proto); inline bool ReadProtoFromTextFile(const string& filename, Message* proto) {