blob: af74d74a8369e5a29773f210f12a0175ff839328 [file] [log] [blame]
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* TO DO:
18 * 1. Perhaps keep several copies of the encrypted key, in case something
19 * goes horribly wrong?
20 *
21 */
22
23#include <sys/types.h>
Ken Sumralle550f782013-08-20 13:48:23 -070024#include <sys/wait.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080025#include <sys/stat.h>
Paul Lawrencef4faa572014-01-29 13:31:03 -080026#include <ctype.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080027#include <fcntl.h>
Elliott Hughes73737162014-06-25 17:27:42 -070028#include <inttypes.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080029#include <unistd.h>
30#include <stdio.h>
31#include <sys/ioctl.h>
32#include <linux/dm-ioctl.h>
33#include <libgen.h>
34#include <stdlib.h>
35#include <sys/param.h>
36#include <string.h>
37#include <sys/mount.h>
38#include <openssl/evp.h>
Adam Langley41405bb2015-01-22 16:45:28 -080039#include <openssl/sha.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080040#include <errno.h>
Ken Sumrall3ed82362011-01-28 23:31:16 -080041#include <ext4.h>
Ken Sumrall29d8da82011-05-18 17:20:07 -070042#include <linux/kdev_t.h>
Ken Sumralle5032c42012-04-01 23:58:44 -070043#include <fs_mgr.h>
Paul Lawrence9c58a872014-09-30 09:12:51 -070044#include <time.h>
Rubin Xu85c01f92014-10-13 12:49:54 +010045#include <math.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080046#include "cryptfs.h"
47#define LOG_TAG "Cryptfs"
48#include "cutils/log.h"
49#include "cutils/properties.h"
Ken Sumralladfba362013-06-04 16:37:52 -070050#include "cutils/android_reboot.h"
Ken Sumrall5d4c68e2011-01-30 19:06:03 -080051#include "hardware_legacy/power.h"
Ken Sumralle550f782013-08-20 13:48:23 -070052#include <logwrap/logwrap.h>
Ken Sumrall29d8da82011-05-18 17:20:07 -070053#include "VolumeManager.h"
Ken Sumrall9caab762013-06-11 19:10:20 -070054#include "VoldUtil.h"
Kenny Rootc4c70f12013-06-14 12:11:38 -070055#include "crypto_scrypt.h"
Paul Lawrenceae59fe62014-01-21 08:23:27 -080056#include "ext4_utils.h"
Daniel Rosenberge82df162014-08-15 22:19:23 +000057#include "f2fs_sparseblock.h"
Paul Lawrence87999172014-02-20 12:21:31 -080058#include "CheckBattery.h"
jessica_yu3f14fe42014-09-22 15:57:40 +080059#include "Process.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080060
Shawn Willdenb87264f2015-02-24 09:51:34 -070061#include <hardware/keymaster0.h>
Paul Lawrence69f4ebd2014-04-14 12:17:14 -070062
Mark Salyzyn3e971272014-01-21 13:27:04 -080063#define UNUSED __attribute__((unused))
64
Mark Salyzyn5eecc442014-02-12 14:16:14 -080065#define UNUSED __attribute__((unused))
66
Ajay Dudani87701e22014-09-17 21:02:52 -070067#ifdef CONFIG_HW_DISK_ENCRYPTION
68#include "cryptfs_hw.h"
69#endif
70
Ken Sumrall8f869aa2010-12-03 03:47:09 -080071#define DM_CRYPT_BUF_SIZE 4096
72
Jason parks70a4b3f2011-01-28 10:10:47 -060073#define HASH_COUNT 2000
74#define KEY_LEN_BYTES 16
75#define IV_LEN_BYTES 16
76
Ken Sumrall29d8da82011-05-18 17:20:07 -070077#define KEY_IN_FOOTER "footer"
78
Paul Lawrencef4faa572014-01-29 13:31:03 -080079// "default_password" encoded into hex (d=0x64 etc)
80#define DEFAULT_PASSWORD "64656661756c745f70617373776f7264"
81
Ken Sumrall29d8da82011-05-18 17:20:07 -070082#define EXT4_FS 1
JP Abgrall62c7af32014-06-16 13:01:23 -070083#define F2FS_FS 2
Ken Sumrall29d8da82011-05-18 17:20:07 -070084
Ken Sumralle919efe2012-09-29 17:07:41 -070085#define TABLE_LOAD_RETRIES 10
86
Shawn Willden47ba10d2014-09-03 17:07:06 -060087#define RSA_KEY_SIZE 2048
88#define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8)
89#define RSA_EXPONENT 0x10001
Paul Lawrence69f4ebd2014-04-14 12:17:14 -070090
Paul Lawrence8e3f4512014-09-08 10:11:17 -070091#define RETRY_MOUNT_ATTEMPTS 10
92#define RETRY_MOUNT_DELAY_SECONDS 1
93
Ken Sumrall8f869aa2010-12-03 03:47:09 -080094char *me = "cryptfs";
95
Jason parks70a4b3f2011-01-28 10:10:47 -060096static unsigned char saved_master_key[KEY_LEN_BYTES];
Ken Sumrall3ad90722011-10-04 20:38:29 -070097static char *saved_mount_point;
Jason parks70a4b3f2011-01-28 10:10:47 -060098static int master_key_saved = 0;
Ken Sumrall160b4d62013-04-22 12:15:39 -070099static struct crypt_persist_data *persist_data = NULL;
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800100
Shawn Willdenb87264f2015-02-24 09:51:34 -0700101static int keymaster_init(keymaster0_device_t **keymaster_dev)
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700102{
103 int rc;
104
105 const hw_module_t* mod;
106 rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod);
107 if (rc) {
108 ALOGE("could not find any keystore module");
109 goto out;
110 }
111
Shawn Willdenb87264f2015-02-24 09:51:34 -0700112 rc = keymaster0_open(mod, keymaster_dev);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700113 if (rc) {
114 ALOGE("could not open keymaster device in %s (%s)",
115 KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc));
116 goto out;
117 }
118
119 return 0;
120
121out:
122 *keymaster_dev = NULL;
123 return rc;
124}
125
126/* Should we use keymaster? */
127static int keymaster_check_compatibility()
128{
Shawn Willdenb87264f2015-02-24 09:51:34 -0700129 keymaster0_device_t *keymaster_dev = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700130 int rc = 0;
131
132 if (keymaster_init(&keymaster_dev)) {
133 SLOGE("Failed to init keymaster");
134 rc = -1;
135 goto out;
136 }
137
Paul Lawrence8c008392014-05-06 14:02:48 -0700138 SLOGI("keymaster version is %d", keymaster_dev->common.module->module_api_version);
139
140 if (keymaster_dev->common.module->module_api_version
141 < KEYMASTER_MODULE_API_VERSION_0_3) {
142 rc = 0;
143 goto out;
144 }
145
Shawn Willden7c49ab02014-10-30 08:12:32 -0600146 if (!(keymaster_dev->flags & KEYMASTER_SOFTWARE_ONLY) &&
147 (keymaster_dev->flags & KEYMASTER_BLOBS_ARE_STANDALONE)) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700148 rc = 1;
149 }
150
151out:
Shawn Willdenb87264f2015-02-24 09:51:34 -0700152 keymaster0_close(keymaster_dev);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700153 return rc;
154}
155
156/* Create a new keymaster key and store it in this footer */
157static int keymaster_create_key(struct crypt_mnt_ftr *ftr)
158{
159 uint8_t* key = 0;
Shawn Willdenb87264f2015-02-24 09:51:34 -0700160 keymaster0_device_t *keymaster_dev = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700161
162 if (keymaster_init(&keymaster_dev)) {
163 SLOGE("Failed to init keymaster");
164 return -1;
165 }
166
167 int rc = 0;
168
169 keymaster_rsa_keygen_params_t params;
170 memset(&params, '\0', sizeof(params));
Shawn Willden47ba10d2014-09-03 17:07:06 -0600171 params.public_exponent = RSA_EXPONENT;
172 params.modulus_size = RSA_KEY_SIZE;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700173
174 size_t key_size;
175 if (keymaster_dev->generate_keypair(keymaster_dev, TYPE_RSA, &params,
176 &key, &key_size)) {
177 SLOGE("Failed to generate keypair");
178 rc = -1;
179 goto out;
180 }
181
182 if (key_size > KEYMASTER_BLOB_SIZE) {
183 SLOGE("Keymaster key too large for crypto footer");
184 rc = -1;
185 goto out;
186 }
187
188 memcpy(ftr->keymaster_blob, key, key_size);
189 ftr->keymaster_blob_size = key_size;
190
191out:
Shawn Willdenb87264f2015-02-24 09:51:34 -0700192 keymaster0_close(keymaster_dev);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700193 free(key);
194 return rc;
195}
196
Shawn Willdene17a9c42014-09-08 13:04:08 -0600197/* This signs the given object using the keymaster key. */
198static int keymaster_sign_object(struct crypt_mnt_ftr *ftr,
Shawn Willden47ba10d2014-09-03 17:07:06 -0600199 const unsigned char *object,
200 const size_t object_size,
201 unsigned char **signature,
202 size_t *signature_size)
203{
204 int rc = 0;
Shawn Willdenb87264f2015-02-24 09:51:34 -0700205 keymaster0_device_t *keymaster_dev = 0;
Shawn Willden47ba10d2014-09-03 17:07:06 -0600206 if (keymaster_init(&keymaster_dev)) {
207 SLOGE("Failed to init keymaster");
208 return -1;
209 }
210
211 /* We currently set the digest type to DIGEST_NONE because it's the
212 * only supported value for keymaster. A similar issue exists with
213 * PADDING_NONE. Long term both of these should likely change.
214 */
215 keymaster_rsa_sign_params_t params;
216 params.digest_type = DIGEST_NONE;
217 params.padding_type = PADDING_NONE;
218
219 unsigned char to_sign[RSA_KEY_SIZE_BYTES];
Shawn Willdene17a9c42014-09-08 13:04:08 -0600220 size_t to_sign_size = sizeof(to_sign);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600221 memset(to_sign, 0, RSA_KEY_SIZE_BYTES);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600222
Shawn Willdene17a9c42014-09-08 13:04:08 -0600223 // To sign a message with RSA, the message must satisfy two
224 // constraints:
225 //
226 // 1. The message, when interpreted as a big-endian numeric value, must
227 // be strictly less than the public modulus of the RSA key. Note
228 // that because the most significant bit of the public modulus is
229 // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit
230 // key), an n-bit message with most significant bit 0 always
231 // satisfies this requirement.
232 //
233 // 2. The message must have the same length in bits as the public
234 // modulus of the RSA key. This requirement isn't mathematically
235 // necessary, but is necessary to ensure consistency in
236 // implementations.
237 switch (ftr->kdf_type) {
238 case KDF_SCRYPT_KEYMASTER_UNPADDED:
239 // This is broken: It produces a message which is shorter than
240 // the public modulus, failing criterion 2.
241 memcpy(to_sign, object, object_size);
242 to_sign_size = object_size;
243 SLOGI("Signing unpadded object");
244 break;
245 case KDF_SCRYPT_KEYMASTER_BADLY_PADDED:
246 // This is broken: Since the value of object is uniformly
247 // distributed, it produces a message that is larger than the
248 // public modulus with probability 0.25.
249 memcpy(to_sign, object, min(RSA_KEY_SIZE_BYTES, object_size));
250 SLOGI("Signing end-padded object");
251 break;
252 case KDF_SCRYPT_KEYMASTER:
253 // This ensures the most significant byte of the signed message
254 // is zero. We could have zero-padded to the left instead, but
255 // this approach is slightly more robust against changes in
256 // object size. However, it's still broken (but not unusably
257 // so) because we really should be using a proper RSA padding
258 // function, such as OAEP.
259 //
260 // TODO(paullawrence): When keymaster 0.4 is available, change
261 // this to use the padding options it provides.
262 memcpy(to_sign + 1, object, min(RSA_KEY_SIZE_BYTES - 1, object_size));
263 SLOGI("Signing safely-padded object");
264 break;
265 default:
266 SLOGE("Unknown KDF type %d", ftr->kdf_type);
267 return -1;
268 }
269
Shawn Willden47ba10d2014-09-03 17:07:06 -0600270 rc = keymaster_dev->sign_data(keymaster_dev,
271 &params,
272 ftr->keymaster_blob,
273 ftr->keymaster_blob_size,
274 to_sign,
Shawn Willdene17a9c42014-09-08 13:04:08 -0600275 to_sign_size,
Shawn Willden47ba10d2014-09-03 17:07:06 -0600276 signature,
277 signature_size);
278
Shawn Willdenb87264f2015-02-24 09:51:34 -0700279 keymaster0_close(keymaster_dev);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600280 return rc;
281}
282
Paul Lawrence399317e2014-03-10 13:20:50 -0700283/* Store password when userdata is successfully decrypted and mounted.
284 * Cleared by cryptfs_clear_password
285 *
286 * To avoid a double prompt at boot, we need to store the CryptKeeper
287 * password and pass it to KeyGuard, which uses it to unlock KeyStore.
288 * Since the entire framework is torn down and rebuilt after encryption,
289 * we have to use a daemon or similar to store the password. Since vold
290 * is secured against IPC except from system processes, it seems a reasonable
291 * place to store this.
292 *
293 * password should be cleared once it has been used.
294 *
295 * password is aged out after password_max_age_seconds seconds.
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800296 */
Paul Lawrence399317e2014-03-10 13:20:50 -0700297static char* password = 0;
298static int password_expiry_time = 0;
299static const int password_max_age_seconds = 60;
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800300
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800301extern struct fstab *fstab;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800302
Paul Lawrence87999172014-02-20 12:21:31 -0800303enum RebootType {reboot, recovery, shutdown};
304static void cryptfs_reboot(enum RebootType rt)
Ken Sumralladfba362013-06-04 16:37:52 -0700305{
Paul Lawrence87999172014-02-20 12:21:31 -0800306 switch(rt) {
307 case reboot:
308 property_set(ANDROID_RB_PROPERTY, "reboot");
309 break;
310
311 case recovery:
312 property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
313 break;
314
315 case shutdown:
316 property_set(ANDROID_RB_PROPERTY, "shutdown");
317 break;
Ken Sumralladfba362013-06-04 16:37:52 -0700318 }
Paul Lawrence87999172014-02-20 12:21:31 -0800319
Ken Sumralladfba362013-06-04 16:37:52 -0700320 sleep(20);
321
322 /* Shouldn't get here, reboot should happen before sleep times out */
323 return;
324}
325
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800326static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
327{
328 memset(io, 0, dataSize);
329 io->data_size = dataSize;
330 io->data_start = sizeof(struct dm_ioctl);
331 io->version[0] = 4;
332 io->version[1] = 0;
333 io->version[2] = 0;
334 io->flags = flags;
335 if (name) {
Marek Pola5e6b9142015-02-05 14:22:34 +0100336 strlcpy(io->name, name, sizeof(io->name));
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800337 }
338}
339
Kenny Rootc4c70f12013-06-14 12:11:38 -0700340/**
341 * Gets the default device scrypt parameters for key derivation time tuning.
342 * The parameters should lead to about one second derivation time for the
343 * given device.
344 */
345static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
346 const int default_params[] = SCRYPT_DEFAULTS;
347 int params[] = SCRYPT_DEFAULTS;
348 char paramstr[PROPERTY_VALUE_MAX];
349 char *token;
350 char *saveptr;
351 int i;
352
353 property_get(SCRYPT_PROP, paramstr, "");
354 if (paramstr[0] != '\0') {
355 /*
356 * The token we're looking for should be three integers separated by
357 * colons (e.g., "12:8:1"). Scan the property to make sure it matches.
358 */
Kenny Root2947e342013-08-14 15:54:49 -0700359 for (i = 0, token = strtok_r(paramstr, ":", &saveptr);
360 token != NULL && i < 3;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700361 i++, token = strtok_r(NULL, ":", &saveptr)) {
362 char *endptr;
363 params[i] = strtol(token, &endptr, 10);
364
365 /*
366 * Check that there was a valid number and it's 8-bit. If not,
367 * break out and the end check will take the default values.
368 */
369 if ((*token == '\0') || (*endptr != '\0') || params[i] < 0 || params[i] > 255) {
370 break;
371 }
372 }
373
374 /*
375 * If there were not enough tokens or a token was malformed (not an
376 * integer), it will end up here and the default parameters can be
377 * taken.
378 */
379 if ((i != 3) || (token != NULL)) {
380 SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
381 memcpy(params, default_params, sizeof(params));
382 }
383 }
384
385 ftr->N_factor = params[0];
386 ftr->r_factor = params[1];
387 ftr->p_factor = params[2];
388}
389
Ken Sumrall3ed82362011-01-28 23:31:16 -0800390static unsigned int get_fs_size(char *dev)
391{
392 int fd, block_size;
393 struct ext4_super_block sb;
394 off64_t len;
395
396 if ((fd = open(dev, O_RDONLY)) < 0) {
397 SLOGE("Cannot open device to get filesystem size ");
398 return 0;
399 }
400
401 if (lseek64(fd, 1024, SEEK_SET) < 0) {
402 SLOGE("Cannot seek to superblock");
403 return 0;
404 }
405
406 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
407 SLOGE("Cannot read superblock");
408 return 0;
409 }
410
411 close(fd);
412
Daniel Rosenberge82df162014-08-15 22:19:23 +0000413 if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) {
414 SLOGE("Not a valid ext4 superblock");
415 return 0;
416 }
Ken Sumrall3ed82362011-01-28 23:31:16 -0800417 block_size = 1024 << sb.s_log_block_size;
418 /* compute length in bytes */
419 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
420
421 /* return length in sectors */
422 return (unsigned int) (len / 512);
423}
424
Ken Sumrall160b4d62013-04-22 12:15:39 -0700425static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
426{
427 static int cached_data = 0;
428 static off64_t cached_off = 0;
429 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
430 int fd;
431 char key_loc[PROPERTY_VALUE_MAX];
432 char real_blkdev[PROPERTY_VALUE_MAX];
433 unsigned int nr_sec;
434 int rc = -1;
435
436 if (!cached_data) {
437 fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
438
439 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
440 if ( (fd = open(real_blkdev, O_RDWR)) < 0) {
441 SLOGE("Cannot open real block device %s\n", real_blkdev);
442 return -1;
443 }
444
445 if ((nr_sec = get_blkdev_size(fd))) {
446 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
447 * encryption info footer and key, and plenty of bytes to spare for future
448 * growth.
449 */
450 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
451 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
452 cached_data = 1;
453 } else {
454 SLOGE("Cannot get size of block device %s\n", real_blkdev);
455 }
456 close(fd);
457 } else {
458 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
459 cached_off = 0;
460 cached_data = 1;
461 }
462 }
463
464 if (cached_data) {
465 if (metadata_fname) {
466 *metadata_fname = cached_metadata_fname;
467 }
468 if (off) {
469 *off = cached_off;
470 }
471 rc = 0;
472 }
473
474 return rc;
475}
476
Ken Sumralle8744072011-01-18 22:01:55 -0800477/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800478 * update the failed mount count but not change the key.
479 */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700480static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800481{
482 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800483 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700484 /* starting_off is set to the SEEK_SET offset
485 * where the crypto structure starts
486 */
487 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800488 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700489 char *fname = NULL;
Ken Sumrall3be890f2011-09-14 16:53:46 -0700490 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800491
Ken Sumrall160b4d62013-04-22 12:15:39 -0700492 if (get_crypt_ftr_info(&fname, &starting_off)) {
493 SLOGE("Unable to get crypt_ftr_info\n");
494 return -1;
495 }
496 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700497 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700498 return -1;
499 }
Ken Sumralle550f782013-08-20 13:48:23 -0700500 if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) {
501 SLOGE("Cannot open footer file %s for put\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700502 return -1;
503 }
504
505 /* Seek to the start of the crypt footer */
506 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
507 SLOGE("Cannot seek to real block device footer\n");
508 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800509 }
510
511 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
512 SLOGE("Cannot write real block device footer\n");
513 goto errout;
514 }
515
Ken Sumrall3be890f2011-09-14 16:53:46 -0700516 fstat(fd, &statbuf);
517 /* If the keys are kept on a raw block device, do not try to truncate it. */
Ken Sumralle550f782013-08-20 13:48:23 -0700518 if (S_ISREG(statbuf.st_mode)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700519 if (ftruncate(fd, 0x4000)) {
Colin Cross59846b62014-02-06 20:34:29 -0800520 SLOGE("Cannot set footer file size\n");
Ken Sumralle8744072011-01-18 22:01:55 -0800521 goto errout;
522 }
523 }
524
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800525 /* Success! */
526 rc = 0;
527
528errout:
529 close(fd);
530 return rc;
531
532}
533
Ken Sumrall160b4d62013-04-22 12:15:39 -0700534static inline int unix_read(int fd, void* buff, int len)
535{
536 return TEMP_FAILURE_RETRY(read(fd, buff, len));
537}
538
539static inline int unix_write(int fd, const void* buff, int len)
540{
541 return TEMP_FAILURE_RETRY(write(fd, buff, len));
542}
543
544static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
545{
546 memset(pdata, 0, len);
547 pdata->persist_magic = PERSIST_DATA_MAGIC;
548 pdata->persist_valid_entries = 0;
549}
550
551/* A routine to update the passed in crypt_ftr to the lastest version.
552 * fd is open read/write on the device that holds the crypto footer and persistent
553 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
554 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
555 */
556static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
557{
Kenny Root7434b312013-06-14 11:29:53 -0700558 int orig_major = crypt_ftr->major_version;
559 int orig_minor = crypt_ftr->minor_version;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700560
Kenny Root7434b312013-06-14 11:29:53 -0700561 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
562 struct crypt_persist_data *pdata;
563 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700564
Kenny Rootc4c70f12013-06-14 12:11:38 -0700565 SLOGW("upgrading crypto footer to 1.1");
566
Kenny Root7434b312013-06-14 11:29:53 -0700567 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
568 if (pdata == NULL) {
569 SLOGE("Cannot allocate persisent data\n");
570 return;
571 }
572 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
573
574 /* Need to initialize the persistent data area */
575 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
576 SLOGE("Cannot seek to persisent data offset\n");
Henrik Baard91064632015-02-05 15:09:17 +0100577 free(pdata);
Kenny Root7434b312013-06-14 11:29:53 -0700578 return;
579 }
580 /* Write all zeros to the first copy, making it invalid */
581 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
582
583 /* Write a valid but empty structure to the second copy */
584 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
585 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
586
587 /* Update the footer */
588 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
589 crypt_ftr->persist_data_offset[0] = pdata_offset;
590 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
591 crypt_ftr->minor_version = 1;
Henrik Baard91064632015-02-05 15:09:17 +0100592 free(pdata);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700593 }
594
Paul Lawrencef4faa572014-01-29 13:31:03 -0800595 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700596 SLOGW("upgrading crypto footer to 1.2");
JP Abgrall7bdfa522013-11-15 13:42:56 -0800597 /* But keep the old kdf_type.
598 * It will get updated later to KDF_SCRYPT after the password has been verified.
599 */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700600 crypt_ftr->kdf_type = KDF_PBKDF2;
601 get_device_scrypt_params(crypt_ftr);
602 crypt_ftr->minor_version = 2;
603 }
604
Paul Lawrencef4faa572014-01-29 13:31:03 -0800605 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
606 SLOGW("upgrading crypto footer to 1.3");
607 crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
608 crypt_ftr->minor_version = 3;
609 }
610
Kenny Root7434b312013-06-14 11:29:53 -0700611 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
612 if (lseek64(fd, offset, SEEK_SET) == -1) {
613 SLOGE("Cannot seek to crypt footer\n");
614 return;
615 }
616 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700617 }
Ken Sumrall160b4d62013-04-22 12:15:39 -0700618}
619
620
621static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800622{
623 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800624 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700625 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800626 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700627 char *fname = NULL;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700628 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800629
Ken Sumrall160b4d62013-04-22 12:15:39 -0700630 if (get_crypt_ftr_info(&fname, &starting_off)) {
631 SLOGE("Unable to get crypt_ftr_info\n");
632 return -1;
633 }
634 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700635 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700636 return -1;
637 }
638 if ( (fd = open(fname, O_RDWR)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700639 SLOGE("Cannot open footer file %s for get\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700640 return -1;
641 }
642
643 /* Make sure it's 16 Kbytes in length */
644 fstat(fd, &statbuf);
645 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
646 SLOGE("footer file %s is not the expected size!\n", fname);
647 goto errout;
648 }
649
650 /* Seek to the start of the crypt footer */
651 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
652 SLOGE("Cannot seek to real block device footer\n");
653 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800654 }
655
656 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
657 SLOGE("Cannot read real block device footer\n");
658 goto errout;
659 }
660
661 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700662 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800663 goto errout;
664 }
665
Kenny Rootc96a5f82013-06-14 12:08:28 -0700666 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
667 SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
668 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800669 goto errout;
670 }
671
Kenny Rootc96a5f82013-06-14 12:08:28 -0700672 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
673 SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
674 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800675 }
676
Ken Sumrall160b4d62013-04-22 12:15:39 -0700677 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
678 * copy on disk before returning.
679 */
Kenny Rootc96a5f82013-06-14 12:08:28 -0700680 if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700681 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
Ken Sumralle8744072011-01-18 22:01:55 -0800682 }
683
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800684 /* Success! */
685 rc = 0;
686
687errout:
688 close(fd);
689 return rc;
690}
691
Ken Sumrall160b4d62013-04-22 12:15:39 -0700692static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
693{
694 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
695 crypt_ftr->persist_data_offset[1]) {
696 SLOGE("Crypt_ftr persist data regions overlap");
697 return -1;
698 }
699
700 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
701 SLOGE("Crypt_ftr persist data region 0 starts after region 1");
702 return -1;
703 }
704
705 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
706 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
707 CRYPT_FOOTER_OFFSET) {
708 SLOGE("Persistent data extends past crypto footer");
709 return -1;
710 }
711
712 return 0;
713}
714
715static int load_persistent_data(void)
716{
717 struct crypt_mnt_ftr crypt_ftr;
718 struct crypt_persist_data *pdata = NULL;
719 char encrypted_state[PROPERTY_VALUE_MAX];
720 char *fname;
721 int found = 0;
722 int fd;
723 int ret;
724 int i;
725
726 if (persist_data) {
727 /* Nothing to do, we've already loaded or initialized it */
728 return 0;
729 }
730
731
732 /* If not encrypted, just allocate an empty table and initialize it */
733 property_get("ro.crypto.state", encrypted_state, "");
734 if (strcmp(encrypted_state, "encrypted") ) {
735 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
736 if (pdata) {
737 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
738 persist_data = pdata;
739 return 0;
740 }
741 return -1;
742 }
743
744 if(get_crypt_ftr_and_key(&crypt_ftr)) {
745 return -1;
746 }
747
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700748 if ((crypt_ftr.major_version < 1)
749 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700750 SLOGE("Crypt_ftr version doesn't support persistent data");
751 return -1;
752 }
753
754 if (get_crypt_ftr_info(&fname, NULL)) {
755 return -1;
756 }
757
758 ret = validate_persistent_data_storage(&crypt_ftr);
759 if (ret) {
760 return -1;
761 }
762
763 fd = open(fname, O_RDONLY);
764 if (fd < 0) {
765 SLOGE("Cannot open %s metadata file", fname);
766 return -1;
767 }
768
769 if (persist_data == NULL) {
770 pdata = malloc(crypt_ftr.persist_data_size);
771 if (pdata == NULL) {
772 SLOGE("Cannot allocate memory for persistent data");
773 goto err;
774 }
775 }
776
777 for (i = 0; i < 2; i++) {
778 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
779 SLOGE("Cannot seek to read persistent data on %s", fname);
780 goto err2;
781 }
782 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
783 SLOGE("Error reading persistent data on iteration %d", i);
784 goto err2;
785 }
786 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
787 found = 1;
788 break;
789 }
790 }
791
792 if (!found) {
793 SLOGI("Could not find valid persistent data, creating");
794 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
795 }
796
797 /* Success */
798 persist_data = pdata;
799 close(fd);
800 return 0;
801
802err2:
803 free(pdata);
804
805err:
806 close(fd);
807 return -1;
808}
809
810static int save_persistent_data(void)
811{
812 struct crypt_mnt_ftr crypt_ftr;
813 struct crypt_persist_data *pdata;
814 char *fname;
815 off64_t write_offset;
816 off64_t erase_offset;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700817 int fd;
818 int ret;
819
820 if (persist_data == NULL) {
821 SLOGE("No persistent data to save");
822 return -1;
823 }
824
825 if(get_crypt_ftr_and_key(&crypt_ftr)) {
826 return -1;
827 }
828
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700829 if ((crypt_ftr.major_version < 1)
830 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700831 SLOGE("Crypt_ftr version doesn't support persistent data");
832 return -1;
833 }
834
835 ret = validate_persistent_data_storage(&crypt_ftr);
836 if (ret) {
837 return -1;
838 }
839
840 if (get_crypt_ftr_info(&fname, NULL)) {
841 return -1;
842 }
843
844 fd = open(fname, O_RDWR);
845 if (fd < 0) {
846 SLOGE("Cannot open %s metadata file", fname);
847 return -1;
848 }
849
850 pdata = malloc(crypt_ftr.persist_data_size);
851 if (pdata == NULL) {
852 SLOGE("Cannot allocate persistant data");
853 goto err;
854 }
855
856 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
857 SLOGE("Cannot seek to read persistent data on %s", fname);
858 goto err2;
859 }
860
861 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
862 SLOGE("Error reading persistent data before save");
863 goto err2;
864 }
865
866 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
867 /* The first copy is the curent valid copy, so write to
868 * the second copy and erase this one */
869 write_offset = crypt_ftr.persist_data_offset[1];
870 erase_offset = crypt_ftr.persist_data_offset[0];
871 } else {
872 /* The second copy must be the valid copy, so write to
873 * the first copy, and erase the second */
874 write_offset = crypt_ftr.persist_data_offset[0];
875 erase_offset = crypt_ftr.persist_data_offset[1];
876 }
877
878 /* Write the new copy first, if successful, then erase the old copy */
Björn Landström96dbee72015-01-20 12:43:56 +0100879 if (lseek64(fd, write_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700880 SLOGE("Cannot seek to write persistent data");
881 goto err2;
882 }
883 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
884 (int) crypt_ftr.persist_data_size) {
Björn Landström96dbee72015-01-20 12:43:56 +0100885 if (lseek64(fd, erase_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700886 SLOGE("Cannot seek to erase previous persistent data");
887 goto err2;
888 }
889 fsync(fd);
890 memset(pdata, 0, crypt_ftr.persist_data_size);
891 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
892 (int) crypt_ftr.persist_data_size) {
893 SLOGE("Cannot write to erase previous persistent data");
894 goto err2;
895 }
896 fsync(fd);
897 } else {
898 SLOGE("Cannot write to save persistent data");
899 goto err2;
900 }
901
902 /* Success */
903 free(pdata);
904 close(fd);
905 return 0;
906
907err2:
908 free(pdata);
909err:
910 close(fd);
911 return -1;
912}
913
Paul Lawrencef4faa572014-01-29 13:31:03 -0800914static int hexdigit (char c)
915{
916 if (c >= '0' && c <= '9') return c - '0';
917 c = tolower(c);
918 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
919 return -1;
920}
921
922static unsigned char* convert_hex_ascii_to_key(const char* master_key_ascii,
923 unsigned int* out_keysize)
924{
925 unsigned int i;
926 *out_keysize = 0;
927
928 size_t size = strlen (master_key_ascii);
929 if (size % 2) {
930 SLOGE("Trying to convert ascii string of odd length");
931 return NULL;
932 }
933
934 unsigned char* master_key = (unsigned char*) malloc(size / 2);
935 if (master_key == 0) {
936 SLOGE("Cannot allocate");
937 return NULL;
938 }
939
940 for (i = 0; i < size; i += 2) {
941 int high_nibble = hexdigit (master_key_ascii[i]);
942 int low_nibble = hexdigit (master_key_ascii[i + 1]);
943
944 if(high_nibble < 0 || low_nibble < 0) {
945 SLOGE("Invalid hex string");
946 free (master_key);
947 return NULL;
948 }
949
950 master_key[*out_keysize] = high_nibble * 16 + low_nibble;
951 (*out_keysize)++;
952 }
953
954 return master_key;
955}
956
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800957/* Convert a binary key of specified length into an ascii hex string equivalent,
958 * without the leading 0x and with null termination
959 */
Paul Lawrencef4faa572014-01-29 13:31:03 -0800960static void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800961 char *master_key_ascii)
962{
963 unsigned int i, a;
964 unsigned char nibble;
965
966 for (i=0, a=0; i<keysize; i++, a+=2) {
967 /* For each byte, write out two ascii hex digits */
968 nibble = (master_key[i] >> 4) & 0xf;
969 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
970
971 nibble = master_key[i] & 0xf;
972 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
973 }
974
975 /* Add the null termination */
976 master_key_ascii[a] = '\0';
977
978}
979
Ken Sumralldb5e0262013-02-05 17:39:48 -0800980static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
981 char *real_blk_name, const char *name, int fd,
982 char *extra_params)
983{
Dan Albertc07fa3f2014-12-18 10:00:55 -0800984 _Alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumralldb5e0262013-02-05 17:39:48 -0800985 struct dm_ioctl *io;
986 struct dm_target_spec *tgt;
987 char *crypt_params;
988 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
989 int i;
990
991 io = (struct dm_ioctl *) buffer;
992
993 /* Load the mapping table for this device */
994 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
995
996 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
997 io->target_count = 1;
998 tgt->status = 0;
999 tgt->sector_start = 0;
1000 tgt->length = crypt_ftr->fs_size;
Ajay Dudani87701e22014-09-17 21:02:52 -07001001#ifdef CONFIG_HW_DISK_ENCRYPTION
Iliyan Malchevbb7d9af2014-11-20 18:42:23 -08001002 if (!strcmp((char *)crypt_ftr->crypto_type_name, "aes-xts")) {
1003 strlcpy(tgt->target_type, "req-crypt", DM_MAX_TYPE_NAME);
1004 }
1005 else {
1006 strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME);
1007 }
Ajay Dudani87701e22014-09-17 21:02:52 -07001008#else
1009 strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME);
1010#endif
Ken Sumralldb5e0262013-02-05 17:39:48 -08001011
1012 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
1013 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
1014 sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name,
1015 master_key_ascii, real_blk_name, extra_params);
1016 crypt_params += strlen(crypt_params) + 1;
1017 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
1018 tgt->next = crypt_params - buffer;
1019
1020 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
1021 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
1022 break;
1023 }
1024 usleep(500000);
1025 }
1026
1027 if (i == TABLE_LOAD_RETRIES) {
1028 /* We failed to load the table, return an error */
1029 return -1;
1030 } else {
1031 return i + 1;
1032 }
1033}
1034
1035
1036static int get_dm_crypt_version(int fd, const char *name, int *version)
1037{
1038 char buffer[DM_CRYPT_BUF_SIZE];
1039 struct dm_ioctl *io;
1040 struct dm_target_versions *v;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001041
1042 io = (struct dm_ioctl *) buffer;
1043
1044 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1045
1046 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
1047 return -1;
1048 }
1049
1050 /* Iterate over the returned versions, looking for name of "crypt".
1051 * When found, get and return the version.
1052 */
1053 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
1054 while (v->next) {
Ajay Dudani87701e22014-09-17 21:02:52 -07001055#ifdef CONFIG_HW_DISK_ENCRYPTION
Iliyan Malchevbb7d9af2014-11-20 18:42:23 -08001056 if (! strcmp(v->name, "crypt") || ! strcmp(v->name, "req-crypt")) {
Ajay Dudani87701e22014-09-17 21:02:52 -07001057#else
Ken Sumralldb5e0262013-02-05 17:39:48 -08001058 if (! strcmp(v->name, "crypt")) {
Ajay Dudani87701e22014-09-17 21:02:52 -07001059#endif
Ken Sumralldb5e0262013-02-05 17:39:48 -08001060 /* We found the crypt driver, return the version, and get out */
1061 version[0] = v->version[0];
1062 version[1] = v->version[1];
1063 version[2] = v->version[2];
1064 return 0;
1065 }
1066 v = (struct dm_target_versions *)(((char *)v) + v->next);
1067 }
1068
1069 return -1;
1070}
1071
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001072static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
Ken Sumrall29d8da82011-05-18 17:20:07 -07001073 char *real_blk_name, char *crypto_blk_name, const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001074{
1075 char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001076 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001077 unsigned int minor;
Ajay Dudani87701e22014-09-17 21:02:52 -07001078 int fd=0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001079 int retval = -1;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001080 int version[3];
1081 char *extra_params;
1082 int load_count;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001083
1084 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
1085 SLOGE("Cannot open device-mapper\n");
1086 goto errout;
1087 }
1088
1089 io = (struct dm_ioctl *) buffer;
1090
1091 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1092 if (ioctl(fd, DM_DEV_CREATE, io)) {
1093 SLOGE("Cannot create dm-crypt device\n");
1094 goto errout;
1095 }
1096
1097 /* Get the device status, in particular, the name of it's device file */
1098 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1099 if (ioctl(fd, DM_DEV_STATUS, io)) {
1100 SLOGE("Cannot retrieve dm-crypt device status\n");
1101 goto errout;
1102 }
1103 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
1104 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
1105
Ken Sumralldb5e0262013-02-05 17:39:48 -08001106 extra_params = "";
1107 if (! get_dm_crypt_version(fd, name, version)) {
1108 /* Support for allow_discards was added in version 1.11.0 */
1109 if ((version[0] >= 2) ||
1110 ((version[0] == 1) && (version[1] >= 11))) {
1111 extra_params = "1 allow_discards";
1112 SLOGI("Enabling support for allow_discards in dmcrypt.\n");
1113 }
Ken Sumralle919efe2012-09-29 17:07:41 -07001114 }
1115
Ken Sumralldb5e0262013-02-05 17:39:48 -08001116 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name,
1117 fd, extra_params);
1118 if (load_count < 0) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001119 SLOGE("Cannot load dm-crypt mapping table.\n");
1120 goto errout;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001121 } else if (load_count > 1) {
1122 SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001123 }
1124
1125 /* Resume this device to activate it */
Ken Sumralldb5e0262013-02-05 17:39:48 -08001126 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001127
1128 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
1129 SLOGE("Cannot resume the dm-crypt device\n");
1130 goto errout;
1131 }
1132
1133 /* We made it here with no errors. Woot! */
1134 retval = 0;
1135
1136errout:
1137 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1138
1139 return retval;
1140}
1141
Ken Sumrall29d8da82011-05-18 17:20:07 -07001142static int delete_crypto_blk_dev(char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001143{
1144 int fd;
1145 char buffer[DM_CRYPT_BUF_SIZE];
1146 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001147 int retval = -1;
1148
1149 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
1150 SLOGE("Cannot open device-mapper\n");
1151 goto errout;
1152 }
1153
1154 io = (struct dm_ioctl *) buffer;
1155
1156 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1157 if (ioctl(fd, DM_DEV_REMOVE, io)) {
1158 SLOGE("Cannot remove dm-crypt device\n");
1159 goto errout;
1160 }
1161
1162 /* We made it here with no errors. Woot! */
1163 retval = 0;
1164
1165errout:
1166 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1167
1168 return retval;
1169
1170}
1171
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001172static int pbkdf2(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001173 unsigned char *ikey, void *params UNUSED)
1174{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001175 SLOGI("Using pbkdf2 for cryptfs KDF");
1176
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001177 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001178 unsigned int keysize;
1179 char* master_key = (char*)convert_hex_ascii_to_key(passwd, &keysize);
1180 if (!master_key) return -1;
1181 PKCS5_PBKDF2_HMAC_SHA1(master_key, keysize, salt, SALT_LEN,
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001182 HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001183
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001184 memset(master_key, 0, keysize);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001185 free (master_key);
1186 return 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001187}
1188
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001189static int scrypt(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001190 unsigned char *ikey, void *params)
1191{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001192 SLOGI("Using scrypt for cryptfs KDF");
1193
Kenny Rootc4c70f12013-06-14 12:11:38 -07001194 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1195
1196 int N = 1 << ftr->N_factor;
1197 int r = 1 << ftr->r_factor;
1198 int p = 1 << ftr->p_factor;
1199
1200 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001201 unsigned int keysize;
1202 unsigned char* master_key = convert_hex_ascii_to_key(passwd, &keysize);
1203 if (!master_key) return -1;
1204 crypto_scrypt(master_key, keysize, salt, SALT_LEN, N, r, p, ikey,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001205 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001206
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001207 memset(master_key, 0, keysize);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001208 free (master_key);
1209 return 0;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001210}
1211
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001212static int scrypt_keymaster(const char *passwd, const unsigned char *salt,
1213 unsigned char *ikey, void *params)
1214{
1215 SLOGI("Using scrypt with keymaster for cryptfs KDF");
1216
1217 int rc;
1218 unsigned int key_size;
1219 size_t signature_size;
1220 unsigned char* signature;
1221 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1222
1223 int N = 1 << ftr->N_factor;
1224 int r = 1 << ftr->r_factor;
1225 int p = 1 << ftr->p_factor;
1226
1227 unsigned char* master_key = convert_hex_ascii_to_key(passwd, &key_size);
1228 if (!master_key) {
1229 SLOGE("Failed to convert passwd from hex");
1230 return -1;
1231 }
1232
1233 rc = crypto_scrypt(master_key, key_size, salt, SALT_LEN,
1234 N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES);
1235 memset(master_key, 0, key_size);
1236 free(master_key);
1237
1238 if (rc) {
1239 SLOGE("scrypt failed");
1240 return -1;
1241 }
1242
Shawn Willdene17a9c42014-09-08 13:04:08 -06001243 if (keymaster_sign_object(ftr, ikey, KEY_LEN_BYTES + IV_LEN_BYTES,
1244 &signature, &signature_size)) {
1245 SLOGE("Signing failed");
1246 return -1;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001247 }
1248
1249 rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN,
1250 N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES);
1251 free(signature);
1252
1253 if (rc) {
1254 SLOGE("scrypt failed");
1255 return -1;
1256 }
1257
1258 return 0;
1259}
1260
1261static int encrypt_master_key(const char *passwd, const unsigned char *salt,
1262 const unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001263 unsigned char *encrypted_master_key,
1264 struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001265{
1266 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
1267 EVP_CIPHER_CTX e_ctx;
1268 int encrypted_len, final_len;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001269 int rc = 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001270
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001271 /* Turn the password into an intermediate key and IV that can decrypt the master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001272 get_device_scrypt_params(crypt_ftr);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001273
1274 switch (crypt_ftr->kdf_type) {
Shawn Willdene17a9c42014-09-08 13:04:08 -06001275 case KDF_SCRYPT_KEYMASTER_UNPADDED:
1276 case KDF_SCRYPT_KEYMASTER_BADLY_PADDED:
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001277 case KDF_SCRYPT_KEYMASTER:
1278 if (keymaster_create_key(crypt_ftr)) {
1279 SLOGE("keymaster_create_key failed");
1280 return -1;
1281 }
1282
1283 if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) {
1284 SLOGE("scrypt failed");
1285 return -1;
1286 }
1287 break;
1288
1289 case KDF_SCRYPT:
1290 if (scrypt(passwd, salt, ikey, crypt_ftr)) {
1291 SLOGE("scrypt failed");
1292 return -1;
1293 }
1294 break;
1295
1296 default:
1297 SLOGE("Invalid kdf_type");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001298 return -1;
1299 }
Kenny Rootc4c70f12013-06-14 12:11:38 -07001300
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001301 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001302 EVP_CIPHER_CTX_init(&e_ctx);
1303 if (! EVP_EncryptInit_ex(&e_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001304 SLOGE("EVP_EncryptInit failed\n");
1305 return -1;
1306 }
1307 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001308
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001309 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001310 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
1311 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001312 SLOGE("EVP_EncryptUpdate failed\n");
1313 return -1;
1314 }
Adam Langley889c4f12014-09-03 14:23:13 -07001315 if (! EVP_EncryptFinal_ex(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001316 SLOGE("EVP_EncryptFinal failed\n");
1317 return -1;
1318 }
1319
1320 if (encrypted_len + final_len != KEY_LEN_BYTES) {
1321 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
1322 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001323 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001324
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001325 /* Store the scrypt of the intermediate key, so we can validate if it's a
1326 password error or mount error when things go wrong.
1327 Note there's no need to check for errors, since if this is incorrect, we
1328 simply won't wipe userdata, which is the correct default behavior
1329 */
1330 int N = 1 << crypt_ftr->N_factor;
1331 int r = 1 << crypt_ftr->r_factor;
1332 int p = 1 << crypt_ftr->p_factor;
1333
1334 rc = crypto_scrypt(ikey, KEY_LEN_BYTES,
1335 crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p,
1336 crypt_ftr->scrypted_intermediate_key,
1337 sizeof(crypt_ftr->scrypted_intermediate_key));
1338
1339 if (rc) {
1340 SLOGE("encrypt_master_key: crypto_scrypt failed");
1341 }
1342
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001343 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001344}
1345
JP Abgrall7bdfa522013-11-15 13:42:56 -08001346static int decrypt_master_key_aux(char *passwd, unsigned char *salt,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001347 unsigned char *encrypted_master_key,
1348 unsigned char *decrypted_master_key,
1349 kdf_func kdf, void *kdf_params,
1350 unsigned char** intermediate_key,
1351 size_t* intermediate_key_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001352{
1353 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 -08001354 EVP_CIPHER_CTX d_ctx;
1355 int decrypted_len, final_len;
1356
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001357 /* Turn the password into an intermediate key and IV that can decrypt the
1358 master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001359 if (kdf(passwd, salt, ikey, kdf_params)) {
1360 SLOGE("kdf failed");
1361 return -1;
1362 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001363
1364 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001365 EVP_CIPHER_CTX_init(&d_ctx);
1366 if (! EVP_DecryptInit_ex(&d_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001367 return -1;
1368 }
1369 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1370 /* Decrypt the master key */
1371 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
1372 encrypted_master_key, KEY_LEN_BYTES)) {
1373 return -1;
1374 }
Adam Langley889c4f12014-09-03 14:23:13 -07001375 if (! EVP_DecryptFinal_ex(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001376 return -1;
1377 }
1378
1379 if (decrypted_len + final_len != KEY_LEN_BYTES) {
1380 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001381 }
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001382
1383 /* Copy intermediate key if needed by params */
1384 if (intermediate_key && intermediate_key_size) {
1385 *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES);
1386 if (intermediate_key) {
1387 memcpy(*intermediate_key, ikey, KEY_LEN_BYTES);
1388 *intermediate_key_size = KEY_LEN_BYTES;
1389 }
1390 }
1391
1392 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001393}
1394
Kenny Rootc4c70f12013-06-14 12:11:38 -07001395static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001396{
Shawn Willdene17a9c42014-09-08 13:04:08 -06001397 if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER_UNPADDED ||
1398 ftr->kdf_type == KDF_SCRYPT_KEYMASTER_BADLY_PADDED ||
1399 ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001400 *kdf = scrypt_keymaster;
1401 *kdf_params = ftr;
1402 } else if (ftr->kdf_type == KDF_SCRYPT) {
Kenny Rootc4c70f12013-06-14 12:11:38 -07001403 *kdf = scrypt;
1404 *kdf_params = ftr;
1405 } else {
1406 *kdf = pbkdf2;
1407 *kdf_params = NULL;
1408 }
1409}
1410
JP Abgrall7bdfa522013-11-15 13:42:56 -08001411static int decrypt_master_key(char *passwd, unsigned char *decrypted_master_key,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001412 struct crypt_mnt_ftr *crypt_ftr,
1413 unsigned char** intermediate_key,
1414 size_t* intermediate_key_size)
Kenny Rootc4c70f12013-06-14 12:11:38 -07001415{
1416 kdf_func kdf;
1417 void *kdf_params;
1418 int ret;
1419
1420 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001421 ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key,
1422 decrypted_master_key, kdf, kdf_params,
1423 intermediate_key, intermediate_key_size);
Kenny Rootc4c70f12013-06-14 12:11:38 -07001424 if (ret != 0) {
1425 SLOGW("failure decrypting master key");
Kenny Rootc4c70f12013-06-14 12:11:38 -07001426 }
1427
1428 return ret;
1429}
1430
1431static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt,
1432 struct crypt_mnt_ftr *crypt_ftr) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001433 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -08001434 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001435
1436 /* Get some random bits for a key */
1437 fd = open("/dev/urandom", O_RDONLY);
Ken Sumralle8744072011-01-18 22:01:55 -08001438 read(fd, key_buf, sizeof(key_buf));
1439 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001440 close(fd);
1441
1442 /* Now encrypt it with the password */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001443 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001444}
1445
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001446static int wait_and_unmount(char *mountpoint, bool kill)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001447{
Greg Hackmann955653e2014-09-24 14:55:20 -07001448 int i, err, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001449#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001450
1451 /* Now umount the tmpfs filesystem */
1452 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001453 if (umount(mountpoint) == 0) {
1454 break;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001455 }
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001456
1457 if (errno == EINVAL) {
1458 /* EINVAL is returned if the directory is not a mountpoint,
1459 * i.e. there is no filesystem mounted there. So just get out.
1460 */
1461 break;
1462 }
1463
1464 err = errno;
1465
1466 /* If allowed, be increasingly aggressive before the last two retries */
1467 if (kill) {
1468 if (i == (WAIT_UNMOUNT_COUNT - 3)) {
1469 SLOGW("sending SIGHUP to processes with open files\n");
1470 vold_killProcessesWithOpenFiles(mountpoint, 1);
1471 } else if (i == (WAIT_UNMOUNT_COUNT - 2)) {
1472 SLOGW("sending SIGKILL to processes with open files\n");
1473 vold_killProcessesWithOpenFiles(mountpoint, 2);
1474 }
1475 }
1476
1477 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001478 }
1479
1480 if (i < WAIT_UNMOUNT_COUNT) {
1481 SLOGD("unmounting %s succeeded\n", mountpoint);
1482 rc = 0;
1483 } else {
jessica_yu3f14fe42014-09-22 15:57:40 +08001484 vold_killProcessesWithOpenFiles(mountpoint, 0);
Greg Hackmann955653e2014-09-24 14:55:20 -07001485 SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001486 rc = -1;
1487 }
1488
1489 return rc;
1490}
1491
Ken Sumrallc5872692013-05-14 15:26:31 -07001492#define DATA_PREP_TIMEOUT 200
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001493static int prep_data_fs(void)
1494{
1495 int i;
1496
1497 /* Do the prep of the /data filesystem */
1498 property_set("vold.post_fs_data_done", "0");
1499 property_set("vold.decrypt", "trigger_post_fs_data");
1500 SLOGD("Just triggered post_fs_data\n");
1501
Ken Sumrallc5872692013-05-14 15:26:31 -07001502 /* Wait a max of 50 seconds, hopefully it takes much less */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001503 for (i=0; i<DATA_PREP_TIMEOUT; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001504 char p[PROPERTY_VALUE_MAX];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001505
1506 property_get("vold.post_fs_data_done", p, "0");
1507 if (*p == '1') {
1508 break;
1509 } else {
1510 usleep(250000);
1511 }
1512 }
1513 if (i == DATA_PREP_TIMEOUT) {
1514 /* Ugh, we failed to prep /data in time. Bail. */
Ken Sumrallc5872692013-05-14 15:26:31 -07001515 SLOGE("post_fs_data timed out!\n");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001516 return -1;
1517 } else {
1518 SLOGD("post_fs_data done\n");
1519 return 0;
1520 }
1521}
1522
Paul Lawrence74f29f12014-08-28 15:54:10 -07001523static void cryptfs_set_corrupt()
1524{
1525 // Mark the footer as bad
1526 struct crypt_mnt_ftr crypt_ftr;
1527 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1528 SLOGE("Failed to get crypto footer - panic");
1529 return;
1530 }
1531
1532 crypt_ftr.flags |= CRYPT_DATA_CORRUPT;
1533 if (put_crypt_ftr_and_key(&crypt_ftr)) {
1534 SLOGE("Failed to set crypto footer - panic");
1535 return;
1536 }
1537}
1538
1539static void cryptfs_trigger_restart_min_framework()
1540{
1541 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
1542 SLOGE("Failed to mount tmpfs on data - panic");
1543 return;
1544 }
1545
1546 if (property_set("vold.decrypt", "trigger_post_fs_data")) {
1547 SLOGE("Failed to trigger post fs data - panic");
1548 return;
1549 }
1550
1551 if (property_set("vold.decrypt", "trigger_restart_min_framework")) {
1552 SLOGE("Failed to trigger restart min framework - panic");
1553 return;
1554 }
1555}
1556
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001557/* returns < 0 on failure */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001558static int cryptfs_restart_internal(int restart_main)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001559{
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001560 char crypto_blkdev[MAXPATHLEN];
Tim Murray8439dc92014-12-15 11:56:11 -08001561 int rc = -1;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001562 static int restart_successful = 0;
1563
1564 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -06001565 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001566 SLOGE("Encrypted filesystem not validated, aborting");
1567 return -1;
1568 }
1569
1570 if (restart_successful) {
1571 SLOGE("System already restarted with encrypted disk, aborting");
1572 return -1;
1573 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001574
Paul Lawrencef4faa572014-01-29 13:31:03 -08001575 if (restart_main) {
1576 /* Here is where we shut down the framework. The init scripts
1577 * start all services in one of three classes: core, main or late_start.
1578 * On boot, we start core and main. Now, we stop main, but not core,
1579 * as core includes vold and a few other really important things that
1580 * we need to keep running. Once main has stopped, we should be able
1581 * to umount the tmpfs /data, then mount the encrypted /data.
1582 * We then restart the class main, and also the class late_start.
1583 * At the moment, I've only put a few things in late_start that I know
1584 * are not needed to bring up the framework, and that also cause problems
1585 * with unmounting the tmpfs /data, but I hope to add add more services
1586 * to the late_start class as we optimize this to decrease the delay
1587 * till the user is asked for the password to the filesystem.
1588 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001589
Paul Lawrencef4faa572014-01-29 13:31:03 -08001590 /* The init files are setup to stop the class main when vold.decrypt is
1591 * set to trigger_reset_main.
1592 */
1593 property_set("vold.decrypt", "trigger_reset_main");
1594 SLOGD("Just asked init to shut down class main\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001595
Paul Lawrencef4faa572014-01-29 13:31:03 -08001596 /* Ugh, shutting down the framework is not synchronous, so until it
1597 * can be fixed, this horrible hack will wait a moment for it all to
1598 * shut down before proceeding. Without it, some devices cannot
1599 * restart the graphics services.
1600 */
1601 sleep(2);
1602 }
Ken Sumrall9dedfd42012-10-09 14:16:59 -07001603
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001604 /* Now that the framework is shutdown, we should be able to umount()
1605 * the tmpfs filesystem, and mount the real one.
1606 */
1607
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001608 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1609 if (strlen(crypto_blkdev) == 0) {
1610 SLOGE("fs_crypto_blkdev not set\n");
1611 return -1;
1612 }
1613
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001614 if (! (rc = wait_and_unmount(DATA_MNT_POINT, true)) ) {
Doug Zongker6fd57712013-12-17 09:43:23 -08001615 /* If ro.crypto.readonly is set to 1, mount the decrypted
1616 * filesystem readonly. This is used when /data is mounted by
1617 * recovery mode.
1618 */
1619 char ro_prop[PROPERTY_VALUE_MAX];
1620 property_get("ro.crypto.readonly", ro_prop, "");
1621 if (strlen(ro_prop) > 0 && atoi(ro_prop)) {
1622 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
1623 rec->flags |= MS_RDONLY;
1624 }
JP Abgrall62c7af32014-06-16 13:01:23 -07001625
Ken Sumralle5032c42012-04-01 23:58:44 -07001626 /* If that succeeded, then mount the decrypted filesystem */
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001627 int retries = RETRY_MOUNT_ATTEMPTS;
1628 int mount_rc;
1629 while ((mount_rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT,
1630 crypto_blkdev, 0))
1631 != 0) {
1632 if (mount_rc == FS_MGR_DOMNT_BUSY) {
1633 /* TODO: invoke something similar to
1634 Process::killProcessWithOpenFiles(DATA_MNT_POINT,
1635 retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */
1636 SLOGI("Failed to mount %s because it is busy - waiting",
1637 crypto_blkdev);
1638 if (--retries) {
1639 sleep(RETRY_MOUNT_DELAY_SECONDS);
1640 } else {
1641 /* Let's hope that a reboot clears away whatever is keeping
1642 the mount busy */
1643 cryptfs_reboot(reboot);
1644 }
1645 } else {
1646 SLOGE("Failed to mount decrypted data");
1647 cryptfs_set_corrupt();
1648 cryptfs_trigger_restart_min_framework();
1649 SLOGI("Started framework to offer wipe");
1650 return -1;
1651 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001652 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001653
Ken Sumralle5032c42012-04-01 23:58:44 -07001654 property_set("vold.decrypt", "trigger_load_persist_props");
1655 /* Create necessary paths on /data */
1656 if (prep_data_fs()) {
1657 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001658 }
Ken Sumralle5032c42012-04-01 23:58:44 -07001659
1660 /* startup service classes main and late_start */
1661 property_set("vold.decrypt", "trigger_restart_framework");
1662 SLOGD("Just triggered restart_framework\n");
1663
1664 /* Give it a few moments to get started */
1665 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001666 }
1667
Ken Sumrall0cc16632011-01-18 20:32:26 -08001668 if (rc == 0) {
1669 restart_successful = 1;
1670 }
1671
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001672 return rc;
1673}
1674
Paul Lawrencef4faa572014-01-29 13:31:03 -08001675int cryptfs_restart(void)
1676{
1677 /* Call internal implementation forcing a restart of main service group */
1678 return cryptfs_restart_internal(1);
1679}
1680
Mark Salyzyn3e971272014-01-21 13:27:04 -08001681static int do_crypto_complete(char *mount_point UNUSED)
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001682{
1683 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001684 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -08001685 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001686
1687 property_get("ro.crypto.state", encrypted_state, "");
1688 if (strcmp(encrypted_state, "encrypted") ) {
1689 SLOGE("not running with encryption, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001690 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001691 }
1692
Ken Sumrall160b4d62013-04-22 12:15:39 -07001693 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001694 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumralle5032c42012-04-01 23:58:44 -07001695
Ken Sumralle1a45852011-12-14 21:24:27 -08001696 /*
1697 * Only report this error if key_loc is a file and it exists.
1698 * If the device was never encrypted, and /data is not mountable for
1699 * some reason, returning 1 should prevent the UI from presenting the
1700 * a "enter password" screen, or worse, a "press button to wipe the
1701 * device" screen.
1702 */
1703 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
1704 SLOGE("master key file does not exist, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001705 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumralle1a45852011-12-14 21:24:27 -08001706 } else {
1707 SLOGE("Error getting crypt footer and key\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001708 return CRYPTO_COMPLETE_BAD_METADATA;
Ken Sumralle1a45852011-12-14 21:24:27 -08001709 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001710 }
1711
Paul Lawrence74f29f12014-08-28 15:54:10 -07001712 // Test for possible error flags
1713 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS){
1714 SLOGE("Encryption process is partway completed\n");
1715 return CRYPTO_COMPLETE_PARTIAL;
1716 }
1717
1718 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE){
1719 SLOGE("Encryption process was interrupted but cannot continue\n");
1720 return CRYPTO_COMPLETE_INCONSISTENT;
1721 }
1722
1723 if (crypt_ftr.flags & CRYPT_DATA_CORRUPT){
1724 SLOGE("Encryption is successful but data is corrupt\n");
1725 return CRYPTO_COMPLETE_CORRUPT;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001726 }
1727
1728 /* We passed the test! We shall diminish, and return to the west */
Paul Lawrence74f29f12014-08-28 15:54:10 -07001729 return CRYPTO_COMPLETE_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001730}
1731
Paul Lawrencef4faa572014-01-29 13:31:03 -08001732static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
1733 char *passwd, char *mount_point, char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001734{
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001735 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001736 unsigned char decrypted_master_key[32];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001737 char crypto_blkdev[MAXPATHLEN];
1738 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001739 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001740 unsigned int orig_failed_decrypt_count;
1741 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001742 int use_keymaster = 0;
1743 int upgrade = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001744 unsigned char* intermediate_key = 0;
1745 size_t intermediate_key_size = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001746
Paul Lawrencef4faa572014-01-29 13:31:03 -08001747 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1748 orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001749
Paul Lawrencef4faa572014-01-29 13:31:03 -08001750 if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001751 if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr,
1752 &intermediate_key, &intermediate_key_size)) {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001753 SLOGE("Failed to decrypt master key\n");
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001754 rc = -1;
1755 goto errout;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001756 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001757 }
1758
Paul Lawrencef4faa572014-01-29 13:31:03 -08001759 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
1760
Ajay Dudani87701e22014-09-17 21:02:52 -07001761#ifdef CONFIG_HW_DISK_ENCRYPTION
Iliyan Malchevbb7d9af2014-11-20 18:42:23 -08001762 if (!strcmp((char *)crypt_ftr->crypto_type_name, "aes-xts")) {
1763 if(!set_hw_device_encryption_key(passwd, (char*) crypt_ftr->crypto_type_name)) {
1764 SLOGE("Hardware encryption key does not match");
1765 }
Ajay Dudani87701e22014-09-17 21:02:52 -07001766 }
1767#endif
1768
Paul Lawrence74f29f12014-08-28 15:54:10 -07001769 // Create crypto block device - all (non fatal) code paths
1770 // need it
Paul Lawrencef4faa572014-01-29 13:31:03 -08001771 if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key,
1772 real_blkdev, crypto_blkdev, label)) {
Paul Lawrence74f29f12014-08-28 15:54:10 -07001773 SLOGE("Error creating decrypted block device\n");
1774 rc = -1;
1775 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001776 }
1777
Paul Lawrence74f29f12014-08-28 15:54:10 -07001778 /* Work out if the problem is the password or the data */
1779 unsigned char scrypted_intermediate_key[sizeof(crypt_ftr->
1780 scrypted_intermediate_key)];
1781 int N = 1 << crypt_ftr->N_factor;
1782 int r = 1 << crypt_ftr->r_factor;
1783 int p = 1 << crypt_ftr->p_factor;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001784
Paul Lawrence74f29f12014-08-28 15:54:10 -07001785 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
1786 crypt_ftr->salt, sizeof(crypt_ftr->salt),
1787 N, r, p, scrypted_intermediate_key,
1788 sizeof(scrypted_intermediate_key));
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001789
Paul Lawrence74f29f12014-08-28 15:54:10 -07001790 // Does the key match the crypto footer?
1791 if (rc == 0 && memcmp(scrypted_intermediate_key,
1792 crypt_ftr->scrypted_intermediate_key,
1793 sizeof(scrypted_intermediate_key)) == 0) {
1794 SLOGI("Password matches");
1795 rc = 0;
1796 } else {
1797 /* Try mounting the file system anyway, just in case the problem's with
1798 * the footer, not the key. */
1799 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
1800 mkdir(tmp_mount_point, 0755);
1801 if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
1802 SLOGE("Error temp mounting decrypted block device\n");
1803 delete_crypto_blk_dev(label);
1804
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001805 rc = ++crypt_ftr->failed_decrypt_count;
1806 put_crypt_ftr_and_key(crypt_ftr);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001807 } else {
1808 /* Success! */
1809 SLOGI("Password did not match but decrypted drive mounted - continue");
1810 umount(tmp_mount_point);
1811 rc = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001812 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001813 }
1814
1815 if (rc == 0) {
1816 crypt_ftr->failed_decrypt_count = 0;
Paul Lawrence72b8b822014-10-05 12:57:37 -07001817 if (orig_failed_decrypt_count != 0) {
1818 put_crypt_ftr_and_key(crypt_ftr);
1819 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001820
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001821 /* Save the name of the crypto block device
Paul Lawrence74f29f12014-08-28 15:54:10 -07001822 * so we can mount it when restarting the framework. */
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001823 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -06001824
1825 /* Also save a the master key so we can reencrypted the key
Paul Lawrence74f29f12014-08-28 15:54:10 -07001826 * the key when we want to change the password on it. */
Jason parks70a4b3f2011-01-28 10:10:47 -06001827 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001828 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -06001829 master_key_saved = 1;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001830 SLOGD("%s(): Master key saved\n", __FUNCTION__);
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001831 rc = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001832
Paul Lawrence74f29f12014-08-28 15:54:10 -07001833 // Upgrade if we're not using the latest KDF.
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001834 use_keymaster = keymaster_check_compatibility();
1835 if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Shawn Willden47ba10d2014-09-03 17:07:06 -06001836 // Don't allow downgrade
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001837 } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) {
1838 crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1839 upgrade = 1;
1840 } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001841 crypt_ftr->kdf_type = KDF_SCRYPT;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001842 upgrade = 1;
1843 }
1844
1845 if (upgrade) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001846 rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1847 crypt_ftr->master_key, crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001848 if (!rc) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001849 rc = put_crypt_ftr_and_key(crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001850 }
1851 SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
Paul Lawrenceb2f682b2014-09-08 11:28:19 -07001852
1853 // Do not fail even if upgrade failed - machine is bootable
1854 // Note that if this code is ever hit, there is a *serious* problem
1855 // since KDFs should never fail. You *must* fix the kdf before
1856 // proceeding!
1857 if (rc) {
1858 SLOGW("Upgrade failed with error %d,"
1859 " but continuing with previous state",
1860 rc);
1861 rc = 0;
1862 }
JP Abgrall7bdfa522013-11-15 13:42:56 -08001863 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001864 }
1865
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001866 errout:
1867 if (intermediate_key) {
1868 memset(intermediate_key, 0, intermediate_key_size);
1869 free(intermediate_key);
1870 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001871 return rc;
1872}
1873
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001874/* Called by vold when it wants to undo the crypto mapping of a volume it
1875 * manages. This is usually in response to a factory reset, when we want
1876 * to undo the crypto mapping so the volume is formatted in the clear.
1877 */
1878int cryptfs_revert_volume(const char *label)
1879{
1880 return delete_crypto_blk_dev((char *)label);
1881}
1882
Ken Sumrall29d8da82011-05-18 17:20:07 -07001883/*
1884 * Called by vold when it's asked to mount an encrypted, nonremovable volume.
1885 * Setup a dm-crypt mapping, use the saved master key from
1886 * setting up the /data mapping, and return the new device path.
1887 */
1888int cryptfs_setup_volume(const char *label, int major, int minor,
1889 char *crypto_sys_path, unsigned int max_path,
1890 int *new_major, int *new_minor)
1891{
1892 char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN];
1893 struct crypt_mnt_ftr sd_crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001894 struct stat statbuf;
Tim Murray8439dc92014-12-15 11:56:11 -08001895 unsigned int nr_sec;
1896 int fd;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001897
1898 sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor);
1899
Ken Sumrall160b4d62013-04-22 12:15:39 -07001900 get_crypt_ftr_and_key(&sd_crypt_ftr);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001901
1902 /* Update the fs_size field to be the size of the volume */
1903 fd = open(real_blkdev, O_RDONLY);
1904 nr_sec = get_blkdev_size(fd);
1905 close(fd);
1906 if (nr_sec == 0) {
1907 SLOGE("Cannot get size of volume %s\n", real_blkdev);
1908 return -1;
1909 }
1910
1911 sd_crypt_ftr.fs_size = nr_sec;
1912 create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev,
1913 crypto_blkdev, label);
1914
JP Abgrall3334c6a2014-10-10 15:52:11 -07001915 if (stat(crypto_blkdev, &statbuf) < 0) {
1916 SLOGE("Error get stat for crypto_blkdev %s. err=%d(%s)\n",
1917 crypto_blkdev, errno, strerror(errno));
1918 }
Ken Sumrall29d8da82011-05-18 17:20:07 -07001919 *new_major = MAJOR(statbuf.st_rdev);
1920 *new_minor = MINOR(statbuf.st_rdev);
1921
1922 /* Create path to sys entry for this block device */
1923 snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1);
1924
1925 return 0;
1926}
1927
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001928int cryptfs_crypto_complete(void)
1929{
1930 return do_crypto_complete("/data");
1931}
1932
Paul Lawrencef4faa572014-01-29 13:31:03 -08001933int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr)
1934{
1935 char encrypted_state[PROPERTY_VALUE_MAX];
1936 property_get("ro.crypto.state", encrypted_state, "");
1937 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
1938 SLOGE("encrypted fs already validated or not running with encryption,"
1939 " aborting");
1940 return -1;
1941 }
1942
1943 if (get_crypt_ftr_and_key(crypt_ftr)) {
1944 SLOGE("Error getting crypt footer and key");
1945 return -1;
1946 }
1947
1948 return 0;
1949}
1950
Paul Lawrencefc615042014-10-04 15:32:29 -07001951/*
1952 * TODO - transition patterns to new format in calling code
1953 * and remove this vile hack, and the use of hex in
1954 * the password passing code.
1955 *
1956 * Patterns are passed in zero based (i.e. the top left dot
1957 * is represented by zero, the top middle one etc), but we want
1958 * to store them '1' based.
1959 * This is to allow us to migrate the calling code to use this
1960 * convention. It also solves a nasty problem whereby scrypt ignores
1961 * trailing zeros, so patterns ending at the top left could be
1962 * truncated, and similarly, you could add the top left to any
1963 * pattern and still match.
1964 * adjust_passwd is a hack function that returns the alternate representation
1965 * if the password appears to be a pattern (hex numbers all less than 09)
1966 * If it succeeds we need to try both, and in particular try the alternate
1967 * first. If the original matches, then we need to update the footer
1968 * with the alternate.
1969 * All code that accepts passwords must adjust them first. Since
1970 * cryptfs_check_passwd is always the first function called after a migration
1971 * (and indeed on any boot) we only need to do the double try in this
1972 * function.
1973 */
1974char* adjust_passwd(const char* passwd)
1975{
1976 size_t index, length;
1977
1978 if (!passwd) {
1979 return 0;
1980 }
1981
1982 // Check even length. Hex encoded passwords are always
1983 // an even length, since each character encodes to two characters.
1984 length = strlen(passwd);
1985 if (length % 2) {
1986 SLOGW("Password not correctly hex encoded.");
1987 return 0;
1988 }
1989
1990 // Check password is old-style pattern - a collection of hex
1991 // encoded bytes less than 9 (00 through 08)
1992 for (index = 0; index < length; index +=2) {
1993 if (passwd[index] != '0'
1994 || passwd[index + 1] < '0' || passwd[index + 1] > '8') {
1995 return 0;
1996 }
1997 }
1998
1999 // Allocate room for adjusted passwd and null terminate
2000 char* adjusted = malloc(length + 1);
2001 adjusted[length] = 0;
2002
2003 // Add 0x31 ('1') to each character
2004 for (index = 0; index < length; index += 2) {
2005 // output is 31 through 39 so set first byte to three, second to src + 1
2006 adjusted[index] = '3';
2007 adjusted[index + 1] = passwd[index + 1] + 1;
2008 }
2009
2010 return adjusted;
2011}
2012
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002013int cryptfs_check_passwd(char *passwd)
2014{
Paul Lawrencef4faa572014-01-29 13:31:03 -08002015 struct crypt_mnt_ftr crypt_ftr;
2016 int rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002017
Paul Lawrencef4faa572014-01-29 13:31:03 -08002018 rc = check_unmounted_and_get_ftr(&crypt_ftr);
2019 if (rc)
2020 return rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002021
Paul Lawrencefc615042014-10-04 15:32:29 -07002022 char* adjusted_passwd = adjust_passwd(passwd);
2023 if (adjusted_passwd) {
2024 int failed_decrypt_count = crypt_ftr.failed_decrypt_count;
2025 rc = test_mount_encrypted_fs(&crypt_ftr, adjusted_passwd,
2026 DATA_MNT_POINT, "userdata");
2027
2028 // Maybe the original one still works?
2029 if (rc) {
2030 // Don't double count this failure
2031 crypt_ftr.failed_decrypt_count = failed_decrypt_count;
2032 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
2033 DATA_MNT_POINT, "userdata");
2034 if (!rc) {
2035 // cryptfs_changepw also adjusts so pass original
2036 // Note that adjust_passwd only recognises patterns
2037 // so we can safely use CRYPT_TYPE_PATTERN
2038 SLOGI("Updating pattern to new format");
2039 cryptfs_changepw(CRYPT_TYPE_PATTERN, passwd);
2040 }
2041 }
2042 free(adjusted_passwd);
2043 } else {
2044 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
2045 DATA_MNT_POINT, "userdata");
2046 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002047
2048 if (rc == 0 && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
Paul Lawrence399317e2014-03-10 13:20:50 -07002049 cryptfs_clear_password();
2050 password = strdup(passwd);
2051 struct timespec now;
2052 clock_gettime(CLOCK_BOOTTIME, &now);
2053 password_expiry_time = now.tv_sec + password_max_age_seconds;
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002054 }
2055
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002056 return rc;
2057}
2058
Ken Sumrall3ad90722011-10-04 20:38:29 -07002059int cryptfs_verify_passwd(char *passwd)
2060{
2061 struct crypt_mnt_ftr crypt_ftr;
2062 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002063 unsigned char decrypted_master_key[32];
Ken Sumrall3ad90722011-10-04 20:38:29 -07002064 char encrypted_state[PROPERTY_VALUE_MAX];
2065 int rc;
2066
2067 property_get("ro.crypto.state", encrypted_state, "");
2068 if (strcmp(encrypted_state, "encrypted") ) {
2069 SLOGE("device not encrypted, aborting");
2070 return -2;
2071 }
2072
2073 if (!master_key_saved) {
2074 SLOGE("encrypted fs not yet mounted, aborting");
2075 return -1;
2076 }
2077
2078 if (!saved_mount_point) {
2079 SLOGE("encrypted fs failed to save mount point, aborting");
2080 return -1;
2081 }
2082
Ken Sumrall160b4d62013-04-22 12:15:39 -07002083 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall3ad90722011-10-04 20:38:29 -07002084 SLOGE("Error getting crypt footer and key\n");
2085 return -1;
2086 }
2087
2088 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
2089 /* If the device has no password, then just say the password is valid */
2090 rc = 0;
2091 } else {
Paul Lawrencefc615042014-10-04 15:32:29 -07002092 char* adjusted_passwd = adjust_passwd(passwd);
2093 if (adjusted_passwd) {
2094 passwd = adjusted_passwd;
2095 }
2096
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002097 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall3ad90722011-10-04 20:38:29 -07002098 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
2099 /* They match, the password is correct */
2100 rc = 0;
2101 } else {
2102 /* If incorrect, sleep for a bit to prevent dictionary attacks */
2103 sleep(1);
2104 rc = 1;
2105 }
Paul Lawrencefc615042014-10-04 15:32:29 -07002106
2107 free(adjusted_passwd);
Ken Sumrall3ad90722011-10-04 20:38:29 -07002108 }
2109
2110 return rc;
2111}
2112
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002113/* Initialize a crypt_mnt_ftr structure. The keysize is
2114 * defaulted to 16 bytes, and the filesystem size to 0.
2115 * Presumably, at a minimum, the caller will update the
2116 * filesystem size and crypto_type_name after calling this function.
2117 */
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002118static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002119{
Ken Sumrall160b4d62013-04-22 12:15:39 -07002120 off64_t off;
2121
2122 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002123 ftr->magic = CRYPT_MNT_MAGIC;
Kenny Rootc96a5f82013-06-14 12:08:28 -07002124 ftr->major_version = CURRENT_MAJOR_VERSION;
2125 ftr->minor_version = CURRENT_MINOR_VERSION;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002126 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Jason parks70a4b3f2011-01-28 10:10:47 -06002127 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002128
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002129 switch (keymaster_check_compatibility()) {
2130 case 1:
2131 ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
2132 break;
2133
2134 case 0:
2135 ftr->kdf_type = KDF_SCRYPT;
2136 break;
2137
2138 default:
2139 SLOGE("keymaster_check_compatibility failed");
2140 return -1;
2141 }
2142
Kenny Rootc4c70f12013-06-14 12:11:38 -07002143 get_device_scrypt_params(ftr);
2144
Ken Sumrall160b4d62013-04-22 12:15:39 -07002145 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
2146 if (get_crypt_ftr_info(NULL, &off) == 0) {
2147 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
2148 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
2149 ftr->persist_data_size;
2150 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002151
2152 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002153}
2154
Ken Sumrall29d8da82011-05-18 17:20:07 -07002155static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002156{
Ken Sumralle550f782013-08-20 13:48:23 -07002157 const char *args[10];
2158 char size_str[32]; /* Must be large enough to hold a %lld and null byte */
2159 int num_args;
2160 int status;
2161 int tmp;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002162 int rc = -1;
2163
Ken Sumrall29d8da82011-05-18 17:20:07 -07002164 if (type == EXT4_FS) {
Ken Sumralle550f782013-08-20 13:48:23 -07002165 args[0] = "/system/bin/make_ext4fs";
2166 args[1] = "-a";
2167 args[2] = "/data";
2168 args[3] = "-l";
Elliott Hughes73737162014-06-25 17:27:42 -07002169 snprintf(size_str, sizeof(size_str), "%" PRId64, size * 512);
Ken Sumralle550f782013-08-20 13:48:23 -07002170 args[4] = size_str;
2171 args[5] = crypto_blkdev;
2172 num_args = 6;
2173 SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n",
2174 args[0], args[1], args[2], args[3], args[4], args[5]);
JP Abgrall62c7af32014-06-16 13:01:23 -07002175 } else if (type == F2FS_FS) {
2176 args[0] = "/system/bin/mkfs.f2fs";
2177 args[1] = "-t";
2178 args[2] = "-d1";
2179 args[3] = crypto_blkdev;
Elliott Hughes73737162014-06-25 17:27:42 -07002180 snprintf(size_str, sizeof(size_str), "%" PRId64, size);
JP Abgrall62c7af32014-06-16 13:01:23 -07002181 args[4] = size_str;
2182 num_args = 5;
2183 SLOGI("Making empty filesystem with command %s %s %s %s %s\n",
2184 args[0], args[1], args[2], args[3], args[4]);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002185 } else {
2186 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
2187 return -1;
2188 }
2189
Ken Sumralle550f782013-08-20 13:48:23 -07002190 tmp = android_fork_execvp(num_args, (char **)args, &status, false, true);
2191
2192 if (tmp != 0) {
2193 SLOGE("Error creating empty filesystem on %s due to logwrap error\n", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002194 } else {
Ken Sumralle550f782013-08-20 13:48:23 -07002195 if (WIFEXITED(status)) {
2196 if (WEXITSTATUS(status)) {
2197 SLOGE("Error creating filesystem on %s, exit status %d ",
2198 crypto_blkdev, WEXITSTATUS(status));
2199 } else {
2200 SLOGD("Successfully created filesystem on %s\n", crypto_blkdev);
2201 rc = 0;
2202 }
2203 } else {
2204 SLOGE("Error creating filesystem on %s, did not exit normally\n", crypto_blkdev);
2205 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002206 }
2207
2208 return rc;
2209}
2210
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002211#define CRYPT_INPLACE_BUFSIZE 4096
Paul Lawrence87999172014-02-20 12:21:31 -08002212#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / CRYPT_SECTOR_SIZE)
2213#define CRYPT_SECTOR_SIZE 512
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002214
2215/* aligned 32K writes tends to make flash happy.
2216 * SD card association recommends it.
2217 */
Ajay Dudani87701e22014-09-17 21:02:52 -07002218#ifndef CONFIG_HW_DISK_ENCRYPTION
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002219#define BLOCKS_AT_A_TIME 8
Ajay Dudani87701e22014-09-17 21:02:52 -07002220#else
2221#define BLOCKS_AT_A_TIME 1024
2222#endif
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002223
2224struct encryptGroupsData
2225{
2226 int realfd;
2227 int cryptofd;
2228 off64_t numblocks;
2229 off64_t one_pct, cur_pct, new_pct;
2230 off64_t blocks_already_done, tot_numblocks;
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002231 off64_t used_blocks_already_done, tot_used_blocks;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002232 char* real_blkdev, * crypto_blkdev;
2233 int count;
2234 off64_t offset;
2235 char* buffer;
Paul Lawrence87999172014-02-20 12:21:31 -08002236 off64_t last_written_sector;
2237 int completed;
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002238 time_t time_started;
2239 int remaining_time;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002240};
2241
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002242static void update_progress(struct encryptGroupsData* data, int is_used)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002243{
2244 data->blocks_already_done++;
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002245
2246 if (is_used) {
2247 data->used_blocks_already_done++;
2248 }
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002249 if (data->tot_used_blocks) {
2250 data->new_pct = data->used_blocks_already_done / data->one_pct;
2251 } else {
2252 data->new_pct = data->blocks_already_done / data->one_pct;
2253 }
2254
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002255 if (data->new_pct > data->cur_pct) {
2256 char buf[8];
2257 data->cur_pct = data->new_pct;
Elliott Hughescb33f572014-06-25 18:25:11 -07002258 snprintf(buf, sizeof(buf), "%" PRId64, data->cur_pct);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002259 property_set("vold.encrypt_progress", buf);
2260 }
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002261
2262 if (data->cur_pct >= 5) {
Paul Lawrence9c58a872014-09-30 09:12:51 -07002263 struct timespec time_now;
2264 if (clock_gettime(CLOCK_MONOTONIC, &time_now)) {
2265 SLOGW("Error getting time");
2266 } else {
2267 double elapsed_time = difftime(time_now.tv_sec, data->time_started);
2268 off64_t remaining_blocks = data->tot_used_blocks
2269 - data->used_blocks_already_done;
2270 int remaining_time = (int)(elapsed_time * remaining_blocks
2271 / data->used_blocks_already_done);
Paul Lawrence71577502014-08-13 14:55:55 -07002272
Paul Lawrence9c58a872014-09-30 09:12:51 -07002273 // Change time only if not yet set, lower, or a lot higher for
2274 // best user experience
2275 if (data->remaining_time == -1
2276 || remaining_time < data->remaining_time
2277 || remaining_time > data->remaining_time + 60) {
2278 char buf[8];
2279 snprintf(buf, sizeof(buf), "%d", remaining_time);
2280 property_set("vold.encrypt_time_remaining", buf);
2281 data->remaining_time = remaining_time;
2282 }
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002283 }
2284 }
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002285}
2286
Paul Lawrence3846be12014-09-22 11:33:54 -07002287static void log_progress(struct encryptGroupsData const* data, bool completed)
2288{
2289 // Precondition - if completed data = 0 else data != 0
2290
2291 // Track progress so we can skip logging blocks
2292 static off64_t offset = -1;
2293
2294 // Need to close existing 'Encrypting from' log?
2295 if (completed || (offset != -1 && data->offset != offset)) {
2296 SLOGI("Encrypted to sector %" PRId64,
2297 offset / info.block_size * CRYPT_SECTOR_SIZE);
2298 offset = -1;
2299 }
2300
2301 // Need to start new 'Encrypting from' log?
2302 if (!completed && offset != data->offset) {
2303 SLOGI("Encrypting from sector %" PRId64,
2304 data->offset / info.block_size * CRYPT_SECTOR_SIZE);
2305 }
2306
2307 // Update offset
2308 if (!completed) {
2309 offset = data->offset + (off64_t)data->count * info.block_size;
2310 }
2311}
2312
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002313static int flush_outstanding_data(struct encryptGroupsData* data)
2314{
2315 if (data->count == 0) {
2316 return 0;
2317 }
2318
Elliott Hughes231bdba2014-06-25 18:36:19 -07002319 SLOGV("Copying %d blocks at offset %" PRIx64, data->count, data->offset);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002320
2321 if (pread64(data->realfd, data->buffer,
2322 info.block_size * data->count, data->offset)
2323 <= 0) {
2324 SLOGE("Error reading real_blkdev %s for inplace encrypt",
2325 data->real_blkdev);
2326 return -1;
2327 }
2328
2329 if (pwrite64(data->cryptofd, data->buffer,
2330 info.block_size * data->count, data->offset)
2331 <= 0) {
2332 SLOGE("Error writing crypto_blkdev %s for inplace encrypt",
2333 data->crypto_blkdev);
2334 return -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002335 } else {
Paul Lawrence3846be12014-09-22 11:33:54 -07002336 log_progress(data, false);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002337 }
2338
2339 data->count = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002340 data->last_written_sector = (data->offset + data->count)
2341 / info.block_size * CRYPT_SECTOR_SIZE - 1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002342 return 0;
2343}
2344
2345static int encrypt_groups(struct encryptGroupsData* data)
2346{
2347 unsigned int i;
2348 u8 *block_bitmap = 0;
2349 unsigned int block;
2350 off64_t ret;
2351 int rc = -1;
2352
2353 data->buffer = malloc(info.block_size * BLOCKS_AT_A_TIME);
2354 if (!data->buffer) {
2355 SLOGE("Failed to allocate crypto buffer");
2356 goto errout;
2357 }
2358
2359 block_bitmap = malloc(info.block_size);
2360 if (!block_bitmap) {
2361 SLOGE("failed to allocate block bitmap");
2362 goto errout;
2363 }
2364
2365 for (i = 0; i < aux_info.groups; ++i) {
2366 SLOGI("Encrypting group %d", i);
2367
2368 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
2369 u32 block_count = min(info.blocks_per_group,
2370 aux_info.len_blocks - first_block);
2371
2372 off64_t offset = (u64)info.block_size
2373 * aux_info.bg_desc[i].bg_block_bitmap;
2374
2375 ret = pread64(data->realfd, block_bitmap, info.block_size, offset);
2376 if (ret != (int)info.block_size) {
2377 SLOGE("failed to read all of block group bitmap %d", i);
2378 goto errout;
2379 }
2380
2381 offset = (u64)info.block_size * first_block;
2382
2383 data->count = 0;
2384
2385 for (block = 0; block < block_count; block++) {
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002386 int used = bitmap_get_bit(block_bitmap, block);
2387 update_progress(data, used);
2388 if (used) {
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002389 if (data->count == 0) {
2390 data->offset = offset;
2391 }
2392 data->count++;
2393 } else {
2394 if (flush_outstanding_data(data)) {
2395 goto errout;
2396 }
2397 }
2398
2399 offset += info.block_size;
2400
2401 /* Write data if we are aligned or buffer size reached */
2402 if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0
2403 || data->count == BLOCKS_AT_A_TIME) {
2404 if (flush_outstanding_data(data)) {
2405 goto errout;
2406 }
2407 }
Paul Lawrence87999172014-02-20 12:21:31 -08002408
Paul Lawrence73d7a022014-06-09 14:10:09 -07002409 if (!is_battery_ok_to_continue()) {
Paul Lawrence87999172014-02-20 12:21:31 -08002410 SLOGE("Stopping encryption due to low battery");
2411 rc = 0;
2412 goto errout;
2413 }
2414
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002415 }
2416 if (flush_outstanding_data(data)) {
2417 goto errout;
2418 }
2419 }
2420
Paul Lawrence87999172014-02-20 12:21:31 -08002421 data->completed = 1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002422 rc = 0;
2423
2424errout:
Paul Lawrence3846be12014-09-22 11:33:54 -07002425 log_progress(0, true);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002426 free(data->buffer);
2427 free(block_bitmap);
2428 return rc;
2429}
2430
2431static int cryptfs_enable_inplace_ext4(char *crypto_blkdev,
2432 char *real_blkdev,
2433 off64_t size,
2434 off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002435 off64_t tot_size,
2436 off64_t previously_encrypted_upto)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002437{
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002438 u32 i;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002439 struct encryptGroupsData data;
Paul Lawrence74f29f12014-08-28 15:54:10 -07002440 int rc; // Can't initialize without causing warning -Wclobbered
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002441
Paul Lawrence87999172014-02-20 12:21:31 -08002442 if (previously_encrypted_upto > *size_already_done) {
2443 SLOGD("Not fast encrypting since resuming part way through");
2444 return -1;
2445 }
2446
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002447 memset(&data, 0, sizeof(data));
2448 data.real_blkdev = real_blkdev;
2449 data.crypto_blkdev = crypto_blkdev;
2450
2451 if ( (data.realfd = open(real_blkdev, O_RDWR)) < 0) {
JP Abgrall3334c6a2014-10-10 15:52:11 -07002452 SLOGE("Error opening real_blkdev %s for inplace encrypt. err=%d(%s)\n",
2453 real_blkdev, errno, strerror(errno));
Paul Lawrence74f29f12014-08-28 15:54:10 -07002454 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002455 goto errout;
2456 }
2457
2458 if ( (data.cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002459 SLOGE("Error opening crypto_blkdev %s for ext4 inplace encrypt. err=%d(%s)\n",
JP Abgrall3334c6a2014-10-10 15:52:11 -07002460 crypto_blkdev, errno, strerror(errno));
JP Abgrall7fc1de82014-10-10 18:43:41 -07002461 rc = ENABLE_INPLACE_ERR_DEV;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002462 goto errout;
2463 }
2464
2465 if (setjmp(setjmp_env)) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002466 SLOGE("Reading ext4 extent caused an exception\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07002467 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002468 goto errout;
2469 }
2470
2471 if (read_ext(data.realfd, 0) != 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002472 SLOGE("Failed to read ext4 extent\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07002473 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002474 goto errout;
2475 }
2476
2477 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2478 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2479 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
2480
JP Abgrall7fc1de82014-10-10 18:43:41 -07002481 SLOGI("Encrypting ext4 filesystem in place...");
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002482
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002483 data.tot_used_blocks = data.numblocks;
2484 for (i = 0; i < aux_info.groups; ++i) {
2485 data.tot_used_blocks -= aux_info.bg_desc[i].bg_free_blocks_count;
2486 }
2487
2488 data.one_pct = data.tot_used_blocks / 100;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002489 data.cur_pct = 0;
Paul Lawrence9c58a872014-09-30 09:12:51 -07002490
2491 struct timespec time_started = {0};
2492 if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
2493 SLOGW("Error getting time at start");
2494 // Note - continue anyway - we'll run with 0
2495 }
2496 data.time_started = time_started.tv_sec;
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002497 data.remaining_time = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002498
2499 rc = encrypt_groups(&data);
2500 if (rc) {
2501 SLOGE("Error encrypting groups");
2502 goto errout;
2503 }
2504
Paul Lawrence87999172014-02-20 12:21:31 -08002505 *size_already_done += data.completed ? size : data.last_written_sector;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002506 rc = 0;
2507
2508errout:
2509 close(data.realfd);
2510 close(data.cryptofd);
2511
2512 return rc;
2513}
2514
Paul Lawrence3846be12014-09-22 11:33:54 -07002515static void log_progress_f2fs(u64 block, bool completed)
2516{
2517 // Precondition - if completed data = 0 else data != 0
2518
2519 // Track progress so we can skip logging blocks
2520 static u64 last_block = (u64)-1;
2521
2522 // Need to close existing 'Encrypting from' log?
2523 if (completed || (last_block != (u64)-1 && block != last_block + 1)) {
2524 SLOGI("Encrypted to block %" PRId64, last_block);
2525 last_block = -1;
2526 }
2527
2528 // Need to start new 'Encrypting from' log?
2529 if (!completed && (last_block == (u64)-1 || block != last_block + 1)) {
2530 SLOGI("Encrypting from block %" PRId64, block);
2531 }
2532
2533 // Update offset
2534 if (!completed) {
2535 last_block = block;
2536 }
2537}
2538
Daniel Rosenberge82df162014-08-15 22:19:23 +00002539static int encrypt_one_block_f2fs(u64 pos, void *data)
2540{
2541 struct encryptGroupsData *priv_dat = (struct encryptGroupsData *)data;
2542
2543 priv_dat->blocks_already_done = pos - 1;
2544 update_progress(priv_dat, 1);
2545
2546 off64_t offset = pos * CRYPT_INPLACE_BUFSIZE;
2547
2548 if (pread64(priv_dat->realfd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002549 SLOGE("Error reading real_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002550 return -1;
2551 }
2552
2553 if (pwrite64(priv_dat->cryptofd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002554 SLOGE("Error writing crypto_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002555 return -1;
2556 } else {
Paul Lawrence3846be12014-09-22 11:33:54 -07002557 log_progress_f2fs(pos, false);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002558 }
2559
2560 return 0;
2561}
2562
2563static int cryptfs_enable_inplace_f2fs(char *crypto_blkdev,
2564 char *real_blkdev,
2565 off64_t size,
2566 off64_t *size_already_done,
2567 off64_t tot_size,
2568 off64_t previously_encrypted_upto)
2569{
Daniel Rosenberge82df162014-08-15 22:19:23 +00002570 struct encryptGroupsData data;
2571 struct f2fs_info *f2fs_info = NULL;
JP Abgrall7fc1de82014-10-10 18:43:41 -07002572 int rc = ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002573 if (previously_encrypted_upto > *size_already_done) {
2574 SLOGD("Not fast encrypting since resuming part way through");
JP Abgrall7fc1de82014-10-10 18:43:41 -07002575 return ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002576 }
2577 memset(&data, 0, sizeof(data));
2578 data.real_blkdev = real_blkdev;
2579 data.crypto_blkdev = crypto_blkdev;
2580 data.realfd = -1;
2581 data.cryptofd = -1;
2582 if ( (data.realfd = open64(real_blkdev, O_RDWR)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002583 SLOGE("Error opening real_blkdev %s for f2fs inplace encrypt\n",
Daniel Rosenberge82df162014-08-15 22:19:23 +00002584 real_blkdev);
2585 goto errout;
2586 }
2587 if ( (data.cryptofd = open64(crypto_blkdev, O_WRONLY)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002588 SLOGE("Error opening crypto_blkdev %s for f2fs inplace encrypt. err=%d(%s)\n",
JP Abgrall3334c6a2014-10-10 15:52:11 -07002589 crypto_blkdev, errno, strerror(errno));
JP Abgrall7fc1de82014-10-10 18:43:41 -07002590 rc = ENABLE_INPLACE_ERR_DEV;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002591 goto errout;
2592 }
2593
2594 f2fs_info = generate_f2fs_info(data.realfd);
2595 if (!f2fs_info)
2596 goto errout;
2597
2598 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2599 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2600 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
2601
2602 data.tot_used_blocks = get_num_blocks_used(f2fs_info);
2603
2604 data.one_pct = data.tot_used_blocks / 100;
2605 data.cur_pct = 0;
2606 data.time_started = time(NULL);
2607 data.remaining_time = -1;
2608
2609 data.buffer = malloc(f2fs_info->block_size);
2610 if (!data.buffer) {
2611 SLOGE("Failed to allocate crypto buffer");
2612 goto errout;
2613 }
2614
2615 data.count = 0;
2616
2617 /* Currently, this either runs to completion, or hits a nonrecoverable error */
2618 rc = run_on_used_blocks(data.blocks_already_done, f2fs_info, &encrypt_one_block_f2fs, &data);
2619
2620 if (rc) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002621 SLOGE("Error in running over f2fs blocks");
2622 rc = ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002623 goto errout;
2624 }
2625
2626 *size_already_done += size;
2627 rc = 0;
2628
2629errout:
2630 if (rc)
2631 SLOGE("Failed to encrypt f2fs filesystem on %s", real_blkdev);
2632
Paul Lawrence3846be12014-09-22 11:33:54 -07002633 log_progress_f2fs(0, true);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002634 free(f2fs_info);
2635 free(data.buffer);
2636 close(data.realfd);
2637 close(data.cryptofd);
2638
2639 return rc;
2640}
2641
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002642static int cryptfs_enable_inplace_full(char *crypto_blkdev, char *real_blkdev,
2643 off64_t size, off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002644 off64_t tot_size,
2645 off64_t previously_encrypted_upto)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002646{
2647 int realfd, cryptofd;
2648 char *buf[CRYPT_INPLACE_BUFSIZE];
JP Abgrall7fc1de82014-10-10 18:43:41 -07002649 int rc = ENABLE_INPLACE_ERR_OTHER;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002650 off64_t numblocks, i, remainder;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002651 off64_t one_pct, cur_pct, new_pct;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002652 off64_t blocks_already_done, tot_numblocks;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002653
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002654 if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) {
2655 SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002656 return ENABLE_INPLACE_ERR_OTHER;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002657 }
2658
2659 if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
JP Abgrall3334c6a2014-10-10 15:52:11 -07002660 SLOGE("Error opening crypto_blkdev %s for inplace encrypt. err=%d(%s)\n",
2661 crypto_blkdev, errno, strerror(errno));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002662 close(realfd);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002663 return ENABLE_INPLACE_ERR_DEV;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002664 }
2665
2666 /* This is pretty much a simple loop of reading 4K, and writing 4K.
2667 * The size passed in is the number of 512 byte sectors in the filesystem.
2668 * So compute the number of whole 4K blocks we should read/write,
2669 * and the remainder.
2670 */
2671 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2672 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002673 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2674 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002675
2676 SLOGE("Encrypting filesystem in place...");
2677
Paul Lawrence87999172014-02-20 12:21:31 -08002678 i = previously_encrypted_upto + 1 - *size_already_done;
2679
2680 if (lseek64(realfd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
2681 SLOGE("Cannot seek to previously encrypted point on %s", real_blkdev);
2682 goto errout;
2683 }
2684
2685 if (lseek64(cryptofd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
2686 SLOGE("Cannot seek to previously encrypted point on %s", crypto_blkdev);
2687 goto errout;
2688 }
2689
2690 for (;i < size && i % CRYPT_SECTORS_PER_BUFSIZE != 0; ++i) {
2691 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2692 SLOGE("Error reading initial sectors from real_blkdev %s for "
2693 "inplace encrypt\n", crypto_blkdev);
2694 goto errout;
2695 }
2696 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2697 SLOGE("Error writing initial sectors to crypto_blkdev %s for "
2698 "inplace encrypt\n", crypto_blkdev);
2699 goto errout;
2700 } else {
Elliott Hughescb33f572014-06-25 18:25:11 -07002701 SLOGI("Encrypted 1 block at %" PRId64, i);
Paul Lawrence87999172014-02-20 12:21:31 -08002702 }
2703 }
2704
Ken Sumrall29d8da82011-05-18 17:20:07 -07002705 one_pct = tot_numblocks / 100;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002706 cur_pct = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002707 /* process the majority of the filesystem in blocks */
Paul Lawrence87999172014-02-20 12:21:31 -08002708 for (i/=CRYPT_SECTORS_PER_BUFSIZE; i<numblocks; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07002709 new_pct = (i + blocks_already_done) / one_pct;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002710 if (new_pct > cur_pct) {
2711 char buf[8];
2712
2713 cur_pct = new_pct;
Elliott Hughes73737162014-06-25 17:27:42 -07002714 snprintf(buf, sizeof(buf), "%" PRId64, cur_pct);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002715 property_set("vold.encrypt_progress", buf);
2716 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002717 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Lawrence87999172014-02-20 12:21:31 -08002718 SLOGE("Error reading real_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002719 goto errout;
2720 }
2721 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Lawrence87999172014-02-20 12:21:31 -08002722 SLOGE("Error writing crypto_blkdev %s for inplace encrypt", crypto_blkdev);
2723 goto errout;
2724 } else {
Elliott Hughescb33f572014-06-25 18:25:11 -07002725 SLOGD("Encrypted %d block at %" PRId64,
Paul Lawrence87999172014-02-20 12:21:31 -08002726 CRYPT_SECTORS_PER_BUFSIZE,
2727 i * CRYPT_SECTORS_PER_BUFSIZE);
2728 }
2729
Paul Lawrence73d7a022014-06-09 14:10:09 -07002730 if (!is_battery_ok_to_continue()) {
Paul Lawrence87999172014-02-20 12:21:31 -08002731 SLOGE("Stopping encryption due to low battery");
2732 *size_already_done += (i + 1) * CRYPT_SECTORS_PER_BUFSIZE - 1;
2733 rc = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002734 goto errout;
2735 }
2736 }
2737
2738 /* Do any remaining sectors */
2739 for (i=0; i<remainder; i++) {
Paul Lawrence87999172014-02-20 12:21:31 -08002740 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2741 SLOGE("Error reading final sectors from real_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002742 goto errout;
2743 }
Paul Lawrence87999172014-02-20 12:21:31 -08002744 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2745 SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002746 goto errout;
Paul Lawrence87999172014-02-20 12:21:31 -08002747 } else {
2748 SLOGI("Encrypted 1 block at next location");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002749 }
2750 }
2751
Ken Sumrall29d8da82011-05-18 17:20:07 -07002752 *size_already_done += size;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002753 rc = 0;
2754
2755errout:
2756 close(realfd);
2757 close(cryptofd);
2758
2759 return rc;
2760}
2761
JP Abgrall7fc1de82014-10-10 18:43:41 -07002762/* returns on of the ENABLE_INPLACE_* return codes */
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002763static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev,
2764 off64_t size, off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002765 off64_t tot_size,
2766 off64_t previously_encrypted_upto)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002767{
JP Abgrall7fc1de82014-10-10 18:43:41 -07002768 int rc_ext4, rc_f2fs, rc_full;
Paul Lawrence87999172014-02-20 12:21:31 -08002769 if (previously_encrypted_upto) {
Elliott Hughescb33f572014-06-25 18:25:11 -07002770 SLOGD("Continuing encryption from %" PRId64, previously_encrypted_upto);
Paul Lawrence87999172014-02-20 12:21:31 -08002771 }
2772
2773 if (*size_already_done + size < previously_encrypted_upto) {
2774 *size_already_done += size;
2775 return 0;
2776 }
2777
Daniel Rosenberge82df162014-08-15 22:19:23 +00002778 /* TODO: identify filesystem type.
2779 * As is, cryptfs_enable_inplace_ext4 will fail on an f2fs partition, and
2780 * then we will drop down to cryptfs_enable_inplace_f2fs.
2781 * */
JP Abgrall7fc1de82014-10-10 18:43:41 -07002782 if ((rc_ext4 = cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev,
Daniel Rosenberge82df162014-08-15 22:19:23 +00002783 size, size_already_done,
JP Abgrall7fc1de82014-10-10 18:43:41 -07002784 tot_size, previously_encrypted_upto)) == 0) {
Daniel Rosenberge82df162014-08-15 22:19:23 +00002785 return 0;
2786 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002787 SLOGD("cryptfs_enable_inplace_ext4()=%d\n", rc_ext4);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002788
JP Abgrall7fc1de82014-10-10 18:43:41 -07002789 if ((rc_f2fs = cryptfs_enable_inplace_f2fs(crypto_blkdev, real_blkdev,
Daniel Rosenberge82df162014-08-15 22:19:23 +00002790 size, size_already_done,
JP Abgrall7fc1de82014-10-10 18:43:41 -07002791 tot_size, previously_encrypted_upto)) == 0) {
Daniel Rosenberge82df162014-08-15 22:19:23 +00002792 return 0;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002793 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002794 SLOGD("cryptfs_enable_inplace_f2fs()=%d\n", rc_f2fs);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002795
JP Abgrall7fc1de82014-10-10 18:43:41 -07002796 rc_full = cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev,
Paul Lawrence87999172014-02-20 12:21:31 -08002797 size, size_already_done, tot_size,
2798 previously_encrypted_upto);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002799 SLOGD("cryptfs_enable_inplace_full()=%d\n", rc_full);
2800
2801 /* Hack for b/17898962, the following is the symptom... */
2802 if (rc_ext4 == ENABLE_INPLACE_ERR_DEV
2803 && rc_f2fs == ENABLE_INPLACE_ERR_DEV
2804 && rc_full == ENABLE_INPLACE_ERR_DEV) {
2805 return ENABLE_INPLACE_ERR_DEV;
2806 }
2807 return rc_full;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002808}
2809
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002810#define CRYPTO_ENABLE_WIPE 1
2811#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002812
2813#define FRAMEWORK_BOOT_WAIT 60
2814
Ken Sumrall29d8da82011-05-18 17:20:07 -07002815static inline int should_encrypt(struct volume_info *volume)
2816{
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002817 return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) ==
Ken Sumrall29d8da82011-05-18 17:20:07 -07002818 (VOL_ENCRYPTABLE | VOL_NONREMOVABLE);
2819}
2820
Paul Lawrence87999172014-02-20 12:21:31 -08002821static int cryptfs_SHA256_fileblock(const char* filename, __le8* buf)
2822{
2823 int fd = open(filename, O_RDONLY);
2824 if (fd == -1) {
2825 SLOGE("Error opening file %s", filename);
2826 return -1;
2827 }
2828
2829 char block[CRYPT_INPLACE_BUFSIZE];
2830 memset(block, 0, sizeof(block));
2831 if (unix_read(fd, block, sizeof(block)) < 0) {
2832 SLOGE("Error reading file %s", filename);
2833 close(fd);
2834 return -1;
2835 }
2836
2837 close(fd);
2838
2839 SHA256_CTX c;
2840 SHA256_Init(&c);
2841 SHA256_Update(&c, block, sizeof(block));
2842 SHA256_Final(buf, &c);
2843
2844 return 0;
2845}
2846
JP Abgrall62c7af32014-06-16 13:01:23 -07002847static int get_fs_type(struct fstab_rec *rec)
2848{
2849 if (!strcmp(rec->fs_type, "ext4")) {
2850 return EXT4_FS;
2851 } else if (!strcmp(rec->fs_type, "f2fs")) {
2852 return F2FS_FS;
2853 } else {
2854 return -1;
2855 }
2856}
2857
Paul Lawrence87999172014-02-20 12:21:31 -08002858static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr *crypt_ftr, int how,
2859 char *crypto_blkdev, char *real_blkdev,
2860 int previously_encrypted_upto)
2861{
2862 off64_t cur_encryption_done=0, tot_encryption_size=0;
Tim Murray8439dc92014-12-15 11:56:11 -08002863 int rc = -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002864
Paul Lawrence73d7a022014-06-09 14:10:09 -07002865 if (!is_battery_ok_to_start()) {
2866 SLOGW("Not starting encryption due to low battery");
Paul Lawrence87999172014-02-20 12:21:31 -08002867 return 0;
2868 }
2869
2870 /* The size of the userdata partition, and add in the vold volumes below */
2871 tot_encryption_size = crypt_ftr->fs_size;
2872
2873 if (how == CRYPTO_ENABLE_WIPE) {
JP Abgrall62c7af32014-06-16 13:01:23 -07002874 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
2875 int fs_type = get_fs_type(rec);
2876 if (fs_type < 0) {
2877 SLOGE("cryptfs_enable: unsupported fs type %s\n", rec->fs_type);
2878 return -1;
2879 }
2880 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr->fs_size, fs_type);
Paul Lawrence87999172014-02-20 12:21:31 -08002881 } else if (how == CRYPTO_ENABLE_INPLACE) {
2882 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev,
2883 crypt_ftr->fs_size, &cur_encryption_done,
2884 tot_encryption_size,
2885 previously_encrypted_upto);
2886
JP Abgrall7fc1de82014-10-10 18:43:41 -07002887 if (rc == ENABLE_INPLACE_ERR_DEV) {
2888 /* Hack for b/17898962 */
2889 SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n");
2890 cryptfs_reboot(reboot);
2891 }
2892
Paul Lawrence73d7a022014-06-09 14:10:09 -07002893 if (!rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002894 crypt_ftr->encrypted_upto = cur_encryption_done;
2895 }
2896
Paul Lawrence73d7a022014-06-09 14:10:09 -07002897 if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002898 /* The inplace routine never actually sets the progress to 100% due
2899 * to the round down nature of integer division, so set it here */
2900 property_set("vold.encrypt_progress", "100");
2901 }
2902 } else {
2903 /* Shouldn't happen */
2904 SLOGE("cryptfs_enable: internal error, unknown option\n");
2905 rc = -1;
2906 }
2907
2908 return rc;
2909}
2910
Paul Lawrence13486032014-02-03 13:28:11 -08002911int cryptfs_enable_internal(char *howarg, int crypt_type, char *passwd,
2912 int allow_reboot)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002913{
2914 int how = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002915 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN];
Ken Sumralle5032c42012-04-01 23:58:44 -07002916 unsigned long nr_sec;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002917 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Tim Murray8439dc92014-12-15 11:56:11 -08002918 int rc=-1, fd, i;
Paul Lawrence87999172014-02-20 12:21:31 -08002919 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002920 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002921 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002922 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07002923 char key_loc[PROPERTY_VALUE_MAX];
2924 char fuse_sdcard[PROPERTY_VALUE_MAX];
2925 char *sd_mnt_point;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002926 int num_vols;
2927 struct volume_info *vol_list = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002928 off64_t previously_encrypted_upto = 0;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002929
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002930 if (!strcmp(howarg, "wipe")) {
2931 how = CRYPTO_ENABLE_WIPE;
2932 } else if (! strcmp(howarg, "inplace")) {
2933 how = CRYPTO_ENABLE_INPLACE;
2934 } else {
2935 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08002936 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002937 }
2938
Paul Lawrence87999172014-02-20 12:21:31 -08002939 /* See if an encryption was underway and interrupted */
2940 if (how == CRYPTO_ENABLE_INPLACE
2941 && get_crypt_ftr_and_key(&crypt_ftr) == 0
2942 && (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS)) {
2943 previously_encrypted_upto = crypt_ftr.encrypted_upto;
2944 crypt_ftr.encrypted_upto = 0;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002945 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
2946
2947 /* At this point, we are in an inconsistent state. Until we successfully
2948 complete encryption, a reboot will leave us broken. So mark the
2949 encryption failed in case that happens.
2950 On successfully completing encryption, remove this flag */
2951 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
2952
2953 put_crypt_ftr_and_key(&crypt_ftr);
Paul Lawrence87999172014-02-20 12:21:31 -08002954 }
2955
2956 property_get("ro.crypto.state", encrypted_state, "");
2957 if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) {
2958 SLOGE("Device is already running encrypted, aborting");
2959 goto error_unencrypted;
2960 }
2961
2962 // TODO refactor fs_mgr_get_crypt_info to get both in one call
2963 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumrall56ad03c2013-02-13 13:00:19 -08002964 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002965
Ken Sumrall3ed82362011-01-28 23:31:16 -08002966 /* Get the size of the real block device */
2967 fd = open(real_blkdev, O_RDONLY);
2968 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
2969 SLOGE("Cannot get size of block device %s\n", real_blkdev);
2970 goto error_unencrypted;
2971 }
2972 close(fd);
2973
2974 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002975 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002976 unsigned int fs_size_sec, max_fs_size_sec;
Jim Millera70abc62014-08-15 02:00:45 +00002977 fs_size_sec = get_fs_size(real_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002978 if (fs_size_sec == 0)
2979 fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev);
2980
Paul Lawrence87999172014-02-20 12:21:31 -08002981 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002982
2983 if (fs_size_sec > max_fs_size_sec) {
2984 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
2985 goto error_unencrypted;
2986 }
2987 }
2988
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002989 /* Get a wakelock as this may take a while, and we don't want the
2990 * device to sleep on us. We'll grab a partial wakelock, and if the UI
2991 * wants to keep the screen on, it can grab a full wakelock.
2992 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002993 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002994 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
2995
Jeff Sharkey7382f812012-08-23 14:08:59 -07002996 /* Get the sdcard mount point */
Jeff Sharkeyb77bc462012-10-01 14:36:26 -07002997 sd_mnt_point = getenv("EMULATED_STORAGE_SOURCE");
Jeff Sharkey7382f812012-08-23 14:08:59 -07002998 if (!sd_mnt_point) {
2999 sd_mnt_point = getenv("EXTERNAL_STORAGE");
3000 }
3001 if (!sd_mnt_point) {
3002 sd_mnt_point = "/mnt/sdcard";
3003 }
Ken Sumrall29d8da82011-05-18 17:20:07 -07003004
Paul Lawrence87999172014-02-20 12:21:31 -08003005 /* TODO
3006 * Currently do not have test devices with multiple encryptable volumes.
3007 * When we acquire some, re-add support.
3008 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07003009 num_vols=vold_getNumDirectVolumes();
3010 vol_list = malloc(sizeof(struct volume_info) * num_vols);
3011 vold_getDirectVolumeList(vol_list);
3012
3013 for (i=0; i<num_vols; i++) {
3014 if (should_encrypt(&vol_list[i])) {
Paul Lawrence87999172014-02-20 12:21:31 -08003015 SLOGE("Cannot encrypt if there are multiple encryptable volumes"
3016 "%s\n", vol_list[i].label);
3017 goto error_unencrypted;
Ken Sumrall29d8da82011-05-18 17:20:07 -07003018 }
3019 }
3020
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003021 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003022 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003023 */
3024 property_set("vold.decrypt", "trigger_shutdown_framework");
3025 SLOGD("Just asked init to shut down class main\n");
3026
Ken Sumrall425524d2012-06-14 20:55:28 -07003027 if (vold_unmountAllAsecs()) {
3028 /* Just report the error. If any are left mounted,
3029 * umounting /data below will fail and handle the error.
3030 */
3031 SLOGE("Error unmounting internal asecs");
3032 }
3033
Ken Sumrall29d8da82011-05-18 17:20:07 -07003034 property_get("ro.crypto.fuse_sdcard", fuse_sdcard, "");
3035 if (!strcmp(fuse_sdcard, "true")) {
3036 /* This is a device using the fuse layer to emulate the sdcard semantics
3037 * on top of the userdata partition. vold does not manage it, it is managed
3038 * by the sdcard service. The sdcard service was killed by the property trigger
3039 * above, so just unmount it now. We must do this _AFTER_ killing the framework,
3040 * unlike the case for vold managed devices above.
3041 */
Greg Hackmann6e8440f2014-10-02 17:18:20 -07003042 if (wait_and_unmount(sd_mnt_point, false)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07003043 goto error_shutting_down;
3044 }
Ken Sumrall2eaf7132011-01-14 12:45:48 -08003045 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003046
3047 /* Now unmount the /data partition. */
Greg Hackmann6e8440f2014-10-02 17:18:20 -07003048 if (wait_and_unmount(DATA_MNT_POINT, false)) {
JP Abgrall502dc742013-11-01 13:06:20 -07003049 if (allow_reboot) {
3050 goto error_shutting_down;
3051 } else {
3052 goto error_unencrypted;
3053 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003054 }
3055
3056 /* Do extra work for a better UX when doing the long inplace encryption */
3057 if (how == CRYPTO_ENABLE_INPLACE) {
3058 /* Now that /data is unmounted, we need to mount a tmpfs
3059 * /data, set a property saying we're doing inplace encryption,
3060 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003061 */
Ken Sumralle5032c42012-04-01 23:58:44 -07003062 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08003063 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003064 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003065 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08003066 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003067
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003068 /* restart the framework. */
3069 /* Create necessary paths on /data */
3070 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08003071 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003072 }
3073
Ken Sumrall92736ef2012-10-17 20:57:14 -07003074 /* Ugh, shutting down the framework is not synchronous, so until it
3075 * can be fixed, this horrible hack will wait a moment for it all to
3076 * shut down before proceeding. Without it, some devices cannot
3077 * restart the graphics services.
3078 */
3079 sleep(2);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003080 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003081
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003082 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003083 /* Initialize a crypt_mnt_ftr for the partition */
Paul Lawrence87999172014-02-20 12:21:31 -08003084 if (previously_encrypted_upto == 0) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07003085 if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) {
3086 goto error_shutting_down;
3087 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003088
Paul Lawrence87999172014-02-20 12:21:31 -08003089 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
3090 crypt_ftr.fs_size = nr_sec
3091 - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
3092 } else {
3093 crypt_ftr.fs_size = nr_sec;
3094 }
Paul Lawrence6bfed202014-07-28 12:47:22 -07003095 /* At this point, we are in an inconsistent state. Until we successfully
3096 complete encryption, a reboot will leave us broken. So mark the
3097 encryption failed in case that happens.
3098 On successfully completing encryption, remove this flag */
3099 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
Paul Lawrence87999172014-02-20 12:21:31 -08003100 crypt_ftr.crypt_type = crypt_type;
Ajay Dudani87701e22014-09-17 21:02:52 -07003101#ifndef CONFIG_HW_DISK_ENCRYPTION
3102 strlcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256", MAX_CRYPTO_TYPE_NAME_LEN);
3103#else
3104 strlcpy((char *)crypt_ftr.crypto_type_name, "aes-xts", MAX_CRYPTO_TYPE_NAME_LEN);
3105
Iliyan Malchevbb7d9af2014-11-20 18:42:23 -08003106 rc = clear_hw_device_encryption_key();
Ajay Dudani87701e22014-09-17 21:02:52 -07003107 if (!rc) {
3108 SLOGE("Error clearing device encryption hardware key. rc = %d", rc);
3109 }
3110
3111 rc = set_hw_device_encryption_key(passwd,
3112 (char*) crypt_ftr.crypto_type_name);
3113 if (!rc) {
3114 SLOGE("Error initializing device encryption hardware key. rc = %d", rc);
3115 goto error_shutting_down;
3116 }
3117#endif
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003118
Paul Lawrence87999172014-02-20 12:21:31 -08003119 /* Make an encrypted master key */
3120 if (create_encrypted_random_key(passwd, crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
3121 SLOGE("Cannot create encrypted master key\n");
3122 goto error_shutting_down;
3123 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003124
Paul Lawrence87999172014-02-20 12:21:31 -08003125 /* Write the key to the end of the partition */
3126 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003127
Paul Lawrence87999172014-02-20 12:21:31 -08003128 /* If any persistent data has been remembered, save it.
3129 * If none, create a valid empty table and save that.
3130 */
3131 if (!persist_data) {
3132 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
3133 if (pdata) {
3134 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
3135 persist_data = pdata;
3136 }
3137 }
3138 if (persist_data) {
3139 save_persistent_data();
3140 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003141 }
3142
Ajay Dudani87701e22014-09-17 21:02:52 -07003143 if (how == CRYPTO_ENABLE_INPLACE) {
3144 /* startup service classes main and late_start */
3145 property_set("vold.decrypt", "trigger_restart_min_framework");
3146 SLOGD("Just triggered restart_min_framework\n");
3147
3148 /* OK, the framework is restarted and will soon be showing a
3149 * progress bar. Time to setup an encrypted mapping, and
3150 * either write a new filesystem, or encrypt in place updating
3151 * the progress bar as we work.
3152 */
3153 }
3154
Paul Lawrenced0c7b172014-08-08 14:28:10 -07003155 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07003156 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
3157 "userdata");
3158
Paul Lawrence87999172014-02-20 12:21:31 -08003159 /* If we are continuing, check checksums match */
3160 rc = 0;
3161 if (previously_encrypted_upto) {
3162 __le8 hash_first_block[SHA256_DIGEST_LENGTH];
3163 rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block);
Ken Sumrall128626f2011-06-28 18:45:14 -07003164
Paul Lawrence87999172014-02-20 12:21:31 -08003165 if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block,
3166 sizeof(hash_first_block)) != 0) {
3167 SLOGE("Checksums do not match - trigger wipe");
3168 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07003169 }
3170 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003171
Paul Lawrence87999172014-02-20 12:21:31 -08003172 if (!rc) {
3173 rc = cryptfs_enable_all_volumes(&crypt_ftr, how,
3174 crypto_blkdev, real_blkdev,
3175 previously_encrypted_upto);
3176 }
3177
3178 /* Calculate checksum if we are not finished */
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08003179 if (!rc && how == CRYPTO_ENABLE_INPLACE
3180 && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08003181 rc = cryptfs_SHA256_fileblock(crypto_blkdev,
3182 crypt_ftr.hash_first_block);
Paul Lawrence73d7a022014-06-09 14:10:09 -07003183 if (rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08003184 SLOGE("Error calculating checksum for continuing encryption");
3185 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07003186 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003187 }
3188
3189 /* Undo the dm-crypt mapping whether we succeed or not */
Ken Sumrall29d8da82011-05-18 17:20:07 -07003190 delete_crypto_blk_dev("userdata");
Ken Sumrall29d8da82011-05-18 17:20:07 -07003191
3192 free(vol_list);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003193
3194 if (! rc) {
3195 /* Success */
Paul Lawrence6bfed202014-07-28 12:47:22 -07003196 crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08003197
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08003198 if (how == CRYPTO_ENABLE_INPLACE
3199 && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08003200 SLOGD("Encrypted up to sector %lld - will continue after reboot",
3201 crypt_ftr.encrypted_upto);
Paul Lawrence6bfed202014-07-28 12:47:22 -07003202 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence87999172014-02-20 12:21:31 -08003203 }
Paul Lawrence73d7a022014-06-09 14:10:09 -07003204
Paul Lawrence6bfed202014-07-28 12:47:22 -07003205 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08003206
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08003207 if (how == CRYPTO_ENABLE_WIPE
3208 || crypt_ftr.encrypted_upto == crypt_ftr.fs_size) {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003209 char value[PROPERTY_VALUE_MAX];
3210 property_get("ro.crypto.state", value, "");
3211 if (!strcmp(value, "")) {
3212 /* default encryption - continue first boot sequence */
3213 property_set("ro.crypto.state", "encrypted");
3214 release_wake_lock(lockid);
3215 cryptfs_check_passwd(DEFAULT_PASSWORD);
3216 cryptfs_restart_internal(1);
3217 return 0;
3218 } else {
3219 sleep(2); /* Give the UI a chance to show 100% progress */
Paul Lawrence87999172014-02-20 12:21:31 -08003220 cryptfs_reboot(reboot);
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003221 }
Paul Lawrence87999172014-02-20 12:21:31 -08003222 } else {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003223 sleep(2); /* Partially encrypted, ensure writes flushed to ssd */
Paul Lawrence87999172014-02-20 12:21:31 -08003224 cryptfs_reboot(shutdown);
3225 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003226 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003227 char value[PROPERTY_VALUE_MAX];
3228
Ken Sumrall319369a2012-06-27 16:30:18 -07003229 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003230 if (!strcmp(value, "1")) {
3231 /* wipe data if encryption failed */
3232 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
3233 mkdir("/cache/recovery", 0700);
Nick Kralevich4684e582012-06-26 15:07:03 -07003234 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC, 0600);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003235 if (fd >= 0) {
Jeff Sharkeydd1a8042014-09-24 11:46:51 -07003236 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
3237 write(fd, "--reason=cryptfs_enable_internal\n", strlen("--reason=cryptfs_enable_internal\n") + 1);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003238 close(fd);
3239 } else {
3240 SLOGE("could not open /cache/recovery/command\n");
3241 }
Paul Lawrence87999172014-02-20 12:21:31 -08003242 cryptfs_reboot(recovery);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003243 } else {
3244 /* set property to trigger dialog */
3245 property_set("vold.encrypt_progress", "error_partially_encrypted");
3246 release_wake_lock(lockid);
3247 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003248 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003249 }
3250
Ken Sumrall3ed82362011-01-28 23:31:16 -08003251 /* hrm, the encrypt step claims success, but the reboot failed.
3252 * This should not happen.
3253 * Set the property and return. Hope the framework can deal with it.
3254 */
3255 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003256 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003257 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08003258
3259error_unencrypted:
Ken Sumrall29d8da82011-05-18 17:20:07 -07003260 free(vol_list);
Ken Sumrall3ed82362011-01-28 23:31:16 -08003261 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003262 if (lockid[0]) {
3263 release_wake_lock(lockid);
3264 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003265 return -1;
3266
3267error_shutting_down:
3268 /* we failed, and have not encrypted anthing, so the users's data is still intact,
3269 * but the framework is stopped and not restarted to show the error, so it's up to
3270 * vold to restart the system.
3271 */
3272 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Paul Lawrence87999172014-02-20 12:21:31 -08003273 cryptfs_reboot(reboot);
Ken Sumrall3ed82362011-01-28 23:31:16 -08003274
3275 /* shouldn't get here */
3276 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall29d8da82011-05-18 17:20:07 -07003277 free(vol_list);
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003278 if (lockid[0]) {
3279 release_wake_lock(lockid);
3280 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003281 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003282}
3283
Paul Lawrence45f10532014-04-04 18:11:56 +00003284int cryptfs_enable(char *howarg, int type, char *passwd, int allow_reboot)
Paul Lawrence13486032014-02-03 13:28:11 -08003285{
Paul Lawrencefc615042014-10-04 15:32:29 -07003286 char* adjusted_passwd = adjust_passwd(passwd);
3287 if (adjusted_passwd) {
3288 passwd = adjusted_passwd;
3289 }
3290
3291 int rc = cryptfs_enable_internal(howarg, type, passwd, allow_reboot);
3292
3293 free(adjusted_passwd);
3294 return rc;
Paul Lawrence13486032014-02-03 13:28:11 -08003295}
3296
3297int cryptfs_enable_default(char *howarg, int allow_reboot)
3298{
3299 return cryptfs_enable_internal(howarg, CRYPT_TYPE_DEFAULT,
3300 DEFAULT_PASSWORD, allow_reboot);
3301}
3302
3303int cryptfs_changepw(int crypt_type, const char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003304{
3305 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003306
3307 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08003308 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08003309 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003310 return -1;
3311 }
3312
Paul Lawrencef4faa572014-01-29 13:31:03 -08003313 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
3314 SLOGE("Invalid crypt_type %d", crypt_type);
3315 return -1;
3316 }
3317
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003318 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07003319 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08003320 SLOGE("Error getting crypt footer and key");
3321 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003322 }
3323
Paul Lawrencef4faa572014-01-29 13:31:03 -08003324 crypt_ftr.crypt_type = crypt_type;
3325
Paul Lawrencefc615042014-10-04 15:32:29 -07003326 char* adjusted_passwd = adjust_passwd(newpw);
3327 if (adjusted_passwd) {
3328 newpw = adjusted_passwd;
3329 }
3330
Paul Lawrencef4faa572014-01-29 13:31:03 -08003331 encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
3332 : newpw,
3333 crypt_ftr.salt,
3334 saved_master_key,
3335 crypt_ftr.master_key,
3336 &crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003337
Jason parks70a4b3f2011-01-28 10:10:47 -06003338 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07003339 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003340
Paul Lawrencefc615042014-10-04 15:32:29 -07003341 free(adjusted_passwd);
Ajay Dudani87701e22014-09-17 21:02:52 -07003342
3343#ifdef CONFIG_HW_DISK_ENCRYPTION
Iliyan Malchevbb7d9af2014-11-20 18:42:23 -08003344 if (!strcmp((char *)crypt_ftr.crypto_type_name, "aes-xts")) {
3345 if (crypt_type == CRYPT_TYPE_DEFAULT) {
3346 int rc = update_hw_device_encryption_key(DEFAULT_PASSWORD, (char*) crypt_ftr.crypto_type_name);
3347 SLOGD("Update hardware encryption key to default for crypt_type: %d. rc = %d", crypt_type, rc);
3348 if (!rc)
3349 return -1;
3350 } else {
3351 int rc = update_hw_device_encryption_key(newpw, (char*) crypt_ftr.crypto_type_name);
3352 SLOGD("Update hardware encryption key for crypt_type: %d. rc = %d", crypt_type, rc);
3353 if (!rc)
3354 return -1;
3355 }
Ajay Dudani87701e22014-09-17 21:02:52 -07003356 }
3357#endif
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003358 return 0;
3359}
Ken Sumrall160b4d62013-04-22 12:15:39 -07003360
Rubin Xu85c01f92014-10-13 12:49:54 +01003361static unsigned int persist_get_max_entries(int encrypted) {
3362 struct crypt_mnt_ftr crypt_ftr;
3363 unsigned int dsize;
3364 unsigned int max_persistent_entries;
3365
3366 /* If encrypted, use the values from the crypt_ftr, otherwise
3367 * use the values for the current spec.
3368 */
3369 if (encrypted) {
3370 if (get_crypt_ftr_and_key(&crypt_ftr)) {
3371 return -1;
3372 }
3373 dsize = crypt_ftr.persist_data_size;
3374 } else {
3375 dsize = CRYPT_PERSIST_DATA_SIZE;
3376 }
3377
3378 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
3379 sizeof(struct crypt_persist_entry);
3380
3381 return max_persistent_entries;
3382}
3383
3384static int persist_get_key(const char *fieldname, char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003385{
3386 unsigned int i;
3387
3388 if (persist_data == NULL) {
3389 return -1;
3390 }
3391 for (i = 0; i < persist_data->persist_valid_entries; i++) {
3392 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
3393 /* We found it! */
3394 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
3395 return 0;
3396 }
3397 }
3398
3399 return -1;
3400}
3401
Rubin Xu85c01f92014-10-13 12:49:54 +01003402static int persist_set_key(const char *fieldname, const char *value, int encrypted)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003403{
3404 unsigned int i;
3405 unsigned int num;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003406 unsigned int max_persistent_entries;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003407
3408 if (persist_data == NULL) {
3409 return -1;
3410 }
3411
Rubin Xu85c01f92014-10-13 12:49:54 +01003412 max_persistent_entries = persist_get_max_entries(encrypted);
Ken Sumrall160b4d62013-04-22 12:15:39 -07003413
3414 num = persist_data->persist_valid_entries;
3415
3416 for (i = 0; i < num; i++) {
3417 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
3418 /* We found an existing entry, update it! */
3419 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
3420 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
3421 return 0;
3422 }
3423 }
3424
3425 /* We didn't find it, add it to the end, if there is room */
3426 if (persist_data->persist_valid_entries < max_persistent_entries) {
3427 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
3428 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
3429 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
3430 persist_data->persist_valid_entries++;
3431 return 0;
3432 }
3433
3434 return -1;
3435}
3436
Rubin Xu85c01f92014-10-13 12:49:54 +01003437/**
3438 * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the
3439 * sequence and its index is greater than or equal to index. Return 0 otherwise.
3440 */
3441static int match_multi_entry(const char *key, const char *field, unsigned index) {
Rubin Xu85c01f92014-10-13 12:49:54 +01003442 unsigned int field_len;
3443 unsigned int key_index;
3444 field_len = strlen(field);
3445
3446 if (index == 0) {
3447 // The first key in a multi-entry field is just the filedname itself.
3448 if (!strcmp(key, field)) {
3449 return 1;
3450 }
3451 }
3452 // Match key against "%s_%d" % (field, index)
3453 if (strlen(key) < field_len + 1 + 1) {
3454 // Need at least a '_' and a digit.
3455 return 0;
3456 }
3457 if (strncmp(key, field, field_len)) {
3458 // If the key does not begin with field, it's not a match.
3459 return 0;
3460 }
3461 if (1 != sscanf(&key[field_len],"_%d", &key_index)) {
3462 return 0;
3463 }
3464 return key_index >= index;
3465}
3466
3467/*
3468 * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all
3469 * remaining entries starting from index will be deleted.
3470 * returns PERSIST_DEL_KEY_OK if deletion succeeds,
3471 * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist,
3472 * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs.
3473 *
3474 */
3475static int persist_del_keys(const char *fieldname, unsigned index)
3476{
3477 unsigned int i;
3478 unsigned int j;
3479 unsigned int num;
3480
3481 if (persist_data == NULL) {
3482 return PERSIST_DEL_KEY_ERROR_OTHER;
3483 }
3484
3485 num = persist_data->persist_valid_entries;
3486
3487 j = 0; // points to the end of non-deleted entries.
3488 // Filter out to-be-deleted entries in place.
3489 for (i = 0; i < num; i++) {
3490 if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) {
3491 persist_data->persist_entry[j] = persist_data->persist_entry[i];
3492 j++;
3493 }
3494 }
3495
3496 if (j < num) {
3497 persist_data->persist_valid_entries = j;
3498 // Zeroise the remaining entries
3499 memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry));
3500 return PERSIST_DEL_KEY_OK;
3501 } else {
3502 // Did not find an entry matching the given fieldname
3503 return PERSIST_DEL_KEY_ERROR_NO_FIELD;
3504 }
3505}
3506
3507static int persist_count_keys(const char *fieldname)
3508{
3509 unsigned int i;
3510 unsigned int count;
3511
3512 if (persist_data == NULL) {
3513 return -1;
3514 }
3515
3516 count = 0;
3517 for (i = 0; i < persist_data->persist_valid_entries; i++) {
3518 if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) {
3519 count++;
3520 }
3521 }
3522
3523 return count;
3524}
3525
Ken Sumrall160b4d62013-04-22 12:15:39 -07003526/* Return the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01003527int cryptfs_getfield(const char *fieldname, char *value, int len)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003528{
3529 char temp_value[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01003530 /* CRYPTO_GETFIELD_OK is success,
3531 * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set,
3532 * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small,
3533 * CRYPTO_GETFIELD_ERROR_OTHER is any other error
Ken Sumrall160b4d62013-04-22 12:15:39 -07003534 */
Rubin Xu85c01f92014-10-13 12:49:54 +01003535 int rc = CRYPTO_GETFIELD_ERROR_OTHER;
3536 int i;
3537 char temp_field[PROPERTY_KEY_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -07003538
3539 if (persist_data == NULL) {
3540 load_persistent_data();
3541 if (persist_data == NULL) {
3542 SLOGE("Getfield error, cannot load persistent data");
3543 goto out;
3544 }
3545 }
3546
Rubin Xu85c01f92014-10-13 12:49:54 +01003547 // Read value from persistent entries. If the original value is split into multiple entries,
3548 // stitch them back together.
Ken Sumrall160b4d62013-04-22 12:15:39 -07003549 if (!persist_get_key(fieldname, temp_value)) {
Rubin Xu85c01f92014-10-13 12:49:54 +01003550 // We found it, copy it to the caller's buffer and keep going until all entries are read.
3551 if (strlcpy(value, temp_value, len) >= (unsigned) len) {
3552 // value too small
3553 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
3554 goto out;
3555 }
3556 rc = CRYPTO_GETFIELD_OK;
3557
3558 for (i = 1; /* break explicitly */; i++) {
3559 if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >=
3560 (int) sizeof(temp_field)) {
3561 // If the fieldname is very long, we stop as soon as it begins to overflow the
3562 // maximum field length. At this point we have in fact fully read out the original
3563 // value because cryptfs_setfield would not allow fields with longer names to be
3564 // written in the first place.
3565 break;
3566 }
3567 if (!persist_get_key(temp_field, temp_value)) {
3568 if (strlcat(value, temp_value, len) >= (unsigned)len) {
3569 // value too small.
3570 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
3571 goto out;
3572 }
3573 } else {
3574 // Exhaust all entries.
3575 break;
3576 }
3577 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003578 } else {
3579 /* Sadness, it's not there. Return the error */
Rubin Xu85c01f92014-10-13 12:49:54 +01003580 rc = CRYPTO_GETFIELD_ERROR_NO_FIELD;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003581 }
3582
3583out:
3584 return rc;
3585}
3586
3587/* Set the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01003588int cryptfs_setfield(const char *fieldname, const char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003589{
Ken Sumrall160b4d62013-04-22 12:15:39 -07003590 char encrypted_state[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01003591 /* 0 is success, negative values are error */
3592 int rc = CRYPTO_SETFIELD_ERROR_OTHER;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003593 int encrypted = 0;
Rubin Xu85c01f92014-10-13 12:49:54 +01003594 unsigned int field_id;
3595 char temp_field[PROPERTY_KEY_MAX];
3596 unsigned int num_entries;
3597 unsigned int max_keylen;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003598
3599 if (persist_data == NULL) {
3600 load_persistent_data();
3601 if (persist_data == NULL) {
3602 SLOGE("Setfield error, cannot load persistent data");
3603 goto out;
3604 }
3605 }
3606
3607 property_get("ro.crypto.state", encrypted_state, "");
3608 if (!strcmp(encrypted_state, "encrypted") ) {
3609 encrypted = 1;
3610 }
3611
Rubin Xu85c01f92014-10-13 12:49:54 +01003612 // Compute the number of entries required to store value, each entry can store up to
3613 // (PROPERTY_VALUE_MAX - 1) chars
3614 if (strlen(value) == 0) {
3615 // Empty value also needs one entry to store.
3616 num_entries = 1;
3617 } else {
3618 num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1);
3619 }
3620
3621 max_keylen = strlen(fieldname);
3622 if (num_entries > 1) {
3623 // Need an extra "_%d" suffix.
3624 max_keylen += 1 + log10(num_entries);
3625 }
3626 if (max_keylen > PROPERTY_KEY_MAX - 1) {
3627 rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003628 goto out;
3629 }
3630
Rubin Xu85c01f92014-10-13 12:49:54 +01003631 // Make sure we have enough space to write the new value
3632 if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) >
3633 persist_get_max_entries(encrypted)) {
3634 rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG;
3635 goto out;
3636 }
3637
3638 // Now that we know persist_data has enough space for value, let's delete the old field first
3639 // to make up space.
3640 persist_del_keys(fieldname, 0);
3641
3642 if (persist_set_key(fieldname, value, encrypted)) {
3643 // fail to set key, should not happen as we have already checked the available space
3644 SLOGE("persist_set_key() error during setfield()");
3645 goto out;
3646 }
3647
3648 for (field_id = 1; field_id < num_entries; field_id++) {
3649 snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, field_id);
3650
3651 if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), encrypted)) {
3652 // fail to set key, should not happen as we have already checked the available space.
3653 SLOGE("persist_set_key() error during setfield()");
3654 goto out;
3655 }
3656 }
3657
Ken Sumrall160b4d62013-04-22 12:15:39 -07003658 /* If we are running encrypted, save the persistent data now */
3659 if (encrypted) {
3660 if (save_persistent_data()) {
3661 SLOGE("Setfield error, cannot save persistent data");
3662 goto out;
3663 }
3664 }
3665
Rubin Xu85c01f92014-10-13 12:49:54 +01003666 rc = CRYPTO_SETFIELD_OK;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003667
3668out:
3669 return rc;
3670}
Paul Lawrencef4faa572014-01-29 13:31:03 -08003671
3672/* Checks userdata. Attempt to mount the volume if default-
3673 * encrypted.
3674 * On success trigger next init phase and return 0.
3675 * Currently do not handle failure - see TODO below.
3676 */
3677int cryptfs_mount_default_encrypted(void)
3678{
3679 char decrypt_state[PROPERTY_VALUE_MAX];
3680 property_get("vold.decrypt", decrypt_state, "0");
3681 if (!strcmp(decrypt_state, "0")) {
3682 SLOGE("Not encrypted - should not call here");
3683 } else {
3684 int crypt_type = cryptfs_get_password_type();
3685 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
3686 SLOGE("Bad crypt type - error");
3687 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
3688 SLOGD("Password is not default - "
3689 "starting min framework to prompt");
3690 property_set("vold.decrypt", "trigger_restart_min_framework");
3691 return 0;
3692 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
3693 SLOGD("Password is default - restarting filesystem");
3694 cryptfs_restart_internal(0);
3695 return 0;
3696 } else {
3697 SLOGE("Encrypted, default crypt type but can't decrypt");
3698 }
3699 }
3700
Paul Lawrence6bfed202014-07-28 12:47:22 -07003701 /** Corrupt. Allow us to boot into framework, which will detect bad
3702 crypto when it calls do_crypto_complete, then do a factory reset
Paul Lawrencef4faa572014-01-29 13:31:03 -08003703 */
Paul Lawrence6bfed202014-07-28 12:47:22 -07003704 property_set("vold.decrypt", "trigger_restart_min_framework");
Paul Lawrencef4faa572014-01-29 13:31:03 -08003705 return 0;
3706}
3707
3708/* Returns type of the password, default, pattern, pin or password.
3709 */
3710int cryptfs_get_password_type(void)
3711{
3712 struct crypt_mnt_ftr crypt_ftr;
3713
3714 if (get_crypt_ftr_and_key(&crypt_ftr)) {
3715 SLOGE("Error getting crypt footer and key\n");
3716 return -1;
3717 }
3718
Paul Lawrence6bfed202014-07-28 12:47:22 -07003719 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) {
3720 return -1;
3721 }
3722
Paul Lawrencef4faa572014-01-29 13:31:03 -08003723 return crypt_ftr.crypt_type;
3724}
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003725
Paul Lawrence399317e2014-03-10 13:20:50 -07003726char* cryptfs_get_password()
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003727{
Paul Lawrence399317e2014-03-10 13:20:50 -07003728 struct timespec now;
Paul Lawrenceef2b5be2014-11-11 12:47:03 -08003729 clock_gettime(CLOCK_BOOTTIME, &now);
Paul Lawrence399317e2014-03-10 13:20:50 -07003730 if (now.tv_sec < password_expiry_time) {
3731 return password;
3732 } else {
3733 cryptfs_clear_password();
3734 return 0;
3735 }
3736}
3737
3738void cryptfs_clear_password()
3739{
3740 if (password) {
3741 size_t len = strlen(password);
3742 memset(password, 0, len);
3743 free(password);
3744 password = 0;
3745 password_expiry_time = 0;
3746 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003747}