OSDN Git Service

DO NOT MERGE - Mark RQ2A.210105.001 as merged.
[android-x86/system-vold.git] / cryptfs.cpp
1 /*
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 #define LOG_TAG "Cryptfs"
18
19 #include "cryptfs.h"
20
21 #include "Checkpoint.h"
22 #include "CryptoType.h"
23 #include "EncryptInplace.h"
24 #include "FsCrypt.h"
25 #include "Keymaster.h"
26 #include "Process.h"
27 #include "ScryptParameters.h"
28 #include "Utils.h"
29 #include "VoldUtil.h"
30 #include "VolumeManager.h"
31
32 #include <android-base/logging.h>
33 #include <android-base/parseint.h>
34 #include <android-base/properties.h>
35 #include <android-base/stringprintf.h>
36 #include <android-base/strings.h>
37 #include <bootloader_message/bootloader_message.h>
38 #include <cutils/android_reboot.h>
39 #include <cutils/properties.h>
40 #include <ext4_utils/ext4_utils.h>
41 #include <f2fs_sparseblock.h>
42 #include <fs_mgr.h>
43 #include <fscrypt/fscrypt.h>
44 #include <libdm/dm.h>
45 #include <log/log.h>
46 #include <logwrap/logwrap.h>
47 #include <openssl/evp.h>
48 #include <openssl/sha.h>
49 #include <selinux/selinux.h>
50 #include <wakelock/wakelock.h>
51
52 #include <ctype.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <inttypes.h>
56 #include <libgen.h>
57 #include <linux/kdev_t.h>
58 #include <math.h>
59 #include <mntent.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.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
71 #include <chrono>
72 #include <thread>
73
74 extern "C" {
75 #include <crypto_scrypt.h>
76 }
77
78 using android::base::ParseUint;
79 using android::base::StringPrintf;
80 using android::fs_mgr::GetEntryForMountPoint;
81 using android::vold::CryptoType;
82 using android::vold::KeyBuffer;
83 using android::vold::KeyGeneration;
84 using namespace android::vold;
85 using namespace android::dm;
86 using namespace std::chrono_literals;
87
88 /* The current cryptfs version */
89 #define CURRENT_MAJOR_VERSION 1
90 #define CURRENT_MINOR_VERSION 3
91
92 #define CRYPT_FOOTER_TO_PERSIST_OFFSET 0x1000
93 #define CRYPT_PERSIST_DATA_SIZE 0x1000
94
95 #define CRYPT_SECTOR_SIZE 512
96
97 #define MAX_CRYPTO_TYPE_NAME_LEN 64
98
99 #define MAX_KEY_LEN 48
100 #define SALT_LEN 16
101 #define SCRYPT_LEN 32
102
103 /* definitions of flags in the structure below */
104 #define CRYPT_MNT_KEY_UNENCRYPTED 0x1 /* The key for the partition is not encrypted. */
105 #define CRYPT_ENCRYPTION_IN_PROGRESS 0x2 /* no longer used */
106 #define CRYPT_INCONSISTENT_STATE                    \
107     0x4 /* Set when starting encryption, clear when \
108            exit cleanly, either through success or  \
109            correctly marked partial encryption */
110 #define CRYPT_DATA_CORRUPT                      \
111     0x8 /* Set when encryption is fine, but the \
112            underlying volume is corrupt */
113 #define CRYPT_FORCE_ENCRYPTION                        \
114     0x10 /* Set when it is time to encrypt this       \
115             volume on boot. Everything in this        \
116             structure is set up correctly as          \
117             though device is encrypted except         \
118             that the master key is encrypted with the \
119             default password. */
120 #define CRYPT_FORCE_COMPLETE                           \
121     0x20 /* Set when the above encryption cycle is     \
122             complete. On next cryptkeeper entry, match \
123             the password. If it matches fix the master \
124             key and remove this flag. */
125
126 /* Allowed values for type in the structure below */
127 #define CRYPT_TYPE_PASSWORD                       \
128     0 /* master_key is encrypted with a password  \
129        * Must be zero to be compatible with pre-L \
130        * devices where type is always password.*/
131 #define CRYPT_TYPE_DEFAULT                                            \
132     1                         /* master_key is encrypted with default \
133                                * password */
134 #define CRYPT_TYPE_PATTERN 2  /* master_key is encrypted with a pattern */
135 #define CRYPT_TYPE_PIN 3      /* master_key is encrypted with a pin */
136 #define CRYPT_TYPE_MAX_TYPE 3 /* type cannot be larger than this value */
137
138 #define CRYPT_MNT_MAGIC 0xD0B5B1C4
139 #define PERSIST_DATA_MAGIC 0xE950CD44
140
141 /* Key Derivation Function algorithms */
142 #define KDF_PBKDF2 1
143 #define KDF_SCRYPT 2
144 /* Algorithms 3 & 4 deprecated before shipping outside of google, so removed */
145 #define KDF_SCRYPT_KEYMASTER 5
146
147 /* Maximum allowed keymaster blob size. */
148 #define KEYMASTER_BLOB_SIZE 2048
149
150 /* __le32 and __le16 defined in system/extras/ext4_utils/ext4_utils.h */
151 #define __le8 unsigned char
152
153 #if !defined(SHA256_DIGEST_LENGTH)
154 #define SHA256_DIGEST_LENGTH 32
155 #endif
156
157 /* This structure starts 16,384 bytes before the end of a hardware
158  * partition that is encrypted, or in a separate partition.  It's location
159  * is specified by a property set in init.<device>.rc.
160  * The structure allocates 48 bytes for a key, but the real key size is
161  * specified in the struct.  Currently, the code is hardcoded to use 128
162  * bit keys.
163  * The fields after salt are only valid in rev 1.1 and later stuctures.
164  * Obviously, the filesystem does not include the last 16 kbytes
165  * of the partition if the crypt_mnt_ftr lives at the end of the
166  * partition.
167  */
168
169 struct crypt_mnt_ftr {
170     __le32 magic; /* See above */
171     __le16 major_version;
172     __le16 minor_version;
173     __le32 ftr_size;             /* in bytes, not including key following */
174     __le32 flags;                /* See above */
175     __le32 keysize;              /* in bytes */
176     __le32 crypt_type;           /* how master_key is encrypted. Must be a
177                                   * CRYPT_TYPE_XXX value */
178     __le64 fs_size;              /* Size of the encrypted fs, in 512 byte sectors */
179     __le32 failed_decrypt_count; /* count of # of failed attempts to decrypt and
180                                     mount, set to 0 on successful mount */
181     unsigned char crypto_type_name[MAX_CRYPTO_TYPE_NAME_LEN]; /* The type of encryption
182                                                                  needed to decrypt this
183                                                                  partition, null terminated */
184     __le32 spare2;                                            /* ignored */
185     unsigned char master_key[MAX_KEY_LEN]; /* The encrypted key for decrypting the filesystem */
186     unsigned char salt[SALT_LEN];          /* The salt used for this encryption */
187     __le64 persist_data_offset[2];         /* Absolute offset to both copies of crypt_persist_data
188                                             * on device with that info, either the footer of the
189                                             * real_blkdevice or the metadata partition. */
190
191     __le32 persist_data_size; /* The number of bytes allocated to each copy of the
192                                * persistent data table*/
193
194     __le8 kdf_type; /* The key derivation function used. */
195
196     /* scrypt parameters. See www.tarsnap.com/scrypt/scrypt.pdf */
197     __le8 N_factor;        /* (1 << N) */
198     __le8 r_factor;        /* (1 << r) */
199     __le8 p_factor;        /* (1 << p) */
200     __le64 encrypted_upto; /* no longer used */
201     __le8 hash_first_block[SHA256_DIGEST_LENGTH]; /* no longer used */
202
203     /* key_master key, used to sign the derived key which is then used to generate
204      * the intermediate key
205      * This key should be used for no other purposes! We use this key to sign unpadded
206      * data, which is acceptable but only if the key is not reused elsewhere. */
207     __le8 keymaster_blob[KEYMASTER_BLOB_SIZE];
208     __le32 keymaster_blob_size;
209
210     /* Store scrypt of salted intermediate key. When decryption fails, we can
211        check if this matches, and if it does, we know that the problem is with the
212        drive, and there is no point in asking the user for more passwords.
213
214        Note that if any part of this structure is corrupt, this will not match and
215        we will continue to believe the user entered the wrong password. In that
216        case the only solution is for the user to enter a password enough times to
217        force a wipe.
218
219        Note also that there is no need to worry about migration. If this data is
220        wrong, we simply won't recognise a right password, and will continue to
221        prompt. On the first password change, this value will be populated and
222        then we will be OK.
223      */
224     unsigned char scrypted_intermediate_key[SCRYPT_LEN];
225
226     /* sha of this structure with this element set to zero
227        Used when encrypting on reboot to validate structure before doing something
228        fatal
229      */
230     unsigned char sha256[SHA256_DIGEST_LENGTH];
231 };
232
233 /* Persistant data that should be available before decryption.
234  * Things like airplane mode, locale and timezone are kept
235  * here and can be retrieved by the CryptKeeper UI to properly
236  * configure the phone before asking for the password
237  * This is only valid if the major and minor version above
238  * is set to 1.1 or higher.
239  *
240  * This is a 4K structure.  There are 2 copies, and the code alternates
241  * writing one and then clearing the previous one.  The reading
242  * code reads the first valid copy it finds, based on the magic number.
243  * The absolute offset to the first of the two copies is kept in rev 1.1
244  * and higher crypt_mnt_ftr structures.
245  */
246 struct crypt_persist_entry {
247     char key[PROPERTY_KEY_MAX];
248     char val[PROPERTY_VALUE_MAX];
249 };
250
251 /* Should be exactly 4K in size */
252 struct crypt_persist_data {
253     __le32 persist_magic;
254     __le32 persist_valid_entries;
255     __le32 persist_spare[30];
256     struct crypt_persist_entry persist_entry[0];
257 };
258
259 static int wait_and_unmount(const char* mountpoint, bool kill);
260
261 typedef int (*kdf_func)(const char* passwd, const unsigned char* salt, unsigned char* ikey,
262                         void* params);
263
264 #define UNUSED __attribute__((unused))
265
266 #define HASH_COUNT 2000
267
268 constexpr size_t INTERMEDIATE_KEY_LEN_BYTES = 16;
269 constexpr size_t INTERMEDIATE_IV_LEN_BYTES = 16;
270 constexpr size_t INTERMEDIATE_BUF_SIZE = (INTERMEDIATE_KEY_LEN_BYTES + INTERMEDIATE_IV_LEN_BYTES);
271
272 // SCRYPT_LEN is used by struct crypt_mnt_ftr for its intermediate key.
273 static_assert(INTERMEDIATE_BUF_SIZE == SCRYPT_LEN, "Mismatch of intermediate key sizes");
274
275 #define KEY_IN_FOOTER "footer"
276
277 #define DEFAULT_PASSWORD "default_password"
278
279 #define CRYPTO_BLOCK_DEVICE "userdata"
280
281 #define BREADCRUMB_FILE "/data/misc/vold/convert_fde"
282
283 #define EXT4_FS 1
284 #define F2FS_FS 2
285
286 #define TABLE_LOAD_RETRIES 10
287
288 #define RSA_KEY_SIZE 2048
289 #define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8)
290 #define RSA_EXPONENT 0x10001
291 #define KEYMASTER_CRYPTFS_RATE_LIMIT 1  // Maximum one try per second
292
293 #define RETRY_MOUNT_ATTEMPTS 10
294 #define RETRY_MOUNT_DELAY_SECONDS 1
295
296 #define CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE (1)
297
298 static int put_crypt_ftr_and_key(struct crypt_mnt_ftr* crypt_ftr);
299
300 static unsigned char saved_master_key[MAX_KEY_LEN];
301 static char* saved_mount_point;
302 static int master_key_saved = 0;
303 static struct crypt_persist_data* persist_data = NULL;
304
305 constexpr CryptoType aes_128_cbc = CryptoType()
306                                            .set_config_name("AES-128-CBC")
307                                            .set_kernel_name("aes-cbc-essiv:sha256")
308                                            .set_keysize(16);
309
310 constexpr CryptoType supported_crypto_types[] = {aes_128_cbc, android::vold::adiantum};
311
312 static_assert(validateSupportedCryptoTypes(MAX_KEY_LEN, supported_crypto_types,
313                                            array_length(supported_crypto_types)),
314               "We have a CryptoType with keysize > MAX_KEY_LEN or which was "
315               "incompletely constructed.");
316
317 static const CryptoType& get_crypto_type() {
318     // We only want to parse this read-only property once.  But we need to wait
319     // until the system is initialized before we can read it.  So we use a static
320     // scoped within this function to get it only once.
321     static CryptoType crypto_type =
322             lookup_crypto_algorithm(supported_crypto_types, array_length(supported_crypto_types),
323                                     aes_128_cbc, "ro.crypto.fde_algorithm");
324     return crypto_type;
325 }
326
327 const KeyGeneration cryptfs_get_keygen() {
328     return KeyGeneration{get_crypto_type().get_keysize(), true, false};
329 }
330
331 static bool write_string_to_buf(const std::string& towrite, uint8_t* buffer, uint32_t buffer_size,
332                                 uint32_t* out_size) {
333     if (!buffer || !out_size) {
334         LOG(ERROR) << "Missing target pointers";
335         return false;
336     }
337     *out_size = towrite.size();
338     if (buffer_size < towrite.size()) {
339         LOG(ERROR) << "Buffer too small " << buffer_size << " < " << towrite.size();
340         return false;
341     }
342     memset(buffer, '\0', buffer_size);
343     std::copy(towrite.begin(), towrite.end(), buffer);
344     return true;
345 }
346
347 static int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
348                                                    uint32_t ratelimit, uint8_t* key_buffer,
349                                                    uint32_t key_buffer_size,
350                                                    uint32_t* key_out_size) {
351     if (key_out_size) {
352         *key_out_size = 0;
353     }
354     Keymaster dev;
355     if (!dev) {
356         LOG(ERROR) << "Failed to initiate keymaster session";
357         return -1;
358     }
359     auto keyParams = km::AuthorizationSetBuilder()
360                              .RsaSigningKey(rsa_key_size, rsa_exponent)
361                              .NoDigestOrPadding()
362                              .Authorization(km::TAG_NO_AUTH_REQUIRED)
363                              .Authorization(km::TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit);
364     std::string key;
365     if (!dev.generateKey(keyParams, &key)) return -1;
366     if (!write_string_to_buf(key, key_buffer, key_buffer_size, key_out_size)) return -1;
367     return 0;
368 }
369
370 /* Create a new keymaster key and store it in this footer */
371 static int keymaster_create_key(struct crypt_mnt_ftr* ftr) {
372     if (ftr->keymaster_blob_size) {
373         SLOGI("Already have key");
374         return 0;
375     }
376
377     int rc = keymaster_create_key_for_cryptfs_scrypt(
378         RSA_KEY_SIZE, RSA_EXPONENT, KEYMASTER_CRYPTFS_RATE_LIMIT, ftr->keymaster_blob,
379         KEYMASTER_BLOB_SIZE, &ftr->keymaster_blob_size);
380     if (rc) {
381         if (ftr->keymaster_blob_size > KEYMASTER_BLOB_SIZE) {
382             SLOGE("Keymaster key blob too large");
383             ftr->keymaster_blob_size = 0;
384         }
385         SLOGE("Failed to generate keypair");
386         return -1;
387     }
388     return 0;
389 }
390
391 static int keymaster_sign_object_for_cryptfs_scrypt(struct crypt_mnt_ftr* ftr, uint32_t ratelimit,
392                                                     const uint8_t* object, const size_t object_size,
393                                                     uint8_t** signature_buffer,
394                                                     size_t* signature_buffer_size) {
395     if (!object || !signature_buffer || !signature_buffer_size) {
396         LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
397         return -1;
398     }
399
400     Keymaster dev;
401     if (!dev) {
402         LOG(ERROR) << "Failed to initiate keymaster session";
403         return -1;
404     }
405
406     km::AuthorizationSet outParams;
407     std::string key(reinterpret_cast<const char*>(ftr->keymaster_blob), ftr->keymaster_blob_size);
408     std::string input(reinterpret_cast<const char*>(object), object_size);
409     std::string output;
410     KeymasterOperation op;
411
412     auto paramBuilder = km::AuthorizationSetBuilder().NoDigestOrPadding().Authorization(
413             km::TAG_PURPOSE, km::KeyPurpose::SIGN);
414     while (true) {
415         op = dev.begin(key, paramBuilder, &outParams);
416         if (op.getErrorCode() == km::ErrorCode::KEY_RATE_LIMIT_EXCEEDED) {
417             sleep(ratelimit);
418             continue;
419         } else
420             break;
421     }
422
423     if (!op) {
424         LOG(ERROR) << "Error starting keymaster signature transaction: "
425                    << int32_t(op.getErrorCode());
426         return -1;
427     }
428
429     if (op.getUpgradedBlob()) {
430         write_string_to_buf(*op.getUpgradedBlob(), ftr->keymaster_blob, KEYMASTER_BLOB_SIZE,
431                             &ftr->keymaster_blob_size);
432
433         SLOGD("Upgrading key");
434         if (put_crypt_ftr_and_key(ftr) != 0) {
435             SLOGE("Failed to write upgraded key to disk");
436             return -1;
437         }
438         SLOGD("Key upgraded successfully");
439     }
440
441     if (!op.updateCompletely(input, &output)) {
442         LOG(ERROR) << "Error sending data to keymaster signature transaction: "
443                    << int32_t(op.getErrorCode());
444         return -1;
445     }
446
447     if (!op.finish(&output)) {
448         LOG(ERROR) << "Error finalizing keymaster signature transaction: "
449                    << int32_t(op.getErrorCode());
450         return -1;
451     }
452
453     *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size()));
454     if (*signature_buffer == nullptr) {
455         LOG(ERROR) << "Error allocation buffer for keymaster signature";
456         return -1;
457     }
458     *signature_buffer_size = output.size();
459     std::copy(output.data(), output.data() + output.size(), *signature_buffer);
460
461     return 0;
462 }
463
464 /* This signs the given object using the keymaster key. */
465 static int keymaster_sign_object(struct crypt_mnt_ftr* ftr, const unsigned char* object,
466                                  const size_t object_size, unsigned char** signature,
467                                  size_t* signature_size) {
468     unsigned char to_sign[RSA_KEY_SIZE_BYTES];
469     size_t to_sign_size = sizeof(to_sign);
470     memset(to_sign, 0, RSA_KEY_SIZE_BYTES);
471
472     // To sign a message with RSA, the message must satisfy two
473     // constraints:
474     //
475     // 1. The message, when interpreted as a big-endian numeric value, must
476     //    be strictly less than the public modulus of the RSA key.  Note
477     //    that because the most significant bit of the public modulus is
478     //    guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit
479     //    key), an n-bit message with most significant bit 0 always
480     //    satisfies this requirement.
481     //
482     // 2. The message must have the same length in bits as the public
483     //    modulus of the RSA key.  This requirement isn't mathematically
484     //    necessary, but is necessary to ensure consistency in
485     //    implementations.
486     switch (ftr->kdf_type) {
487         case KDF_SCRYPT_KEYMASTER:
488             // This ensures the most significant byte of the signed message
489             // is zero.  We could have zero-padded to the left instead, but
490             // this approach is slightly more robust against changes in
491             // object size.  However, it's still broken (but not unusably
492             // so) because we really should be using a proper deterministic
493             // RSA padding function, such as PKCS1.
494             memcpy(to_sign + 1, object, std::min((size_t)RSA_KEY_SIZE_BYTES - 1, object_size));
495             SLOGI("Signing safely-padded object");
496             break;
497         default:
498             SLOGE("Unknown KDF type %d", ftr->kdf_type);
499             return -1;
500     }
501     return keymaster_sign_object_for_cryptfs_scrypt(ftr, KEYMASTER_CRYPTFS_RATE_LIMIT, to_sign,
502                                                     to_sign_size, signature, signature_size);
503 }
504
505 /* Store password when userdata is successfully decrypted and mounted.
506  * Cleared by cryptfs_clear_password
507  *
508  * To avoid a double prompt at boot, we need to store the CryptKeeper
509  * password and pass it to KeyGuard, which uses it to unlock KeyStore.
510  * Since the entire framework is torn down and rebuilt after encryption,
511  * we have to use a daemon or similar to store the password. Since vold
512  * is secured against IPC except from system processes, it seems a reasonable
513  * place to store this.
514  *
515  * password should be cleared once it has been used.
516  *
517  * password is aged out after password_max_age_seconds seconds.
518  */
519 static char* password = 0;
520 static int password_expiry_time = 0;
521 static const int password_max_age_seconds = 60;
522
523 enum class RebootType { reboot, recovery, shutdown };
524 static void cryptfs_reboot(RebootType rt) {
525     switch (rt) {
526         case RebootType::reboot:
527             property_set(ANDROID_RB_PROPERTY, "reboot");
528             break;
529
530         case RebootType::recovery:
531             property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
532             break;
533
534         case RebootType::shutdown:
535             property_set(ANDROID_RB_PROPERTY, "shutdown");
536             break;
537     }
538
539     sleep(20);
540
541     /* Shouldn't get here, reboot should happen before sleep times out */
542     return;
543 }
544
545 /**
546  * Gets the default device scrypt parameters for key derivation time tuning.
547  * The parameters should lead to about one second derivation time for the
548  * given device.
549  */
550 static void get_device_scrypt_params(struct crypt_mnt_ftr* ftr) {
551     char paramstr[PROPERTY_VALUE_MAX];
552     int Nf, rf, pf;
553
554     property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS);
555     if (!parse_scrypt_parameters(paramstr, &Nf, &rf, &pf)) {
556         SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
557         parse_scrypt_parameters(SCRYPT_DEFAULTS, &Nf, &rf, &pf);
558     }
559     ftr->N_factor = Nf;
560     ftr->r_factor = rf;
561     ftr->p_factor = pf;
562 }
563
564 static uint64_t get_fs_size(const char* dev) {
565     int fd, block_size;
566     struct ext4_super_block sb;
567     uint64_t len;
568
569     if ((fd = open(dev, O_RDONLY | O_CLOEXEC)) < 0) {
570         SLOGE("Cannot open device to get filesystem size ");
571         return 0;
572     }
573
574     if (lseek64(fd, 1024, SEEK_SET) < 0) {
575         SLOGE("Cannot seek to superblock");
576         return 0;
577     }
578
579     if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
580         SLOGE("Cannot read superblock");
581         return 0;
582     }
583
584     close(fd);
585
586     if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) {
587         SLOGE("Not a valid ext4 superblock");
588         return 0;
589     }
590     block_size = 1024 << sb.s_log_block_size;
591     /* compute length in bytes */
592     len = (((uint64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
593
594     /* return length in sectors */
595     return len / 512;
596 }
597
598 static void get_crypt_info(std::string* key_loc, std::string* real_blk_device) {
599     for (const auto& entry : fstab_default) {
600         if (!entry.fs_mgr_flags.vold_managed &&
601             (entry.fs_mgr_flags.crypt || entry.fs_mgr_flags.force_crypt ||
602              entry.fs_mgr_flags.force_fde_or_fbe || entry.fs_mgr_flags.file_encryption)) {
603             if (key_loc != nullptr) {
604                 *key_loc = entry.key_loc;
605             }
606             if (real_blk_device != nullptr) {
607                 *real_blk_device = entry.blk_device;
608             }
609             return;
610         }
611     }
612 }
613
614 static int get_crypt_ftr_info(char** metadata_fname, off64_t* off) {
615     static int cached_data = 0;
616     static uint64_t cached_off = 0;
617     static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
618     char key_loc[PROPERTY_VALUE_MAX];
619     char real_blkdev[PROPERTY_VALUE_MAX];
620     int rc = -1;
621
622     if (!cached_data) {
623         std::string key_loc;
624         std::string real_blkdev;
625         get_crypt_info(&key_loc, &real_blkdev);
626
627         if (key_loc == KEY_IN_FOOTER) {
628             if (android::vold::GetBlockDevSize(real_blkdev, &cached_off) == android::OK) {
629                 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
630                  * encryption info footer and key, and plenty of bytes to spare for future
631                  * growth.
632                  */
633                 strlcpy(cached_metadata_fname, real_blkdev.c_str(), sizeof(cached_metadata_fname));
634                 cached_off -= CRYPT_FOOTER_OFFSET;
635                 cached_data = 1;
636             } else {
637                 SLOGE("Cannot get size of block device %s\n", real_blkdev.c_str());
638             }
639         } else {
640             strlcpy(cached_metadata_fname, key_loc.c_str(), sizeof(cached_metadata_fname));
641             cached_off = 0;
642             cached_data = 1;
643         }
644     }
645
646     if (cached_data) {
647         if (metadata_fname) {
648             *metadata_fname = cached_metadata_fname;
649         }
650         if (off) {
651             *off = cached_off;
652         }
653         rc = 0;
654     }
655
656     return rc;
657 }
658
659 /* Set sha256 checksum in structure */
660 static void set_ftr_sha(struct crypt_mnt_ftr* crypt_ftr) {
661     SHA256_CTX c;
662     SHA256_Init(&c);
663     memset(crypt_ftr->sha256, 0, sizeof(crypt_ftr->sha256));
664     SHA256_Update(&c, crypt_ftr, sizeof(*crypt_ftr));
665     SHA256_Final(crypt_ftr->sha256, &c);
666 }
667
668 /* key or salt can be NULL, in which case just skip writing that value.  Useful to
669  * update the failed mount count but not change the key.
670  */
671 static int put_crypt_ftr_and_key(struct crypt_mnt_ftr* crypt_ftr) {
672     int fd;
673     unsigned int cnt;
674     /* starting_off is set to the SEEK_SET offset
675      * where the crypto structure starts
676      */
677     off64_t starting_off;
678     int rc = -1;
679     char* fname = NULL;
680     struct stat statbuf;
681
682     set_ftr_sha(crypt_ftr);
683
684     if (get_crypt_ftr_info(&fname, &starting_off)) {
685         SLOGE("Unable to get crypt_ftr_info\n");
686         return -1;
687     }
688     if (fname[0] != '/') {
689         SLOGE("Unexpected value for crypto key location\n");
690         return -1;
691     }
692     if ((fd = open(fname, O_RDWR | O_CREAT | O_CLOEXEC, 0600)) < 0) {
693         SLOGE("Cannot open footer file %s for put\n", fname);
694         return -1;
695     }
696
697     /* Seek to the start of the crypt footer */
698     if (lseek64(fd, starting_off, SEEK_SET) == -1) {
699         SLOGE("Cannot seek to real block device footer\n");
700         goto errout;
701     }
702
703     if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
704         SLOGE("Cannot write real block device footer\n");
705         goto errout;
706     }
707
708     fstat(fd, &statbuf);
709     /* If the keys are kept on a raw block device, do not try to truncate it. */
710     if (S_ISREG(statbuf.st_mode)) {
711         if (ftruncate(fd, 0x4000)) {
712             SLOGE("Cannot set footer file size\n");
713             goto errout;
714         }
715     }
716
717     /* Success! */
718     rc = 0;
719
720 errout:
721     close(fd);
722     return rc;
723 }
724
725 static bool check_ftr_sha(const struct crypt_mnt_ftr* crypt_ftr) {
726     struct crypt_mnt_ftr copy;
727     memcpy(&copy, crypt_ftr, sizeof(copy));
728     set_ftr_sha(&copy);
729     return memcmp(copy.sha256, crypt_ftr->sha256, sizeof(copy.sha256)) == 0;
730 }
731
732 static inline int unix_read(int fd, void* buff, int len) {
733     return TEMP_FAILURE_RETRY(read(fd, buff, len));
734 }
735
736 static inline int unix_write(int fd, const void* buff, int len) {
737     return TEMP_FAILURE_RETRY(write(fd, buff, len));
738 }
739
740 static void init_empty_persist_data(struct crypt_persist_data* pdata, int len) {
741     memset(pdata, 0, len);
742     pdata->persist_magic = PERSIST_DATA_MAGIC;
743     pdata->persist_valid_entries = 0;
744 }
745
746 /* A routine to update the passed in crypt_ftr to the lastest version.
747  * fd is open read/write on the device that holds the crypto footer and persistent
748  * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
749  * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
750  */
751 static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr* crypt_ftr, off64_t offset) {
752     int orig_major = crypt_ftr->major_version;
753     int orig_minor = crypt_ftr->minor_version;
754
755     if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
756         struct crypt_persist_data* pdata;
757         off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
758
759         SLOGW("upgrading crypto footer to 1.1");
760
761         pdata = (crypt_persist_data*)malloc(CRYPT_PERSIST_DATA_SIZE);
762         if (pdata == NULL) {
763             SLOGE("Cannot allocate persisent data\n");
764             return;
765         }
766         memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
767
768         /* Need to initialize the persistent data area */
769         if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
770             SLOGE("Cannot seek to persisent data offset\n");
771             free(pdata);
772             return;
773         }
774         /* Write all zeros to the first copy, making it invalid */
775         unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
776
777         /* Write a valid but empty structure to the second copy */
778         init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
779         unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
780
781         /* Update the footer */
782         crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
783         crypt_ftr->persist_data_offset[0] = pdata_offset;
784         crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
785         crypt_ftr->minor_version = 1;
786         free(pdata);
787     }
788
789     if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
790         SLOGW("upgrading crypto footer to 1.2");
791         /* But keep the old kdf_type.
792          * It will get updated later to KDF_SCRYPT after the password has been verified.
793          */
794         crypt_ftr->kdf_type = KDF_PBKDF2;
795         get_device_scrypt_params(crypt_ftr);
796         crypt_ftr->minor_version = 2;
797     }
798
799     if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
800         SLOGW("upgrading crypto footer to 1.3");
801         crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
802         crypt_ftr->minor_version = 3;
803     }
804
805     if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
806         if (lseek64(fd, offset, SEEK_SET) == -1) {
807             SLOGE("Cannot seek to crypt footer\n");
808             return;
809         }
810         unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
811     }
812 }
813
814 static int get_crypt_ftr_and_key(struct crypt_mnt_ftr* crypt_ftr) {
815     int fd;
816     unsigned int cnt;
817     off64_t starting_off;
818     int rc = -1;
819     char* fname = NULL;
820     struct stat statbuf;
821
822     if (get_crypt_ftr_info(&fname, &starting_off)) {
823         SLOGE("Unable to get crypt_ftr_info\n");
824         return -1;
825     }
826     if (fname[0] != '/') {
827         SLOGE("Unexpected value for crypto key location\n");
828         return -1;
829     }
830     if ((fd = open(fname, O_RDWR | O_CLOEXEC)) < 0) {
831         SLOGE("Cannot open footer file %s for get\n", fname);
832         return -1;
833     }
834
835     /* Make sure it's 16 Kbytes in length */
836     fstat(fd, &statbuf);
837     if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
838         SLOGE("footer file %s is not the expected size!\n", fname);
839         goto errout;
840     }
841
842     /* Seek to the start of the crypt footer */
843     if (lseek64(fd, starting_off, SEEK_SET) == -1) {
844         SLOGE("Cannot seek to real block device footer\n");
845         goto errout;
846     }
847
848     if ((cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
849         SLOGE("Cannot read real block device footer\n");
850         goto errout;
851     }
852
853     if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
854         SLOGE("Bad magic for real block device %s\n", fname);
855         goto errout;
856     }
857
858     if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
859         SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
860               crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
861         goto errout;
862     }
863
864     // We risk buffer overflows with oversized keys, so we just reject them.
865     // 0-sized keys are problematic (essentially by-passing encryption), and
866     // AES-CBC key wrapping only works for multiples of 16 bytes.
867     if ((crypt_ftr->keysize == 0) || ((crypt_ftr->keysize % 16) != 0) ||
868         (crypt_ftr->keysize > MAX_KEY_LEN)) {
869         SLOGE(
870             "Invalid keysize (%u) for block device %s; Must be non-zero, "
871             "divisible by 16, and <= %d\n",
872             crypt_ftr->keysize, fname, MAX_KEY_LEN);
873         goto errout;
874     }
875
876     if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
877         SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
878               crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
879     }
880
881     /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
882      * copy on disk before returning.
883      */
884     if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
885         upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
886     }
887
888     /* Success! */
889     rc = 0;
890
891 errout:
892     close(fd);
893     return rc;
894 }
895
896 static int validate_persistent_data_storage(struct crypt_mnt_ftr* crypt_ftr) {
897     if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
898         crypt_ftr->persist_data_offset[1]) {
899         SLOGE("Crypt_ftr persist data regions overlap");
900         return -1;
901     }
902
903     if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
904         SLOGE("Crypt_ftr persist data region 0 starts after region 1");
905         return -1;
906     }
907
908     if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
909          (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
910         CRYPT_FOOTER_OFFSET) {
911         SLOGE("Persistent data extends past crypto footer");
912         return -1;
913     }
914
915     return 0;
916 }
917
918 static int load_persistent_data(void) {
919     struct crypt_mnt_ftr crypt_ftr;
920     struct crypt_persist_data* pdata = NULL;
921     char encrypted_state[PROPERTY_VALUE_MAX];
922     char* fname;
923     int found = 0;
924     int fd;
925     int ret;
926     int i;
927
928     if (persist_data) {
929         /* Nothing to do, we've already loaded or initialized it */
930         return 0;
931     }
932
933     /* If not encrypted, just allocate an empty table and initialize it */
934     property_get("ro.crypto.state", encrypted_state, "");
935     if (strcmp(encrypted_state, "encrypted")) {
936         pdata = (crypt_persist_data*)malloc(CRYPT_PERSIST_DATA_SIZE);
937         if (pdata) {
938             init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
939             persist_data = pdata;
940             return 0;
941         }
942         return -1;
943     }
944
945     if (get_crypt_ftr_and_key(&crypt_ftr)) {
946         return -1;
947     }
948
949     if ((crypt_ftr.major_version < 1) ||
950         (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
951         SLOGE("Crypt_ftr version doesn't support persistent data");
952         return -1;
953     }
954
955     if (get_crypt_ftr_info(&fname, NULL)) {
956         return -1;
957     }
958
959     ret = validate_persistent_data_storage(&crypt_ftr);
960     if (ret) {
961         return -1;
962     }
963
964     fd = open(fname, O_RDONLY | O_CLOEXEC);
965     if (fd < 0) {
966         SLOGE("Cannot open %s metadata file", fname);
967         return -1;
968     }
969
970     pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
971     if (pdata == NULL) {
972         SLOGE("Cannot allocate memory for persistent data");
973         goto err;
974     }
975
976     for (i = 0; i < 2; i++) {
977         if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
978             SLOGE("Cannot seek to read persistent data on %s", fname);
979             goto err2;
980         }
981         if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
982             SLOGE("Error reading persistent data on iteration %d", i);
983             goto err2;
984         }
985         if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
986             found = 1;
987             break;
988         }
989     }
990
991     if (!found) {
992         SLOGI("Could not find valid persistent data, creating");
993         init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
994     }
995
996     /* Success */
997     persist_data = pdata;
998     close(fd);
999     return 0;
1000
1001 err2:
1002     free(pdata);
1003
1004 err:
1005     close(fd);
1006     return -1;
1007 }
1008
1009 static int save_persistent_data(void) {
1010     struct crypt_mnt_ftr crypt_ftr;
1011     struct crypt_persist_data* pdata;
1012     char* fname;
1013     off64_t write_offset;
1014     off64_t erase_offset;
1015     int fd;
1016     int ret;
1017
1018     if (persist_data == NULL) {
1019         SLOGE("No persistent data to save");
1020         return -1;
1021     }
1022
1023     if (get_crypt_ftr_and_key(&crypt_ftr)) {
1024         return -1;
1025     }
1026
1027     if ((crypt_ftr.major_version < 1) ||
1028         (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
1029         SLOGE("Crypt_ftr version doesn't support persistent data");
1030         return -1;
1031     }
1032
1033     ret = validate_persistent_data_storage(&crypt_ftr);
1034     if (ret) {
1035         return -1;
1036     }
1037
1038     if (get_crypt_ftr_info(&fname, NULL)) {
1039         return -1;
1040     }
1041
1042     fd = open(fname, O_RDWR | O_CLOEXEC);
1043     if (fd < 0) {
1044         SLOGE("Cannot open %s metadata file", fname);
1045         return -1;
1046     }
1047
1048     pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
1049     if (pdata == NULL) {
1050         SLOGE("Cannot allocate persistant data");
1051         goto err;
1052     }
1053
1054     if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
1055         SLOGE("Cannot seek to read persistent data on %s", fname);
1056         goto err2;
1057     }
1058
1059     if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
1060         SLOGE("Error reading persistent data before save");
1061         goto err2;
1062     }
1063
1064     if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
1065         /* The first copy is the curent valid copy, so write to
1066          * the second copy and erase this one */
1067         write_offset = crypt_ftr.persist_data_offset[1];
1068         erase_offset = crypt_ftr.persist_data_offset[0];
1069     } else {
1070         /* The second copy must be the valid copy, so write to
1071          * the first copy, and erase the second */
1072         write_offset = crypt_ftr.persist_data_offset[0];
1073         erase_offset = crypt_ftr.persist_data_offset[1];
1074     }
1075
1076     /* Write the new copy first, if successful, then erase the old copy */
1077     if (lseek64(fd, write_offset, SEEK_SET) < 0) {
1078         SLOGE("Cannot seek to write persistent data");
1079         goto err2;
1080     }
1081     if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
1082         (int)crypt_ftr.persist_data_size) {
1083         if (lseek64(fd, erase_offset, SEEK_SET) < 0) {
1084             SLOGE("Cannot seek to erase previous persistent data");
1085             goto err2;
1086         }
1087         fsync(fd);
1088         memset(pdata, 0, crypt_ftr.persist_data_size);
1089         if (unix_write(fd, pdata, crypt_ftr.persist_data_size) != (int)crypt_ftr.persist_data_size) {
1090             SLOGE("Cannot write to erase previous persistent data");
1091             goto err2;
1092         }
1093         fsync(fd);
1094     } else {
1095         SLOGE("Cannot write to save persistent data");
1096         goto err2;
1097     }
1098
1099     /* Success */
1100     free(pdata);
1101     close(fd);
1102     return 0;
1103
1104 err2:
1105     free(pdata);
1106 err:
1107     close(fd);
1108     return -1;
1109 }
1110
1111 /* Convert a binary key of specified length into an ascii hex string equivalent,
1112  * without the leading 0x and with null termination
1113  */
1114 static void convert_key_to_hex_ascii(const unsigned char* master_key, unsigned int keysize,
1115                                      char* master_key_ascii) {
1116     unsigned int i, a;
1117     unsigned char nibble;
1118
1119     for (i = 0, a = 0; i < keysize; i++, a += 2) {
1120         /* For each byte, write out two ascii hex digits */
1121         nibble = (master_key[i] >> 4) & 0xf;
1122         master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
1123
1124         nibble = master_key[i] & 0xf;
1125         master_key_ascii[a + 1] = nibble + (nibble > 9 ? 0x37 : 0x30);
1126     }
1127
1128     /* Add the null termination */
1129     master_key_ascii[a] = '\0';
1130 }
1131
1132 /*
1133  * If the ro.crypto.fde_sector_size system property is set, append the
1134  * parameters to make dm-crypt use the specified crypto sector size and round
1135  * the crypto device size down to a crypto sector boundary.
1136  */
1137 static int add_sector_size_param(DmTargetCrypt* target, struct crypt_mnt_ftr* ftr) {
1138     constexpr char DM_CRYPT_SECTOR_SIZE[] = "ro.crypto.fde_sector_size";
1139     char value[PROPERTY_VALUE_MAX];
1140
1141     if (property_get(DM_CRYPT_SECTOR_SIZE, value, "") > 0) {
1142         unsigned int sector_size;
1143
1144         if (!ParseUint(value, &sector_size) || sector_size < 512 || sector_size > 4096 ||
1145             (sector_size & (sector_size - 1)) != 0) {
1146             SLOGE("Invalid value for %s: %s.  Must be >= 512, <= 4096, and a power of 2\n",
1147                   DM_CRYPT_SECTOR_SIZE, value);
1148             return -1;
1149         }
1150
1151         target->SetSectorSize(sector_size);
1152
1153         // With this option, IVs will match the sector numbering, instead
1154         // of being hard-coded to being based on 512-byte sectors.
1155         target->SetIvLargeSectors();
1156
1157         // Round the crypto device size down to a crypto sector boundary.
1158         ftr->fs_size &= ~((sector_size / 512) - 1);
1159     }
1160     return 0;
1161 }
1162
1163 static int create_crypto_blk_dev(struct crypt_mnt_ftr* crypt_ftr, const unsigned char* master_key,
1164                                  const char* real_blk_name, std::string* crypto_blk_name,
1165                                  const char* name, uint32_t flags) {
1166     auto& dm = DeviceMapper::Instance();
1167
1168     // We need two ASCII characters to represent each byte, and need space for
1169     // the '\0' terminator.
1170     char master_key_ascii[MAX_KEY_LEN * 2 + 1];
1171     convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
1172
1173     auto target = std::make_unique<DmTargetCrypt>(0, crypt_ftr->fs_size,
1174                                                   (const char*)crypt_ftr->crypto_type_name,
1175                                                   master_key_ascii, 0, real_blk_name, 0);
1176     target->AllowDiscards();
1177
1178     if (flags & CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE) {
1179         target->AllowEncryptOverride();
1180     }
1181     if (add_sector_size_param(target.get(), crypt_ftr)) {
1182         SLOGE("Error processing dm-crypt sector size param\n");
1183         return -1;
1184     }
1185
1186     DmTable table;
1187     table.AddTarget(std::move(target));
1188
1189     int load_count = 1;
1190     while (load_count < TABLE_LOAD_RETRIES) {
1191         if (dm.CreateDevice(name, table)) {
1192             break;
1193         }
1194         load_count++;
1195     }
1196
1197     if (load_count >= TABLE_LOAD_RETRIES) {
1198         SLOGE("Cannot load dm-crypt mapping table.\n");
1199         return -1;
1200     }
1201     if (load_count > 1) {
1202         SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
1203     }
1204
1205     if (!dm.GetDmDevicePathByName(name, crypto_blk_name)) {
1206         SLOGE("Cannot determine dm-crypt path for %s.\n", name);
1207         return -1;
1208     }
1209
1210     /* Ensure the dm device has been created before returning. */
1211     if (android::vold::WaitForFile(crypto_blk_name->c_str(), 1s) < 0) {
1212         // WaitForFile generates a suitable log message
1213         return -1;
1214     }
1215     return 0;
1216 }
1217
1218 static int delete_crypto_blk_dev(const std::string& name) {
1219     bool ret;
1220     auto& dm = DeviceMapper::Instance();
1221     // TODO(b/149396179) there appears to be a race somewhere in the system where trying
1222     // to delete the device fails with EBUSY; for now, work around this by retrying.
1223     int tries = 5;
1224     while (tries-- > 0) {
1225         ret = dm.DeleteDevice(name);
1226         if (ret || errno != EBUSY) {
1227             break;
1228         }
1229         SLOGW("DM_DEV Cannot remove dm-crypt device %s: %s, retrying...\n", name.c_str(),
1230               strerror(errno));
1231         std::this_thread::sleep_for(std::chrono::milliseconds(100));
1232     }
1233     if (!ret) {
1234         SLOGE("DM_DEV Cannot remove dm-crypt device %s: %s\n", name.c_str(), strerror(errno));
1235         return -1;
1236     }
1237     return 0;
1238 }
1239
1240 static int pbkdf2(const char* passwd, const unsigned char* salt, unsigned char* ikey,
1241                   void* params UNUSED) {
1242     SLOGI("Using pbkdf2 for cryptfs KDF");
1243
1244     /* Turn the password into a key and IV that can decrypt the master key */
1245     return PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN, HASH_COUNT,
1246                                   INTERMEDIATE_BUF_SIZE, ikey) != 1;
1247 }
1248
1249 static int scrypt(const char* passwd, const unsigned char* salt, unsigned char* ikey, void* params) {
1250     SLOGI("Using scrypt for cryptfs KDF");
1251
1252     struct crypt_mnt_ftr* ftr = (struct crypt_mnt_ftr*)params;
1253
1254     int N = 1 << ftr->N_factor;
1255     int r = 1 << ftr->r_factor;
1256     int p = 1 << ftr->p_factor;
1257
1258     /* Turn the password into a key and IV that can decrypt the master key */
1259     crypto_scrypt((const uint8_t*)passwd, strlen(passwd), salt, SALT_LEN, N, r, p, ikey,
1260                   INTERMEDIATE_BUF_SIZE);
1261
1262     return 0;
1263 }
1264
1265 static int scrypt_keymaster(const char* passwd, const unsigned char* salt, unsigned char* ikey,
1266                             void* params) {
1267     SLOGI("Using scrypt with keymaster for cryptfs KDF");
1268
1269     int rc;
1270     size_t signature_size;
1271     unsigned char* signature;
1272     struct crypt_mnt_ftr* ftr = (struct crypt_mnt_ftr*)params;
1273
1274     int N = 1 << ftr->N_factor;
1275     int r = 1 << ftr->r_factor;
1276     int p = 1 << ftr->p_factor;
1277
1278     rc = crypto_scrypt((const uint8_t*)passwd, strlen(passwd), salt, SALT_LEN, N, r, p, ikey,
1279                        INTERMEDIATE_BUF_SIZE);
1280
1281     if (rc) {
1282         SLOGE("scrypt failed");
1283         return -1;
1284     }
1285
1286     if (keymaster_sign_object(ftr, ikey, INTERMEDIATE_BUF_SIZE, &signature, &signature_size)) {
1287         SLOGE("Signing failed");
1288         return -1;
1289     }
1290
1291     rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN, N, r, p, ikey,
1292                        INTERMEDIATE_BUF_SIZE);
1293     free(signature);
1294
1295     if (rc) {
1296         SLOGE("scrypt failed");
1297         return -1;
1298     }
1299
1300     return 0;
1301 }
1302
1303 static int encrypt_master_key(const char* passwd, const unsigned char* salt,
1304                               const unsigned char* decrypted_master_key,
1305                               unsigned char* encrypted_master_key, struct crypt_mnt_ftr* crypt_ftr) {
1306     unsigned char ikey[INTERMEDIATE_BUF_SIZE] = {0};
1307     EVP_CIPHER_CTX e_ctx;
1308     int encrypted_len, final_len;
1309     int rc = 0;
1310
1311     /* Turn the password into an intermediate key and IV that can decrypt the master key */
1312     get_device_scrypt_params(crypt_ftr);
1313
1314     switch (crypt_ftr->kdf_type) {
1315         case KDF_SCRYPT_KEYMASTER:
1316             if (keymaster_create_key(crypt_ftr)) {
1317                 SLOGE("keymaster_create_key failed");
1318                 return -1;
1319             }
1320
1321             if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) {
1322                 SLOGE("scrypt failed");
1323                 return -1;
1324             }
1325             break;
1326
1327         case KDF_SCRYPT:
1328             if (scrypt(passwd, salt, ikey, crypt_ftr)) {
1329                 SLOGE("scrypt failed");
1330                 return -1;
1331             }
1332             break;
1333
1334         default:
1335             SLOGE("Invalid kdf_type");
1336             return -1;
1337     }
1338
1339     /* Initialize the decryption engine */
1340     EVP_CIPHER_CTX_init(&e_ctx);
1341     if (!EVP_EncryptInit_ex(&e_ctx, EVP_aes_128_cbc(), NULL, ikey,
1342                             ikey + INTERMEDIATE_KEY_LEN_BYTES)) {
1343         SLOGE("EVP_EncryptInit failed\n");
1344         return -1;
1345     }
1346     EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
1347
1348     /* Encrypt the master key */
1349     if (!EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len, decrypted_master_key,
1350                            crypt_ftr->keysize)) {
1351         SLOGE("EVP_EncryptUpdate failed\n");
1352         return -1;
1353     }
1354     if (!EVP_EncryptFinal_ex(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
1355         SLOGE("EVP_EncryptFinal failed\n");
1356         return -1;
1357     }
1358
1359     if (encrypted_len + final_len != static_cast<int>(crypt_ftr->keysize)) {
1360         SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
1361         return -1;
1362     }
1363
1364     /* Store the scrypt of the intermediate key, so we can validate if it's a
1365        password error or mount error when things go wrong.
1366        Note there's no need to check for errors, since if this is incorrect, we
1367        simply won't wipe userdata, which is the correct default behavior
1368     */
1369     int N = 1 << crypt_ftr->N_factor;
1370     int r = 1 << crypt_ftr->r_factor;
1371     int p = 1 << crypt_ftr->p_factor;
1372
1373     rc = crypto_scrypt(ikey, INTERMEDIATE_KEY_LEN_BYTES, crypt_ftr->salt, sizeof(crypt_ftr->salt),
1374                        N, r, p, crypt_ftr->scrypted_intermediate_key,
1375                        sizeof(crypt_ftr->scrypted_intermediate_key));
1376
1377     if (rc) {
1378         SLOGE("encrypt_master_key: crypto_scrypt failed");
1379     }
1380
1381     EVP_CIPHER_CTX_cleanup(&e_ctx);
1382
1383     return 0;
1384 }
1385
1386 static int decrypt_master_key_aux(const char* passwd, unsigned char* salt,
1387                                   const unsigned char* encrypted_master_key, size_t keysize,
1388                                   unsigned char* decrypted_master_key, kdf_func kdf,
1389                                   void* kdf_params, unsigned char** intermediate_key,
1390                                   size_t* intermediate_key_size) {
1391     unsigned char ikey[INTERMEDIATE_BUF_SIZE] = {0};
1392     EVP_CIPHER_CTX d_ctx;
1393     int decrypted_len, final_len;
1394
1395     /* Turn the password into an intermediate key and IV that can decrypt the
1396        master key */
1397     if (kdf(passwd, salt, ikey, kdf_params)) {
1398         SLOGE("kdf failed");
1399         return -1;
1400     }
1401
1402     /* Initialize the decryption engine */
1403     EVP_CIPHER_CTX_init(&d_ctx);
1404     if (!EVP_DecryptInit_ex(&d_ctx, EVP_aes_128_cbc(), NULL, ikey,
1405                             ikey + INTERMEDIATE_KEY_LEN_BYTES)) {
1406         return -1;
1407     }
1408     EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1409     /* Decrypt the master key */
1410     if (!EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len, encrypted_master_key,
1411                            keysize)) {
1412         return -1;
1413     }
1414     if (!EVP_DecryptFinal_ex(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
1415         return -1;
1416     }
1417
1418     if (decrypted_len + final_len != static_cast<int>(keysize)) {
1419         return -1;
1420     }
1421
1422     /* Copy intermediate key if needed by params */
1423     if (intermediate_key && intermediate_key_size) {
1424         *intermediate_key = (unsigned char*)malloc(INTERMEDIATE_KEY_LEN_BYTES);
1425         if (*intermediate_key) {
1426             memcpy(*intermediate_key, ikey, INTERMEDIATE_KEY_LEN_BYTES);
1427             *intermediate_key_size = INTERMEDIATE_KEY_LEN_BYTES;
1428         }
1429     }
1430
1431     EVP_CIPHER_CTX_cleanup(&d_ctx);
1432
1433     return 0;
1434 }
1435
1436 static void get_kdf_func(struct crypt_mnt_ftr* ftr, kdf_func* kdf, void** kdf_params) {
1437     if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
1438         *kdf = scrypt_keymaster;
1439         *kdf_params = ftr;
1440     } else if (ftr->kdf_type == KDF_SCRYPT) {
1441         *kdf = scrypt;
1442         *kdf_params = ftr;
1443     } else {
1444         *kdf = pbkdf2;
1445         *kdf_params = NULL;
1446     }
1447 }
1448
1449 static int decrypt_master_key(const char* passwd, unsigned char* decrypted_master_key,
1450                               struct crypt_mnt_ftr* crypt_ftr, unsigned char** intermediate_key,
1451                               size_t* intermediate_key_size) {
1452     kdf_func kdf;
1453     void* kdf_params;
1454     int ret;
1455
1456     get_kdf_func(crypt_ftr, &kdf, &kdf_params);
1457     ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key, crypt_ftr->keysize,
1458                                  decrypted_master_key, kdf, kdf_params, intermediate_key,
1459                                  intermediate_key_size);
1460     if (ret != 0) {
1461         SLOGW("failure decrypting master key");
1462     }
1463
1464     return ret;
1465 }
1466
1467 static int create_encrypted_random_key(const char* passwd, unsigned char* master_key,
1468                                        unsigned char* salt, struct crypt_mnt_ftr* crypt_ftr) {
1469     unsigned char key_buf[MAX_KEY_LEN];
1470
1471     /* Get some random bits for a key and salt */
1472     if (android::vold::ReadRandomBytes(sizeof(key_buf), reinterpret_cast<char*>(key_buf)) != 0) {
1473         return -1;
1474     }
1475     if (android::vold::ReadRandomBytes(SALT_LEN, reinterpret_cast<char*>(salt)) != 0) {
1476         return -1;
1477     }
1478
1479     /* Now encrypt it with the password */
1480     return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
1481 }
1482
1483 static void ensure_subdirectory_unmounted(const char *prefix) {
1484     std::vector<std::string> umount_points;
1485     std::unique_ptr<FILE, int (*)(FILE*)> mnts(setmntent("/proc/mounts", "r"), endmntent);
1486     if (!mnts) {
1487         SLOGW("could not read mount files");
1488         return;
1489     }
1490
1491     //Find sudirectory mount point
1492     mntent* mentry;
1493     std::string top_directory(prefix);
1494     if (!android::base::EndsWith(prefix, "/")) {
1495         top_directory = top_directory + "/";
1496     }
1497     while ((mentry = getmntent(mnts.get())) != nullptr) {
1498         if (strcmp(mentry->mnt_dir, top_directory.c_str()) == 0) {
1499             continue;
1500         }
1501
1502         if (android::base::StartsWith(mentry->mnt_dir, top_directory)) {
1503             SLOGW("found sub-directory mount %s - %s\n", prefix, mentry->mnt_dir);
1504             umount_points.push_back(mentry->mnt_dir);
1505         }
1506     }
1507
1508     //Sort by path length to umount longest path first
1509     std::sort(std::begin(umount_points), std::end(umount_points),
1510         [](const std::string& s1, const std::string& s2) {return s1.length() > s2.length(); });
1511
1512     for (std::string& mount_point : umount_points) {
1513         umount(mount_point.c_str());
1514         SLOGW("umount sub-directory mount %s\n", mount_point.c_str());
1515     }
1516 }
1517
1518 static int wait_and_unmount(const char* mountpoint, bool kill) {
1519     int i, err, rc;
1520
1521     // Subdirectory mount will cause a failure of umount.
1522     ensure_subdirectory_unmounted(mountpoint);
1523 #define WAIT_UNMOUNT_COUNT 20
1524
1525     /*  Now umount the tmpfs filesystem */
1526     for (i = 0; i < WAIT_UNMOUNT_COUNT; i++) {
1527         if (umount(mountpoint) == 0) {
1528             break;
1529         }
1530
1531         if (errno == EINVAL) {
1532             /* EINVAL is returned if the directory is not a mountpoint,
1533              * i.e. there is no filesystem mounted there.  So just get out.
1534              */
1535             break;
1536         }
1537
1538         err = errno;
1539
1540         /* If allowed, be increasingly aggressive before the last two retries */
1541         if (kill) {
1542             if (i == (WAIT_UNMOUNT_COUNT - 3)) {
1543                 SLOGW("sending SIGHUP to processes with open files\n");
1544                 android::vold::KillProcessesWithOpenFiles(mountpoint, SIGTERM);
1545             } else if (i == (WAIT_UNMOUNT_COUNT - 2)) {
1546                 SLOGW("sending SIGKILL to processes with open files\n");
1547                 android::vold::KillProcessesWithOpenFiles(mountpoint, SIGKILL);
1548             }
1549         }
1550
1551         sleep(1);
1552     }
1553
1554     if (i < WAIT_UNMOUNT_COUNT) {
1555         SLOGD("unmounting %s succeeded\n", mountpoint);
1556         rc = 0;
1557     } else {
1558         android::vold::KillProcessesWithOpenFiles(mountpoint, 0);
1559         SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err));
1560         rc = -1;
1561     }
1562
1563     return rc;
1564 }
1565
1566 static void prep_data_fs(void) {
1567     // NOTE: post_fs_data results in init calling back around to vold, so all
1568     // callers to this method must be async
1569
1570     /* Do the prep of the /data filesystem */
1571     property_set("vold.post_fs_data_done", "0");
1572     property_set("vold.decrypt", "trigger_post_fs_data");
1573     SLOGD("Just triggered post_fs_data");
1574
1575     /* Wait a max of 50 seconds, hopefully it takes much less */
1576     while (!android::base::WaitForProperty("vold.post_fs_data_done", "1", std::chrono::seconds(15))) {
1577         /* We timed out to prep /data in time.  Continue wait. */
1578         SLOGE("waited 15s for vold.post_fs_data_done, still waiting...");
1579     }
1580     SLOGD("post_fs_data done");
1581 }
1582
1583 static void cryptfs_set_corrupt() {
1584     // Mark the footer as bad
1585     struct crypt_mnt_ftr crypt_ftr;
1586     if (get_crypt_ftr_and_key(&crypt_ftr)) {
1587         SLOGE("Failed to get crypto footer - panic");
1588         return;
1589     }
1590
1591     crypt_ftr.flags |= CRYPT_DATA_CORRUPT;
1592     if (put_crypt_ftr_and_key(&crypt_ftr)) {
1593         SLOGE("Failed to set crypto footer - panic");
1594         return;
1595     }
1596 }
1597
1598 static void cryptfs_trigger_restart_min_framework() {
1599     if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
1600         SLOGE("Failed to mount tmpfs on data - panic");
1601         return;
1602     }
1603
1604     if (property_set("vold.decrypt", "trigger_post_fs_data")) {
1605         SLOGE("Failed to trigger post fs data - panic");
1606         return;
1607     }
1608
1609     if (property_set("vold.decrypt", "trigger_restart_min_framework")) {
1610         SLOGE("Failed to trigger restart min framework - panic");
1611         return;
1612     }
1613 }
1614
1615 /* returns < 0 on failure */
1616 static int cryptfs_restart_internal(int restart_main) {
1617     char crypto_blkdev[MAXPATHLEN];
1618     int rc = -1;
1619     static int restart_successful = 0;
1620
1621     /* Validate that it's OK to call this routine */
1622     if (!master_key_saved) {
1623         SLOGE("Encrypted filesystem not validated, aborting");
1624         return -1;
1625     }
1626
1627     if (restart_successful) {
1628         SLOGE("System already restarted with encrypted disk, aborting");
1629         return -1;
1630     }
1631
1632     if (restart_main) {
1633         /* Here is where we shut down the framework.  The init scripts
1634          * start all services in one of these classes: core, early_hal, hal,
1635          * main and late_start. To get to the minimal UI for PIN entry, we
1636          * need to start core, early_hal, hal and main. When we want to
1637          * shutdown the framework again, we need to stop most of the services in
1638          * these classes, but only those services that were started after
1639          * /data was mounted. This excludes critical services like vold and
1640          * ueventd, which need to keep running. We could possible stop
1641          * even fewer services, but because we want services to pick up APEX
1642          * libraries from the real /data, restarting is better, as it makes
1643          * these devices consistent with FBE devices and lets them use the
1644          * most recent code.
1645          *
1646          * Once these services have stopped, we should be able
1647          * to umount the tmpfs /data, then mount the encrypted /data.
1648          * We then restart the class core, hal, main, and also the class
1649          * late_start.
1650          *
1651          * At the moment, I've only put a few things in late_start that I know
1652          * are not needed to bring up the framework, and that also cause problems
1653          * with unmounting the tmpfs /data, but I hope to add add more services
1654          * to the late_start class as we optimize this to decrease the delay
1655          * till the user is asked for the password to the filesystem.
1656          */
1657
1658         /* The init files are setup to stop the right set of services when
1659          * vold.decrypt is set to trigger_shutdown_framework.
1660          */
1661         property_set("vold.decrypt", "trigger_shutdown_framework");
1662         SLOGD("Just asked init to shut down class main\n");
1663
1664         /* Ugh, shutting down the framework is not synchronous, so until it
1665          * can be fixed, this horrible hack will wait a moment for it all to
1666          * shut down before proceeding.  Without it, some devices cannot
1667          * restart the graphics services.
1668          */
1669         sleep(2);
1670     }
1671
1672     /* Now that the framework is shutdown, we should be able to umount()
1673      * the tmpfs filesystem, and mount the real one.
1674      */
1675
1676     property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1677     if (strlen(crypto_blkdev) == 0) {
1678         SLOGE("fs_crypto_blkdev not set\n");
1679         return -1;
1680     }
1681
1682     if (!(rc = wait_and_unmount(DATA_MNT_POINT, true))) {
1683         /* If ro.crypto.readonly is set to 1, mount the decrypted
1684          * filesystem readonly.  This is used when /data is mounted by
1685          * recovery mode.
1686          */
1687         char ro_prop[PROPERTY_VALUE_MAX];
1688         property_get("ro.crypto.readonly", ro_prop, "");
1689         if (strlen(ro_prop) > 0 && std::stoi(ro_prop)) {
1690             auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
1691             if (entry != nullptr) {
1692                 entry->flags |= MS_RDONLY;
1693             }
1694         }
1695
1696         /* If that succeeded, then mount the decrypted filesystem */
1697         int retries = RETRY_MOUNT_ATTEMPTS;
1698         int mount_rc;
1699
1700         /*
1701          * fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
1702          * partitions in the fsck domain.
1703          */
1704         if (setexeccon(android::vold::sFsckContext)) {
1705             SLOGE("Failed to setexeccon");
1706             return -1;
1707         }
1708         bool needs_cp = android::vold::cp_needsCheckpoint();
1709         while ((mount_rc = fs_mgr_do_mount(&fstab_default, DATA_MNT_POINT, crypto_blkdev, 0,
1710                                            needs_cp, false)) != 0) {
1711             if (mount_rc == FS_MGR_DOMNT_BUSY) {
1712                 /* TODO: invoke something similar to
1713                    Process::killProcessWithOpenFiles(DATA_MNT_POINT,
1714                                    retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */
1715                 SLOGI("Failed to mount %s because it is busy - waiting", crypto_blkdev);
1716                 if (--retries) {
1717                     sleep(RETRY_MOUNT_DELAY_SECONDS);
1718                 } else {
1719                     /* Let's hope that a reboot clears away whatever is keeping
1720                        the mount busy */
1721                     cryptfs_reboot(RebootType::reboot);
1722                 }
1723             } else {
1724                 SLOGE("Failed to mount decrypted data");
1725                 cryptfs_set_corrupt();
1726                 cryptfs_trigger_restart_min_framework();
1727                 SLOGI("Started framework to offer wipe");
1728                 if (setexeccon(NULL)) {
1729                     SLOGE("Failed to setexeccon");
1730                 }
1731                 return -1;
1732             }
1733         }
1734         if (setexeccon(NULL)) {
1735             SLOGE("Failed to setexeccon");
1736             return -1;
1737         }
1738
1739         /* Create necessary paths on /data */
1740         prep_data_fs();
1741         property_set("vold.decrypt", "trigger_load_persist_props");
1742
1743         /* startup service classes main and late_start */
1744         property_set("vold.decrypt", "trigger_restart_framework");
1745         SLOGD("Just triggered restart_framework\n");
1746
1747         /* Give it a few moments to get started */
1748         sleep(1);
1749     }
1750
1751     if (rc == 0) {
1752         restart_successful = 1;
1753     }
1754
1755     return rc;
1756 }
1757
1758 int cryptfs_restart(void) {
1759     SLOGI("cryptfs_restart");
1760     if (fscrypt_is_native()) {
1761         SLOGE("cryptfs_restart not valid for file encryption:");
1762         return -1;
1763     }
1764
1765     /* Call internal implementation forcing a restart of main service group */
1766     return cryptfs_restart_internal(1);
1767 }
1768
1769 static int do_crypto_complete(const char* mount_point) {
1770     struct crypt_mnt_ftr crypt_ftr;
1771     char encrypted_state[PROPERTY_VALUE_MAX];
1772
1773     property_get("ro.crypto.state", encrypted_state, "");
1774     if (strcmp(encrypted_state, "encrypted")) {
1775         SLOGE("not running with encryption, aborting");
1776         return CRYPTO_COMPLETE_NOT_ENCRYPTED;
1777     }
1778
1779     // crypto_complete is full disk encrypted status
1780     if (fscrypt_is_native()) {
1781         return CRYPTO_COMPLETE_NOT_ENCRYPTED;
1782     }
1783
1784     if (get_crypt_ftr_and_key(&crypt_ftr)) {
1785         std::string key_loc;
1786         get_crypt_info(&key_loc, nullptr);
1787
1788         /*
1789          * Only report this error if key_loc is a file and it exists.
1790          * If the device was never encrypted, and /data is not mountable for
1791          * some reason, returning 1 should prevent the UI from presenting the
1792          * a "enter password" screen, or worse, a "press button to wipe the
1793          * device" screen.
1794          */
1795         if (!key_loc.empty() && key_loc[0] == '/' && (access("key_loc", F_OK) == -1)) {
1796             SLOGE("master key file does not exist, aborting");
1797             return CRYPTO_COMPLETE_NOT_ENCRYPTED;
1798         } else {
1799             SLOGE("Error getting crypt footer and key\n");
1800             return CRYPTO_COMPLETE_BAD_METADATA;
1801         }
1802     }
1803
1804     // Test for possible error flags
1805     if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
1806         SLOGE("Encryption process is partway completed\n");
1807         return CRYPTO_COMPLETE_PARTIAL;
1808     }
1809
1810     if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) {
1811         SLOGE("Encryption process was interrupted but cannot continue\n");
1812         return CRYPTO_COMPLETE_INCONSISTENT;
1813     }
1814
1815     if (crypt_ftr.flags & CRYPT_DATA_CORRUPT) {
1816         SLOGE("Encryption is successful but data is corrupt\n");
1817         return CRYPTO_COMPLETE_CORRUPT;
1818     }
1819
1820     /* We passed the test! We shall diminish, and return to the west */
1821     return CRYPTO_COMPLETE_ENCRYPTED;
1822 }
1823
1824 static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr, const char* passwd,
1825                                    const char* mount_point, const char* label) {
1826     unsigned char decrypted_master_key[MAX_KEY_LEN];
1827     std::string crypto_blkdev;
1828     std::string real_blkdev;
1829     char tmp_mount_point[64];
1830     unsigned int orig_failed_decrypt_count;
1831     int rc;
1832     int upgrade = 0;
1833     unsigned char* intermediate_key = 0;
1834     size_t intermediate_key_size = 0;
1835     int N = 1 << crypt_ftr->N_factor;
1836     int r = 1 << crypt_ftr->r_factor;
1837     int p = 1 << crypt_ftr->p_factor;
1838
1839     SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1840     orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
1841
1842     if (!(crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED)) {
1843         if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr, &intermediate_key,
1844                                &intermediate_key_size)) {
1845             SLOGE("Failed to decrypt master key\n");
1846             rc = -1;
1847             goto errout;
1848         }
1849     }
1850
1851     get_crypt_info(nullptr, &real_blkdev);
1852
1853     // Create crypto block device - all (non fatal) code paths
1854     // need it
1855     if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, real_blkdev.c_str(), &crypto_blkdev,
1856                               label, 0)) {
1857         SLOGE("Error creating decrypted block device\n");
1858         rc = -1;
1859         goto errout;
1860     }
1861
1862     /* Work out if the problem is the password or the data */
1863     unsigned char scrypted_intermediate_key[sizeof(crypt_ftr->scrypted_intermediate_key)];
1864
1865     rc = crypto_scrypt(intermediate_key, intermediate_key_size, crypt_ftr->salt,
1866                        sizeof(crypt_ftr->salt), N, r, p, scrypted_intermediate_key,
1867                        sizeof(scrypted_intermediate_key));
1868
1869     // Does the key match the crypto footer?
1870     if (rc == 0 && memcmp(scrypted_intermediate_key, crypt_ftr->scrypted_intermediate_key,
1871                           sizeof(scrypted_intermediate_key)) == 0) {
1872         SLOGI("Password matches");
1873         rc = 0;
1874     } else {
1875         /* Try mounting the file system anyway, just in case the problem's with
1876          * the footer, not the key. */
1877         snprintf(tmp_mount_point, sizeof(tmp_mount_point), "%s/tmp_mnt", mount_point);
1878         mkdir(tmp_mount_point, 0755);
1879         if (fs_mgr_do_mount(&fstab_default, DATA_MNT_POINT,
1880                             const_cast<char*>(crypto_blkdev.c_str()), tmp_mount_point)) {
1881             SLOGE("Error temp mounting decrypted block device\n");
1882             delete_crypto_blk_dev(label);
1883
1884             rc = ++crypt_ftr->failed_decrypt_count;
1885             put_crypt_ftr_and_key(crypt_ftr);
1886         } else {
1887             /* Success! */
1888             SLOGI("Password did not match but decrypted drive mounted - continue");
1889             umount(tmp_mount_point);
1890             rc = 0;
1891         }
1892     }
1893
1894     if (rc == 0) {
1895         crypt_ftr->failed_decrypt_count = 0;
1896         if (orig_failed_decrypt_count != 0) {
1897             put_crypt_ftr_and_key(crypt_ftr);
1898         }
1899
1900         /* Save the name of the crypto block device
1901          * so we can mount it when restarting the framework. */
1902         property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev.c_str());
1903
1904         /* Also save a the master key so we can reencrypted the key
1905          * the key when we want to change the password on it. */
1906         memcpy(saved_master_key, decrypted_master_key, crypt_ftr->keysize);
1907         saved_mount_point = strdup(mount_point);
1908         master_key_saved = 1;
1909         SLOGD("%s(): Master key saved\n", __FUNCTION__);
1910         rc = 0;
1911
1912         // Upgrade if we're not using the latest KDF.
1913         if (crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) {
1914             crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1915             upgrade = 1;
1916         }
1917
1918         if (upgrade) {
1919             rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1920                                     crypt_ftr->master_key, crypt_ftr);
1921             if (!rc) {
1922                 rc = put_crypt_ftr_and_key(crypt_ftr);
1923             }
1924             SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
1925
1926             // Do not fail even if upgrade failed - machine is bootable
1927             // Note that if this code is ever hit, there is a *serious* problem
1928             // since KDFs should never fail. You *must* fix the kdf before
1929             // proceeding!
1930             if (rc) {
1931                 SLOGW(
1932                     "Upgrade failed with error %d,"
1933                     " but continuing with previous state",
1934                     rc);
1935                 rc = 0;
1936             }
1937         }
1938     }
1939
1940 errout:
1941     if (intermediate_key) {
1942         memset(intermediate_key, 0, intermediate_key_size);
1943         free(intermediate_key);
1944     }
1945     return rc;
1946 }
1947
1948 /*
1949  * Called by vold when it's asked to mount an encrypted external
1950  * storage volume. The incoming partition has no crypto header/footer,
1951  * as any metadata is been stored in a separate, small partition.  We
1952  * assume it must be using our same crypt type and keysize.
1953  */
1954 int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev, const KeyBuffer& key,
1955                              std::string* out_crypto_blkdev) {
1956     auto crypto_type = get_crypto_type();
1957     if (key.size() != crypto_type.get_keysize()) {
1958         SLOGE("Raw keysize %zu does not match crypt keysize %zu", key.size(),
1959               crypto_type.get_keysize());
1960         return -1;
1961     }
1962     uint64_t nr_sec = 0;
1963     if (android::vold::GetBlockDev512Sectors(real_blkdev, &nr_sec) != android::OK) {
1964         SLOGE("Failed to get size of %s: %s", real_blkdev, strerror(errno));
1965         return -1;
1966     }
1967
1968     struct crypt_mnt_ftr ext_crypt_ftr;
1969     memset(&ext_crypt_ftr, 0, sizeof(ext_crypt_ftr));
1970     ext_crypt_ftr.fs_size = nr_sec;
1971     ext_crypt_ftr.keysize = crypto_type.get_keysize();
1972     strlcpy((char*)ext_crypt_ftr.crypto_type_name, crypto_type.get_kernel_name(),
1973             MAX_CRYPTO_TYPE_NAME_LEN);
1974     uint32_t flags = 0;
1975     if (fscrypt_is_native() &&
1976         android::base::GetBoolProperty("ro.crypto.allow_encrypt_override", false))
1977         flags |= CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE;
1978
1979     return create_crypto_blk_dev(&ext_crypt_ftr, reinterpret_cast<const unsigned char*>(key.data()),
1980                                  real_blkdev, out_crypto_blkdev, label, flags);
1981 }
1982
1983 int cryptfs_crypto_complete(void) {
1984     return do_crypto_complete("/data");
1985 }
1986
1987 int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr) {
1988     char encrypted_state[PROPERTY_VALUE_MAX];
1989     property_get("ro.crypto.state", encrypted_state, "");
1990     if (master_key_saved || strcmp(encrypted_state, "encrypted")) {
1991         SLOGE(
1992             "encrypted fs already validated or not running with encryption,"
1993             " aborting");
1994         return -1;
1995     }
1996
1997     if (get_crypt_ftr_and_key(crypt_ftr)) {
1998         SLOGE("Error getting crypt footer and key");
1999         return -1;
2000     }
2001
2002     return 0;
2003 }
2004
2005 int cryptfs_check_passwd(const char* passwd) {
2006     SLOGI("cryptfs_check_passwd");
2007     if (fscrypt_is_native()) {
2008         SLOGE("cryptfs_check_passwd not valid for file encryption");
2009         return -1;
2010     }
2011
2012     struct crypt_mnt_ftr crypt_ftr;
2013     int rc;
2014
2015     rc = check_unmounted_and_get_ftr(&crypt_ftr);
2016     if (rc) {
2017         SLOGE("Could not get footer");
2018         return rc;
2019     }
2020
2021     rc = test_mount_encrypted_fs(&crypt_ftr, passwd, DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
2022     if (rc) {
2023         SLOGE("Password did not match");
2024         return rc;
2025     }
2026
2027     if (crypt_ftr.flags & CRYPT_FORCE_COMPLETE) {
2028         // Here we have a default actual password but a real password
2029         // we must test against the scrypted value
2030         // First, we must delete the crypto block device that
2031         // test_mount_encrypted_fs leaves behind as a side effect
2032         delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
2033         rc = test_mount_encrypted_fs(&crypt_ftr, DEFAULT_PASSWORD, DATA_MNT_POINT,
2034                                      CRYPTO_BLOCK_DEVICE);
2035         if (rc) {
2036             SLOGE("Default password did not match on reboot encryption");
2037             return rc;
2038         }
2039
2040         crypt_ftr.flags &= ~CRYPT_FORCE_COMPLETE;
2041         put_crypt_ftr_and_key(&crypt_ftr);
2042         rc = cryptfs_changepw(crypt_ftr.crypt_type, passwd);
2043         if (rc) {
2044             SLOGE("Could not change password on reboot encryption");
2045             return rc;
2046         }
2047     }
2048
2049     if (crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
2050         cryptfs_clear_password();
2051         password = strdup(passwd);
2052         struct timespec now;
2053         clock_gettime(CLOCK_BOOTTIME, &now);
2054         password_expiry_time = now.tv_sec + password_max_age_seconds;
2055     }
2056
2057     return rc;
2058 }
2059
2060 int cryptfs_verify_passwd(const char* passwd) {
2061     struct crypt_mnt_ftr crypt_ftr;
2062     unsigned char decrypted_master_key[MAX_KEY_LEN];
2063     char encrypted_state[PROPERTY_VALUE_MAX];
2064     int rc;
2065
2066     property_get("ro.crypto.state", encrypted_state, "");
2067     if (strcmp(encrypted_state, "encrypted")) {
2068         SLOGE("device not encrypted, aborting");
2069         return -2;
2070     }
2071
2072     if (!master_key_saved) {
2073         SLOGE("encrypted fs not yet mounted, aborting");
2074         return -1;
2075     }
2076
2077     if (!saved_mount_point) {
2078         SLOGE("encrypted fs failed to save mount point, aborting");
2079         return -1;
2080     }
2081
2082     if (get_crypt_ftr_and_key(&crypt_ftr)) {
2083         SLOGE("Error getting crypt footer and key\n");
2084         return -1;
2085     }
2086
2087     if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
2088         /* If the device has no password, then just say the password is valid */
2089         rc = 0;
2090     } else {
2091         decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
2092         if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
2093             /* They match, the password is correct */
2094             rc = 0;
2095         } else {
2096             /* If incorrect, sleep for a bit to prevent dictionary attacks */
2097             sleep(1);
2098             rc = 1;
2099         }
2100     }
2101
2102     return rc;
2103 }
2104
2105 /* Initialize a crypt_mnt_ftr structure.  The keysize is
2106  * defaulted to get_crypto_type().get_keysize() bytes, and the filesystem size to 0.
2107  * Presumably, at a minimum, the caller will update the
2108  * filesystem size and crypto_type_name after calling this function.
2109  */
2110 static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr* ftr) {
2111     off64_t off;
2112
2113     memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
2114     ftr->magic = CRYPT_MNT_MAGIC;
2115     ftr->major_version = CURRENT_MAJOR_VERSION;
2116     ftr->minor_version = CURRENT_MINOR_VERSION;
2117     ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
2118     ftr->keysize = get_crypto_type().get_keysize();
2119     ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
2120
2121     get_device_scrypt_params(ftr);
2122
2123     ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
2124     if (get_crypt_ftr_info(NULL, &off) == 0) {
2125         ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
2126         ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET + ftr->persist_data_size;
2127     }
2128
2129     return 0;
2130 }
2131
2132 #define FRAMEWORK_BOOT_WAIT 60
2133
2134 static int vold_unmountAll(void) {
2135     VolumeManager* vm = VolumeManager::Instance();
2136     return vm->unmountAll();
2137 }
2138
2139 int cryptfs_enable_internal(int crypt_type, const char* passwd, int no_ui) {
2140     std::string crypto_blkdev;
2141     std::string real_blkdev;
2142     unsigned char decrypted_master_key[MAX_KEY_LEN];
2143     int rc = -1, i;
2144     struct crypt_mnt_ftr crypt_ftr;
2145     struct crypt_persist_data* pdata;
2146     char encrypted_state[PROPERTY_VALUE_MAX];
2147     char lockid[32] = {0};
2148     std::string key_loc;
2149     int num_vols;
2150     bool rebootEncryption = false;
2151     bool onlyCreateHeader = false;
2152
2153     /* Get a wakelock as this may take a while, and we don't want the
2154      * device to sleep on us.  We'll grab a partial wakelock, and if the UI
2155      * wants to keep the screen on, it can grab a full wakelock.
2156      */
2157     snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int)getpid());
2158     auto wl = android::wakelock::WakeLock::tryGet(lockid);
2159     if (!wl.has_value()) {
2160         return android::UNEXPECTED_NULL;
2161     }
2162
2163     if (get_crypt_ftr_and_key(&crypt_ftr) == 0) {
2164         if (crypt_ftr.flags & CRYPT_FORCE_ENCRYPTION) {
2165             if (!check_ftr_sha(&crypt_ftr)) {
2166                 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
2167                 put_crypt_ftr_and_key(&crypt_ftr);
2168                 goto error_unencrypted;
2169             }
2170
2171             /* Doing a reboot-encryption*/
2172             crypt_ftr.flags &= ~CRYPT_FORCE_ENCRYPTION;
2173             crypt_ftr.flags |= CRYPT_FORCE_COMPLETE;
2174             rebootEncryption = true;
2175         }
2176     } else {
2177         // We don't want to accidentally reference invalid data.
2178         memset(&crypt_ftr, 0, sizeof(crypt_ftr));
2179     }
2180
2181     property_get("ro.crypto.state", encrypted_state, "");
2182     if (!strcmp(encrypted_state, "encrypted")) {
2183         SLOGE("Device is already running encrypted, aborting");
2184         goto error_unencrypted;
2185     }
2186
2187     get_crypt_info(&key_loc, &real_blkdev);
2188
2189     /* Get the size of the real block device */
2190     uint64_t nr_sec;
2191     if (android::vold::GetBlockDev512Sectors(real_blkdev, &nr_sec) != android::OK) {
2192         SLOGE("Cannot get size of block device %s\n", real_blkdev.c_str());
2193         goto error_unencrypted;
2194     }
2195
2196     /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
2197     if (key_loc == KEY_IN_FOOTER) {
2198         uint64_t fs_size_sec, max_fs_size_sec;
2199         fs_size_sec = get_fs_size(real_blkdev.c_str());
2200         if (fs_size_sec == 0) fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev.data());
2201
2202         max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
2203
2204         if (fs_size_sec > max_fs_size_sec) {
2205             SLOGE("Orig filesystem overlaps crypto footer region.  Cannot encrypt in place.");
2206             goto error_unencrypted;
2207         }
2208     }
2209
2210     /* The init files are setup to stop the class main and late start when
2211      * vold sets trigger_shutdown_framework.
2212      */
2213     property_set("vold.decrypt", "trigger_shutdown_framework");
2214     SLOGD("Just asked init to shut down class main\n");
2215
2216     /* Ask vold to unmount all devices that it manages */
2217     if (vold_unmountAll()) {
2218         SLOGE("Failed to unmount all vold managed devices");
2219     }
2220
2221     /* no_ui means we are being called from init, not settings.
2222        Now we always reboot from settings, so !no_ui means reboot
2223      */
2224     if (!no_ui) {
2225         /* Try fallback, which is to reboot and try there */
2226         onlyCreateHeader = true;
2227         FILE* breadcrumb = fopen(BREADCRUMB_FILE, "we");
2228         if (breadcrumb == 0) {
2229             SLOGE("Failed to create breadcrumb file");
2230             goto error_shutting_down;
2231         }
2232         fclose(breadcrumb);
2233     }
2234
2235     /* Do extra work for a better UX when doing the long inplace encryption */
2236     if (!onlyCreateHeader) {
2237         /* Now that /data is unmounted, we need to mount a tmpfs
2238          * /data, set a property saying we're doing inplace encryption,
2239          * and restart the framework.
2240          */
2241         wait_and_unmount(DATA_MNT_POINT, true);
2242         if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
2243             goto error_shutting_down;
2244         }
2245         /* Tells the framework that inplace encryption is starting */
2246         property_set("vold.encrypt_progress", "0");
2247
2248         /* restart the framework. */
2249         /* Create necessary paths on /data */
2250         prep_data_fs();
2251
2252         /* Ugh, shutting down the framework is not synchronous, so until it
2253          * can be fixed, this horrible hack will wait a moment for it all to
2254          * shut down before proceeding.  Without it, some devices cannot
2255          * restart the graphics services.
2256          */
2257         sleep(2);
2258     }
2259
2260     /* Start the actual work of making an encrypted filesystem */
2261     /* Initialize a crypt_mnt_ftr for the partition */
2262     if (!rebootEncryption) {
2263         if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) {
2264             goto error_shutting_down;
2265         }
2266
2267         if (key_loc == KEY_IN_FOOTER) {
2268             crypt_ftr.fs_size = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
2269         } else {
2270             crypt_ftr.fs_size = nr_sec;
2271         }
2272         /* At this point, we are in an inconsistent state. Until we successfully
2273            complete encryption, a reboot will leave us broken. So mark the
2274            encryption failed in case that happens.
2275            On successfully completing encryption, remove this flag */
2276         if (onlyCreateHeader) {
2277             crypt_ftr.flags |= CRYPT_FORCE_ENCRYPTION;
2278         } else {
2279             crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
2280         }
2281         crypt_ftr.crypt_type = crypt_type;
2282         strlcpy((char*)crypt_ftr.crypto_type_name, get_crypto_type().get_kernel_name(),
2283                 MAX_CRYPTO_TYPE_NAME_LEN);
2284
2285         /* Make an encrypted master key */
2286         if (create_encrypted_random_key(onlyCreateHeader ? DEFAULT_PASSWORD : passwd,
2287                                         crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
2288             SLOGE("Cannot create encrypted master key\n");
2289             goto error_shutting_down;
2290         }
2291
2292         /* Replace scrypted intermediate key if we are preparing for a reboot */
2293         if (onlyCreateHeader) {
2294             unsigned char fake_master_key[MAX_KEY_LEN];
2295             unsigned char encrypted_fake_master_key[MAX_KEY_LEN];
2296             memset(fake_master_key, 0, sizeof(fake_master_key));
2297             encrypt_master_key(passwd, crypt_ftr.salt, fake_master_key, encrypted_fake_master_key,
2298                                &crypt_ftr);
2299         }
2300
2301         /* Write the key to the end of the partition */
2302         put_crypt_ftr_and_key(&crypt_ftr);
2303
2304         /* If any persistent data has been remembered, save it.
2305          * If none, create a valid empty table and save that.
2306          */
2307         if (!persist_data) {
2308             pdata = (crypt_persist_data*)malloc(CRYPT_PERSIST_DATA_SIZE);
2309             if (pdata) {
2310                 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
2311                 persist_data = pdata;
2312             }
2313         }
2314         if (persist_data) {
2315             save_persistent_data();
2316         }
2317     }
2318
2319     if (onlyCreateHeader) {
2320         sleep(2);
2321         cryptfs_reboot(RebootType::reboot);
2322     }
2323
2324     if (!no_ui || rebootEncryption) {
2325         /* startup service classes main and late_start */
2326         property_set("vold.decrypt", "trigger_restart_min_framework");
2327         SLOGD("Just triggered restart_min_framework\n");
2328
2329         /* OK, the framework is restarted and will soon be showing a
2330          * progress bar.  Time to setup an encrypted mapping, and
2331          * either write a new filesystem, or encrypt in place updating
2332          * the progress bar as we work.
2333          */
2334     }
2335
2336     decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
2337     rc = create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev.c_str(),
2338                                &crypto_blkdev, CRYPTO_BLOCK_DEVICE, 0);
2339     if (!rc) {
2340         if (encrypt_inplace(crypto_blkdev, real_blkdev, crypt_ftr.fs_size, true)) {
2341             crypt_ftr.encrypted_upto = crypt_ftr.fs_size;
2342             rc = 0;
2343         } else {
2344             rc = -1;
2345         }
2346         /* Undo the dm-crypt mapping whether we succeed or not */
2347         delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
2348     }
2349
2350     if (!rc) {
2351         /* Success */
2352         crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE;
2353
2354         put_crypt_ftr_and_key(&crypt_ftr);
2355
2356         char value[PROPERTY_VALUE_MAX];
2357         property_get("ro.crypto.state", value, "");
2358         if (!strcmp(value, "")) {
2359             /* default encryption - continue first boot sequence */
2360             property_set("ro.crypto.state", "encrypted");
2361             property_set("ro.crypto.type", "block");
2362             wl.reset();
2363             if (rebootEncryption && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
2364                 // Bring up cryptkeeper that will check the password and set it
2365                 property_set("vold.decrypt", "trigger_shutdown_framework");
2366                 sleep(2);
2367                 property_set("vold.encrypt_progress", "");
2368                 cryptfs_trigger_restart_min_framework();
2369             } else {
2370                 cryptfs_check_passwd(DEFAULT_PASSWORD);
2371                 cryptfs_restart_internal(1);
2372             }
2373             return 0;
2374         } else {
2375             sleep(2); /* Give the UI a chance to show 100% progress */
2376             cryptfs_reboot(RebootType::reboot);
2377         }
2378     } else {
2379         char value[PROPERTY_VALUE_MAX];
2380
2381         property_get("ro.vold.wipe_on_crypt_fail", value, "0");
2382         if (!strcmp(value, "1")) {
2383             /* wipe data if encryption failed */
2384             SLOGE("encryption failed - rebooting into recovery to wipe data\n");
2385             std::string err;
2386             const std::vector<std::string> options = {
2387                 "--wipe_data\n--reason=cryptfs_enable_internal\n"};
2388             if (!write_bootloader_message(options, &err)) {
2389                 SLOGE("could not write bootloader message: %s", err.c_str());
2390             }
2391             cryptfs_reboot(RebootType::recovery);
2392         } else {
2393             /* set property to trigger dialog */
2394             property_set("vold.encrypt_progress", "error_partially_encrypted");
2395         }
2396         return -1;
2397     }
2398
2399     /* hrm, the encrypt step claims success, but the reboot failed.
2400      * This should not happen.
2401      * Set the property and return.  Hope the framework can deal with it.
2402      */
2403     property_set("vold.encrypt_progress", "error_reboot_failed");
2404     return rc;
2405
2406 error_unencrypted:
2407     property_set("vold.encrypt_progress", "error_not_encrypted");
2408     return -1;
2409
2410 error_shutting_down:
2411     /* we failed, and have not encrypted anthing, so the users's data is still intact,
2412      * but the framework is stopped and not restarted to show the error, so it's up to
2413      * vold to restart the system.
2414      */
2415     SLOGE(
2416         "Error enabling encryption after framework is shutdown, no data changed, restarting "
2417         "system");
2418     cryptfs_reboot(RebootType::reboot);
2419
2420     /* shouldn't get here */
2421     property_set("vold.encrypt_progress", "error_shutting_down");
2422     return -1;
2423 }
2424
2425 int cryptfs_enable(int type, const char* passwd, int no_ui) {
2426     return cryptfs_enable_internal(type, passwd, no_ui);
2427 }
2428
2429 int cryptfs_enable_default(int no_ui) {
2430     return cryptfs_enable_internal(CRYPT_TYPE_DEFAULT, DEFAULT_PASSWORD, no_ui);
2431 }
2432
2433 int cryptfs_changepw(int crypt_type, const char* newpw) {
2434     if (fscrypt_is_native()) {
2435         SLOGE("cryptfs_changepw not valid for file encryption");
2436         return -1;
2437     }
2438
2439     struct crypt_mnt_ftr crypt_ftr;
2440     int rc;
2441
2442     /* This is only allowed after we've successfully decrypted the master key */
2443     if (!master_key_saved) {
2444         SLOGE("Key not saved, aborting");
2445         return -1;
2446     }
2447
2448     if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2449         SLOGE("Invalid crypt_type %d", crypt_type);
2450         return -1;
2451     }
2452
2453     /* get key */
2454     if (get_crypt_ftr_and_key(&crypt_ftr)) {
2455         SLOGE("Error getting crypt footer and key");
2456         return -1;
2457     }
2458
2459     crypt_ftr.crypt_type = crypt_type;
2460
2461     rc = encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD : newpw,
2462                             crypt_ftr.salt, saved_master_key, crypt_ftr.master_key, &crypt_ftr);
2463     if (rc) {
2464         SLOGE("Encrypt master key failed: %d", rc);
2465         return -1;
2466     }
2467     /* save the key */
2468     put_crypt_ftr_and_key(&crypt_ftr);
2469
2470     return 0;
2471 }
2472
2473 static unsigned int persist_get_max_entries(int encrypted) {
2474     struct crypt_mnt_ftr crypt_ftr;
2475     unsigned int dsize;
2476
2477     /* If encrypted, use the values from the crypt_ftr, otherwise
2478      * use the values for the current spec.
2479      */
2480     if (encrypted) {
2481         if (get_crypt_ftr_and_key(&crypt_ftr)) {
2482             /* Something is wrong, assume no space for entries */
2483             return 0;
2484         }
2485         dsize = crypt_ftr.persist_data_size;
2486     } else {
2487         dsize = CRYPT_PERSIST_DATA_SIZE;
2488     }
2489
2490     if (dsize > sizeof(struct crypt_persist_data)) {
2491         return (dsize - sizeof(struct crypt_persist_data)) / sizeof(struct crypt_persist_entry);
2492     } else {
2493         return 0;
2494     }
2495 }
2496
2497 static int persist_get_key(const char* fieldname, char* value) {
2498     unsigned int i;
2499
2500     if (persist_data == NULL) {
2501         return -1;
2502     }
2503     for (i = 0; i < persist_data->persist_valid_entries; i++) {
2504         if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2505             /* We found it! */
2506             strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
2507             return 0;
2508         }
2509     }
2510
2511     return -1;
2512 }
2513
2514 static int persist_set_key(const char* fieldname, const char* value, int encrypted) {
2515     unsigned int i;
2516     unsigned int num;
2517     unsigned int max_persistent_entries;
2518
2519     if (persist_data == NULL) {
2520         return -1;
2521     }
2522
2523     max_persistent_entries = persist_get_max_entries(encrypted);
2524
2525     num = persist_data->persist_valid_entries;
2526
2527     for (i = 0; i < num; i++) {
2528         if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2529             /* We found an existing entry, update it! */
2530             memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
2531             strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
2532             return 0;
2533         }
2534     }
2535
2536     /* We didn't find it, add it to the end, if there is room */
2537     if (persist_data->persist_valid_entries < max_persistent_entries) {
2538         memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
2539         strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
2540         strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
2541         persist_data->persist_valid_entries++;
2542         return 0;
2543     }
2544
2545     return -1;
2546 }
2547
2548 /**
2549  * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the
2550  * sequence and its index is greater than or equal to index. Return 0 otherwise.
2551  */
2552 int match_multi_entry(const char* key, const char* field, unsigned index) {
2553     std::string key_ = key;
2554     std::string field_ = field;
2555
2556     std::string parsed_field;
2557     unsigned parsed_index;
2558
2559     std::string::size_type split = key_.find_last_of('_');
2560     if (split == std::string::npos) {
2561         parsed_field = key_;
2562         parsed_index = 0;
2563     } else {
2564         parsed_field = key_.substr(0, split);
2565         parsed_index = std::stoi(key_.substr(split + 1));
2566     }
2567
2568     return parsed_field == field_ && parsed_index >= index;
2569 }
2570
2571 /*
2572  * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all
2573  * remaining entries starting from index will be deleted.
2574  * returns PERSIST_DEL_KEY_OK if deletion succeeds,
2575  * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist,
2576  * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs.
2577  *
2578  */
2579 static int persist_del_keys(const char* fieldname, unsigned index) {
2580     unsigned int i;
2581     unsigned int j;
2582     unsigned int num;
2583
2584     if (persist_data == NULL) {
2585         return PERSIST_DEL_KEY_ERROR_OTHER;
2586     }
2587
2588     num = persist_data->persist_valid_entries;
2589
2590     j = 0;  // points to the end of non-deleted entries.
2591     // Filter out to-be-deleted entries in place.
2592     for (i = 0; i < num; i++) {
2593         if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) {
2594             persist_data->persist_entry[j] = persist_data->persist_entry[i];
2595             j++;
2596         }
2597     }
2598
2599     if (j < num) {
2600         persist_data->persist_valid_entries = j;
2601         // Zeroise the remaining entries
2602         memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry));
2603         return PERSIST_DEL_KEY_OK;
2604     } else {
2605         // Did not find an entry matching the given fieldname
2606         return PERSIST_DEL_KEY_ERROR_NO_FIELD;
2607     }
2608 }
2609
2610 static int persist_count_keys(const char* fieldname) {
2611     unsigned int i;
2612     unsigned int count;
2613
2614     if (persist_data == NULL) {
2615         return -1;
2616     }
2617
2618     count = 0;
2619     for (i = 0; i < persist_data->persist_valid_entries; i++) {
2620         if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) {
2621             count++;
2622         }
2623     }
2624
2625     return count;
2626 }
2627
2628 /* Return the value of the specified field. */
2629 int cryptfs_getfield(const char* fieldname, char* value, int len) {
2630     if (fscrypt_is_native()) {
2631         SLOGE("Cannot get field when file encrypted");
2632         return -1;
2633     }
2634
2635     char temp_value[PROPERTY_VALUE_MAX];
2636     /* CRYPTO_GETFIELD_OK is success,
2637      * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set,
2638      * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small,
2639      * CRYPTO_GETFIELD_ERROR_OTHER is any other error
2640      */
2641     int rc = CRYPTO_GETFIELD_ERROR_OTHER;
2642     int i;
2643     char temp_field[PROPERTY_KEY_MAX];
2644
2645     if (persist_data == NULL) {
2646         load_persistent_data();
2647         if (persist_data == NULL) {
2648             SLOGE("Getfield error, cannot load persistent data");
2649             goto out;
2650         }
2651     }
2652
2653     // Read value from persistent entries. If the original value is split into multiple entries,
2654     // stitch them back together.
2655     if (!persist_get_key(fieldname, temp_value)) {
2656         // We found it, copy it to the caller's buffer and keep going until all entries are read.
2657         if (strlcpy(value, temp_value, len) >= (unsigned)len) {
2658             // value too small
2659             rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
2660             goto out;
2661         }
2662         rc = CRYPTO_GETFIELD_OK;
2663
2664         for (i = 1; /* break explicitly */; i++) {
2665             if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >=
2666                 (int)sizeof(temp_field)) {
2667                 // If the fieldname is very long, we stop as soon as it begins to overflow the
2668                 // maximum field length. At this point we have in fact fully read out the original
2669                 // value because cryptfs_setfield would not allow fields with longer names to be
2670                 // written in the first place.
2671                 break;
2672             }
2673             if (!persist_get_key(temp_field, temp_value)) {
2674                 if (strlcat(value, temp_value, len) >= (unsigned)len) {
2675                     // value too small.
2676                     rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
2677                     goto out;
2678                 }
2679             } else {
2680                 // Exhaust all entries.
2681                 break;
2682             }
2683         }
2684     } else {
2685         /* Sadness, it's not there.  Return the error */
2686         rc = CRYPTO_GETFIELD_ERROR_NO_FIELD;
2687     }
2688
2689 out:
2690     return rc;
2691 }
2692
2693 /* Set the value of the specified field. */
2694 int cryptfs_setfield(const char* fieldname, const char* value) {
2695     if (fscrypt_is_native()) {
2696         SLOGE("Cannot set field when file encrypted");
2697         return -1;
2698     }
2699
2700     char encrypted_state[PROPERTY_VALUE_MAX];
2701     /* 0 is success, negative values are error */
2702     int rc = CRYPTO_SETFIELD_ERROR_OTHER;
2703     int encrypted = 0;
2704     unsigned int field_id;
2705     char temp_field[PROPERTY_KEY_MAX];
2706     unsigned int num_entries;
2707     unsigned int max_keylen;
2708
2709     if (persist_data == NULL) {
2710         load_persistent_data();
2711         if (persist_data == NULL) {
2712             SLOGE("Setfield error, cannot load persistent data");
2713             goto out;
2714         }
2715     }
2716
2717     property_get("ro.crypto.state", encrypted_state, "");
2718     if (!strcmp(encrypted_state, "encrypted")) {
2719         encrypted = 1;
2720     }
2721
2722     // Compute the number of entries required to store value, each entry can store up to
2723     // (PROPERTY_VALUE_MAX - 1) chars
2724     if (strlen(value) == 0) {
2725         // Empty value also needs one entry to store.
2726         num_entries = 1;
2727     } else {
2728         num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1);
2729     }
2730
2731     max_keylen = strlen(fieldname);
2732     if (num_entries > 1) {
2733         // Need an extra "_%d" suffix.
2734         max_keylen += 1 + log10(num_entries);
2735     }
2736     if (max_keylen > PROPERTY_KEY_MAX - 1) {
2737         rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG;
2738         goto out;
2739     }
2740
2741     // Make sure we have enough space to write the new value
2742     if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) >
2743         persist_get_max_entries(encrypted)) {
2744         rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG;
2745         goto out;
2746     }
2747
2748     // Now that we know persist_data has enough space for value, let's delete the old field first
2749     // to make up space.
2750     persist_del_keys(fieldname, 0);
2751
2752     if (persist_set_key(fieldname, value, encrypted)) {
2753         // fail to set key, should not happen as we have already checked the available space
2754         SLOGE("persist_set_key() error during setfield()");
2755         goto out;
2756     }
2757
2758     for (field_id = 1; field_id < num_entries; field_id++) {
2759         snprintf(temp_field, sizeof(temp_field), "%s_%u", fieldname, field_id);
2760
2761         if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), encrypted)) {
2762             // fail to set key, should not happen as we have already checked the available space.
2763             SLOGE("persist_set_key() error during setfield()");
2764             goto out;
2765         }
2766     }
2767
2768     /* If we are running encrypted, save the persistent data now */
2769     if (encrypted) {
2770         if (save_persistent_data()) {
2771             SLOGE("Setfield error, cannot save persistent data");
2772             goto out;
2773         }
2774     }
2775
2776     rc = CRYPTO_SETFIELD_OK;
2777
2778 out:
2779     return rc;
2780 }
2781
2782 /* Checks userdata. Attempt to mount the volume if default-
2783  * encrypted.
2784  * On success trigger next init phase and return 0.
2785  * Currently do not handle failure - see TODO below.
2786  */
2787 int cryptfs_mount_default_encrypted(void) {
2788     int crypt_type = cryptfs_get_password_type();
2789     if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2790         SLOGE("Bad crypt type - error");
2791     } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
2792         SLOGD(
2793             "Password is not default - "
2794             "starting min framework to prompt");
2795         property_set("vold.decrypt", "trigger_restart_min_framework");
2796         return 0;
2797     } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
2798         SLOGD("Password is default - restarting filesystem");
2799         cryptfs_restart_internal(0);
2800         return 0;
2801     } else {
2802         SLOGE("Encrypted, default crypt type but can't decrypt");
2803     }
2804
2805     /** Corrupt. Allow us to boot into framework, which will detect bad
2806         crypto when it calls do_crypto_complete, then do a factory reset
2807      */
2808     property_set("vold.decrypt", "trigger_restart_min_framework");
2809     return 0;
2810 }
2811
2812 /* Returns type of the password, default, pattern, pin or password.
2813  */
2814 int cryptfs_get_password_type(void) {
2815     if (fscrypt_is_native()) {
2816         SLOGE("cryptfs_get_password_type not valid for file encryption");
2817         return -1;
2818     }
2819
2820     struct crypt_mnt_ftr crypt_ftr;
2821
2822     if (get_crypt_ftr_and_key(&crypt_ftr)) {
2823         SLOGE("Error getting crypt footer and key\n");
2824         return -1;
2825     }
2826
2827     if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) {
2828         return -1;
2829     }
2830
2831     return crypt_ftr.crypt_type;
2832 }
2833
2834 const char* cryptfs_get_password() {
2835     if (fscrypt_is_native()) {
2836         SLOGE("cryptfs_get_password not valid for file encryption");
2837         return 0;
2838     }
2839
2840     struct timespec now;
2841     clock_gettime(CLOCK_BOOTTIME, &now);
2842     if (now.tv_sec < password_expiry_time) {
2843         return password;
2844     } else {
2845         cryptfs_clear_password();
2846         return 0;
2847     }
2848 }
2849
2850 void cryptfs_clear_password() {
2851     if (password) {
2852         size_t len = strlen(password);
2853         memset(password, 0, len);
2854         free(password);
2855         password = 0;
2856         password_expiry_time = 0;
2857     }
2858 }
2859
2860 int cryptfs_isConvertibleToFBE() {
2861     auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
2862     return entry && entry->fs_mgr_flags.force_fde_or_fbe;
2863 }