Revert "fskeyring & userspace reboot: support CE keys"

Userspace reboot turned out to be a dead end and is no longer supported.
Therefore, remove the code from vold that handled keeping CE storage
unlocked past the userdata filesystem being unmounted and mounted.

This is a revert of commit 1c6731c64972 (https://r.android.com/1254615)
with various conflicts resolved.

Bug: 292469129
Change-Id: If530edaf7c1566dd3bd8b1322f935f38a2e66beb
Merged-In: If530edaf7c1566dd3bd8b1322f935f38a2e66beb
(cherry picked from commit 2b97a88ba4f29b98efd8b31ce6f2f36a2334acaf)
diff --git a/KeyUtil.cpp b/KeyUtil.cpp
index 5a8b550..9e8920d 100644
--- a/KeyUtil.cpp
+++ b/KeyUtil.cpp
@@ -164,7 +164,7 @@
     return true;
 }
 
-// Add an encryption key of type "logon" to the global session keyring.
+// 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));
@@ -187,32 +187,6 @@
     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.
 static bool buildKeySpecifier(fscrypt_key_specifier* spec, const EncryptionPolicy& policy) {
     switch (policy.options.version) {
@@ -240,34 +214,6 @@
     }
 }
 
-// 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_HW_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,
                 const KeyBuffer& key, EncryptionPolicy* policy) {
     const std::lock_guard<std::mutex> lock(fscrypt_keyring_mutex);
@@ -304,24 +250,33 @@
             return false;
     }
 
+    if (options.use_hw_wrapped_key) arg->__flags |= __FSCRYPT_ADD_KEY_FLAG_HW_WRAPPED;
+    // Provide the raw key.
     arg->raw_size = key.size();
     memcpy(arg->raw, key.data(), key.size());
 
-    if (!installFsKeyringKey(mountpoint, options, arg)) return false;
+    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.
         policy->key_raw_ref =
                 std::string((char*)arg->key_spec.u.identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
     }
-    std::string ref = keyrefstring(policy->key_raw_ref);
-    LOG(DEBUG) << "Installed fscrypt key with ref " << ref << " to " << mountpoint;
-
-    if (!installProvisioningKey(key, ref, arg->key_spec)) return false;
+    LOG(DEBUG) << "Installed fscrypt key with ref " << keyrefstring(policy->key_raw_ref) << " to "
+               << mountpoint;
     return true;
 }
 
-// Remove an encryption key of type "logon" from the global session keyring.
+// 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;
@@ -344,26 +299,6 @@
     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;
-}
-
 static void waitForBusyFiles(const struct fscrypt_key_specifier key_spec, const std::string ref,
                              const std::string mountpoint) {
     android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
@@ -462,8 +397,6 @@
         std::thread busyFilesThread(waitForBusyFiles, arg.key_spec, ref, mountpoint);
         busyFilesThread.detach();
     }
-
-    if (!evictProvisioningKey(ref)) return false;
     return true;
 }
 
@@ -485,31 +418,5 @@
     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 android