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