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