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