blob: 582eea2df30770687bbacc91c67b468062fe4085 [file] [log] [blame]
Paul Crowleyf71ace32016-06-02 11:01:19 -07001/*
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 Biggersf3dc4202019-09-30 13:05:58 -070023#include <fcntl.h>
Eric Biggers3e9c9962019-12-16 15:55:12 -080024#include <linux/fscrypt.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070025#include <openssl/sha.h>
Eric Biggersf3dc4202019-09-30 13:05:58 -070026#include <sys/ioctl.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070027
28#include <android-base/file.h>
29#include <android-base/logging.h>
Nikita Ioffeeea8bd32020-04-20 22:21:49 +010030#include <android-base/properties.h>
Elliott Hughesc3bda182017-05-09 17:01:04 -070031#include <keyutils.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070032
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080033#include <fscrypt_uapi.h>
Paul Crowleyf71ace32016-06-02 11:01:19 -070034#include "KeyStorage.h"
35#include "Utils.h"
36
37namespace android {
38namespace vold {
39
Paul Crowley4eac2642020-02-12 11:04:05 -080040const KeyGeneration neverGen() {
41 return KeyGeneration{0, false, false};
42}
43
44static bool randomKey(size_t size, KeyBuffer* key) {
45 *key = KeyBuffer(size);
Pavel Grafove2e2d302017-08-01 17:15:53 +010046 if (ReadRandomBytes(key->size(), key->data()) != 0) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070047 // TODO status_t plays badly with PLOG, fix it.
48 LOG(ERROR) << "Random read failed";
49 return false;
50 }
51 return true;
52}
53
Paul Crowley4eac2642020-02-12 11:04:05 -080054bool generateStorageKey(const KeyGeneration& gen, KeyBuffer* key) {
55 if (!gen.allow_gen) return false;
56 if (gen.use_hw_wrapped_key) {
57 if (gen.keysize != FSCRYPT_MAX_KEY_SIZE) {
58 LOG(ERROR) << "Cannot generate a wrapped key " << gen.keysize << " bytes long";
59 return false;
60 }
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080061 return generateWrappedStorageKey(key);
Paul Crowley4eac2642020-02-12 11:04:05 -080062 } else {
63 return randomKey(gen.keysize, key);
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080064 }
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080065}
66
Eric Biggers7604eb92020-07-16 14:29:59 -070067static bool isFsKeyringSupportedImpl() {
68 android::base::unique_fd fd(open("/data", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
69
70 // FS_IOC_ADD_ENCRYPTION_KEY with a NULL argument will fail with ENOTTY if
71 // the ioctl isn't supported. Otherwise it will fail with another error
72 // code such as EFAULT.
73 //
74 // Note that there's no need to check for FS_IOC_REMOVE_ENCRYPTION_KEY,
75 // since it's guaranteed to be available if FS_IOC_ADD_ENCRYPTION_KEY is.
76 // There's also no need to check for support on external volumes separately
77 // from /data, since either the kernel supports the ioctls on all
78 // fscrypt-capable filesystems or it doesn't.
79 errno = 0;
80 (void)ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, NULL);
81 if (errno == ENOTTY) {
82 LOG(INFO) << "Kernel doesn't support FS_IOC_ADD_ENCRYPTION_KEY. Falling back to "
83 "session keyring";
84 return false;
85 }
86 if (errno != EFAULT) {
87 PLOG(WARNING) << "Unexpected error from FS_IOC_ADD_ENCRYPTION_KEY";
88 }
89 LOG(DEBUG) << "Detected support for FS_IOC_ADD_ENCRYPTION_KEY";
90 android::base::SetProperty("ro.crypto.uses_fs_ioc_add_encryption_key", "true");
91 return true;
92}
93
Eric Biggersf3dc4202019-09-30 13:05:58 -070094// Return true if the kernel supports the ioctls to add/remove fscrypt keys
95// directly to/from the filesystem.
96bool isFsKeyringSupported(void) {
Eric Biggers7604eb92020-07-16 14:29:59 -070097 static bool supported = isFsKeyringSupportedImpl();
Eric Biggersf3dc4202019-09-30 13:05:58 -070098 return supported;
99}
100
Paul Crowleyf71ace32016-06-02 11:01:19 -0700101// Get raw keyref - used to make keyname and to pass to ioctl
Eric Biggersba997ee2018-10-23 13:07:43 -0700102static std::string generateKeyRef(const uint8_t* key, int length) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700103 SHA512_CTX c;
104
105 SHA512_Init(&c);
106 SHA512_Update(&c, key, length);
107 unsigned char key_ref1[SHA512_DIGEST_LENGTH];
108 SHA512_Final(key_ref1, &c);
109
110 SHA512_Init(&c);
111 SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH);
112 unsigned char key_ref2[SHA512_DIGEST_LENGTH];
113 SHA512_Final(key_ref2, &c);
114
Eric Biggers506342f2019-12-17 13:11:25 -0800115 static_assert(FSCRYPT_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH,
116 "Hash too short for descriptor");
117 return std::string((char*)key_ref2, FSCRYPT_KEY_DESCRIPTOR_SIZE);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700118}
119
Eric Biggersa701c452018-10-23 13:06:55 -0700120static bool fillKey(const KeyBuffer& key, fscrypt_key* fs_key) {
Eric Biggers506342f2019-12-17 13:11:25 -0800121 if (key.size() != FSCRYPT_MAX_KEY_SIZE) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700122 LOG(ERROR) << "Wrong size key " << key.size();
123 return false;
124 }
Eric Biggers506342f2019-12-17 13:11:25 -0800125 static_assert(FSCRYPT_MAX_KEY_SIZE == sizeof(fs_key->raw), "Mismatch of max key sizes");
126 fs_key->mode = 0; // unused by kernel
Eric Biggersa701c452018-10-23 13:06:55 -0700127 memcpy(fs_key->raw, key.data(), key.size());
Eric Biggers506342f2019-12-17 13:11:25 -0800128 fs_key->size = key.size();
Paul Crowleyf71ace32016-06-02 11:01:19 -0700129 return true;
130}
131
Paul Crowley14c8c072018-09-18 13:30:21 -0700132static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr};
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700133
Eric Biggersf3dc4202019-09-30 13:05:58 -0700134static std::string keyrefstring(const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700135 std::ostringstream o;
Chen, Luhai5744dfe2017-08-18 14:49:45 +0800136 for (unsigned char i : raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700137 o << std::hex << std::setw(2) << std::setfill('0') << (int)i;
138 }
139 return o.str();
140}
141
Eric Biggersf3dc4202019-09-30 13:05:58 -0700142static std::string buildLegacyKeyName(const std::string& prefix, const std::string& raw_ref) {
143 return prefix + ":" + keyrefstring(raw_ref);
144}
145
146// Get the ID of the keyring we store all fscrypt keys in when the kernel is too
147// old to support FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY.
Eric Biggersa701c452018-10-23 13:06:55 -0700148static bool fscryptKeyring(key_serial_t* device_keyring) {
149 *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "fscrypt", 0);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700150 if (*device_keyring == -1) {
151 PLOG(ERROR) << "Unable to find device keyring";
152 return false;
153 }
154 return true;
155}
156
Nikita Ioffe1c6731c2020-02-28 19:50:31 +0000157// Add an encryption key of type "logon" to the global session keyring.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700158static bool installKeyLegacy(const KeyBuffer& key, const std::string& raw_ref) {
Eric Biggersa701c452018-10-23 13:06:55 -0700159 // Place fscrypt_key into automatically zeroing buffer.
160 KeyBuffer fsKeyBuffer(sizeof(fscrypt_key));
161 fscrypt_key& fs_key = *reinterpret_cast<fscrypt_key*>(fsKeyBuffer.data());
Pavel Grafove2e2d302017-08-01 17:15:53 +0100162
Eric Biggersa701c452018-10-23 13:06:55 -0700163 if (!fillKey(key, &fs_key)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700164 key_serial_t device_keyring;
Eric Biggersa701c452018-10-23 13:06:55 -0700165 if (!fscryptKeyring(&device_keyring)) return false;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700166 for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700167 auto ref = buildLegacyKeyName(*name_prefix, raw_ref);
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700168 key_serial_t key_id =
Eric Biggersa701c452018-10-23 13:06:55 -0700169 add_key("logon", ref.c_str(), (void*)&fs_key, sizeof(fs_key), device_keyring);
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700170 if (key_id == -1) {
171 PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring;
172 return false;
173 }
174 LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring
175 << " in process " << getpid();
Paul Crowleyf71ace32016-06-02 11:01:19 -0700176 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700177 return true;
178}
179
Nikita Ioffe1c6731c2020-02-28 19:50:31 +0000180// Installs fscrypt-provisioning key into session level kernel keyring.
181// This allows for the given key to be installed back into filesystem keyring.
182// For more context see reloadKeyFromSessionKeyring.
183static bool installProvisioningKey(const KeyBuffer& key, const std::string& ref,
184 const fscrypt_key_specifier& key_spec) {
185 key_serial_t device_keyring;
186 if (!fscryptKeyring(&device_keyring)) return false;
187
188 // Place fscrypt_provisioning_key_payload into automatically zeroing buffer.
189 KeyBuffer buf(sizeof(fscrypt_provisioning_key_payload) + key.size(), 0);
190 fscrypt_provisioning_key_payload& provisioning_key =
191 *reinterpret_cast<fscrypt_provisioning_key_payload*>(buf.data());
192 memcpy(provisioning_key.raw, key.data(), key.size());
193 provisioning_key.type = key_spec.type;
194
195 key_serial_t key_id = add_key("fscrypt-provisioning", ref.c_str(), (void*)&provisioning_key,
196 buf.size(), device_keyring);
197 if (key_id == -1) {
198 PLOG(ERROR) << "Failed to insert fscrypt-provisioning key for " << ref
199 << " into session keyring";
200 return false;
201 }
202 LOG(DEBUG) << "Added fscrypt-provisioning key for " << ref << " to session keyring";
203 return true;
204}
205
Eric Biggers83a73d72019-09-30 13:06:47 -0700206// Build a struct fscrypt_key_specifier for use in the key management ioctls.
Paul Crowley77df7f22020-01-23 15:29:30 -0800207static bool buildKeySpecifier(fscrypt_key_specifier* spec, const EncryptionPolicy& policy) {
208 switch (policy.options.version) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700209 case 1:
Paul Crowley77df7f22020-01-23 15:29:30 -0800210 if (policy.key_raw_ref.size() != FSCRYPT_KEY_DESCRIPTOR_SIZE) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700211 LOG(ERROR) << "Invalid key specifier size for v1 encryption policy: "
Paul Crowley77df7f22020-01-23 15:29:30 -0800212 << policy.key_raw_ref.size();
Eric Biggers83a73d72019-09-30 13:06:47 -0700213 return false;
214 }
215 spec->type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
Paul Crowley77df7f22020-01-23 15:29:30 -0800216 memcpy(spec->u.descriptor, policy.key_raw_ref.c_str(), FSCRYPT_KEY_DESCRIPTOR_SIZE);
Eric Biggers83a73d72019-09-30 13:06:47 -0700217 return true;
218 case 2:
Paul Crowley77df7f22020-01-23 15:29:30 -0800219 if (policy.key_raw_ref.size() != FSCRYPT_KEY_IDENTIFIER_SIZE) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700220 LOG(ERROR) << "Invalid key specifier size for v2 encryption policy: "
Paul Crowley77df7f22020-01-23 15:29:30 -0800221 << policy.key_raw_ref.size();
Eric Biggers83a73d72019-09-30 13:06:47 -0700222 return false;
223 }
224 spec->type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
Paul Crowley77df7f22020-01-23 15:29:30 -0800225 memcpy(spec->u.identifier, policy.key_raw_ref.c_str(), FSCRYPT_KEY_IDENTIFIER_SIZE);
Eric Biggers83a73d72019-09-30 13:06:47 -0700226 return true;
227 default:
Paul Crowley77df7f22020-01-23 15:29:30 -0800228 LOG(ERROR) << "Invalid encryption policy version: " << policy.options.version;
Eric Biggers83a73d72019-09-30 13:06:47 -0700229 return false;
230 }
231}
232
Nikita Ioffe1c6731c2020-02-28 19:50:31 +0000233// Installs key into keyring of a filesystem mounted on |mountpoint|.
234//
235// It's callers responsibility to fill key specifier, and either arg->raw or arg->key_id.
236//
237// In case arg->key_spec.type equals to FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER
238// arg->key_spec.u.identifier will be populated with raw key reference generated
239// by kernel.
240//
241// For documentation on difference between arg->raw and arg->key_id see
242// https://www.kernel.org/doc/html/latest/filesystems/fscrypt.html#fs-ioc-add-encryption-key
243static bool installFsKeyringKey(const std::string& mountpoint, const EncryptionOptions& options,
244 fscrypt_add_key_arg* arg) {
245 if (options.use_hw_wrapped_key) arg->flags |= FSCRYPT_ADD_KEY_FLAG_WRAPPED;
246
247 android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
248 if (fd == -1) {
249 PLOG(ERROR) << "Failed to open " << mountpoint << " to install key";
250 return false;
251 }
252
253 if (ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, arg) != 0) {
254 PLOG(ERROR) << "Failed to install fscrypt key to " << mountpoint;
255 return false;
256 }
257
258 return true;
259}
260
Paul Crowley77df7f22020-01-23 15:29:30 -0800261bool installKey(const std::string& mountpoint, const EncryptionOptions& options,
262 const KeyBuffer& key, EncryptionPolicy* policy) {
263 policy->options = options;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700264 // Put the fscrypt_add_key_arg in an automatically-zeroing buffer, since we
265 // have to copy the raw key into it.
266 KeyBuffer arg_buf(sizeof(struct fscrypt_add_key_arg) + key.size(), 0);
267 struct fscrypt_add_key_arg* arg = (struct fscrypt_add_key_arg*)arg_buf.data();
268
Eric Biggers83a73d72019-09-30 13:06:47 -0700269 // Initialize the "key specifier", which is like a name for the key.
Paul Crowley77df7f22020-01-23 15:29:30 -0800270 switch (options.version) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700271 case 1:
272 // A key for a v1 policy is specified by an arbitrary 8-byte
273 // "descriptor", which must be provided by userspace. We use the
274 // first 8 bytes from the double SHA-512 of the key itself.
Paul Crowley77df7f22020-01-23 15:29:30 -0800275 policy->key_raw_ref = generateKeyRef((const uint8_t*)key.data(), key.size());
Eric Biggers83a73d72019-09-30 13:06:47 -0700276 if (!isFsKeyringSupported()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800277 return installKeyLegacy(key, policy->key_raw_ref);
Eric Biggers83a73d72019-09-30 13:06:47 -0700278 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800279 if (!buildKeySpecifier(&arg->key_spec, *policy)) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700280 return false;
281 }
282 break;
283 case 2:
284 // A key for a v2 policy is specified by an 16-byte "identifier",
285 // which is a cryptographic hash of the key itself which the kernel
286 // computes and returns. Any user-provided value is ignored; we
287 // just need to set the specifier type to indicate that we're adding
288 // this type of key.
289 arg->key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
290 break;
291 default:
Paul Crowley77df7f22020-01-23 15:29:30 -0800292 LOG(ERROR) << "Invalid encryption policy version: " << options.version;
Eric Biggers83a73d72019-09-30 13:06:47 -0700293 return false;
294 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700295
296 arg->raw_size = key.size();
297 memcpy(arg->raw, key.data(), key.size());
298
Nikita Ioffe1c6731c2020-02-28 19:50:31 +0000299 if (!installFsKeyringKey(mountpoint, options, arg)) return false;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700300
Eric Biggers83a73d72019-09-30 13:06:47 -0700301 if (arg->key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
302 // Retrieve the key identifier that the kernel computed.
Paul Crowley77df7f22020-01-23 15:29:30 -0800303 policy->key_raw_ref =
304 std::string((char*)arg->key_spec.u.identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
Eric Biggers83a73d72019-09-30 13:06:47 -0700305 }
Nikita Ioffe1c6731c2020-02-28 19:50:31 +0000306 std::string ref = keyrefstring(policy->key_raw_ref);
307 LOG(DEBUG) << "Installed fscrypt key with ref " << ref << " to " << mountpoint;
308
309 if (!installProvisioningKey(key, ref, arg->key_spec)) return false;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700310 return true;
311}
312
Nikita Ioffe1c6731c2020-02-28 19:50:31 +0000313// Remove an encryption key of type "logon" from the global session keyring.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700314static bool evictKeyLegacy(const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700315 key_serial_t device_keyring;
Eric Biggersa701c452018-10-23 13:06:55 -0700316 if (!fscryptKeyring(&device_keyring)) return false;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700317 bool success = true;
318 for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700319 auto ref = buildLegacyKeyName(*name_prefix, raw_ref);
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700320 auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700321
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700322 // Unlink the key from the keyring. Prefer unlinking to revoking or
323 // invalidating, since unlinking is actually no less secure currently, and
324 // it avoids bugs in certain kernel versions where the keyring key is
325 // referenced from places it shouldn't be.
326 if (keyctl_unlink(key_serial, device_keyring) != 0) {
327 PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref;
328 success = false;
329 } else {
330 LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref;
331 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700332 }
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700333 return success;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700334}
335
Nikita Ioffe1c6731c2020-02-28 19:50:31 +0000336static bool evictProvisioningKey(const std::string& ref) {
337 key_serial_t device_keyring;
338 if (!fscryptKeyring(&device_keyring)) {
339 return false;
340 }
341
342 auto key_serial = keyctl_search(device_keyring, "fscrypt-provisioning", ref.c_str(), 0);
343 if (key_serial == -1 && errno != ENOKEY) {
344 PLOG(ERROR) << "Error searching session keyring for fscrypt-provisioning key for " << ref;
345 return false;
346 }
347
348 if (key_serial != -1 && keyctl_unlink(key_serial, device_keyring) != 0) {
349 PLOG(ERROR) << "Failed to unlink fscrypt-provisioning key for " << ref
350 << " from session keyring";
351 return false;
352 }
353 return true;
354}
355
Paul Crowley77df7f22020-01-23 15:29:30 -0800356bool evictKey(const std::string& mountpoint, const EncryptionPolicy& policy) {
357 if (policy.options.version == 1 && !isFsKeyringSupported()) {
358 return evictKeyLegacy(policy.key_raw_ref);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700359 }
360
361 android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
362 if (fd == -1) {
363 PLOG(ERROR) << "Failed to open " << mountpoint << " to evict key";
364 return false;
365 }
366
367 struct fscrypt_remove_key_arg arg;
368 memset(&arg, 0, sizeof(arg));
369
Paul Crowley77df7f22020-01-23 15:29:30 -0800370 if (!buildKeySpecifier(&arg.key_spec, policy)) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700371 return false;
372 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700373
Paul Crowley77df7f22020-01-23 15:29:30 -0800374 std::string ref = keyrefstring(policy.key_raw_ref);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700375
376 if (ioctl(fd, FS_IOC_REMOVE_ENCRYPTION_KEY, &arg) != 0) {
377 PLOG(ERROR) << "Failed to evict fscrypt key with ref " << ref << " from " << mountpoint;
378 return false;
379 }
380
381 LOG(DEBUG) << "Evicted fscrypt key with ref " << ref << " from " << mountpoint;
382 if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS) {
383 // Should never happen because keys are only added/removed as root.
384 LOG(ERROR) << "Unexpected case: key with ref " << ref << " is still added by other users!";
385 } else if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY) {
386 LOG(ERROR) << "Files still open after removing key with ref " << ref
387 << ". These files were not locked!";
388 }
Nikita Ioffe1c6731c2020-02-28 19:50:31 +0000389
390 if (!evictProvisioningKey(ref)) return false;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700391 return true;
392}
393
Paul Crowley4eac2642020-02-12 11:04:05 -0800394bool retrieveOrGenerateKey(const std::string& key_path, const std::string& tmp_path,
395 const KeyAuthentication& key_authentication, const KeyGeneration& gen,
396 KeyBuffer* key, bool keepOld) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700397 if (pathExists(key_path)) {
398 LOG(DEBUG) << "Key exists, using: " << key_path;
Paul Crowley77df7f22020-01-23 15:29:30 -0800399 if (!retrieveKey(key_path, key_authentication, key, keepOld)) return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700400 } else {
Paul Crowley4eac2642020-02-12 11:04:05 -0800401 if (!gen.allow_gen) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700402 LOG(ERROR) << "No key found in " << key_path;
403 return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700404 }
405 LOG(INFO) << "Creating new key in " << key_path;
Paul Crowley4eac2642020-02-12 11:04:05 -0800406 if (!generateStorageKey(gen, key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800407 if (!storeKeyAtomically(key_path, tmp_path, key_authentication, *key)) return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700408 }
409 return true;
410}
411
Nikita Ioffe1c6731c2020-02-28 19:50:31 +0000412bool reloadKeyFromSessionKeyring(const std::string& mountpoint, const EncryptionPolicy& policy) {
413 key_serial_t device_keyring;
414 if (!fscryptKeyring(&device_keyring)) {
415 return false;
416 }
417
418 std::string ref = keyrefstring(policy.key_raw_ref);
419 auto key_serial = keyctl_search(device_keyring, "fscrypt-provisioning", ref.c_str(), 0);
420 if (key_serial == -1) {
421 PLOG(ERROR) << "Failed to find fscrypt-provisioning key for " << ref
422 << " in session keyring";
423 return false;
424 }
425
426 LOG(DEBUG) << "Installing fscrypt-provisioning key for " << ref << " back into " << mountpoint
427 << " fs-keyring";
428
429 struct fscrypt_add_key_arg arg;
430 memset(&arg, 0, sizeof(arg));
431 if (!buildKeySpecifier(&arg.key_spec, policy)) return false;
432 arg.key_id = key_serial;
433 if (!installFsKeyringKey(mountpoint, policy.options, &arg)) return false;
434
435 return true;
436}
437
Paul Crowleyf71ace32016-06-02 11:01:19 -0700438} // namespace vold
439} // namespace android