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