summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandroid-build-team Robot <android-build-team-robot@google.com>2017-11-09 21:35:09 +0000
committerandroid-build-team Robot <android-build-team-robot@google.com>2017-11-09 21:35:09 +0000
commit9a90d236fb1a2077bd1315f7ec0e41ffe520b3d5 (patch)
tree4fe3a5eb9a71694d79a4756c1dd85f66542a3d52
parent33483d4d6a0effce02332afefe17b5c4d49af0d3 (diff)
parent7ce9f66723ff5f2fd2704ee2fd37e56b72e19bea (diff)
downloadvold-oreo-m2-release.tar.gz
Change-Id: I030f6bb3bc3acea74dd1457b8ed8222b40316f9d
-rw-r--r--KeyStorage.cpp22
1 files changed, 21 insertions, 1 deletions
diff --git a/KeyStorage.cpp b/KeyStorage.cpp
index 9d615550..20b23915 100644
--- a/KeyStorage.cpp
+++ b/KeyStorage.cpp
@@ -35,6 +35,7 @@
#include <android-base/file.h>
#include <android-base/logging.h>
+#include <android-base/unique_fd.h>
#include <cutils/properties.h>
@@ -153,10 +154,29 @@ static bool readFileToString(const std::string& filename, std::string* result) {
}
static bool writeStringToFile(const std::string& payload, const std::string& filename) {
- if (!android::base::WriteStringToFile(payload, filename)) {
+ android::base::unique_fd fd(TEMP_FAILURE_RETRY(
+ open(filename.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0666)));
+ if (fd == -1) {
+ PLOG(ERROR) << "Failed to open " << filename;
+ return false;
+ }
+ if (!android::base::WriteStringToFd(payload, fd)) {
PLOG(ERROR) << "Failed to write to " << filename;
+ unlink(filename.c_str());
return false;
}
+ // fsync as close won't guarantee flush data
+ // see close(2), fsync(2) and b/68901441
+ if (fsync(fd) == -1) {
+ if (errno == EROFS || errno == EINVAL) {
+ PLOG(WARNING) << "Skip fsync " << filename
+ << " on a file system does not support synchronization";
+ } else {
+ PLOG(ERROR) << "Failed to fsync " << filename;
+ unlink(filename.c_str());
+ return false;
+ }
+ }
return true;
}