blob: 17f5e577a275dea02b37471c16b6ffe911354993 [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
Shawn Willden7c49ab02014-10-30 08:12:32 -0600141 if (!(keymaster_dev->flags & KEYMASTER_SOFTWARE_ONLY) &&
142 (keymaster_dev->flags & KEYMASTER_BLOBS_ARE_STANDALONE)) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700143 rc = 1;
144 }
145
146out:
147 keymaster_close(keymaster_dev);
148 return rc;
149}
150
151/* Create a new keymaster key and store it in this footer */
152static int keymaster_create_key(struct crypt_mnt_ftr *ftr)
153{
154 uint8_t* key = 0;
155 keymaster_device_t *keymaster_dev = 0;
156
157 if (keymaster_init(&keymaster_dev)) {
158 SLOGE("Failed to init keymaster");
159 return -1;
160 }
161
162 int rc = 0;
163
164 keymaster_rsa_keygen_params_t params;
165 memset(&params, '\0', sizeof(params));
Shawn Willden47ba10d2014-09-03 17:07:06 -0600166 params.public_exponent = RSA_EXPONENT;
167 params.modulus_size = RSA_KEY_SIZE;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700168
169 size_t key_size;
170 if (keymaster_dev->generate_keypair(keymaster_dev, TYPE_RSA, &params,
171 &key, &key_size)) {
172 SLOGE("Failed to generate keypair");
173 rc = -1;
174 goto out;
175 }
176
177 if (key_size > KEYMASTER_BLOB_SIZE) {
178 SLOGE("Keymaster key too large for crypto footer");
179 rc = -1;
180 goto out;
181 }
182
183 memcpy(ftr->keymaster_blob, key, key_size);
184 ftr->keymaster_blob_size = key_size;
185
186out:
187 keymaster_close(keymaster_dev);
188 free(key);
189 return rc;
190}
191
Shawn Willdene17a9c42014-09-08 13:04:08 -0600192/* This signs the given object using the keymaster key. */
193static int keymaster_sign_object(struct crypt_mnt_ftr *ftr,
Shawn Willden47ba10d2014-09-03 17:07:06 -0600194 const unsigned char *object,
195 const size_t object_size,
196 unsigned char **signature,
197 size_t *signature_size)
198{
199 int rc = 0;
200 keymaster_device_t *keymaster_dev = 0;
201 if (keymaster_init(&keymaster_dev)) {
202 SLOGE("Failed to init keymaster");
203 return -1;
204 }
205
206 /* We currently set the digest type to DIGEST_NONE because it's the
207 * only supported value for keymaster. A similar issue exists with
208 * PADDING_NONE. Long term both of these should likely change.
209 */
210 keymaster_rsa_sign_params_t params;
211 params.digest_type = DIGEST_NONE;
212 params.padding_type = PADDING_NONE;
213
214 unsigned char to_sign[RSA_KEY_SIZE_BYTES];
Shawn Willdene17a9c42014-09-08 13:04:08 -0600215 size_t to_sign_size = sizeof(to_sign);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600216 memset(to_sign, 0, RSA_KEY_SIZE_BYTES);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600217
Shawn Willdene17a9c42014-09-08 13:04:08 -0600218 // To sign a message with RSA, the message must satisfy two
219 // constraints:
220 //
221 // 1. The message, when interpreted as a big-endian numeric value, must
222 // be strictly less than the public modulus of the RSA key. Note
223 // that because the most significant bit of the public modulus is
224 // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit
225 // key), an n-bit message with most significant bit 0 always
226 // satisfies this requirement.
227 //
228 // 2. The message must have the same length in bits as the public
229 // modulus of the RSA key. This requirement isn't mathematically
230 // necessary, but is necessary to ensure consistency in
231 // implementations.
232 switch (ftr->kdf_type) {
233 case KDF_SCRYPT_KEYMASTER_UNPADDED:
234 // This is broken: It produces a message which is shorter than
235 // the public modulus, failing criterion 2.
236 memcpy(to_sign, object, object_size);
237 to_sign_size = object_size;
238 SLOGI("Signing unpadded object");
239 break;
240 case KDF_SCRYPT_KEYMASTER_BADLY_PADDED:
241 // This is broken: Since the value of object is uniformly
242 // distributed, it produces a message that is larger than the
243 // public modulus with probability 0.25.
244 memcpy(to_sign, object, min(RSA_KEY_SIZE_BYTES, object_size));
245 SLOGI("Signing end-padded object");
246 break;
247 case KDF_SCRYPT_KEYMASTER:
248 // This ensures the most significant byte of the signed message
249 // is zero. We could have zero-padded to the left instead, but
250 // this approach is slightly more robust against changes in
251 // object size. However, it's still broken (but not unusably
252 // so) because we really should be using a proper RSA padding
253 // function, such as OAEP.
254 //
255 // TODO(paullawrence): When keymaster 0.4 is available, change
256 // this to use the padding options it provides.
257 memcpy(to_sign + 1, object, min(RSA_KEY_SIZE_BYTES - 1, object_size));
258 SLOGI("Signing safely-padded object");
259 break;
260 default:
261 SLOGE("Unknown KDF type %d", ftr->kdf_type);
262 return -1;
263 }
264
Shawn Willden47ba10d2014-09-03 17:07:06 -0600265 rc = keymaster_dev->sign_data(keymaster_dev,
266 &params,
267 ftr->keymaster_blob,
268 ftr->keymaster_blob_size,
269 to_sign,
Shawn Willdene17a9c42014-09-08 13:04:08 -0600270 to_sign_size,
Shawn Willden47ba10d2014-09-03 17:07:06 -0600271 signature,
272 signature_size);
273
274 keymaster_close(keymaster_dev);
275 return rc;
276}
277
Paul Lawrence399317e2014-03-10 13:20:50 -0700278/* Store password when userdata is successfully decrypted and mounted.
279 * Cleared by cryptfs_clear_password
280 *
281 * To avoid a double prompt at boot, we need to store the CryptKeeper
282 * password and pass it to KeyGuard, which uses it to unlock KeyStore.
283 * Since the entire framework is torn down and rebuilt after encryption,
284 * we have to use a daemon or similar to store the password. Since vold
285 * is secured against IPC except from system processes, it seems a reasonable
286 * place to store this.
287 *
288 * password should be cleared once it has been used.
289 *
290 * password is aged out after password_max_age_seconds seconds.
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800291 */
Paul Lawrence399317e2014-03-10 13:20:50 -0700292static char* password = 0;
293static int password_expiry_time = 0;
294static const int password_max_age_seconds = 60;
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800295
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800296extern struct fstab *fstab;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800297
Paul Lawrence87999172014-02-20 12:21:31 -0800298enum RebootType {reboot, recovery, shutdown};
299static void cryptfs_reboot(enum RebootType rt)
Ken Sumralladfba362013-06-04 16:37:52 -0700300{
Paul Lawrence87999172014-02-20 12:21:31 -0800301 switch(rt) {
302 case reboot:
303 property_set(ANDROID_RB_PROPERTY, "reboot");
304 break;
305
306 case recovery:
307 property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
308 break;
309
310 case shutdown:
311 property_set(ANDROID_RB_PROPERTY, "shutdown");
312 break;
Ken Sumralladfba362013-06-04 16:37:52 -0700313 }
Paul Lawrence87999172014-02-20 12:21:31 -0800314
Ken Sumralladfba362013-06-04 16:37:52 -0700315 sleep(20);
316
317 /* Shouldn't get here, reboot should happen before sleep times out */
318 return;
319}
320
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800321static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
322{
323 memset(io, 0, dataSize);
324 io->data_size = dataSize;
325 io->data_start = sizeof(struct dm_ioctl);
326 io->version[0] = 4;
327 io->version[1] = 0;
328 io->version[2] = 0;
329 io->flags = flags;
330 if (name) {
331 strncpy(io->name, name, sizeof(io->name));
332 }
333}
334
Kenny Rootc4c70f12013-06-14 12:11:38 -0700335/**
336 * Gets the default device scrypt parameters for key derivation time tuning.
337 * The parameters should lead to about one second derivation time for the
338 * given device.
339 */
340static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
341 const int default_params[] = SCRYPT_DEFAULTS;
342 int params[] = SCRYPT_DEFAULTS;
343 char paramstr[PROPERTY_VALUE_MAX];
344 char *token;
345 char *saveptr;
346 int i;
347
348 property_get(SCRYPT_PROP, paramstr, "");
349 if (paramstr[0] != '\0') {
350 /*
351 * The token we're looking for should be three integers separated by
352 * colons (e.g., "12:8:1"). Scan the property to make sure it matches.
353 */
Kenny Root2947e342013-08-14 15:54:49 -0700354 for (i = 0, token = strtok_r(paramstr, ":", &saveptr);
355 token != NULL && i < 3;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700356 i++, token = strtok_r(NULL, ":", &saveptr)) {
357 char *endptr;
358 params[i] = strtol(token, &endptr, 10);
359
360 /*
361 * Check that there was a valid number and it's 8-bit. If not,
362 * break out and the end check will take the default values.
363 */
364 if ((*token == '\0') || (*endptr != '\0') || params[i] < 0 || params[i] > 255) {
365 break;
366 }
367 }
368
369 /*
370 * If there were not enough tokens or a token was malformed (not an
371 * integer), it will end up here and the default parameters can be
372 * taken.
373 */
374 if ((i != 3) || (token != NULL)) {
375 SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
376 memcpy(params, default_params, sizeof(params));
377 }
378 }
379
380 ftr->N_factor = params[0];
381 ftr->r_factor = params[1];
382 ftr->p_factor = params[2];
383}
384
Ken Sumrall3ed82362011-01-28 23:31:16 -0800385static unsigned int get_fs_size(char *dev)
386{
387 int fd, block_size;
388 struct ext4_super_block sb;
389 off64_t len;
390
391 if ((fd = open(dev, O_RDONLY)) < 0) {
392 SLOGE("Cannot open device to get filesystem size ");
393 return 0;
394 }
395
396 if (lseek64(fd, 1024, SEEK_SET) < 0) {
397 SLOGE("Cannot seek to superblock");
398 return 0;
399 }
400
401 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
402 SLOGE("Cannot read superblock");
403 return 0;
404 }
405
406 close(fd);
407
Daniel Rosenberge82df162014-08-15 22:19:23 +0000408 if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) {
409 SLOGE("Not a valid ext4 superblock");
410 return 0;
411 }
Ken Sumrall3ed82362011-01-28 23:31:16 -0800412 block_size = 1024 << sb.s_log_block_size;
413 /* compute length in bytes */
414 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
415
416 /* return length in sectors */
417 return (unsigned int) (len / 512);
418}
419
Ken Sumrall160b4d62013-04-22 12:15:39 -0700420static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
421{
422 static int cached_data = 0;
423 static off64_t cached_off = 0;
424 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
425 int fd;
426 char key_loc[PROPERTY_VALUE_MAX];
427 char real_blkdev[PROPERTY_VALUE_MAX];
428 unsigned int nr_sec;
429 int rc = -1;
430
431 if (!cached_data) {
432 fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
433
434 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
435 if ( (fd = open(real_blkdev, O_RDWR)) < 0) {
436 SLOGE("Cannot open real block device %s\n", real_blkdev);
437 return -1;
438 }
439
440 if ((nr_sec = get_blkdev_size(fd))) {
441 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
442 * encryption info footer and key, and plenty of bytes to spare for future
443 * growth.
444 */
445 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
446 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
447 cached_data = 1;
448 } else {
449 SLOGE("Cannot get size of block device %s\n", real_blkdev);
450 }
451 close(fd);
452 } else {
453 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
454 cached_off = 0;
455 cached_data = 1;
456 }
457 }
458
459 if (cached_data) {
460 if (metadata_fname) {
461 *metadata_fname = cached_metadata_fname;
462 }
463 if (off) {
464 *off = cached_off;
465 }
466 rc = 0;
467 }
468
469 return rc;
470}
471
Ken Sumralle8744072011-01-18 22:01:55 -0800472/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800473 * update the failed mount count but not change the key.
474 */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700475static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800476{
477 int fd;
478 unsigned int nr_sec, cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700479 /* starting_off is set to the SEEK_SET offset
480 * where the crypto structure starts
481 */
482 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800483 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700484 char *fname = NULL;
Ken Sumrall3be890f2011-09-14 16:53:46 -0700485 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800486
Ken Sumrall160b4d62013-04-22 12:15:39 -0700487 if (get_crypt_ftr_info(&fname, &starting_off)) {
488 SLOGE("Unable to get crypt_ftr_info\n");
489 return -1;
490 }
491 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700492 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700493 return -1;
494 }
Ken Sumralle550f782013-08-20 13:48:23 -0700495 if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) {
496 SLOGE("Cannot open footer file %s for put\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700497 return -1;
498 }
499
500 /* Seek to the start of the crypt footer */
501 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
502 SLOGE("Cannot seek to real block device footer\n");
503 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800504 }
505
506 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
507 SLOGE("Cannot write real block device footer\n");
508 goto errout;
509 }
510
Ken Sumrall3be890f2011-09-14 16:53:46 -0700511 fstat(fd, &statbuf);
512 /* If the keys are kept on a raw block device, do not try to truncate it. */
Ken Sumralle550f782013-08-20 13:48:23 -0700513 if (S_ISREG(statbuf.st_mode)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700514 if (ftruncate(fd, 0x4000)) {
Colin Cross59846b62014-02-06 20:34:29 -0800515 SLOGE("Cannot set footer file size\n");
Ken Sumralle8744072011-01-18 22:01:55 -0800516 goto errout;
517 }
518 }
519
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800520 /* Success! */
521 rc = 0;
522
523errout:
524 close(fd);
525 return rc;
526
527}
528
Ken Sumrall160b4d62013-04-22 12:15:39 -0700529static inline int unix_read(int fd, void* buff, int len)
530{
531 return TEMP_FAILURE_RETRY(read(fd, buff, len));
532}
533
534static inline int unix_write(int fd, const void* buff, int len)
535{
536 return TEMP_FAILURE_RETRY(write(fd, buff, len));
537}
538
539static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
540{
541 memset(pdata, 0, len);
542 pdata->persist_magic = PERSIST_DATA_MAGIC;
543 pdata->persist_valid_entries = 0;
544}
545
546/* A routine to update the passed in crypt_ftr to the lastest version.
547 * fd is open read/write on the device that holds the crypto footer and persistent
548 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
549 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
550 */
551static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
552{
Kenny Root7434b312013-06-14 11:29:53 -0700553 int orig_major = crypt_ftr->major_version;
554 int orig_minor = crypt_ftr->minor_version;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700555
Kenny Root7434b312013-06-14 11:29:53 -0700556 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
557 struct crypt_persist_data *pdata;
558 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700559
Kenny Rootc4c70f12013-06-14 12:11:38 -0700560 SLOGW("upgrading crypto footer to 1.1");
561
Kenny Root7434b312013-06-14 11:29:53 -0700562 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
563 if (pdata == NULL) {
564 SLOGE("Cannot allocate persisent data\n");
565 return;
566 }
567 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
568
569 /* Need to initialize the persistent data area */
570 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
571 SLOGE("Cannot seek to persisent data offset\n");
572 return;
573 }
574 /* Write all zeros to the first copy, making it invalid */
575 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
576
577 /* Write a valid but empty structure to the second copy */
578 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
579 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
580
581 /* Update the footer */
582 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
583 crypt_ftr->persist_data_offset[0] = pdata_offset;
584 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
585 crypt_ftr->minor_version = 1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700586 }
587
Paul Lawrencef4faa572014-01-29 13:31:03 -0800588 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700589 SLOGW("upgrading crypto footer to 1.2");
JP Abgrall7bdfa522013-11-15 13:42:56 -0800590 /* But keep the old kdf_type.
591 * It will get updated later to KDF_SCRYPT after the password has been verified.
592 */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700593 crypt_ftr->kdf_type = KDF_PBKDF2;
594 get_device_scrypt_params(crypt_ftr);
595 crypt_ftr->minor_version = 2;
596 }
597
Paul Lawrencef4faa572014-01-29 13:31:03 -0800598 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
599 SLOGW("upgrading crypto footer to 1.3");
600 crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
601 crypt_ftr->minor_version = 3;
602 }
603
Kenny Root7434b312013-06-14 11:29:53 -0700604 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
605 if (lseek64(fd, offset, SEEK_SET) == -1) {
606 SLOGE("Cannot seek to crypt footer\n");
607 return;
608 }
609 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700610 }
Ken Sumrall160b4d62013-04-22 12:15:39 -0700611}
612
613
614static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800615{
616 int fd;
617 unsigned int nr_sec, cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700618 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800619 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700620 char *fname = NULL;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700621 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800622
Ken Sumrall160b4d62013-04-22 12:15:39 -0700623 if (get_crypt_ftr_info(&fname, &starting_off)) {
624 SLOGE("Unable to get crypt_ftr_info\n");
625 return -1;
626 }
627 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700628 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700629 return -1;
630 }
631 if ( (fd = open(fname, O_RDWR)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700632 SLOGE("Cannot open footer file %s for get\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700633 return -1;
634 }
635
636 /* Make sure it's 16 Kbytes in length */
637 fstat(fd, &statbuf);
638 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
639 SLOGE("footer file %s is not the expected size!\n", fname);
640 goto errout;
641 }
642
643 /* Seek to the start of the crypt footer */
644 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
645 SLOGE("Cannot seek to real block device footer\n");
646 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800647 }
648
649 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
650 SLOGE("Cannot read real block device footer\n");
651 goto errout;
652 }
653
654 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700655 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800656 goto errout;
657 }
658
Kenny Rootc96a5f82013-06-14 12:08:28 -0700659 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
660 SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
661 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800662 goto errout;
663 }
664
Kenny Rootc96a5f82013-06-14 12:08:28 -0700665 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
666 SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
667 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800668 }
669
Ken Sumrall160b4d62013-04-22 12:15:39 -0700670 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
671 * copy on disk before returning.
672 */
Kenny Rootc96a5f82013-06-14 12:08:28 -0700673 if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700674 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
Ken Sumralle8744072011-01-18 22:01:55 -0800675 }
676
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800677 /* Success! */
678 rc = 0;
679
680errout:
681 close(fd);
682 return rc;
683}
684
Ken Sumrall160b4d62013-04-22 12:15:39 -0700685static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
686{
687 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
688 crypt_ftr->persist_data_offset[1]) {
689 SLOGE("Crypt_ftr persist data regions overlap");
690 return -1;
691 }
692
693 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
694 SLOGE("Crypt_ftr persist data region 0 starts after region 1");
695 return -1;
696 }
697
698 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
699 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
700 CRYPT_FOOTER_OFFSET) {
701 SLOGE("Persistent data extends past crypto footer");
702 return -1;
703 }
704
705 return 0;
706}
707
708static int load_persistent_data(void)
709{
710 struct crypt_mnt_ftr crypt_ftr;
711 struct crypt_persist_data *pdata = NULL;
712 char encrypted_state[PROPERTY_VALUE_MAX];
713 char *fname;
714 int found = 0;
715 int fd;
716 int ret;
717 int i;
718
719 if (persist_data) {
720 /* Nothing to do, we've already loaded or initialized it */
721 return 0;
722 }
723
724
725 /* If not encrypted, just allocate an empty table and initialize it */
726 property_get("ro.crypto.state", encrypted_state, "");
727 if (strcmp(encrypted_state, "encrypted") ) {
728 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
729 if (pdata) {
730 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
731 persist_data = pdata;
732 return 0;
733 }
734 return -1;
735 }
736
737 if(get_crypt_ftr_and_key(&crypt_ftr)) {
738 return -1;
739 }
740
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700741 if ((crypt_ftr.major_version < 1)
742 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700743 SLOGE("Crypt_ftr version doesn't support persistent data");
744 return -1;
745 }
746
747 if (get_crypt_ftr_info(&fname, NULL)) {
748 return -1;
749 }
750
751 ret = validate_persistent_data_storage(&crypt_ftr);
752 if (ret) {
753 return -1;
754 }
755
756 fd = open(fname, O_RDONLY);
757 if (fd < 0) {
758 SLOGE("Cannot open %s metadata file", fname);
759 return -1;
760 }
761
762 if (persist_data == NULL) {
763 pdata = malloc(crypt_ftr.persist_data_size);
764 if (pdata == NULL) {
765 SLOGE("Cannot allocate memory for persistent data");
766 goto err;
767 }
768 }
769
770 for (i = 0; i < 2; i++) {
771 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
772 SLOGE("Cannot seek to read persistent data on %s", fname);
773 goto err2;
774 }
775 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
776 SLOGE("Error reading persistent data on iteration %d", i);
777 goto err2;
778 }
779 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
780 found = 1;
781 break;
782 }
783 }
784
785 if (!found) {
786 SLOGI("Could not find valid persistent data, creating");
787 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
788 }
789
790 /* Success */
791 persist_data = pdata;
792 close(fd);
793 return 0;
794
795err2:
796 free(pdata);
797
798err:
799 close(fd);
800 return -1;
801}
802
803static int save_persistent_data(void)
804{
805 struct crypt_mnt_ftr crypt_ftr;
806 struct crypt_persist_data *pdata;
807 char *fname;
808 off64_t write_offset;
809 off64_t erase_offset;
810 int found = 0;
811 int fd;
812 int ret;
813
814 if (persist_data == NULL) {
815 SLOGE("No persistent data to save");
816 return -1;
817 }
818
819 if(get_crypt_ftr_and_key(&crypt_ftr)) {
820 return -1;
821 }
822
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700823 if ((crypt_ftr.major_version < 1)
824 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700825 SLOGE("Crypt_ftr version doesn't support persistent data");
826 return -1;
827 }
828
829 ret = validate_persistent_data_storage(&crypt_ftr);
830 if (ret) {
831 return -1;
832 }
833
834 if (get_crypt_ftr_info(&fname, NULL)) {
835 return -1;
836 }
837
838 fd = open(fname, O_RDWR);
839 if (fd < 0) {
840 SLOGE("Cannot open %s metadata file", fname);
841 return -1;
842 }
843
844 pdata = malloc(crypt_ftr.persist_data_size);
845 if (pdata == NULL) {
846 SLOGE("Cannot allocate persistant data");
847 goto err;
848 }
849
850 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
851 SLOGE("Cannot seek to read persistent data on %s", fname);
852 goto err2;
853 }
854
855 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
856 SLOGE("Error reading persistent data before save");
857 goto err2;
858 }
859
860 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
861 /* The first copy is the curent valid copy, so write to
862 * the second copy and erase this one */
863 write_offset = crypt_ftr.persist_data_offset[1];
864 erase_offset = crypt_ftr.persist_data_offset[0];
865 } else {
866 /* The second copy must be the valid copy, so write to
867 * the first copy, and erase the second */
868 write_offset = crypt_ftr.persist_data_offset[0];
869 erase_offset = crypt_ftr.persist_data_offset[1];
870 }
871
872 /* Write the new copy first, if successful, then erase the old copy */
873 if (lseek(fd, write_offset, SEEK_SET) < 0) {
874 SLOGE("Cannot seek to write persistent data");
875 goto err2;
876 }
877 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
878 (int) crypt_ftr.persist_data_size) {
879 if (lseek(fd, erase_offset, SEEK_SET) < 0) {
880 SLOGE("Cannot seek to erase previous persistent data");
881 goto err2;
882 }
883 fsync(fd);
884 memset(pdata, 0, crypt_ftr.persist_data_size);
885 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
886 (int) crypt_ftr.persist_data_size) {
887 SLOGE("Cannot write to erase previous persistent data");
888 goto err2;
889 }
890 fsync(fd);
891 } else {
892 SLOGE("Cannot write to save persistent data");
893 goto err2;
894 }
895
896 /* Success */
897 free(pdata);
898 close(fd);
899 return 0;
900
901err2:
902 free(pdata);
903err:
904 close(fd);
905 return -1;
906}
907
Paul Lawrencef4faa572014-01-29 13:31:03 -0800908static int hexdigit (char c)
909{
910 if (c >= '0' && c <= '9') return c - '0';
911 c = tolower(c);
912 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
913 return -1;
914}
915
916static unsigned char* convert_hex_ascii_to_key(const char* master_key_ascii,
917 unsigned int* out_keysize)
918{
919 unsigned int i;
920 *out_keysize = 0;
921
922 size_t size = strlen (master_key_ascii);
923 if (size % 2) {
924 SLOGE("Trying to convert ascii string of odd length");
925 return NULL;
926 }
927
928 unsigned char* master_key = (unsigned char*) malloc(size / 2);
929 if (master_key == 0) {
930 SLOGE("Cannot allocate");
931 return NULL;
932 }
933
934 for (i = 0; i < size; i += 2) {
935 int high_nibble = hexdigit (master_key_ascii[i]);
936 int low_nibble = hexdigit (master_key_ascii[i + 1]);
937
938 if(high_nibble < 0 || low_nibble < 0) {
939 SLOGE("Invalid hex string");
940 free (master_key);
941 return NULL;
942 }
943
944 master_key[*out_keysize] = high_nibble * 16 + low_nibble;
945 (*out_keysize)++;
946 }
947
948 return master_key;
949}
950
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800951/* Convert a binary key of specified length into an ascii hex string equivalent,
952 * without the leading 0x and with null termination
953 */
Paul Lawrencef4faa572014-01-29 13:31:03 -0800954static void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800955 char *master_key_ascii)
956{
957 unsigned int i, a;
958 unsigned char nibble;
959
960 for (i=0, a=0; i<keysize; i++, a+=2) {
961 /* For each byte, write out two ascii hex digits */
962 nibble = (master_key[i] >> 4) & 0xf;
963 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
964
965 nibble = master_key[i] & 0xf;
966 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
967 }
968
969 /* Add the null termination */
970 master_key_ascii[a] = '\0';
971
972}
973
Ken Sumralldb5e0262013-02-05 17:39:48 -0800974static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
975 char *real_blk_name, const char *name, int fd,
976 char *extra_params)
977{
978 char buffer[DM_CRYPT_BUF_SIZE];
979 struct dm_ioctl *io;
980 struct dm_target_spec *tgt;
981 char *crypt_params;
982 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
983 int i;
984
985 io = (struct dm_ioctl *) buffer;
986
987 /* Load the mapping table for this device */
988 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
989
990 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
991 io->target_count = 1;
992 tgt->status = 0;
993 tgt->sector_start = 0;
994 tgt->length = crypt_ftr->fs_size;
995 strcpy(tgt->target_type, "crypt");
996
997 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
998 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
999 sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name,
1000 master_key_ascii, real_blk_name, extra_params);
1001 crypt_params += strlen(crypt_params) + 1;
1002 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
1003 tgt->next = crypt_params - buffer;
1004
1005 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
1006 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
1007 break;
1008 }
1009 usleep(500000);
1010 }
1011
1012 if (i == TABLE_LOAD_RETRIES) {
1013 /* We failed to load the table, return an error */
1014 return -1;
1015 } else {
1016 return i + 1;
1017 }
1018}
1019
1020
1021static int get_dm_crypt_version(int fd, const char *name, int *version)
1022{
1023 char buffer[DM_CRYPT_BUF_SIZE];
1024 struct dm_ioctl *io;
1025 struct dm_target_versions *v;
1026 int i;
1027
1028 io = (struct dm_ioctl *) buffer;
1029
1030 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1031
1032 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
1033 return -1;
1034 }
1035
1036 /* Iterate over the returned versions, looking for name of "crypt".
1037 * When found, get and return the version.
1038 */
1039 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
1040 while (v->next) {
1041 if (! strcmp(v->name, "crypt")) {
1042 /* We found the crypt driver, return the version, and get out */
1043 version[0] = v->version[0];
1044 version[1] = v->version[1];
1045 version[2] = v->version[2];
1046 return 0;
1047 }
1048 v = (struct dm_target_versions *)(((char *)v) + v->next);
1049 }
1050
1051 return -1;
1052}
1053
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001054static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
Ken Sumrall29d8da82011-05-18 17:20:07 -07001055 char *real_blk_name, char *crypto_blk_name, const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001056{
1057 char buffer[DM_CRYPT_BUF_SIZE];
1058 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
1059 char *crypt_params;
1060 struct dm_ioctl *io;
1061 struct dm_target_spec *tgt;
1062 unsigned int minor;
1063 int fd;
Ken Sumralle919efe2012-09-29 17:07:41 -07001064 int i;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001065 int retval = -1;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001066 int version[3];
1067 char *extra_params;
1068 int load_count;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001069
1070 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
1071 SLOGE("Cannot open device-mapper\n");
1072 goto errout;
1073 }
1074
1075 io = (struct dm_ioctl *) buffer;
1076
1077 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1078 if (ioctl(fd, DM_DEV_CREATE, io)) {
1079 SLOGE("Cannot create dm-crypt device\n");
1080 goto errout;
1081 }
1082
1083 /* Get the device status, in particular, the name of it's device file */
1084 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1085 if (ioctl(fd, DM_DEV_STATUS, io)) {
1086 SLOGE("Cannot retrieve dm-crypt device status\n");
1087 goto errout;
1088 }
1089 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
1090 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
1091
Ken Sumralldb5e0262013-02-05 17:39:48 -08001092 extra_params = "";
1093 if (! get_dm_crypt_version(fd, name, version)) {
1094 /* Support for allow_discards was added in version 1.11.0 */
1095 if ((version[0] >= 2) ||
1096 ((version[0] == 1) && (version[1] >= 11))) {
1097 extra_params = "1 allow_discards";
1098 SLOGI("Enabling support for allow_discards in dmcrypt.\n");
1099 }
Ken Sumralle919efe2012-09-29 17:07:41 -07001100 }
1101
Ken Sumralldb5e0262013-02-05 17:39:48 -08001102 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name,
1103 fd, extra_params);
1104 if (load_count < 0) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001105 SLOGE("Cannot load dm-crypt mapping table.\n");
1106 goto errout;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001107 } else if (load_count > 1) {
1108 SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001109 }
1110
1111 /* Resume this device to activate it */
Ken Sumralldb5e0262013-02-05 17:39:48 -08001112 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001113
1114 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
1115 SLOGE("Cannot resume the dm-crypt device\n");
1116 goto errout;
1117 }
1118
1119 /* We made it here with no errors. Woot! */
1120 retval = 0;
1121
1122errout:
1123 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1124
1125 return retval;
1126}
1127
Ken Sumrall29d8da82011-05-18 17:20:07 -07001128static int delete_crypto_blk_dev(char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001129{
1130 int fd;
1131 char buffer[DM_CRYPT_BUF_SIZE];
1132 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001133 int retval = -1;
1134
1135 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
1136 SLOGE("Cannot open device-mapper\n");
1137 goto errout;
1138 }
1139
1140 io = (struct dm_ioctl *) buffer;
1141
1142 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1143 if (ioctl(fd, DM_DEV_REMOVE, io)) {
1144 SLOGE("Cannot remove dm-crypt device\n");
1145 goto errout;
1146 }
1147
1148 /* We made it here with no errors. Woot! */
1149 retval = 0;
1150
1151errout:
1152 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1153
1154 return retval;
1155
1156}
1157
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001158static int pbkdf2(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001159 unsigned char *ikey, void *params UNUSED)
1160{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001161 SLOGI("Using pbkdf2 for cryptfs KDF");
1162
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001163 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001164 unsigned int keysize;
1165 char* master_key = (char*)convert_hex_ascii_to_key(passwd, &keysize);
1166 if (!master_key) return -1;
1167 PKCS5_PBKDF2_HMAC_SHA1(master_key, keysize, salt, SALT_LEN,
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001168 HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001169
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001170 memset(master_key, 0, keysize);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001171 free (master_key);
1172 return 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001173}
1174
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001175static int scrypt(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001176 unsigned char *ikey, void *params)
1177{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001178 SLOGI("Using scrypt for cryptfs KDF");
1179
Kenny Rootc4c70f12013-06-14 12:11:38 -07001180 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1181
1182 int N = 1 << ftr->N_factor;
1183 int r = 1 << ftr->r_factor;
1184 int p = 1 << ftr->p_factor;
1185
1186 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001187 unsigned int keysize;
1188 unsigned char* master_key = convert_hex_ascii_to_key(passwd, &keysize);
1189 if (!master_key) return -1;
1190 crypto_scrypt(master_key, keysize, salt, SALT_LEN, N, r, p, ikey,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001191 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001192
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001193 memset(master_key, 0, keysize);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001194 free (master_key);
1195 return 0;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001196}
1197
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001198static int scrypt_keymaster(const char *passwd, const unsigned char *salt,
1199 unsigned char *ikey, void *params)
1200{
1201 SLOGI("Using scrypt with keymaster for cryptfs KDF");
1202
1203 int rc;
1204 unsigned int key_size;
1205 size_t signature_size;
1206 unsigned char* signature;
1207 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1208
1209 int N = 1 << ftr->N_factor;
1210 int r = 1 << ftr->r_factor;
1211 int p = 1 << ftr->p_factor;
1212
1213 unsigned char* master_key = convert_hex_ascii_to_key(passwd, &key_size);
1214 if (!master_key) {
1215 SLOGE("Failed to convert passwd from hex");
1216 return -1;
1217 }
1218
1219 rc = crypto_scrypt(master_key, key_size, salt, SALT_LEN,
1220 N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES);
1221 memset(master_key, 0, key_size);
1222 free(master_key);
1223
1224 if (rc) {
1225 SLOGE("scrypt failed");
1226 return -1;
1227 }
1228
Shawn Willdene17a9c42014-09-08 13:04:08 -06001229 if (keymaster_sign_object(ftr, ikey, KEY_LEN_BYTES + IV_LEN_BYTES,
1230 &signature, &signature_size)) {
1231 SLOGE("Signing failed");
1232 return -1;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001233 }
1234
1235 rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN,
1236 N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES);
1237 free(signature);
1238
1239 if (rc) {
1240 SLOGE("scrypt failed");
1241 return -1;
1242 }
1243
1244 return 0;
1245}
1246
1247static int encrypt_master_key(const char *passwd, const unsigned char *salt,
1248 const unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001249 unsigned char *encrypted_master_key,
1250 struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001251{
1252 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
1253 EVP_CIPHER_CTX e_ctx;
1254 int encrypted_len, final_len;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001255 int rc = 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001256
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001257 /* Turn the password into an intermediate key and IV that can decrypt the master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001258 get_device_scrypt_params(crypt_ftr);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001259
1260 switch (crypt_ftr->kdf_type) {
Shawn Willdene17a9c42014-09-08 13:04:08 -06001261 case KDF_SCRYPT_KEYMASTER_UNPADDED:
1262 case KDF_SCRYPT_KEYMASTER_BADLY_PADDED:
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001263 case KDF_SCRYPT_KEYMASTER:
1264 if (keymaster_create_key(crypt_ftr)) {
1265 SLOGE("keymaster_create_key failed");
1266 return -1;
1267 }
1268
1269 if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) {
1270 SLOGE("scrypt failed");
1271 return -1;
1272 }
1273 break;
1274
1275 case KDF_SCRYPT:
1276 if (scrypt(passwd, salt, ikey, crypt_ftr)) {
1277 SLOGE("scrypt failed");
1278 return -1;
1279 }
1280 break;
1281
1282 default:
1283 SLOGE("Invalid kdf_type");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001284 return -1;
1285 }
Kenny Rootc4c70f12013-06-14 12:11:38 -07001286
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001287 /* Initialize the decryption engine */
1288 if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
1289 SLOGE("EVP_EncryptInit failed\n");
1290 return -1;
1291 }
1292 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001293
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001294 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001295 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
1296 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001297 SLOGE("EVP_EncryptUpdate failed\n");
1298 return -1;
1299 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001300 if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001301 SLOGE("EVP_EncryptFinal failed\n");
1302 return -1;
1303 }
1304
1305 if (encrypted_len + final_len != KEY_LEN_BYTES) {
1306 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
1307 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001308 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001309
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001310 /* Store the scrypt of the intermediate key, so we can validate if it's a
1311 password error or mount error when things go wrong.
1312 Note there's no need to check for errors, since if this is incorrect, we
1313 simply won't wipe userdata, which is the correct default behavior
1314 */
1315 int N = 1 << crypt_ftr->N_factor;
1316 int r = 1 << crypt_ftr->r_factor;
1317 int p = 1 << crypt_ftr->p_factor;
1318
1319 rc = crypto_scrypt(ikey, KEY_LEN_BYTES,
1320 crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p,
1321 crypt_ftr->scrypted_intermediate_key,
1322 sizeof(crypt_ftr->scrypted_intermediate_key));
1323
1324 if (rc) {
1325 SLOGE("encrypt_master_key: crypto_scrypt failed");
1326 }
1327
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001328 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001329}
1330
JP Abgrall7bdfa522013-11-15 13:42:56 -08001331static int decrypt_master_key_aux(char *passwd, unsigned char *salt,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001332 unsigned char *encrypted_master_key,
1333 unsigned char *decrypted_master_key,
1334 kdf_func kdf, void *kdf_params,
1335 unsigned char** intermediate_key,
1336 size_t* intermediate_key_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001337{
1338 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 -08001339 EVP_CIPHER_CTX d_ctx;
1340 int decrypted_len, final_len;
1341
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001342 /* Turn the password into an intermediate key and IV that can decrypt the
1343 master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001344 if (kdf(passwd, salt, ikey, kdf_params)) {
1345 SLOGE("kdf failed");
1346 return -1;
1347 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001348
1349 /* Initialize the decryption engine */
1350 if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
1351 return -1;
1352 }
1353 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1354 /* Decrypt the master key */
1355 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
1356 encrypted_master_key, KEY_LEN_BYTES)) {
1357 return -1;
1358 }
1359 if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
1360 return -1;
1361 }
1362
1363 if (decrypted_len + final_len != KEY_LEN_BYTES) {
1364 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001365 }
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001366
1367 /* Copy intermediate key if needed by params */
1368 if (intermediate_key && intermediate_key_size) {
1369 *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES);
1370 if (intermediate_key) {
1371 memcpy(*intermediate_key, ikey, KEY_LEN_BYTES);
1372 *intermediate_key_size = KEY_LEN_BYTES;
1373 }
1374 }
1375
1376 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001377}
1378
Kenny Rootc4c70f12013-06-14 12:11:38 -07001379static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001380{
Shawn Willdene17a9c42014-09-08 13:04:08 -06001381 if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER_UNPADDED ||
1382 ftr->kdf_type == KDF_SCRYPT_KEYMASTER_BADLY_PADDED ||
1383 ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001384 *kdf = scrypt_keymaster;
1385 *kdf_params = ftr;
1386 } else if (ftr->kdf_type == KDF_SCRYPT) {
Kenny Rootc4c70f12013-06-14 12:11:38 -07001387 *kdf = scrypt;
1388 *kdf_params = ftr;
1389 } else {
1390 *kdf = pbkdf2;
1391 *kdf_params = NULL;
1392 }
1393}
1394
JP Abgrall7bdfa522013-11-15 13:42:56 -08001395static int decrypt_master_key(char *passwd, unsigned char *decrypted_master_key,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001396 struct crypt_mnt_ftr *crypt_ftr,
1397 unsigned char** intermediate_key,
1398 size_t* intermediate_key_size)
Kenny Rootc4c70f12013-06-14 12:11:38 -07001399{
1400 kdf_func kdf;
1401 void *kdf_params;
1402 int ret;
1403
1404 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001405 ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key,
1406 decrypted_master_key, kdf, kdf_params,
1407 intermediate_key, intermediate_key_size);
Kenny Rootc4c70f12013-06-14 12:11:38 -07001408 if (ret != 0) {
1409 SLOGW("failure decrypting master key");
Kenny Rootc4c70f12013-06-14 12:11:38 -07001410 }
1411
1412 return ret;
1413}
1414
1415static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt,
1416 struct crypt_mnt_ftr *crypt_ftr) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001417 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -08001418 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001419 EVP_CIPHER_CTX e_ctx;
1420 int encrypted_len, final_len;
1421
1422 /* Get some random bits for a key */
1423 fd = open("/dev/urandom", O_RDONLY);
Ken Sumralle8744072011-01-18 22:01:55 -08001424 read(fd, key_buf, sizeof(key_buf));
1425 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001426 close(fd);
1427
1428 /* Now encrypt it with the password */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001429 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001430}
1431
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001432static int wait_and_unmount(char *mountpoint, bool kill)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001433{
Greg Hackmann955653e2014-09-24 14:55:20 -07001434 int i, err, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001435#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001436
1437 /* Now umount the tmpfs filesystem */
1438 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001439 if (umount(mountpoint) == 0) {
1440 break;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001441 }
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001442
1443 if (errno == EINVAL) {
1444 /* EINVAL is returned if the directory is not a mountpoint,
1445 * i.e. there is no filesystem mounted there. So just get out.
1446 */
1447 break;
1448 }
1449
1450 err = errno;
1451
1452 /* If allowed, be increasingly aggressive before the last two retries */
1453 if (kill) {
1454 if (i == (WAIT_UNMOUNT_COUNT - 3)) {
1455 SLOGW("sending SIGHUP to processes with open files\n");
1456 vold_killProcessesWithOpenFiles(mountpoint, 1);
1457 } else if (i == (WAIT_UNMOUNT_COUNT - 2)) {
1458 SLOGW("sending SIGKILL to processes with open files\n");
1459 vold_killProcessesWithOpenFiles(mountpoint, 2);
1460 }
1461 }
1462
1463 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001464 }
1465
1466 if (i < WAIT_UNMOUNT_COUNT) {
1467 SLOGD("unmounting %s succeeded\n", mountpoint);
1468 rc = 0;
1469 } else {
jessica_yu3f14fe42014-09-22 15:57:40 +08001470 vold_killProcessesWithOpenFiles(mountpoint, 0);
Greg Hackmann955653e2014-09-24 14:55:20 -07001471 SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001472 rc = -1;
1473 }
1474
1475 return rc;
1476}
1477
Ken Sumrallc5872692013-05-14 15:26:31 -07001478#define DATA_PREP_TIMEOUT 200
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001479static int prep_data_fs(void)
1480{
1481 int i;
1482
1483 /* Do the prep of the /data filesystem */
1484 property_set("vold.post_fs_data_done", "0");
1485 property_set("vold.decrypt", "trigger_post_fs_data");
1486 SLOGD("Just triggered post_fs_data\n");
1487
Ken Sumrallc5872692013-05-14 15:26:31 -07001488 /* Wait a max of 50 seconds, hopefully it takes much less */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001489 for (i=0; i<DATA_PREP_TIMEOUT; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001490 char p[PROPERTY_VALUE_MAX];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001491
1492 property_get("vold.post_fs_data_done", p, "0");
1493 if (*p == '1') {
1494 break;
1495 } else {
1496 usleep(250000);
1497 }
1498 }
1499 if (i == DATA_PREP_TIMEOUT) {
1500 /* Ugh, we failed to prep /data in time. Bail. */
Ken Sumrallc5872692013-05-14 15:26:31 -07001501 SLOGE("post_fs_data timed out!\n");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001502 return -1;
1503 } else {
1504 SLOGD("post_fs_data done\n");
1505 return 0;
1506 }
1507}
1508
Paul Lawrence74f29f12014-08-28 15:54:10 -07001509static void cryptfs_set_corrupt()
1510{
1511 // Mark the footer as bad
1512 struct crypt_mnt_ftr crypt_ftr;
1513 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1514 SLOGE("Failed to get crypto footer - panic");
1515 return;
1516 }
1517
1518 crypt_ftr.flags |= CRYPT_DATA_CORRUPT;
1519 if (put_crypt_ftr_and_key(&crypt_ftr)) {
1520 SLOGE("Failed to set crypto footer - panic");
1521 return;
1522 }
1523}
1524
1525static void cryptfs_trigger_restart_min_framework()
1526{
1527 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
1528 SLOGE("Failed to mount tmpfs on data - panic");
1529 return;
1530 }
1531
1532 if (property_set("vold.decrypt", "trigger_post_fs_data")) {
1533 SLOGE("Failed to trigger post fs data - panic");
1534 return;
1535 }
1536
1537 if (property_set("vold.decrypt", "trigger_restart_min_framework")) {
1538 SLOGE("Failed to trigger restart min framework - panic");
1539 return;
1540 }
1541}
1542
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001543/* returns < 0 on failure */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001544static int cryptfs_restart_internal(int restart_main)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001545{
1546 char fs_type[32];
1547 char real_blkdev[MAXPATHLEN];
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001548 char crypto_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001549 char fs_options[256];
1550 unsigned long mnt_flags;
1551 struct stat statbuf;
1552 int rc = -1, i;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001553 static int restart_successful = 0;
1554
1555 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -06001556 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001557 SLOGE("Encrypted filesystem not validated, aborting");
1558 return -1;
1559 }
1560
1561 if (restart_successful) {
1562 SLOGE("System already restarted with encrypted disk, aborting");
1563 return -1;
1564 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001565
Paul Lawrencef4faa572014-01-29 13:31:03 -08001566 if (restart_main) {
1567 /* Here is where we shut down the framework. The init scripts
1568 * start all services in one of three classes: core, main or late_start.
1569 * On boot, we start core and main. Now, we stop main, but not core,
1570 * as core includes vold and a few other really important things that
1571 * we need to keep running. Once main has stopped, we should be able
1572 * to umount the tmpfs /data, then mount the encrypted /data.
1573 * We then restart the class main, and also the class late_start.
1574 * At the moment, I've only put a few things in late_start that I know
1575 * are not needed to bring up the framework, and that also cause problems
1576 * with unmounting the tmpfs /data, but I hope to add add more services
1577 * to the late_start class as we optimize this to decrease the delay
1578 * till the user is asked for the password to the filesystem.
1579 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001580
Paul Lawrencef4faa572014-01-29 13:31:03 -08001581 /* The init files are setup to stop the class main when vold.decrypt is
1582 * set to trigger_reset_main.
1583 */
1584 property_set("vold.decrypt", "trigger_reset_main");
1585 SLOGD("Just asked init to shut down class main\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001586
Paul Lawrencef4faa572014-01-29 13:31:03 -08001587 /* Ugh, shutting down the framework is not synchronous, so until it
1588 * can be fixed, this horrible hack will wait a moment for it all to
1589 * shut down before proceeding. Without it, some devices cannot
1590 * restart the graphics services.
1591 */
1592 sleep(2);
1593 }
Ken Sumrall9dedfd42012-10-09 14:16:59 -07001594
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001595 /* Now that the framework is shutdown, we should be able to umount()
1596 * the tmpfs filesystem, and mount the real one.
1597 */
1598
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001599 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1600 if (strlen(crypto_blkdev) == 0) {
1601 SLOGE("fs_crypto_blkdev not set\n");
1602 return -1;
1603 }
1604
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001605 if (! (rc = wait_and_unmount(DATA_MNT_POINT, true)) ) {
Doug Zongker6fd57712013-12-17 09:43:23 -08001606 /* If ro.crypto.readonly is set to 1, mount the decrypted
1607 * filesystem readonly. This is used when /data is mounted by
1608 * recovery mode.
1609 */
1610 char ro_prop[PROPERTY_VALUE_MAX];
1611 property_get("ro.crypto.readonly", ro_prop, "");
1612 if (strlen(ro_prop) > 0 && atoi(ro_prop)) {
1613 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
1614 rec->flags |= MS_RDONLY;
1615 }
JP Abgrall62c7af32014-06-16 13:01:23 -07001616
Ken Sumralle5032c42012-04-01 23:58:44 -07001617 /* If that succeeded, then mount the decrypted filesystem */
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001618 int retries = RETRY_MOUNT_ATTEMPTS;
1619 int mount_rc;
1620 while ((mount_rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT,
1621 crypto_blkdev, 0))
1622 != 0) {
1623 if (mount_rc == FS_MGR_DOMNT_BUSY) {
1624 /* TODO: invoke something similar to
1625 Process::killProcessWithOpenFiles(DATA_MNT_POINT,
1626 retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */
1627 SLOGI("Failed to mount %s because it is busy - waiting",
1628 crypto_blkdev);
1629 if (--retries) {
1630 sleep(RETRY_MOUNT_DELAY_SECONDS);
1631 } else {
1632 /* Let's hope that a reboot clears away whatever is keeping
1633 the mount busy */
1634 cryptfs_reboot(reboot);
1635 }
1636 } else {
1637 SLOGE("Failed to mount decrypted data");
1638 cryptfs_set_corrupt();
1639 cryptfs_trigger_restart_min_framework();
1640 SLOGI("Started framework to offer wipe");
1641 return -1;
1642 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001643 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001644
Ken Sumralle5032c42012-04-01 23:58:44 -07001645 property_set("vold.decrypt", "trigger_load_persist_props");
1646 /* Create necessary paths on /data */
1647 if (prep_data_fs()) {
1648 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001649 }
Ken Sumralle5032c42012-04-01 23:58:44 -07001650
1651 /* startup service classes main and late_start */
1652 property_set("vold.decrypt", "trigger_restart_framework");
1653 SLOGD("Just triggered restart_framework\n");
1654
1655 /* Give it a few moments to get started */
1656 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001657 }
1658
Ken Sumrall0cc16632011-01-18 20:32:26 -08001659 if (rc == 0) {
1660 restart_successful = 1;
1661 }
1662
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001663 return rc;
1664}
1665
Paul Lawrencef4faa572014-01-29 13:31:03 -08001666int cryptfs_restart(void)
1667{
1668 /* Call internal implementation forcing a restart of main service group */
1669 return cryptfs_restart_internal(1);
1670}
1671
Mark Salyzyn3e971272014-01-21 13:27:04 -08001672static int do_crypto_complete(char *mount_point UNUSED)
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001673{
1674 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001675 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -08001676 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001677
1678 property_get("ro.crypto.state", encrypted_state, "");
1679 if (strcmp(encrypted_state, "encrypted") ) {
1680 SLOGE("not running with encryption, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001681 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001682 }
1683
Ken Sumrall160b4d62013-04-22 12:15:39 -07001684 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001685 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumralle5032c42012-04-01 23:58:44 -07001686
Ken Sumralle1a45852011-12-14 21:24:27 -08001687 /*
1688 * Only report this error if key_loc is a file and it exists.
1689 * If the device was never encrypted, and /data is not mountable for
1690 * some reason, returning 1 should prevent the UI from presenting the
1691 * a "enter password" screen, or worse, a "press button to wipe the
1692 * device" screen.
1693 */
1694 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
1695 SLOGE("master key file does not exist, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001696 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumralle1a45852011-12-14 21:24:27 -08001697 } else {
1698 SLOGE("Error getting crypt footer and key\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001699 return CRYPTO_COMPLETE_BAD_METADATA;
Ken Sumralle1a45852011-12-14 21:24:27 -08001700 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001701 }
1702
Paul Lawrence74f29f12014-08-28 15:54:10 -07001703 // Test for possible error flags
1704 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS){
1705 SLOGE("Encryption process is partway completed\n");
1706 return CRYPTO_COMPLETE_PARTIAL;
1707 }
1708
1709 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE){
1710 SLOGE("Encryption process was interrupted but cannot continue\n");
1711 return CRYPTO_COMPLETE_INCONSISTENT;
1712 }
1713
1714 if (crypt_ftr.flags & CRYPT_DATA_CORRUPT){
1715 SLOGE("Encryption is successful but data is corrupt\n");
1716 return CRYPTO_COMPLETE_CORRUPT;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001717 }
1718
1719 /* We passed the test! We shall diminish, and return to the west */
Paul Lawrence74f29f12014-08-28 15:54:10 -07001720 return CRYPTO_COMPLETE_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001721}
1722
Paul Lawrencef4faa572014-01-29 13:31:03 -08001723static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
1724 char *passwd, char *mount_point, char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001725{
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001726 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001727 unsigned char decrypted_master_key[32];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001728 char crypto_blkdev[MAXPATHLEN];
1729 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001730 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001731 unsigned int orig_failed_decrypt_count;
1732 int rc;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001733 kdf_func kdf;
1734 void *kdf_params;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001735 int use_keymaster = 0;
1736 int upgrade = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001737 unsigned char* intermediate_key = 0;
1738 size_t intermediate_key_size = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001739
Paul Lawrencef4faa572014-01-29 13:31:03 -08001740 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1741 orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001742
Paul Lawrencef4faa572014-01-29 13:31:03 -08001743 if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001744 if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr,
1745 &intermediate_key, &intermediate_key_size)) {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001746 SLOGE("Failed to decrypt master key\n");
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001747 rc = -1;
1748 goto errout;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001749 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001750 }
1751
Paul Lawrencef4faa572014-01-29 13:31:03 -08001752 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
1753
Paul Lawrence74f29f12014-08-28 15:54:10 -07001754 // Create crypto block device - all (non fatal) code paths
1755 // need it
Paul Lawrencef4faa572014-01-29 13:31:03 -08001756 if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key,
1757 real_blkdev, crypto_blkdev, label)) {
Paul Lawrence74f29f12014-08-28 15:54:10 -07001758 SLOGE("Error creating decrypted block device\n");
1759 rc = -1;
1760 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001761 }
1762
Paul Lawrence74f29f12014-08-28 15:54:10 -07001763 /* Work out if the problem is the password or the data */
1764 unsigned char scrypted_intermediate_key[sizeof(crypt_ftr->
1765 scrypted_intermediate_key)];
1766 int N = 1 << crypt_ftr->N_factor;
1767 int r = 1 << crypt_ftr->r_factor;
1768 int p = 1 << crypt_ftr->p_factor;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001769
Paul Lawrence74f29f12014-08-28 15:54:10 -07001770 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
1771 crypt_ftr->salt, sizeof(crypt_ftr->salt),
1772 N, r, p, scrypted_intermediate_key,
1773 sizeof(scrypted_intermediate_key));
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001774
Paul Lawrence74f29f12014-08-28 15:54:10 -07001775 // Does the key match the crypto footer?
1776 if (rc == 0 && memcmp(scrypted_intermediate_key,
1777 crypt_ftr->scrypted_intermediate_key,
1778 sizeof(scrypted_intermediate_key)) == 0) {
1779 SLOGI("Password matches");
1780 rc = 0;
1781 } else {
1782 /* Try mounting the file system anyway, just in case the problem's with
1783 * the footer, not the key. */
1784 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
1785 mkdir(tmp_mount_point, 0755);
1786 if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
1787 SLOGE("Error temp mounting decrypted block device\n");
1788 delete_crypto_blk_dev(label);
1789
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001790 rc = ++crypt_ftr->failed_decrypt_count;
1791 put_crypt_ftr_and_key(crypt_ftr);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001792 } else {
1793 /* Success! */
1794 SLOGI("Password did not match but decrypted drive mounted - continue");
1795 umount(tmp_mount_point);
1796 rc = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001797 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001798 }
1799
1800 if (rc == 0) {
1801 crypt_ftr->failed_decrypt_count = 0;
Paul Lawrence72b8b822014-10-05 12:57:37 -07001802 if (orig_failed_decrypt_count != 0) {
1803 put_crypt_ftr_and_key(crypt_ftr);
1804 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001805
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001806 /* Save the name of the crypto block device
Paul Lawrence74f29f12014-08-28 15:54:10 -07001807 * so we can mount it when restarting the framework. */
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001808 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -06001809
1810 /* Also save a the master key so we can reencrypted the key
Paul Lawrence74f29f12014-08-28 15:54:10 -07001811 * the key when we want to change the password on it. */
Jason parks70a4b3f2011-01-28 10:10:47 -06001812 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001813 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -06001814 master_key_saved = 1;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001815 SLOGD("%s(): Master key saved\n", __FUNCTION__);
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001816 rc = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001817
Paul Lawrence74f29f12014-08-28 15:54:10 -07001818 // Upgrade if we're not using the latest KDF.
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001819 use_keymaster = keymaster_check_compatibility();
1820 if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Shawn Willden47ba10d2014-09-03 17:07:06 -06001821 // Don't allow downgrade
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001822 } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) {
1823 crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1824 upgrade = 1;
1825 } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001826 crypt_ftr->kdf_type = KDF_SCRYPT;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001827 upgrade = 1;
1828 }
1829
1830 if (upgrade) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001831 rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1832 crypt_ftr->master_key, crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001833 if (!rc) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001834 rc = put_crypt_ftr_and_key(crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001835 }
1836 SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
Paul Lawrenceb2f682b2014-09-08 11:28:19 -07001837
1838 // Do not fail even if upgrade failed - machine is bootable
1839 // Note that if this code is ever hit, there is a *serious* problem
1840 // since KDFs should never fail. You *must* fix the kdf before
1841 // proceeding!
1842 if (rc) {
1843 SLOGW("Upgrade failed with error %d,"
1844 " but continuing with previous state",
1845 rc);
1846 rc = 0;
1847 }
JP Abgrall7bdfa522013-11-15 13:42:56 -08001848 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001849 }
1850
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001851 errout:
1852 if (intermediate_key) {
1853 memset(intermediate_key, 0, intermediate_key_size);
1854 free(intermediate_key);
1855 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001856 return rc;
1857}
1858
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001859/* Called by vold when it wants to undo the crypto mapping of a volume it
1860 * manages. This is usually in response to a factory reset, when we want
1861 * to undo the crypto mapping so the volume is formatted in the clear.
1862 */
1863int cryptfs_revert_volume(const char *label)
1864{
1865 return delete_crypto_blk_dev((char *)label);
1866}
1867
Ken Sumrall29d8da82011-05-18 17:20:07 -07001868/*
1869 * Called by vold when it's asked to mount an encrypted, nonremovable volume.
1870 * Setup a dm-crypt mapping, use the saved master key from
1871 * setting up the /data mapping, and return the new device path.
1872 */
1873int cryptfs_setup_volume(const char *label, int major, int minor,
1874 char *crypto_sys_path, unsigned int max_path,
1875 int *new_major, int *new_minor)
1876{
1877 char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN];
1878 struct crypt_mnt_ftr sd_crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001879 struct stat statbuf;
1880 int nr_sec, fd;
1881
1882 sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor);
1883
Ken Sumrall160b4d62013-04-22 12:15:39 -07001884 get_crypt_ftr_and_key(&sd_crypt_ftr);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001885
1886 /* Update the fs_size field to be the size of the volume */
1887 fd = open(real_blkdev, O_RDONLY);
1888 nr_sec = get_blkdev_size(fd);
1889 close(fd);
1890 if (nr_sec == 0) {
1891 SLOGE("Cannot get size of volume %s\n", real_blkdev);
1892 return -1;
1893 }
1894
1895 sd_crypt_ftr.fs_size = nr_sec;
1896 create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev,
1897 crypto_blkdev, label);
1898
JP Abgrall3334c6a2014-10-10 15:52:11 -07001899 if (stat(crypto_blkdev, &statbuf) < 0) {
1900 SLOGE("Error get stat for crypto_blkdev %s. err=%d(%s)\n",
1901 crypto_blkdev, errno, strerror(errno));
1902 }
Ken Sumrall29d8da82011-05-18 17:20:07 -07001903 *new_major = MAJOR(statbuf.st_rdev);
1904 *new_minor = MINOR(statbuf.st_rdev);
1905
1906 /* Create path to sys entry for this block device */
1907 snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1);
1908
1909 return 0;
1910}
1911
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001912int cryptfs_crypto_complete(void)
1913{
1914 return do_crypto_complete("/data");
1915}
1916
Paul Lawrencef4faa572014-01-29 13:31:03 -08001917int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr)
1918{
1919 char encrypted_state[PROPERTY_VALUE_MAX];
1920 property_get("ro.crypto.state", encrypted_state, "");
1921 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
1922 SLOGE("encrypted fs already validated or not running with encryption,"
1923 " aborting");
1924 return -1;
1925 }
1926
1927 if (get_crypt_ftr_and_key(crypt_ftr)) {
1928 SLOGE("Error getting crypt footer and key");
1929 return -1;
1930 }
1931
1932 return 0;
1933}
1934
Paul Lawrencefc615042014-10-04 15:32:29 -07001935/*
1936 * TODO - transition patterns to new format in calling code
1937 * and remove this vile hack, and the use of hex in
1938 * the password passing code.
1939 *
1940 * Patterns are passed in zero based (i.e. the top left dot
1941 * is represented by zero, the top middle one etc), but we want
1942 * to store them '1' based.
1943 * This is to allow us to migrate the calling code to use this
1944 * convention. It also solves a nasty problem whereby scrypt ignores
1945 * trailing zeros, so patterns ending at the top left could be
1946 * truncated, and similarly, you could add the top left to any
1947 * pattern and still match.
1948 * adjust_passwd is a hack function that returns the alternate representation
1949 * if the password appears to be a pattern (hex numbers all less than 09)
1950 * If it succeeds we need to try both, and in particular try the alternate
1951 * first. If the original matches, then we need to update the footer
1952 * with the alternate.
1953 * All code that accepts passwords must adjust them first. Since
1954 * cryptfs_check_passwd is always the first function called after a migration
1955 * (and indeed on any boot) we only need to do the double try in this
1956 * function.
1957 */
1958char* adjust_passwd(const char* passwd)
1959{
1960 size_t index, length;
1961
1962 if (!passwd) {
1963 return 0;
1964 }
1965
1966 // Check even length. Hex encoded passwords are always
1967 // an even length, since each character encodes to two characters.
1968 length = strlen(passwd);
1969 if (length % 2) {
1970 SLOGW("Password not correctly hex encoded.");
1971 return 0;
1972 }
1973
1974 // Check password is old-style pattern - a collection of hex
1975 // encoded bytes less than 9 (00 through 08)
1976 for (index = 0; index < length; index +=2) {
1977 if (passwd[index] != '0'
1978 || passwd[index + 1] < '0' || passwd[index + 1] > '8') {
1979 return 0;
1980 }
1981 }
1982
1983 // Allocate room for adjusted passwd and null terminate
1984 char* adjusted = malloc(length + 1);
1985 adjusted[length] = 0;
1986
1987 // Add 0x31 ('1') to each character
1988 for (index = 0; index < length; index += 2) {
1989 // output is 31 through 39 so set first byte to three, second to src + 1
1990 adjusted[index] = '3';
1991 adjusted[index + 1] = passwd[index + 1] + 1;
1992 }
1993
1994 return adjusted;
1995}
1996
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001997int cryptfs_check_passwd(char *passwd)
1998{
Paul Lawrencef4faa572014-01-29 13:31:03 -08001999 struct crypt_mnt_ftr crypt_ftr;
2000 int rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002001
Paul Lawrencef4faa572014-01-29 13:31:03 -08002002 rc = check_unmounted_and_get_ftr(&crypt_ftr);
2003 if (rc)
2004 return rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002005
Paul Lawrencefc615042014-10-04 15:32:29 -07002006 char* adjusted_passwd = adjust_passwd(passwd);
2007 if (adjusted_passwd) {
2008 int failed_decrypt_count = crypt_ftr.failed_decrypt_count;
2009 rc = test_mount_encrypted_fs(&crypt_ftr, adjusted_passwd,
2010 DATA_MNT_POINT, "userdata");
2011
2012 // Maybe the original one still works?
2013 if (rc) {
2014 // Don't double count this failure
2015 crypt_ftr.failed_decrypt_count = failed_decrypt_count;
2016 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
2017 DATA_MNT_POINT, "userdata");
2018 if (!rc) {
2019 // cryptfs_changepw also adjusts so pass original
2020 // Note that adjust_passwd only recognises patterns
2021 // so we can safely use CRYPT_TYPE_PATTERN
2022 SLOGI("Updating pattern to new format");
2023 cryptfs_changepw(CRYPT_TYPE_PATTERN, passwd);
2024 }
2025 }
2026 free(adjusted_passwd);
2027 } else {
2028 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
2029 DATA_MNT_POINT, "userdata");
2030 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002031
2032 if (rc == 0 && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
Paul Lawrence399317e2014-03-10 13:20:50 -07002033 cryptfs_clear_password();
2034 password = strdup(passwd);
2035 struct timespec now;
2036 clock_gettime(CLOCK_BOOTTIME, &now);
2037 password_expiry_time = now.tv_sec + password_max_age_seconds;
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002038 }
2039
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002040 return rc;
2041}
2042
Ken Sumrall3ad90722011-10-04 20:38:29 -07002043int cryptfs_verify_passwd(char *passwd)
2044{
2045 struct crypt_mnt_ftr crypt_ftr;
2046 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002047 unsigned char decrypted_master_key[32];
Ken Sumrall3ad90722011-10-04 20:38:29 -07002048 char encrypted_state[PROPERTY_VALUE_MAX];
2049 int rc;
2050
2051 property_get("ro.crypto.state", encrypted_state, "");
2052 if (strcmp(encrypted_state, "encrypted") ) {
2053 SLOGE("device not encrypted, aborting");
2054 return -2;
2055 }
2056
2057 if (!master_key_saved) {
2058 SLOGE("encrypted fs not yet mounted, aborting");
2059 return -1;
2060 }
2061
2062 if (!saved_mount_point) {
2063 SLOGE("encrypted fs failed to save mount point, aborting");
2064 return -1;
2065 }
2066
Ken Sumrall160b4d62013-04-22 12:15:39 -07002067 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall3ad90722011-10-04 20:38:29 -07002068 SLOGE("Error getting crypt footer and key\n");
2069 return -1;
2070 }
2071
2072 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
2073 /* If the device has no password, then just say the password is valid */
2074 rc = 0;
2075 } else {
Paul Lawrencefc615042014-10-04 15:32:29 -07002076 char* adjusted_passwd = adjust_passwd(passwd);
2077 if (adjusted_passwd) {
2078 passwd = adjusted_passwd;
2079 }
2080
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002081 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall3ad90722011-10-04 20:38:29 -07002082 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
2083 /* They match, the password is correct */
2084 rc = 0;
2085 } else {
2086 /* If incorrect, sleep for a bit to prevent dictionary attacks */
2087 sleep(1);
2088 rc = 1;
2089 }
Paul Lawrencefc615042014-10-04 15:32:29 -07002090
2091 free(adjusted_passwd);
Ken Sumrall3ad90722011-10-04 20:38:29 -07002092 }
2093
2094 return rc;
2095}
2096
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002097/* Initialize a crypt_mnt_ftr structure. The keysize is
2098 * defaulted to 16 bytes, and the filesystem size to 0.
2099 * Presumably, at a minimum, the caller will update the
2100 * filesystem size and crypto_type_name after calling this function.
2101 */
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002102static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002103{
Ken Sumrall160b4d62013-04-22 12:15:39 -07002104 off64_t off;
2105
2106 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002107 ftr->magic = CRYPT_MNT_MAGIC;
Kenny Rootc96a5f82013-06-14 12:08:28 -07002108 ftr->major_version = CURRENT_MAJOR_VERSION;
2109 ftr->minor_version = CURRENT_MINOR_VERSION;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002110 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Jason parks70a4b3f2011-01-28 10:10:47 -06002111 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002112
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002113 switch (keymaster_check_compatibility()) {
2114 case 1:
2115 ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
2116 break;
2117
2118 case 0:
2119 ftr->kdf_type = KDF_SCRYPT;
2120 break;
2121
2122 default:
2123 SLOGE("keymaster_check_compatibility failed");
2124 return -1;
2125 }
2126
Kenny Rootc4c70f12013-06-14 12:11:38 -07002127 get_device_scrypt_params(ftr);
2128
Ken Sumrall160b4d62013-04-22 12:15:39 -07002129 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
2130 if (get_crypt_ftr_info(NULL, &off) == 0) {
2131 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
2132 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
2133 ftr->persist_data_size;
2134 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002135
2136 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002137}
2138
Ken Sumrall29d8da82011-05-18 17:20:07 -07002139static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002140{
Ken Sumralle550f782013-08-20 13:48:23 -07002141 const char *args[10];
2142 char size_str[32]; /* Must be large enough to hold a %lld and null byte */
2143 int num_args;
2144 int status;
2145 int tmp;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002146 int rc = -1;
2147
Ken Sumrall29d8da82011-05-18 17:20:07 -07002148 if (type == EXT4_FS) {
Ken Sumralle550f782013-08-20 13:48:23 -07002149 args[0] = "/system/bin/make_ext4fs";
2150 args[1] = "-a";
2151 args[2] = "/data";
2152 args[3] = "-l";
Elliott Hughes73737162014-06-25 17:27:42 -07002153 snprintf(size_str, sizeof(size_str), "%" PRId64, size * 512);
Ken Sumralle550f782013-08-20 13:48:23 -07002154 args[4] = size_str;
2155 args[5] = crypto_blkdev;
2156 num_args = 6;
2157 SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n",
2158 args[0], args[1], args[2], args[3], args[4], args[5]);
JP Abgrall62c7af32014-06-16 13:01:23 -07002159 } else if (type == F2FS_FS) {
2160 args[0] = "/system/bin/mkfs.f2fs";
2161 args[1] = "-t";
2162 args[2] = "-d1";
2163 args[3] = crypto_blkdev;
Elliott Hughes73737162014-06-25 17:27:42 -07002164 snprintf(size_str, sizeof(size_str), "%" PRId64, size);
JP Abgrall62c7af32014-06-16 13:01:23 -07002165 args[4] = size_str;
2166 num_args = 5;
2167 SLOGI("Making empty filesystem with command %s %s %s %s %s\n",
2168 args[0], args[1], args[2], args[3], args[4]);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002169 } else {
2170 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
2171 return -1;
2172 }
2173
Ken Sumralle550f782013-08-20 13:48:23 -07002174 tmp = android_fork_execvp(num_args, (char **)args, &status, false, true);
2175
2176 if (tmp != 0) {
2177 SLOGE("Error creating empty filesystem on %s due to logwrap error\n", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002178 } else {
Ken Sumralle550f782013-08-20 13:48:23 -07002179 if (WIFEXITED(status)) {
2180 if (WEXITSTATUS(status)) {
2181 SLOGE("Error creating filesystem on %s, exit status %d ",
2182 crypto_blkdev, WEXITSTATUS(status));
2183 } else {
2184 SLOGD("Successfully created filesystem on %s\n", crypto_blkdev);
2185 rc = 0;
2186 }
2187 } else {
2188 SLOGE("Error creating filesystem on %s, did not exit normally\n", crypto_blkdev);
2189 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002190 }
2191
2192 return rc;
2193}
2194
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002195#define CRYPT_INPLACE_BUFSIZE 4096
Paul Lawrence87999172014-02-20 12:21:31 -08002196#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / CRYPT_SECTOR_SIZE)
2197#define CRYPT_SECTOR_SIZE 512
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002198
2199/* aligned 32K writes tends to make flash happy.
2200 * SD card association recommends it.
2201 */
2202#define BLOCKS_AT_A_TIME 8
2203
2204struct encryptGroupsData
2205{
2206 int realfd;
2207 int cryptofd;
2208 off64_t numblocks;
2209 off64_t one_pct, cur_pct, new_pct;
2210 off64_t blocks_already_done, tot_numblocks;
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002211 off64_t used_blocks_already_done, tot_used_blocks;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002212 char* real_blkdev, * crypto_blkdev;
2213 int count;
2214 off64_t offset;
2215 char* buffer;
Paul Lawrence87999172014-02-20 12:21:31 -08002216 off64_t last_written_sector;
2217 int completed;
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002218 time_t time_started;
2219 int remaining_time;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002220};
2221
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002222static void update_progress(struct encryptGroupsData* data, int is_used)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002223{
2224 data->blocks_already_done++;
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002225
2226 if (is_used) {
2227 data->used_blocks_already_done++;
2228 }
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002229 if (data->tot_used_blocks) {
2230 data->new_pct = data->used_blocks_already_done / data->one_pct;
2231 } else {
2232 data->new_pct = data->blocks_already_done / data->one_pct;
2233 }
2234
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002235 if (data->new_pct > data->cur_pct) {
2236 char buf[8];
2237 data->cur_pct = data->new_pct;
Elliott Hughescb33f572014-06-25 18:25:11 -07002238 snprintf(buf, sizeof(buf), "%" PRId64, data->cur_pct);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002239 property_set("vold.encrypt_progress", buf);
2240 }
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002241
2242 if (data->cur_pct >= 5) {
Paul Lawrence9c58a872014-09-30 09:12:51 -07002243 struct timespec time_now;
2244 if (clock_gettime(CLOCK_MONOTONIC, &time_now)) {
2245 SLOGW("Error getting time");
2246 } else {
2247 double elapsed_time = difftime(time_now.tv_sec, data->time_started);
2248 off64_t remaining_blocks = data->tot_used_blocks
2249 - data->used_blocks_already_done;
2250 int remaining_time = (int)(elapsed_time * remaining_blocks
2251 / data->used_blocks_already_done);
Paul Lawrence71577502014-08-13 14:55:55 -07002252
Paul Lawrence9c58a872014-09-30 09:12:51 -07002253 // Change time only if not yet set, lower, or a lot higher for
2254 // best user experience
2255 if (data->remaining_time == -1
2256 || remaining_time < data->remaining_time
2257 || remaining_time > data->remaining_time + 60) {
2258 char buf[8];
2259 snprintf(buf, sizeof(buf), "%d", remaining_time);
2260 property_set("vold.encrypt_time_remaining", buf);
2261 data->remaining_time = remaining_time;
2262 }
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002263 }
2264 }
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002265}
2266
Paul Lawrence3846be12014-09-22 11:33:54 -07002267static void log_progress(struct encryptGroupsData const* data, bool completed)
2268{
2269 // Precondition - if completed data = 0 else data != 0
2270
2271 // Track progress so we can skip logging blocks
2272 static off64_t offset = -1;
2273
2274 // Need to close existing 'Encrypting from' log?
2275 if (completed || (offset != -1 && data->offset != offset)) {
2276 SLOGI("Encrypted to sector %" PRId64,
2277 offset / info.block_size * CRYPT_SECTOR_SIZE);
2278 offset = -1;
2279 }
2280
2281 // Need to start new 'Encrypting from' log?
2282 if (!completed && offset != data->offset) {
2283 SLOGI("Encrypting from sector %" PRId64,
2284 data->offset / info.block_size * CRYPT_SECTOR_SIZE);
2285 }
2286
2287 // Update offset
2288 if (!completed) {
2289 offset = data->offset + (off64_t)data->count * info.block_size;
2290 }
2291}
2292
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002293static int flush_outstanding_data(struct encryptGroupsData* data)
2294{
2295 if (data->count == 0) {
2296 return 0;
2297 }
2298
Elliott Hughes231bdba2014-06-25 18:36:19 -07002299 SLOGV("Copying %d blocks at offset %" PRIx64, data->count, data->offset);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002300
2301 if (pread64(data->realfd, data->buffer,
2302 info.block_size * data->count, data->offset)
2303 <= 0) {
2304 SLOGE("Error reading real_blkdev %s for inplace encrypt",
2305 data->real_blkdev);
2306 return -1;
2307 }
2308
2309 if (pwrite64(data->cryptofd, data->buffer,
2310 info.block_size * data->count, data->offset)
2311 <= 0) {
2312 SLOGE("Error writing crypto_blkdev %s for inplace encrypt",
2313 data->crypto_blkdev);
2314 return -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002315 } else {
Paul Lawrence3846be12014-09-22 11:33:54 -07002316 log_progress(data, false);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002317 }
2318
2319 data->count = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002320 data->last_written_sector = (data->offset + data->count)
2321 / info.block_size * CRYPT_SECTOR_SIZE - 1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002322 return 0;
2323}
2324
2325static int encrypt_groups(struct encryptGroupsData* data)
2326{
2327 unsigned int i;
2328 u8 *block_bitmap = 0;
2329 unsigned int block;
2330 off64_t ret;
2331 int rc = -1;
2332
2333 data->buffer = malloc(info.block_size * BLOCKS_AT_A_TIME);
2334 if (!data->buffer) {
2335 SLOGE("Failed to allocate crypto buffer");
2336 goto errout;
2337 }
2338
2339 block_bitmap = malloc(info.block_size);
2340 if (!block_bitmap) {
2341 SLOGE("failed to allocate block bitmap");
2342 goto errout;
2343 }
2344
2345 for (i = 0; i < aux_info.groups; ++i) {
2346 SLOGI("Encrypting group %d", i);
2347
2348 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
2349 u32 block_count = min(info.blocks_per_group,
2350 aux_info.len_blocks - first_block);
2351
2352 off64_t offset = (u64)info.block_size
2353 * aux_info.bg_desc[i].bg_block_bitmap;
2354
2355 ret = pread64(data->realfd, block_bitmap, info.block_size, offset);
2356 if (ret != (int)info.block_size) {
2357 SLOGE("failed to read all of block group bitmap %d", i);
2358 goto errout;
2359 }
2360
2361 offset = (u64)info.block_size * first_block;
2362
2363 data->count = 0;
2364
2365 for (block = 0; block < block_count; block++) {
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002366 int used = bitmap_get_bit(block_bitmap, block);
2367 update_progress(data, used);
2368 if (used) {
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002369 if (data->count == 0) {
2370 data->offset = offset;
2371 }
2372 data->count++;
2373 } else {
2374 if (flush_outstanding_data(data)) {
2375 goto errout;
2376 }
2377 }
2378
2379 offset += info.block_size;
2380
2381 /* Write data if we are aligned or buffer size reached */
2382 if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0
2383 || data->count == BLOCKS_AT_A_TIME) {
2384 if (flush_outstanding_data(data)) {
2385 goto errout;
2386 }
2387 }
Paul Lawrence87999172014-02-20 12:21:31 -08002388
Paul Lawrence73d7a022014-06-09 14:10:09 -07002389 if (!is_battery_ok_to_continue()) {
Paul Lawrence87999172014-02-20 12:21:31 -08002390 SLOGE("Stopping encryption due to low battery");
2391 rc = 0;
2392 goto errout;
2393 }
2394
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002395 }
2396 if (flush_outstanding_data(data)) {
2397 goto errout;
2398 }
2399 }
2400
Paul Lawrence87999172014-02-20 12:21:31 -08002401 data->completed = 1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002402 rc = 0;
2403
2404errout:
Paul Lawrence3846be12014-09-22 11:33:54 -07002405 log_progress(0, true);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002406 free(data->buffer);
2407 free(block_bitmap);
2408 return rc;
2409}
2410
2411static int cryptfs_enable_inplace_ext4(char *crypto_blkdev,
2412 char *real_blkdev,
2413 off64_t size,
2414 off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002415 off64_t tot_size,
2416 off64_t previously_encrypted_upto)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002417{
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002418 u32 i;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002419 struct encryptGroupsData data;
Paul Lawrence74f29f12014-08-28 15:54:10 -07002420 int rc; // Can't initialize without causing warning -Wclobbered
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002421
Paul Lawrence87999172014-02-20 12:21:31 -08002422 if (previously_encrypted_upto > *size_already_done) {
2423 SLOGD("Not fast encrypting since resuming part way through");
2424 return -1;
2425 }
2426
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002427 memset(&data, 0, sizeof(data));
2428 data.real_blkdev = real_blkdev;
2429 data.crypto_blkdev = crypto_blkdev;
2430
2431 if ( (data.realfd = open(real_blkdev, O_RDWR)) < 0) {
JP Abgrall3334c6a2014-10-10 15:52:11 -07002432 SLOGE("Error opening real_blkdev %s for inplace encrypt. err=%d(%s)\n",
2433 real_blkdev, errno, strerror(errno));
Paul Lawrence74f29f12014-08-28 15:54:10 -07002434 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002435 goto errout;
2436 }
2437
2438 if ( (data.cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002439 SLOGE("Error opening crypto_blkdev %s for ext4 inplace encrypt. err=%d(%s)\n",
JP Abgrall3334c6a2014-10-10 15:52:11 -07002440 crypto_blkdev, errno, strerror(errno));
JP Abgrall7fc1de82014-10-10 18:43:41 -07002441 rc = ENABLE_INPLACE_ERR_DEV;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002442 goto errout;
2443 }
2444
2445 if (setjmp(setjmp_env)) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002446 SLOGE("Reading ext4 extent caused an exception\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07002447 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002448 goto errout;
2449 }
2450
2451 if (read_ext(data.realfd, 0) != 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002452 SLOGE("Failed to read ext4 extent\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07002453 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002454 goto errout;
2455 }
2456
2457 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2458 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2459 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
2460
JP Abgrall7fc1de82014-10-10 18:43:41 -07002461 SLOGI("Encrypting ext4 filesystem in place...");
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002462
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002463 data.tot_used_blocks = data.numblocks;
2464 for (i = 0; i < aux_info.groups; ++i) {
2465 data.tot_used_blocks -= aux_info.bg_desc[i].bg_free_blocks_count;
2466 }
2467
2468 data.one_pct = data.tot_used_blocks / 100;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002469 data.cur_pct = 0;
Paul Lawrence9c58a872014-09-30 09:12:51 -07002470
2471 struct timespec time_started = {0};
2472 if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
2473 SLOGW("Error getting time at start");
2474 // Note - continue anyway - we'll run with 0
2475 }
2476 data.time_started = time_started.tv_sec;
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002477 data.remaining_time = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002478
2479 rc = encrypt_groups(&data);
2480 if (rc) {
2481 SLOGE("Error encrypting groups");
2482 goto errout;
2483 }
2484
Paul Lawrence87999172014-02-20 12:21:31 -08002485 *size_already_done += data.completed ? size : data.last_written_sector;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002486 rc = 0;
2487
2488errout:
2489 close(data.realfd);
2490 close(data.cryptofd);
2491
2492 return rc;
2493}
2494
Paul Lawrence3846be12014-09-22 11:33:54 -07002495static void log_progress_f2fs(u64 block, bool completed)
2496{
2497 // Precondition - if completed data = 0 else data != 0
2498
2499 // Track progress so we can skip logging blocks
2500 static u64 last_block = (u64)-1;
2501
2502 // Need to close existing 'Encrypting from' log?
2503 if (completed || (last_block != (u64)-1 && block != last_block + 1)) {
2504 SLOGI("Encrypted to block %" PRId64, last_block);
2505 last_block = -1;
2506 }
2507
2508 // Need to start new 'Encrypting from' log?
2509 if (!completed && (last_block == (u64)-1 || block != last_block + 1)) {
2510 SLOGI("Encrypting from block %" PRId64, block);
2511 }
2512
2513 // Update offset
2514 if (!completed) {
2515 last_block = block;
2516 }
2517}
2518
Daniel Rosenberge82df162014-08-15 22:19:23 +00002519static int encrypt_one_block_f2fs(u64 pos, void *data)
2520{
2521 struct encryptGroupsData *priv_dat = (struct encryptGroupsData *)data;
2522
2523 priv_dat->blocks_already_done = pos - 1;
2524 update_progress(priv_dat, 1);
2525
2526 off64_t offset = pos * CRYPT_INPLACE_BUFSIZE;
2527
2528 if (pread64(priv_dat->realfd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002529 SLOGE("Error reading real_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002530 return -1;
2531 }
2532
2533 if (pwrite64(priv_dat->cryptofd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002534 SLOGE("Error writing crypto_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002535 return -1;
2536 } else {
Paul Lawrence3846be12014-09-22 11:33:54 -07002537 log_progress_f2fs(pos, false);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002538 }
2539
2540 return 0;
2541}
2542
2543static int cryptfs_enable_inplace_f2fs(char *crypto_blkdev,
2544 char *real_blkdev,
2545 off64_t size,
2546 off64_t *size_already_done,
2547 off64_t tot_size,
2548 off64_t previously_encrypted_upto)
2549{
2550 u32 i;
2551 struct encryptGroupsData data;
2552 struct f2fs_info *f2fs_info = NULL;
JP Abgrall7fc1de82014-10-10 18:43:41 -07002553 int rc = ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002554 if (previously_encrypted_upto > *size_already_done) {
2555 SLOGD("Not fast encrypting since resuming part way through");
JP Abgrall7fc1de82014-10-10 18:43:41 -07002556 return ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002557 }
2558 memset(&data, 0, sizeof(data));
2559 data.real_blkdev = real_blkdev;
2560 data.crypto_blkdev = crypto_blkdev;
2561 data.realfd = -1;
2562 data.cryptofd = -1;
2563 if ( (data.realfd = open64(real_blkdev, O_RDWR)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002564 SLOGE("Error opening real_blkdev %s for f2fs inplace encrypt\n",
Daniel Rosenberge82df162014-08-15 22:19:23 +00002565 real_blkdev);
2566 goto errout;
2567 }
2568 if ( (data.cryptofd = open64(crypto_blkdev, O_WRONLY)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002569 SLOGE("Error opening crypto_blkdev %s for f2fs inplace encrypt. err=%d(%s)\n",
JP Abgrall3334c6a2014-10-10 15:52:11 -07002570 crypto_blkdev, errno, strerror(errno));
JP Abgrall7fc1de82014-10-10 18:43:41 -07002571 rc = ENABLE_INPLACE_ERR_DEV;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002572 goto errout;
2573 }
2574
2575 f2fs_info = generate_f2fs_info(data.realfd);
2576 if (!f2fs_info)
2577 goto errout;
2578
2579 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2580 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2581 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
2582
2583 data.tot_used_blocks = get_num_blocks_used(f2fs_info);
2584
2585 data.one_pct = data.tot_used_blocks / 100;
2586 data.cur_pct = 0;
2587 data.time_started = time(NULL);
2588 data.remaining_time = -1;
2589
2590 data.buffer = malloc(f2fs_info->block_size);
2591 if (!data.buffer) {
2592 SLOGE("Failed to allocate crypto buffer");
2593 goto errout;
2594 }
2595
2596 data.count = 0;
2597
2598 /* Currently, this either runs to completion, or hits a nonrecoverable error */
2599 rc = run_on_used_blocks(data.blocks_already_done, f2fs_info, &encrypt_one_block_f2fs, &data);
2600
2601 if (rc) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002602 SLOGE("Error in running over f2fs blocks");
2603 rc = ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002604 goto errout;
2605 }
2606
2607 *size_already_done += size;
2608 rc = 0;
2609
2610errout:
2611 if (rc)
2612 SLOGE("Failed to encrypt f2fs filesystem on %s", real_blkdev);
2613
Paul Lawrence3846be12014-09-22 11:33:54 -07002614 log_progress_f2fs(0, true);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002615 free(f2fs_info);
2616 free(data.buffer);
2617 close(data.realfd);
2618 close(data.cryptofd);
2619
2620 return rc;
2621}
2622
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002623static int cryptfs_enable_inplace_full(char *crypto_blkdev, char *real_blkdev,
2624 off64_t size, off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002625 off64_t tot_size,
2626 off64_t previously_encrypted_upto)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002627{
2628 int realfd, cryptofd;
2629 char *buf[CRYPT_INPLACE_BUFSIZE];
JP Abgrall7fc1de82014-10-10 18:43:41 -07002630 int rc = ENABLE_INPLACE_ERR_OTHER;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002631 off64_t numblocks, i, remainder;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002632 off64_t one_pct, cur_pct, new_pct;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002633 off64_t blocks_already_done, tot_numblocks;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002634
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002635 if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) {
2636 SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002637 return ENABLE_INPLACE_ERR_OTHER;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002638 }
2639
2640 if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
JP Abgrall3334c6a2014-10-10 15:52:11 -07002641 SLOGE("Error opening crypto_blkdev %s for inplace encrypt. err=%d(%s)\n",
2642 crypto_blkdev, errno, strerror(errno));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002643 close(realfd);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002644 return ENABLE_INPLACE_ERR_DEV;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002645 }
2646
2647 /* This is pretty much a simple loop of reading 4K, and writing 4K.
2648 * The size passed in is the number of 512 byte sectors in the filesystem.
2649 * So compute the number of whole 4K blocks we should read/write,
2650 * and the remainder.
2651 */
2652 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2653 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002654 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2655 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002656
2657 SLOGE("Encrypting filesystem in place...");
2658
Paul Lawrence87999172014-02-20 12:21:31 -08002659 i = previously_encrypted_upto + 1 - *size_already_done;
2660
2661 if (lseek64(realfd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
2662 SLOGE("Cannot seek to previously encrypted point on %s", real_blkdev);
2663 goto errout;
2664 }
2665
2666 if (lseek64(cryptofd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
2667 SLOGE("Cannot seek to previously encrypted point on %s", crypto_blkdev);
2668 goto errout;
2669 }
2670
2671 for (;i < size && i % CRYPT_SECTORS_PER_BUFSIZE != 0; ++i) {
2672 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2673 SLOGE("Error reading initial sectors from real_blkdev %s for "
2674 "inplace encrypt\n", crypto_blkdev);
2675 goto errout;
2676 }
2677 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2678 SLOGE("Error writing initial sectors to crypto_blkdev %s for "
2679 "inplace encrypt\n", crypto_blkdev);
2680 goto errout;
2681 } else {
Elliott Hughescb33f572014-06-25 18:25:11 -07002682 SLOGI("Encrypted 1 block at %" PRId64, i);
Paul Lawrence87999172014-02-20 12:21:31 -08002683 }
2684 }
2685
Ken Sumrall29d8da82011-05-18 17:20:07 -07002686 one_pct = tot_numblocks / 100;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002687 cur_pct = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002688 /* process the majority of the filesystem in blocks */
Paul Lawrence87999172014-02-20 12:21:31 -08002689 for (i/=CRYPT_SECTORS_PER_BUFSIZE; i<numblocks; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07002690 new_pct = (i + blocks_already_done) / one_pct;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002691 if (new_pct > cur_pct) {
2692 char buf[8];
2693
2694 cur_pct = new_pct;
Elliott Hughes73737162014-06-25 17:27:42 -07002695 snprintf(buf, sizeof(buf), "%" PRId64, cur_pct);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002696 property_set("vold.encrypt_progress", buf);
2697 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002698 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Lawrence87999172014-02-20 12:21:31 -08002699 SLOGE("Error reading real_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002700 goto errout;
2701 }
2702 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Lawrence87999172014-02-20 12:21:31 -08002703 SLOGE("Error writing crypto_blkdev %s for inplace encrypt", crypto_blkdev);
2704 goto errout;
2705 } else {
Elliott Hughescb33f572014-06-25 18:25:11 -07002706 SLOGD("Encrypted %d block at %" PRId64,
Paul Lawrence87999172014-02-20 12:21:31 -08002707 CRYPT_SECTORS_PER_BUFSIZE,
2708 i * CRYPT_SECTORS_PER_BUFSIZE);
2709 }
2710
Paul Lawrence73d7a022014-06-09 14:10:09 -07002711 if (!is_battery_ok_to_continue()) {
Paul Lawrence87999172014-02-20 12:21:31 -08002712 SLOGE("Stopping encryption due to low battery");
2713 *size_already_done += (i + 1) * CRYPT_SECTORS_PER_BUFSIZE - 1;
2714 rc = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002715 goto errout;
2716 }
2717 }
2718
2719 /* Do any remaining sectors */
2720 for (i=0; i<remainder; i++) {
Paul Lawrence87999172014-02-20 12:21:31 -08002721 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2722 SLOGE("Error reading final sectors from real_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002723 goto errout;
2724 }
Paul Lawrence87999172014-02-20 12:21:31 -08002725 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2726 SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002727 goto errout;
Paul Lawrence87999172014-02-20 12:21:31 -08002728 } else {
2729 SLOGI("Encrypted 1 block at next location");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002730 }
2731 }
2732
Ken Sumrall29d8da82011-05-18 17:20:07 -07002733 *size_already_done += size;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002734 rc = 0;
2735
2736errout:
2737 close(realfd);
2738 close(cryptofd);
2739
2740 return rc;
2741}
2742
JP Abgrall7fc1de82014-10-10 18:43:41 -07002743/* returns on of the ENABLE_INPLACE_* return codes */
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002744static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev,
2745 off64_t size, off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002746 off64_t tot_size,
2747 off64_t previously_encrypted_upto)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002748{
JP Abgrall7fc1de82014-10-10 18:43:41 -07002749 int rc_ext4, rc_f2fs, rc_full;
Paul Lawrence87999172014-02-20 12:21:31 -08002750 if (previously_encrypted_upto) {
Elliott Hughescb33f572014-06-25 18:25:11 -07002751 SLOGD("Continuing encryption from %" PRId64, previously_encrypted_upto);
Paul Lawrence87999172014-02-20 12:21:31 -08002752 }
2753
2754 if (*size_already_done + size < previously_encrypted_upto) {
2755 *size_already_done += size;
2756 return 0;
2757 }
2758
Daniel Rosenberge82df162014-08-15 22:19:23 +00002759 /* TODO: identify filesystem type.
2760 * As is, cryptfs_enable_inplace_ext4 will fail on an f2fs partition, and
2761 * then we will drop down to cryptfs_enable_inplace_f2fs.
2762 * */
JP Abgrall7fc1de82014-10-10 18:43:41 -07002763 if ((rc_ext4 = cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev,
Daniel Rosenberge82df162014-08-15 22:19:23 +00002764 size, size_already_done,
JP Abgrall7fc1de82014-10-10 18:43:41 -07002765 tot_size, previously_encrypted_upto)) == 0) {
Daniel Rosenberge82df162014-08-15 22:19:23 +00002766 return 0;
2767 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002768 SLOGD("cryptfs_enable_inplace_ext4()=%d\n", rc_ext4);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002769
JP Abgrall7fc1de82014-10-10 18:43:41 -07002770 if ((rc_f2fs = cryptfs_enable_inplace_f2fs(crypto_blkdev, real_blkdev,
Daniel Rosenberge82df162014-08-15 22:19:23 +00002771 size, size_already_done,
JP Abgrall7fc1de82014-10-10 18:43:41 -07002772 tot_size, previously_encrypted_upto)) == 0) {
Daniel Rosenberge82df162014-08-15 22:19:23 +00002773 return 0;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002774 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002775 SLOGD("cryptfs_enable_inplace_f2fs()=%d\n", rc_f2fs);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002776
JP Abgrall7fc1de82014-10-10 18:43:41 -07002777 rc_full = cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev,
Paul Lawrence87999172014-02-20 12:21:31 -08002778 size, size_already_done, tot_size,
2779 previously_encrypted_upto);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002780 SLOGD("cryptfs_enable_inplace_full()=%d\n", rc_full);
2781
2782 /* Hack for b/17898962, the following is the symptom... */
2783 if (rc_ext4 == ENABLE_INPLACE_ERR_DEV
2784 && rc_f2fs == ENABLE_INPLACE_ERR_DEV
2785 && rc_full == ENABLE_INPLACE_ERR_DEV) {
2786 return ENABLE_INPLACE_ERR_DEV;
2787 }
2788 return rc_full;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002789}
2790
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002791#define CRYPTO_ENABLE_WIPE 1
2792#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002793
2794#define FRAMEWORK_BOOT_WAIT 60
2795
Ken Sumrall29d8da82011-05-18 17:20:07 -07002796static inline int should_encrypt(struct volume_info *volume)
2797{
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002798 return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) ==
Ken Sumrall29d8da82011-05-18 17:20:07 -07002799 (VOL_ENCRYPTABLE | VOL_NONREMOVABLE);
2800}
2801
Paul Lawrence87999172014-02-20 12:21:31 -08002802static int cryptfs_SHA256_fileblock(const char* filename, __le8* buf)
2803{
2804 int fd = open(filename, O_RDONLY);
2805 if (fd == -1) {
2806 SLOGE("Error opening file %s", filename);
2807 return -1;
2808 }
2809
2810 char block[CRYPT_INPLACE_BUFSIZE];
2811 memset(block, 0, sizeof(block));
2812 if (unix_read(fd, block, sizeof(block)) < 0) {
2813 SLOGE("Error reading file %s", filename);
2814 close(fd);
2815 return -1;
2816 }
2817
2818 close(fd);
2819
2820 SHA256_CTX c;
2821 SHA256_Init(&c);
2822 SHA256_Update(&c, block, sizeof(block));
2823 SHA256_Final(buf, &c);
2824
2825 return 0;
2826}
2827
JP Abgrall62c7af32014-06-16 13:01:23 -07002828static int get_fs_type(struct fstab_rec *rec)
2829{
2830 if (!strcmp(rec->fs_type, "ext4")) {
2831 return EXT4_FS;
2832 } else if (!strcmp(rec->fs_type, "f2fs")) {
2833 return F2FS_FS;
2834 } else {
2835 return -1;
2836 }
2837}
2838
Paul Lawrence87999172014-02-20 12:21:31 -08002839static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr *crypt_ftr, int how,
2840 char *crypto_blkdev, char *real_blkdev,
2841 int previously_encrypted_upto)
2842{
2843 off64_t cur_encryption_done=0, tot_encryption_size=0;
2844 int i, rc = -1;
2845
Paul Lawrence73d7a022014-06-09 14:10:09 -07002846 if (!is_battery_ok_to_start()) {
2847 SLOGW("Not starting encryption due to low battery");
Paul Lawrence87999172014-02-20 12:21:31 -08002848 return 0;
2849 }
2850
2851 /* The size of the userdata partition, and add in the vold volumes below */
2852 tot_encryption_size = crypt_ftr->fs_size;
2853
2854 if (how == CRYPTO_ENABLE_WIPE) {
JP Abgrall62c7af32014-06-16 13:01:23 -07002855 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
2856 int fs_type = get_fs_type(rec);
2857 if (fs_type < 0) {
2858 SLOGE("cryptfs_enable: unsupported fs type %s\n", rec->fs_type);
2859 return -1;
2860 }
2861 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr->fs_size, fs_type);
Paul Lawrence87999172014-02-20 12:21:31 -08002862 } else if (how == CRYPTO_ENABLE_INPLACE) {
2863 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev,
2864 crypt_ftr->fs_size, &cur_encryption_done,
2865 tot_encryption_size,
2866 previously_encrypted_upto);
2867
JP Abgrall7fc1de82014-10-10 18:43:41 -07002868 if (rc == ENABLE_INPLACE_ERR_DEV) {
2869 /* Hack for b/17898962 */
2870 SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n");
2871 cryptfs_reboot(reboot);
2872 }
2873
Paul Lawrence73d7a022014-06-09 14:10:09 -07002874 if (!rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002875 crypt_ftr->encrypted_upto = cur_encryption_done;
2876 }
2877
Paul Lawrence73d7a022014-06-09 14:10:09 -07002878 if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002879 /* The inplace routine never actually sets the progress to 100% due
2880 * to the round down nature of integer division, so set it here */
2881 property_set("vold.encrypt_progress", "100");
2882 }
2883 } else {
2884 /* Shouldn't happen */
2885 SLOGE("cryptfs_enable: internal error, unknown option\n");
2886 rc = -1;
2887 }
2888
2889 return rc;
2890}
2891
Paul Lawrence13486032014-02-03 13:28:11 -08002892int cryptfs_enable_internal(char *howarg, int crypt_type, char *passwd,
2893 int allow_reboot)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002894{
2895 int how = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002896 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN];
Ken Sumralle5032c42012-04-01 23:58:44 -07002897 unsigned long nr_sec;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002898 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Ken Sumrall319b1042011-06-14 14:01:55 -07002899 int rc=-1, fd, i, ret;
Paul Lawrence87999172014-02-20 12:21:31 -08002900 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002901 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002902 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002903 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07002904 char key_loc[PROPERTY_VALUE_MAX];
2905 char fuse_sdcard[PROPERTY_VALUE_MAX];
2906 char *sd_mnt_point;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002907 int num_vols;
2908 struct volume_info *vol_list = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002909 off64_t previously_encrypted_upto = 0;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002910
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002911 if (!strcmp(howarg, "wipe")) {
2912 how = CRYPTO_ENABLE_WIPE;
2913 } else if (! strcmp(howarg, "inplace")) {
2914 how = CRYPTO_ENABLE_INPLACE;
2915 } else {
2916 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08002917 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002918 }
2919
Paul Lawrence87999172014-02-20 12:21:31 -08002920 /* See if an encryption was underway and interrupted */
2921 if (how == CRYPTO_ENABLE_INPLACE
2922 && get_crypt_ftr_and_key(&crypt_ftr) == 0
2923 && (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS)) {
2924 previously_encrypted_upto = crypt_ftr.encrypted_upto;
2925 crypt_ftr.encrypted_upto = 0;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002926 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
2927
2928 /* At this point, we are in an inconsistent state. Until we successfully
2929 complete encryption, a reboot will leave us broken. So mark the
2930 encryption failed in case that happens.
2931 On successfully completing encryption, remove this flag */
2932 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
2933
2934 put_crypt_ftr_and_key(&crypt_ftr);
Paul Lawrence87999172014-02-20 12:21:31 -08002935 }
2936
2937 property_get("ro.crypto.state", encrypted_state, "");
2938 if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) {
2939 SLOGE("Device is already running encrypted, aborting");
2940 goto error_unencrypted;
2941 }
2942
2943 // TODO refactor fs_mgr_get_crypt_info to get both in one call
2944 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumrall56ad03c2013-02-13 13:00:19 -08002945 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002946
Ken Sumrall3ed82362011-01-28 23:31:16 -08002947 /* Get the size of the real block device */
2948 fd = open(real_blkdev, O_RDONLY);
2949 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
2950 SLOGE("Cannot get size of block device %s\n", real_blkdev);
2951 goto error_unencrypted;
2952 }
2953 close(fd);
2954
2955 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002956 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002957 unsigned int fs_size_sec, max_fs_size_sec;
Jim Millera70abc62014-08-15 02:00:45 +00002958 fs_size_sec = get_fs_size(real_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002959 if (fs_size_sec == 0)
2960 fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev);
2961
Paul Lawrence87999172014-02-20 12:21:31 -08002962 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002963
2964 if (fs_size_sec > max_fs_size_sec) {
2965 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
2966 goto error_unencrypted;
2967 }
2968 }
2969
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002970 /* Get a wakelock as this may take a while, and we don't want the
2971 * device to sleep on us. We'll grab a partial wakelock, and if the UI
2972 * wants to keep the screen on, it can grab a full wakelock.
2973 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002974 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002975 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
2976
Jeff Sharkey7382f812012-08-23 14:08:59 -07002977 /* Get the sdcard mount point */
Jeff Sharkeyb77bc462012-10-01 14:36:26 -07002978 sd_mnt_point = getenv("EMULATED_STORAGE_SOURCE");
Jeff Sharkey7382f812012-08-23 14:08:59 -07002979 if (!sd_mnt_point) {
2980 sd_mnt_point = getenv("EXTERNAL_STORAGE");
2981 }
2982 if (!sd_mnt_point) {
2983 sd_mnt_point = "/mnt/sdcard";
2984 }
Ken Sumrall29d8da82011-05-18 17:20:07 -07002985
Paul Lawrence87999172014-02-20 12:21:31 -08002986 /* TODO
2987 * Currently do not have test devices with multiple encryptable volumes.
2988 * When we acquire some, re-add support.
2989 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002990 num_vols=vold_getNumDirectVolumes();
2991 vol_list = malloc(sizeof(struct volume_info) * num_vols);
2992 vold_getDirectVolumeList(vol_list);
2993
2994 for (i=0; i<num_vols; i++) {
2995 if (should_encrypt(&vol_list[i])) {
Paul Lawrence87999172014-02-20 12:21:31 -08002996 SLOGE("Cannot encrypt if there are multiple encryptable volumes"
2997 "%s\n", vol_list[i].label);
2998 goto error_unencrypted;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002999 }
3000 }
3001
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003002 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003003 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003004 */
3005 property_set("vold.decrypt", "trigger_shutdown_framework");
3006 SLOGD("Just asked init to shut down class main\n");
3007
Ken Sumrall425524d2012-06-14 20:55:28 -07003008 if (vold_unmountAllAsecs()) {
3009 /* Just report the error. If any are left mounted,
3010 * umounting /data below will fail and handle the error.
3011 */
3012 SLOGE("Error unmounting internal asecs");
3013 }
3014
Ken Sumrall29d8da82011-05-18 17:20:07 -07003015 property_get("ro.crypto.fuse_sdcard", fuse_sdcard, "");
3016 if (!strcmp(fuse_sdcard, "true")) {
3017 /* This is a device using the fuse layer to emulate the sdcard semantics
3018 * on top of the userdata partition. vold does not manage it, it is managed
3019 * by the sdcard service. The sdcard service was killed by the property trigger
3020 * above, so just unmount it now. We must do this _AFTER_ killing the framework,
3021 * unlike the case for vold managed devices above.
3022 */
Greg Hackmann6e8440f2014-10-02 17:18:20 -07003023 if (wait_and_unmount(sd_mnt_point, false)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07003024 goto error_shutting_down;
3025 }
Ken Sumrall2eaf7132011-01-14 12:45:48 -08003026 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003027
3028 /* Now unmount the /data partition. */
Greg Hackmann6e8440f2014-10-02 17:18:20 -07003029 if (wait_and_unmount(DATA_MNT_POINT, false)) {
JP Abgrall502dc742013-11-01 13:06:20 -07003030 if (allow_reboot) {
3031 goto error_shutting_down;
3032 } else {
3033 goto error_unencrypted;
3034 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003035 }
3036
3037 /* Do extra work for a better UX when doing the long inplace encryption */
3038 if (how == CRYPTO_ENABLE_INPLACE) {
3039 /* Now that /data is unmounted, we need to mount a tmpfs
3040 * /data, set a property saying we're doing inplace encryption,
3041 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003042 */
Ken Sumralle5032c42012-04-01 23:58:44 -07003043 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08003044 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003045 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003046 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08003047 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003048
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003049 /* restart the framework. */
3050 /* Create necessary paths on /data */
3051 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08003052 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003053 }
3054
Ken Sumrall92736ef2012-10-17 20:57:14 -07003055 /* Ugh, shutting down the framework is not synchronous, so until it
3056 * can be fixed, this horrible hack will wait a moment for it all to
3057 * shut down before proceeding. Without it, some devices cannot
3058 * restart the graphics services.
3059 */
3060 sleep(2);
3061
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003062 /* startup service classes main and late_start */
3063 property_set("vold.decrypt", "trigger_restart_min_framework");
3064 SLOGD("Just triggered restart_min_framework\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003065
Ken Sumrall7df84122011-01-18 14:04:08 -08003066 /* OK, the framework is restarted and will soon be showing a
3067 * progress bar. Time to setup an encrypted mapping, and
3068 * either write a new filesystem, or encrypt in place updating
3069 * the progress bar as we work.
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003070 */
3071 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003072
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003073 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003074 /* Initialize a crypt_mnt_ftr for the partition */
Paul Lawrence87999172014-02-20 12:21:31 -08003075 if (previously_encrypted_upto == 0) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07003076 if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) {
3077 goto error_shutting_down;
3078 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003079
Paul Lawrence87999172014-02-20 12:21:31 -08003080 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
3081 crypt_ftr.fs_size = nr_sec
3082 - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
3083 } else {
3084 crypt_ftr.fs_size = nr_sec;
3085 }
Paul Lawrence6bfed202014-07-28 12:47:22 -07003086 /* At this point, we are in an inconsistent state. Until we successfully
3087 complete encryption, a reboot will leave us broken. So mark the
3088 encryption failed in case that happens.
3089 On successfully completing encryption, remove this flag */
3090 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
Paul Lawrence87999172014-02-20 12:21:31 -08003091 crypt_ftr.crypt_type = crypt_type;
3092 strcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003093
Paul Lawrence87999172014-02-20 12:21:31 -08003094 /* Make an encrypted master key */
3095 if (create_encrypted_random_key(passwd, crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
3096 SLOGE("Cannot create encrypted master key\n");
3097 goto error_shutting_down;
3098 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003099
Paul Lawrence87999172014-02-20 12:21:31 -08003100 /* Write the key to the end of the partition */
3101 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003102
Paul Lawrence87999172014-02-20 12:21:31 -08003103 /* If any persistent data has been remembered, save it.
3104 * If none, create a valid empty table and save that.
3105 */
3106 if (!persist_data) {
3107 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
3108 if (pdata) {
3109 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
3110 persist_data = pdata;
3111 }
3112 }
3113 if (persist_data) {
3114 save_persistent_data();
3115 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003116 }
3117
Paul Lawrenced0c7b172014-08-08 14:28:10 -07003118 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07003119 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
3120 "userdata");
3121
Paul Lawrence87999172014-02-20 12:21:31 -08003122 /* If we are continuing, check checksums match */
3123 rc = 0;
3124 if (previously_encrypted_upto) {
3125 __le8 hash_first_block[SHA256_DIGEST_LENGTH];
3126 rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block);
Ken Sumrall128626f2011-06-28 18:45:14 -07003127
Paul Lawrence87999172014-02-20 12:21:31 -08003128 if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block,
3129 sizeof(hash_first_block)) != 0) {
3130 SLOGE("Checksums do not match - trigger wipe");
3131 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07003132 }
3133 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003134
Paul Lawrence87999172014-02-20 12:21:31 -08003135 if (!rc) {
3136 rc = cryptfs_enable_all_volumes(&crypt_ftr, how,
3137 crypto_blkdev, real_blkdev,
3138 previously_encrypted_upto);
3139 }
3140
3141 /* Calculate checksum if we are not finished */
Paul Lawrence73d7a022014-06-09 14:10:09 -07003142 if (!rc && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08003143 rc = cryptfs_SHA256_fileblock(crypto_blkdev,
3144 crypt_ftr.hash_first_block);
Paul Lawrence73d7a022014-06-09 14:10:09 -07003145 if (rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08003146 SLOGE("Error calculating checksum for continuing encryption");
3147 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07003148 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003149 }
3150
3151 /* Undo the dm-crypt mapping whether we succeed or not */
Ken Sumrall29d8da82011-05-18 17:20:07 -07003152 delete_crypto_blk_dev("userdata");
Ken Sumrall29d8da82011-05-18 17:20:07 -07003153
3154 free(vol_list);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003155
3156 if (! rc) {
3157 /* Success */
Paul Lawrence6bfed202014-07-28 12:47:22 -07003158 crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08003159
Paul Lawrence6bfed202014-07-28 12:47:22 -07003160 if (crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08003161 SLOGD("Encrypted up to sector %lld - will continue after reboot",
3162 crypt_ftr.encrypted_upto);
Paul Lawrence6bfed202014-07-28 12:47:22 -07003163 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence87999172014-02-20 12:21:31 -08003164 }
Paul Lawrence73d7a022014-06-09 14:10:09 -07003165
Paul Lawrence6bfed202014-07-28 12:47:22 -07003166 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08003167
Paul Lawrence73d7a022014-06-09 14:10:09 -07003168 if (crypt_ftr.encrypted_upto == crypt_ftr.fs_size) {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003169 char value[PROPERTY_VALUE_MAX];
3170 property_get("ro.crypto.state", value, "");
3171 if (!strcmp(value, "")) {
3172 /* default encryption - continue first boot sequence */
3173 property_set("ro.crypto.state", "encrypted");
3174 release_wake_lock(lockid);
3175 cryptfs_check_passwd(DEFAULT_PASSWORD);
3176 cryptfs_restart_internal(1);
3177 return 0;
3178 } else {
3179 sleep(2); /* Give the UI a chance to show 100% progress */
Paul Lawrence87999172014-02-20 12:21:31 -08003180 cryptfs_reboot(reboot);
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003181 }
Paul Lawrence87999172014-02-20 12:21:31 -08003182 } else {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003183 sleep(2); /* Partially encrypted, ensure writes flushed to ssd */
Paul Lawrence87999172014-02-20 12:21:31 -08003184 cryptfs_reboot(shutdown);
3185 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003186 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003187 char value[PROPERTY_VALUE_MAX];
3188
Ken Sumrall319369a2012-06-27 16:30:18 -07003189 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003190 if (!strcmp(value, "1")) {
3191 /* wipe data if encryption failed */
3192 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
3193 mkdir("/cache/recovery", 0700);
Nick Kralevich4684e582012-06-26 15:07:03 -07003194 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC, 0600);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003195 if (fd >= 0) {
Jeff Sharkeydd1a8042014-09-24 11:46:51 -07003196 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
3197 write(fd, "--reason=cryptfs_enable_internal\n", strlen("--reason=cryptfs_enable_internal\n") + 1);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003198 close(fd);
3199 } else {
3200 SLOGE("could not open /cache/recovery/command\n");
3201 }
Paul Lawrence87999172014-02-20 12:21:31 -08003202 cryptfs_reboot(recovery);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003203 } else {
3204 /* set property to trigger dialog */
3205 property_set("vold.encrypt_progress", "error_partially_encrypted");
3206 release_wake_lock(lockid);
3207 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003208 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003209 }
3210
Ken Sumrall3ed82362011-01-28 23:31:16 -08003211 /* hrm, the encrypt step claims success, but the reboot failed.
3212 * This should not happen.
3213 * Set the property and return. Hope the framework can deal with it.
3214 */
3215 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003216 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003217 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08003218
3219error_unencrypted:
Ken Sumrall29d8da82011-05-18 17:20:07 -07003220 free(vol_list);
Ken Sumrall3ed82362011-01-28 23:31:16 -08003221 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003222 if (lockid[0]) {
3223 release_wake_lock(lockid);
3224 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003225 return -1;
3226
3227error_shutting_down:
3228 /* we failed, and have not encrypted anthing, so the users's data is still intact,
3229 * but the framework is stopped and not restarted to show the error, so it's up to
3230 * vold to restart the system.
3231 */
3232 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Paul Lawrence87999172014-02-20 12:21:31 -08003233 cryptfs_reboot(reboot);
Ken Sumrall3ed82362011-01-28 23:31:16 -08003234
3235 /* shouldn't get here */
3236 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall29d8da82011-05-18 17:20:07 -07003237 free(vol_list);
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003238 if (lockid[0]) {
3239 release_wake_lock(lockid);
3240 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003241 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003242}
3243
Paul Lawrence45f10532014-04-04 18:11:56 +00003244int cryptfs_enable(char *howarg, int type, char *passwd, int allow_reboot)
Paul Lawrence13486032014-02-03 13:28:11 -08003245{
Paul Lawrencefc615042014-10-04 15:32:29 -07003246 char* adjusted_passwd = adjust_passwd(passwd);
3247 if (adjusted_passwd) {
3248 passwd = adjusted_passwd;
3249 }
3250
3251 int rc = cryptfs_enable_internal(howarg, type, passwd, allow_reboot);
3252
3253 free(adjusted_passwd);
3254 return rc;
Paul Lawrence13486032014-02-03 13:28:11 -08003255}
3256
3257int cryptfs_enable_default(char *howarg, int allow_reboot)
3258{
3259 return cryptfs_enable_internal(howarg, CRYPT_TYPE_DEFAULT,
3260 DEFAULT_PASSWORD, allow_reboot);
3261}
3262
3263int cryptfs_changepw(int crypt_type, const char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003264{
3265 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003266 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003267
3268 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08003269 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08003270 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003271 return -1;
3272 }
3273
Paul Lawrencef4faa572014-01-29 13:31:03 -08003274 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
3275 SLOGE("Invalid crypt_type %d", crypt_type);
3276 return -1;
3277 }
3278
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003279 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07003280 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08003281 SLOGE("Error getting crypt footer and key");
3282 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003283 }
3284
Paul Lawrencef4faa572014-01-29 13:31:03 -08003285 crypt_ftr.crypt_type = crypt_type;
3286
Paul Lawrencefc615042014-10-04 15:32:29 -07003287 char* adjusted_passwd = adjust_passwd(newpw);
3288 if (adjusted_passwd) {
3289 newpw = adjusted_passwd;
3290 }
3291
Paul Lawrencef4faa572014-01-29 13:31:03 -08003292 encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
3293 : newpw,
3294 crypt_ftr.salt,
3295 saved_master_key,
3296 crypt_ftr.master_key,
3297 &crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003298
Jason parks70a4b3f2011-01-28 10:10:47 -06003299 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07003300 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003301
Paul Lawrencefc615042014-10-04 15:32:29 -07003302 free(adjusted_passwd);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003303 return 0;
3304}
Ken Sumrall160b4d62013-04-22 12:15:39 -07003305
Rubin Xu85c01f92014-10-13 12:49:54 +01003306static unsigned int persist_get_max_entries(int encrypted) {
3307 struct crypt_mnt_ftr crypt_ftr;
3308 unsigned int dsize;
3309 unsigned int max_persistent_entries;
3310
3311 /* If encrypted, use the values from the crypt_ftr, otherwise
3312 * use the values for the current spec.
3313 */
3314 if (encrypted) {
3315 if (get_crypt_ftr_and_key(&crypt_ftr)) {
3316 return -1;
3317 }
3318 dsize = crypt_ftr.persist_data_size;
3319 } else {
3320 dsize = CRYPT_PERSIST_DATA_SIZE;
3321 }
3322
3323 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
3324 sizeof(struct crypt_persist_entry);
3325
3326 return max_persistent_entries;
3327}
3328
3329static int persist_get_key(const char *fieldname, char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003330{
3331 unsigned int i;
3332
3333 if (persist_data == NULL) {
3334 return -1;
3335 }
3336 for (i = 0; i < persist_data->persist_valid_entries; i++) {
3337 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
3338 /* We found it! */
3339 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
3340 return 0;
3341 }
3342 }
3343
3344 return -1;
3345}
3346
Rubin Xu85c01f92014-10-13 12:49:54 +01003347static int persist_set_key(const char *fieldname, const char *value, int encrypted)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003348{
3349 unsigned int i;
3350 unsigned int num;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003351 unsigned int max_persistent_entries;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003352
3353 if (persist_data == NULL) {
3354 return -1;
3355 }
3356
Rubin Xu85c01f92014-10-13 12:49:54 +01003357 max_persistent_entries = persist_get_max_entries(encrypted);
Ken Sumrall160b4d62013-04-22 12:15:39 -07003358
3359 num = persist_data->persist_valid_entries;
3360
3361 for (i = 0; i < num; i++) {
3362 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
3363 /* We found an existing entry, update it! */
3364 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
3365 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
3366 return 0;
3367 }
3368 }
3369
3370 /* We didn't find it, add it to the end, if there is room */
3371 if (persist_data->persist_valid_entries < max_persistent_entries) {
3372 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
3373 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
3374 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
3375 persist_data->persist_valid_entries++;
3376 return 0;
3377 }
3378
3379 return -1;
3380}
3381
Rubin Xu85c01f92014-10-13 12:49:54 +01003382/**
3383 * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the
3384 * sequence and its index is greater than or equal to index. Return 0 otherwise.
3385 */
3386static int match_multi_entry(const char *key, const char *field, unsigned index) {
3387 unsigned int i;
3388 unsigned int field_len;
3389 unsigned int key_index;
3390 field_len = strlen(field);
3391
3392 if (index == 0) {
3393 // The first key in a multi-entry field is just the filedname itself.
3394 if (!strcmp(key, field)) {
3395 return 1;
3396 }
3397 }
3398 // Match key against "%s_%d" % (field, index)
3399 if (strlen(key) < field_len + 1 + 1) {
3400 // Need at least a '_' and a digit.
3401 return 0;
3402 }
3403 if (strncmp(key, field, field_len)) {
3404 // If the key does not begin with field, it's not a match.
3405 return 0;
3406 }
3407 if (1 != sscanf(&key[field_len],"_%d", &key_index)) {
3408 return 0;
3409 }
3410 return key_index >= index;
3411}
3412
3413/*
3414 * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all
3415 * remaining entries starting from index will be deleted.
3416 * returns PERSIST_DEL_KEY_OK if deletion succeeds,
3417 * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist,
3418 * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs.
3419 *
3420 */
3421static int persist_del_keys(const char *fieldname, unsigned index)
3422{
3423 unsigned int i;
3424 unsigned int j;
3425 unsigned int num;
3426
3427 if (persist_data == NULL) {
3428 return PERSIST_DEL_KEY_ERROR_OTHER;
3429 }
3430
3431 num = persist_data->persist_valid_entries;
3432
3433 j = 0; // points to the end of non-deleted entries.
3434 // Filter out to-be-deleted entries in place.
3435 for (i = 0; i < num; i++) {
3436 if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) {
3437 persist_data->persist_entry[j] = persist_data->persist_entry[i];
3438 j++;
3439 }
3440 }
3441
3442 if (j < num) {
3443 persist_data->persist_valid_entries = j;
3444 // Zeroise the remaining entries
3445 memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry));
3446 return PERSIST_DEL_KEY_OK;
3447 } else {
3448 // Did not find an entry matching the given fieldname
3449 return PERSIST_DEL_KEY_ERROR_NO_FIELD;
3450 }
3451}
3452
3453static int persist_count_keys(const char *fieldname)
3454{
3455 unsigned int i;
3456 unsigned int count;
3457
3458 if (persist_data == NULL) {
3459 return -1;
3460 }
3461
3462 count = 0;
3463 for (i = 0; i < persist_data->persist_valid_entries; i++) {
3464 if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) {
3465 count++;
3466 }
3467 }
3468
3469 return count;
3470}
3471
Ken Sumrall160b4d62013-04-22 12:15:39 -07003472/* Return the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01003473int cryptfs_getfield(const char *fieldname, char *value, int len)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003474{
3475 char temp_value[PROPERTY_VALUE_MAX];
3476 char real_blkdev[MAXPATHLEN];
Rubin Xu85c01f92014-10-13 12:49:54 +01003477 /* CRYPTO_GETFIELD_OK is success,
3478 * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set,
3479 * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small,
3480 * CRYPTO_GETFIELD_ERROR_OTHER is any other error
Ken Sumrall160b4d62013-04-22 12:15:39 -07003481 */
Rubin Xu85c01f92014-10-13 12:49:54 +01003482 int rc = CRYPTO_GETFIELD_ERROR_OTHER;
3483 int i;
3484 char temp_field[PROPERTY_KEY_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -07003485
3486 if (persist_data == NULL) {
3487 load_persistent_data();
3488 if (persist_data == NULL) {
3489 SLOGE("Getfield error, cannot load persistent data");
3490 goto out;
3491 }
3492 }
3493
Rubin Xu85c01f92014-10-13 12:49:54 +01003494 // Read value from persistent entries. If the original value is split into multiple entries,
3495 // stitch them back together.
Ken Sumrall160b4d62013-04-22 12:15:39 -07003496 if (!persist_get_key(fieldname, temp_value)) {
Rubin Xu85c01f92014-10-13 12:49:54 +01003497 // We found it, copy it to the caller's buffer and keep going until all entries are read.
3498 if (strlcpy(value, temp_value, len) >= (unsigned) len) {
3499 // value too small
3500 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
3501 goto out;
3502 }
3503 rc = CRYPTO_GETFIELD_OK;
3504
3505 for (i = 1; /* break explicitly */; i++) {
3506 if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >=
3507 (int) sizeof(temp_field)) {
3508 // If the fieldname is very long, we stop as soon as it begins to overflow the
3509 // maximum field length. At this point we have in fact fully read out the original
3510 // value because cryptfs_setfield would not allow fields with longer names to be
3511 // written in the first place.
3512 break;
3513 }
3514 if (!persist_get_key(temp_field, temp_value)) {
3515 if (strlcat(value, temp_value, len) >= (unsigned)len) {
3516 // value too small.
3517 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
3518 goto out;
3519 }
3520 } else {
3521 // Exhaust all entries.
3522 break;
3523 }
3524 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003525 } else {
3526 /* Sadness, it's not there. Return the error */
Rubin Xu85c01f92014-10-13 12:49:54 +01003527 rc = CRYPTO_GETFIELD_ERROR_NO_FIELD;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003528 }
3529
3530out:
3531 return rc;
3532}
3533
3534/* Set the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01003535int cryptfs_setfield(const char *fieldname, const char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003536{
3537 struct crypt_persist_data stored_pdata;
3538 struct crypt_persist_data *pdata_p;
3539 struct crypt_mnt_ftr crypt_ftr;
3540 char encrypted_state[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01003541 /* 0 is success, negative values are error */
3542 int rc = CRYPTO_SETFIELD_ERROR_OTHER;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003543 int encrypted = 0;
Rubin Xu85c01f92014-10-13 12:49:54 +01003544 unsigned int field_id;
3545 char temp_field[PROPERTY_KEY_MAX];
3546 unsigned int num_entries;
3547 unsigned int max_keylen;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003548
3549 if (persist_data == NULL) {
3550 load_persistent_data();
3551 if (persist_data == NULL) {
3552 SLOGE("Setfield error, cannot load persistent data");
3553 goto out;
3554 }
3555 }
3556
3557 property_get("ro.crypto.state", encrypted_state, "");
3558 if (!strcmp(encrypted_state, "encrypted") ) {
3559 encrypted = 1;
3560 }
3561
Rubin Xu85c01f92014-10-13 12:49:54 +01003562 // Compute the number of entries required to store value, each entry can store up to
3563 // (PROPERTY_VALUE_MAX - 1) chars
3564 if (strlen(value) == 0) {
3565 // Empty value also needs one entry to store.
3566 num_entries = 1;
3567 } else {
3568 num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1);
3569 }
3570
3571 max_keylen = strlen(fieldname);
3572 if (num_entries > 1) {
3573 // Need an extra "_%d" suffix.
3574 max_keylen += 1 + log10(num_entries);
3575 }
3576 if (max_keylen > PROPERTY_KEY_MAX - 1) {
3577 rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003578 goto out;
3579 }
3580
Rubin Xu85c01f92014-10-13 12:49:54 +01003581 // Make sure we have enough space to write the new value
3582 if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) >
3583 persist_get_max_entries(encrypted)) {
3584 rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG;
3585 goto out;
3586 }
3587
3588 // Now that we know persist_data has enough space for value, let's delete the old field first
3589 // to make up space.
3590 persist_del_keys(fieldname, 0);
3591
3592 if (persist_set_key(fieldname, value, encrypted)) {
3593 // fail to set key, should not happen as we have already checked the available space
3594 SLOGE("persist_set_key() error during setfield()");
3595 goto out;
3596 }
3597
3598 for (field_id = 1; field_id < num_entries; field_id++) {
3599 snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, field_id);
3600
3601 if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), encrypted)) {
3602 // fail to set key, should not happen as we have already checked the available space.
3603 SLOGE("persist_set_key() error during setfield()");
3604 goto out;
3605 }
3606 }
3607
Ken Sumrall160b4d62013-04-22 12:15:39 -07003608 /* If we are running encrypted, save the persistent data now */
3609 if (encrypted) {
3610 if (save_persistent_data()) {
3611 SLOGE("Setfield error, cannot save persistent data");
3612 goto out;
3613 }
3614 }
3615
Rubin Xu85c01f92014-10-13 12:49:54 +01003616 rc = CRYPTO_SETFIELD_OK;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003617
3618out:
3619 return rc;
3620}
Paul Lawrencef4faa572014-01-29 13:31:03 -08003621
3622/* Checks userdata. Attempt to mount the volume if default-
3623 * encrypted.
3624 * On success trigger next init phase and return 0.
3625 * Currently do not handle failure - see TODO below.
3626 */
3627int cryptfs_mount_default_encrypted(void)
3628{
3629 char decrypt_state[PROPERTY_VALUE_MAX];
3630 property_get("vold.decrypt", decrypt_state, "0");
3631 if (!strcmp(decrypt_state, "0")) {
3632 SLOGE("Not encrypted - should not call here");
3633 } else {
3634 int crypt_type = cryptfs_get_password_type();
3635 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
3636 SLOGE("Bad crypt type - error");
3637 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
3638 SLOGD("Password is not default - "
3639 "starting min framework to prompt");
3640 property_set("vold.decrypt", "trigger_restart_min_framework");
3641 return 0;
3642 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
3643 SLOGD("Password is default - restarting filesystem");
3644 cryptfs_restart_internal(0);
3645 return 0;
3646 } else {
3647 SLOGE("Encrypted, default crypt type but can't decrypt");
3648 }
3649 }
3650
Paul Lawrence6bfed202014-07-28 12:47:22 -07003651 /** Corrupt. Allow us to boot into framework, which will detect bad
3652 crypto when it calls do_crypto_complete, then do a factory reset
Paul Lawrencef4faa572014-01-29 13:31:03 -08003653 */
Paul Lawrence6bfed202014-07-28 12:47:22 -07003654 property_set("vold.decrypt", "trigger_restart_min_framework");
Paul Lawrencef4faa572014-01-29 13:31:03 -08003655 return 0;
3656}
3657
3658/* Returns type of the password, default, pattern, pin or password.
3659 */
3660int cryptfs_get_password_type(void)
3661{
3662 struct crypt_mnt_ftr crypt_ftr;
3663
3664 if (get_crypt_ftr_and_key(&crypt_ftr)) {
3665 SLOGE("Error getting crypt footer and key\n");
3666 return -1;
3667 }
3668
Paul Lawrence6bfed202014-07-28 12:47:22 -07003669 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) {
3670 return -1;
3671 }
3672
Paul Lawrencef4faa572014-01-29 13:31:03 -08003673 return crypt_ftr.crypt_type;
3674}
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003675
Paul Lawrence399317e2014-03-10 13:20:50 -07003676char* cryptfs_get_password()
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003677{
Paul Lawrence399317e2014-03-10 13:20:50 -07003678 struct timespec now;
3679 clock_gettime(CLOCK_MONOTONIC, &now);
3680 if (now.tv_sec < password_expiry_time) {
3681 return password;
3682 } else {
3683 cryptfs_clear_password();
3684 return 0;
3685 }
3686}
3687
3688void cryptfs_clear_password()
3689{
3690 if (password) {
3691 size_t len = strlen(password);
3692 memset(password, 0, len);
3693 free(password);
3694 password = 0;
3695 password_expiry_time = 0;
3696 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003697}