fskeyring & userspace reboot: support CE keys
During userspace reboot /data might be unmounted & remounted, meaning that CE keys stored in fs-level keyring will be lost. In order to be able to restore them, when installing new key to fs-level keyring, it's also added to session-level keyring with type "fscrypt-provisioning". Then when init_user0 is called during userspace reboot, vold will try to load CE keys from the session-level keyring back into fs-level keyring for all the users that were unlocked before the reboot. If for any user vold fails to install the key, init_user0 will fail and fallback to hard reboot will be triggered. Test: set a pin pattern Test: adb shell setprop sys.init.userdata_remount.force_umount 1 Test: adb shell svc power reboot userspace Test: atest CtsUserspaceRebootHostSideTestCases Bug: 143970043 Change-Id: I37603dc136c7ededc7b0381e4d730cb0ffd912b4
This commit is contained in:
parent
16df3dc2b5
commit
1ee35cf002
4 changed files with 149 additions and 21 deletions
18
FsCrypt.cpp
18
FsCrypt.cpp
|
@ -378,6 +378,17 @@ static bool load_all_de_keys() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attempt to reinstall CE keys for users that we think are unlocked.
|
||||||
|
static bool try_reload_ce_keys() {
|
||||||
|
for (const auto& it : s_ce_policies) {
|
||||||
|
if (!android::vold::reloadKeyFromSessionKeyring(DATA_MNT_POINT, it.second)) {
|
||||||
|
LOG(ERROR) << "Failed to load CE key from session keyring for user " << it.first;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool fscrypt_initialize_systemwide_keys() {
|
bool fscrypt_initialize_systemwide_keys() {
|
||||||
LOG(INFO) << "fscrypt_initialize_systemwide_keys";
|
LOG(INFO) << "fscrypt_initialize_systemwide_keys";
|
||||||
|
|
||||||
|
@ -444,6 +455,13 @@ bool fscrypt_init_user0() {
|
||||||
fscrypt_unlock_user_key(0, 0, "!", "!");
|
fscrypt_unlock_user_key(0, 0, "!", "!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// In some scenarios (e.g. userspace reboot) we might unmount userdata
|
||||||
|
// without doing a hard reboot. If CE keys were stored in fs keyring then
|
||||||
|
// they will be lost after unmount. Attempt to re-install them.
|
||||||
|
if (fscrypt_is_native() && android::vold::isFsKeyringSupported()) {
|
||||||
|
if (!try_reload_ce_keys()) return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
125
KeyUtil.cpp
125
KeyUtil.cpp
|
@ -155,7 +155,7 @@ static bool fscryptKeyring(key_serial_t* device_keyring) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add an encryption key to the legacy global session keyring.
|
// Add an encryption key of type "logon" to the global session keyring.
|
||||||
static bool installKeyLegacy(const KeyBuffer& key, const std::string& raw_ref) {
|
static bool installKeyLegacy(const KeyBuffer& key, const std::string& raw_ref) {
|
||||||
// Place fscrypt_key into automatically zeroing buffer.
|
// Place fscrypt_key into automatically zeroing buffer.
|
||||||
KeyBuffer fsKeyBuffer(sizeof(fscrypt_key));
|
KeyBuffer fsKeyBuffer(sizeof(fscrypt_key));
|
||||||
|
@ -178,6 +178,32 @@ static bool installKeyLegacy(const KeyBuffer& key, const std::string& raw_ref) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Installs fscrypt-provisioning key into session level kernel keyring.
|
||||||
|
// This allows for the given key to be installed back into filesystem keyring.
|
||||||
|
// For more context see reloadKeyFromSessionKeyring.
|
||||||
|
static bool installProvisioningKey(const KeyBuffer& key, const std::string& ref,
|
||||||
|
const fscrypt_key_specifier& key_spec) {
|
||||||
|
key_serial_t device_keyring;
|
||||||
|
if (!fscryptKeyring(&device_keyring)) return false;
|
||||||
|
|
||||||
|
// Place fscrypt_provisioning_key_payload into automatically zeroing buffer.
|
||||||
|
KeyBuffer buf(sizeof(fscrypt_provisioning_key_payload) + key.size(), 0);
|
||||||
|
fscrypt_provisioning_key_payload& provisioning_key =
|
||||||
|
*reinterpret_cast<fscrypt_provisioning_key_payload*>(buf.data());
|
||||||
|
memcpy(provisioning_key.raw, key.data(), key.size());
|
||||||
|
provisioning_key.type = key_spec.type;
|
||||||
|
|
||||||
|
key_serial_t key_id = add_key("fscrypt-provisioning", ref.c_str(), (void*)&provisioning_key,
|
||||||
|
buf.size(), device_keyring);
|
||||||
|
if (key_id == -1) {
|
||||||
|
PLOG(ERROR) << "Failed to insert fscrypt-provisioning key for " << ref
|
||||||
|
<< " into session keyring";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
LOG(DEBUG) << "Added fscrypt-provisioning key for " << ref << " to session keyring";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// Build a struct fscrypt_key_specifier for use in the key management ioctls.
|
// Build a struct fscrypt_key_specifier for use in the key management ioctls.
|
||||||
static bool buildKeySpecifier(fscrypt_key_specifier* spec, const EncryptionPolicy& policy) {
|
static bool buildKeySpecifier(fscrypt_key_specifier* spec, const EncryptionPolicy& policy) {
|
||||||
switch (policy.options.version) {
|
switch (policy.options.version) {
|
||||||
|
@ -205,6 +231,34 @@ static bool buildKeySpecifier(fscrypt_key_specifier* spec, const EncryptionPolic
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Installs key into keyring of a filesystem mounted on |mountpoint|.
|
||||||
|
//
|
||||||
|
// It's callers responsibility to fill key specifier, and either arg->raw or arg->key_id.
|
||||||
|
//
|
||||||
|
// In case arg->key_spec.type equals to FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER
|
||||||
|
// arg->key_spec.u.identifier will be populated with raw key reference generated
|
||||||
|
// by kernel.
|
||||||
|
//
|
||||||
|
// For documentation on difference between arg->raw and arg->key_id see
|
||||||
|
// https://www.kernel.org/doc/html/latest/filesystems/fscrypt.html#fs-ioc-add-encryption-key
|
||||||
|
static bool installFsKeyringKey(const std::string& mountpoint, const EncryptionOptions& options,
|
||||||
|
fscrypt_add_key_arg* arg) {
|
||||||
|
if (options.use_hw_wrapped_key) arg->flags |= FSCRYPT_ADD_KEY_FLAG_WRAPPED;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool installKey(const std::string& mountpoint, const EncryptionOptions& options,
|
bool installKey(const std::string& mountpoint, const EncryptionOptions& options,
|
||||||
const KeyBuffer& key, EncryptionPolicy* policy) {
|
const KeyBuffer& key, EncryptionPolicy* policy) {
|
||||||
policy->options = options;
|
policy->options = options;
|
||||||
|
@ -240,33 +294,24 @@ bool installKey(const std::string& mountpoint, const EncryptionOptions& options,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.use_hw_wrapped_key) arg->flags |= FSCRYPT_ADD_KEY_FLAG_WRAPPED;
|
|
||||||
// Provide the raw key.
|
|
||||||
arg->raw_size = key.size();
|
arg->raw_size = key.size();
|
||||||
memcpy(arg->raw, key.data(), 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 (!installFsKeyringKey(mountpoint, options, arg)) return false;
|
||||||
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) {
|
if (arg->key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
|
||||||
// Retrieve the key identifier that the kernel computed.
|
// Retrieve the key identifier that the kernel computed.
|
||||||
policy->key_raw_ref =
|
policy->key_raw_ref =
|
||||||
std::string((char*)arg->key_spec.u.identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
|
std::string((char*)arg->key_spec.u.identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
|
||||||
}
|
}
|
||||||
LOG(DEBUG) << "Installed fscrypt key with ref " << keyrefstring(policy->key_raw_ref) << " to "
|
std::string ref = keyrefstring(policy->key_raw_ref);
|
||||||
<< mountpoint;
|
LOG(DEBUG) << "Installed fscrypt key with ref " << ref << " to " << mountpoint;
|
||||||
|
|
||||||
|
if (!installProvisioningKey(key, ref, arg->key_spec)) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove an encryption key from the legacy global session keyring.
|
// Remove an encryption key of type "logon" from the global session keyring.
|
||||||
static bool evictKeyLegacy(const std::string& raw_ref) {
|
static bool evictKeyLegacy(const std::string& raw_ref) {
|
||||||
key_serial_t device_keyring;
|
key_serial_t device_keyring;
|
||||||
if (!fscryptKeyring(&device_keyring)) return false;
|
if (!fscryptKeyring(&device_keyring)) return false;
|
||||||
|
@ -289,6 +334,26 @@ static bool evictKeyLegacy(const std::string& raw_ref) {
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool evictProvisioningKey(const std::string& ref) {
|
||||||
|
key_serial_t device_keyring;
|
||||||
|
if (!fscryptKeyring(&device_keyring)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto key_serial = keyctl_search(device_keyring, "fscrypt-provisioning", ref.c_str(), 0);
|
||||||
|
if (key_serial == -1 && errno != ENOKEY) {
|
||||||
|
PLOG(ERROR) << "Error searching session keyring for fscrypt-provisioning key for " << ref;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key_serial != -1 && keyctl_unlink(key_serial, device_keyring) != 0) {
|
||||||
|
PLOG(ERROR) << "Failed to unlink fscrypt-provisioning key for " << ref
|
||||||
|
<< " from session keyring";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool evictKey(const std::string& mountpoint, const EncryptionPolicy& policy) {
|
bool evictKey(const std::string& mountpoint, const EncryptionPolicy& policy) {
|
||||||
if (policy.options.version == 1 && !isFsKeyringSupported()) {
|
if (policy.options.version == 1 && !isFsKeyringSupported()) {
|
||||||
return evictKeyLegacy(policy.key_raw_ref);
|
return evictKeyLegacy(policy.key_raw_ref);
|
||||||
|
@ -322,6 +387,8 @@ bool evictKey(const std::string& mountpoint, const EncryptionPolicy& policy) {
|
||||||
LOG(ERROR) << "Files still open after removing key with ref " << ref
|
LOG(ERROR) << "Files still open after removing key with ref " << ref
|
||||||
<< ". These files were not locked!";
|
<< ". These files were not locked!";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!evictProvisioningKey(ref)) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -343,5 +410,31 @@ bool retrieveOrGenerateKey(const std::string& key_path, const std::string& tmp_p
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool reloadKeyFromSessionKeyring(const std::string& mountpoint, const EncryptionPolicy& policy) {
|
||||||
|
key_serial_t device_keyring;
|
||||||
|
if (!fscryptKeyring(&device_keyring)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ref = keyrefstring(policy.key_raw_ref);
|
||||||
|
auto key_serial = keyctl_search(device_keyring, "fscrypt-provisioning", ref.c_str(), 0);
|
||||||
|
if (key_serial == -1) {
|
||||||
|
PLOG(ERROR) << "Failed to find fscrypt-provisioning key for " << ref
|
||||||
|
<< " in session keyring";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG(DEBUG) << "Installing fscrypt-provisioning key for " << ref << " back into " << mountpoint
|
||||||
|
<< " fs-keyring";
|
||||||
|
|
||||||
|
struct fscrypt_add_key_arg arg;
|
||||||
|
memset(&arg, 0, sizeof(arg));
|
||||||
|
if (!buildKeySpecifier(&arg.key_spec, policy)) return false;
|
||||||
|
arg.key_id = key_serial;
|
||||||
|
if (!installFsKeyringKey(mountpoint, policy.options, &arg)) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace vold
|
} // namespace vold
|
||||||
} // namespace android
|
} // namespace android
|
||||||
|
|
17
KeyUtil.h
17
KeyUtil.h
|
@ -51,11 +51,16 @@ bool isFsKeyringSupported(void);
|
||||||
// on the specified filesystem using the specified encryption policy version.
|
// 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.
|
// 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.
|
// Otherwise we add the key to the global session keyring as a "logon" key.
|
||||||
//
|
//
|
||||||
// For v2 policies, we always use FS_IOC_ADD_ENCRYPTION_KEY; it's the only way
|
// For v2 policies, we always use FS_IOC_ADD_ENCRYPTION_KEY; it's the only way
|
||||||
// the kernel supports.
|
// the kernel supports.
|
||||||
//
|
//
|
||||||
|
// If kernel supports FS_IOC_ADD_ENCRYPTION_KEY, also installs key of
|
||||||
|
// fscrypt-provisioning type to the global session keyring. This makes it
|
||||||
|
// possible to unmount and then remount mountpoint without losing the file-based
|
||||||
|
// key.
|
||||||
|
//
|
||||||
// Returns %true on success, %false on failure. On success also sets *policy
|
// Returns %true on success, %false on failure. On success also sets *policy
|
||||||
// to the EncryptionPolicy used to refer to this key.
|
// to the EncryptionPolicy used to refer to this key.
|
||||||
bool installKey(const std::string& mountpoint, const EncryptionOptions& options,
|
bool installKey(const std::string& mountpoint, const EncryptionOptions& options,
|
||||||
|
@ -63,16 +68,20 @@ bool installKey(const std::string& mountpoint, const EncryptionOptions& options,
|
||||||
|
|
||||||
// Evict a file-based encryption key from the kernel.
|
// Evict a file-based encryption key from the kernel.
|
||||||
//
|
//
|
||||||
// We use FS_IOC_REMOVE_ENCRYPTION_KEY if the kernel supports it. Otherwise we
|
// This undoes the effect of installKey().
|
||||||
// remove the key from the legacy global session keyring.
|
|
||||||
//
|
//
|
||||||
// In the latter case, the caller is responsible for dropping caches.
|
// If the kernel doesn't support the filesystem-level keyring, the caller is
|
||||||
|
// responsible for dropping caches.
|
||||||
bool evictKey(const std::string& mountpoint, const EncryptionPolicy& policy);
|
bool evictKey(const std::string& mountpoint, const EncryptionPolicy& policy);
|
||||||
|
|
||||||
bool retrieveOrGenerateKey(const std::string& key_path, const std::string& tmp_path,
|
bool retrieveOrGenerateKey(const std::string& key_path, const std::string& tmp_path,
|
||||||
const KeyAuthentication& key_authentication, const KeyGeneration& gen,
|
const KeyAuthentication& key_authentication, const KeyGeneration& gen,
|
||||||
KeyBuffer* key, bool keepOld = true);
|
KeyBuffer* key, bool keepOld = true);
|
||||||
|
|
||||||
|
// Re-installs a file-based encryption key of fscrypt-provisioning type from the
|
||||||
|
// global session keyring back into fs keyring of the mountpoint.
|
||||||
|
bool reloadKeyFromSessionKeyring(const std::string& mountpoint, const EncryptionPolicy& policy);
|
||||||
|
|
||||||
} // namespace vold
|
} // namespace vold
|
||||||
} // namespace android
|
} // namespace android
|
||||||
|
|
||||||
|
|
|
@ -9,11 +9,19 @@
|
||||||
struct sys_fscrypt_add_key_arg {
|
struct sys_fscrypt_add_key_arg {
|
||||||
struct fscrypt_key_specifier key_spec;
|
struct fscrypt_key_specifier key_spec;
|
||||||
__u32 raw_size;
|
__u32 raw_size;
|
||||||
__u32 __reserved[8];
|
__u32 key_id;
|
||||||
|
__u32 __reserved[7];
|
||||||
__u32 flags;
|
__u32 flags;
|
||||||
__u8 raw[];
|
__u8 raw[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct sys_fscrypt_provisioning_key_payload {
|
||||||
|
__u32 type;
|
||||||
|
__u32 __reserved;
|
||||||
|
__u8 raw[];
|
||||||
|
};
|
||||||
|
|
||||||
#define fscrypt_add_key_arg sys_fscrypt_add_key_arg
|
#define fscrypt_add_key_arg sys_fscrypt_add_key_arg
|
||||||
|
#define fscrypt_provisioning_key_payload sys_fscrypt_provisioning_key_payload
|
||||||
|
|
||||||
#endif //_UAPI_LINUX_FSCRYPT_VOLD_H
|
#endif //_UAPI_LINUX_FSCRYPT_VOLD_H
|
||||||
|
|
Loading…
Reference in a new issue