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