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