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