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> |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 41 | #include <ext4_utils/ext4_crypt.h> |
Tao Bao | 5a95ddb | 2016-10-05 18:01:19 -0700 | [diff] [blame] | 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" |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 59 | #include "Ext4Crypt.h" |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 60 | #include "f2fs_sparseblock.h" |
Paul Crowley | f71ace3 | 2016-06-02 11:01:19 -0700 | [diff] [blame] | 61 | #include "EncryptInplace.h" |
jessica_yu | 3f14fe4 | 2014-09-22 15:57:40 +0800 | [diff] [blame] | 62 | #include "Process.h" |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 63 | #include "Keymaster.h" |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 64 | #include "android-base/properties.h" |
Yabin Cui | 1fb5966 | 2016-06-24 14:48:49 -0700 | [diff] [blame] | 65 | #include <bootloader_message/bootloader_message.h> |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 66 | extern "C" { |
| 67 | #include <crypto_scrypt.h> |
| 68 | } |
Mark Salyzyn | 3e97127 | 2014-01-21 13:27:04 -0800 | [diff] [blame] | 69 | |
Mark Salyzyn | 5eecc44 | 2014-02-12 14:16:14 -0800 | [diff] [blame] | 70 | #define UNUSED __attribute__((unused)) |
| 71 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 72 | #define DM_CRYPT_BUF_SIZE 4096 |
| 73 | |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 74 | #define HASH_COUNT 2000 |
| 75 | #define KEY_LEN_BYTES 16 |
| 76 | #define IV_LEN_BYTES 16 |
| 77 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 78 | #define KEY_IN_FOOTER "footer" |
| 79 | |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 80 | #define DEFAULT_PASSWORD "default_password" |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 81 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 82 | #define CRYPTO_BLOCK_DEVICE "userdata" |
| 83 | |
| 84 | #define BREADCRUMB_FILE "/data/misc/vold/convert_fde" |
| 85 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 86 | #define EXT4_FS 1 |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 87 | #define F2FS_FS 2 |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 88 | |
Ken Sumrall | e919efe | 2012-09-29 17:07:41 -0700 | [diff] [blame] | 89 | #define TABLE_LOAD_RETRIES 10 |
| 90 | |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 91 | #define RSA_KEY_SIZE 2048 |
| 92 | #define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8) |
| 93 | #define RSA_EXPONENT 0x10001 |
Shawn Willden | da6e899 | 2015-06-03 09:40:45 -0600 | [diff] [blame] | 94 | #define KEYMASTER_CRYPTFS_RATE_LIMIT 1 // Maximum one try per second |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 95 | |
Paul Lawrence | 8e3f451 | 2014-09-08 10:11:17 -0700 | [diff] [blame] | 96 | #define RETRY_MOUNT_ATTEMPTS 10 |
| 97 | #define RETRY_MOUNT_DELAY_SECONDS 1 |
| 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. |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 165 | memcpy(to_sign + 1, object, std::min((size_t)RSA_KEY_SIZE_BYTES - 1, object_size)); |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 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 | |
Josh Gao | fec4437 | 2017-08-28 13:22:55 -0700 | [diff] [blame] | 194 | enum class RebootType {reboot, recovery, shutdown}; |
| 195 | static void cryptfs_reboot(RebootType rt) |
Ken Sumrall | adfba36 | 2013-06-04 16:37:52 -0700 | [diff] [blame] | 196 | { |
Josh Gao | fec4437 | 2017-08-28 13:22:55 -0700 | [diff] [blame] | 197 | switch (rt) { |
| 198 | case RebootType::reboot: |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 199 | property_set(ANDROID_RB_PROPERTY, "reboot"); |
| 200 | break; |
| 201 | |
Josh Gao | fec4437 | 2017-08-28 13:22:55 -0700 | [diff] [blame] | 202 | case RebootType::recovery: |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 203 | property_set(ANDROID_RB_PROPERTY, "reboot,recovery"); |
| 204 | break; |
| 205 | |
Josh Gao | fec4437 | 2017-08-28 13:22:55 -0700 | [diff] [blame] | 206 | case RebootType::shutdown: |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 207 | property_set(ANDROID_RB_PROPERTY, "shutdown"); |
| 208 | break; |
Ken Sumrall | adfba36 | 2013-06-04 16:37:52 -0700 | [diff] [blame] | 209 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 210 | |
Ken Sumrall | adfba36 | 2013-06-04 16:37:52 -0700 | [diff] [blame] | 211 | sleep(20); |
| 212 | |
| 213 | /* Shouldn't get here, reboot should happen before sleep times out */ |
| 214 | return; |
| 215 | } |
| 216 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 217 | static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags) |
| 218 | { |
| 219 | memset(io, 0, dataSize); |
| 220 | io->data_size = dataSize; |
| 221 | io->data_start = sizeof(struct dm_ioctl); |
| 222 | io->version[0] = 4; |
| 223 | io->version[1] = 0; |
| 224 | io->version[2] = 0; |
| 225 | io->flags = flags; |
| 226 | if (name) { |
Marek Pola | 5e6b914 | 2015-02-05 14:22:34 +0100 | [diff] [blame] | 227 | strlcpy(io->name, name, sizeof(io->name)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 231 | /** |
| 232 | * Gets the default device scrypt parameters for key derivation time tuning. |
| 233 | * The parameters should lead to about one second derivation time for the |
| 234 | * given device. |
| 235 | */ |
| 236 | static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) { |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 237 | char paramstr[PROPERTY_VALUE_MAX]; |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 238 | int Nf, rf, pf; |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 239 | |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 240 | property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS); |
| 241 | if (!parse_scrypt_parameters(paramstr, &Nf, &rf, &pf)) { |
| 242 | SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr); |
| 243 | parse_scrypt_parameters(SCRYPT_DEFAULTS, &Nf, &rf, &pf); |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 244 | } |
Paul Crowley | 63c18d3 | 2016-02-10 14:02:47 +0000 | [diff] [blame] | 245 | ftr->N_factor = Nf; |
| 246 | ftr->r_factor = rf; |
| 247 | ftr->p_factor = pf; |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 250 | static unsigned int get_fs_size(char *dev) |
| 251 | { |
| 252 | int fd, block_size; |
| 253 | struct ext4_super_block sb; |
| 254 | off64_t len; |
| 255 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 256 | if ((fd = open(dev, O_RDONLY|O_CLOEXEC)) < 0) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 257 | SLOGE("Cannot open device to get filesystem size "); |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | if (lseek64(fd, 1024, SEEK_SET) < 0) { |
| 262 | SLOGE("Cannot seek to superblock"); |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) { |
| 267 | SLOGE("Cannot read superblock"); |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | close(fd); |
| 272 | |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 273 | if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) { |
| 274 | SLOGE("Not a valid ext4 superblock"); |
| 275 | return 0; |
| 276 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 277 | block_size = 1024 << sb.s_log_block_size; |
| 278 | /* compute length in bytes */ |
| 279 | len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size; |
| 280 | |
| 281 | /* return length in sectors */ |
| 282 | return (unsigned int) (len / 512); |
| 283 | } |
| 284 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 285 | static int get_crypt_ftr_info(char **metadata_fname, off64_t *off) |
| 286 | { |
| 287 | static int cached_data = 0; |
| 288 | static off64_t cached_off = 0; |
| 289 | static char cached_metadata_fname[PROPERTY_VALUE_MAX] = ""; |
| 290 | int fd; |
| 291 | char key_loc[PROPERTY_VALUE_MAX]; |
| 292 | char real_blkdev[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 293 | int rc = -1; |
| 294 | |
| 295 | if (!cached_data) { |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 296 | fs_mgr_get_crypt_info(fstab_default, key_loc, real_blkdev, sizeof(key_loc)); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 297 | |
| 298 | if (!strcmp(key_loc, KEY_IN_FOOTER)) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 299 | if ( (fd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 300 | SLOGE("Cannot open real block device %s\n", real_blkdev); |
| 301 | return -1; |
| 302 | } |
| 303 | |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 304 | unsigned long nr_sec = 0; |
| 305 | get_blkdev_size(fd, &nr_sec); |
| 306 | if (nr_sec != 0) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 307 | /* If it's an encrypted Android partition, the last 16 Kbytes contain the |
| 308 | * encryption info footer and key, and plenty of bytes to spare for future |
| 309 | * growth. |
| 310 | */ |
| 311 | strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname)); |
| 312 | cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET; |
| 313 | cached_data = 1; |
| 314 | } else { |
| 315 | SLOGE("Cannot get size of block device %s\n", real_blkdev); |
| 316 | } |
| 317 | close(fd); |
| 318 | } else { |
| 319 | strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname)); |
| 320 | cached_off = 0; |
| 321 | cached_data = 1; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if (cached_data) { |
| 326 | if (metadata_fname) { |
| 327 | *metadata_fname = cached_metadata_fname; |
| 328 | } |
| 329 | if (off) { |
| 330 | *off = cached_off; |
| 331 | } |
| 332 | rc = 0; |
| 333 | } |
| 334 | |
| 335 | return rc; |
| 336 | } |
| 337 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 338 | /* Set sha256 checksum in structure */ |
| 339 | static void set_ftr_sha(struct crypt_mnt_ftr *crypt_ftr) |
| 340 | { |
| 341 | SHA256_CTX c; |
| 342 | SHA256_Init(&c); |
| 343 | memset(crypt_ftr->sha256, 0, sizeof(crypt_ftr->sha256)); |
| 344 | SHA256_Update(&c, crypt_ftr, sizeof(*crypt_ftr)); |
| 345 | SHA256_Final(crypt_ftr->sha256, &c); |
| 346 | } |
| 347 | |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 348 | /* 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] | 349 | * update the failed mount count but not change the key. |
| 350 | */ |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 351 | 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] | 352 | { |
| 353 | int fd; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 354 | unsigned int cnt; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 355 | /* starting_off is set to the SEEK_SET offset |
| 356 | * where the crypto structure starts |
| 357 | */ |
| 358 | off64_t starting_off; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 359 | int rc = -1; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 360 | char *fname = NULL; |
Ken Sumrall | 3be890f | 2011-09-14 16:53:46 -0700 | [diff] [blame] | 361 | struct stat statbuf; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 362 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 363 | set_ftr_sha(crypt_ftr); |
| 364 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 365 | if (get_crypt_ftr_info(&fname, &starting_off)) { |
| 366 | SLOGE("Unable to get crypt_ftr_info\n"); |
| 367 | return -1; |
| 368 | } |
| 369 | if (fname[0] != '/') { |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 370 | SLOGE("Unexpected value for crypto key location\n"); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 371 | return -1; |
| 372 | } |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 373 | if ( (fd = open(fname, O_RDWR | O_CREAT|O_CLOEXEC, 0600)) < 0) { |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 374 | SLOGE("Cannot open footer file %s for put\n", fname); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | /* Seek to the start of the crypt footer */ |
| 379 | if (lseek64(fd, starting_off, SEEK_SET) == -1) { |
| 380 | SLOGE("Cannot seek to real block device footer\n"); |
| 381 | goto errout; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) { |
| 385 | SLOGE("Cannot write real block device footer\n"); |
| 386 | goto errout; |
| 387 | } |
| 388 | |
Ken Sumrall | 3be890f | 2011-09-14 16:53:46 -0700 | [diff] [blame] | 389 | fstat(fd, &statbuf); |
| 390 | /* 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] | 391 | if (S_ISREG(statbuf.st_mode)) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 392 | if (ftruncate(fd, 0x4000)) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 393 | SLOGE("Cannot set footer file size\n"); |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 394 | goto errout; |
| 395 | } |
| 396 | } |
| 397 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 398 | /* Success! */ |
| 399 | rc = 0; |
| 400 | |
| 401 | errout: |
| 402 | close(fd); |
| 403 | return rc; |
| 404 | |
| 405 | } |
| 406 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 407 | static bool check_ftr_sha(const struct crypt_mnt_ftr *crypt_ftr) |
| 408 | { |
| 409 | struct crypt_mnt_ftr copy; |
| 410 | memcpy(©, crypt_ftr, sizeof(copy)); |
| 411 | set_ftr_sha(©); |
| 412 | return memcmp(copy.sha256, crypt_ftr->sha256, sizeof(copy.sha256)) == 0; |
| 413 | } |
| 414 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 415 | static inline int unix_read(int fd, void* buff, int len) |
| 416 | { |
| 417 | return TEMP_FAILURE_RETRY(read(fd, buff, len)); |
| 418 | } |
| 419 | |
| 420 | static inline int unix_write(int fd, const void* buff, int len) |
| 421 | { |
| 422 | return TEMP_FAILURE_RETRY(write(fd, buff, len)); |
| 423 | } |
| 424 | |
| 425 | static void init_empty_persist_data(struct crypt_persist_data *pdata, int len) |
| 426 | { |
| 427 | memset(pdata, 0, len); |
| 428 | pdata->persist_magic = PERSIST_DATA_MAGIC; |
| 429 | pdata->persist_valid_entries = 0; |
| 430 | } |
| 431 | |
| 432 | /* A routine to update the passed in crypt_ftr to the lastest version. |
| 433 | * fd is open read/write on the device that holds the crypto footer and persistent |
| 434 | * data, crypt_ftr is a pointer to the struct to be updated, and offset is the |
| 435 | * absolute offset to the start of the crypt_mnt_ftr on the passed in fd. |
| 436 | */ |
| 437 | static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset) |
| 438 | { |
Kenny Root | 7434b31 | 2013-06-14 11:29:53 -0700 | [diff] [blame] | 439 | int orig_major = crypt_ftr->major_version; |
| 440 | int orig_minor = crypt_ftr->minor_version; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 441 | |
Kenny Root | 7434b31 | 2013-06-14 11:29:53 -0700 | [diff] [blame] | 442 | if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) { |
| 443 | struct crypt_persist_data *pdata; |
| 444 | off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 445 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 446 | SLOGW("upgrading crypto footer to 1.1"); |
| 447 | |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 448 | pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE); |
Kenny Root | 7434b31 | 2013-06-14 11:29:53 -0700 | [diff] [blame] | 449 | if (pdata == NULL) { |
| 450 | SLOGE("Cannot allocate persisent data\n"); |
| 451 | return; |
| 452 | } |
| 453 | memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE); |
| 454 | |
| 455 | /* Need to initialize the persistent data area */ |
| 456 | if (lseek64(fd, pdata_offset, SEEK_SET) == -1) { |
| 457 | SLOGE("Cannot seek to persisent data offset\n"); |
Henrik Baard | 9106463 | 2015-02-05 15:09:17 +0100 | [diff] [blame] | 458 | free(pdata); |
Kenny Root | 7434b31 | 2013-06-14 11:29:53 -0700 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | /* Write all zeros to the first copy, making it invalid */ |
| 462 | unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE); |
| 463 | |
| 464 | /* Write a valid but empty structure to the second copy */ |
| 465 | init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE); |
| 466 | unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE); |
| 467 | |
| 468 | /* Update the footer */ |
| 469 | crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE; |
| 470 | crypt_ftr->persist_data_offset[0] = pdata_offset; |
| 471 | crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE; |
| 472 | crypt_ftr->minor_version = 1; |
Henrik Baard | 9106463 | 2015-02-05 15:09:17 +0100 | [diff] [blame] | 473 | free(pdata); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 476 | if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) { |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 477 | SLOGW("upgrading crypto footer to 1.2"); |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 478 | /* But keep the old kdf_type. |
| 479 | * It will get updated later to KDF_SCRYPT after the password has been verified. |
| 480 | */ |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 481 | crypt_ftr->kdf_type = KDF_PBKDF2; |
| 482 | get_device_scrypt_params(crypt_ftr); |
| 483 | crypt_ftr->minor_version = 2; |
| 484 | } |
| 485 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 486 | if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) { |
| 487 | SLOGW("upgrading crypto footer to 1.3"); |
| 488 | crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD; |
| 489 | crypt_ftr->minor_version = 3; |
| 490 | } |
| 491 | |
Kenny Root | 7434b31 | 2013-06-14 11:29:53 -0700 | [diff] [blame] | 492 | if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) { |
| 493 | if (lseek64(fd, offset, SEEK_SET) == -1) { |
| 494 | SLOGE("Cannot seek to crypt footer\n"); |
| 495 | return; |
| 496 | } |
| 497 | unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr)); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 498 | } |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | |
| 502 | 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] | 503 | { |
| 504 | int fd; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 505 | unsigned int cnt; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 506 | off64_t starting_off; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 507 | int rc = -1; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 508 | char *fname = NULL; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 509 | struct stat statbuf; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 510 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 511 | if (get_crypt_ftr_info(&fname, &starting_off)) { |
| 512 | SLOGE("Unable to get crypt_ftr_info\n"); |
| 513 | return -1; |
| 514 | } |
| 515 | if (fname[0] != '/') { |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 516 | SLOGE("Unexpected value for crypto key location\n"); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 517 | return -1; |
| 518 | } |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 519 | if ( (fd = open(fname, O_RDWR|O_CLOEXEC)) < 0) { |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 520 | SLOGE("Cannot open footer file %s for get\n", fname); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 521 | return -1; |
| 522 | } |
| 523 | |
| 524 | /* Make sure it's 16 Kbytes in length */ |
| 525 | fstat(fd, &statbuf); |
| 526 | if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) { |
| 527 | SLOGE("footer file %s is not the expected size!\n", fname); |
| 528 | goto errout; |
| 529 | } |
| 530 | |
| 531 | /* Seek to the start of the crypt footer */ |
| 532 | if (lseek64(fd, starting_off, SEEK_SET) == -1) { |
| 533 | SLOGE("Cannot seek to real block device footer\n"); |
| 534 | goto errout; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) { |
| 538 | SLOGE("Cannot read real block device footer\n"); |
| 539 | goto errout; |
| 540 | } |
| 541 | |
| 542 | if (crypt_ftr->magic != CRYPT_MNT_MAGIC) { |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 543 | SLOGE("Bad magic for real block device %s\n", fname); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 544 | goto errout; |
| 545 | } |
| 546 | |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 547 | if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) { |
| 548 | SLOGE("Cannot understand major version %d real block device footer; expected %d\n", |
| 549 | crypt_ftr->major_version, CURRENT_MAJOR_VERSION); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 550 | goto errout; |
| 551 | } |
| 552 | |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 553 | if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) { |
| 554 | SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n", |
| 555 | crypt_ftr->minor_version, CURRENT_MINOR_VERSION); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 558 | /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the |
| 559 | * copy on disk before returning. |
| 560 | */ |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 561 | if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 562 | upgrade_crypt_ftr(fd, crypt_ftr, starting_off); |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 563 | } |
| 564 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 565 | /* Success! */ |
| 566 | rc = 0; |
| 567 | |
| 568 | errout: |
| 569 | close(fd); |
| 570 | return rc; |
| 571 | } |
| 572 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 573 | static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr) |
| 574 | { |
| 575 | if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size > |
| 576 | crypt_ftr->persist_data_offset[1]) { |
| 577 | SLOGE("Crypt_ftr persist data regions overlap"); |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) { |
| 582 | SLOGE("Crypt_ftr persist data region 0 starts after region 1"); |
| 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) - |
| 587 | (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) > |
| 588 | CRYPT_FOOTER_OFFSET) { |
| 589 | SLOGE("Persistent data extends past crypto footer"); |
| 590 | return -1; |
| 591 | } |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | static int load_persistent_data(void) |
| 597 | { |
| 598 | struct crypt_mnt_ftr crypt_ftr; |
| 599 | struct crypt_persist_data *pdata = NULL; |
| 600 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 601 | char *fname; |
| 602 | int found = 0; |
| 603 | int fd; |
| 604 | int ret; |
| 605 | int i; |
| 606 | |
| 607 | if (persist_data) { |
| 608 | /* Nothing to do, we've already loaded or initialized it */ |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | |
| 613 | /* If not encrypted, just allocate an empty table and initialize it */ |
| 614 | property_get("ro.crypto.state", encrypted_state, ""); |
| 615 | if (strcmp(encrypted_state, "encrypted") ) { |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 616 | pdata = (crypt_persist_data*)malloc(CRYPT_PERSIST_DATA_SIZE); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 617 | if (pdata) { |
| 618 | init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE); |
| 619 | persist_data = pdata; |
| 620 | return 0; |
| 621 | } |
| 622 | return -1; |
| 623 | } |
| 624 | |
| 625 | if(get_crypt_ftr_and_key(&crypt_ftr)) { |
| 626 | return -1; |
| 627 | } |
| 628 | |
Paul Lawrence | 8561b5c | 2014-03-17 14:10:51 -0700 | [diff] [blame] | 629 | if ((crypt_ftr.major_version < 1) |
| 630 | || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 631 | SLOGE("Crypt_ftr version doesn't support persistent data"); |
| 632 | return -1; |
| 633 | } |
| 634 | |
| 635 | if (get_crypt_ftr_info(&fname, NULL)) { |
| 636 | return -1; |
| 637 | } |
| 638 | |
| 639 | ret = validate_persistent_data_storage(&crypt_ftr); |
| 640 | if (ret) { |
| 641 | return -1; |
| 642 | } |
| 643 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 644 | fd = open(fname, O_RDONLY|O_CLOEXEC); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 645 | if (fd < 0) { |
| 646 | SLOGE("Cannot open %s metadata file", fname); |
| 647 | return -1; |
| 648 | } |
| 649 | |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 650 | pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size); |
Paul Lawrence | 300dae7 | 2016-03-11 11:02:52 -0800 | [diff] [blame] | 651 | if (pdata == NULL) { |
| 652 | SLOGE("Cannot allocate memory for persistent data"); |
| 653 | goto err; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | for (i = 0; i < 2; i++) { |
| 657 | if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) { |
| 658 | SLOGE("Cannot seek to read persistent data on %s", fname); |
| 659 | goto err2; |
| 660 | } |
| 661 | if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){ |
| 662 | SLOGE("Error reading persistent data on iteration %d", i); |
| 663 | goto err2; |
| 664 | } |
| 665 | if (pdata->persist_magic == PERSIST_DATA_MAGIC) { |
| 666 | found = 1; |
| 667 | break; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | if (!found) { |
| 672 | SLOGI("Could not find valid persistent data, creating"); |
| 673 | init_empty_persist_data(pdata, crypt_ftr.persist_data_size); |
| 674 | } |
| 675 | |
| 676 | /* Success */ |
| 677 | persist_data = pdata; |
| 678 | close(fd); |
| 679 | return 0; |
| 680 | |
| 681 | err2: |
| 682 | free(pdata); |
| 683 | |
| 684 | err: |
| 685 | close(fd); |
| 686 | return -1; |
| 687 | } |
| 688 | |
| 689 | static int save_persistent_data(void) |
| 690 | { |
| 691 | struct crypt_mnt_ftr crypt_ftr; |
| 692 | struct crypt_persist_data *pdata; |
| 693 | char *fname; |
| 694 | off64_t write_offset; |
| 695 | off64_t erase_offset; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 696 | int fd; |
| 697 | int ret; |
| 698 | |
| 699 | if (persist_data == NULL) { |
| 700 | SLOGE("No persistent data to save"); |
| 701 | return -1; |
| 702 | } |
| 703 | |
| 704 | if(get_crypt_ftr_and_key(&crypt_ftr)) { |
| 705 | return -1; |
| 706 | } |
| 707 | |
Paul Lawrence | 8561b5c | 2014-03-17 14:10:51 -0700 | [diff] [blame] | 708 | if ((crypt_ftr.major_version < 1) |
| 709 | || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 710 | SLOGE("Crypt_ftr version doesn't support persistent data"); |
| 711 | return -1; |
| 712 | } |
| 713 | |
| 714 | ret = validate_persistent_data_storage(&crypt_ftr); |
| 715 | if (ret) { |
| 716 | return -1; |
| 717 | } |
| 718 | |
| 719 | if (get_crypt_ftr_info(&fname, NULL)) { |
| 720 | return -1; |
| 721 | } |
| 722 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 723 | fd = open(fname, O_RDWR|O_CLOEXEC); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 724 | if (fd < 0) { |
| 725 | SLOGE("Cannot open %s metadata file", fname); |
| 726 | return -1; |
| 727 | } |
| 728 | |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 729 | pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 730 | if (pdata == NULL) { |
| 731 | SLOGE("Cannot allocate persistant data"); |
| 732 | goto err; |
| 733 | } |
| 734 | |
| 735 | if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) { |
| 736 | SLOGE("Cannot seek to read persistent data on %s", fname); |
| 737 | goto err2; |
| 738 | } |
| 739 | |
| 740 | if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) { |
| 741 | SLOGE("Error reading persistent data before save"); |
| 742 | goto err2; |
| 743 | } |
| 744 | |
| 745 | if (pdata->persist_magic == PERSIST_DATA_MAGIC) { |
| 746 | /* The first copy is the curent valid copy, so write to |
| 747 | * the second copy and erase this one */ |
| 748 | write_offset = crypt_ftr.persist_data_offset[1]; |
| 749 | erase_offset = crypt_ftr.persist_data_offset[0]; |
| 750 | } else { |
| 751 | /* The second copy must be the valid copy, so write to |
| 752 | * the first copy, and erase the second */ |
| 753 | write_offset = crypt_ftr.persist_data_offset[0]; |
| 754 | erase_offset = crypt_ftr.persist_data_offset[1]; |
| 755 | } |
| 756 | |
| 757 | /* 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] | 758 | if (lseek64(fd, write_offset, SEEK_SET) < 0) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 759 | SLOGE("Cannot seek to write persistent data"); |
| 760 | goto err2; |
| 761 | } |
| 762 | if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) == |
| 763 | (int) crypt_ftr.persist_data_size) { |
Björn Landström | 96dbee7 | 2015-01-20 12:43:56 +0100 | [diff] [blame] | 764 | if (lseek64(fd, erase_offset, SEEK_SET) < 0) { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 765 | SLOGE("Cannot seek to erase previous persistent data"); |
| 766 | goto err2; |
| 767 | } |
| 768 | fsync(fd); |
| 769 | memset(pdata, 0, crypt_ftr.persist_data_size); |
| 770 | if (unix_write(fd, pdata, crypt_ftr.persist_data_size) != |
| 771 | (int) crypt_ftr.persist_data_size) { |
| 772 | SLOGE("Cannot write to erase previous persistent data"); |
| 773 | goto err2; |
| 774 | } |
| 775 | fsync(fd); |
| 776 | } else { |
| 777 | SLOGE("Cannot write to save persistent data"); |
| 778 | goto err2; |
| 779 | } |
| 780 | |
| 781 | /* Success */ |
| 782 | free(pdata); |
| 783 | close(fd); |
| 784 | return 0; |
| 785 | |
| 786 | err2: |
| 787 | free(pdata); |
| 788 | err: |
| 789 | close(fd); |
| 790 | return -1; |
| 791 | } |
| 792 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 793 | /* Convert a binary key of specified length into an ascii hex string equivalent, |
| 794 | * without the leading 0x and with null termination |
| 795 | */ |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 796 | static void convert_key_to_hex_ascii(const unsigned char *master_key, |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 797 | unsigned int keysize, char *master_key_ascii) { |
| 798 | unsigned int i, a; |
| 799 | unsigned char nibble; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 800 | |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 801 | for (i=0, a=0; i<keysize; i++, a+=2) { |
| 802 | /* For each byte, write out two ascii hex digits */ |
| 803 | nibble = (master_key[i] >> 4) & 0xf; |
| 804 | master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 805 | |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 806 | nibble = master_key[i] & 0xf; |
| 807 | master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30); |
| 808 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 809 | |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 810 | /* Add the null termination */ |
| 811 | master_key_ascii[a] = '\0'; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 812 | |
| 813 | } |
| 814 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 815 | static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, |
| 816 | const unsigned char *master_key, const char *real_blk_name, |
| 817 | const char *name, int fd, const char *extra_params) { |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 818 | alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE]; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 819 | struct dm_ioctl *io; |
| 820 | struct dm_target_spec *tgt; |
| 821 | char *crypt_params; |
| 822 | 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] | 823 | size_t buff_offset; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 824 | int i; |
| 825 | |
| 826 | io = (struct dm_ioctl *) buffer; |
| 827 | |
| 828 | /* Load the mapping table for this device */ |
| 829 | tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)]; |
| 830 | |
| 831 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 832 | io->target_count = 1; |
| 833 | tgt->status = 0; |
| 834 | tgt->sector_start = 0; |
| 835 | tgt->length = crypt_ftr->fs_size; |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 836 | strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME); |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 837 | |
| 838 | crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec); |
| 839 | 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] | 840 | |
| 841 | buff_offset = crypt_params - buffer; |
| 842 | snprintf(crypt_params, sizeof(buffer) - buff_offset, "%s %s 0 %s 0 %s", |
| 843 | crypt_ftr->crypto_type_name, master_key_ascii, real_blk_name, |
| 844 | extra_params); |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 845 | crypt_params += strlen(crypt_params) + 1; |
| 846 | crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */ |
| 847 | tgt->next = crypt_params - buffer; |
| 848 | |
| 849 | for (i = 0; i < TABLE_LOAD_RETRIES; i++) { |
| 850 | if (! ioctl(fd, DM_TABLE_LOAD, io)) { |
| 851 | break; |
| 852 | } |
| 853 | usleep(500000); |
| 854 | } |
| 855 | |
| 856 | if (i == TABLE_LOAD_RETRIES) { |
| 857 | /* We failed to load the table, return an error */ |
| 858 | return -1; |
| 859 | } else { |
| 860 | return i + 1; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | |
| 865 | static int get_dm_crypt_version(int fd, const char *name, int *version) |
| 866 | { |
| 867 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 868 | struct dm_ioctl *io; |
| 869 | struct dm_target_versions *v; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 870 | |
| 871 | io = (struct dm_ioctl *) buffer; |
| 872 | |
| 873 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 874 | |
| 875 | if (ioctl(fd, DM_LIST_VERSIONS, io)) { |
| 876 | return -1; |
| 877 | } |
| 878 | |
| 879 | /* Iterate over the returned versions, looking for name of "crypt". |
| 880 | * When found, get and return the version. |
| 881 | */ |
| 882 | v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)]; |
| 883 | while (v->next) { |
| 884 | if (! strcmp(v->name, "crypt")) { |
| 885 | /* We found the crypt driver, return the version, and get out */ |
| 886 | version[0] = v->version[0]; |
| 887 | version[1] = v->version[1]; |
| 888 | version[2] = v->version[2]; |
| 889 | return 0; |
| 890 | } |
| 891 | v = (struct dm_target_versions *)(((char *)v) + v->next); |
| 892 | } |
| 893 | |
| 894 | return -1; |
| 895 | } |
| 896 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 897 | static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, |
| 898 | const unsigned char *master_key, const char *real_blk_name, |
| 899 | char *crypto_blk_name, const char *name) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 900 | char buffer[DM_CRYPT_BUF_SIZE]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 901 | struct dm_ioctl *io; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 902 | unsigned int minor; |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 903 | int fd=0; |
Daniel Rosenberg | 25a5213 | 2016-02-26 16:44:36 -0800 | [diff] [blame] | 904 | int err; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 905 | int retval = -1; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 906 | int version[3]; |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 907 | const char *extra_params; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 908 | int load_count; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 909 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 910 | if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 911 | SLOGE("Cannot open device-mapper\n"); |
| 912 | goto errout; |
| 913 | } |
| 914 | |
| 915 | io = (struct dm_ioctl *) buffer; |
| 916 | |
| 917 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
Daniel Rosenberg | 25a5213 | 2016-02-26 16:44:36 -0800 | [diff] [blame] | 918 | err = ioctl(fd, DM_DEV_CREATE, io); |
| 919 | if (err) { |
| 920 | SLOGE("Cannot create dm-crypt device %s: %s\n", name, strerror(errno)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 921 | goto errout; |
| 922 | } |
| 923 | |
| 924 | /* Get the device status, in particular, the name of it's device file */ |
| 925 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 926 | if (ioctl(fd, DM_DEV_STATUS, io)) { |
| 927 | SLOGE("Cannot retrieve dm-crypt device status\n"); |
| 928 | goto errout; |
| 929 | } |
| 930 | minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00); |
| 931 | snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor); |
| 932 | |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 933 | extra_params = ""; |
| 934 | if (! get_dm_crypt_version(fd, name, version)) { |
| 935 | /* Support for allow_discards was added in version 1.11.0 */ |
| 936 | if ((version[0] >= 2) || |
| 937 | ((version[0] == 1) && (version[1] >= 11))) { |
| 938 | extra_params = "1 allow_discards"; |
| 939 | SLOGI("Enabling support for allow_discards in dmcrypt.\n"); |
| 940 | } |
Ken Sumrall | e919efe | 2012-09-29 17:07:41 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 943 | load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name, |
| 944 | fd, extra_params); |
| 945 | if (load_count < 0) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 946 | SLOGE("Cannot load dm-crypt mapping table.\n"); |
| 947 | goto errout; |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 948 | } else if (load_count > 1) { |
| 949 | SLOGI("Took %d tries to load dmcrypt table.\n", load_count); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /* Resume this device to activate it */ |
Ken Sumrall | db5e026 | 2013-02-05 17:39:48 -0800 | [diff] [blame] | 953 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 954 | |
| 955 | if (ioctl(fd, DM_DEV_SUSPEND, io)) { |
| 956 | SLOGE("Cannot resume the dm-crypt device\n"); |
| 957 | goto errout; |
| 958 | } |
| 959 | |
| 960 | /* We made it here with no errors. Woot! */ |
| 961 | retval = 0; |
| 962 | |
| 963 | errout: |
| 964 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 965 | |
| 966 | return retval; |
| 967 | } |
| 968 | |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 969 | static int delete_crypto_blk_dev(const char *name) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 970 | { |
| 971 | int fd; |
| 972 | char buffer[DM_CRYPT_BUF_SIZE]; |
| 973 | struct dm_ioctl *io; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 974 | int retval = -1; |
| 975 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 976 | if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 977 | SLOGE("Cannot open device-mapper\n"); |
| 978 | goto errout; |
| 979 | } |
| 980 | |
| 981 | io = (struct dm_ioctl *) buffer; |
| 982 | |
| 983 | ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0); |
| 984 | if (ioctl(fd, DM_DEV_REMOVE, io)) { |
| 985 | SLOGE("Cannot remove dm-crypt device\n"); |
| 986 | goto errout; |
| 987 | } |
| 988 | |
| 989 | /* We made it here with no errors. Woot! */ |
| 990 | retval = 0; |
| 991 | |
| 992 | errout: |
| 993 | close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */ |
| 994 | |
| 995 | return retval; |
| 996 | |
| 997 | } |
| 998 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 999 | static int pbkdf2(const char *passwd, const unsigned char *salt, |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1000 | unsigned char *ikey, void *params UNUSED) |
| 1001 | { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1002 | SLOGI("Using pbkdf2 for cryptfs KDF"); |
| 1003 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1004 | /* 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] | 1005 | return PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN, |
| 1006 | HASH_COUNT, KEY_LEN_BYTES + IV_LEN_BYTES, |
| 1007 | ikey) != 1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1008 | } |
| 1009 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1010 | static int scrypt(const char *passwd, const unsigned char *salt, |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1011 | unsigned char *ikey, void *params) |
| 1012 | { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1013 | SLOGI("Using scrypt for cryptfs KDF"); |
| 1014 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1015 | struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params; |
| 1016 | |
| 1017 | int N = 1 << ftr->N_factor; |
| 1018 | int r = 1 << ftr->r_factor; |
| 1019 | int p = 1 << ftr->p_factor; |
| 1020 | |
| 1021 | /* 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] | 1022 | unsigned int keysize; |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 1023 | crypto_scrypt((const uint8_t*)passwd, strlen(passwd), |
| 1024 | salt, SALT_LEN, N, r, p, ikey, |
| 1025 | KEY_LEN_BYTES + IV_LEN_BYTES); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1026 | |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 1027 | return 0; |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1030 | static int scrypt_keymaster(const char *passwd, const unsigned char *salt, |
| 1031 | unsigned char *ikey, void *params) |
| 1032 | { |
| 1033 | SLOGI("Using scrypt with keymaster for cryptfs KDF"); |
| 1034 | |
| 1035 | int rc; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1036 | size_t signature_size; |
| 1037 | unsigned char* signature; |
| 1038 | struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params; |
| 1039 | |
| 1040 | int N = 1 << ftr->N_factor; |
| 1041 | int r = 1 << ftr->r_factor; |
| 1042 | int p = 1 << ftr->p_factor; |
| 1043 | |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 1044 | rc = crypto_scrypt((const uint8_t*)passwd, strlen(passwd), |
| 1045 | salt, SALT_LEN, N, r, p, ikey, |
| 1046 | KEY_LEN_BYTES + IV_LEN_BYTES); |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1047 | |
| 1048 | if (rc) { |
| 1049 | SLOGE("scrypt failed"); |
| 1050 | return -1; |
| 1051 | } |
| 1052 | |
Shawn Willden | e17a9c4 | 2014-09-08 13:04:08 -0600 | [diff] [blame] | 1053 | if (keymaster_sign_object(ftr, ikey, KEY_LEN_BYTES + IV_LEN_BYTES, |
| 1054 | &signature, &signature_size)) { |
| 1055 | SLOGE("Signing failed"); |
| 1056 | return -1; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN, |
| 1060 | N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES); |
| 1061 | free(signature); |
| 1062 | |
| 1063 | if (rc) { |
| 1064 | SLOGE("scrypt failed"); |
| 1065 | return -1; |
| 1066 | } |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | static int encrypt_master_key(const char *passwd, const unsigned char *salt, |
| 1072 | const unsigned char *decrypted_master_key, |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1073 | unsigned char *encrypted_master_key, |
| 1074 | struct crypt_mnt_ftr *crypt_ftr) |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1075 | { |
| 1076 | unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */ |
| 1077 | EVP_CIPHER_CTX e_ctx; |
| 1078 | int encrypted_len, final_len; |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1079 | int rc = 0; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1080 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1081 | /* 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] | 1082 | get_device_scrypt_params(crypt_ftr); |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1083 | |
| 1084 | switch (crypt_ftr->kdf_type) { |
| 1085 | case KDF_SCRYPT_KEYMASTER: |
| 1086 | if (keymaster_create_key(crypt_ftr)) { |
| 1087 | SLOGE("keymaster_create_key failed"); |
| 1088 | return -1; |
| 1089 | } |
| 1090 | |
| 1091 | if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) { |
| 1092 | SLOGE("scrypt failed"); |
| 1093 | return -1; |
| 1094 | } |
| 1095 | break; |
| 1096 | |
| 1097 | case KDF_SCRYPT: |
| 1098 | if (scrypt(passwd, salt, ikey, crypt_ftr)) { |
| 1099 | SLOGE("scrypt failed"); |
| 1100 | return -1; |
| 1101 | } |
| 1102 | break; |
| 1103 | |
| 1104 | default: |
| 1105 | SLOGE("Invalid kdf_type"); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1106 | return -1; |
| 1107 | } |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1108 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1109 | /* Initialize the decryption engine */ |
Adam Langley | 889c4f1 | 2014-09-03 14:23:13 -0700 | [diff] [blame] | 1110 | EVP_CIPHER_CTX_init(&e_ctx); |
| 1111 | 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] | 1112 | SLOGE("EVP_EncryptInit failed\n"); |
| 1113 | return -1; |
| 1114 | } |
| 1115 | 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] | 1116 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1117 | /* Encrypt the master key */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1118 | if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len, |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 1119 | decrypted_master_key, KEY_LEN_BYTES)) { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1120 | SLOGE("EVP_EncryptUpdate failed\n"); |
| 1121 | return -1; |
| 1122 | } |
Adam Langley | 889c4f1 | 2014-09-03 14:23:13 -0700 | [diff] [blame] | 1123 | 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] | 1124 | SLOGE("EVP_EncryptFinal failed\n"); |
| 1125 | return -1; |
| 1126 | } |
| 1127 | |
| 1128 | if (encrypted_len + final_len != KEY_LEN_BYTES) { |
| 1129 | SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len); |
| 1130 | return -1; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1131 | } |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1132 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1133 | /* Store the scrypt of the intermediate key, so we can validate if it's a |
| 1134 | password error or mount error when things go wrong. |
| 1135 | Note there's no need to check for errors, since if this is incorrect, we |
| 1136 | simply won't wipe userdata, which is the correct default behavior |
| 1137 | */ |
| 1138 | int N = 1 << crypt_ftr->N_factor; |
| 1139 | int r = 1 << crypt_ftr->r_factor; |
| 1140 | int p = 1 << crypt_ftr->p_factor; |
| 1141 | |
| 1142 | rc = crypto_scrypt(ikey, KEY_LEN_BYTES, |
| 1143 | crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p, |
| 1144 | crypt_ftr->scrypted_intermediate_key, |
| 1145 | sizeof(crypt_ftr->scrypted_intermediate_key)); |
| 1146 | |
| 1147 | if (rc) { |
| 1148 | SLOGE("encrypt_master_key: crypto_scrypt failed"); |
| 1149 | } |
| 1150 | |
Thurston Hou Yeen Dang | 06dc311 | 2016-07-18 14:16:37 -0700 | [diff] [blame] | 1151 | EVP_CIPHER_CTX_cleanup(&e_ctx); |
| 1152 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1153 | return 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1154 | } |
| 1155 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 1156 | static int decrypt_master_key_aux(const char *passwd, unsigned char *salt, |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1157 | unsigned char *encrypted_master_key, |
| 1158 | unsigned char *decrypted_master_key, |
| 1159 | kdf_func kdf, void *kdf_params, |
| 1160 | unsigned char** intermediate_key, |
| 1161 | size_t* intermediate_key_size) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1162 | { |
| 1163 | 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] | 1164 | EVP_CIPHER_CTX d_ctx; |
| 1165 | int decrypted_len, final_len; |
| 1166 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1167 | /* Turn the password into an intermediate key and IV that can decrypt the |
| 1168 | master key */ |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1169 | if (kdf(passwd, salt, ikey, kdf_params)) { |
| 1170 | SLOGE("kdf failed"); |
| 1171 | return -1; |
| 1172 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1173 | |
| 1174 | /* Initialize the decryption engine */ |
Adam Langley | 889c4f1 | 2014-09-03 14:23:13 -0700 | [diff] [blame] | 1175 | EVP_CIPHER_CTX_init(&d_ctx); |
| 1176 | 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] | 1177 | return -1; |
| 1178 | } |
| 1179 | EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */ |
| 1180 | /* Decrypt the master key */ |
| 1181 | if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len, |
| 1182 | encrypted_master_key, KEY_LEN_BYTES)) { |
| 1183 | return -1; |
| 1184 | } |
Adam Langley | 889c4f1 | 2014-09-03 14:23:13 -0700 | [diff] [blame] | 1185 | 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] | 1186 | return -1; |
| 1187 | } |
| 1188 | |
| 1189 | if (decrypted_len + final_len != KEY_LEN_BYTES) { |
| 1190 | return -1; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1191 | } |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1192 | |
| 1193 | /* Copy intermediate key if needed by params */ |
| 1194 | if (intermediate_key && intermediate_key_size) { |
| 1195 | *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES); |
Greg Kaiser | e8167af | 2016-04-20 10:50:15 -0700 | [diff] [blame] | 1196 | if (*intermediate_key) { |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1197 | memcpy(*intermediate_key, ikey, KEY_LEN_BYTES); |
| 1198 | *intermediate_key_size = KEY_LEN_BYTES; |
| 1199 | } |
| 1200 | } |
| 1201 | |
Thurston Hou Yeen Dang | 06dc311 | 2016-07-18 14:16:37 -0700 | [diff] [blame] | 1202 | EVP_CIPHER_CTX_cleanup(&d_ctx); |
| 1203 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1204 | return 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1205 | } |
| 1206 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1207 | 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] | 1208 | { |
Paul Lawrence | db3730c | 2015-02-03 13:08:10 -0800 | [diff] [blame] | 1209 | if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER) { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1210 | *kdf = scrypt_keymaster; |
| 1211 | *kdf_params = ftr; |
| 1212 | } else if (ftr->kdf_type == KDF_SCRYPT) { |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1213 | *kdf = scrypt; |
| 1214 | *kdf_params = ftr; |
| 1215 | } else { |
| 1216 | *kdf = pbkdf2; |
| 1217 | *kdf_params = NULL; |
| 1218 | } |
| 1219 | } |
| 1220 | |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 1221 | 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] | 1222 | struct crypt_mnt_ftr *crypt_ftr, |
| 1223 | unsigned char** intermediate_key, |
| 1224 | size_t* intermediate_key_size) |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1225 | { |
| 1226 | kdf_func kdf; |
| 1227 | void *kdf_params; |
| 1228 | int ret; |
| 1229 | |
| 1230 | get_kdf_func(crypt_ftr, &kdf, &kdf_params); |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1231 | ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key, |
| 1232 | decrypted_master_key, kdf, kdf_params, |
| 1233 | intermediate_key, intermediate_key_size); |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1234 | if (ret != 0) { |
| 1235 | SLOGW("failure decrypting master key"); |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | return ret; |
| 1239 | } |
| 1240 | |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 1241 | static int create_encrypted_random_key(const char *passwd, unsigned char *master_key, unsigned char *salt, |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1242 | struct crypt_mnt_ftr *crypt_ftr) { |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1243 | int fd; |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1244 | unsigned char key_buf[KEY_LEN_BYTES]; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1245 | |
| 1246 | /* Get some random bits for a key */ |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1247 | fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC); |
Ken Sumrall | e874407 | 2011-01-18 22:01:55 -0800 | [diff] [blame] | 1248 | read(fd, key_buf, sizeof(key_buf)); |
| 1249 | read(fd, salt, SALT_LEN); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1250 | close(fd); |
| 1251 | |
| 1252 | /* Now encrypt it with the password */ |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1253 | return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
Paul Lawrence | 2f32cda | 2015-05-05 14:28:25 -0700 | [diff] [blame] | 1256 | int wait_and_unmount(const char *mountpoint, bool kill) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1257 | { |
Greg Hackmann | 955653e | 2014-09-24 14:55:20 -0700 | [diff] [blame] | 1258 | int i, err, rc; |
Ken Sumrall | 2eaf713 | 2011-01-14 12:45:48 -0800 | [diff] [blame] | 1259 | #define WAIT_UNMOUNT_COUNT 20 |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1260 | |
| 1261 | /* Now umount the tmpfs filesystem */ |
| 1262 | for (i=0; i<WAIT_UNMOUNT_COUNT; i++) { |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 1263 | if (umount(mountpoint) == 0) { |
| 1264 | break; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1265 | } |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 1266 | |
| 1267 | if (errno == EINVAL) { |
| 1268 | /* EINVAL is returned if the directory is not a mountpoint, |
| 1269 | * i.e. there is no filesystem mounted there. So just get out. |
| 1270 | */ |
| 1271 | break; |
| 1272 | } |
| 1273 | |
| 1274 | err = errno; |
| 1275 | |
| 1276 | /* If allowed, be increasingly aggressive before the last two retries */ |
| 1277 | if (kill) { |
| 1278 | if (i == (WAIT_UNMOUNT_COUNT - 3)) { |
| 1279 | SLOGW("sending SIGHUP to processes with open files\n"); |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1280 | android::vold::KillProcessesWithOpenFiles(mountpoint, SIGTERM); |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 1281 | } else if (i == (WAIT_UNMOUNT_COUNT - 2)) { |
| 1282 | SLOGW("sending SIGKILL to processes with open files\n"); |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1283 | android::vold::KillProcessesWithOpenFiles(mountpoint, SIGKILL); |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | sleep(1); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | if (i < WAIT_UNMOUNT_COUNT) { |
| 1291 | SLOGD("unmounting %s succeeded\n", mountpoint); |
| 1292 | rc = 0; |
| 1293 | } else { |
Jeff Sharkey | 3472e52 | 2017-10-06 18:02:53 -0600 | [diff] [blame] | 1294 | android::vold::KillProcessesWithOpenFiles(mountpoint, 0); |
Greg Hackmann | 955653e | 2014-09-24 14:55:20 -0700 | [diff] [blame] | 1295 | SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1296 | rc = -1; |
| 1297 | } |
| 1298 | |
| 1299 | return rc; |
| 1300 | } |
| 1301 | |
Wei Wang | 42e3810 | 2017-06-07 10:46:12 -0700 | [diff] [blame] | 1302 | static void prep_data_fs(void) |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1303 | { |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 1304 | // NOTE: post_fs_data results in init calling back around to vold, so all |
| 1305 | // callers to this method must be async |
| 1306 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1307 | /* Do the prep of the /data filesystem */ |
| 1308 | property_set("vold.post_fs_data_done", "0"); |
| 1309 | property_set("vold.decrypt", "trigger_post_fs_data"); |
Wei Wang | 42e3810 | 2017-06-07 10:46:12 -0700 | [diff] [blame] | 1310 | SLOGD("Just triggered post_fs_data"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1311 | |
Ken Sumrall | c587269 | 2013-05-14 15:26:31 -0700 | [diff] [blame] | 1312 | /* Wait a max of 50 seconds, hopefully it takes much less */ |
Wei Wang | 42e3810 | 2017-06-07 10:46:12 -0700 | [diff] [blame] | 1313 | while (!android::base::WaitForProperty("vold.post_fs_data_done", |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 1314 | "1", |
Wei Wang | 42e3810 | 2017-06-07 10:46:12 -0700 | [diff] [blame] | 1315 | std::chrono::seconds(15))) { |
| 1316 | /* We timed out to prep /data in time. Continue wait. */ |
| 1317 | SLOGE("waited 15s for vold.post_fs_data_done, still waiting..."); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1318 | } |
Wei Wang | 42e3810 | 2017-06-07 10:46:12 -0700 | [diff] [blame] | 1319 | SLOGD("post_fs_data done"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1320 | } |
| 1321 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1322 | static void cryptfs_set_corrupt() |
| 1323 | { |
| 1324 | // Mark the footer as bad |
| 1325 | struct crypt_mnt_ftr crypt_ftr; |
| 1326 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
| 1327 | SLOGE("Failed to get crypto footer - panic"); |
| 1328 | return; |
| 1329 | } |
| 1330 | |
| 1331 | crypt_ftr.flags |= CRYPT_DATA_CORRUPT; |
| 1332 | if (put_crypt_ftr_and_key(&crypt_ftr)) { |
| 1333 | SLOGE("Failed to set crypto footer - panic"); |
| 1334 | return; |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | static void cryptfs_trigger_restart_min_framework() |
| 1339 | { |
| 1340 | if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) { |
| 1341 | SLOGE("Failed to mount tmpfs on data - panic"); |
| 1342 | return; |
| 1343 | } |
| 1344 | |
| 1345 | if (property_set("vold.decrypt", "trigger_post_fs_data")) { |
| 1346 | SLOGE("Failed to trigger post fs data - panic"); |
| 1347 | return; |
| 1348 | } |
| 1349 | |
| 1350 | if (property_set("vold.decrypt", "trigger_restart_min_framework")) { |
| 1351 | SLOGE("Failed to trigger restart min framework - panic"); |
| 1352 | return; |
| 1353 | } |
| 1354 | } |
| 1355 | |
Paul Lawrence | 8e3f451 | 2014-09-08 10:11:17 -0700 | [diff] [blame] | 1356 | /* returns < 0 on failure */ |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1357 | static int cryptfs_restart_internal(int restart_main) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1358 | { |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 1359 | char crypto_blkdev[MAXPATHLEN]; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 1360 | int rc = -1; |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1361 | static int restart_successful = 0; |
| 1362 | |
| 1363 | /* Validate that it's OK to call this routine */ |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1364 | if (! master_key_saved) { |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1365 | SLOGE("Encrypted filesystem not validated, aborting"); |
| 1366 | return -1; |
| 1367 | } |
| 1368 | |
| 1369 | if (restart_successful) { |
| 1370 | SLOGE("System already restarted with encrypted disk, aborting"); |
| 1371 | return -1; |
| 1372 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1373 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1374 | if (restart_main) { |
| 1375 | /* Here is where we shut down the framework. The init scripts |
| 1376 | * start all services in one of three classes: core, main or late_start. |
| 1377 | * On boot, we start core and main. Now, we stop main, but not core, |
| 1378 | * as core includes vold and a few other really important things that |
| 1379 | * we need to keep running. Once main has stopped, we should be able |
| 1380 | * to umount the tmpfs /data, then mount the encrypted /data. |
| 1381 | * We then restart the class main, and also the class late_start. |
| 1382 | * At the moment, I've only put a few things in late_start that I know |
| 1383 | * are not needed to bring up the framework, and that also cause problems |
| 1384 | * with unmounting the tmpfs /data, but I hope to add add more services |
| 1385 | * to the late_start class as we optimize this to decrease the delay |
| 1386 | * till the user is asked for the password to the filesystem. |
| 1387 | */ |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1388 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1389 | /* The init files are setup to stop the class main when vold.decrypt is |
| 1390 | * set to trigger_reset_main. |
| 1391 | */ |
| 1392 | property_set("vold.decrypt", "trigger_reset_main"); |
| 1393 | SLOGD("Just asked init to shut down class main\n"); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1394 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1395 | /* Ugh, shutting down the framework is not synchronous, so until it |
| 1396 | * can be fixed, this horrible hack will wait a moment for it all to |
| 1397 | * shut down before proceeding. Without it, some devices cannot |
| 1398 | * restart the graphics services. |
| 1399 | */ |
| 1400 | sleep(2); |
| 1401 | } |
Ken Sumrall | 9dedfd4 | 2012-10-09 14:16:59 -0700 | [diff] [blame] | 1402 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1403 | /* Now that the framework is shutdown, we should be able to umount() |
| 1404 | * the tmpfs filesystem, and mount the real one. |
| 1405 | */ |
| 1406 | |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 1407 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, ""); |
| 1408 | if (strlen(crypto_blkdev) == 0) { |
| 1409 | SLOGE("fs_crypto_blkdev not set\n"); |
| 1410 | return -1; |
| 1411 | } |
| 1412 | |
Greg Hackmann | 6e8440f | 2014-10-02 17:18:20 -0700 | [diff] [blame] | 1413 | if (! (rc = wait_and_unmount(DATA_MNT_POINT, true)) ) { |
Doug Zongker | 6fd5771 | 2013-12-17 09:43:23 -0800 | [diff] [blame] | 1414 | /* If ro.crypto.readonly is set to 1, mount the decrypted |
| 1415 | * filesystem readonly. This is used when /data is mounted by |
| 1416 | * recovery mode. |
| 1417 | */ |
| 1418 | char ro_prop[PROPERTY_VALUE_MAX]; |
| 1419 | property_get("ro.crypto.readonly", ro_prop, ""); |
Jeff Sharkey | 95440eb | 2017-09-18 18:19:28 -0600 | [diff] [blame] | 1420 | if (strlen(ro_prop) > 0 && std::stoi(ro_prop)) { |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 1421 | struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT); |
Doug Zongker | 6fd5771 | 2013-12-17 09:43:23 -0800 | [diff] [blame] | 1422 | rec->flags |= MS_RDONLY; |
| 1423 | } |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 1424 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1425 | /* If that succeeded, then mount the decrypted filesystem */ |
Paul Lawrence | 8e3f451 | 2014-09-08 10:11:17 -0700 | [diff] [blame] | 1426 | int retries = RETRY_MOUNT_ATTEMPTS; |
| 1427 | int mount_rc; |
Jeff Vander Stoep | df72575 | 2016-01-29 15:34:43 -0800 | [diff] [blame] | 1428 | |
| 1429 | /* |
| 1430 | * fs_mgr_do_mount runs fsck. Use setexeccon to run trusted |
| 1431 | * partitions in the fsck domain. |
| 1432 | */ |
| 1433 | if (setexeccon(secontextFsck())){ |
| 1434 | SLOGE("Failed to setexeccon"); |
| 1435 | return -1; |
| 1436 | } |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 1437 | while ((mount_rc = fs_mgr_do_mount(fstab_default, DATA_MNT_POINT, |
Paul Lawrence | 8e3f451 | 2014-09-08 10:11:17 -0700 | [diff] [blame] | 1438 | crypto_blkdev, 0)) |
| 1439 | != 0) { |
| 1440 | if (mount_rc == FS_MGR_DOMNT_BUSY) { |
| 1441 | /* TODO: invoke something similar to |
| 1442 | Process::killProcessWithOpenFiles(DATA_MNT_POINT, |
| 1443 | retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */ |
| 1444 | SLOGI("Failed to mount %s because it is busy - waiting", |
| 1445 | crypto_blkdev); |
| 1446 | if (--retries) { |
| 1447 | sleep(RETRY_MOUNT_DELAY_SECONDS); |
| 1448 | } else { |
| 1449 | /* Let's hope that a reboot clears away whatever is keeping |
| 1450 | the mount busy */ |
Josh Gao | fec4437 | 2017-08-28 13:22:55 -0700 | [diff] [blame] | 1451 | cryptfs_reboot(RebootType::reboot); |
Paul Lawrence | 8e3f451 | 2014-09-08 10:11:17 -0700 | [diff] [blame] | 1452 | } |
| 1453 | } else { |
| 1454 | SLOGE("Failed to mount decrypted data"); |
| 1455 | cryptfs_set_corrupt(); |
| 1456 | cryptfs_trigger_restart_min_framework(); |
| 1457 | SLOGI("Started framework to offer wipe"); |
Jeff Vander Stoep | df72575 | 2016-01-29 15:34:43 -0800 | [diff] [blame] | 1458 | if (setexeccon(NULL)) { |
| 1459 | SLOGE("Failed to setexeccon"); |
| 1460 | } |
Paul Lawrence | 8e3f451 | 2014-09-08 10:11:17 -0700 | [diff] [blame] | 1461 | return -1; |
| 1462 | } |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1463 | } |
Jeff Vander Stoep | df72575 | 2016-01-29 15:34:43 -0800 | [diff] [blame] | 1464 | if (setexeccon(NULL)) { |
| 1465 | SLOGE("Failed to setexeccon"); |
| 1466 | return -1; |
| 1467 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1468 | |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1469 | /* Create necessary paths on /data */ |
Wei Wang | 42e3810 | 2017-06-07 10:46:12 -0700 | [diff] [blame] | 1470 | prep_data_fs(); |
Seigo Nonaka | e2ef0c0 | 2016-06-20 17:05:40 +0900 | [diff] [blame] | 1471 | property_set("vold.decrypt", "trigger_load_persist_props"); |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1472 | |
| 1473 | /* startup service classes main and late_start */ |
| 1474 | property_set("vold.decrypt", "trigger_restart_framework"); |
| 1475 | SLOGD("Just triggered restart_framework\n"); |
| 1476 | |
| 1477 | /* Give it a few moments to get started */ |
| 1478 | sleep(1); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1479 | } |
| 1480 | |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1481 | if (rc == 0) { |
| 1482 | restart_successful = 1; |
| 1483 | } |
| 1484 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1485 | return rc; |
| 1486 | } |
| 1487 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1488 | int cryptfs_restart(void) |
| 1489 | { |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 1490 | SLOGI("cryptfs_restart"); |
Paul Crowley | 38132a1 | 2016-02-09 09:50:32 +0000 | [diff] [blame] | 1491 | if (e4crypt_is_native()) { |
Paul Lawrence | 7b6b565 | 2016-02-02 11:14:59 -0800 | [diff] [blame] | 1492 | SLOGE("cryptfs_restart not valid for file encryption:"); |
| 1493 | return -1; |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 1494 | } |
| 1495 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1496 | /* Call internal implementation forcing a restart of main service group */ |
| 1497 | return cryptfs_restart_internal(1); |
| 1498 | } |
| 1499 | |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 1500 | static int do_crypto_complete(const char *mount_point) |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1501 | { |
| 1502 | struct crypt_mnt_ftr crypt_ftr; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1503 | char encrypted_state[PROPERTY_VALUE_MAX]; |
Ken Sumrall | e1a4585 | 2011-12-14 21:24:27 -0800 | [diff] [blame] | 1504 | char key_loc[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1505 | |
| 1506 | property_get("ro.crypto.state", encrypted_state, ""); |
| 1507 | if (strcmp(encrypted_state, "encrypted") ) { |
| 1508 | SLOGE("not running with encryption, aborting"); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1509 | return CRYPTO_COMPLETE_NOT_ENCRYPTED; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1510 | } |
| 1511 | |
Paul Lawrence | 7b6b565 | 2016-02-02 11:14:59 -0800 | [diff] [blame] | 1512 | // crypto_complete is full disk encrypted status |
Paul Crowley | 38132a1 | 2016-02-09 09:50:32 +0000 | [diff] [blame] | 1513 | if (e4crypt_is_native()) { |
Paul Lawrence | 7b6b565 | 2016-02-02 11:14:59 -0800 | [diff] [blame] | 1514 | return CRYPTO_COMPLETE_NOT_ENCRYPTED; |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 1515 | } |
| 1516 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 1517 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 1518 | fs_mgr_get_crypt_info(fstab_default, key_loc, 0, sizeof(key_loc)); |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 1519 | |
Ken Sumrall | e1a4585 | 2011-12-14 21:24:27 -0800 | [diff] [blame] | 1520 | /* |
| 1521 | * Only report this error if key_loc is a file and it exists. |
| 1522 | * If the device was never encrypted, and /data is not mountable for |
| 1523 | * some reason, returning 1 should prevent the UI from presenting the |
| 1524 | * a "enter password" screen, or worse, a "press button to wipe the |
| 1525 | * device" screen. |
| 1526 | */ |
| 1527 | if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) { |
| 1528 | SLOGE("master key file does not exist, aborting"); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1529 | return CRYPTO_COMPLETE_NOT_ENCRYPTED; |
Ken Sumrall | e1a4585 | 2011-12-14 21:24:27 -0800 | [diff] [blame] | 1530 | } else { |
| 1531 | SLOGE("Error getting crypt footer and key\n"); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1532 | return CRYPTO_COMPLETE_BAD_METADATA; |
Ken Sumrall | e1a4585 | 2011-12-14 21:24:27 -0800 | [diff] [blame] | 1533 | } |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1534 | } |
| 1535 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1536 | // Test for possible error flags |
| 1537 | if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS){ |
| 1538 | SLOGE("Encryption process is partway completed\n"); |
| 1539 | return CRYPTO_COMPLETE_PARTIAL; |
| 1540 | } |
| 1541 | |
| 1542 | if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE){ |
| 1543 | SLOGE("Encryption process was interrupted but cannot continue\n"); |
| 1544 | return CRYPTO_COMPLETE_INCONSISTENT; |
| 1545 | } |
| 1546 | |
| 1547 | if (crypt_ftr.flags & CRYPT_DATA_CORRUPT){ |
| 1548 | SLOGE("Encryption is successful but data is corrupt\n"); |
| 1549 | return CRYPTO_COMPLETE_CORRUPT; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | /* We passed the test! We shall diminish, and return to the west */ |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1553 | return CRYPTO_COMPLETE_ENCRYPTED; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1554 | } |
| 1555 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1556 | static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr, |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 1557 | const char *passwd, const char *mount_point, const char *label) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1558 | { |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1559 | /* 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] | 1560 | unsigned char decrypted_master_key[32]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1561 | char crypto_blkdev[MAXPATHLEN]; |
| 1562 | char real_blkdev[MAXPATHLEN]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1563 | char tmp_mount_point[64]; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1564 | unsigned int orig_failed_decrypt_count; |
| 1565 | int rc; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1566 | int use_keymaster = 0; |
| 1567 | int upgrade = 0; |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1568 | unsigned char* intermediate_key = 0; |
| 1569 | size_t intermediate_key_size = 0; |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 1570 | int N = 1 << crypt_ftr->N_factor; |
| 1571 | int r = 1 << crypt_ftr->r_factor; |
| 1572 | int p = 1 << crypt_ftr->p_factor; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1573 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1574 | SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size); |
| 1575 | orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count; |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 1576 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1577 | if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) { |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1578 | if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr, |
| 1579 | &intermediate_key, &intermediate_key_size)) { |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1580 | SLOGE("Failed to decrypt master key\n"); |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1581 | rc = -1; |
| 1582 | goto errout; |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1583 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1584 | } |
| 1585 | |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 1586 | fs_mgr_get_crypt_info(fstab_default, 0, real_blkdev, sizeof(real_blkdev)); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1587 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1588 | // Create crypto block device - all (non fatal) code paths |
| 1589 | // need it |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1590 | if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, |
| 1591 | real_blkdev, crypto_blkdev, label)) { |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1592 | SLOGE("Error creating decrypted block device\n"); |
| 1593 | rc = -1; |
| 1594 | goto errout; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1595 | } |
| 1596 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1597 | /* Work out if the problem is the password or the data */ |
| 1598 | unsigned char scrypted_intermediate_key[sizeof(crypt_ftr-> |
| 1599 | scrypted_intermediate_key)]; |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1600 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1601 | rc = crypto_scrypt(intermediate_key, intermediate_key_size, |
| 1602 | crypt_ftr->salt, sizeof(crypt_ftr->salt), |
| 1603 | N, r, p, scrypted_intermediate_key, |
| 1604 | sizeof(scrypted_intermediate_key)); |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1605 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1606 | // Does the key match the crypto footer? |
| 1607 | if (rc == 0 && memcmp(scrypted_intermediate_key, |
| 1608 | crypt_ftr->scrypted_intermediate_key, |
| 1609 | sizeof(scrypted_intermediate_key)) == 0) { |
| 1610 | SLOGI("Password matches"); |
| 1611 | rc = 0; |
| 1612 | } else { |
| 1613 | /* Try mounting the file system anyway, just in case the problem's with |
| 1614 | * the footer, not the key. */ |
George Burgess IV | 605d7ae | 2016-02-29 13:39:17 -0800 | [diff] [blame] | 1615 | snprintf(tmp_mount_point, sizeof(tmp_mount_point), "%s/tmp_mnt", |
| 1616 | mount_point); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1617 | mkdir(tmp_mount_point, 0755); |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 1618 | if (fs_mgr_do_mount(fstab_default, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) { |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1619 | SLOGE("Error temp mounting decrypted block device\n"); |
| 1620 | delete_crypto_blk_dev(label); |
| 1621 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1622 | rc = ++crypt_ftr->failed_decrypt_count; |
| 1623 | put_crypt_ftr_and_key(crypt_ftr); |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1624 | } else { |
| 1625 | /* Success! */ |
| 1626 | SLOGI("Password did not match but decrypted drive mounted - continue"); |
| 1627 | umount(tmp_mount_point); |
| 1628 | rc = 0; |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1629 | } |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | if (rc == 0) { |
| 1633 | crypt_ftr->failed_decrypt_count = 0; |
Paul Lawrence | 72b8b82 | 2014-10-05 12:57:37 -0700 | [diff] [blame] | 1634 | if (orig_failed_decrypt_count != 0) { |
| 1635 | put_crypt_ftr_and_key(crypt_ftr); |
| 1636 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1637 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1638 | /* Save the name of the crypto block device |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1639 | * so we can mount it when restarting the framework. */ |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 1640 | property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1641 | |
| 1642 | /* Also save a the master key so we can reencrypted the key |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1643 | * the key when we want to change the password on it. */ |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1644 | memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES); |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 1645 | saved_mount_point = strdup(mount_point); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1646 | master_key_saved = 1; |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1647 | SLOGD("%s(): Master key saved\n", __FUNCTION__); |
Ken Sumrall | 6864b7e | 2011-01-14 15:20:02 -0800 | [diff] [blame] | 1648 | rc = 0; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1649 | |
Paul Lawrence | 74f29f1 | 2014-08-28 15:54:10 -0700 | [diff] [blame] | 1650 | // Upgrade if we're not using the latest KDF. |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1651 | use_keymaster = keymaster_check_compatibility(); |
| 1652 | if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) { |
Shawn Willden | 47ba10d | 2014-09-03 17:07:06 -0600 | [diff] [blame] | 1653 | // Don't allow downgrade |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1654 | } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) { |
| 1655 | crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER; |
| 1656 | upgrade = 1; |
| 1657 | } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1658 | crypt_ftr->kdf_type = KDF_SCRYPT; |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1659 | upgrade = 1; |
| 1660 | } |
| 1661 | |
| 1662 | if (upgrade) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1663 | rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key, |
| 1664 | crypt_ftr->master_key, crypt_ftr); |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1665 | if (!rc) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1666 | rc = put_crypt_ftr_and_key(crypt_ftr); |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1667 | } |
| 1668 | SLOGD("Key Derivation Function upgrade: rc=%d\n", rc); |
Paul Lawrence | b2f682b | 2014-09-08 11:28:19 -0700 | [diff] [blame] | 1669 | |
| 1670 | // Do not fail even if upgrade failed - machine is bootable |
| 1671 | // Note that if this code is ever hit, there is a *serious* problem |
| 1672 | // since KDFs should never fail. You *must* fix the kdf before |
| 1673 | // proceeding! |
| 1674 | if (rc) { |
| 1675 | SLOGW("Upgrade failed with error %d," |
| 1676 | " but continuing with previous state", |
| 1677 | rc); |
| 1678 | rc = 0; |
| 1679 | } |
JP Abgrall | 7bdfa52 | 2013-11-15 13:42:56 -0800 | [diff] [blame] | 1680 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1681 | } |
| 1682 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1683 | errout: |
| 1684 | if (intermediate_key) { |
| 1685 | memset(intermediate_key, 0, intermediate_key_size); |
| 1686 | free(intermediate_key); |
| 1687 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1688 | return rc; |
| 1689 | } |
| 1690 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1691 | /* |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1692 | * Called by vold when it's asked to mount an encrypted external |
| 1693 | * storage volume. The incoming partition has no crypto header/footer, |
| 1694 | * as any metadata is been stored in a separate, small partition. |
| 1695 | * |
| 1696 | * out_crypto_blkdev must be MAXPATHLEN. |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1697 | */ |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1698 | int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev, |
| 1699 | const unsigned char* key, int keysize, char* out_crypto_blkdev) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1700 | int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC); |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1701 | if (fd == -1) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1702 | SLOGE("Failed to open %s: %s", real_blkdev, strerror(errno)); |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1703 | return -1; |
| 1704 | } |
| 1705 | |
| 1706 | unsigned long nr_sec = 0; |
| 1707 | get_blkdev_size(fd, &nr_sec); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1708 | close(fd); |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 1709 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1710 | if (nr_sec == 0) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1711 | SLOGE("Failed to get size of %s: %s", real_blkdev, strerror(errno)); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1712 | return -1; |
| 1713 | } |
| 1714 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1715 | struct crypt_mnt_ftr ext_crypt_ftr; |
| 1716 | memset(&ext_crypt_ftr, 0, sizeof(ext_crypt_ftr)); |
| 1717 | ext_crypt_ftr.fs_size = nr_sec; |
| 1718 | ext_crypt_ftr.keysize = keysize; |
Jeff Sharkey | 32ebb73 | 2017-03-27 16:18:50 -0600 | [diff] [blame] | 1719 | strlcpy((char*) ext_crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256", |
| 1720 | MAX_CRYPTO_TYPE_NAME_LEN); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1721 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1722 | return create_crypto_blk_dev(&ext_crypt_ftr, key, real_blkdev, |
| 1723 | out_crypto_blkdev, label); |
| 1724 | } |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1725 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 1726 | /* |
| 1727 | * Called by vold when it's asked to unmount an encrypted external |
| 1728 | * storage volume. |
| 1729 | */ |
| 1730 | int cryptfs_revert_ext_volume(const char* label) { |
| 1731 | return delete_crypto_blk_dev((char*) label); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1732 | } |
| 1733 | |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 1734 | int cryptfs_crypto_complete(void) |
| 1735 | { |
| 1736 | return do_crypto_complete("/data"); |
| 1737 | } |
| 1738 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1739 | int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr) |
| 1740 | { |
| 1741 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 1742 | property_get("ro.crypto.state", encrypted_state, ""); |
| 1743 | if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) { |
| 1744 | SLOGE("encrypted fs already validated or not running with encryption," |
| 1745 | " aborting"); |
| 1746 | return -1; |
| 1747 | } |
| 1748 | |
| 1749 | if (get_crypt_ftr_and_key(crypt_ftr)) { |
| 1750 | SLOGE("Error getting crypt footer and key"); |
| 1751 | return -1; |
| 1752 | } |
| 1753 | |
| 1754 | return 0; |
| 1755 | } |
| 1756 | |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 1757 | int cryptfs_check_passwd(const char *passwd) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1758 | { |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 1759 | SLOGI("cryptfs_check_passwd"); |
Paul Crowley | 38132a1 | 2016-02-09 09:50:32 +0000 | [diff] [blame] | 1760 | if (e4crypt_is_native()) { |
Paul Lawrence | 7b6b565 | 2016-02-02 11:14:59 -0800 | [diff] [blame] | 1761 | SLOGE("cryptfs_check_passwd not valid for file encryption"); |
| 1762 | return -1; |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 1763 | } |
| 1764 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1765 | struct crypt_mnt_ftr crypt_ftr; |
| 1766 | int rc; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1767 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1768 | rc = check_unmounted_and_get_ftr(&crypt_ftr); |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 1769 | if (rc) { |
| 1770 | SLOGE("Could not get footer"); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 1771 | return rc; |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 1772 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1773 | |
Paul Lawrence | 3bd36d5 | 2015-06-09 13:37:44 -0700 | [diff] [blame] | 1774 | rc = test_mount_encrypted_fs(&crypt_ftr, passwd, |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 1775 | DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE); |
| 1776 | if (rc) { |
| 1777 | SLOGE("Password did not match"); |
| 1778 | return rc; |
| 1779 | } |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 1780 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 1781 | if (crypt_ftr.flags & CRYPT_FORCE_COMPLETE) { |
| 1782 | // Here we have a default actual password but a real password |
| 1783 | // we must test against the scrypted value |
| 1784 | // First, we must delete the crypto block device that |
| 1785 | // test_mount_encrypted_fs leaves behind as a side effect |
| 1786 | delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE); |
| 1787 | rc = test_mount_encrypted_fs(&crypt_ftr, DEFAULT_PASSWORD, |
| 1788 | DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE); |
| 1789 | if (rc) { |
| 1790 | SLOGE("Default password did not match on reboot encryption"); |
| 1791 | return rc; |
| 1792 | } |
| 1793 | |
| 1794 | crypt_ftr.flags &= ~CRYPT_FORCE_COMPLETE; |
| 1795 | put_crypt_ftr_and_key(&crypt_ftr); |
| 1796 | rc = cryptfs_changepw(crypt_ftr.crypt_type, passwd); |
| 1797 | if (rc) { |
| 1798 | SLOGE("Could not change password on reboot encryption"); |
| 1799 | return rc; |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | if (crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) { |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 1804 | cryptfs_clear_password(); |
| 1805 | password = strdup(passwd); |
| 1806 | struct timespec now; |
| 1807 | clock_gettime(CLOCK_BOOTTIME, &now); |
| 1808 | password_expiry_time = now.tv_sec + password_max_age_seconds; |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 1809 | } |
| 1810 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1811 | return rc; |
| 1812 | } |
| 1813 | |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 1814 | int cryptfs_verify_passwd(const char *passwd) |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 1815 | { |
| 1816 | struct crypt_mnt_ftr crypt_ftr; |
| 1817 | /* 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] | 1818 | unsigned char decrypted_master_key[32]; |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 1819 | char encrypted_state[PROPERTY_VALUE_MAX]; |
| 1820 | int rc; |
| 1821 | |
| 1822 | property_get("ro.crypto.state", encrypted_state, ""); |
| 1823 | if (strcmp(encrypted_state, "encrypted") ) { |
| 1824 | SLOGE("device not encrypted, aborting"); |
| 1825 | return -2; |
| 1826 | } |
| 1827 | |
| 1828 | if (!master_key_saved) { |
| 1829 | SLOGE("encrypted fs not yet mounted, aborting"); |
| 1830 | return -1; |
| 1831 | } |
| 1832 | |
| 1833 | if (!saved_mount_point) { |
| 1834 | SLOGE("encrypted fs failed to save mount point, aborting"); |
| 1835 | return -1; |
| 1836 | } |
| 1837 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 1838 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 1839 | SLOGE("Error getting crypt footer and key\n"); |
| 1840 | return -1; |
| 1841 | } |
| 1842 | |
| 1843 | if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) { |
| 1844 | /* If the device has no password, then just say the password is valid */ |
| 1845 | rc = 0; |
| 1846 | } else { |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 1847 | decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0); |
Ken Sumrall | 3ad9072 | 2011-10-04 20:38:29 -0700 | [diff] [blame] | 1848 | if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) { |
| 1849 | /* They match, the password is correct */ |
| 1850 | rc = 0; |
| 1851 | } else { |
| 1852 | /* If incorrect, sleep for a bit to prevent dictionary attacks */ |
| 1853 | sleep(1); |
| 1854 | rc = 1; |
| 1855 | } |
| 1856 | } |
| 1857 | |
| 1858 | return rc; |
| 1859 | } |
| 1860 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1861 | /* Initialize a crypt_mnt_ftr structure. The keysize is |
| 1862 | * defaulted to 16 bytes, and the filesystem size to 0. |
| 1863 | * Presumably, at a minimum, the caller will update the |
| 1864 | * filesystem size and crypto_type_name after calling this function. |
| 1865 | */ |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1866 | static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1867 | { |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 1868 | off64_t off; |
| 1869 | |
| 1870 | memset(ftr, 0, sizeof(struct crypt_mnt_ftr)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1871 | ftr->magic = CRYPT_MNT_MAGIC; |
Kenny Root | c96a5f8 | 2013-06-14 12:08:28 -0700 | [diff] [blame] | 1872 | ftr->major_version = CURRENT_MAJOR_VERSION; |
| 1873 | ftr->minor_version = CURRENT_MINOR_VERSION; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1874 | ftr->ftr_size = sizeof(struct crypt_mnt_ftr); |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 1875 | ftr->keysize = KEY_LEN_BYTES; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 1876 | |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1877 | switch (keymaster_check_compatibility()) { |
| 1878 | case 1: |
| 1879 | ftr->kdf_type = KDF_SCRYPT_KEYMASTER; |
| 1880 | break; |
| 1881 | |
| 1882 | case 0: |
| 1883 | ftr->kdf_type = KDF_SCRYPT; |
| 1884 | break; |
| 1885 | |
| 1886 | default: |
| 1887 | SLOGE("keymaster_check_compatibility failed"); |
| 1888 | return -1; |
| 1889 | } |
| 1890 | |
Kenny Root | c4c70f1 | 2013-06-14 12:11:38 -0700 | [diff] [blame] | 1891 | get_device_scrypt_params(ftr); |
| 1892 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 1893 | ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE; |
| 1894 | if (get_crypt_ftr_info(NULL, &off) == 0) { |
| 1895 | ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET; |
| 1896 | ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET + |
| 1897 | ftr->persist_data_size; |
| 1898 | } |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 1899 | |
| 1900 | return 0; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1901 | } |
| 1902 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1903 | 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] | 1904 | { |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 1905 | const char *args[10]; |
| 1906 | char size_str[32]; /* Must be large enough to hold a %lld and null byte */ |
| 1907 | int num_args; |
| 1908 | int status; |
| 1909 | int tmp; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1910 | int rc = -1; |
| 1911 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1912 | if (type == EXT4_FS) { |
Adrien Schildknecht | e0f409c | 2016-11-21 15:14:37 -0800 | [diff] [blame] | 1913 | args[0] = "/system/bin/mke2fs"; |
| 1914 | args[1] = "-M"; |
| 1915 | args[2] = "/data"; |
| 1916 | args[3] = "-b"; |
| 1917 | args[4] = "4096"; |
| 1918 | args[5] = "-t"; |
| 1919 | args[6] = "ext4"; |
| 1920 | args[7] = crypto_blkdev; |
| 1921 | snprintf(size_str, sizeof(size_str), "%" PRId64, size / (4096 / 512)); |
| 1922 | args[8] = size_str; |
| 1923 | num_args = 9; |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 1924 | SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n", |
| 1925 | args[0], args[1], args[2], args[3], args[4], args[5]); |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 1926 | } else if (type == F2FS_FS) { |
Jaegeuk Kim | ab48bc9 | 2017-06-05 10:22:04 -0700 | [diff] [blame] | 1927 | args[0] = "/system/bin/make_f2fs"; |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 1928 | args[1] = "-t"; |
| 1929 | args[2] = "-d1"; |
Jaegeuk Kim | ab48bc9 | 2017-06-05 10:22:04 -0700 | [diff] [blame] | 1930 | args[3] = "-f"; |
| 1931 | args[4] = "-O encrypt"; |
| 1932 | args[5] = crypto_blkdev; |
Elliott Hughes | 7373716 | 2014-06-25 17:27:42 -0700 | [diff] [blame] | 1933 | snprintf(size_str, sizeof(size_str), "%" PRId64, size); |
Jaegeuk Kim | ab48bc9 | 2017-06-05 10:22:04 -0700 | [diff] [blame] | 1934 | args[6] = size_str; |
| 1935 | num_args = 7; |
| 1936 | SLOGI("Making empty filesystem with command %s %s %s %s %s %s %s\n", |
| 1937 | args[0], args[1], args[2], args[3], args[4], args[5], args[6]); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 1938 | } else { |
| 1939 | SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type); |
| 1940 | return -1; |
| 1941 | } |
| 1942 | |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 1943 | tmp = android_fork_execvp(num_args, (char **)args, &status, false, true); |
| 1944 | |
| 1945 | if (tmp != 0) { |
| 1946 | 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] | 1947 | } else { |
Ken Sumrall | e550f78 | 2013-08-20 13:48:23 -0700 | [diff] [blame] | 1948 | if (WIFEXITED(status)) { |
| 1949 | if (WEXITSTATUS(status)) { |
| 1950 | SLOGE("Error creating filesystem on %s, exit status %d ", |
| 1951 | crypto_blkdev, WEXITSTATUS(status)); |
| 1952 | } else { |
| 1953 | SLOGD("Successfully created filesystem on %s\n", crypto_blkdev); |
| 1954 | rc = 0; |
| 1955 | } |
| 1956 | } else { |
| 1957 | SLOGE("Error creating filesystem on %s, did not exit normally\n", crypto_blkdev); |
| 1958 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | return rc; |
| 1962 | } |
| 1963 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 1964 | #define CRYPTO_ENABLE_WIPE 1 |
| 1965 | #define CRYPTO_ENABLE_INPLACE 2 |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 1966 | |
| 1967 | #define FRAMEWORK_BOOT_WAIT 60 |
| 1968 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 1969 | static int cryptfs_SHA256_fileblock(const char* filename, __le8* buf) |
| 1970 | { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 1971 | int fd = open(filename, O_RDONLY|O_CLOEXEC); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 1972 | if (fd == -1) { |
| 1973 | SLOGE("Error opening file %s", filename); |
| 1974 | return -1; |
| 1975 | } |
| 1976 | |
| 1977 | char block[CRYPT_INPLACE_BUFSIZE]; |
| 1978 | memset(block, 0, sizeof(block)); |
| 1979 | if (unix_read(fd, block, sizeof(block)) < 0) { |
| 1980 | SLOGE("Error reading file %s", filename); |
| 1981 | close(fd); |
| 1982 | return -1; |
| 1983 | } |
| 1984 | |
| 1985 | close(fd); |
| 1986 | |
| 1987 | SHA256_CTX c; |
| 1988 | SHA256_Init(&c); |
| 1989 | SHA256_Update(&c, block, sizeof(block)); |
| 1990 | SHA256_Final(buf, &c); |
| 1991 | |
| 1992 | return 0; |
| 1993 | } |
| 1994 | |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 1995 | static int get_fs_type(struct fstab_rec *rec) |
| 1996 | { |
| 1997 | if (!strcmp(rec->fs_type, "ext4")) { |
| 1998 | return EXT4_FS; |
| 1999 | } else if (!strcmp(rec->fs_type, "f2fs")) { |
| 2000 | return F2FS_FS; |
| 2001 | } else { |
| 2002 | return -1; |
| 2003 | } |
| 2004 | } |
| 2005 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2006 | static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr *crypt_ftr, int how, |
| 2007 | char *crypto_blkdev, char *real_blkdev, |
| 2008 | int previously_encrypted_upto) |
| 2009 | { |
| 2010 | off64_t cur_encryption_done=0, tot_encryption_size=0; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 2011 | int rc = -1; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2012 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2013 | /* The size of the userdata partition, and add in the vold volumes below */ |
| 2014 | tot_encryption_size = crypt_ftr->fs_size; |
| 2015 | |
| 2016 | if (how == CRYPTO_ENABLE_WIPE) { |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 2017 | struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT); |
JP Abgrall | 62c7af3 | 2014-06-16 13:01:23 -0700 | [diff] [blame] | 2018 | int fs_type = get_fs_type(rec); |
| 2019 | if (fs_type < 0) { |
| 2020 | SLOGE("cryptfs_enable: unsupported fs type %s\n", rec->fs_type); |
| 2021 | return -1; |
| 2022 | } |
| 2023 | rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr->fs_size, fs_type); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2024 | } else if (how == CRYPTO_ENABLE_INPLACE) { |
| 2025 | rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, |
| 2026 | crypt_ftr->fs_size, &cur_encryption_done, |
| 2027 | tot_encryption_size, |
| 2028 | previously_encrypted_upto); |
| 2029 | |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2030 | if (rc == ENABLE_INPLACE_ERR_DEV) { |
| 2031 | /* Hack for b/17898962 */ |
| 2032 | SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n"); |
Josh Gao | fec4437 | 2017-08-28 13:22:55 -0700 | [diff] [blame] | 2033 | cryptfs_reboot(RebootType::reboot); |
JP Abgrall | 7fc1de8 | 2014-10-10 18:43:41 -0700 | [diff] [blame] | 2034 | } |
| 2035 | |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 2036 | if (!rc) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2037 | crypt_ftr->encrypted_upto = cur_encryption_done; |
| 2038 | } |
| 2039 | |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 2040 | if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2041 | /* The inplace routine never actually sets the progress to 100% due |
| 2042 | * to the round down nature of integer division, so set it here */ |
| 2043 | property_set("vold.encrypt_progress", "100"); |
| 2044 | } |
| 2045 | } else { |
| 2046 | /* Shouldn't happen */ |
| 2047 | SLOGE("cryptfs_enable: internal error, unknown option\n"); |
| 2048 | rc = -1; |
| 2049 | } |
| 2050 | |
| 2051 | return rc; |
| 2052 | } |
| 2053 | |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 2054 | int cryptfs_enable_internal(const char *howarg, int crypt_type, const char *passwd, |
Paul Lawrence | 569649f | 2015-09-09 12:13:00 -0700 | [diff] [blame] | 2055 | int no_ui) |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2056 | { |
| 2057 | int how = 0; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2058 | char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN]; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2059 | unsigned char decrypted_master_key[KEY_LEN_BYTES]; |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 2060 | int rc=-1, i; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2061 | struct crypt_mnt_ftr crypt_ftr; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2062 | struct crypt_persist_data *pdata; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2063 | char encrypted_state[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 2064 | char lockid[32] = { 0 }; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2065 | char key_loc[PROPERTY_VALUE_MAX]; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2066 | int num_vols; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2067 | off64_t previously_encrypted_upto = 0; |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2068 | bool rebootEncryption = false; |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 2069 | bool onlyCreateHeader = false; |
| 2070 | int fd = -1; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2071 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2072 | if (!strcmp(howarg, "wipe")) { |
| 2073 | how = CRYPTO_ENABLE_WIPE; |
| 2074 | } else if (! strcmp(howarg, "inplace")) { |
| 2075 | how = CRYPTO_ENABLE_INPLACE; |
| 2076 | } else { |
| 2077 | /* Shouldn't happen, as CommandListener vets the args */ |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2078 | goto error_unencrypted; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2079 | } |
| 2080 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2081 | if (how == CRYPTO_ENABLE_INPLACE |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2082 | && get_crypt_ftr_and_key(&crypt_ftr) == 0) { |
| 2083 | if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) { |
| 2084 | /* An encryption was underway and was interrupted */ |
| 2085 | previously_encrypted_upto = crypt_ftr.encrypted_upto; |
| 2086 | crypt_ftr.encrypted_upto = 0; |
| 2087 | crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS; |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 2088 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2089 | /* At this point, we are in an inconsistent state. Until we successfully |
| 2090 | complete encryption, a reboot will leave us broken. So mark the |
| 2091 | encryption failed in case that happens. |
| 2092 | On successfully completing encryption, remove this flag */ |
| 2093 | crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE; |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 2094 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2095 | put_crypt_ftr_and_key(&crypt_ftr); |
| 2096 | } else if (crypt_ftr.flags & CRYPT_FORCE_ENCRYPTION) { |
| 2097 | if (!check_ftr_sha(&crypt_ftr)) { |
| 2098 | memset(&crypt_ftr, 0, sizeof(crypt_ftr)); |
| 2099 | put_crypt_ftr_and_key(&crypt_ftr); |
| 2100 | goto error_unencrypted; |
| 2101 | } |
| 2102 | |
| 2103 | /* Doing a reboot-encryption*/ |
| 2104 | crypt_ftr.flags &= ~CRYPT_FORCE_ENCRYPTION; |
| 2105 | crypt_ftr.flags |= CRYPT_FORCE_COMPLETE; |
| 2106 | rebootEncryption = true; |
| 2107 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | property_get("ro.crypto.state", encrypted_state, ""); |
| 2111 | if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) { |
| 2112 | SLOGE("Device is already running encrypted, aborting"); |
| 2113 | goto error_unencrypted; |
| 2114 | } |
| 2115 | |
| 2116 | // TODO refactor fs_mgr_get_crypt_info to get both in one call |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 2117 | fs_mgr_get_crypt_info(fstab_default, key_loc, 0, sizeof(key_loc)); |
| 2118 | fs_mgr_get_crypt_info(fstab_default, 0, real_blkdev, sizeof(real_blkdev)); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2119 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2120 | /* Get the size of the real block device */ |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 2121 | fd = open(real_blkdev, O_RDONLY|O_CLOEXEC); |
Hiroaki Miyazawa | 14eab55 | 2015-02-04 13:29:15 +0900 | [diff] [blame] | 2122 | if (fd == -1) { |
| 2123 | SLOGE("Cannot open block device %s\n", real_blkdev); |
| 2124 | goto error_unencrypted; |
| 2125 | } |
| 2126 | unsigned long nr_sec; |
| 2127 | get_blkdev_size(fd, &nr_sec); |
| 2128 | if (nr_sec == 0) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2129 | SLOGE("Cannot get size of block device %s\n", real_blkdev); |
| 2130 | goto error_unencrypted; |
| 2131 | } |
| 2132 | close(fd); |
| 2133 | |
| 2134 | /* 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] | 2135 | if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2136 | unsigned int fs_size_sec, max_fs_size_sec; |
Jim Miller | a70abc6 | 2014-08-15 02:00:45 +0000 | [diff] [blame] | 2137 | fs_size_sec = get_fs_size(real_blkdev); |
Daniel Rosenberg | e82df16 | 2014-08-15 22:19:23 +0000 | [diff] [blame] | 2138 | if (fs_size_sec == 0) |
| 2139 | fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev); |
| 2140 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2141 | max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2142 | |
| 2143 | if (fs_size_sec > max_fs_size_sec) { |
| 2144 | SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place."); |
| 2145 | goto error_unencrypted; |
| 2146 | } |
| 2147 | } |
| 2148 | |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 2149 | /* Get a wakelock as this may take a while, and we don't want the |
| 2150 | * device to sleep on us. We'll grab a partial wakelock, and if the UI |
| 2151 | * wants to keep the screen on, it can grab a full wakelock. |
| 2152 | */ |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2153 | snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid()); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 2154 | acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid); |
| 2155 | |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2156 | /* 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] | 2157 | * vold sets trigger_shutdown_framework. |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2158 | */ |
| 2159 | property_set("vold.decrypt", "trigger_shutdown_framework"); |
| 2160 | SLOGD("Just asked init to shut down class main\n"); |
| 2161 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 2162 | /* Ask vold to unmount all devices that it manages */ |
| 2163 | if (vold_unmountAll()) { |
| 2164 | SLOGE("Failed to unmount all vold managed devices"); |
Ken Sumrall | 2eaf713 | 2011-01-14 12:45:48 -0800 | [diff] [blame] | 2165 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2166 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2167 | /* no_ui means we are being called from init, not settings. |
| 2168 | Now we always reboot from settings, so !no_ui means reboot |
| 2169 | */ |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2170 | if (!no_ui) { |
| 2171 | /* Try fallback, which is to reboot and try there */ |
| 2172 | onlyCreateHeader = true; |
| 2173 | FILE* breadcrumb = fopen(BREADCRUMB_FILE, "we"); |
| 2174 | if (breadcrumb == 0) { |
| 2175 | SLOGE("Failed to create breadcrumb file"); |
| 2176 | goto error_shutting_down; |
| 2177 | } |
| 2178 | fclose(breadcrumb); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | /* 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] | 2182 | if (how == CRYPTO_ENABLE_INPLACE && !onlyCreateHeader) { |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2183 | /* Now that /data is unmounted, we need to mount a tmpfs |
| 2184 | * /data, set a property saying we're doing inplace encryption, |
| 2185 | * and restart the framework. |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2186 | */ |
Ken Sumrall | e5032c4 | 2012-04-01 23:58:44 -0700 | [diff] [blame] | 2187 | if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) { |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2188 | goto error_shutting_down; |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2189 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2190 | /* Tells the framework that inplace encryption is starting */ |
Ken Sumrall | 7df8412 | 2011-01-18 14:04:08 -0800 | [diff] [blame] | 2191 | property_set("vold.encrypt_progress", "0"); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2192 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2193 | /* restart the framework. */ |
| 2194 | /* Create necessary paths on /data */ |
Wei Wang | 42e3810 | 2017-06-07 10:46:12 -0700 | [diff] [blame] | 2195 | prep_data_fs(); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2196 | |
Ken Sumrall | 92736ef | 2012-10-17 20:57:14 -0700 | [diff] [blame] | 2197 | /* Ugh, shutting down the framework is not synchronous, so until it |
| 2198 | * can be fixed, this horrible hack will wait a moment for it all to |
| 2199 | * shut down before proceeding. Without it, some devices cannot |
| 2200 | * restart the graphics services. |
| 2201 | */ |
| 2202 | sleep(2); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2203 | } |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2204 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2205 | /* Start the actual work of making an encrypted filesystem */ |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2206 | /* Initialize a crypt_mnt_ftr for the partition */ |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2207 | if (previously_encrypted_upto == 0 && !rebootEncryption) { |
Paul Lawrence | 69f4ebd | 2014-04-14 12:17:14 -0700 | [diff] [blame] | 2208 | if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) { |
| 2209 | goto error_shutting_down; |
| 2210 | } |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2211 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2212 | if (!strcmp(key_loc, KEY_IN_FOOTER)) { |
| 2213 | crypt_ftr.fs_size = nr_sec |
| 2214 | - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE); |
| 2215 | } else { |
| 2216 | crypt_ftr.fs_size = nr_sec; |
| 2217 | } |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 2218 | /* At this point, we are in an inconsistent state. Until we successfully |
| 2219 | complete encryption, a reboot will leave us broken. So mark the |
| 2220 | encryption failed in case that happens. |
| 2221 | On successfully completing encryption, remove this flag */ |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2222 | if (onlyCreateHeader) { |
| 2223 | crypt_ftr.flags |= CRYPT_FORCE_ENCRYPTION; |
| 2224 | } else { |
| 2225 | crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE; |
| 2226 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2227 | crypt_ftr.crypt_type = crypt_type; |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 2228 | 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] | 2229 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2230 | /* Make an encrypted master key */ |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2231 | if (create_encrypted_random_key(onlyCreateHeader ? DEFAULT_PASSWORD : passwd, |
| 2232 | crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2233 | SLOGE("Cannot create encrypted master key\n"); |
| 2234 | goto error_shutting_down; |
| 2235 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2236 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2237 | /* Replace scrypted intermediate key if we are preparing for a reboot */ |
| 2238 | if (onlyCreateHeader) { |
| 2239 | unsigned char fake_master_key[KEY_LEN_BYTES]; |
| 2240 | unsigned char encrypted_fake_master_key[KEY_LEN_BYTES]; |
| 2241 | memset(fake_master_key, 0, sizeof(fake_master_key)); |
| 2242 | encrypt_master_key(passwd, crypt_ftr.salt, fake_master_key, |
| 2243 | encrypted_fake_master_key, &crypt_ftr); |
| 2244 | } |
| 2245 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2246 | /* Write the key to the end of the partition */ |
| 2247 | put_crypt_ftr_and_key(&crypt_ftr); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2248 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2249 | /* If any persistent data has been remembered, save it. |
| 2250 | * If none, create a valid empty table and save that. |
| 2251 | */ |
| 2252 | if (!persist_data) { |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 2253 | pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2254 | if (pdata) { |
| 2255 | init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE); |
| 2256 | persist_data = pdata; |
| 2257 | } |
| 2258 | } |
| 2259 | if (persist_data) { |
| 2260 | save_persistent_data(); |
| 2261 | } |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2262 | } |
| 2263 | |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2264 | if (onlyCreateHeader) { |
| 2265 | sleep(2); |
Josh Gao | fec4437 | 2017-08-28 13:22:55 -0700 | [diff] [blame] | 2266 | cryptfs_reboot(RebootType::reboot); |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | if (how == CRYPTO_ENABLE_INPLACE && (!no_ui || rebootEncryption)) { |
Ajay Dudani | 87701e2 | 2014-09-17 21:02:52 -0700 | [diff] [blame] | 2270 | /* startup service classes main and late_start */ |
| 2271 | property_set("vold.decrypt", "trigger_restart_min_framework"); |
| 2272 | SLOGD("Just triggered restart_min_framework\n"); |
| 2273 | |
| 2274 | /* OK, the framework is restarted and will soon be showing a |
| 2275 | * progress bar. Time to setup an encrypted mapping, and |
| 2276 | * either write a new filesystem, or encrypt in place updating |
| 2277 | * the progress bar as we work. |
| 2278 | */ |
| 2279 | } |
| 2280 | |
Paul Lawrence | d0c7b17 | 2014-08-08 14:28:10 -0700 | [diff] [blame] | 2281 | decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2282 | 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] | 2283 | CRYPTO_BLOCK_DEVICE); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2284 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2285 | /* If we are continuing, check checksums match */ |
| 2286 | rc = 0; |
| 2287 | if (previously_encrypted_upto) { |
| 2288 | __le8 hash_first_block[SHA256_DIGEST_LENGTH]; |
| 2289 | rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block); |
Ken Sumrall | 128626f | 2011-06-28 18:45:14 -0700 | [diff] [blame] | 2290 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2291 | if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block, |
| 2292 | sizeof(hash_first_block)) != 0) { |
| 2293 | SLOGE("Checksums do not match - trigger wipe"); |
| 2294 | rc = -1; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2295 | } |
| 2296 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2297 | |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2298 | if (!rc) { |
| 2299 | rc = cryptfs_enable_all_volumes(&crypt_ftr, how, |
| 2300 | crypto_blkdev, real_blkdev, |
| 2301 | previously_encrypted_upto); |
| 2302 | } |
| 2303 | |
| 2304 | /* Calculate checksum if we are not finished */ |
Paul Lawrence | b1eb7a0 | 2014-11-25 14:57:32 -0800 | [diff] [blame] | 2305 | if (!rc && how == CRYPTO_ENABLE_INPLACE |
| 2306 | && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2307 | rc = cryptfs_SHA256_fileblock(crypto_blkdev, |
| 2308 | crypt_ftr.hash_first_block); |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 2309 | if (rc) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2310 | SLOGE("Error calculating checksum for continuing encryption"); |
| 2311 | rc = -1; |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2312 | } |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | /* Undo the dm-crypt mapping whether we succeed or not */ |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2316 | delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 2317 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2318 | if (! rc) { |
| 2319 | /* Success */ |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 2320 | crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE; |
Ken Sumrall | 7f7dbaa | 2011-02-01 15:46:41 -0800 | [diff] [blame] | 2321 | |
Paul Lawrence | b1eb7a0 | 2014-11-25 14:57:32 -0800 | [diff] [blame] | 2322 | if (how == CRYPTO_ENABLE_INPLACE |
| 2323 | && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) { |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2324 | SLOGD("Encrypted up to sector %lld - will continue after reboot", |
| 2325 | crypt_ftr.encrypted_upto); |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 2326 | crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS; |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2327 | } |
Paul Lawrence | 73d7a02 | 2014-06-09 14:10:09 -0700 | [diff] [blame] | 2328 | |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 2329 | put_crypt_ftr_and_key(&crypt_ftr); |
Ken Sumrall | d33d417 | 2011-02-01 00:49:13 -0800 | [diff] [blame] | 2330 | |
Paul Lawrence | b1eb7a0 | 2014-11-25 14:57:32 -0800 | [diff] [blame] | 2331 | if (how == CRYPTO_ENABLE_WIPE |
| 2332 | || crypt_ftr.encrypted_upto == crypt_ftr.fs_size) { |
Paul Lawrence | b6672e1 | 2014-08-15 07:37:28 -0700 | [diff] [blame] | 2333 | char value[PROPERTY_VALUE_MAX]; |
| 2334 | property_get("ro.crypto.state", value, ""); |
| 2335 | if (!strcmp(value, "")) { |
| 2336 | /* default encryption - continue first boot sequence */ |
| 2337 | property_set("ro.crypto.state", "encrypted"); |
Paul Lawrence | 4ed4526 | 2016-03-10 15:44:21 -0800 | [diff] [blame] | 2338 | property_set("ro.crypto.type", "block"); |
Paul Lawrence | b6672e1 | 2014-08-15 07:37:28 -0700 | [diff] [blame] | 2339 | release_wake_lock(lockid); |
Paul Lawrence | 3d99eba | 2015-11-20 07:07:19 -0800 | [diff] [blame] | 2340 | if (rebootEncryption && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) { |
| 2341 | // Bring up cryptkeeper that will check the password and set it |
| 2342 | property_set("vold.decrypt", "trigger_shutdown_framework"); |
| 2343 | sleep(2); |
| 2344 | property_set("vold.encrypt_progress", ""); |
| 2345 | cryptfs_trigger_restart_min_framework(); |
| 2346 | } else { |
| 2347 | cryptfs_check_passwd(DEFAULT_PASSWORD); |
| 2348 | cryptfs_restart_internal(1); |
| 2349 | } |
Paul Lawrence | b6672e1 | 2014-08-15 07:37:28 -0700 | [diff] [blame] | 2350 | return 0; |
| 2351 | } else { |
| 2352 | sleep(2); /* Give the UI a chance to show 100% progress */ |
Josh Gao | fec4437 | 2017-08-28 13:22:55 -0700 | [diff] [blame] | 2353 | cryptfs_reboot(RebootType::reboot); |
Paul Lawrence | b6672e1 | 2014-08-15 07:37:28 -0700 | [diff] [blame] | 2354 | } |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2355 | } else { |
Paul Lawrence | b6672e1 | 2014-08-15 07:37:28 -0700 | [diff] [blame] | 2356 | sleep(2); /* Partially encrypted, ensure writes flushed to ssd */ |
Josh Gao | fec4437 | 2017-08-28 13:22:55 -0700 | [diff] [blame] | 2357 | cryptfs_reboot(RebootType::shutdown); |
Paul Lawrence | 8799917 | 2014-02-20 12:21:31 -0800 | [diff] [blame] | 2358 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2359 | } else { |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 2360 | char value[PROPERTY_VALUE_MAX]; |
| 2361 | |
Ken Sumrall | 319369a | 2012-06-27 16:30:18 -0700 | [diff] [blame] | 2362 | property_get("ro.vold.wipe_on_crypt_fail", value, "0"); |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 2363 | if (!strcmp(value, "1")) { |
| 2364 | /* wipe data if encryption failed */ |
| 2365 | SLOGE("encryption failed - rebooting into recovery to wipe data\n"); |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 2366 | std::string err; |
| 2367 | const std::vector<std::string> options = { |
| 2368 | "--wipe_data\n--reason=cryptfs_enable_internal\n" |
| 2369 | }; |
| 2370 | if (!write_bootloader_message(options, &err)) { |
| 2371 | SLOGE("could not write bootloader message: %s", err.c_str()); |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 2372 | } |
Josh Gao | fec4437 | 2017-08-28 13:22:55 -0700 | [diff] [blame] | 2373 | cryptfs_reboot(RebootType::recovery); |
Mike Lockwood | ee6d8c4 | 2012-02-15 13:43:28 -0800 | [diff] [blame] | 2374 | } else { |
| 2375 | /* set property to trigger dialog */ |
| 2376 | property_set("vold.encrypt_progress", "error_partially_encrypted"); |
| 2377 | release_wake_lock(lockid); |
| 2378 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2379 | return -1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2380 | } |
| 2381 | |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2382 | /* hrm, the encrypt step claims success, but the reboot failed. |
| 2383 | * This should not happen. |
| 2384 | * Set the property and return. Hope the framework can deal with it. |
| 2385 | */ |
| 2386 | property_set("vold.encrypt_progress", "error_reboot_failed"); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 2387 | release_wake_lock(lockid); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2388 | return rc; |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2389 | |
| 2390 | error_unencrypted: |
| 2391 | property_set("vold.encrypt_progress", "error_not_encrypted"); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 2392 | if (lockid[0]) { |
| 2393 | release_wake_lock(lockid); |
| 2394 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2395 | return -1; |
| 2396 | |
| 2397 | error_shutting_down: |
| 2398 | /* we failed, and have not encrypted anthing, so the users's data is still intact, |
| 2399 | * but the framework is stopped and not restarted to show the error, so it's up to |
| 2400 | * vold to restart the system. |
| 2401 | */ |
| 2402 | SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system"); |
Josh Gao | fec4437 | 2017-08-28 13:22:55 -0700 | [diff] [blame] | 2403 | cryptfs_reboot(RebootType::reboot); |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2404 | |
| 2405 | /* shouldn't get here */ |
| 2406 | property_set("vold.encrypt_progress", "error_shutting_down"); |
Ken Sumrall | 5d4c68e | 2011-01-30 19:06:03 -0800 | [diff] [blame] | 2407 | if (lockid[0]) { |
| 2408 | release_wake_lock(lockid); |
| 2409 | } |
Ken Sumrall | 3ed8236 | 2011-01-28 23:31:16 -0800 | [diff] [blame] | 2410 | return -1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2411 | } |
| 2412 | |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 2413 | int cryptfs_enable(const char *howarg, int type, const char *passwd, int no_ui) |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 2414 | { |
Paul Lawrence | 569649f | 2015-09-09 12:13:00 -0700 | [diff] [blame] | 2415 | return cryptfs_enable_internal(howarg, type, passwd, no_ui); |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 2416 | } |
| 2417 | |
Jeff Sharkey | 83b559c | 2017-09-12 16:30:52 -0600 | [diff] [blame] | 2418 | int cryptfs_enable_default(const char *howarg, int no_ui) |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 2419 | { |
| 2420 | return cryptfs_enable_internal(howarg, CRYPT_TYPE_DEFAULT, |
Paul Lawrence | 569649f | 2015-09-09 12:13:00 -0700 | [diff] [blame] | 2421 | DEFAULT_PASSWORD, no_ui); |
Paul Lawrence | 1348603 | 2014-02-03 13:28:11 -0800 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | int cryptfs_changepw(int crypt_type, const char *newpw) |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2425 | { |
Paul Crowley | 38132a1 | 2016-02-09 09:50:32 +0000 | [diff] [blame] | 2426 | if (e4crypt_is_native()) { |
Paul Lawrence | 7b6b565 | 2016-02-02 11:14:59 -0800 | [diff] [blame] | 2427 | SLOGE("cryptfs_changepw not valid for file encryption"); |
| 2428 | return -1; |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 2429 | } |
| 2430 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2431 | struct crypt_mnt_ftr crypt_ftr; |
JP Abgrall | 933216c | 2015-02-11 13:44:32 -0800 | [diff] [blame] | 2432 | int rc; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2433 | |
| 2434 | /* This is only allowed after we've successfully decrypted the master key */ |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2435 | if (!master_key_saved) { |
Ken Sumrall | 0cc1663 | 2011-01-18 20:32:26 -0800 | [diff] [blame] | 2436 | SLOGE("Key not saved, aborting"); |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2437 | return -1; |
| 2438 | } |
| 2439 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2440 | if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) { |
| 2441 | SLOGE("Invalid crypt_type %d", crypt_type); |
| 2442 | return -1; |
| 2443 | } |
| 2444 | |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2445 | /* get key */ |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2446 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2447 | SLOGE("Error getting crypt footer and key"); |
| 2448 | return -1; |
Ken Sumrall | 8ddbe40 | 2011-01-17 15:26:29 -0800 | [diff] [blame] | 2449 | } |
| 2450 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2451 | crypt_ftr.crypt_type = crypt_type; |
| 2452 | |
JP Abgrall | 933216c | 2015-02-11 13:44:32 -0800 | [diff] [blame] | 2453 | rc = encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2454 | : newpw, |
| 2455 | crypt_ftr.salt, |
| 2456 | saved_master_key, |
| 2457 | crypt_ftr.master_key, |
| 2458 | &crypt_ftr); |
JP Abgrall | 933216c | 2015-02-11 13:44:32 -0800 | [diff] [blame] | 2459 | if (rc) { |
| 2460 | SLOGE("Encrypt master key failed: %d", rc); |
| 2461 | return -1; |
| 2462 | } |
Jason parks | 70a4b3f | 2011-01-28 10:10:47 -0600 | [diff] [blame] | 2463 | /* save the key */ |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2464 | put_crypt_ftr_and_key(&crypt_ftr); |
Ken Sumrall | 8f869aa | 2010-12-03 03:47:09 -0800 | [diff] [blame] | 2465 | |
| 2466 | return 0; |
| 2467 | } |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2468 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2469 | static unsigned int persist_get_max_entries(int encrypted) { |
| 2470 | struct crypt_mnt_ftr crypt_ftr; |
| 2471 | unsigned int dsize; |
| 2472 | unsigned int max_persistent_entries; |
| 2473 | |
| 2474 | /* If encrypted, use the values from the crypt_ftr, otherwise |
| 2475 | * use the values for the current spec. |
| 2476 | */ |
| 2477 | if (encrypted) { |
| 2478 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
| 2479 | return -1; |
| 2480 | } |
| 2481 | dsize = crypt_ftr.persist_data_size; |
| 2482 | } else { |
| 2483 | dsize = CRYPT_PERSIST_DATA_SIZE; |
| 2484 | } |
| 2485 | |
| 2486 | max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) / |
| 2487 | sizeof(struct crypt_persist_entry); |
| 2488 | |
| 2489 | return max_persistent_entries; |
| 2490 | } |
| 2491 | |
| 2492 | static int persist_get_key(const char *fieldname, char *value) |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2493 | { |
| 2494 | unsigned int i; |
| 2495 | |
| 2496 | if (persist_data == NULL) { |
| 2497 | return -1; |
| 2498 | } |
| 2499 | for (i = 0; i < persist_data->persist_valid_entries; i++) { |
| 2500 | if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) { |
| 2501 | /* We found it! */ |
| 2502 | strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX); |
| 2503 | return 0; |
| 2504 | } |
| 2505 | } |
| 2506 | |
| 2507 | return -1; |
| 2508 | } |
| 2509 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2510 | 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] | 2511 | { |
| 2512 | unsigned int i; |
| 2513 | unsigned int num; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2514 | unsigned int max_persistent_entries; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2515 | |
| 2516 | if (persist_data == NULL) { |
| 2517 | return -1; |
| 2518 | } |
| 2519 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2520 | max_persistent_entries = persist_get_max_entries(encrypted); |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2521 | |
| 2522 | num = persist_data->persist_valid_entries; |
| 2523 | |
| 2524 | for (i = 0; i < num; i++) { |
| 2525 | if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) { |
| 2526 | /* We found an existing entry, update it! */ |
| 2527 | memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX); |
| 2528 | strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX); |
| 2529 | return 0; |
| 2530 | } |
| 2531 | } |
| 2532 | |
| 2533 | /* We didn't find it, add it to the end, if there is room */ |
| 2534 | if (persist_data->persist_valid_entries < max_persistent_entries) { |
| 2535 | memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry)); |
| 2536 | strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX); |
| 2537 | strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX); |
| 2538 | persist_data->persist_valid_entries++; |
| 2539 | return 0; |
| 2540 | } |
| 2541 | |
| 2542 | return -1; |
| 2543 | } |
| 2544 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2545 | /** |
| 2546 | * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the |
| 2547 | * sequence and its index is greater than or equal to index. Return 0 otherwise. |
| 2548 | */ |
Jeff Sharkey | 95440eb | 2017-09-18 18:19:28 -0600 | [diff] [blame] | 2549 | int match_multi_entry(const char *key, const char *field, unsigned index) { |
| 2550 | std::string key_ = key; |
| 2551 | std::string field_ = field; |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2552 | |
Jeff Sharkey | 95440eb | 2017-09-18 18:19:28 -0600 | [diff] [blame] | 2553 | std::string parsed_field; |
| 2554 | unsigned parsed_index; |
| 2555 | |
| 2556 | std::string::size_type split = key_.find_last_of('_'); |
| 2557 | if (split == std::string::npos) { |
| 2558 | parsed_field = key_; |
| 2559 | parsed_index = 0; |
| 2560 | } else { |
| 2561 | parsed_field = key_.substr(0, split); |
| 2562 | parsed_index = std::stoi(key_.substr(split + 1)); |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2563 | } |
Jeff Sharkey | 95440eb | 2017-09-18 18:19:28 -0600 | [diff] [blame] | 2564 | |
| 2565 | return parsed_field == field_ && parsed_index >= index; |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | /* |
| 2569 | * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all |
| 2570 | * remaining entries starting from index will be deleted. |
| 2571 | * returns PERSIST_DEL_KEY_OK if deletion succeeds, |
| 2572 | * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist, |
| 2573 | * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs. |
| 2574 | * |
| 2575 | */ |
| 2576 | static int persist_del_keys(const char *fieldname, unsigned index) |
| 2577 | { |
| 2578 | unsigned int i; |
| 2579 | unsigned int j; |
| 2580 | unsigned int num; |
| 2581 | |
| 2582 | if (persist_data == NULL) { |
| 2583 | return PERSIST_DEL_KEY_ERROR_OTHER; |
| 2584 | } |
| 2585 | |
| 2586 | num = persist_data->persist_valid_entries; |
| 2587 | |
| 2588 | j = 0; // points to the end of non-deleted entries. |
| 2589 | // Filter out to-be-deleted entries in place. |
| 2590 | for (i = 0; i < num; i++) { |
| 2591 | if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) { |
| 2592 | persist_data->persist_entry[j] = persist_data->persist_entry[i]; |
| 2593 | j++; |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | if (j < num) { |
| 2598 | persist_data->persist_valid_entries = j; |
| 2599 | // Zeroise the remaining entries |
| 2600 | memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry)); |
| 2601 | return PERSIST_DEL_KEY_OK; |
| 2602 | } else { |
| 2603 | // Did not find an entry matching the given fieldname |
| 2604 | return PERSIST_DEL_KEY_ERROR_NO_FIELD; |
| 2605 | } |
| 2606 | } |
| 2607 | |
| 2608 | static int persist_count_keys(const char *fieldname) |
| 2609 | { |
| 2610 | unsigned int i; |
| 2611 | unsigned int count; |
| 2612 | |
| 2613 | if (persist_data == NULL) { |
| 2614 | return -1; |
| 2615 | } |
| 2616 | |
| 2617 | count = 0; |
| 2618 | for (i = 0; i < persist_data->persist_valid_entries; i++) { |
| 2619 | if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) { |
| 2620 | count++; |
| 2621 | } |
| 2622 | } |
| 2623 | |
| 2624 | return count; |
| 2625 | } |
| 2626 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2627 | /* Return the value of the specified field. */ |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2628 | int cryptfs_getfield(const char *fieldname, char *value, int len) |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2629 | { |
Paul Crowley | 38132a1 | 2016-02-09 09:50:32 +0000 | [diff] [blame] | 2630 | if (e4crypt_is_native()) { |
Paul Lawrence | 5a06a64 | 2016-02-03 13:39:13 -0800 | [diff] [blame] | 2631 | SLOGE("Cannot get field when file encrypted"); |
| 2632 | return -1; |
Paul Lawrence | 368d794 | 2015-04-15 14:12:00 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2635 | char temp_value[PROPERTY_VALUE_MAX]; |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2636 | /* CRYPTO_GETFIELD_OK is success, |
| 2637 | * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set, |
| 2638 | * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small, |
| 2639 | * CRYPTO_GETFIELD_ERROR_OTHER is any other error |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2640 | */ |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2641 | int rc = CRYPTO_GETFIELD_ERROR_OTHER; |
| 2642 | int i; |
| 2643 | char temp_field[PROPERTY_KEY_MAX]; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2644 | |
| 2645 | if (persist_data == NULL) { |
| 2646 | load_persistent_data(); |
| 2647 | if (persist_data == NULL) { |
| 2648 | SLOGE("Getfield error, cannot load persistent data"); |
| 2649 | goto out; |
| 2650 | } |
| 2651 | } |
| 2652 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2653 | // Read value from persistent entries. If the original value is split into multiple entries, |
| 2654 | // stitch them back together. |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2655 | if (!persist_get_key(fieldname, temp_value)) { |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2656 | // We found it, copy it to the caller's buffer and keep going until all entries are read. |
| 2657 | if (strlcpy(value, temp_value, len) >= (unsigned) len) { |
| 2658 | // value too small |
| 2659 | rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL; |
| 2660 | goto out; |
| 2661 | } |
| 2662 | rc = CRYPTO_GETFIELD_OK; |
| 2663 | |
| 2664 | for (i = 1; /* break explicitly */; i++) { |
| 2665 | if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >= |
| 2666 | (int) sizeof(temp_field)) { |
| 2667 | // If the fieldname is very long, we stop as soon as it begins to overflow the |
| 2668 | // maximum field length. At this point we have in fact fully read out the original |
| 2669 | // value because cryptfs_setfield would not allow fields with longer names to be |
| 2670 | // written in the first place. |
| 2671 | break; |
| 2672 | } |
| 2673 | if (!persist_get_key(temp_field, temp_value)) { |
| 2674 | if (strlcat(value, temp_value, len) >= (unsigned)len) { |
| 2675 | // value too small. |
| 2676 | rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL; |
| 2677 | goto out; |
| 2678 | } |
| 2679 | } else { |
| 2680 | // Exhaust all entries. |
| 2681 | break; |
| 2682 | } |
| 2683 | } |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2684 | } else { |
| 2685 | /* Sadness, it's not there. Return the error */ |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2686 | rc = CRYPTO_GETFIELD_ERROR_NO_FIELD; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2687 | } |
| 2688 | |
| 2689 | out: |
| 2690 | return rc; |
| 2691 | } |
| 2692 | |
| 2693 | /* Set the value of the specified field. */ |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2694 | int cryptfs_setfield(const char *fieldname, const char *value) |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2695 | { |
Paul Crowley | 38132a1 | 2016-02-09 09:50:32 +0000 | [diff] [blame] | 2696 | if (e4crypt_is_native()) { |
Paul Lawrence | 5a06a64 | 2016-02-03 13:39:13 -0800 | [diff] [blame] | 2697 | SLOGE("Cannot set field when file encrypted"); |
| 2698 | return -1; |
Paul Lawrence | 368d794 | 2015-04-15 14:12:00 -0700 | [diff] [blame] | 2699 | } |
| 2700 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2701 | char encrypted_state[PROPERTY_VALUE_MAX]; |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2702 | /* 0 is success, negative values are error */ |
| 2703 | int rc = CRYPTO_SETFIELD_ERROR_OTHER; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2704 | int encrypted = 0; |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2705 | unsigned int field_id; |
| 2706 | char temp_field[PROPERTY_KEY_MAX]; |
| 2707 | unsigned int num_entries; |
| 2708 | unsigned int max_keylen; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2709 | |
| 2710 | if (persist_data == NULL) { |
| 2711 | load_persistent_data(); |
| 2712 | if (persist_data == NULL) { |
| 2713 | SLOGE("Setfield error, cannot load persistent data"); |
| 2714 | goto out; |
| 2715 | } |
| 2716 | } |
| 2717 | |
| 2718 | property_get("ro.crypto.state", encrypted_state, ""); |
| 2719 | if (!strcmp(encrypted_state, "encrypted") ) { |
| 2720 | encrypted = 1; |
| 2721 | } |
| 2722 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2723 | // Compute the number of entries required to store value, each entry can store up to |
| 2724 | // (PROPERTY_VALUE_MAX - 1) chars |
| 2725 | if (strlen(value) == 0) { |
| 2726 | // Empty value also needs one entry to store. |
| 2727 | num_entries = 1; |
| 2728 | } else { |
| 2729 | num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1); |
| 2730 | } |
| 2731 | |
| 2732 | max_keylen = strlen(fieldname); |
| 2733 | if (num_entries > 1) { |
| 2734 | // Need an extra "_%d" suffix. |
| 2735 | max_keylen += 1 + log10(num_entries); |
| 2736 | } |
| 2737 | if (max_keylen > PROPERTY_KEY_MAX - 1) { |
| 2738 | rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2739 | goto out; |
| 2740 | } |
| 2741 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2742 | // Make sure we have enough space to write the new value |
| 2743 | if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) > |
| 2744 | persist_get_max_entries(encrypted)) { |
| 2745 | rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG; |
| 2746 | goto out; |
| 2747 | } |
| 2748 | |
| 2749 | // Now that we know persist_data has enough space for value, let's delete the old field first |
| 2750 | // to make up space. |
| 2751 | persist_del_keys(fieldname, 0); |
| 2752 | |
| 2753 | if (persist_set_key(fieldname, value, encrypted)) { |
| 2754 | // fail to set key, should not happen as we have already checked the available space |
| 2755 | SLOGE("persist_set_key() error during setfield()"); |
| 2756 | goto out; |
| 2757 | } |
| 2758 | |
| 2759 | for (field_id = 1; field_id < num_entries; field_id++) { |
| 2760 | snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, field_id); |
| 2761 | |
| 2762 | if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), encrypted)) { |
| 2763 | // fail to set key, should not happen as we have already checked the available space. |
| 2764 | SLOGE("persist_set_key() error during setfield()"); |
| 2765 | goto out; |
| 2766 | } |
| 2767 | } |
| 2768 | |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2769 | /* If we are running encrypted, save the persistent data now */ |
| 2770 | if (encrypted) { |
| 2771 | if (save_persistent_data()) { |
| 2772 | SLOGE("Setfield error, cannot save persistent data"); |
| 2773 | goto out; |
| 2774 | } |
| 2775 | } |
| 2776 | |
Rubin Xu | 85c01f9 | 2014-10-13 12:49:54 +0100 | [diff] [blame] | 2777 | rc = CRYPTO_SETFIELD_OK; |
Ken Sumrall | 160b4d6 | 2013-04-22 12:15:39 -0700 | [diff] [blame] | 2778 | |
| 2779 | out: |
| 2780 | return rc; |
| 2781 | } |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2782 | |
| 2783 | /* Checks userdata. Attempt to mount the volume if default- |
| 2784 | * encrypted. |
| 2785 | * On success trigger next init phase and return 0. |
| 2786 | * Currently do not handle failure - see TODO below. |
| 2787 | */ |
| 2788 | int cryptfs_mount_default_encrypted(void) |
| 2789 | { |
Paul Lawrence | 84274cc | 2016-04-15 15:41:33 -0700 | [diff] [blame] | 2790 | int crypt_type = cryptfs_get_password_type(); |
| 2791 | if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) { |
| 2792 | SLOGE("Bad crypt type - error"); |
| 2793 | } else if (crypt_type != CRYPT_TYPE_DEFAULT) { |
| 2794 | SLOGD("Password is not default - " |
| 2795 | "starting min framework to prompt"); |
| 2796 | property_set("vold.decrypt", "trigger_restart_min_framework"); |
| 2797 | return 0; |
| 2798 | } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) { |
| 2799 | SLOGD("Password is default - restarting filesystem"); |
| 2800 | cryptfs_restart_internal(0); |
| 2801 | return 0; |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2802 | } else { |
Paul Lawrence | 84274cc | 2016-04-15 15:41:33 -0700 | [diff] [blame] | 2803 | SLOGE("Encrypted, default crypt type but can't decrypt"); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2804 | } |
| 2805 | |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 2806 | /** Corrupt. Allow us to boot into framework, which will detect bad |
| 2807 | crypto when it calls do_crypto_complete, then do a factory reset |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2808 | */ |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 2809 | property_set("vold.decrypt", "trigger_restart_min_framework"); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2810 | return 0; |
| 2811 | } |
| 2812 | |
| 2813 | /* Returns type of the password, default, pattern, pin or password. |
| 2814 | */ |
| 2815 | int cryptfs_get_password_type(void) |
| 2816 | { |
Paul Crowley | 38132a1 | 2016-02-09 09:50:32 +0000 | [diff] [blame] | 2817 | if (e4crypt_is_native()) { |
Paul Lawrence | 7b6b565 | 2016-02-02 11:14:59 -0800 | [diff] [blame] | 2818 | SLOGE("cryptfs_get_password_type not valid for file encryption"); |
| 2819 | return -1; |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 2820 | } |
| 2821 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2822 | struct crypt_mnt_ftr crypt_ftr; |
| 2823 | |
| 2824 | if (get_crypt_ftr_and_key(&crypt_ftr)) { |
| 2825 | SLOGE("Error getting crypt footer and key\n"); |
| 2826 | return -1; |
| 2827 | } |
| 2828 | |
Paul Lawrence | 6bfed20 | 2014-07-28 12:47:22 -0700 | [diff] [blame] | 2829 | if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) { |
| 2830 | return -1; |
| 2831 | } |
| 2832 | |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 2833 | return crypt_ftr.crypt_type; |
| 2834 | } |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 2835 | |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 2836 | const char* cryptfs_get_password() |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 2837 | { |
Paul Crowley | 38132a1 | 2016-02-09 09:50:32 +0000 | [diff] [blame] | 2838 | if (e4crypt_is_native()) { |
Paul Lawrence | 7b6b565 | 2016-02-02 11:14:59 -0800 | [diff] [blame] | 2839 | SLOGE("cryptfs_get_password not valid for file encryption"); |
| 2840 | return 0; |
Paul Lawrence | 05335c3 | 2015-03-05 09:46:23 -0800 | [diff] [blame] | 2841 | } |
| 2842 | |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 2843 | struct timespec now; |
Paul Lawrence | ef2b5be | 2014-11-11 12:47:03 -0800 | [diff] [blame] | 2844 | clock_gettime(CLOCK_BOOTTIME, &now); |
Paul Lawrence | 399317e | 2014-03-10 13:20:50 -0700 | [diff] [blame] | 2845 | if (now.tv_sec < password_expiry_time) { |
| 2846 | return password; |
| 2847 | } else { |
| 2848 | cryptfs_clear_password(); |
| 2849 | return 0; |
| 2850 | } |
| 2851 | } |
| 2852 | |
| 2853 | void cryptfs_clear_password() |
| 2854 | { |
| 2855 | if (password) { |
| 2856 | size_t len = strlen(password); |
| 2857 | memset(password, 0, len); |
| 2858 | free(password); |
| 2859 | password = 0; |
| 2860 | password_expiry_time = 0; |
| 2861 | } |
Paul Lawrence | 684dbdf | 2014-02-07 12:07:22 -0800 | [diff] [blame] | 2862 | } |
Paul Lawrence | 731a7a2 | 2015-04-28 22:14:15 +0000 | [diff] [blame] | 2863 | |
Paul Lawrence | 0c24746 | 2015-10-29 10:30:57 -0700 | [diff] [blame] | 2864 | int cryptfs_isConvertibleToFBE() |
| 2865 | { |
Paul Crowley | e2ee152 | 2017-09-26 14:05:26 -0700 | [diff] [blame] | 2866 | struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT); |
Paul Lawrence | 0c24746 | 2015-10-29 10:30:57 -0700 | [diff] [blame] | 2867 | return fs_mgr_is_convertible_to_fbe(rec) ? 1 : 0; |
| 2868 | } |