blob: 7d19dff03f5027f379e9c8f290e5afbf2837102f [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>
39#include <errno.h>
Ken Sumrall3ed82362011-01-28 23:31:16 -080040#include <ext4.h>
Ken Sumrall29d8da82011-05-18 17:20:07 -070041#include <linux/kdev_t.h>
Ken Sumralle5032c42012-04-01 23:58:44 -070042#include <fs_mgr.h>
Paul Lawrence9c58a872014-09-30 09:12:51 -070043#include <time.h>
Rubin Xu85c01f92014-10-13 12:49:54 +010044#include <math.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080045#include "cryptfs.h"
46#define LOG_TAG "Cryptfs"
47#include "cutils/log.h"
48#include "cutils/properties.h"
Ken Sumralladfba362013-06-04 16:37:52 -070049#include "cutils/android_reboot.h"
Ken Sumrall5d4c68e2011-01-30 19:06:03 -080050#include "hardware_legacy/power.h"
Ken Sumralle550f782013-08-20 13:48:23 -070051#include <logwrap/logwrap.h>
Ken Sumrall29d8da82011-05-18 17:20:07 -070052#include "VolumeManager.h"
Ken Sumrall9caab762013-06-11 19:10:20 -070053#include "VoldUtil.h"
Kenny Rootc4c70f12013-06-14 12:11:38 -070054#include "crypto_scrypt.h"
Paul Lawrenceae59fe62014-01-21 08:23:27 -080055#include "ext4_utils.h"
Daniel Rosenberge82df162014-08-15 22:19:23 +000056#include "f2fs_sparseblock.h"
Paul Lawrence87999172014-02-20 12:21:31 -080057#include "CheckBattery.h"
jessica_yu3f14fe42014-09-22 15:57:40 +080058#include "Process.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080059
Paul Lawrence69f4ebd2014-04-14 12:17:14 -070060#include <hardware/keymaster.h>
61
Mark Salyzyn3e971272014-01-21 13:27:04 -080062#define UNUSED __attribute__((unused))
63
Mark Salyzyn5eecc442014-02-12 14:16:14 -080064#define UNUSED __attribute__((unused))
65
Ken Sumrall8f869aa2010-12-03 03:47:09 -080066#define DM_CRYPT_BUF_SIZE 4096
67
Jason parks70a4b3f2011-01-28 10:10:47 -060068#define HASH_COUNT 2000
69#define KEY_LEN_BYTES 16
70#define IV_LEN_BYTES 16
71
Ken Sumrall29d8da82011-05-18 17:20:07 -070072#define KEY_IN_FOOTER "footer"
73
Paul Lawrencef4faa572014-01-29 13:31:03 -080074// "default_password" encoded into hex (d=0x64 etc)
75#define DEFAULT_PASSWORD "64656661756c745f70617373776f7264"
76
Ken Sumrall29d8da82011-05-18 17:20:07 -070077#define EXT4_FS 1
JP Abgrall62c7af32014-06-16 13:01:23 -070078#define F2FS_FS 2
Ken Sumrall29d8da82011-05-18 17:20:07 -070079
Ken Sumralle919efe2012-09-29 17:07:41 -070080#define TABLE_LOAD_RETRIES 10
81
Shawn Willden47ba10d2014-09-03 17:07:06 -060082#define RSA_KEY_SIZE 2048
83#define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8)
84#define RSA_EXPONENT 0x10001
Paul Lawrence69f4ebd2014-04-14 12:17:14 -070085
Paul Lawrence8e3f4512014-09-08 10:11:17 -070086#define RETRY_MOUNT_ATTEMPTS 10
87#define RETRY_MOUNT_DELAY_SECONDS 1
88
Ken Sumrall8f869aa2010-12-03 03:47:09 -080089char *me = "cryptfs";
90
Jason parks70a4b3f2011-01-28 10:10:47 -060091static unsigned char saved_master_key[KEY_LEN_BYTES];
Ken Sumrall3ad90722011-10-04 20:38:29 -070092static char *saved_mount_point;
Jason parks70a4b3f2011-01-28 10:10:47 -060093static int master_key_saved = 0;
Ken Sumrall160b4d62013-04-22 12:15:39 -070094static struct crypt_persist_data *persist_data = NULL;
Ken Sumrall56ad03c2013-02-13 13:00:19 -080095
Paul Lawrence69f4ebd2014-04-14 12:17:14 -070096static int keymaster_init(keymaster_device_t **keymaster_dev)
97{
98 int rc;
99
100 const hw_module_t* mod;
101 rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod);
102 if (rc) {
103 ALOGE("could not find any keystore module");
104 goto out;
105 }
106
107 rc = keymaster_open(mod, keymaster_dev);
108 if (rc) {
109 ALOGE("could not open keymaster device in %s (%s)",
110 KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc));
111 goto out;
112 }
113
114 return 0;
115
116out:
117 *keymaster_dev = NULL;
118 return rc;
119}
120
121/* Should we use keymaster? */
122static int keymaster_check_compatibility()
123{
124 keymaster_device_t *keymaster_dev = 0;
125 int rc = 0;
126
127 if (keymaster_init(&keymaster_dev)) {
128 SLOGE("Failed to init keymaster");
129 rc = -1;
130 goto out;
131 }
132
Paul Lawrence8c008392014-05-06 14:02:48 -0700133 SLOGI("keymaster version is %d", keymaster_dev->common.module->module_api_version);
134
135 if (keymaster_dev->common.module->module_api_version
136 < KEYMASTER_MODULE_API_VERSION_0_3) {
137 rc = 0;
138 goto out;
139 }
140
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700141 if (keymaster_dev->flags & KEYMASTER_BLOBS_ARE_STANDALONE) {
142 rc = 1;
143 }
144
145out:
146 keymaster_close(keymaster_dev);
147 return rc;
148}
149
150/* Create a new keymaster key and store it in this footer */
151static int keymaster_create_key(struct crypt_mnt_ftr *ftr)
152{
153 uint8_t* key = 0;
154 keymaster_device_t *keymaster_dev = 0;
155
156 if (keymaster_init(&keymaster_dev)) {
157 SLOGE("Failed to init keymaster");
158 return -1;
159 }
160
161 int rc = 0;
162
163 keymaster_rsa_keygen_params_t params;
164 memset(&params, '\0', sizeof(params));
Shawn Willden47ba10d2014-09-03 17:07:06 -0600165 params.public_exponent = RSA_EXPONENT;
166 params.modulus_size = RSA_KEY_SIZE;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700167
168 size_t key_size;
169 if (keymaster_dev->generate_keypair(keymaster_dev, TYPE_RSA, &params,
170 &key, &key_size)) {
171 SLOGE("Failed to generate keypair");
172 rc = -1;
173 goto out;
174 }
175
176 if (key_size > KEYMASTER_BLOB_SIZE) {
177 SLOGE("Keymaster key too large for crypto footer");
178 rc = -1;
179 goto out;
180 }
181
182 memcpy(ftr->keymaster_blob, key, key_size);
183 ftr->keymaster_blob_size = key_size;
184
185out:
186 keymaster_close(keymaster_dev);
187 free(key);
188 return rc;
189}
190
Shawn Willdene17a9c42014-09-08 13:04:08 -0600191/* This signs the given object using the keymaster key. */
192static int keymaster_sign_object(struct crypt_mnt_ftr *ftr,
Shawn Willden47ba10d2014-09-03 17:07:06 -0600193 const unsigned char *object,
194 const size_t object_size,
195 unsigned char **signature,
196 size_t *signature_size)
197{
198 int rc = 0;
199 keymaster_device_t *keymaster_dev = 0;
200 if (keymaster_init(&keymaster_dev)) {
201 SLOGE("Failed to init keymaster");
202 return -1;
203 }
204
205 /* We currently set the digest type to DIGEST_NONE because it's the
206 * only supported value for keymaster. A similar issue exists with
207 * PADDING_NONE. Long term both of these should likely change.
208 */
209 keymaster_rsa_sign_params_t params;
210 params.digest_type = DIGEST_NONE;
211 params.padding_type = PADDING_NONE;
212
213 unsigned char to_sign[RSA_KEY_SIZE_BYTES];
Shawn Willdene17a9c42014-09-08 13:04:08 -0600214 size_t to_sign_size = sizeof(to_sign);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600215 memset(to_sign, 0, RSA_KEY_SIZE_BYTES);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600216
Shawn Willdene17a9c42014-09-08 13:04:08 -0600217 // To sign a message with RSA, the message must satisfy two
218 // constraints:
219 //
220 // 1. The message, when interpreted as a big-endian numeric value, must
221 // be strictly less than the public modulus of the RSA key. Note
222 // that because the most significant bit of the public modulus is
223 // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit
224 // key), an n-bit message with most significant bit 0 always
225 // satisfies this requirement.
226 //
227 // 2. The message must have the same length in bits as the public
228 // modulus of the RSA key. This requirement isn't mathematically
229 // necessary, but is necessary to ensure consistency in
230 // implementations.
231 switch (ftr->kdf_type) {
232 case KDF_SCRYPT_KEYMASTER_UNPADDED:
233 // This is broken: It produces a message which is shorter than
234 // the public modulus, failing criterion 2.
235 memcpy(to_sign, object, object_size);
236 to_sign_size = object_size;
237 SLOGI("Signing unpadded object");
238 break;
239 case KDF_SCRYPT_KEYMASTER_BADLY_PADDED:
240 // This is broken: Since the value of object is uniformly
241 // distributed, it produces a message that is larger than the
242 // public modulus with probability 0.25.
243 memcpy(to_sign, object, min(RSA_KEY_SIZE_BYTES, object_size));
244 SLOGI("Signing end-padded object");
245 break;
246 case KDF_SCRYPT_KEYMASTER:
247 // This ensures the most significant byte of the signed message
248 // is zero. We could have zero-padded to the left instead, but
249 // this approach is slightly more robust against changes in
250 // object size. However, it's still broken (but not unusably
251 // so) because we really should be using a proper RSA padding
252 // function, such as OAEP.
253 //
254 // TODO(paullawrence): When keymaster 0.4 is available, change
255 // this to use the padding options it provides.
256 memcpy(to_sign + 1, object, min(RSA_KEY_SIZE_BYTES - 1, object_size));
257 SLOGI("Signing safely-padded object");
258 break;
259 default:
260 SLOGE("Unknown KDF type %d", ftr->kdf_type);
261 return -1;
262 }
263
Shawn Willden47ba10d2014-09-03 17:07:06 -0600264 rc = keymaster_dev->sign_data(keymaster_dev,
265 &params,
266 ftr->keymaster_blob,
267 ftr->keymaster_blob_size,
268 to_sign,
Shawn Willdene17a9c42014-09-08 13:04:08 -0600269 to_sign_size,
Shawn Willden47ba10d2014-09-03 17:07:06 -0600270 signature,
271 signature_size);
272
273 keymaster_close(keymaster_dev);
274 return rc;
275}
276
Paul Lawrence399317e2014-03-10 13:20:50 -0700277/* Store password when userdata is successfully decrypted and mounted.
278 * Cleared by cryptfs_clear_password
279 *
280 * To avoid a double prompt at boot, we need to store the CryptKeeper
281 * password and pass it to KeyGuard, which uses it to unlock KeyStore.
282 * Since the entire framework is torn down and rebuilt after encryption,
283 * we have to use a daemon or similar to store the password. Since vold
284 * is secured against IPC except from system processes, it seems a reasonable
285 * place to store this.
286 *
287 * password should be cleared once it has been used.
288 *
289 * password is aged out after password_max_age_seconds seconds.
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800290 */
Paul Lawrence399317e2014-03-10 13:20:50 -0700291static char* password = 0;
292static int password_expiry_time = 0;
293static const int password_max_age_seconds = 60;
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800294
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800295extern struct fstab *fstab;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800296
Paul Lawrence87999172014-02-20 12:21:31 -0800297enum RebootType {reboot, recovery, shutdown};
298static void cryptfs_reboot(enum RebootType rt)
Ken Sumralladfba362013-06-04 16:37:52 -0700299{
Paul Lawrence87999172014-02-20 12:21:31 -0800300 switch(rt) {
301 case reboot:
302 property_set(ANDROID_RB_PROPERTY, "reboot");
303 break;
304
305 case recovery:
306 property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
307 break;
308
309 case shutdown:
310 property_set(ANDROID_RB_PROPERTY, "shutdown");
311 break;
Ken Sumralladfba362013-06-04 16:37:52 -0700312 }
Paul Lawrence87999172014-02-20 12:21:31 -0800313
Ken Sumralladfba362013-06-04 16:37:52 -0700314 sleep(20);
315
316 /* Shouldn't get here, reboot should happen before sleep times out */
317 return;
318}
319
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800320static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
321{
322 memset(io, 0, dataSize);
323 io->data_size = dataSize;
324 io->data_start = sizeof(struct dm_ioctl);
325 io->version[0] = 4;
326 io->version[1] = 0;
327 io->version[2] = 0;
328 io->flags = flags;
329 if (name) {
330 strncpy(io->name, name, sizeof(io->name));
331 }
332}
333
Kenny Rootc4c70f12013-06-14 12:11:38 -0700334/**
335 * Gets the default device scrypt parameters for key derivation time tuning.
336 * The parameters should lead to about one second derivation time for the
337 * given device.
338 */
339static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
340 const int default_params[] = SCRYPT_DEFAULTS;
341 int params[] = SCRYPT_DEFAULTS;
342 char paramstr[PROPERTY_VALUE_MAX];
343 char *token;
344 char *saveptr;
345 int i;
346
347 property_get(SCRYPT_PROP, paramstr, "");
348 if (paramstr[0] != '\0') {
349 /*
350 * The token we're looking for should be three integers separated by
351 * colons (e.g., "12:8:1"). Scan the property to make sure it matches.
352 */
Kenny Root2947e342013-08-14 15:54:49 -0700353 for (i = 0, token = strtok_r(paramstr, ":", &saveptr);
354 token != NULL && i < 3;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700355 i++, token = strtok_r(NULL, ":", &saveptr)) {
356 char *endptr;
357 params[i] = strtol(token, &endptr, 10);
358
359 /*
360 * Check that there was a valid number and it's 8-bit. If not,
361 * break out and the end check will take the default values.
362 */
363 if ((*token == '\0') || (*endptr != '\0') || params[i] < 0 || params[i] > 255) {
364 break;
365 }
366 }
367
368 /*
369 * If there were not enough tokens or a token was malformed (not an
370 * integer), it will end up here and the default parameters can be
371 * taken.
372 */
373 if ((i != 3) || (token != NULL)) {
374 SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
375 memcpy(params, default_params, sizeof(params));
376 }
377 }
378
379 ftr->N_factor = params[0];
380 ftr->r_factor = params[1];
381 ftr->p_factor = params[2];
382}
383
Ken Sumrall3ed82362011-01-28 23:31:16 -0800384static unsigned int get_fs_size(char *dev)
385{
386 int fd, block_size;
387 struct ext4_super_block sb;
388 off64_t len;
389
390 if ((fd = open(dev, O_RDONLY)) < 0) {
391 SLOGE("Cannot open device to get filesystem size ");
392 return 0;
393 }
394
395 if (lseek64(fd, 1024, SEEK_SET) < 0) {
396 SLOGE("Cannot seek to superblock");
397 return 0;
398 }
399
400 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
401 SLOGE("Cannot read superblock");
402 return 0;
403 }
404
405 close(fd);
406
Daniel Rosenberge82df162014-08-15 22:19:23 +0000407 if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) {
408 SLOGE("Not a valid ext4 superblock");
409 return 0;
410 }
Ken Sumrall3ed82362011-01-28 23:31:16 -0800411 block_size = 1024 << sb.s_log_block_size;
412 /* compute length in bytes */
413 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
414
415 /* return length in sectors */
416 return (unsigned int) (len / 512);
417}
418
Ken Sumrall160b4d62013-04-22 12:15:39 -0700419static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
420{
421 static int cached_data = 0;
422 static off64_t cached_off = 0;
423 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
424 int fd;
425 char key_loc[PROPERTY_VALUE_MAX];
426 char real_blkdev[PROPERTY_VALUE_MAX];
427 unsigned int nr_sec;
428 int rc = -1;
429
430 if (!cached_data) {
431 fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
432
433 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
434 if ( (fd = open(real_blkdev, O_RDWR)) < 0) {
435 SLOGE("Cannot open real block device %s\n", real_blkdev);
436 return -1;
437 }
438
439 if ((nr_sec = get_blkdev_size(fd))) {
440 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
441 * encryption info footer and key, and plenty of bytes to spare for future
442 * growth.
443 */
444 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
445 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
446 cached_data = 1;
447 } else {
448 SLOGE("Cannot get size of block device %s\n", real_blkdev);
449 }
450 close(fd);
451 } else {
452 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
453 cached_off = 0;
454 cached_data = 1;
455 }
456 }
457
458 if (cached_data) {
459 if (metadata_fname) {
460 *metadata_fname = cached_metadata_fname;
461 }
462 if (off) {
463 *off = cached_off;
464 }
465 rc = 0;
466 }
467
468 return rc;
469}
470
Ken Sumralle8744072011-01-18 22:01:55 -0800471/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800472 * update the failed mount count but not change the key.
473 */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700474static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800475{
476 int fd;
477 unsigned int nr_sec, cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700478 /* starting_off is set to the SEEK_SET offset
479 * where the crypto structure starts
480 */
481 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800482 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700483 char *fname = NULL;
Ken Sumrall3be890f2011-09-14 16:53:46 -0700484 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800485
Ken Sumrall160b4d62013-04-22 12:15:39 -0700486 if (get_crypt_ftr_info(&fname, &starting_off)) {
487 SLOGE("Unable to get crypt_ftr_info\n");
488 return -1;
489 }
490 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700491 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700492 return -1;
493 }
Ken Sumralle550f782013-08-20 13:48:23 -0700494 if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) {
495 SLOGE("Cannot open footer file %s for put\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700496 return -1;
497 }
498
499 /* Seek to the start of the crypt footer */
500 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
501 SLOGE("Cannot seek to real block device footer\n");
502 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800503 }
504
505 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
506 SLOGE("Cannot write real block device footer\n");
507 goto errout;
508 }
509
Ken Sumrall3be890f2011-09-14 16:53:46 -0700510 fstat(fd, &statbuf);
511 /* If the keys are kept on a raw block device, do not try to truncate it. */
Ken Sumralle550f782013-08-20 13:48:23 -0700512 if (S_ISREG(statbuf.st_mode)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700513 if (ftruncate(fd, 0x4000)) {
Colin Cross59846b62014-02-06 20:34:29 -0800514 SLOGE("Cannot set footer file size\n");
Ken Sumralle8744072011-01-18 22:01:55 -0800515 goto errout;
516 }
517 }
518
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800519 /* Success! */
520 rc = 0;
521
522errout:
523 close(fd);
524 return rc;
525
526}
527
Ken Sumrall160b4d62013-04-22 12:15:39 -0700528static inline int unix_read(int fd, void* buff, int len)
529{
530 return TEMP_FAILURE_RETRY(read(fd, buff, len));
531}
532
533static inline int unix_write(int fd, const void* buff, int len)
534{
535 return TEMP_FAILURE_RETRY(write(fd, buff, len));
536}
537
538static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
539{
540 memset(pdata, 0, len);
541 pdata->persist_magic = PERSIST_DATA_MAGIC;
542 pdata->persist_valid_entries = 0;
543}
544
545/* A routine to update the passed in crypt_ftr to the lastest version.
546 * fd is open read/write on the device that holds the crypto footer and persistent
547 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
548 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
549 */
550static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
551{
Kenny Root7434b312013-06-14 11:29:53 -0700552 int orig_major = crypt_ftr->major_version;
553 int orig_minor = crypt_ftr->minor_version;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700554
Kenny Root7434b312013-06-14 11:29:53 -0700555 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
556 struct crypt_persist_data *pdata;
557 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700558
Kenny Rootc4c70f12013-06-14 12:11:38 -0700559 SLOGW("upgrading crypto footer to 1.1");
560
Kenny Root7434b312013-06-14 11:29:53 -0700561 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
562 if (pdata == NULL) {
563 SLOGE("Cannot allocate persisent data\n");
564 return;
565 }
566 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
567
568 /* Need to initialize the persistent data area */
569 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
570 SLOGE("Cannot seek to persisent data offset\n");
571 return;
572 }
573 /* Write all zeros to the first copy, making it invalid */
574 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
575
576 /* Write a valid but empty structure to the second copy */
577 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
578 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
579
580 /* Update the footer */
581 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
582 crypt_ftr->persist_data_offset[0] = pdata_offset;
583 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
584 crypt_ftr->minor_version = 1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700585 }
586
Paul Lawrencef4faa572014-01-29 13:31:03 -0800587 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700588 SLOGW("upgrading crypto footer to 1.2");
JP Abgrall7bdfa522013-11-15 13:42:56 -0800589 /* But keep the old kdf_type.
590 * It will get updated later to KDF_SCRYPT after the password has been verified.
591 */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700592 crypt_ftr->kdf_type = KDF_PBKDF2;
593 get_device_scrypt_params(crypt_ftr);
594 crypt_ftr->minor_version = 2;
595 }
596
Paul Lawrencef4faa572014-01-29 13:31:03 -0800597 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
598 SLOGW("upgrading crypto footer to 1.3");
599 crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
600 crypt_ftr->minor_version = 3;
601 }
602
Kenny Root7434b312013-06-14 11:29:53 -0700603 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
604 if (lseek64(fd, offset, SEEK_SET) == -1) {
605 SLOGE("Cannot seek to crypt footer\n");
606 return;
607 }
608 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700609 }
Ken Sumrall160b4d62013-04-22 12:15:39 -0700610}
611
612
613static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800614{
615 int fd;
616 unsigned int nr_sec, cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700617 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800618 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700619 char *fname = NULL;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700620 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800621
Ken Sumrall160b4d62013-04-22 12:15:39 -0700622 if (get_crypt_ftr_info(&fname, &starting_off)) {
623 SLOGE("Unable to get crypt_ftr_info\n");
624 return -1;
625 }
626 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700627 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700628 return -1;
629 }
630 if ( (fd = open(fname, O_RDWR)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700631 SLOGE("Cannot open footer file %s for get\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700632 return -1;
633 }
634
635 /* Make sure it's 16 Kbytes in length */
636 fstat(fd, &statbuf);
637 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
638 SLOGE("footer file %s is not the expected size!\n", fname);
639 goto errout;
640 }
641
642 /* Seek to the start of the crypt footer */
643 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
644 SLOGE("Cannot seek to real block device footer\n");
645 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800646 }
647
648 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
649 SLOGE("Cannot read real block device footer\n");
650 goto errout;
651 }
652
653 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700654 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800655 goto errout;
656 }
657
Kenny Rootc96a5f82013-06-14 12:08:28 -0700658 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
659 SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
660 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800661 goto errout;
662 }
663
Kenny Rootc96a5f82013-06-14 12:08:28 -0700664 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
665 SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
666 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800667 }
668
Ken Sumrall160b4d62013-04-22 12:15:39 -0700669 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
670 * copy on disk before returning.
671 */
Kenny Rootc96a5f82013-06-14 12:08:28 -0700672 if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700673 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
Ken Sumralle8744072011-01-18 22:01:55 -0800674 }
675
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800676 /* Success! */
677 rc = 0;
678
679errout:
680 close(fd);
681 return rc;
682}
683
Ken Sumrall160b4d62013-04-22 12:15:39 -0700684static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
685{
686 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
687 crypt_ftr->persist_data_offset[1]) {
688 SLOGE("Crypt_ftr persist data regions overlap");
689 return -1;
690 }
691
692 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
693 SLOGE("Crypt_ftr persist data region 0 starts after region 1");
694 return -1;
695 }
696
697 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
698 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
699 CRYPT_FOOTER_OFFSET) {
700 SLOGE("Persistent data extends past crypto footer");
701 return -1;
702 }
703
704 return 0;
705}
706
707static int load_persistent_data(void)
708{
709 struct crypt_mnt_ftr crypt_ftr;
710 struct crypt_persist_data *pdata = NULL;
711 char encrypted_state[PROPERTY_VALUE_MAX];
712 char *fname;
713 int found = 0;
714 int fd;
715 int ret;
716 int i;
717
718 if (persist_data) {
719 /* Nothing to do, we've already loaded or initialized it */
720 return 0;
721 }
722
723
724 /* If not encrypted, just allocate an empty table and initialize it */
725 property_get("ro.crypto.state", encrypted_state, "");
726 if (strcmp(encrypted_state, "encrypted") ) {
727 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
728 if (pdata) {
729 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
730 persist_data = pdata;
731 return 0;
732 }
733 return -1;
734 }
735
736 if(get_crypt_ftr_and_key(&crypt_ftr)) {
737 return -1;
738 }
739
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700740 if ((crypt_ftr.major_version < 1)
741 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700742 SLOGE("Crypt_ftr version doesn't support persistent data");
743 return -1;
744 }
745
746 if (get_crypt_ftr_info(&fname, NULL)) {
747 return -1;
748 }
749
750 ret = validate_persistent_data_storage(&crypt_ftr);
751 if (ret) {
752 return -1;
753 }
754
755 fd = open(fname, O_RDONLY);
756 if (fd < 0) {
757 SLOGE("Cannot open %s metadata file", fname);
758 return -1;
759 }
760
761 if (persist_data == NULL) {
762 pdata = malloc(crypt_ftr.persist_data_size);
763 if (pdata == NULL) {
764 SLOGE("Cannot allocate memory for persistent data");
765 goto err;
766 }
767 }
768
769 for (i = 0; i < 2; i++) {
770 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
771 SLOGE("Cannot seek to read persistent data on %s", fname);
772 goto err2;
773 }
774 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
775 SLOGE("Error reading persistent data on iteration %d", i);
776 goto err2;
777 }
778 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
779 found = 1;
780 break;
781 }
782 }
783
784 if (!found) {
785 SLOGI("Could not find valid persistent data, creating");
786 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
787 }
788
789 /* Success */
790 persist_data = pdata;
791 close(fd);
792 return 0;
793
794err2:
795 free(pdata);
796
797err:
798 close(fd);
799 return -1;
800}
801
802static int save_persistent_data(void)
803{
804 struct crypt_mnt_ftr crypt_ftr;
805 struct crypt_persist_data *pdata;
806 char *fname;
807 off64_t write_offset;
808 off64_t erase_offset;
809 int found = 0;
810 int fd;
811 int ret;
812
813 if (persist_data == NULL) {
814 SLOGE("No persistent data to save");
815 return -1;
816 }
817
818 if(get_crypt_ftr_and_key(&crypt_ftr)) {
819 return -1;
820 }
821
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700822 if ((crypt_ftr.major_version < 1)
823 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700824 SLOGE("Crypt_ftr version doesn't support persistent data");
825 return -1;
826 }
827
828 ret = validate_persistent_data_storage(&crypt_ftr);
829 if (ret) {
830 return -1;
831 }
832
833 if (get_crypt_ftr_info(&fname, NULL)) {
834 return -1;
835 }
836
837 fd = open(fname, O_RDWR);
838 if (fd < 0) {
839 SLOGE("Cannot open %s metadata file", fname);
840 return -1;
841 }
842
843 pdata = malloc(crypt_ftr.persist_data_size);
844 if (pdata == NULL) {
845 SLOGE("Cannot allocate persistant data");
846 goto err;
847 }
848
849 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
850 SLOGE("Cannot seek to read persistent data on %s", fname);
851 goto err2;
852 }
853
854 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
855 SLOGE("Error reading persistent data before save");
856 goto err2;
857 }
858
859 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
860 /* The first copy is the curent valid copy, so write to
861 * the second copy and erase this one */
862 write_offset = crypt_ftr.persist_data_offset[1];
863 erase_offset = crypt_ftr.persist_data_offset[0];
864 } else {
865 /* The second copy must be the valid copy, so write to
866 * the first copy, and erase the second */
867 write_offset = crypt_ftr.persist_data_offset[0];
868 erase_offset = crypt_ftr.persist_data_offset[1];
869 }
870
871 /* Write the new copy first, if successful, then erase the old copy */
872 if (lseek(fd, write_offset, SEEK_SET) < 0) {
873 SLOGE("Cannot seek to write persistent data");
874 goto err2;
875 }
876 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
877 (int) crypt_ftr.persist_data_size) {
878 if (lseek(fd, erase_offset, SEEK_SET) < 0) {
879 SLOGE("Cannot seek to erase previous persistent data");
880 goto err2;
881 }
882 fsync(fd);
883 memset(pdata, 0, crypt_ftr.persist_data_size);
884 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
885 (int) crypt_ftr.persist_data_size) {
886 SLOGE("Cannot write to erase previous persistent data");
887 goto err2;
888 }
889 fsync(fd);
890 } else {
891 SLOGE("Cannot write to save persistent data");
892 goto err2;
893 }
894
895 /* Success */
896 free(pdata);
897 close(fd);
898 return 0;
899
900err2:
901 free(pdata);
902err:
903 close(fd);
904 return -1;
905}
906
Paul Lawrencef4faa572014-01-29 13:31:03 -0800907static int hexdigit (char c)
908{
909 if (c >= '0' && c <= '9') return c - '0';
910 c = tolower(c);
911 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
912 return -1;
913}
914
915static unsigned char* convert_hex_ascii_to_key(const char* master_key_ascii,
916 unsigned int* out_keysize)
917{
918 unsigned int i;
919 *out_keysize = 0;
920
921 size_t size = strlen (master_key_ascii);
922 if (size % 2) {
923 SLOGE("Trying to convert ascii string of odd length");
924 return NULL;
925 }
926
927 unsigned char* master_key = (unsigned char*) malloc(size / 2);
928 if (master_key == 0) {
929 SLOGE("Cannot allocate");
930 return NULL;
931 }
932
933 for (i = 0; i < size; i += 2) {
934 int high_nibble = hexdigit (master_key_ascii[i]);
935 int low_nibble = hexdigit (master_key_ascii[i + 1]);
936
937 if(high_nibble < 0 || low_nibble < 0) {
938 SLOGE("Invalid hex string");
939 free (master_key);
940 return NULL;
941 }
942
943 master_key[*out_keysize] = high_nibble * 16 + low_nibble;
944 (*out_keysize)++;
945 }
946
947 return master_key;
948}
949
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800950/* Convert a binary key of specified length into an ascii hex string equivalent,
951 * without the leading 0x and with null termination
952 */
Paul Lawrencef4faa572014-01-29 13:31:03 -0800953static void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800954 char *master_key_ascii)
955{
956 unsigned int i, a;
957 unsigned char nibble;
958
959 for (i=0, a=0; i<keysize; i++, a+=2) {
960 /* For each byte, write out two ascii hex digits */
961 nibble = (master_key[i] >> 4) & 0xf;
962 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
963
964 nibble = master_key[i] & 0xf;
965 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
966 }
967
968 /* Add the null termination */
969 master_key_ascii[a] = '\0';
970
971}
972
Ken Sumralldb5e0262013-02-05 17:39:48 -0800973static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
974 char *real_blk_name, const char *name, int fd,
975 char *extra_params)
976{
977 char buffer[DM_CRYPT_BUF_SIZE];
978 struct dm_ioctl *io;
979 struct dm_target_spec *tgt;
980 char *crypt_params;
981 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
982 int i;
983
984 io = (struct dm_ioctl *) buffer;
985
986 /* Load the mapping table for this device */
987 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
988
989 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
990 io->target_count = 1;
991 tgt->status = 0;
992 tgt->sector_start = 0;
993 tgt->length = crypt_ftr->fs_size;
994 strcpy(tgt->target_type, "crypt");
995
996 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
997 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
998 sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name,
999 master_key_ascii, real_blk_name, extra_params);
1000 crypt_params += strlen(crypt_params) + 1;
1001 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
1002 tgt->next = crypt_params - buffer;
1003
1004 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
1005 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
1006 break;
1007 }
1008 usleep(500000);
1009 }
1010
1011 if (i == TABLE_LOAD_RETRIES) {
1012 /* We failed to load the table, return an error */
1013 return -1;
1014 } else {
1015 return i + 1;
1016 }
1017}
1018
1019
1020static int get_dm_crypt_version(int fd, const char *name, int *version)
1021{
1022 char buffer[DM_CRYPT_BUF_SIZE];
1023 struct dm_ioctl *io;
1024 struct dm_target_versions *v;
1025 int i;
1026
1027 io = (struct dm_ioctl *) buffer;
1028
1029 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1030
1031 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
1032 return -1;
1033 }
1034
1035 /* Iterate over the returned versions, looking for name of "crypt".
1036 * When found, get and return the version.
1037 */
1038 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
1039 while (v->next) {
1040 if (! strcmp(v->name, "crypt")) {
1041 /* We found the crypt driver, return the version, and get out */
1042 version[0] = v->version[0];
1043 version[1] = v->version[1];
1044 version[2] = v->version[2];
1045 return 0;
1046 }
1047 v = (struct dm_target_versions *)(((char *)v) + v->next);
1048 }
1049
1050 return -1;
1051}
1052
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001053static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
Ken Sumrall29d8da82011-05-18 17:20:07 -07001054 char *real_blk_name, char *crypto_blk_name, const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001055{
1056 char buffer[DM_CRYPT_BUF_SIZE];
1057 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
1058 char *crypt_params;
1059 struct dm_ioctl *io;
1060 struct dm_target_spec *tgt;
1061 unsigned int minor;
1062 int fd;
Ken Sumralle919efe2012-09-29 17:07:41 -07001063 int i;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001064 int retval = -1;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001065 int version[3];
1066 char *extra_params;
1067 int load_count;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001068
1069 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
1070 SLOGE("Cannot open device-mapper\n");
1071 goto errout;
1072 }
1073
1074 io = (struct dm_ioctl *) buffer;
1075
1076 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1077 if (ioctl(fd, DM_DEV_CREATE, io)) {
1078 SLOGE("Cannot create dm-crypt device\n");
1079 goto errout;
1080 }
1081
1082 /* Get the device status, in particular, the name of it's device file */
1083 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1084 if (ioctl(fd, DM_DEV_STATUS, io)) {
1085 SLOGE("Cannot retrieve dm-crypt device status\n");
1086 goto errout;
1087 }
1088 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
1089 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
1090
Ken Sumralldb5e0262013-02-05 17:39:48 -08001091 extra_params = "";
1092 if (! get_dm_crypt_version(fd, name, version)) {
1093 /* Support for allow_discards was added in version 1.11.0 */
1094 if ((version[0] >= 2) ||
1095 ((version[0] == 1) && (version[1] >= 11))) {
1096 extra_params = "1 allow_discards";
1097 SLOGI("Enabling support for allow_discards in dmcrypt.\n");
1098 }
Ken Sumralle919efe2012-09-29 17:07:41 -07001099 }
1100
Ken Sumralldb5e0262013-02-05 17:39:48 -08001101 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name,
1102 fd, extra_params);
1103 if (load_count < 0) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001104 SLOGE("Cannot load dm-crypt mapping table.\n");
1105 goto errout;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001106 } else if (load_count > 1) {
1107 SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001108 }
1109
1110 /* Resume this device to activate it */
Ken Sumralldb5e0262013-02-05 17:39:48 -08001111 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001112
1113 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
1114 SLOGE("Cannot resume the dm-crypt device\n");
1115 goto errout;
1116 }
1117
1118 /* We made it here with no errors. Woot! */
1119 retval = 0;
1120
1121errout:
1122 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1123
1124 return retval;
1125}
1126
Ken Sumrall29d8da82011-05-18 17:20:07 -07001127static int delete_crypto_blk_dev(char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001128{
1129 int fd;
1130 char buffer[DM_CRYPT_BUF_SIZE];
1131 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001132 int retval = -1;
1133
1134 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
1135 SLOGE("Cannot open device-mapper\n");
1136 goto errout;
1137 }
1138
1139 io = (struct dm_ioctl *) buffer;
1140
1141 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1142 if (ioctl(fd, DM_DEV_REMOVE, io)) {
1143 SLOGE("Cannot remove dm-crypt device\n");
1144 goto errout;
1145 }
1146
1147 /* We made it here with no errors. Woot! */
1148 retval = 0;
1149
1150errout:
1151 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1152
1153 return retval;
1154
1155}
1156
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001157static int pbkdf2(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001158 unsigned char *ikey, void *params UNUSED)
1159{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001160 SLOGI("Using pbkdf2 for cryptfs KDF");
1161
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001162 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001163 unsigned int keysize;
1164 char* master_key = (char*)convert_hex_ascii_to_key(passwd, &keysize);
1165 if (!master_key) return -1;
1166 PKCS5_PBKDF2_HMAC_SHA1(master_key, keysize, salt, SALT_LEN,
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001167 HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001168
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001169 memset(master_key, 0, keysize);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001170 free (master_key);
1171 return 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001172}
1173
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001174static int scrypt(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001175 unsigned char *ikey, void *params)
1176{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001177 SLOGI("Using scrypt for cryptfs KDF");
1178
Kenny Rootc4c70f12013-06-14 12:11:38 -07001179 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1180
1181 int N = 1 << ftr->N_factor;
1182 int r = 1 << ftr->r_factor;
1183 int p = 1 << ftr->p_factor;
1184
1185 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001186 unsigned int keysize;
1187 unsigned char* master_key = convert_hex_ascii_to_key(passwd, &keysize);
1188 if (!master_key) return -1;
1189 crypto_scrypt(master_key, keysize, salt, SALT_LEN, N, r, p, ikey,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001190 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001191
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001192 memset(master_key, 0, keysize);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001193 free (master_key);
1194 return 0;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001195}
1196
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001197static int scrypt_keymaster(const char *passwd, const unsigned char *salt,
1198 unsigned char *ikey, void *params)
1199{
1200 SLOGI("Using scrypt with keymaster for cryptfs KDF");
1201
1202 int rc;
1203 unsigned int key_size;
1204 size_t signature_size;
1205 unsigned char* signature;
1206 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1207
1208 int N = 1 << ftr->N_factor;
1209 int r = 1 << ftr->r_factor;
1210 int p = 1 << ftr->p_factor;
1211
1212 unsigned char* master_key = convert_hex_ascii_to_key(passwd, &key_size);
1213 if (!master_key) {
1214 SLOGE("Failed to convert passwd from hex");
1215 return -1;
1216 }
1217
1218 rc = crypto_scrypt(master_key, key_size, salt, SALT_LEN,
1219 N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES);
1220 memset(master_key, 0, key_size);
1221 free(master_key);
1222
1223 if (rc) {
1224 SLOGE("scrypt failed");
1225 return -1;
1226 }
1227
Shawn Willdene17a9c42014-09-08 13:04:08 -06001228 if (keymaster_sign_object(ftr, ikey, KEY_LEN_BYTES + IV_LEN_BYTES,
1229 &signature, &signature_size)) {
1230 SLOGE("Signing failed");
1231 return -1;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001232 }
1233
1234 rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN,
1235 N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES);
1236 free(signature);
1237
1238 if (rc) {
1239 SLOGE("scrypt failed");
1240 return -1;
1241 }
1242
1243 return 0;
1244}
1245
1246static int encrypt_master_key(const char *passwd, const unsigned char *salt,
1247 const unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001248 unsigned char *encrypted_master_key,
1249 struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001250{
1251 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
1252 EVP_CIPHER_CTX e_ctx;
1253 int encrypted_len, final_len;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001254 int rc = 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001255
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001256 /* Turn the password into an intermediate key and IV that can decrypt the master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001257 get_device_scrypt_params(crypt_ftr);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001258
1259 switch (crypt_ftr->kdf_type) {
Shawn Willdene17a9c42014-09-08 13:04:08 -06001260 case KDF_SCRYPT_KEYMASTER_UNPADDED:
1261 case KDF_SCRYPT_KEYMASTER_BADLY_PADDED:
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001262 case KDF_SCRYPT_KEYMASTER:
1263 if (keymaster_create_key(crypt_ftr)) {
1264 SLOGE("keymaster_create_key failed");
1265 return -1;
1266 }
1267
1268 if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) {
1269 SLOGE("scrypt failed");
1270 return -1;
1271 }
1272 break;
1273
1274 case KDF_SCRYPT:
1275 if (scrypt(passwd, salt, ikey, crypt_ftr)) {
1276 SLOGE("scrypt failed");
1277 return -1;
1278 }
1279 break;
1280
1281 default:
1282 SLOGE("Invalid kdf_type");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001283 return -1;
1284 }
Kenny Rootc4c70f12013-06-14 12:11:38 -07001285
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001286 /* Initialize the decryption engine */
1287 if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
1288 SLOGE("EVP_EncryptInit failed\n");
1289 return -1;
1290 }
1291 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001292
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001293 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001294 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
1295 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001296 SLOGE("EVP_EncryptUpdate failed\n");
1297 return -1;
1298 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001299 if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001300 SLOGE("EVP_EncryptFinal failed\n");
1301 return -1;
1302 }
1303
1304 if (encrypted_len + final_len != KEY_LEN_BYTES) {
1305 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
1306 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001307 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001308
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001309 /* Store the scrypt of the intermediate key, so we can validate if it's a
1310 password error or mount error when things go wrong.
1311 Note there's no need to check for errors, since if this is incorrect, we
1312 simply won't wipe userdata, which is the correct default behavior
1313 */
1314 int N = 1 << crypt_ftr->N_factor;
1315 int r = 1 << crypt_ftr->r_factor;
1316 int p = 1 << crypt_ftr->p_factor;
1317
1318 rc = crypto_scrypt(ikey, KEY_LEN_BYTES,
1319 crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p,
1320 crypt_ftr->scrypted_intermediate_key,
1321 sizeof(crypt_ftr->scrypted_intermediate_key));
1322
1323 if (rc) {
1324 SLOGE("encrypt_master_key: crypto_scrypt failed");
1325 }
1326
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001327 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001328}
1329
JP Abgrall7bdfa522013-11-15 13:42:56 -08001330static int decrypt_master_key_aux(char *passwd, unsigned char *salt,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001331 unsigned char *encrypted_master_key,
1332 unsigned char *decrypted_master_key,
1333 kdf_func kdf, void *kdf_params,
1334 unsigned char** intermediate_key,
1335 size_t* intermediate_key_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001336{
1337 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 -08001338 EVP_CIPHER_CTX d_ctx;
1339 int decrypted_len, final_len;
1340
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001341 /* Turn the password into an intermediate key and IV that can decrypt the
1342 master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001343 if (kdf(passwd, salt, ikey, kdf_params)) {
1344 SLOGE("kdf failed");
1345 return -1;
1346 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001347
1348 /* Initialize the decryption engine */
1349 if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
1350 return -1;
1351 }
1352 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1353 /* Decrypt the master key */
1354 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
1355 encrypted_master_key, KEY_LEN_BYTES)) {
1356 return -1;
1357 }
1358 if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
1359 return -1;
1360 }
1361
1362 if (decrypted_len + final_len != KEY_LEN_BYTES) {
1363 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001364 }
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001365
1366 /* Copy intermediate key if needed by params */
1367 if (intermediate_key && intermediate_key_size) {
1368 *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES);
1369 if (intermediate_key) {
1370 memcpy(*intermediate_key, ikey, KEY_LEN_BYTES);
1371 *intermediate_key_size = KEY_LEN_BYTES;
1372 }
1373 }
1374
1375 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001376}
1377
Kenny Rootc4c70f12013-06-14 12:11:38 -07001378static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001379{
Shawn Willdene17a9c42014-09-08 13:04:08 -06001380 if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER_UNPADDED ||
1381 ftr->kdf_type == KDF_SCRYPT_KEYMASTER_BADLY_PADDED ||
1382 ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001383 *kdf = scrypt_keymaster;
1384 *kdf_params = ftr;
1385 } else if (ftr->kdf_type == KDF_SCRYPT) {
Kenny Rootc4c70f12013-06-14 12:11:38 -07001386 *kdf = scrypt;
1387 *kdf_params = ftr;
1388 } else {
1389 *kdf = pbkdf2;
1390 *kdf_params = NULL;
1391 }
1392}
1393
JP Abgrall7bdfa522013-11-15 13:42:56 -08001394static int decrypt_master_key(char *passwd, unsigned char *decrypted_master_key,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001395 struct crypt_mnt_ftr *crypt_ftr,
1396 unsigned char** intermediate_key,
1397 size_t* intermediate_key_size)
Kenny Rootc4c70f12013-06-14 12:11:38 -07001398{
1399 kdf_func kdf;
1400 void *kdf_params;
1401 int ret;
1402
1403 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001404 ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key,
1405 decrypted_master_key, kdf, kdf_params,
1406 intermediate_key, intermediate_key_size);
Kenny Rootc4c70f12013-06-14 12:11:38 -07001407 if (ret != 0) {
1408 SLOGW("failure decrypting master key");
Kenny Rootc4c70f12013-06-14 12:11:38 -07001409 }
1410
1411 return ret;
1412}
1413
1414static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt,
1415 struct crypt_mnt_ftr *crypt_ftr) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001416 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -08001417 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001418 EVP_CIPHER_CTX e_ctx;
1419 int encrypted_len, final_len;
1420
1421 /* Get some random bits for a key */
1422 fd = open("/dev/urandom", O_RDONLY);
Ken Sumralle8744072011-01-18 22:01:55 -08001423 read(fd, key_buf, sizeof(key_buf));
1424 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001425 close(fd);
1426
1427 /* Now encrypt it with the password */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001428 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001429}
1430
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001431static int wait_and_unmount(char *mountpoint, bool kill)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001432{
Greg Hackmann955653e2014-09-24 14:55:20 -07001433 int i, err, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001434#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001435
1436 /* Now umount the tmpfs filesystem */
1437 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001438 if (umount(mountpoint) == 0) {
1439 break;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001440 }
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001441
1442 if (errno == EINVAL) {
1443 /* EINVAL is returned if the directory is not a mountpoint,
1444 * i.e. there is no filesystem mounted there. So just get out.
1445 */
1446 break;
1447 }
1448
1449 err = errno;
1450
1451 /* If allowed, be increasingly aggressive before the last two retries */
1452 if (kill) {
1453 if (i == (WAIT_UNMOUNT_COUNT - 3)) {
1454 SLOGW("sending SIGHUP to processes with open files\n");
1455 vold_killProcessesWithOpenFiles(mountpoint, 1);
1456 } else if (i == (WAIT_UNMOUNT_COUNT - 2)) {
1457 SLOGW("sending SIGKILL to processes with open files\n");
1458 vold_killProcessesWithOpenFiles(mountpoint, 2);
1459 }
1460 }
1461
1462 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001463 }
1464
1465 if (i < WAIT_UNMOUNT_COUNT) {
1466 SLOGD("unmounting %s succeeded\n", mountpoint);
1467 rc = 0;
1468 } else {
jessica_yu3f14fe42014-09-22 15:57:40 +08001469 vold_killProcessesWithOpenFiles(mountpoint, 0);
Greg Hackmann955653e2014-09-24 14:55:20 -07001470 SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001471 rc = -1;
1472 }
1473
1474 return rc;
1475}
1476
Ken Sumrallc5872692013-05-14 15:26:31 -07001477#define DATA_PREP_TIMEOUT 200
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001478static int prep_data_fs(void)
1479{
1480 int i;
1481
1482 /* Do the prep of the /data filesystem */
1483 property_set("vold.post_fs_data_done", "0");
1484 property_set("vold.decrypt", "trigger_post_fs_data");
1485 SLOGD("Just triggered post_fs_data\n");
1486
Ken Sumrallc5872692013-05-14 15:26:31 -07001487 /* Wait a max of 50 seconds, hopefully it takes much less */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001488 for (i=0; i<DATA_PREP_TIMEOUT; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001489 char p[PROPERTY_VALUE_MAX];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001490
1491 property_get("vold.post_fs_data_done", p, "0");
1492 if (*p == '1') {
1493 break;
1494 } else {
1495 usleep(250000);
1496 }
1497 }
1498 if (i == DATA_PREP_TIMEOUT) {
1499 /* Ugh, we failed to prep /data in time. Bail. */
Ken Sumrallc5872692013-05-14 15:26:31 -07001500 SLOGE("post_fs_data timed out!\n");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001501 return -1;
1502 } else {
1503 SLOGD("post_fs_data done\n");
1504 return 0;
1505 }
1506}
1507
Paul Lawrence74f29f12014-08-28 15:54:10 -07001508static void cryptfs_set_corrupt()
1509{
1510 // Mark the footer as bad
1511 struct crypt_mnt_ftr crypt_ftr;
1512 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1513 SLOGE("Failed to get crypto footer - panic");
1514 return;
1515 }
1516
1517 crypt_ftr.flags |= CRYPT_DATA_CORRUPT;
1518 if (put_crypt_ftr_and_key(&crypt_ftr)) {
1519 SLOGE("Failed to set crypto footer - panic");
1520 return;
1521 }
1522}
1523
1524static void cryptfs_trigger_restart_min_framework()
1525{
1526 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
1527 SLOGE("Failed to mount tmpfs on data - panic");
1528 return;
1529 }
1530
1531 if (property_set("vold.decrypt", "trigger_post_fs_data")) {
1532 SLOGE("Failed to trigger post fs data - panic");
1533 return;
1534 }
1535
1536 if (property_set("vold.decrypt", "trigger_restart_min_framework")) {
1537 SLOGE("Failed to trigger restart min framework - panic");
1538 return;
1539 }
1540}
1541
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001542/* returns < 0 on failure */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001543static int cryptfs_restart_internal(int restart_main)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001544{
1545 char fs_type[32];
1546 char real_blkdev[MAXPATHLEN];
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001547 char crypto_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001548 char fs_options[256];
1549 unsigned long mnt_flags;
1550 struct stat statbuf;
1551 int rc = -1, i;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001552 static int restart_successful = 0;
1553
1554 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -06001555 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001556 SLOGE("Encrypted filesystem not validated, aborting");
1557 return -1;
1558 }
1559
1560 if (restart_successful) {
1561 SLOGE("System already restarted with encrypted disk, aborting");
1562 return -1;
1563 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001564
Paul Lawrencef4faa572014-01-29 13:31:03 -08001565 if (restart_main) {
1566 /* Here is where we shut down the framework. The init scripts
1567 * start all services in one of three classes: core, main or late_start.
1568 * On boot, we start core and main. Now, we stop main, but not core,
1569 * as core includes vold and a few other really important things that
1570 * we need to keep running. Once main has stopped, we should be able
1571 * to umount the tmpfs /data, then mount the encrypted /data.
1572 * We then restart the class main, and also the class late_start.
1573 * At the moment, I've only put a few things in late_start that I know
1574 * are not needed to bring up the framework, and that also cause problems
1575 * with unmounting the tmpfs /data, but I hope to add add more services
1576 * to the late_start class as we optimize this to decrease the delay
1577 * till the user is asked for the password to the filesystem.
1578 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001579
Paul Lawrencef4faa572014-01-29 13:31:03 -08001580 /* The init files are setup to stop the class main when vold.decrypt is
1581 * set to trigger_reset_main.
1582 */
1583 property_set("vold.decrypt", "trigger_reset_main");
1584 SLOGD("Just asked init to shut down class main\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001585
Paul Lawrencef4faa572014-01-29 13:31:03 -08001586 /* Ugh, shutting down the framework is not synchronous, so until it
1587 * can be fixed, this horrible hack will wait a moment for it all to
1588 * shut down before proceeding. Without it, some devices cannot
1589 * restart the graphics services.
1590 */
1591 sleep(2);
1592 }
Ken Sumrall9dedfd42012-10-09 14:16:59 -07001593
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001594 /* Now that the framework is shutdown, we should be able to umount()
1595 * the tmpfs filesystem, and mount the real one.
1596 */
1597
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001598 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1599 if (strlen(crypto_blkdev) == 0) {
1600 SLOGE("fs_crypto_blkdev not set\n");
1601 return -1;
1602 }
1603
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001604 if (! (rc = wait_and_unmount(DATA_MNT_POINT, true)) ) {
Doug Zongker6fd57712013-12-17 09:43:23 -08001605 /* If ro.crypto.readonly is set to 1, mount the decrypted
1606 * filesystem readonly. This is used when /data is mounted by
1607 * recovery mode.
1608 */
1609 char ro_prop[PROPERTY_VALUE_MAX];
1610 property_get("ro.crypto.readonly", ro_prop, "");
1611 if (strlen(ro_prop) > 0 && atoi(ro_prop)) {
1612 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
1613 rec->flags |= MS_RDONLY;
1614 }
JP Abgrall62c7af32014-06-16 13:01:23 -07001615
Ken Sumralle5032c42012-04-01 23:58:44 -07001616 /* If that succeeded, then mount the decrypted filesystem */
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001617 int retries = RETRY_MOUNT_ATTEMPTS;
1618 int mount_rc;
1619 while ((mount_rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT,
1620 crypto_blkdev, 0))
1621 != 0) {
1622 if (mount_rc == FS_MGR_DOMNT_BUSY) {
1623 /* TODO: invoke something similar to
1624 Process::killProcessWithOpenFiles(DATA_MNT_POINT,
1625 retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */
1626 SLOGI("Failed to mount %s because it is busy - waiting",
1627 crypto_blkdev);
1628 if (--retries) {
1629 sleep(RETRY_MOUNT_DELAY_SECONDS);
1630 } else {
1631 /* Let's hope that a reboot clears away whatever is keeping
1632 the mount busy */
1633 cryptfs_reboot(reboot);
1634 }
1635 } else {
1636 SLOGE("Failed to mount decrypted data");
1637 cryptfs_set_corrupt();
1638 cryptfs_trigger_restart_min_framework();
1639 SLOGI("Started framework to offer wipe");
1640 return -1;
1641 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001642 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001643
Ken Sumralle5032c42012-04-01 23:58:44 -07001644 property_set("vold.decrypt", "trigger_load_persist_props");
1645 /* Create necessary paths on /data */
1646 if (prep_data_fs()) {
1647 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001648 }
Ken Sumralle5032c42012-04-01 23:58:44 -07001649
1650 /* startup service classes main and late_start */
1651 property_set("vold.decrypt", "trigger_restart_framework");
1652 SLOGD("Just triggered restart_framework\n");
1653
1654 /* Give it a few moments to get started */
1655 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001656 }
1657
Ken Sumrall0cc16632011-01-18 20:32:26 -08001658 if (rc == 0) {
1659 restart_successful = 1;
1660 }
1661
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001662 return rc;
1663}
1664
Paul Lawrencef4faa572014-01-29 13:31:03 -08001665int cryptfs_restart(void)
1666{
1667 /* Call internal implementation forcing a restart of main service group */
1668 return cryptfs_restart_internal(1);
1669}
1670
Mark Salyzyn3e971272014-01-21 13:27:04 -08001671static int do_crypto_complete(char *mount_point UNUSED)
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001672{
1673 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001674 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -08001675 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001676
1677 property_get("ro.crypto.state", encrypted_state, "");
1678 if (strcmp(encrypted_state, "encrypted") ) {
1679 SLOGE("not running with encryption, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001680 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001681 }
1682
Ken Sumrall160b4d62013-04-22 12:15:39 -07001683 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001684 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumralle5032c42012-04-01 23:58:44 -07001685
Ken Sumralle1a45852011-12-14 21:24:27 -08001686 /*
1687 * Only report this error if key_loc is a file and it exists.
1688 * If the device was never encrypted, and /data is not mountable for
1689 * some reason, returning 1 should prevent the UI from presenting the
1690 * a "enter password" screen, or worse, a "press button to wipe the
1691 * device" screen.
1692 */
1693 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
1694 SLOGE("master key file does not exist, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001695 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumralle1a45852011-12-14 21:24:27 -08001696 } else {
1697 SLOGE("Error getting crypt footer and key\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001698 return CRYPTO_COMPLETE_BAD_METADATA;
Ken Sumralle1a45852011-12-14 21:24:27 -08001699 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001700 }
1701
Paul Lawrence74f29f12014-08-28 15:54:10 -07001702 // Test for possible error flags
1703 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS){
1704 SLOGE("Encryption process is partway completed\n");
1705 return CRYPTO_COMPLETE_PARTIAL;
1706 }
1707
1708 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE){
1709 SLOGE("Encryption process was interrupted but cannot continue\n");
1710 return CRYPTO_COMPLETE_INCONSISTENT;
1711 }
1712
1713 if (crypt_ftr.flags & CRYPT_DATA_CORRUPT){
1714 SLOGE("Encryption is successful but data is corrupt\n");
1715 return CRYPTO_COMPLETE_CORRUPT;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001716 }
1717
1718 /* We passed the test! We shall diminish, and return to the west */
Paul Lawrence74f29f12014-08-28 15:54:10 -07001719 return CRYPTO_COMPLETE_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001720}
1721
Paul Lawrencef4faa572014-01-29 13:31:03 -08001722static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
1723 char *passwd, char *mount_point, char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001724{
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001725 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001726 unsigned char decrypted_master_key[32];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001727 char crypto_blkdev[MAXPATHLEN];
1728 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001729 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001730 unsigned int orig_failed_decrypt_count;
1731 int rc;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001732 kdf_func kdf;
1733 void *kdf_params;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001734 int use_keymaster = 0;
1735 int upgrade = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001736 unsigned char* intermediate_key = 0;
1737 size_t intermediate_key_size = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001738
Paul Lawrencef4faa572014-01-29 13:31:03 -08001739 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1740 orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001741
Paul Lawrencef4faa572014-01-29 13:31:03 -08001742 if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001743 if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr,
1744 &intermediate_key, &intermediate_key_size)) {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001745 SLOGE("Failed to decrypt master key\n");
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001746 rc = -1;
1747 goto errout;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001748 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001749 }
1750
Paul Lawrencef4faa572014-01-29 13:31:03 -08001751 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
1752
Paul Lawrence74f29f12014-08-28 15:54:10 -07001753 // Create crypto block device - all (non fatal) code paths
1754 // need it
Paul Lawrencef4faa572014-01-29 13:31:03 -08001755 if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key,
1756 real_blkdev, crypto_blkdev, label)) {
Paul Lawrence74f29f12014-08-28 15:54:10 -07001757 SLOGE("Error creating decrypted block device\n");
1758 rc = -1;
1759 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001760 }
1761
Paul Lawrence74f29f12014-08-28 15:54:10 -07001762 /* Work out if the problem is the password or the data */
1763 unsigned char scrypted_intermediate_key[sizeof(crypt_ftr->
1764 scrypted_intermediate_key)];
1765 int N = 1 << crypt_ftr->N_factor;
1766 int r = 1 << crypt_ftr->r_factor;
1767 int p = 1 << crypt_ftr->p_factor;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001768
Paul Lawrence74f29f12014-08-28 15:54:10 -07001769 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
1770 crypt_ftr->salt, sizeof(crypt_ftr->salt),
1771 N, r, p, scrypted_intermediate_key,
1772 sizeof(scrypted_intermediate_key));
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001773
Paul Lawrence74f29f12014-08-28 15:54:10 -07001774 // Does the key match the crypto footer?
1775 if (rc == 0 && memcmp(scrypted_intermediate_key,
1776 crypt_ftr->scrypted_intermediate_key,
1777 sizeof(scrypted_intermediate_key)) == 0) {
1778 SLOGI("Password matches");
1779 rc = 0;
1780 } else {
1781 /* Try mounting the file system anyway, just in case the problem's with
1782 * the footer, not the key. */
1783 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
1784 mkdir(tmp_mount_point, 0755);
1785 if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
1786 SLOGE("Error temp mounting decrypted block device\n");
1787 delete_crypto_blk_dev(label);
1788
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001789 rc = ++crypt_ftr->failed_decrypt_count;
1790 put_crypt_ftr_and_key(crypt_ftr);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001791 } else {
1792 /* Success! */
1793 SLOGI("Password did not match but decrypted drive mounted - continue");
1794 umount(tmp_mount_point);
1795 rc = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001796 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001797 }
1798
1799 if (rc == 0) {
1800 crypt_ftr->failed_decrypt_count = 0;
Paul Lawrence72b8b822014-10-05 12:57:37 -07001801 if (orig_failed_decrypt_count != 0) {
1802 put_crypt_ftr_and_key(crypt_ftr);
1803 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001804
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001805 /* Save the name of the crypto block device
Paul Lawrence74f29f12014-08-28 15:54:10 -07001806 * so we can mount it when restarting the framework. */
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001807 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -06001808
1809 /* Also save a the master key so we can reencrypted the key
Paul Lawrence74f29f12014-08-28 15:54:10 -07001810 * the key when we want to change the password on it. */
Jason parks70a4b3f2011-01-28 10:10:47 -06001811 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001812 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -06001813 master_key_saved = 1;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001814 SLOGD("%s(): Master key saved\n", __FUNCTION__);
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001815 rc = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001816
Paul Lawrence74f29f12014-08-28 15:54:10 -07001817 // Upgrade if we're not using the latest KDF.
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001818 use_keymaster = keymaster_check_compatibility();
1819 if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Shawn Willden47ba10d2014-09-03 17:07:06 -06001820 // Don't allow downgrade
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001821 } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) {
1822 crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1823 upgrade = 1;
1824 } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001825 crypt_ftr->kdf_type = KDF_SCRYPT;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001826 upgrade = 1;
1827 }
1828
1829 if (upgrade) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001830 rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1831 crypt_ftr->master_key, crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001832 if (!rc) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001833 rc = put_crypt_ftr_and_key(crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001834 }
1835 SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
Paul Lawrenceb2f682b2014-09-08 11:28:19 -07001836
1837 // Do not fail even if upgrade failed - machine is bootable
1838 // Note that if this code is ever hit, there is a *serious* problem
1839 // since KDFs should never fail. You *must* fix the kdf before
1840 // proceeding!
1841 if (rc) {
1842 SLOGW("Upgrade failed with error %d,"
1843 " but continuing with previous state",
1844 rc);
1845 rc = 0;
1846 }
JP Abgrall7bdfa522013-11-15 13:42:56 -08001847 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001848 }
1849
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001850 errout:
1851 if (intermediate_key) {
1852 memset(intermediate_key, 0, intermediate_key_size);
1853 free(intermediate_key);
1854 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001855 return rc;
1856}
1857
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001858/* Called by vold when it wants to undo the crypto mapping of a volume it
1859 * manages. This is usually in response to a factory reset, when we want
1860 * to undo the crypto mapping so the volume is formatted in the clear.
1861 */
1862int cryptfs_revert_volume(const char *label)
1863{
1864 return delete_crypto_blk_dev((char *)label);
1865}
1866
Ken Sumrall29d8da82011-05-18 17:20:07 -07001867/*
1868 * Called by vold when it's asked to mount an encrypted, nonremovable volume.
1869 * Setup a dm-crypt mapping, use the saved master key from
1870 * setting up the /data mapping, and return the new device path.
1871 */
1872int cryptfs_setup_volume(const char *label, int major, int minor,
1873 char *crypto_sys_path, unsigned int max_path,
1874 int *new_major, int *new_minor)
1875{
1876 char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN];
1877 struct crypt_mnt_ftr sd_crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001878 struct stat statbuf;
1879 int nr_sec, fd;
1880
1881 sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor);
1882
Ken Sumrall160b4d62013-04-22 12:15:39 -07001883 get_crypt_ftr_and_key(&sd_crypt_ftr);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001884
1885 /* Update the fs_size field to be the size of the volume */
1886 fd = open(real_blkdev, O_RDONLY);
1887 nr_sec = get_blkdev_size(fd);
1888 close(fd);
1889 if (nr_sec == 0) {
1890 SLOGE("Cannot get size of volume %s\n", real_blkdev);
1891 return -1;
1892 }
1893
1894 sd_crypt_ftr.fs_size = nr_sec;
1895 create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev,
1896 crypto_blkdev, label);
1897
JP Abgrall3334c6a2014-10-10 15:52:11 -07001898 if (stat(crypto_blkdev, &statbuf) < 0) {
1899 SLOGE("Error get stat for crypto_blkdev %s. err=%d(%s)\n",
1900 crypto_blkdev, errno, strerror(errno));
1901 }
Ken Sumrall29d8da82011-05-18 17:20:07 -07001902 *new_major = MAJOR(statbuf.st_rdev);
1903 *new_minor = MINOR(statbuf.st_rdev);
1904
1905 /* Create path to sys entry for this block device */
1906 snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1);
1907
1908 return 0;
1909}
1910
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001911int cryptfs_crypto_complete(void)
1912{
1913 return do_crypto_complete("/data");
1914}
1915
Paul Lawrencef4faa572014-01-29 13:31:03 -08001916int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr)
1917{
1918 char encrypted_state[PROPERTY_VALUE_MAX];
1919 property_get("ro.crypto.state", encrypted_state, "");
1920 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
1921 SLOGE("encrypted fs already validated or not running with encryption,"
1922 " aborting");
1923 return -1;
1924 }
1925
1926 if (get_crypt_ftr_and_key(crypt_ftr)) {
1927 SLOGE("Error getting crypt footer and key");
1928 return -1;
1929 }
1930
1931 return 0;
1932}
1933
Paul Lawrencefc615042014-10-04 15:32:29 -07001934/*
1935 * TODO - transition patterns to new format in calling code
1936 * and remove this vile hack, and the use of hex in
1937 * the password passing code.
1938 *
1939 * Patterns are passed in zero based (i.e. the top left dot
1940 * is represented by zero, the top middle one etc), but we want
1941 * to store them '1' based.
1942 * This is to allow us to migrate the calling code to use this
1943 * convention. It also solves a nasty problem whereby scrypt ignores
1944 * trailing zeros, so patterns ending at the top left could be
1945 * truncated, and similarly, you could add the top left to any
1946 * pattern and still match.
1947 * adjust_passwd is a hack function that returns the alternate representation
1948 * if the password appears to be a pattern (hex numbers all less than 09)
1949 * If it succeeds we need to try both, and in particular try the alternate
1950 * first. If the original matches, then we need to update the footer
1951 * with the alternate.
1952 * All code that accepts passwords must adjust them first. Since
1953 * cryptfs_check_passwd is always the first function called after a migration
1954 * (and indeed on any boot) we only need to do the double try in this
1955 * function.
1956 */
1957char* adjust_passwd(const char* passwd)
1958{
1959 size_t index, length;
1960
1961 if (!passwd) {
1962 return 0;
1963 }
1964
1965 // Check even length. Hex encoded passwords are always
1966 // an even length, since each character encodes to two characters.
1967 length = strlen(passwd);
1968 if (length % 2) {
1969 SLOGW("Password not correctly hex encoded.");
1970 return 0;
1971 }
1972
1973 // Check password is old-style pattern - a collection of hex
1974 // encoded bytes less than 9 (00 through 08)
1975 for (index = 0; index < length; index +=2) {
1976 if (passwd[index] != '0'
1977 || passwd[index + 1] < '0' || passwd[index + 1] > '8') {
1978 return 0;
1979 }
1980 }
1981
1982 // Allocate room for adjusted passwd and null terminate
1983 char* adjusted = malloc(length + 1);
1984 adjusted[length] = 0;
1985
1986 // Add 0x31 ('1') to each character
1987 for (index = 0; index < length; index += 2) {
1988 // output is 31 through 39 so set first byte to three, second to src + 1
1989 adjusted[index] = '3';
1990 adjusted[index + 1] = passwd[index + 1] + 1;
1991 }
1992
1993 return adjusted;
1994}
1995
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001996int cryptfs_check_passwd(char *passwd)
1997{
Paul Lawrencef4faa572014-01-29 13:31:03 -08001998 struct crypt_mnt_ftr crypt_ftr;
1999 int rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002000
Paul Lawrencef4faa572014-01-29 13:31:03 -08002001 rc = check_unmounted_and_get_ftr(&crypt_ftr);
2002 if (rc)
2003 return rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002004
Paul Lawrencefc615042014-10-04 15:32:29 -07002005 char* adjusted_passwd = adjust_passwd(passwd);
2006 if (adjusted_passwd) {
2007 int failed_decrypt_count = crypt_ftr.failed_decrypt_count;
2008 rc = test_mount_encrypted_fs(&crypt_ftr, adjusted_passwd,
2009 DATA_MNT_POINT, "userdata");
2010
2011 // Maybe the original one still works?
2012 if (rc) {
2013 // Don't double count this failure
2014 crypt_ftr.failed_decrypt_count = failed_decrypt_count;
2015 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
2016 DATA_MNT_POINT, "userdata");
2017 if (!rc) {
2018 // cryptfs_changepw also adjusts so pass original
2019 // Note that adjust_passwd only recognises patterns
2020 // so we can safely use CRYPT_TYPE_PATTERN
2021 SLOGI("Updating pattern to new format");
2022 cryptfs_changepw(CRYPT_TYPE_PATTERN, passwd);
2023 }
2024 }
2025 free(adjusted_passwd);
2026 } else {
2027 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
2028 DATA_MNT_POINT, "userdata");
2029 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002030
2031 if (rc == 0 && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
Paul Lawrence399317e2014-03-10 13:20:50 -07002032 cryptfs_clear_password();
2033 password = strdup(passwd);
2034 struct timespec now;
2035 clock_gettime(CLOCK_BOOTTIME, &now);
2036 password_expiry_time = now.tv_sec + password_max_age_seconds;
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002037 }
2038
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002039 return rc;
2040}
2041
Ken Sumrall3ad90722011-10-04 20:38:29 -07002042int cryptfs_verify_passwd(char *passwd)
2043{
2044 struct crypt_mnt_ftr crypt_ftr;
2045 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002046 unsigned char decrypted_master_key[32];
Ken Sumrall3ad90722011-10-04 20:38:29 -07002047 char encrypted_state[PROPERTY_VALUE_MAX];
2048 int rc;
2049
2050 property_get("ro.crypto.state", encrypted_state, "");
2051 if (strcmp(encrypted_state, "encrypted") ) {
2052 SLOGE("device not encrypted, aborting");
2053 return -2;
2054 }
2055
2056 if (!master_key_saved) {
2057 SLOGE("encrypted fs not yet mounted, aborting");
2058 return -1;
2059 }
2060
2061 if (!saved_mount_point) {
2062 SLOGE("encrypted fs failed to save mount point, aborting");
2063 return -1;
2064 }
2065
Ken Sumrall160b4d62013-04-22 12:15:39 -07002066 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall3ad90722011-10-04 20:38:29 -07002067 SLOGE("Error getting crypt footer and key\n");
2068 return -1;
2069 }
2070
2071 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
2072 /* If the device has no password, then just say the password is valid */
2073 rc = 0;
2074 } else {
Paul Lawrencefc615042014-10-04 15:32:29 -07002075 char* adjusted_passwd = adjust_passwd(passwd);
2076 if (adjusted_passwd) {
2077 passwd = adjusted_passwd;
2078 }
2079
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002080 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall3ad90722011-10-04 20:38:29 -07002081 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
2082 /* They match, the password is correct */
2083 rc = 0;
2084 } else {
2085 /* If incorrect, sleep for a bit to prevent dictionary attacks */
2086 sleep(1);
2087 rc = 1;
2088 }
Paul Lawrencefc615042014-10-04 15:32:29 -07002089
2090 free(adjusted_passwd);
Ken Sumrall3ad90722011-10-04 20:38:29 -07002091 }
2092
2093 return rc;
2094}
2095
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002096/* Initialize a crypt_mnt_ftr structure. The keysize is
2097 * defaulted to 16 bytes, and the filesystem size to 0.
2098 * Presumably, at a minimum, the caller will update the
2099 * filesystem size and crypto_type_name after calling this function.
2100 */
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002101static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002102{
Ken Sumrall160b4d62013-04-22 12:15:39 -07002103 off64_t off;
2104
2105 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002106 ftr->magic = CRYPT_MNT_MAGIC;
Kenny Rootc96a5f82013-06-14 12:08:28 -07002107 ftr->major_version = CURRENT_MAJOR_VERSION;
2108 ftr->minor_version = CURRENT_MINOR_VERSION;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002109 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Jason parks70a4b3f2011-01-28 10:10:47 -06002110 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002111
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002112 switch (keymaster_check_compatibility()) {
2113 case 1:
2114 ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
2115 break;
2116
2117 case 0:
2118 ftr->kdf_type = KDF_SCRYPT;
2119 break;
2120
2121 default:
2122 SLOGE("keymaster_check_compatibility failed");
2123 return -1;
2124 }
2125
Kenny Rootc4c70f12013-06-14 12:11:38 -07002126 get_device_scrypt_params(ftr);
2127
Ken Sumrall160b4d62013-04-22 12:15:39 -07002128 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
2129 if (get_crypt_ftr_info(NULL, &off) == 0) {
2130 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
2131 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
2132 ftr->persist_data_size;
2133 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002134
2135 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002136}
2137
Ken Sumrall29d8da82011-05-18 17:20:07 -07002138static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002139{
Ken Sumralle550f782013-08-20 13:48:23 -07002140 const char *args[10];
2141 char size_str[32]; /* Must be large enough to hold a %lld and null byte */
2142 int num_args;
2143 int status;
2144 int tmp;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002145 int rc = -1;
2146
Ken Sumrall29d8da82011-05-18 17:20:07 -07002147 if (type == EXT4_FS) {
Ken Sumralle550f782013-08-20 13:48:23 -07002148 args[0] = "/system/bin/make_ext4fs";
2149 args[1] = "-a";
2150 args[2] = "/data";
2151 args[3] = "-l";
Elliott Hughes73737162014-06-25 17:27:42 -07002152 snprintf(size_str, sizeof(size_str), "%" PRId64, size * 512);
Ken Sumralle550f782013-08-20 13:48:23 -07002153 args[4] = size_str;
2154 args[5] = crypto_blkdev;
2155 num_args = 6;
2156 SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n",
2157 args[0], args[1], args[2], args[3], args[4], args[5]);
JP Abgrall62c7af32014-06-16 13:01:23 -07002158 } else if (type == F2FS_FS) {
2159 args[0] = "/system/bin/mkfs.f2fs";
2160 args[1] = "-t";
2161 args[2] = "-d1";
2162 args[3] = crypto_blkdev;
Elliott Hughes73737162014-06-25 17:27:42 -07002163 snprintf(size_str, sizeof(size_str), "%" PRId64, size);
JP Abgrall62c7af32014-06-16 13:01:23 -07002164 args[4] = size_str;
2165 num_args = 5;
2166 SLOGI("Making empty filesystem with command %s %s %s %s %s\n",
2167 args[0], args[1], args[2], args[3], args[4]);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002168 } else {
2169 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
2170 return -1;
2171 }
2172
Ken Sumralle550f782013-08-20 13:48:23 -07002173 tmp = android_fork_execvp(num_args, (char **)args, &status, false, true);
2174
2175 if (tmp != 0) {
2176 SLOGE("Error creating empty filesystem on %s due to logwrap error\n", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002177 } else {
Ken Sumralle550f782013-08-20 13:48:23 -07002178 if (WIFEXITED(status)) {
2179 if (WEXITSTATUS(status)) {
2180 SLOGE("Error creating filesystem on %s, exit status %d ",
2181 crypto_blkdev, WEXITSTATUS(status));
2182 } else {
2183 SLOGD("Successfully created filesystem on %s\n", crypto_blkdev);
2184 rc = 0;
2185 }
2186 } else {
2187 SLOGE("Error creating filesystem on %s, did not exit normally\n", crypto_blkdev);
2188 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002189 }
2190
2191 return rc;
2192}
2193
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002194#define CRYPT_INPLACE_BUFSIZE 4096
Paul Lawrence87999172014-02-20 12:21:31 -08002195#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / CRYPT_SECTOR_SIZE)
2196#define CRYPT_SECTOR_SIZE 512
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002197
2198/* aligned 32K writes tends to make flash happy.
2199 * SD card association recommends it.
2200 */
2201#define BLOCKS_AT_A_TIME 8
2202
2203struct encryptGroupsData
2204{
2205 int realfd;
2206 int cryptofd;
2207 off64_t numblocks;
2208 off64_t one_pct, cur_pct, new_pct;
2209 off64_t blocks_already_done, tot_numblocks;
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002210 off64_t used_blocks_already_done, tot_used_blocks;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002211 char* real_blkdev, * crypto_blkdev;
2212 int count;
2213 off64_t offset;
2214 char* buffer;
Paul Lawrence87999172014-02-20 12:21:31 -08002215 off64_t last_written_sector;
2216 int completed;
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002217 time_t time_started;
2218 int remaining_time;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002219};
2220
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002221static void update_progress(struct encryptGroupsData* data, int is_used)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002222{
2223 data->blocks_already_done++;
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002224
2225 if (is_used) {
2226 data->used_blocks_already_done++;
2227 }
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002228 if (data->tot_used_blocks) {
2229 data->new_pct = data->used_blocks_already_done / data->one_pct;
2230 } else {
2231 data->new_pct = data->blocks_already_done / data->one_pct;
2232 }
2233
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002234 if (data->new_pct > data->cur_pct) {
2235 char buf[8];
2236 data->cur_pct = data->new_pct;
Elliott Hughescb33f572014-06-25 18:25:11 -07002237 snprintf(buf, sizeof(buf), "%" PRId64, data->cur_pct);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002238 property_set("vold.encrypt_progress", buf);
2239 }
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002240
2241 if (data->cur_pct >= 5) {
Paul Lawrence9c58a872014-09-30 09:12:51 -07002242 struct timespec time_now;
2243 if (clock_gettime(CLOCK_MONOTONIC, &time_now)) {
2244 SLOGW("Error getting time");
2245 } else {
2246 double elapsed_time = difftime(time_now.tv_sec, data->time_started);
2247 off64_t remaining_blocks = data->tot_used_blocks
2248 - data->used_blocks_already_done;
2249 int remaining_time = (int)(elapsed_time * remaining_blocks
2250 / data->used_blocks_already_done);
Paul Lawrence71577502014-08-13 14:55:55 -07002251
Paul Lawrence9c58a872014-09-30 09:12:51 -07002252 // Change time only if not yet set, lower, or a lot higher for
2253 // best user experience
2254 if (data->remaining_time == -1
2255 || remaining_time < data->remaining_time
2256 || remaining_time > data->remaining_time + 60) {
2257 char buf[8];
2258 snprintf(buf, sizeof(buf), "%d", remaining_time);
2259 property_set("vold.encrypt_time_remaining", buf);
2260 data->remaining_time = remaining_time;
2261 }
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002262 }
2263 }
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002264}
2265
Paul Lawrence3846be12014-09-22 11:33:54 -07002266static void log_progress(struct encryptGroupsData const* data, bool completed)
2267{
2268 // Precondition - if completed data = 0 else data != 0
2269
2270 // Track progress so we can skip logging blocks
2271 static off64_t offset = -1;
2272
2273 // Need to close existing 'Encrypting from' log?
2274 if (completed || (offset != -1 && data->offset != offset)) {
2275 SLOGI("Encrypted to sector %" PRId64,
2276 offset / info.block_size * CRYPT_SECTOR_SIZE);
2277 offset = -1;
2278 }
2279
2280 // Need to start new 'Encrypting from' log?
2281 if (!completed && offset != data->offset) {
2282 SLOGI("Encrypting from sector %" PRId64,
2283 data->offset / info.block_size * CRYPT_SECTOR_SIZE);
2284 }
2285
2286 // Update offset
2287 if (!completed) {
2288 offset = data->offset + (off64_t)data->count * info.block_size;
2289 }
2290}
2291
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002292static int flush_outstanding_data(struct encryptGroupsData* data)
2293{
2294 if (data->count == 0) {
2295 return 0;
2296 }
2297
Elliott Hughes231bdba2014-06-25 18:36:19 -07002298 SLOGV("Copying %d blocks at offset %" PRIx64, data->count, data->offset);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002299
2300 if (pread64(data->realfd, data->buffer,
2301 info.block_size * data->count, data->offset)
2302 <= 0) {
2303 SLOGE("Error reading real_blkdev %s for inplace encrypt",
2304 data->real_blkdev);
2305 return -1;
2306 }
2307
2308 if (pwrite64(data->cryptofd, data->buffer,
2309 info.block_size * data->count, data->offset)
2310 <= 0) {
2311 SLOGE("Error writing crypto_blkdev %s for inplace encrypt",
2312 data->crypto_blkdev);
2313 return -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002314 } else {
Paul Lawrence3846be12014-09-22 11:33:54 -07002315 log_progress(data, false);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002316 }
2317
2318 data->count = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002319 data->last_written_sector = (data->offset + data->count)
2320 / info.block_size * CRYPT_SECTOR_SIZE - 1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002321 return 0;
2322}
2323
2324static int encrypt_groups(struct encryptGroupsData* data)
2325{
2326 unsigned int i;
2327 u8 *block_bitmap = 0;
2328 unsigned int block;
2329 off64_t ret;
2330 int rc = -1;
2331
2332 data->buffer = malloc(info.block_size * BLOCKS_AT_A_TIME);
2333 if (!data->buffer) {
2334 SLOGE("Failed to allocate crypto buffer");
2335 goto errout;
2336 }
2337
2338 block_bitmap = malloc(info.block_size);
2339 if (!block_bitmap) {
2340 SLOGE("failed to allocate block bitmap");
2341 goto errout;
2342 }
2343
2344 for (i = 0; i < aux_info.groups; ++i) {
2345 SLOGI("Encrypting group %d", i);
2346
2347 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
2348 u32 block_count = min(info.blocks_per_group,
2349 aux_info.len_blocks - first_block);
2350
2351 off64_t offset = (u64)info.block_size
2352 * aux_info.bg_desc[i].bg_block_bitmap;
2353
2354 ret = pread64(data->realfd, block_bitmap, info.block_size, offset);
2355 if (ret != (int)info.block_size) {
2356 SLOGE("failed to read all of block group bitmap %d", i);
2357 goto errout;
2358 }
2359
2360 offset = (u64)info.block_size * first_block;
2361
2362 data->count = 0;
2363
2364 for (block = 0; block < block_count; block++) {
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002365 int used = bitmap_get_bit(block_bitmap, block);
2366 update_progress(data, used);
2367 if (used) {
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002368 if (data->count == 0) {
2369 data->offset = offset;
2370 }
2371 data->count++;
2372 } else {
2373 if (flush_outstanding_data(data)) {
2374 goto errout;
2375 }
2376 }
2377
2378 offset += info.block_size;
2379
2380 /* Write data if we are aligned or buffer size reached */
2381 if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0
2382 || data->count == BLOCKS_AT_A_TIME) {
2383 if (flush_outstanding_data(data)) {
2384 goto errout;
2385 }
2386 }
Paul Lawrence87999172014-02-20 12:21:31 -08002387
Paul Lawrence73d7a022014-06-09 14:10:09 -07002388 if (!is_battery_ok_to_continue()) {
Paul Lawrence87999172014-02-20 12:21:31 -08002389 SLOGE("Stopping encryption due to low battery");
2390 rc = 0;
2391 goto errout;
2392 }
2393
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002394 }
2395 if (flush_outstanding_data(data)) {
2396 goto errout;
2397 }
2398 }
2399
Paul Lawrence87999172014-02-20 12:21:31 -08002400 data->completed = 1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002401 rc = 0;
2402
2403errout:
Paul Lawrence3846be12014-09-22 11:33:54 -07002404 log_progress(0, true);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002405 free(data->buffer);
2406 free(block_bitmap);
2407 return rc;
2408}
2409
2410static int cryptfs_enable_inplace_ext4(char *crypto_blkdev,
2411 char *real_blkdev,
2412 off64_t size,
2413 off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002414 off64_t tot_size,
2415 off64_t previously_encrypted_upto)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002416{
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002417 u32 i;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002418 struct encryptGroupsData data;
Paul Lawrence74f29f12014-08-28 15:54:10 -07002419 int rc; // Can't initialize without causing warning -Wclobbered
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002420
Paul Lawrence87999172014-02-20 12:21:31 -08002421 if (previously_encrypted_upto > *size_already_done) {
2422 SLOGD("Not fast encrypting since resuming part way through");
2423 return -1;
2424 }
2425
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002426 memset(&data, 0, sizeof(data));
2427 data.real_blkdev = real_blkdev;
2428 data.crypto_blkdev = crypto_blkdev;
2429
2430 if ( (data.realfd = open(real_blkdev, O_RDWR)) < 0) {
JP Abgrall3334c6a2014-10-10 15:52:11 -07002431 SLOGE("Error opening real_blkdev %s for inplace encrypt. err=%d(%s)\n",
2432 real_blkdev, errno, strerror(errno));
Paul Lawrence74f29f12014-08-28 15:54:10 -07002433 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002434 goto errout;
2435 }
2436
2437 if ( (data.cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002438 SLOGE("Error opening crypto_blkdev %s for ext4 inplace encrypt. err=%d(%s)\n",
JP Abgrall3334c6a2014-10-10 15:52:11 -07002439 crypto_blkdev, errno, strerror(errno));
JP Abgrall7fc1de82014-10-10 18:43:41 -07002440 rc = ENABLE_INPLACE_ERR_DEV;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002441 goto errout;
2442 }
2443
2444 if (setjmp(setjmp_env)) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002445 SLOGE("Reading ext4 extent caused an exception\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07002446 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002447 goto errout;
2448 }
2449
2450 if (read_ext(data.realfd, 0) != 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002451 SLOGE("Failed to read ext4 extent\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07002452 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002453 goto errout;
2454 }
2455
2456 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2457 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2458 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
2459
JP Abgrall7fc1de82014-10-10 18:43:41 -07002460 SLOGI("Encrypting ext4 filesystem in place...");
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002461
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002462 data.tot_used_blocks = data.numblocks;
2463 for (i = 0; i < aux_info.groups; ++i) {
2464 data.tot_used_blocks -= aux_info.bg_desc[i].bg_free_blocks_count;
2465 }
2466
2467 data.one_pct = data.tot_used_blocks / 100;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002468 data.cur_pct = 0;
Paul Lawrence9c58a872014-09-30 09:12:51 -07002469
2470 struct timespec time_started = {0};
2471 if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
2472 SLOGW("Error getting time at start");
2473 // Note - continue anyway - we'll run with 0
2474 }
2475 data.time_started = time_started.tv_sec;
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002476 data.remaining_time = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002477
2478 rc = encrypt_groups(&data);
2479 if (rc) {
2480 SLOGE("Error encrypting groups");
2481 goto errout;
2482 }
2483
Paul Lawrence87999172014-02-20 12:21:31 -08002484 *size_already_done += data.completed ? size : data.last_written_sector;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002485 rc = 0;
2486
2487errout:
2488 close(data.realfd);
2489 close(data.cryptofd);
2490
2491 return rc;
2492}
2493
Paul Lawrence3846be12014-09-22 11:33:54 -07002494static void log_progress_f2fs(u64 block, bool completed)
2495{
2496 // Precondition - if completed data = 0 else data != 0
2497
2498 // Track progress so we can skip logging blocks
2499 static u64 last_block = (u64)-1;
2500
2501 // Need to close existing 'Encrypting from' log?
2502 if (completed || (last_block != (u64)-1 && block != last_block + 1)) {
2503 SLOGI("Encrypted to block %" PRId64, last_block);
2504 last_block = -1;
2505 }
2506
2507 // Need to start new 'Encrypting from' log?
2508 if (!completed && (last_block == (u64)-1 || block != last_block + 1)) {
2509 SLOGI("Encrypting from block %" PRId64, block);
2510 }
2511
2512 // Update offset
2513 if (!completed) {
2514 last_block = block;
2515 }
2516}
2517
Daniel Rosenberge82df162014-08-15 22:19:23 +00002518static int encrypt_one_block_f2fs(u64 pos, void *data)
2519{
2520 struct encryptGroupsData *priv_dat = (struct encryptGroupsData *)data;
2521
2522 priv_dat->blocks_already_done = pos - 1;
2523 update_progress(priv_dat, 1);
2524
2525 off64_t offset = pos * CRYPT_INPLACE_BUFSIZE;
2526
2527 if (pread64(priv_dat->realfd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002528 SLOGE("Error reading real_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002529 return -1;
2530 }
2531
2532 if (pwrite64(priv_dat->cryptofd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002533 SLOGE("Error writing crypto_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002534 return -1;
2535 } else {
Paul Lawrence3846be12014-09-22 11:33:54 -07002536 log_progress_f2fs(pos, false);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002537 }
2538
2539 return 0;
2540}
2541
2542static int cryptfs_enable_inplace_f2fs(char *crypto_blkdev,
2543 char *real_blkdev,
2544 off64_t size,
2545 off64_t *size_already_done,
2546 off64_t tot_size,
2547 off64_t previously_encrypted_upto)
2548{
2549 u32 i;
2550 struct encryptGroupsData data;
2551 struct f2fs_info *f2fs_info = NULL;
JP Abgrall7fc1de82014-10-10 18:43:41 -07002552 int rc = ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002553 if (previously_encrypted_upto > *size_already_done) {
2554 SLOGD("Not fast encrypting since resuming part way through");
JP Abgrall7fc1de82014-10-10 18:43:41 -07002555 return ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002556 }
2557 memset(&data, 0, sizeof(data));
2558 data.real_blkdev = real_blkdev;
2559 data.crypto_blkdev = crypto_blkdev;
2560 data.realfd = -1;
2561 data.cryptofd = -1;
2562 if ( (data.realfd = open64(real_blkdev, O_RDWR)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002563 SLOGE("Error opening real_blkdev %s for f2fs inplace encrypt\n",
Daniel Rosenberge82df162014-08-15 22:19:23 +00002564 real_blkdev);
2565 goto errout;
2566 }
2567 if ( (data.cryptofd = open64(crypto_blkdev, O_WRONLY)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002568 SLOGE("Error opening crypto_blkdev %s for f2fs inplace encrypt. err=%d(%s)\n",
JP Abgrall3334c6a2014-10-10 15:52:11 -07002569 crypto_blkdev, errno, strerror(errno));
JP Abgrall7fc1de82014-10-10 18:43:41 -07002570 rc = ENABLE_INPLACE_ERR_DEV;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002571 goto errout;
2572 }
2573
2574 f2fs_info = generate_f2fs_info(data.realfd);
2575 if (!f2fs_info)
2576 goto errout;
2577
2578 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2579 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2580 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
2581
2582 data.tot_used_blocks = get_num_blocks_used(f2fs_info);
2583
2584 data.one_pct = data.tot_used_blocks / 100;
2585 data.cur_pct = 0;
2586 data.time_started = time(NULL);
2587 data.remaining_time = -1;
2588
2589 data.buffer = malloc(f2fs_info->block_size);
2590 if (!data.buffer) {
2591 SLOGE("Failed to allocate crypto buffer");
2592 goto errout;
2593 }
2594
2595 data.count = 0;
2596
2597 /* Currently, this either runs to completion, or hits a nonrecoverable error */
2598 rc = run_on_used_blocks(data.blocks_already_done, f2fs_info, &encrypt_one_block_f2fs, &data);
2599
2600 if (rc) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002601 SLOGE("Error in running over f2fs blocks");
2602 rc = ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002603 goto errout;
2604 }
2605
2606 *size_already_done += size;
2607 rc = 0;
2608
2609errout:
2610 if (rc)
2611 SLOGE("Failed to encrypt f2fs filesystem on %s", real_blkdev);
2612
Paul Lawrence3846be12014-09-22 11:33:54 -07002613 log_progress_f2fs(0, true);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002614 free(f2fs_info);
2615 free(data.buffer);
2616 close(data.realfd);
2617 close(data.cryptofd);
2618
2619 return rc;
2620}
2621
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002622static int cryptfs_enable_inplace_full(char *crypto_blkdev, char *real_blkdev,
2623 off64_t size, off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002624 off64_t tot_size,
2625 off64_t previously_encrypted_upto)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002626{
2627 int realfd, cryptofd;
2628 char *buf[CRYPT_INPLACE_BUFSIZE];
JP Abgrall7fc1de82014-10-10 18:43:41 -07002629 int rc = ENABLE_INPLACE_ERR_OTHER;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002630 off64_t numblocks, i, remainder;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002631 off64_t one_pct, cur_pct, new_pct;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002632 off64_t blocks_already_done, tot_numblocks;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002633
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002634 if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) {
2635 SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002636 return ENABLE_INPLACE_ERR_OTHER;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002637 }
2638
2639 if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
JP Abgrall3334c6a2014-10-10 15:52:11 -07002640 SLOGE("Error opening crypto_blkdev %s for inplace encrypt. err=%d(%s)\n",
2641 crypto_blkdev, errno, strerror(errno));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002642 close(realfd);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002643 return ENABLE_INPLACE_ERR_DEV;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002644 }
2645
2646 /* This is pretty much a simple loop of reading 4K, and writing 4K.
2647 * The size passed in is the number of 512 byte sectors in the filesystem.
2648 * So compute the number of whole 4K blocks we should read/write,
2649 * and the remainder.
2650 */
2651 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2652 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002653 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2654 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002655
2656 SLOGE("Encrypting filesystem in place...");
2657
Paul Lawrence87999172014-02-20 12:21:31 -08002658 i = previously_encrypted_upto + 1 - *size_already_done;
2659
2660 if (lseek64(realfd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
2661 SLOGE("Cannot seek to previously encrypted point on %s", real_blkdev);
2662 goto errout;
2663 }
2664
2665 if (lseek64(cryptofd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
2666 SLOGE("Cannot seek to previously encrypted point on %s", crypto_blkdev);
2667 goto errout;
2668 }
2669
2670 for (;i < size && i % CRYPT_SECTORS_PER_BUFSIZE != 0; ++i) {
2671 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2672 SLOGE("Error reading initial sectors from real_blkdev %s for "
2673 "inplace encrypt\n", crypto_blkdev);
2674 goto errout;
2675 }
2676 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2677 SLOGE("Error writing initial sectors to crypto_blkdev %s for "
2678 "inplace encrypt\n", crypto_blkdev);
2679 goto errout;
2680 } else {
Elliott Hughescb33f572014-06-25 18:25:11 -07002681 SLOGI("Encrypted 1 block at %" PRId64, i);
Paul Lawrence87999172014-02-20 12:21:31 -08002682 }
2683 }
2684
Ken Sumrall29d8da82011-05-18 17:20:07 -07002685 one_pct = tot_numblocks / 100;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002686 cur_pct = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002687 /* process the majority of the filesystem in blocks */
Paul Lawrence87999172014-02-20 12:21:31 -08002688 for (i/=CRYPT_SECTORS_PER_BUFSIZE; i<numblocks; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07002689 new_pct = (i + blocks_already_done) / one_pct;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002690 if (new_pct > cur_pct) {
2691 char buf[8];
2692
2693 cur_pct = new_pct;
Elliott Hughes73737162014-06-25 17:27:42 -07002694 snprintf(buf, sizeof(buf), "%" PRId64, cur_pct);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002695 property_set("vold.encrypt_progress", buf);
2696 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002697 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Lawrence87999172014-02-20 12:21:31 -08002698 SLOGE("Error reading real_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002699 goto errout;
2700 }
2701 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Lawrence87999172014-02-20 12:21:31 -08002702 SLOGE("Error writing crypto_blkdev %s for inplace encrypt", crypto_blkdev);
2703 goto errout;
2704 } else {
Elliott Hughescb33f572014-06-25 18:25:11 -07002705 SLOGD("Encrypted %d block at %" PRId64,
Paul Lawrence87999172014-02-20 12:21:31 -08002706 CRYPT_SECTORS_PER_BUFSIZE,
2707 i * CRYPT_SECTORS_PER_BUFSIZE);
2708 }
2709
Paul Lawrence73d7a022014-06-09 14:10:09 -07002710 if (!is_battery_ok_to_continue()) {
Paul Lawrence87999172014-02-20 12:21:31 -08002711 SLOGE("Stopping encryption due to low battery");
2712 *size_already_done += (i + 1) * CRYPT_SECTORS_PER_BUFSIZE - 1;
2713 rc = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002714 goto errout;
2715 }
2716 }
2717
2718 /* Do any remaining sectors */
2719 for (i=0; i<remainder; i++) {
Paul Lawrence87999172014-02-20 12:21:31 -08002720 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2721 SLOGE("Error reading final sectors from real_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002722 goto errout;
2723 }
Paul Lawrence87999172014-02-20 12:21:31 -08002724 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2725 SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002726 goto errout;
Paul Lawrence87999172014-02-20 12:21:31 -08002727 } else {
2728 SLOGI("Encrypted 1 block at next location");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002729 }
2730 }
2731
Ken Sumrall29d8da82011-05-18 17:20:07 -07002732 *size_already_done += size;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002733 rc = 0;
2734
2735errout:
2736 close(realfd);
2737 close(cryptofd);
2738
2739 return rc;
2740}
2741
JP Abgrall7fc1de82014-10-10 18:43:41 -07002742/* returns on of the ENABLE_INPLACE_* return codes */
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002743static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev,
2744 off64_t size, off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002745 off64_t tot_size,
2746 off64_t previously_encrypted_upto)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002747{
JP Abgrall7fc1de82014-10-10 18:43:41 -07002748 int rc_ext4, rc_f2fs, rc_full;
Paul Lawrence87999172014-02-20 12:21:31 -08002749 if (previously_encrypted_upto) {
Elliott Hughescb33f572014-06-25 18:25:11 -07002750 SLOGD("Continuing encryption from %" PRId64, previously_encrypted_upto);
Paul Lawrence87999172014-02-20 12:21:31 -08002751 }
2752
2753 if (*size_already_done + size < previously_encrypted_upto) {
2754 *size_already_done += size;
2755 return 0;
2756 }
2757
Daniel Rosenberge82df162014-08-15 22:19:23 +00002758 /* TODO: identify filesystem type.
2759 * As is, cryptfs_enable_inplace_ext4 will fail on an f2fs partition, and
2760 * then we will drop down to cryptfs_enable_inplace_f2fs.
2761 * */
JP Abgrall7fc1de82014-10-10 18:43:41 -07002762 if ((rc_ext4 = cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev,
Daniel Rosenberge82df162014-08-15 22:19:23 +00002763 size, size_already_done,
JP Abgrall7fc1de82014-10-10 18:43:41 -07002764 tot_size, previously_encrypted_upto)) == 0) {
Daniel Rosenberge82df162014-08-15 22:19:23 +00002765 return 0;
2766 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002767 SLOGD("cryptfs_enable_inplace_ext4()=%d\n", rc_ext4);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002768
JP Abgrall7fc1de82014-10-10 18:43:41 -07002769 if ((rc_f2fs = cryptfs_enable_inplace_f2fs(crypto_blkdev, real_blkdev,
Daniel Rosenberge82df162014-08-15 22:19:23 +00002770 size, size_already_done,
JP Abgrall7fc1de82014-10-10 18:43:41 -07002771 tot_size, previously_encrypted_upto)) == 0) {
Daniel Rosenberge82df162014-08-15 22:19:23 +00002772 return 0;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002773 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002774 SLOGD("cryptfs_enable_inplace_f2fs()=%d\n", rc_f2fs);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002775
JP Abgrall7fc1de82014-10-10 18:43:41 -07002776 rc_full = cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev,
Paul Lawrence87999172014-02-20 12:21:31 -08002777 size, size_already_done, tot_size,
2778 previously_encrypted_upto);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002779 SLOGD("cryptfs_enable_inplace_full()=%d\n", rc_full);
2780
2781 /* Hack for b/17898962, the following is the symptom... */
2782 if (rc_ext4 == ENABLE_INPLACE_ERR_DEV
2783 && rc_f2fs == ENABLE_INPLACE_ERR_DEV
2784 && rc_full == ENABLE_INPLACE_ERR_DEV) {
2785 return ENABLE_INPLACE_ERR_DEV;
2786 }
2787 return rc_full;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002788}
2789
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002790#define CRYPTO_ENABLE_WIPE 1
2791#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002792
2793#define FRAMEWORK_BOOT_WAIT 60
2794
Ken Sumrall29d8da82011-05-18 17:20:07 -07002795static inline int should_encrypt(struct volume_info *volume)
2796{
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002797 return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) ==
Ken Sumrall29d8da82011-05-18 17:20:07 -07002798 (VOL_ENCRYPTABLE | VOL_NONREMOVABLE);
2799}
2800
Paul Lawrence87999172014-02-20 12:21:31 -08002801static int cryptfs_SHA256_fileblock(const char* filename, __le8* buf)
2802{
2803 int fd = open(filename, O_RDONLY);
2804 if (fd == -1) {
2805 SLOGE("Error opening file %s", filename);
2806 return -1;
2807 }
2808
2809 char block[CRYPT_INPLACE_BUFSIZE];
2810 memset(block, 0, sizeof(block));
2811 if (unix_read(fd, block, sizeof(block)) < 0) {
2812 SLOGE("Error reading file %s", filename);
2813 close(fd);
2814 return -1;
2815 }
2816
2817 close(fd);
2818
2819 SHA256_CTX c;
2820 SHA256_Init(&c);
2821 SHA256_Update(&c, block, sizeof(block));
2822 SHA256_Final(buf, &c);
2823
2824 return 0;
2825}
2826
JP Abgrall62c7af32014-06-16 13:01:23 -07002827static int get_fs_type(struct fstab_rec *rec)
2828{
2829 if (!strcmp(rec->fs_type, "ext4")) {
2830 return EXT4_FS;
2831 } else if (!strcmp(rec->fs_type, "f2fs")) {
2832 return F2FS_FS;
2833 } else {
2834 return -1;
2835 }
2836}
2837
Paul Lawrence87999172014-02-20 12:21:31 -08002838static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr *crypt_ftr, int how,
2839 char *crypto_blkdev, char *real_blkdev,
2840 int previously_encrypted_upto)
2841{
2842 off64_t cur_encryption_done=0, tot_encryption_size=0;
2843 int i, rc = -1;
2844
Paul Lawrence73d7a022014-06-09 14:10:09 -07002845 if (!is_battery_ok_to_start()) {
2846 SLOGW("Not starting encryption due to low battery");
Paul Lawrence87999172014-02-20 12:21:31 -08002847 return 0;
2848 }
2849
2850 /* The size of the userdata partition, and add in the vold volumes below */
2851 tot_encryption_size = crypt_ftr->fs_size;
2852
2853 if (how == CRYPTO_ENABLE_WIPE) {
JP Abgrall62c7af32014-06-16 13:01:23 -07002854 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
2855 int fs_type = get_fs_type(rec);
2856 if (fs_type < 0) {
2857 SLOGE("cryptfs_enable: unsupported fs type %s\n", rec->fs_type);
2858 return -1;
2859 }
2860 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr->fs_size, fs_type);
Paul Lawrence87999172014-02-20 12:21:31 -08002861 } else if (how == CRYPTO_ENABLE_INPLACE) {
2862 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev,
2863 crypt_ftr->fs_size, &cur_encryption_done,
2864 tot_encryption_size,
2865 previously_encrypted_upto);
2866
JP Abgrall7fc1de82014-10-10 18:43:41 -07002867 if (rc == ENABLE_INPLACE_ERR_DEV) {
2868 /* Hack for b/17898962 */
2869 SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n");
2870 cryptfs_reboot(reboot);
2871 }
2872
Paul Lawrence73d7a022014-06-09 14:10:09 -07002873 if (!rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002874 crypt_ftr->encrypted_upto = cur_encryption_done;
2875 }
2876
Paul Lawrence73d7a022014-06-09 14:10:09 -07002877 if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002878 /* The inplace routine never actually sets the progress to 100% due
2879 * to the round down nature of integer division, so set it here */
2880 property_set("vold.encrypt_progress", "100");
2881 }
2882 } else {
2883 /* Shouldn't happen */
2884 SLOGE("cryptfs_enable: internal error, unknown option\n");
2885 rc = -1;
2886 }
2887
2888 return rc;
2889}
2890
Paul Lawrence13486032014-02-03 13:28:11 -08002891int cryptfs_enable_internal(char *howarg, int crypt_type, char *passwd,
2892 int allow_reboot)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002893{
2894 int how = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002895 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN];
Ken Sumralle5032c42012-04-01 23:58:44 -07002896 unsigned long nr_sec;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002897 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Ken Sumrall319b1042011-06-14 14:01:55 -07002898 int rc=-1, fd, i, ret;
Paul Lawrence87999172014-02-20 12:21:31 -08002899 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002900 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002901 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002902 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07002903 char key_loc[PROPERTY_VALUE_MAX];
2904 char fuse_sdcard[PROPERTY_VALUE_MAX];
2905 char *sd_mnt_point;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002906 int num_vols;
2907 struct volume_info *vol_list = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002908 off64_t previously_encrypted_upto = 0;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002909
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002910 if (!strcmp(howarg, "wipe")) {
2911 how = CRYPTO_ENABLE_WIPE;
2912 } else if (! strcmp(howarg, "inplace")) {
2913 how = CRYPTO_ENABLE_INPLACE;
2914 } else {
2915 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08002916 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002917 }
2918
Paul Lawrence87999172014-02-20 12:21:31 -08002919 /* See if an encryption was underway and interrupted */
2920 if (how == CRYPTO_ENABLE_INPLACE
2921 && get_crypt_ftr_and_key(&crypt_ftr) == 0
2922 && (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS)) {
2923 previously_encrypted_upto = crypt_ftr.encrypted_upto;
2924 crypt_ftr.encrypted_upto = 0;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002925 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
2926
2927 /* At this point, we are in an inconsistent state. Until we successfully
2928 complete encryption, a reboot will leave us broken. So mark the
2929 encryption failed in case that happens.
2930 On successfully completing encryption, remove this flag */
2931 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
2932
2933 put_crypt_ftr_and_key(&crypt_ftr);
Paul Lawrence87999172014-02-20 12:21:31 -08002934 }
2935
2936 property_get("ro.crypto.state", encrypted_state, "");
2937 if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) {
2938 SLOGE("Device is already running encrypted, aborting");
2939 goto error_unencrypted;
2940 }
2941
2942 // TODO refactor fs_mgr_get_crypt_info to get both in one call
2943 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumrall56ad03c2013-02-13 13:00:19 -08002944 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002945
Ken Sumrall3ed82362011-01-28 23:31:16 -08002946 /* Get the size of the real block device */
2947 fd = open(real_blkdev, O_RDONLY);
2948 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
2949 SLOGE("Cannot get size of block device %s\n", real_blkdev);
2950 goto error_unencrypted;
2951 }
2952 close(fd);
2953
2954 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002955 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002956 unsigned int fs_size_sec, max_fs_size_sec;
Jim Millera70abc62014-08-15 02:00:45 +00002957 fs_size_sec = get_fs_size(real_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002958 if (fs_size_sec == 0)
2959 fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev);
2960
Paul Lawrence87999172014-02-20 12:21:31 -08002961 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002962
2963 if (fs_size_sec > max_fs_size_sec) {
2964 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
2965 goto error_unencrypted;
2966 }
2967 }
2968
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002969 /* Get a wakelock as this may take a while, and we don't want the
2970 * device to sleep on us. We'll grab a partial wakelock, and if the UI
2971 * wants to keep the screen on, it can grab a full wakelock.
2972 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002973 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002974 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
2975
Jeff Sharkey7382f812012-08-23 14:08:59 -07002976 /* Get the sdcard mount point */
Jeff Sharkeyb77bc462012-10-01 14:36:26 -07002977 sd_mnt_point = getenv("EMULATED_STORAGE_SOURCE");
Jeff Sharkey7382f812012-08-23 14:08:59 -07002978 if (!sd_mnt_point) {
2979 sd_mnt_point = getenv("EXTERNAL_STORAGE");
2980 }
2981 if (!sd_mnt_point) {
2982 sd_mnt_point = "/mnt/sdcard";
2983 }
Ken Sumrall29d8da82011-05-18 17:20:07 -07002984
Paul Lawrence87999172014-02-20 12:21:31 -08002985 /* TODO
2986 * Currently do not have test devices with multiple encryptable volumes.
2987 * When we acquire some, re-add support.
2988 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002989 num_vols=vold_getNumDirectVolumes();
2990 vol_list = malloc(sizeof(struct volume_info) * num_vols);
2991 vold_getDirectVolumeList(vol_list);
2992
2993 for (i=0; i<num_vols; i++) {
2994 if (should_encrypt(&vol_list[i])) {
Paul Lawrence87999172014-02-20 12:21:31 -08002995 SLOGE("Cannot encrypt if there are multiple encryptable volumes"
2996 "%s\n", vol_list[i].label);
2997 goto error_unencrypted;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002998 }
2999 }
3000
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003001 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003002 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003003 */
3004 property_set("vold.decrypt", "trigger_shutdown_framework");
3005 SLOGD("Just asked init to shut down class main\n");
3006
Ken Sumrall425524d2012-06-14 20:55:28 -07003007 if (vold_unmountAllAsecs()) {
3008 /* Just report the error. If any are left mounted,
3009 * umounting /data below will fail and handle the error.
3010 */
3011 SLOGE("Error unmounting internal asecs");
3012 }
3013
Ken Sumrall29d8da82011-05-18 17:20:07 -07003014 property_get("ro.crypto.fuse_sdcard", fuse_sdcard, "");
3015 if (!strcmp(fuse_sdcard, "true")) {
3016 /* This is a device using the fuse layer to emulate the sdcard semantics
3017 * on top of the userdata partition. vold does not manage it, it is managed
3018 * by the sdcard service. The sdcard service was killed by the property trigger
3019 * above, so just unmount it now. We must do this _AFTER_ killing the framework,
3020 * unlike the case for vold managed devices above.
3021 */
Greg Hackmann6e8440f2014-10-02 17:18:20 -07003022 if (wait_and_unmount(sd_mnt_point, false)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07003023 goto error_shutting_down;
3024 }
Ken Sumrall2eaf7132011-01-14 12:45:48 -08003025 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003026
3027 /* Now unmount the /data partition. */
Greg Hackmann6e8440f2014-10-02 17:18:20 -07003028 if (wait_and_unmount(DATA_MNT_POINT, false)) {
JP Abgrall502dc742013-11-01 13:06:20 -07003029 if (allow_reboot) {
3030 goto error_shutting_down;
3031 } else {
3032 goto error_unencrypted;
3033 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003034 }
3035
3036 /* Do extra work for a better UX when doing the long inplace encryption */
3037 if (how == CRYPTO_ENABLE_INPLACE) {
3038 /* Now that /data is unmounted, we need to mount a tmpfs
3039 * /data, set a property saying we're doing inplace encryption,
3040 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003041 */
Ken Sumralle5032c42012-04-01 23:58:44 -07003042 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08003043 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003044 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003045 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08003046 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003047
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003048 /* restart the framework. */
3049 /* Create necessary paths on /data */
3050 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08003051 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003052 }
3053
Ken Sumrall92736ef2012-10-17 20:57:14 -07003054 /* Ugh, shutting down the framework is not synchronous, so until it
3055 * can be fixed, this horrible hack will wait a moment for it all to
3056 * shut down before proceeding. Without it, some devices cannot
3057 * restart the graphics services.
3058 */
3059 sleep(2);
3060
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003061 /* startup service classes main and late_start */
3062 property_set("vold.decrypt", "trigger_restart_min_framework");
3063 SLOGD("Just triggered restart_min_framework\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003064
Ken Sumrall7df84122011-01-18 14:04:08 -08003065 /* OK, the framework is restarted and will soon be showing a
3066 * progress bar. Time to setup an encrypted mapping, and
3067 * either write a new filesystem, or encrypt in place updating
3068 * the progress bar as we work.
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003069 */
3070 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003071
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003072 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003073 /* Initialize a crypt_mnt_ftr for the partition */
Paul Lawrence87999172014-02-20 12:21:31 -08003074 if (previously_encrypted_upto == 0) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07003075 if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) {
3076 goto error_shutting_down;
3077 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003078
Paul Lawrence87999172014-02-20 12:21:31 -08003079 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
3080 crypt_ftr.fs_size = nr_sec
3081 - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
3082 } else {
3083 crypt_ftr.fs_size = nr_sec;
3084 }
Paul Lawrence6bfed202014-07-28 12:47:22 -07003085 /* At this point, we are in an inconsistent state. Until we successfully
3086 complete encryption, a reboot will leave us broken. So mark the
3087 encryption failed in case that happens.
3088 On successfully completing encryption, remove this flag */
3089 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
Paul Lawrence87999172014-02-20 12:21:31 -08003090 crypt_ftr.crypt_type = crypt_type;
3091 strcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003092
Paul Lawrence87999172014-02-20 12:21:31 -08003093 /* Make an encrypted master key */
3094 if (create_encrypted_random_key(passwd, crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
3095 SLOGE("Cannot create encrypted master key\n");
3096 goto error_shutting_down;
3097 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003098
Paul Lawrence87999172014-02-20 12:21:31 -08003099 /* Write the key to the end of the partition */
3100 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003101
Paul Lawrence87999172014-02-20 12:21:31 -08003102 /* If any persistent data has been remembered, save it.
3103 * If none, create a valid empty table and save that.
3104 */
3105 if (!persist_data) {
3106 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
3107 if (pdata) {
3108 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
3109 persist_data = pdata;
3110 }
3111 }
3112 if (persist_data) {
3113 save_persistent_data();
3114 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003115 }
3116
Paul Lawrenced0c7b172014-08-08 14:28:10 -07003117 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07003118 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
3119 "userdata");
3120
Paul Lawrence87999172014-02-20 12:21:31 -08003121 /* If we are continuing, check checksums match */
3122 rc = 0;
3123 if (previously_encrypted_upto) {
3124 __le8 hash_first_block[SHA256_DIGEST_LENGTH];
3125 rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block);
Ken Sumrall128626f2011-06-28 18:45:14 -07003126
Paul Lawrence87999172014-02-20 12:21:31 -08003127 if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block,
3128 sizeof(hash_first_block)) != 0) {
3129 SLOGE("Checksums do not match - trigger wipe");
3130 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07003131 }
3132 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003133
Paul Lawrence87999172014-02-20 12:21:31 -08003134 if (!rc) {
3135 rc = cryptfs_enable_all_volumes(&crypt_ftr, how,
3136 crypto_blkdev, real_blkdev,
3137 previously_encrypted_upto);
3138 }
3139
3140 /* Calculate checksum if we are not finished */
Paul Lawrence73d7a022014-06-09 14:10:09 -07003141 if (!rc && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08003142 rc = cryptfs_SHA256_fileblock(crypto_blkdev,
3143 crypt_ftr.hash_first_block);
Paul Lawrence73d7a022014-06-09 14:10:09 -07003144 if (rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08003145 SLOGE("Error calculating checksum for continuing encryption");
3146 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07003147 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003148 }
3149
3150 /* Undo the dm-crypt mapping whether we succeed or not */
Ken Sumrall29d8da82011-05-18 17:20:07 -07003151 delete_crypto_blk_dev("userdata");
Ken Sumrall29d8da82011-05-18 17:20:07 -07003152
3153 free(vol_list);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003154
3155 if (! rc) {
3156 /* Success */
Paul Lawrence6bfed202014-07-28 12:47:22 -07003157 crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08003158
Paul Lawrence6bfed202014-07-28 12:47:22 -07003159 if (crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08003160 SLOGD("Encrypted up to sector %lld - will continue after reboot",
3161 crypt_ftr.encrypted_upto);
Paul Lawrence6bfed202014-07-28 12:47:22 -07003162 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence87999172014-02-20 12:21:31 -08003163 }
Paul Lawrence73d7a022014-06-09 14:10:09 -07003164
Paul Lawrence6bfed202014-07-28 12:47:22 -07003165 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08003166
Paul Lawrence73d7a022014-06-09 14:10:09 -07003167 if (crypt_ftr.encrypted_upto == crypt_ftr.fs_size) {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003168 char value[PROPERTY_VALUE_MAX];
3169 property_get("ro.crypto.state", value, "");
3170 if (!strcmp(value, "")) {
3171 /* default encryption - continue first boot sequence */
3172 property_set("ro.crypto.state", "encrypted");
3173 release_wake_lock(lockid);
3174 cryptfs_check_passwd(DEFAULT_PASSWORD);
3175 cryptfs_restart_internal(1);
3176 return 0;
3177 } else {
3178 sleep(2); /* Give the UI a chance to show 100% progress */
Paul Lawrence87999172014-02-20 12:21:31 -08003179 cryptfs_reboot(reboot);
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003180 }
Paul Lawrence87999172014-02-20 12:21:31 -08003181 } else {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003182 sleep(2); /* Partially encrypted, ensure writes flushed to ssd */
Paul Lawrence87999172014-02-20 12:21:31 -08003183 cryptfs_reboot(shutdown);
3184 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003185 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003186 char value[PROPERTY_VALUE_MAX];
3187
Ken Sumrall319369a2012-06-27 16:30:18 -07003188 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003189 if (!strcmp(value, "1")) {
3190 /* wipe data if encryption failed */
3191 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
3192 mkdir("/cache/recovery", 0700);
Nick Kralevich4684e582012-06-26 15:07:03 -07003193 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC, 0600);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003194 if (fd >= 0) {
Jeff Sharkeydd1a8042014-09-24 11:46:51 -07003195 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
3196 write(fd, "--reason=cryptfs_enable_internal\n", strlen("--reason=cryptfs_enable_internal\n") + 1);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003197 close(fd);
3198 } else {
3199 SLOGE("could not open /cache/recovery/command\n");
3200 }
Paul Lawrence87999172014-02-20 12:21:31 -08003201 cryptfs_reboot(recovery);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003202 } else {
3203 /* set property to trigger dialog */
3204 property_set("vold.encrypt_progress", "error_partially_encrypted");
3205 release_wake_lock(lockid);
3206 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003207 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003208 }
3209
Ken Sumrall3ed82362011-01-28 23:31:16 -08003210 /* hrm, the encrypt step claims success, but the reboot failed.
3211 * This should not happen.
3212 * Set the property and return. Hope the framework can deal with it.
3213 */
3214 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003215 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003216 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08003217
3218error_unencrypted:
Ken Sumrall29d8da82011-05-18 17:20:07 -07003219 free(vol_list);
Ken Sumrall3ed82362011-01-28 23:31:16 -08003220 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003221 if (lockid[0]) {
3222 release_wake_lock(lockid);
3223 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003224 return -1;
3225
3226error_shutting_down:
3227 /* we failed, and have not encrypted anthing, so the users's data is still intact,
3228 * but the framework is stopped and not restarted to show the error, so it's up to
3229 * vold to restart the system.
3230 */
3231 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Paul Lawrence87999172014-02-20 12:21:31 -08003232 cryptfs_reboot(reboot);
Ken Sumrall3ed82362011-01-28 23:31:16 -08003233
3234 /* shouldn't get here */
3235 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall29d8da82011-05-18 17:20:07 -07003236 free(vol_list);
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003237 if (lockid[0]) {
3238 release_wake_lock(lockid);
3239 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003240 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003241}
3242
Paul Lawrence45f10532014-04-04 18:11:56 +00003243int cryptfs_enable(char *howarg, int type, char *passwd, int allow_reboot)
Paul Lawrence13486032014-02-03 13:28:11 -08003244{
Paul Lawrencefc615042014-10-04 15:32:29 -07003245 char* adjusted_passwd = adjust_passwd(passwd);
3246 if (adjusted_passwd) {
3247 passwd = adjusted_passwd;
3248 }
3249
3250 int rc = cryptfs_enable_internal(howarg, type, passwd, allow_reboot);
3251
3252 free(adjusted_passwd);
3253 return rc;
Paul Lawrence13486032014-02-03 13:28:11 -08003254}
3255
3256int cryptfs_enable_default(char *howarg, int allow_reboot)
3257{
3258 return cryptfs_enable_internal(howarg, CRYPT_TYPE_DEFAULT,
3259 DEFAULT_PASSWORD, allow_reboot);
3260}
3261
3262int cryptfs_changepw(int crypt_type, const char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003263{
3264 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003265 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003266
3267 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08003268 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08003269 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003270 return -1;
3271 }
3272
Paul Lawrencef4faa572014-01-29 13:31:03 -08003273 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
3274 SLOGE("Invalid crypt_type %d", crypt_type);
3275 return -1;
3276 }
3277
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003278 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07003279 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08003280 SLOGE("Error getting crypt footer and key");
3281 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003282 }
3283
Paul Lawrencef4faa572014-01-29 13:31:03 -08003284 crypt_ftr.crypt_type = crypt_type;
3285
Paul Lawrencefc615042014-10-04 15:32:29 -07003286 char* adjusted_passwd = adjust_passwd(newpw);
3287 if (adjusted_passwd) {
3288 newpw = adjusted_passwd;
3289 }
3290
Paul Lawrencef4faa572014-01-29 13:31:03 -08003291 encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
3292 : newpw,
3293 crypt_ftr.salt,
3294 saved_master_key,
3295 crypt_ftr.master_key,
3296 &crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003297
Jason parks70a4b3f2011-01-28 10:10:47 -06003298 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07003299 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003300
Paul Lawrencefc615042014-10-04 15:32:29 -07003301 free(adjusted_passwd);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003302 return 0;
3303}
Ken Sumrall160b4d62013-04-22 12:15:39 -07003304
Rubin Xu85c01f92014-10-13 12:49:54 +01003305static unsigned int persist_get_max_entries(int encrypted) {
3306 struct crypt_mnt_ftr crypt_ftr;
3307 unsigned int dsize;
3308 unsigned int max_persistent_entries;
3309
3310 /* If encrypted, use the values from the crypt_ftr, otherwise
3311 * use the values for the current spec.
3312 */
3313 if (encrypted) {
3314 if (get_crypt_ftr_and_key(&crypt_ftr)) {
3315 return -1;
3316 }
3317 dsize = crypt_ftr.persist_data_size;
3318 } else {
3319 dsize = CRYPT_PERSIST_DATA_SIZE;
3320 }
3321
3322 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
3323 sizeof(struct crypt_persist_entry);
3324
3325 return max_persistent_entries;
3326}
3327
3328static int persist_get_key(const char *fieldname, char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003329{
3330 unsigned int i;
3331
3332 if (persist_data == NULL) {
3333 return -1;
3334 }
3335 for (i = 0; i < persist_data->persist_valid_entries; i++) {
3336 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
3337 /* We found it! */
3338 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
3339 return 0;
3340 }
3341 }
3342
3343 return -1;
3344}
3345
Rubin Xu85c01f92014-10-13 12:49:54 +01003346static int persist_set_key(const char *fieldname, const char *value, int encrypted)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003347{
3348 unsigned int i;
3349 unsigned int num;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003350 unsigned int max_persistent_entries;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003351
3352 if (persist_data == NULL) {
3353 return -1;
3354 }
3355
Rubin Xu85c01f92014-10-13 12:49:54 +01003356 max_persistent_entries = persist_get_max_entries(encrypted);
Ken Sumrall160b4d62013-04-22 12:15:39 -07003357
3358 num = persist_data->persist_valid_entries;
3359
3360 for (i = 0; i < num; i++) {
3361 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
3362 /* We found an existing entry, update it! */
3363 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
3364 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
3365 return 0;
3366 }
3367 }
3368
3369 /* We didn't find it, add it to the end, if there is room */
3370 if (persist_data->persist_valid_entries < max_persistent_entries) {
3371 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
3372 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
3373 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
3374 persist_data->persist_valid_entries++;
3375 return 0;
3376 }
3377
3378 return -1;
3379}
3380
Rubin Xu85c01f92014-10-13 12:49:54 +01003381/**
3382 * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the
3383 * sequence and its index is greater than or equal to index. Return 0 otherwise.
3384 */
3385static int match_multi_entry(const char *key, const char *field, unsigned index) {
3386 unsigned int i;
3387 unsigned int field_len;
3388 unsigned int key_index;
3389 field_len = strlen(field);
3390
3391 if (index == 0) {
3392 // The first key in a multi-entry field is just the filedname itself.
3393 if (!strcmp(key, field)) {
3394 return 1;
3395 }
3396 }
3397 // Match key against "%s_%d" % (field, index)
3398 if (strlen(key) < field_len + 1 + 1) {
3399 // Need at least a '_' and a digit.
3400 return 0;
3401 }
3402 if (strncmp(key, field, field_len)) {
3403 // If the key does not begin with field, it's not a match.
3404 return 0;
3405 }
3406 if (1 != sscanf(&key[field_len],"_%d", &key_index)) {
3407 return 0;
3408 }
3409 return key_index >= index;
3410}
3411
3412/*
3413 * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all
3414 * remaining entries starting from index will be deleted.
3415 * returns PERSIST_DEL_KEY_OK if deletion succeeds,
3416 * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist,
3417 * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs.
3418 *
3419 */
3420static int persist_del_keys(const char *fieldname, unsigned index)
3421{
3422 unsigned int i;
3423 unsigned int j;
3424 unsigned int num;
3425
3426 if (persist_data == NULL) {
3427 return PERSIST_DEL_KEY_ERROR_OTHER;
3428 }
3429
3430 num = persist_data->persist_valid_entries;
3431
3432 j = 0; // points to the end of non-deleted entries.
3433 // Filter out to-be-deleted entries in place.
3434 for (i = 0; i < num; i++) {
3435 if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) {
3436 persist_data->persist_entry[j] = persist_data->persist_entry[i];
3437 j++;
3438 }
3439 }
3440
3441 if (j < num) {
3442 persist_data->persist_valid_entries = j;
3443 // Zeroise the remaining entries
3444 memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry));
3445 return PERSIST_DEL_KEY_OK;
3446 } else {
3447 // Did not find an entry matching the given fieldname
3448 return PERSIST_DEL_KEY_ERROR_NO_FIELD;
3449 }
3450}
3451
3452static int persist_count_keys(const char *fieldname)
3453{
3454 unsigned int i;
3455 unsigned int count;
3456
3457 if (persist_data == NULL) {
3458 return -1;
3459 }
3460
3461 count = 0;
3462 for (i = 0; i < persist_data->persist_valid_entries; i++) {
3463 if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) {
3464 count++;
3465 }
3466 }
3467
3468 return count;
3469}
3470
Ken Sumrall160b4d62013-04-22 12:15:39 -07003471/* Return the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01003472int cryptfs_getfield(const char *fieldname, char *value, int len)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003473{
3474 char temp_value[PROPERTY_VALUE_MAX];
3475 char real_blkdev[MAXPATHLEN];
Rubin Xu85c01f92014-10-13 12:49:54 +01003476 /* CRYPTO_GETFIELD_OK is success,
3477 * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set,
3478 * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small,
3479 * CRYPTO_GETFIELD_ERROR_OTHER is any other error
Ken Sumrall160b4d62013-04-22 12:15:39 -07003480 */
Rubin Xu85c01f92014-10-13 12:49:54 +01003481 int rc = CRYPTO_GETFIELD_ERROR_OTHER;
3482 int i;
3483 char temp_field[PROPERTY_KEY_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -07003484
3485 if (persist_data == NULL) {
3486 load_persistent_data();
3487 if (persist_data == NULL) {
3488 SLOGE("Getfield error, cannot load persistent data");
3489 goto out;
3490 }
3491 }
3492
Rubin Xu85c01f92014-10-13 12:49:54 +01003493 // Read value from persistent entries. If the original value is split into multiple entries,
3494 // stitch them back together.
Ken Sumrall160b4d62013-04-22 12:15:39 -07003495 if (!persist_get_key(fieldname, temp_value)) {
Rubin Xu85c01f92014-10-13 12:49:54 +01003496 // We found it, copy it to the caller's buffer and keep going until all entries are read.
3497 if (strlcpy(value, temp_value, len) >= (unsigned) len) {
3498 // value too small
3499 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
3500 goto out;
3501 }
3502 rc = CRYPTO_GETFIELD_OK;
3503
3504 for (i = 1; /* break explicitly */; i++) {
3505 if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >=
3506 (int) sizeof(temp_field)) {
3507 // If the fieldname is very long, we stop as soon as it begins to overflow the
3508 // maximum field length. At this point we have in fact fully read out the original
3509 // value because cryptfs_setfield would not allow fields with longer names to be
3510 // written in the first place.
3511 break;
3512 }
3513 if (!persist_get_key(temp_field, temp_value)) {
3514 if (strlcat(value, temp_value, len) >= (unsigned)len) {
3515 // value too small.
3516 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
3517 goto out;
3518 }
3519 } else {
3520 // Exhaust all entries.
3521 break;
3522 }
3523 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003524 } else {
3525 /* Sadness, it's not there. Return the error */
Rubin Xu85c01f92014-10-13 12:49:54 +01003526 rc = CRYPTO_GETFIELD_ERROR_NO_FIELD;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003527 }
3528
3529out:
3530 return rc;
3531}
3532
3533/* Set the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01003534int cryptfs_setfield(const char *fieldname, const char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003535{
3536 struct crypt_persist_data stored_pdata;
3537 struct crypt_persist_data *pdata_p;
3538 struct crypt_mnt_ftr crypt_ftr;
3539 char encrypted_state[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01003540 /* 0 is success, negative values are error */
3541 int rc = CRYPTO_SETFIELD_ERROR_OTHER;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003542 int encrypted = 0;
Rubin Xu85c01f92014-10-13 12:49:54 +01003543 unsigned int field_id;
3544 char temp_field[PROPERTY_KEY_MAX];
3545 unsigned int num_entries;
3546 unsigned int max_keylen;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003547
3548 if (persist_data == NULL) {
3549 load_persistent_data();
3550 if (persist_data == NULL) {
3551 SLOGE("Setfield error, cannot load persistent data");
3552 goto out;
3553 }
3554 }
3555
3556 property_get("ro.crypto.state", encrypted_state, "");
3557 if (!strcmp(encrypted_state, "encrypted") ) {
3558 encrypted = 1;
3559 }
3560
Rubin Xu85c01f92014-10-13 12:49:54 +01003561 // Compute the number of entries required to store value, each entry can store up to
3562 // (PROPERTY_VALUE_MAX - 1) chars
3563 if (strlen(value) == 0) {
3564 // Empty value also needs one entry to store.
3565 num_entries = 1;
3566 } else {
3567 num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1);
3568 }
3569
3570 max_keylen = strlen(fieldname);
3571 if (num_entries > 1) {
3572 // Need an extra "_%d" suffix.
3573 max_keylen += 1 + log10(num_entries);
3574 }
3575 if (max_keylen > PROPERTY_KEY_MAX - 1) {
3576 rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003577 goto out;
3578 }
3579
Rubin Xu85c01f92014-10-13 12:49:54 +01003580 // Make sure we have enough space to write the new value
3581 if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) >
3582 persist_get_max_entries(encrypted)) {
3583 rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG;
3584 goto out;
3585 }
3586
3587 // Now that we know persist_data has enough space for value, let's delete the old field first
3588 // to make up space.
3589 persist_del_keys(fieldname, 0);
3590
3591 if (persist_set_key(fieldname, value, encrypted)) {
3592 // fail to set key, should not happen as we have already checked the available space
3593 SLOGE("persist_set_key() error during setfield()");
3594 goto out;
3595 }
3596
3597 for (field_id = 1; field_id < num_entries; field_id++) {
3598 snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, field_id);
3599
3600 if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), encrypted)) {
3601 // fail to set key, should not happen as we have already checked the available space.
3602 SLOGE("persist_set_key() error during setfield()");
3603 goto out;
3604 }
3605 }
3606
Ken Sumrall160b4d62013-04-22 12:15:39 -07003607 /* If we are running encrypted, save the persistent data now */
3608 if (encrypted) {
3609 if (save_persistent_data()) {
3610 SLOGE("Setfield error, cannot save persistent data");
3611 goto out;
3612 }
3613 }
3614
Rubin Xu85c01f92014-10-13 12:49:54 +01003615 rc = CRYPTO_SETFIELD_OK;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003616
3617out:
3618 return rc;
3619}
Paul Lawrencef4faa572014-01-29 13:31:03 -08003620
3621/* Checks userdata. Attempt to mount the volume if default-
3622 * encrypted.
3623 * On success trigger next init phase and return 0.
3624 * Currently do not handle failure - see TODO below.
3625 */
3626int cryptfs_mount_default_encrypted(void)
3627{
3628 char decrypt_state[PROPERTY_VALUE_MAX];
3629 property_get("vold.decrypt", decrypt_state, "0");
3630 if (!strcmp(decrypt_state, "0")) {
3631 SLOGE("Not encrypted - should not call here");
3632 } else {
3633 int crypt_type = cryptfs_get_password_type();
3634 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
3635 SLOGE("Bad crypt type - error");
3636 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
3637 SLOGD("Password is not default - "
3638 "starting min framework to prompt");
3639 property_set("vold.decrypt", "trigger_restart_min_framework");
3640 return 0;
3641 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
3642 SLOGD("Password is default - restarting filesystem");
3643 cryptfs_restart_internal(0);
3644 return 0;
3645 } else {
3646 SLOGE("Encrypted, default crypt type but can't decrypt");
3647 }
3648 }
3649
Paul Lawrence6bfed202014-07-28 12:47:22 -07003650 /** Corrupt. Allow us to boot into framework, which will detect bad
3651 crypto when it calls do_crypto_complete, then do a factory reset
Paul Lawrencef4faa572014-01-29 13:31:03 -08003652 */
Paul Lawrence6bfed202014-07-28 12:47:22 -07003653 property_set("vold.decrypt", "trigger_restart_min_framework");
Paul Lawrencef4faa572014-01-29 13:31:03 -08003654 return 0;
3655}
3656
3657/* Returns type of the password, default, pattern, pin or password.
3658 */
3659int cryptfs_get_password_type(void)
3660{
3661 struct crypt_mnt_ftr crypt_ftr;
3662
3663 if (get_crypt_ftr_and_key(&crypt_ftr)) {
3664 SLOGE("Error getting crypt footer and key\n");
3665 return -1;
3666 }
3667
Paul Lawrence6bfed202014-07-28 12:47:22 -07003668 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) {
3669 return -1;
3670 }
3671
Paul Lawrencef4faa572014-01-29 13:31:03 -08003672 return crypt_ftr.crypt_type;
3673}
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003674
Paul Lawrence399317e2014-03-10 13:20:50 -07003675char* cryptfs_get_password()
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003676{
Paul Lawrence399317e2014-03-10 13:20:50 -07003677 struct timespec now;
3678 clock_gettime(CLOCK_MONOTONIC, &now);
3679 if (now.tv_sec < password_expiry_time) {
3680 return password;
3681 } else {
3682 cryptfs_clear_password();
3683 return 0;
3684 }
3685}
3686
3687void cryptfs_clear_password()
3688{
3689 if (password) {
3690 size_t len = strlen(password);
3691 memset(password, 0, len);
3692 free(password);
3693 password = 0;
3694 password_expiry_time = 0;
3695 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003696}