From b6698d56ac5ddbbeb39013e192c1489a8b1222a9 Mon Sep 17 00:00:00 2001 From: Jie Date: Mon, 12 Nov 2018 15:26:02 +0800 Subject: [PATCH] Add fsync for renaming user ce key path Device can't start up after the following steps: 1. set screen lock to PIN/Pattern/Password 2. set screen lock to Swipe/None 3. power down immediately after pressing "YES, REMOVE" 4. reboot failed log: Failed to read from /data/misc/vold/user_keys/ce/0/current/keymaster_key_blob root cause: flushing data failed because of power down issue: https://partnerissuetracker.corp.google.com/u/1/issues/119382750 --- FsCrypt.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/FsCrypt.cpp b/FsCrypt.cpp index 087b916..cf179c4 100644 --- a/FsCrypt.cpp +++ b/FsCrypt.cpp @@ -57,6 +57,7 @@ #include #include #include +#include using android::base::StringPrintf; using android::base::WriteStringToFile; @@ -172,8 +173,25 @@ static void fixate_user_ce_key(const std::string& directory_path, const std::str auto const current_path = get_ce_key_current_path(directory_path); if (to_fix != current_path) { LOG(DEBUG) << "Renaming " << to_fix << " to " << current_path; + android::base::unique_fd fd(TEMP_FAILURE_RETRY( + open(to_fix.c_str(), O_RDONLY | O_CLOEXEC))); + if (fd == -1) { + PLOG(ERROR) << "Failed to open " << to_fix; + return; + } + if (fsync(fd) == -1) { + if (errno == EROFS || errno == EINVAL) { + PLOG(WARNING) << "Skip fsync " << to_fix + << " on a file system does not support synchronization"; + } else { + PLOG(ERROR) << "Failed to fsync " << to_fix; + unlink(to_fix.c_str()); + return; + } + } if (rename(to_fix.c_str(), current_path.c_str()) != 0) { PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path; + return; } } }