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