blob: 5c2b3e80d189e4a71e2f4b133878e33b11131a34 [file] [log] [blame]
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001/*
2 * Copyright (C) 2010 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/* TO DO:
18 * 1. Perhaps keep several copies of the encrypted key, in case something
19 * goes horribly wrong?
20 *
21 */
22
Logan Chiend557d762018-05-02 11:36:45 +080023#define LOG_TAG "Cryptfs"
24
25#include "cryptfs.h"
26
27#include "EncryptInplace.h"
28#include "Ext4Crypt.h"
29#include "Keymaster.h"
30#include "Process.h"
31#include "ScryptParameters.h"
32#include "VoldUtil.h"
33#include "VolumeManager.h"
34#include "secontext.h"
35
Logan Chien3f2b1222018-05-02 11:39:03 +080036#include <android-base/properties.h>
Logan Chiend557d762018-05-02 11:36:45 +080037#include <bootloader_message/bootloader_message.h>
Logan Chien3f2b1222018-05-02 11:39:03 +080038#include <cutils/android_reboot.h>
39#include <cutils/log.h>
40#include <cutils/properties.h>
Paul Crowley3b71fc52017-10-09 10:55:21 -070041#include <ext4_utils/ext4_crypt.h>
Tao Bao5a95ddb2016-10-05 18:01:19 -070042#include <ext4_utils/ext4_utils.h>
Logan Chien3f2b1222018-05-02 11:39:03 +080043#include <f2fs_sparseblock.h>
Ken Sumralle5032c42012-04-01 23:58:44 -070044#include <fs_mgr.h>
Logan Chien3f2b1222018-05-02 11:39:03 +080045#include <hardware_legacy/power.h>
Logan Chiend557d762018-05-02 11:36:45 +080046#include <logwrap/logwrap.h>
47#include <openssl/evp.h>
48#include <openssl/sha.h>
Jeff Vander Stoepdf725752016-01-29 15:34:43 -080049#include <selinux/selinux.h>
Logan Chiend557d762018-05-02 11:36:45 +080050
51#include <ctype.h>
52#include <errno.h>
53#include <fcntl.h>
54#include <inttypes.h>
55#include <libgen.h>
56#include <linux/dm-ioctl.h>
57#include <linux/kdev_t.h>
58#include <math.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <sys/ioctl.h>
63#include <sys/mount.h>
64#include <sys/param.h>
65#include <sys/stat.h>
66#include <sys/types.h>
67#include <sys/wait.h>
68#include <time.h>
69#include <unistd.h>
70
Wei Wang4375f1b2017-02-24 17:43:01 -080071extern "C" {
72#include <crypto_scrypt.h>
73}
Mark Salyzyn3e971272014-01-21 13:27:04 -080074
Mark Salyzyn5eecc442014-02-12 14:16:14 -080075#define UNUSED __attribute__((unused))
76
Ken Sumrall8f869aa2010-12-03 03:47:09 -080077#define DM_CRYPT_BUF_SIZE 4096
78
Jason parks70a4b3f2011-01-28 10:10:47 -060079#define HASH_COUNT 2000
Greg Kaiserc0de9c72018-02-14 20:05:54 -080080
81constexpr size_t INTERMEDIATE_KEY_LEN_BYTES = 16;
82constexpr size_t INTERMEDIATE_IV_LEN_BYTES = 16;
83constexpr size_t INTERMEDIATE_BUF_SIZE =
84 (INTERMEDIATE_KEY_LEN_BYTES + INTERMEDIATE_IV_LEN_BYTES);
85
86// SCRYPT_LEN is used by struct crypt_mnt_ftr for its intermediate key.
87static_assert(INTERMEDIATE_BUF_SIZE == SCRYPT_LEN,
88 "Mismatch of intermediate key sizes");
Jason parks70a4b3f2011-01-28 10:10:47 -060089
Ken Sumrall29d8da82011-05-18 17:20:07 -070090#define KEY_IN_FOOTER "footer"
91
Paul Lawrence3bd36d52015-06-09 13:37:44 -070092#define DEFAULT_PASSWORD "default_password"
Paul Lawrencef4faa572014-01-29 13:31:03 -080093
Paul Lawrence3d99eba2015-11-20 07:07:19 -080094#define CRYPTO_BLOCK_DEVICE "userdata"
95
96#define BREADCRUMB_FILE "/data/misc/vold/convert_fde"
97
Ken Sumrall29d8da82011-05-18 17:20:07 -070098#define EXT4_FS 1
JP Abgrall62c7af32014-06-16 13:01:23 -070099#define F2FS_FS 2
Ken Sumrall29d8da82011-05-18 17:20:07 -0700100
Ken Sumralle919efe2012-09-29 17:07:41 -0700101#define TABLE_LOAD_RETRIES 10
102
Shawn Willden47ba10d2014-09-03 17:07:06 -0600103#define RSA_KEY_SIZE 2048
104#define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8)
105#define RSA_EXPONENT 0x10001
Shawn Willdenda6e8992015-06-03 09:40:45 -0600106#define KEYMASTER_CRYPTFS_RATE_LIMIT 1 // Maximum one try per second
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700107
Paul Lawrence8e3f4512014-09-08 10:11:17 -0700108#define RETRY_MOUNT_ATTEMPTS 10
109#define RETRY_MOUNT_DELAY_SECONDS 1
110
Paul Crowley5afbc622017-11-27 09:42:17 -0800111#define CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE (1)
112
Paul Crowley73473332017-11-21 15:43:51 -0800113static int put_crypt_ftr_and_key(struct crypt_mnt_ftr* crypt_ftr);
114
Greg Kaiser59ad0182018-02-16 13:01:36 -0800115static unsigned char saved_master_key[MAX_KEY_LEN];
Ken Sumrall3ad90722011-10-04 20:38:29 -0700116static char *saved_mount_point;
Jason parks70a4b3f2011-01-28 10:10:47 -0600117static int master_key_saved = 0;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700118static struct crypt_persist_data *persist_data = NULL;
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800119
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700120/* Should we use keymaster? */
121static int keymaster_check_compatibility()
122{
Janis Danisevskis015ec302017-01-31 11:31:08 +0000123 return keymaster_compatibility_cryptfs_scrypt();
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700124}
125
126/* Create a new keymaster key and store it in this footer */
127static int keymaster_create_key(struct crypt_mnt_ftr *ftr)
128{
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800129 if (ftr->keymaster_blob_size) {
130 SLOGI("Already have key");
131 return 0;
132 }
133
Janis Danisevskis015ec302017-01-31 11:31:08 +0000134 int rc = keymaster_create_key_for_cryptfs_scrypt(RSA_KEY_SIZE, RSA_EXPONENT,
135 KEYMASTER_CRYPTFS_RATE_LIMIT, ftr->keymaster_blob, KEYMASTER_BLOB_SIZE,
136 &ftr->keymaster_blob_size);
137 if (rc) {
138 if (ftr->keymaster_blob_size > KEYMASTER_BLOB_SIZE) {
Paul Crowley73473332017-11-21 15:43:51 -0800139 SLOGE("Keymaster key blob too large");
Janis Danisevskis015ec302017-01-31 11:31:08 +0000140 ftr->keymaster_blob_size = 0;
141 }
142 SLOGE("Failed to generate keypair");
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700143 return -1;
144 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000145 return 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700146}
147
Shawn Willdene17a9c42014-09-08 13:04:08 -0600148/* This signs the given object using the keymaster key. */
149static int keymaster_sign_object(struct crypt_mnt_ftr *ftr,
Shawn Willden47ba10d2014-09-03 17:07:06 -0600150 const unsigned char *object,
151 const size_t object_size,
152 unsigned char **signature,
153 size_t *signature_size)
154{
Shawn Willden47ba10d2014-09-03 17:07:06 -0600155 unsigned char to_sign[RSA_KEY_SIZE_BYTES];
Shawn Willdene17a9c42014-09-08 13:04:08 -0600156 size_t to_sign_size = sizeof(to_sign);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600157 memset(to_sign, 0, RSA_KEY_SIZE_BYTES);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600158
Shawn Willdene17a9c42014-09-08 13:04:08 -0600159 // To sign a message with RSA, the message must satisfy two
160 // constraints:
161 //
162 // 1. The message, when interpreted as a big-endian numeric value, must
163 // be strictly less than the public modulus of the RSA key. Note
164 // that because the most significant bit of the public modulus is
165 // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit
166 // key), an n-bit message with most significant bit 0 always
167 // satisfies this requirement.
168 //
169 // 2. The message must have the same length in bits as the public
170 // modulus of the RSA key. This requirement isn't mathematically
171 // necessary, but is necessary to ensure consistency in
172 // implementations.
173 switch (ftr->kdf_type) {
Shawn Willdene17a9c42014-09-08 13:04:08 -0600174 case KDF_SCRYPT_KEYMASTER:
175 // This ensures the most significant byte of the signed message
176 // is zero. We could have zero-padded to the left instead, but
177 // this approach is slightly more robust against changes in
178 // object size. However, it's still broken (but not unusably
Shawn Willdenda6e8992015-06-03 09:40:45 -0600179 // so) because we really should be using a proper deterministic
180 // RSA padding function, such as PKCS1.
Wei Wang4375f1b2017-02-24 17:43:01 -0800181 memcpy(to_sign + 1, object, std::min((size_t)RSA_KEY_SIZE_BYTES - 1, object_size));
Shawn Willdene17a9c42014-09-08 13:04:08 -0600182 SLOGI("Signing safely-padded object");
183 break;
184 default:
185 SLOGE("Unknown KDF type %d", ftr->kdf_type);
Janis Danisevskis015ec302017-01-31 11:31:08 +0000186 return -1;
Shawn Willdene17a9c42014-09-08 13:04:08 -0600187 }
Paul Crowley73473332017-11-21 15:43:51 -0800188 for (;;) {
189 auto result = keymaster_sign_object_for_cryptfs_scrypt(
190 ftr->keymaster_blob, ftr->keymaster_blob_size, KEYMASTER_CRYPTFS_RATE_LIMIT, to_sign,
191 to_sign_size, signature, signature_size);
192 switch (result) {
193 case KeymasterSignResult::ok:
194 return 0;
195 case KeymasterSignResult::upgrade:
196 break;
197 default:
198 return -1;
199 }
200 SLOGD("Upgrading key");
201 if (keymaster_upgrade_key_for_cryptfs_scrypt(
202 RSA_KEY_SIZE, RSA_EXPONENT, KEYMASTER_CRYPTFS_RATE_LIMIT, ftr->keymaster_blob,
203 ftr->keymaster_blob_size, ftr->keymaster_blob, KEYMASTER_BLOB_SIZE,
204 &ftr->keymaster_blob_size) != 0) {
205 SLOGE("Failed to upgrade key");
206 return -1;
207 }
208 if (put_crypt_ftr_and_key(ftr) != 0) {
209 SLOGE("Failed to write upgraded key to disk");
210 }
211 SLOGD("Key upgraded successfully");
212 }
Shawn Willden47ba10d2014-09-03 17:07:06 -0600213}
214
Paul Lawrence399317e2014-03-10 13:20:50 -0700215/* Store password when userdata is successfully decrypted and mounted.
216 * Cleared by cryptfs_clear_password
217 *
218 * To avoid a double prompt at boot, we need to store the CryptKeeper
219 * password and pass it to KeyGuard, which uses it to unlock KeyStore.
220 * Since the entire framework is torn down and rebuilt after encryption,
221 * we have to use a daemon or similar to store the password. Since vold
222 * is secured against IPC except from system processes, it seems a reasonable
223 * place to store this.
224 *
225 * password should be cleared once it has been used.
226 *
227 * password is aged out after password_max_age_seconds seconds.
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800228 */
Paul Lawrence399317e2014-03-10 13:20:50 -0700229static char* password = 0;
230static int password_expiry_time = 0;
231static const int password_max_age_seconds = 60;
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800232
Josh Gaofec44372017-08-28 13:22:55 -0700233enum class RebootType {reboot, recovery, shutdown};
234static void cryptfs_reboot(RebootType rt)
Ken Sumralladfba362013-06-04 16:37:52 -0700235{
Josh Gaofec44372017-08-28 13:22:55 -0700236 switch (rt) {
237 case RebootType::reboot:
Paul Lawrence87999172014-02-20 12:21:31 -0800238 property_set(ANDROID_RB_PROPERTY, "reboot");
239 break;
240
Josh Gaofec44372017-08-28 13:22:55 -0700241 case RebootType::recovery:
Paul Lawrence87999172014-02-20 12:21:31 -0800242 property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
243 break;
244
Josh Gaofec44372017-08-28 13:22:55 -0700245 case RebootType::shutdown:
Paul Lawrence87999172014-02-20 12:21:31 -0800246 property_set(ANDROID_RB_PROPERTY, "shutdown");
247 break;
Ken Sumralladfba362013-06-04 16:37:52 -0700248 }
Paul Lawrence87999172014-02-20 12:21:31 -0800249
Ken Sumralladfba362013-06-04 16:37:52 -0700250 sleep(20);
251
252 /* Shouldn't get here, reboot should happen before sleep times out */
253 return;
254}
255
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800256static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
257{
258 memset(io, 0, dataSize);
259 io->data_size = dataSize;
260 io->data_start = sizeof(struct dm_ioctl);
261 io->version[0] = 4;
262 io->version[1] = 0;
263 io->version[2] = 0;
264 io->flags = flags;
265 if (name) {
Marek Pola5e6b9142015-02-05 14:22:34 +0100266 strlcpy(io->name, name, sizeof(io->name));
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800267 }
268}
269
Greg Kaiser38723f22018-02-16 13:35:35 -0800270namespace {
271
272struct CryptoType;
273
274// Use to get the CryptoType in use on this device.
275const CryptoType &get_crypto_type();
276
277struct CryptoType {
278 // We should only be constructing CryptoTypes as part of
279 // supported_crypto_types[]. We do it via this pseudo-builder pattern,
280 // which isn't pure or fully protected as a concession to being able to
281 // do it all at compile time. Add new CryptoTypes in
282 // supported_crypto_types[] below.
283 constexpr CryptoType() : CryptoType(nullptr, nullptr, 0xFFFFFFFF) {}
284 constexpr CryptoType set_keysize(uint32_t size) const {
285 return CryptoType(this->property_name, this->crypto_name, size);
286 }
287 constexpr CryptoType set_property_name(const char *property) const {
288 return CryptoType(property, this->crypto_name, this->keysize);
289 }
290 constexpr CryptoType set_crypto_name(const char *crypto) const {
291 return CryptoType(this->property_name, crypto, this->keysize);
292 }
293
294 constexpr const char *get_property_name() const { return property_name; }
295 constexpr const char *get_crypto_name() const { return crypto_name; }
296 constexpr uint32_t get_keysize() const { return keysize; }
297
298 private:
299 const char *property_name;
300 const char *crypto_name;
301 uint32_t keysize;
302
303 constexpr CryptoType(const char *property, const char *crypto,
304 uint32_t ksize)
305 : property_name(property), crypto_name(crypto), keysize(ksize) {}
306 friend const CryptoType &get_crypto_type();
307 static const CryptoType &get_device_crypto_algorithm();
308};
309
310// We only want to parse this read-only property once. But we need to wait
311// until the system is initialized before we can read it. So we use a static
312// scoped within this function to get it only once.
313const CryptoType &get_crypto_type() {
314 static CryptoType crypto_type = CryptoType::get_device_crypto_algorithm();
315 return crypto_type;
316}
317
318constexpr CryptoType default_crypto_type = CryptoType()
319 .set_property_name("AES-128-CBC")
320 .set_crypto_name("aes-cbc-essiv:sha256")
321 .set_keysize(16);
322
323constexpr CryptoType supported_crypto_types[] = {
324 default_crypto_type,
325 CryptoType()
326 .set_property_name("Speck128/128-XTS")
327 .set_crypto_name("speck128-xts-plain64")
328 .set_keysize(32),
329 // Add new CryptoTypes here. Order is not important.
330};
331
332
333// ---------- START COMPILE-TIME SANITY CHECK BLOCK -------------------------
334// We confirm all supported_crypto_types have a small enough keysize and
335// had both set_property_name() and set_crypto_name() called.
336
337template <typename T, size_t N>
338constexpr size_t array_length(T (&)[N]) { return N; }
339
340constexpr bool indexOutOfBoundsForCryptoTypes(size_t index) {
341 return (index >= array_length(supported_crypto_types));
342}
343
344constexpr bool isValidCryptoType(const CryptoType &crypto_type) {
345 return ((crypto_type.get_property_name() != nullptr) &&
346 (crypto_type.get_crypto_name() != nullptr) &&
347 (crypto_type.get_keysize() <= MAX_KEY_LEN));
348}
349
350// Note in C++11 that constexpr functions can only have a single line.
351// So our code is a bit convoluted (using recursion instead of a loop),
352// but it's asserting at compile time that all of our key lengths are valid.
353constexpr bool validateSupportedCryptoTypes(size_t index) {
354 return indexOutOfBoundsForCryptoTypes(index) ||
355 (isValidCryptoType(supported_crypto_types[index]) &&
356 validateSupportedCryptoTypes(index + 1));
357}
358
359static_assert(validateSupportedCryptoTypes(0),
360 "We have a CryptoType with keysize > MAX_KEY_LEN or which was "
361 "incompletely constructed.");
362// ---------- END COMPILE-TIME SANITY CHECK BLOCK -------------------------
363
364
365// Don't call this directly, use get_crypto_type(), which caches this result.
366const CryptoType &CryptoType::get_device_crypto_algorithm() {
367 constexpr char CRYPT_ALGO_PROP[] = "ro.crypto.fde_algorithm";
368 char paramstr[PROPERTY_VALUE_MAX];
369
370 property_get(CRYPT_ALGO_PROP, paramstr,
371 default_crypto_type.get_property_name());
372 for (auto const &ctype : supported_crypto_types) {
373 if (strcmp(paramstr, ctype.get_property_name()) == 0) {
374 return ctype;
375 }
376 }
377 ALOGE("Invalid name (%s) for %s. Defaulting to %s\n", paramstr,
378 CRYPT_ALGO_PROP, default_crypto_type.get_property_name());
379 return default_crypto_type;
380}
381
382} // namespace
383
384
385
Kenny Rootc4c70f12013-06-14 12:11:38 -0700386/**
387 * Gets the default device scrypt parameters for key derivation time tuning.
388 * The parameters should lead to about one second derivation time for the
389 * given device.
390 */
391static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700392 char paramstr[PROPERTY_VALUE_MAX];
Paul Crowley63c18d32016-02-10 14:02:47 +0000393 int Nf, rf, pf;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700394
Paul Crowley63c18d32016-02-10 14:02:47 +0000395 property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS);
396 if (!parse_scrypt_parameters(paramstr, &Nf, &rf, &pf)) {
397 SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
398 parse_scrypt_parameters(SCRYPT_DEFAULTS, &Nf, &rf, &pf);
Kenny Rootc4c70f12013-06-14 12:11:38 -0700399 }
Paul Crowley63c18d32016-02-10 14:02:47 +0000400 ftr->N_factor = Nf;
401 ftr->r_factor = rf;
402 ftr->p_factor = pf;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700403}
404
Greg Kaiser57f9af62018-02-16 13:13:58 -0800405uint32_t cryptfs_get_keysize() {
Greg Kaiser38723f22018-02-16 13:35:35 -0800406 return get_crypto_type().get_keysize();
Greg Kaiser57f9af62018-02-16 13:13:58 -0800407}
408
409const char *cryptfs_get_crypto_name() {
Greg Kaiser38723f22018-02-16 13:35:35 -0800410 return get_crypto_type().get_crypto_name();
Greg Kaiser57f9af62018-02-16 13:13:58 -0800411}
412
Ken Sumrall3ed82362011-01-28 23:31:16 -0800413static unsigned int get_fs_size(char *dev)
414{
415 int fd, block_size;
416 struct ext4_super_block sb;
417 off64_t len;
418
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700419 if ((fd = open(dev, O_RDONLY|O_CLOEXEC)) < 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -0800420 SLOGE("Cannot open device to get filesystem size ");
421 return 0;
422 }
423
424 if (lseek64(fd, 1024, SEEK_SET) < 0) {
425 SLOGE("Cannot seek to superblock");
426 return 0;
427 }
428
429 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
430 SLOGE("Cannot read superblock");
431 return 0;
432 }
433
434 close(fd);
435
Daniel Rosenberge82df162014-08-15 22:19:23 +0000436 if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) {
437 SLOGE("Not a valid ext4 superblock");
438 return 0;
439 }
Ken Sumrall3ed82362011-01-28 23:31:16 -0800440 block_size = 1024 << sb.s_log_block_size;
441 /* compute length in bytes */
442 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
443
444 /* return length in sectors */
445 return (unsigned int) (len / 512);
446}
447
Ken Sumrall160b4d62013-04-22 12:15:39 -0700448static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
449{
450 static int cached_data = 0;
451 static off64_t cached_off = 0;
452 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
453 int fd;
454 char key_loc[PROPERTY_VALUE_MAX];
455 char real_blkdev[PROPERTY_VALUE_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -0700456 int rc = -1;
457
458 if (!cached_data) {
Paul Crowleye2ee1522017-09-26 14:05:26 -0700459 fs_mgr_get_crypt_info(fstab_default, key_loc, real_blkdev, sizeof(key_loc));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700460
461 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700462 if ( (fd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700463 SLOGE("Cannot open real block device %s\n", real_blkdev);
464 return -1;
465 }
466
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900467 unsigned long nr_sec = 0;
468 get_blkdev_size(fd, &nr_sec);
469 if (nr_sec != 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700470 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
471 * encryption info footer and key, and plenty of bytes to spare for future
472 * growth.
473 */
474 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
475 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
476 cached_data = 1;
477 } else {
478 SLOGE("Cannot get size of block device %s\n", real_blkdev);
479 }
480 close(fd);
481 } else {
482 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
483 cached_off = 0;
484 cached_data = 1;
485 }
486 }
487
488 if (cached_data) {
489 if (metadata_fname) {
490 *metadata_fname = cached_metadata_fname;
491 }
492 if (off) {
493 *off = cached_off;
494 }
495 rc = 0;
496 }
497
498 return rc;
499}
500
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800501/* Set sha256 checksum in structure */
502static void set_ftr_sha(struct crypt_mnt_ftr *crypt_ftr)
503{
504 SHA256_CTX c;
505 SHA256_Init(&c);
506 memset(crypt_ftr->sha256, 0, sizeof(crypt_ftr->sha256));
507 SHA256_Update(&c, crypt_ftr, sizeof(*crypt_ftr));
508 SHA256_Final(crypt_ftr->sha256, &c);
509}
510
Ken Sumralle8744072011-01-18 22:01:55 -0800511/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800512 * update the failed mount count but not change the key.
513 */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700514static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800515{
516 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800517 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700518 /* starting_off is set to the SEEK_SET offset
519 * where the crypto structure starts
520 */
521 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800522 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700523 char *fname = NULL;
Ken Sumrall3be890f2011-09-14 16:53:46 -0700524 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800525
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800526 set_ftr_sha(crypt_ftr);
527
Ken Sumrall160b4d62013-04-22 12:15:39 -0700528 if (get_crypt_ftr_info(&fname, &starting_off)) {
529 SLOGE("Unable to get crypt_ftr_info\n");
530 return -1;
531 }
532 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700533 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700534 return -1;
535 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700536 if ( (fd = open(fname, O_RDWR | O_CREAT|O_CLOEXEC, 0600)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700537 SLOGE("Cannot open footer file %s for put\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700538 return -1;
539 }
540
541 /* Seek to the start of the crypt footer */
542 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
543 SLOGE("Cannot seek to real block device footer\n");
544 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800545 }
546
547 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
548 SLOGE("Cannot write real block device footer\n");
549 goto errout;
550 }
551
Ken Sumrall3be890f2011-09-14 16:53:46 -0700552 fstat(fd, &statbuf);
553 /* If the keys are kept on a raw block device, do not try to truncate it. */
Ken Sumralle550f782013-08-20 13:48:23 -0700554 if (S_ISREG(statbuf.st_mode)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700555 if (ftruncate(fd, 0x4000)) {
Colin Cross59846b62014-02-06 20:34:29 -0800556 SLOGE("Cannot set footer file size\n");
Ken Sumralle8744072011-01-18 22:01:55 -0800557 goto errout;
558 }
559 }
560
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800561 /* Success! */
562 rc = 0;
563
564errout:
565 close(fd);
566 return rc;
567
568}
569
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800570static bool check_ftr_sha(const struct crypt_mnt_ftr *crypt_ftr)
571{
572 struct crypt_mnt_ftr copy;
573 memcpy(&copy, crypt_ftr, sizeof(copy));
574 set_ftr_sha(&copy);
575 return memcmp(copy.sha256, crypt_ftr->sha256, sizeof(copy.sha256)) == 0;
576}
577
Ken Sumrall160b4d62013-04-22 12:15:39 -0700578static inline int unix_read(int fd, void* buff, int len)
579{
580 return TEMP_FAILURE_RETRY(read(fd, buff, len));
581}
582
583static inline int unix_write(int fd, const void* buff, int len)
584{
585 return TEMP_FAILURE_RETRY(write(fd, buff, len));
586}
587
588static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
589{
590 memset(pdata, 0, len);
591 pdata->persist_magic = PERSIST_DATA_MAGIC;
592 pdata->persist_valid_entries = 0;
593}
594
595/* A routine to update the passed in crypt_ftr to the lastest version.
596 * fd is open read/write on the device that holds the crypto footer and persistent
597 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
598 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
599 */
600static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
601{
Kenny Root7434b312013-06-14 11:29:53 -0700602 int orig_major = crypt_ftr->major_version;
603 int orig_minor = crypt_ftr->minor_version;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700604
Kenny Root7434b312013-06-14 11:29:53 -0700605 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
606 struct crypt_persist_data *pdata;
607 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700608
Kenny Rootc4c70f12013-06-14 12:11:38 -0700609 SLOGW("upgrading crypto footer to 1.1");
610
Wei Wang4375f1b2017-02-24 17:43:01 -0800611 pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
Kenny Root7434b312013-06-14 11:29:53 -0700612 if (pdata == NULL) {
613 SLOGE("Cannot allocate persisent data\n");
614 return;
615 }
616 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
617
618 /* Need to initialize the persistent data area */
619 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
620 SLOGE("Cannot seek to persisent data offset\n");
Henrik Baard91064632015-02-05 15:09:17 +0100621 free(pdata);
Kenny Root7434b312013-06-14 11:29:53 -0700622 return;
623 }
624 /* Write all zeros to the first copy, making it invalid */
625 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
626
627 /* Write a valid but empty structure to the second copy */
628 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
629 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
630
631 /* Update the footer */
632 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
633 crypt_ftr->persist_data_offset[0] = pdata_offset;
634 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
635 crypt_ftr->minor_version = 1;
Henrik Baard91064632015-02-05 15:09:17 +0100636 free(pdata);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700637 }
638
Paul Lawrencef4faa572014-01-29 13:31:03 -0800639 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700640 SLOGW("upgrading crypto footer to 1.2");
JP Abgrall7bdfa522013-11-15 13:42:56 -0800641 /* But keep the old kdf_type.
642 * It will get updated later to KDF_SCRYPT after the password has been verified.
643 */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700644 crypt_ftr->kdf_type = KDF_PBKDF2;
645 get_device_scrypt_params(crypt_ftr);
646 crypt_ftr->minor_version = 2;
647 }
648
Paul Lawrencef4faa572014-01-29 13:31:03 -0800649 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
650 SLOGW("upgrading crypto footer to 1.3");
651 crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
652 crypt_ftr->minor_version = 3;
653 }
654
Kenny Root7434b312013-06-14 11:29:53 -0700655 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
656 if (lseek64(fd, offset, SEEK_SET) == -1) {
657 SLOGE("Cannot seek to crypt footer\n");
658 return;
659 }
660 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700661 }
Ken Sumrall160b4d62013-04-22 12:15:39 -0700662}
663
664
665static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800666{
667 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800668 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700669 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800670 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700671 char *fname = NULL;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700672 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800673
Ken Sumrall160b4d62013-04-22 12:15:39 -0700674 if (get_crypt_ftr_info(&fname, &starting_off)) {
675 SLOGE("Unable to get crypt_ftr_info\n");
676 return -1;
677 }
678 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700679 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700680 return -1;
681 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700682 if ( (fd = open(fname, O_RDWR|O_CLOEXEC)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700683 SLOGE("Cannot open footer file %s for get\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700684 return -1;
685 }
686
687 /* Make sure it's 16 Kbytes in length */
688 fstat(fd, &statbuf);
689 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
690 SLOGE("footer file %s is not the expected size!\n", fname);
691 goto errout;
692 }
693
694 /* Seek to the start of the crypt footer */
695 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
696 SLOGE("Cannot seek to real block device footer\n");
697 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800698 }
699
700 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
701 SLOGE("Cannot read real block device footer\n");
702 goto errout;
703 }
704
705 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700706 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800707 goto errout;
708 }
709
Kenny Rootc96a5f82013-06-14 12:08:28 -0700710 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
711 SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
712 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800713 goto errout;
714 }
715
Greg Kaiser59ad0182018-02-16 13:01:36 -0800716 // We risk buffer overflows with oversized keys, so we just reject them.
717 // 0-sized keys are problematic (essentially by-passing encryption), and
718 // AES-CBC key wrapping only works for multiples of 16 bytes.
719 if ((crypt_ftr->keysize == 0) || ((crypt_ftr->keysize % 16) != 0) ||
720 (crypt_ftr->keysize > MAX_KEY_LEN)) {
721 SLOGE("Invalid keysize (%u) for block device %s; Must be non-zero, "
722 "divisible by 16, and <= %d\n", crypt_ftr->keysize, fname,
723 MAX_KEY_LEN);
724 goto errout;
725 }
726
Kenny Rootc96a5f82013-06-14 12:08:28 -0700727 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
728 SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
729 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800730 }
731
Ken Sumrall160b4d62013-04-22 12:15:39 -0700732 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
733 * copy on disk before returning.
734 */
Kenny Rootc96a5f82013-06-14 12:08:28 -0700735 if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700736 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
Ken Sumralle8744072011-01-18 22:01:55 -0800737 }
738
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800739 /* Success! */
740 rc = 0;
741
742errout:
743 close(fd);
744 return rc;
745}
746
Ken Sumrall160b4d62013-04-22 12:15:39 -0700747static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
748{
749 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
750 crypt_ftr->persist_data_offset[1]) {
751 SLOGE("Crypt_ftr persist data regions overlap");
752 return -1;
753 }
754
755 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
756 SLOGE("Crypt_ftr persist data region 0 starts after region 1");
757 return -1;
758 }
759
760 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
761 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
762 CRYPT_FOOTER_OFFSET) {
763 SLOGE("Persistent data extends past crypto footer");
764 return -1;
765 }
766
767 return 0;
768}
769
770static int load_persistent_data(void)
771{
772 struct crypt_mnt_ftr crypt_ftr;
773 struct crypt_persist_data *pdata = NULL;
774 char encrypted_state[PROPERTY_VALUE_MAX];
775 char *fname;
776 int found = 0;
777 int fd;
778 int ret;
779 int i;
780
781 if (persist_data) {
782 /* Nothing to do, we've already loaded or initialized it */
783 return 0;
784 }
785
786
787 /* If not encrypted, just allocate an empty table and initialize it */
788 property_get("ro.crypto.state", encrypted_state, "");
789 if (strcmp(encrypted_state, "encrypted") ) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800790 pdata = (crypt_persist_data*)malloc(CRYPT_PERSIST_DATA_SIZE);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700791 if (pdata) {
792 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
793 persist_data = pdata;
794 return 0;
795 }
796 return -1;
797 }
798
799 if(get_crypt_ftr_and_key(&crypt_ftr)) {
800 return -1;
801 }
802
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700803 if ((crypt_ftr.major_version < 1)
804 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700805 SLOGE("Crypt_ftr version doesn't support persistent data");
806 return -1;
807 }
808
809 if (get_crypt_ftr_info(&fname, NULL)) {
810 return -1;
811 }
812
813 ret = validate_persistent_data_storage(&crypt_ftr);
814 if (ret) {
815 return -1;
816 }
817
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700818 fd = open(fname, O_RDONLY|O_CLOEXEC);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700819 if (fd < 0) {
820 SLOGE("Cannot open %s metadata file", fname);
821 return -1;
822 }
823
Wei Wang4375f1b2017-02-24 17:43:01 -0800824 pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
Paul Lawrence300dae72016-03-11 11:02:52 -0800825 if (pdata == NULL) {
826 SLOGE("Cannot allocate memory for persistent data");
827 goto err;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700828 }
829
830 for (i = 0; i < 2; i++) {
831 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
832 SLOGE("Cannot seek to read persistent data on %s", fname);
833 goto err2;
834 }
835 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
836 SLOGE("Error reading persistent data on iteration %d", i);
837 goto err2;
838 }
839 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
840 found = 1;
841 break;
842 }
843 }
844
845 if (!found) {
846 SLOGI("Could not find valid persistent data, creating");
847 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
848 }
849
850 /* Success */
851 persist_data = pdata;
852 close(fd);
853 return 0;
854
855err2:
856 free(pdata);
857
858err:
859 close(fd);
860 return -1;
861}
862
863static int save_persistent_data(void)
864{
865 struct crypt_mnt_ftr crypt_ftr;
866 struct crypt_persist_data *pdata;
867 char *fname;
868 off64_t write_offset;
869 off64_t erase_offset;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700870 int fd;
871 int ret;
872
873 if (persist_data == NULL) {
874 SLOGE("No persistent data to save");
875 return -1;
876 }
877
878 if(get_crypt_ftr_and_key(&crypt_ftr)) {
879 return -1;
880 }
881
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700882 if ((crypt_ftr.major_version < 1)
883 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700884 SLOGE("Crypt_ftr version doesn't support persistent data");
885 return -1;
886 }
887
888 ret = validate_persistent_data_storage(&crypt_ftr);
889 if (ret) {
890 return -1;
891 }
892
893 if (get_crypt_ftr_info(&fname, NULL)) {
894 return -1;
895 }
896
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700897 fd = open(fname, O_RDWR|O_CLOEXEC);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700898 if (fd < 0) {
899 SLOGE("Cannot open %s metadata file", fname);
900 return -1;
901 }
902
Wei Wang4375f1b2017-02-24 17:43:01 -0800903 pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700904 if (pdata == NULL) {
905 SLOGE("Cannot allocate persistant data");
906 goto err;
907 }
908
909 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
910 SLOGE("Cannot seek to read persistent data on %s", fname);
911 goto err2;
912 }
913
914 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
915 SLOGE("Error reading persistent data before save");
916 goto err2;
917 }
918
919 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
920 /* The first copy is the curent valid copy, so write to
921 * the second copy and erase this one */
922 write_offset = crypt_ftr.persist_data_offset[1];
923 erase_offset = crypt_ftr.persist_data_offset[0];
924 } else {
925 /* The second copy must be the valid copy, so write to
926 * the first copy, and erase the second */
927 write_offset = crypt_ftr.persist_data_offset[0];
928 erase_offset = crypt_ftr.persist_data_offset[1];
929 }
930
931 /* Write the new copy first, if successful, then erase the old copy */
Björn Landström96dbee72015-01-20 12:43:56 +0100932 if (lseek64(fd, write_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700933 SLOGE("Cannot seek to write persistent data");
934 goto err2;
935 }
936 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
937 (int) crypt_ftr.persist_data_size) {
Björn Landström96dbee72015-01-20 12:43:56 +0100938 if (lseek64(fd, erase_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700939 SLOGE("Cannot seek to erase previous persistent data");
940 goto err2;
941 }
942 fsync(fd);
943 memset(pdata, 0, crypt_ftr.persist_data_size);
944 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
945 (int) crypt_ftr.persist_data_size) {
946 SLOGE("Cannot write to erase previous persistent data");
947 goto err2;
948 }
949 fsync(fd);
950 } else {
951 SLOGE("Cannot write to save persistent data");
952 goto err2;
953 }
954
955 /* Success */
956 free(pdata);
957 close(fd);
958 return 0;
959
960err2:
961 free(pdata);
962err:
963 close(fd);
964 return -1;
965}
966
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800967/* Convert a binary key of specified length into an ascii hex string equivalent,
968 * without the leading 0x and with null termination
969 */
Jeff Sharkey9c484982015-03-31 10:35:33 -0700970static void convert_key_to_hex_ascii(const unsigned char *master_key,
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700971 unsigned int keysize, char *master_key_ascii) {
972 unsigned int i, a;
973 unsigned char nibble;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800974
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700975 for (i=0, a=0; i<keysize; i++, a+=2) {
976 /* For each byte, write out two ascii hex digits */
977 nibble = (master_key[i] >> 4) & 0xf;
978 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800979
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700980 nibble = master_key[i] & 0xf;
981 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
982 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800983
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700984 /* Add the null termination */
985 master_key_ascii[a] = '\0';
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800986
987}
988
Jeff Sharkey9c484982015-03-31 10:35:33 -0700989static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr,
990 const unsigned char *master_key, const char *real_blk_name,
991 const char *name, int fd, const char *extra_params) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800992 alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumralldb5e0262013-02-05 17:39:48 -0800993 struct dm_ioctl *io;
994 struct dm_target_spec *tgt;
995 char *crypt_params;
Greg Kaiser59ad0182018-02-16 13:01:36 -0800996 // We need two ASCII characters to represent each byte, and need space for
997 // the '\0' terminator.
998 char master_key_ascii[MAX_KEY_LEN * 2 + 1];
George Burgess IV605d7ae2016-02-29 13:39:17 -0800999 size_t buff_offset;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001000 int i;
1001
1002 io = (struct dm_ioctl *) buffer;
1003
1004 /* Load the mapping table for this device */
1005 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
1006
1007 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1008 io->target_count = 1;
1009 tgt->status = 0;
1010 tgt->sector_start = 0;
1011 tgt->length = crypt_ftr->fs_size;
Ajay Dudani87701e22014-09-17 21:02:52 -07001012 strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME);
Ken Sumralldb5e0262013-02-05 17:39:48 -08001013
1014 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
1015 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
George Burgess IV605d7ae2016-02-29 13:39:17 -08001016
1017 buff_offset = crypt_params - buffer;
Paul Crowley5afbc622017-11-27 09:42:17 -08001018 SLOGI("Extra parameters for dm_crypt: %s\n", extra_params);
George Burgess IV605d7ae2016-02-29 13:39:17 -08001019 snprintf(crypt_params, sizeof(buffer) - buff_offset, "%s %s 0 %s 0 %s",
1020 crypt_ftr->crypto_type_name, master_key_ascii, real_blk_name,
1021 extra_params);
Ken Sumralldb5e0262013-02-05 17:39:48 -08001022 crypt_params += strlen(crypt_params) + 1;
1023 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
1024 tgt->next = crypt_params - buffer;
1025
1026 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
1027 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
1028 break;
1029 }
1030 usleep(500000);
1031 }
1032
1033 if (i == TABLE_LOAD_RETRIES) {
1034 /* We failed to load the table, return an error */
1035 return -1;
1036 } else {
1037 return i + 1;
1038 }
1039}
1040
1041
1042static int get_dm_crypt_version(int fd, const char *name, int *version)
1043{
1044 char buffer[DM_CRYPT_BUF_SIZE];
1045 struct dm_ioctl *io;
1046 struct dm_target_versions *v;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001047
1048 io = (struct dm_ioctl *) buffer;
1049
1050 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1051
1052 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
1053 return -1;
1054 }
1055
1056 /* Iterate over the returned versions, looking for name of "crypt".
1057 * When found, get and return the version.
1058 */
1059 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
1060 while (v->next) {
1061 if (! strcmp(v->name, "crypt")) {
1062 /* We found the crypt driver, return the version, and get out */
1063 version[0] = v->version[0];
1064 version[1] = v->version[1];
1065 version[2] = v->version[2];
1066 return 0;
1067 }
1068 v = (struct dm_target_versions *)(((char *)v) + v->next);
1069 }
1070
1071 return -1;
1072}
1073
Paul Crowley5afbc622017-11-27 09:42:17 -08001074static std::string extra_params_as_string(const std::vector<std::string>& extra_params_vec) {
1075 if (extra_params_vec.empty()) return "";
1076 std::string extra_params = std::to_string(extra_params_vec.size());
1077 for (const auto& p : extra_params_vec) {
1078 extra_params.append(" ");
1079 extra_params.append(p);
1080 }
1081 return extra_params;
1082}
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001083
Paul Crowley5afbc622017-11-27 09:42:17 -08001084static int create_crypto_blk_dev(struct crypt_mnt_ftr* crypt_ftr, const unsigned char* master_key,
1085 const char* real_blk_name, char* crypto_blk_name, const char* name,
1086 uint32_t flags) {
1087 char buffer[DM_CRYPT_BUF_SIZE];
1088 struct dm_ioctl* io;
1089 unsigned int minor;
1090 int fd = 0;
1091 int err;
1092 int retval = -1;
1093 int version[3];
1094 int load_count;
1095 std::vector<std::string> extra_params_vec;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001096
Paul Crowley5afbc622017-11-27 09:42:17 -08001097 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
1098 SLOGE("Cannot open device-mapper\n");
1099 goto errout;
1100 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001101
Paul Crowley5afbc622017-11-27 09:42:17 -08001102 io = (struct dm_ioctl*)buffer;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001103
Paul Crowley5afbc622017-11-27 09:42:17 -08001104 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1105 err = ioctl(fd, DM_DEV_CREATE, io);
1106 if (err) {
1107 SLOGE("Cannot create dm-crypt device %s: %s\n", name, strerror(errno));
1108 goto errout;
1109 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001110
Paul Crowley5afbc622017-11-27 09:42:17 -08001111 /* Get the device status, in particular, the name of it's device file */
1112 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1113 if (ioctl(fd, DM_DEV_STATUS, io)) {
1114 SLOGE("Cannot retrieve dm-crypt device status\n");
1115 goto errout;
1116 }
1117 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
1118 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
Ken Sumralle919efe2012-09-29 17:07:41 -07001119
Paul Crowley5afbc622017-11-27 09:42:17 -08001120 if (!get_dm_crypt_version(fd, name, version)) {
1121 /* Support for allow_discards was added in version 1.11.0 */
1122 if ((version[0] >= 2) || ((version[0] == 1) && (version[1] >= 11))) {
1123 extra_params_vec.emplace_back("allow_discards");
1124 }
1125 }
1126 if (flags & CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE) {
1127 extra_params_vec.emplace_back("allow_encrypt_override");
1128 }
1129 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name, fd,
1130 extra_params_as_string(extra_params_vec).c_str());
1131 if (load_count < 0) {
1132 SLOGE("Cannot load dm-crypt mapping table.\n");
1133 goto errout;
1134 } else if (load_count > 1) {
1135 SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
1136 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001137
Paul Crowley5afbc622017-11-27 09:42:17 -08001138 /* Resume this device to activate it */
1139 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001140
Paul Crowley5afbc622017-11-27 09:42:17 -08001141 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
1142 SLOGE("Cannot resume the dm-crypt device\n");
1143 goto errout;
1144 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001145
Paul Crowley5afbc622017-11-27 09:42:17 -08001146 /* We made it here with no errors. Woot! */
1147 retval = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001148
1149errout:
1150 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1151
1152 return retval;
1153}
1154
Wei Wang4375f1b2017-02-24 17:43:01 -08001155static int delete_crypto_blk_dev(const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001156{
1157 int fd;
1158 char buffer[DM_CRYPT_BUF_SIZE];
1159 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001160 int retval = -1;
1161
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001162 if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001163 SLOGE("Cannot open device-mapper\n");
1164 goto errout;
1165 }
1166
1167 io = (struct dm_ioctl *) buffer;
1168
1169 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1170 if (ioctl(fd, DM_DEV_REMOVE, io)) {
1171 SLOGE("Cannot remove dm-crypt device\n");
1172 goto errout;
1173 }
1174
1175 /* We made it here with no errors. Woot! */
1176 retval = 0;
1177
1178errout:
1179 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1180
1181 return retval;
1182
1183}
1184
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001185static int pbkdf2(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001186 unsigned char *ikey, void *params UNUSED)
1187{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001188 SLOGI("Using pbkdf2 for cryptfs KDF");
1189
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001190 /* Turn the password into a key and IV that can decrypt the master key */
Adam Langleybf0d9722015-11-04 14:51:39 -08001191 return PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001192 HASH_COUNT, INTERMEDIATE_BUF_SIZE,
Adam Langleybf0d9722015-11-04 14:51:39 -08001193 ikey) != 1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001194}
1195
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001196static int scrypt(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001197 unsigned char *ikey, void *params)
1198{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001199 SLOGI("Using scrypt for cryptfs KDF");
1200
Kenny Rootc4c70f12013-06-14 12:11:38 -07001201 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1202
1203 int N = 1 << ftr->N_factor;
1204 int r = 1 << ftr->r_factor;
1205 int p = 1 << ftr->p_factor;
1206
1207 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001208 crypto_scrypt((const uint8_t*)passwd, strlen(passwd),
1209 salt, SALT_LEN, N, r, p, ikey,
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001210 INTERMEDIATE_BUF_SIZE);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001211
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001212 return 0;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001213}
1214
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001215static int scrypt_keymaster(const char *passwd, const unsigned char *salt,
1216 unsigned char *ikey, void *params)
1217{
1218 SLOGI("Using scrypt with keymaster for cryptfs KDF");
1219
1220 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001221 size_t signature_size;
1222 unsigned char* signature;
1223 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1224
1225 int N = 1 << ftr->N_factor;
1226 int r = 1 << ftr->r_factor;
1227 int p = 1 << ftr->p_factor;
1228
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001229 rc = crypto_scrypt((const uint8_t*)passwd, strlen(passwd),
1230 salt, SALT_LEN, N, r, p, ikey,
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001231 INTERMEDIATE_BUF_SIZE);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001232
1233 if (rc) {
1234 SLOGE("scrypt failed");
1235 return -1;
1236 }
1237
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001238 if (keymaster_sign_object(ftr, ikey, INTERMEDIATE_BUF_SIZE,
Shawn Willdene17a9c42014-09-08 13:04:08 -06001239 &signature, &signature_size)) {
1240 SLOGE("Signing failed");
1241 return -1;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001242 }
1243
1244 rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN,
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001245 N, r, p, ikey, INTERMEDIATE_BUF_SIZE);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001246 free(signature);
1247
1248 if (rc) {
1249 SLOGE("scrypt failed");
1250 return -1;
1251 }
1252
1253 return 0;
1254}
1255
1256static int encrypt_master_key(const char *passwd, const unsigned char *salt,
1257 const unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001258 unsigned char *encrypted_master_key,
1259 struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001260{
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001261 unsigned char ikey[INTERMEDIATE_BUF_SIZE] = { 0 };
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001262 EVP_CIPHER_CTX e_ctx;
1263 int encrypted_len, final_len;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001264 int rc = 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001265
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001266 /* Turn the password into an intermediate key and IV that can decrypt the master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001267 get_device_scrypt_params(crypt_ftr);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001268
1269 switch (crypt_ftr->kdf_type) {
1270 case KDF_SCRYPT_KEYMASTER:
1271 if (keymaster_create_key(crypt_ftr)) {
1272 SLOGE("keymaster_create_key failed");
1273 return -1;
1274 }
1275
1276 if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) {
1277 SLOGE("scrypt failed");
1278 return -1;
1279 }
1280 break;
1281
1282 case KDF_SCRYPT:
1283 if (scrypt(passwd, salt, ikey, crypt_ftr)) {
1284 SLOGE("scrypt failed");
1285 return -1;
1286 }
1287 break;
1288
1289 default:
1290 SLOGE("Invalid kdf_type");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001291 return -1;
1292 }
Kenny Rootc4c70f12013-06-14 12:11:38 -07001293
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001294 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001295 EVP_CIPHER_CTX_init(&e_ctx);
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001296 if (! EVP_EncryptInit_ex(&e_ctx, EVP_aes_128_cbc(), NULL, ikey,
1297 ikey+INTERMEDIATE_KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001298 SLOGE("EVP_EncryptInit failed\n");
1299 return -1;
1300 }
1301 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001302
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001303 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001304 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
Greg Kaiser59ad0182018-02-16 13:01:36 -08001305 decrypted_master_key, crypt_ftr->keysize)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001306 SLOGE("EVP_EncryptUpdate failed\n");
1307 return -1;
1308 }
Adam Langley889c4f12014-09-03 14:23:13 -07001309 if (! EVP_EncryptFinal_ex(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001310 SLOGE("EVP_EncryptFinal failed\n");
1311 return -1;
1312 }
1313
Greg Kaiser59ad0182018-02-16 13:01:36 -08001314 if (encrypted_len + final_len != static_cast<int>(crypt_ftr->keysize)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001315 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
1316 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001317 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001318
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001319 /* Store the scrypt of the intermediate key, so we can validate if it's a
1320 password error or mount error when things go wrong.
1321 Note there's no need to check for errors, since if this is incorrect, we
1322 simply won't wipe userdata, which is the correct default behavior
1323 */
1324 int N = 1 << crypt_ftr->N_factor;
1325 int r = 1 << crypt_ftr->r_factor;
1326 int p = 1 << crypt_ftr->p_factor;
1327
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001328 rc = crypto_scrypt(ikey, INTERMEDIATE_KEY_LEN_BYTES,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001329 crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p,
1330 crypt_ftr->scrypted_intermediate_key,
1331 sizeof(crypt_ftr->scrypted_intermediate_key));
1332
1333 if (rc) {
1334 SLOGE("encrypt_master_key: crypto_scrypt failed");
1335 }
1336
Thurston Hou Yeen Dang06dc3112016-07-18 14:16:37 -07001337 EVP_CIPHER_CTX_cleanup(&e_ctx);
1338
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001339 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001340}
1341
Paul Lawrence731a7a22015-04-28 22:14:15 +00001342static int decrypt_master_key_aux(const char *passwd, unsigned char *salt,
Greg Kaiser59ad0182018-02-16 13:01:36 -08001343 const unsigned char *encrypted_master_key,
1344 size_t keysize,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001345 unsigned char *decrypted_master_key,
1346 kdf_func kdf, void *kdf_params,
1347 unsigned char** intermediate_key,
1348 size_t* intermediate_key_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001349{
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001350 unsigned char ikey[INTERMEDIATE_BUF_SIZE] = { 0 };
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001351 EVP_CIPHER_CTX d_ctx;
1352 int decrypted_len, final_len;
1353
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001354 /* Turn the password into an intermediate key and IV that can decrypt the
1355 master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001356 if (kdf(passwd, salt, ikey, kdf_params)) {
1357 SLOGE("kdf failed");
1358 return -1;
1359 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001360
1361 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001362 EVP_CIPHER_CTX_init(&d_ctx);
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001363 if (! EVP_DecryptInit_ex(&d_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+INTERMEDIATE_KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001364 return -1;
1365 }
1366 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1367 /* Decrypt the master key */
1368 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
Greg Kaiser59ad0182018-02-16 13:01:36 -08001369 encrypted_master_key, keysize)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001370 return -1;
1371 }
Adam Langley889c4f12014-09-03 14:23:13 -07001372 if (! EVP_DecryptFinal_ex(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001373 return -1;
1374 }
1375
Greg Kaiser59ad0182018-02-16 13:01:36 -08001376 if (decrypted_len + final_len != static_cast<int>(keysize)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001377 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001378 }
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001379
1380 /* Copy intermediate key if needed by params */
1381 if (intermediate_key && intermediate_key_size) {
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001382 *intermediate_key = (unsigned char*) malloc(INTERMEDIATE_KEY_LEN_BYTES);
Greg Kaisere8167af2016-04-20 10:50:15 -07001383 if (*intermediate_key) {
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001384 memcpy(*intermediate_key, ikey, INTERMEDIATE_KEY_LEN_BYTES);
1385 *intermediate_key_size = INTERMEDIATE_KEY_LEN_BYTES;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001386 }
1387 }
1388
Thurston Hou Yeen Dang06dc3112016-07-18 14:16:37 -07001389 EVP_CIPHER_CTX_cleanup(&d_ctx);
1390
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001391 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001392}
1393
Kenny Rootc4c70f12013-06-14 12:11:38 -07001394static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001395{
Paul Lawrencedb3730c2015-02-03 13:08:10 -08001396 if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001397 *kdf = scrypt_keymaster;
1398 *kdf_params = ftr;
1399 } else if (ftr->kdf_type == KDF_SCRYPT) {
Kenny Rootc4c70f12013-06-14 12:11:38 -07001400 *kdf = scrypt;
1401 *kdf_params = ftr;
1402 } else {
1403 *kdf = pbkdf2;
1404 *kdf_params = NULL;
1405 }
1406}
1407
Paul Lawrence731a7a22015-04-28 22:14:15 +00001408static int decrypt_master_key(const char *passwd, unsigned char *decrypted_master_key,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001409 struct crypt_mnt_ftr *crypt_ftr,
1410 unsigned char** intermediate_key,
1411 size_t* intermediate_key_size)
Kenny Rootc4c70f12013-06-14 12:11:38 -07001412{
1413 kdf_func kdf;
1414 void *kdf_params;
1415 int ret;
1416
1417 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001418 ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key,
Greg Kaiser59ad0182018-02-16 13:01:36 -08001419 crypt_ftr->keysize,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001420 decrypted_master_key, kdf, kdf_params,
1421 intermediate_key, intermediate_key_size);
Kenny Rootc4c70f12013-06-14 12:11:38 -07001422 if (ret != 0) {
1423 SLOGW("failure decrypting master key");
Kenny Rootc4c70f12013-06-14 12:11:38 -07001424 }
1425
1426 return ret;
1427}
1428
Wei Wang4375f1b2017-02-24 17:43:01 -08001429static int create_encrypted_random_key(const char *passwd, unsigned char *master_key, unsigned char *salt,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001430 struct crypt_mnt_ftr *crypt_ftr) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001431 int fd;
Greg Kaiser59ad0182018-02-16 13:01:36 -08001432 unsigned char key_buf[MAX_KEY_LEN];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001433
1434 /* Get some random bits for a key */
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001435 fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC);
Ken Sumralle8744072011-01-18 22:01:55 -08001436 read(fd, key_buf, sizeof(key_buf));
1437 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001438 close(fd);
1439
1440 /* Now encrypt it with the password */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001441 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001442}
1443
Paul Lawrence2f32cda2015-05-05 14:28:25 -07001444int wait_and_unmount(const char *mountpoint, bool kill)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001445{
Greg Hackmann955653e2014-09-24 14:55:20 -07001446 int i, err, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001447#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001448
1449 /* Now umount the tmpfs filesystem */
1450 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001451 if (umount(mountpoint) == 0) {
1452 break;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001453 }
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001454
1455 if (errno == EINVAL) {
1456 /* EINVAL is returned if the directory is not a mountpoint,
1457 * i.e. there is no filesystem mounted there. So just get out.
1458 */
1459 break;
1460 }
1461
1462 err = errno;
1463
1464 /* If allowed, be increasingly aggressive before the last two retries */
1465 if (kill) {
1466 if (i == (WAIT_UNMOUNT_COUNT - 3)) {
1467 SLOGW("sending SIGHUP to processes with open files\n");
Jeff Sharkey3472e522017-10-06 18:02:53 -06001468 android::vold::KillProcessesWithOpenFiles(mountpoint, SIGTERM);
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001469 } else if (i == (WAIT_UNMOUNT_COUNT - 2)) {
1470 SLOGW("sending SIGKILL to processes with open files\n");
Jeff Sharkey3472e522017-10-06 18:02:53 -06001471 android::vold::KillProcessesWithOpenFiles(mountpoint, SIGKILL);
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001472 }
1473 }
1474
1475 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001476 }
1477
1478 if (i < WAIT_UNMOUNT_COUNT) {
1479 SLOGD("unmounting %s succeeded\n", mountpoint);
1480 rc = 0;
1481 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001482 android::vold::KillProcessesWithOpenFiles(mountpoint, 0);
Greg Hackmann955653e2014-09-24 14:55:20 -07001483 SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001484 rc = -1;
1485 }
1486
1487 return rc;
1488}
1489
Wei Wang42e38102017-06-07 10:46:12 -07001490static void prep_data_fs(void)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001491{
Jeff Sharkey47695b22016-02-01 17:02:29 -07001492 // NOTE: post_fs_data results in init calling back around to vold, so all
1493 // callers to this method must be async
1494
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001495 /* Do the prep of the /data filesystem */
1496 property_set("vold.post_fs_data_done", "0");
1497 property_set("vold.decrypt", "trigger_post_fs_data");
Wei Wang42e38102017-06-07 10:46:12 -07001498 SLOGD("Just triggered post_fs_data");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001499
Ken Sumrallc5872692013-05-14 15:26:31 -07001500 /* Wait a max of 50 seconds, hopefully it takes much less */
Wei Wang42e38102017-06-07 10:46:12 -07001501 while (!android::base::WaitForProperty("vold.post_fs_data_done",
Wei Wang4375f1b2017-02-24 17:43:01 -08001502 "1",
Wei Wang42e38102017-06-07 10:46:12 -07001503 std::chrono::seconds(15))) {
1504 /* We timed out to prep /data in time. Continue wait. */
1505 SLOGE("waited 15s for vold.post_fs_data_done, still waiting...");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001506 }
Wei Wang42e38102017-06-07 10:46:12 -07001507 SLOGD("post_fs_data done");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001508}
1509
Paul Lawrence74f29f12014-08-28 15:54:10 -07001510static void cryptfs_set_corrupt()
1511{
1512 // Mark the footer as bad
1513 struct crypt_mnt_ftr crypt_ftr;
1514 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1515 SLOGE("Failed to get crypto footer - panic");
1516 return;
1517 }
1518
1519 crypt_ftr.flags |= CRYPT_DATA_CORRUPT;
1520 if (put_crypt_ftr_and_key(&crypt_ftr)) {
1521 SLOGE("Failed to set crypto footer - panic");
1522 return;
1523 }
1524}
1525
1526static void cryptfs_trigger_restart_min_framework()
1527{
1528 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
1529 SLOGE("Failed to mount tmpfs on data - panic");
1530 return;
1531 }
1532
1533 if (property_set("vold.decrypt", "trigger_post_fs_data")) {
1534 SLOGE("Failed to trigger post fs data - panic");
1535 return;
1536 }
1537
1538 if (property_set("vold.decrypt", "trigger_restart_min_framework")) {
1539 SLOGE("Failed to trigger restart min framework - panic");
1540 return;
1541 }
1542}
1543
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001544/* returns < 0 on failure */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001545static int cryptfs_restart_internal(int restart_main)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001546{
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001547 char crypto_blkdev[MAXPATHLEN];
Tim Murray8439dc92014-12-15 11:56:11 -08001548 int rc = -1;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001549 static int restart_successful = 0;
1550
1551 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -06001552 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001553 SLOGE("Encrypted filesystem not validated, aborting");
1554 return -1;
1555 }
1556
1557 if (restart_successful) {
1558 SLOGE("System already restarted with encrypted disk, aborting");
1559 return -1;
1560 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001561
Paul Lawrencef4faa572014-01-29 13:31:03 -08001562 if (restart_main) {
1563 /* Here is where we shut down the framework. The init scripts
1564 * start all services in one of three classes: core, main or late_start.
1565 * On boot, we start core and main. Now, we stop main, but not core,
1566 * as core includes vold and a few other really important things that
1567 * we need to keep running. Once main has stopped, we should be able
1568 * to umount the tmpfs /data, then mount the encrypted /data.
1569 * We then restart the class main, and also the class late_start.
1570 * At the moment, I've only put a few things in late_start that I know
1571 * are not needed to bring up the framework, and that also cause problems
1572 * with unmounting the tmpfs /data, but I hope to add add more services
1573 * to the late_start class as we optimize this to decrease the delay
1574 * till the user is asked for the password to the filesystem.
1575 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001576
Paul Lawrencef4faa572014-01-29 13:31:03 -08001577 /* The init files are setup to stop the class main when vold.decrypt is
1578 * set to trigger_reset_main.
1579 */
1580 property_set("vold.decrypt", "trigger_reset_main");
1581 SLOGD("Just asked init to shut down class main\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001582
Paul Lawrencef4faa572014-01-29 13:31:03 -08001583 /* Ugh, shutting down the framework is not synchronous, so until it
1584 * can be fixed, this horrible hack will wait a moment for it all to
1585 * shut down before proceeding. Without it, some devices cannot
1586 * restart the graphics services.
1587 */
1588 sleep(2);
1589 }
Ken Sumrall9dedfd42012-10-09 14:16:59 -07001590
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001591 /* Now that the framework is shutdown, we should be able to umount()
1592 * the tmpfs filesystem, and mount the real one.
1593 */
1594
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001595 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1596 if (strlen(crypto_blkdev) == 0) {
1597 SLOGE("fs_crypto_blkdev not set\n");
1598 return -1;
1599 }
1600
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001601 if (! (rc = wait_and_unmount(DATA_MNT_POINT, true)) ) {
Doug Zongker6fd57712013-12-17 09:43:23 -08001602 /* If ro.crypto.readonly is set to 1, mount the decrypted
1603 * filesystem readonly. This is used when /data is mounted by
1604 * recovery mode.
1605 */
1606 char ro_prop[PROPERTY_VALUE_MAX];
1607 property_get("ro.crypto.readonly", ro_prop, "");
Jeff Sharkey95440eb2017-09-18 18:19:28 -06001608 if (strlen(ro_prop) > 0 && std::stoi(ro_prop)) {
Paul Crowleye2ee1522017-09-26 14:05:26 -07001609 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
Doug Zongker6fd57712013-12-17 09:43:23 -08001610 rec->flags |= MS_RDONLY;
1611 }
JP Abgrall62c7af32014-06-16 13:01:23 -07001612
Ken Sumralle5032c42012-04-01 23:58:44 -07001613 /* If that succeeded, then mount the decrypted filesystem */
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001614 int retries = RETRY_MOUNT_ATTEMPTS;
1615 int mount_rc;
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001616
1617 /*
1618 * fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
1619 * partitions in the fsck domain.
1620 */
1621 if (setexeccon(secontextFsck())){
1622 SLOGE("Failed to setexeccon");
1623 return -1;
1624 }
Paul Crowleye2ee1522017-09-26 14:05:26 -07001625 while ((mount_rc = fs_mgr_do_mount(fstab_default, DATA_MNT_POINT,
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001626 crypto_blkdev, 0))
1627 != 0) {
1628 if (mount_rc == FS_MGR_DOMNT_BUSY) {
1629 /* TODO: invoke something similar to
1630 Process::killProcessWithOpenFiles(DATA_MNT_POINT,
1631 retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */
1632 SLOGI("Failed to mount %s because it is busy - waiting",
1633 crypto_blkdev);
1634 if (--retries) {
1635 sleep(RETRY_MOUNT_DELAY_SECONDS);
1636 } else {
1637 /* Let's hope that a reboot clears away whatever is keeping
1638 the mount busy */
Josh Gaofec44372017-08-28 13:22:55 -07001639 cryptfs_reboot(RebootType::reboot);
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001640 }
1641 } else {
1642 SLOGE("Failed to mount decrypted data");
1643 cryptfs_set_corrupt();
1644 cryptfs_trigger_restart_min_framework();
1645 SLOGI("Started framework to offer wipe");
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001646 if (setexeccon(NULL)) {
1647 SLOGE("Failed to setexeccon");
1648 }
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001649 return -1;
1650 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001651 }
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001652 if (setexeccon(NULL)) {
1653 SLOGE("Failed to setexeccon");
1654 return -1;
1655 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001656
Ken Sumralle5032c42012-04-01 23:58:44 -07001657 /* Create necessary paths on /data */
Wei Wang42e38102017-06-07 10:46:12 -07001658 prep_data_fs();
Seigo Nonakae2ef0c02016-06-20 17:05:40 +09001659 property_set("vold.decrypt", "trigger_load_persist_props");
Ken Sumralle5032c42012-04-01 23:58:44 -07001660
1661 /* startup service classes main and late_start */
1662 property_set("vold.decrypt", "trigger_restart_framework");
1663 SLOGD("Just triggered restart_framework\n");
1664
1665 /* Give it a few moments to get started */
1666 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001667 }
1668
Ken Sumrall0cc16632011-01-18 20:32:26 -08001669 if (rc == 0) {
1670 restart_successful = 1;
1671 }
1672
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001673 return rc;
1674}
1675
Paul Lawrencef4faa572014-01-29 13:31:03 -08001676int cryptfs_restart(void)
1677{
Paul Lawrence05335c32015-03-05 09:46:23 -08001678 SLOGI("cryptfs_restart");
Paul Crowley38132a12016-02-09 09:50:32 +00001679 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001680 SLOGE("cryptfs_restart not valid for file encryption:");
1681 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08001682 }
1683
Paul Lawrencef4faa572014-01-29 13:31:03 -08001684 /* Call internal implementation forcing a restart of main service group */
1685 return cryptfs_restart_internal(1);
1686}
1687
Wei Wang4375f1b2017-02-24 17:43:01 -08001688static int do_crypto_complete(const char *mount_point)
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001689{
1690 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001691 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -08001692 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001693
1694 property_get("ro.crypto.state", encrypted_state, "");
1695 if (strcmp(encrypted_state, "encrypted") ) {
1696 SLOGE("not running with encryption, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001697 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001698 }
1699
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001700 // crypto_complete is full disk encrypted status
Paul Crowley38132a12016-02-09 09:50:32 +00001701 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001702 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Paul Lawrence05335c32015-03-05 09:46:23 -08001703 }
1704
Ken Sumrall160b4d62013-04-22 12:15:39 -07001705 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Crowleye2ee1522017-09-26 14:05:26 -07001706 fs_mgr_get_crypt_info(fstab_default, key_loc, 0, sizeof(key_loc));
Ken Sumralle5032c42012-04-01 23:58:44 -07001707
Ken Sumralle1a45852011-12-14 21:24:27 -08001708 /*
1709 * Only report this error if key_loc is a file and it exists.
1710 * If the device was never encrypted, and /data is not mountable for
1711 * some reason, returning 1 should prevent the UI from presenting the
1712 * a "enter password" screen, or worse, a "press button to wipe the
1713 * device" screen.
1714 */
1715 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
1716 SLOGE("master key file does not exist, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001717 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumralle1a45852011-12-14 21:24:27 -08001718 } else {
1719 SLOGE("Error getting crypt footer and key\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001720 return CRYPTO_COMPLETE_BAD_METADATA;
Ken Sumralle1a45852011-12-14 21:24:27 -08001721 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001722 }
1723
Paul Lawrence74f29f12014-08-28 15:54:10 -07001724 // Test for possible error flags
1725 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS){
1726 SLOGE("Encryption process is partway completed\n");
1727 return CRYPTO_COMPLETE_PARTIAL;
1728 }
1729
1730 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE){
1731 SLOGE("Encryption process was interrupted but cannot continue\n");
1732 return CRYPTO_COMPLETE_INCONSISTENT;
1733 }
1734
1735 if (crypt_ftr.flags & CRYPT_DATA_CORRUPT){
1736 SLOGE("Encryption is successful but data is corrupt\n");
1737 return CRYPTO_COMPLETE_CORRUPT;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001738 }
1739
1740 /* We passed the test! We shall diminish, and return to the west */
Paul Lawrence74f29f12014-08-28 15:54:10 -07001741 return CRYPTO_COMPLETE_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001742}
1743
Paul Lawrencef4faa572014-01-29 13:31:03 -08001744static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
Wei Wang4375f1b2017-02-24 17:43:01 -08001745 const char *passwd, const char *mount_point, const char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001746{
Greg Kaiser59ad0182018-02-16 13:01:36 -08001747 unsigned char decrypted_master_key[MAX_KEY_LEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001748 char crypto_blkdev[MAXPATHLEN];
1749 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001750 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001751 unsigned int orig_failed_decrypt_count;
1752 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001753 int use_keymaster = 0;
1754 int upgrade = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001755 unsigned char* intermediate_key = 0;
1756 size_t intermediate_key_size = 0;
Wei Wang4375f1b2017-02-24 17:43:01 -08001757 int N = 1 << crypt_ftr->N_factor;
1758 int r = 1 << crypt_ftr->r_factor;
1759 int p = 1 << crypt_ftr->p_factor;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001760
Paul Lawrencef4faa572014-01-29 13:31:03 -08001761 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1762 orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001763
Paul Lawrencef4faa572014-01-29 13:31:03 -08001764 if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001765 if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr,
1766 &intermediate_key, &intermediate_key_size)) {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001767 SLOGE("Failed to decrypt master key\n");
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001768 rc = -1;
1769 goto errout;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001770 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001771 }
1772
Paul Crowleye2ee1522017-09-26 14:05:26 -07001773 fs_mgr_get_crypt_info(fstab_default, 0, real_blkdev, sizeof(real_blkdev));
Paul Lawrencef4faa572014-01-29 13:31:03 -08001774
Paul Lawrence74f29f12014-08-28 15:54:10 -07001775 // Create crypto block device - all (non fatal) code paths
1776 // need it
Paul Crowley5afbc622017-11-27 09:42:17 -08001777 if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev, label, 0)) {
1778 SLOGE("Error creating decrypted block device\n");
1779 rc = -1;
1780 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001781 }
1782
Paul Lawrence74f29f12014-08-28 15:54:10 -07001783 /* Work out if the problem is the password or the data */
1784 unsigned char scrypted_intermediate_key[sizeof(crypt_ftr->
1785 scrypted_intermediate_key)];
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001786
Paul Lawrence74f29f12014-08-28 15:54:10 -07001787 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
1788 crypt_ftr->salt, sizeof(crypt_ftr->salt),
1789 N, r, p, scrypted_intermediate_key,
1790 sizeof(scrypted_intermediate_key));
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001791
Paul Lawrence74f29f12014-08-28 15:54:10 -07001792 // Does the key match the crypto footer?
1793 if (rc == 0 && memcmp(scrypted_intermediate_key,
1794 crypt_ftr->scrypted_intermediate_key,
1795 sizeof(scrypted_intermediate_key)) == 0) {
1796 SLOGI("Password matches");
1797 rc = 0;
1798 } else {
1799 /* Try mounting the file system anyway, just in case the problem's with
1800 * the footer, not the key. */
George Burgess IV605d7ae2016-02-29 13:39:17 -08001801 snprintf(tmp_mount_point, sizeof(tmp_mount_point), "%s/tmp_mnt",
1802 mount_point);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001803 mkdir(tmp_mount_point, 0755);
Paul Crowleye2ee1522017-09-26 14:05:26 -07001804 if (fs_mgr_do_mount(fstab_default, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
Paul Lawrence74f29f12014-08-28 15:54:10 -07001805 SLOGE("Error temp mounting decrypted block device\n");
1806 delete_crypto_blk_dev(label);
1807
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001808 rc = ++crypt_ftr->failed_decrypt_count;
1809 put_crypt_ftr_and_key(crypt_ftr);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001810 } else {
1811 /* Success! */
1812 SLOGI("Password did not match but decrypted drive mounted - continue");
1813 umount(tmp_mount_point);
1814 rc = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001815 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001816 }
1817
1818 if (rc == 0) {
1819 crypt_ftr->failed_decrypt_count = 0;
Paul Lawrence72b8b822014-10-05 12:57:37 -07001820 if (orig_failed_decrypt_count != 0) {
1821 put_crypt_ftr_and_key(crypt_ftr);
1822 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001823
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001824 /* Save the name of the crypto block device
Paul Lawrence74f29f12014-08-28 15:54:10 -07001825 * so we can mount it when restarting the framework. */
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001826 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -06001827
1828 /* Also save a the master key so we can reencrypted the key
Paul Lawrence74f29f12014-08-28 15:54:10 -07001829 * the key when we want to change the password on it. */
Greg Kaiser59ad0182018-02-16 13:01:36 -08001830 memcpy(saved_master_key, decrypted_master_key, crypt_ftr->keysize);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001831 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -06001832 master_key_saved = 1;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001833 SLOGD("%s(): Master key saved\n", __FUNCTION__);
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001834 rc = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001835
Paul Lawrence74f29f12014-08-28 15:54:10 -07001836 // Upgrade if we're not using the latest KDF.
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001837 use_keymaster = keymaster_check_compatibility();
1838 if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Shawn Willden47ba10d2014-09-03 17:07:06 -06001839 // Don't allow downgrade
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001840 } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) {
1841 crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1842 upgrade = 1;
1843 } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001844 crypt_ftr->kdf_type = KDF_SCRYPT;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001845 upgrade = 1;
1846 }
1847
1848 if (upgrade) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001849 rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1850 crypt_ftr->master_key, crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001851 if (!rc) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001852 rc = put_crypt_ftr_and_key(crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001853 }
1854 SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
Paul Lawrenceb2f682b2014-09-08 11:28:19 -07001855
1856 // Do not fail even if upgrade failed - machine is bootable
1857 // Note that if this code is ever hit, there is a *serious* problem
1858 // since KDFs should never fail. You *must* fix the kdf before
1859 // proceeding!
1860 if (rc) {
1861 SLOGW("Upgrade failed with error %d,"
1862 " but continuing with previous state",
1863 rc);
1864 rc = 0;
1865 }
JP Abgrall7bdfa522013-11-15 13:42:56 -08001866 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001867 }
1868
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001869 errout:
1870 if (intermediate_key) {
1871 memset(intermediate_key, 0, intermediate_key_size);
1872 free(intermediate_key);
1873 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001874 return rc;
1875}
1876
Ken Sumrall29d8da82011-05-18 17:20:07 -07001877/*
Jeff Sharkey9c484982015-03-31 10:35:33 -07001878 * Called by vold when it's asked to mount an encrypted external
1879 * storage volume. The incoming partition has no crypto header/footer,
Greg Kaiser57f9af62018-02-16 13:13:58 -08001880 * as any metadata is been stored in a separate, small partition. We
1881 * assume it must be using our same crypt type and keysize.
Jeff Sharkey9c484982015-03-31 10:35:33 -07001882 *
1883 * out_crypto_blkdev must be MAXPATHLEN.
Ken Sumrall29d8da82011-05-18 17:20:07 -07001884 */
Jeff Sharkey9c484982015-03-31 10:35:33 -07001885int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev,
Greg Kaiser57f9af62018-02-16 13:13:58 -08001886 const unsigned char* key, char* out_crypto_blkdev) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001887 int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001888 if (fd == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001889 SLOGE("Failed to open %s: %s", real_blkdev, strerror(errno));
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001890 return -1;
1891 }
1892
1893 unsigned long nr_sec = 0;
1894 get_blkdev_size(fd, &nr_sec);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001895 close(fd);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001896
Ken Sumrall29d8da82011-05-18 17:20:07 -07001897 if (nr_sec == 0) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001898 SLOGE("Failed to get size of %s: %s", real_blkdev, strerror(errno));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001899 return -1;
1900 }
1901
Jeff Sharkey9c484982015-03-31 10:35:33 -07001902 struct crypt_mnt_ftr ext_crypt_ftr;
1903 memset(&ext_crypt_ftr, 0, sizeof(ext_crypt_ftr));
1904 ext_crypt_ftr.fs_size = nr_sec;
Greg Kaiser57f9af62018-02-16 13:13:58 -08001905 ext_crypt_ftr.keysize = cryptfs_get_keysize();
1906 strlcpy((char*) ext_crypt_ftr.crypto_type_name, cryptfs_get_crypto_name(),
Jeff Sharkey32ebb732017-03-27 16:18:50 -06001907 MAX_CRYPTO_TYPE_NAME_LEN);
Paul Crowley385cb8c2018-03-29 13:27:23 -07001908 uint32_t flags = 0;
1909 if (e4crypt_is_native() &&
1910 android::base::GetBoolProperty("ro.crypto.allow_encrypt_override", false))
1911 flags |= CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001912
Paul Crowley385cb8c2018-03-29 13:27:23 -07001913 return create_crypto_blk_dev(&ext_crypt_ftr, key, real_blkdev, out_crypto_blkdev, label, flags);
Jeff Sharkey9c484982015-03-31 10:35:33 -07001914}
Ken Sumrall29d8da82011-05-18 17:20:07 -07001915
Jeff Sharkey9c484982015-03-31 10:35:33 -07001916/*
1917 * Called by vold when it's asked to unmount an encrypted external
1918 * storage volume.
1919 */
1920int cryptfs_revert_ext_volume(const char* label) {
1921 return delete_crypto_blk_dev((char*) label);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001922}
1923
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001924int cryptfs_crypto_complete(void)
1925{
1926 return do_crypto_complete("/data");
1927}
1928
Paul Lawrencef4faa572014-01-29 13:31:03 -08001929int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr)
1930{
1931 char encrypted_state[PROPERTY_VALUE_MAX];
1932 property_get("ro.crypto.state", encrypted_state, "");
1933 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
1934 SLOGE("encrypted fs already validated or not running with encryption,"
1935 " aborting");
1936 return -1;
1937 }
1938
1939 if (get_crypt_ftr_and_key(crypt_ftr)) {
1940 SLOGE("Error getting crypt footer and key");
1941 return -1;
1942 }
1943
1944 return 0;
1945}
1946
Wei Wang4375f1b2017-02-24 17:43:01 -08001947int cryptfs_check_passwd(const char *passwd)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001948{
Paul Lawrence05335c32015-03-05 09:46:23 -08001949 SLOGI("cryptfs_check_passwd");
Paul Crowley38132a12016-02-09 09:50:32 +00001950 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001951 SLOGE("cryptfs_check_passwd not valid for file encryption");
1952 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08001953 }
1954
Paul Lawrencef4faa572014-01-29 13:31:03 -08001955 struct crypt_mnt_ftr crypt_ftr;
1956 int rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001957
Paul Lawrencef4faa572014-01-29 13:31:03 -08001958 rc = check_unmounted_and_get_ftr(&crypt_ftr);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001959 if (rc) {
1960 SLOGE("Could not get footer");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001961 return rc;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001962 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001963
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001964 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001965 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
1966 if (rc) {
1967 SLOGE("Password did not match");
1968 return rc;
1969 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08001970
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001971 if (crypt_ftr.flags & CRYPT_FORCE_COMPLETE) {
1972 // Here we have a default actual password but a real password
1973 // we must test against the scrypted value
1974 // First, we must delete the crypto block device that
1975 // test_mount_encrypted_fs leaves behind as a side effect
1976 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
1977 rc = test_mount_encrypted_fs(&crypt_ftr, DEFAULT_PASSWORD,
1978 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
1979 if (rc) {
1980 SLOGE("Default password did not match on reboot encryption");
1981 return rc;
1982 }
1983
1984 crypt_ftr.flags &= ~CRYPT_FORCE_COMPLETE;
1985 put_crypt_ftr_and_key(&crypt_ftr);
1986 rc = cryptfs_changepw(crypt_ftr.crypt_type, passwd);
1987 if (rc) {
1988 SLOGE("Could not change password on reboot encryption");
1989 return rc;
1990 }
1991 }
1992
1993 if (crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
Paul Lawrence399317e2014-03-10 13:20:50 -07001994 cryptfs_clear_password();
1995 password = strdup(passwd);
1996 struct timespec now;
1997 clock_gettime(CLOCK_BOOTTIME, &now);
1998 password_expiry_time = now.tv_sec + password_max_age_seconds;
Paul Lawrence684dbdf2014-02-07 12:07:22 -08001999 }
2000
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002001 return rc;
2002}
2003
Jeff Sharkey83b559c2017-09-12 16:30:52 -06002004int cryptfs_verify_passwd(const char *passwd)
Ken Sumrall3ad90722011-10-04 20:38:29 -07002005{
2006 struct crypt_mnt_ftr crypt_ftr;
Greg Kaiser59ad0182018-02-16 13:01:36 -08002007 unsigned char decrypted_master_key[MAX_KEY_LEN];
Ken Sumrall3ad90722011-10-04 20:38:29 -07002008 char encrypted_state[PROPERTY_VALUE_MAX];
2009 int rc;
2010
2011 property_get("ro.crypto.state", encrypted_state, "");
2012 if (strcmp(encrypted_state, "encrypted") ) {
2013 SLOGE("device not encrypted, aborting");
2014 return -2;
2015 }
2016
2017 if (!master_key_saved) {
2018 SLOGE("encrypted fs not yet mounted, aborting");
2019 return -1;
2020 }
2021
2022 if (!saved_mount_point) {
2023 SLOGE("encrypted fs failed to save mount point, aborting");
2024 return -1;
2025 }
2026
Ken Sumrall160b4d62013-04-22 12:15:39 -07002027 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall3ad90722011-10-04 20:38:29 -07002028 SLOGE("Error getting crypt footer and key\n");
2029 return -1;
2030 }
2031
2032 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
2033 /* If the device has no password, then just say the password is valid */
2034 rc = 0;
2035 } else {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002036 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall3ad90722011-10-04 20:38:29 -07002037 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
2038 /* They match, the password is correct */
2039 rc = 0;
2040 } else {
2041 /* If incorrect, sleep for a bit to prevent dictionary attacks */
2042 sleep(1);
2043 rc = 1;
2044 }
2045 }
2046
2047 return rc;
2048}
2049
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002050/* Initialize a crypt_mnt_ftr structure. The keysize is
Greg Kaiser57f9af62018-02-16 13:13:58 -08002051 * defaulted to cryptfs_get_keysize() bytes, and the filesystem size to 0.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002052 * Presumably, at a minimum, the caller will update the
2053 * filesystem size and crypto_type_name after calling this function.
2054 */
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002055static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002056{
Ken Sumrall160b4d62013-04-22 12:15:39 -07002057 off64_t off;
2058
2059 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002060 ftr->magic = CRYPT_MNT_MAGIC;
Kenny Rootc96a5f82013-06-14 12:08:28 -07002061 ftr->major_version = CURRENT_MAJOR_VERSION;
2062 ftr->minor_version = CURRENT_MINOR_VERSION;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002063 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Greg Kaiser57f9af62018-02-16 13:13:58 -08002064 ftr->keysize = cryptfs_get_keysize();
Ken Sumrall160b4d62013-04-22 12:15:39 -07002065
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002066 switch (keymaster_check_compatibility()) {
2067 case 1:
2068 ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
2069 break;
2070
2071 case 0:
2072 ftr->kdf_type = KDF_SCRYPT;
2073 break;
2074
2075 default:
2076 SLOGE("keymaster_check_compatibility failed");
2077 return -1;
2078 }
2079
Kenny Rootc4c70f12013-06-14 12:11:38 -07002080 get_device_scrypt_params(ftr);
2081
Ken Sumrall160b4d62013-04-22 12:15:39 -07002082 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
2083 if (get_crypt_ftr_info(NULL, &off) == 0) {
2084 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
2085 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
2086 ftr->persist_data_size;
2087 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002088
2089 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002090}
2091
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002092#define FRAMEWORK_BOOT_WAIT 60
2093
Paul Lawrence87999172014-02-20 12:21:31 -08002094static int cryptfs_SHA256_fileblock(const char* filename, __le8* buf)
2095{
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002096 int fd = open(filename, O_RDONLY|O_CLOEXEC);
Paul Lawrence87999172014-02-20 12:21:31 -08002097 if (fd == -1) {
2098 SLOGE("Error opening file %s", filename);
2099 return -1;
2100 }
2101
2102 char block[CRYPT_INPLACE_BUFSIZE];
2103 memset(block, 0, sizeof(block));
2104 if (unix_read(fd, block, sizeof(block)) < 0) {
2105 SLOGE("Error reading file %s", filename);
2106 close(fd);
2107 return -1;
2108 }
2109
2110 close(fd);
2111
2112 SHA256_CTX c;
2113 SHA256_Init(&c);
2114 SHA256_Update(&c, block, sizeof(block));
2115 SHA256_Final(buf, &c);
2116
2117 return 0;
2118}
2119
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002120static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr* crypt_ftr, char* crypto_blkdev,
2121 char* real_blkdev, int previously_encrypted_upto) {
Paul Lawrence87999172014-02-20 12:21:31 -08002122 off64_t cur_encryption_done=0, tot_encryption_size=0;
Tim Murray8439dc92014-12-15 11:56:11 -08002123 int rc = -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002124
Paul Lawrence87999172014-02-20 12:21:31 -08002125 /* The size of the userdata partition, and add in the vold volumes below */
2126 tot_encryption_size = crypt_ftr->fs_size;
2127
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002128 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, crypt_ftr->fs_size, &cur_encryption_done,
Paul Crowley0fd26262018-01-30 09:48:19 -08002129 tot_encryption_size, previously_encrypted_upto, true);
Paul Lawrence87999172014-02-20 12:21:31 -08002130
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002131 if (rc == ENABLE_INPLACE_ERR_DEV) {
2132 /* Hack for b/17898962 */
2133 SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n");
2134 cryptfs_reboot(RebootType::reboot);
2135 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002136
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002137 if (!rc) {
2138 crypt_ftr->encrypted_upto = cur_encryption_done;
2139 }
Paul Lawrence87999172014-02-20 12:21:31 -08002140
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002141 if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) {
2142 /* The inplace routine never actually sets the progress to 100% due
2143 * to the round down nature of integer division, so set it here */
2144 property_set("vold.encrypt_progress", "100");
Paul Lawrence87999172014-02-20 12:21:31 -08002145 }
2146
2147 return rc;
2148}
2149
Paul Crowleyb64933a2017-10-31 08:25:55 -07002150static int vold_unmountAll(void) {
2151 VolumeManager* vm = VolumeManager::Instance();
2152 return vm->unmountAll();
2153}
2154
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002155int cryptfs_enable_internal(int crypt_type, const char* passwd, int no_ui) {
Paul Lawrence87999172014-02-20 12:21:31 -08002156 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN];
Greg Kaiser59ad0182018-02-16 13:01:36 -08002157 unsigned char decrypted_master_key[MAX_KEY_LEN];
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002158 int rc=-1, i;
Paul Lawrence87999172014-02-20 12:21:31 -08002159 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002160 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002161 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002162 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07002163 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall29d8da82011-05-18 17:20:07 -07002164 int num_vols;
Paul Lawrence87999172014-02-20 12:21:31 -08002165 off64_t previously_encrypted_upto = 0;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002166 bool rebootEncryption = false;
Wei Wang4375f1b2017-02-24 17:43:01 -08002167 bool onlyCreateHeader = false;
2168 int fd = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002169
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002170 if (get_crypt_ftr_and_key(&crypt_ftr) == 0) {
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002171 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
2172 /* An encryption was underway and was interrupted */
2173 previously_encrypted_upto = crypt_ftr.encrypted_upto;
2174 crypt_ftr.encrypted_upto = 0;
2175 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002176
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002177 /* At this point, we are in an inconsistent state. Until we successfully
2178 complete encryption, a reboot will leave us broken. So mark the
2179 encryption failed in case that happens.
2180 On successfully completing encryption, remove this flag */
2181 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002182
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002183 put_crypt_ftr_and_key(&crypt_ftr);
2184 } else if (crypt_ftr.flags & CRYPT_FORCE_ENCRYPTION) {
2185 if (!check_ftr_sha(&crypt_ftr)) {
2186 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
2187 put_crypt_ftr_and_key(&crypt_ftr);
2188 goto error_unencrypted;
2189 }
2190
2191 /* Doing a reboot-encryption*/
2192 crypt_ftr.flags &= ~CRYPT_FORCE_ENCRYPTION;
2193 crypt_ftr.flags |= CRYPT_FORCE_COMPLETE;
2194 rebootEncryption = true;
2195 }
Greg Kaiser59ad0182018-02-16 13:01:36 -08002196 } else {
2197 // We don't want to accidentally reference invalid data.
2198 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
Paul Lawrence87999172014-02-20 12:21:31 -08002199 }
2200
2201 property_get("ro.crypto.state", encrypted_state, "");
2202 if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) {
2203 SLOGE("Device is already running encrypted, aborting");
2204 goto error_unencrypted;
2205 }
2206
2207 // TODO refactor fs_mgr_get_crypt_info to get both in one call
Paul Crowleye2ee1522017-09-26 14:05:26 -07002208 fs_mgr_get_crypt_info(fstab_default, key_loc, 0, sizeof(key_loc));
2209 fs_mgr_get_crypt_info(fstab_default, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002210
Ken Sumrall3ed82362011-01-28 23:31:16 -08002211 /* Get the size of the real block device */
Wei Wang4375f1b2017-02-24 17:43:01 -08002212 fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002213 if (fd == -1) {
2214 SLOGE("Cannot open block device %s\n", real_blkdev);
2215 goto error_unencrypted;
2216 }
2217 unsigned long nr_sec;
2218 get_blkdev_size(fd, &nr_sec);
2219 if (nr_sec == 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002220 SLOGE("Cannot get size of block device %s\n", real_blkdev);
2221 goto error_unencrypted;
2222 }
2223 close(fd);
2224
2225 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002226 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002227 unsigned int fs_size_sec, max_fs_size_sec;
Jim Millera70abc62014-08-15 02:00:45 +00002228 fs_size_sec = get_fs_size(real_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002229 if (fs_size_sec == 0)
2230 fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev);
2231
Paul Lawrence87999172014-02-20 12:21:31 -08002232 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002233
2234 if (fs_size_sec > max_fs_size_sec) {
2235 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
2236 goto error_unencrypted;
2237 }
2238 }
2239
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002240 /* Get a wakelock as this may take a while, and we don't want the
2241 * device to sleep on us. We'll grab a partial wakelock, and if the UI
2242 * wants to keep the screen on, it can grab a full wakelock.
2243 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002244 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002245 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
2246
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002247 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002248 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002249 */
2250 property_set("vold.decrypt", "trigger_shutdown_framework");
2251 SLOGD("Just asked init to shut down class main\n");
2252
Jeff Sharkey9c484982015-03-31 10:35:33 -07002253 /* Ask vold to unmount all devices that it manages */
2254 if (vold_unmountAll()) {
2255 SLOGE("Failed to unmount all vold managed devices");
Ken Sumrall2eaf7132011-01-14 12:45:48 -08002256 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002257
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002258 /* no_ui means we are being called from init, not settings.
2259 Now we always reboot from settings, so !no_ui means reboot
2260 */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002261 if (!no_ui) {
2262 /* Try fallback, which is to reboot and try there */
2263 onlyCreateHeader = true;
2264 FILE* breadcrumb = fopen(BREADCRUMB_FILE, "we");
2265 if (breadcrumb == 0) {
2266 SLOGE("Failed to create breadcrumb file");
2267 goto error_shutting_down;
2268 }
2269 fclose(breadcrumb);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002270 }
2271
2272 /* Do extra work for a better UX when doing the long inplace encryption */
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002273 if (!onlyCreateHeader) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002274 /* Now that /data is unmounted, we need to mount a tmpfs
2275 * /data, set a property saying we're doing inplace encryption,
2276 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002277 */
Ken Sumralle5032c42012-04-01 23:58:44 -07002278 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002279 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002280 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002281 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08002282 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002283
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002284 /* restart the framework. */
2285 /* Create necessary paths on /data */
Wei Wang42e38102017-06-07 10:46:12 -07002286 prep_data_fs();
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002287
Ken Sumrall92736ef2012-10-17 20:57:14 -07002288 /* Ugh, shutting down the framework is not synchronous, so until it
2289 * can be fixed, this horrible hack will wait a moment for it all to
2290 * shut down before proceeding. Without it, some devices cannot
2291 * restart the graphics services.
2292 */
2293 sleep(2);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002294 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002295
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002296 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002297 /* Initialize a crypt_mnt_ftr for the partition */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002298 if (previously_encrypted_upto == 0 && !rebootEncryption) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002299 if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) {
2300 goto error_shutting_down;
2301 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002302
Paul Lawrence87999172014-02-20 12:21:31 -08002303 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
2304 crypt_ftr.fs_size = nr_sec
2305 - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
2306 } else {
2307 crypt_ftr.fs_size = nr_sec;
2308 }
Paul Lawrence6bfed202014-07-28 12:47:22 -07002309 /* At this point, we are in an inconsistent state. Until we successfully
2310 complete encryption, a reboot will leave us broken. So mark the
2311 encryption failed in case that happens.
2312 On successfully completing encryption, remove this flag */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002313 if (onlyCreateHeader) {
2314 crypt_ftr.flags |= CRYPT_FORCE_ENCRYPTION;
2315 } else {
2316 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
2317 }
Paul Lawrence87999172014-02-20 12:21:31 -08002318 crypt_ftr.crypt_type = crypt_type;
Greg Kaiser57f9af62018-02-16 13:13:58 -08002319 strlcpy((char *)crypt_ftr.crypto_type_name, cryptfs_get_crypto_name(), MAX_CRYPTO_TYPE_NAME_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002320
Paul Lawrence87999172014-02-20 12:21:31 -08002321 /* Make an encrypted master key */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002322 if (create_encrypted_random_key(onlyCreateHeader ? DEFAULT_PASSWORD : passwd,
2323 crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
Paul Lawrence87999172014-02-20 12:21:31 -08002324 SLOGE("Cannot create encrypted master key\n");
2325 goto error_shutting_down;
2326 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002327
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002328 /* Replace scrypted intermediate key if we are preparing for a reboot */
2329 if (onlyCreateHeader) {
Greg Kaiser59ad0182018-02-16 13:01:36 -08002330 unsigned char fake_master_key[MAX_KEY_LEN];
2331 unsigned char encrypted_fake_master_key[MAX_KEY_LEN];
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002332 memset(fake_master_key, 0, sizeof(fake_master_key));
2333 encrypt_master_key(passwd, crypt_ftr.salt, fake_master_key,
2334 encrypted_fake_master_key, &crypt_ftr);
2335 }
2336
Paul Lawrence87999172014-02-20 12:21:31 -08002337 /* Write the key to the end of the partition */
2338 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002339
Paul Lawrence87999172014-02-20 12:21:31 -08002340 /* If any persistent data has been remembered, save it.
2341 * If none, create a valid empty table and save that.
2342 */
2343 if (!persist_data) {
Wei Wang4375f1b2017-02-24 17:43:01 -08002344 pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
Paul Lawrence87999172014-02-20 12:21:31 -08002345 if (pdata) {
2346 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
2347 persist_data = pdata;
2348 }
2349 }
2350 if (persist_data) {
2351 save_persistent_data();
2352 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002353 }
2354
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002355 if (onlyCreateHeader) {
2356 sleep(2);
Josh Gaofec44372017-08-28 13:22:55 -07002357 cryptfs_reboot(RebootType::reboot);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002358 }
2359
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002360 if (!no_ui || rebootEncryption) {
Ajay Dudani87701e22014-09-17 21:02:52 -07002361 /* startup service classes main and late_start */
2362 property_set("vold.decrypt", "trigger_restart_min_framework");
2363 SLOGD("Just triggered restart_min_framework\n");
2364
2365 /* OK, the framework is restarted and will soon be showing a
2366 * progress bar. Time to setup an encrypted mapping, and
2367 * either write a new filesystem, or encrypt in place updating
2368 * the progress bar as we work.
2369 */
2370 }
2371
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002372 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002373 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
Paul Crowley5afbc622017-11-27 09:42:17 -08002374 CRYPTO_BLOCK_DEVICE, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002375
Paul Lawrence87999172014-02-20 12:21:31 -08002376 /* If we are continuing, check checksums match */
2377 rc = 0;
2378 if (previously_encrypted_upto) {
2379 __le8 hash_first_block[SHA256_DIGEST_LENGTH];
2380 rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block);
Ken Sumrall128626f2011-06-28 18:45:14 -07002381
Paul Lawrence87999172014-02-20 12:21:31 -08002382 if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block,
2383 sizeof(hash_first_block)) != 0) {
2384 SLOGE("Checksums do not match - trigger wipe");
2385 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002386 }
2387 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002388
Paul Lawrence87999172014-02-20 12:21:31 -08002389 if (!rc) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002390 rc = cryptfs_enable_all_volumes(&crypt_ftr, crypto_blkdev, real_blkdev,
Paul Lawrence87999172014-02-20 12:21:31 -08002391 previously_encrypted_upto);
2392 }
2393
2394 /* Calculate checksum if we are not finished */
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002395 if (!rc && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002396 rc = cryptfs_SHA256_fileblock(crypto_blkdev,
2397 crypt_ftr.hash_first_block);
Paul Lawrence73d7a022014-06-09 14:10:09 -07002398 if (rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002399 SLOGE("Error calculating checksum for continuing encryption");
2400 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002401 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002402 }
2403
2404 /* Undo the dm-crypt mapping whether we succeed or not */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002405 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002406
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002407 if (! rc) {
2408 /* Success */
Paul Lawrence6bfed202014-07-28 12:47:22 -07002409 crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08002410
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002411 if (crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002412 SLOGD("Encrypted up to sector %lld - will continue after reboot",
2413 crypt_ftr.encrypted_upto);
Paul Lawrence6bfed202014-07-28 12:47:22 -07002414 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence87999172014-02-20 12:21:31 -08002415 }
Paul Lawrence73d7a022014-06-09 14:10:09 -07002416
Paul Lawrence6bfed202014-07-28 12:47:22 -07002417 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08002418
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002419 if (crypt_ftr.encrypted_upto == crypt_ftr.fs_size) {
2420 char value[PROPERTY_VALUE_MAX];
2421 property_get("ro.crypto.state", value, "");
2422 if (!strcmp(value, "")) {
2423 /* default encryption - continue first boot sequence */
2424 property_set("ro.crypto.state", "encrypted");
2425 property_set("ro.crypto.type", "block");
2426 release_wake_lock(lockid);
2427 if (rebootEncryption && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
2428 // Bring up cryptkeeper that will check the password and set it
2429 property_set("vold.decrypt", "trigger_shutdown_framework");
2430 sleep(2);
2431 property_set("vold.encrypt_progress", "");
2432 cryptfs_trigger_restart_min_framework();
2433 } else {
2434 cryptfs_check_passwd(DEFAULT_PASSWORD);
2435 cryptfs_restart_internal(1);
2436 }
2437 return 0;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002438 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002439 sleep(2); /* Give the UI a chance to show 100% progress */
2440 cryptfs_reboot(RebootType::reboot);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002441 }
Paul Lawrence87999172014-02-20 12:21:31 -08002442 } else {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002443 sleep(2); /* Partially encrypted, ensure writes flushed to ssd */
Josh Gaofec44372017-08-28 13:22:55 -07002444 cryptfs_reboot(RebootType::shutdown);
Paul Lawrence87999172014-02-20 12:21:31 -08002445 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002446 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002447 char value[PROPERTY_VALUE_MAX];
2448
Ken Sumrall319369a2012-06-27 16:30:18 -07002449 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002450 if (!strcmp(value, "1")) {
2451 /* wipe data if encryption failed */
2452 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
Wei Wang4375f1b2017-02-24 17:43:01 -08002453 std::string err;
2454 const std::vector<std::string> options = {
2455 "--wipe_data\n--reason=cryptfs_enable_internal\n"
2456 };
2457 if (!write_bootloader_message(options, &err)) {
2458 SLOGE("could not write bootloader message: %s", err.c_str());
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002459 }
Josh Gaofec44372017-08-28 13:22:55 -07002460 cryptfs_reboot(RebootType::recovery);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002461 } else {
2462 /* set property to trigger dialog */
2463 property_set("vold.encrypt_progress", "error_partially_encrypted");
2464 release_wake_lock(lockid);
2465 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002466 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002467 }
2468
Ken Sumrall3ed82362011-01-28 23:31:16 -08002469 /* hrm, the encrypt step claims success, but the reboot failed.
2470 * This should not happen.
2471 * Set the property and return. Hope the framework can deal with it.
2472 */
2473 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002474 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002475 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08002476
2477error_unencrypted:
2478 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002479 if (lockid[0]) {
2480 release_wake_lock(lockid);
2481 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002482 return -1;
2483
2484error_shutting_down:
2485 /* we failed, and have not encrypted anthing, so the users's data is still intact,
2486 * but the framework is stopped and not restarted to show the error, so it's up to
2487 * vold to restart the system.
2488 */
2489 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Josh Gaofec44372017-08-28 13:22:55 -07002490 cryptfs_reboot(RebootType::reboot);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002491
2492 /* shouldn't get here */
2493 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002494 if (lockid[0]) {
2495 release_wake_lock(lockid);
2496 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002497 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002498}
2499
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002500int cryptfs_enable(int type, const char* passwd, int no_ui) {
2501 return cryptfs_enable_internal(type, passwd, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08002502}
2503
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002504int cryptfs_enable_default(int no_ui) {
2505 return cryptfs_enable_internal(CRYPT_TYPE_DEFAULT, DEFAULT_PASSWORD, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08002506}
2507
2508int cryptfs_changepw(int crypt_type, const char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002509{
Paul Crowley38132a12016-02-09 09:50:32 +00002510 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002511 SLOGE("cryptfs_changepw not valid for file encryption");
2512 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08002513 }
2514
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002515 struct crypt_mnt_ftr crypt_ftr;
JP Abgrall933216c2015-02-11 13:44:32 -08002516 int rc;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002517
2518 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08002519 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08002520 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002521 return -1;
2522 }
2523
Paul Lawrencef4faa572014-01-29 13:31:03 -08002524 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2525 SLOGE("Invalid crypt_type %d", crypt_type);
2526 return -1;
2527 }
2528
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002529 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002530 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08002531 SLOGE("Error getting crypt footer and key");
2532 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002533 }
2534
Paul Lawrencef4faa572014-01-29 13:31:03 -08002535 crypt_ftr.crypt_type = crypt_type;
2536
JP Abgrall933216c2015-02-11 13:44:32 -08002537 rc = encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
Paul Lawrencef4faa572014-01-29 13:31:03 -08002538 : newpw,
2539 crypt_ftr.salt,
2540 saved_master_key,
2541 crypt_ftr.master_key,
2542 &crypt_ftr);
JP Abgrall933216c2015-02-11 13:44:32 -08002543 if (rc) {
2544 SLOGE("Encrypt master key failed: %d", rc);
2545 return -1;
2546 }
Jason parks70a4b3f2011-01-28 10:10:47 -06002547 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002548 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002549
2550 return 0;
2551}
Ken Sumrall160b4d62013-04-22 12:15:39 -07002552
Rubin Xu85c01f92014-10-13 12:49:54 +01002553static unsigned int persist_get_max_entries(int encrypted) {
2554 struct crypt_mnt_ftr crypt_ftr;
2555 unsigned int dsize;
2556 unsigned int max_persistent_entries;
2557
2558 /* If encrypted, use the values from the crypt_ftr, otherwise
2559 * use the values for the current spec.
2560 */
2561 if (encrypted) {
2562 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2563 return -1;
2564 }
2565 dsize = crypt_ftr.persist_data_size;
2566 } else {
2567 dsize = CRYPT_PERSIST_DATA_SIZE;
2568 }
2569
2570 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
2571 sizeof(struct crypt_persist_entry);
2572
2573 return max_persistent_entries;
2574}
2575
2576static int persist_get_key(const char *fieldname, char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002577{
2578 unsigned int i;
2579
2580 if (persist_data == NULL) {
2581 return -1;
2582 }
2583 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2584 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2585 /* We found it! */
2586 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
2587 return 0;
2588 }
2589 }
2590
2591 return -1;
2592}
2593
Rubin Xu85c01f92014-10-13 12:49:54 +01002594static int persist_set_key(const char *fieldname, const char *value, int encrypted)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002595{
2596 unsigned int i;
2597 unsigned int num;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002598 unsigned int max_persistent_entries;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002599
2600 if (persist_data == NULL) {
2601 return -1;
2602 }
2603
Rubin Xu85c01f92014-10-13 12:49:54 +01002604 max_persistent_entries = persist_get_max_entries(encrypted);
Ken Sumrall160b4d62013-04-22 12:15:39 -07002605
2606 num = persist_data->persist_valid_entries;
2607
2608 for (i = 0; i < num; i++) {
2609 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2610 /* We found an existing entry, update it! */
2611 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
2612 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
2613 return 0;
2614 }
2615 }
2616
2617 /* We didn't find it, add it to the end, if there is room */
2618 if (persist_data->persist_valid_entries < max_persistent_entries) {
2619 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
2620 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
2621 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
2622 persist_data->persist_valid_entries++;
2623 return 0;
2624 }
2625
2626 return -1;
2627}
2628
Rubin Xu85c01f92014-10-13 12:49:54 +01002629/**
2630 * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the
2631 * sequence and its index is greater than or equal to index. Return 0 otherwise.
2632 */
Jeff Sharkey95440eb2017-09-18 18:19:28 -06002633int match_multi_entry(const char *key, const char *field, unsigned index) {
2634 std::string key_ = key;
2635 std::string field_ = field;
Rubin Xu85c01f92014-10-13 12:49:54 +01002636
Jeff Sharkey95440eb2017-09-18 18:19:28 -06002637 std::string parsed_field;
2638 unsigned parsed_index;
2639
2640 std::string::size_type split = key_.find_last_of('_');
2641 if (split == std::string::npos) {
2642 parsed_field = key_;
2643 parsed_index = 0;
2644 } else {
2645 parsed_field = key_.substr(0, split);
2646 parsed_index = std::stoi(key_.substr(split + 1));
Rubin Xu85c01f92014-10-13 12:49:54 +01002647 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -06002648
2649 return parsed_field == field_ && parsed_index >= index;
Rubin Xu85c01f92014-10-13 12:49:54 +01002650}
2651
2652/*
2653 * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all
2654 * remaining entries starting from index will be deleted.
2655 * returns PERSIST_DEL_KEY_OK if deletion succeeds,
2656 * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist,
2657 * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs.
2658 *
2659 */
2660static int persist_del_keys(const char *fieldname, unsigned index)
2661{
2662 unsigned int i;
2663 unsigned int j;
2664 unsigned int num;
2665
2666 if (persist_data == NULL) {
2667 return PERSIST_DEL_KEY_ERROR_OTHER;
2668 }
2669
2670 num = persist_data->persist_valid_entries;
2671
2672 j = 0; // points to the end of non-deleted entries.
2673 // Filter out to-be-deleted entries in place.
2674 for (i = 0; i < num; i++) {
2675 if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) {
2676 persist_data->persist_entry[j] = persist_data->persist_entry[i];
2677 j++;
2678 }
2679 }
2680
2681 if (j < num) {
2682 persist_data->persist_valid_entries = j;
2683 // Zeroise the remaining entries
2684 memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry));
2685 return PERSIST_DEL_KEY_OK;
2686 } else {
2687 // Did not find an entry matching the given fieldname
2688 return PERSIST_DEL_KEY_ERROR_NO_FIELD;
2689 }
2690}
2691
2692static int persist_count_keys(const char *fieldname)
2693{
2694 unsigned int i;
2695 unsigned int count;
2696
2697 if (persist_data == NULL) {
2698 return -1;
2699 }
2700
2701 count = 0;
2702 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2703 if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) {
2704 count++;
2705 }
2706 }
2707
2708 return count;
2709}
2710
Ken Sumrall160b4d62013-04-22 12:15:39 -07002711/* Return the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01002712int cryptfs_getfield(const char *fieldname, char *value, int len)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002713{
Paul Crowley38132a12016-02-09 09:50:32 +00002714 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08002715 SLOGE("Cannot get field when file encrypted");
2716 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07002717 }
2718
Ken Sumrall160b4d62013-04-22 12:15:39 -07002719 char temp_value[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01002720 /* CRYPTO_GETFIELD_OK is success,
2721 * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set,
2722 * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small,
2723 * CRYPTO_GETFIELD_ERROR_OTHER is any other error
Ken Sumrall160b4d62013-04-22 12:15:39 -07002724 */
Rubin Xu85c01f92014-10-13 12:49:54 +01002725 int rc = CRYPTO_GETFIELD_ERROR_OTHER;
2726 int i;
2727 char temp_field[PROPERTY_KEY_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -07002728
2729 if (persist_data == NULL) {
2730 load_persistent_data();
2731 if (persist_data == NULL) {
2732 SLOGE("Getfield error, cannot load persistent data");
2733 goto out;
2734 }
2735 }
2736
Rubin Xu85c01f92014-10-13 12:49:54 +01002737 // Read value from persistent entries. If the original value is split into multiple entries,
2738 // stitch them back together.
Ken Sumrall160b4d62013-04-22 12:15:39 -07002739 if (!persist_get_key(fieldname, temp_value)) {
Rubin Xu85c01f92014-10-13 12:49:54 +01002740 // We found it, copy it to the caller's buffer and keep going until all entries are read.
2741 if (strlcpy(value, temp_value, len) >= (unsigned) len) {
2742 // value too small
2743 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
2744 goto out;
2745 }
2746 rc = CRYPTO_GETFIELD_OK;
2747
2748 for (i = 1; /* break explicitly */; i++) {
2749 if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >=
2750 (int) sizeof(temp_field)) {
2751 // If the fieldname is very long, we stop as soon as it begins to overflow the
2752 // maximum field length. At this point we have in fact fully read out the original
2753 // value because cryptfs_setfield would not allow fields with longer names to be
2754 // written in the first place.
2755 break;
2756 }
2757 if (!persist_get_key(temp_field, temp_value)) {
2758 if (strlcat(value, temp_value, len) >= (unsigned)len) {
2759 // value too small.
2760 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
2761 goto out;
2762 }
2763 } else {
2764 // Exhaust all entries.
2765 break;
2766 }
2767 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002768 } else {
2769 /* Sadness, it's not there. Return the error */
Rubin Xu85c01f92014-10-13 12:49:54 +01002770 rc = CRYPTO_GETFIELD_ERROR_NO_FIELD;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002771 }
2772
2773out:
2774 return rc;
2775}
2776
2777/* Set the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01002778int cryptfs_setfield(const char *fieldname, const char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002779{
Paul Crowley38132a12016-02-09 09:50:32 +00002780 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08002781 SLOGE("Cannot set field when file encrypted");
2782 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07002783 }
2784
Ken Sumrall160b4d62013-04-22 12:15:39 -07002785 char encrypted_state[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01002786 /* 0 is success, negative values are error */
2787 int rc = CRYPTO_SETFIELD_ERROR_OTHER;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002788 int encrypted = 0;
Rubin Xu85c01f92014-10-13 12:49:54 +01002789 unsigned int field_id;
2790 char temp_field[PROPERTY_KEY_MAX];
2791 unsigned int num_entries;
2792 unsigned int max_keylen;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002793
2794 if (persist_data == NULL) {
2795 load_persistent_data();
2796 if (persist_data == NULL) {
2797 SLOGE("Setfield error, cannot load persistent data");
2798 goto out;
2799 }
2800 }
2801
2802 property_get("ro.crypto.state", encrypted_state, "");
2803 if (!strcmp(encrypted_state, "encrypted") ) {
2804 encrypted = 1;
2805 }
2806
Rubin Xu85c01f92014-10-13 12:49:54 +01002807 // Compute the number of entries required to store value, each entry can store up to
2808 // (PROPERTY_VALUE_MAX - 1) chars
2809 if (strlen(value) == 0) {
2810 // Empty value also needs one entry to store.
2811 num_entries = 1;
2812 } else {
2813 num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1);
2814 }
2815
2816 max_keylen = strlen(fieldname);
2817 if (num_entries > 1) {
2818 // Need an extra "_%d" suffix.
2819 max_keylen += 1 + log10(num_entries);
2820 }
2821 if (max_keylen > PROPERTY_KEY_MAX - 1) {
2822 rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002823 goto out;
2824 }
2825
Rubin Xu85c01f92014-10-13 12:49:54 +01002826 // Make sure we have enough space to write the new value
2827 if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) >
2828 persist_get_max_entries(encrypted)) {
2829 rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG;
2830 goto out;
2831 }
2832
2833 // Now that we know persist_data has enough space for value, let's delete the old field first
2834 // to make up space.
2835 persist_del_keys(fieldname, 0);
2836
2837 if (persist_set_key(fieldname, value, encrypted)) {
2838 // fail to set key, should not happen as we have already checked the available space
2839 SLOGE("persist_set_key() error during setfield()");
2840 goto out;
2841 }
2842
2843 for (field_id = 1; field_id < num_entries; field_id++) {
Greg Kaiserb610e772018-02-09 09:19:54 -08002844 snprintf(temp_field, sizeof(temp_field), "%s_%u", fieldname, field_id);
Rubin Xu85c01f92014-10-13 12:49:54 +01002845
2846 if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), encrypted)) {
2847 // fail to set key, should not happen as we have already checked the available space.
2848 SLOGE("persist_set_key() error during setfield()");
2849 goto out;
2850 }
2851 }
2852
Ken Sumrall160b4d62013-04-22 12:15:39 -07002853 /* If we are running encrypted, save the persistent data now */
2854 if (encrypted) {
2855 if (save_persistent_data()) {
2856 SLOGE("Setfield error, cannot save persistent data");
2857 goto out;
2858 }
2859 }
2860
Rubin Xu85c01f92014-10-13 12:49:54 +01002861 rc = CRYPTO_SETFIELD_OK;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002862
2863out:
2864 return rc;
2865}
Paul Lawrencef4faa572014-01-29 13:31:03 -08002866
2867/* Checks userdata. Attempt to mount the volume if default-
2868 * encrypted.
2869 * On success trigger next init phase and return 0.
2870 * Currently do not handle failure - see TODO below.
2871 */
2872int cryptfs_mount_default_encrypted(void)
2873{
Paul Lawrence84274cc2016-04-15 15:41:33 -07002874 int crypt_type = cryptfs_get_password_type();
2875 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2876 SLOGE("Bad crypt type - error");
2877 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
2878 SLOGD("Password is not default - "
2879 "starting min framework to prompt");
2880 property_set("vold.decrypt", "trigger_restart_min_framework");
2881 return 0;
2882 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
2883 SLOGD("Password is default - restarting filesystem");
2884 cryptfs_restart_internal(0);
2885 return 0;
Paul Lawrencef4faa572014-01-29 13:31:03 -08002886 } else {
Paul Lawrence84274cc2016-04-15 15:41:33 -07002887 SLOGE("Encrypted, default crypt type but can't decrypt");
Paul Lawrencef4faa572014-01-29 13:31:03 -08002888 }
2889
Paul Lawrence6bfed202014-07-28 12:47:22 -07002890 /** Corrupt. Allow us to boot into framework, which will detect bad
2891 crypto when it calls do_crypto_complete, then do a factory reset
Paul Lawrencef4faa572014-01-29 13:31:03 -08002892 */
Paul Lawrence6bfed202014-07-28 12:47:22 -07002893 property_set("vold.decrypt", "trigger_restart_min_framework");
Paul Lawrencef4faa572014-01-29 13:31:03 -08002894 return 0;
2895}
2896
2897/* Returns type of the password, default, pattern, pin or password.
2898 */
2899int cryptfs_get_password_type(void)
2900{
Paul Crowley38132a12016-02-09 09:50:32 +00002901 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002902 SLOGE("cryptfs_get_password_type not valid for file encryption");
2903 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08002904 }
2905
Paul Lawrencef4faa572014-01-29 13:31:03 -08002906 struct crypt_mnt_ftr crypt_ftr;
2907
2908 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2909 SLOGE("Error getting crypt footer and key\n");
2910 return -1;
2911 }
2912
Paul Lawrence6bfed202014-07-28 12:47:22 -07002913 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) {
2914 return -1;
2915 }
2916
Paul Lawrencef4faa572014-01-29 13:31:03 -08002917 return crypt_ftr.crypt_type;
2918}
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002919
Paul Lawrence05335c32015-03-05 09:46:23 -08002920const char* cryptfs_get_password()
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002921{
Paul Crowley38132a12016-02-09 09:50:32 +00002922 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002923 SLOGE("cryptfs_get_password not valid for file encryption");
2924 return 0;
Paul Lawrence05335c32015-03-05 09:46:23 -08002925 }
2926
Paul Lawrence399317e2014-03-10 13:20:50 -07002927 struct timespec now;
Paul Lawrenceef2b5be2014-11-11 12:47:03 -08002928 clock_gettime(CLOCK_BOOTTIME, &now);
Paul Lawrence399317e2014-03-10 13:20:50 -07002929 if (now.tv_sec < password_expiry_time) {
2930 return password;
2931 } else {
2932 cryptfs_clear_password();
2933 return 0;
2934 }
2935}
2936
2937void cryptfs_clear_password()
2938{
2939 if (password) {
2940 size_t len = strlen(password);
2941 memset(password, 0, len);
2942 free(password);
2943 password = 0;
2944 password_expiry_time = 0;
2945 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002946}
Paul Lawrence731a7a22015-04-28 22:14:15 +00002947
Paul Lawrence0c247462015-10-29 10:30:57 -07002948int cryptfs_isConvertibleToFBE()
2949{
Paul Crowleye2ee1522017-09-26 14:05:26 -07002950 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
Paul Lawrence0c247462015-10-29 10:30:57 -07002951 return fs_mgr_is_convertible_to_fbe(rec) ? 1 : 0;
2952}