Fsync directories after creating files

Bug: 120248692
Test: adb shell locksettings set-pin 1111 && \
    adb shell "echo b > /proc/sysrq-trigger"
Change-Id: I53d252942c21365983b4f8b6e0948b1864f195c1
This commit is contained in:
Paul Crowley 2018-12-07 15:36:09 -08:00
parent 2374693556
commit 621d9b9732
4 changed files with 23 additions and 16 deletions

View file

@ -173,27 +173,12 @@ 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); auto const current_path = get_ce_key_current_path(directory_path);
if (to_fix != current_path) { if (to_fix != current_path) {
LOG(DEBUG) << "Renaming " << to_fix << " to " << 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) { if (rename(to_fix.c_str(), current_path.c_str()) != 0) {
PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path; PLOG(WARNING) << "Unable to rename " << to_fix << " to " << current_path;
return; return;
} }
} }
android::vold::FsyncDirectory(directory_path);
} }
static bool read_and_fixate_user_ce_key(userid_t user_id, static bool read_and_fixate_user_ce_key(userid_t user_id,
@ -588,6 +573,7 @@ bool fscrypt_add_user_key_auth(userid_t user_id, int serial, const std::string&
std::string ce_key_path; std::string ce_key_path;
if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false; if (!get_ce_key_new_path(directory_path, paths, &ce_key_path)) return false;
if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, auth, ce_key)) return false; if (!android::vold::storeKeyAtomically(ce_key_path, user_key_temp, auth, ce_key)) return false;
if (!android::vold::FsyncDirectory(directory_path)) return false;
return true; return true;
} }

View file

@ -484,6 +484,7 @@ bool storeKey(const std::string& dir, const KeyAuthentication& auth, const KeyBu
if (!encryptWithoutKeymaster(appId, key, &encryptedKey)) return false; if (!encryptWithoutKeymaster(appId, key, &encryptedKey)) return false;
} }
if (!writeStringToFile(encryptedKey, dir + "/" + kFn_encrypted_key)) return false; if (!writeStringToFile(encryptedKey, dir + "/" + kFn_encrypted_key)) return false;
if (!FsyncDirectory(dir)) return false;
return true; return true;
} }

View file

@ -776,5 +776,23 @@ status_t WaitForFile(const char* filename, std::chrono::nanoseconds timeout) {
return -1; return -1;
} }
bool FsyncDirectory(const std::string& dirname) {
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dirname.c_str(), O_RDONLY | O_CLOEXEC)));
if (fd == -1) {
PLOG(ERROR) << "Failed to open " << dirname;
return false;
}
if (fsync(fd) == -1) {
if (errno == EROFS || errno == EINVAL) {
PLOG(WARNING) << "Skip fsync " << dirname
<< " on a file system does not support synchronization";
} else {
PLOG(ERROR) << "Failed to fsync " << dirname;
return false;
}
}
return true;
}
} // namespace vold } // namespace vold
} // namespace android } // namespace android

View file

@ -131,6 +131,8 @@ status_t UnmountTree(const std::string& prefix);
status_t WaitForFile(const char* filename, std::chrono::nanoseconds timeout); status_t WaitForFile(const char* filename, std::chrono::nanoseconds timeout);
bool FsyncDirectory(const std::string& dirname);
} // namespace vold } // namespace vold
} // namespace android } // namespace android