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