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