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