blob: 719198db6f860308bc958eac82b98ebff10bd7ef [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 Ioffe78f80612020-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>
Scott Lobdell314dbc72018-12-05 16:01:16 -080034#include "FsCrypt.h"
Paul Crowleyf71ace32016-06-02 11:01:19 -070035#include "KeyStorage.h"
36#include "Utils.h"
37
38namespace android {
39namespace vold {
40
Paul Crowley4eac2642020-02-12 11:04:05 -080041const KeyGeneration neverGen() {
42 return KeyGeneration{0, false, false};
43}
44
45static bool randomKey(size_t size, KeyBuffer* key) {
46 *key = KeyBuffer(size);
Pavel Grafove2e2d302017-08-01 17:15:53 +010047 if (ReadRandomBytes(key->size(), key->data()) != 0) {
Paul Crowleyf71ace32016-06-02 11:01:19 -070048 // TODO status_t plays badly with PLOG, fix it.
49 LOG(ERROR) << "Random read failed";
50 return false;
51 }
52 return true;
53}
54
Paul Crowley4eac2642020-02-12 11:04:05 -080055bool generateStorageKey(const KeyGeneration& gen, KeyBuffer* key) {
56 if (!gen.allow_gen) return false;
57 if (gen.use_hw_wrapped_key) {
58 if (gen.keysize != FSCRYPT_MAX_KEY_SIZE) {
59 LOG(ERROR) << "Cannot generate a wrapped key " << gen.keysize << " bytes long";
60 return false;
61 }
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080062 return generateWrappedStorageKey(key);
Paul Crowley4eac2642020-02-12 11:04:05 -080063 } else {
64 return randomKey(gen.keysize, key);
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080065 }
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -080066}
67
Eric Biggersf3dc4202019-09-30 13:05:58 -070068// Return true if the kernel supports the ioctls to add/remove fscrypt keys
69// directly to/from the filesystem.
70bool isFsKeyringSupported(void) {
71 static bool initialized = false;
72 static bool supported;
73
74 if (!initialized) {
75 android::base::unique_fd fd(open("/data", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
76
77 // FS_IOC_ADD_ENCRYPTION_KEY with a NULL argument will fail with ENOTTY
78 // if the ioctl isn't supported. Otherwise it will fail with another
79 // error code such as EFAULT.
80 errno = 0;
81 (void)ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, NULL);
82 if (errno == ENOTTY) {
83 LOG(INFO) << "Kernel doesn't support FS_IOC_ADD_ENCRYPTION_KEY. Falling back to "
84 "session keyring";
85 supported = false;
86 } else {
87 if (errno != EFAULT) {
88 PLOG(WARNING) << "Unexpected error from FS_IOC_ADD_ENCRYPTION_KEY";
89 }
90 LOG(DEBUG) << "Detected support for FS_IOC_ADD_ENCRYPTION_KEY";
91 supported = true;
Nikita Ioffe78f80612020-04-20 22:21:49 +010092 android::base::SetProperty("ro.crypto.uses_fs_ioc_add_encryption_key", "true");
Eric Biggersf3dc4202019-09-30 13:05:58 -070093 }
94 // There's no need to check for FS_IOC_REMOVE_ENCRYPTION_KEY, since it's
95 // guaranteed to be available if FS_IOC_ADD_ENCRYPTION_KEY is. There's
96 // also no need to check for support on external volumes separately from
97 // /data, since either the kernel supports the ioctls on all
98 // fscrypt-capable filesystems or it doesn't.
99
100 initialized = true;
101 }
102 return supported;
103}
104
Paul Crowleyf71ace32016-06-02 11:01:19 -0700105// Get raw keyref - used to make keyname and to pass to ioctl
Eric Biggersba997ee2018-10-23 13:07:43 -0700106static std::string generateKeyRef(const uint8_t* key, int length) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700107 SHA512_CTX c;
108
109 SHA512_Init(&c);
110 SHA512_Update(&c, key, length);
111 unsigned char key_ref1[SHA512_DIGEST_LENGTH];
112 SHA512_Final(key_ref1, &c);
113
114 SHA512_Init(&c);
115 SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH);
116 unsigned char key_ref2[SHA512_DIGEST_LENGTH];
117 SHA512_Final(key_ref2, &c);
118
Eric Biggers506342f2019-12-17 13:11:25 -0800119 static_assert(FSCRYPT_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH,
120 "Hash too short for descriptor");
121 return std::string((char*)key_ref2, FSCRYPT_KEY_DESCRIPTOR_SIZE);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700122}
123
Eric Biggersa701c452018-10-23 13:06:55 -0700124static bool fillKey(const KeyBuffer& key, fscrypt_key* fs_key) {
Eric Biggers506342f2019-12-17 13:11:25 -0800125 if (key.size() != FSCRYPT_MAX_KEY_SIZE) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700126 LOG(ERROR) << "Wrong size key " << key.size();
127 return false;
128 }
Eric Biggers506342f2019-12-17 13:11:25 -0800129 static_assert(FSCRYPT_MAX_KEY_SIZE == sizeof(fs_key->raw), "Mismatch of max key sizes");
130 fs_key->mode = 0; // unused by kernel
Eric Biggersa701c452018-10-23 13:06:55 -0700131 memcpy(fs_key->raw, key.data(), key.size());
Eric Biggers506342f2019-12-17 13:11:25 -0800132 fs_key->size = key.size();
Paul Crowleyf71ace32016-06-02 11:01:19 -0700133 return true;
134}
135
Paul Crowley14c8c072018-09-18 13:30:21 -0700136static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr};
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700137
Eric Biggersf3dc4202019-09-30 13:05:58 -0700138static std::string keyrefstring(const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700139 std::ostringstream o;
Chen, Luhai5744dfe2017-08-18 14:49:45 +0800140 for (unsigned char i : raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700141 o << std::hex << std::setw(2) << std::setfill('0') << (int)i;
142 }
143 return o.str();
144}
145
Eric Biggersf3dc4202019-09-30 13:05:58 -0700146static std::string buildLegacyKeyName(const std::string& prefix, const std::string& raw_ref) {
147 return prefix + ":" + keyrefstring(raw_ref);
148}
149
150// Get the ID of the keyring we store all fscrypt keys in when the kernel is too
151// old to support FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY.
Eric Biggersa701c452018-10-23 13:06:55 -0700152static bool fscryptKeyring(key_serial_t* device_keyring) {
153 *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "fscrypt", 0);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700154 if (*device_keyring == -1) {
155 PLOG(ERROR) << "Unable to find device keyring";
156 return false;
157 }
158 return true;
159}
160
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000161// Add an encryption key of type "logon" to the global session keyring.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700162static bool installKeyLegacy(const KeyBuffer& key, const std::string& raw_ref) {
Eric Biggersa701c452018-10-23 13:06:55 -0700163 // Place fscrypt_key into automatically zeroing buffer.
164 KeyBuffer fsKeyBuffer(sizeof(fscrypt_key));
165 fscrypt_key& fs_key = *reinterpret_cast<fscrypt_key*>(fsKeyBuffer.data());
Pavel Grafove2e2d302017-08-01 17:15:53 +0100166
Eric Biggersa701c452018-10-23 13:06:55 -0700167 if (!fillKey(key, &fs_key)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700168 key_serial_t device_keyring;
Eric Biggersa701c452018-10-23 13:06:55 -0700169 if (!fscryptKeyring(&device_keyring)) return false;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700170 for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700171 auto ref = buildLegacyKeyName(*name_prefix, raw_ref);
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700172 key_serial_t key_id =
Eric Biggersa701c452018-10-23 13:06:55 -0700173 add_key("logon", ref.c_str(), (void*)&fs_key, sizeof(fs_key), device_keyring);
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700174 if (key_id == -1) {
175 PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring;
176 return false;
177 }
178 LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring
179 << " in process " << getpid();
Paul Crowleyf71ace32016-06-02 11:01:19 -0700180 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700181 return true;
182}
183
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000184// Installs fscrypt-provisioning key into session level kernel keyring.
185// This allows for the given key to be installed back into filesystem keyring.
186// For more context see reloadKeyFromSessionKeyring.
187static bool installProvisioningKey(const KeyBuffer& key, const std::string& ref,
188 const fscrypt_key_specifier& key_spec) {
189 key_serial_t device_keyring;
190 if (!fscryptKeyring(&device_keyring)) return false;
191
192 // Place fscrypt_provisioning_key_payload into automatically zeroing buffer.
193 KeyBuffer buf(sizeof(fscrypt_provisioning_key_payload) + key.size(), 0);
194 fscrypt_provisioning_key_payload& provisioning_key =
195 *reinterpret_cast<fscrypt_provisioning_key_payload*>(buf.data());
196 memcpy(provisioning_key.raw, key.data(), key.size());
197 provisioning_key.type = key_spec.type;
198
199 key_serial_t key_id = add_key("fscrypt-provisioning", ref.c_str(), (void*)&provisioning_key,
200 buf.size(), device_keyring);
201 if (key_id == -1) {
202 PLOG(ERROR) << "Failed to insert fscrypt-provisioning key for " << ref
203 << " into session keyring";
204 return false;
205 }
206 LOG(DEBUG) << "Added fscrypt-provisioning key for " << ref << " to session keyring";
207 return true;
208}
209
Eric Biggers83a73d72019-09-30 13:06:47 -0700210// Build a struct fscrypt_key_specifier for use in the key management ioctls.
Paul Crowley77df7f22020-01-23 15:29:30 -0800211static bool buildKeySpecifier(fscrypt_key_specifier* spec, const EncryptionPolicy& policy) {
212 switch (policy.options.version) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700213 case 1:
Paul Crowley77df7f22020-01-23 15:29:30 -0800214 if (policy.key_raw_ref.size() != FSCRYPT_KEY_DESCRIPTOR_SIZE) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700215 LOG(ERROR) << "Invalid key specifier size for v1 encryption policy: "
Paul Crowley77df7f22020-01-23 15:29:30 -0800216 << policy.key_raw_ref.size();
Eric Biggers83a73d72019-09-30 13:06:47 -0700217 return false;
218 }
219 spec->type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
Paul Crowley77df7f22020-01-23 15:29:30 -0800220 memcpy(spec->u.descriptor, policy.key_raw_ref.c_str(), FSCRYPT_KEY_DESCRIPTOR_SIZE);
Eric Biggers83a73d72019-09-30 13:06:47 -0700221 return true;
222 case 2:
Paul Crowley77df7f22020-01-23 15:29:30 -0800223 if (policy.key_raw_ref.size() != FSCRYPT_KEY_IDENTIFIER_SIZE) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700224 LOG(ERROR) << "Invalid key specifier size for v2 encryption policy: "
Paul Crowley77df7f22020-01-23 15:29:30 -0800225 << policy.key_raw_ref.size();
Eric Biggers83a73d72019-09-30 13:06:47 -0700226 return false;
227 }
228 spec->type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
Paul Crowley77df7f22020-01-23 15:29:30 -0800229 memcpy(spec->u.identifier, policy.key_raw_ref.c_str(), FSCRYPT_KEY_IDENTIFIER_SIZE);
Eric Biggers83a73d72019-09-30 13:06:47 -0700230 return true;
231 default:
Paul Crowley77df7f22020-01-23 15:29:30 -0800232 LOG(ERROR) << "Invalid encryption policy version: " << policy.options.version;
Eric Biggers83a73d72019-09-30 13:06:47 -0700233 return false;
234 }
235}
236
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000237// Installs key into keyring of a filesystem mounted on |mountpoint|.
238//
239// It's callers responsibility to fill key specifier, and either arg->raw or arg->key_id.
240//
241// In case arg->key_spec.type equals to FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER
242// arg->key_spec.u.identifier will be populated with raw key reference generated
243// by kernel.
244//
245// For documentation on difference between arg->raw and arg->key_id see
246// https://www.kernel.org/doc/html/latest/filesystems/fscrypt.html#fs-ioc-add-encryption-key
247static bool installFsKeyringKey(const std::string& mountpoint, const EncryptionOptions& options,
248 fscrypt_add_key_arg* arg) {
249 if (options.use_hw_wrapped_key) arg->flags |= FSCRYPT_ADD_KEY_FLAG_WRAPPED;
250
251 android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
252 if (fd == -1) {
253 PLOG(ERROR) << "Failed to open " << mountpoint << " to install key";
254 return false;
255 }
256
257 if (ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, arg) != 0) {
258 PLOG(ERROR) << "Failed to install fscrypt key to " << mountpoint;
259 return false;
260 }
261
262 return true;
263}
264
Paul Crowley77df7f22020-01-23 15:29:30 -0800265bool installKey(const std::string& mountpoint, const EncryptionOptions& options,
266 const KeyBuffer& key, EncryptionPolicy* policy) {
267 policy->options = options;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700268 // Put the fscrypt_add_key_arg in an automatically-zeroing buffer, since we
269 // have to copy the raw key into it.
270 KeyBuffer arg_buf(sizeof(struct fscrypt_add_key_arg) + key.size(), 0);
271 struct fscrypt_add_key_arg* arg = (struct fscrypt_add_key_arg*)arg_buf.data();
272
Eric Biggers83a73d72019-09-30 13:06:47 -0700273 // Initialize the "key specifier", which is like a name for the key.
Paul Crowley77df7f22020-01-23 15:29:30 -0800274 switch (options.version) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700275 case 1:
276 // A key for a v1 policy is specified by an arbitrary 8-byte
277 // "descriptor", which must be provided by userspace. We use the
278 // first 8 bytes from the double SHA-512 of the key itself.
Steven Laver7fe4cdb2020-02-14 13:18:26 -0800279 if (options.use_hw_wrapped_key) {
Steven Lavered747be2019-10-24 16:40:01 -0700280 /* When wrapped key is supported, only the first 32 bytes are
281 the same per boot. The second 32 bytes can change as the ephemeral
282 key is different. */
Steven Lavered7b3532020-02-12 12:49:40 -0800283 policy->key_raw_ref = generateKeyRef((const uint8_t*)key.data(), key.size()/2);
Steven Lavered747be2019-10-24 16:40:01 -0700284 } else {
Steven Lavered7b3532020-02-12 12:49:40 -0800285 policy->key_raw_ref = generateKeyRef((const uint8_t*)key.data(), key.size());
Steven Lavered747be2019-10-24 16:40:01 -0700286 }
Eric Biggers83a73d72019-09-30 13:06:47 -0700287 if (!isFsKeyringSupported()) {
Paul Crowley77df7f22020-01-23 15:29:30 -0800288 return installKeyLegacy(key, policy->key_raw_ref);
Eric Biggers83a73d72019-09-30 13:06:47 -0700289 }
Paul Crowley77df7f22020-01-23 15:29:30 -0800290 if (!buildKeySpecifier(&arg->key_spec, *policy)) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700291 return false;
292 }
293 break;
294 case 2:
295 // A key for a v2 policy is specified by an 16-byte "identifier",
296 // which is a cryptographic hash of the key itself which the kernel
297 // computes and returns. Any user-provided value is ignored; we
298 // just need to set the specifier type to indicate that we're adding
299 // this type of key.
300 arg->key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
301 break;
302 default:
Paul Crowley77df7f22020-01-23 15:29:30 -0800303 LOG(ERROR) << "Invalid encryption policy version: " << options.version;
Eric Biggers83a73d72019-09-30 13:06:47 -0700304 return false;
305 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700306
307 arg->raw_size = key.size();
308 memcpy(arg->raw, key.data(), key.size());
309
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000310 if (!installFsKeyringKey(mountpoint, options, arg)) return false;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700311
Eric Biggers83a73d72019-09-30 13:06:47 -0700312 if (arg->key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
313 // Retrieve the key identifier that the kernel computed.
Paul Crowley77df7f22020-01-23 15:29:30 -0800314 policy->key_raw_ref =
315 std::string((char*)arg->key_spec.u.identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
Eric Biggers83a73d72019-09-30 13:06:47 -0700316 }
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000317 std::string ref = keyrefstring(policy->key_raw_ref);
318 LOG(DEBUG) << "Installed fscrypt key with ref " << ref << " to " << mountpoint;
319
320 if (!installProvisioningKey(key, ref, arg->key_spec)) return false;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700321 return true;
322}
323
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000324// Remove an encryption key of type "logon" from the global session keyring.
Eric Biggersf3dc4202019-09-30 13:05:58 -0700325static bool evictKeyLegacy(const std::string& raw_ref) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700326 key_serial_t device_keyring;
Eric Biggersa701c452018-10-23 13:06:55 -0700327 if (!fscryptKeyring(&device_keyring)) return false;
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700328 bool success = true;
329 for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
Eric Biggersf3dc4202019-09-30 13:05:58 -0700330 auto ref = buildLegacyKeyName(*name_prefix, raw_ref);
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700331 auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
Paul Crowleyf71ace32016-06-02 11:01:19 -0700332
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700333 // Unlink the key from the keyring. Prefer unlinking to revoking or
334 // invalidating, since unlinking is actually no less secure currently, and
335 // it avoids bugs in certain kernel versions where the keyring key is
336 // referenced from places it shouldn't be.
337 if (keyctl_unlink(key_serial, device_keyring) != 0) {
338 PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref;
339 success = false;
340 } else {
341 LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref;
342 }
Paul Crowleyf71ace32016-06-02 11:01:19 -0700343 }
Paul Crowleycd8bfe32017-06-19 16:05:55 -0700344 return success;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700345}
346
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000347static bool evictProvisioningKey(const std::string& ref) {
348 key_serial_t device_keyring;
349 if (!fscryptKeyring(&device_keyring)) {
350 return false;
351 }
352
353 auto key_serial = keyctl_search(device_keyring, "fscrypt-provisioning", ref.c_str(), 0);
354 if (key_serial == -1 && errno != ENOKEY) {
355 PLOG(ERROR) << "Error searching session keyring for fscrypt-provisioning key for " << ref;
356 return false;
357 }
358
359 if (key_serial != -1 && keyctl_unlink(key_serial, device_keyring) != 0) {
360 PLOG(ERROR) << "Failed to unlink fscrypt-provisioning key for " << ref
361 << " from session keyring";
362 return false;
363 }
364 return true;
365}
366
Paul Crowley77df7f22020-01-23 15:29:30 -0800367bool evictKey(const std::string& mountpoint, const EncryptionPolicy& policy) {
368 if (policy.options.version == 1 && !isFsKeyringSupported()) {
369 return evictKeyLegacy(policy.key_raw_ref);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700370 }
371
372 android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
373 if (fd == -1) {
374 PLOG(ERROR) << "Failed to open " << mountpoint << " to evict key";
375 return false;
376 }
377
378 struct fscrypt_remove_key_arg arg;
379 memset(&arg, 0, sizeof(arg));
380
Paul Crowley77df7f22020-01-23 15:29:30 -0800381 if (!buildKeySpecifier(&arg.key_spec, policy)) {
Eric Biggers83a73d72019-09-30 13:06:47 -0700382 return false;
383 }
Eric Biggersf3dc4202019-09-30 13:05:58 -0700384
Paul Crowley77df7f22020-01-23 15:29:30 -0800385 std::string ref = keyrefstring(policy.key_raw_ref);
Eric Biggersf3dc4202019-09-30 13:05:58 -0700386
387 if (ioctl(fd, FS_IOC_REMOVE_ENCRYPTION_KEY, &arg) != 0) {
388 PLOG(ERROR) << "Failed to evict fscrypt key with ref " << ref << " from " << mountpoint;
389 return false;
390 }
391
392 LOG(DEBUG) << "Evicted fscrypt key with ref " << ref << " from " << mountpoint;
393 if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS) {
394 // Should never happen because keys are only added/removed as root.
395 LOG(ERROR) << "Unexpected case: key with ref " << ref << " is still added by other users!";
396 } else if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY) {
397 LOG(ERROR) << "Files still open after removing key with ref " << ref
398 << ". These files were not locked!";
399 }
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000400
401 if (!evictProvisioningKey(ref)) return false;
Eric Biggersf3dc4202019-09-30 13:05:58 -0700402 return true;
403}
404
Paul Crowley4eac2642020-02-12 11:04:05 -0800405bool retrieveOrGenerateKey(const std::string& key_path, const std::string& tmp_path,
406 const KeyAuthentication& key_authentication, const KeyGeneration& gen,
407 KeyBuffer* key, bool keepOld) {
Paul Crowleyf71ace32016-06-02 11:01:19 -0700408 if (pathExists(key_path)) {
409 LOG(DEBUG) << "Key exists, using: " << key_path;
Paul Crowley77df7f22020-01-23 15:29:30 -0800410 if (!retrieveKey(key_path, key_authentication, key, keepOld)) return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700411 } else {
Paul Crowley4eac2642020-02-12 11:04:05 -0800412 if (!gen.allow_gen) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700413 LOG(ERROR) << "No key found in " << key_path;
414 return false;
Paul Crowleyf71ace32016-06-02 11:01:19 -0700415 }
416 LOG(INFO) << "Creating new key in " << key_path;
Paul Crowley4eac2642020-02-12 11:04:05 -0800417 if (!generateStorageKey(gen, key)) return false;
Paul Crowley77df7f22020-01-23 15:29:30 -0800418 if (!storeKeyAtomically(key_path, tmp_path, key_authentication, *key)) return false;
Paul Crowleyd5759812016-06-02 11:04:27 -0700419 }
420 return true;
421}
422
Nikita Ioffe1ee35cf2020-02-28 19:50:31 +0000423bool reloadKeyFromSessionKeyring(const std::string& mountpoint, const EncryptionPolicy& policy) {
424 key_serial_t device_keyring;
425 if (!fscryptKeyring(&device_keyring)) {
426 return false;
427 }
428
429 std::string ref = keyrefstring(policy.key_raw_ref);
430 auto key_serial = keyctl_search(device_keyring, "fscrypt-provisioning", ref.c_str(), 0);
431 if (key_serial == -1) {
432 PLOG(ERROR) << "Failed to find fscrypt-provisioning key for " << ref
433 << " in session keyring";
434 return false;
435 }
436
437 LOG(DEBUG) << "Installing fscrypt-provisioning key for " << ref << " back into " << mountpoint
438 << " fs-keyring";
439
440 struct fscrypt_add_key_arg arg;
441 memset(&arg, 0, sizeof(arg));
442 if (!buildKeySpecifier(&arg.key_spec, policy)) return false;
443 arg.key_id = key_serial;
444 if (!installFsKeyringKey(mountpoint, policy.options, &arg)) return false;
445
446 return true;
447}
448
Paul Crowleyf71ace32016-06-02 11:01:19 -0700449} // namespace vold
450} // namespace android