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