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> |
| 22 | |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 23 | #include <fcntl.h> |
| 24 | #include <linux/fs.h> |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 25 | #include <openssl/sha.h> |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 26 | #include <sys/ioctl.h> |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 27 | |
| 28 | #include <android-base/file.h> |
| 29 | #include <android-base/logging.h> |
Elliott Hughes | c3bda18 | 2017-05-09 17:01:04 -0700 | [diff] [blame] | 30 | #include <keyutils.h> |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 31 | |
Scott Lobdell | 314dbc7 | 2018-12-05 16:01:16 -0800 | [diff] [blame] | 32 | #include "FsCrypt.h" |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 33 | #include "KeyStorage.h" |
| 34 | #include "Utils.h" |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 35 | #include "fscrypt_uapi.h" |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 36 | |
Shivaprasad Hongal | 9229262 | 2018-07-05 14:49:12 -0700 | [diff] [blame] | 37 | #define MAX_USER_ID 0xFFFFFFFF |
| 38 | |
| 39 | using android::hardware::keymaster::V4_0::KeyFormat; |
| 40 | using android::vold::KeyType; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | namespace vold { |
| 43 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 44 | constexpr int FS_AES_256_XTS_KEY_SIZE = 64; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 45 | |
| 46 | bool randomKey(KeyBuffer* key) { |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 47 | *key = KeyBuffer(FS_AES_256_XTS_KEY_SIZE); |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 48 | if (ReadRandomBytes(key->size(), key->data()) != 0) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 49 | // TODO status_t plays badly with PLOG, fix it. |
| 50 | LOG(ERROR) << "Random read failed"; |
| 51 | return false; |
| 52 | } |
| 53 | return true; |
| 54 | } |
| 55 | |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 56 | // Return true if the kernel supports the ioctls to add/remove fscrypt keys |
| 57 | // directly to/from the filesystem. |
| 58 | bool isFsKeyringSupported(void) { |
| 59 | static bool initialized = false; |
| 60 | static bool supported; |
| 61 | |
| 62 | if (!initialized) { |
| 63 | android::base::unique_fd fd(open("/data", O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 64 | |
| 65 | // FS_IOC_ADD_ENCRYPTION_KEY with a NULL argument will fail with ENOTTY |
| 66 | // if the ioctl isn't supported. Otherwise it will fail with another |
| 67 | // error code such as EFAULT. |
| 68 | errno = 0; |
| 69 | (void)ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, NULL); |
| 70 | if (errno == ENOTTY) { |
| 71 | LOG(INFO) << "Kernel doesn't support FS_IOC_ADD_ENCRYPTION_KEY. Falling back to " |
| 72 | "session keyring"; |
| 73 | supported = false; |
| 74 | } else { |
| 75 | if (errno != EFAULT) { |
| 76 | PLOG(WARNING) << "Unexpected error from FS_IOC_ADD_ENCRYPTION_KEY"; |
| 77 | } |
| 78 | LOG(DEBUG) << "Detected support for FS_IOC_ADD_ENCRYPTION_KEY"; |
| 79 | supported = true; |
| 80 | } |
| 81 | // There's no need to check for FS_IOC_REMOVE_ENCRYPTION_KEY, since it's |
| 82 | // guaranteed to be available if FS_IOC_ADD_ENCRYPTION_KEY is. There's |
| 83 | // also no need to check for support on external volumes separately from |
| 84 | // /data, since either the kernel supports the ioctls on all |
| 85 | // fscrypt-capable filesystems or it doesn't. |
| 86 | |
| 87 | initialized = true; |
| 88 | } |
| 89 | return supported; |
| 90 | } |
| 91 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 92 | // Get raw keyref - used to make keyname and to pass to ioctl |
Eric Biggers | ba997ee | 2018-10-23 13:07:43 -0700 | [diff] [blame] | 93 | static std::string generateKeyRef(const uint8_t* key, int length) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 94 | SHA512_CTX c; |
| 95 | |
| 96 | SHA512_Init(&c); |
| 97 | SHA512_Update(&c, key, length); |
| 98 | unsigned char key_ref1[SHA512_DIGEST_LENGTH]; |
| 99 | SHA512_Final(key_ref1, &c); |
| 100 | |
| 101 | SHA512_Init(&c); |
| 102 | SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH); |
| 103 | unsigned char key_ref2[SHA512_DIGEST_LENGTH]; |
| 104 | SHA512_Final(key_ref2, &c); |
| 105 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 106 | static_assert(FS_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH, "Hash too short for descriptor"); |
| 107 | return std::string((char*)key_ref2, FS_KEY_DESCRIPTOR_SIZE); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 110 | static bool fillKey(const KeyBuffer& key, fscrypt_key* fs_key) { |
| 111 | if (key.size() != FS_AES_256_XTS_KEY_SIZE) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 112 | LOG(ERROR) << "Wrong size key " << key.size(); |
| 113 | return false; |
| 114 | } |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 115 | static_assert(FS_AES_256_XTS_KEY_SIZE <= sizeof(fs_key->raw), "Key too long!"); |
| 116 | fs_key->mode = FS_ENCRYPTION_MODE_AES_256_XTS; |
| 117 | fs_key->size = key.size(); |
| 118 | memset(fs_key->raw, 0, sizeof(fs_key->raw)); |
| 119 | memcpy(fs_key->raw, key.data(), key.size()); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 123 | static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr}; |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 124 | |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 125 | static std::string keyrefstring(const std::string& raw_ref) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 126 | std::ostringstream o; |
Chen, Luhai | 5744dfe | 2017-08-18 14:49:45 +0800 | [diff] [blame] | 127 | for (unsigned char i : raw_ref) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 128 | o << std::hex << std::setw(2) << std::setfill('0') << (int)i; |
| 129 | } |
| 130 | return o.str(); |
| 131 | } |
| 132 | |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 133 | static std::string buildLegacyKeyName(const std::string& prefix, const std::string& raw_ref) { |
| 134 | return prefix + ":" + keyrefstring(raw_ref); |
| 135 | } |
| 136 | |
| 137 | // Get the ID of the keyring we store all fscrypt keys in when the kernel is too |
| 138 | // 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] | 139 | static bool fscryptKeyring(key_serial_t* device_keyring) { |
| 140 | *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "fscrypt", 0); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 141 | if (*device_keyring == -1) { |
| 142 | PLOG(ERROR) << "Unable to find device keyring"; |
| 143 | return false; |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 148 | // Add an encryption key to the legacy global session keyring. |
| 149 | static bool installKeyLegacy(const KeyBuffer& key, const std::string& raw_ref) { |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 150 | // Place fscrypt_key into automatically zeroing buffer. |
| 151 | KeyBuffer fsKeyBuffer(sizeof(fscrypt_key)); |
| 152 | fscrypt_key& fs_key = *reinterpret_cast<fscrypt_key*>(fsKeyBuffer.data()); |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 153 | |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 154 | if (!fillKey(key, &fs_key)) return false; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 155 | key_serial_t device_keyring; |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 156 | if (!fscryptKeyring(&device_keyring)) return false; |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 157 | 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] | 158 | auto ref = buildLegacyKeyName(*name_prefix, raw_ref); |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 159 | key_serial_t key_id = |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 160 | 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] | 161 | if (key_id == -1) { |
| 162 | PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring; |
| 163 | return false; |
| 164 | } |
| 165 | LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring |
| 166 | << " in process " << getpid(); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 167 | } |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 168 | return true; |
| 169 | } |
| 170 | |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 171 | // Build a struct fscrypt_key_specifier for use in the key management ioctls. |
| 172 | static bool buildKeySpecifier(fscrypt_key_specifier* spec, const std::string& raw_ref, |
| 173 | int policy_version) { |
| 174 | switch (policy_version) { |
| 175 | case 1: |
| 176 | if (raw_ref.size() != FSCRYPT_KEY_DESCRIPTOR_SIZE) { |
| 177 | LOG(ERROR) << "Invalid key specifier size for v1 encryption policy: " |
| 178 | << raw_ref.size(); |
| 179 | return false; |
| 180 | } |
| 181 | spec->type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR; |
| 182 | memcpy(spec->u.descriptor, raw_ref.c_str(), FSCRYPT_KEY_DESCRIPTOR_SIZE); |
| 183 | return true; |
| 184 | case 2: |
| 185 | if (raw_ref.size() != FSCRYPT_KEY_IDENTIFIER_SIZE) { |
| 186 | LOG(ERROR) << "Invalid key specifier size for v2 encryption policy: " |
| 187 | << raw_ref.size(); |
| 188 | return false; |
| 189 | } |
| 190 | spec->type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; |
| 191 | memcpy(spec->u.identifier, raw_ref.c_str(), FSCRYPT_KEY_IDENTIFIER_SIZE); |
| 192 | return true; |
| 193 | default: |
| 194 | LOG(ERROR) << "Invalid encryption policy version: " << policy_version; |
| 195 | return false; |
| 196 | } |
| 197 | } |
| 198 | |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 199 | // Install a file-based encryption key to the kernel, for use by encrypted files |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 200 | // on the specified filesystem using the specified encryption policy version. |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 201 | // |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 202 | // For v1 policies, we use FS_IOC_ADD_ENCRYPTION_KEY if the kernel supports it. |
| 203 | // Otherwise we add the key to the legacy global session keyring. |
| 204 | // |
| 205 | // For v2 policies, we always use FS_IOC_ADD_ENCRYPTION_KEY; it's the only way |
| 206 | // the kernel supports. |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 207 | // |
| 208 | // Returns %true on success, %false on failure. On success also sets *raw_ref |
| 209 | // to the raw key reference for use in the encryption policy. |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 210 | bool installKey(const KeyBuffer& key, const std::string& mountpoint, int policy_version, |
| 211 | std::string* raw_ref) { |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 212 | // Put the fscrypt_add_key_arg in an automatically-zeroing buffer, since we |
| 213 | // have to copy the raw key into it. |
| 214 | KeyBuffer arg_buf(sizeof(struct fscrypt_add_key_arg) + key.size(), 0); |
| 215 | struct fscrypt_add_key_arg* arg = (struct fscrypt_add_key_arg*)arg_buf.data(); |
| 216 | |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 217 | // Initialize the "key specifier", which is like a name for the key. |
| 218 | switch (policy_version) { |
| 219 | case 1: |
| 220 | // A key for a v1 policy is specified by an arbitrary 8-byte |
| 221 | // "descriptor", which must be provided by userspace. We use the |
| 222 | // first 8 bytes from the double SHA-512 of the key itself. |
Steven Laver | ed747be | 2019-10-24 16:40:01 -0700 | [diff] [blame^] | 223 | if (is_wrapped_key_supported()) { |
| 224 | /* When wrapped key is supported, only the first 32 bytes are |
| 225 | the same per boot. The second 32 bytes can change as the ephemeral |
| 226 | key is different. */ |
| 227 | *raw_ref = generateKeyRef((const uint8_t*)key.data(), key.size()/2); |
| 228 | } else { |
| 229 | *raw_ref = generateKeyRef((const uint8_t*)key.data(), key.size()); |
| 230 | } |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 231 | if (!isFsKeyringSupported()) { |
| 232 | return installKeyLegacy(key, *raw_ref); |
| 233 | } |
| 234 | if (!buildKeySpecifier(&arg->key_spec, *raw_ref, policy_version)) { |
| 235 | return false; |
| 236 | } |
| 237 | break; |
| 238 | case 2: |
| 239 | // A key for a v2 policy is specified by an 16-byte "identifier", |
| 240 | // which is a cryptographic hash of the key itself which the kernel |
| 241 | // computes and returns. Any user-provided value is ignored; we |
| 242 | // just need to set the specifier type to indicate that we're adding |
| 243 | // this type of key. |
| 244 | arg->key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; |
| 245 | break; |
| 246 | default: |
| 247 | LOG(ERROR) << "Invalid encryption policy version: " << policy_version; |
| 248 | return false; |
| 249 | } |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 250 | |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 251 | // Provide the raw key. |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 252 | arg->raw_size = key.size(); |
| 253 | memcpy(arg->raw, key.data(), key.size()); |
| 254 | |
| 255 | android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 256 | if (fd == -1) { |
| 257 | PLOG(ERROR) << "Failed to open " << mountpoint << " to install key"; |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | if (ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, arg) != 0) { |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 262 | PLOG(ERROR) << "Failed to install fscrypt key to " << mountpoint; |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 263 | return false; |
| 264 | } |
| 265 | |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 266 | if (arg->key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) { |
| 267 | // Retrieve the key identifier that the kernel computed. |
| 268 | *raw_ref = std::string((char*)arg->key_spec.u.identifier, FSCRYPT_KEY_IDENTIFIER_SIZE); |
| 269 | } |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 270 | LOG(DEBUG) << "Installed fscrypt key with ref " << keyrefstring(*raw_ref) << " to " |
| 271 | << mountpoint; |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | // Remove an encryption key from the legacy global session keyring. |
| 276 | static bool evictKeyLegacy(const std::string& raw_ref) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 277 | key_serial_t device_keyring; |
Eric Biggers | a701c45 | 2018-10-23 13:06:55 -0700 | [diff] [blame] | 278 | if (!fscryptKeyring(&device_keyring)) return false; |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 279 | bool success = true; |
| 280 | 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] | 281 | auto ref = buildLegacyKeyName(*name_prefix, raw_ref); |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 282 | auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0); |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 283 | |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 284 | // Unlink the key from the keyring. Prefer unlinking to revoking or |
| 285 | // invalidating, since unlinking is actually no less secure currently, and |
| 286 | // it avoids bugs in certain kernel versions where the keyring key is |
| 287 | // referenced from places it shouldn't be. |
| 288 | if (keyctl_unlink(key_serial, device_keyring) != 0) { |
| 289 | PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref; |
| 290 | success = false; |
| 291 | } else { |
| 292 | LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref; |
| 293 | } |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 294 | } |
Paul Crowley | cd8bfe3 | 2017-06-19 16:05:55 -0700 | [diff] [blame] | 295 | return success; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 298 | // Evict a file-based encryption key from the kernel. |
| 299 | // |
| 300 | // We use FS_IOC_REMOVE_ENCRYPTION_KEY if the kernel supports it. Otherwise we |
| 301 | // remove the key from the legacy global session keyring. |
| 302 | // |
| 303 | // In the latter case, the caller is responsible for dropping caches. |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 304 | bool evictKey(const std::string& mountpoint, const std::string& raw_ref, int policy_version) { |
| 305 | if (policy_version == 1 && !isFsKeyringSupported()) { |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 306 | return evictKeyLegacy(raw_ref); |
| 307 | } |
| 308 | |
| 309 | android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC)); |
| 310 | if (fd == -1) { |
| 311 | PLOG(ERROR) << "Failed to open " << mountpoint << " to evict key"; |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | struct fscrypt_remove_key_arg arg; |
| 316 | memset(&arg, 0, sizeof(arg)); |
| 317 | |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 318 | if (!buildKeySpecifier(&arg.key_spec, raw_ref, policy_version)) { |
| 319 | return false; |
| 320 | } |
Eric Biggers | f3dc420 | 2019-09-30 13:05:58 -0700 | [diff] [blame] | 321 | |
| 322 | std::string ref = keyrefstring(raw_ref); |
| 323 | |
| 324 | if (ioctl(fd, FS_IOC_REMOVE_ENCRYPTION_KEY, &arg) != 0) { |
| 325 | PLOG(ERROR) << "Failed to evict fscrypt key with ref " << ref << " from " << mountpoint; |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | LOG(DEBUG) << "Evicted fscrypt key with ref " << ref << " from " << mountpoint; |
| 330 | if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS) { |
| 331 | // Should never happen because keys are only added/removed as root. |
| 332 | LOG(ERROR) << "Unexpected case: key with ref " << ref << " is still added by other users!"; |
| 333 | } else if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY) { |
| 334 | LOG(ERROR) << "Files still open after removing key with ref " << ref |
| 335 | << ". These files were not locked!"; |
| 336 | } |
| 337 | return true; |
| 338 | } |
| 339 | |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 340 | bool retrieveAndInstallKey(bool create_if_absent, const KeyAuthentication& key_authentication, |
| 341 | const std::string& key_path, const std::string& tmp_path, |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 342 | const std::string& volume_uuid, int policy_version, |
Shivaprasad Hongal | 9229262 | 2018-07-05 14:49:12 -0700 | [diff] [blame] | 343 | std::string* key_ref, bool wrapped_key_supported) { |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 344 | KeyBuffer key; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 345 | if (pathExists(key_path)) { |
| 346 | LOG(DEBUG) << "Key exists, using: " << key_path; |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 347 | if (!retrieveKey(key_path, key_authentication, &key)) return false; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 348 | } else { |
| 349 | if (!create_if_absent) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 350 | LOG(ERROR) << "No key found in " << key_path; |
| 351 | return false; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 352 | } |
| 353 | LOG(INFO) << "Creating new key in " << key_path; |
Shivaprasad Hongal | 9229262 | 2018-07-05 14:49:12 -0700 | [diff] [blame] | 354 | if (wrapped_key_supported) { |
| 355 | if(!generateWrappedKey(MAX_USER_ID, KeyType::DE_SYS, &key)) return false; |
| 356 | } else { |
| 357 | if (!randomKey(&key)) return false; |
| 358 | } |
Paul Crowley | 26a5388 | 2017-10-26 11:16:39 -0700 | [diff] [blame] | 359 | if (!storeKeyAtomically(key_path, tmp_path, key_authentication, key)) return false; |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Shivaprasad Hongal | 9229262 | 2018-07-05 14:49:12 -0700 | [diff] [blame] | 362 | if (wrapped_key_supported) { |
| 363 | KeyBuffer ephemeral_wrapped_key; |
| 364 | if (!getEphemeralWrappedKey(KeyFormat::RAW, key, &ephemeral_wrapped_key)) { |
| 365 | LOG(ERROR) << "Failed to export key in retrieveAndInstallKey"; |
| 366 | return false; |
| 367 | } |
| 368 | key = std::move(ephemeral_wrapped_key); |
| 369 | } |
| 370 | |
Eric Biggers | 83a73d7 | 2019-09-30 13:06:47 -0700 | [diff] [blame] | 371 | if (!installKey(key, BuildDataPath(volume_uuid), policy_version, key_ref)) { |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 372 | LOG(ERROR) << "Failed to install key in " << key_path; |
| 373 | return false; |
| 374 | } |
| 375 | return true; |
| 376 | } |
| 377 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 378 | bool retrieveKey(bool create_if_absent, const std::string& key_path, const std::string& tmp_path, |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 379 | KeyBuffer* key, bool keepOld) { |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 380 | if (pathExists(key_path)) { |
| 381 | LOG(DEBUG) << "Key exists, using: " << key_path; |
Daniel Rosenberg | 690d6de | 2018-12-14 01:08:10 -0800 | [diff] [blame] | 382 | if (!retrieveKey(key_path, kEmptyAuthentication, key, keepOld)) return false; |
Neeraj Soni | c480f91 | 2018-12-14 15:18:15 +0530 | [diff] [blame] | 383 | if (is_metadata_wrapped_key_supported()) { |
| 384 | KeyBuffer ephemeral_wrapped_key; |
| 385 | if (!getEphemeralWrappedKey(KeyFormat::RAW, *key, &ephemeral_wrapped_key)) { |
| 386 | LOG(ERROR) << "Failed to export key for retrieved key"; |
| 387 | return false; |
| 388 | } |
| 389 | *key = std::move(ephemeral_wrapped_key); |
| 390 | } |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 391 | } else { |
| 392 | if (!create_if_absent) { |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 393 | LOG(ERROR) << "No key found in " << key_path; |
| 394 | return false; |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 395 | } |
| 396 | LOG(INFO) << "Creating new key in " << key_path; |
Neeraj Soni | c480f91 | 2018-12-14 15:18:15 +0530 | [diff] [blame] | 397 | if (is_metadata_wrapped_key_supported()) { |
| 398 | if(!generateWrappedKey(MAX_USER_ID, KeyType::ME, key)) return false; |
| 399 | } else { |
| 400 | if (!randomKey(key)) return false; |
| 401 | } |
| 402 | if (!storeKeyAtomically(key_path, tmp_path, |
| 403 | kEmptyAuthentication, *key)) return false; |
| 404 | if (is_metadata_wrapped_key_supported()) { |
| 405 | KeyBuffer ephemeral_wrapped_key; |
| 406 | if (!getEphemeralWrappedKey(KeyFormat::RAW, *key, &ephemeral_wrapped_key)) { |
| 407 | LOG(ERROR) << "Failed to export key for generated key"; |
| 408 | return false; |
| 409 | } |
| 410 | *key = std::move(ephemeral_wrapped_key); |
| 411 | } |
Paul Crowley | d575981 | 2016-06-02 11:04:27 -0700 | [diff] [blame] | 412 | } |
| 413 | return true; |
| 414 | } |
| 415 | |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 416 | } // namespace vold |
| 417 | } // namespace android |