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