Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "KeyUtil.h" |
| 18 | |
| 19 | #include <iomanip> |
| 20 | #include <sstream> |
| 21 | #include <string> |
Nathan Huckleberry | 759ac5f | 2023-02-22 02:28:28 +0000 | [diff] [blame] | 22 | #include <thread> |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 23 | |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 24 | #include <fcntl.h> |
Eric Biggers | 3e9c996 | 2019-12-16 15:55:12 -0800 | [diff] [blame] | 25 | #include <linux/fscrypt.h> |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 26 | #include <openssl/sha.h> |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 27 | #include <sys/ioctl.h> |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 28 | |
| 29 | #include <android-base/file.h> |
| 30 | #include <android-base/logging.h> |
Nikita Ioffe | eea8bd3 | 2020-04-20 22:21:49 +0100 | [diff] [blame] | 31 | #include <android-base/properties.h> |
Elliott Hughes | c3bda18 | 2017-05-09 17:01:04 -0700 | [diff] [blame] | 32 | #include <keyutils.h> |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 33 | |
| 34 | #include "KeyStorage.h" |
| 35 | #include "Utils.h" |
| 36 | |
| 37 | namespace android { |
| 38 | namespace vold { |
| 39 | |
Paul Crowley | f443038 | 2020-04-05 19:34:31 -0700 | [diff] [blame] | 40 | using android::fscrypt::EncryptionOptions; |
| 41 | using android::fscrypt::EncryptionPolicy; |
| 42 | |
Nathan Huckleberry | 759ac5f | 2023-02-22 02:28:28 +0000 | [diff] [blame] | 43 | // This must be acquired before calling fscrypt ioctls that operate on keys. |
| 44 | // This prevents race conditions between evicting and reinstalling keys. |
| 45 | static std::mutex fscrypt_keyring_mutex; |
| 46 | |
Paul Crowley | 4eac264 | 2020-02-12 11:04:05 -0800 | [diff] [blame] | 47 | const KeyGeneration neverGen() { |
| 48 | return KeyGeneration{0, false, false}; |
| 49 | } |
| 50 | |
| 51 | static bool randomKey(size_t size, KeyBuffer* key) { |
| 52 | *key = KeyBuffer(size); |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 53 | if (ReadRandomBytes(key->size(), key->data()) != 0) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 54 | // TODO status_t plays badly with PLOG, fix it. |
| 55 | LOG(ERROR) << "Random read failed"; |
| 56 | return false; |
| 57 | } |
| 58 | return true; |
| 59 | } |
| 60 | |
Paul Crowley | 4eac264 | 2020-02-12 11:04:05 -0800 | [diff] [blame] | 61 | bool generateStorageKey(const KeyGeneration& gen, KeyBuffer* key) { |
David Anderson | e179157 | 2021-11-05 18:57:49 -0700 | [diff] [blame] | 62 | if (!gen.allow_gen) { |
| 63 | LOG(ERROR) << "Generating storage key not allowed"; |
| 64 | return false; |
| 65 | } |
Paul Crowley | 4eac264 | 2020-02-12 11:04:05 -0800 | [diff] [blame] | 66 | if (gen.use_hw_wrapped_key) { |
| 67 | if (gen.keysize != FSCRYPT_MAX_KEY_SIZE) { |
| 68 | LOG(ERROR) << "Cannot generate a wrapped key " << gen.keysize << " bytes long"; |
| 69 | return false; |
| 70 | } |
Eric Biggers | b2024e0 | 2021-03-15 12:44:36 -0700 | [diff] [blame] | 71 | LOG(DEBUG) << "Generating wrapped storage key"; |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 72 | return generateWrappedStorageKey(key); |
Paul Crowley | 4eac264 | 2020-02-12 11:04:05 -0800 | [diff] [blame] | 73 | } else { |
Eric Biggers | b2024e0 | 2021-03-15 12:44:36 -0700 | [diff] [blame] | 74 | LOG(DEBUG) << "Generating standard storage key"; |
Paul Crowley | 4eac264 | 2020-02-12 11:04:05 -0800 | [diff] [blame] | 75 | return randomKey(gen.keysize, key); |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 76 | } |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Eric Biggers | 7604eb9 | 2020-07-16 14:29:59 -0700 | [diff] [blame] | 79 | static bool isFsKeyringSupportedImpl() { |
| 80 | android::base::unique_fd fd(open("/data", O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 81 | |
| 82 | // FS_IOC_ADD_ENCRYPTION_KEY with a NULL argument will fail with ENOTTY if |
| 83 | // the ioctl isn't supported. Otherwise it will fail with another error |
| 84 | // code such as EFAULT. |
| 85 | // |
| 86 | // Note that there's no need to check for FS_IOC_REMOVE_ENCRYPTION_KEY, |
| 87 | // since it's guaranteed to be available if FS_IOC_ADD_ENCRYPTION_KEY is. |
| 88 | // There's also no need to check for support on external volumes separately |
| 89 | // from /data, since either the kernel supports the ioctls on all |
| 90 | // fscrypt-capable filesystems or it doesn't. |
| 91 | errno = 0; |
| 92 | (void)ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, NULL); |
| 93 | if (errno == ENOTTY) { |
| 94 | LOG(INFO) << "Kernel doesn't support FS_IOC_ADD_ENCRYPTION_KEY. Falling back to " |
| 95 | "session keyring"; |
| 96 | return false; |
| 97 | } |
| 98 | if (errno != EFAULT) { |
| 99 | PLOG(WARNING) << "Unexpected error from FS_IOC_ADD_ENCRYPTION_KEY"; |
| 100 | } |
| 101 | LOG(DEBUG) << "Detected support for FS_IOC_ADD_ENCRYPTION_KEY"; |
| 102 | android::base::SetProperty("ro.crypto.uses_fs_ioc_add_encryption_key", "true"); |
| 103 | return true; |
| 104 | } |
| 105 | |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 106 | // Return true if the kernel supports the ioctls to add/remove fscrypt keys |
| 107 | // directly to/from the filesystem. |
| 108 | bool isFsKeyringSupported(void) { |
Eric Biggers | 7604eb9 | 2020-07-16 14:29:59 -0700 | [diff] [blame] | 109 | static bool supported = isFsKeyringSupportedImpl(); |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 110 | return supported; |
| 111 | } |
| 112 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 113 | // Get raw keyref - used to make keyname and to pass to ioctl |
Eric Biggers | ba997ee | 2018-10-23 13:07:43 -0700 | [diff] [blame] | 114 | static std::string generateKeyRef(const uint8_t* key, int length) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 115 | SHA512_CTX c; |
| 116 | |
| 117 | SHA512_Init(&c); |
| 118 | SHA512_Update(&c, key, length); |
| 119 | unsigned char key_ref1[SHA512_DIGEST_LENGTH]; |
| 120 | SHA512_Final(key_ref1, &c); |
| 121 | |
| 122 | SHA512_Init(&c); |
| 123 | SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH); |
| 124 | unsigned char key_ref2[SHA512_DIGEST_LENGTH]; |
| 125 | SHA512_Final(key_ref2, &c); |
| 126 | |
Eric Biggers | 506342f | 2019-12-17 13:11:25 -0800 | [diff] [blame] | 127 | static_assert(FSCRYPT_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH, |
| 128 | "Hash too short for descriptor"); |
| 129 | return std::string((char*)key_ref2, FSCRYPT_KEY_DESCRIPTOR_SIZE); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 132 | static bool fillKey(const KeyBuffer& key, fscrypt_key* fs_key) { |
Eric Biggers | 506342f | 2019-12-17 13:11:25 -0800 | [diff] [blame] | 133 | if (key.size() != FSCRYPT_MAX_KEY_SIZE) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 134 | LOG(ERROR) << "Wrong size key " << key.size(); |
| 135 | return false; |
| 136 | } |
Eric Biggers | 506342f | 2019-12-17 13:11:25 -0800 | [diff] [blame] | 137 | static_assert(FSCRYPT_MAX_KEY_SIZE == sizeof(fs_key->raw), "Mismatch of max key sizes"); |
| 138 | fs_key->mode = 0; // unused by kernel |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 139 | memcpy(fs_key->raw, key.data(), key.size()); |
Eric Biggers | 506342f | 2019-12-17 13:11:25 -0800 | [diff] [blame] | 140 | fs_key->size = key.size(); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 144 | static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr}; |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 145 | |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 146 | static std::string keyrefstring(const std::string& raw_ref) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 147 | std::ostringstream o; |
Chen, Luhai | 5744dfe | 2017-08-18 14:49:45 +0800 | [diff] [blame] | 148 | for (unsigned char i : raw_ref) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 149 | o << std::hex << std::setw(2) << std::setfill('0') << (int)i; |
| 150 | } |
| 151 | return o.str(); |
| 152 | } |
| 153 | |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 154 | static std::string buildLegacyKeyName(const std::string& prefix, const std::string& raw_ref) { |
| 155 | return prefix + ":" + keyrefstring(raw_ref); |
| 156 | } |
| 157 | |
| 158 | // Get the ID of the keyring we store all fscrypt keys in when the kernel is too |
| 159 | // old to support FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY. |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 160 | static bool fscryptKeyring(key_serial_t* device_keyring) { |
| 161 | *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "fscrypt", 0); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 162 | if (*device_keyring == -1) { |
| 163 | PLOG(ERROR) << "Unable to find device keyring"; |
| 164 | return false; |
| 165 | } |
| 166 | return true; |
| 167 | } |
| 168 | |
Nikita Ioffe | 1c6731c | 2020-02-28 19:50:31 +0000 | [diff] [blame] | 169 | // Add an encryption key of type "logon" to the global session keyring. |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 170 | static bool installKeyLegacy(const KeyBuffer& key, const std::string& raw_ref) { |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 171 | // Place fscrypt_key into automatically zeroing buffer. |
| 172 | KeyBuffer fsKeyBuffer(sizeof(fscrypt_key)); |
| 173 | fscrypt_key& fs_key = *reinterpret_cast<fscrypt_key*>(fsKeyBuffer.data()); |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 174 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 175 | if (!fillKey(key, &fs_key)) return false; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 176 | key_serial_t device_keyring; |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 177 | if (!fscryptKeyring(&device_keyring)) return false; |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 178 | for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) { |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 179 | auto ref = buildLegacyKeyName(*name_prefix, raw_ref); |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 180 | key_serial_t key_id = |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 181 | add_key("logon", ref.c_str(), (void*)&fs_key, sizeof(fs_key), device_keyring); |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 182 | if (key_id == -1) { |
| 183 | PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring; |
| 184 | return false; |
| 185 | } |
| 186 | LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring |
| 187 | << " in process " << getpid(); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 188 | } |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 189 | return true; |
| 190 | } |
| 191 | |
Nikita Ioffe | 1c6731c | 2020-02-28 19:50:31 +0000 | [diff] [blame] | 192 | // Installs fscrypt-provisioning key into session level kernel keyring. |
| 193 | // This allows for the given key to be installed back into filesystem keyring. |
| 194 | // For more context see reloadKeyFromSessionKeyring. |
| 195 | static bool installProvisioningKey(const KeyBuffer& key, const std::string& ref, |
| 196 | const fscrypt_key_specifier& key_spec) { |
| 197 | key_serial_t device_keyring; |
| 198 | if (!fscryptKeyring(&device_keyring)) return false; |
| 199 | |
| 200 | // Place fscrypt_provisioning_key_payload into automatically zeroing buffer. |
| 201 | KeyBuffer buf(sizeof(fscrypt_provisioning_key_payload) + key.size(), 0); |
| 202 | fscrypt_provisioning_key_payload& provisioning_key = |
| 203 | *reinterpret_cast<fscrypt_provisioning_key_payload*>(buf.data()); |
| 204 | memcpy(provisioning_key.raw, key.data(), key.size()); |
| 205 | provisioning_key.type = key_spec.type; |
| 206 | |
| 207 | key_serial_t key_id = add_key("fscrypt-provisioning", ref.c_str(), (void*)&provisioning_key, |
| 208 | buf.size(), device_keyring); |
| 209 | if (key_id == -1) { |
| 210 | PLOG(ERROR) << "Failed to insert fscrypt-provisioning key for " << ref |
| 211 | << " into session keyring"; |
| 212 | return false; |
| 213 | } |
| 214 | LOG(DEBUG) << "Added fscrypt-provisioning key for " << ref << " to session keyring"; |
| 215 | return true; |
| 216 | } |
| 217 | |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 218 | // Build a struct fscrypt_key_specifier for use in the key management ioctls. |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 219 | static bool buildKeySpecifier(fscrypt_key_specifier* spec, const EncryptionPolicy& policy) { |
| 220 | switch (policy.options.version) { |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 221 | case 1: |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 222 | if (policy.key_raw_ref.size() != FSCRYPT_KEY_DESCRIPTOR_SIZE) { |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 223 | LOG(ERROR) << "Invalid key specifier size for v1 encryption policy: " |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 224 | << policy.key_raw_ref.size(); |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 225 | return false; |
| 226 | } |
| 227 | spec->type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR; |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 228 | memcpy(spec->u.descriptor, policy.key_raw_ref.c_str(), FSCRYPT_KEY_DESCRIPTOR_SIZE); |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 229 | return true; |
| 230 | case 2: |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 231 | if (policy.key_raw_ref.size() != FSCRYPT_KEY_IDENTIFIER_SIZE) { |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 232 | LOG(ERROR) << "Invalid key specifier size for v2 encryption policy: " |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 233 | << policy.key_raw_ref.size(); |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 234 | return false; |
| 235 | } |
| 236 | spec->type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 237 | memcpy(spec->u.identifier, policy.key_raw_ref.c_str(), FSCRYPT_KEY_IDENTIFIER_SIZE); |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 238 | return true; |
| 239 | default: |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 240 | LOG(ERROR) << "Invalid encryption policy version: " << policy.options.version; |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | } |
| 244 | |
Nikita Ioffe | 1c6731c | 2020-02-28 19:50:31 +0000 | [diff] [blame] | 245 | // Installs key into keyring of a filesystem mounted on |mountpoint|. |
| 246 | // |
| 247 | // It's callers responsibility to fill key specifier, and either arg->raw or arg->key_id. |
| 248 | // |
| 249 | // In case arg->key_spec.type equals to FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER |
| 250 | // arg->key_spec.u.identifier will be populated with raw key reference generated |
| 251 | // by kernel. |
| 252 | // |
| 253 | // For documentation on difference between arg->raw and arg->key_id see |
| 254 | // https://www.kernel.org/doc/html/latest/filesystems/fscrypt.html#fs-ioc-add-encryption-key |
| 255 | static bool installFsKeyringKey(const std::string& mountpoint, const EncryptionOptions& options, |
| 256 | fscrypt_add_key_arg* arg) { |
Eric Biggers | e0217d7 | 2020-07-16 16:31:00 -0700 | [diff] [blame] | 257 | if (options.use_hw_wrapped_key) arg->__flags |= __FSCRYPT_ADD_KEY_FLAG_HW_WRAPPED; |
Nikita Ioffe | 1c6731c | 2020-02-28 19:50:31 +0000 | [diff] [blame] | 258 | |
| 259 | android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 260 | if (fd == -1) { |
| 261 | PLOG(ERROR) << "Failed to open " << mountpoint << " to install key"; |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | if (ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, arg) != 0) { |
| 266 | PLOG(ERROR) << "Failed to install fscrypt key to " << mountpoint; |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 273 | bool installKey(const std::string& mountpoint, const EncryptionOptions& options, |
| 274 | const KeyBuffer& key, EncryptionPolicy* policy) { |
Nathan Huckleberry | 759ac5f | 2023-02-22 02:28:28 +0000 | [diff] [blame] | 275 | const std::lock_guard<std::mutex> lock(fscrypt_keyring_mutex); |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 276 | policy->options = options; |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 277 | // Put the fscrypt_add_key_arg in an automatically-zeroing buffer, since we |
| 278 | // have to copy the raw key into it. |
| 279 | KeyBuffer arg_buf(sizeof(struct fscrypt_add_key_arg) + key.size(), 0); |
| 280 | struct fscrypt_add_key_arg* arg = (struct fscrypt_add_key_arg*)arg_buf.data(); |
| 281 | |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 282 | // Initialize the "key specifier", which is like a name for the key. |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 283 | switch (options.version) { |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 284 | case 1: |
| 285 | // A key for a v1 policy is specified by an arbitrary 8-byte |
| 286 | // "descriptor", which must be provided by userspace. We use the |
| 287 | // first 8 bytes from the double SHA-512 of the key itself. |
Pig | adb9199 | 2020-09-25 22:56:33 +0800 | [diff] [blame] | 288 | if (options.use_hw_wrapped_key) { |
| 289 | /* When wrapped key is supported, only the first 32 bytes are |
| 290 | the same per boot. The second 32 bytes can change as the ephemeral |
| 291 | key is different. */ |
| 292 | policy->key_raw_ref = generateKeyRef((const uint8_t*)key.data(), key.size()/2); |
| 293 | } else { |
| 294 | policy->key_raw_ref = generateKeyRef((const uint8_t*)key.data(), key.size()); |
| 295 | } |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 296 | if (!isFsKeyringSupported()) { |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 297 | return installKeyLegacy(key, policy->key_raw_ref); |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 298 | } |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 299 | if (!buildKeySpecifier(&arg->key_spec, *policy)) { |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 300 | return false; |
| 301 | } |
| 302 | break; |
| 303 | case 2: |
| 304 | // A key for a v2 policy is specified by an 16-byte "identifier", |
| 305 | // which is a cryptographic hash of the key itself which the kernel |
| 306 | // computes and returns. Any user-provided value is ignored; we |
| 307 | // just need to set the specifier type to indicate that we're adding |
| 308 | // this type of key. |
| 309 | arg->key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; |
| 310 | break; |
| 311 | default: |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 312 | LOG(ERROR) << "Invalid encryption policy version: " << options.version; |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 313 | return false; |
| 314 | } |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 315 | |
| 316 | arg->raw_size = key.size(); |
| 317 | memcpy(arg->raw, key.data(), key.size()); |
| 318 | |
Nikita Ioffe | 1c6731c | 2020-02-28 19:50:31 +0000 | [diff] [blame] | 319 | if (!installFsKeyringKey(mountpoint, options, arg)) return false; |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 320 | |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 321 | if (arg->key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) { |
| 322 | // Retrieve the key identifier that the kernel computed. |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 323 | policy->key_raw_ref = |
| 324 | std::string((char*)arg->key_spec.u.identifier, FSCRYPT_KEY_IDENTIFIER_SIZE); |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 325 | } |
Nikita Ioffe | 1c6731c | 2020-02-28 19:50:31 +0000 | [diff] [blame] | 326 | std::string ref = keyrefstring(policy->key_raw_ref); |
| 327 | LOG(DEBUG) << "Installed fscrypt key with ref " << ref << " to " << mountpoint; |
| 328 | |
| 329 | if (!installProvisioningKey(key, ref, arg->key_spec)) return false; |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 330 | return true; |
| 331 | } |
| 332 | |
Nikita Ioffe | 1c6731c | 2020-02-28 19:50:31 +0000 | [diff] [blame] | 333 | // Remove an encryption key of type "logon" from the global session keyring. |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 334 | static bool evictKeyLegacy(const std::string& raw_ref) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 335 | key_serial_t device_keyring; |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 336 | if (!fscryptKeyring(&device_keyring)) return false; |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 337 | bool success = true; |
| 338 | for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) { |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 339 | auto ref = buildLegacyKeyName(*name_prefix, raw_ref); |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 340 | auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 341 | |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 342 | // Unlink the key from the keyring. Prefer unlinking to revoking or |
| 343 | // invalidating, since unlinking is actually no less secure currently, and |
| 344 | // it avoids bugs in certain kernel versions where the keyring key is |
| 345 | // referenced from places it shouldn't be. |
| 346 | if (keyctl_unlink(key_serial, device_keyring) != 0) { |
| 347 | PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref; |
| 348 | success = false; |
| 349 | } else { |
| 350 | LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref; |
| 351 | } |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 352 | } |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 353 | return success; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Nikita Ioffe | 1c6731c | 2020-02-28 19:50:31 +0000 | [diff] [blame] | 356 | static bool evictProvisioningKey(const std::string& ref) { |
| 357 | key_serial_t device_keyring; |
| 358 | if (!fscryptKeyring(&device_keyring)) { |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | auto key_serial = keyctl_search(device_keyring, "fscrypt-provisioning", ref.c_str(), 0); |
| 363 | if (key_serial == -1 && errno != ENOKEY) { |
| 364 | PLOG(ERROR) << "Error searching session keyring for fscrypt-provisioning key for " << ref; |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | if (key_serial != -1 && keyctl_unlink(key_serial, device_keyring) != 0) { |
| 369 | PLOG(ERROR) << "Failed to unlink fscrypt-provisioning key for " << ref |
| 370 | << " from session keyring"; |
| 371 | return false; |
| 372 | } |
| 373 | return true; |
| 374 | } |
| 375 | |
Nathan Huckleberry | 759ac5f | 2023-02-22 02:28:28 +0000 | [diff] [blame] | 376 | static void waitForBusyFiles(const struct fscrypt_key_specifier key_spec, const std::string ref, |
| 377 | const std::string mountpoint) { |
| 378 | android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 379 | if (fd == -1) { |
| 380 | PLOG(ERROR) << "Failed to open " << mountpoint << " to evict key"; |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | std::chrono::milliseconds wait_time(3200); |
| 385 | std::chrono::milliseconds total_wait_time(0); |
| 386 | while (wait_time <= std::chrono::milliseconds(51200)) { |
| 387 | total_wait_time += wait_time; |
| 388 | std::this_thread::sleep_for(wait_time); |
| 389 | |
| 390 | const std::lock_guard<std::mutex> lock(fscrypt_keyring_mutex); |
| 391 | |
| 392 | struct fscrypt_get_key_status_arg get_arg; |
| 393 | memset(&get_arg, 0, sizeof(get_arg)); |
| 394 | get_arg.key_spec = key_spec; |
| 395 | |
| 396 | if (ioctl(fd, FS_IOC_GET_ENCRYPTION_KEY_STATUS, &get_arg) != 0) { |
| 397 | PLOG(ERROR) << "Failed to get status for fscrypt key with ref " << ref << " from " |
| 398 | << mountpoint; |
| 399 | return; |
| 400 | } |
| 401 | if (get_arg.status != FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED) { |
| 402 | LOG(DEBUG) << "Key status changed, cancelling busy file cleanup for key with ref " |
| 403 | << ref << "."; |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | struct fscrypt_remove_key_arg remove_arg; |
| 408 | memset(&remove_arg, 0, sizeof(remove_arg)); |
| 409 | remove_arg.key_spec = key_spec; |
| 410 | |
| 411 | if (ioctl(fd, FS_IOC_REMOVE_ENCRYPTION_KEY, &remove_arg) != 0) { |
| 412 | PLOG(ERROR) << "Failed to clean up busy files for fscrypt key with ref " << ref |
| 413 | << " from " << mountpoint; |
| 414 | return; |
| 415 | } |
| 416 | if (remove_arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS) { |
| 417 | // Should never happen because keys are only added/removed as root. |
| 418 | LOG(ERROR) << "Unexpected case: key with ref " << ref |
| 419 | << " is still added by other users!"; |
| 420 | } else if (!(remove_arg.removal_status_flags & |
| 421 | FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY)) { |
| 422 | LOG(INFO) << "Successfully cleaned up busy files for key with ref " << ref |
| 423 | << ". After waiting " << total_wait_time.count() << "ms."; |
| 424 | return; |
| 425 | } |
| 426 | LOG(WARNING) << "Files still open after waiting " << total_wait_time.count() |
| 427 | << "ms. Key with ref " << ref << " still has unlocked files!"; |
| 428 | wait_time *= 2; |
| 429 | } |
| 430 | LOG(ERROR) << "Waiting for files to close never completed. Files using key with ref " << ref |
| 431 | << " were not locked!"; |
| 432 | } |
| 433 | |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 434 | bool evictKey(const std::string& mountpoint, const EncryptionPolicy& policy) { |
Nathan Huckleberry | 759ac5f | 2023-02-22 02:28:28 +0000 | [diff] [blame] | 435 | const std::lock_guard<std::mutex> lock(fscrypt_keyring_mutex); |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 436 | if (policy.options.version == 1 && !isFsKeyringSupported()) { |
| 437 | return evictKeyLegacy(policy.key_raw_ref); |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 441 | if (fd == -1) { |
| 442 | PLOG(ERROR) << "Failed to open " << mountpoint << " to evict key"; |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | struct fscrypt_remove_key_arg arg; |
| 447 | memset(&arg, 0, sizeof(arg)); |
| 448 | |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 449 | if (!buildKeySpecifier(&arg.key_spec, policy)) { |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 450 | return false; |
| 451 | } |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 452 | |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 453 | std::string ref = keyrefstring(policy.key_raw_ref); |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 454 | |
| 455 | if (ioctl(fd, FS_IOC_REMOVE_ENCRYPTION_KEY, &arg) != 0) { |
| 456 | PLOG(ERROR) << "Failed to evict fscrypt key with ref " << ref << " from " << mountpoint; |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | LOG(DEBUG) << "Evicted fscrypt key with ref " << ref << " from " << mountpoint; |
| 461 | if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS) { |
| 462 | // Should never happen because keys are only added/removed as root. |
| 463 | LOG(ERROR) << "Unexpected case: key with ref " << ref << " is still added by other users!"; |
| 464 | } else if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY) { |
Nathan Huckleberry | 759ac5f | 2023-02-22 02:28:28 +0000 | [diff] [blame] | 465 | LOG(WARNING) |
| 466 | << "Files still open after removing key with ref " << ref |
| 467 | << ". These files were not locked! Punting busy file clean up to worker thread."; |
| 468 | // Processes are killed asynchronously in ActivityManagerService due to performance issues |
| 469 | // with synchronous kills. If there were busy files they will probably be killed soon. Wait |
| 470 | // for them asynchronously. |
| 471 | std::thread busyFilesThread(waitForBusyFiles, arg.key_spec, ref, mountpoint); |
| 472 | busyFilesThread.detach(); |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 473 | } |
Nikita Ioffe | 1c6731c | 2020-02-28 19:50:31 +0000 | [diff] [blame] | 474 | |
| 475 | if (!evictProvisioningKey(ref)) return false; |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 476 | return true; |
| 477 | } |
| 478 | |
Paul Crowley | 4eac264 | 2020-02-12 11:04:05 -0800 | [diff] [blame] | 479 | bool retrieveOrGenerateKey(const std::string& key_path, const std::string& tmp_path, |
| 480 | const KeyAuthentication& key_authentication, const KeyGeneration& gen, |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 481 | KeyBuffer* key) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 482 | if (pathExists(key_path)) { |
| 483 | LOG(DEBUG) << "Key exists, using: " << key_path; |
Eric Biggers | f74373b | 2020-11-05 19:58:26 -0800 | [diff] [blame] | 484 | if (!retrieveKey(key_path, key_authentication, key)) return false; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 485 | } else { |
Paul Crowley | 4eac264 | 2020-02-12 11:04:05 -0800 | [diff] [blame] | 486 | if (!gen.allow_gen) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 487 | LOG(ERROR) << "No key found in " << key_path; |
| 488 | return false; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 489 | } |
| 490 | LOG(INFO) << "Creating new key in " << key_path; |
Paul Crowley | 4eac264 | 2020-02-12 11:04:05 -0800 | [diff] [blame] | 491 | if (!generateStorageKey(gen, key)) return false; |
Paul Crowley | 77df7f2 | 2020-01-23 15:29:30 -0800 | [diff] [blame] | 492 | if (!storeKeyAtomically(key_path, tmp_path, key_authentication, *key)) return false; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 493 | } |
| 494 | return true; |
| 495 | } |
| 496 | |
Nikita Ioffe | 1c6731c | 2020-02-28 19:50:31 +0000 | [diff] [blame] | 497 | bool reloadKeyFromSessionKeyring(const std::string& mountpoint, const EncryptionPolicy& policy) { |
| 498 | key_serial_t device_keyring; |
| 499 | if (!fscryptKeyring(&device_keyring)) { |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | std::string ref = keyrefstring(policy.key_raw_ref); |
| 504 | auto key_serial = keyctl_search(device_keyring, "fscrypt-provisioning", ref.c_str(), 0); |
| 505 | if (key_serial == -1) { |
| 506 | PLOG(ERROR) << "Failed to find fscrypt-provisioning key for " << ref |
| 507 | << " in session keyring"; |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | LOG(DEBUG) << "Installing fscrypt-provisioning key for " << ref << " back into " << mountpoint |
| 512 | << " fs-keyring"; |
| 513 | |
| 514 | struct fscrypt_add_key_arg arg; |
| 515 | memset(&arg, 0, sizeof(arg)); |
| 516 | if (!buildKeySpecifier(&arg.key_spec, policy)) return false; |
| 517 | arg.key_id = key_serial; |
| 518 | if (!installFsKeyringKey(mountpoint, policy.options, &arg)) return false; |
| 519 | |
| 520 | return true; |
| 521 | } |
| 522 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 523 | } // namespace vold |
| 524 | } // namespace android |