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