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