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