blob: b1d1fc7886c2dc8710b9b2c6d5151a0658d59d56 [file] [log] [blame]
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001/*
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 Sumralle550f782013-08-20 13:48:23 -070024#include <sys/wait.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080025#include <sys/stat.h>
Paul Lawrencef4faa572014-01-29 13:31:03 -080026#include <ctype.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080027#include <fcntl.h>
Elliott Hughes73737162014-06-25 17:27:42 -070028#include <inttypes.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080029#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 Langley41405bb2015-01-22 16:45:28 -080039#include <openssl/sha.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080040#include <errno.h>
Tao Bao989fec22016-10-05 18:01:19 -070041#include <ext4_utils/ext4.h>
42#include <ext4_utils/ext4_utils.h>
Ken Sumrall29d8da82011-05-18 17:20:07 -070043#include <linux/kdev_t.h>
Ken Sumralle5032c42012-04-01 23:58:44 -070044#include <fs_mgr.h>
Paul Lawrence9c58a872014-09-30 09:12:51 -070045#include <time.h>
Rubin Xu85c01f92014-10-13 12:49:54 +010046#include <math.h>
Jeff Vander Stoepdf725752016-01-29 15:34:43 -080047#include <selinux/selinux.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080048#include "cryptfs.h"
Jeff Vander Stoepdf725752016-01-29 15:34:43 -080049#include "secontext.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080050#define LOG_TAG "Cryptfs"
51#include "cutils/log.h"
52#include "cutils/properties.h"
Ken Sumralladfba362013-06-04 16:37:52 -070053#include "cutils/android_reboot.h"
Ken Sumrall5d4c68e2011-01-30 19:06:03 -080054#include "hardware_legacy/power.h"
Ken Sumralle550f782013-08-20 13:48:23 -070055#include <logwrap/logwrap.h>
Paul Crowley63c18d32016-02-10 14:02:47 +000056#include "ScryptParameters.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070057#include "VolumeManager.h"
Ken Sumrall9caab762013-06-11 19:10:20 -070058#include "VoldUtil.h"
Kenny Rootc4c70f12013-06-14 12:11:38 -070059#include "crypto_scrypt.h"
Paul Lawrence731a7a22015-04-28 22:14:15 +000060#include "Ext4Crypt.h"
Daniel Rosenberge82df162014-08-15 22:19:23 +000061#include "f2fs_sparseblock.h"
Paul Lawrence87999172014-02-20 12:21:31 -080062#include "CheckBattery.h"
jessica_yu3f14fe42014-09-22 15:57:40 +080063#include "Process.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080064
Yabin Cui1fb59662016-06-24 14:48:49 -070065#include <bootloader_message/bootloader_message.h>
Shawn Willden8af33352015-02-24 09:51:34 -070066#include <hardware/keymaster0.h>
Shawn Willdenda6e8992015-06-03 09:40:45 -060067#include <hardware/keymaster1.h>
Paul Lawrence69f4ebd2014-04-14 12:17:14 -070068
Mark Salyzyn3e971272014-01-21 13:27:04 -080069#define UNUSED __attribute__((unused))
70
Mark Salyzyn5eecc442014-02-12 14:16:14 -080071#define UNUSED __attribute__((unused))
72
Ken Sumrall8f869aa2010-12-03 03:47:09 -080073#define DM_CRYPT_BUF_SIZE 4096
74
Jason parks70a4b3f2011-01-28 10:10:47 -060075#define HASH_COUNT 2000
76#define KEY_LEN_BYTES 16
77#define IV_LEN_BYTES 16
78
Ken Sumrall29d8da82011-05-18 17:20:07 -070079#define KEY_IN_FOOTER "footer"
80
Paul Lawrence3bd36d52015-06-09 13:37:44 -070081#define DEFAULT_PASSWORD "default_password"
Paul Lawrencef4faa572014-01-29 13:31:03 -080082
Paul Lawrence3d99eba2015-11-20 07:07:19 -080083#define CRYPTO_BLOCK_DEVICE "userdata"
84
85#define BREADCRUMB_FILE "/data/misc/vold/convert_fde"
86
Ken Sumrall29d8da82011-05-18 17:20:07 -070087#define EXT4_FS 1
JP Abgrall62c7af32014-06-16 13:01:23 -070088#define F2FS_FS 2
Ken Sumrall29d8da82011-05-18 17:20:07 -070089
Ken Sumralle919efe2012-09-29 17:07:41 -070090#define TABLE_LOAD_RETRIES 10
91
Shawn Willden47ba10d2014-09-03 17:07:06 -060092#define RSA_KEY_SIZE 2048
93#define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8)
94#define RSA_EXPONENT 0x10001
Shawn Willdenda6e8992015-06-03 09:40:45 -060095#define KEYMASTER_CRYPTFS_RATE_LIMIT 1 // Maximum one try per second
Paul Lawrence69f4ebd2014-04-14 12:17:14 -070096
Paul Lawrence8e3f4512014-09-08 10:11:17 -070097#define RETRY_MOUNT_ATTEMPTS 10
98#define RETRY_MOUNT_DELAY_SECONDS 1
99
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800100char *me = "cryptfs";
101
Jason parks70a4b3f2011-01-28 10:10:47 -0600102static unsigned char saved_master_key[KEY_LEN_BYTES];
Ken Sumrall3ad90722011-10-04 20:38:29 -0700103static char *saved_mount_point;
Jason parks70a4b3f2011-01-28 10:10:47 -0600104static int master_key_saved = 0;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700105static struct crypt_persist_data *persist_data = NULL;
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800106
Shawn Willdenda6e8992015-06-03 09:40:45 -0600107static int keymaster_init(keymaster0_device_t **keymaster0_dev,
108 keymaster1_device_t **keymaster1_dev)
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700109{
110 int rc;
111
112 const hw_module_t* mod;
113 rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod);
114 if (rc) {
115 ALOGE("could not find any keystore module");
Shawn Willdenda6e8992015-06-03 09:40:45 -0600116 goto err;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700117 }
118
Shawn Willdenda6e8992015-06-03 09:40:45 -0600119 SLOGI("keymaster module name is %s", mod->name);
120 SLOGI("keymaster version is %d", mod->module_api_version);
121
122 *keymaster0_dev = NULL;
123 *keymaster1_dev = NULL;
124 if (mod->module_api_version == KEYMASTER_MODULE_API_VERSION_1_0) {
125 SLOGI("Found keymaster1 module, using keymaster1 API.");
126 rc = keymaster1_open(mod, keymaster1_dev);
127 } else {
128 SLOGI("Found keymaster0 module, using keymaster0 API.");
129 rc = keymaster0_open(mod, keymaster0_dev);
130 }
131
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700132 if (rc) {
133 ALOGE("could not open keymaster device in %s (%s)",
Shawn Willdenda6e8992015-06-03 09:40:45 -0600134 KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc));
135 goto err;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700136 }
137
138 return 0;
139
Shawn Willdenda6e8992015-06-03 09:40:45 -0600140err:
141 *keymaster0_dev = NULL;
142 *keymaster1_dev = NULL;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700143 return rc;
144}
145
146/* Should we use keymaster? */
147static int keymaster_check_compatibility()
148{
Shawn Willdenda6e8992015-06-03 09:40:45 -0600149 keymaster0_device_t *keymaster0_dev = 0;
150 keymaster1_device_t *keymaster1_dev = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700151 int rc = 0;
152
Shawn Willdenda6e8992015-06-03 09:40:45 -0600153 if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700154 SLOGE("Failed to init keymaster");
155 rc = -1;
156 goto out;
157 }
158
Shawn Willdenda6e8992015-06-03 09:40:45 -0600159 if (keymaster1_dev) {
160 rc = 1;
161 goto out;
162 }
Paul Lawrence8c008392014-05-06 14:02:48 -0700163
Paul Lawrence300dae72016-03-11 11:02:52 -0800164 if (!keymaster0_dev || !keymaster0_dev->common.module) {
165 rc = -1;
166 goto out;
167 }
168
Shawn Willdenda6e8992015-06-03 09:40:45 -0600169 // TODO(swillden): Check to see if there's any reason to require v0.3. I think v0.1 and v0.2
170 // should work.
171 if (keymaster0_dev->common.module->module_api_version
Paul Lawrence8c008392014-05-06 14:02:48 -0700172 < KEYMASTER_MODULE_API_VERSION_0_3) {
173 rc = 0;
174 goto out;
175 }
176
Shawn Willdenda6e8992015-06-03 09:40:45 -0600177 if (!(keymaster0_dev->flags & KEYMASTER_SOFTWARE_ONLY) &&
178 (keymaster0_dev->flags & KEYMASTER_BLOBS_ARE_STANDALONE)) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700179 rc = 1;
180 }
181
182out:
Shawn Willdenda6e8992015-06-03 09:40:45 -0600183 if (keymaster1_dev) {
184 keymaster1_close(keymaster1_dev);
185 }
186 if (keymaster0_dev) {
187 keymaster0_close(keymaster0_dev);
188 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700189 return rc;
190}
191
192/* Create a new keymaster key and store it in this footer */
193static int keymaster_create_key(struct crypt_mnt_ftr *ftr)
194{
195 uint8_t* key = 0;
Shawn Willdenda6e8992015-06-03 09:40:45 -0600196 keymaster0_device_t *keymaster0_dev = 0;
197 keymaster1_device_t *keymaster1_dev = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700198
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800199 if (ftr->keymaster_blob_size) {
200 SLOGI("Already have key");
201 return 0;
202 }
203
Shawn Willdenda6e8992015-06-03 09:40:45 -0600204 if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700205 SLOGE("Failed to init keymaster");
206 return -1;
207 }
208
209 int rc = 0;
Shawn Willdenda6e8992015-06-03 09:40:45 -0600210 size_t key_size = 0;
211 if (keymaster1_dev) {
212 keymaster_key_param_t params[] = {
213 /* Algorithm & size specifications. Stick with RSA for now. Switch to AES later. */
214 keymaster_param_enum(KM_TAG_ALGORITHM, KM_ALGORITHM_RSA),
215 keymaster_param_int(KM_TAG_KEY_SIZE, RSA_KEY_SIZE),
216 keymaster_param_long(KM_TAG_RSA_PUBLIC_EXPONENT, RSA_EXPONENT),
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700217
Shawn Willden86af3552015-06-24 07:21:54 -0700218 /* The only allowed purpose for this key is signing. */
219 keymaster_param_enum(KM_TAG_PURPOSE, KM_PURPOSE_SIGN),
220
221 /* Padding & digest specifications. */
Shawn Willdenda6e8992015-06-03 09:40:45 -0600222 keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE),
Shawn Willdenda6e8992015-06-03 09:40:45 -0600223 keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE),
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700224
Shawn Willdenda6e8992015-06-03 09:40:45 -0600225 /* Require that the key be usable in standalone mode. File system isn't available. */
226 keymaster_param_enum(KM_TAG_BLOB_USAGE_REQUIREMENTS, KM_BLOB_STANDALONE),
227
228 /* No auth requirements, because cryptfs is not yet integrated with gatekeeper. */
229 keymaster_param_bool(KM_TAG_NO_AUTH_REQUIRED),
230
Shawn Willdenda6e8992015-06-03 09:40:45 -0600231 /* Rate-limit key usage attempts, to rate-limit brute force */
232 keymaster_param_int(KM_TAG_MIN_SECONDS_BETWEEN_OPS, KEYMASTER_CRYPTFS_RATE_LIMIT),
233 };
234 keymaster_key_param_set_t param_set = { params, sizeof(params)/sizeof(*params) };
235 keymaster_key_blob_t key_blob;
236 keymaster_error_t error = keymaster1_dev->generate_key(keymaster1_dev, &param_set,
237 &key_blob,
238 NULL /* characteristics */);
239 if (error != KM_ERROR_OK) {
240 SLOGE("Failed to generate keymaster1 key, error %d", error);
241 rc = -1;
242 goto out;
243 }
244
245 key = (uint8_t*)key_blob.key_material;
246 key_size = key_blob.key_material_size;
247 }
248 else if (keymaster0_dev) {
249 keymaster_rsa_keygen_params_t params;
250 memset(&params, '\0', sizeof(params));
251 params.public_exponent = RSA_EXPONENT;
252 params.modulus_size = RSA_KEY_SIZE;
253
254 if (keymaster0_dev->generate_keypair(keymaster0_dev, TYPE_RSA, &params,
255 &key, &key_size)) {
256 SLOGE("Failed to generate keypair");
257 rc = -1;
258 goto out;
259 }
260 } else {
261 SLOGE("Cryptfs bug: keymaster_init succeeded but didn't initialize a device");
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700262 rc = -1;
263 goto out;
264 }
265
266 if (key_size > KEYMASTER_BLOB_SIZE) {
267 SLOGE("Keymaster key too large for crypto footer");
268 rc = -1;
269 goto out;
270 }
271
272 memcpy(ftr->keymaster_blob, key, key_size);
273 ftr->keymaster_blob_size = key_size;
274
275out:
Shawn Willdenda6e8992015-06-03 09:40:45 -0600276 if (keymaster0_dev)
277 keymaster0_close(keymaster0_dev);
278 if (keymaster1_dev)
279 keymaster1_close(keymaster1_dev);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700280 free(key);
281 return rc;
282}
283
Shawn Willdene17a9c42014-09-08 13:04:08 -0600284/* This signs the given object using the keymaster key. */
285static int keymaster_sign_object(struct crypt_mnt_ftr *ftr,
Shawn Willden47ba10d2014-09-03 17:07:06 -0600286 const unsigned char *object,
287 const size_t object_size,
288 unsigned char **signature,
289 size_t *signature_size)
290{
291 int rc = 0;
Shawn Willdenda6e8992015-06-03 09:40:45 -0600292 keymaster0_device_t *keymaster0_dev = 0;
293 keymaster1_device_t *keymaster1_dev = 0;
294 if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) {
Shawn Willden47ba10d2014-09-03 17:07:06 -0600295 SLOGE("Failed to init keymaster");
Shawn Willdenda6e8992015-06-03 09:40:45 -0600296 rc = -1;
297 goto out;
Shawn Willden47ba10d2014-09-03 17:07:06 -0600298 }
299
Shawn Willden47ba10d2014-09-03 17:07:06 -0600300 unsigned char to_sign[RSA_KEY_SIZE_BYTES];
Shawn Willdene17a9c42014-09-08 13:04:08 -0600301 size_t to_sign_size = sizeof(to_sign);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600302 memset(to_sign, 0, RSA_KEY_SIZE_BYTES);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600303
Shawn Willdene17a9c42014-09-08 13:04:08 -0600304 // To sign a message with RSA, the message must satisfy two
305 // constraints:
306 //
307 // 1. The message, when interpreted as a big-endian numeric value, must
308 // be strictly less than the public modulus of the RSA key. Note
309 // that because the most significant bit of the public modulus is
310 // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit
311 // key), an n-bit message with most significant bit 0 always
312 // satisfies this requirement.
313 //
314 // 2. The message must have the same length in bits as the public
315 // modulus of the RSA key. This requirement isn't mathematically
316 // necessary, but is necessary to ensure consistency in
317 // implementations.
318 switch (ftr->kdf_type) {
Shawn Willdene17a9c42014-09-08 13:04:08 -0600319 case KDF_SCRYPT_KEYMASTER:
320 // This ensures the most significant byte of the signed message
321 // is zero. We could have zero-padded to the left instead, but
322 // this approach is slightly more robust against changes in
323 // object size. However, it's still broken (but not unusably
Shawn Willdenda6e8992015-06-03 09:40:45 -0600324 // so) because we really should be using a proper deterministic
325 // RSA padding function, such as PKCS1.
Shawn Willdene17a9c42014-09-08 13:04:08 -0600326 memcpy(to_sign + 1, object, min(RSA_KEY_SIZE_BYTES - 1, object_size));
327 SLOGI("Signing safely-padded object");
328 break;
329 default:
330 SLOGE("Unknown KDF type %d", ftr->kdf_type);
Shawn Willdenda6e8992015-06-03 09:40:45 -0600331 rc = -1;
332 goto out;
Shawn Willdene17a9c42014-09-08 13:04:08 -0600333 }
334
Shawn Willdenda6e8992015-06-03 09:40:45 -0600335 if (keymaster0_dev) {
336 keymaster_rsa_sign_params_t params;
337 params.digest_type = DIGEST_NONE;
338 params.padding_type = PADDING_NONE;
Shawn Willden47ba10d2014-09-03 17:07:06 -0600339
Shawn Willdenda6e8992015-06-03 09:40:45 -0600340 rc = keymaster0_dev->sign_data(keymaster0_dev,
341 &params,
342 ftr->keymaster_blob,
343 ftr->keymaster_blob_size,
344 to_sign,
345 to_sign_size,
346 signature,
347 signature_size);
348 goto out;
349 } else if (keymaster1_dev) {
350 keymaster_key_blob_t key = { ftr->keymaster_blob, ftr->keymaster_blob_size };
351 keymaster_key_param_t params[] = {
352 keymaster_param_enum(KM_TAG_PADDING, KM_PAD_NONE),
353 keymaster_param_enum(KM_TAG_DIGEST, KM_DIGEST_NONE),
354 };
355 keymaster_key_param_set_t param_set = { params, sizeof(params)/sizeof(*params) };
356 keymaster_operation_handle_t op_handle;
357 keymaster_error_t error = keymaster1_dev->begin(keymaster1_dev, KM_PURPOSE_SIGN, &key,
358 &param_set, NULL /* out_params */,
359 &op_handle);
Shawn Willden04170602015-06-18 12:26:59 -0600360 if (error == KM_ERROR_KEY_RATE_LIMIT_EXCEEDED) {
Shawn Willdenda6e8992015-06-03 09:40:45 -0600361 // Key usage has been rate-limited. Wait a bit and try again.
362 sleep(KEYMASTER_CRYPTFS_RATE_LIMIT);
363 error = keymaster1_dev->begin(keymaster1_dev, KM_PURPOSE_SIGN, &key,
364 &param_set, NULL /* out_params */,
365 &op_handle);
366 }
367 if (error != KM_ERROR_OK) {
368 SLOGE("Error starting keymaster signature transaction: %d", error);
369 rc = -1;
370 goto out;
371 }
372
373 keymaster_blob_t input = { to_sign, to_sign_size };
374 size_t input_consumed;
375 error = keymaster1_dev->update(keymaster1_dev, op_handle, NULL /* in_params */,
376 &input, &input_consumed, NULL /* out_params */,
377 NULL /* output */);
378 if (error != KM_ERROR_OK) {
379 SLOGE("Error sending data to keymaster signature transaction: %d", error);
380 rc = -1;
381 goto out;
382 }
383 if (input_consumed != to_sign_size) {
384 // This should never happen. If it does, it's a bug in the keymaster implementation.
385 SLOGE("Keymaster update() did not consume all data.");
386 keymaster1_dev->abort(keymaster1_dev, op_handle);
387 rc = -1;
388 goto out;
389 }
390
391 keymaster_blob_t tmp_sig;
392 error = keymaster1_dev->finish(keymaster1_dev, op_handle, NULL /* in_params */,
393 NULL /* verify signature */, NULL /* out_params */,
394 &tmp_sig);
395 if (error != KM_ERROR_OK) {
396 SLOGE("Error finishing keymaster signature transaction: %d", error);
397 rc = -1;
398 goto out;
399 }
400
401 *signature = (uint8_t*)tmp_sig.data;
402 *signature_size = tmp_sig.data_length;
403 } else {
404 SLOGE("Cryptfs bug: keymaster_init succeded but didn't initialize a device.");
405 rc = -1;
406 goto out;
407 }
408
409 out:
410 if (keymaster1_dev)
411 keymaster1_close(keymaster1_dev);
412 if (keymaster0_dev)
413 keymaster0_close(keymaster0_dev);
414
415 return rc;
Shawn Willden47ba10d2014-09-03 17:07:06 -0600416}
417
Paul Lawrence399317e2014-03-10 13:20:50 -0700418/* Store password when userdata is successfully decrypted and mounted.
419 * Cleared by cryptfs_clear_password
420 *
421 * To avoid a double prompt at boot, we need to store the CryptKeeper
422 * password and pass it to KeyGuard, which uses it to unlock KeyStore.
423 * Since the entire framework is torn down and rebuilt after encryption,
424 * we have to use a daemon or similar to store the password. Since vold
425 * is secured against IPC except from system processes, it seems a reasonable
426 * place to store this.
427 *
428 * password should be cleared once it has been used.
429 *
430 * password is aged out after password_max_age_seconds seconds.
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800431 */
Paul Lawrence399317e2014-03-10 13:20:50 -0700432static char* password = 0;
433static int password_expiry_time = 0;
434static const int password_max_age_seconds = 60;
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800435
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800436extern struct fstab *fstab;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800437
Paul Lawrence87999172014-02-20 12:21:31 -0800438enum RebootType {reboot, recovery, shutdown};
439static void cryptfs_reboot(enum RebootType rt)
Ken Sumralladfba362013-06-04 16:37:52 -0700440{
Paul Lawrence87999172014-02-20 12:21:31 -0800441 switch(rt) {
442 case reboot:
443 property_set(ANDROID_RB_PROPERTY, "reboot");
444 break;
445
446 case recovery:
447 property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
448 break;
449
450 case shutdown:
451 property_set(ANDROID_RB_PROPERTY, "shutdown");
452 break;
Ken Sumralladfba362013-06-04 16:37:52 -0700453 }
Paul Lawrence87999172014-02-20 12:21:31 -0800454
Ken Sumralladfba362013-06-04 16:37:52 -0700455 sleep(20);
456
457 /* Shouldn't get here, reboot should happen before sleep times out */
458 return;
459}
460
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800461static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
462{
463 memset(io, 0, dataSize);
464 io->data_size = dataSize;
465 io->data_start = sizeof(struct dm_ioctl);
466 io->version[0] = 4;
467 io->version[1] = 0;
468 io->version[2] = 0;
469 io->flags = flags;
470 if (name) {
Marek Pola5e6b9142015-02-05 14:22:34 +0100471 strlcpy(io->name, name, sizeof(io->name));
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800472 }
473}
474
Kenny Rootc4c70f12013-06-14 12:11:38 -0700475/**
476 * Gets the default device scrypt parameters for key derivation time tuning.
477 * The parameters should lead to about one second derivation time for the
478 * given device.
479 */
480static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700481 char paramstr[PROPERTY_VALUE_MAX];
Paul Crowley63c18d32016-02-10 14:02:47 +0000482 int Nf, rf, pf;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700483
Paul Crowley63c18d32016-02-10 14:02:47 +0000484 property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS);
485 if (!parse_scrypt_parameters(paramstr, &Nf, &rf, &pf)) {
486 SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
487 parse_scrypt_parameters(SCRYPT_DEFAULTS, &Nf, &rf, &pf);
Kenny Rootc4c70f12013-06-14 12:11:38 -0700488 }
Paul Crowley63c18d32016-02-10 14:02:47 +0000489 ftr->N_factor = Nf;
490 ftr->r_factor = rf;
491 ftr->p_factor = pf;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700492}
493
Ken Sumrall3ed82362011-01-28 23:31:16 -0800494static unsigned int get_fs_size(char *dev)
495{
496 int fd, block_size;
497 struct ext4_super_block sb;
498 off64_t len;
499
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700500 if ((fd = open(dev, O_RDONLY|O_CLOEXEC)) < 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -0800501 SLOGE("Cannot open device to get filesystem size ");
502 return 0;
503 }
504
505 if (lseek64(fd, 1024, SEEK_SET) < 0) {
506 SLOGE("Cannot seek to superblock");
507 return 0;
508 }
509
510 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
511 SLOGE("Cannot read superblock");
512 return 0;
513 }
514
515 close(fd);
516
Daniel Rosenberge82df162014-08-15 22:19:23 +0000517 if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) {
518 SLOGE("Not a valid ext4 superblock");
519 return 0;
520 }
Ken Sumrall3ed82362011-01-28 23:31:16 -0800521 block_size = 1024 << sb.s_log_block_size;
522 /* compute length in bytes */
523 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
524
525 /* return length in sectors */
526 return (unsigned int) (len / 512);
527}
528
Ken Sumrall160b4d62013-04-22 12:15:39 -0700529static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
530{
531 static int cached_data = 0;
532 static off64_t cached_off = 0;
533 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
534 int fd;
535 char key_loc[PROPERTY_VALUE_MAX];
536 char real_blkdev[PROPERTY_VALUE_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -0700537 int rc = -1;
538
539 if (!cached_data) {
540 fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
541
542 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700543 if ( (fd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700544 SLOGE("Cannot open real block device %s\n", real_blkdev);
545 return -1;
546 }
547
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900548 unsigned long nr_sec = 0;
549 get_blkdev_size(fd, &nr_sec);
550 if (nr_sec != 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700551 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
552 * encryption info footer and key, and plenty of bytes to spare for future
553 * growth.
554 */
555 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
556 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
557 cached_data = 1;
558 } else {
559 SLOGE("Cannot get size of block device %s\n", real_blkdev);
560 }
561 close(fd);
562 } else {
563 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
564 cached_off = 0;
565 cached_data = 1;
566 }
567 }
568
569 if (cached_data) {
570 if (metadata_fname) {
571 *metadata_fname = cached_metadata_fname;
572 }
573 if (off) {
574 *off = cached_off;
575 }
576 rc = 0;
577 }
578
579 return rc;
580}
581
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800582/* Set sha256 checksum in structure */
583static void set_ftr_sha(struct crypt_mnt_ftr *crypt_ftr)
584{
585 SHA256_CTX c;
586 SHA256_Init(&c);
587 memset(crypt_ftr->sha256, 0, sizeof(crypt_ftr->sha256));
588 SHA256_Update(&c, crypt_ftr, sizeof(*crypt_ftr));
589 SHA256_Final(crypt_ftr->sha256, &c);
590}
591
Ken Sumralle8744072011-01-18 22:01:55 -0800592/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800593 * update the failed mount count but not change the key.
594 */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700595static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800596{
597 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800598 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700599 /* starting_off is set to the SEEK_SET offset
600 * where the crypto structure starts
601 */
602 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800603 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700604 char *fname = NULL;
Ken Sumrall3be890f2011-09-14 16:53:46 -0700605 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800606
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800607 set_ftr_sha(crypt_ftr);
608
Ken Sumrall160b4d62013-04-22 12:15:39 -0700609 if (get_crypt_ftr_info(&fname, &starting_off)) {
610 SLOGE("Unable to get crypt_ftr_info\n");
611 return -1;
612 }
613 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700614 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700615 return -1;
616 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700617 if ( (fd = open(fname, O_RDWR | O_CREAT|O_CLOEXEC, 0600)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700618 SLOGE("Cannot open footer file %s for put\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700619 return -1;
620 }
621
622 /* Seek to the start of the crypt footer */
623 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
624 SLOGE("Cannot seek to real block device footer\n");
625 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800626 }
627
628 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
629 SLOGE("Cannot write real block device footer\n");
630 goto errout;
631 }
632
Ken Sumrall3be890f2011-09-14 16:53:46 -0700633 fstat(fd, &statbuf);
634 /* If the keys are kept on a raw block device, do not try to truncate it. */
Ken Sumralle550f782013-08-20 13:48:23 -0700635 if (S_ISREG(statbuf.st_mode)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700636 if (ftruncate(fd, 0x4000)) {
Colin Cross59846b62014-02-06 20:34:29 -0800637 SLOGE("Cannot set footer file size\n");
Ken Sumralle8744072011-01-18 22:01:55 -0800638 goto errout;
639 }
640 }
641
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800642 /* Success! */
643 rc = 0;
644
645errout:
646 close(fd);
647 return rc;
648
649}
650
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800651static bool check_ftr_sha(const struct crypt_mnt_ftr *crypt_ftr)
652{
653 struct crypt_mnt_ftr copy;
654 memcpy(&copy, crypt_ftr, sizeof(copy));
655 set_ftr_sha(&copy);
656 return memcmp(copy.sha256, crypt_ftr->sha256, sizeof(copy.sha256)) == 0;
657}
658
Ken Sumrall160b4d62013-04-22 12:15:39 -0700659static inline int unix_read(int fd, void* buff, int len)
660{
661 return TEMP_FAILURE_RETRY(read(fd, buff, len));
662}
663
664static inline int unix_write(int fd, const void* buff, int len)
665{
666 return TEMP_FAILURE_RETRY(write(fd, buff, len));
667}
668
669static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
670{
671 memset(pdata, 0, len);
672 pdata->persist_magic = PERSIST_DATA_MAGIC;
673 pdata->persist_valid_entries = 0;
674}
675
676/* A routine to update the passed in crypt_ftr to the lastest version.
677 * fd is open read/write on the device that holds the crypto footer and persistent
678 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
679 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
680 */
681static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
682{
Kenny Root7434b312013-06-14 11:29:53 -0700683 int orig_major = crypt_ftr->major_version;
684 int orig_minor = crypt_ftr->minor_version;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700685
Kenny Root7434b312013-06-14 11:29:53 -0700686 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
687 struct crypt_persist_data *pdata;
688 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700689
Kenny Rootc4c70f12013-06-14 12:11:38 -0700690 SLOGW("upgrading crypto footer to 1.1");
691
Kenny Root7434b312013-06-14 11:29:53 -0700692 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
693 if (pdata == NULL) {
694 SLOGE("Cannot allocate persisent data\n");
695 return;
696 }
697 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
698
699 /* Need to initialize the persistent data area */
700 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
701 SLOGE("Cannot seek to persisent data offset\n");
Henrik Baard91064632015-02-05 15:09:17 +0100702 free(pdata);
Kenny Root7434b312013-06-14 11:29:53 -0700703 return;
704 }
705 /* Write all zeros to the first copy, making it invalid */
706 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
707
708 /* Write a valid but empty structure to the second copy */
709 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
710 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
711
712 /* Update the footer */
713 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
714 crypt_ftr->persist_data_offset[0] = pdata_offset;
715 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
716 crypt_ftr->minor_version = 1;
Henrik Baard91064632015-02-05 15:09:17 +0100717 free(pdata);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700718 }
719
Paul Lawrencef4faa572014-01-29 13:31:03 -0800720 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700721 SLOGW("upgrading crypto footer to 1.2");
JP Abgrall7bdfa522013-11-15 13:42:56 -0800722 /* But keep the old kdf_type.
723 * It will get updated later to KDF_SCRYPT after the password has been verified.
724 */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700725 crypt_ftr->kdf_type = KDF_PBKDF2;
726 get_device_scrypt_params(crypt_ftr);
727 crypt_ftr->minor_version = 2;
728 }
729
Paul Lawrencef4faa572014-01-29 13:31:03 -0800730 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
731 SLOGW("upgrading crypto footer to 1.3");
732 crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
733 crypt_ftr->minor_version = 3;
734 }
735
Kenny Root7434b312013-06-14 11:29:53 -0700736 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
737 if (lseek64(fd, offset, SEEK_SET) == -1) {
738 SLOGE("Cannot seek to crypt footer\n");
739 return;
740 }
741 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700742 }
Ken Sumrall160b4d62013-04-22 12:15:39 -0700743}
744
745
746static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800747{
748 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800749 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700750 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800751 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700752 char *fname = NULL;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700753 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800754
Ken Sumrall160b4d62013-04-22 12:15:39 -0700755 if (get_crypt_ftr_info(&fname, &starting_off)) {
756 SLOGE("Unable to get crypt_ftr_info\n");
757 return -1;
758 }
759 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700760 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700761 return -1;
762 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700763 if ( (fd = open(fname, O_RDWR|O_CLOEXEC)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700764 SLOGE("Cannot open footer file %s for get\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700765 return -1;
766 }
767
768 /* Make sure it's 16 Kbytes in length */
769 fstat(fd, &statbuf);
770 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
771 SLOGE("footer file %s is not the expected size!\n", fname);
772 goto errout;
773 }
774
775 /* Seek to the start of the crypt footer */
776 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
777 SLOGE("Cannot seek to real block device footer\n");
778 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800779 }
780
781 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
782 SLOGE("Cannot read real block device footer\n");
783 goto errout;
784 }
785
786 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700787 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800788 goto errout;
789 }
790
Kenny Rootc96a5f82013-06-14 12:08:28 -0700791 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
792 SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
793 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800794 goto errout;
795 }
796
Kenny Rootc96a5f82013-06-14 12:08:28 -0700797 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
798 SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
799 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800800 }
801
Ken Sumrall160b4d62013-04-22 12:15:39 -0700802 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
803 * copy on disk before returning.
804 */
Kenny Rootc96a5f82013-06-14 12:08:28 -0700805 if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700806 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
Ken Sumralle8744072011-01-18 22:01:55 -0800807 }
808
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800809 /* Success! */
810 rc = 0;
811
812errout:
813 close(fd);
814 return rc;
815}
816
Ken Sumrall160b4d62013-04-22 12:15:39 -0700817static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
818{
819 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
820 crypt_ftr->persist_data_offset[1]) {
821 SLOGE("Crypt_ftr persist data regions overlap");
822 return -1;
823 }
824
825 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
826 SLOGE("Crypt_ftr persist data region 0 starts after region 1");
827 return -1;
828 }
829
830 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
831 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
832 CRYPT_FOOTER_OFFSET) {
833 SLOGE("Persistent data extends past crypto footer");
834 return -1;
835 }
836
837 return 0;
838}
839
840static int load_persistent_data(void)
841{
842 struct crypt_mnt_ftr crypt_ftr;
843 struct crypt_persist_data *pdata = NULL;
844 char encrypted_state[PROPERTY_VALUE_MAX];
845 char *fname;
846 int found = 0;
847 int fd;
848 int ret;
849 int i;
850
851 if (persist_data) {
852 /* Nothing to do, we've already loaded or initialized it */
853 return 0;
854 }
855
856
857 /* If not encrypted, just allocate an empty table and initialize it */
858 property_get("ro.crypto.state", encrypted_state, "");
859 if (strcmp(encrypted_state, "encrypted") ) {
860 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
861 if (pdata) {
862 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
863 persist_data = pdata;
864 return 0;
865 }
866 return -1;
867 }
868
869 if(get_crypt_ftr_and_key(&crypt_ftr)) {
870 return -1;
871 }
872
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700873 if ((crypt_ftr.major_version < 1)
874 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700875 SLOGE("Crypt_ftr version doesn't support persistent data");
876 return -1;
877 }
878
879 if (get_crypt_ftr_info(&fname, NULL)) {
880 return -1;
881 }
882
883 ret = validate_persistent_data_storage(&crypt_ftr);
884 if (ret) {
885 return -1;
886 }
887
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700888 fd = open(fname, O_RDONLY|O_CLOEXEC);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700889 if (fd < 0) {
890 SLOGE("Cannot open %s metadata file", fname);
891 return -1;
892 }
893
Paul Lawrence300dae72016-03-11 11:02:52 -0800894 pdata = malloc(crypt_ftr.persist_data_size);
895 if (pdata == NULL) {
896 SLOGE("Cannot allocate memory for persistent data");
897 goto err;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700898 }
899
900 for (i = 0; i < 2; i++) {
901 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
902 SLOGE("Cannot seek to read persistent data on %s", fname);
903 goto err2;
904 }
905 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
906 SLOGE("Error reading persistent data on iteration %d", i);
907 goto err2;
908 }
909 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
910 found = 1;
911 break;
912 }
913 }
914
915 if (!found) {
916 SLOGI("Could not find valid persistent data, creating");
917 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
918 }
919
920 /* Success */
921 persist_data = pdata;
922 close(fd);
923 return 0;
924
925err2:
926 free(pdata);
927
928err:
929 close(fd);
930 return -1;
931}
932
933static int save_persistent_data(void)
934{
935 struct crypt_mnt_ftr crypt_ftr;
936 struct crypt_persist_data *pdata;
937 char *fname;
938 off64_t write_offset;
939 off64_t erase_offset;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700940 int fd;
941 int ret;
942
943 if (persist_data == NULL) {
944 SLOGE("No persistent data to save");
945 return -1;
946 }
947
948 if(get_crypt_ftr_and_key(&crypt_ftr)) {
949 return -1;
950 }
951
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700952 if ((crypt_ftr.major_version < 1)
953 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700954 SLOGE("Crypt_ftr version doesn't support persistent data");
955 return -1;
956 }
957
958 ret = validate_persistent_data_storage(&crypt_ftr);
959 if (ret) {
960 return -1;
961 }
962
963 if (get_crypt_ftr_info(&fname, NULL)) {
964 return -1;
965 }
966
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700967 fd = open(fname, O_RDWR|O_CLOEXEC);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700968 if (fd < 0) {
969 SLOGE("Cannot open %s metadata file", fname);
970 return -1;
971 }
972
973 pdata = malloc(crypt_ftr.persist_data_size);
974 if (pdata == NULL) {
975 SLOGE("Cannot allocate persistant data");
976 goto err;
977 }
978
979 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
980 SLOGE("Cannot seek to read persistent data on %s", fname);
981 goto err2;
982 }
983
984 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
985 SLOGE("Error reading persistent data before save");
986 goto err2;
987 }
988
989 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
990 /* The first copy is the curent valid copy, so write to
991 * the second copy and erase this one */
992 write_offset = crypt_ftr.persist_data_offset[1];
993 erase_offset = crypt_ftr.persist_data_offset[0];
994 } else {
995 /* The second copy must be the valid copy, so write to
996 * the first copy, and erase the second */
997 write_offset = crypt_ftr.persist_data_offset[0];
998 erase_offset = crypt_ftr.persist_data_offset[1];
999 }
1000
1001 /* Write the new copy first, if successful, then erase the old copy */
Björn Landström96dbee72015-01-20 12:43:56 +01001002 if (lseek64(fd, write_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -07001003 SLOGE("Cannot seek to write persistent data");
1004 goto err2;
1005 }
1006 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
1007 (int) crypt_ftr.persist_data_size) {
Björn Landström96dbee72015-01-20 12:43:56 +01001008 if (lseek64(fd, erase_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -07001009 SLOGE("Cannot seek to erase previous persistent data");
1010 goto err2;
1011 }
1012 fsync(fd);
1013 memset(pdata, 0, crypt_ftr.persist_data_size);
1014 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
1015 (int) crypt_ftr.persist_data_size) {
1016 SLOGE("Cannot write to erase previous persistent data");
1017 goto err2;
1018 }
1019 fsync(fd);
1020 } else {
1021 SLOGE("Cannot write to save persistent data");
1022 goto err2;
1023 }
1024
1025 /* Success */
1026 free(pdata);
1027 close(fd);
1028 return 0;
1029
1030err2:
1031 free(pdata);
1032err:
1033 close(fd);
1034 return -1;
1035}
1036
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001037/* Convert a binary key of specified length into an ascii hex string equivalent,
1038 * without the leading 0x and with null termination
1039 */
Jeff Sharkey9c484982015-03-31 10:35:33 -07001040static void convert_key_to_hex_ascii(const unsigned char *master_key,
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001041 unsigned int keysize, char *master_key_ascii) {
1042 unsigned int i, a;
1043 unsigned char nibble;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001044
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001045 for (i=0, a=0; i<keysize; i++, a+=2) {
1046 /* For each byte, write out two ascii hex digits */
1047 nibble = (master_key[i] >> 4) & 0xf;
1048 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001049
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001050 nibble = master_key[i] & 0xf;
1051 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
1052 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001053
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001054 /* Add the null termination */
1055 master_key_ascii[a] = '\0';
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001056
1057}
1058
Jeff Sharkey9c484982015-03-31 10:35:33 -07001059static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr,
1060 const unsigned char *master_key, const char *real_blk_name,
1061 const char *name, int fd, const char *extra_params) {
Dan Albertc07fa3f2014-12-18 10:00:55 -08001062 _Alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumralldb5e0262013-02-05 17:39:48 -08001063 struct dm_ioctl *io;
1064 struct dm_target_spec *tgt;
1065 char *crypt_params;
1066 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
1067 int i;
1068
1069 io = (struct dm_ioctl *) buffer;
1070
1071 /* Load the mapping table for this device */
1072 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
1073
1074 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1075 io->target_count = 1;
1076 tgt->status = 0;
1077 tgt->sector_start = 0;
1078 tgt->length = crypt_ftr->fs_size;
Ajay Dudani87701e22014-09-17 21:02:52 -07001079 strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME);
Ken Sumralldb5e0262013-02-05 17:39:48 -08001080
1081 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
1082 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
1083 sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name,
1084 master_key_ascii, real_blk_name, extra_params);
1085 crypt_params += strlen(crypt_params) + 1;
1086 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
1087 tgt->next = crypt_params - buffer;
1088
1089 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
1090 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
1091 break;
1092 }
1093 usleep(500000);
1094 }
1095
1096 if (i == TABLE_LOAD_RETRIES) {
1097 /* We failed to load the table, return an error */
1098 return -1;
1099 } else {
1100 return i + 1;
1101 }
1102}
1103
1104
1105static int get_dm_crypt_version(int fd, const char *name, int *version)
1106{
1107 char buffer[DM_CRYPT_BUF_SIZE];
1108 struct dm_ioctl *io;
1109 struct dm_target_versions *v;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001110
1111 io = (struct dm_ioctl *) buffer;
1112
1113 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1114
1115 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
1116 return -1;
1117 }
1118
1119 /* Iterate over the returned versions, looking for name of "crypt".
1120 * When found, get and return the version.
1121 */
1122 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
1123 while (v->next) {
1124 if (! strcmp(v->name, "crypt")) {
1125 /* We found the crypt driver, return the version, and get out */
1126 version[0] = v->version[0];
1127 version[1] = v->version[1];
1128 version[2] = v->version[2];
1129 return 0;
1130 }
1131 v = (struct dm_target_versions *)(((char *)v) + v->next);
1132 }
1133
1134 return -1;
1135}
1136
Jeff Sharkey9c484982015-03-31 10:35:33 -07001137static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr,
1138 const unsigned char *master_key, const char *real_blk_name,
1139 char *crypto_blk_name, const char *name) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001140 char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001141 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001142 unsigned int minor;
Ajay Dudani87701e22014-09-17 21:02:52 -07001143 int fd=0;
Daniel Rosenberg25a52132016-02-26 16:44:36 -08001144 int err;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001145 int retval = -1;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001146 int version[3];
1147 char *extra_params;
1148 int load_count;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001149
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001150 if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001151 SLOGE("Cannot open device-mapper\n");
1152 goto errout;
1153 }
1154
1155 io = (struct dm_ioctl *) buffer;
1156
1157 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Daniel Rosenberg25a52132016-02-26 16:44:36 -08001158 err = ioctl(fd, DM_DEV_CREATE, io);
1159 if (err) {
1160 SLOGE("Cannot create dm-crypt device %s: %s\n", name, strerror(errno));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001161 goto errout;
1162 }
1163
1164 /* Get the device status, in particular, the name of it's device file */
1165 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1166 if (ioctl(fd, DM_DEV_STATUS, io)) {
1167 SLOGE("Cannot retrieve dm-crypt device status\n");
1168 goto errout;
1169 }
1170 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
1171 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
1172
Ken Sumralldb5e0262013-02-05 17:39:48 -08001173 extra_params = "";
1174 if (! get_dm_crypt_version(fd, name, version)) {
1175 /* Support for allow_discards was added in version 1.11.0 */
1176 if ((version[0] >= 2) ||
1177 ((version[0] == 1) && (version[1] >= 11))) {
1178 extra_params = "1 allow_discards";
1179 SLOGI("Enabling support for allow_discards in dmcrypt.\n");
1180 }
Ken Sumralle919efe2012-09-29 17:07:41 -07001181 }
1182
Ken Sumralldb5e0262013-02-05 17:39:48 -08001183 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name,
1184 fd, extra_params);
1185 if (load_count < 0) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001186 SLOGE("Cannot load dm-crypt mapping table.\n");
1187 goto errout;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001188 } else if (load_count > 1) {
1189 SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001190 }
1191
1192 /* Resume this device to activate it */
Ken Sumralldb5e0262013-02-05 17:39:48 -08001193 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001194
1195 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
1196 SLOGE("Cannot resume the dm-crypt device\n");
1197 goto errout;
1198 }
1199
1200 /* We made it here with no errors. Woot! */
1201 retval = 0;
1202
1203errout:
1204 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1205
1206 return retval;
1207}
1208
Ken Sumrall29d8da82011-05-18 17:20:07 -07001209static int delete_crypto_blk_dev(char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001210{
1211 int fd;
1212 char buffer[DM_CRYPT_BUF_SIZE];
1213 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001214 int retval = -1;
1215
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001216 if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001217 SLOGE("Cannot open device-mapper\n");
1218 goto errout;
1219 }
1220
1221 io = (struct dm_ioctl *) buffer;
1222
1223 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1224 if (ioctl(fd, DM_DEV_REMOVE, io)) {
1225 SLOGE("Cannot remove dm-crypt device\n");
1226 goto errout;
1227 }
1228
1229 /* We made it here with no errors. Woot! */
1230 retval = 0;
1231
1232errout:
1233 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1234
1235 return retval;
1236
1237}
1238
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001239static int pbkdf2(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001240 unsigned char *ikey, void *params UNUSED)
1241{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001242 SLOGI("Using pbkdf2 for cryptfs KDF");
1243
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001244 /* Turn the password into a key and IV that can decrypt the master key */
Adam Langleybf0d9722015-11-04 14:51:39 -08001245 return PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
1246 HASH_COUNT, KEY_LEN_BYTES + IV_LEN_BYTES,
1247 ikey) != 1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001248}
1249
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001250static int scrypt(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001251 unsigned char *ikey, void *params)
1252{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001253 SLOGI("Using scrypt for cryptfs KDF");
1254
Kenny Rootc4c70f12013-06-14 12:11:38 -07001255 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1256
1257 int N = 1 << ftr->N_factor;
1258 int r = 1 << ftr->r_factor;
1259 int p = 1 << ftr->p_factor;
1260
1261 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001262 unsigned int keysize;
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001263 crypto_scrypt((const uint8_t*)passwd, strlen(passwd),
1264 salt, SALT_LEN, N, r, p, ikey,
1265 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001266
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001267 return 0;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001268}
1269
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001270static int scrypt_keymaster(const char *passwd, const unsigned char *salt,
1271 unsigned char *ikey, void *params)
1272{
1273 SLOGI("Using scrypt with keymaster for cryptfs KDF");
1274
1275 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001276 size_t signature_size;
1277 unsigned char* signature;
1278 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1279
1280 int N = 1 << ftr->N_factor;
1281 int r = 1 << ftr->r_factor;
1282 int p = 1 << ftr->p_factor;
1283
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001284 rc = crypto_scrypt((const uint8_t*)passwd, strlen(passwd),
1285 salt, SALT_LEN, N, r, p, ikey,
1286 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001287
1288 if (rc) {
1289 SLOGE("scrypt failed");
1290 return -1;
1291 }
1292
Shawn Willdene17a9c42014-09-08 13:04:08 -06001293 if (keymaster_sign_object(ftr, ikey, KEY_LEN_BYTES + IV_LEN_BYTES,
1294 &signature, &signature_size)) {
1295 SLOGE("Signing failed");
1296 return -1;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001297 }
1298
1299 rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN,
1300 N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES);
1301 free(signature);
1302
1303 if (rc) {
1304 SLOGE("scrypt failed");
1305 return -1;
1306 }
1307
1308 return 0;
1309}
1310
1311static int encrypt_master_key(const char *passwd, const unsigned char *salt,
1312 const unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001313 unsigned char *encrypted_master_key,
1314 struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001315{
1316 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
1317 EVP_CIPHER_CTX e_ctx;
1318 int encrypted_len, final_len;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001319 int rc = 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001320
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001321 /* Turn the password into an intermediate key and IV that can decrypt the master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001322 get_device_scrypt_params(crypt_ftr);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001323
1324 switch (crypt_ftr->kdf_type) {
1325 case KDF_SCRYPT_KEYMASTER:
1326 if (keymaster_create_key(crypt_ftr)) {
1327 SLOGE("keymaster_create_key failed");
1328 return -1;
1329 }
1330
1331 if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) {
1332 SLOGE("scrypt failed");
1333 return -1;
1334 }
1335 break;
1336
1337 case KDF_SCRYPT:
1338 if (scrypt(passwd, salt, ikey, crypt_ftr)) {
1339 SLOGE("scrypt failed");
1340 return -1;
1341 }
1342 break;
1343
1344 default:
1345 SLOGE("Invalid kdf_type");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001346 return -1;
1347 }
Kenny Rootc4c70f12013-06-14 12:11:38 -07001348
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001349 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001350 EVP_CIPHER_CTX_init(&e_ctx);
1351 if (! EVP_EncryptInit_ex(&e_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001352 SLOGE("EVP_EncryptInit failed\n");
1353 return -1;
1354 }
1355 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001356
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001357 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001358 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
Paul Lawrence731a7a22015-04-28 22:14:15 +00001359 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001360 SLOGE("EVP_EncryptUpdate failed\n");
1361 return -1;
1362 }
Adam Langley889c4f12014-09-03 14:23:13 -07001363 if (! EVP_EncryptFinal_ex(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001364 SLOGE("EVP_EncryptFinal failed\n");
1365 return -1;
1366 }
1367
1368 if (encrypted_len + final_len != KEY_LEN_BYTES) {
1369 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
1370 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001371 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001372
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001373 /* Store the scrypt of the intermediate key, so we can validate if it's a
1374 password error or mount error when things go wrong.
1375 Note there's no need to check for errors, since if this is incorrect, we
1376 simply won't wipe userdata, which is the correct default behavior
1377 */
1378 int N = 1 << crypt_ftr->N_factor;
1379 int r = 1 << crypt_ftr->r_factor;
1380 int p = 1 << crypt_ftr->p_factor;
1381
1382 rc = crypto_scrypt(ikey, KEY_LEN_BYTES,
1383 crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p,
1384 crypt_ftr->scrypted_intermediate_key,
1385 sizeof(crypt_ftr->scrypted_intermediate_key));
1386
1387 if (rc) {
1388 SLOGE("encrypt_master_key: crypto_scrypt failed");
1389 }
1390
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001391 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001392}
1393
Paul Lawrence731a7a22015-04-28 22:14:15 +00001394static int decrypt_master_key_aux(const char *passwd, unsigned char *salt,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001395 unsigned char *encrypted_master_key,
1396 unsigned char *decrypted_master_key,
1397 kdf_func kdf, void *kdf_params,
1398 unsigned char** intermediate_key,
1399 size_t* intermediate_key_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001400{
1401 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001402 EVP_CIPHER_CTX d_ctx;
1403 int decrypted_len, final_len;
1404
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001405 /* Turn the password into an intermediate key and IV that can decrypt the
1406 master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001407 if (kdf(passwd, salt, ikey, kdf_params)) {
1408 SLOGE("kdf failed");
1409 return -1;
1410 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001411
1412 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001413 EVP_CIPHER_CTX_init(&d_ctx);
1414 if (! EVP_DecryptInit_ex(&d_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001415 return -1;
1416 }
1417 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1418 /* Decrypt the master key */
1419 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
1420 encrypted_master_key, KEY_LEN_BYTES)) {
1421 return -1;
1422 }
Adam Langley889c4f12014-09-03 14:23:13 -07001423 if (! EVP_DecryptFinal_ex(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001424 return -1;
1425 }
1426
1427 if (decrypted_len + final_len != KEY_LEN_BYTES) {
1428 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001429 }
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001430
1431 /* Copy intermediate key if needed by params */
1432 if (intermediate_key && intermediate_key_size) {
1433 *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES);
1434 if (intermediate_key) {
1435 memcpy(*intermediate_key, ikey, KEY_LEN_BYTES);
1436 *intermediate_key_size = KEY_LEN_BYTES;
1437 }
1438 }
1439
1440 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001441}
1442
Kenny Rootc4c70f12013-06-14 12:11:38 -07001443static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001444{
Paul Lawrencedb3730c2015-02-03 13:08:10 -08001445 if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001446 *kdf = scrypt_keymaster;
1447 *kdf_params = ftr;
1448 } else if (ftr->kdf_type == KDF_SCRYPT) {
Kenny Rootc4c70f12013-06-14 12:11:38 -07001449 *kdf = scrypt;
1450 *kdf_params = ftr;
1451 } else {
1452 *kdf = pbkdf2;
1453 *kdf_params = NULL;
1454 }
1455}
1456
Paul Lawrence731a7a22015-04-28 22:14:15 +00001457static int decrypt_master_key(const char *passwd, unsigned char *decrypted_master_key,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001458 struct crypt_mnt_ftr *crypt_ftr,
1459 unsigned char** intermediate_key,
1460 size_t* intermediate_key_size)
Kenny Rootc4c70f12013-06-14 12:11:38 -07001461{
1462 kdf_func kdf;
1463 void *kdf_params;
1464 int ret;
1465
1466 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001467 ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key,
1468 decrypted_master_key, kdf, kdf_params,
1469 intermediate_key, intermediate_key_size);
Kenny Rootc4c70f12013-06-14 12:11:38 -07001470 if (ret != 0) {
1471 SLOGW("failure decrypting master key");
Kenny Rootc4c70f12013-06-14 12:11:38 -07001472 }
1473
1474 return ret;
1475}
1476
1477static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt,
1478 struct crypt_mnt_ftr *crypt_ftr) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001479 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -08001480 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001481
1482 /* Get some random bits for a key */
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001483 fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC);
Ken Sumralle8744072011-01-18 22:01:55 -08001484 read(fd, key_buf, sizeof(key_buf));
1485 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001486 close(fd);
1487
1488 /* Now encrypt it with the password */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001489 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001490}
1491
Paul Lawrence2f32cda2015-05-05 14:28:25 -07001492int wait_and_unmount(const char *mountpoint, bool kill)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001493{
Greg Hackmann955653e2014-09-24 14:55:20 -07001494 int i, err, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001495#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001496
1497 /* Now umount the tmpfs filesystem */
1498 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001499 if (umount(mountpoint) == 0) {
1500 break;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001501 }
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001502
1503 if (errno == EINVAL) {
1504 /* EINVAL is returned if the directory is not a mountpoint,
1505 * i.e. there is no filesystem mounted there. So just get out.
1506 */
1507 break;
1508 }
1509
1510 err = errno;
1511
1512 /* If allowed, be increasingly aggressive before the last two retries */
1513 if (kill) {
1514 if (i == (WAIT_UNMOUNT_COUNT - 3)) {
1515 SLOGW("sending SIGHUP to processes with open files\n");
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001516 vold_killProcessesWithOpenFiles(mountpoint, SIGTERM);
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001517 } else if (i == (WAIT_UNMOUNT_COUNT - 2)) {
1518 SLOGW("sending SIGKILL to processes with open files\n");
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001519 vold_killProcessesWithOpenFiles(mountpoint, SIGKILL);
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001520 }
1521 }
1522
1523 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001524 }
1525
1526 if (i < WAIT_UNMOUNT_COUNT) {
1527 SLOGD("unmounting %s succeeded\n", mountpoint);
1528 rc = 0;
1529 } else {
jessica_yu3f14fe42014-09-22 15:57:40 +08001530 vold_killProcessesWithOpenFiles(mountpoint, 0);
Greg Hackmann955653e2014-09-24 14:55:20 -07001531 SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001532 rc = -1;
1533 }
1534
1535 return rc;
1536}
1537
Paul Lawrenceb1ef4662015-06-11 11:15:29 -07001538#define DATA_PREP_TIMEOUT 1000
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001539static int prep_data_fs(void)
1540{
1541 int i;
1542
Jeff Sharkey47695b22016-02-01 17:02:29 -07001543 // NOTE: post_fs_data results in init calling back around to vold, so all
1544 // callers to this method must be async
1545
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001546 /* Do the prep of the /data filesystem */
1547 property_set("vold.post_fs_data_done", "0");
1548 property_set("vold.decrypt", "trigger_post_fs_data");
1549 SLOGD("Just triggered post_fs_data\n");
1550
Ken Sumrallc5872692013-05-14 15:26:31 -07001551 /* Wait a max of 50 seconds, hopefully it takes much less */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001552 for (i=0; i<DATA_PREP_TIMEOUT; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001553 char p[PROPERTY_VALUE_MAX];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001554
1555 property_get("vold.post_fs_data_done", p, "0");
1556 if (*p == '1') {
1557 break;
1558 } else {
Paul Lawrenceb1ef4662015-06-11 11:15:29 -07001559 usleep(50000);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001560 }
1561 }
1562 if (i == DATA_PREP_TIMEOUT) {
1563 /* Ugh, we failed to prep /data in time. Bail. */
Ken Sumrallc5872692013-05-14 15:26:31 -07001564 SLOGE("post_fs_data timed out!\n");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001565 return -1;
1566 } else {
1567 SLOGD("post_fs_data done\n");
1568 return 0;
1569 }
1570}
1571
Paul Lawrence74f29f12014-08-28 15:54:10 -07001572static void cryptfs_set_corrupt()
1573{
1574 // Mark the footer as bad
1575 struct crypt_mnt_ftr crypt_ftr;
1576 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1577 SLOGE("Failed to get crypto footer - panic");
1578 return;
1579 }
1580
1581 crypt_ftr.flags |= CRYPT_DATA_CORRUPT;
1582 if (put_crypt_ftr_and_key(&crypt_ftr)) {
1583 SLOGE("Failed to set crypto footer - panic");
1584 return;
1585 }
1586}
1587
1588static void cryptfs_trigger_restart_min_framework()
1589{
1590 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
1591 SLOGE("Failed to mount tmpfs on data - panic");
1592 return;
1593 }
1594
1595 if (property_set("vold.decrypt", "trigger_post_fs_data")) {
1596 SLOGE("Failed to trigger post fs data - panic");
1597 return;
1598 }
1599
1600 if (property_set("vold.decrypt", "trigger_restart_min_framework")) {
1601 SLOGE("Failed to trigger restart min framework - panic");
1602 return;
1603 }
1604}
1605
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001606/* returns < 0 on failure */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001607static int cryptfs_restart_internal(int restart_main)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001608{
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001609 char crypto_blkdev[MAXPATHLEN];
Tim Murray8439dc92014-12-15 11:56:11 -08001610 int rc = -1;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001611 static int restart_successful = 0;
1612
1613 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -06001614 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001615 SLOGE("Encrypted filesystem not validated, aborting");
1616 return -1;
1617 }
1618
1619 if (restart_successful) {
1620 SLOGE("System already restarted with encrypted disk, aborting");
1621 return -1;
1622 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001623
Paul Lawrencef4faa572014-01-29 13:31:03 -08001624 if (restart_main) {
1625 /* Here is where we shut down the framework. The init scripts
1626 * start all services in one of three classes: core, main or late_start.
1627 * On boot, we start core and main. Now, we stop main, but not core,
1628 * as core includes vold and a few other really important things that
1629 * we need to keep running. Once main has stopped, we should be able
1630 * to umount the tmpfs /data, then mount the encrypted /data.
1631 * We then restart the class main, and also the class late_start.
1632 * At the moment, I've only put a few things in late_start that I know
1633 * are not needed to bring up the framework, and that also cause problems
1634 * with unmounting the tmpfs /data, but I hope to add add more services
1635 * to the late_start class as we optimize this to decrease the delay
1636 * till the user is asked for the password to the filesystem.
1637 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001638
Paul Lawrencef4faa572014-01-29 13:31:03 -08001639 /* The init files are setup to stop the class main when vold.decrypt is
1640 * set to trigger_reset_main.
1641 */
1642 property_set("vold.decrypt", "trigger_reset_main");
1643 SLOGD("Just asked init to shut down class main\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001644
Paul Lawrencef4faa572014-01-29 13:31:03 -08001645 /* Ugh, shutting down the framework is not synchronous, so until it
1646 * can be fixed, this horrible hack will wait a moment for it all to
1647 * shut down before proceeding. Without it, some devices cannot
1648 * restart the graphics services.
1649 */
1650 sleep(2);
1651 }
Ken Sumrall9dedfd42012-10-09 14:16:59 -07001652
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001653 /* Now that the framework is shutdown, we should be able to umount()
1654 * the tmpfs filesystem, and mount the real one.
1655 */
1656
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001657 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1658 if (strlen(crypto_blkdev) == 0) {
1659 SLOGE("fs_crypto_blkdev not set\n");
1660 return -1;
1661 }
1662
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001663 if (! (rc = wait_and_unmount(DATA_MNT_POINT, true)) ) {
Doug Zongker6fd57712013-12-17 09:43:23 -08001664 /* If ro.crypto.readonly is set to 1, mount the decrypted
1665 * filesystem readonly. This is used when /data is mounted by
1666 * recovery mode.
1667 */
1668 char ro_prop[PROPERTY_VALUE_MAX];
1669 property_get("ro.crypto.readonly", ro_prop, "");
1670 if (strlen(ro_prop) > 0 && atoi(ro_prop)) {
1671 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
1672 rec->flags |= MS_RDONLY;
1673 }
JP Abgrall62c7af32014-06-16 13:01:23 -07001674
Ken Sumralle5032c42012-04-01 23:58:44 -07001675 /* If that succeeded, then mount the decrypted filesystem */
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001676 int retries = RETRY_MOUNT_ATTEMPTS;
1677 int mount_rc;
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001678
1679 /*
1680 * fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
1681 * partitions in the fsck domain.
1682 */
1683 if (setexeccon(secontextFsck())){
1684 SLOGE("Failed to setexeccon");
1685 return -1;
1686 }
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001687 while ((mount_rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT,
1688 crypto_blkdev, 0))
1689 != 0) {
1690 if (mount_rc == FS_MGR_DOMNT_BUSY) {
1691 /* TODO: invoke something similar to
1692 Process::killProcessWithOpenFiles(DATA_MNT_POINT,
1693 retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */
1694 SLOGI("Failed to mount %s because it is busy - waiting",
1695 crypto_blkdev);
1696 if (--retries) {
1697 sleep(RETRY_MOUNT_DELAY_SECONDS);
1698 } else {
1699 /* Let's hope that a reboot clears away whatever is keeping
1700 the mount busy */
1701 cryptfs_reboot(reboot);
1702 }
1703 } else {
1704 SLOGE("Failed to mount decrypted data");
1705 cryptfs_set_corrupt();
1706 cryptfs_trigger_restart_min_framework();
1707 SLOGI("Started framework to offer wipe");
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001708 if (setexeccon(NULL)) {
1709 SLOGE("Failed to setexeccon");
1710 }
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001711 return -1;
1712 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001713 }
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001714 if (setexeccon(NULL)) {
1715 SLOGE("Failed to setexeccon");
1716 return -1;
1717 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001718
Ken Sumralle5032c42012-04-01 23:58:44 -07001719 property_set("vold.decrypt", "trigger_load_persist_props");
1720 /* Create necessary paths on /data */
1721 if (prep_data_fs()) {
1722 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001723 }
Ken Sumralle5032c42012-04-01 23:58:44 -07001724
1725 /* startup service classes main and late_start */
1726 property_set("vold.decrypt", "trigger_restart_framework");
1727 SLOGD("Just triggered restart_framework\n");
1728
1729 /* Give it a few moments to get started */
1730 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001731 }
1732
Ken Sumrall0cc16632011-01-18 20:32:26 -08001733 if (rc == 0) {
1734 restart_successful = 1;
1735 }
1736
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001737 return rc;
1738}
1739
Paul Lawrencef4faa572014-01-29 13:31:03 -08001740int cryptfs_restart(void)
1741{
Paul Lawrence05335c32015-03-05 09:46:23 -08001742 SLOGI("cryptfs_restart");
Paul Crowley38132a12016-02-09 09:50:32 +00001743 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001744 SLOGE("cryptfs_restart not valid for file encryption:");
1745 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08001746 }
1747
Paul Lawrencef4faa572014-01-29 13:31:03 -08001748 /* Call internal implementation forcing a restart of main service group */
1749 return cryptfs_restart_internal(1);
1750}
1751
Paul Lawrence05335c32015-03-05 09:46:23 -08001752static int do_crypto_complete(char *mount_point)
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001753{
1754 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001755 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -08001756 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001757
1758 property_get("ro.crypto.state", encrypted_state, "");
1759 if (strcmp(encrypted_state, "encrypted") ) {
1760 SLOGE("not running with encryption, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001761 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001762 }
1763
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001764 // crypto_complete is full disk encrypted status
Paul Crowley38132a12016-02-09 09:50:32 +00001765 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001766 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Paul Lawrence05335c32015-03-05 09:46:23 -08001767 }
1768
Ken Sumrall160b4d62013-04-22 12:15:39 -07001769 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001770 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumralle5032c42012-04-01 23:58:44 -07001771
Ken Sumralle1a45852011-12-14 21:24:27 -08001772 /*
1773 * Only report this error if key_loc is a file and it exists.
1774 * If the device was never encrypted, and /data is not mountable for
1775 * some reason, returning 1 should prevent the UI from presenting the
1776 * a "enter password" screen, or worse, a "press button to wipe the
1777 * device" screen.
1778 */
1779 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
1780 SLOGE("master key file does not exist, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001781 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumralle1a45852011-12-14 21:24:27 -08001782 } else {
1783 SLOGE("Error getting crypt footer and key\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001784 return CRYPTO_COMPLETE_BAD_METADATA;
Ken Sumralle1a45852011-12-14 21:24:27 -08001785 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001786 }
1787
Paul Lawrence74f29f12014-08-28 15:54:10 -07001788 // Test for possible error flags
1789 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS){
1790 SLOGE("Encryption process is partway completed\n");
1791 return CRYPTO_COMPLETE_PARTIAL;
1792 }
1793
1794 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE){
1795 SLOGE("Encryption process was interrupted but cannot continue\n");
1796 return CRYPTO_COMPLETE_INCONSISTENT;
1797 }
1798
1799 if (crypt_ftr.flags & CRYPT_DATA_CORRUPT){
1800 SLOGE("Encryption is successful but data is corrupt\n");
1801 return CRYPTO_COMPLETE_CORRUPT;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001802 }
1803
1804 /* We passed the test! We shall diminish, and return to the west */
Paul Lawrence74f29f12014-08-28 15:54:10 -07001805 return CRYPTO_COMPLETE_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001806}
1807
Paul Lawrencef4faa572014-01-29 13:31:03 -08001808static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
1809 char *passwd, char *mount_point, char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001810{
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001811 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001812 unsigned char decrypted_master_key[32];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001813 char crypto_blkdev[MAXPATHLEN];
1814 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001815 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001816 unsigned int orig_failed_decrypt_count;
1817 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001818 int use_keymaster = 0;
1819 int upgrade = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001820 unsigned char* intermediate_key = 0;
1821 size_t intermediate_key_size = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001822
Paul Lawrencef4faa572014-01-29 13:31:03 -08001823 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1824 orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001825
Paul Lawrencef4faa572014-01-29 13:31:03 -08001826 if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001827 if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr,
1828 &intermediate_key, &intermediate_key_size)) {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001829 SLOGE("Failed to decrypt master key\n");
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001830 rc = -1;
1831 goto errout;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001832 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001833 }
1834
Paul Lawrencef4faa572014-01-29 13:31:03 -08001835 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
1836
Paul Lawrence74f29f12014-08-28 15:54:10 -07001837 // Create crypto block device - all (non fatal) code paths
1838 // need it
Paul Lawrencef4faa572014-01-29 13:31:03 -08001839 if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key,
1840 real_blkdev, crypto_blkdev, label)) {
Paul Lawrence74f29f12014-08-28 15:54:10 -07001841 SLOGE("Error creating decrypted block device\n");
1842 rc = -1;
1843 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001844 }
1845
Paul Lawrence74f29f12014-08-28 15:54:10 -07001846 /* Work out if the problem is the password or the data */
1847 unsigned char scrypted_intermediate_key[sizeof(crypt_ftr->
1848 scrypted_intermediate_key)];
1849 int N = 1 << crypt_ftr->N_factor;
1850 int r = 1 << crypt_ftr->r_factor;
1851 int p = 1 << crypt_ftr->p_factor;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001852
Paul Lawrence74f29f12014-08-28 15:54:10 -07001853 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
1854 crypt_ftr->salt, sizeof(crypt_ftr->salt),
1855 N, r, p, scrypted_intermediate_key,
1856 sizeof(scrypted_intermediate_key));
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001857
Paul Lawrence74f29f12014-08-28 15:54:10 -07001858 // Does the key match the crypto footer?
1859 if (rc == 0 && memcmp(scrypted_intermediate_key,
1860 crypt_ftr->scrypted_intermediate_key,
1861 sizeof(scrypted_intermediate_key)) == 0) {
1862 SLOGI("Password matches");
1863 rc = 0;
1864 } else {
1865 /* Try mounting the file system anyway, just in case the problem's with
1866 * the footer, not the key. */
1867 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
1868 mkdir(tmp_mount_point, 0755);
1869 if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
1870 SLOGE("Error temp mounting decrypted block device\n");
1871 delete_crypto_blk_dev(label);
1872
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001873 rc = ++crypt_ftr->failed_decrypt_count;
1874 put_crypt_ftr_and_key(crypt_ftr);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001875 } else {
1876 /* Success! */
1877 SLOGI("Password did not match but decrypted drive mounted - continue");
1878 umount(tmp_mount_point);
1879 rc = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001880 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001881 }
1882
1883 if (rc == 0) {
1884 crypt_ftr->failed_decrypt_count = 0;
Paul Lawrence72b8b822014-10-05 12:57:37 -07001885 if (orig_failed_decrypt_count != 0) {
1886 put_crypt_ftr_and_key(crypt_ftr);
1887 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001888
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001889 /* Save the name of the crypto block device
Paul Lawrence74f29f12014-08-28 15:54:10 -07001890 * so we can mount it when restarting the framework. */
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001891 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -06001892
1893 /* Also save a the master key so we can reencrypted the key
Paul Lawrence74f29f12014-08-28 15:54:10 -07001894 * the key when we want to change the password on it. */
Jason parks70a4b3f2011-01-28 10:10:47 -06001895 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001896 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -06001897 master_key_saved = 1;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001898 SLOGD("%s(): Master key saved\n", __FUNCTION__);
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001899 rc = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001900
Paul Lawrence74f29f12014-08-28 15:54:10 -07001901 // Upgrade if we're not using the latest KDF.
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001902 use_keymaster = keymaster_check_compatibility();
1903 if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Shawn Willden47ba10d2014-09-03 17:07:06 -06001904 // Don't allow downgrade
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001905 } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) {
1906 crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1907 upgrade = 1;
1908 } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001909 crypt_ftr->kdf_type = KDF_SCRYPT;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001910 upgrade = 1;
1911 }
1912
1913 if (upgrade) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001914 rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1915 crypt_ftr->master_key, crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001916 if (!rc) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001917 rc = put_crypt_ftr_and_key(crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001918 }
1919 SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
Paul Lawrenceb2f682b2014-09-08 11:28:19 -07001920
1921 // Do not fail even if upgrade failed - machine is bootable
1922 // Note that if this code is ever hit, there is a *serious* problem
1923 // since KDFs should never fail. You *must* fix the kdf before
1924 // proceeding!
1925 if (rc) {
1926 SLOGW("Upgrade failed with error %d,"
1927 " but continuing with previous state",
1928 rc);
1929 rc = 0;
1930 }
JP Abgrall7bdfa522013-11-15 13:42:56 -08001931 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001932 }
1933
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001934 errout:
1935 if (intermediate_key) {
1936 memset(intermediate_key, 0, intermediate_key_size);
1937 free(intermediate_key);
1938 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001939 return rc;
1940}
1941
Ken Sumrall29d8da82011-05-18 17:20:07 -07001942/*
Jeff Sharkey9c484982015-03-31 10:35:33 -07001943 * Called by vold when it's asked to mount an encrypted external
1944 * storage volume. The incoming partition has no crypto header/footer,
1945 * as any metadata is been stored in a separate, small partition.
1946 *
1947 * out_crypto_blkdev must be MAXPATHLEN.
Ken Sumrall29d8da82011-05-18 17:20:07 -07001948 */
Jeff Sharkey9c484982015-03-31 10:35:33 -07001949int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev,
1950 const unsigned char* key, int keysize, char* out_crypto_blkdev) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001951 int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001952 if (fd == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001953 SLOGE("Failed to open %s: %s", real_blkdev, strerror(errno));
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001954 return -1;
1955 }
1956
1957 unsigned long nr_sec = 0;
1958 get_blkdev_size(fd, &nr_sec);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001959 close(fd);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001960
Ken Sumrall29d8da82011-05-18 17:20:07 -07001961 if (nr_sec == 0) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001962 SLOGE("Failed to get size of %s: %s", real_blkdev, strerror(errno));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001963 return -1;
1964 }
1965
Jeff Sharkey9c484982015-03-31 10:35:33 -07001966 struct crypt_mnt_ftr ext_crypt_ftr;
1967 memset(&ext_crypt_ftr, 0, sizeof(ext_crypt_ftr));
1968 ext_crypt_ftr.fs_size = nr_sec;
1969 ext_crypt_ftr.keysize = keysize;
1970 strcpy((char*) ext_crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");
Ken Sumrall29d8da82011-05-18 17:20:07 -07001971
Jeff Sharkey9c484982015-03-31 10:35:33 -07001972 return create_crypto_blk_dev(&ext_crypt_ftr, key, real_blkdev,
1973 out_crypto_blkdev, label);
1974}
Ken Sumrall29d8da82011-05-18 17:20:07 -07001975
Jeff Sharkey9c484982015-03-31 10:35:33 -07001976/*
1977 * Called by vold when it's asked to unmount an encrypted external
1978 * storage volume.
1979 */
1980int cryptfs_revert_ext_volume(const char* label) {
1981 return delete_crypto_blk_dev((char*) label);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001982}
1983
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001984int cryptfs_crypto_complete(void)
1985{
1986 return do_crypto_complete("/data");
1987}
1988
Paul Lawrencef4faa572014-01-29 13:31:03 -08001989int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr)
1990{
1991 char encrypted_state[PROPERTY_VALUE_MAX];
1992 property_get("ro.crypto.state", encrypted_state, "");
1993 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
1994 SLOGE("encrypted fs already validated or not running with encryption,"
1995 " aborting");
1996 return -1;
1997 }
1998
1999 if (get_crypt_ftr_and_key(crypt_ftr)) {
2000 SLOGE("Error getting crypt footer and key");
2001 return -1;
2002 }
2003
2004 return 0;
2005}
2006
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002007int cryptfs_check_passwd(char *passwd)
2008{
Paul Lawrence05335c32015-03-05 09:46:23 -08002009 SLOGI("cryptfs_check_passwd");
Paul Crowley38132a12016-02-09 09:50:32 +00002010 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002011 SLOGE("cryptfs_check_passwd not valid for file encryption");
2012 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08002013 }
2014
Paul Lawrencef4faa572014-01-29 13:31:03 -08002015 struct crypt_mnt_ftr crypt_ftr;
2016 int rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002017
Paul Lawrencef4faa572014-01-29 13:31:03 -08002018 rc = check_unmounted_and_get_ftr(&crypt_ftr);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002019 if (rc) {
2020 SLOGE("Could not get footer");
Paul Lawrencef4faa572014-01-29 13:31:03 -08002021 return rc;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002022 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002023
Paul Lawrence3bd36d52015-06-09 13:37:44 -07002024 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002025 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
2026 if (rc) {
2027 SLOGE("Password did not match");
2028 return rc;
2029 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002030
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002031 if (crypt_ftr.flags & CRYPT_FORCE_COMPLETE) {
2032 // Here we have a default actual password but a real password
2033 // we must test against the scrypted value
2034 // First, we must delete the crypto block device that
2035 // test_mount_encrypted_fs leaves behind as a side effect
2036 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
2037 rc = test_mount_encrypted_fs(&crypt_ftr, DEFAULT_PASSWORD,
2038 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
2039 if (rc) {
2040 SLOGE("Default password did not match on reboot encryption");
2041 return rc;
2042 }
2043
2044 crypt_ftr.flags &= ~CRYPT_FORCE_COMPLETE;
2045 put_crypt_ftr_and_key(&crypt_ftr);
2046 rc = cryptfs_changepw(crypt_ftr.crypt_type, passwd);
2047 if (rc) {
2048 SLOGE("Could not change password on reboot encryption");
2049 return rc;
2050 }
2051 }
2052
2053 if (crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
Paul Lawrence399317e2014-03-10 13:20:50 -07002054 cryptfs_clear_password();
2055 password = strdup(passwd);
2056 struct timespec now;
2057 clock_gettime(CLOCK_BOOTTIME, &now);
2058 password_expiry_time = now.tv_sec + password_max_age_seconds;
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002059 }
2060
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002061 return rc;
2062}
2063
Ken Sumrall3ad90722011-10-04 20:38:29 -07002064int cryptfs_verify_passwd(char *passwd)
2065{
2066 struct crypt_mnt_ftr crypt_ftr;
2067 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002068 unsigned char decrypted_master_key[32];
Ken Sumrall3ad90722011-10-04 20:38:29 -07002069 char encrypted_state[PROPERTY_VALUE_MAX];
2070 int rc;
2071
2072 property_get("ro.crypto.state", encrypted_state, "");
2073 if (strcmp(encrypted_state, "encrypted") ) {
2074 SLOGE("device not encrypted, aborting");
2075 return -2;
2076 }
2077
2078 if (!master_key_saved) {
2079 SLOGE("encrypted fs not yet mounted, aborting");
2080 return -1;
2081 }
2082
2083 if (!saved_mount_point) {
2084 SLOGE("encrypted fs failed to save mount point, aborting");
2085 return -1;
2086 }
2087
Ken Sumrall160b4d62013-04-22 12:15:39 -07002088 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall3ad90722011-10-04 20:38:29 -07002089 SLOGE("Error getting crypt footer and key\n");
2090 return -1;
2091 }
2092
2093 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
2094 /* If the device has no password, then just say the password is valid */
2095 rc = 0;
2096 } else {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002097 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall3ad90722011-10-04 20:38:29 -07002098 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
2099 /* They match, the password is correct */
2100 rc = 0;
2101 } else {
2102 /* If incorrect, sleep for a bit to prevent dictionary attacks */
2103 sleep(1);
2104 rc = 1;
2105 }
2106 }
2107
2108 return rc;
2109}
2110
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002111/* Initialize a crypt_mnt_ftr structure. The keysize is
2112 * defaulted to 16 bytes, and the filesystem size to 0.
2113 * Presumably, at a minimum, the caller will update the
2114 * filesystem size and crypto_type_name after calling this function.
2115 */
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002116static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002117{
Ken Sumrall160b4d62013-04-22 12:15:39 -07002118 off64_t off;
2119
2120 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002121 ftr->magic = CRYPT_MNT_MAGIC;
Kenny Rootc96a5f82013-06-14 12:08:28 -07002122 ftr->major_version = CURRENT_MAJOR_VERSION;
2123 ftr->minor_version = CURRENT_MINOR_VERSION;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002124 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Jason parks70a4b3f2011-01-28 10:10:47 -06002125 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002126
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002127 switch (keymaster_check_compatibility()) {
2128 case 1:
2129 ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
2130 break;
2131
2132 case 0:
2133 ftr->kdf_type = KDF_SCRYPT;
2134 break;
2135
2136 default:
2137 SLOGE("keymaster_check_compatibility failed");
2138 return -1;
2139 }
2140
Kenny Rootc4c70f12013-06-14 12:11:38 -07002141 get_device_scrypt_params(ftr);
2142
Ken Sumrall160b4d62013-04-22 12:15:39 -07002143 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
2144 if (get_crypt_ftr_info(NULL, &off) == 0) {
2145 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
2146 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
2147 ftr->persist_data_size;
2148 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002149
2150 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002151}
2152
Ken Sumrall29d8da82011-05-18 17:20:07 -07002153static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002154{
Ken Sumralle550f782013-08-20 13:48:23 -07002155 const char *args[10];
2156 char size_str[32]; /* Must be large enough to hold a %lld and null byte */
2157 int num_args;
2158 int status;
2159 int tmp;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002160 int rc = -1;
2161
Ken Sumrall29d8da82011-05-18 17:20:07 -07002162 if (type == EXT4_FS) {
Adrien Schildknechte0f409c2016-11-21 15:14:37 -08002163#ifdef TARGET_USES_MKE2FS
2164 args[0] = "/system/bin/mke2fs";
2165 args[1] = "-M";
2166 args[2] = "/data";
2167 args[3] = "-b";
2168 args[4] = "4096";
2169 args[5] = "-t";
2170 args[6] = "ext4";
2171 args[7] = crypto_blkdev;
2172 snprintf(size_str, sizeof(size_str), "%" PRId64, size / (4096 / 512));
2173 args[8] = size_str;
2174 num_args = 9;
2175#else
Ken Sumralle550f782013-08-20 13:48:23 -07002176 args[0] = "/system/bin/make_ext4fs";
2177 args[1] = "-a";
2178 args[2] = "/data";
2179 args[3] = "-l";
Elliott Hughes73737162014-06-25 17:27:42 -07002180 snprintf(size_str, sizeof(size_str), "%" PRId64, size * 512);
Ken Sumralle550f782013-08-20 13:48:23 -07002181 args[4] = size_str;
2182 args[5] = crypto_blkdev;
2183 num_args = 6;
Adrien Schildknechte0f409c2016-11-21 15:14:37 -08002184#endif
Ken Sumralle550f782013-08-20 13:48:23 -07002185 SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n",
2186 args[0], args[1], args[2], args[3], args[4], args[5]);
JP Abgrall62c7af32014-06-16 13:01:23 -07002187 } else if (type == F2FS_FS) {
2188 args[0] = "/system/bin/mkfs.f2fs";
2189 args[1] = "-t";
2190 args[2] = "-d1";
2191 args[3] = crypto_blkdev;
Elliott Hughes73737162014-06-25 17:27:42 -07002192 snprintf(size_str, sizeof(size_str), "%" PRId64, size);
JP Abgrall62c7af32014-06-16 13:01:23 -07002193 args[4] = size_str;
2194 num_args = 5;
2195 SLOGI("Making empty filesystem with command %s %s %s %s %s\n",
2196 args[0], args[1], args[2], args[3], args[4]);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002197 } else {
2198 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
2199 return -1;
2200 }
2201
Ken Sumralle550f782013-08-20 13:48:23 -07002202 tmp = android_fork_execvp(num_args, (char **)args, &status, false, true);
2203
2204 if (tmp != 0) {
2205 SLOGE("Error creating empty filesystem on %s due to logwrap error\n", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002206 } else {
Ken Sumralle550f782013-08-20 13:48:23 -07002207 if (WIFEXITED(status)) {
2208 if (WEXITSTATUS(status)) {
2209 SLOGE("Error creating filesystem on %s, exit status %d ",
2210 crypto_blkdev, WEXITSTATUS(status));
2211 } else {
2212 SLOGD("Successfully created filesystem on %s\n", crypto_blkdev);
2213 rc = 0;
2214 }
2215 } else {
2216 SLOGE("Error creating filesystem on %s, did not exit normally\n", crypto_blkdev);
2217 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002218 }
2219
2220 return rc;
2221}
2222
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002223#define CRYPT_INPLACE_BUFSIZE 4096
Paul Lawrence87999172014-02-20 12:21:31 -08002224#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / CRYPT_SECTOR_SIZE)
2225#define CRYPT_SECTOR_SIZE 512
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002226
2227/* aligned 32K writes tends to make flash happy.
2228 * SD card association recommends it.
2229 */
2230#define BLOCKS_AT_A_TIME 8
2231
2232struct encryptGroupsData
2233{
2234 int realfd;
2235 int cryptofd;
2236 off64_t numblocks;
2237 off64_t one_pct, cur_pct, new_pct;
2238 off64_t blocks_already_done, tot_numblocks;
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002239 off64_t used_blocks_already_done, tot_used_blocks;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002240 char* real_blkdev, * crypto_blkdev;
2241 int count;
2242 off64_t offset;
2243 char* buffer;
Paul Lawrence87999172014-02-20 12:21:31 -08002244 off64_t last_written_sector;
2245 int completed;
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002246 time_t time_started;
2247 int remaining_time;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002248};
2249
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002250static void update_progress(struct encryptGroupsData* data, int is_used)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002251{
2252 data->blocks_already_done++;
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002253
2254 if (is_used) {
2255 data->used_blocks_already_done++;
2256 }
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002257 if (data->tot_used_blocks) {
2258 data->new_pct = data->used_blocks_already_done / data->one_pct;
2259 } else {
2260 data->new_pct = data->blocks_already_done / data->one_pct;
2261 }
2262
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002263 if (data->new_pct > data->cur_pct) {
2264 char buf[8];
2265 data->cur_pct = data->new_pct;
Elliott Hughescb33f572014-06-25 18:25:11 -07002266 snprintf(buf, sizeof(buf), "%" PRId64, data->cur_pct);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002267 property_set("vold.encrypt_progress", buf);
2268 }
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002269
2270 if (data->cur_pct >= 5) {
Paul Lawrence9c58a872014-09-30 09:12:51 -07002271 struct timespec time_now;
2272 if (clock_gettime(CLOCK_MONOTONIC, &time_now)) {
2273 SLOGW("Error getting time");
2274 } else {
2275 double elapsed_time = difftime(time_now.tv_sec, data->time_started);
2276 off64_t remaining_blocks = data->tot_used_blocks
2277 - data->used_blocks_already_done;
2278 int remaining_time = (int)(elapsed_time * remaining_blocks
2279 / data->used_blocks_already_done);
Paul Lawrence71577502014-08-13 14:55:55 -07002280
Paul Lawrence9c58a872014-09-30 09:12:51 -07002281 // Change time only if not yet set, lower, or a lot higher for
2282 // best user experience
2283 if (data->remaining_time == -1
2284 || remaining_time < data->remaining_time
2285 || remaining_time > data->remaining_time + 60) {
2286 char buf[8];
2287 snprintf(buf, sizeof(buf), "%d", remaining_time);
2288 property_set("vold.encrypt_time_remaining", buf);
2289 data->remaining_time = remaining_time;
2290 }
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002291 }
2292 }
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002293}
2294
Paul Lawrence3846be12014-09-22 11:33:54 -07002295static void log_progress(struct encryptGroupsData const* data, bool completed)
2296{
2297 // Precondition - if completed data = 0 else data != 0
2298
2299 // Track progress so we can skip logging blocks
2300 static off64_t offset = -1;
2301
2302 // Need to close existing 'Encrypting from' log?
2303 if (completed || (offset != -1 && data->offset != offset)) {
2304 SLOGI("Encrypted to sector %" PRId64,
2305 offset / info.block_size * CRYPT_SECTOR_SIZE);
2306 offset = -1;
2307 }
2308
2309 // Need to start new 'Encrypting from' log?
2310 if (!completed && offset != data->offset) {
2311 SLOGI("Encrypting from sector %" PRId64,
2312 data->offset / info.block_size * CRYPT_SECTOR_SIZE);
2313 }
2314
2315 // Update offset
2316 if (!completed) {
2317 offset = data->offset + (off64_t)data->count * info.block_size;
2318 }
2319}
2320
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002321static int flush_outstanding_data(struct encryptGroupsData* data)
2322{
2323 if (data->count == 0) {
2324 return 0;
2325 }
2326
Elliott Hughes231bdba2014-06-25 18:36:19 -07002327 SLOGV("Copying %d blocks at offset %" PRIx64, data->count, data->offset);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002328
2329 if (pread64(data->realfd, data->buffer,
2330 info.block_size * data->count, data->offset)
2331 <= 0) {
2332 SLOGE("Error reading real_blkdev %s for inplace encrypt",
2333 data->real_blkdev);
2334 return -1;
2335 }
2336
2337 if (pwrite64(data->cryptofd, data->buffer,
2338 info.block_size * data->count, data->offset)
2339 <= 0) {
2340 SLOGE("Error writing crypto_blkdev %s for inplace encrypt",
2341 data->crypto_blkdev);
2342 return -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002343 } else {
Paul Lawrence3846be12014-09-22 11:33:54 -07002344 log_progress(data, false);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002345 }
2346
2347 data->count = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002348 data->last_written_sector = (data->offset + data->count)
2349 / info.block_size * CRYPT_SECTOR_SIZE - 1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002350 return 0;
2351}
2352
2353static int encrypt_groups(struct encryptGroupsData* data)
2354{
2355 unsigned int i;
2356 u8 *block_bitmap = 0;
2357 unsigned int block;
2358 off64_t ret;
2359 int rc = -1;
2360
2361 data->buffer = malloc(info.block_size * BLOCKS_AT_A_TIME);
2362 if (!data->buffer) {
2363 SLOGE("Failed to allocate crypto buffer");
2364 goto errout;
2365 }
2366
2367 block_bitmap = malloc(info.block_size);
2368 if (!block_bitmap) {
2369 SLOGE("failed to allocate block bitmap");
2370 goto errout;
2371 }
2372
2373 for (i = 0; i < aux_info.groups; ++i) {
2374 SLOGI("Encrypting group %d", i);
2375
2376 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
2377 u32 block_count = min(info.blocks_per_group,
2378 aux_info.len_blocks - first_block);
2379
2380 off64_t offset = (u64)info.block_size
2381 * aux_info.bg_desc[i].bg_block_bitmap;
2382
2383 ret = pread64(data->realfd, block_bitmap, info.block_size, offset);
2384 if (ret != (int)info.block_size) {
2385 SLOGE("failed to read all of block group bitmap %d", i);
2386 goto errout;
2387 }
2388
2389 offset = (u64)info.block_size * first_block;
2390
2391 data->count = 0;
2392
2393 for (block = 0; block < block_count; block++) {
liminghaoaa08e582016-01-06 10:30:49 +08002394 int used = (aux_info.bg_desc[i].bg_flags & EXT4_BG_BLOCK_UNINIT) ?
2395 0 : bitmap_get_bit(block_bitmap, block);
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002396 update_progress(data, used);
2397 if (used) {
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002398 if (data->count == 0) {
2399 data->offset = offset;
2400 }
2401 data->count++;
2402 } else {
2403 if (flush_outstanding_data(data)) {
2404 goto errout;
2405 }
2406 }
2407
2408 offset += info.block_size;
2409
2410 /* Write data if we are aligned or buffer size reached */
2411 if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0
2412 || data->count == BLOCKS_AT_A_TIME) {
2413 if (flush_outstanding_data(data)) {
2414 goto errout;
2415 }
2416 }
Paul Lawrence87999172014-02-20 12:21:31 -08002417
Paul Lawrence73d7a022014-06-09 14:10:09 -07002418 if (!is_battery_ok_to_continue()) {
Paul Lawrence87999172014-02-20 12:21:31 -08002419 SLOGE("Stopping encryption due to low battery");
2420 rc = 0;
2421 goto errout;
2422 }
2423
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002424 }
2425 if (flush_outstanding_data(data)) {
2426 goto errout;
2427 }
2428 }
2429
Paul Lawrence87999172014-02-20 12:21:31 -08002430 data->completed = 1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002431 rc = 0;
2432
2433errout:
Paul Lawrence3846be12014-09-22 11:33:54 -07002434 log_progress(0, true);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002435 free(data->buffer);
2436 free(block_bitmap);
2437 return rc;
2438}
2439
2440static int cryptfs_enable_inplace_ext4(char *crypto_blkdev,
2441 char *real_blkdev,
2442 off64_t size,
2443 off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002444 off64_t tot_size,
2445 off64_t previously_encrypted_upto)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002446{
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002447 u32 i;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002448 struct encryptGroupsData data;
Paul Lawrence74f29f12014-08-28 15:54:10 -07002449 int rc; // Can't initialize without causing warning -Wclobbered
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002450
Paul Lawrence87999172014-02-20 12:21:31 -08002451 if (previously_encrypted_upto > *size_already_done) {
2452 SLOGD("Not fast encrypting since resuming part way through");
2453 return -1;
2454 }
2455
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002456 memset(&data, 0, sizeof(data));
2457 data.real_blkdev = real_blkdev;
2458 data.crypto_blkdev = crypto_blkdev;
2459
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002460 if ( (data.realfd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
JP Abgrall3334c6a2014-10-10 15:52:11 -07002461 SLOGE("Error opening real_blkdev %s for inplace encrypt. err=%d(%s)\n",
2462 real_blkdev, errno, strerror(errno));
Paul Lawrence74f29f12014-08-28 15:54:10 -07002463 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002464 goto errout;
2465 }
2466
David Ng82fd8042015-01-21 13:55:21 -08002467 // Wait until the block device appears. Re-use the mount retry values since it is reasonable.
2468 int retries = RETRY_MOUNT_ATTEMPTS;
2469 while ((data.cryptofd = open(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
2470 if (--retries) {
2471 SLOGE("Error opening crypto_blkdev %s for ext4 inplace encrypt. err=%d(%s), retrying\n",
2472 crypto_blkdev, errno, strerror(errno));
2473 sleep(RETRY_MOUNT_DELAY_SECONDS);
2474 } else {
2475 SLOGE("Error opening crypto_blkdev %s for ext4 inplace encrypt. err=%d(%s)\n",
2476 crypto_blkdev, errno, strerror(errno));
2477 rc = ENABLE_INPLACE_ERR_DEV;
2478 goto errout;
2479 }
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002480 }
2481
2482 if (setjmp(setjmp_env)) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002483 SLOGE("Reading ext4 extent caused an exception\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07002484 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002485 goto errout;
2486 }
2487
2488 if (read_ext(data.realfd, 0) != 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002489 SLOGE("Failed to read ext4 extent\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07002490 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002491 goto errout;
2492 }
2493
2494 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2495 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2496 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
2497
JP Abgrall7fc1de82014-10-10 18:43:41 -07002498 SLOGI("Encrypting ext4 filesystem in place...");
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002499
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002500 data.tot_used_blocks = data.numblocks;
2501 for (i = 0; i < aux_info.groups; ++i) {
2502 data.tot_used_blocks -= aux_info.bg_desc[i].bg_free_blocks_count;
2503 }
2504
2505 data.one_pct = data.tot_used_blocks / 100;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002506 data.cur_pct = 0;
Paul Lawrence9c58a872014-09-30 09:12:51 -07002507
2508 struct timespec time_started = {0};
2509 if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
2510 SLOGW("Error getting time at start");
2511 // Note - continue anyway - we'll run with 0
2512 }
2513 data.time_started = time_started.tv_sec;
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002514 data.remaining_time = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002515
2516 rc = encrypt_groups(&data);
2517 if (rc) {
2518 SLOGE("Error encrypting groups");
2519 goto errout;
2520 }
2521
Paul Lawrence87999172014-02-20 12:21:31 -08002522 *size_already_done += data.completed ? size : data.last_written_sector;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002523 rc = 0;
2524
2525errout:
2526 close(data.realfd);
2527 close(data.cryptofd);
2528
2529 return rc;
2530}
2531
Paul Lawrence3846be12014-09-22 11:33:54 -07002532static void log_progress_f2fs(u64 block, bool completed)
2533{
2534 // Precondition - if completed data = 0 else data != 0
2535
2536 // Track progress so we can skip logging blocks
2537 static u64 last_block = (u64)-1;
2538
2539 // Need to close existing 'Encrypting from' log?
2540 if (completed || (last_block != (u64)-1 && block != last_block + 1)) {
2541 SLOGI("Encrypted to block %" PRId64, last_block);
2542 last_block = -1;
2543 }
2544
2545 // Need to start new 'Encrypting from' log?
2546 if (!completed && (last_block == (u64)-1 || block != last_block + 1)) {
2547 SLOGI("Encrypting from block %" PRId64, block);
2548 }
2549
2550 // Update offset
2551 if (!completed) {
2552 last_block = block;
2553 }
2554}
2555
Daniel Rosenberge82df162014-08-15 22:19:23 +00002556static int encrypt_one_block_f2fs(u64 pos, void *data)
2557{
2558 struct encryptGroupsData *priv_dat = (struct encryptGroupsData *)data;
2559
2560 priv_dat->blocks_already_done = pos - 1;
2561 update_progress(priv_dat, 1);
2562
2563 off64_t offset = pos * CRYPT_INPLACE_BUFSIZE;
2564
2565 if (pread64(priv_dat->realfd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002566 SLOGE("Error reading real_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002567 return -1;
2568 }
2569
2570 if (pwrite64(priv_dat->cryptofd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002571 SLOGE("Error writing crypto_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002572 return -1;
2573 } else {
Paul Lawrence3846be12014-09-22 11:33:54 -07002574 log_progress_f2fs(pos, false);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002575 }
2576
2577 return 0;
2578}
2579
2580static int cryptfs_enable_inplace_f2fs(char *crypto_blkdev,
2581 char *real_blkdev,
2582 off64_t size,
2583 off64_t *size_already_done,
2584 off64_t tot_size,
2585 off64_t previously_encrypted_upto)
2586{
Daniel Rosenberge82df162014-08-15 22:19:23 +00002587 struct encryptGroupsData data;
2588 struct f2fs_info *f2fs_info = NULL;
JP Abgrall7fc1de82014-10-10 18:43:41 -07002589 int rc = ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002590 if (previously_encrypted_upto > *size_already_done) {
2591 SLOGD("Not fast encrypting since resuming part way through");
JP Abgrall7fc1de82014-10-10 18:43:41 -07002592 return ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002593 }
2594 memset(&data, 0, sizeof(data));
2595 data.real_blkdev = real_blkdev;
2596 data.crypto_blkdev = crypto_blkdev;
2597 data.realfd = -1;
2598 data.cryptofd = -1;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002599 if ( (data.realfd = open64(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002600 SLOGE("Error opening real_blkdev %s for f2fs inplace encrypt\n",
Daniel Rosenberge82df162014-08-15 22:19:23 +00002601 real_blkdev);
2602 goto errout;
2603 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002604 if ( (data.cryptofd = open64(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002605 SLOGE("Error opening crypto_blkdev %s for f2fs inplace encrypt. err=%d(%s)\n",
JP Abgrall3334c6a2014-10-10 15:52:11 -07002606 crypto_blkdev, errno, strerror(errno));
JP Abgrall7fc1de82014-10-10 18:43:41 -07002607 rc = ENABLE_INPLACE_ERR_DEV;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002608 goto errout;
2609 }
2610
2611 f2fs_info = generate_f2fs_info(data.realfd);
2612 if (!f2fs_info)
2613 goto errout;
2614
2615 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2616 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2617 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
2618
2619 data.tot_used_blocks = get_num_blocks_used(f2fs_info);
2620
2621 data.one_pct = data.tot_used_blocks / 100;
2622 data.cur_pct = 0;
2623 data.time_started = time(NULL);
2624 data.remaining_time = -1;
2625
2626 data.buffer = malloc(f2fs_info->block_size);
2627 if (!data.buffer) {
2628 SLOGE("Failed to allocate crypto buffer");
2629 goto errout;
2630 }
2631
2632 data.count = 0;
2633
2634 /* Currently, this either runs to completion, or hits a nonrecoverable error */
2635 rc = run_on_used_blocks(data.blocks_already_done, f2fs_info, &encrypt_one_block_f2fs, &data);
2636
2637 if (rc) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002638 SLOGE("Error in running over f2fs blocks");
2639 rc = ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002640 goto errout;
2641 }
2642
2643 *size_already_done += size;
2644 rc = 0;
2645
2646errout:
2647 if (rc)
2648 SLOGE("Failed to encrypt f2fs filesystem on %s", real_blkdev);
2649
Paul Lawrence3846be12014-09-22 11:33:54 -07002650 log_progress_f2fs(0, true);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002651 free(f2fs_info);
2652 free(data.buffer);
2653 close(data.realfd);
2654 close(data.cryptofd);
2655
2656 return rc;
2657}
2658
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002659static int cryptfs_enable_inplace_full(char *crypto_blkdev, char *real_blkdev,
2660 off64_t size, off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002661 off64_t tot_size,
2662 off64_t previously_encrypted_upto)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002663{
2664 int realfd, cryptofd;
2665 char *buf[CRYPT_INPLACE_BUFSIZE];
JP Abgrall7fc1de82014-10-10 18:43:41 -07002666 int rc = ENABLE_INPLACE_ERR_OTHER;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002667 off64_t numblocks, i, remainder;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002668 off64_t one_pct, cur_pct, new_pct;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002669 off64_t blocks_already_done, tot_numblocks;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002670
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002671 if ( (realfd = open(real_blkdev, O_RDONLY|O_CLOEXEC)) < 0) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002672 SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002673 return ENABLE_INPLACE_ERR_OTHER;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002674 }
2675
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002676 if ( (cryptofd = open(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
JP Abgrall3334c6a2014-10-10 15:52:11 -07002677 SLOGE("Error opening crypto_blkdev %s for inplace encrypt. err=%d(%s)\n",
2678 crypto_blkdev, errno, strerror(errno));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002679 close(realfd);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002680 return ENABLE_INPLACE_ERR_DEV;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002681 }
2682
2683 /* This is pretty much a simple loop of reading 4K, and writing 4K.
2684 * The size passed in is the number of 512 byte sectors in the filesystem.
2685 * So compute the number of whole 4K blocks we should read/write,
2686 * and the remainder.
2687 */
2688 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2689 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002690 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2691 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002692
2693 SLOGE("Encrypting filesystem in place...");
2694
Paul Lawrence87999172014-02-20 12:21:31 -08002695 i = previously_encrypted_upto + 1 - *size_already_done;
2696
2697 if (lseek64(realfd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
2698 SLOGE("Cannot seek to previously encrypted point on %s", real_blkdev);
2699 goto errout;
2700 }
2701
2702 if (lseek64(cryptofd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
2703 SLOGE("Cannot seek to previously encrypted point on %s", crypto_blkdev);
2704 goto errout;
2705 }
2706
2707 for (;i < size && i % CRYPT_SECTORS_PER_BUFSIZE != 0; ++i) {
2708 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2709 SLOGE("Error reading initial sectors from real_blkdev %s for "
2710 "inplace encrypt\n", crypto_blkdev);
2711 goto errout;
2712 }
2713 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2714 SLOGE("Error writing initial sectors to crypto_blkdev %s for "
2715 "inplace encrypt\n", crypto_blkdev);
2716 goto errout;
2717 } else {
Elliott Hughescb33f572014-06-25 18:25:11 -07002718 SLOGI("Encrypted 1 block at %" PRId64, i);
Paul Lawrence87999172014-02-20 12:21:31 -08002719 }
2720 }
2721
Ken Sumrall29d8da82011-05-18 17:20:07 -07002722 one_pct = tot_numblocks / 100;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002723 cur_pct = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002724 /* process the majority of the filesystem in blocks */
Paul Lawrence87999172014-02-20 12:21:31 -08002725 for (i/=CRYPT_SECTORS_PER_BUFSIZE; i<numblocks; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07002726 new_pct = (i + blocks_already_done) / one_pct;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002727 if (new_pct > cur_pct) {
2728 char buf[8];
2729
2730 cur_pct = new_pct;
Elliott Hughes73737162014-06-25 17:27:42 -07002731 snprintf(buf, sizeof(buf), "%" PRId64, cur_pct);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002732 property_set("vold.encrypt_progress", buf);
2733 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002734 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Lawrence87999172014-02-20 12:21:31 -08002735 SLOGE("Error reading real_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002736 goto errout;
2737 }
2738 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Lawrence87999172014-02-20 12:21:31 -08002739 SLOGE("Error writing crypto_blkdev %s for inplace encrypt", crypto_blkdev);
2740 goto errout;
2741 } else {
Elliott Hughescb33f572014-06-25 18:25:11 -07002742 SLOGD("Encrypted %d block at %" PRId64,
Paul Lawrence87999172014-02-20 12:21:31 -08002743 CRYPT_SECTORS_PER_BUFSIZE,
2744 i * CRYPT_SECTORS_PER_BUFSIZE);
2745 }
2746
Paul Lawrence73d7a022014-06-09 14:10:09 -07002747 if (!is_battery_ok_to_continue()) {
Paul Lawrence87999172014-02-20 12:21:31 -08002748 SLOGE("Stopping encryption due to low battery");
2749 *size_already_done += (i + 1) * CRYPT_SECTORS_PER_BUFSIZE - 1;
2750 rc = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002751 goto errout;
2752 }
2753 }
2754
2755 /* Do any remaining sectors */
2756 for (i=0; i<remainder; i++) {
Paul Lawrence87999172014-02-20 12:21:31 -08002757 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2758 SLOGE("Error reading final sectors from real_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002759 goto errout;
2760 }
Paul Lawrence87999172014-02-20 12:21:31 -08002761 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2762 SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002763 goto errout;
Paul Lawrence87999172014-02-20 12:21:31 -08002764 } else {
2765 SLOGI("Encrypted 1 block at next location");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002766 }
2767 }
2768
Ken Sumrall29d8da82011-05-18 17:20:07 -07002769 *size_already_done += size;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002770 rc = 0;
2771
2772errout:
2773 close(realfd);
2774 close(cryptofd);
2775
2776 return rc;
2777}
2778
JP Abgrall7fc1de82014-10-10 18:43:41 -07002779/* returns on of the ENABLE_INPLACE_* return codes */
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002780static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev,
2781 off64_t size, off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002782 off64_t tot_size,
2783 off64_t previously_encrypted_upto)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002784{
JP Abgrall7fc1de82014-10-10 18:43:41 -07002785 int rc_ext4, rc_f2fs, rc_full;
Paul Lawrence87999172014-02-20 12:21:31 -08002786 if (previously_encrypted_upto) {
Elliott Hughescb33f572014-06-25 18:25:11 -07002787 SLOGD("Continuing encryption from %" PRId64, previously_encrypted_upto);
Paul Lawrence87999172014-02-20 12:21:31 -08002788 }
2789
2790 if (*size_already_done + size < previously_encrypted_upto) {
2791 *size_already_done += size;
2792 return 0;
2793 }
2794
Daniel Rosenberge82df162014-08-15 22:19:23 +00002795 /* TODO: identify filesystem type.
2796 * As is, cryptfs_enable_inplace_ext4 will fail on an f2fs partition, and
2797 * then we will drop down to cryptfs_enable_inplace_f2fs.
2798 * */
JP Abgrall7fc1de82014-10-10 18:43:41 -07002799 if ((rc_ext4 = cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev,
Daniel Rosenberge82df162014-08-15 22:19:23 +00002800 size, size_already_done,
JP Abgrall7fc1de82014-10-10 18:43:41 -07002801 tot_size, previously_encrypted_upto)) == 0) {
Daniel Rosenberge82df162014-08-15 22:19:23 +00002802 return 0;
2803 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002804 SLOGD("cryptfs_enable_inplace_ext4()=%d\n", rc_ext4);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002805
JP Abgrall7fc1de82014-10-10 18:43:41 -07002806 if ((rc_f2fs = cryptfs_enable_inplace_f2fs(crypto_blkdev, real_blkdev,
Daniel Rosenberge82df162014-08-15 22:19:23 +00002807 size, size_already_done,
JP Abgrall7fc1de82014-10-10 18:43:41 -07002808 tot_size, previously_encrypted_upto)) == 0) {
Daniel Rosenberge82df162014-08-15 22:19:23 +00002809 return 0;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002810 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002811 SLOGD("cryptfs_enable_inplace_f2fs()=%d\n", rc_f2fs);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002812
JP Abgrall7fc1de82014-10-10 18:43:41 -07002813 rc_full = cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev,
Paul Lawrence87999172014-02-20 12:21:31 -08002814 size, size_already_done, tot_size,
2815 previously_encrypted_upto);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002816 SLOGD("cryptfs_enable_inplace_full()=%d\n", rc_full);
2817
2818 /* Hack for b/17898962, the following is the symptom... */
2819 if (rc_ext4 == ENABLE_INPLACE_ERR_DEV
2820 && rc_f2fs == ENABLE_INPLACE_ERR_DEV
2821 && rc_full == ENABLE_INPLACE_ERR_DEV) {
2822 return ENABLE_INPLACE_ERR_DEV;
2823 }
2824 return rc_full;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002825}
2826
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002827#define CRYPTO_ENABLE_WIPE 1
2828#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002829
2830#define FRAMEWORK_BOOT_WAIT 60
2831
Paul Lawrence87999172014-02-20 12:21:31 -08002832static int cryptfs_SHA256_fileblock(const char* filename, __le8* buf)
2833{
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002834 int fd = open(filename, O_RDONLY|O_CLOEXEC);
Paul Lawrence87999172014-02-20 12:21:31 -08002835 if (fd == -1) {
2836 SLOGE("Error opening file %s", filename);
2837 return -1;
2838 }
2839
2840 char block[CRYPT_INPLACE_BUFSIZE];
2841 memset(block, 0, sizeof(block));
2842 if (unix_read(fd, block, sizeof(block)) < 0) {
2843 SLOGE("Error reading file %s", filename);
2844 close(fd);
2845 return -1;
2846 }
2847
2848 close(fd);
2849
2850 SHA256_CTX c;
2851 SHA256_Init(&c);
2852 SHA256_Update(&c, block, sizeof(block));
2853 SHA256_Final(buf, &c);
2854
2855 return 0;
2856}
2857
JP Abgrall62c7af32014-06-16 13:01:23 -07002858static int get_fs_type(struct fstab_rec *rec)
2859{
2860 if (!strcmp(rec->fs_type, "ext4")) {
2861 return EXT4_FS;
2862 } else if (!strcmp(rec->fs_type, "f2fs")) {
2863 return F2FS_FS;
2864 } else {
2865 return -1;
2866 }
2867}
2868
Paul Lawrence87999172014-02-20 12:21:31 -08002869static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr *crypt_ftr, int how,
2870 char *crypto_blkdev, char *real_blkdev,
2871 int previously_encrypted_upto)
2872{
2873 off64_t cur_encryption_done=0, tot_encryption_size=0;
Tim Murray8439dc92014-12-15 11:56:11 -08002874 int rc = -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002875
Paul Lawrence73d7a022014-06-09 14:10:09 -07002876 if (!is_battery_ok_to_start()) {
2877 SLOGW("Not starting encryption due to low battery");
Paul Lawrence87999172014-02-20 12:21:31 -08002878 return 0;
2879 }
2880
2881 /* The size of the userdata partition, and add in the vold volumes below */
2882 tot_encryption_size = crypt_ftr->fs_size;
2883
2884 if (how == CRYPTO_ENABLE_WIPE) {
JP Abgrall62c7af32014-06-16 13:01:23 -07002885 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
2886 int fs_type = get_fs_type(rec);
2887 if (fs_type < 0) {
2888 SLOGE("cryptfs_enable: unsupported fs type %s\n", rec->fs_type);
2889 return -1;
2890 }
2891 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr->fs_size, fs_type);
Paul Lawrence87999172014-02-20 12:21:31 -08002892 } else if (how == CRYPTO_ENABLE_INPLACE) {
2893 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev,
2894 crypt_ftr->fs_size, &cur_encryption_done,
2895 tot_encryption_size,
2896 previously_encrypted_upto);
2897
JP Abgrall7fc1de82014-10-10 18:43:41 -07002898 if (rc == ENABLE_INPLACE_ERR_DEV) {
2899 /* Hack for b/17898962 */
2900 SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n");
2901 cryptfs_reboot(reboot);
2902 }
2903
Paul Lawrence73d7a022014-06-09 14:10:09 -07002904 if (!rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002905 crypt_ftr->encrypted_upto = cur_encryption_done;
2906 }
2907
Paul Lawrence73d7a022014-06-09 14:10:09 -07002908 if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002909 /* The inplace routine never actually sets the progress to 100% due
2910 * to the round down nature of integer division, so set it here */
2911 property_set("vold.encrypt_progress", "100");
2912 }
2913 } else {
2914 /* Shouldn't happen */
2915 SLOGE("cryptfs_enable: internal error, unknown option\n");
2916 rc = -1;
2917 }
2918
2919 return rc;
2920}
2921
Paul Lawrence13486032014-02-03 13:28:11 -08002922int cryptfs_enable_internal(char *howarg, int crypt_type, char *passwd,
Paul Lawrence569649f2015-09-09 12:13:00 -07002923 int no_ui)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002924{
2925 int how = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002926 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN];
Ken Sumrall160b4d62013-04-22 12:15:39 -07002927 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002928 int rc=-1, i;
Paul Lawrence87999172014-02-20 12:21:31 -08002929 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002930 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002931 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002932 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07002933 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall29d8da82011-05-18 17:20:07 -07002934 int num_vols;
Paul Lawrence87999172014-02-20 12:21:31 -08002935 off64_t previously_encrypted_upto = 0;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002936 bool rebootEncryption = false;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002937
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002938 if (!strcmp(howarg, "wipe")) {
2939 how = CRYPTO_ENABLE_WIPE;
2940 } else if (! strcmp(howarg, "inplace")) {
2941 how = CRYPTO_ENABLE_INPLACE;
2942 } else {
2943 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08002944 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002945 }
2946
Paul Lawrence87999172014-02-20 12:21:31 -08002947 if (how == CRYPTO_ENABLE_INPLACE
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002948 && get_crypt_ftr_and_key(&crypt_ftr) == 0) {
2949 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
2950 /* An encryption was underway and was interrupted */
2951 previously_encrypted_upto = crypt_ftr.encrypted_upto;
2952 crypt_ftr.encrypted_upto = 0;
2953 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002954
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002955 /* At this point, we are in an inconsistent state. Until we successfully
2956 complete encryption, a reboot will leave us broken. So mark the
2957 encryption failed in case that happens.
2958 On successfully completing encryption, remove this flag */
2959 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002960
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002961 put_crypt_ftr_and_key(&crypt_ftr);
2962 } else if (crypt_ftr.flags & CRYPT_FORCE_ENCRYPTION) {
2963 if (!check_ftr_sha(&crypt_ftr)) {
2964 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
2965 put_crypt_ftr_and_key(&crypt_ftr);
2966 goto error_unencrypted;
2967 }
2968
2969 /* Doing a reboot-encryption*/
2970 crypt_ftr.flags &= ~CRYPT_FORCE_ENCRYPTION;
2971 crypt_ftr.flags |= CRYPT_FORCE_COMPLETE;
2972 rebootEncryption = true;
2973 }
Paul Lawrence87999172014-02-20 12:21:31 -08002974 }
2975
2976 property_get("ro.crypto.state", encrypted_state, "");
2977 if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) {
2978 SLOGE("Device is already running encrypted, aborting");
2979 goto error_unencrypted;
2980 }
2981
2982 // TODO refactor fs_mgr_get_crypt_info to get both in one call
2983 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumrall56ad03c2013-02-13 13:00:19 -08002984 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002985
Ken Sumrall3ed82362011-01-28 23:31:16 -08002986 /* Get the size of the real block device */
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002987 int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002988 if (fd == -1) {
2989 SLOGE("Cannot open block device %s\n", real_blkdev);
2990 goto error_unencrypted;
2991 }
2992 unsigned long nr_sec;
2993 get_blkdev_size(fd, &nr_sec);
2994 if (nr_sec == 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002995 SLOGE("Cannot get size of block device %s\n", real_blkdev);
2996 goto error_unencrypted;
2997 }
2998 close(fd);
2999
3000 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07003001 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08003002 unsigned int fs_size_sec, max_fs_size_sec;
Jim Millera70abc62014-08-15 02:00:45 +00003003 fs_size_sec = get_fs_size(real_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00003004 if (fs_size_sec == 0)
3005 fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev);
3006
Paul Lawrence87999172014-02-20 12:21:31 -08003007 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
Ken Sumrall3ed82362011-01-28 23:31:16 -08003008
3009 if (fs_size_sec > max_fs_size_sec) {
3010 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
3011 goto error_unencrypted;
3012 }
3013 }
3014
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003015 /* Get a wakelock as this may take a while, and we don't want the
3016 * device to sleep on us. We'll grab a partial wakelock, and if the UI
3017 * wants to keep the screen on, it can grab a full wakelock.
3018 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07003019 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003020 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
3021
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003022 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003023 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003024 */
3025 property_set("vold.decrypt", "trigger_shutdown_framework");
3026 SLOGD("Just asked init to shut down class main\n");
3027
Jeff Sharkey9c484982015-03-31 10:35:33 -07003028 /* Ask vold to unmount all devices that it manages */
3029 if (vold_unmountAll()) {
3030 SLOGE("Failed to unmount all vold managed devices");
Ken Sumrall2eaf7132011-01-14 12:45:48 -08003031 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003032
Paul Lawrence3d99eba2015-11-20 07:07:19 -08003033 /* no_ui means we are being called from init, not settings.
3034 Now we always reboot from settings, so !no_ui means reboot
3035 */
3036 bool onlyCreateHeader = false;
3037 if (!no_ui) {
3038 /* Try fallback, which is to reboot and try there */
3039 onlyCreateHeader = true;
3040 FILE* breadcrumb = fopen(BREADCRUMB_FILE, "we");
3041 if (breadcrumb == 0) {
3042 SLOGE("Failed to create breadcrumb file");
3043 goto error_shutting_down;
3044 }
3045 fclose(breadcrumb);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003046 }
3047
3048 /* Do extra work for a better UX when doing the long inplace encryption */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08003049 if (how == CRYPTO_ENABLE_INPLACE && !onlyCreateHeader) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003050 /* Now that /data is unmounted, we need to mount a tmpfs
3051 * /data, set a property saying we're doing inplace encryption,
3052 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003053 */
Ken Sumralle5032c42012-04-01 23:58:44 -07003054 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08003055 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003056 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003057 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08003058 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003059
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003060 /* restart the framework. */
3061 /* Create necessary paths on /data */
3062 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08003063 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003064 }
3065
Ken Sumrall92736ef2012-10-17 20:57:14 -07003066 /* Ugh, shutting down the framework is not synchronous, so until it
3067 * can be fixed, this horrible hack will wait a moment for it all to
3068 * shut down before proceeding. Without it, some devices cannot
3069 * restart the graphics services.
3070 */
3071 sleep(2);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003072 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003073
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003074 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003075 /* Initialize a crypt_mnt_ftr for the partition */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08003076 if (previously_encrypted_upto == 0 && !rebootEncryption) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07003077 if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) {
3078 goto error_shutting_down;
3079 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003080
Paul Lawrence87999172014-02-20 12:21:31 -08003081 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
3082 crypt_ftr.fs_size = nr_sec
3083 - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
3084 } else {
3085 crypt_ftr.fs_size = nr_sec;
3086 }
Paul Lawrence6bfed202014-07-28 12:47:22 -07003087 /* At this point, we are in an inconsistent state. Until we successfully
3088 complete encryption, a reboot will leave us broken. So mark the
3089 encryption failed in case that happens.
3090 On successfully completing encryption, remove this flag */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08003091 if (onlyCreateHeader) {
3092 crypt_ftr.flags |= CRYPT_FORCE_ENCRYPTION;
3093 } else {
3094 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
3095 }
Paul Lawrence87999172014-02-20 12:21:31 -08003096 crypt_ftr.crypt_type = crypt_type;
Ajay Dudani87701e22014-09-17 21:02:52 -07003097 strlcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256", MAX_CRYPTO_TYPE_NAME_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003098
Paul Lawrence87999172014-02-20 12:21:31 -08003099 /* Make an encrypted master key */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08003100 if (create_encrypted_random_key(onlyCreateHeader ? DEFAULT_PASSWORD : passwd,
3101 crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
Paul Lawrence87999172014-02-20 12:21:31 -08003102 SLOGE("Cannot create encrypted master key\n");
3103 goto error_shutting_down;
3104 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003105
Paul Lawrence3d99eba2015-11-20 07:07:19 -08003106 /* Replace scrypted intermediate key if we are preparing for a reboot */
3107 if (onlyCreateHeader) {
3108 unsigned char fake_master_key[KEY_LEN_BYTES];
3109 unsigned char encrypted_fake_master_key[KEY_LEN_BYTES];
3110 memset(fake_master_key, 0, sizeof(fake_master_key));
3111 encrypt_master_key(passwd, crypt_ftr.salt, fake_master_key,
3112 encrypted_fake_master_key, &crypt_ftr);
3113 }
3114
Paul Lawrence87999172014-02-20 12:21:31 -08003115 /* Write the key to the end of the partition */
3116 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003117
Paul Lawrence87999172014-02-20 12:21:31 -08003118 /* If any persistent data has been remembered, save it.
3119 * If none, create a valid empty table and save that.
3120 */
3121 if (!persist_data) {
3122 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
3123 if (pdata) {
3124 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
3125 persist_data = pdata;
3126 }
3127 }
3128 if (persist_data) {
3129 save_persistent_data();
3130 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003131 }
3132
Paul Lawrence3d99eba2015-11-20 07:07:19 -08003133 if (onlyCreateHeader) {
3134 sleep(2);
3135 cryptfs_reboot(reboot);
3136 }
3137
3138 if (how == CRYPTO_ENABLE_INPLACE && (!no_ui || rebootEncryption)) {
Ajay Dudani87701e22014-09-17 21:02:52 -07003139 /* startup service classes main and late_start */
3140 property_set("vold.decrypt", "trigger_restart_min_framework");
3141 SLOGD("Just triggered restart_min_framework\n");
3142
3143 /* OK, the framework is restarted and will soon be showing a
3144 * progress bar. Time to setup an encrypted mapping, and
3145 * either write a new filesystem, or encrypt in place updating
3146 * the progress bar as we work.
3147 */
3148 }
3149
Paul Lawrenced0c7b172014-08-08 14:28:10 -07003150 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07003151 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
Paul Lawrence3d99eba2015-11-20 07:07:19 -08003152 CRYPTO_BLOCK_DEVICE);
Ken Sumrall29d8da82011-05-18 17:20:07 -07003153
Paul Lawrence87999172014-02-20 12:21:31 -08003154 /* If we are continuing, check checksums match */
3155 rc = 0;
3156 if (previously_encrypted_upto) {
3157 __le8 hash_first_block[SHA256_DIGEST_LENGTH];
3158 rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block);
Ken Sumrall128626f2011-06-28 18:45:14 -07003159
Paul Lawrence87999172014-02-20 12:21:31 -08003160 if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block,
3161 sizeof(hash_first_block)) != 0) {
3162 SLOGE("Checksums do not match - trigger wipe");
3163 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07003164 }
3165 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003166
Paul Lawrence87999172014-02-20 12:21:31 -08003167 if (!rc) {
3168 rc = cryptfs_enable_all_volumes(&crypt_ftr, how,
3169 crypto_blkdev, real_blkdev,
3170 previously_encrypted_upto);
3171 }
3172
3173 /* Calculate checksum if we are not finished */
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08003174 if (!rc && how == CRYPTO_ENABLE_INPLACE
3175 && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08003176 rc = cryptfs_SHA256_fileblock(crypto_blkdev,
3177 crypt_ftr.hash_first_block);
Paul Lawrence73d7a022014-06-09 14:10:09 -07003178 if (rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08003179 SLOGE("Error calculating checksum for continuing encryption");
3180 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07003181 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003182 }
3183
3184 /* Undo the dm-crypt mapping whether we succeed or not */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08003185 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
Ken Sumrall29d8da82011-05-18 17:20:07 -07003186
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003187 if (! rc) {
3188 /* Success */
Paul Lawrence6bfed202014-07-28 12:47:22 -07003189 crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08003190
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08003191 if (how == CRYPTO_ENABLE_INPLACE
3192 && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08003193 SLOGD("Encrypted up to sector %lld - will continue after reboot",
3194 crypt_ftr.encrypted_upto);
Paul Lawrence6bfed202014-07-28 12:47:22 -07003195 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence87999172014-02-20 12:21:31 -08003196 }
Paul Lawrence73d7a022014-06-09 14:10:09 -07003197
Paul Lawrence6bfed202014-07-28 12:47:22 -07003198 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08003199
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08003200 if (how == CRYPTO_ENABLE_WIPE
3201 || crypt_ftr.encrypted_upto == crypt_ftr.fs_size) {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003202 char value[PROPERTY_VALUE_MAX];
3203 property_get("ro.crypto.state", value, "");
3204 if (!strcmp(value, "")) {
3205 /* default encryption - continue first boot sequence */
3206 property_set("ro.crypto.state", "encrypted");
Paul Lawrence4ed45262016-03-10 15:44:21 -08003207 property_set("ro.crypto.type", "block");
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003208 release_wake_lock(lockid);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08003209 if (rebootEncryption && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
3210 // Bring up cryptkeeper that will check the password and set it
3211 property_set("vold.decrypt", "trigger_shutdown_framework");
3212 sleep(2);
3213 property_set("vold.encrypt_progress", "");
3214 cryptfs_trigger_restart_min_framework();
3215 } else {
3216 cryptfs_check_passwd(DEFAULT_PASSWORD);
3217 cryptfs_restart_internal(1);
3218 }
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003219 return 0;
3220 } else {
3221 sleep(2); /* Give the UI a chance to show 100% progress */
Paul Lawrence87999172014-02-20 12:21:31 -08003222 cryptfs_reboot(reboot);
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003223 }
Paul Lawrence87999172014-02-20 12:21:31 -08003224 } else {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003225 sleep(2); /* Partially encrypted, ensure writes flushed to ssd */
Paul Lawrence87999172014-02-20 12:21:31 -08003226 cryptfs_reboot(shutdown);
3227 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003228 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003229 char value[PROPERTY_VALUE_MAX];
3230
Ken Sumrall319369a2012-06-27 16:30:18 -07003231 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003232 if (!strcmp(value, "1")) {
3233 /* wipe data if encryption failed */
3234 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
Yabin Cuiffa2e092016-04-06 17:21:38 -07003235 if (!write_bootloader_message("--wipe_data\n--reason=cryptfs_enable_internal\n")) {
3236 SLOGE("could not write bootloader message\n");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003237 }
Paul Lawrence87999172014-02-20 12:21:31 -08003238 cryptfs_reboot(recovery);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003239 } else {
3240 /* set property to trigger dialog */
3241 property_set("vold.encrypt_progress", "error_partially_encrypted");
3242 release_wake_lock(lockid);
3243 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003244 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003245 }
3246
Ken Sumrall3ed82362011-01-28 23:31:16 -08003247 /* hrm, the encrypt step claims success, but the reboot failed.
3248 * This should not happen.
3249 * Set the property and return. Hope the framework can deal with it.
3250 */
3251 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003252 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003253 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08003254
3255error_unencrypted:
3256 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003257 if (lockid[0]) {
3258 release_wake_lock(lockid);
3259 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003260 return -1;
3261
3262error_shutting_down:
3263 /* we failed, and have not encrypted anthing, so the users's data is still intact,
3264 * but the framework is stopped and not restarted to show the error, so it's up to
3265 * vold to restart the system.
3266 */
3267 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Paul Lawrence87999172014-02-20 12:21:31 -08003268 cryptfs_reboot(reboot);
Ken Sumrall3ed82362011-01-28 23:31:16 -08003269
3270 /* shouldn't get here */
3271 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003272 if (lockid[0]) {
3273 release_wake_lock(lockid);
3274 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003275 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003276}
3277
Paul Lawrence569649f2015-09-09 12:13:00 -07003278int cryptfs_enable(char *howarg, int type, char *passwd, int no_ui)
Paul Lawrence13486032014-02-03 13:28:11 -08003279{
Paul Lawrence569649f2015-09-09 12:13:00 -07003280 return cryptfs_enable_internal(howarg, type, passwd, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08003281}
3282
Paul Lawrence569649f2015-09-09 12:13:00 -07003283int cryptfs_enable_default(char *howarg, int no_ui)
Paul Lawrence13486032014-02-03 13:28:11 -08003284{
3285 return cryptfs_enable_internal(howarg, CRYPT_TYPE_DEFAULT,
Paul Lawrence569649f2015-09-09 12:13:00 -07003286 DEFAULT_PASSWORD, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08003287}
3288
3289int cryptfs_changepw(int crypt_type, const char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003290{
Paul Crowley38132a12016-02-09 09:50:32 +00003291 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08003292 SLOGE("cryptfs_changepw not valid for file encryption");
3293 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08003294 }
3295
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003296 struct crypt_mnt_ftr crypt_ftr;
JP Abgrall933216c2015-02-11 13:44:32 -08003297 int rc;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003298
3299 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08003300 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08003301 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003302 return -1;
3303 }
3304
Paul Lawrencef4faa572014-01-29 13:31:03 -08003305 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
3306 SLOGE("Invalid crypt_type %d", crypt_type);
3307 return -1;
3308 }
3309
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003310 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07003311 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08003312 SLOGE("Error getting crypt footer and key");
3313 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003314 }
3315
Paul Lawrencef4faa572014-01-29 13:31:03 -08003316 crypt_ftr.crypt_type = crypt_type;
3317
JP Abgrall933216c2015-02-11 13:44:32 -08003318 rc = encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
Paul Lawrencef4faa572014-01-29 13:31:03 -08003319 : newpw,
3320 crypt_ftr.salt,
3321 saved_master_key,
3322 crypt_ftr.master_key,
3323 &crypt_ftr);
JP Abgrall933216c2015-02-11 13:44:32 -08003324 if (rc) {
3325 SLOGE("Encrypt master key failed: %d", rc);
3326 return -1;
3327 }
Jason parks70a4b3f2011-01-28 10:10:47 -06003328 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07003329 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003330
3331 return 0;
3332}
Ken Sumrall160b4d62013-04-22 12:15:39 -07003333
Rubin Xu85c01f92014-10-13 12:49:54 +01003334static unsigned int persist_get_max_entries(int encrypted) {
3335 struct crypt_mnt_ftr crypt_ftr;
3336 unsigned int dsize;
3337 unsigned int max_persistent_entries;
3338
3339 /* If encrypted, use the values from the crypt_ftr, otherwise
3340 * use the values for the current spec.
3341 */
3342 if (encrypted) {
3343 if (get_crypt_ftr_and_key(&crypt_ftr)) {
3344 return -1;
3345 }
3346 dsize = crypt_ftr.persist_data_size;
3347 } else {
3348 dsize = CRYPT_PERSIST_DATA_SIZE;
3349 }
3350
3351 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
3352 sizeof(struct crypt_persist_entry);
3353
3354 return max_persistent_entries;
3355}
3356
3357static int persist_get_key(const char *fieldname, char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003358{
3359 unsigned int i;
3360
3361 if (persist_data == NULL) {
3362 return -1;
3363 }
3364 for (i = 0; i < persist_data->persist_valid_entries; i++) {
3365 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
3366 /* We found it! */
3367 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
3368 return 0;
3369 }
3370 }
3371
3372 return -1;
3373}
3374
Rubin Xu85c01f92014-10-13 12:49:54 +01003375static int persist_set_key(const char *fieldname, const char *value, int encrypted)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003376{
3377 unsigned int i;
3378 unsigned int num;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003379 unsigned int max_persistent_entries;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003380
3381 if (persist_data == NULL) {
3382 return -1;
3383 }
3384
Rubin Xu85c01f92014-10-13 12:49:54 +01003385 max_persistent_entries = persist_get_max_entries(encrypted);
Ken Sumrall160b4d62013-04-22 12:15:39 -07003386
3387 num = persist_data->persist_valid_entries;
3388
3389 for (i = 0; i < num; i++) {
3390 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
3391 /* We found an existing entry, update it! */
3392 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
3393 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
3394 return 0;
3395 }
3396 }
3397
3398 /* We didn't find it, add it to the end, if there is room */
3399 if (persist_data->persist_valid_entries < max_persistent_entries) {
3400 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
3401 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
3402 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
3403 persist_data->persist_valid_entries++;
3404 return 0;
3405 }
3406
3407 return -1;
3408}
3409
Rubin Xu85c01f92014-10-13 12:49:54 +01003410/**
3411 * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the
3412 * sequence and its index is greater than or equal to index. Return 0 otherwise.
3413 */
3414static int match_multi_entry(const char *key, const char *field, unsigned index) {
Rubin Xu85c01f92014-10-13 12:49:54 +01003415 unsigned int field_len;
3416 unsigned int key_index;
3417 field_len = strlen(field);
3418
3419 if (index == 0) {
3420 // The first key in a multi-entry field is just the filedname itself.
3421 if (!strcmp(key, field)) {
3422 return 1;
3423 }
3424 }
3425 // Match key against "%s_%d" % (field, index)
3426 if (strlen(key) < field_len + 1 + 1) {
3427 // Need at least a '_' and a digit.
3428 return 0;
3429 }
3430 if (strncmp(key, field, field_len)) {
3431 // If the key does not begin with field, it's not a match.
3432 return 0;
3433 }
3434 if (1 != sscanf(&key[field_len],"_%d", &key_index)) {
3435 return 0;
3436 }
3437 return key_index >= index;
3438}
3439
3440/*
3441 * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all
3442 * remaining entries starting from index will be deleted.
3443 * returns PERSIST_DEL_KEY_OK if deletion succeeds,
3444 * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist,
3445 * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs.
3446 *
3447 */
3448static int persist_del_keys(const char *fieldname, unsigned index)
3449{
3450 unsigned int i;
3451 unsigned int j;
3452 unsigned int num;
3453
3454 if (persist_data == NULL) {
3455 return PERSIST_DEL_KEY_ERROR_OTHER;
3456 }
3457
3458 num = persist_data->persist_valid_entries;
3459
3460 j = 0; // points to the end of non-deleted entries.
3461 // Filter out to-be-deleted entries in place.
3462 for (i = 0; i < num; i++) {
3463 if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) {
3464 persist_data->persist_entry[j] = persist_data->persist_entry[i];
3465 j++;
3466 }
3467 }
3468
3469 if (j < num) {
3470 persist_data->persist_valid_entries = j;
3471 // Zeroise the remaining entries
3472 memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry));
3473 return PERSIST_DEL_KEY_OK;
3474 } else {
3475 // Did not find an entry matching the given fieldname
3476 return PERSIST_DEL_KEY_ERROR_NO_FIELD;
3477 }
3478}
3479
3480static int persist_count_keys(const char *fieldname)
3481{
3482 unsigned int i;
3483 unsigned int count;
3484
3485 if (persist_data == NULL) {
3486 return -1;
3487 }
3488
3489 count = 0;
3490 for (i = 0; i < persist_data->persist_valid_entries; i++) {
3491 if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) {
3492 count++;
3493 }
3494 }
3495
3496 return count;
3497}
3498
Ken Sumrall160b4d62013-04-22 12:15:39 -07003499/* Return the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01003500int cryptfs_getfield(const char *fieldname, char *value, int len)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003501{
Paul Crowley38132a12016-02-09 09:50:32 +00003502 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08003503 SLOGE("Cannot get field when file encrypted");
3504 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07003505 }
3506
Ken Sumrall160b4d62013-04-22 12:15:39 -07003507 char temp_value[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01003508 /* CRYPTO_GETFIELD_OK is success,
3509 * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set,
3510 * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small,
3511 * CRYPTO_GETFIELD_ERROR_OTHER is any other error
Ken Sumrall160b4d62013-04-22 12:15:39 -07003512 */
Rubin Xu85c01f92014-10-13 12:49:54 +01003513 int rc = CRYPTO_GETFIELD_ERROR_OTHER;
3514 int i;
3515 char temp_field[PROPERTY_KEY_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -07003516
3517 if (persist_data == NULL) {
3518 load_persistent_data();
3519 if (persist_data == NULL) {
3520 SLOGE("Getfield error, cannot load persistent data");
3521 goto out;
3522 }
3523 }
3524
Rubin Xu85c01f92014-10-13 12:49:54 +01003525 // Read value from persistent entries. If the original value is split into multiple entries,
3526 // stitch them back together.
Ken Sumrall160b4d62013-04-22 12:15:39 -07003527 if (!persist_get_key(fieldname, temp_value)) {
Rubin Xu85c01f92014-10-13 12:49:54 +01003528 // We found it, copy it to the caller's buffer and keep going until all entries are read.
3529 if (strlcpy(value, temp_value, len) >= (unsigned) len) {
3530 // value too small
3531 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
3532 goto out;
3533 }
3534 rc = CRYPTO_GETFIELD_OK;
3535
3536 for (i = 1; /* break explicitly */; i++) {
3537 if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >=
3538 (int) sizeof(temp_field)) {
3539 // If the fieldname is very long, we stop as soon as it begins to overflow the
3540 // maximum field length. At this point we have in fact fully read out the original
3541 // value because cryptfs_setfield would not allow fields with longer names to be
3542 // written in the first place.
3543 break;
3544 }
3545 if (!persist_get_key(temp_field, temp_value)) {
3546 if (strlcat(value, temp_value, len) >= (unsigned)len) {
3547 // value too small.
3548 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
3549 goto out;
3550 }
3551 } else {
3552 // Exhaust all entries.
3553 break;
3554 }
3555 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003556 } else {
3557 /* Sadness, it's not there. Return the error */
Rubin Xu85c01f92014-10-13 12:49:54 +01003558 rc = CRYPTO_GETFIELD_ERROR_NO_FIELD;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003559 }
3560
3561out:
3562 return rc;
3563}
3564
3565/* Set the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01003566int cryptfs_setfield(const char *fieldname, const char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003567{
Paul Crowley38132a12016-02-09 09:50:32 +00003568 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08003569 SLOGE("Cannot set field when file encrypted");
3570 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07003571 }
3572
Ken Sumrall160b4d62013-04-22 12:15:39 -07003573 char encrypted_state[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01003574 /* 0 is success, negative values are error */
3575 int rc = CRYPTO_SETFIELD_ERROR_OTHER;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003576 int encrypted = 0;
Rubin Xu85c01f92014-10-13 12:49:54 +01003577 unsigned int field_id;
3578 char temp_field[PROPERTY_KEY_MAX];
3579 unsigned int num_entries;
3580 unsigned int max_keylen;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003581
3582 if (persist_data == NULL) {
3583 load_persistent_data();
3584 if (persist_data == NULL) {
3585 SLOGE("Setfield error, cannot load persistent data");
3586 goto out;
3587 }
3588 }
3589
3590 property_get("ro.crypto.state", encrypted_state, "");
3591 if (!strcmp(encrypted_state, "encrypted") ) {
3592 encrypted = 1;
3593 }
3594
Rubin Xu85c01f92014-10-13 12:49:54 +01003595 // Compute the number of entries required to store value, each entry can store up to
3596 // (PROPERTY_VALUE_MAX - 1) chars
3597 if (strlen(value) == 0) {
3598 // Empty value also needs one entry to store.
3599 num_entries = 1;
3600 } else {
3601 num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1);
3602 }
3603
3604 max_keylen = strlen(fieldname);
3605 if (num_entries > 1) {
3606 // Need an extra "_%d" suffix.
3607 max_keylen += 1 + log10(num_entries);
3608 }
3609 if (max_keylen > PROPERTY_KEY_MAX - 1) {
3610 rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003611 goto out;
3612 }
3613
Rubin Xu85c01f92014-10-13 12:49:54 +01003614 // Make sure we have enough space to write the new value
3615 if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) >
3616 persist_get_max_entries(encrypted)) {
3617 rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG;
3618 goto out;
3619 }
3620
3621 // Now that we know persist_data has enough space for value, let's delete the old field first
3622 // to make up space.
3623 persist_del_keys(fieldname, 0);
3624
3625 if (persist_set_key(fieldname, value, encrypted)) {
3626 // fail to set key, should not happen as we have already checked the available space
3627 SLOGE("persist_set_key() error during setfield()");
3628 goto out;
3629 }
3630
3631 for (field_id = 1; field_id < num_entries; field_id++) {
3632 snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, field_id);
3633
3634 if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), encrypted)) {
3635 // fail to set key, should not happen as we have already checked the available space.
3636 SLOGE("persist_set_key() error during setfield()");
3637 goto out;
3638 }
3639 }
3640
Ken Sumrall160b4d62013-04-22 12:15:39 -07003641 /* If we are running encrypted, save the persistent data now */
3642 if (encrypted) {
3643 if (save_persistent_data()) {
3644 SLOGE("Setfield error, cannot save persistent data");
3645 goto out;
3646 }
3647 }
3648
Rubin Xu85c01f92014-10-13 12:49:54 +01003649 rc = CRYPTO_SETFIELD_OK;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003650
3651out:
3652 return rc;
3653}
Paul Lawrencef4faa572014-01-29 13:31:03 -08003654
3655/* Checks userdata. Attempt to mount the volume if default-
3656 * encrypted.
3657 * On success trigger next init phase and return 0.
3658 * Currently do not handle failure - see TODO below.
3659 */
3660int cryptfs_mount_default_encrypted(void)
3661{
Paul Lawrence84274cc2016-04-15 15:41:33 -07003662 int crypt_type = cryptfs_get_password_type();
3663 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
3664 SLOGE("Bad crypt type - error");
3665 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
3666 SLOGD("Password is not default - "
3667 "starting min framework to prompt");
3668 property_set("vold.decrypt", "trigger_restart_min_framework");
3669 return 0;
3670 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
3671 SLOGD("Password is default - restarting filesystem");
3672 cryptfs_restart_internal(0);
3673 return 0;
Paul Lawrencef4faa572014-01-29 13:31:03 -08003674 } else {
Paul Lawrence84274cc2016-04-15 15:41:33 -07003675 SLOGE("Encrypted, default crypt type but can't decrypt");
Paul Lawrencef4faa572014-01-29 13:31:03 -08003676 }
3677
Paul Lawrence6bfed202014-07-28 12:47:22 -07003678 /** Corrupt. Allow us to boot into framework, which will detect bad
3679 crypto when it calls do_crypto_complete, then do a factory reset
Paul Lawrencef4faa572014-01-29 13:31:03 -08003680 */
Paul Lawrence6bfed202014-07-28 12:47:22 -07003681 property_set("vold.decrypt", "trigger_restart_min_framework");
Paul Lawrencef4faa572014-01-29 13:31:03 -08003682 return 0;
3683}
3684
3685/* Returns type of the password, default, pattern, pin or password.
3686 */
3687int cryptfs_get_password_type(void)
3688{
Paul Crowley38132a12016-02-09 09:50:32 +00003689 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08003690 SLOGE("cryptfs_get_password_type not valid for file encryption");
3691 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08003692 }
3693
Paul Lawrencef4faa572014-01-29 13:31:03 -08003694 struct crypt_mnt_ftr crypt_ftr;
3695
3696 if (get_crypt_ftr_and_key(&crypt_ftr)) {
3697 SLOGE("Error getting crypt footer and key\n");
3698 return -1;
3699 }
3700
Paul Lawrence6bfed202014-07-28 12:47:22 -07003701 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) {
3702 return -1;
3703 }
3704
Paul Lawrencef4faa572014-01-29 13:31:03 -08003705 return crypt_ftr.crypt_type;
3706}
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003707
Paul Lawrence05335c32015-03-05 09:46:23 -08003708const char* cryptfs_get_password()
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003709{
Paul Crowley38132a12016-02-09 09:50:32 +00003710 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08003711 SLOGE("cryptfs_get_password not valid for file encryption");
3712 return 0;
Paul Lawrence05335c32015-03-05 09:46:23 -08003713 }
3714
Paul Lawrence399317e2014-03-10 13:20:50 -07003715 struct timespec now;
Paul Lawrenceef2b5be2014-11-11 12:47:03 -08003716 clock_gettime(CLOCK_BOOTTIME, &now);
Paul Lawrence399317e2014-03-10 13:20:50 -07003717 if (now.tv_sec < password_expiry_time) {
3718 return password;
3719 } else {
3720 cryptfs_clear_password();
3721 return 0;
3722 }
3723}
3724
3725void cryptfs_clear_password()
3726{
3727 if (password) {
3728 size_t len = strlen(password);
3729 memset(password, 0, len);
3730 free(password);
3731 password = 0;
3732 password_expiry_time = 0;
3733 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003734}
Paul Lawrence731a7a22015-04-28 22:14:15 +00003735
3736int cryptfs_enable_file()
3737{
Paul Crowley38132a12016-02-09 09:50:32 +00003738 return e4crypt_initialize_global_de();
Paul Lawrence731a7a22015-04-28 22:14:15 +00003739}
3740
Paul Lawrence0c247462015-10-29 10:30:57 -07003741int cryptfs_isConvertibleToFBE()
3742{
3743 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
3744 return fs_mgr_is_convertible_to_fbe(rec) ? 1 : 0;
3745}
3746
Paul Lawrence731a7a22015-04-28 22:14:15 +00003747int cryptfs_create_default_ftr(struct crypt_mnt_ftr* crypt_ftr, __attribute__((unused))int key_length)
3748{
3749 if (cryptfs_init_crypt_mnt_ftr(crypt_ftr)) {
3750 SLOGE("Failed to initialize crypt_ftr");
3751 return -1;
3752 }
3753
3754 if (create_encrypted_random_key(DEFAULT_PASSWORD, crypt_ftr->master_key,
3755 crypt_ftr->salt, crypt_ftr)) {
3756 SLOGE("Cannot create encrypted master key\n");
3757 return -1;
3758 }
3759
3760 //crypt_ftr->keysize = key_length / 8;
3761 return 0;
3762}
3763
3764int cryptfs_get_master_key(struct crypt_mnt_ftr* ftr, const char* password,
3765 unsigned char* master_key)
3766{
3767 int rc;
3768
Paul Lawrence731a7a22015-04-28 22:14:15 +00003769 unsigned char* intermediate_key = 0;
3770 size_t intermediate_key_size = 0;
Paul Lawrencec78c71b2015-04-14 15:26:29 -07003771
3772 if (password == 0 || *password == 0) {
3773 password = DEFAULT_PASSWORD;
3774 }
3775
Paul Lawrence731a7a22015-04-28 22:14:15 +00003776 rc = decrypt_master_key(password, master_key, ftr, &intermediate_key,
3777 &intermediate_key_size);
3778
Paul Lawrence300dae72016-03-11 11:02:52 -08003779 if (rc) {
3780 SLOGE("Can't calculate intermediate key");
3781 return rc;
3782 }
3783
Paul Lawrencec78c71b2015-04-14 15:26:29 -07003784 int N = 1 << ftr->N_factor;
3785 int r = 1 << ftr->r_factor;
3786 int p = 1 << ftr->p_factor;
3787
3788 unsigned char scrypted_intermediate_key[sizeof(ftr->scrypted_intermediate_key)];
3789
3790 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
3791 ftr->salt, sizeof(ftr->salt), N, r, p,
3792 scrypted_intermediate_key,
3793 sizeof(scrypted_intermediate_key));
3794
3795 free(intermediate_key);
3796
3797 if (rc) {
Paul Lawrence300dae72016-03-11 11:02:52 -08003798 SLOGE("Can't scrypt intermediate key");
Paul Lawrencec78c71b2015-04-14 15:26:29 -07003799 return rc;
3800 }
3801
3802 return memcmp(scrypted_intermediate_key, ftr->scrypted_intermediate_key,
3803 intermediate_key_size);
Paul Lawrence731a7a22015-04-28 22:14:15 +00003804}
3805
3806int cryptfs_set_password(struct crypt_mnt_ftr* ftr, const char* password,
3807 const unsigned char* master_key)
3808{
3809 return encrypt_master_key(password, ftr->salt, master_key, ftr->master_key,
3810 ftr);
3811}
Paul Lawrence6e410592016-05-24 14:20:38 -07003812
3813const char* cryptfs_get_file_encryption_mode()
3814{
3815 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
3816 return fs_mgr_get_file_encryption_mode(rec);
3817}