Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* TO DO: |
| 18 | * 1. Perhaps keep several copies of the encrypted key, in case something |
| 19 | * goes horribly wrong? |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <sys/types.h> |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 24 | #include <sys/wait.h> |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 25 | #include <sys/stat.h> |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 26 | #include <ctype.h> |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 27 | #include <fcntl.h> |
Elliott Hughes | 7373716 | 2014-06-25 17:27:42 -0700 | [diff] [blame] | 28 | #include <inttypes.h> |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | #include <stdio.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <linux/dm-ioctl.h> |
| 33 | #include <libgen.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <sys/param.h> |
| 36 | #include <string.h> |
| 37 | #include <sys/mount.h> |
| 38 | #include <openssl/evp.h> |
| 39 | #include <errno.h> |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 40 | #include <ext4.h> |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 41 | #include <linux/kdev_t.h> |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 42 | #include <fs_mgr.h> |
Paul Lawrence | 9c58a87 | 2014-09-30 09:12:51 -0700 | [diff] [blame] | 43 | #include <time.h> |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 44 | #include "cryptfs.h" |
| 45 | #define LOG_TAG "Cryptfs" |
| 46 | #include "cutils/log.h" |
| 47 | #include "cutils/properties.h" |
Ken Sumrall | adfba36 | 2013-06-04 16:37:52 -0700 | [diff] [blame] | 48 | #include "cutils/android_reboot.h" |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 49 | #include "hardware_legacy/power.h" |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 50 | #include <logwrap/logwrap.h> |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 51 | #include "VolumeManager.h" |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 52 | #include "VoldUtil.h" |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 53 | #include "crypto_scrypt.h" |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 54 | #include "ext4_utils.h" |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 55 | #include "f2fs_sparseblock.h" |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 56 | #include "CheckBattery.h" |
jessica_yu | 3f14fe4 | 2014-09-22 15:57:40 +0800 | [diff] [blame] | 57 | #include "Process.h" |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 58 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 59 | #include <hardware/keymaster.h> |
| 60 | |
Mark Salyzyn | 3e97127 | 2014-01-21 13:27:04 -0800 | [diff] [blame] | 61 | #define UNUSED __attribute__((unused)) |
| 62 | |
Mark Salyzyn | 5eecc44 | 2014-02-12 14:16:14 -0800 | [diff] [blame] | 63 | #define UNUSED __attribute__((unused)) |
| 64 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 65 | #define DM_CRYPT_BUF_SIZE 4096 |
| 66 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 67 | #define HASH_COUNT 2000 |
| 68 | #define KEY_LEN_BYTES 16 |
| 69 | #define IV_LEN_BYTES 16 |
| 70 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 71 | #define KEY_IN_FOOTER "footer" |
| 72 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 73 | // "default_password" encoded into hex (d=0x64 etc) |
| 74 | #define DEFAULT_PASSWORD "64656661756c745f70617373776f7264" |
| 75 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 76 | #define EXT4_FS 1 |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 77 | #define F2FS_FS 2 |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 78 | |
Ken Sumrall | e919efe | 2012-09-29 17:07:41 -0700 | [diff] [blame] | 79 | #define TABLE_LOAD_RETRIES 10 |
| 80 | |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 81 | #define RSA_KEY_SIZE 2048 |
| 82 | #define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8) |
| 83 | #define RSA_EXPONENT 0x10001 |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 84 | |
Paul Lawrence | 8e3f451 | 2014-09-08 10:11:17 -0700 | [diff] [blame] | 85 | #define RETRY_MOUNT_ATTEMPTS 10 |
| 86 | #define RETRY_MOUNT_DELAY_SECONDS 1 |
| 87 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 88 | char *me = "cryptfs"; |
| 89 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 90 | static unsigned char saved_master_key[KEY_LEN_BYTES]; |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 91 | static char *saved_mount_point; |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 92 | static int master_key_saved = 0; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 93 | static struct crypt_persist_data *persist_data = NULL; |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 94 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 95 | static int keymaster_init(keymaster_device_t **keymaster_dev) |
| 96 | { |
| 97 | int rc; |
| 98 | |
| 99 | const hw_module_t* mod; |
| 100 | rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod); |
| 101 | if (rc) { |
| 102 | ALOGE("could not find any keystore module"); |
| 103 | goto out; |
| 104 | } |
| 105 | |
| 106 | rc = keymaster_open(mod, keymaster_dev); |
| 107 | if (rc) { |
| 108 | ALOGE("could not open keymaster device in %s (%s)", |
| 109 | KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc)); |
| 110 | goto out; |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | |
| 115 | out: |
| 116 | *keymaster_dev = NULL; |
| 117 | return rc; |
| 118 | } |
| 119 | |
| 120 | /* Should we use keymaster? */ |
| 121 | static int keymaster_check_compatibility() |
| 122 | { |
| 123 | keymaster_device_t *keymaster_dev = 0; |
| 124 | int rc = 0; |
| 125 | |
| 126 | if (keymaster_init(&keymaster_dev)) { |
| 127 | SLOGE("Failed to init keymaster"); |
| 128 | rc = -1; |
| 129 | goto out; |
| 130 | } |
| 131 | |
Paul Lawrence | 8c00839 | 2014-05-06 14:02:48 -0700 | [diff] [blame] | 132 | SLOGI("keymaster version is %d", keymaster_dev->common.module->module_api_version); |
| 133 | |
| 134 | if (keymaster_dev->common.module->module_api_version |
| 135 | < KEYMASTER_MODULE_API_VERSION_0_3) { |
| 136 | rc = 0; |
| 137 | goto out; |
| 138 | } |
| 139 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 140 | if (keymaster_dev->flags & KEYMASTER_BLOBS_ARE_STANDALONE) { |
| 141 | rc = 1; |
| 142 | } |
| 143 | |
| 144 | out: |
| 145 | keymaster_close(keymaster_dev); |
| 146 | return rc; |
| 147 | } |
| 148 | |
| 149 | /* Create a new keymaster key and store it in this footer */ |
| 150 | static int keymaster_create_key(struct crypt_mnt_ftr *ftr) |
| 151 | { |
| 152 | uint8_t* key = 0; |
| 153 | keymaster_device_t *keymaster_dev = 0; |
| 154 | |
| 155 | if (keymaster_init(&keymaster_dev)) { |
| 156 | SLOGE("Failed to init keymaster"); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | int rc = 0; |
| 161 | |
| 162 | keymaster_rsa_keygen_params_t params; |
| 163 | memset(¶ms, '\0', sizeof(params)); |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 164 | params.public_exponent = RSA_EXPONENT; |
| 165 | params.modulus_size = RSA_KEY_SIZE; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 166 | |
| 167 | size_t key_size; |
| 168 | if (keymaster_dev->generate_keypair(keymaster_dev, TYPE_RSA, ¶ms, |
| 169 | &key, &key_size)) { |
| 170 | SLOGE("Failed to generate keypair"); |
| 171 | rc = -1; |
| 172 | goto out; |
| 173 | } |
| 174 | |
| 175 | if (key_size > KEYMASTER_BLOB_SIZE) { |
| 176 | SLOGE("Keymaster key too large for crypto footer"); |
| 177 | rc = -1; |
| 178 | goto out; |
| 179 | } |
| 180 | |
| 181 | memcpy(ftr->keymaster_blob, key, key_size); |
| 182 | ftr->keymaster_blob_size = key_size; |
| 183 | |
| 184 | out: |
| 185 | keymaster_close(keymaster_dev); |
| 186 | free(key); |
| 187 | return rc; |
| 188 | } |
| 189 | |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 190 | /* This signs the given object using the keymaster key. */ |
| 191 | static int keymaster_sign_object(struct crypt_mnt_ftr *ftr, |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 192 | const unsigned char *object, |
| 193 | const size_t object_size, |
| 194 | unsigned char **signature, |
| 195 | size_t *signature_size) |
| 196 | { |
| 197 | int rc = 0; |
| 198 | keymaster_device_t *keymaster_dev = 0; |
| 199 | if (keymaster_init(&keymaster_dev)) { |
| 200 | SLOGE("Failed to init keymaster"); |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | /* We currently set the digest type to DIGEST_NONE because it's the |
| 205 | * only supported value for keymaster. A similar issue exists with |
| 206 | * PADDING_NONE. Long term both of these should likely change. |
| 207 | */ |
| 208 | keymaster_rsa_sign_params_t params; |
| 209 | params.digest_type = DIGEST_NONE; |
| 210 | params.padding_type = PADDING_NONE; |
| 211 | |
| 212 | unsigned char to_sign[RSA_KEY_SIZE_BYTES]; |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 213 | size_t to_sign_size = sizeof(to_sign); |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 214 | memset(to_sign, 0, RSA_KEY_SIZE_BYTES); |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 215 | |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 216 | // To sign a message with RSA, the message must satisfy two |
| 217 | // constraints: |
| 218 | // |
| 219 | // 1. The message, when interpreted as a big-endian numeric value, must |
| 220 | // be strictly less than the public modulus of the RSA key. Note |
| 221 | // that because the most significant bit of the public modulus is |
| 222 | // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit |
| 223 | // key), an n-bit message with most significant bit 0 always |
| 224 | // satisfies this requirement. |
| 225 | // |
| 226 | // 2. The message must have the same length in bits as the public |
| 227 | // modulus of the RSA key. This requirement isn't mathematically |
| 228 | // necessary, but is necessary to ensure consistency in |
| 229 | // implementations. |
| 230 | switch (ftr->kdf_type) { |
| 231 | case KDF_SCRYPT_KEYMASTER_UNPADDED: |
| 232 | // This is broken: It produces a message which is shorter than |
| 233 | // the public modulus, failing criterion 2. |
| 234 | memcpy(to_sign, object, object_size); |
| 235 | to_sign_size = object_size; |
| 236 | SLOGI("Signing unpadded object"); |
| 237 | break; |
| 238 | case KDF_SCRYPT_KEYMASTER_BADLY_PADDED: |
| 239 | // This is broken: Since the value of object is uniformly |
| 240 | // distributed, it produces a message that is larger than the |
| 241 | // public modulus with probability 0.25. |
| 242 | memcpy(to_sign, object, min(RSA_KEY_SIZE_BYTES, object_size)); |
| 243 | SLOGI("Signing end-padded object"); |
| 244 | break; |
| 245 | case KDF_SCRYPT_KEYMASTER: |
| 246 | // This ensures the most significant byte of the signed message |
| 247 | // is zero. We could have zero-padded to the left instead, but |
| 248 | // this approach is slightly more robust against changes in |
| 249 | // object size. However, it's still broken (but not unusably |
| 250 | // so) because we really should be using a proper RSA padding |
| 251 | // function, such as OAEP. |
| 252 | // |
| 253 | // TODO(paullawrence): When keymaster 0.4 is available, change |
| 254 | // this to use the padding options it provides. |
| 255 | memcpy(to_sign + 1, object, min(RSA_KEY_SIZE_BYTES - 1, object_size)); |
| 256 | SLOGI("Signing safely-padded object"); |
| 257 | break; |
| 258 | default: |
| 259 | SLOGE("Unknown KDF type %d", ftr->kdf_type); |
| 260 | return -1; |
| 261 | } |
| 262 | |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 263 | rc = keymaster_dev->sign_data(keymaster_dev, |
| 264 | ¶ms, |
| 265 | ftr->keymaster_blob, |
| 266 | ftr->keymaster_blob_size, |
| 267 | to_sign, |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 268 | to_sign_size, |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 269 | signature, |
| 270 | signature_size); |
| 271 | |
| 272 | keymaster_close(keymaster_dev); |
| 273 | return rc; |
| 274 | } |
| 275 | |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 276 | /* Store password when userdata is successfully decrypted and mounted. |
| 277 | * Cleared by cryptfs_clear_password |
| 278 | * |
| 279 | * To avoid a double prompt at boot, we need to store the CryptKeeper |
| 280 | * password and pass it to KeyGuard, which uses it to unlock KeyStore. |
| 281 | * Since the entire framework is torn down and rebuilt after encryption, |
| 282 | * we have to use a daemon or similar to store the password. Since vold |
| 283 | * is secured against IPC except from system processes, it seems a reasonable |
| 284 | * place to store this. |
| 285 | * |
| 286 | * password should be cleared once it has been used. |
| 287 | * |
| 288 | * password is aged out after password_max_age_seconds seconds. |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 289 | */ |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 290 | static char* password = 0; |
| 291 | static int password_expiry_time = 0; |
| 292 | static const int password_max_age_seconds = 60; |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 293 | |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 294 | extern struct fstab *fstab; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 295 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 296 | enum RebootType {reboot, recovery, shutdown}; |
| 297 | static void cryptfs_reboot(enum RebootType rt) |
Ken Sumrall | adfba36 | 2013-06-04 16:37:52 -0700 | [diff] [blame] | 298 | { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 299 | switch(rt) { |
| 300 | case reboot: |
| 301 | property_set(ANDROID_RB_PROPERTY, "reboot"); |
| 302 | break; |
| 303 | |
| 304 | case recovery: |
| 305 | property_set(ANDROID_RB_PROPERTY, "reboot,recovery"); |
| 306 | break; |
| 307 | |
| 308 | case shutdown: |
| 309 | property_set(ANDROID_RB_PROPERTY, "shutdown"); |
| 310 | break; |
Ken Sumrall | adfba36 | 2013-06-04 16:37:52 -0700 | [diff] [blame] | 311 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 312 | |
Ken Sumrall | adfba36 | 2013-06-04 16:37:52 -0700 | [diff] [blame] | 313 | sleep(20); |
| 314 | |
| 315 | /* Shouldn't get here, reboot should happen before sleep times out */ |
| 316 | return; |
| 317 | } |
| 318 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 319 | static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags) |
| 320 | { |
| 321 | memset(io, 0, dataSize); |
| 322 | io->data_size = dataSize; |
| 323 | io->data_start = sizeof(struct dm_ioctl); |
| 324 | io->version[0] = 4; |
| 325 | io->version[1] = 0; |
| 326 | io->version[2] = 0; |
| 327 | io->flags = flags; |
| 328 | if (name) { |
Marek Pola | 5e6b914 | 2015-02-05 14:22:34 +0100 | [diff] [blame^] | 329 | strlcpy(io->name, name, sizeof(io->name)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 333 | /** |
| 334 | * Gets the default device scrypt parameters for key derivation time tuning. |
| 335 | * The parameters should lead to about one second derivation time for the |
| 336 | * given device. |
| 337 | */ |
| 338 | static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) { |
| 339 | const int default_params[] = SCRYPT_DEFAULTS; |
| 340 | int params[] = SCRYPT_DEFAULTS; |
| 341 | char paramstr[PROPERTY_VALUE_MAX]; |
| 342 | char *token; |
| 343 | char *saveptr; |
| 344 | int i; |
| 345 | |
| 346 | property_get(SCRYPT_PROP, paramstr, ""); |
| 347 | if (paramstr[0] != '\0') { |
| 348 | /* |
| 349 | * The token we're looking for should be three integers separated by |
| 350 | * colons (e.g., "12:8:1"). Scan the property to make sure it matches. |
| 351 | */ |
Kenny Root | 2947e34 | 2013-08-14 15:54:49 -0700 | [diff] [blame] | 352 | for (i = 0, token = strtok_r(paramstr, ":", &saveptr); |
| 353 | token != NULL && i < 3; |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 354 | i++, token = strtok_r(NULL, ":", &saveptr)) { |
| 355 | char *endptr; |
| 356 | params[i] = strtol(token, &endptr, 10); |
| 357 | |
| 358 | /* |
| 359 | * Check that there was a valid number and it's 8-bit. If not, |
| 360 | * break out and the end check will take the default values. |
| 361 | */ |
| 362 | if ((*token == '\0') || (*endptr != '\0') || params[i] < 0 || params[i] > 255) { |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * If there were not enough tokens or a token was malformed (not an |
| 369 | * integer), it will end up here and the default parameters can be |
| 370 | * taken. |
| 371 | */ |
| 372 | if ((i != 3) || (token != NULL)) { |
| 373 | SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr); |
| 374 | memcpy(params, default_params, sizeof(params)); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | ftr->N_factor = params[0]; |
| 379 | ftr->r_factor = params[1]; |
| 380 | ftr->p_factor = params[2]; |
| 381 | } |
| 382 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 383 | static unsigned int get_fs_size(char *dev) |
| 384 | { |
| 385 | int fd, block_size; |
| 386 | struct ext4_super_block sb; |
| 387 | off64_t len; |
| 388 | |
| 389 | if ((fd = open(dev, O_RDONLY)) < 0) { |
| 390 | SLOGE("Cannot open device to get filesystem size "); |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | if (lseek64(fd, 1024, SEEK_SET) < 0) { |
| 395 | SLOGE("Cannot seek to superblock"); |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) { |
| 400 | SLOGE("Cannot read superblock"); |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | close(fd); |
| 405 | |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 406 | if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) { |
| 407 | SLOGE("Not a valid ext4 superblock"); |
| 408 | return 0; |
| 409 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 410 | block_size = 1024 << sb.s_log_block_size; |
| 411 | /* compute length in bytes */ |
| 412 | len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size; |
| 413 | |
| 414 | /* return length in sectors */ |
| 415 | return (unsigned int) (len / 512); |
| 416 | } |
| 417 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 418 | static int get_crypt_ftr_info(char **metadata_fname, off64_t *off) |
| 419 | { |
| 420 | static int cached_data = 0; |
| 421 | static off64_t cached_off = 0; |
| 422 | static char cached_metadata_fname[PROPERTY_VALUE_MAX] = ""; |
| 423 | int fd; |
| 424 | char key_loc[PROPERTY_VALUE_MAX]; |
| 425 | char real_blkdev[PROPERTY_VALUE_MAX]; |
| 426 | unsigned int nr_sec; |
| 427 | int rc = -1; |
| 428 | |
| 429 | if (!cached_data) { |
| 430 | fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc)); |
| 431 | |
| 432 | if (!strcmp(key_loc, KEY_IN_FOOTER)) { |
| 433 | if ( (fd = open(real_blkdev, O_RDWR)) < 0) { |
| 434 | SLOGE("Cannot open real block device %s\n", real_blkdev); |
| 435 | return -1; |
| 436 | } |
| 437 | |
| 438 | if ((nr_sec = get_blkdev_size(fd))) { |
| 439 | /* If it's an encrypted Android partition, the last 16 Kbytes contain the |
| 440 | * encryption info footer and key, and plenty of bytes to spare for future |
| 441 | * growth. |
| 442 | */ |
| 443 | strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname)); |
| 444 | cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET; |
| 445 | cached_data = 1; |
| 446 | } else { |
| 447 | SLOGE("Cannot get size of block device %s\n", real_blkdev); |
| 448 | } |
| 449 | close(fd); |
| 450 | } else { |
| 451 | strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname)); |
| 452 | cached_off = 0; |
| 453 | cached_data = 1; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | if (cached_data) { |
| 458 | if (metadata_fname) { |
| 459 | *metadata_fname = cached_metadata_fname; |
| 460 | } |
| 461 | if (off) { |
| 462 | *off = cached_off; |
| 463 | } |
| 464 | rc = 0; |
| 465 | } |
| 466 | |
| 467 | return rc; |
| 468 | } |
| 469 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 470 | /* 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] | 471 | * update the failed mount count but not change the key. |
| 472 | */ |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 473 | 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] | 474 | { |
| 475 | int fd; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 476 | unsigned int cnt; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 477 | /* starting_off is set to the SEEK_SET offset |
| 478 | * where the crypto structure starts |
| 479 | */ |
| 480 | off64_t starting_off; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 481 | int rc = -1; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 482 | char *fname = NULL; |
Ken Sumrall | 3be890f | 2011-09-14 16:53:46 -0700 | [diff] [blame] | 483 | struct stat statbuf; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 484 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 485 | if (get_crypt_ftr_info(&fname, &starting_off)) { |
| 486 | SLOGE("Unable to get crypt_ftr_info\n"); |
| 487 | return -1; |
| 488 | } |
| 489 | if (fname[0] != '/') { |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 490 | SLOGE("Unexpected value for crypto key location\n"); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 491 | return -1; |
| 492 | } |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 493 | if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) { |
| 494 | SLOGE("Cannot open footer file %s for put\n", fname); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 495 | return -1; |
| 496 | } |
| 497 | |
| 498 | /* Seek to the start of the crypt footer */ |
| 499 | if (lseek64(fd, starting_off, SEEK_SET) == -1) { |
| 500 | SLOGE("Cannot seek to real block device footer\n"); |
| 501 | goto errout; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) { |
| 505 | SLOGE("Cannot write real block device footer\n"); |
| 506 | goto errout; |
| 507 | } |
| 508 | |
Ken Sumrall | 3be890f | 2011-09-14 16:53:46 -0700 | [diff] [blame] | 509 | fstat(fd, &statbuf); |
| 510 | /* 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] | 511 | if (S_ISREG(statbuf.st_mode)) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 512 | if (ftruncate(fd, 0x4000)) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 513 | SLOGE("Cannot set footer file size\n"); |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 514 | goto errout; |
| 515 | } |
| 516 | } |
| 517 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 518 | /* Success! */ |
| 519 | rc = 0; |
| 520 | |
| 521 | errout: |
| 522 | close(fd); |
| 523 | return rc; |
| 524 | |
| 525 | } |
| 526 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 527 | static inline int unix_read(int fd, void* buff, int len) |
| 528 | { |
| 529 | return TEMP_FAILURE_RETRY(read(fd, buff, len)); |
| 530 | } |
| 531 | |
| 532 | static inline int unix_write(int fd, const void* buff, int len) |
| 533 | { |
| 534 | return TEMP_FAILURE_RETRY(write(fd, buff, len)); |
| 535 | } |
| 536 | |
| 537 | static void init_empty_persist_data(struct crypt_persist_data *pdata, int len) |
| 538 | { |
| 539 | memset(pdata, 0, len); |
| 540 | pdata->persist_magic = PERSIST_DATA_MAGIC; |
| 541 | pdata->persist_valid_entries = 0; |
| 542 | } |
| 543 | |
| 544 | /* A routine to update the passed in crypt_ftr to the lastest version. |
| 545 | * fd is open read/write on the device that holds the crypto footer and persistent |
| 546 | * data, crypt_ftr is a pointer to the struct to be updated, and offset is the |
| 547 | * absolute offset to the start of the crypt_mnt_ftr on the passed in fd. |
| 548 | */ |
| 549 | static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset) |
| 550 | { |
Kenny Root | 7434b31 | 2013-06-14 11:29:53 -0700 | [diff] [blame] | 551 | int orig_major = crypt_ftr->major_version; |
| 552 | int orig_minor = crypt_ftr->minor_version; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 553 | |
Kenny Root | 7434b31 | 2013-06-14 11:29:53 -0700 | [diff] [blame] | 554 | if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) { |
| 555 | struct crypt_persist_data *pdata; |
| 556 | off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 557 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 558 | SLOGW("upgrading crypto footer to 1.1"); |
| 559 | |
Kenny Root | 7434b31 | 2013-06-14 11:29:53 -0700 | [diff] [blame] | 560 | pdata = malloc(CRYPT_PERSIST_DATA_SIZE); |
| 561 | if (pdata == NULL) { |
| 562 | SLOGE("Cannot allocate persisent data\n"); |
| 563 | return; |
| 564 | } |
| 565 | memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE); |
| 566 | |
| 567 | /* Need to initialize the persistent data area */ |
| 568 | if (lseek64(fd, pdata_offset, SEEK_SET) == -1) { |
| 569 | SLOGE("Cannot seek to persisent data offset\n"); |
| 570 | return; |
| 571 | } |
| 572 | /* Write all zeros to the first copy, making it invalid */ |
| 573 | unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE); |
| 574 | |
| 575 | /* Write a valid but empty structure to the second copy */ |
| 576 | init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE); |
| 577 | unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE); |
| 578 | |
| 579 | /* Update the footer */ |
| 580 | crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE; |
| 581 | crypt_ftr->persist_data_offset[0] = pdata_offset; |
| 582 | crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE; |
| 583 | crypt_ftr->minor_version = 1; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 586 | if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) { |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 587 | SLOGW("upgrading crypto footer to 1.2"); |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 588 | /* But keep the old kdf_type. |
| 589 | * It will get updated later to KDF_SCRYPT after the password has been verified. |
| 590 | */ |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 591 | crypt_ftr->kdf_type = KDF_PBKDF2; |
| 592 | get_device_scrypt_params(crypt_ftr); |
| 593 | crypt_ftr->minor_version = 2; |
| 594 | } |
| 595 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 596 | if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) { |
| 597 | SLOGW("upgrading crypto footer to 1.3"); |
| 598 | crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD; |
| 599 | crypt_ftr->minor_version = 3; |
| 600 | } |
| 601 | |
Kenny Root | 7434b31 | 2013-06-14 11:29:53 -0700 | [diff] [blame] | 602 | if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) { |
| 603 | if (lseek64(fd, offset, SEEK_SET) == -1) { |
| 604 | SLOGE("Cannot seek to crypt footer\n"); |
| 605 | return; |
| 606 | } |
| 607 | unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr)); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 608 | } |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | |
| 612 | 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] | 613 | { |
| 614 | int fd; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 615 | unsigned int cnt; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 616 | off64_t starting_off; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 617 | int rc = -1; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 618 | char *fname = NULL; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 619 | struct stat statbuf; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 620 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 621 | if (get_crypt_ftr_info(&fname, &starting_off)) { |
| 622 | SLOGE("Unable to get crypt_ftr_info\n"); |
| 623 | return -1; |
| 624 | } |
| 625 | if (fname[0] != '/') { |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 626 | SLOGE("Unexpected value for crypto key location\n"); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 627 | return -1; |
| 628 | } |
| 629 | if ( (fd = open(fname, O_RDWR)) < 0) { |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 630 | SLOGE("Cannot open footer file %s for get\n", fname); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 631 | return -1; |
| 632 | } |
| 633 | |
| 634 | /* Make sure it's 16 Kbytes in length */ |
| 635 | fstat(fd, &statbuf); |
| 636 | if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) { |
| 637 | SLOGE("footer file %s is not the expected size!\n", fname); |
| 638 | goto errout; |
| 639 | } |
| 640 | |
| 641 | /* Seek to the start of the crypt footer */ |
| 642 | if (lseek64(fd, starting_off, SEEK_SET) == -1) { |
| 643 | SLOGE("Cannot seek to real block device footer\n"); |
| 644 | goto errout; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) { |
| 648 | SLOGE("Cannot read real block device footer\n"); |
| 649 | goto errout; |
| 650 | } |
| 651 | |
| 652 | if (crypt_ftr->magic != CRYPT_MNT_MAGIC) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 653 | SLOGE("Bad magic for real block device %s\n", fname); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 654 | goto errout; |
| 655 | } |
| 656 | |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 657 | if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) { |
| 658 | SLOGE("Cannot understand major version %d real block device footer; expected %d\n", |
| 659 | crypt_ftr->major_version, CURRENT_MAJOR_VERSION); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 660 | goto errout; |
| 661 | } |
| 662 | |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 663 | if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) { |
| 664 | SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n", |
| 665 | crypt_ftr->minor_version, CURRENT_MINOR_VERSION); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 666 | } |
| 667 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 668 | /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the |
| 669 | * copy on disk before returning. |
| 670 | */ |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 671 | if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 672 | upgrade_crypt_ftr(fd, crypt_ftr, starting_off); |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 675 | /* Success! */ |
| 676 | rc = 0; |
| 677 | |
| 678 | errout: |
| 679 | close(fd); |
| 680 | return rc; |
| 681 | } |
| 682 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 683 | static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr) |
| 684 | { |
| 685 | if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size > |
| 686 | crypt_ftr->persist_data_offset[1]) { |
| 687 | SLOGE("Crypt_ftr persist data regions overlap"); |
| 688 | return -1; |
| 689 | } |
| 690 | |
| 691 | if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) { |
| 692 | SLOGE("Crypt_ftr persist data region 0 starts after region 1"); |
| 693 | return -1; |
| 694 | } |
| 695 | |
| 696 | if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) - |
| 697 | (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) > |
| 698 | CRYPT_FOOTER_OFFSET) { |
| 699 | SLOGE("Persistent data extends past crypto footer"); |
| 700 | return -1; |
| 701 | } |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | static int load_persistent_data(void) |
| 707 | { |
| 708 | struct crypt_mnt_ftr crypt_ftr; |
| 709 | struct crypt_persist_data *pdata = NULL; |
| 710 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 711 | char *fname; |
| 712 | int found = 0; |
| 713 | int fd; |
| 714 | int ret; |
| 715 | int i; |
| 716 | |
| 717 | if (persist_data) { |
| 718 | /* Nothing to do, we've already loaded or initialized it */ |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | |
| 723 | /* If not encrypted, just allocate an empty table and initialize it */ |
| 724 | property_get("ro.crypto.state", encrypted_state, ""); |
| 725 | if (strcmp(encrypted_state, "encrypted") ) { |
| 726 | pdata = malloc(CRYPT_PERSIST_DATA_SIZE); |
| 727 | if (pdata) { |
| 728 | init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE); |
| 729 | persist_data = pdata; |
| 730 | return 0; |
| 731 | } |
| 732 | return -1; |
| 733 | } |
| 734 | |
| 735 | if(get_crypt_ftr_and_key(&crypt_ftr)) { |
| 736 | return -1; |
| 737 | } |
| 738 | |
Paul Lawrence | 8561b5c | 2014-03-17 14:10:51 -0700 | [diff] [blame] | 739 | if ((crypt_ftr.major_version < 1) |
| 740 | || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 741 | SLOGE("Crypt_ftr version doesn't support persistent data"); |
| 742 | return -1; |
| 743 | } |
| 744 | |
| 745 | if (get_crypt_ftr_info(&fname, NULL)) { |
| 746 | return -1; |
| 747 | } |
| 748 | |
| 749 | ret = validate_persistent_data_storage(&crypt_ftr); |
| 750 | if (ret) { |
| 751 | return -1; |
| 752 | } |
| 753 | |
| 754 | fd = open(fname, O_RDONLY); |
| 755 | if (fd < 0) { |
| 756 | SLOGE("Cannot open %s metadata file", fname); |
| 757 | return -1; |
| 758 | } |
| 759 | |
| 760 | if (persist_data == NULL) { |
| 761 | pdata = malloc(crypt_ftr.persist_data_size); |
| 762 | if (pdata == NULL) { |
| 763 | SLOGE("Cannot allocate memory for persistent data"); |
| 764 | goto err; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | for (i = 0; i < 2; i++) { |
| 769 | if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) { |
| 770 | SLOGE("Cannot seek to read persistent data on %s", fname); |
| 771 | goto err2; |
| 772 | } |
| 773 | if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){ |
| 774 | SLOGE("Error reading persistent data on iteration %d", i); |
| 775 | goto err2; |
| 776 | } |
| 777 | if (pdata->persist_magic == PERSIST_DATA_MAGIC) { |
| 778 | found = 1; |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | if (!found) { |
| 784 | SLOGI("Could not find valid persistent data, creating"); |
| 785 | init_empty_persist_data(pdata, crypt_ftr.persist_data_size); |
| 786 | } |
| 787 | |
| 788 | /* Success */ |
| 789 | persist_data = pdata; |
| 790 | close(fd); |
| 791 | return 0; |
| 792 | |
| 793 | err2: |
| 794 | free(pdata); |
| 795 | |
| 796 | err: |
| 797 | close(fd); |
| 798 | return -1; |
| 799 | } |
| 800 | |
| 801 | static int save_persistent_data(void) |
| 802 | { |
| 803 | struct crypt_mnt_ftr crypt_ftr; |
| 804 | struct crypt_persist_data *pdata; |
| 805 | char *fname; |
| 806 | off64_t write_offset; |
| 807 | off64_t erase_offset; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 808 | int fd; |
| 809 | int ret; |
| 810 | |
| 811 | if (persist_data == NULL) { |
| 812 | SLOGE("No persistent data to save"); |
| 813 | return -1; |
| 814 | } |
| 815 | |
| 816 | if(get_crypt_ftr_and_key(&crypt_ftr)) { |
| 817 | return -1; |
| 818 | } |
| 819 | |
Paul Lawrence | 8561b5c | 2014-03-17 14:10:51 -0700 | [diff] [blame] | 820 | if ((crypt_ftr.major_version < 1) |
| 821 | || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 822 | SLOGE("Crypt_ftr version doesn't support persistent data"); |
| 823 | return -1; |
| 824 | } |
| 825 | |
| 826 | ret = validate_persistent_data_storage(&crypt_ftr); |
| 827 | if (ret) { |
| 828 | return -1; |
| 829 | } |
| 830 | |
| 831 | if (get_crypt_ftr_info(&fname, NULL)) { |
| 832 | return -1; |
| 833 | } |
| 834 | |
| 835 | fd = open(fname, O_RDWR); |
| 836 | if (fd < 0) { |
| 837 | SLOGE("Cannot open %s metadata file", fname); |
| 838 | return -1; |
| 839 | } |
| 840 | |
| 841 | pdata = malloc(crypt_ftr.persist_data_size); |
| 842 | if (pdata == NULL) { |
| 843 | SLOGE("Cannot allocate persistant data"); |
| 844 | goto err; |
| 845 | } |
| 846 | |
| 847 | if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) { |
| 848 | SLOGE("Cannot seek to read persistent data on %s", fname); |
| 849 | goto err2; |
| 850 | } |
| 851 | |
| 852 | if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) { |
| 853 | SLOGE("Error reading persistent data before save"); |
| 854 | goto err2; |
| 855 | } |
| 856 | |
| 857 | if (pdata->persist_magic == PERSIST_DATA_MAGIC) { |
| 858 | /* The first copy is the curent valid copy, so write to |
| 859 | * the second copy and erase this one */ |
| 860 | write_offset = crypt_ftr.persist_data_offset[1]; |
| 861 | erase_offset = crypt_ftr.persist_data_offset[0]; |
| 862 | } else { |
| 863 | /* The second copy must be the valid copy, so write to |
| 864 | * the first copy, and erase the second */ |
| 865 | write_offset = crypt_ftr.persist_data_offset[0]; |
| 866 | erase_offset = crypt_ftr.persist_data_offset[1]; |
| 867 | } |
| 868 | |
| 869 | /* Write the new copy first, if successful, then erase the old copy */ |
| 870 | if (lseek(fd, write_offset, SEEK_SET) < 0) { |
| 871 | SLOGE("Cannot seek to write persistent data"); |
| 872 | goto err2; |
| 873 | } |
| 874 | if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) == |
| 875 | (int) crypt_ftr.persist_data_size) { |
| 876 | if (lseek(fd, erase_offset, SEEK_SET) < 0) { |
| 877 | SLOGE("Cannot seek to erase previous persistent data"); |
| 878 | goto err2; |
| 879 | } |
| 880 | fsync(fd); |
| 881 | memset(pdata, 0, crypt_ftr.persist_data_size); |
| 882 | if (unix_write(fd, pdata, crypt_ftr.persist_data_size) != |
| 883 | (int) crypt_ftr.persist_data_size) { |
| 884 | SLOGE("Cannot write to erase previous persistent data"); |
| 885 | goto err2; |
| 886 | } |
| 887 | fsync(fd); |
| 888 | } else { |
| 889 | SLOGE("Cannot write to save persistent data"); |
| 890 | goto err2; |
| 891 | } |
| 892 | |
| 893 | /* Success */ |
| 894 | free(pdata); |
| 895 | close(fd); |
| 896 | return 0; |
| 897 | |
| 898 | err2: |
| 899 | free(pdata); |
| 900 | err: |
| 901 | close(fd); |
| 902 | return -1; |
| 903 | } |
| 904 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 905 | static int hexdigit (char c) |
| 906 | { |
| 907 | if (c >= '0' && c <= '9') return c - '0'; |
| 908 | c = tolower(c); |
| 909 | if (c >= 'a' && c <= 'f') return c - 'a' + 10; |
| 910 | return -1; |
| 911 | } |
| 912 | |
| 913 | static unsigned char* convert_hex_ascii_to_key(const char* master_key_ascii, |
| 914 | unsigned int* out_keysize) |
| 915 | { |
| 916 | unsigned int i; |
| 917 | *out_keysize = 0; |
| 918 | |
| 919 | size_t size = strlen (master_key_ascii); |
| 920 | if (size % 2) { |
| 921 | SLOGE("Trying to convert ascii string of odd length"); |
| 922 | return NULL; |
| 923 | } |
| 924 | |
| 925 | unsigned char* master_key = (unsigned char*) malloc(size / 2); |
| 926 | if (master_key == 0) { |
| 927 | SLOGE("Cannot allocate"); |
| 928 | return NULL; |
| 929 | } |
| 930 | |
| 931 | for (i = 0; i < size; i += 2) { |
| 932 | int high_nibble = hexdigit (master_key_ascii[i]); |
| 933 | int low_nibble = hexdigit (master_key_ascii[i + 1]); |
| 934 | |
| 935 | if(high_nibble < 0 || low_nibble < 0) { |
| 936 | SLOGE("Invalid hex string"); |
| 937 | free (master_key); |
| 938 | return NULL; |
| 939 | } |
| 940 | |
| 941 | master_key[*out_keysize] = high_nibble * 16 + low_nibble; |
| 942 | (*out_keysize)++; |
| 943 | } |
| 944 | |
| 945 | return master_key; |
| 946 | } |
| 947 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 948 | /* Convert a binary key of specified length into an ascii hex string equivalent, |
| 949 | * without the leading 0x and with null termination |
| 950 | */ |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 951 | static void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize, |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 952 | char *master_key_ascii) |
| 953 | { |
| 954 | unsigned int i, a; |
| 955 | unsigned char nibble; |
| 956 | |
| 957 | for (i=0, a=0; i<keysize; i++, a+=2) { |
| 958 | /* For each byte, write out two ascii hex digits */ |
| 959 | nibble = (master_key[i] >> 4) & 0xf; |
| 960 | master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30); |
| 961 | |
| 962 | nibble = master_key[i] & 0xf; |
| 963 | master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30); |
| 964 | } |
| 965 | |
| 966 | /* Add the null termination */ |
| 967 | master_key_ascii[a] = '\0'; |
| 968 | |
| 969 | } |
| 970 | |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 971 | static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key, |
| 972 | char *real_blk_name, const char *name, int fd, |
| 973 | char *extra_params) |
| 974 | { |
Dan Albert | c07fa3f | 2014-12-18 10:00:55 -0800 | [diff] [blame] | 975 | _Alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE]; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 976 | struct dm_ioctl *io; |
| 977 | struct dm_target_spec *tgt; |
| 978 | char *crypt_params; |
| 979 | char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */ |
| 980 | int i; |
| 981 | |
| 982 | io = (struct dm_ioctl *) buffer; |
| 983 | |
| 984 | /* Load the mapping table for this device */ |
| 985 | tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)]; |
| 986 | |
| 987 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 988 | io->target_count = 1; |
| 989 | tgt->status = 0; |
| 990 | tgt->sector_start = 0; |
| 991 | tgt->length = crypt_ftr->fs_size; |
| 992 | strcpy(tgt->target_type, "crypt"); |
| 993 | |
| 994 | crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec); |
| 995 | convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii); |
| 996 | sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name, |
| 997 | master_key_ascii, real_blk_name, extra_params); |
| 998 | crypt_params += strlen(crypt_params) + 1; |
| 999 | crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */ |
| 1000 | tgt->next = crypt_params - buffer; |
| 1001 | |
| 1002 | for (i = 0; i < TABLE_LOAD_RETRIES; i++) { |
| 1003 | if (! ioctl(fd, DM_TABLE_LOAD, io)) { |
| 1004 | break; |
| 1005 | } |
| 1006 | usleep(500000); |
| 1007 | } |
| 1008 | |
| 1009 | if (i == TABLE_LOAD_RETRIES) { |
| 1010 | /* We failed to load the table, return an error */ |
| 1011 | return -1; |
| 1012 | } else { |
| 1013 | return i + 1; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | |
| 1018 | static int get_dm_crypt_version(int fd, const char *name, int *version) |
| 1019 | { |
| 1020 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 1021 | struct dm_ioctl *io; |
| 1022 | struct dm_target_versions *v; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1023 | |
| 1024 | io = (struct dm_ioctl *) buffer; |
| 1025 | |
| 1026 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1027 | |
| 1028 | if (ioctl(fd, DM_LIST_VERSIONS, io)) { |
| 1029 | return -1; |
| 1030 | } |
| 1031 | |
| 1032 | /* Iterate over the returned versions, looking for name of "crypt". |
| 1033 | * When found, get and return the version. |
| 1034 | */ |
| 1035 | v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)]; |
| 1036 | while (v->next) { |
| 1037 | if (! strcmp(v->name, "crypt")) { |
| 1038 | /* We found the crypt driver, return the version, and get out */ |
| 1039 | version[0] = v->version[0]; |
| 1040 | version[1] = v->version[1]; |
| 1041 | version[2] = v->version[2]; |
| 1042 | return 0; |
| 1043 | } |
| 1044 | v = (struct dm_target_versions *)(((char *)v) + v->next); |
| 1045 | } |
| 1046 | |
| 1047 | return -1; |
| 1048 | } |
| 1049 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1050 | static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key, |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1051 | char *real_blk_name, char *crypto_blk_name, const char *name) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1052 | { |
| 1053 | char buffer[DM_CRYPT_BUF_SIZE]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1054 | struct dm_ioctl *io; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1055 | unsigned int minor; |
| 1056 | int fd; |
| 1057 | int retval = -1; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1058 | int version[3]; |
| 1059 | char *extra_params; |
| 1060 | int load_count; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1061 | |
| 1062 | if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) { |
| 1063 | SLOGE("Cannot open device-mapper\n"); |
| 1064 | goto errout; |
| 1065 | } |
| 1066 | |
| 1067 | io = (struct dm_ioctl *) buffer; |
| 1068 | |
| 1069 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1070 | if (ioctl(fd, DM_DEV_CREATE, io)) { |
| 1071 | SLOGE("Cannot create dm-crypt device\n"); |
| 1072 | goto errout; |
| 1073 | } |
| 1074 | |
| 1075 | /* Get the device status, in particular, the name of it's device file */ |
| 1076 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1077 | if (ioctl(fd, DM_DEV_STATUS, io)) { |
| 1078 | SLOGE("Cannot retrieve dm-crypt device status\n"); |
| 1079 | goto errout; |
| 1080 | } |
| 1081 | minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00); |
| 1082 | snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor); |
| 1083 | |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1084 | extra_params = ""; |
| 1085 | if (! get_dm_crypt_version(fd, name, version)) { |
| 1086 | /* Support for allow_discards was added in version 1.11.0 */ |
| 1087 | if ((version[0] >= 2) || |
| 1088 | ((version[0] == 1) && (version[1] >= 11))) { |
| 1089 | extra_params = "1 allow_discards"; |
| 1090 | SLOGI("Enabling support for allow_discards in dmcrypt.\n"); |
| 1091 | } |
Ken Sumrall | e919efe | 2012-09-29 17:07:41 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1094 | load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name, |
| 1095 | fd, extra_params); |
| 1096 | if (load_count < 0) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1097 | SLOGE("Cannot load dm-crypt mapping table.\n"); |
| 1098 | goto errout; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1099 | } else if (load_count > 1) { |
| 1100 | SLOGI("Took %d tries to load dmcrypt table.\n", load_count); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | /* Resume this device to activate it */ |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 1104 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1105 | |
| 1106 | if (ioctl(fd, DM_DEV_SUSPEND, io)) { |
| 1107 | SLOGE("Cannot resume the dm-crypt device\n"); |
| 1108 | goto errout; |
| 1109 | } |
| 1110 | |
| 1111 | /* We made it here with no errors. Woot! */ |
| 1112 | retval = 0; |
| 1113 | |
| 1114 | errout: |
| 1115 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 1116 | |
| 1117 | return retval; |
| 1118 | } |
| 1119 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1120 | static int delete_crypto_blk_dev(char *name) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1121 | { |
| 1122 | int fd; |
| 1123 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 1124 | struct dm_ioctl *io; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1125 | int retval = -1; |
| 1126 | |
| 1127 | if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) { |
| 1128 | SLOGE("Cannot open device-mapper\n"); |
| 1129 | goto errout; |
| 1130 | } |
| 1131 | |
| 1132 | io = (struct dm_ioctl *) buffer; |
| 1133 | |
| 1134 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 1135 | if (ioctl(fd, DM_DEV_REMOVE, io)) { |
| 1136 | SLOGE("Cannot remove dm-crypt device\n"); |
| 1137 | goto errout; |
| 1138 | } |
| 1139 | |
| 1140 | /* We made it here with no errors. Woot! */ |
| 1141 | retval = 0; |
| 1142 | |
| 1143 | errout: |
| 1144 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 1145 | |
| 1146 | return retval; |
| 1147 | |
| 1148 | } |
| 1149 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1150 | static int pbkdf2(const char *passwd, const unsigned char *salt, |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1151 | unsigned char *ikey, void *params UNUSED) |
| 1152 | { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1153 | SLOGI("Using pbkdf2 for cryptfs KDF"); |
| 1154 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1155 | /* 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] | 1156 | unsigned int keysize; |
| 1157 | char* master_key = (char*)convert_hex_ascii_to_key(passwd, &keysize); |
| 1158 | if (!master_key) return -1; |
| 1159 | PKCS5_PBKDF2_HMAC_SHA1(master_key, keysize, salt, SALT_LEN, |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1160 | HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1161 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1162 | memset(master_key, 0, keysize); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1163 | free (master_key); |
| 1164 | return 0; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1167 | static int scrypt(const char *passwd, const unsigned char *salt, |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1168 | unsigned char *ikey, void *params) |
| 1169 | { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1170 | SLOGI("Using scrypt for cryptfs KDF"); |
| 1171 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1172 | struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params; |
| 1173 | |
| 1174 | int N = 1 << ftr->N_factor; |
| 1175 | int r = 1 << ftr->r_factor; |
| 1176 | int p = 1 << ftr->p_factor; |
| 1177 | |
| 1178 | /* 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] | 1179 | unsigned int keysize; |
| 1180 | unsigned char* master_key = convert_hex_ascii_to_key(passwd, &keysize); |
| 1181 | if (!master_key) return -1; |
| 1182 | crypto_scrypt(master_key, keysize, salt, SALT_LEN, N, r, p, ikey, |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1183 | KEY_LEN_BYTES + IV_LEN_BYTES); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1184 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1185 | memset(master_key, 0, keysize); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1186 | free (master_key); |
| 1187 | return 0; |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1190 | static int scrypt_keymaster(const char *passwd, const unsigned char *salt, |
| 1191 | unsigned char *ikey, void *params) |
| 1192 | { |
| 1193 | SLOGI("Using scrypt with keymaster for cryptfs KDF"); |
| 1194 | |
| 1195 | int rc; |
| 1196 | unsigned int key_size; |
| 1197 | size_t signature_size; |
| 1198 | unsigned char* signature; |
| 1199 | struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params; |
| 1200 | |
| 1201 | int N = 1 << ftr->N_factor; |
| 1202 | int r = 1 << ftr->r_factor; |
| 1203 | int p = 1 << ftr->p_factor; |
| 1204 | |
| 1205 | unsigned char* master_key = convert_hex_ascii_to_key(passwd, &key_size); |
| 1206 | if (!master_key) { |
| 1207 | SLOGE("Failed to convert passwd from hex"); |
| 1208 | return -1; |
| 1209 | } |
| 1210 | |
| 1211 | rc = crypto_scrypt(master_key, key_size, salt, SALT_LEN, |
| 1212 | N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES); |
| 1213 | memset(master_key, 0, key_size); |
| 1214 | free(master_key); |
| 1215 | |
| 1216 | if (rc) { |
| 1217 | SLOGE("scrypt failed"); |
| 1218 | return -1; |
| 1219 | } |
| 1220 | |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 1221 | if (keymaster_sign_object(ftr, ikey, KEY_LEN_BYTES + IV_LEN_BYTES, |
| 1222 | &signature, &signature_size)) { |
| 1223 | SLOGE("Signing failed"); |
| 1224 | return -1; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN, |
| 1228 | N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES); |
| 1229 | free(signature); |
| 1230 | |
| 1231 | if (rc) { |
| 1232 | SLOGE("scrypt failed"); |
| 1233 | return -1; |
| 1234 | } |
| 1235 | |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | static int encrypt_master_key(const char *passwd, const unsigned char *salt, |
| 1240 | const unsigned char *decrypted_master_key, |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1241 | unsigned char *encrypted_master_key, |
| 1242 | struct crypt_mnt_ftr *crypt_ftr) |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1243 | { |
| 1244 | unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */ |
| 1245 | EVP_CIPHER_CTX e_ctx; |
| 1246 | int encrypted_len, final_len; |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1247 | int rc = 0; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1248 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1249 | /* 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] | 1250 | get_device_scrypt_params(crypt_ftr); |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1251 | |
| 1252 | switch (crypt_ftr->kdf_type) { |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 1253 | case KDF_SCRYPT_KEYMASTER_UNPADDED: |
| 1254 | case KDF_SCRYPT_KEYMASTER_BADLY_PADDED: |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1255 | case KDF_SCRYPT_KEYMASTER: |
| 1256 | if (keymaster_create_key(crypt_ftr)) { |
| 1257 | SLOGE("keymaster_create_key failed"); |
| 1258 | return -1; |
| 1259 | } |
| 1260 | |
| 1261 | if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) { |
| 1262 | SLOGE("scrypt failed"); |
| 1263 | return -1; |
| 1264 | } |
| 1265 | break; |
| 1266 | |
| 1267 | case KDF_SCRYPT: |
| 1268 | if (scrypt(passwd, salt, ikey, crypt_ftr)) { |
| 1269 | SLOGE("scrypt failed"); |
| 1270 | return -1; |
| 1271 | } |
| 1272 | break; |
| 1273 | |
| 1274 | default: |
| 1275 | SLOGE("Invalid kdf_type"); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1276 | return -1; |
| 1277 | } |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1278 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1279 | /* Initialize the decryption engine */ |
Adam Langley | 889c4f1 | 2014-09-03 14:23:13 -0700 | [diff] [blame] | 1280 | EVP_CIPHER_CTX_init(&e_ctx); |
| 1281 | 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] | 1282 | SLOGE("EVP_EncryptInit failed\n"); |
| 1283 | return -1; |
| 1284 | } |
| 1285 | 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] | 1286 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1287 | /* Encrypt the master key */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1288 | if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len, |
| 1289 | decrypted_master_key, KEY_LEN_BYTES)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1290 | SLOGE("EVP_EncryptUpdate failed\n"); |
| 1291 | return -1; |
| 1292 | } |
Adam Langley | 889c4f1 | 2014-09-03 14:23:13 -0700 | [diff] [blame] | 1293 | 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] | 1294 | SLOGE("EVP_EncryptFinal failed\n"); |
| 1295 | return -1; |
| 1296 | } |
| 1297 | |
| 1298 | if (encrypted_len + final_len != KEY_LEN_BYTES) { |
| 1299 | SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len); |
| 1300 | return -1; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1301 | } |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1302 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1303 | /* Store the scrypt of the intermediate key, so we can validate if it's a |
| 1304 | password error or mount error when things go wrong. |
| 1305 | Note there's no need to check for errors, since if this is incorrect, we |
| 1306 | simply won't wipe userdata, which is the correct default behavior |
| 1307 | */ |
| 1308 | int N = 1 << crypt_ftr->N_factor; |
| 1309 | int r = 1 << crypt_ftr->r_factor; |
| 1310 | int p = 1 << crypt_ftr->p_factor; |
| 1311 | |
| 1312 | rc = crypto_scrypt(ikey, KEY_LEN_BYTES, |
| 1313 | crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p, |
| 1314 | crypt_ftr->scrypted_intermediate_key, |
| 1315 | sizeof(crypt_ftr->scrypted_intermediate_key)); |
| 1316 | |
| 1317 | if (rc) { |
| 1318 | SLOGE("encrypt_master_key: crypto_scrypt failed"); |
| 1319 | } |
| 1320 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1321 | return 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1324 | static int decrypt_master_key_aux(char *passwd, unsigned char *salt, |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1325 | unsigned char *encrypted_master_key, |
| 1326 | unsigned char *decrypted_master_key, |
| 1327 | kdf_func kdf, void *kdf_params, |
| 1328 | unsigned char** intermediate_key, |
| 1329 | size_t* intermediate_key_size) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1330 | { |
| 1331 | 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] | 1332 | EVP_CIPHER_CTX d_ctx; |
| 1333 | int decrypted_len, final_len; |
| 1334 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1335 | /* Turn the password into an intermediate key and IV that can decrypt the |
| 1336 | master key */ |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1337 | if (kdf(passwd, salt, ikey, kdf_params)) { |
| 1338 | SLOGE("kdf failed"); |
| 1339 | return -1; |
| 1340 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1341 | |
| 1342 | /* Initialize the decryption engine */ |
Adam Langley | 889c4f1 | 2014-09-03 14:23:13 -0700 | [diff] [blame] | 1343 | EVP_CIPHER_CTX_init(&d_ctx); |
| 1344 | 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] | 1345 | return -1; |
| 1346 | } |
| 1347 | EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */ |
| 1348 | /* Decrypt the master key */ |
| 1349 | if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len, |
| 1350 | encrypted_master_key, KEY_LEN_BYTES)) { |
| 1351 | return -1; |
| 1352 | } |
Adam Langley | 889c4f1 | 2014-09-03 14:23:13 -0700 | [diff] [blame] | 1353 | 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] | 1354 | return -1; |
| 1355 | } |
| 1356 | |
| 1357 | if (decrypted_len + final_len != KEY_LEN_BYTES) { |
| 1358 | return -1; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1359 | } |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1360 | |
| 1361 | /* Copy intermediate key if needed by params */ |
| 1362 | if (intermediate_key && intermediate_key_size) { |
| 1363 | *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES); |
| 1364 | if (intermediate_key) { |
| 1365 | memcpy(*intermediate_key, ikey, KEY_LEN_BYTES); |
| 1366 | *intermediate_key_size = KEY_LEN_BYTES; |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | return 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1371 | } |
| 1372 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1373 | 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] | 1374 | { |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 1375 | if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER_UNPADDED || |
| 1376 | ftr->kdf_type == KDF_SCRYPT_KEYMASTER_BADLY_PADDED || |
| 1377 | ftr->kdf_type == KDF_SCRYPT_KEYMASTER) { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1378 | *kdf = scrypt_keymaster; |
| 1379 | *kdf_params = ftr; |
| 1380 | } else if (ftr->kdf_type == KDF_SCRYPT) { |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1381 | *kdf = scrypt; |
| 1382 | *kdf_params = ftr; |
| 1383 | } else { |
| 1384 | *kdf = pbkdf2; |
| 1385 | *kdf_params = NULL; |
| 1386 | } |
| 1387 | } |
| 1388 | |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1389 | static int decrypt_master_key(char *passwd, unsigned char *decrypted_master_key, |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1390 | struct crypt_mnt_ftr *crypt_ftr, |
| 1391 | unsigned char** intermediate_key, |
| 1392 | size_t* intermediate_key_size) |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1393 | { |
| 1394 | kdf_func kdf; |
| 1395 | void *kdf_params; |
| 1396 | int ret; |
| 1397 | |
| 1398 | get_kdf_func(crypt_ftr, &kdf, &kdf_params); |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1399 | ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key, |
| 1400 | decrypted_master_key, kdf, kdf_params, |
| 1401 | intermediate_key, intermediate_key_size); |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1402 | if (ret != 0) { |
| 1403 | SLOGW("failure decrypting master key"); |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | return ret; |
| 1407 | } |
| 1408 | |
| 1409 | static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt, |
| 1410 | struct crypt_mnt_ftr *crypt_ftr) { |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1411 | int fd; |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1412 | unsigned char key_buf[KEY_LEN_BYTES]; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1413 | |
| 1414 | /* Get some random bits for a key */ |
| 1415 | fd = open("/dev/urandom", O_RDONLY); |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1416 | read(fd, key_buf, sizeof(key_buf)); |
| 1417 | read(fd, salt, SALT_LEN); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1418 | close(fd); |
| 1419 | |
| 1420 | /* Now encrypt it with the password */ |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1421 | return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1422 | } |
| 1423 | |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 1424 | static int wait_and_unmount(char *mountpoint, bool kill) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1425 | { |
Greg Hackmann | 955653e | 2014-09-24 14:55:20 -0700 | [diff] [blame] | 1426 | int i, err, rc; |
Ken Sumrall | 2eaf713 | 2011-01-14 12:45:48 -0800 | [diff] [blame] | 1427 | #define WAIT_UNMOUNT_COUNT 20 |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1428 | |
| 1429 | /* Now umount the tmpfs filesystem */ |
| 1430 | for (i=0; i<WAIT_UNMOUNT_COUNT; i++) { |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 1431 | if (umount(mountpoint) == 0) { |
| 1432 | break; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1433 | } |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 1434 | |
| 1435 | if (errno == EINVAL) { |
| 1436 | /* EINVAL is returned if the directory is not a mountpoint, |
| 1437 | * i.e. there is no filesystem mounted there. So just get out. |
| 1438 | */ |
| 1439 | break; |
| 1440 | } |
| 1441 | |
| 1442 | err = errno; |
| 1443 | |
| 1444 | /* If allowed, be increasingly aggressive before the last two retries */ |
| 1445 | if (kill) { |
| 1446 | if (i == (WAIT_UNMOUNT_COUNT - 3)) { |
| 1447 | SLOGW("sending SIGHUP to processes with open files\n"); |
| 1448 | vold_killProcessesWithOpenFiles(mountpoint, 1); |
| 1449 | } else if (i == (WAIT_UNMOUNT_COUNT - 2)) { |
| 1450 | SLOGW("sending SIGKILL to processes with open files\n"); |
| 1451 | vold_killProcessesWithOpenFiles(mountpoint, 2); |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | sleep(1); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | if (i < WAIT_UNMOUNT_COUNT) { |
| 1459 | SLOGD("unmounting %s succeeded\n", mountpoint); |
| 1460 | rc = 0; |
| 1461 | } else { |
jessica_yu | 3f14fe4 | 2014-09-22 15:57:40 +0800 | [diff] [blame] | 1462 | vold_killProcessesWithOpenFiles(mountpoint, 0); |
Greg Hackmann | 955653e | 2014-09-24 14:55:20 -0700 | [diff] [blame] | 1463 | SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1464 | rc = -1; |
| 1465 | } |
| 1466 | |
| 1467 | return rc; |
| 1468 | } |
| 1469 | |
Ken Sumrall | c587269 | 2013-05-14 15:26:31 -0700 | [diff] [blame] | 1470 | #define DATA_PREP_TIMEOUT 200 |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1471 | static int prep_data_fs(void) |
| 1472 | { |
| 1473 | int i; |
| 1474 | |
| 1475 | /* Do the prep of the /data filesystem */ |
| 1476 | property_set("vold.post_fs_data_done", "0"); |
| 1477 | property_set("vold.decrypt", "trigger_post_fs_data"); |
| 1478 | SLOGD("Just triggered post_fs_data\n"); |
| 1479 | |
Ken Sumrall | c587269 | 2013-05-14 15:26:31 -0700 | [diff] [blame] | 1480 | /* Wait a max of 50 seconds, hopefully it takes much less */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1481 | for (i=0; i<DATA_PREP_TIMEOUT; i++) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1482 | char p[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1483 | |
| 1484 | property_get("vold.post_fs_data_done", p, "0"); |
| 1485 | if (*p == '1') { |
| 1486 | break; |
| 1487 | } else { |
| 1488 | usleep(250000); |
| 1489 | } |
| 1490 | } |
| 1491 | if (i == DATA_PREP_TIMEOUT) { |
| 1492 | /* Ugh, we failed to prep /data in time. Bail. */ |
Ken Sumrall | c587269 | 2013-05-14 15:26:31 -0700 | [diff] [blame] | 1493 | SLOGE("post_fs_data timed out!\n"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1494 | return -1; |
| 1495 | } else { |
| 1496 | SLOGD("post_fs_data done\n"); |
| 1497 | return 0; |
| 1498 | } |
| 1499 | } |
| 1500 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1501 | static void cryptfs_set_corrupt() |
| 1502 | { |
| 1503 | // Mark the footer as bad |
| 1504 | struct crypt_mnt_ftr crypt_ftr; |
| 1505 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
| 1506 | SLOGE("Failed to get crypto footer - panic"); |
| 1507 | return; |
| 1508 | } |
| 1509 | |
| 1510 | crypt_ftr.flags |= CRYPT_DATA_CORRUPT; |
| 1511 | if (put_crypt_ftr_and_key(&crypt_ftr)) { |
| 1512 | SLOGE("Failed to set crypto footer - panic"); |
| 1513 | return; |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | static void cryptfs_trigger_restart_min_framework() |
| 1518 | { |
| 1519 | if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) { |
| 1520 | SLOGE("Failed to mount tmpfs on data - panic"); |
| 1521 | return; |
| 1522 | } |
| 1523 | |
| 1524 | if (property_set("vold.decrypt", "trigger_post_fs_data")) { |
| 1525 | SLOGE("Failed to trigger post fs data - panic"); |
| 1526 | return; |
| 1527 | } |
| 1528 | |
| 1529 | if (property_set("vold.decrypt", "trigger_restart_min_framework")) { |
| 1530 | SLOGE("Failed to trigger restart min framework - panic"); |
| 1531 | return; |
| 1532 | } |
| 1533 | } |
| 1534 | |
Paul Lawrence | 8e3f451 | 2014-09-08 10:11:17 -0700 | [diff] [blame] | 1535 | /* returns < 0 on failure */ |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1536 | static int cryptfs_restart_internal(int restart_main) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1537 | { |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 1538 | char crypto_blkdev[MAXPATHLEN]; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 1539 | int rc = -1; |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1540 | static int restart_successful = 0; |
| 1541 | |
| 1542 | /* Validate that it's OK to call this routine */ |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1543 | if (! master_key_saved) { |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1544 | SLOGE("Encrypted filesystem not validated, aborting"); |
| 1545 | return -1; |
| 1546 | } |
| 1547 | |
| 1548 | if (restart_successful) { |
| 1549 | SLOGE("System already restarted with encrypted disk, aborting"); |
| 1550 | return -1; |
| 1551 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1552 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1553 | if (restart_main) { |
| 1554 | /* Here is where we shut down the framework. The init scripts |
| 1555 | * start all services in one of three classes: core, main or late_start. |
| 1556 | * On boot, we start core and main. Now, we stop main, but not core, |
| 1557 | * as core includes vold and a few other really important things that |
| 1558 | * we need to keep running. Once main has stopped, we should be able |
| 1559 | * to umount the tmpfs /data, then mount the encrypted /data. |
| 1560 | * We then restart the class main, and also the class late_start. |
| 1561 | * At the moment, I've only put a few things in late_start that I know |
| 1562 | * are not needed to bring up the framework, and that also cause problems |
| 1563 | * with unmounting the tmpfs /data, but I hope to add add more services |
| 1564 | * to the late_start class as we optimize this to decrease the delay |
| 1565 | * till the user is asked for the password to the filesystem. |
| 1566 | */ |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1567 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1568 | /* The init files are setup to stop the class main when vold.decrypt is |
| 1569 | * set to trigger_reset_main. |
| 1570 | */ |
| 1571 | property_set("vold.decrypt", "trigger_reset_main"); |
| 1572 | SLOGD("Just asked init to shut down class main\n"); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1573 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1574 | /* Ugh, shutting down the framework is not synchronous, so until it |
| 1575 | * can be fixed, this horrible hack will wait a moment for it all to |
| 1576 | * shut down before proceeding. Without it, some devices cannot |
| 1577 | * restart the graphics services. |
| 1578 | */ |
| 1579 | sleep(2); |
| 1580 | } |
Ken Sumrall | 9dedfd4 | 2012-10-09 14:16:59 -0700 | [diff] [blame] | 1581 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1582 | /* Now that the framework is shutdown, we should be able to umount() |
| 1583 | * the tmpfs filesystem, and mount the real one. |
| 1584 | */ |
| 1585 | |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 1586 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, ""); |
| 1587 | if (strlen(crypto_blkdev) == 0) { |
| 1588 | SLOGE("fs_crypto_blkdev not set\n"); |
| 1589 | return -1; |
| 1590 | } |
| 1591 | |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 1592 | if (! (rc = wait_and_unmount(DATA_MNT_POINT, true)) ) { |
Doug Zongker | 6fd5771 | 2013-12-17 09:43:23 -0800 | [diff] [blame] | 1593 | /* If ro.crypto.readonly is set to 1, mount the decrypted |
| 1594 | * filesystem readonly. This is used when /data is mounted by |
| 1595 | * recovery mode. |
| 1596 | */ |
| 1597 | char ro_prop[PROPERTY_VALUE_MAX]; |
| 1598 | property_get("ro.crypto.readonly", ro_prop, ""); |
| 1599 | if (strlen(ro_prop) > 0 && atoi(ro_prop)) { |
| 1600 | struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT); |
| 1601 | rec->flags |= MS_RDONLY; |
| 1602 | } |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 1603 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1604 | /* If that succeeded, then mount the decrypted filesystem */ |
Paul Lawrence | 8e3f451 | 2014-09-08 10:11:17 -0700 | [diff] [blame] | 1605 | int retries = RETRY_MOUNT_ATTEMPTS; |
| 1606 | int mount_rc; |
| 1607 | while ((mount_rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT, |
| 1608 | crypto_blkdev, 0)) |
| 1609 | != 0) { |
| 1610 | if (mount_rc == FS_MGR_DOMNT_BUSY) { |
| 1611 | /* TODO: invoke something similar to |
| 1612 | Process::killProcessWithOpenFiles(DATA_MNT_POINT, |
| 1613 | retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */ |
| 1614 | SLOGI("Failed to mount %s because it is busy - waiting", |
| 1615 | crypto_blkdev); |
| 1616 | if (--retries) { |
| 1617 | sleep(RETRY_MOUNT_DELAY_SECONDS); |
| 1618 | } else { |
| 1619 | /* Let's hope that a reboot clears away whatever is keeping |
| 1620 | the mount busy */ |
| 1621 | cryptfs_reboot(reboot); |
| 1622 | } |
| 1623 | } else { |
| 1624 | SLOGE("Failed to mount decrypted data"); |
| 1625 | cryptfs_set_corrupt(); |
| 1626 | cryptfs_trigger_restart_min_framework(); |
| 1627 | SLOGI("Started framework to offer wipe"); |
| 1628 | return -1; |
| 1629 | } |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1630 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1631 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1632 | property_set("vold.decrypt", "trigger_load_persist_props"); |
| 1633 | /* Create necessary paths on /data */ |
| 1634 | if (prep_data_fs()) { |
| 1635 | return -1; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1636 | } |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1637 | |
| 1638 | /* startup service classes main and late_start */ |
| 1639 | property_set("vold.decrypt", "trigger_restart_framework"); |
| 1640 | SLOGD("Just triggered restart_framework\n"); |
| 1641 | |
| 1642 | /* Give it a few moments to get started */ |
| 1643 | sleep(1); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1646 | if (rc == 0) { |
| 1647 | restart_successful = 1; |
| 1648 | } |
| 1649 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1650 | return rc; |
| 1651 | } |
| 1652 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1653 | int cryptfs_restart(void) |
| 1654 | { |
| 1655 | /* Call internal implementation forcing a restart of main service group */ |
| 1656 | return cryptfs_restart_internal(1); |
| 1657 | } |
| 1658 | |
Mark Salyzyn | 3e97127 | 2014-01-21 13:27:04 -0800 | [diff] [blame] | 1659 | static int do_crypto_complete(char *mount_point UNUSED) |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1660 | { |
| 1661 | struct crypt_mnt_ftr crypt_ftr; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1662 | char encrypted_state[PROPERTY_VALUE_MAX]; |
Ken Sumrall | e1a4585 | 2011-12-14 21:24:27 -0800 | [diff] [blame] | 1663 | char key_loc[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1664 | |
| 1665 | property_get("ro.crypto.state", encrypted_state, ""); |
| 1666 | if (strcmp(encrypted_state, "encrypted") ) { |
| 1667 | SLOGE("not running with encryption, aborting"); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1668 | return CRYPTO_COMPLETE_NOT_ENCRYPTED; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1669 | } |
| 1670 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 1671 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 1672 | fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc)); |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1673 | |
Ken Sumrall | e1a4585 | 2011-12-14 21:24:27 -0800 | [diff] [blame] | 1674 | /* |
| 1675 | * Only report this error if key_loc is a file and it exists. |
| 1676 | * If the device was never encrypted, and /data is not mountable for |
| 1677 | * some reason, returning 1 should prevent the UI from presenting the |
| 1678 | * a "enter password" screen, or worse, a "press button to wipe the |
| 1679 | * device" screen. |
| 1680 | */ |
| 1681 | if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) { |
| 1682 | SLOGE("master key file does not exist, aborting"); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1683 | return CRYPTO_COMPLETE_NOT_ENCRYPTED; |
Ken Sumrall | e1a4585 | 2011-12-14 21:24:27 -0800 | [diff] [blame] | 1684 | } else { |
| 1685 | SLOGE("Error getting crypt footer and key\n"); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1686 | return CRYPTO_COMPLETE_BAD_METADATA; |
Ken Sumrall | e1a4585 | 2011-12-14 21:24:27 -0800 | [diff] [blame] | 1687 | } |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1688 | } |
| 1689 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1690 | // Test for possible error flags |
| 1691 | if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS){ |
| 1692 | SLOGE("Encryption process is partway completed\n"); |
| 1693 | return CRYPTO_COMPLETE_PARTIAL; |
| 1694 | } |
| 1695 | |
| 1696 | if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE){ |
| 1697 | SLOGE("Encryption process was interrupted but cannot continue\n"); |
| 1698 | return CRYPTO_COMPLETE_INCONSISTENT; |
| 1699 | } |
| 1700 | |
| 1701 | if (crypt_ftr.flags & CRYPT_DATA_CORRUPT){ |
| 1702 | SLOGE("Encryption is successful but data is corrupt\n"); |
| 1703 | return CRYPTO_COMPLETE_CORRUPT; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | /* We passed the test! We shall diminish, and return to the west */ |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1707 | return CRYPTO_COMPLETE_ENCRYPTED; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1708 | } |
| 1709 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1710 | static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr, |
| 1711 | char *passwd, char *mount_point, char *label) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1712 | { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1713 | /* 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] | 1714 | unsigned char decrypted_master_key[32]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1715 | char crypto_blkdev[MAXPATHLEN]; |
| 1716 | char real_blkdev[MAXPATHLEN]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1717 | char tmp_mount_point[64]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1718 | unsigned int orig_failed_decrypt_count; |
| 1719 | int rc; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1720 | int use_keymaster = 0; |
| 1721 | int upgrade = 0; |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1722 | unsigned char* intermediate_key = 0; |
| 1723 | size_t intermediate_key_size = 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1724 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1725 | SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size); |
| 1726 | orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count; |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1727 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1728 | if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) { |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1729 | if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr, |
| 1730 | &intermediate_key, &intermediate_key_size)) { |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1731 | SLOGE("Failed to decrypt master key\n"); |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1732 | rc = -1; |
| 1733 | goto errout; |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1734 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1735 | } |
| 1736 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1737 | fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev)); |
| 1738 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1739 | // Create crypto block device - all (non fatal) code paths |
| 1740 | // need it |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1741 | if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, |
| 1742 | real_blkdev, crypto_blkdev, label)) { |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1743 | SLOGE("Error creating decrypted block device\n"); |
| 1744 | rc = -1; |
| 1745 | goto errout; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1746 | } |
| 1747 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1748 | /* Work out if the problem is the password or the data */ |
| 1749 | unsigned char scrypted_intermediate_key[sizeof(crypt_ftr-> |
| 1750 | scrypted_intermediate_key)]; |
| 1751 | int N = 1 << crypt_ftr->N_factor; |
| 1752 | int r = 1 << crypt_ftr->r_factor; |
| 1753 | int p = 1 << crypt_ftr->p_factor; |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1754 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1755 | rc = crypto_scrypt(intermediate_key, intermediate_key_size, |
| 1756 | crypt_ftr->salt, sizeof(crypt_ftr->salt), |
| 1757 | N, r, p, scrypted_intermediate_key, |
| 1758 | sizeof(scrypted_intermediate_key)); |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1759 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1760 | // Does the key match the crypto footer? |
| 1761 | if (rc == 0 && memcmp(scrypted_intermediate_key, |
| 1762 | crypt_ftr->scrypted_intermediate_key, |
| 1763 | sizeof(scrypted_intermediate_key)) == 0) { |
| 1764 | SLOGI("Password matches"); |
| 1765 | rc = 0; |
| 1766 | } else { |
| 1767 | /* Try mounting the file system anyway, just in case the problem's with |
| 1768 | * the footer, not the key. */ |
| 1769 | sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point); |
| 1770 | mkdir(tmp_mount_point, 0755); |
| 1771 | if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) { |
| 1772 | SLOGE("Error temp mounting decrypted block device\n"); |
| 1773 | delete_crypto_blk_dev(label); |
| 1774 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1775 | rc = ++crypt_ftr->failed_decrypt_count; |
| 1776 | put_crypt_ftr_and_key(crypt_ftr); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1777 | } else { |
| 1778 | /* Success! */ |
| 1779 | SLOGI("Password did not match but decrypted drive mounted - continue"); |
| 1780 | umount(tmp_mount_point); |
| 1781 | rc = 0; |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1782 | } |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | if (rc == 0) { |
| 1786 | crypt_ftr->failed_decrypt_count = 0; |
Paul Lawrence | 72b8b82 | 2014-10-05 12:57:37 -0700 | [diff] [blame] | 1787 | if (orig_failed_decrypt_count != 0) { |
| 1788 | put_crypt_ftr_and_key(crypt_ftr); |
| 1789 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1790 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1791 | /* Save the name of the crypto block device |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1792 | * so we can mount it when restarting the framework. */ |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 1793 | property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1794 | |
| 1795 | /* Also save a the master key so we can reencrypted the key |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1796 | * the key when we want to change the password on it. */ |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1797 | memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES); |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 1798 | saved_mount_point = strdup(mount_point); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1799 | master_key_saved = 1; |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1800 | SLOGD("%s(): Master key saved\n", __FUNCTION__); |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 1801 | rc = 0; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1802 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1803 | // Upgrade if we're not using the latest KDF. |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1804 | use_keymaster = keymaster_check_compatibility(); |
| 1805 | if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) { |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 1806 | // Don't allow downgrade |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1807 | } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) { |
| 1808 | crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER; |
| 1809 | upgrade = 1; |
| 1810 | } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1811 | crypt_ftr->kdf_type = KDF_SCRYPT; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1812 | upgrade = 1; |
| 1813 | } |
| 1814 | |
| 1815 | if (upgrade) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1816 | rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key, |
| 1817 | crypt_ftr->master_key, crypt_ftr); |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1818 | if (!rc) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1819 | rc = put_crypt_ftr_and_key(crypt_ftr); |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1820 | } |
| 1821 | SLOGD("Key Derivation Function upgrade: rc=%d\n", rc); |
Paul Lawrence | b2f682b | 2014-09-08 11:28:19 -0700 | [diff] [blame] | 1822 | |
| 1823 | // Do not fail even if upgrade failed - machine is bootable |
| 1824 | // Note that if this code is ever hit, there is a *serious* problem |
| 1825 | // since KDFs should never fail. You *must* fix the kdf before |
| 1826 | // proceeding! |
| 1827 | if (rc) { |
| 1828 | SLOGW("Upgrade failed with error %d," |
| 1829 | " but continuing with previous state", |
| 1830 | rc); |
| 1831 | rc = 0; |
| 1832 | } |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1833 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1834 | } |
| 1835 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1836 | errout: |
| 1837 | if (intermediate_key) { |
| 1838 | memset(intermediate_key, 0, intermediate_key_size); |
| 1839 | free(intermediate_key); |
| 1840 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1841 | return rc; |
| 1842 | } |
| 1843 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 1844 | /* Called by vold when it wants to undo the crypto mapping of a volume it |
| 1845 | * manages. This is usually in response to a factory reset, when we want |
| 1846 | * to undo the crypto mapping so the volume is formatted in the clear. |
| 1847 | */ |
| 1848 | int cryptfs_revert_volume(const char *label) |
| 1849 | { |
| 1850 | return delete_crypto_blk_dev((char *)label); |
| 1851 | } |
| 1852 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1853 | /* |
| 1854 | * Called by vold when it's asked to mount an encrypted, nonremovable volume. |
| 1855 | * Setup a dm-crypt mapping, use the saved master key from |
| 1856 | * setting up the /data mapping, and return the new device path. |
| 1857 | */ |
| 1858 | int cryptfs_setup_volume(const char *label, int major, int minor, |
| 1859 | char *crypto_sys_path, unsigned int max_path, |
| 1860 | int *new_major, int *new_minor) |
| 1861 | { |
| 1862 | char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN]; |
| 1863 | struct crypt_mnt_ftr sd_crypt_ftr; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1864 | struct stat statbuf; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 1865 | unsigned int nr_sec; |
| 1866 | int fd; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1867 | |
| 1868 | sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor); |
| 1869 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 1870 | get_crypt_ftr_and_key(&sd_crypt_ftr); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1871 | |
| 1872 | /* Update the fs_size field to be the size of the volume */ |
| 1873 | fd = open(real_blkdev, O_RDONLY); |
| 1874 | nr_sec = get_blkdev_size(fd); |
| 1875 | close(fd); |
| 1876 | if (nr_sec == 0) { |
| 1877 | SLOGE("Cannot get size of volume %s\n", real_blkdev); |
| 1878 | return -1; |
| 1879 | } |
| 1880 | |
| 1881 | sd_crypt_ftr.fs_size = nr_sec; |
| 1882 | create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev, |
| 1883 | crypto_blkdev, label); |
| 1884 | |
| 1885 | stat(crypto_blkdev, &statbuf); |
| 1886 | *new_major = MAJOR(statbuf.st_rdev); |
| 1887 | *new_minor = MINOR(statbuf.st_rdev); |
| 1888 | |
| 1889 | /* Create path to sys entry for this block device */ |
| 1890 | snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1); |
| 1891 | |
| 1892 | return 0; |
| 1893 | } |
| 1894 | |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1895 | int cryptfs_crypto_complete(void) |
| 1896 | { |
| 1897 | return do_crypto_complete("/data"); |
| 1898 | } |
| 1899 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1900 | int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr) |
| 1901 | { |
| 1902 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 1903 | property_get("ro.crypto.state", encrypted_state, ""); |
| 1904 | if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) { |
| 1905 | SLOGE("encrypted fs already validated or not running with encryption," |
| 1906 | " aborting"); |
| 1907 | return -1; |
| 1908 | } |
| 1909 | |
| 1910 | if (get_crypt_ftr_and_key(crypt_ftr)) { |
| 1911 | SLOGE("Error getting crypt footer and key"); |
| 1912 | return -1; |
| 1913 | } |
| 1914 | |
| 1915 | return 0; |
| 1916 | } |
| 1917 | |
Paul Lawrence | fc61504 | 2014-10-04 15:32:29 -0700 | [diff] [blame] | 1918 | /* |
| 1919 | * TODO - transition patterns to new format in calling code |
| 1920 | * and remove this vile hack, and the use of hex in |
| 1921 | * the password passing code. |
| 1922 | * |
| 1923 | * Patterns are passed in zero based (i.e. the top left dot |
| 1924 | * is represented by zero, the top middle one etc), but we want |
| 1925 | * to store them '1' based. |
| 1926 | * This is to allow us to migrate the calling code to use this |
| 1927 | * convention. It also solves a nasty problem whereby scrypt ignores |
| 1928 | * trailing zeros, so patterns ending at the top left could be |
| 1929 | * truncated, and similarly, you could add the top left to any |
| 1930 | * pattern and still match. |
| 1931 | * adjust_passwd is a hack function that returns the alternate representation |
| 1932 | * if the password appears to be a pattern (hex numbers all less than 09) |
| 1933 | * If it succeeds we need to try both, and in particular try the alternate |
| 1934 | * first. If the original matches, then we need to update the footer |
| 1935 | * with the alternate. |
| 1936 | * All code that accepts passwords must adjust them first. Since |
| 1937 | * cryptfs_check_passwd is always the first function called after a migration |
| 1938 | * (and indeed on any boot) we only need to do the double try in this |
| 1939 | * function. |
| 1940 | */ |
| 1941 | char* adjust_passwd(const char* passwd) |
| 1942 | { |
| 1943 | size_t index, length; |
| 1944 | |
| 1945 | if (!passwd) { |
| 1946 | return 0; |
| 1947 | } |
| 1948 | |
| 1949 | // Check even length. Hex encoded passwords are always |
| 1950 | // an even length, since each character encodes to two characters. |
| 1951 | length = strlen(passwd); |
| 1952 | if (length % 2) { |
| 1953 | SLOGW("Password not correctly hex encoded."); |
| 1954 | return 0; |
| 1955 | } |
| 1956 | |
| 1957 | // Check password is old-style pattern - a collection of hex |
| 1958 | // encoded bytes less than 9 (00 through 08) |
| 1959 | for (index = 0; index < length; index +=2) { |
| 1960 | if (passwd[index] != '0' |
| 1961 | || passwd[index + 1] < '0' || passwd[index + 1] > '8') { |
| 1962 | return 0; |
| 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | // Allocate room for adjusted passwd and null terminate |
| 1967 | char* adjusted = malloc(length + 1); |
| 1968 | adjusted[length] = 0; |
| 1969 | |
| 1970 | // Add 0x31 ('1') to each character |
| 1971 | for (index = 0; index < length; index += 2) { |
| 1972 | // output is 31 through 39 so set first byte to three, second to src + 1 |
| 1973 | adjusted[index] = '3'; |
| 1974 | adjusted[index + 1] = passwd[index + 1] + 1; |
| 1975 | } |
| 1976 | |
| 1977 | return adjusted; |
| 1978 | } |
| 1979 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1980 | int cryptfs_check_passwd(char *passwd) |
| 1981 | { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1982 | struct crypt_mnt_ftr crypt_ftr; |
| 1983 | int rc; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1984 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1985 | rc = check_unmounted_and_get_ftr(&crypt_ftr); |
| 1986 | if (rc) |
| 1987 | return rc; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1988 | |
Paul Lawrence | fc61504 | 2014-10-04 15:32:29 -0700 | [diff] [blame] | 1989 | char* adjusted_passwd = adjust_passwd(passwd); |
| 1990 | if (adjusted_passwd) { |
| 1991 | int failed_decrypt_count = crypt_ftr.failed_decrypt_count; |
| 1992 | rc = test_mount_encrypted_fs(&crypt_ftr, adjusted_passwd, |
| 1993 | DATA_MNT_POINT, "userdata"); |
| 1994 | |
| 1995 | // Maybe the original one still works? |
| 1996 | if (rc) { |
| 1997 | // Don't double count this failure |
| 1998 | crypt_ftr.failed_decrypt_count = failed_decrypt_count; |
| 1999 | rc = test_mount_encrypted_fs(&crypt_ftr, passwd, |
| 2000 | DATA_MNT_POINT, "userdata"); |
| 2001 | if (!rc) { |
| 2002 | // cryptfs_changepw also adjusts so pass original |
| 2003 | // Note that adjust_passwd only recognises patterns |
| 2004 | // so we can safely use CRYPT_TYPE_PATTERN |
| 2005 | SLOGI("Updating pattern to new format"); |
| 2006 | cryptfs_changepw(CRYPT_TYPE_PATTERN, passwd); |
| 2007 | } |
| 2008 | } |
| 2009 | free(adjusted_passwd); |
| 2010 | } else { |
| 2011 | rc = test_mount_encrypted_fs(&crypt_ftr, passwd, |
| 2012 | DATA_MNT_POINT, "userdata"); |
| 2013 | } |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 2014 | |
| 2015 | if (rc == 0 && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) { |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 2016 | cryptfs_clear_password(); |
| 2017 | password = strdup(passwd); |
| 2018 | struct timespec now; |
| 2019 | clock_gettime(CLOCK_BOOTTIME, &now); |
| 2020 | password_expiry_time = now.tv_sec + password_max_age_seconds; |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 2021 | } |
| 2022 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2023 | return rc; |
| 2024 | } |
| 2025 | |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 2026 | int cryptfs_verify_passwd(char *passwd) |
| 2027 | { |
| 2028 | struct crypt_mnt_ftr crypt_ftr; |
| 2029 | /* 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] | 2030 | unsigned char decrypted_master_key[32]; |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 2031 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 2032 | int rc; |
| 2033 | |
| 2034 | property_get("ro.crypto.state", encrypted_state, ""); |
| 2035 | if (strcmp(encrypted_state, "encrypted") ) { |
| 2036 | SLOGE("device not encrypted, aborting"); |
| 2037 | return -2; |
| 2038 | } |
| 2039 | |
| 2040 | if (!master_key_saved) { |
| 2041 | SLOGE("encrypted fs not yet mounted, aborting"); |
| 2042 | return -1; |
| 2043 | } |
| 2044 | |
| 2045 | if (!saved_mount_point) { |
| 2046 | SLOGE("encrypted fs failed to save mount point, aborting"); |
| 2047 | return -1; |
| 2048 | } |
| 2049 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2050 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 2051 | SLOGE("Error getting crypt footer and key\n"); |
| 2052 | return -1; |
| 2053 | } |
| 2054 | |
| 2055 | if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) { |
| 2056 | /* If the device has no password, then just say the password is valid */ |
| 2057 | rc = 0; |
| 2058 | } else { |
Paul Lawrence | fc61504 | 2014-10-04 15:32:29 -0700 | [diff] [blame] | 2059 | char* adjusted_passwd = adjust_passwd(passwd); |
| 2060 | if (adjusted_passwd) { |
| 2061 | passwd = adjusted_passwd; |
| 2062 | } |
| 2063 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 2064 | decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0); |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 2065 | if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) { |
| 2066 | /* They match, the password is correct */ |
| 2067 | rc = 0; |
| 2068 | } else { |
| 2069 | /* If incorrect, sleep for a bit to prevent dictionary attacks */ |
| 2070 | sleep(1); |
| 2071 | rc = 1; |
| 2072 | } |
Paul Lawrence | fc61504 | 2014-10-04 15:32:29 -0700 | [diff] [blame] | 2073 | |
| 2074 | free(adjusted_passwd); |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | return rc; |
| 2078 | } |
| 2079 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2080 | /* Initialize a crypt_mnt_ftr structure. The keysize is |
| 2081 | * defaulted to 16 bytes, and the filesystem size to 0. |
| 2082 | * Presumably, at a minimum, the caller will update the |
| 2083 | * filesystem size and crypto_type_name after calling this function. |
| 2084 | */ |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 2085 | static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2086 | { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2087 | off64_t off; |
| 2088 | |
| 2089 | memset(ftr, 0, sizeof(struct crypt_mnt_ftr)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2090 | ftr->magic = CRYPT_MNT_MAGIC; |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 2091 | ftr->major_version = CURRENT_MAJOR_VERSION; |
| 2092 | ftr->minor_version = CURRENT_MINOR_VERSION; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2093 | ftr->ftr_size = sizeof(struct crypt_mnt_ftr); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 2094 | ftr->keysize = KEY_LEN_BYTES; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2095 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 2096 | switch (keymaster_check_compatibility()) { |
| 2097 | case 1: |
| 2098 | ftr->kdf_type = KDF_SCRYPT_KEYMASTER; |
| 2099 | break; |
| 2100 | |
| 2101 | case 0: |
| 2102 | ftr->kdf_type = KDF_SCRYPT; |
| 2103 | break; |
| 2104 | |
| 2105 | default: |
| 2106 | SLOGE("keymaster_check_compatibility failed"); |
| 2107 | return -1; |
| 2108 | } |
| 2109 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 2110 | get_device_scrypt_params(ftr); |
| 2111 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2112 | ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE; |
| 2113 | if (get_crypt_ftr_info(NULL, &off) == 0) { |
| 2114 | ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET; |
| 2115 | ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET + |
| 2116 | ftr->persist_data_size; |
| 2117 | } |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 2118 | |
| 2119 | return 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2120 | } |
| 2121 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2122 | 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] | 2123 | { |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 2124 | const char *args[10]; |
| 2125 | char size_str[32]; /* Must be large enough to hold a %lld and null byte */ |
| 2126 | int num_args; |
| 2127 | int status; |
| 2128 | int tmp; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2129 | int rc = -1; |
| 2130 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2131 | if (type == EXT4_FS) { |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 2132 | args[0] = "/system/bin/make_ext4fs"; |
| 2133 | args[1] = "-a"; |
| 2134 | args[2] = "/data"; |
| 2135 | args[3] = "-l"; |
Elliott Hughes | 7373716 | 2014-06-25 17:27:42 -0700 | [diff] [blame] | 2136 | snprintf(size_str, sizeof(size_str), "%" PRId64, size * 512); |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 2137 | args[4] = size_str; |
| 2138 | args[5] = crypto_blkdev; |
| 2139 | num_args = 6; |
| 2140 | SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n", |
| 2141 | args[0], args[1], args[2], args[3], args[4], args[5]); |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 2142 | } else if (type == F2FS_FS) { |
| 2143 | args[0] = "/system/bin/mkfs.f2fs"; |
| 2144 | args[1] = "-t"; |
| 2145 | args[2] = "-d1"; |
| 2146 | args[3] = crypto_blkdev; |
Elliott Hughes | 7373716 | 2014-06-25 17:27:42 -0700 | [diff] [blame] | 2147 | snprintf(size_str, sizeof(size_str), "%" PRId64, size); |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 2148 | args[4] = size_str; |
| 2149 | num_args = 5; |
| 2150 | SLOGI("Making empty filesystem with command %s %s %s %s %s\n", |
| 2151 | args[0], args[1], args[2], args[3], args[4]); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2152 | } else { |
| 2153 | SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type); |
| 2154 | return -1; |
| 2155 | } |
| 2156 | |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 2157 | tmp = android_fork_execvp(num_args, (char **)args, &status, false, true); |
| 2158 | |
| 2159 | if (tmp != 0) { |
| 2160 | 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] | 2161 | } else { |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 2162 | if (WIFEXITED(status)) { |
| 2163 | if (WEXITSTATUS(status)) { |
| 2164 | SLOGE("Error creating filesystem on %s, exit status %d ", |
| 2165 | crypto_blkdev, WEXITSTATUS(status)); |
| 2166 | } else { |
| 2167 | SLOGD("Successfully created filesystem on %s\n", crypto_blkdev); |
| 2168 | rc = 0; |
| 2169 | } |
| 2170 | } else { |
| 2171 | SLOGE("Error creating filesystem on %s, did not exit normally\n", crypto_blkdev); |
| 2172 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | return rc; |
| 2176 | } |
| 2177 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2178 | #define CRYPT_INPLACE_BUFSIZE 4096 |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2179 | #define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / CRYPT_SECTOR_SIZE) |
| 2180 | #define CRYPT_SECTOR_SIZE 512 |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2181 | |
| 2182 | /* aligned 32K writes tends to make flash happy. |
| 2183 | * SD card association recommends it. |
| 2184 | */ |
| 2185 | #define BLOCKS_AT_A_TIME 8 |
| 2186 | |
| 2187 | struct encryptGroupsData |
| 2188 | { |
| 2189 | int realfd; |
| 2190 | int cryptofd; |
| 2191 | off64_t numblocks; |
| 2192 | off64_t one_pct, cur_pct, new_pct; |
| 2193 | off64_t blocks_already_done, tot_numblocks; |
Paul Lawrence | 58c58cf | 2014-06-04 13:12:21 -0700 | [diff] [blame] | 2194 | off64_t used_blocks_already_done, tot_used_blocks; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2195 | char* real_blkdev, * crypto_blkdev; |
| 2196 | int count; |
| 2197 | off64_t offset; |
| 2198 | char* buffer; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2199 | off64_t last_written_sector; |
| 2200 | int completed; |
Paul Lawrence | a96d9c9 | 2014-06-04 14:05:01 -0700 | [diff] [blame] | 2201 | time_t time_started; |
| 2202 | int remaining_time; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2203 | }; |
| 2204 | |
Paul Lawrence | 58c58cf | 2014-06-04 13:12:21 -0700 | [diff] [blame] | 2205 | static void update_progress(struct encryptGroupsData* data, int is_used) |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2206 | { |
| 2207 | data->blocks_already_done++; |
Paul Lawrence | 58c58cf | 2014-06-04 13:12:21 -0700 | [diff] [blame] | 2208 | |
| 2209 | if (is_used) { |
| 2210 | data->used_blocks_already_done++; |
| 2211 | } |
Paul Lawrence | 58c58cf | 2014-06-04 13:12:21 -0700 | [diff] [blame] | 2212 | if (data->tot_used_blocks) { |
| 2213 | data->new_pct = data->used_blocks_already_done / data->one_pct; |
| 2214 | } else { |
| 2215 | data->new_pct = data->blocks_already_done / data->one_pct; |
| 2216 | } |
| 2217 | |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2218 | if (data->new_pct > data->cur_pct) { |
| 2219 | char buf[8]; |
| 2220 | data->cur_pct = data->new_pct; |
Elliott Hughes | cb33f57 | 2014-06-25 18:25:11 -0700 | [diff] [blame] | 2221 | snprintf(buf, sizeof(buf), "%" PRId64, data->cur_pct); |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2222 | property_set("vold.encrypt_progress", buf); |
| 2223 | } |
Paul Lawrence | a96d9c9 | 2014-06-04 14:05:01 -0700 | [diff] [blame] | 2224 | |
| 2225 | if (data->cur_pct >= 5) { |
Paul Lawrence | 9c58a87 | 2014-09-30 09:12:51 -0700 | [diff] [blame] | 2226 | struct timespec time_now; |
| 2227 | if (clock_gettime(CLOCK_MONOTONIC, &time_now)) { |
| 2228 | SLOGW("Error getting time"); |
| 2229 | } else { |
| 2230 | double elapsed_time = difftime(time_now.tv_sec, data->time_started); |
| 2231 | off64_t remaining_blocks = data->tot_used_blocks |
| 2232 | - data->used_blocks_already_done; |
| 2233 | int remaining_time = (int)(elapsed_time * remaining_blocks |
| 2234 | / data->used_blocks_already_done); |
Paul Lawrence | 7157750 | 2014-08-13 14:55:55 -0700 | [diff] [blame] | 2235 | |
Paul Lawrence | 9c58a87 | 2014-09-30 09:12:51 -0700 | [diff] [blame] | 2236 | // Change time only if not yet set, lower, or a lot higher for |
| 2237 | // best user experience |
| 2238 | if (data->remaining_time == -1 |
| 2239 | || remaining_time < data->remaining_time |
| 2240 | || remaining_time > data->remaining_time + 60) { |
| 2241 | char buf[8]; |
| 2242 | snprintf(buf, sizeof(buf), "%d", remaining_time); |
| 2243 | property_set("vold.encrypt_time_remaining", buf); |
| 2244 | data->remaining_time = remaining_time; |
| 2245 | } |
Paul Lawrence | a96d9c9 | 2014-06-04 14:05:01 -0700 | [diff] [blame] | 2246 | } |
| 2247 | } |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2248 | } |
| 2249 | |
Paul Lawrence | 3846be1 | 2014-09-22 11:33:54 -0700 | [diff] [blame] | 2250 | static void log_progress(struct encryptGroupsData const* data, bool completed) |
| 2251 | { |
| 2252 | // Precondition - if completed data = 0 else data != 0 |
| 2253 | |
| 2254 | // Track progress so we can skip logging blocks |
| 2255 | static off64_t offset = -1; |
| 2256 | |
| 2257 | // Need to close existing 'Encrypting from' log? |
| 2258 | if (completed || (offset != -1 && data->offset != offset)) { |
| 2259 | SLOGI("Encrypted to sector %" PRId64, |
| 2260 | offset / info.block_size * CRYPT_SECTOR_SIZE); |
| 2261 | offset = -1; |
| 2262 | } |
| 2263 | |
| 2264 | // Need to start new 'Encrypting from' log? |
| 2265 | if (!completed && offset != data->offset) { |
| 2266 | SLOGI("Encrypting from sector %" PRId64, |
| 2267 | data->offset / info.block_size * CRYPT_SECTOR_SIZE); |
| 2268 | } |
| 2269 | |
| 2270 | // Update offset |
| 2271 | if (!completed) { |
| 2272 | offset = data->offset + (off64_t)data->count * info.block_size; |
| 2273 | } |
| 2274 | } |
| 2275 | |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2276 | static int flush_outstanding_data(struct encryptGroupsData* data) |
| 2277 | { |
| 2278 | if (data->count == 0) { |
| 2279 | return 0; |
| 2280 | } |
| 2281 | |
Elliott Hughes | 231bdba | 2014-06-25 18:36:19 -0700 | [diff] [blame] | 2282 | SLOGV("Copying %d blocks at offset %" PRIx64, data->count, data->offset); |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2283 | |
| 2284 | if (pread64(data->realfd, data->buffer, |
| 2285 | info.block_size * data->count, data->offset) |
| 2286 | <= 0) { |
| 2287 | SLOGE("Error reading real_blkdev %s for inplace encrypt", |
| 2288 | data->real_blkdev); |
| 2289 | return -1; |
| 2290 | } |
| 2291 | |
| 2292 | if (pwrite64(data->cryptofd, data->buffer, |
| 2293 | info.block_size * data->count, data->offset) |
| 2294 | <= 0) { |
| 2295 | SLOGE("Error writing crypto_blkdev %s for inplace encrypt", |
| 2296 | data->crypto_blkdev); |
| 2297 | return -1; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2298 | } else { |
Paul Lawrence | 3846be1 | 2014-09-22 11:33:54 -0700 | [diff] [blame] | 2299 | log_progress(data, false); |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | data->count = 0; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2303 | data->last_written_sector = (data->offset + data->count) |
| 2304 | / info.block_size * CRYPT_SECTOR_SIZE - 1; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2305 | return 0; |
| 2306 | } |
| 2307 | |
| 2308 | static int encrypt_groups(struct encryptGroupsData* data) |
| 2309 | { |
| 2310 | unsigned int i; |
| 2311 | u8 *block_bitmap = 0; |
| 2312 | unsigned int block; |
| 2313 | off64_t ret; |
| 2314 | int rc = -1; |
| 2315 | |
| 2316 | data->buffer = malloc(info.block_size * BLOCKS_AT_A_TIME); |
| 2317 | if (!data->buffer) { |
| 2318 | SLOGE("Failed to allocate crypto buffer"); |
| 2319 | goto errout; |
| 2320 | } |
| 2321 | |
| 2322 | block_bitmap = malloc(info.block_size); |
| 2323 | if (!block_bitmap) { |
| 2324 | SLOGE("failed to allocate block bitmap"); |
| 2325 | goto errout; |
| 2326 | } |
| 2327 | |
| 2328 | for (i = 0; i < aux_info.groups; ++i) { |
| 2329 | SLOGI("Encrypting group %d", i); |
| 2330 | |
| 2331 | u32 first_block = aux_info.first_data_block + i * info.blocks_per_group; |
| 2332 | u32 block_count = min(info.blocks_per_group, |
| 2333 | aux_info.len_blocks - first_block); |
| 2334 | |
| 2335 | off64_t offset = (u64)info.block_size |
| 2336 | * aux_info.bg_desc[i].bg_block_bitmap; |
| 2337 | |
| 2338 | ret = pread64(data->realfd, block_bitmap, info.block_size, offset); |
| 2339 | if (ret != (int)info.block_size) { |
| 2340 | SLOGE("failed to read all of block group bitmap %d", i); |
| 2341 | goto errout; |
| 2342 | } |
| 2343 | |
| 2344 | offset = (u64)info.block_size * first_block; |
| 2345 | |
| 2346 | data->count = 0; |
| 2347 | |
| 2348 | for (block = 0; block < block_count; block++) { |
Paul Lawrence | 58c58cf | 2014-06-04 13:12:21 -0700 | [diff] [blame] | 2349 | int used = bitmap_get_bit(block_bitmap, block); |
| 2350 | update_progress(data, used); |
| 2351 | if (used) { |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2352 | if (data->count == 0) { |
| 2353 | data->offset = offset; |
| 2354 | } |
| 2355 | data->count++; |
| 2356 | } else { |
| 2357 | if (flush_outstanding_data(data)) { |
| 2358 | goto errout; |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | offset += info.block_size; |
| 2363 | |
| 2364 | /* Write data if we are aligned or buffer size reached */ |
| 2365 | if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0 |
| 2366 | || data->count == BLOCKS_AT_A_TIME) { |
| 2367 | if (flush_outstanding_data(data)) { |
| 2368 | goto errout; |
| 2369 | } |
| 2370 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2371 | |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 2372 | if (!is_battery_ok_to_continue()) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2373 | SLOGE("Stopping encryption due to low battery"); |
| 2374 | rc = 0; |
| 2375 | goto errout; |
| 2376 | } |
| 2377 | |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2378 | } |
| 2379 | if (flush_outstanding_data(data)) { |
| 2380 | goto errout; |
| 2381 | } |
| 2382 | } |
| 2383 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2384 | data->completed = 1; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2385 | rc = 0; |
| 2386 | |
| 2387 | errout: |
Paul Lawrence | 3846be1 | 2014-09-22 11:33:54 -0700 | [diff] [blame] | 2388 | log_progress(0, true); |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2389 | free(data->buffer); |
| 2390 | free(block_bitmap); |
| 2391 | return rc; |
| 2392 | } |
| 2393 | |
| 2394 | static int cryptfs_enable_inplace_ext4(char *crypto_blkdev, |
| 2395 | char *real_blkdev, |
| 2396 | off64_t size, |
| 2397 | off64_t *size_already_done, |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2398 | off64_t tot_size, |
| 2399 | off64_t previously_encrypted_upto) |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2400 | { |
Paul Lawrence | 58c58cf | 2014-06-04 13:12:21 -0700 | [diff] [blame] | 2401 | u32 i; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2402 | struct encryptGroupsData data; |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 2403 | int rc; // Can't initialize without causing warning -Wclobbered |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2404 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2405 | if (previously_encrypted_upto > *size_already_done) { |
| 2406 | SLOGD("Not fast encrypting since resuming part way through"); |
| 2407 | return -1; |
| 2408 | } |
| 2409 | |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2410 | memset(&data, 0, sizeof(data)); |
| 2411 | data.real_blkdev = real_blkdev; |
| 2412 | data.crypto_blkdev = crypto_blkdev; |
| 2413 | |
| 2414 | if ( (data.realfd = open(real_blkdev, O_RDWR)) < 0) { |
JP Abgrall | 7776871 | 2014-10-10 15:52:11 -0700 | [diff] [blame] | 2415 | SLOGE("Error opening real_blkdev %s for inplace encrypt. err=%d(%s)\n", |
| 2416 | real_blkdev, errno, strerror(errno)); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 2417 | rc = -1; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2418 | goto errout; |
| 2419 | } |
| 2420 | |
| 2421 | if ( (data.cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) { |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2422 | SLOGE("Error opening crypto_blkdev %s for ext4 inplace encrypt. err=%d(%s)\n", |
JP Abgrall | 7776871 | 2014-10-10 15:52:11 -0700 | [diff] [blame] | 2423 | crypto_blkdev, errno, strerror(errno)); |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2424 | rc = ENABLE_INPLACE_ERR_DEV; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2425 | goto errout; |
| 2426 | } |
| 2427 | |
| 2428 | if (setjmp(setjmp_env)) { |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2429 | SLOGE("Reading ext4 extent caused an exception\n"); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 2430 | rc = -1; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2431 | goto errout; |
| 2432 | } |
| 2433 | |
| 2434 | if (read_ext(data.realfd, 0) != 0) { |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2435 | SLOGE("Failed to read ext4 extent\n"); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 2436 | rc = -1; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2437 | goto errout; |
| 2438 | } |
| 2439 | |
| 2440 | data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE; |
| 2441 | data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE; |
| 2442 | data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE; |
| 2443 | |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2444 | SLOGI("Encrypting ext4 filesystem in place..."); |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2445 | |
Paul Lawrence | 58c58cf | 2014-06-04 13:12:21 -0700 | [diff] [blame] | 2446 | data.tot_used_blocks = data.numblocks; |
| 2447 | for (i = 0; i < aux_info.groups; ++i) { |
| 2448 | data.tot_used_blocks -= aux_info.bg_desc[i].bg_free_blocks_count; |
| 2449 | } |
| 2450 | |
| 2451 | data.one_pct = data.tot_used_blocks / 100; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2452 | data.cur_pct = 0; |
Paul Lawrence | 9c58a87 | 2014-09-30 09:12:51 -0700 | [diff] [blame] | 2453 | |
| 2454 | struct timespec time_started = {0}; |
| 2455 | if (clock_gettime(CLOCK_MONOTONIC, &time_started)) { |
| 2456 | SLOGW("Error getting time at start"); |
| 2457 | // Note - continue anyway - we'll run with 0 |
| 2458 | } |
| 2459 | data.time_started = time_started.tv_sec; |
Paul Lawrence | a96d9c9 | 2014-06-04 14:05:01 -0700 | [diff] [blame] | 2460 | data.remaining_time = -1; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2461 | |
| 2462 | rc = encrypt_groups(&data); |
| 2463 | if (rc) { |
| 2464 | SLOGE("Error encrypting groups"); |
| 2465 | goto errout; |
| 2466 | } |
| 2467 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2468 | *size_already_done += data.completed ? size : data.last_written_sector; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2469 | rc = 0; |
| 2470 | |
| 2471 | errout: |
| 2472 | close(data.realfd); |
| 2473 | close(data.cryptofd); |
| 2474 | |
| 2475 | return rc; |
| 2476 | } |
| 2477 | |
Paul Lawrence | 3846be1 | 2014-09-22 11:33:54 -0700 | [diff] [blame] | 2478 | static void log_progress_f2fs(u64 block, bool completed) |
| 2479 | { |
| 2480 | // Precondition - if completed data = 0 else data != 0 |
| 2481 | |
| 2482 | // Track progress so we can skip logging blocks |
| 2483 | static u64 last_block = (u64)-1; |
| 2484 | |
| 2485 | // Need to close existing 'Encrypting from' log? |
| 2486 | if (completed || (last_block != (u64)-1 && block != last_block + 1)) { |
| 2487 | SLOGI("Encrypted to block %" PRId64, last_block); |
| 2488 | last_block = -1; |
| 2489 | } |
| 2490 | |
| 2491 | // Need to start new 'Encrypting from' log? |
| 2492 | if (!completed && (last_block == (u64)-1 || block != last_block + 1)) { |
| 2493 | SLOGI("Encrypting from block %" PRId64, block); |
| 2494 | } |
| 2495 | |
| 2496 | // Update offset |
| 2497 | if (!completed) { |
| 2498 | last_block = block; |
| 2499 | } |
| 2500 | } |
| 2501 | |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2502 | static int encrypt_one_block_f2fs(u64 pos, void *data) |
| 2503 | { |
| 2504 | struct encryptGroupsData *priv_dat = (struct encryptGroupsData *)data; |
| 2505 | |
| 2506 | priv_dat->blocks_already_done = pos - 1; |
| 2507 | update_progress(priv_dat, 1); |
| 2508 | |
| 2509 | off64_t offset = pos * CRYPT_INPLACE_BUFSIZE; |
| 2510 | |
| 2511 | if (pread64(priv_dat->realfd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) { |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2512 | SLOGE("Error reading real_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev); |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2513 | return -1; |
| 2514 | } |
| 2515 | |
| 2516 | if (pwrite64(priv_dat->cryptofd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) { |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2517 | SLOGE("Error writing crypto_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev); |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2518 | return -1; |
| 2519 | } else { |
Paul Lawrence | 3846be1 | 2014-09-22 11:33:54 -0700 | [diff] [blame] | 2520 | log_progress_f2fs(pos, false); |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
| 2523 | return 0; |
| 2524 | } |
| 2525 | |
| 2526 | static int cryptfs_enable_inplace_f2fs(char *crypto_blkdev, |
| 2527 | char *real_blkdev, |
| 2528 | off64_t size, |
| 2529 | off64_t *size_already_done, |
| 2530 | off64_t tot_size, |
| 2531 | off64_t previously_encrypted_upto) |
| 2532 | { |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2533 | struct encryptGroupsData data; |
| 2534 | struct f2fs_info *f2fs_info = NULL; |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2535 | int rc = ENABLE_INPLACE_ERR_OTHER; |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2536 | if (previously_encrypted_upto > *size_already_done) { |
| 2537 | SLOGD("Not fast encrypting since resuming part way through"); |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2538 | return ENABLE_INPLACE_ERR_OTHER; |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2539 | } |
| 2540 | memset(&data, 0, sizeof(data)); |
| 2541 | data.real_blkdev = real_blkdev; |
| 2542 | data.crypto_blkdev = crypto_blkdev; |
| 2543 | data.realfd = -1; |
| 2544 | data.cryptofd = -1; |
| 2545 | if ( (data.realfd = open64(real_blkdev, O_RDWR)) < 0) { |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2546 | SLOGE("Error opening real_blkdev %s for f2fs inplace encrypt\n", |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2547 | real_blkdev); |
| 2548 | goto errout; |
| 2549 | } |
| 2550 | if ( (data.cryptofd = open64(crypto_blkdev, O_WRONLY)) < 0) { |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2551 | SLOGE("Error opening crypto_blkdev %s for f2fs inplace encrypt. err=%d(%s)\n", |
JP Abgrall | 7776871 | 2014-10-10 15:52:11 -0700 | [diff] [blame] | 2552 | crypto_blkdev, errno, strerror(errno)); |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2553 | rc = ENABLE_INPLACE_ERR_DEV; |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2554 | goto errout; |
| 2555 | } |
| 2556 | |
| 2557 | f2fs_info = generate_f2fs_info(data.realfd); |
| 2558 | if (!f2fs_info) |
| 2559 | goto errout; |
| 2560 | |
| 2561 | data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE; |
| 2562 | data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE; |
| 2563 | data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE; |
| 2564 | |
| 2565 | data.tot_used_blocks = get_num_blocks_used(f2fs_info); |
| 2566 | |
| 2567 | data.one_pct = data.tot_used_blocks / 100; |
| 2568 | data.cur_pct = 0; |
| 2569 | data.time_started = time(NULL); |
| 2570 | data.remaining_time = -1; |
| 2571 | |
| 2572 | data.buffer = malloc(f2fs_info->block_size); |
| 2573 | if (!data.buffer) { |
| 2574 | SLOGE("Failed to allocate crypto buffer"); |
| 2575 | goto errout; |
| 2576 | } |
| 2577 | |
| 2578 | data.count = 0; |
| 2579 | |
| 2580 | /* Currently, this either runs to completion, or hits a nonrecoverable error */ |
| 2581 | rc = run_on_used_blocks(data.blocks_already_done, f2fs_info, &encrypt_one_block_f2fs, &data); |
| 2582 | |
| 2583 | if (rc) { |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2584 | SLOGE("Error in running over f2fs blocks"); |
| 2585 | rc = ENABLE_INPLACE_ERR_OTHER; |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2586 | goto errout; |
| 2587 | } |
| 2588 | |
| 2589 | *size_already_done += size; |
| 2590 | rc = 0; |
| 2591 | |
| 2592 | errout: |
| 2593 | if (rc) |
| 2594 | SLOGE("Failed to encrypt f2fs filesystem on %s", real_blkdev); |
| 2595 | |
Paul Lawrence | 3846be1 | 2014-09-22 11:33:54 -0700 | [diff] [blame] | 2596 | log_progress_f2fs(0, true); |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2597 | free(f2fs_info); |
| 2598 | free(data.buffer); |
| 2599 | close(data.realfd); |
| 2600 | close(data.cryptofd); |
| 2601 | |
| 2602 | return rc; |
| 2603 | } |
| 2604 | |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2605 | static int cryptfs_enable_inplace_full(char *crypto_blkdev, char *real_blkdev, |
| 2606 | off64_t size, off64_t *size_already_done, |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2607 | off64_t tot_size, |
| 2608 | off64_t previously_encrypted_upto) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2609 | { |
| 2610 | int realfd, cryptofd; |
| 2611 | char *buf[CRYPT_INPLACE_BUFSIZE]; |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2612 | int rc = ENABLE_INPLACE_ERR_OTHER; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2613 | off64_t numblocks, i, remainder; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2614 | off64_t one_pct, cur_pct, new_pct; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2615 | off64_t blocks_already_done, tot_numblocks; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2616 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2617 | if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) { |
| 2618 | SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev); |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2619 | return ENABLE_INPLACE_ERR_OTHER; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) { |
JP Abgrall | 7776871 | 2014-10-10 15:52:11 -0700 | [diff] [blame] | 2623 | SLOGE("Error opening crypto_blkdev %s for inplace encrypt. err=%d(%s)\n", |
| 2624 | crypto_blkdev, errno, strerror(errno)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2625 | close(realfd); |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2626 | return ENABLE_INPLACE_ERR_DEV; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | /* This is pretty much a simple loop of reading 4K, and writing 4K. |
| 2630 | * The size passed in is the number of 512 byte sectors in the filesystem. |
| 2631 | * So compute the number of whole 4K blocks we should read/write, |
| 2632 | * and the remainder. |
| 2633 | */ |
| 2634 | numblocks = size / CRYPT_SECTORS_PER_BUFSIZE; |
| 2635 | remainder = size % CRYPT_SECTORS_PER_BUFSIZE; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2636 | tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE; |
| 2637 | blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2638 | |
| 2639 | SLOGE("Encrypting filesystem in place..."); |
| 2640 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2641 | i = previously_encrypted_upto + 1 - *size_already_done; |
| 2642 | |
| 2643 | if (lseek64(realfd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) { |
| 2644 | SLOGE("Cannot seek to previously encrypted point on %s", real_blkdev); |
| 2645 | goto errout; |
| 2646 | } |
| 2647 | |
| 2648 | if (lseek64(cryptofd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) { |
| 2649 | SLOGE("Cannot seek to previously encrypted point on %s", crypto_blkdev); |
| 2650 | goto errout; |
| 2651 | } |
| 2652 | |
| 2653 | for (;i < size && i % CRYPT_SECTORS_PER_BUFSIZE != 0; ++i) { |
| 2654 | if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) { |
| 2655 | SLOGE("Error reading initial sectors from real_blkdev %s for " |
| 2656 | "inplace encrypt\n", crypto_blkdev); |
| 2657 | goto errout; |
| 2658 | } |
| 2659 | if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) { |
| 2660 | SLOGE("Error writing initial sectors to crypto_blkdev %s for " |
| 2661 | "inplace encrypt\n", crypto_blkdev); |
| 2662 | goto errout; |
| 2663 | } else { |
Elliott Hughes | cb33f57 | 2014-06-25 18:25:11 -0700 | [diff] [blame] | 2664 | SLOGI("Encrypted 1 block at %" PRId64, i); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2665 | } |
| 2666 | } |
| 2667 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2668 | one_pct = tot_numblocks / 100; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2669 | cur_pct = 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2670 | /* process the majority of the filesystem in blocks */ |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2671 | for (i/=CRYPT_SECTORS_PER_BUFSIZE; i<numblocks; i++) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2672 | new_pct = (i + blocks_already_done) / one_pct; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2673 | if (new_pct > cur_pct) { |
| 2674 | char buf[8]; |
| 2675 | |
| 2676 | cur_pct = new_pct; |
Elliott Hughes | 7373716 | 2014-06-25 17:27:42 -0700 | [diff] [blame] | 2677 | snprintf(buf, sizeof(buf), "%" PRId64, cur_pct); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2678 | property_set("vold.encrypt_progress", buf); |
| 2679 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2680 | if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2681 | SLOGE("Error reading real_blkdev %s for inplace encrypt", crypto_blkdev); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2682 | goto errout; |
| 2683 | } |
| 2684 | if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2685 | SLOGE("Error writing crypto_blkdev %s for inplace encrypt", crypto_blkdev); |
| 2686 | goto errout; |
| 2687 | } else { |
Elliott Hughes | cb33f57 | 2014-06-25 18:25:11 -0700 | [diff] [blame] | 2688 | SLOGD("Encrypted %d block at %" PRId64, |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2689 | CRYPT_SECTORS_PER_BUFSIZE, |
| 2690 | i * CRYPT_SECTORS_PER_BUFSIZE); |
| 2691 | } |
| 2692 | |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 2693 | if (!is_battery_ok_to_continue()) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2694 | SLOGE("Stopping encryption due to low battery"); |
| 2695 | *size_already_done += (i + 1) * CRYPT_SECTORS_PER_BUFSIZE - 1; |
| 2696 | rc = 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2697 | goto errout; |
| 2698 | } |
| 2699 | } |
| 2700 | |
| 2701 | /* Do any remaining sectors */ |
| 2702 | for (i=0; i<remainder; i++) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2703 | if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) { |
| 2704 | SLOGE("Error reading final sectors from real_blkdev %s for inplace encrypt", crypto_blkdev); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2705 | goto errout; |
| 2706 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2707 | if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) { |
| 2708 | SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt", crypto_blkdev); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2709 | goto errout; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2710 | } else { |
| 2711 | SLOGI("Encrypted 1 block at next location"); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2712 | } |
| 2713 | } |
| 2714 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2715 | *size_already_done += size; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2716 | rc = 0; |
| 2717 | |
| 2718 | errout: |
| 2719 | close(realfd); |
| 2720 | close(cryptofd); |
| 2721 | |
| 2722 | return rc; |
| 2723 | } |
| 2724 | |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2725 | /* returns on of the ENABLE_INPLACE_* return codes */ |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2726 | static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev, |
| 2727 | off64_t size, off64_t *size_already_done, |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2728 | off64_t tot_size, |
| 2729 | off64_t previously_encrypted_upto) |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2730 | { |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2731 | int rc_ext4, rc_f2fs, rc_full; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2732 | if (previously_encrypted_upto) { |
Elliott Hughes | cb33f57 | 2014-06-25 18:25:11 -0700 | [diff] [blame] | 2733 | SLOGD("Continuing encryption from %" PRId64, previously_encrypted_upto); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | if (*size_already_done + size < previously_encrypted_upto) { |
| 2737 | *size_already_done += size; |
| 2738 | return 0; |
| 2739 | } |
| 2740 | |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2741 | /* TODO: identify filesystem type. |
| 2742 | * As is, cryptfs_enable_inplace_ext4 will fail on an f2fs partition, and |
| 2743 | * then we will drop down to cryptfs_enable_inplace_f2fs. |
| 2744 | * */ |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2745 | if ((rc_ext4 = cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev, |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2746 | size, size_already_done, |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2747 | tot_size, previously_encrypted_upto)) == 0) { |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2748 | return 0; |
| 2749 | } |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2750 | SLOGD("cryptfs_enable_inplace_ext4()=%d\n", rc_ext4); |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2751 | |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2752 | if ((rc_f2fs = cryptfs_enable_inplace_f2fs(crypto_blkdev, real_blkdev, |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2753 | size, size_already_done, |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2754 | tot_size, previously_encrypted_upto)) == 0) { |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2755 | return 0; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2756 | } |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2757 | SLOGD("cryptfs_enable_inplace_f2fs()=%d\n", rc_f2fs); |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2758 | |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2759 | rc_full = cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev, |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2760 | size, size_already_done, tot_size, |
| 2761 | previously_encrypted_upto); |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2762 | SLOGD("cryptfs_enable_inplace_full()=%d\n", rc_full); |
| 2763 | |
| 2764 | /* Hack for b/17898962, the following is the symptom... */ |
| 2765 | if (rc_ext4 == ENABLE_INPLACE_ERR_DEV |
| 2766 | && rc_f2fs == ENABLE_INPLACE_ERR_DEV |
| 2767 | && rc_full == ENABLE_INPLACE_ERR_DEV) { |
| 2768 | return ENABLE_INPLACE_ERR_DEV; |
| 2769 | } |
| 2770 | return rc_full; |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2771 | } |
| 2772 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2773 | #define CRYPTO_ENABLE_WIPE 1 |
| 2774 | #define CRYPTO_ENABLE_INPLACE 2 |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2775 | |
| 2776 | #define FRAMEWORK_BOOT_WAIT 60 |
| 2777 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2778 | static inline int should_encrypt(struct volume_info *volume) |
| 2779 | { |
Paul Lawrence | ae59fe6 | 2014-01-21 08:23:27 -0800 | [diff] [blame] | 2780 | return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) == |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2781 | (VOL_ENCRYPTABLE | VOL_NONREMOVABLE); |
| 2782 | } |
| 2783 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2784 | static int cryptfs_SHA256_fileblock(const char* filename, __le8* buf) |
| 2785 | { |
| 2786 | int fd = open(filename, O_RDONLY); |
| 2787 | if (fd == -1) { |
| 2788 | SLOGE("Error opening file %s", filename); |
| 2789 | return -1; |
| 2790 | } |
| 2791 | |
| 2792 | char block[CRYPT_INPLACE_BUFSIZE]; |
| 2793 | memset(block, 0, sizeof(block)); |
| 2794 | if (unix_read(fd, block, sizeof(block)) < 0) { |
| 2795 | SLOGE("Error reading file %s", filename); |
| 2796 | close(fd); |
| 2797 | return -1; |
| 2798 | } |
| 2799 | |
| 2800 | close(fd); |
| 2801 | |
| 2802 | SHA256_CTX c; |
| 2803 | SHA256_Init(&c); |
| 2804 | SHA256_Update(&c, block, sizeof(block)); |
| 2805 | SHA256_Final(buf, &c); |
| 2806 | |
| 2807 | return 0; |
| 2808 | } |
| 2809 | |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 2810 | static int get_fs_type(struct fstab_rec *rec) |
| 2811 | { |
| 2812 | if (!strcmp(rec->fs_type, "ext4")) { |
| 2813 | return EXT4_FS; |
| 2814 | } else if (!strcmp(rec->fs_type, "f2fs")) { |
| 2815 | return F2FS_FS; |
| 2816 | } else { |
| 2817 | return -1; |
| 2818 | } |
| 2819 | } |
| 2820 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2821 | static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr *crypt_ftr, int how, |
| 2822 | char *crypto_blkdev, char *real_blkdev, |
| 2823 | int previously_encrypted_upto) |
| 2824 | { |
| 2825 | off64_t cur_encryption_done=0, tot_encryption_size=0; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 2826 | int rc = -1; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2827 | |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 2828 | if (!is_battery_ok_to_start()) { |
| 2829 | SLOGW("Not starting encryption due to low battery"); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2830 | return 0; |
| 2831 | } |
| 2832 | |
| 2833 | /* The size of the userdata partition, and add in the vold volumes below */ |
| 2834 | tot_encryption_size = crypt_ftr->fs_size; |
| 2835 | |
| 2836 | if (how == CRYPTO_ENABLE_WIPE) { |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 2837 | struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT); |
| 2838 | int fs_type = get_fs_type(rec); |
| 2839 | if (fs_type < 0) { |
| 2840 | SLOGE("cryptfs_enable: unsupported fs type %s\n", rec->fs_type); |
| 2841 | return -1; |
| 2842 | } |
| 2843 | rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr->fs_size, fs_type); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2844 | } else if (how == CRYPTO_ENABLE_INPLACE) { |
| 2845 | rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, |
| 2846 | crypt_ftr->fs_size, &cur_encryption_done, |
| 2847 | tot_encryption_size, |
| 2848 | previously_encrypted_upto); |
| 2849 | |
JP Abgrall | 512f0d5 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2850 | if (rc == ENABLE_INPLACE_ERR_DEV) { |
| 2851 | /* Hack for b/17898962 */ |
| 2852 | SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n"); |
| 2853 | cryptfs_reboot(reboot); |
| 2854 | } |
| 2855 | |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 2856 | if (!rc) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2857 | crypt_ftr->encrypted_upto = cur_encryption_done; |
| 2858 | } |
| 2859 | |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 2860 | if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2861 | /* The inplace routine never actually sets the progress to 100% due |
| 2862 | * to the round down nature of integer division, so set it here */ |
| 2863 | property_set("vold.encrypt_progress", "100"); |
| 2864 | } |
| 2865 | } else { |
| 2866 | /* Shouldn't happen */ |
| 2867 | SLOGE("cryptfs_enable: internal error, unknown option\n"); |
| 2868 | rc = -1; |
| 2869 | } |
| 2870 | |
| 2871 | return rc; |
| 2872 | } |
| 2873 | |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 2874 | int cryptfs_enable_internal(char *howarg, int crypt_type, char *passwd, |
| 2875 | int allow_reboot) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2876 | { |
| 2877 | int how = 0; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2878 | char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN]; |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 2879 | unsigned long nr_sec; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2880 | unsigned char decrypted_master_key[KEY_LEN_BYTES]; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 2881 | int rc=-1, fd, i; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2882 | struct crypt_mnt_ftr crypt_ftr; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2883 | struct crypt_persist_data *pdata; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2884 | char encrypted_state[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 2885 | char lockid[32] = { 0 }; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2886 | char key_loc[PROPERTY_VALUE_MAX]; |
| 2887 | char fuse_sdcard[PROPERTY_VALUE_MAX]; |
| 2888 | char *sd_mnt_point; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2889 | int num_vols; |
| 2890 | struct volume_info *vol_list = 0; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2891 | off64_t previously_encrypted_upto = 0; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2892 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2893 | if (!strcmp(howarg, "wipe")) { |
| 2894 | how = CRYPTO_ENABLE_WIPE; |
| 2895 | } else if (! strcmp(howarg, "inplace")) { |
| 2896 | how = CRYPTO_ENABLE_INPLACE; |
| 2897 | } else { |
| 2898 | /* Shouldn't happen, as CommandListener vets the args */ |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2899 | goto error_unencrypted; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2900 | } |
| 2901 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2902 | /* See if an encryption was underway and interrupted */ |
| 2903 | if (how == CRYPTO_ENABLE_INPLACE |
| 2904 | && get_crypt_ftr_and_key(&crypt_ftr) == 0 |
| 2905 | && (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS)) { |
| 2906 | previously_encrypted_upto = crypt_ftr.encrypted_upto; |
| 2907 | crypt_ftr.encrypted_upto = 0; |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 2908 | crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS; |
| 2909 | |
| 2910 | /* At this point, we are in an inconsistent state. Until we successfully |
| 2911 | complete encryption, a reboot will leave us broken. So mark the |
| 2912 | encryption failed in case that happens. |
| 2913 | On successfully completing encryption, remove this flag */ |
| 2914 | crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE; |
| 2915 | |
| 2916 | put_crypt_ftr_and_key(&crypt_ftr); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | property_get("ro.crypto.state", encrypted_state, ""); |
| 2920 | if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) { |
| 2921 | SLOGE("Device is already running encrypted, aborting"); |
| 2922 | goto error_unencrypted; |
| 2923 | } |
| 2924 | |
| 2925 | // TODO refactor fs_mgr_get_crypt_info to get both in one call |
| 2926 | fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc)); |
Ken Sumrall | 56ad03c | 2013-02-13 13:00:19 -0800 | [diff] [blame] | 2927 | fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2928 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2929 | /* Get the size of the real block device */ |
| 2930 | fd = open(real_blkdev, O_RDONLY); |
| 2931 | if ( (nr_sec = get_blkdev_size(fd)) == 0) { |
| 2932 | SLOGE("Cannot get size of block device %s\n", real_blkdev); |
| 2933 | goto error_unencrypted; |
| 2934 | } |
| 2935 | close(fd); |
| 2936 | |
| 2937 | /* 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] | 2938 | if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2939 | unsigned int fs_size_sec, max_fs_size_sec; |
Jim Miller | a70abc6 | 2014-08-15 02:00:45 +0000 | [diff] [blame] | 2940 | fs_size_sec = get_fs_size(real_blkdev); |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2941 | if (fs_size_sec == 0) |
| 2942 | fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev); |
| 2943 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2944 | max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2945 | |
| 2946 | if (fs_size_sec > max_fs_size_sec) { |
| 2947 | SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place."); |
| 2948 | goto error_unencrypted; |
| 2949 | } |
| 2950 | } |
| 2951 | |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 2952 | /* Get a wakelock as this may take a while, and we don't want the |
| 2953 | * device to sleep on us. We'll grab a partial wakelock, and if the UI |
| 2954 | * wants to keep the screen on, it can grab a full wakelock. |
| 2955 | */ |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2956 | snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid()); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 2957 | acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid); |
| 2958 | |
Jeff Sharkey | 7382f81 | 2012-08-23 14:08:59 -0700 | [diff] [blame] | 2959 | /* Get the sdcard mount point */ |
Jeff Sharkey | b77bc46 | 2012-10-01 14:36:26 -0700 | [diff] [blame] | 2960 | sd_mnt_point = getenv("EMULATED_STORAGE_SOURCE"); |
Jeff Sharkey | 7382f81 | 2012-08-23 14:08:59 -0700 | [diff] [blame] | 2961 | if (!sd_mnt_point) { |
| 2962 | sd_mnt_point = getenv("EXTERNAL_STORAGE"); |
| 2963 | } |
| 2964 | if (!sd_mnt_point) { |
| 2965 | sd_mnt_point = "/mnt/sdcard"; |
| 2966 | } |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2967 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2968 | /* TODO |
| 2969 | * Currently do not have test devices with multiple encryptable volumes. |
| 2970 | * When we acquire some, re-add support. |
| 2971 | */ |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2972 | num_vols=vold_getNumDirectVolumes(); |
| 2973 | vol_list = malloc(sizeof(struct volume_info) * num_vols); |
| 2974 | vold_getDirectVolumeList(vol_list); |
| 2975 | |
| 2976 | for (i=0; i<num_vols; i++) { |
| 2977 | if (should_encrypt(&vol_list[i])) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2978 | SLOGE("Cannot encrypt if there are multiple encryptable volumes" |
| 2979 | "%s\n", vol_list[i].label); |
| 2980 | goto error_unencrypted; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2981 | } |
| 2982 | } |
| 2983 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2984 | /* 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] | 2985 | * vold sets trigger_shutdown_framework. |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2986 | */ |
| 2987 | property_set("vold.decrypt", "trigger_shutdown_framework"); |
| 2988 | SLOGD("Just asked init to shut down class main\n"); |
| 2989 | |
Ken Sumrall | 425524d | 2012-06-14 20:55:28 -0700 | [diff] [blame] | 2990 | if (vold_unmountAllAsecs()) { |
| 2991 | /* Just report the error. If any are left mounted, |
| 2992 | * umounting /data below will fail and handle the error. |
| 2993 | */ |
| 2994 | SLOGE("Error unmounting internal asecs"); |
| 2995 | } |
| 2996 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2997 | property_get("ro.crypto.fuse_sdcard", fuse_sdcard, ""); |
| 2998 | if (!strcmp(fuse_sdcard, "true")) { |
| 2999 | /* This is a device using the fuse layer to emulate the sdcard semantics |
| 3000 | * on top of the userdata partition. vold does not manage it, it is managed |
| 3001 | * by the sdcard service. The sdcard service was killed by the property trigger |
| 3002 | * above, so just unmount it now. We must do this _AFTER_ killing the framework, |
| 3003 | * unlike the case for vold managed devices above. |
| 3004 | */ |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 3005 | if (wait_and_unmount(sd_mnt_point, false)) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3006 | goto error_shutting_down; |
| 3007 | } |
Ken Sumrall | 2eaf713 | 2011-01-14 12:45:48 -0800 | [diff] [blame] | 3008 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3009 | |
| 3010 | /* Now unmount the /data partition. */ |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 3011 | if (wait_and_unmount(DATA_MNT_POINT, false)) { |
JP Abgrall | 502dc74 | 2013-11-01 13:06:20 -0700 | [diff] [blame] | 3012 | if (allow_reboot) { |
| 3013 | goto error_shutting_down; |
| 3014 | } else { |
| 3015 | goto error_unencrypted; |
| 3016 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | /* Do extra work for a better UX when doing the long inplace encryption */ |
| 3020 | if (how == CRYPTO_ENABLE_INPLACE) { |
| 3021 | /* Now that /data is unmounted, we need to mount a tmpfs |
| 3022 | * /data, set a property saying we're doing inplace encryption, |
| 3023 | * and restart the framework. |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3024 | */ |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 3025 | if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3026 | goto error_shutting_down; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3027 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3028 | /* Tells the framework that inplace encryption is starting */ |
Ken Sumrall | 7df8412 | 2011-01-18 14:04:08 -0800 | [diff] [blame] | 3029 | property_set("vold.encrypt_progress", "0"); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3030 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3031 | /* restart the framework. */ |
| 3032 | /* Create necessary paths on /data */ |
| 3033 | if (prep_data_fs()) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3034 | goto error_shutting_down; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3035 | } |
| 3036 | |
Ken Sumrall | 92736ef | 2012-10-17 20:57:14 -0700 | [diff] [blame] | 3037 | /* Ugh, shutting down the framework is not synchronous, so until it |
| 3038 | * can be fixed, this horrible hack will wait a moment for it all to |
| 3039 | * shut down before proceeding. Without it, some devices cannot |
| 3040 | * restart the graphics services. |
| 3041 | */ |
| 3042 | sleep(2); |
| 3043 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3044 | /* startup service classes main and late_start */ |
| 3045 | property_set("vold.decrypt", "trigger_restart_min_framework"); |
| 3046 | SLOGD("Just triggered restart_min_framework\n"); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3047 | |
Ken Sumrall | 7df8412 | 2011-01-18 14:04:08 -0800 | [diff] [blame] | 3048 | /* OK, the framework is restarted and will soon be showing a |
| 3049 | * progress bar. Time to setup an encrypted mapping, and |
| 3050 | * either write a new filesystem, or encrypt in place updating |
| 3051 | * the progress bar as we work. |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3052 | */ |
| 3053 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3054 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3055 | /* Start the actual work of making an encrypted filesystem */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3056 | /* Initialize a crypt_mnt_ftr for the partition */ |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3057 | if (previously_encrypted_upto == 0) { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 3058 | if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) { |
| 3059 | goto error_shutting_down; |
| 3060 | } |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3061 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3062 | if (!strcmp(key_loc, KEY_IN_FOOTER)) { |
| 3063 | crypt_ftr.fs_size = nr_sec |
| 3064 | - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE); |
| 3065 | } else { |
| 3066 | crypt_ftr.fs_size = nr_sec; |
| 3067 | } |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3068 | /* At this point, we are in an inconsistent state. Until we successfully |
| 3069 | complete encryption, a reboot will leave us broken. So mark the |
| 3070 | encryption failed in case that happens. |
| 3071 | On successfully completing encryption, remove this flag */ |
| 3072 | crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3073 | crypt_ftr.crypt_type = crypt_type; |
| 3074 | strcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3075 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3076 | /* Make an encrypted master key */ |
| 3077 | if (create_encrypted_random_key(passwd, crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) { |
| 3078 | SLOGE("Cannot create encrypted master key\n"); |
| 3079 | goto error_shutting_down; |
| 3080 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3081 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3082 | /* Write the key to the end of the partition */ |
| 3083 | put_crypt_ftr_and_key(&crypt_ftr); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3084 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3085 | /* If any persistent data has been remembered, save it. |
| 3086 | * If none, create a valid empty table and save that. |
| 3087 | */ |
| 3088 | if (!persist_data) { |
| 3089 | pdata = malloc(CRYPT_PERSIST_DATA_SIZE); |
| 3090 | if (pdata) { |
| 3091 | init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE); |
| 3092 | persist_data = pdata; |
| 3093 | } |
| 3094 | } |
| 3095 | if (persist_data) { |
| 3096 | save_persistent_data(); |
| 3097 | } |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3098 | } |
| 3099 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 3100 | decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3101 | create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev, |
| 3102 | "userdata"); |
| 3103 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3104 | /* If we are continuing, check checksums match */ |
| 3105 | rc = 0; |
| 3106 | if (previously_encrypted_upto) { |
| 3107 | __le8 hash_first_block[SHA256_DIGEST_LENGTH]; |
| 3108 | rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block); |
Ken Sumrall | 128626f | 2011-06-28 18:45:14 -0700 | [diff] [blame] | 3109 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3110 | if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block, |
| 3111 | sizeof(hash_first_block)) != 0) { |
| 3112 | SLOGE("Checksums do not match - trigger wipe"); |
| 3113 | rc = -1; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3114 | } |
| 3115 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3116 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3117 | if (!rc) { |
| 3118 | rc = cryptfs_enable_all_volumes(&crypt_ftr, how, |
| 3119 | crypto_blkdev, real_blkdev, |
| 3120 | previously_encrypted_upto); |
| 3121 | } |
| 3122 | |
| 3123 | /* Calculate checksum if we are not finished */ |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 3124 | if (!rc && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3125 | rc = cryptfs_SHA256_fileblock(crypto_blkdev, |
| 3126 | crypt_ftr.hash_first_block); |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 3127 | if (rc) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3128 | SLOGE("Error calculating checksum for continuing encryption"); |
| 3129 | rc = -1; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3130 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3131 | } |
| 3132 | |
| 3133 | /* Undo the dm-crypt mapping whether we succeed or not */ |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3134 | delete_crypto_blk_dev("userdata"); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3135 | |
| 3136 | free(vol_list); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3137 | |
| 3138 | if (! rc) { |
| 3139 | /* Success */ |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3140 | crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 3141 | |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3142 | if (crypt_ftr.encrypted_upto != crypt_ftr.fs_size) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3143 | SLOGD("Encrypted up to sector %lld - will continue after reboot", |
| 3144 | crypt_ftr.encrypted_upto); |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3145 | crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3146 | } |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 3147 | |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3148 | put_crypt_ftr_and_key(&crypt_ftr); |
Ken Sumrall | d33d417 | 2011-02-01 00:49:13 -0800 | [diff] [blame] | 3149 | |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 3150 | if (crypt_ftr.encrypted_upto == crypt_ftr.fs_size) { |
Paul Lawrence | b6672e1 | 2014-08-15 07:37:28 -0700 | [diff] [blame] | 3151 | char value[PROPERTY_VALUE_MAX]; |
| 3152 | property_get("ro.crypto.state", value, ""); |
| 3153 | if (!strcmp(value, "")) { |
| 3154 | /* default encryption - continue first boot sequence */ |
| 3155 | property_set("ro.crypto.state", "encrypted"); |
| 3156 | release_wake_lock(lockid); |
| 3157 | cryptfs_check_passwd(DEFAULT_PASSWORD); |
| 3158 | cryptfs_restart_internal(1); |
| 3159 | return 0; |
| 3160 | } else { |
| 3161 | sleep(2); /* Give the UI a chance to show 100% progress */ |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3162 | cryptfs_reboot(reboot); |
Paul Lawrence | b6672e1 | 2014-08-15 07:37:28 -0700 | [diff] [blame] | 3163 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3164 | } else { |
Paul Lawrence | b6672e1 | 2014-08-15 07:37:28 -0700 | [diff] [blame] | 3165 | sleep(2); /* Partially encrypted, ensure writes flushed to ssd */ |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3166 | cryptfs_reboot(shutdown); |
| 3167 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3168 | } else { |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 3169 | char value[PROPERTY_VALUE_MAX]; |
| 3170 | |
Ken Sumrall | 319369a | 2012-06-27 16:30:18 -0700 | [diff] [blame] | 3171 | property_get("ro.vold.wipe_on_crypt_fail", value, "0"); |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 3172 | if (!strcmp(value, "1")) { |
| 3173 | /* wipe data if encryption failed */ |
| 3174 | SLOGE("encryption failed - rebooting into recovery to wipe data\n"); |
| 3175 | mkdir("/cache/recovery", 0700); |
Nick Kralevich | 4684e58 | 2012-06-26 15:07:03 -0700 | [diff] [blame] | 3176 | int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC, 0600); |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 3177 | if (fd >= 0) { |
Jeff Sharkey | dd1a804 | 2014-09-24 11:46:51 -0700 | [diff] [blame] | 3178 | write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1); |
| 3179 | write(fd, "--reason=cryptfs_enable_internal\n", strlen("--reason=cryptfs_enable_internal\n") + 1); |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 3180 | close(fd); |
| 3181 | } else { |
| 3182 | SLOGE("could not open /cache/recovery/command\n"); |
| 3183 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3184 | cryptfs_reboot(recovery); |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 3185 | } else { |
| 3186 | /* set property to trigger dialog */ |
| 3187 | property_set("vold.encrypt_progress", "error_partially_encrypted"); |
| 3188 | release_wake_lock(lockid); |
| 3189 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3190 | return -1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3191 | } |
| 3192 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3193 | /* hrm, the encrypt step claims success, but the reboot failed. |
| 3194 | * This should not happen. |
| 3195 | * Set the property and return. Hope the framework can deal with it. |
| 3196 | */ |
| 3197 | property_set("vold.encrypt_progress", "error_reboot_failed"); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 3198 | release_wake_lock(lockid); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3199 | return rc; |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3200 | |
| 3201 | error_unencrypted: |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3202 | free(vol_list); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3203 | property_set("vold.encrypt_progress", "error_not_encrypted"); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 3204 | if (lockid[0]) { |
| 3205 | release_wake_lock(lockid); |
| 3206 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3207 | return -1; |
| 3208 | |
| 3209 | error_shutting_down: |
| 3210 | /* we failed, and have not encrypted anthing, so the users's data is still intact, |
| 3211 | * but the framework is stopped and not restarted to show the error, so it's up to |
| 3212 | * vold to restart the system. |
| 3213 | */ |
| 3214 | SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system"); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 3215 | cryptfs_reboot(reboot); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3216 | |
| 3217 | /* shouldn't get here */ |
| 3218 | property_set("vold.encrypt_progress", "error_shutting_down"); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 3219 | free(vol_list); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 3220 | if (lockid[0]) { |
| 3221 | release_wake_lock(lockid); |
| 3222 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 3223 | return -1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3224 | } |
| 3225 | |
Paul Lawrence | 45f1053 | 2014-04-04 18:11:56 +0000 | [diff] [blame] | 3226 | int cryptfs_enable(char *howarg, int type, char *passwd, int allow_reboot) |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 3227 | { |
Paul Lawrence | fc61504 | 2014-10-04 15:32:29 -0700 | [diff] [blame] | 3228 | char* adjusted_passwd = adjust_passwd(passwd); |
| 3229 | if (adjusted_passwd) { |
| 3230 | passwd = adjusted_passwd; |
| 3231 | } |
| 3232 | |
| 3233 | int rc = cryptfs_enable_internal(howarg, type, passwd, allow_reboot); |
| 3234 | |
| 3235 | free(adjusted_passwd); |
| 3236 | return rc; |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | int cryptfs_enable_default(char *howarg, int allow_reboot) |
| 3240 | { |
| 3241 | return cryptfs_enable_internal(howarg, CRYPT_TYPE_DEFAULT, |
| 3242 | DEFAULT_PASSWORD, allow_reboot); |
| 3243 | } |
| 3244 | |
| 3245 | int cryptfs_changepw(int crypt_type, const char *newpw) |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3246 | { |
| 3247 | struct crypt_mnt_ftr crypt_ftr; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3248 | |
| 3249 | /* This is only allowed after we've successfully decrypted the master key */ |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3250 | if (!master_key_saved) { |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 3251 | SLOGE("Key not saved, aborting"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3252 | return -1; |
| 3253 | } |
| 3254 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3255 | if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) { |
| 3256 | SLOGE("Invalid crypt_type %d", crypt_type); |
| 3257 | return -1; |
| 3258 | } |
| 3259 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3260 | /* get key */ |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3261 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3262 | SLOGE("Error getting crypt footer and key"); |
| 3263 | return -1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3264 | } |
| 3265 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3266 | crypt_ftr.crypt_type = crypt_type; |
| 3267 | |
Paul Lawrence | fc61504 | 2014-10-04 15:32:29 -0700 | [diff] [blame] | 3268 | char* adjusted_passwd = adjust_passwd(newpw); |
| 3269 | if (adjusted_passwd) { |
| 3270 | newpw = adjusted_passwd; |
| 3271 | } |
| 3272 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3273 | encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD |
| 3274 | : newpw, |
| 3275 | crypt_ftr.salt, |
| 3276 | saved_master_key, |
| 3277 | crypt_ftr.master_key, |
| 3278 | &crypt_ftr); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 3279 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 3280 | /* save the key */ |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3281 | put_crypt_ftr_and_key(&crypt_ftr); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3282 | |
Paul Lawrence | fc61504 | 2014-10-04 15:32:29 -0700 | [diff] [blame] | 3283 | free(adjusted_passwd); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 3284 | return 0; |
| 3285 | } |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3286 | |
| 3287 | static int persist_get_key(char *fieldname, char *value) |
| 3288 | { |
| 3289 | unsigned int i; |
| 3290 | |
| 3291 | if (persist_data == NULL) { |
| 3292 | return -1; |
| 3293 | } |
| 3294 | for (i = 0; i < persist_data->persist_valid_entries; i++) { |
| 3295 | if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) { |
| 3296 | /* We found it! */ |
| 3297 | strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX); |
| 3298 | return 0; |
| 3299 | } |
| 3300 | } |
| 3301 | |
| 3302 | return -1; |
| 3303 | } |
| 3304 | |
| 3305 | static int persist_set_key(char *fieldname, char *value, int encrypted) |
| 3306 | { |
| 3307 | unsigned int i; |
| 3308 | unsigned int num; |
| 3309 | struct crypt_mnt_ftr crypt_ftr; |
| 3310 | unsigned int max_persistent_entries; |
| 3311 | unsigned int dsize; |
| 3312 | |
| 3313 | if (persist_data == NULL) { |
| 3314 | return -1; |
| 3315 | } |
| 3316 | |
| 3317 | /* If encrypted, use the values from the crypt_ftr, otherwise |
| 3318 | * use the values for the current spec. |
| 3319 | */ |
| 3320 | if (encrypted) { |
| 3321 | if(get_crypt_ftr_and_key(&crypt_ftr)) { |
| 3322 | return -1; |
| 3323 | } |
| 3324 | dsize = crypt_ftr.persist_data_size; |
| 3325 | } else { |
| 3326 | dsize = CRYPT_PERSIST_DATA_SIZE; |
| 3327 | } |
| 3328 | max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) / |
| 3329 | sizeof(struct crypt_persist_entry); |
| 3330 | |
| 3331 | num = persist_data->persist_valid_entries; |
| 3332 | |
| 3333 | for (i = 0; i < num; i++) { |
| 3334 | if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) { |
| 3335 | /* We found an existing entry, update it! */ |
| 3336 | memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX); |
| 3337 | strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX); |
| 3338 | return 0; |
| 3339 | } |
| 3340 | } |
| 3341 | |
| 3342 | /* We didn't find it, add it to the end, if there is room */ |
| 3343 | if (persist_data->persist_valid_entries < max_persistent_entries) { |
| 3344 | memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry)); |
| 3345 | strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX); |
| 3346 | strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX); |
| 3347 | persist_data->persist_valid_entries++; |
| 3348 | return 0; |
| 3349 | } |
| 3350 | |
| 3351 | return -1; |
| 3352 | } |
| 3353 | |
| 3354 | /* Return the value of the specified field. */ |
| 3355 | int cryptfs_getfield(char *fieldname, char *value, int len) |
| 3356 | { |
| 3357 | char temp_value[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3358 | /* 0 is success, 1 is not encrypted, |
| 3359 | * -1 is value not set, -2 is any other error |
| 3360 | */ |
| 3361 | int rc = -2; |
| 3362 | |
| 3363 | if (persist_data == NULL) { |
| 3364 | load_persistent_data(); |
| 3365 | if (persist_data == NULL) { |
| 3366 | SLOGE("Getfield error, cannot load persistent data"); |
| 3367 | goto out; |
| 3368 | } |
| 3369 | } |
| 3370 | |
| 3371 | if (!persist_get_key(fieldname, temp_value)) { |
| 3372 | /* We found it, copy it to the caller's buffer and return */ |
| 3373 | strlcpy(value, temp_value, len); |
| 3374 | rc = 0; |
| 3375 | } else { |
| 3376 | /* Sadness, it's not there. Return the error */ |
| 3377 | rc = -1; |
| 3378 | } |
| 3379 | |
| 3380 | out: |
| 3381 | return rc; |
| 3382 | } |
| 3383 | |
| 3384 | /* Set the value of the specified field. */ |
| 3385 | int cryptfs_setfield(char *fieldname, char *value) |
| 3386 | { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 3387 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 3388 | /* 0 is success, -1 is an error */ |
| 3389 | int rc = -1; |
| 3390 | int encrypted = 0; |
| 3391 | |
| 3392 | if (persist_data == NULL) { |
| 3393 | load_persistent_data(); |
| 3394 | if (persist_data == NULL) { |
| 3395 | SLOGE("Setfield error, cannot load persistent data"); |
| 3396 | goto out; |
| 3397 | } |
| 3398 | } |
| 3399 | |
| 3400 | property_get("ro.crypto.state", encrypted_state, ""); |
| 3401 | if (!strcmp(encrypted_state, "encrypted") ) { |
| 3402 | encrypted = 1; |
| 3403 | } |
| 3404 | |
| 3405 | if (persist_set_key(fieldname, value, encrypted)) { |
| 3406 | goto out; |
| 3407 | } |
| 3408 | |
| 3409 | /* If we are running encrypted, save the persistent data now */ |
| 3410 | if (encrypted) { |
| 3411 | if (save_persistent_data()) { |
| 3412 | SLOGE("Setfield error, cannot save persistent data"); |
| 3413 | goto out; |
| 3414 | } |
| 3415 | } |
| 3416 | |
| 3417 | rc = 0; |
| 3418 | |
| 3419 | out: |
| 3420 | return rc; |
| 3421 | } |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3422 | |
| 3423 | /* Checks userdata. Attempt to mount the volume if default- |
| 3424 | * encrypted. |
| 3425 | * On success trigger next init phase and return 0. |
| 3426 | * Currently do not handle failure - see TODO below. |
| 3427 | */ |
| 3428 | int cryptfs_mount_default_encrypted(void) |
| 3429 | { |
| 3430 | char decrypt_state[PROPERTY_VALUE_MAX]; |
| 3431 | property_get("vold.decrypt", decrypt_state, "0"); |
| 3432 | if (!strcmp(decrypt_state, "0")) { |
| 3433 | SLOGE("Not encrypted - should not call here"); |
| 3434 | } else { |
| 3435 | int crypt_type = cryptfs_get_password_type(); |
| 3436 | if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) { |
| 3437 | SLOGE("Bad crypt type - error"); |
| 3438 | } else if (crypt_type != CRYPT_TYPE_DEFAULT) { |
| 3439 | SLOGD("Password is not default - " |
| 3440 | "starting min framework to prompt"); |
| 3441 | property_set("vold.decrypt", "trigger_restart_min_framework"); |
| 3442 | return 0; |
| 3443 | } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) { |
| 3444 | SLOGD("Password is default - restarting filesystem"); |
| 3445 | cryptfs_restart_internal(0); |
| 3446 | return 0; |
| 3447 | } else { |
| 3448 | SLOGE("Encrypted, default crypt type but can't decrypt"); |
| 3449 | } |
| 3450 | } |
| 3451 | |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3452 | /** Corrupt. Allow us to boot into framework, which will detect bad |
| 3453 | crypto when it calls do_crypto_complete, then do a factory reset |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3454 | */ |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3455 | property_set("vold.decrypt", "trigger_restart_min_framework"); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3456 | return 0; |
| 3457 | } |
| 3458 | |
| 3459 | /* Returns type of the password, default, pattern, pin or password. |
| 3460 | */ |
| 3461 | int cryptfs_get_password_type(void) |
| 3462 | { |
| 3463 | struct crypt_mnt_ftr crypt_ftr; |
| 3464 | |
| 3465 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
| 3466 | SLOGE("Error getting crypt footer and key\n"); |
| 3467 | return -1; |
| 3468 | } |
| 3469 | |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 3470 | if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) { |
| 3471 | return -1; |
| 3472 | } |
| 3473 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 3474 | return crypt_ftr.crypt_type; |
| 3475 | } |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 3476 | |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 3477 | char* cryptfs_get_password() |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 3478 | { |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 3479 | struct timespec now; |
| 3480 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 3481 | if (now.tv_sec < password_expiry_time) { |
| 3482 | return password; |
| 3483 | } else { |
| 3484 | cryptfs_clear_password(); |
| 3485 | return 0; |
| 3486 | } |
| 3487 | } |
| 3488 | |
| 3489 | void cryptfs_clear_password() |
| 3490 | { |
| 3491 | if (password) { |
| 3492 | size_t len = strlen(password); |
| 3493 | memset(password, 0, len); |
| 3494 | free(password); |
| 3495 | password = 0; |
| 3496 | password_expiry_time = 0; |
| 3497 | } |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 3498 | } |