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