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