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