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