Merge changes from topic "fscrypt-key-mgmt-improvements"
* changes: vold: support v2 encryption policies vold: use new ioctls to add/remove fscrypt keys when supported
This commit is contained in:
commit
22d50012b0
4 changed files with 343 additions and 54 deletions
134
FsCrypt.cpp
134
FsCrypt.cpp
|
@ -57,6 +57,7 @@
|
|||
#include <android-base/logging.h>
|
||||
#include <android-base/properties.h>
|
||||
#include <android-base/stringprintf.h>
|
||||
#include <android-base/strings.h>
|
||||
#include <android-base/unique_fd.h>
|
||||
|
||||
using android::base::StringPrintf;
|
||||
|
@ -70,7 +71,10 @@ namespace {
|
|||
struct PolicyKeyRef {
|
||||
std::string contents_mode;
|
||||
std::string filenames_mode;
|
||||
int policy_version;
|
||||
std::string key_raw_ref;
|
||||
|
||||
PolicyKeyRef() : policy_version(0) {}
|
||||
};
|
||||
|
||||
const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
|
||||
|
@ -199,13 +203,66 @@ static bool read_and_fixate_user_ce_key(userid_t user_id,
|
|||
return false;
|
||||
}
|
||||
|
||||
// Retrieve the options to use for encryption policies on the /data filesystem.
|
||||
static void get_data_file_encryption_options(PolicyKeyRef* key_ref) {
|
||||
auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
|
||||
if (entry == nullptr) {
|
||||
return;
|
||||
}
|
||||
key_ref->contents_mode = entry->file_contents_mode;
|
||||
key_ref->filenames_mode = entry->file_names_mode;
|
||||
key_ref->policy_version = entry->file_policy_version;
|
||||
}
|
||||
|
||||
// Retrieve the version to use for encryption policies on the /data filesystem.
|
||||
static int get_data_file_policy_version(void) {
|
||||
auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
|
||||
if (entry == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
return entry->file_policy_version;
|
||||
}
|
||||
|
||||
// Retrieve the options to use for encryption policies on adoptable storage.
|
||||
static bool get_volume_file_encryption_options(PolicyKeyRef* key_ref) {
|
||||
key_ref->contents_mode =
|
||||
android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
|
||||
key_ref->filenames_mode =
|
||||
android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
|
||||
key_ref->policy_version = 1;
|
||||
|
||||
std::string raw_flags = android::base::GetProperty("ro.crypto.volume.flags", "");
|
||||
auto flags = android::base::Split(raw_flags, "+");
|
||||
for (const auto& flag : flags) {
|
||||
if (flag == "v1") {
|
||||
key_ref->policy_version = 1;
|
||||
} else if (flag == "v2") {
|
||||
key_ref->policy_version = 2;
|
||||
} else {
|
||||
LOG(ERROR) << "Unknown flag in ro.crypto.volume.flags: " << flag;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 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());
|
||||
}
|
||||
|
||||
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;
|
||||
KeyBuffer ce_key;
|
||||
if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
|
||||
std::string ce_raw_ref;
|
||||
if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false;
|
||||
if (!install_data_key(ce_key, &ce_raw_ref)) return false;
|
||||
s_ce_keys[user_id] = std::move(ce_key);
|
||||
s_ce_key_raw_refs[user_id] = ce_raw_ref;
|
||||
LOG(DEBUG) << "Installed ce key for user " << user_id;
|
||||
|
@ -255,10 +312,10 @@ static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral
|
|||
return false;
|
||||
}
|
||||
std::string de_raw_ref;
|
||||
if (!android::vold::installKey(de_key, &de_raw_ref)) return false;
|
||||
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 (!android::vold::installKey(ce_key, &ce_raw_ref)) return false;
|
||||
if (!install_data_key(ce_key, &ce_raw_ref)) return false;
|
||||
s_ce_keys[user_id] = ce_key;
|
||||
s_ce_key_raw_refs[user_id] = ce_raw_ref;
|
||||
LOG(DEBUG) << "Created keys for user " << user_id;
|
||||
|
@ -276,19 +333,10 @@ static bool lookup_key_ref(const std::map<userid_t, std::string>& key_map, useri
|
|||
return true;
|
||||
}
|
||||
|
||||
static void get_data_file_encryption_modes(PolicyKeyRef* key_ref) {
|
||||
auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
|
||||
if (entry == nullptr) {
|
||||
return;
|
||||
}
|
||||
key_ref->contents_mode = entry->file_contents_mode;
|
||||
key_ref->filenames_mode = entry->file_names_mode;
|
||||
}
|
||||
|
||||
static bool ensure_policy(const PolicyKeyRef& key_ref, const std::string& path) {
|
||||
return fscrypt_policy_ensure(path.c_str(), key_ref.key_raw_ref.data(),
|
||||
key_ref.key_raw_ref.size(), key_ref.contents_mode.c_str(),
|
||||
key_ref.filenames_mode.c_str()) == 0;
|
||||
key_ref.filenames_mode.c_str(), key_ref.policy_version) == 0;
|
||||
}
|
||||
|
||||
static bool is_numeric(const char* name) {
|
||||
|
@ -325,7 +373,7 @@ static bool load_all_de_keys() {
|
|||
KeyBuffer key;
|
||||
if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
|
||||
std::string raw_ref;
|
||||
if (!android::vold::installKey(key, &raw_ref)) return false;
|
||||
if (!install_data_key(key, &raw_ref)) return false;
|
||||
s_de_key_raw_refs[user_id] = raw_ref;
|
||||
LOG(DEBUG) << "Installed de key for user " << user_id;
|
||||
}
|
||||
|
@ -344,14 +392,18 @@ bool fscrypt_initialize_systemwide_keys() {
|
|||
}
|
||||
|
||||
PolicyKeyRef device_ref;
|
||||
if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, device_key_path,
|
||||
device_key_temp, &device_ref.key_raw_ref))
|
||||
return false;
|
||||
get_data_file_encryption_modes(&device_ref);
|
||||
get_data_file_encryption_options(&device_ref);
|
||||
|
||||
std::string modestring = device_ref.contents_mode + ":" + device_ref.filenames_mode;
|
||||
std::string mode_filename = std::string("/data") + fscrypt_key_mode;
|
||||
if (!android::vold::writeStringToFile(modestring, mode_filename)) return false;
|
||||
if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, device_key_path,
|
||||
device_key_temp, "", device_ref.policy_version,
|
||||
&device_ref.key_raw_ref))
|
||||
return false;
|
||||
|
||||
std::string options_string =
|
||||
StringPrintf("%s:%s:v%d", device_ref.contents_mode.c_str(),
|
||||
device_ref.filenames_mode.c_str(), device_ref.policy_version);
|
||||
std::string options_filename = std::string("/data") + fscrypt_key_mode;
|
||||
if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
|
||||
|
||||
std::string ref_filename = std::string("/data") + fscrypt_key_ref;
|
||||
if (!android::vold::writeStringToFile(device_ref.key_raw_ref, ref_filename)) return false;
|
||||
|
@ -360,7 +412,7 @@ bool fscrypt_initialize_systemwide_keys() {
|
|||
KeyBuffer per_boot_key;
|
||||
if (!android::vold::randomKey(&per_boot_key)) return false;
|
||||
std::string per_boot_raw_ref;
|
||||
if (!android::vold::installKey(per_boot_key, &per_boot_raw_ref)) return false;
|
||||
if (!install_data_key(per_boot_key, &per_boot_raw_ref)) 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;
|
||||
LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
|
||||
|
@ -419,15 +471,20 @@ bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral)
|
|||
}
|
||||
|
||||
// "Lock" all encrypted directories whose key has been removed. This is needed
|
||||
// because merely removing the keyring key doesn't affect inodes in the kernel's
|
||||
// inode cache whose per-file key was already set up. So to remove the per-file
|
||||
// keys and make the files "appear encrypted", these inodes must be evicted.
|
||||
// in the case where the keys are being put in the session keyring (rather in
|
||||
// the newer filesystem-level keyrings), because removing a key from the session
|
||||
// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
|
||||
// was already set up. So to remove the per-file keys and make the files
|
||||
// "appear encrypted", these inodes must be evicted.
|
||||
//
|
||||
// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
|
||||
// objects systemwide. This is overkill, but it's the best available method
|
||||
// currently. Don't use drop_caches mode "3" because that also evicts pagecache
|
||||
// for in-use files; all files relevant here are already closed and sync'ed.
|
||||
static void drop_caches() {
|
||||
static void drop_caches_if_needed() {
|
||||
if (android::vold::isFsKeyringSupported()) {
|
||||
return;
|
||||
}
|
||||
sync();
|
||||
if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
|
||||
PLOG(ERROR) << "Failed to drop caches during key eviction";
|
||||
|
@ -440,8 +497,8 @@ static bool evict_ce_key(userid_t user_id) {
|
|||
std::string raw_ref;
|
||||
// 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 &= android::vold::evictKey(raw_ref);
|
||||
drop_caches();
|
||||
success &= evict_data_key(raw_ref);
|
||||
drop_caches_if_needed();
|
||||
}
|
||||
s_ce_key_raw_refs.erase(user_id);
|
||||
return success;
|
||||
|
@ -455,8 +512,7 @@ bool fscrypt_destroy_user_key(userid_t user_id) {
|
|||
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) && android::vold::evictKey(raw_ref);
|
||||
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);
|
||||
auto it = s_ephemeral_users.find(user_id);
|
||||
if (it != s_ephemeral_users.end()) {
|
||||
|
@ -546,14 +602,12 @@ static bool read_or_create_volkey(const std::string& misc_path, const std::strin
|
|||
return false;
|
||||
}
|
||||
android::vold::KeyAuthentication auth("", secdiscardable_hash);
|
||||
if (!android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
|
||||
&key_ref->key_raw_ref))
|
||||
return false;
|
||||
key_ref->contents_mode =
|
||||
android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
|
||||
key_ref->filenames_mode =
|
||||
android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
|
||||
return true;
|
||||
|
||||
if (!get_volume_file_encryption_options(key_ref)) return false;
|
||||
|
||||
return android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
|
||||
volume_uuid, key_ref->policy_version,
|
||||
&key_ref->key_raw_ref);
|
||||
}
|
||||
|
||||
static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
|
||||
|
@ -701,7 +755,7 @@ bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_
|
|||
PolicyKeyRef de_ref;
|
||||
if (volume_uuid.empty()) {
|
||||
if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_ref.key_raw_ref)) return false;
|
||||
get_data_file_encryption_modes(&de_ref);
|
||||
get_data_file_encryption_options(&de_ref);
|
||||
if (!ensure_policy(de_ref, system_de_path)) return false;
|
||||
if (!ensure_policy(de_ref, misc_de_path)) return false;
|
||||
if (!ensure_policy(de_ref, vendor_de_path)) return false;
|
||||
|
@ -732,7 +786,7 @@ bool fscrypt_prepare_user_storage(const std::string& volume_uuid, userid_t user_
|
|||
PolicyKeyRef ce_ref;
|
||||
if (volume_uuid.empty()) {
|
||||
if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_ref.key_raw_ref)) return false;
|
||||
get_data_file_encryption_modes(&ce_ref);
|
||||
get_data_file_encryption_options(&ce_ref);
|
||||
if (!ensure_policy(ce_ref, system_ce_path)) return false;
|
||||
if (!ensure_policy(ce_ref, misc_ce_path)) return false;
|
||||
if (!ensure_policy(ce_ref, vendor_ce_path)) return false;
|
||||
|
|
206
KeyUtil.cpp
206
KeyUtil.cpp
|
@ -16,12 +16,14 @@
|
|||
|
||||
#include "KeyUtil.h"
|
||||
|
||||
#include <linux/fs.h>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <linux/fs.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <android-base/file.h>
|
||||
#include <android-base/logging.h>
|
||||
|
@ -29,6 +31,7 @@
|
|||
|
||||
#include "KeyStorage.h"
|
||||
#include "Utils.h"
|
||||
#include "fscrypt_uapi.h"
|
||||
|
||||
namespace android {
|
||||
namespace vold {
|
||||
|
@ -45,6 +48,42 @@ bool randomKey(KeyBuffer* key) {
|
|||
return true;
|
||||
}
|
||||
|
||||
// Return true if the kernel supports the ioctls to add/remove fscrypt keys
|
||||
// directly to/from the filesystem.
|
||||
bool isFsKeyringSupported(void) {
|
||||
static bool initialized = false;
|
||||
static bool supported;
|
||||
|
||||
if (!initialized) {
|
||||
android::base::unique_fd fd(open("/data", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
|
||||
|
||||
// FS_IOC_ADD_ENCRYPTION_KEY with a NULL argument will fail with ENOTTY
|
||||
// if the ioctl isn't supported. Otherwise it will fail with another
|
||||
// error code such as EFAULT.
|
||||
errno = 0;
|
||||
(void)ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, NULL);
|
||||
if (errno == ENOTTY) {
|
||||
LOG(INFO) << "Kernel doesn't support FS_IOC_ADD_ENCRYPTION_KEY. Falling back to "
|
||||
"session keyring";
|
||||
supported = false;
|
||||
} else {
|
||||
if (errno != EFAULT) {
|
||||
PLOG(WARNING) << "Unexpected error from FS_IOC_ADD_ENCRYPTION_KEY";
|
||||
}
|
||||
LOG(DEBUG) << "Detected support for FS_IOC_ADD_ENCRYPTION_KEY";
|
||||
supported = true;
|
||||
}
|
||||
// There's no need to check for FS_IOC_REMOVE_ENCRYPTION_KEY, since it's
|
||||
// guaranteed to be available if FS_IOC_ADD_ENCRYPTION_KEY is. There's
|
||||
// also no need to check for support on external volumes separately from
|
||||
// /data, since either the kernel supports the ioctls on all
|
||||
// fscrypt-capable filesystems or it doesn't.
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
return supported;
|
||||
}
|
||||
|
||||
// Get raw keyref - used to make keyname and to pass to ioctl
|
||||
static std::string generateKeyRef(const uint8_t* key, int length) {
|
||||
SHA512_CTX c;
|
||||
|
@ -78,16 +117,20 @@ static bool fillKey(const KeyBuffer& key, fscrypt_key* fs_key) {
|
|||
|
||||
static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr};
|
||||
|
||||
static std::string keyname(const std::string& prefix, const std::string& raw_ref) {
|
||||
static std::string keyrefstring(const std::string& raw_ref) {
|
||||
std::ostringstream o;
|
||||
o << prefix << ":";
|
||||
for (unsigned char i : raw_ref) {
|
||||
o << std::hex << std::setw(2) << std::setfill('0') << (int)i;
|
||||
}
|
||||
return o.str();
|
||||
}
|
||||
|
||||
// Get the keyring we store all keys in
|
||||
static std::string buildLegacyKeyName(const std::string& prefix, const std::string& raw_ref) {
|
||||
return prefix + ":" + keyrefstring(raw_ref);
|
||||
}
|
||||
|
||||
// Get the ID of the keyring we store all fscrypt keys in when the kernel is too
|
||||
// old to support FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY.
|
||||
static bool fscryptKeyring(key_serial_t* device_keyring) {
|
||||
*device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "fscrypt", 0);
|
||||
if (*device_keyring == -1) {
|
||||
|
@ -97,19 +140,17 @@ static bool fscryptKeyring(key_serial_t* device_keyring) {
|
|||
return true;
|
||||
}
|
||||
|
||||
// Install password into global keyring
|
||||
// Return raw key reference for use in policy
|
||||
bool installKey(const KeyBuffer& key, std::string* raw_ref) {
|
||||
// Add an encryption key to the legacy global session keyring.
|
||||
static bool installKeyLegacy(const KeyBuffer& key, const std::string& raw_ref) {
|
||||
// Place fscrypt_key into automatically zeroing buffer.
|
||||
KeyBuffer fsKeyBuffer(sizeof(fscrypt_key));
|
||||
fscrypt_key& fs_key = *reinterpret_cast<fscrypt_key*>(fsKeyBuffer.data());
|
||||
|
||||
if (!fillKey(key, &fs_key)) return false;
|
||||
*raw_ref = generateKeyRef(fs_key.raw, fs_key.size);
|
||||
key_serial_t device_keyring;
|
||||
if (!fscryptKeyring(&device_keyring)) return false;
|
||||
for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
|
||||
auto ref = keyname(*name_prefix, *raw_ref);
|
||||
auto ref = buildLegacyKeyName(*name_prefix, raw_ref);
|
||||
key_serial_t key_id =
|
||||
add_key("logon", ref.c_str(), (void*)&fs_key, sizeof(fs_key), device_keyring);
|
||||
if (key_id == -1) {
|
||||
|
@ -122,12 +163,110 @@ bool installKey(const KeyBuffer& key, std::string* raw_ref) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool evictKey(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) {
|
||||
case 1:
|
||||
if (raw_ref.size() != FSCRYPT_KEY_DESCRIPTOR_SIZE) {
|
||||
LOG(ERROR) << "Invalid key specifier size for v1 encryption policy: "
|
||||
<< raw_ref.size();
|
||||
return false;
|
||||
}
|
||||
spec->type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
|
||||
memcpy(spec->u.descriptor, raw_ref.c_str(), FSCRYPT_KEY_DESCRIPTOR_SIZE);
|
||||
return true;
|
||||
case 2:
|
||||
if (raw_ref.size() != FSCRYPT_KEY_IDENTIFIER_SIZE) {
|
||||
LOG(ERROR) << "Invalid key specifier size for v2 encryption policy: "
|
||||
<< raw_ref.size();
|
||||
return false;
|
||||
}
|
||||
spec->type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
|
||||
memcpy(spec->u.identifier, raw_ref.c_str(), FSCRYPT_KEY_IDENTIFIER_SIZE);
|
||||
return true;
|
||||
default:
|
||||
LOG(ERROR) << "Invalid encryption policy version: " << policy_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) {
|
||||
// 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) {
|
||||
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());
|
||||
if (!isFsKeyringSupported()) {
|
||||
return installKeyLegacy(key, *raw_ref);
|
||||
}
|
||||
if (!buildKeySpecifier(&arg->key_spec, *raw_ref, policy_version)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
// A key for a v2 policy is specified by an 16-byte "identifier",
|
||||
// which is a cryptographic hash of the key itself which the kernel
|
||||
// computes and returns. Any user-provided value is ignored; we
|
||||
// just need to set the specifier type to indicate that we're adding
|
||||
// this type of key.
|
||||
arg->key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
|
||||
break;
|
||||
default:
|
||||
LOG(ERROR) << "Invalid encryption policy version: " << policy_version;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Provide the raw key.
|
||||
arg->raw_size = key.size();
|
||||
memcpy(arg->raw, key.data(), key.size());
|
||||
|
||||
android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
|
||||
if (fd == -1) {
|
||||
PLOG(ERROR) << "Failed to open " << mountpoint << " to install key";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, arg) != 0) {
|
||||
PLOG(ERROR) << "Failed to install fscrypt key to " << mountpoint;
|
||||
return false;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
LOG(DEBUG) << "Installed fscrypt key with ref " << keyrefstring(*raw_ref) << " to "
|
||||
<< mountpoint;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Remove an encryption key from the legacy global session keyring.
|
||||
static bool evictKeyLegacy(const std::string& raw_ref) {
|
||||
key_serial_t device_keyring;
|
||||
if (!fscryptKeyring(&device_keyring)) return false;
|
||||
bool success = true;
|
||||
for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
|
||||
auto ref = keyname(*name_prefix, raw_ref);
|
||||
auto ref = buildLegacyKeyName(*name_prefix, raw_ref);
|
||||
auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
|
||||
|
||||
// Unlink the key from the keyring. Prefer unlinking to revoking or
|
||||
|
@ -144,8 +283,51 @@ bool evictKey(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);
|
||||
}
|
||||
|
||||
android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
|
||||
if (fd == -1) {
|
||||
PLOG(ERROR) << "Failed to open " << mountpoint << " to evict key";
|
||||
return false;
|
||||
}
|
||||
|
||||
struct fscrypt_remove_key_arg arg;
|
||||
memset(&arg, 0, sizeof(arg));
|
||||
|
||||
if (!buildKeySpecifier(&arg.key_spec, raw_ref, policy_version)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string ref = keyrefstring(raw_ref);
|
||||
|
||||
if (ioctl(fd, FS_IOC_REMOVE_ENCRYPTION_KEY, &arg) != 0) {
|
||||
PLOG(ERROR) << "Failed to evict fscrypt key with ref " << ref << " from " << mountpoint;
|
||||
return false;
|
||||
}
|
||||
|
||||
LOG(DEBUG) << "Evicted fscrypt key with ref " << ref << " from " << mountpoint;
|
||||
if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS) {
|
||||
// Should never happen because keys are only added/removed as root.
|
||||
LOG(ERROR) << "Unexpected case: key with ref " << ref << " is still added by other users!";
|
||||
} else if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY) {
|
||||
LOG(ERROR) << "Files still open after removing key with ref " << ref
|
||||
<< ". These files were not locked!";
|
||||
}
|
||||
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;
|
||||
if (pathExists(key_path)) {
|
||||
|
@ -161,7 +343,7 @@ bool retrieveAndInstallKey(bool create_if_absent, const KeyAuthentication& key_a
|
|||
if (!storeKeyAtomically(key_path, tmp_path, key_authentication, key)) return false;
|
||||
}
|
||||
|
||||
if (!installKey(key, key_ref)) {
|
||||
if (!installKey(key, BuildDataPath(volume_uuid), policy_version, key_ref)) {
|
||||
LOG(ERROR) << "Failed to install key in " << key_path;
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -27,10 +27,15 @@ namespace android {
|
|||
namespace vold {
|
||||
|
||||
bool randomKey(KeyBuffer* key);
|
||||
bool installKey(const KeyBuffer& key, std::string* raw_ref);
|
||||
bool evictKey(const std::string& raw_ref);
|
||||
|
||||
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);
|
||||
|
|
48
fscrypt_uapi.h
Normal file
48
fscrypt_uapi.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
#ifndef _UAPI_LINUX_FSCRYPT_H
|
||||
#define _UAPI_LINUX_FSCRYPT_H
|
||||
|
||||
// Definitions for FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY
|
||||
|
||||
// TODO: switch to <linux/fscrypt.h> once it's in Bionic
|
||||
|
||||
#ifndef FS_IOC_ADD_ENCRYPTION_KEY
|
||||
|
||||
#include <linux/types.h>
|
||||
|
||||
#define FSCRYPT_KEY_DESCRIPTOR_SIZE 8
|
||||
#define FSCRYPT_KEY_IDENTIFIER_SIZE 16
|
||||
|
||||
#define FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR 1
|
||||
#define FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER 2
|
||||
|
||||
struct fscrypt_key_specifier {
|
||||
__u32 type;
|
||||
__u32 __reserved;
|
||||
union {
|
||||
__u8 __reserved[32];
|
||||
__u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
|
||||
__u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
|
||||
} u;
|
||||
};
|
||||
|
||||
struct fscrypt_add_key_arg {
|
||||
struct fscrypt_key_specifier key_spec;
|
||||
__u32 raw_size;
|
||||
__u32 __reserved[9];
|
||||
__u8 raw[];
|
||||
};
|
||||
|
||||
struct fscrypt_remove_key_arg {
|
||||
struct fscrypt_key_specifier key_spec;
|
||||
#define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY 0x00000001
|
||||
#define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS 0x00000002
|
||||
__u32 removal_status_flags;
|
||||
__u32 __reserved[5];
|
||||
};
|
||||
|
||||
#define FS_IOC_ADD_ENCRYPTION_KEY _IOWR('f', 23, struct fscrypt_add_key_arg)
|
||||
#define FS_IOC_REMOVE_ENCRYPTION_KEY _IOWR('f', 24, struct fscrypt_remove_key_arg)
|
||||
|
||||
#endif /* FS_IOC_ADD_ENCRYPTION_KEY */
|
||||
|
||||
#endif /* _UAPI_LINUX_FSCRYPT_H */
|
Loading…
Reference in a new issue