Refactor to use EncryptionPolicy everywhere we used to use raw_ref
Test: Boots, no bad log messages: Cuttlefish with v2 policies, Taimen Bug: 147733587 Change-Id: Ice4acac3236b6b7d90e60a2f57b46814aa1949f5
This commit is contained in:
parent
432ca5af06
commit
77df7f207d
4 changed files with 136 additions and 156 deletions
150
FsCrypt.cpp
150
FsCrypt.cpp
|
@ -63,6 +63,7 @@
|
|||
|
||||
using android::base::StringPrintf;
|
||||
using android::fs_mgr::GetEntryForMountPoint;
|
||||
using android::vold::BuildDataPath;
|
||||
using android::vold::kEmptyAuthentication;
|
||||
using android::vold::KeyBuffer;
|
||||
using android::vold::writeStringToFile;
|
||||
|
@ -86,9 +87,9 @@ bool s_systemwide_keys_initialized = false;
|
|||
// Some users are ephemeral, don't try to wipe their keys from disk
|
||||
std::set<userid_t> s_ephemeral_users;
|
||||
|
||||
// Map user ids to key references
|
||||
std::map<userid_t, std::string> s_de_key_raw_refs;
|
||||
std::map<userid_t, std::string> s_ce_key_raw_refs;
|
||||
// Map user ids to encryption policies
|
||||
std::map<userid_t, EncryptionPolicy> s_de_policies;
|
||||
std::map<userid_t, EncryptionPolicy> s_ce_policies;
|
||||
|
||||
} // namespace
|
||||
|
||||
|
@ -195,23 +196,18 @@ static bool read_and_fixate_user_ce_key(userid_t user_id,
|
|||
}
|
||||
|
||||
// Retrieve the options to use for encryption policies on the /data filesystem.
|
||||
static void get_data_file_encryption_options(EncryptionOptions* options) {
|
||||
static bool get_data_file_encryption_options(EncryptionOptions* options) {
|
||||
auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
|
||||
if (entry == nullptr) {
|
||||
return;
|
||||
LOG(ERROR) << "No mount point entry for " << DATA_MNT_POINT;
|
||||
return false;
|
||||
}
|
||||
if (!ParseOptions(entry->encryption_options, options)) {
|
||||
LOG(ERROR) << "Unable to parse encryption options for " << DATA_MNT_POINT ": "
|
||||
<< entry->encryption_options;
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieve the version to use for encryption policies on the /data filesystem.
|
||||
static int get_data_file_policy_version(void) {
|
||||
EncryptionOptions options;
|
||||
get_data_file_encryption_options(&options);
|
||||
return options.version;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Retrieve the options to use for encryption policies on adoptable storage.
|
||||
|
@ -220,29 +216,25 @@ static bool get_volume_file_encryption_options(EncryptionOptions* options) {
|
|||
android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
|
||||
auto filenames_mode =
|
||||
android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
|
||||
return ParseOptions(android::base::GetProperty("ro.crypto.volume.options",
|
||||
contents_mode + ":" + filenames_mode + ":v1"),
|
||||
options);
|
||||
}
|
||||
|
||||
// Install a key for use by encrypted files on the /data filesystem.
|
||||
static bool install_data_key(const KeyBuffer& key, std::string* raw_ref) {
|
||||
return android::vold::installKey(key, DATA_MNT_POINT, get_data_file_policy_version(), raw_ref);
|
||||
}
|
||||
|
||||
// Evict a key for use by encrypted files on the /data filesystem.
|
||||
static bool evict_data_key(const std::string& raw_ref) {
|
||||
return android::vold::evictKey(DATA_MNT_POINT, raw_ref, get_data_file_policy_version());
|
||||
auto options_string = android::base::GetProperty("ro.crypto.volume.options",
|
||||
contents_mode + ":" + filenames_mode + ":v1");
|
||||
if (!ParseOptions(options_string, options)) {
|
||||
LOG(ERROR) << "Unable to parse volume encryption options: " << options_string;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool read_and_install_user_ce_key(userid_t user_id,
|
||||
const android::vold::KeyAuthentication& auth) {
|
||||
if (s_ce_key_raw_refs.count(user_id) != 0) return true;
|
||||
if (s_ce_policies.count(user_id) != 0) return true;
|
||||
EncryptionOptions options;
|
||||
if (!get_data_file_encryption_options(&options)) return false;
|
||||
KeyBuffer ce_key;
|
||||
if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
|
||||
std::string ce_raw_ref;
|
||||
if (!install_data_key(ce_key, &ce_raw_ref)) return false;
|
||||
s_ce_key_raw_refs[user_id] = ce_raw_ref;
|
||||
EncryptionPolicy ce_policy;
|
||||
if (!installKey(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
|
||||
s_ce_policies[user_id] = ce_policy;
|
||||
LOG(DEBUG) << "Installed ce key for user " << user_id;
|
||||
return true;
|
||||
}
|
||||
|
@ -268,6 +260,8 @@ static bool destroy_dir(const std::string& dir) {
|
|||
// NB this assumes that there is only one thread listening for crypt commands, because
|
||||
// it creates keys in a fixed location.
|
||||
static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
|
||||
EncryptionOptions options;
|
||||
if (!get_data_file_encryption_options(&options)) return false;
|
||||
KeyBuffer de_key, ce_key;
|
||||
if (!android::vold::randomKey(&de_key)) return false;
|
||||
if (!android::vold::randomKey(&ce_key)) return false;
|
||||
|
@ -289,24 +283,24 @@ static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral
|
|||
kEmptyAuthentication, de_key))
|
||||
return false;
|
||||
}
|
||||
std::string de_raw_ref;
|
||||
if (!install_data_key(de_key, &de_raw_ref)) return false;
|
||||
s_de_key_raw_refs[user_id] = de_raw_ref;
|
||||
std::string ce_raw_ref;
|
||||
if (!install_data_key(ce_key, &ce_raw_ref)) return false;
|
||||
s_ce_key_raw_refs[user_id] = ce_raw_ref;
|
||||
EncryptionPolicy de_policy;
|
||||
if (!installKey(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
|
||||
s_de_policies[user_id] = de_policy;
|
||||
EncryptionPolicy ce_policy;
|
||||
if (!installKey(DATA_MNT_POINT, options, ce_key, &ce_policy)) return false;
|
||||
s_ce_policies[user_id] = ce_policy;
|
||||
LOG(DEBUG) << "Created keys for user " << user_id;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, userid_t user_id,
|
||||
std::string* raw_ref) {
|
||||
static bool lookup_policy(const std::map<userid_t, EncryptionPolicy>& key_map, userid_t user_id,
|
||||
EncryptionPolicy* policy) {
|
||||
auto refi = key_map.find(user_id);
|
||||
if (refi == key_map.end()) {
|
||||
LOG(DEBUG) << "Cannot find key for " << user_id;
|
||||
return false;
|
||||
}
|
||||
*raw_ref = refi->second;
|
||||
*policy = refi->second;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -318,6 +312,8 @@ static bool is_numeric(const char* name) {
|
|||
}
|
||||
|
||||
static bool load_all_de_keys() {
|
||||
EncryptionOptions options;
|
||||
if (!get_data_file_encryption_options(&options)) return false;
|
||||
auto de_dir = user_key_dir + "/de";
|
||||
auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(de_dir.c_str()), closedir);
|
||||
if (!dirp) {
|
||||
|
@ -339,13 +335,13 @@ static bool load_all_de_keys() {
|
|||
continue;
|
||||
}
|
||||
userid_t user_id = std::stoi(entry->d_name);
|
||||
if (s_de_key_raw_refs.count(user_id) == 0) {
|
||||
if (s_de_policies.count(user_id) == 0) {
|
||||
auto key_path = de_dir + "/" + entry->d_name;
|
||||
KeyBuffer key;
|
||||
if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
|
||||
std::string raw_ref;
|
||||
if (!install_data_key(key, &raw_ref)) return false;
|
||||
s_de_key_raw_refs[user_id] = raw_ref;
|
||||
KeyBuffer de_key;
|
||||
if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &de_key)) return false;
|
||||
EncryptionPolicy de_policy;
|
||||
if (!installKey(DATA_MNT_POINT, options, de_key, &de_policy)) return false;
|
||||
s_de_policies[user_id] = de_policy;
|
||||
LOG(DEBUG) << "Installed de key for user " << user_id;
|
||||
}
|
||||
}
|
||||
|
@ -361,13 +357,16 @@ bool fscrypt_initialize_systemwide_keys() {
|
|||
LOG(INFO) << "Already initialized";
|
||||
return true;
|
||||
}
|
||||
EncryptionOptions options;
|
||||
if (!get_data_file_encryption_options(&options)) return false;
|
||||
|
||||
KeyBuffer device_key;
|
||||
if (!android::vold::retrieveKey(true, kEmptyAuthentication, device_key_path, device_key_temp,
|
||||
&device_key))
|
||||
return false;
|
||||
|
||||
EncryptionPolicy device_policy;
|
||||
get_data_file_encryption_options(&device_policy.options);
|
||||
|
||||
if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, device_key_path,
|
||||
device_key_temp, "", device_policy.options.version,
|
||||
&device_policy.key_raw_ref))
|
||||
if (!android::vold::installKey(DATA_MNT_POINT, options, device_key, &device_policy))
|
||||
return false;
|
||||
|
||||
std::string options_string;
|
||||
|
@ -375,19 +374,21 @@ bool fscrypt_initialize_systemwide_keys() {
|
|||
LOG(ERROR) << "Unable to serialize options";
|
||||
return false;
|
||||
}
|
||||
std::string options_filename = std::string("/data") + fscrypt_key_mode;
|
||||
std::string options_filename = std::string(DATA_MNT_POINT) + fscrypt_key_mode;
|
||||
if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
|
||||
|
||||
std::string ref_filename = std::string("/data") + fscrypt_key_ref;
|
||||
std::string ref_filename = std::string(DATA_MNT_POINT) + fscrypt_key_ref;
|
||||
if (!android::vold::writeStringToFile(device_policy.key_raw_ref, ref_filename)) return false;
|
||||
LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
|
||||
|
||||
KeyBuffer per_boot_key;
|
||||
if (!android::vold::randomKey(&per_boot_key)) return false;
|
||||
std::string per_boot_raw_ref;
|
||||
if (!install_data_key(per_boot_key, &per_boot_raw_ref)) return false;
|
||||
EncryptionPolicy per_boot_policy;
|
||||
if (!android::vold::installKey(DATA_MNT_POINT, options, per_boot_key, &per_boot_policy))
|
||||
return false;
|
||||
std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
|
||||
if (!android::vold::writeStringToFile(per_boot_raw_ref, per_boot_ref_filename)) return false;
|
||||
if (!android::vold::writeStringToFile(per_boot_policy.key_raw_ref, per_boot_ref_filename))
|
||||
return false;
|
||||
LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
|
||||
|
||||
if (!android::vold::FsyncDirectory(device_key_dir)) return false;
|
||||
|
@ -431,7 +432,7 @@ bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral)
|
|||
return true;
|
||||
}
|
||||
// FIXME test for existence of key that is not loaded yet
|
||||
if (s_ce_key_raw_refs.count(user_id) != 0) {
|
||||
if (s_ce_policies.count(user_id) != 0) {
|
||||
LOG(ERROR) << "Already exists, can't fscrypt_vold_create_user_key for " << user_id
|
||||
<< " serial " << serial;
|
||||
// FIXME should we fail the command?
|
||||
|
@ -466,13 +467,13 @@ static void drop_caches_if_needed() {
|
|||
|
||||
static bool evict_ce_key(userid_t user_id) {
|
||||
bool success = true;
|
||||
std::string raw_ref;
|
||||
EncryptionPolicy policy;
|
||||
// If we haven't loaded the CE key, no need to evict it.
|
||||
if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
|
||||
success &= evict_data_key(raw_ref);
|
||||
if (lookup_policy(s_ce_policies, user_id, &policy)) {
|
||||
success &= android::vold::evictKey(DATA_MNT_POINT, policy);
|
||||
drop_caches_if_needed();
|
||||
}
|
||||
s_ce_key_raw_refs.erase(user_id);
|
||||
s_ce_policies.erase(user_id);
|
||||
return success;
|
||||
}
|
||||
|
||||
|
@ -482,10 +483,11 @@ bool fscrypt_destroy_user_key(userid_t user_id) {
|
|||
return true;
|
||||
}
|
||||
bool success = true;
|
||||
std::string raw_ref;
|
||||
success &= evict_ce_key(user_id);
|
||||
success &= lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) && evict_data_key(raw_ref);
|
||||
s_de_key_raw_refs.erase(user_id);
|
||||
EncryptionPolicy de_policy;
|
||||
success &= lookup_policy(s_de_policies, user_id, &de_policy) &&
|
||||
android::vold::evictKey(DATA_MNT_POINT, de_policy);
|
||||
s_de_policies.erase(user_id);
|
||||
auto it = s_ephemeral_users.find(user_id);
|
||||
if (it != s_ephemeral_users.end()) {
|
||||
s_ephemeral_users.erase(it);
|
||||
|
@ -587,11 +589,12 @@ static bool read_or_create_volkey(const std::string& misc_path, const std::strin
|
|||
}
|
||||
android::vold::KeyAuthentication auth("", secdiscardable_hash);
|
||||
|
||||
if (!get_volume_file_encryption_options(&policy->options)) return false;
|
||||
|
||||
return android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
|
||||
volume_uuid, policy->options.version,
|
||||
&policy->key_raw_ref);
|
||||
EncryptionOptions options;
|
||||
if (!get_volume_file_encryption_options(&options)) return false;
|
||||
KeyBuffer key;
|
||||
if (!android::vold::retrieveKey(true, auth, key_path, key_path + "_tmp", &key)) return false;
|
||||
if (!android::vold::installKey(BuildDataPath(volume_uuid), options, key, policy)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
|
||||
|
@ -667,7 +670,7 @@ bool fscrypt_unlock_user_key(userid_t user_id, int serial, const std::string& to
|
|||
LOG(DEBUG) << "fscrypt_unlock_user_key " << user_id << " serial=" << serial
|
||||
<< " token_present=" << (token_hex != "!");
|
||||
if (fscrypt_is_native()) {
|
||||
if (s_ce_key_raw_refs.count(user_id) != 0) {
|
||||
if (s_ce_policies.count(user_id) != 0) {
|
||||
LOG(WARNING) << "Tried to unlock already-unlocked key for user " << user_id;
|
||||
return true;
|
||||
}
|
||||
|
@ -757,9 +760,7 @@ bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_
|
|||
if (fscrypt_is_native()) {
|
||||
EncryptionPolicy de_policy;
|
||||
if (volume_uuid.empty()) {
|
||||
if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_policy.key_raw_ref))
|
||||
return false;
|
||||
get_data_file_encryption_options(&de_policy.options);
|
||||
if (!lookup_policy(s_de_policies, user_id, &de_policy)) return false;
|
||||
if (!EnsurePolicy(de_policy, system_de_path)) return false;
|
||||
if (!EnsurePolicy(de_policy, misc_de_path)) return false;
|
||||
if (!EnsurePolicy(de_policy, vendor_de_path)) return false;
|
||||
|
@ -789,13 +790,10 @@ bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_
|
|||
if (fscrypt_is_native()) {
|
||||
EncryptionPolicy ce_policy;
|
||||
if (volume_uuid.empty()) {
|
||||
if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_policy.key_raw_ref))
|
||||
return false;
|
||||
get_data_file_encryption_options(&ce_policy.options);
|
||||
if (!lookup_policy(s_ce_policies, user_id, &ce_policy)) return false;
|
||||
if (!EnsurePolicy(ce_policy, system_ce_path)) return false;
|
||||
if (!EnsurePolicy(ce_policy, misc_ce_path)) return false;
|
||||
if (!EnsurePolicy(ce_policy, vendor_ce_path)) return false;
|
||||
|
||||
} else {
|
||||
if (!read_or_create_volkey(misc_ce_path, volume_uuid, &ce_policy)) return false;
|
||||
}
|
||||
|
|
100
KeyUtil.cpp
100
KeyUtil.cpp
|
@ -161,62 +161,51 @@ static bool installKeyLegacy(const KeyBuffer& key, const std::string& raw_ref) {
|
|||
}
|
||||
|
||||
// Build a struct fscrypt_key_specifier for use in the key management ioctls.
|
||||
static bool buildKeySpecifier(fscrypt_key_specifier* spec, const std::string& raw_ref,
|
||||
int policy_version) {
|
||||
switch (policy_version) {
|
||||
static bool buildKeySpecifier(fscrypt_key_specifier* spec, const EncryptionPolicy& policy) {
|
||||
switch (policy.options.version) {
|
||||
case 1:
|
||||
if (raw_ref.size() != FSCRYPT_KEY_DESCRIPTOR_SIZE) {
|
||||
if (policy.key_raw_ref.size() != FSCRYPT_KEY_DESCRIPTOR_SIZE) {
|
||||
LOG(ERROR) << "Invalid key specifier size for v1 encryption policy: "
|
||||
<< raw_ref.size();
|
||||
<< policy.key_raw_ref.size();
|
||||
return false;
|
||||
}
|
||||
spec->type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
|
||||
memcpy(spec->u.descriptor, raw_ref.c_str(), FSCRYPT_KEY_DESCRIPTOR_SIZE);
|
||||
memcpy(spec->u.descriptor, policy.key_raw_ref.c_str(), FSCRYPT_KEY_DESCRIPTOR_SIZE);
|
||||
return true;
|
||||
case 2:
|
||||
if (raw_ref.size() != FSCRYPT_KEY_IDENTIFIER_SIZE) {
|
||||
if (policy.key_raw_ref.size() != FSCRYPT_KEY_IDENTIFIER_SIZE) {
|
||||
LOG(ERROR) << "Invalid key specifier size for v2 encryption policy: "
|
||||
<< raw_ref.size();
|
||||
<< policy.key_raw_ref.size();
|
||||
return false;
|
||||
}
|
||||
spec->type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
|
||||
memcpy(spec->u.identifier, raw_ref.c_str(), FSCRYPT_KEY_IDENTIFIER_SIZE);
|
||||
memcpy(spec->u.identifier, policy.key_raw_ref.c_str(), FSCRYPT_KEY_IDENTIFIER_SIZE);
|
||||
return true;
|
||||
default:
|
||||
LOG(ERROR) << "Invalid encryption policy version: " << policy_version;
|
||||
LOG(ERROR) << "Invalid encryption policy version: " << policy.options.version;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Install a file-based encryption key to the kernel, for use by encrypted files
|
||||
// on the specified filesystem using the specified encryption policy version.
|
||||
//
|
||||
// For v1 policies, we use FS_IOC_ADD_ENCRYPTION_KEY if the kernel supports it.
|
||||
// Otherwise we add the key to the legacy global session keyring.
|
||||
//
|
||||
// For v2 policies, we always use FS_IOC_ADD_ENCRYPTION_KEY; it's the only way
|
||||
// the kernel supports.
|
||||
//
|
||||
// Returns %true on success, %false on failure. On success also sets *raw_ref
|
||||
// to the raw key reference for use in the encryption policy.
|
||||
bool installKey(const KeyBuffer& key, const std::string& mountpoint, int policy_version,
|
||||
std::string* raw_ref) {
|
||||
bool installKey(const std::string& mountpoint, const EncryptionOptions& options,
|
||||
const KeyBuffer& key, EncryptionPolicy* policy) {
|
||||
policy->options = options;
|
||||
// Put the fscrypt_add_key_arg in an automatically-zeroing buffer, since we
|
||||
// have to copy the raw key into it.
|
||||
KeyBuffer arg_buf(sizeof(struct fscrypt_add_key_arg) + key.size(), 0);
|
||||
struct fscrypt_add_key_arg* arg = (struct fscrypt_add_key_arg*)arg_buf.data();
|
||||
|
||||
// Initialize the "key specifier", which is like a name for the key.
|
||||
switch (policy_version) {
|
||||
switch (options.version) {
|
||||
case 1:
|
||||
// A key for a v1 policy is specified by an arbitrary 8-byte
|
||||
// "descriptor", which must be provided by userspace. We use the
|
||||
// first 8 bytes from the double SHA-512 of the key itself.
|
||||
*raw_ref = generateKeyRef((const uint8_t*)key.data(), key.size());
|
||||
policy->key_raw_ref = generateKeyRef((const uint8_t*)key.data(), key.size());
|
||||
if (!isFsKeyringSupported()) {
|
||||
return installKeyLegacy(key, *raw_ref);
|
||||
return installKeyLegacy(key, policy->key_raw_ref);
|
||||
}
|
||||
if (!buildKeySpecifier(&arg->key_spec, *raw_ref, policy_version)) {
|
||||
if (!buildKeySpecifier(&arg->key_spec, *policy)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
@ -229,7 +218,7 @@ bool installKey(const KeyBuffer& key, const std::string& mountpoint, int policy_
|
|||
arg->key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
|
||||
break;
|
||||
default:
|
||||
LOG(ERROR) << "Invalid encryption policy version: " << policy_version;
|
||||
LOG(ERROR) << "Invalid encryption policy version: " << options.version;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -250,9 +239,10 @@ bool installKey(const KeyBuffer& key, const std::string& mountpoint, int policy_
|
|||
|
||||
if (arg->key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
|
||||
// Retrieve the key identifier that the kernel computed.
|
||||
*raw_ref = std::string((char*)arg->key_spec.u.identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
|
||||
policy->key_raw_ref =
|
||||
std::string((char*)arg->key_spec.u.identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
|
||||
}
|
||||
LOG(DEBUG) << "Installed fscrypt key with ref " << keyrefstring(*raw_ref) << " to "
|
||||
LOG(DEBUG) << "Installed fscrypt key with ref " << keyrefstring(policy->key_raw_ref) << " to "
|
||||
<< mountpoint;
|
||||
return true;
|
||||
}
|
||||
|
@ -280,15 +270,9 @@ static bool evictKeyLegacy(const std::string& raw_ref) {
|
|||
return success;
|
||||
}
|
||||
|
||||
// Evict a file-based encryption key from the kernel.
|
||||
//
|
||||
// We use FS_IOC_REMOVE_ENCRYPTION_KEY if the kernel supports it. Otherwise we
|
||||
// remove the key from the legacy global session keyring.
|
||||
//
|
||||
// In the latter case, the caller is responsible for dropping caches.
|
||||
bool evictKey(const std::string& mountpoint, const std::string& raw_ref, int policy_version) {
|
||||
if (policy_version == 1 && !isFsKeyringSupported()) {
|
||||
return evictKeyLegacy(raw_ref);
|
||||
bool evictKey(const std::string& mountpoint, const EncryptionPolicy& policy) {
|
||||
if (policy.options.version == 1 && !isFsKeyringSupported()) {
|
||||
return evictKeyLegacy(policy.key_raw_ref);
|
||||
}
|
||||
|
||||
android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
|
||||
|
@ -300,11 +284,11 @@ bool evictKey(const std::string& mountpoint, const std::string& raw_ref, int pol
|
|||
struct fscrypt_remove_key_arg arg;
|
||||
memset(&arg, 0, sizeof(arg));
|
||||
|
||||
if (!buildKeySpecifier(&arg.key_spec, raw_ref, policy_version)) {
|
||||
if (!buildKeySpecifier(&arg.key_spec, policy)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string ref = keyrefstring(raw_ref);
|
||||
std::string ref = keyrefstring(policy.key_raw_ref);
|
||||
|
||||
if (ioctl(fd, FS_IOC_REMOVE_ENCRYPTION_KEY, &arg) != 0) {
|
||||
PLOG(ERROR) << "Failed to evict fscrypt key with ref " << ref << " from " << mountpoint;
|
||||
|
@ -322,36 +306,12 @@ bool evictKey(const std::string& mountpoint, const std::string& raw_ref, int pol
|
|||
return true;
|
||||
}
|
||||
|
||||
bool retrieveAndInstallKey(bool create_if_absent, const KeyAuthentication& key_authentication,
|
||||
const std::string& key_path, const std::string& tmp_path,
|
||||
const std::string& volume_uuid, int policy_version,
|
||||
std::string* key_ref) {
|
||||
KeyBuffer key;
|
||||
bool retrieveKey(bool create_if_absent, const KeyAuthentication& key_authentication,
|
||||
const std::string& key_path, const std::string& tmp_path, KeyBuffer* key,
|
||||
bool keepOld) {
|
||||
if (pathExists(key_path)) {
|
||||
LOG(DEBUG) << "Key exists, using: " << key_path;
|
||||
if (!retrieveKey(key_path, key_authentication, &key)) return false;
|
||||
} else {
|
||||
if (!create_if_absent) {
|
||||
LOG(ERROR) << "No key found in " << key_path;
|
||||
return false;
|
||||
}
|
||||
LOG(INFO) << "Creating new key in " << key_path;
|
||||
if (!randomKey(&key)) return false;
|
||||
if (!storeKeyAtomically(key_path, tmp_path, key_authentication, key)) return false;
|
||||
}
|
||||
|
||||
if (!installKey(key, BuildDataPath(volume_uuid), policy_version, key_ref)) {
|
||||
LOG(ERROR) << "Failed to install key in " << key_path;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool retrieveKey(bool create_if_absent, const std::string& key_path, const std::string& tmp_path,
|
||||
KeyBuffer* key, bool keepOld) {
|
||||
if (pathExists(key_path)) {
|
||||
LOG(DEBUG) << "Key exists, using: " << key_path;
|
||||
if (!retrieveKey(key_path, kEmptyAuthentication, key, keepOld)) return false;
|
||||
if (!retrieveKey(key_path, key_authentication, key, keepOld)) return false;
|
||||
} else {
|
||||
if (!create_if_absent) {
|
||||
LOG(ERROR) << "No key found in " << key_path;
|
||||
|
@ -359,7 +319,7 @@ bool retrieveKey(bool create_if_absent, const std::string& key_path, const std::
|
|||
}
|
||||
LOG(INFO) << "Creating new key in " << key_path;
|
||||
if (!randomKey(key)) return false;
|
||||
if (!storeKeyAtomically(key_path, tmp_path, kEmptyAuthentication, *key)) return false;
|
||||
if (!storeKeyAtomically(key_path, tmp_path, key_authentication, *key)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
38
KeyUtil.h
38
KeyUtil.h
|
@ -20,25 +20,45 @@
|
|||
#include "KeyBuffer.h"
|
||||
#include "KeyStorage.h"
|
||||
|
||||
#include <fscrypt/fscrypt.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace android {
|
||||
namespace vold {
|
||||
|
||||
using namespace android::fscrypt;
|
||||
|
||||
bool randomKey(KeyBuffer* key);
|
||||
|
||||
bool isFsKeyringSupported(void);
|
||||
|
||||
bool installKey(const KeyBuffer& key, const std::string& mountpoint, int policy_version,
|
||||
std::string* raw_ref);
|
||||
bool evictKey(const std::string& mountpoint, const std::string& raw_ref, int policy_version);
|
||||
bool retrieveAndInstallKey(bool create_if_absent, const KeyAuthentication& key_authentication,
|
||||
const std::string& key_path, const std::string& tmp_path,
|
||||
const std::string& volume_uuid, int policy_version,
|
||||
std::string* key_ref);
|
||||
bool retrieveKey(bool create_if_absent, const std::string& key_path, const std::string& tmp_path,
|
||||
KeyBuffer* key, bool keepOld = true);
|
||||
// Install a file-based encryption key to the kernel, for use by encrypted files
|
||||
// on the specified filesystem using the specified encryption policy version.
|
||||
//
|
||||
// For v1 policies, we use FS_IOC_ADD_ENCRYPTION_KEY if the kernel supports it.
|
||||
// Otherwise we add the key to the legacy global session keyring.
|
||||
//
|
||||
// For v2 policies, we always use FS_IOC_ADD_ENCRYPTION_KEY; it's the only way
|
||||
// the kernel supports.
|
||||
//
|
||||
// Returns %true on success, %false on failure. On success also sets *policy
|
||||
// to the EncryptionPolicy used to refer to this key.
|
||||
bool installKey(const std::string& mountpoint, const EncryptionOptions& options,
|
||||
const KeyBuffer& key, EncryptionPolicy* policy);
|
||||
|
||||
// Evict a file-based encryption key from the kernel.
|
||||
//
|
||||
// We use FS_IOC_REMOVE_ENCRYPTION_KEY if the kernel supports it. Otherwise we
|
||||
// remove the key from the legacy global session keyring.
|
||||
//
|
||||
// In the latter case, the caller is responsible for dropping caches.
|
||||
bool evictKey(const std::string& mountpoint, const EncryptionPolicy& policy);
|
||||
|
||||
bool retrieveKey(bool create_if_absent, const KeyAuthentication& key_authentication,
|
||||
const std::string& key_path, const std::string& tmp_path, KeyBuffer* key,
|
||||
bool keepOld = true);
|
||||
|
||||
} // namespace vold
|
||||
} // namespace android
|
||||
|
|
|
@ -135,7 +135,9 @@ static bool read_key(const FstabEntry& data_rec, bool create_if_absent, KeyBuffe
|
|||
unlink(newKeyPath.c_str());
|
||||
}
|
||||
bool needs_cp = cp_needsCheckpoint();
|
||||
if (!android::vold::retrieveKey(create_if_absent, dir, temp, key, needs_cp)) return false;
|
||||
if (!android::vold::retrieveKey(create_if_absent, kEmptyAuthentication, dir, temp, key,
|
||||
needs_cp))
|
||||
return false;
|
||||
if (needs_cp && pathExists(newKeyPath)) std::thread(commit_key, dir).detach();
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue