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