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