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