2016-06-02 20:04:27 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "MetadataCrypt.h"
|
2018-09-18 22:30:21 +02:00
|
|
|
#include "KeyBuffer.h"
|
2016-06-02 20:04:27 +02:00
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
#include <algorithm>
|
2016-06-02 20:04:27 +02:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2018-12-14 10:08:10 +01:00
|
|
|
#include <android-base/file.h>
|
2016-06-02 20:04:27 +02:00
|
|
|
#include <android-base/logging.h>
|
2018-01-30 18:48:19 +01:00
|
|
|
#include <android-base/properties.h>
|
2017-06-16 18:21:18 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2018-01-30 18:48:19 +01:00
|
|
|
#include <cutils/fs.h>
|
2016-06-02 20:04:27 +02:00
|
|
|
#include <fs_mgr.h>
|
2019-05-13 22:02:54 +02:00
|
|
|
#include <libdm/dm.h>
|
2016-06-02 20:04:27 +02:00
|
|
|
|
2018-08-28 10:58:49 +02:00
|
|
|
#include "Checkpoint.h"
|
2016-06-02 20:04:27 +02:00
|
|
|
#include "EncryptInplace.h"
|
|
|
|
#include "KeyStorage.h"
|
|
|
|
#include "KeyUtil.h"
|
2018-12-14 10:08:10 +01:00
|
|
|
#include "Keymaster.h"
|
2016-06-02 20:04:27 +02:00
|
|
|
#include "Utils.h"
|
|
|
|
#include "VoldUtil.h"
|
|
|
|
|
|
|
|
#define TABLE_LOAD_RETRIES 10
|
|
|
|
|
2019-01-29 23:34:01 +01:00
|
|
|
using android::fs_mgr::FstabEntry;
|
|
|
|
using android::fs_mgr::GetEntryForMountPoint;
|
2017-08-01 18:15:53 +02:00
|
|
|
using android::vold::KeyBuffer;
|
2019-05-13 22:02:54 +02:00
|
|
|
using namespace android::dm;
|
2017-08-01 18:15:53 +02:00
|
|
|
|
2016-06-02 20:04:27 +02:00
|
|
|
static const std::string kDmNameUserdata = "userdata";
|
|
|
|
|
2018-12-14 10:08:10 +01:00
|
|
|
static const char* kFn_keymaster_key_blob = "keymaster_key_blob";
|
|
|
|
static const char* kFn_keymaster_key_blob_upgraded = "keymaster_key_blob_upgraded";
|
|
|
|
|
2016-06-02 20:04:27 +02:00
|
|
|
static bool mount_via_fs_mgr(const char* mount_point, const char* blk_device) {
|
|
|
|
// fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
|
|
|
|
// partitions in the fsck domain.
|
2019-01-30 09:03:14 +01:00
|
|
|
if (setexeccon(android::vold::sFsckContext)) {
|
2016-06-02 20:04:27 +02:00
|
|
|
PLOG(ERROR) << "Failed to setexeccon";
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-29 23:34:01 +01:00
|
|
|
auto mount_rc = fs_mgr_do_mount(&fstab_default, const_cast<char*>(mount_point),
|
2018-08-28 10:58:49 +02:00
|
|
|
const_cast<char*>(blk_device), nullptr,
|
|
|
|
android::vold::cp_needsCheckpoint());
|
2016-06-02 20:04:27 +02:00
|
|
|
if (setexeccon(nullptr)) {
|
|
|
|
PLOG(ERROR) << "Failed to clear setexeccon";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mount_rc != 0) {
|
|
|
|
LOG(ERROR) << "fs_mgr_do_mount failed with rc " << mount_rc;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOG(DEBUG) << "Mounted " << mount_point;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-14 10:08:10 +01:00
|
|
|
namespace android {
|
|
|
|
namespace vold {
|
|
|
|
|
|
|
|
// Note: It is possible to orphan a key if it is removed before deleting
|
|
|
|
// Update this once keymaster APIs change, and we have a proper commit.
|
2018-12-18 20:10:31 +01:00
|
|
|
static void commit_key(const std::string& dir) {
|
2018-12-14 10:08:10 +01:00
|
|
|
while (!android::base::WaitForProperty("vold.checkpoint_committed", "1")) {
|
|
|
|
LOG(ERROR) << "Wait for boot timed out";
|
|
|
|
}
|
|
|
|
Keymaster keymaster;
|
|
|
|
auto keyPath = dir + "/" + kFn_keymaster_key_blob;
|
|
|
|
auto newKeyPath = dir + "/" + kFn_keymaster_key_blob_upgraded;
|
|
|
|
std::string key;
|
|
|
|
|
|
|
|
if (!android::base::ReadFileToString(keyPath, &key)) {
|
|
|
|
LOG(ERROR) << "Failed to read old key: " << dir;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rename(newKeyPath.c_str(), keyPath.c_str()) != 0) {
|
|
|
|
PLOG(ERROR) << "Unable to move upgraded key to location: " << keyPath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!keymaster.deleteKey(key)) {
|
|
|
|
LOG(ERROR) << "Key deletion failed during upgrade, continuing anyway: " << dir;
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Old Key deleted: " << dir;
|
|
|
|
}
|
|
|
|
|
2019-01-29 23:34:01 +01:00
|
|
|
static bool read_key(const FstabEntry& data_rec, bool create_if_absent, KeyBuffer* key) {
|
|
|
|
if (data_rec.key_dir.empty()) {
|
2016-06-02 20:04:27 +02:00
|
|
|
LOG(ERROR) << "Failed to get key_dir";
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-29 23:34:01 +01:00
|
|
|
std::string key_dir = data_rec.key_dir;
|
2018-12-14 10:08:10 +01:00
|
|
|
std::string sKey;
|
2016-06-02 20:04:27 +02:00
|
|
|
auto dir = key_dir + "/key";
|
2018-05-09 22:01:16 +02:00
|
|
|
LOG(DEBUG) << "key_dir/key: " << dir;
|
|
|
|
if (fs_mkdirs(dir.c_str(), 0700)) {
|
2018-01-30 18:48:19 +01:00
|
|
|
PLOG(ERROR) << "Creating directories: " << dir;
|
2018-05-09 22:01:16 +02:00
|
|
|
return false;
|
2018-01-30 18:48:19 +01:00
|
|
|
}
|
2016-06-02 20:04:27 +02:00
|
|
|
auto temp = key_dir + "/tmp";
|
2018-12-14 10:08:10 +01:00
|
|
|
auto newKeyPath = dir + "/" + kFn_keymaster_key_blob_upgraded;
|
|
|
|
/* If we have a leftover upgraded key, delete it.
|
|
|
|
* We either failed an update and must return to the old key,
|
|
|
|
* or we rebooted before commiting the keys in a freak accident.
|
|
|
|
* Either way, we can re-upgrade the key if we need to.
|
|
|
|
*/
|
|
|
|
Keymaster keymaster;
|
|
|
|
if (pathExists(newKeyPath)) {
|
|
|
|
if (!android::base::ReadFileToString(newKeyPath, &sKey))
|
|
|
|
LOG(ERROR) << "Failed to read old key: " << dir;
|
|
|
|
else if (!keymaster.deleteKey(sKey))
|
|
|
|
LOG(ERROR) << "Old key deletion failed, continuing anyway: " << dir;
|
|
|
|
else
|
|
|
|
unlink(newKeyPath.c_str());
|
|
|
|
}
|
|
|
|
bool needs_cp = cp_needsCheckpoint();
|
|
|
|
if (!android::vold::retrieveKey(create_if_absent, dir, temp, key, needs_cp)) return false;
|
|
|
|
if (needs_cp && pathExists(newKeyPath)) std::thread(commit_key, dir).detach();
|
2016-06-02 20:04:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-14 10:08:10 +01:00
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
static bool get_number_of_sectors(const std::string& real_blkdev, uint64_t* nr_sec) {
|
2018-05-23 10:50:46 +02:00
|
|
|
if (android::vold::GetBlockDev512Sectors(real_blkdev, nr_sec) != android::OK) {
|
2016-06-02 20:04:27 +02:00
|
|
|
PLOG(ERROR) << "Unable to measure size of " << real_blkdev;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool create_crypto_blk_dev(const std::string& dm_name, uint64_t nr_sec,
|
2019-05-13 22:02:54 +02:00
|
|
|
const std::string& real_blkdev, const KeyBuffer& key,
|
2016-06-02 20:04:27 +02:00
|
|
|
std::string* crypto_blkdev) {
|
2019-05-13 22:02:54 +02:00
|
|
|
auto& dm = DeviceMapper::Instance();
|
2016-06-02 20:04:27 +02:00
|
|
|
|
2019-05-13 22:02:54 +02:00
|
|
|
KeyBuffer hex_key_buffer;
|
|
|
|
if (android::vold::StrToHex(key, hex_key_buffer) != android::OK) {
|
|
|
|
LOG(ERROR) << "Failed to turn key to hex";
|
2016-06-02 20:04:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-13 22:02:54 +02:00
|
|
|
std::string hex_key(hex_key_buffer.data(), hex_key_buffer.size());
|
2016-06-02 20:04:27 +02:00
|
|
|
|
2019-05-13 22:02:54 +02:00
|
|
|
DmTable table;
|
|
|
|
table.Emplace<DmTargetDefaultKey>(0, nr_sec, "AES-256-XTS", hex_key, real_blkdev, 0);
|
2016-06-02 20:04:27 +02:00
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
for (int i = 0;; i++) {
|
2019-05-13 22:02:54 +02:00
|
|
|
if (dm.CreateDevice(dm_name, table)) {
|
2016-06-02 20:04:27 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-09-18 22:30:21 +02:00
|
|
|
if (i + 1 >= TABLE_LOAD_RETRIES) {
|
2019-05-13 22:02:54 +02:00
|
|
|
LOG(ERROR) << "Could not create default-key device " << dm_name;
|
2016-06-02 20:04:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-13 22:02:54 +02:00
|
|
|
PLOG(INFO) << "Could not create default-key device, retrying";
|
2016-06-02 20:04:27 +02:00
|
|
|
usleep(500000);
|
|
|
|
}
|
|
|
|
|
2019-05-13 22:02:54 +02:00
|
|
|
if (!dm.GetDmDevicePathByName(dm_name, crypto_blkdev)) {
|
|
|
|
LOG(ERROR) << "Cannot retrieve default-key device status " << dm_name;
|
2016-06-02 20:04:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-25 23:44:33 +02:00
|
|
|
bool fscrypt_mount_metadata_encrypted(const std::string& blk_device, const std::string& mount_point,
|
|
|
|
bool needs_encrypt) {
|
2018-10-23 22:06:55 +02:00
|
|
|
LOG(DEBUG) << "fscrypt_mount_metadata_encrypted: " << mount_point << " " << needs_encrypt;
|
2018-01-30 18:48:19 +01:00
|
|
|
auto encrypted_state = android::base::GetProperty("ro.crypto.state", "");
|
|
|
|
if (encrypted_state != "") {
|
2018-10-23 22:06:55 +02:00
|
|
|
LOG(DEBUG) << "fscrypt_enable_crypto got unexpected starting state: " << encrypted_state;
|
2016-06-02 20:04:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-29 23:34:01 +01:00
|
|
|
|
|
|
|
auto data_rec = GetEntryForMountPoint(&fstab_default, mount_point);
|
2016-06-02 20:04:27 +02:00
|
|
|
if (!data_rec) {
|
|
|
|
LOG(ERROR) << "Failed to get data_rec";
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-30 18:48:19 +01:00
|
|
|
KeyBuffer key;
|
2019-01-29 23:34:01 +01:00
|
|
|
if (!read_key(*data_rec, needs_encrypt, &key)) return false;
|
2016-06-02 20:04:27 +02:00
|
|
|
uint64_t nr_sec;
|
|
|
|
if (!get_number_of_sectors(data_rec->blk_device, &nr_sec)) return false;
|
|
|
|
std::string crypto_blkdev;
|
2019-06-25 23:44:33 +02:00
|
|
|
if (!create_crypto_blk_dev(kDmNameUserdata, nr_sec, blk_device, key, &crypto_blkdev))
|
2016-06-02 20:04:27 +02:00
|
|
|
return false;
|
2019-06-25 23:44:33 +02:00
|
|
|
|
2018-01-30 18:48:19 +01:00
|
|
|
// FIXME handle the corrupt case
|
|
|
|
if (needs_encrypt) {
|
|
|
|
LOG(INFO) << "Beginning inplace encryption, nr_sec: " << nr_sec;
|
|
|
|
off64_t size_already_done = 0;
|
2019-06-25 23:44:33 +02:00
|
|
|
auto rc = cryptfs_enable_inplace(crypto_blkdev.data(), blk_device.data(), nr_sec,
|
2019-01-29 23:34:01 +01:00
|
|
|
&size_already_done, nr_sec, 0, false);
|
2018-01-30 18:48:19 +01:00
|
|
|
if (rc != 0) {
|
|
|
|
LOG(ERROR) << "Inplace crypto failed with code: " << rc;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (static_cast<uint64_t>(size_already_done) != nr_sec) {
|
|
|
|
LOG(ERROR) << "Inplace crypto only got up to sector: " << size_already_done;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Inplace encryption complete";
|
2016-06-02 20:04:27 +02:00
|
|
|
}
|
|
|
|
|
2018-01-30 18:48:19 +01:00
|
|
|
LOG(DEBUG) << "Mounting metadata-encrypted filesystem:" << mount_point;
|
2019-01-29 23:34:01 +01:00
|
|
|
mount_via_fs_mgr(data_rec->mount_point.c_str(), crypto_blkdev.c_str());
|
2016-06-02 20:04:27 +02:00
|
|
|
return true;
|
|
|
|
}
|