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