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