blob: 1e8f85b6adf4f6a531d4d7ae055a7fd7512ea03c [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 Lawrence8175a0b2015-03-05 09:46:23 -080056#include "ext4_crypt.h"
Paul Lawrenceae59fe62014-01-21 08:23:27 -080057#include "ext4_utils.h"
Daniel Rosenberge82df162014-08-15 22:19:23 +000058#include "f2fs_sparseblock.h"
Paul Lawrence87999172014-02-20 12:21:31 -080059#include "CheckBattery.h"
jessica_yu3f14fe42014-09-22 15:57:40 +080060#include "Process.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080061
Shawn Willdend1fd8462015-02-24 09:51:34 -070062#include <hardware/keymaster0.h>
Paul Lawrence69f4ebd2014-04-14 12:17:14 -070063
Mark Salyzyn3e971272014-01-21 13:27:04 -080064#define UNUSED __attribute__((unused))
65
Mark Salyzyn5eecc442014-02-12 14:16:14 -080066#define UNUSED __attribute__((unused))
67
Ajay Dudani87701e22014-09-17 21:02:52 -070068#ifdef CONFIG_HW_DISK_ENCRYPTION
69#include "cryptfs_hw.h"
70#endif
71
Ken Sumrall8f869aa2010-12-03 03:47:09 -080072#define DM_CRYPT_BUF_SIZE 4096
73
Jason parks70a4b3f2011-01-28 10:10:47 -060074#define HASH_COUNT 2000
75#define KEY_LEN_BYTES 16
76#define IV_LEN_BYTES 16
77
Ken Sumrall29d8da82011-05-18 17:20:07 -070078#define KEY_IN_FOOTER "footer"
79
Paul Lawrencef4faa572014-01-29 13:31:03 -080080// "default_password" encoded into hex (d=0x64 etc)
81#define DEFAULT_PASSWORD "64656661756c745f70617373776f7264"
82
Ken Sumrall29d8da82011-05-18 17:20:07 -070083#define EXT4_FS 1
JP Abgrall62c7af32014-06-16 13:01:23 -070084#define F2FS_FS 2
Ken Sumrall29d8da82011-05-18 17:20:07 -070085
Ken Sumralle919efe2012-09-29 17:07:41 -070086#define TABLE_LOAD_RETRIES 10
87
Shawn Willden47ba10d2014-09-03 17:07:06 -060088#define RSA_KEY_SIZE 2048
89#define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8)
90#define RSA_EXPONENT 0x10001
Paul Lawrence69f4ebd2014-04-14 12:17:14 -070091
Paul Lawrence8e3f4512014-09-08 10:11:17 -070092#define RETRY_MOUNT_ATTEMPTS 10
93#define RETRY_MOUNT_DELAY_SECONDS 1
94
Ken Sumrall8f869aa2010-12-03 03:47:09 -080095char *me = "cryptfs";
96
Jason parks70a4b3f2011-01-28 10:10:47 -060097static unsigned char saved_master_key[KEY_LEN_BYTES];
Ken Sumrall3ad90722011-10-04 20:38:29 -070098static char *saved_mount_point;
Jason parks70a4b3f2011-01-28 10:10:47 -060099static int master_key_saved = 0;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700100static struct crypt_persist_data *persist_data = NULL;
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800101
Shawn Willdend1fd8462015-02-24 09:51:34 -0700102static int keymaster_init(keymaster0_device_t **keymaster_dev)
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700103{
104 int rc;
105
106 const hw_module_t* mod;
107 rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod);
108 if (rc) {
109 ALOGE("could not find any keystore module");
110 goto out;
111 }
112
Shawn Willdend1fd8462015-02-24 09:51:34 -0700113 rc = keymaster0_open(mod, keymaster_dev);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700114 if (rc) {
115 ALOGE("could not open keymaster device in %s (%s)",
116 KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc));
117 goto out;
118 }
119
120 return 0;
121
122out:
123 *keymaster_dev = NULL;
124 return rc;
125}
126
127/* Should we use keymaster? */
128static int keymaster_check_compatibility()
129{
Shawn Willdend1fd8462015-02-24 09:51:34 -0700130 keymaster0_device_t *keymaster_dev = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700131 int rc = 0;
132
133 if (keymaster_init(&keymaster_dev)) {
134 SLOGE("Failed to init keymaster");
135 rc = -1;
136 goto out;
137 }
138
Paul Lawrence8c008392014-05-06 14:02:48 -0700139 SLOGI("keymaster version is %d", keymaster_dev->common.module->module_api_version);
140
141 if (keymaster_dev->common.module->module_api_version
142 < KEYMASTER_MODULE_API_VERSION_0_3) {
143 rc = 0;
144 goto out;
145 }
146
Shawn Willden7c49ab02014-10-30 08:12:32 -0600147 if (!(keymaster_dev->flags & KEYMASTER_SOFTWARE_ONLY) &&
148 (keymaster_dev->flags & KEYMASTER_BLOBS_ARE_STANDALONE)) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700149 rc = 1;
150 }
151
152out:
Shawn Willdend1fd8462015-02-24 09:51:34 -0700153 keymaster0_close(keymaster_dev);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700154 return rc;
155}
156
157/* Create a new keymaster key and store it in this footer */
158static int keymaster_create_key(struct crypt_mnt_ftr *ftr)
159{
160 uint8_t* key = 0;
Shawn Willdend1fd8462015-02-24 09:51:34 -0700161 keymaster0_device_t *keymaster_dev = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700162
163 if (keymaster_init(&keymaster_dev)) {
164 SLOGE("Failed to init keymaster");
165 return -1;
166 }
167
168 int rc = 0;
169
170 keymaster_rsa_keygen_params_t params;
171 memset(&params, '\0', sizeof(params));
Shawn Willden47ba10d2014-09-03 17:07:06 -0600172 params.public_exponent = RSA_EXPONENT;
173 params.modulus_size = RSA_KEY_SIZE;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700174
175 size_t key_size;
176 if (keymaster_dev->generate_keypair(keymaster_dev, TYPE_RSA, &params,
177 &key, &key_size)) {
178 SLOGE("Failed to generate keypair");
179 rc = -1;
180 goto out;
181 }
182
183 if (key_size > KEYMASTER_BLOB_SIZE) {
184 SLOGE("Keymaster key too large for crypto footer");
185 rc = -1;
186 goto out;
187 }
188
189 memcpy(ftr->keymaster_blob, key, key_size);
190 ftr->keymaster_blob_size = key_size;
191
192out:
Shawn Willdend1fd8462015-02-24 09:51:34 -0700193 keymaster0_close(keymaster_dev);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700194 free(key);
195 return rc;
196}
197
Shawn Willdene17a9c42014-09-08 13:04:08 -0600198/* This signs the given object using the keymaster key. */
199static int keymaster_sign_object(struct crypt_mnt_ftr *ftr,
Shawn Willden47ba10d2014-09-03 17:07:06 -0600200 const unsigned char *object,
201 const size_t object_size,
202 unsigned char **signature,
203 size_t *signature_size)
204{
205 int rc = 0;
Shawn Willdend1fd8462015-02-24 09:51:34 -0700206 keymaster0_device_t *keymaster_dev = 0;
Shawn Willden47ba10d2014-09-03 17:07:06 -0600207 if (keymaster_init(&keymaster_dev)) {
208 SLOGE("Failed to init keymaster");
209 return -1;
210 }
211
212 /* We currently set the digest type to DIGEST_NONE because it's the
213 * only supported value for keymaster. A similar issue exists with
214 * PADDING_NONE. Long term both of these should likely change.
215 */
216 keymaster_rsa_sign_params_t params;
217 params.digest_type = DIGEST_NONE;
218 params.padding_type = PADDING_NONE;
219
220 unsigned char to_sign[RSA_KEY_SIZE_BYTES];
Shawn Willdene17a9c42014-09-08 13:04:08 -0600221 size_t to_sign_size = sizeof(to_sign);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600222 memset(to_sign, 0, RSA_KEY_SIZE_BYTES);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600223
Shawn Willdene17a9c42014-09-08 13:04:08 -0600224 // To sign a message with RSA, the message must satisfy two
225 // constraints:
226 //
227 // 1. The message, when interpreted as a big-endian numeric value, must
228 // be strictly less than the public modulus of the RSA key. Note
229 // that because the most significant bit of the public modulus is
230 // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit
231 // key), an n-bit message with most significant bit 0 always
232 // satisfies this requirement.
233 //
234 // 2. The message must have the same length in bits as the public
235 // modulus of the RSA key. This requirement isn't mathematically
236 // necessary, but is necessary to ensure consistency in
237 // implementations.
238 switch (ftr->kdf_type) {
239 case KDF_SCRYPT_KEYMASTER_UNPADDED:
240 // This is broken: It produces a message which is shorter than
241 // the public modulus, failing criterion 2.
242 memcpy(to_sign, object, object_size);
243 to_sign_size = object_size;
244 SLOGI("Signing unpadded object");
245 break;
246 case KDF_SCRYPT_KEYMASTER_BADLY_PADDED:
247 // This is broken: Since the value of object is uniformly
248 // distributed, it produces a message that is larger than the
249 // public modulus with probability 0.25.
250 memcpy(to_sign, object, min(RSA_KEY_SIZE_BYTES, object_size));
251 SLOGI("Signing end-padded object");
252 break;
253 case KDF_SCRYPT_KEYMASTER:
254 // This ensures the most significant byte of the signed message
255 // is zero. We could have zero-padded to the left instead, but
256 // this approach is slightly more robust against changes in
257 // object size. However, it's still broken (but not unusably
258 // so) because we really should be using a proper RSA padding
259 // function, such as OAEP.
260 //
261 // TODO(paullawrence): When keymaster 0.4 is available, change
262 // this to use the padding options it provides.
263 memcpy(to_sign + 1, object, min(RSA_KEY_SIZE_BYTES - 1, object_size));
264 SLOGI("Signing safely-padded object");
265 break;
266 default:
267 SLOGE("Unknown KDF type %d", ftr->kdf_type);
268 return -1;
269 }
270
Shawn Willden47ba10d2014-09-03 17:07:06 -0600271 rc = keymaster_dev->sign_data(keymaster_dev,
272 &params,
273 ftr->keymaster_blob,
274 ftr->keymaster_blob_size,
275 to_sign,
Shawn Willdene17a9c42014-09-08 13:04:08 -0600276 to_sign_size,
Shawn Willden47ba10d2014-09-03 17:07:06 -0600277 signature,
278 signature_size);
279
Shawn Willdend1fd8462015-02-24 09:51:34 -0700280 keymaster0_close(keymaster_dev);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600281 return rc;
282}
283
Paul Lawrence399317e2014-03-10 13:20:50 -0700284/* Store password when userdata is successfully decrypted and mounted.
285 * Cleared by cryptfs_clear_password
286 *
287 * To avoid a double prompt at boot, we need to store the CryptKeeper
288 * password and pass it to KeyGuard, which uses it to unlock KeyStore.
289 * Since the entire framework is torn down and rebuilt after encryption,
290 * we have to use a daemon or similar to store the password. Since vold
291 * is secured against IPC except from system processes, it seems a reasonable
292 * place to store this.
293 *
294 * password should be cleared once it has been used.
295 *
296 * password is aged out after password_max_age_seconds seconds.
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800297 */
Paul Lawrence399317e2014-03-10 13:20:50 -0700298static char* password = 0;
299static int password_expiry_time = 0;
300static const int password_max_age_seconds = 60;
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800301
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800302extern struct fstab *fstab;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800303
Paul Lawrence87999172014-02-20 12:21:31 -0800304enum RebootType {reboot, recovery, shutdown};
305static void cryptfs_reboot(enum RebootType rt)
Ken Sumralladfba362013-06-04 16:37:52 -0700306{
Paul Lawrence87999172014-02-20 12:21:31 -0800307 switch(rt) {
308 case reboot:
309 property_set(ANDROID_RB_PROPERTY, "reboot");
310 break;
311
312 case recovery:
313 property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
314 break;
315
316 case shutdown:
317 property_set(ANDROID_RB_PROPERTY, "shutdown");
318 break;
Ken Sumralladfba362013-06-04 16:37:52 -0700319 }
Paul Lawrence87999172014-02-20 12:21:31 -0800320
Ken Sumralladfba362013-06-04 16:37:52 -0700321 sleep(20);
322
323 /* Shouldn't get here, reboot should happen before sleep times out */
324 return;
325}
326
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800327static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
328{
329 memset(io, 0, dataSize);
330 io->data_size = dataSize;
331 io->data_start = sizeof(struct dm_ioctl);
332 io->version[0] = 4;
333 io->version[1] = 0;
334 io->version[2] = 0;
335 io->flags = flags;
336 if (name) {
Marek Pola5e6b9142015-02-05 14:22:34 +0100337 strlcpy(io->name, name, sizeof(io->name));
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800338 }
339}
340
Kenny Rootc4c70f12013-06-14 12:11:38 -0700341/**
342 * Gets the default device scrypt parameters for key derivation time tuning.
343 * The parameters should lead to about one second derivation time for the
344 * given device.
345 */
346static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
347 const int default_params[] = SCRYPT_DEFAULTS;
348 int params[] = SCRYPT_DEFAULTS;
349 char paramstr[PROPERTY_VALUE_MAX];
350 char *token;
351 char *saveptr;
352 int i;
353
354 property_get(SCRYPT_PROP, paramstr, "");
355 if (paramstr[0] != '\0') {
356 /*
357 * The token we're looking for should be three integers separated by
358 * colons (e.g., "12:8:1"). Scan the property to make sure it matches.
359 */
Kenny Root2947e342013-08-14 15:54:49 -0700360 for (i = 0, token = strtok_r(paramstr, ":", &saveptr);
361 token != NULL && i < 3;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700362 i++, token = strtok_r(NULL, ":", &saveptr)) {
363 char *endptr;
364 params[i] = strtol(token, &endptr, 10);
365
366 /*
367 * Check that there was a valid number and it's 8-bit. If not,
368 * break out and the end check will take the default values.
369 */
370 if ((*token == '\0') || (*endptr != '\0') || params[i] < 0 || params[i] > 255) {
371 break;
372 }
373 }
374
375 /*
376 * If there were not enough tokens or a token was malformed (not an
377 * integer), it will end up here and the default parameters can be
378 * taken.
379 */
380 if ((i != 3) || (token != NULL)) {
381 SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
382 memcpy(params, default_params, sizeof(params));
383 }
384 }
385
386 ftr->N_factor = params[0];
387 ftr->r_factor = params[1];
388 ftr->p_factor = params[2];
389}
390
Ken Sumrall3ed82362011-01-28 23:31:16 -0800391static unsigned int get_fs_size(char *dev)
392{
393 int fd, block_size;
394 struct ext4_super_block sb;
395 off64_t len;
396
397 if ((fd = open(dev, O_RDONLY)) < 0) {
398 SLOGE("Cannot open device to get filesystem size ");
399 return 0;
400 }
401
402 if (lseek64(fd, 1024, SEEK_SET) < 0) {
403 SLOGE("Cannot seek to superblock");
404 return 0;
405 }
406
407 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
408 SLOGE("Cannot read superblock");
409 return 0;
410 }
411
412 close(fd);
413
Daniel Rosenberge82df162014-08-15 22:19:23 +0000414 if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) {
415 SLOGE("Not a valid ext4 superblock");
416 return 0;
417 }
Ken Sumrall3ed82362011-01-28 23:31:16 -0800418 block_size = 1024 << sb.s_log_block_size;
419 /* compute length in bytes */
420 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
421
422 /* return length in sectors */
423 return (unsigned int) (len / 512);
424}
425
Ken Sumrall160b4d62013-04-22 12:15:39 -0700426static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
427{
428 static int cached_data = 0;
429 static off64_t cached_off = 0;
430 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
431 int fd;
432 char key_loc[PROPERTY_VALUE_MAX];
433 char real_blkdev[PROPERTY_VALUE_MAX];
434 unsigned int nr_sec;
435 int rc = -1;
436
437 if (!cached_data) {
438 fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
439
440 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
441 if ( (fd = open(real_blkdev, O_RDWR)) < 0) {
442 SLOGE("Cannot open real block device %s\n", real_blkdev);
443 return -1;
444 }
445
446 if ((nr_sec = get_blkdev_size(fd))) {
447 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
448 * encryption info footer and key, and plenty of bytes to spare for future
449 * growth.
450 */
451 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
452 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
453 cached_data = 1;
454 } else {
455 SLOGE("Cannot get size of block device %s\n", real_blkdev);
456 }
457 close(fd);
458 } else {
459 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
460 cached_off = 0;
461 cached_data = 1;
462 }
463 }
464
465 if (cached_data) {
466 if (metadata_fname) {
467 *metadata_fname = cached_metadata_fname;
468 }
469 if (off) {
470 *off = cached_off;
471 }
472 rc = 0;
473 }
474
475 return rc;
476}
477
Ken Sumralle8744072011-01-18 22:01:55 -0800478/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800479 * update the failed mount count but not change the key.
480 */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700481static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800482{
483 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800484 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700485 /* starting_off is set to the SEEK_SET offset
486 * where the crypto structure starts
487 */
488 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800489 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700490 char *fname = NULL;
Ken Sumrall3be890f2011-09-14 16:53:46 -0700491 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800492
Ken Sumrall160b4d62013-04-22 12:15:39 -0700493 if (get_crypt_ftr_info(&fname, &starting_off)) {
494 SLOGE("Unable to get crypt_ftr_info\n");
495 return -1;
496 }
497 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700498 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700499 return -1;
500 }
Ken Sumralle550f782013-08-20 13:48:23 -0700501 if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) {
502 SLOGE("Cannot open footer file %s for put\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700503 return -1;
504 }
505
506 /* Seek to the start of the crypt footer */
507 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
508 SLOGE("Cannot seek to real block device footer\n");
509 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800510 }
511
512 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
513 SLOGE("Cannot write real block device footer\n");
514 goto errout;
515 }
516
Ken Sumrall3be890f2011-09-14 16:53:46 -0700517 fstat(fd, &statbuf);
518 /* If the keys are kept on a raw block device, do not try to truncate it. */
Ken Sumralle550f782013-08-20 13:48:23 -0700519 if (S_ISREG(statbuf.st_mode)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700520 if (ftruncate(fd, 0x4000)) {
Colin Cross59846b62014-02-06 20:34:29 -0800521 SLOGE("Cannot set footer file size\n");
Ken Sumralle8744072011-01-18 22:01:55 -0800522 goto errout;
523 }
524 }
525
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800526 /* Success! */
527 rc = 0;
528
529errout:
530 close(fd);
531 return rc;
532
533}
534
Ken Sumrall160b4d62013-04-22 12:15:39 -0700535static inline int unix_read(int fd, void* buff, int len)
536{
537 return TEMP_FAILURE_RETRY(read(fd, buff, len));
538}
539
540static inline int unix_write(int fd, const void* buff, int len)
541{
542 return TEMP_FAILURE_RETRY(write(fd, buff, len));
543}
544
545static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
546{
547 memset(pdata, 0, len);
548 pdata->persist_magic = PERSIST_DATA_MAGIC;
549 pdata->persist_valid_entries = 0;
550}
551
552/* A routine to update the passed in crypt_ftr to the lastest version.
553 * fd is open read/write on the device that holds the crypto footer and persistent
554 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
555 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
556 */
557static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
558{
Kenny Root7434b312013-06-14 11:29:53 -0700559 int orig_major = crypt_ftr->major_version;
560 int orig_minor = crypt_ftr->minor_version;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700561
Kenny Root7434b312013-06-14 11:29:53 -0700562 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
563 struct crypt_persist_data *pdata;
564 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700565
Kenny Rootc4c70f12013-06-14 12:11:38 -0700566 SLOGW("upgrading crypto footer to 1.1");
567
Kenny Root7434b312013-06-14 11:29:53 -0700568 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
569 if (pdata == NULL) {
570 SLOGE("Cannot allocate persisent data\n");
571 return;
572 }
573 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
574
575 /* Need to initialize the persistent data area */
576 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
577 SLOGE("Cannot seek to persisent data offset\n");
Henrik Baard91064632015-02-05 15:09:17 +0100578 free(pdata);
Kenny Root7434b312013-06-14 11:29:53 -0700579 return;
580 }
581 /* Write all zeros to the first copy, making it invalid */
582 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
583
584 /* Write a valid but empty structure to the second copy */
585 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
586 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
587
588 /* Update the footer */
589 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
590 crypt_ftr->persist_data_offset[0] = pdata_offset;
591 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
592 crypt_ftr->minor_version = 1;
Henrik Baard91064632015-02-05 15:09:17 +0100593 free(pdata);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700594 }
595
Paul Lawrencef4faa572014-01-29 13:31:03 -0800596 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700597 SLOGW("upgrading crypto footer to 1.2");
JP Abgrall7bdfa522013-11-15 13:42:56 -0800598 /* But keep the old kdf_type.
599 * It will get updated later to KDF_SCRYPT after the password has been verified.
600 */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700601 crypt_ftr->kdf_type = KDF_PBKDF2;
602 get_device_scrypt_params(crypt_ftr);
603 crypt_ftr->minor_version = 2;
604 }
605
Paul Lawrencef4faa572014-01-29 13:31:03 -0800606 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
607 SLOGW("upgrading crypto footer to 1.3");
608 crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
609 crypt_ftr->minor_version = 3;
610 }
611
Kenny Root7434b312013-06-14 11:29:53 -0700612 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
613 if (lseek64(fd, offset, SEEK_SET) == -1) {
614 SLOGE("Cannot seek to crypt footer\n");
615 return;
616 }
617 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700618 }
Ken Sumrall160b4d62013-04-22 12:15:39 -0700619}
620
621
622static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800623{
624 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800625 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700626 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800627 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700628 char *fname = NULL;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700629 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800630
Ken Sumrall160b4d62013-04-22 12:15:39 -0700631 if (get_crypt_ftr_info(&fname, &starting_off)) {
632 SLOGE("Unable to get crypt_ftr_info\n");
633 return -1;
634 }
635 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700636 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700637 return -1;
638 }
639 if ( (fd = open(fname, O_RDWR)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700640 SLOGE("Cannot open footer file %s for get\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700641 return -1;
642 }
643
644 /* Make sure it's 16 Kbytes in length */
645 fstat(fd, &statbuf);
646 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
647 SLOGE("footer file %s is not the expected size!\n", fname);
648 goto errout;
649 }
650
651 /* Seek to the start of the crypt footer */
652 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
653 SLOGE("Cannot seek to real block device footer\n");
654 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800655 }
656
657 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
658 SLOGE("Cannot read real block device footer\n");
659 goto errout;
660 }
661
662 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700663 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800664 goto errout;
665 }
666
Kenny Rootc96a5f82013-06-14 12:08:28 -0700667 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
668 SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
669 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800670 goto errout;
671 }
672
Kenny Rootc96a5f82013-06-14 12:08:28 -0700673 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
674 SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
675 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800676 }
677
Ken Sumrall160b4d62013-04-22 12:15:39 -0700678 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
679 * copy on disk before returning.
680 */
Kenny Rootc96a5f82013-06-14 12:08:28 -0700681 if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700682 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
Ken Sumralle8744072011-01-18 22:01:55 -0800683 }
684
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800685 /* Success! */
686 rc = 0;
687
688errout:
689 close(fd);
690 return rc;
691}
692
Ken Sumrall160b4d62013-04-22 12:15:39 -0700693static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
694{
695 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
696 crypt_ftr->persist_data_offset[1]) {
697 SLOGE("Crypt_ftr persist data regions overlap");
698 return -1;
699 }
700
701 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
702 SLOGE("Crypt_ftr persist data region 0 starts after region 1");
703 return -1;
704 }
705
706 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
707 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
708 CRYPT_FOOTER_OFFSET) {
709 SLOGE("Persistent data extends past crypto footer");
710 return -1;
711 }
712
713 return 0;
714}
715
716static int load_persistent_data(void)
717{
718 struct crypt_mnt_ftr crypt_ftr;
719 struct crypt_persist_data *pdata = NULL;
720 char encrypted_state[PROPERTY_VALUE_MAX];
721 char *fname;
722 int found = 0;
723 int fd;
724 int ret;
725 int i;
726
727 if (persist_data) {
728 /* Nothing to do, we've already loaded or initialized it */
729 return 0;
730 }
731
732
733 /* If not encrypted, just allocate an empty table and initialize it */
734 property_get("ro.crypto.state", encrypted_state, "");
735 if (strcmp(encrypted_state, "encrypted") ) {
736 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
737 if (pdata) {
738 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
739 persist_data = pdata;
740 return 0;
741 }
742 return -1;
743 }
744
745 if(get_crypt_ftr_and_key(&crypt_ftr)) {
746 return -1;
747 }
748
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700749 if ((crypt_ftr.major_version < 1)
750 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700751 SLOGE("Crypt_ftr version doesn't support persistent data");
752 return -1;
753 }
754
755 if (get_crypt_ftr_info(&fname, NULL)) {
756 return -1;
757 }
758
759 ret = validate_persistent_data_storage(&crypt_ftr);
760 if (ret) {
761 return -1;
762 }
763
764 fd = open(fname, O_RDONLY);
765 if (fd < 0) {
766 SLOGE("Cannot open %s metadata file", fname);
767 return -1;
768 }
769
770 if (persist_data == NULL) {
771 pdata = malloc(crypt_ftr.persist_data_size);
772 if (pdata == NULL) {
773 SLOGE("Cannot allocate memory for persistent data");
774 goto err;
775 }
776 }
777
778 for (i = 0; i < 2; i++) {
779 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
780 SLOGE("Cannot seek to read persistent data on %s", fname);
781 goto err2;
782 }
783 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
784 SLOGE("Error reading persistent data on iteration %d", i);
785 goto err2;
786 }
787 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
788 found = 1;
789 break;
790 }
791 }
792
793 if (!found) {
794 SLOGI("Could not find valid persistent data, creating");
795 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
796 }
797
798 /* Success */
799 persist_data = pdata;
800 close(fd);
801 return 0;
802
803err2:
804 free(pdata);
805
806err:
807 close(fd);
808 return -1;
809}
810
811static int save_persistent_data(void)
812{
813 struct crypt_mnt_ftr crypt_ftr;
814 struct crypt_persist_data *pdata;
815 char *fname;
816 off64_t write_offset;
817 off64_t erase_offset;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700818 int fd;
819 int ret;
820
821 if (persist_data == NULL) {
822 SLOGE("No persistent data to save");
823 return -1;
824 }
825
826 if(get_crypt_ftr_and_key(&crypt_ftr)) {
827 return -1;
828 }
829
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700830 if ((crypt_ftr.major_version < 1)
831 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700832 SLOGE("Crypt_ftr version doesn't support persistent data");
833 return -1;
834 }
835
836 ret = validate_persistent_data_storage(&crypt_ftr);
837 if (ret) {
838 return -1;
839 }
840
841 if (get_crypt_ftr_info(&fname, NULL)) {
842 return -1;
843 }
844
845 fd = open(fname, O_RDWR);
846 if (fd < 0) {
847 SLOGE("Cannot open %s metadata file", fname);
848 return -1;
849 }
850
851 pdata = malloc(crypt_ftr.persist_data_size);
852 if (pdata == NULL) {
853 SLOGE("Cannot allocate persistant data");
854 goto err;
855 }
856
857 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
858 SLOGE("Cannot seek to read persistent data on %s", fname);
859 goto err2;
860 }
861
862 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
863 SLOGE("Error reading persistent data before save");
864 goto err2;
865 }
866
867 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
868 /* The first copy is the curent valid copy, so write to
869 * the second copy and erase this one */
870 write_offset = crypt_ftr.persist_data_offset[1];
871 erase_offset = crypt_ftr.persist_data_offset[0];
872 } else {
873 /* The second copy must be the valid copy, so write to
874 * the first copy, and erase the second */
875 write_offset = crypt_ftr.persist_data_offset[0];
876 erase_offset = crypt_ftr.persist_data_offset[1];
877 }
878
879 /* Write the new copy first, if successful, then erase the old copy */
Björn Landström96dbee72015-01-20 12:43:56 +0100880 if (lseek64(fd, write_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700881 SLOGE("Cannot seek to write persistent data");
882 goto err2;
883 }
884 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
885 (int) crypt_ftr.persist_data_size) {
Björn Landström96dbee72015-01-20 12:43:56 +0100886 if (lseek64(fd, erase_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700887 SLOGE("Cannot seek to erase previous persistent data");
888 goto err2;
889 }
890 fsync(fd);
891 memset(pdata, 0, crypt_ftr.persist_data_size);
892 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
893 (int) crypt_ftr.persist_data_size) {
894 SLOGE("Cannot write to erase previous persistent data");
895 goto err2;
896 }
897 fsync(fd);
898 } else {
899 SLOGE("Cannot write to save persistent data");
900 goto err2;
901 }
902
903 /* Success */
904 free(pdata);
905 close(fd);
906 return 0;
907
908err2:
909 free(pdata);
910err:
911 close(fd);
912 return -1;
913}
914
Paul Lawrencef4faa572014-01-29 13:31:03 -0800915static int hexdigit (char c)
916{
917 if (c >= '0' && c <= '9') return c - '0';
918 c = tolower(c);
919 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
920 return -1;
921}
922
923static unsigned char* convert_hex_ascii_to_key(const char* master_key_ascii,
924 unsigned int* out_keysize)
925{
926 unsigned int i;
927 *out_keysize = 0;
928
929 size_t size = strlen (master_key_ascii);
930 if (size % 2) {
931 SLOGE("Trying to convert ascii string of odd length");
932 return NULL;
933 }
934
935 unsigned char* master_key = (unsigned char*) malloc(size / 2);
936 if (master_key == 0) {
937 SLOGE("Cannot allocate");
938 return NULL;
939 }
940
941 for (i = 0; i < size; i += 2) {
942 int high_nibble = hexdigit (master_key_ascii[i]);
943 int low_nibble = hexdigit (master_key_ascii[i + 1]);
944
945 if(high_nibble < 0 || low_nibble < 0) {
946 SLOGE("Invalid hex string");
947 free (master_key);
948 return NULL;
949 }
950
951 master_key[*out_keysize] = high_nibble * 16 + low_nibble;
952 (*out_keysize)++;
953 }
954
955 return master_key;
956}
957
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800958/* Convert a binary key of specified length into an ascii hex string equivalent,
959 * without the leading 0x and with null termination
960 */
Paul Lawrencef4faa572014-01-29 13:31:03 -0800961static void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800962 char *master_key_ascii)
963{
964 unsigned int i, a;
965 unsigned char nibble;
966
967 for (i=0, a=0; i<keysize; i++, a+=2) {
968 /* For each byte, write out two ascii hex digits */
969 nibble = (master_key[i] >> 4) & 0xf;
970 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
971
972 nibble = master_key[i] & 0xf;
973 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
974 }
975
976 /* Add the null termination */
977 master_key_ascii[a] = '\0';
978
979}
980
Ken Sumralldb5e0262013-02-05 17:39:48 -0800981static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
982 char *real_blk_name, const char *name, int fd,
983 char *extra_params)
984{
Dan Albertc07fa3f2014-12-18 10:00:55 -0800985 _Alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumralldb5e0262013-02-05 17:39:48 -0800986 struct dm_ioctl *io;
987 struct dm_target_spec *tgt;
988 char *crypt_params;
989 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
990 int i;
991
992 io = (struct dm_ioctl *) buffer;
993
994 /* Load the mapping table for this device */
995 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
996
997 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
998 io->target_count = 1;
999 tgt->status = 0;
1000 tgt->sector_start = 0;
1001 tgt->length = crypt_ftr->fs_size;
Ajay Dudani87701e22014-09-17 21:02:52 -07001002#ifdef CONFIG_HW_DISK_ENCRYPTION
Iliyan Malchevbb7d9af2014-11-20 18:42:23 -08001003 if (!strcmp((char *)crypt_ftr->crypto_type_name, "aes-xts")) {
1004 strlcpy(tgt->target_type, "req-crypt", DM_MAX_TYPE_NAME);
1005 }
1006 else {
1007 strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME);
1008 }
Ajay Dudani87701e22014-09-17 21:02:52 -07001009#else
1010 strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME);
1011#endif
Ken Sumralldb5e0262013-02-05 17:39:48 -08001012
1013 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
1014 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
1015 sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name,
1016 master_key_ascii, real_blk_name, extra_params);
1017 crypt_params += strlen(crypt_params) + 1;
1018 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
1019 tgt->next = crypt_params - buffer;
1020
1021 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
1022 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
1023 break;
1024 }
1025 usleep(500000);
1026 }
1027
1028 if (i == TABLE_LOAD_RETRIES) {
1029 /* We failed to load the table, return an error */
1030 return -1;
1031 } else {
1032 return i + 1;
1033 }
1034}
1035
1036
1037static int get_dm_crypt_version(int fd, const char *name, int *version)
1038{
1039 char buffer[DM_CRYPT_BUF_SIZE];
1040 struct dm_ioctl *io;
1041 struct dm_target_versions *v;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001042
1043 io = (struct dm_ioctl *) buffer;
1044
1045 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1046
1047 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
1048 return -1;
1049 }
1050
1051 /* Iterate over the returned versions, looking for name of "crypt".
1052 * When found, get and return the version.
1053 */
1054 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
1055 while (v->next) {
Ajay Dudani87701e22014-09-17 21:02:52 -07001056#ifdef CONFIG_HW_DISK_ENCRYPTION
Iliyan Malchevbb7d9af2014-11-20 18:42:23 -08001057 if (! strcmp(v->name, "crypt") || ! strcmp(v->name, "req-crypt")) {
Ajay Dudani87701e22014-09-17 21:02:52 -07001058#else
Ken Sumralldb5e0262013-02-05 17:39:48 -08001059 if (! strcmp(v->name, "crypt")) {
Ajay Dudani87701e22014-09-17 21:02:52 -07001060#endif
Ken Sumralldb5e0262013-02-05 17:39:48 -08001061 /* We found the crypt driver, return the version, and get out */
1062 version[0] = v->version[0];
1063 version[1] = v->version[1];
1064 version[2] = v->version[2];
1065 return 0;
1066 }
1067 v = (struct dm_target_versions *)(((char *)v) + v->next);
1068 }
1069
1070 return -1;
1071}
1072
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001073static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
Ken Sumrall29d8da82011-05-18 17:20:07 -07001074 char *real_blk_name, char *crypto_blk_name, const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001075{
1076 char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001077 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001078 unsigned int minor;
Ajay Dudani87701e22014-09-17 21:02:52 -07001079 int fd=0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001080 int retval = -1;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001081 int version[3];
1082 char *extra_params;
1083 int load_count;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001084
1085 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
1086 SLOGE("Cannot open device-mapper\n");
1087 goto errout;
1088 }
1089
1090 io = (struct dm_ioctl *) buffer;
1091
1092 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1093 if (ioctl(fd, DM_DEV_CREATE, io)) {
1094 SLOGE("Cannot create dm-crypt device\n");
1095 goto errout;
1096 }
1097
1098 /* Get the device status, in particular, the name of it's device file */
1099 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1100 if (ioctl(fd, DM_DEV_STATUS, io)) {
1101 SLOGE("Cannot retrieve dm-crypt device status\n");
1102 goto errout;
1103 }
1104 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
1105 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
1106
Ken Sumralldb5e0262013-02-05 17:39:48 -08001107 extra_params = "";
1108 if (! get_dm_crypt_version(fd, name, version)) {
1109 /* Support for allow_discards was added in version 1.11.0 */
1110 if ((version[0] >= 2) ||
1111 ((version[0] == 1) && (version[1] >= 11))) {
1112 extra_params = "1 allow_discards";
1113 SLOGI("Enabling support for allow_discards in dmcrypt.\n");
1114 }
Ken Sumralle919efe2012-09-29 17:07:41 -07001115 }
1116
Ken Sumralldb5e0262013-02-05 17:39:48 -08001117 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name,
1118 fd, extra_params);
1119 if (load_count < 0) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001120 SLOGE("Cannot load dm-crypt mapping table.\n");
1121 goto errout;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001122 } else if (load_count > 1) {
1123 SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001124 }
1125
1126 /* Resume this device to activate it */
Ken Sumralldb5e0262013-02-05 17:39:48 -08001127 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001128
1129 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
1130 SLOGE("Cannot resume the dm-crypt device\n");
1131 goto errout;
1132 }
1133
1134 /* We made it here with no errors. Woot! */
1135 retval = 0;
1136
1137errout:
1138 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1139
1140 return retval;
1141}
1142
Ken Sumrall29d8da82011-05-18 17:20:07 -07001143static int delete_crypto_blk_dev(char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001144{
1145 int fd;
1146 char buffer[DM_CRYPT_BUF_SIZE];
1147 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001148 int retval = -1;
1149
1150 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
1151 SLOGE("Cannot open device-mapper\n");
1152 goto errout;
1153 }
1154
1155 io = (struct dm_ioctl *) buffer;
1156
1157 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1158 if (ioctl(fd, DM_DEV_REMOVE, io)) {
1159 SLOGE("Cannot remove dm-crypt device\n");
1160 goto errout;
1161 }
1162
1163 /* We made it here with no errors. Woot! */
1164 retval = 0;
1165
1166errout:
1167 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1168
1169 return retval;
1170
1171}
1172
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001173static int pbkdf2(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001174 unsigned char *ikey, void *params UNUSED)
1175{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001176 SLOGI("Using pbkdf2 for cryptfs KDF");
1177
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001178 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001179 unsigned int keysize;
1180 char* master_key = (char*)convert_hex_ascii_to_key(passwd, &keysize);
1181 if (!master_key) return -1;
1182 PKCS5_PBKDF2_HMAC_SHA1(master_key, keysize, salt, SALT_LEN,
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001183 HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001184
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001185 memset(master_key, 0, keysize);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001186 free (master_key);
1187 return 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001188}
1189
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001190static int scrypt(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001191 unsigned char *ikey, void *params)
1192{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001193 SLOGI("Using scrypt for cryptfs KDF");
1194
Kenny Rootc4c70f12013-06-14 12:11:38 -07001195 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1196
1197 int N = 1 << ftr->N_factor;
1198 int r = 1 << ftr->r_factor;
1199 int p = 1 << ftr->p_factor;
1200
1201 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001202 unsigned int keysize;
1203 unsigned char* master_key = convert_hex_ascii_to_key(passwd, &keysize);
1204 if (!master_key) return -1;
1205 crypto_scrypt(master_key, keysize, salt, SALT_LEN, N, r, p, ikey,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001206 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001207
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001208 memset(master_key, 0, keysize);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001209 free (master_key);
1210 return 0;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001211}
1212
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001213static int scrypt_keymaster(const char *passwd, const unsigned char *salt,
1214 unsigned char *ikey, void *params)
1215{
1216 SLOGI("Using scrypt with keymaster for cryptfs KDF");
1217
1218 int rc;
1219 unsigned int key_size;
1220 size_t signature_size;
1221 unsigned char* signature;
1222 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1223
1224 int N = 1 << ftr->N_factor;
1225 int r = 1 << ftr->r_factor;
1226 int p = 1 << ftr->p_factor;
1227
1228 unsigned char* master_key = convert_hex_ascii_to_key(passwd, &key_size);
1229 if (!master_key) {
1230 SLOGE("Failed to convert passwd from hex");
1231 return -1;
1232 }
1233
1234 rc = crypto_scrypt(master_key, key_size, salt, SALT_LEN,
1235 N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES);
1236 memset(master_key, 0, key_size);
1237 free(master_key);
1238
1239 if (rc) {
1240 SLOGE("scrypt failed");
1241 return -1;
1242 }
1243
Shawn Willdene17a9c42014-09-08 13:04:08 -06001244 if (keymaster_sign_object(ftr, ikey, KEY_LEN_BYTES + IV_LEN_BYTES,
1245 &signature, &signature_size)) {
1246 SLOGE("Signing failed");
1247 return -1;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001248 }
1249
1250 rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN,
1251 N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES);
1252 free(signature);
1253
1254 if (rc) {
1255 SLOGE("scrypt failed");
1256 return -1;
1257 }
1258
1259 return 0;
1260}
1261
1262static int encrypt_master_key(const char *passwd, const unsigned char *salt,
1263 const unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001264 unsigned char *encrypted_master_key,
1265 struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001266{
1267 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
1268 EVP_CIPHER_CTX e_ctx;
1269 int encrypted_len, final_len;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001270 int rc = 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001271
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001272 /* Turn the password into an intermediate key and IV that can decrypt the master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001273 get_device_scrypt_params(crypt_ftr);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001274
1275 switch (crypt_ftr->kdf_type) {
Shawn Willdene17a9c42014-09-08 13:04:08 -06001276 case KDF_SCRYPT_KEYMASTER_UNPADDED:
1277 case KDF_SCRYPT_KEYMASTER_BADLY_PADDED:
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001278 case KDF_SCRYPT_KEYMASTER:
1279 if (keymaster_create_key(crypt_ftr)) {
1280 SLOGE("keymaster_create_key failed");
1281 return -1;
1282 }
1283
1284 if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) {
1285 SLOGE("scrypt failed");
1286 return -1;
1287 }
1288 break;
1289
1290 case KDF_SCRYPT:
1291 if (scrypt(passwd, salt, ikey, crypt_ftr)) {
1292 SLOGE("scrypt failed");
1293 return -1;
1294 }
1295 break;
1296
1297 default:
1298 SLOGE("Invalid kdf_type");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001299 return -1;
1300 }
Kenny Rootc4c70f12013-06-14 12:11:38 -07001301
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001302 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001303 EVP_CIPHER_CTX_init(&e_ctx);
1304 if (! EVP_EncryptInit_ex(&e_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001305 SLOGE("EVP_EncryptInit failed\n");
1306 return -1;
1307 }
1308 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001309
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001310 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001311 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
1312 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001313 SLOGE("EVP_EncryptUpdate failed\n");
1314 return -1;
1315 }
Adam Langley889c4f12014-09-03 14:23:13 -07001316 if (! EVP_EncryptFinal_ex(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001317 SLOGE("EVP_EncryptFinal failed\n");
1318 return -1;
1319 }
1320
1321 if (encrypted_len + final_len != KEY_LEN_BYTES) {
1322 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
1323 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001324 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001325
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001326 /* Store the scrypt of the intermediate key, so we can validate if it's a
1327 password error or mount error when things go wrong.
1328 Note there's no need to check for errors, since if this is incorrect, we
1329 simply won't wipe userdata, which is the correct default behavior
1330 */
1331 int N = 1 << crypt_ftr->N_factor;
1332 int r = 1 << crypt_ftr->r_factor;
1333 int p = 1 << crypt_ftr->p_factor;
1334
1335 rc = crypto_scrypt(ikey, KEY_LEN_BYTES,
1336 crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p,
1337 crypt_ftr->scrypted_intermediate_key,
1338 sizeof(crypt_ftr->scrypted_intermediate_key));
1339
1340 if (rc) {
1341 SLOGE("encrypt_master_key: crypto_scrypt failed");
1342 }
1343
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001344 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001345}
1346
JP Abgrall7bdfa522013-11-15 13:42:56 -08001347static int decrypt_master_key_aux(char *passwd, unsigned char *salt,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001348 unsigned char *encrypted_master_key,
1349 unsigned char *decrypted_master_key,
1350 kdf_func kdf, void *kdf_params,
1351 unsigned char** intermediate_key,
1352 size_t* intermediate_key_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001353{
1354 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 -08001355 EVP_CIPHER_CTX d_ctx;
1356 int decrypted_len, final_len;
1357
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001358 /* Turn the password into an intermediate key and IV that can decrypt the
1359 master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001360 if (kdf(passwd, salt, ikey, kdf_params)) {
1361 SLOGE("kdf failed");
1362 return -1;
1363 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001364
1365 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001366 EVP_CIPHER_CTX_init(&d_ctx);
1367 if (! EVP_DecryptInit_ex(&d_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001368 return -1;
1369 }
1370 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1371 /* Decrypt the master key */
1372 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
1373 encrypted_master_key, KEY_LEN_BYTES)) {
1374 return -1;
1375 }
Adam Langley889c4f12014-09-03 14:23:13 -07001376 if (! EVP_DecryptFinal_ex(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001377 return -1;
1378 }
1379
1380 if (decrypted_len + final_len != KEY_LEN_BYTES) {
1381 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001382 }
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001383
1384 /* Copy intermediate key if needed by params */
1385 if (intermediate_key && intermediate_key_size) {
1386 *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES);
1387 if (intermediate_key) {
1388 memcpy(*intermediate_key, ikey, KEY_LEN_BYTES);
1389 *intermediate_key_size = KEY_LEN_BYTES;
1390 }
1391 }
1392
1393 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001394}
1395
Kenny Rootc4c70f12013-06-14 12:11:38 -07001396static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001397{
Shawn Willdene17a9c42014-09-08 13:04:08 -06001398 if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER_UNPADDED ||
1399 ftr->kdf_type == KDF_SCRYPT_KEYMASTER_BADLY_PADDED ||
1400 ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001401 *kdf = scrypt_keymaster;
1402 *kdf_params = ftr;
1403 } else if (ftr->kdf_type == KDF_SCRYPT) {
Kenny Rootc4c70f12013-06-14 12:11:38 -07001404 *kdf = scrypt;
1405 *kdf_params = ftr;
1406 } else {
1407 *kdf = pbkdf2;
1408 *kdf_params = NULL;
1409 }
1410}
1411
JP Abgrall7bdfa522013-11-15 13:42:56 -08001412static int decrypt_master_key(char *passwd, unsigned char *decrypted_master_key,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001413 struct crypt_mnt_ftr *crypt_ftr,
1414 unsigned char** intermediate_key,
1415 size_t* intermediate_key_size)
Kenny Rootc4c70f12013-06-14 12:11:38 -07001416{
1417 kdf_func kdf;
1418 void *kdf_params;
1419 int ret;
1420
1421 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001422 ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key,
1423 decrypted_master_key, kdf, kdf_params,
1424 intermediate_key, intermediate_key_size);
Kenny Rootc4c70f12013-06-14 12:11:38 -07001425 if (ret != 0) {
1426 SLOGW("failure decrypting master key");
Kenny Rootc4c70f12013-06-14 12:11:38 -07001427 }
1428
1429 return ret;
1430}
1431
1432static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt,
1433 struct crypt_mnt_ftr *crypt_ftr) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001434 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -08001435 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001436
1437 /* Get some random bits for a key */
1438 fd = open("/dev/urandom", O_RDONLY);
Ken Sumralle8744072011-01-18 22:01:55 -08001439 read(fd, key_buf, sizeof(key_buf));
1440 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001441 close(fd);
1442
1443 /* Now encrypt it with the password */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001444 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001445}
1446
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001447static int wait_and_unmount(char *mountpoint, bool kill)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001448{
Greg Hackmann955653e2014-09-24 14:55:20 -07001449 int i, err, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001450#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001451
1452 /* Now umount the tmpfs filesystem */
1453 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001454 if (umount(mountpoint) == 0) {
1455 break;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001456 }
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001457
1458 if (errno == EINVAL) {
1459 /* EINVAL is returned if the directory is not a mountpoint,
1460 * i.e. there is no filesystem mounted there. So just get out.
1461 */
1462 break;
1463 }
1464
1465 err = errno;
1466
1467 /* If allowed, be increasingly aggressive before the last two retries */
1468 if (kill) {
1469 if (i == (WAIT_UNMOUNT_COUNT - 3)) {
1470 SLOGW("sending SIGHUP to processes with open files\n");
1471 vold_killProcessesWithOpenFiles(mountpoint, 1);
1472 } else if (i == (WAIT_UNMOUNT_COUNT - 2)) {
1473 SLOGW("sending SIGKILL to processes with open files\n");
1474 vold_killProcessesWithOpenFiles(mountpoint, 2);
1475 }
1476 }
1477
1478 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001479 }
1480
1481 if (i < WAIT_UNMOUNT_COUNT) {
1482 SLOGD("unmounting %s succeeded\n", mountpoint);
1483 rc = 0;
1484 } else {
jessica_yu3f14fe42014-09-22 15:57:40 +08001485 vold_killProcessesWithOpenFiles(mountpoint, 0);
Greg Hackmann955653e2014-09-24 14:55:20 -07001486 SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001487 rc = -1;
1488 }
1489
1490 return rc;
1491}
1492
Ken Sumrallc5872692013-05-14 15:26:31 -07001493#define DATA_PREP_TIMEOUT 200
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001494static int prep_data_fs(void)
1495{
1496 int i;
1497
1498 /* Do the prep of the /data filesystem */
1499 property_set("vold.post_fs_data_done", "0");
1500 property_set("vold.decrypt", "trigger_post_fs_data");
1501 SLOGD("Just triggered post_fs_data\n");
1502
Ken Sumrallc5872692013-05-14 15:26:31 -07001503 /* Wait a max of 50 seconds, hopefully it takes much less */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001504 for (i=0; i<DATA_PREP_TIMEOUT; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001505 char p[PROPERTY_VALUE_MAX];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001506
1507 property_get("vold.post_fs_data_done", p, "0");
1508 if (*p == '1') {
1509 break;
1510 } else {
1511 usleep(250000);
1512 }
1513 }
1514 if (i == DATA_PREP_TIMEOUT) {
1515 /* Ugh, we failed to prep /data in time. Bail. */
Ken Sumrallc5872692013-05-14 15:26:31 -07001516 SLOGE("post_fs_data timed out!\n");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001517 return -1;
1518 } else {
1519 SLOGD("post_fs_data done\n");
1520 return 0;
1521 }
1522}
1523
Paul Lawrence74f29f12014-08-28 15:54:10 -07001524static void cryptfs_set_corrupt()
1525{
1526 // Mark the footer as bad
1527 struct crypt_mnt_ftr crypt_ftr;
1528 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1529 SLOGE("Failed to get crypto footer - panic");
1530 return;
1531 }
1532
1533 crypt_ftr.flags |= CRYPT_DATA_CORRUPT;
1534 if (put_crypt_ftr_and_key(&crypt_ftr)) {
1535 SLOGE("Failed to set crypto footer - panic");
1536 return;
1537 }
1538}
1539
1540static void cryptfs_trigger_restart_min_framework()
1541{
1542 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
1543 SLOGE("Failed to mount tmpfs on data - panic");
1544 return;
1545 }
1546
1547 if (property_set("vold.decrypt", "trigger_post_fs_data")) {
1548 SLOGE("Failed to trigger post fs data - panic");
1549 return;
1550 }
1551
1552 if (property_set("vold.decrypt", "trigger_restart_min_framework")) {
1553 SLOGE("Failed to trigger restart min framework - panic");
1554 return;
1555 }
1556}
1557
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001558/* returns < 0 on failure */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001559static int cryptfs_restart_internal(int restart_main)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001560{
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001561 char crypto_blkdev[MAXPATHLEN];
Tim Murray8439dc92014-12-15 11:56:11 -08001562 int rc = -1;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001563 static int restart_successful = 0;
1564
1565 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -06001566 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001567 SLOGE("Encrypted filesystem not validated, aborting");
1568 return -1;
1569 }
1570
1571 if (restart_successful) {
1572 SLOGE("System already restarted with encrypted disk, aborting");
1573 return -1;
1574 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001575
Paul Lawrencef4faa572014-01-29 13:31:03 -08001576 if (restart_main) {
1577 /* Here is where we shut down the framework. The init scripts
1578 * start all services in one of three classes: core, main or late_start.
1579 * On boot, we start core and main. Now, we stop main, but not core,
1580 * as core includes vold and a few other really important things that
1581 * we need to keep running. Once main has stopped, we should be able
1582 * to umount the tmpfs /data, then mount the encrypted /data.
1583 * We then restart the class main, and also the class late_start.
1584 * At the moment, I've only put a few things in late_start that I know
1585 * are not needed to bring up the framework, and that also cause problems
1586 * with unmounting the tmpfs /data, but I hope to add add more services
1587 * to the late_start class as we optimize this to decrease the delay
1588 * till the user is asked for the password to the filesystem.
1589 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001590
Paul Lawrencef4faa572014-01-29 13:31:03 -08001591 /* The init files are setup to stop the class main when vold.decrypt is
1592 * set to trigger_reset_main.
1593 */
1594 property_set("vold.decrypt", "trigger_reset_main");
1595 SLOGD("Just asked init to shut down class main\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001596
Paul Lawrencef4faa572014-01-29 13:31:03 -08001597 /* Ugh, shutting down the framework is not synchronous, so until it
1598 * can be fixed, this horrible hack will wait a moment for it all to
1599 * shut down before proceeding. Without it, some devices cannot
1600 * restart the graphics services.
1601 */
1602 sleep(2);
1603 }
Ken Sumrall9dedfd42012-10-09 14:16:59 -07001604
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001605 /* Now that the framework is shutdown, we should be able to umount()
1606 * the tmpfs filesystem, and mount the real one.
1607 */
1608
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001609 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1610 if (strlen(crypto_blkdev) == 0) {
1611 SLOGE("fs_crypto_blkdev not set\n");
1612 return -1;
1613 }
1614
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001615 if (! (rc = wait_and_unmount(DATA_MNT_POINT, true)) ) {
Doug Zongker6fd57712013-12-17 09:43:23 -08001616 /* If ro.crypto.readonly is set to 1, mount the decrypted
1617 * filesystem readonly. This is used when /data is mounted by
1618 * recovery mode.
1619 */
1620 char ro_prop[PROPERTY_VALUE_MAX];
1621 property_get("ro.crypto.readonly", ro_prop, "");
1622 if (strlen(ro_prop) > 0 && atoi(ro_prop)) {
1623 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
1624 rec->flags |= MS_RDONLY;
1625 }
JP Abgrall62c7af32014-06-16 13:01:23 -07001626
Ken Sumralle5032c42012-04-01 23:58:44 -07001627 /* If that succeeded, then mount the decrypted filesystem */
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001628 int retries = RETRY_MOUNT_ATTEMPTS;
1629 int mount_rc;
1630 while ((mount_rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT,
1631 crypto_blkdev, 0))
1632 != 0) {
1633 if (mount_rc == FS_MGR_DOMNT_BUSY) {
1634 /* TODO: invoke something similar to
1635 Process::killProcessWithOpenFiles(DATA_MNT_POINT,
1636 retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */
1637 SLOGI("Failed to mount %s because it is busy - waiting",
1638 crypto_blkdev);
1639 if (--retries) {
1640 sleep(RETRY_MOUNT_DELAY_SECONDS);
1641 } else {
1642 /* Let's hope that a reboot clears away whatever is keeping
1643 the mount busy */
1644 cryptfs_reboot(reboot);
1645 }
1646 } else {
1647 SLOGE("Failed to mount decrypted data");
1648 cryptfs_set_corrupt();
1649 cryptfs_trigger_restart_min_framework();
1650 SLOGI("Started framework to offer wipe");
1651 return -1;
1652 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001653 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001654
Ken Sumralle5032c42012-04-01 23:58:44 -07001655 property_set("vold.decrypt", "trigger_load_persist_props");
1656 /* Create necessary paths on /data */
1657 if (prep_data_fs()) {
1658 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001659 }
Ken Sumralle5032c42012-04-01 23:58:44 -07001660
1661 /* startup service classes main and late_start */
1662 property_set("vold.decrypt", "trigger_restart_framework");
1663 SLOGD("Just triggered restart_framework\n");
1664
1665 /* Give it a few moments to get started */
1666 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001667 }
1668
Ken Sumrall0cc16632011-01-18 20:32:26 -08001669 if (rc == 0) {
1670 restart_successful = 1;
1671 }
1672
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001673 return rc;
1674}
1675
Paul Lawrencef4faa572014-01-29 13:31:03 -08001676int cryptfs_restart(void)
1677{
Paul Lawrence8175a0b2015-03-05 09:46:23 -08001678 SLOGI("cryptfs_restart");
1679 if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
1680 struct fstab_rec* rec;
1681 int rc;
1682
1683 if (e4crypt_restart(DATA_MNT_POINT)) {
1684 SLOGE("Can't unmount e4crypt temp volume\n");
1685 return -1;
1686 }
1687
1688 rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
1689 if (!rec) {
1690 SLOGE("Can't get fstab record for %s\n", DATA_MNT_POINT);
1691 return -1;
1692 }
1693
1694 rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT, rec->blk_device, 0);
1695 if (rc) {
1696 SLOGE("Can't mount %s\n", DATA_MNT_POINT);
1697 return rc;
1698 }
1699
1700 property_set("vold.decrypt", "trigger_restart_framework");
1701 return 0;
1702 }
1703
Paul Lawrencef4faa572014-01-29 13:31:03 -08001704 /* Call internal implementation forcing a restart of main service group */
1705 return cryptfs_restart_internal(1);
1706}
1707
Paul Lawrence8175a0b2015-03-05 09:46:23 -08001708static int do_crypto_complete(char *mount_point)
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001709{
1710 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001711 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -08001712 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001713
1714 property_get("ro.crypto.state", encrypted_state, "");
1715 if (strcmp(encrypted_state, "encrypted") ) {
1716 SLOGE("not running with encryption, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001717 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001718 }
1719
Paul Lawrence8175a0b2015-03-05 09:46:23 -08001720 if (e4crypt_crypto_complete(mount_point) == 0) {
1721 return CRYPTO_COMPLETE_ENCRYPTED;
1722 }
1723
Ken Sumrall160b4d62013-04-22 12:15:39 -07001724 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001725 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumralle5032c42012-04-01 23:58:44 -07001726
Ken Sumralle1a45852011-12-14 21:24:27 -08001727 /*
1728 * Only report this error if key_loc is a file and it exists.
1729 * If the device was never encrypted, and /data is not mountable for
1730 * some reason, returning 1 should prevent the UI from presenting the
1731 * a "enter password" screen, or worse, a "press button to wipe the
1732 * device" screen.
1733 */
1734 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
1735 SLOGE("master key file does not exist, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001736 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumralle1a45852011-12-14 21:24:27 -08001737 } else {
1738 SLOGE("Error getting crypt footer and key\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001739 return CRYPTO_COMPLETE_BAD_METADATA;
Ken Sumralle1a45852011-12-14 21:24:27 -08001740 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001741 }
1742
Paul Lawrence74f29f12014-08-28 15:54:10 -07001743 // Test for possible error flags
1744 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS){
1745 SLOGE("Encryption process is partway completed\n");
1746 return CRYPTO_COMPLETE_PARTIAL;
1747 }
1748
1749 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE){
1750 SLOGE("Encryption process was interrupted but cannot continue\n");
1751 return CRYPTO_COMPLETE_INCONSISTENT;
1752 }
1753
1754 if (crypt_ftr.flags & CRYPT_DATA_CORRUPT){
1755 SLOGE("Encryption is successful but data is corrupt\n");
1756 return CRYPTO_COMPLETE_CORRUPT;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001757 }
1758
1759 /* We passed the test! We shall diminish, and return to the west */
Paul Lawrence74f29f12014-08-28 15:54:10 -07001760 return CRYPTO_COMPLETE_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001761}
1762
Paul Lawrencef4faa572014-01-29 13:31:03 -08001763static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
1764 char *passwd, char *mount_point, char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001765{
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001766 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001767 unsigned char decrypted_master_key[32];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001768 char crypto_blkdev[MAXPATHLEN];
1769 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001770 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001771 unsigned int orig_failed_decrypt_count;
1772 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001773 int use_keymaster = 0;
1774 int upgrade = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001775 unsigned char* intermediate_key = 0;
1776 size_t intermediate_key_size = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001777
Paul Lawrencef4faa572014-01-29 13:31:03 -08001778 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1779 orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001780
Paul Lawrencef4faa572014-01-29 13:31:03 -08001781 if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001782 if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr,
1783 &intermediate_key, &intermediate_key_size)) {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001784 SLOGE("Failed to decrypt master key\n");
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001785 rc = -1;
1786 goto errout;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001787 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001788 }
1789
Paul Lawrencef4faa572014-01-29 13:31:03 -08001790 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
1791
Ajay Dudani87701e22014-09-17 21:02:52 -07001792#ifdef CONFIG_HW_DISK_ENCRYPTION
Iliyan Malchevbb7d9af2014-11-20 18:42:23 -08001793 if (!strcmp((char *)crypt_ftr->crypto_type_name, "aes-xts")) {
1794 if(!set_hw_device_encryption_key(passwd, (char*) crypt_ftr->crypto_type_name)) {
1795 SLOGE("Hardware encryption key does not match");
1796 }
Ajay Dudani87701e22014-09-17 21:02:52 -07001797 }
1798#endif
1799
Paul Lawrence74f29f12014-08-28 15:54:10 -07001800 // Create crypto block device - all (non fatal) code paths
1801 // need it
Paul Lawrencef4faa572014-01-29 13:31:03 -08001802 if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key,
1803 real_blkdev, crypto_blkdev, label)) {
Paul Lawrence74f29f12014-08-28 15:54:10 -07001804 SLOGE("Error creating decrypted block device\n");
1805 rc = -1;
1806 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001807 }
1808
Paul Lawrence74f29f12014-08-28 15:54:10 -07001809 /* Work out if the problem is the password or the data */
1810 unsigned char scrypted_intermediate_key[sizeof(crypt_ftr->
1811 scrypted_intermediate_key)];
1812 int N = 1 << crypt_ftr->N_factor;
1813 int r = 1 << crypt_ftr->r_factor;
1814 int p = 1 << crypt_ftr->p_factor;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001815
Paul Lawrence74f29f12014-08-28 15:54:10 -07001816 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
1817 crypt_ftr->salt, sizeof(crypt_ftr->salt),
1818 N, r, p, scrypted_intermediate_key,
1819 sizeof(scrypted_intermediate_key));
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001820
Paul Lawrence74f29f12014-08-28 15:54:10 -07001821 // Does the key match the crypto footer?
1822 if (rc == 0 && memcmp(scrypted_intermediate_key,
1823 crypt_ftr->scrypted_intermediate_key,
1824 sizeof(scrypted_intermediate_key)) == 0) {
1825 SLOGI("Password matches");
1826 rc = 0;
1827 } else {
1828 /* Try mounting the file system anyway, just in case the problem's with
1829 * the footer, not the key. */
1830 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
1831 mkdir(tmp_mount_point, 0755);
1832 if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
1833 SLOGE("Error temp mounting decrypted block device\n");
1834 delete_crypto_blk_dev(label);
1835
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001836 rc = ++crypt_ftr->failed_decrypt_count;
1837 put_crypt_ftr_and_key(crypt_ftr);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001838 } else {
1839 /* Success! */
1840 SLOGI("Password did not match but decrypted drive mounted - continue");
1841 umount(tmp_mount_point);
1842 rc = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001843 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001844 }
1845
1846 if (rc == 0) {
1847 crypt_ftr->failed_decrypt_count = 0;
Paul Lawrence72b8b822014-10-05 12:57:37 -07001848 if (orig_failed_decrypt_count != 0) {
1849 put_crypt_ftr_and_key(crypt_ftr);
1850 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001851
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001852 /* Save the name of the crypto block device
Paul Lawrence74f29f12014-08-28 15:54:10 -07001853 * so we can mount it when restarting the framework. */
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001854 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -06001855
1856 /* Also save a the master key so we can reencrypted the key
Paul Lawrence74f29f12014-08-28 15:54:10 -07001857 * the key when we want to change the password on it. */
Jason parks70a4b3f2011-01-28 10:10:47 -06001858 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001859 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -06001860 master_key_saved = 1;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001861 SLOGD("%s(): Master key saved\n", __FUNCTION__);
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001862 rc = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001863
Paul Lawrence74f29f12014-08-28 15:54:10 -07001864 // Upgrade if we're not using the latest KDF.
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001865 use_keymaster = keymaster_check_compatibility();
1866 if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Shawn Willden47ba10d2014-09-03 17:07:06 -06001867 // Don't allow downgrade
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001868 } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) {
1869 crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1870 upgrade = 1;
1871 } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001872 crypt_ftr->kdf_type = KDF_SCRYPT;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001873 upgrade = 1;
1874 }
1875
1876 if (upgrade) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001877 rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1878 crypt_ftr->master_key, crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001879 if (!rc) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001880 rc = put_crypt_ftr_and_key(crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001881 }
1882 SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
Paul Lawrenceb2f682b2014-09-08 11:28:19 -07001883
1884 // Do not fail even if upgrade failed - machine is bootable
1885 // Note that if this code is ever hit, there is a *serious* problem
1886 // since KDFs should never fail. You *must* fix the kdf before
1887 // proceeding!
1888 if (rc) {
1889 SLOGW("Upgrade failed with error %d,"
1890 " but continuing with previous state",
1891 rc);
1892 rc = 0;
1893 }
JP Abgrall7bdfa522013-11-15 13:42:56 -08001894 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001895 }
1896
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001897 errout:
1898 if (intermediate_key) {
1899 memset(intermediate_key, 0, intermediate_key_size);
1900 free(intermediate_key);
1901 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001902 return rc;
1903}
1904
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001905/* Called by vold when it wants to undo the crypto mapping of a volume it
1906 * manages. This is usually in response to a factory reset, when we want
1907 * to undo the crypto mapping so the volume is formatted in the clear.
1908 */
1909int cryptfs_revert_volume(const char *label)
1910{
1911 return delete_crypto_blk_dev((char *)label);
1912}
1913
Ken Sumrall29d8da82011-05-18 17:20:07 -07001914/*
1915 * Called by vold when it's asked to mount an encrypted, nonremovable volume.
1916 * Setup a dm-crypt mapping, use the saved master key from
1917 * setting up the /data mapping, and return the new device path.
1918 */
1919int cryptfs_setup_volume(const char *label, int major, int minor,
1920 char *crypto_sys_path, unsigned int max_path,
1921 int *new_major, int *new_minor)
1922{
1923 char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN];
1924 struct crypt_mnt_ftr sd_crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001925 struct stat statbuf;
Tim Murray8439dc92014-12-15 11:56:11 -08001926 unsigned int nr_sec;
1927 int fd;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001928
1929 sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor);
1930
Ken Sumrall160b4d62013-04-22 12:15:39 -07001931 get_crypt_ftr_and_key(&sd_crypt_ftr);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001932
1933 /* Update the fs_size field to be the size of the volume */
1934 fd = open(real_blkdev, O_RDONLY);
1935 nr_sec = get_blkdev_size(fd);
1936 close(fd);
1937 if (nr_sec == 0) {
1938 SLOGE("Cannot get size of volume %s\n", real_blkdev);
1939 return -1;
1940 }
1941
1942 sd_crypt_ftr.fs_size = nr_sec;
1943 create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev,
1944 crypto_blkdev, label);
1945
JP Abgrall3334c6a2014-10-10 15:52:11 -07001946 if (stat(crypto_blkdev, &statbuf) < 0) {
1947 SLOGE("Error get stat for crypto_blkdev %s. err=%d(%s)\n",
1948 crypto_blkdev, errno, strerror(errno));
1949 }
Ken Sumrall29d8da82011-05-18 17:20:07 -07001950 *new_major = MAJOR(statbuf.st_rdev);
1951 *new_minor = MINOR(statbuf.st_rdev);
1952
1953 /* Create path to sys entry for this block device */
1954 snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1);
1955
1956 return 0;
1957}
1958
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001959int cryptfs_crypto_complete(void)
1960{
1961 return do_crypto_complete("/data");
1962}
1963
Paul Lawrencef4faa572014-01-29 13:31:03 -08001964int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr)
1965{
1966 char encrypted_state[PROPERTY_VALUE_MAX];
1967 property_get("ro.crypto.state", encrypted_state, "");
1968 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
1969 SLOGE("encrypted fs already validated or not running with encryption,"
1970 " aborting");
1971 return -1;
1972 }
1973
1974 if (get_crypt_ftr_and_key(crypt_ftr)) {
1975 SLOGE("Error getting crypt footer and key");
1976 return -1;
1977 }
1978
1979 return 0;
1980}
1981
Paul Lawrencefc615042014-10-04 15:32:29 -07001982/*
1983 * TODO - transition patterns to new format in calling code
1984 * and remove this vile hack, and the use of hex in
1985 * the password passing code.
1986 *
1987 * Patterns are passed in zero based (i.e. the top left dot
1988 * is represented by zero, the top middle one etc), but we want
1989 * to store them '1' based.
1990 * This is to allow us to migrate the calling code to use this
1991 * convention. It also solves a nasty problem whereby scrypt ignores
1992 * trailing zeros, so patterns ending at the top left could be
1993 * truncated, and similarly, you could add the top left to any
1994 * pattern and still match.
1995 * adjust_passwd is a hack function that returns the alternate representation
1996 * if the password appears to be a pattern (hex numbers all less than 09)
1997 * If it succeeds we need to try both, and in particular try the alternate
1998 * first. If the original matches, then we need to update the footer
1999 * with the alternate.
2000 * All code that accepts passwords must adjust them first. Since
2001 * cryptfs_check_passwd is always the first function called after a migration
2002 * (and indeed on any boot) we only need to do the double try in this
2003 * function.
2004 */
2005char* adjust_passwd(const char* passwd)
2006{
2007 size_t index, length;
2008
2009 if (!passwd) {
2010 return 0;
2011 }
2012
2013 // Check even length. Hex encoded passwords are always
2014 // an even length, since each character encodes to two characters.
2015 length = strlen(passwd);
2016 if (length % 2) {
2017 SLOGW("Password not correctly hex encoded.");
2018 return 0;
2019 }
2020
2021 // Check password is old-style pattern - a collection of hex
2022 // encoded bytes less than 9 (00 through 08)
2023 for (index = 0; index < length; index +=2) {
2024 if (passwd[index] != '0'
2025 || passwd[index + 1] < '0' || passwd[index + 1] > '8') {
2026 return 0;
2027 }
2028 }
2029
2030 // Allocate room for adjusted passwd and null terminate
2031 char* adjusted = malloc(length + 1);
2032 adjusted[length] = 0;
2033
2034 // Add 0x31 ('1') to each character
2035 for (index = 0; index < length; index += 2) {
2036 // output is 31 through 39 so set first byte to three, second to src + 1
2037 adjusted[index] = '3';
2038 adjusted[index + 1] = passwd[index + 1] + 1;
2039 }
2040
2041 return adjusted;
2042}
2043
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002044int cryptfs_check_passwd(char *passwd)
2045{
Paul Lawrence8175a0b2015-03-05 09:46:23 -08002046 SLOGI("cryptfs_check_passwd");
2047 if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
2048 return e4crypt_check_passwd(DATA_MNT_POINT, passwd);
2049 }
2050
Paul Lawrencef4faa572014-01-29 13:31:03 -08002051 struct crypt_mnt_ftr crypt_ftr;
2052 int rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002053
Paul Lawrencef4faa572014-01-29 13:31:03 -08002054 rc = check_unmounted_and_get_ftr(&crypt_ftr);
2055 if (rc)
2056 return rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002057
Paul Lawrencefc615042014-10-04 15:32:29 -07002058 char* adjusted_passwd = adjust_passwd(passwd);
2059 if (adjusted_passwd) {
2060 int failed_decrypt_count = crypt_ftr.failed_decrypt_count;
2061 rc = test_mount_encrypted_fs(&crypt_ftr, adjusted_passwd,
2062 DATA_MNT_POINT, "userdata");
2063
2064 // Maybe the original one still works?
2065 if (rc) {
2066 // Don't double count this failure
2067 crypt_ftr.failed_decrypt_count = failed_decrypt_count;
2068 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
2069 DATA_MNT_POINT, "userdata");
2070 if (!rc) {
2071 // cryptfs_changepw also adjusts so pass original
2072 // Note that adjust_passwd only recognises patterns
2073 // so we can safely use CRYPT_TYPE_PATTERN
2074 SLOGI("Updating pattern to new format");
2075 cryptfs_changepw(CRYPT_TYPE_PATTERN, passwd);
2076 }
2077 }
2078 free(adjusted_passwd);
2079 } else {
2080 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
2081 DATA_MNT_POINT, "userdata");
2082 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002083
2084 if (rc == 0 && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
Paul Lawrence399317e2014-03-10 13:20:50 -07002085 cryptfs_clear_password();
2086 password = strdup(passwd);
2087 struct timespec now;
2088 clock_gettime(CLOCK_BOOTTIME, &now);
2089 password_expiry_time = now.tv_sec + password_max_age_seconds;
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002090 }
2091
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002092 return rc;
2093}
2094
Ken Sumrall3ad90722011-10-04 20:38:29 -07002095int cryptfs_verify_passwd(char *passwd)
2096{
2097 struct crypt_mnt_ftr crypt_ftr;
2098 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002099 unsigned char decrypted_master_key[32];
Ken Sumrall3ad90722011-10-04 20:38:29 -07002100 char encrypted_state[PROPERTY_VALUE_MAX];
2101 int rc;
2102
2103 property_get("ro.crypto.state", encrypted_state, "");
2104 if (strcmp(encrypted_state, "encrypted") ) {
2105 SLOGE("device not encrypted, aborting");
2106 return -2;
2107 }
2108
2109 if (!master_key_saved) {
2110 SLOGE("encrypted fs not yet mounted, aborting");
2111 return -1;
2112 }
2113
2114 if (!saved_mount_point) {
2115 SLOGE("encrypted fs failed to save mount point, aborting");
2116 return -1;
2117 }
2118
Ken Sumrall160b4d62013-04-22 12:15:39 -07002119 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall3ad90722011-10-04 20:38:29 -07002120 SLOGE("Error getting crypt footer and key\n");
2121 return -1;
2122 }
2123
2124 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
2125 /* If the device has no password, then just say the password is valid */
2126 rc = 0;
2127 } else {
Paul Lawrencefc615042014-10-04 15:32:29 -07002128 char* adjusted_passwd = adjust_passwd(passwd);
2129 if (adjusted_passwd) {
2130 passwd = adjusted_passwd;
2131 }
2132
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002133 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall3ad90722011-10-04 20:38:29 -07002134 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
2135 /* They match, the password is correct */
2136 rc = 0;
2137 } else {
2138 /* If incorrect, sleep for a bit to prevent dictionary attacks */
2139 sleep(1);
2140 rc = 1;
2141 }
Paul Lawrencefc615042014-10-04 15:32:29 -07002142
2143 free(adjusted_passwd);
Ken Sumrall3ad90722011-10-04 20:38:29 -07002144 }
2145
2146 return rc;
2147}
2148
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002149/* Initialize a crypt_mnt_ftr structure. The keysize is
2150 * defaulted to 16 bytes, and the filesystem size to 0.
2151 * Presumably, at a minimum, the caller will update the
2152 * filesystem size and crypto_type_name after calling this function.
2153 */
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002154static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002155{
Ken Sumrall160b4d62013-04-22 12:15:39 -07002156 off64_t off;
2157
2158 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002159 ftr->magic = CRYPT_MNT_MAGIC;
Kenny Rootc96a5f82013-06-14 12:08:28 -07002160 ftr->major_version = CURRENT_MAJOR_VERSION;
2161 ftr->minor_version = CURRENT_MINOR_VERSION;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002162 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Jason parks70a4b3f2011-01-28 10:10:47 -06002163 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002164
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002165 switch (keymaster_check_compatibility()) {
2166 case 1:
2167 ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
2168 break;
2169
2170 case 0:
2171 ftr->kdf_type = KDF_SCRYPT;
2172 break;
2173
2174 default:
2175 SLOGE("keymaster_check_compatibility failed");
2176 return -1;
2177 }
2178
Kenny Rootc4c70f12013-06-14 12:11:38 -07002179 get_device_scrypt_params(ftr);
2180
Ken Sumrall160b4d62013-04-22 12:15:39 -07002181 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
2182 if (get_crypt_ftr_info(NULL, &off) == 0) {
2183 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
2184 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
2185 ftr->persist_data_size;
2186 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002187
2188 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002189}
2190
Ken Sumrall29d8da82011-05-18 17:20:07 -07002191static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002192{
Ken Sumralle550f782013-08-20 13:48:23 -07002193 const char *args[10];
2194 char size_str[32]; /* Must be large enough to hold a %lld and null byte */
2195 int num_args;
2196 int status;
2197 int tmp;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002198 int rc = -1;
2199
Ken Sumrall29d8da82011-05-18 17:20:07 -07002200 if (type == EXT4_FS) {
Ken Sumralle550f782013-08-20 13:48:23 -07002201 args[0] = "/system/bin/make_ext4fs";
2202 args[1] = "-a";
2203 args[2] = "/data";
2204 args[3] = "-l";
Elliott Hughes73737162014-06-25 17:27:42 -07002205 snprintf(size_str, sizeof(size_str), "%" PRId64, size * 512);
Ken Sumralle550f782013-08-20 13:48:23 -07002206 args[4] = size_str;
2207 args[5] = crypto_blkdev;
2208 num_args = 6;
2209 SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n",
2210 args[0], args[1], args[2], args[3], args[4], args[5]);
JP Abgrall62c7af32014-06-16 13:01:23 -07002211 } else if (type == F2FS_FS) {
2212 args[0] = "/system/bin/mkfs.f2fs";
2213 args[1] = "-t";
2214 args[2] = "-d1";
2215 args[3] = crypto_blkdev;
Elliott Hughes73737162014-06-25 17:27:42 -07002216 snprintf(size_str, sizeof(size_str), "%" PRId64, size);
JP Abgrall62c7af32014-06-16 13:01:23 -07002217 args[4] = size_str;
2218 num_args = 5;
2219 SLOGI("Making empty filesystem with command %s %s %s %s %s\n",
2220 args[0], args[1], args[2], args[3], args[4]);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002221 } else {
2222 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
2223 return -1;
2224 }
2225
Ken Sumralle550f782013-08-20 13:48:23 -07002226 tmp = android_fork_execvp(num_args, (char **)args, &status, false, true);
2227
2228 if (tmp != 0) {
2229 SLOGE("Error creating empty filesystem on %s due to logwrap error\n", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002230 } else {
Ken Sumralle550f782013-08-20 13:48:23 -07002231 if (WIFEXITED(status)) {
2232 if (WEXITSTATUS(status)) {
2233 SLOGE("Error creating filesystem on %s, exit status %d ",
2234 crypto_blkdev, WEXITSTATUS(status));
2235 } else {
2236 SLOGD("Successfully created filesystem on %s\n", crypto_blkdev);
2237 rc = 0;
2238 }
2239 } else {
2240 SLOGE("Error creating filesystem on %s, did not exit normally\n", crypto_blkdev);
2241 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002242 }
2243
2244 return rc;
2245}
2246
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002247#define CRYPT_INPLACE_BUFSIZE 4096
Paul Lawrence87999172014-02-20 12:21:31 -08002248#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / CRYPT_SECTOR_SIZE)
2249#define CRYPT_SECTOR_SIZE 512
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002250
2251/* aligned 32K writes tends to make flash happy.
2252 * SD card association recommends it.
2253 */
Ajay Dudani87701e22014-09-17 21:02:52 -07002254#ifndef CONFIG_HW_DISK_ENCRYPTION
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002255#define BLOCKS_AT_A_TIME 8
Ajay Dudani87701e22014-09-17 21:02:52 -07002256#else
2257#define BLOCKS_AT_A_TIME 1024
2258#endif
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002259
2260struct encryptGroupsData
2261{
2262 int realfd;
2263 int cryptofd;
2264 off64_t numblocks;
2265 off64_t one_pct, cur_pct, new_pct;
2266 off64_t blocks_already_done, tot_numblocks;
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002267 off64_t used_blocks_already_done, tot_used_blocks;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002268 char* real_blkdev, * crypto_blkdev;
2269 int count;
2270 off64_t offset;
2271 char* buffer;
Paul Lawrence87999172014-02-20 12:21:31 -08002272 off64_t last_written_sector;
2273 int completed;
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002274 time_t time_started;
2275 int remaining_time;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002276};
2277
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002278static void update_progress(struct encryptGroupsData* data, int is_used)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002279{
2280 data->blocks_already_done++;
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002281
2282 if (is_used) {
2283 data->used_blocks_already_done++;
2284 }
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002285 if (data->tot_used_blocks) {
2286 data->new_pct = data->used_blocks_already_done / data->one_pct;
2287 } else {
2288 data->new_pct = data->blocks_already_done / data->one_pct;
2289 }
2290
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002291 if (data->new_pct > data->cur_pct) {
2292 char buf[8];
2293 data->cur_pct = data->new_pct;
Elliott Hughescb33f572014-06-25 18:25:11 -07002294 snprintf(buf, sizeof(buf), "%" PRId64, data->cur_pct);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002295 property_set("vold.encrypt_progress", buf);
2296 }
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002297
2298 if (data->cur_pct >= 5) {
Paul Lawrence9c58a872014-09-30 09:12:51 -07002299 struct timespec time_now;
2300 if (clock_gettime(CLOCK_MONOTONIC, &time_now)) {
2301 SLOGW("Error getting time");
2302 } else {
2303 double elapsed_time = difftime(time_now.tv_sec, data->time_started);
2304 off64_t remaining_blocks = data->tot_used_blocks
2305 - data->used_blocks_already_done;
2306 int remaining_time = (int)(elapsed_time * remaining_blocks
2307 / data->used_blocks_already_done);
Paul Lawrence71577502014-08-13 14:55:55 -07002308
Paul Lawrence9c58a872014-09-30 09:12:51 -07002309 // Change time only if not yet set, lower, or a lot higher for
2310 // best user experience
2311 if (data->remaining_time == -1
2312 || remaining_time < data->remaining_time
2313 || remaining_time > data->remaining_time + 60) {
2314 char buf[8];
2315 snprintf(buf, sizeof(buf), "%d", remaining_time);
2316 property_set("vold.encrypt_time_remaining", buf);
2317 data->remaining_time = remaining_time;
2318 }
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002319 }
2320 }
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002321}
2322
Paul Lawrence3846be12014-09-22 11:33:54 -07002323static void log_progress(struct encryptGroupsData const* data, bool completed)
2324{
2325 // Precondition - if completed data = 0 else data != 0
2326
2327 // Track progress so we can skip logging blocks
2328 static off64_t offset = -1;
2329
2330 // Need to close existing 'Encrypting from' log?
2331 if (completed || (offset != -1 && data->offset != offset)) {
2332 SLOGI("Encrypted to sector %" PRId64,
2333 offset / info.block_size * CRYPT_SECTOR_SIZE);
2334 offset = -1;
2335 }
2336
2337 // Need to start new 'Encrypting from' log?
2338 if (!completed && offset != data->offset) {
2339 SLOGI("Encrypting from sector %" PRId64,
2340 data->offset / info.block_size * CRYPT_SECTOR_SIZE);
2341 }
2342
2343 // Update offset
2344 if (!completed) {
2345 offset = data->offset + (off64_t)data->count * info.block_size;
2346 }
2347}
2348
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002349static int flush_outstanding_data(struct encryptGroupsData* data)
2350{
2351 if (data->count == 0) {
2352 return 0;
2353 }
2354
Elliott Hughes231bdba2014-06-25 18:36:19 -07002355 SLOGV("Copying %d blocks at offset %" PRIx64, data->count, data->offset);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002356
2357 if (pread64(data->realfd, data->buffer,
2358 info.block_size * data->count, data->offset)
2359 <= 0) {
2360 SLOGE("Error reading real_blkdev %s for inplace encrypt",
2361 data->real_blkdev);
2362 return -1;
2363 }
2364
2365 if (pwrite64(data->cryptofd, data->buffer,
2366 info.block_size * data->count, data->offset)
2367 <= 0) {
2368 SLOGE("Error writing crypto_blkdev %s for inplace encrypt",
2369 data->crypto_blkdev);
2370 return -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002371 } else {
Paul Lawrence3846be12014-09-22 11:33:54 -07002372 log_progress(data, false);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002373 }
2374
2375 data->count = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002376 data->last_written_sector = (data->offset + data->count)
2377 / info.block_size * CRYPT_SECTOR_SIZE - 1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002378 return 0;
2379}
2380
2381static int encrypt_groups(struct encryptGroupsData* data)
2382{
2383 unsigned int i;
2384 u8 *block_bitmap = 0;
2385 unsigned int block;
2386 off64_t ret;
2387 int rc = -1;
2388
2389 data->buffer = malloc(info.block_size * BLOCKS_AT_A_TIME);
2390 if (!data->buffer) {
2391 SLOGE("Failed to allocate crypto buffer");
2392 goto errout;
2393 }
2394
2395 block_bitmap = malloc(info.block_size);
2396 if (!block_bitmap) {
2397 SLOGE("failed to allocate block bitmap");
2398 goto errout;
2399 }
2400
2401 for (i = 0; i < aux_info.groups; ++i) {
2402 SLOGI("Encrypting group %d", i);
2403
2404 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
2405 u32 block_count = min(info.blocks_per_group,
2406 aux_info.len_blocks - first_block);
2407
2408 off64_t offset = (u64)info.block_size
2409 * aux_info.bg_desc[i].bg_block_bitmap;
2410
2411 ret = pread64(data->realfd, block_bitmap, info.block_size, offset);
2412 if (ret != (int)info.block_size) {
2413 SLOGE("failed to read all of block group bitmap %d", i);
2414 goto errout;
2415 }
2416
2417 offset = (u64)info.block_size * first_block;
2418
2419 data->count = 0;
2420
2421 for (block = 0; block < block_count; block++) {
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002422 int used = bitmap_get_bit(block_bitmap, block);
2423 update_progress(data, used);
2424 if (used) {
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002425 if (data->count == 0) {
2426 data->offset = offset;
2427 }
2428 data->count++;
2429 } else {
2430 if (flush_outstanding_data(data)) {
2431 goto errout;
2432 }
2433 }
2434
2435 offset += info.block_size;
2436
2437 /* Write data if we are aligned or buffer size reached */
2438 if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0
2439 || data->count == BLOCKS_AT_A_TIME) {
2440 if (flush_outstanding_data(data)) {
2441 goto errout;
2442 }
2443 }
Paul Lawrence87999172014-02-20 12:21:31 -08002444
Paul Lawrence73d7a022014-06-09 14:10:09 -07002445 if (!is_battery_ok_to_continue()) {
Paul Lawrence87999172014-02-20 12:21:31 -08002446 SLOGE("Stopping encryption due to low battery");
2447 rc = 0;
2448 goto errout;
2449 }
2450
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002451 }
2452 if (flush_outstanding_data(data)) {
2453 goto errout;
2454 }
2455 }
2456
Paul Lawrence87999172014-02-20 12:21:31 -08002457 data->completed = 1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002458 rc = 0;
2459
2460errout:
Paul Lawrence3846be12014-09-22 11:33:54 -07002461 log_progress(0, true);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002462 free(data->buffer);
2463 free(block_bitmap);
2464 return rc;
2465}
2466
2467static int cryptfs_enable_inplace_ext4(char *crypto_blkdev,
2468 char *real_blkdev,
2469 off64_t size,
2470 off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002471 off64_t tot_size,
2472 off64_t previously_encrypted_upto)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002473{
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002474 u32 i;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002475 struct encryptGroupsData data;
Paul Lawrence74f29f12014-08-28 15:54:10 -07002476 int rc; // Can't initialize without causing warning -Wclobbered
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002477
Paul Lawrence87999172014-02-20 12:21:31 -08002478 if (previously_encrypted_upto > *size_already_done) {
2479 SLOGD("Not fast encrypting since resuming part way through");
2480 return -1;
2481 }
2482
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002483 memset(&data, 0, sizeof(data));
2484 data.real_blkdev = real_blkdev;
2485 data.crypto_blkdev = crypto_blkdev;
2486
2487 if ( (data.realfd = open(real_blkdev, O_RDWR)) < 0) {
JP Abgrall3334c6a2014-10-10 15:52:11 -07002488 SLOGE("Error opening real_blkdev %s for inplace encrypt. err=%d(%s)\n",
2489 real_blkdev, errno, strerror(errno));
Paul Lawrence74f29f12014-08-28 15:54:10 -07002490 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002491 goto errout;
2492 }
2493
2494 if ( (data.cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002495 SLOGE("Error opening crypto_blkdev %s for ext4 inplace encrypt. err=%d(%s)\n",
JP Abgrall3334c6a2014-10-10 15:52:11 -07002496 crypto_blkdev, errno, strerror(errno));
JP Abgrall7fc1de82014-10-10 18:43:41 -07002497 rc = ENABLE_INPLACE_ERR_DEV;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002498 goto errout;
2499 }
2500
2501 if (setjmp(setjmp_env)) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002502 SLOGE("Reading ext4 extent caused an exception\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07002503 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002504 goto errout;
2505 }
2506
2507 if (read_ext(data.realfd, 0) != 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002508 SLOGE("Failed to read ext4 extent\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07002509 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002510 goto errout;
2511 }
2512
2513 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2514 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2515 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
2516
JP Abgrall7fc1de82014-10-10 18:43:41 -07002517 SLOGI("Encrypting ext4 filesystem in place...");
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002518
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002519 data.tot_used_blocks = data.numblocks;
2520 for (i = 0; i < aux_info.groups; ++i) {
2521 data.tot_used_blocks -= aux_info.bg_desc[i].bg_free_blocks_count;
2522 }
2523
2524 data.one_pct = data.tot_used_blocks / 100;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002525 data.cur_pct = 0;
Paul Lawrence9c58a872014-09-30 09:12:51 -07002526
2527 struct timespec time_started = {0};
2528 if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
2529 SLOGW("Error getting time at start");
2530 // Note - continue anyway - we'll run with 0
2531 }
2532 data.time_started = time_started.tv_sec;
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002533 data.remaining_time = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002534
2535 rc = encrypt_groups(&data);
2536 if (rc) {
2537 SLOGE("Error encrypting groups");
2538 goto errout;
2539 }
2540
Paul Lawrence87999172014-02-20 12:21:31 -08002541 *size_already_done += data.completed ? size : data.last_written_sector;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002542 rc = 0;
2543
2544errout:
2545 close(data.realfd);
2546 close(data.cryptofd);
2547
2548 return rc;
2549}
2550
Paul Lawrence3846be12014-09-22 11:33:54 -07002551static void log_progress_f2fs(u64 block, bool completed)
2552{
2553 // Precondition - if completed data = 0 else data != 0
2554
2555 // Track progress so we can skip logging blocks
2556 static u64 last_block = (u64)-1;
2557
2558 // Need to close existing 'Encrypting from' log?
2559 if (completed || (last_block != (u64)-1 && block != last_block + 1)) {
2560 SLOGI("Encrypted to block %" PRId64, last_block);
2561 last_block = -1;
2562 }
2563
2564 // Need to start new 'Encrypting from' log?
2565 if (!completed && (last_block == (u64)-1 || block != last_block + 1)) {
2566 SLOGI("Encrypting from block %" PRId64, block);
2567 }
2568
2569 // Update offset
2570 if (!completed) {
2571 last_block = block;
2572 }
2573}
2574
Daniel Rosenberge82df162014-08-15 22:19:23 +00002575static int encrypt_one_block_f2fs(u64 pos, void *data)
2576{
2577 struct encryptGroupsData *priv_dat = (struct encryptGroupsData *)data;
2578
2579 priv_dat->blocks_already_done = pos - 1;
2580 update_progress(priv_dat, 1);
2581
2582 off64_t offset = pos * CRYPT_INPLACE_BUFSIZE;
2583
2584 if (pread64(priv_dat->realfd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002585 SLOGE("Error reading real_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002586 return -1;
2587 }
2588
2589 if (pwrite64(priv_dat->cryptofd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002590 SLOGE("Error writing crypto_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002591 return -1;
2592 } else {
Paul Lawrence3846be12014-09-22 11:33:54 -07002593 log_progress_f2fs(pos, false);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002594 }
2595
2596 return 0;
2597}
2598
2599static int cryptfs_enable_inplace_f2fs(char *crypto_blkdev,
2600 char *real_blkdev,
2601 off64_t size,
2602 off64_t *size_already_done,
2603 off64_t tot_size,
2604 off64_t previously_encrypted_upto)
2605{
Daniel Rosenberge82df162014-08-15 22:19:23 +00002606 struct encryptGroupsData data;
2607 struct f2fs_info *f2fs_info = NULL;
JP Abgrall7fc1de82014-10-10 18:43:41 -07002608 int rc = ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002609 if (previously_encrypted_upto > *size_already_done) {
2610 SLOGD("Not fast encrypting since resuming part way through");
JP Abgrall7fc1de82014-10-10 18:43:41 -07002611 return ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002612 }
2613 memset(&data, 0, sizeof(data));
2614 data.real_blkdev = real_blkdev;
2615 data.crypto_blkdev = crypto_blkdev;
2616 data.realfd = -1;
2617 data.cryptofd = -1;
2618 if ( (data.realfd = open64(real_blkdev, O_RDWR)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002619 SLOGE("Error opening real_blkdev %s for f2fs inplace encrypt\n",
Daniel Rosenberge82df162014-08-15 22:19:23 +00002620 real_blkdev);
2621 goto errout;
2622 }
2623 if ( (data.cryptofd = open64(crypto_blkdev, O_WRONLY)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002624 SLOGE("Error opening crypto_blkdev %s for f2fs inplace encrypt. err=%d(%s)\n",
JP Abgrall3334c6a2014-10-10 15:52:11 -07002625 crypto_blkdev, errno, strerror(errno));
JP Abgrall7fc1de82014-10-10 18:43:41 -07002626 rc = ENABLE_INPLACE_ERR_DEV;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002627 goto errout;
2628 }
2629
2630 f2fs_info = generate_f2fs_info(data.realfd);
2631 if (!f2fs_info)
2632 goto errout;
2633
2634 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2635 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2636 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
2637
2638 data.tot_used_blocks = get_num_blocks_used(f2fs_info);
2639
2640 data.one_pct = data.tot_used_blocks / 100;
2641 data.cur_pct = 0;
2642 data.time_started = time(NULL);
2643 data.remaining_time = -1;
2644
2645 data.buffer = malloc(f2fs_info->block_size);
2646 if (!data.buffer) {
2647 SLOGE("Failed to allocate crypto buffer");
2648 goto errout;
2649 }
2650
2651 data.count = 0;
2652
2653 /* Currently, this either runs to completion, or hits a nonrecoverable error */
2654 rc = run_on_used_blocks(data.blocks_already_done, f2fs_info, &encrypt_one_block_f2fs, &data);
2655
2656 if (rc) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002657 SLOGE("Error in running over f2fs blocks");
2658 rc = ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002659 goto errout;
2660 }
2661
2662 *size_already_done += size;
2663 rc = 0;
2664
2665errout:
2666 if (rc)
2667 SLOGE("Failed to encrypt f2fs filesystem on %s", real_blkdev);
2668
Paul Lawrence3846be12014-09-22 11:33:54 -07002669 log_progress_f2fs(0, true);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002670 free(f2fs_info);
2671 free(data.buffer);
2672 close(data.realfd);
2673 close(data.cryptofd);
2674
2675 return rc;
2676}
2677
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002678static int cryptfs_enable_inplace_full(char *crypto_blkdev, char *real_blkdev,
2679 off64_t size, off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002680 off64_t tot_size,
2681 off64_t previously_encrypted_upto)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002682{
2683 int realfd, cryptofd;
2684 char *buf[CRYPT_INPLACE_BUFSIZE];
JP Abgrall7fc1de82014-10-10 18:43:41 -07002685 int rc = ENABLE_INPLACE_ERR_OTHER;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002686 off64_t numblocks, i, remainder;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002687 off64_t one_pct, cur_pct, new_pct;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002688 off64_t blocks_already_done, tot_numblocks;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002689
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002690 if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) {
2691 SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002692 return ENABLE_INPLACE_ERR_OTHER;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002693 }
2694
2695 if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
JP Abgrall3334c6a2014-10-10 15:52:11 -07002696 SLOGE("Error opening crypto_blkdev %s for inplace encrypt. err=%d(%s)\n",
2697 crypto_blkdev, errno, strerror(errno));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002698 close(realfd);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002699 return ENABLE_INPLACE_ERR_DEV;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002700 }
2701
2702 /* This is pretty much a simple loop of reading 4K, and writing 4K.
2703 * The size passed in is the number of 512 byte sectors in the filesystem.
2704 * So compute the number of whole 4K blocks we should read/write,
2705 * and the remainder.
2706 */
2707 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2708 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002709 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2710 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002711
2712 SLOGE("Encrypting filesystem in place...");
2713
Paul Lawrence87999172014-02-20 12:21:31 -08002714 i = previously_encrypted_upto + 1 - *size_already_done;
2715
2716 if (lseek64(realfd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
2717 SLOGE("Cannot seek to previously encrypted point on %s", real_blkdev);
2718 goto errout;
2719 }
2720
2721 if (lseek64(cryptofd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
2722 SLOGE("Cannot seek to previously encrypted point on %s", crypto_blkdev);
2723 goto errout;
2724 }
2725
2726 for (;i < size && i % CRYPT_SECTORS_PER_BUFSIZE != 0; ++i) {
2727 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2728 SLOGE("Error reading initial sectors from real_blkdev %s for "
2729 "inplace encrypt\n", crypto_blkdev);
2730 goto errout;
2731 }
2732 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2733 SLOGE("Error writing initial sectors to crypto_blkdev %s for "
2734 "inplace encrypt\n", crypto_blkdev);
2735 goto errout;
2736 } else {
Elliott Hughescb33f572014-06-25 18:25:11 -07002737 SLOGI("Encrypted 1 block at %" PRId64, i);
Paul Lawrence87999172014-02-20 12:21:31 -08002738 }
2739 }
2740
Ken Sumrall29d8da82011-05-18 17:20:07 -07002741 one_pct = tot_numblocks / 100;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002742 cur_pct = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002743 /* process the majority of the filesystem in blocks */
Paul Lawrence87999172014-02-20 12:21:31 -08002744 for (i/=CRYPT_SECTORS_PER_BUFSIZE; i<numblocks; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07002745 new_pct = (i + blocks_already_done) / one_pct;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002746 if (new_pct > cur_pct) {
2747 char buf[8];
2748
2749 cur_pct = new_pct;
Elliott Hughes73737162014-06-25 17:27:42 -07002750 snprintf(buf, sizeof(buf), "%" PRId64, cur_pct);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002751 property_set("vold.encrypt_progress", buf);
2752 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002753 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Lawrence87999172014-02-20 12:21:31 -08002754 SLOGE("Error reading real_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002755 goto errout;
2756 }
2757 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Lawrence87999172014-02-20 12:21:31 -08002758 SLOGE("Error writing crypto_blkdev %s for inplace encrypt", crypto_blkdev);
2759 goto errout;
2760 } else {
Elliott Hughescb33f572014-06-25 18:25:11 -07002761 SLOGD("Encrypted %d block at %" PRId64,
Paul Lawrence87999172014-02-20 12:21:31 -08002762 CRYPT_SECTORS_PER_BUFSIZE,
2763 i * CRYPT_SECTORS_PER_BUFSIZE);
2764 }
2765
Paul Lawrence73d7a022014-06-09 14:10:09 -07002766 if (!is_battery_ok_to_continue()) {
Paul Lawrence87999172014-02-20 12:21:31 -08002767 SLOGE("Stopping encryption due to low battery");
2768 *size_already_done += (i + 1) * CRYPT_SECTORS_PER_BUFSIZE - 1;
2769 rc = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002770 goto errout;
2771 }
2772 }
2773
2774 /* Do any remaining sectors */
2775 for (i=0; i<remainder; i++) {
Paul Lawrence87999172014-02-20 12:21:31 -08002776 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2777 SLOGE("Error reading final sectors from real_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002778 goto errout;
2779 }
Paul Lawrence87999172014-02-20 12:21:31 -08002780 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2781 SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002782 goto errout;
Paul Lawrence87999172014-02-20 12:21:31 -08002783 } else {
2784 SLOGI("Encrypted 1 block at next location");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002785 }
2786 }
2787
Ken Sumrall29d8da82011-05-18 17:20:07 -07002788 *size_already_done += size;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002789 rc = 0;
2790
2791errout:
2792 close(realfd);
2793 close(cryptofd);
2794
2795 return rc;
2796}
2797
JP Abgrall7fc1de82014-10-10 18:43:41 -07002798/* returns on of the ENABLE_INPLACE_* return codes */
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002799static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev,
2800 off64_t size, off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002801 off64_t tot_size,
2802 off64_t previously_encrypted_upto)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002803{
JP Abgrall7fc1de82014-10-10 18:43:41 -07002804 int rc_ext4, rc_f2fs, rc_full;
Paul Lawrence87999172014-02-20 12:21:31 -08002805 if (previously_encrypted_upto) {
Elliott Hughescb33f572014-06-25 18:25:11 -07002806 SLOGD("Continuing encryption from %" PRId64, previously_encrypted_upto);
Paul Lawrence87999172014-02-20 12:21:31 -08002807 }
2808
2809 if (*size_already_done + size < previously_encrypted_upto) {
2810 *size_already_done += size;
2811 return 0;
2812 }
2813
Daniel Rosenberge82df162014-08-15 22:19:23 +00002814 /* TODO: identify filesystem type.
2815 * As is, cryptfs_enable_inplace_ext4 will fail on an f2fs partition, and
2816 * then we will drop down to cryptfs_enable_inplace_f2fs.
2817 * */
JP Abgrall7fc1de82014-10-10 18:43:41 -07002818 if ((rc_ext4 = cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev,
Daniel Rosenberge82df162014-08-15 22:19:23 +00002819 size, size_already_done,
JP Abgrall7fc1de82014-10-10 18:43:41 -07002820 tot_size, previously_encrypted_upto)) == 0) {
Daniel Rosenberge82df162014-08-15 22:19:23 +00002821 return 0;
2822 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002823 SLOGD("cryptfs_enable_inplace_ext4()=%d\n", rc_ext4);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002824
JP Abgrall7fc1de82014-10-10 18:43:41 -07002825 if ((rc_f2fs = cryptfs_enable_inplace_f2fs(crypto_blkdev, real_blkdev,
Daniel Rosenberge82df162014-08-15 22:19:23 +00002826 size, size_already_done,
JP Abgrall7fc1de82014-10-10 18:43:41 -07002827 tot_size, previously_encrypted_upto)) == 0) {
Daniel Rosenberge82df162014-08-15 22:19:23 +00002828 return 0;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002829 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002830 SLOGD("cryptfs_enable_inplace_f2fs()=%d\n", rc_f2fs);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002831
JP Abgrall7fc1de82014-10-10 18:43:41 -07002832 rc_full = cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev,
Paul Lawrence87999172014-02-20 12:21:31 -08002833 size, size_already_done, tot_size,
2834 previously_encrypted_upto);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002835 SLOGD("cryptfs_enable_inplace_full()=%d\n", rc_full);
2836
2837 /* Hack for b/17898962, the following is the symptom... */
2838 if (rc_ext4 == ENABLE_INPLACE_ERR_DEV
2839 && rc_f2fs == ENABLE_INPLACE_ERR_DEV
2840 && rc_full == ENABLE_INPLACE_ERR_DEV) {
2841 return ENABLE_INPLACE_ERR_DEV;
2842 }
2843 return rc_full;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002844}
2845
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002846#define CRYPTO_ENABLE_WIPE 1
2847#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002848
2849#define FRAMEWORK_BOOT_WAIT 60
2850
Ken Sumrall29d8da82011-05-18 17:20:07 -07002851static inline int should_encrypt(struct volume_info *volume)
2852{
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002853 return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) ==
Ken Sumrall29d8da82011-05-18 17:20:07 -07002854 (VOL_ENCRYPTABLE | VOL_NONREMOVABLE);
2855}
2856
Paul Lawrence87999172014-02-20 12:21:31 -08002857static int cryptfs_SHA256_fileblock(const char* filename, __le8* buf)
2858{
2859 int fd = open(filename, O_RDONLY);
2860 if (fd == -1) {
2861 SLOGE("Error opening file %s", filename);
2862 return -1;
2863 }
2864
2865 char block[CRYPT_INPLACE_BUFSIZE];
2866 memset(block, 0, sizeof(block));
2867 if (unix_read(fd, block, sizeof(block)) < 0) {
2868 SLOGE("Error reading file %s", filename);
2869 close(fd);
2870 return -1;
2871 }
2872
2873 close(fd);
2874
2875 SHA256_CTX c;
2876 SHA256_Init(&c);
2877 SHA256_Update(&c, block, sizeof(block));
2878 SHA256_Final(buf, &c);
2879
2880 return 0;
2881}
2882
JP Abgrall62c7af32014-06-16 13:01:23 -07002883static int get_fs_type(struct fstab_rec *rec)
2884{
2885 if (!strcmp(rec->fs_type, "ext4")) {
2886 return EXT4_FS;
2887 } else if (!strcmp(rec->fs_type, "f2fs")) {
2888 return F2FS_FS;
2889 } else {
2890 return -1;
2891 }
2892}
2893
Paul Lawrence87999172014-02-20 12:21:31 -08002894static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr *crypt_ftr, int how,
2895 char *crypto_blkdev, char *real_blkdev,
2896 int previously_encrypted_upto)
2897{
2898 off64_t cur_encryption_done=0, tot_encryption_size=0;
Tim Murray8439dc92014-12-15 11:56:11 -08002899 int rc = -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002900
Paul Lawrence73d7a022014-06-09 14:10:09 -07002901 if (!is_battery_ok_to_start()) {
2902 SLOGW("Not starting encryption due to low battery");
Paul Lawrence87999172014-02-20 12:21:31 -08002903 return 0;
2904 }
2905
2906 /* The size of the userdata partition, and add in the vold volumes below */
2907 tot_encryption_size = crypt_ftr->fs_size;
2908
2909 if (how == CRYPTO_ENABLE_WIPE) {
JP Abgrall62c7af32014-06-16 13:01:23 -07002910 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
2911 int fs_type = get_fs_type(rec);
2912 if (fs_type < 0) {
2913 SLOGE("cryptfs_enable: unsupported fs type %s\n", rec->fs_type);
2914 return -1;
2915 }
2916 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr->fs_size, fs_type);
Paul Lawrence87999172014-02-20 12:21:31 -08002917 } else if (how == CRYPTO_ENABLE_INPLACE) {
2918 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev,
2919 crypt_ftr->fs_size, &cur_encryption_done,
2920 tot_encryption_size,
2921 previously_encrypted_upto);
2922
JP Abgrall7fc1de82014-10-10 18:43:41 -07002923 if (rc == ENABLE_INPLACE_ERR_DEV) {
2924 /* Hack for b/17898962 */
2925 SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n");
2926 cryptfs_reboot(reboot);
2927 }
2928
Paul Lawrence73d7a022014-06-09 14:10:09 -07002929 if (!rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002930 crypt_ftr->encrypted_upto = cur_encryption_done;
2931 }
2932
Paul Lawrence73d7a022014-06-09 14:10:09 -07002933 if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002934 /* The inplace routine never actually sets the progress to 100% due
2935 * to the round down nature of integer division, so set it here */
2936 property_set("vold.encrypt_progress", "100");
2937 }
2938 } else {
2939 /* Shouldn't happen */
2940 SLOGE("cryptfs_enable: internal error, unknown option\n");
2941 rc = -1;
2942 }
2943
2944 return rc;
2945}
2946
Paul Lawrence13486032014-02-03 13:28:11 -08002947int cryptfs_enable_internal(char *howarg, int crypt_type, char *passwd,
2948 int allow_reboot)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002949{
2950 int how = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002951 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN];
Ken Sumralle5032c42012-04-01 23:58:44 -07002952 unsigned long nr_sec;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002953 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Tim Murray8439dc92014-12-15 11:56:11 -08002954 int rc=-1, fd, i;
Paul Lawrence87999172014-02-20 12:21:31 -08002955 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002956 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002957 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002958 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07002959 char key_loc[PROPERTY_VALUE_MAX];
2960 char fuse_sdcard[PROPERTY_VALUE_MAX];
2961 char *sd_mnt_point;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002962 int num_vols;
2963 struct volume_info *vol_list = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002964 off64_t previously_encrypted_upto = 0;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002965
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002966 if (!strcmp(howarg, "wipe")) {
2967 how = CRYPTO_ENABLE_WIPE;
2968 } else if (! strcmp(howarg, "inplace")) {
2969 how = CRYPTO_ENABLE_INPLACE;
2970 } else {
2971 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08002972 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002973 }
2974
Paul Lawrence87999172014-02-20 12:21:31 -08002975 /* See if an encryption was underway and interrupted */
2976 if (how == CRYPTO_ENABLE_INPLACE
2977 && get_crypt_ftr_and_key(&crypt_ftr) == 0
2978 && (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS)) {
2979 previously_encrypted_upto = crypt_ftr.encrypted_upto;
2980 crypt_ftr.encrypted_upto = 0;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002981 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
2982
2983 /* At this point, we are in an inconsistent state. Until we successfully
2984 complete encryption, a reboot will leave us broken. So mark the
2985 encryption failed in case that happens.
2986 On successfully completing encryption, remove this flag */
2987 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
2988
2989 put_crypt_ftr_and_key(&crypt_ftr);
Paul Lawrence87999172014-02-20 12:21:31 -08002990 }
2991
2992 property_get("ro.crypto.state", encrypted_state, "");
2993 if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) {
2994 SLOGE("Device is already running encrypted, aborting");
2995 goto error_unencrypted;
2996 }
2997
2998 // TODO refactor fs_mgr_get_crypt_info to get both in one call
2999 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumrall56ad03c2013-02-13 13:00:19 -08003000 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003001
Ken Sumrall3ed82362011-01-28 23:31:16 -08003002 /* Get the size of the real block device */
3003 fd = open(real_blkdev, O_RDONLY);
3004 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
3005 SLOGE("Cannot get size of block device %s\n", real_blkdev);
3006 goto error_unencrypted;
3007 }
3008 close(fd);
3009
3010 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07003011 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08003012 unsigned int fs_size_sec, max_fs_size_sec;
Jim Millera70abc62014-08-15 02:00:45 +00003013 fs_size_sec = get_fs_size(real_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00003014 if (fs_size_sec == 0)
3015 fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev);
3016
Paul Lawrence87999172014-02-20 12:21:31 -08003017 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
Ken Sumrall3ed82362011-01-28 23:31:16 -08003018
3019 if (fs_size_sec > max_fs_size_sec) {
3020 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
3021 goto error_unencrypted;
3022 }
3023 }
3024
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003025 /* Get a wakelock as this may take a while, and we don't want the
3026 * device to sleep on us. We'll grab a partial wakelock, and if the UI
3027 * wants to keep the screen on, it can grab a full wakelock.
3028 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07003029 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003030 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
3031
Jeff Sharkey7382f812012-08-23 14:08:59 -07003032 /* Get the sdcard mount point */
Jeff Sharkeyb77bc462012-10-01 14:36:26 -07003033 sd_mnt_point = getenv("EMULATED_STORAGE_SOURCE");
Jeff Sharkey7382f812012-08-23 14:08:59 -07003034 if (!sd_mnt_point) {
3035 sd_mnt_point = getenv("EXTERNAL_STORAGE");
3036 }
3037 if (!sd_mnt_point) {
3038 sd_mnt_point = "/mnt/sdcard";
3039 }
Ken Sumrall29d8da82011-05-18 17:20:07 -07003040
Paul Lawrence87999172014-02-20 12:21:31 -08003041 /* TODO
3042 * Currently do not have test devices with multiple encryptable volumes.
3043 * When we acquire some, re-add support.
3044 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07003045 num_vols=vold_getNumDirectVolumes();
3046 vol_list = malloc(sizeof(struct volume_info) * num_vols);
3047 vold_getDirectVolumeList(vol_list);
3048
3049 for (i=0; i<num_vols; i++) {
3050 if (should_encrypt(&vol_list[i])) {
Paul Lawrence87999172014-02-20 12:21:31 -08003051 SLOGE("Cannot encrypt if there are multiple encryptable volumes"
3052 "%s\n", vol_list[i].label);
3053 goto error_unencrypted;
Ken Sumrall29d8da82011-05-18 17:20:07 -07003054 }
3055 }
3056
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003057 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003058 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003059 */
3060 property_set("vold.decrypt", "trigger_shutdown_framework");
3061 SLOGD("Just asked init to shut down class main\n");
3062
Ken Sumrall425524d2012-06-14 20:55:28 -07003063 if (vold_unmountAllAsecs()) {
3064 /* Just report the error. If any are left mounted,
3065 * umounting /data below will fail and handle the error.
3066 */
3067 SLOGE("Error unmounting internal asecs");
3068 }
3069
Ken Sumrall29d8da82011-05-18 17:20:07 -07003070 property_get("ro.crypto.fuse_sdcard", fuse_sdcard, "");
3071 if (!strcmp(fuse_sdcard, "true")) {
3072 /* This is a device using the fuse layer to emulate the sdcard semantics
3073 * on top of the userdata partition. vold does not manage it, it is managed
3074 * by the sdcard service. The sdcard service was killed by the property trigger
3075 * above, so just unmount it now. We must do this _AFTER_ killing the framework,
3076 * unlike the case for vold managed devices above.
3077 */
Greg Hackmann6e8440f2014-10-02 17:18:20 -07003078 if (wait_and_unmount(sd_mnt_point, false)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07003079 goto error_shutting_down;
3080 }
Ken Sumrall2eaf7132011-01-14 12:45:48 -08003081 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003082
3083 /* Now unmount the /data partition. */
Greg Hackmann6e8440f2014-10-02 17:18:20 -07003084 if (wait_and_unmount(DATA_MNT_POINT, false)) {
JP Abgrall502dc742013-11-01 13:06:20 -07003085 if (allow_reboot) {
3086 goto error_shutting_down;
3087 } else {
3088 goto error_unencrypted;
3089 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003090 }
3091
3092 /* Do extra work for a better UX when doing the long inplace encryption */
3093 if (how == CRYPTO_ENABLE_INPLACE) {
3094 /* Now that /data is unmounted, we need to mount a tmpfs
3095 * /data, set a property saying we're doing inplace encryption,
3096 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003097 */
Ken Sumralle5032c42012-04-01 23:58:44 -07003098 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08003099 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003100 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003101 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08003102 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003103
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003104 /* restart the framework. */
3105 /* Create necessary paths on /data */
3106 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08003107 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003108 }
3109
Ken Sumrall92736ef2012-10-17 20:57:14 -07003110 /* Ugh, shutting down the framework is not synchronous, so until it
3111 * can be fixed, this horrible hack will wait a moment for it all to
3112 * shut down before proceeding. Without it, some devices cannot
3113 * restart the graphics services.
3114 */
3115 sleep(2);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003116 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003117
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003118 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003119 /* Initialize a crypt_mnt_ftr for the partition */
Paul Lawrence87999172014-02-20 12:21:31 -08003120 if (previously_encrypted_upto == 0) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07003121 if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) {
3122 goto error_shutting_down;
3123 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003124
Paul Lawrence87999172014-02-20 12:21:31 -08003125 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
3126 crypt_ftr.fs_size = nr_sec
3127 - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
3128 } else {
3129 crypt_ftr.fs_size = nr_sec;
3130 }
Paul Lawrence6bfed202014-07-28 12:47:22 -07003131 /* At this point, we are in an inconsistent state. Until we successfully
3132 complete encryption, a reboot will leave us broken. So mark the
3133 encryption failed in case that happens.
3134 On successfully completing encryption, remove this flag */
3135 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
Paul Lawrence87999172014-02-20 12:21:31 -08003136 crypt_ftr.crypt_type = crypt_type;
Ajay Dudani87701e22014-09-17 21:02:52 -07003137#ifndef CONFIG_HW_DISK_ENCRYPTION
3138 strlcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256", MAX_CRYPTO_TYPE_NAME_LEN);
3139#else
3140 strlcpy((char *)crypt_ftr.crypto_type_name, "aes-xts", MAX_CRYPTO_TYPE_NAME_LEN);
3141
Iliyan Malchevbb7d9af2014-11-20 18:42:23 -08003142 rc = clear_hw_device_encryption_key();
Ajay Dudani87701e22014-09-17 21:02:52 -07003143 if (!rc) {
3144 SLOGE("Error clearing device encryption hardware key. rc = %d", rc);
3145 }
3146
3147 rc = set_hw_device_encryption_key(passwd,
3148 (char*) crypt_ftr.crypto_type_name);
3149 if (!rc) {
3150 SLOGE("Error initializing device encryption hardware key. rc = %d", rc);
3151 goto error_shutting_down;
3152 }
3153#endif
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003154
Paul Lawrence87999172014-02-20 12:21:31 -08003155 /* Make an encrypted master key */
3156 if (create_encrypted_random_key(passwd, crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
3157 SLOGE("Cannot create encrypted master key\n");
3158 goto error_shutting_down;
3159 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003160
Paul Lawrence87999172014-02-20 12:21:31 -08003161 /* Write the key to the end of the partition */
3162 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003163
Paul Lawrence87999172014-02-20 12:21:31 -08003164 /* If any persistent data has been remembered, save it.
3165 * If none, create a valid empty table and save that.
3166 */
3167 if (!persist_data) {
3168 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
3169 if (pdata) {
3170 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
3171 persist_data = pdata;
3172 }
3173 }
3174 if (persist_data) {
3175 save_persistent_data();
3176 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003177 }
3178
Ajay Dudani87701e22014-09-17 21:02:52 -07003179 if (how == CRYPTO_ENABLE_INPLACE) {
3180 /* startup service classes main and late_start */
3181 property_set("vold.decrypt", "trigger_restart_min_framework");
3182 SLOGD("Just triggered restart_min_framework\n");
3183
3184 /* OK, the framework is restarted and will soon be showing a
3185 * progress bar. Time to setup an encrypted mapping, and
3186 * either write a new filesystem, or encrypt in place updating
3187 * the progress bar as we work.
3188 */
3189 }
3190
Paul Lawrenced0c7b172014-08-08 14:28:10 -07003191 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07003192 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
3193 "userdata");
3194
Paul Lawrence87999172014-02-20 12:21:31 -08003195 /* If we are continuing, check checksums match */
3196 rc = 0;
3197 if (previously_encrypted_upto) {
3198 __le8 hash_first_block[SHA256_DIGEST_LENGTH];
3199 rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block);
Ken Sumrall128626f2011-06-28 18:45:14 -07003200
Paul Lawrence87999172014-02-20 12:21:31 -08003201 if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block,
3202 sizeof(hash_first_block)) != 0) {
3203 SLOGE("Checksums do not match - trigger wipe");
3204 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07003205 }
3206 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003207
Paul Lawrence87999172014-02-20 12:21:31 -08003208 if (!rc) {
3209 rc = cryptfs_enable_all_volumes(&crypt_ftr, how,
3210 crypto_blkdev, real_blkdev,
3211 previously_encrypted_upto);
3212 }
3213
3214 /* Calculate checksum if we are not finished */
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08003215 if (!rc && how == CRYPTO_ENABLE_INPLACE
3216 && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08003217 rc = cryptfs_SHA256_fileblock(crypto_blkdev,
3218 crypt_ftr.hash_first_block);
Paul Lawrence73d7a022014-06-09 14:10:09 -07003219 if (rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08003220 SLOGE("Error calculating checksum for continuing encryption");
3221 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07003222 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003223 }
3224
3225 /* Undo the dm-crypt mapping whether we succeed or not */
Ken Sumrall29d8da82011-05-18 17:20:07 -07003226 delete_crypto_blk_dev("userdata");
Ken Sumrall29d8da82011-05-18 17:20:07 -07003227
3228 free(vol_list);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003229
3230 if (! rc) {
3231 /* Success */
Paul Lawrence6bfed202014-07-28 12:47:22 -07003232 crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08003233
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08003234 if (how == CRYPTO_ENABLE_INPLACE
3235 && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08003236 SLOGD("Encrypted up to sector %lld - will continue after reboot",
3237 crypt_ftr.encrypted_upto);
Paul Lawrence6bfed202014-07-28 12:47:22 -07003238 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence87999172014-02-20 12:21:31 -08003239 }
Paul Lawrence73d7a022014-06-09 14:10:09 -07003240
Paul Lawrence6bfed202014-07-28 12:47:22 -07003241 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08003242
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08003243 if (how == CRYPTO_ENABLE_WIPE
3244 || crypt_ftr.encrypted_upto == crypt_ftr.fs_size) {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003245 char value[PROPERTY_VALUE_MAX];
3246 property_get("ro.crypto.state", value, "");
3247 if (!strcmp(value, "")) {
3248 /* default encryption - continue first boot sequence */
3249 property_set("ro.crypto.state", "encrypted");
3250 release_wake_lock(lockid);
3251 cryptfs_check_passwd(DEFAULT_PASSWORD);
3252 cryptfs_restart_internal(1);
3253 return 0;
3254 } else {
3255 sleep(2); /* Give the UI a chance to show 100% progress */
Paul Lawrence87999172014-02-20 12:21:31 -08003256 cryptfs_reboot(reboot);
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003257 }
Paul Lawrence87999172014-02-20 12:21:31 -08003258 } else {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07003259 sleep(2); /* Partially encrypted, ensure writes flushed to ssd */
Paul Lawrence87999172014-02-20 12:21:31 -08003260 cryptfs_reboot(shutdown);
3261 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003262 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003263 char value[PROPERTY_VALUE_MAX];
3264
Ken Sumrall319369a2012-06-27 16:30:18 -07003265 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003266 if (!strcmp(value, "1")) {
3267 /* wipe data if encryption failed */
3268 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
3269 mkdir("/cache/recovery", 0700);
Nick Kralevich4684e582012-06-26 15:07:03 -07003270 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC, 0600);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003271 if (fd >= 0) {
Jeff Sharkeydd1a8042014-09-24 11:46:51 -07003272 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
3273 write(fd, "--reason=cryptfs_enable_internal\n", strlen("--reason=cryptfs_enable_internal\n") + 1);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003274 close(fd);
3275 } else {
3276 SLOGE("could not open /cache/recovery/command\n");
3277 }
Paul Lawrence87999172014-02-20 12:21:31 -08003278 cryptfs_reboot(recovery);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003279 } else {
3280 /* set property to trigger dialog */
3281 property_set("vold.encrypt_progress", "error_partially_encrypted");
3282 release_wake_lock(lockid);
3283 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003284 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003285 }
3286
Ken Sumrall3ed82362011-01-28 23:31:16 -08003287 /* hrm, the encrypt step claims success, but the reboot failed.
3288 * This should not happen.
3289 * Set the property and return. Hope the framework can deal with it.
3290 */
3291 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003292 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003293 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08003294
3295error_unencrypted:
Ken Sumrall29d8da82011-05-18 17:20:07 -07003296 free(vol_list);
Ken Sumrall3ed82362011-01-28 23:31:16 -08003297 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003298 if (lockid[0]) {
3299 release_wake_lock(lockid);
3300 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003301 return -1;
3302
3303error_shutting_down:
3304 /* we failed, and have not encrypted anthing, so the users's data is still intact,
3305 * but the framework is stopped and not restarted to show the error, so it's up to
3306 * vold to restart the system.
3307 */
3308 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Paul Lawrence87999172014-02-20 12:21:31 -08003309 cryptfs_reboot(reboot);
Ken Sumrall3ed82362011-01-28 23:31:16 -08003310
3311 /* shouldn't get here */
3312 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall29d8da82011-05-18 17:20:07 -07003313 free(vol_list);
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003314 if (lockid[0]) {
3315 release_wake_lock(lockid);
3316 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003317 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003318}
3319
Paul Lawrence45f10532014-04-04 18:11:56 +00003320int cryptfs_enable(char *howarg, int type, char *passwd, int allow_reboot)
Paul Lawrence13486032014-02-03 13:28:11 -08003321{
Paul Lawrencefc615042014-10-04 15:32:29 -07003322 char* adjusted_passwd = adjust_passwd(passwd);
3323 if (adjusted_passwd) {
3324 passwd = adjusted_passwd;
3325 }
3326
3327 int rc = cryptfs_enable_internal(howarg, type, passwd, allow_reboot);
3328
3329 free(adjusted_passwd);
3330 return rc;
Paul Lawrence13486032014-02-03 13:28:11 -08003331}
3332
3333int cryptfs_enable_default(char *howarg, int allow_reboot)
3334{
3335 return cryptfs_enable_internal(howarg, CRYPT_TYPE_DEFAULT,
3336 DEFAULT_PASSWORD, allow_reboot);
3337}
3338
3339int cryptfs_changepw(int crypt_type, const char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003340{
Paul Lawrence8175a0b2015-03-05 09:46:23 -08003341 if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
3342 return e4crypt_change_password(DATA_MNT_POINT, crypt_type, newpw);
3343 }
3344
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003345 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003346
3347 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08003348 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08003349 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003350 return -1;
3351 }
3352
Paul Lawrencef4faa572014-01-29 13:31:03 -08003353 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
3354 SLOGE("Invalid crypt_type %d", crypt_type);
3355 return -1;
3356 }
3357
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003358 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07003359 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08003360 SLOGE("Error getting crypt footer and key");
3361 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003362 }
3363
Paul Lawrencef4faa572014-01-29 13:31:03 -08003364 crypt_ftr.crypt_type = crypt_type;
3365
Paul Lawrencefc615042014-10-04 15:32:29 -07003366 char* adjusted_passwd = adjust_passwd(newpw);
3367 if (adjusted_passwd) {
3368 newpw = adjusted_passwd;
3369 }
3370
Paul Lawrencef4faa572014-01-29 13:31:03 -08003371 encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
3372 : newpw,
3373 crypt_ftr.salt,
3374 saved_master_key,
3375 crypt_ftr.master_key,
3376 &crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003377
Jason parks70a4b3f2011-01-28 10:10:47 -06003378 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07003379 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003380
Paul Lawrencefc615042014-10-04 15:32:29 -07003381 free(adjusted_passwd);
Ajay Dudani87701e22014-09-17 21:02:52 -07003382
3383#ifdef CONFIG_HW_DISK_ENCRYPTION
Iliyan Malchevbb7d9af2014-11-20 18:42:23 -08003384 if (!strcmp((char *)crypt_ftr.crypto_type_name, "aes-xts")) {
3385 if (crypt_type == CRYPT_TYPE_DEFAULT) {
3386 int rc = update_hw_device_encryption_key(DEFAULT_PASSWORD, (char*) crypt_ftr.crypto_type_name);
3387 SLOGD("Update hardware encryption key to default for crypt_type: %d. rc = %d", crypt_type, rc);
3388 if (!rc)
3389 return -1;
3390 } else {
3391 int rc = update_hw_device_encryption_key(newpw, (char*) crypt_ftr.crypto_type_name);
3392 SLOGD("Update hardware encryption key for crypt_type: %d. rc = %d", crypt_type, rc);
3393 if (!rc)
3394 return -1;
3395 }
Ajay Dudani87701e22014-09-17 21:02:52 -07003396 }
3397#endif
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003398 return 0;
3399}
Ken Sumrall160b4d62013-04-22 12:15:39 -07003400
Rubin Xu85c01f92014-10-13 12:49:54 +01003401static unsigned int persist_get_max_entries(int encrypted) {
3402 struct crypt_mnt_ftr crypt_ftr;
3403 unsigned int dsize;
3404 unsigned int max_persistent_entries;
3405
3406 /* If encrypted, use the values from the crypt_ftr, otherwise
3407 * use the values for the current spec.
3408 */
3409 if (encrypted) {
3410 if (get_crypt_ftr_and_key(&crypt_ftr)) {
3411 return -1;
3412 }
3413 dsize = crypt_ftr.persist_data_size;
3414 } else {
3415 dsize = CRYPT_PERSIST_DATA_SIZE;
3416 }
3417
3418 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
3419 sizeof(struct crypt_persist_entry);
3420
3421 return max_persistent_entries;
3422}
3423
3424static int persist_get_key(const char *fieldname, char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003425{
3426 unsigned int i;
3427
3428 if (persist_data == NULL) {
3429 return -1;
3430 }
3431 for (i = 0; i < persist_data->persist_valid_entries; i++) {
3432 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
3433 /* We found it! */
3434 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
3435 return 0;
3436 }
3437 }
3438
3439 return -1;
3440}
3441
Rubin Xu85c01f92014-10-13 12:49:54 +01003442static int persist_set_key(const char *fieldname, const char *value, int encrypted)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003443{
3444 unsigned int i;
3445 unsigned int num;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003446 unsigned int max_persistent_entries;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003447
3448 if (persist_data == NULL) {
3449 return -1;
3450 }
3451
Rubin Xu85c01f92014-10-13 12:49:54 +01003452 max_persistent_entries = persist_get_max_entries(encrypted);
Ken Sumrall160b4d62013-04-22 12:15:39 -07003453
3454 num = persist_data->persist_valid_entries;
3455
3456 for (i = 0; i < num; i++) {
3457 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
3458 /* We found an existing entry, update it! */
3459 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
3460 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
3461 return 0;
3462 }
3463 }
3464
3465 /* We didn't find it, add it to the end, if there is room */
3466 if (persist_data->persist_valid_entries < max_persistent_entries) {
3467 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
3468 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
3469 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
3470 persist_data->persist_valid_entries++;
3471 return 0;
3472 }
3473
3474 return -1;
3475}
3476
Rubin Xu85c01f92014-10-13 12:49:54 +01003477/**
3478 * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the
3479 * sequence and its index is greater than or equal to index. Return 0 otherwise.
3480 */
3481static int match_multi_entry(const char *key, const char *field, unsigned index) {
Rubin Xu85c01f92014-10-13 12:49:54 +01003482 unsigned int field_len;
3483 unsigned int key_index;
3484 field_len = strlen(field);
3485
3486 if (index == 0) {
3487 // The first key in a multi-entry field is just the filedname itself.
3488 if (!strcmp(key, field)) {
3489 return 1;
3490 }
3491 }
3492 // Match key against "%s_%d" % (field, index)
3493 if (strlen(key) < field_len + 1 + 1) {
3494 // Need at least a '_' and a digit.
3495 return 0;
3496 }
3497 if (strncmp(key, field, field_len)) {
3498 // If the key does not begin with field, it's not a match.
3499 return 0;
3500 }
3501 if (1 != sscanf(&key[field_len],"_%d", &key_index)) {
3502 return 0;
3503 }
3504 return key_index >= index;
3505}
3506
3507/*
3508 * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all
3509 * remaining entries starting from index will be deleted.
3510 * returns PERSIST_DEL_KEY_OK if deletion succeeds,
3511 * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist,
3512 * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs.
3513 *
3514 */
3515static int persist_del_keys(const char *fieldname, unsigned index)
3516{
3517 unsigned int i;
3518 unsigned int j;
3519 unsigned int num;
3520
3521 if (persist_data == NULL) {
3522 return PERSIST_DEL_KEY_ERROR_OTHER;
3523 }
3524
3525 num = persist_data->persist_valid_entries;
3526
3527 j = 0; // points to the end of non-deleted entries.
3528 // Filter out to-be-deleted entries in place.
3529 for (i = 0; i < num; i++) {
3530 if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) {
3531 persist_data->persist_entry[j] = persist_data->persist_entry[i];
3532 j++;
3533 }
3534 }
3535
3536 if (j < num) {
3537 persist_data->persist_valid_entries = j;
3538 // Zeroise the remaining entries
3539 memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry));
3540 return PERSIST_DEL_KEY_OK;
3541 } else {
3542 // Did not find an entry matching the given fieldname
3543 return PERSIST_DEL_KEY_ERROR_NO_FIELD;
3544 }
3545}
3546
3547static int persist_count_keys(const char *fieldname)
3548{
3549 unsigned int i;
3550 unsigned int count;
3551
3552 if (persist_data == NULL) {
3553 return -1;
3554 }
3555
3556 count = 0;
3557 for (i = 0; i < persist_data->persist_valid_entries; i++) {
3558 if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) {
3559 count++;
3560 }
3561 }
3562
3563 return count;
3564}
3565
Ken Sumrall160b4d62013-04-22 12:15:39 -07003566/* Return the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01003567int cryptfs_getfield(const char *fieldname, char *value, int len)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003568{
3569 char temp_value[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01003570 /* CRYPTO_GETFIELD_OK is success,
3571 * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set,
3572 * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small,
3573 * CRYPTO_GETFIELD_ERROR_OTHER is any other error
Ken Sumrall160b4d62013-04-22 12:15:39 -07003574 */
Rubin Xu85c01f92014-10-13 12:49:54 +01003575 int rc = CRYPTO_GETFIELD_ERROR_OTHER;
3576 int i;
3577 char temp_field[PROPERTY_KEY_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -07003578
3579 if (persist_data == NULL) {
3580 load_persistent_data();
3581 if (persist_data == NULL) {
3582 SLOGE("Getfield error, cannot load persistent data");
3583 goto out;
3584 }
3585 }
3586
Rubin Xu85c01f92014-10-13 12:49:54 +01003587 // Read value from persistent entries. If the original value is split into multiple entries,
3588 // stitch them back together.
Ken Sumrall160b4d62013-04-22 12:15:39 -07003589 if (!persist_get_key(fieldname, temp_value)) {
Rubin Xu85c01f92014-10-13 12:49:54 +01003590 // We found it, copy it to the caller's buffer and keep going until all entries are read.
3591 if (strlcpy(value, temp_value, len) >= (unsigned) len) {
3592 // value too small
3593 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
3594 goto out;
3595 }
3596 rc = CRYPTO_GETFIELD_OK;
3597
3598 for (i = 1; /* break explicitly */; i++) {
3599 if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >=
3600 (int) sizeof(temp_field)) {
3601 // If the fieldname is very long, we stop as soon as it begins to overflow the
3602 // maximum field length. At this point we have in fact fully read out the original
3603 // value because cryptfs_setfield would not allow fields with longer names to be
3604 // written in the first place.
3605 break;
3606 }
3607 if (!persist_get_key(temp_field, temp_value)) {
3608 if (strlcat(value, temp_value, len) >= (unsigned)len) {
3609 // value too small.
3610 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
3611 goto out;
3612 }
3613 } else {
3614 // Exhaust all entries.
3615 break;
3616 }
3617 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003618 } else {
3619 /* Sadness, it's not there. Return the error */
Rubin Xu85c01f92014-10-13 12:49:54 +01003620 rc = CRYPTO_GETFIELD_ERROR_NO_FIELD;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003621 }
3622
3623out:
3624 return rc;
3625}
3626
3627/* Set the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01003628int cryptfs_setfield(const char *fieldname, const char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003629{
Ken Sumrall160b4d62013-04-22 12:15:39 -07003630 char encrypted_state[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01003631 /* 0 is success, negative values are error */
3632 int rc = CRYPTO_SETFIELD_ERROR_OTHER;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003633 int encrypted = 0;
Rubin Xu85c01f92014-10-13 12:49:54 +01003634 unsigned int field_id;
3635 char temp_field[PROPERTY_KEY_MAX];
3636 unsigned int num_entries;
3637 unsigned int max_keylen;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003638
3639 if (persist_data == NULL) {
3640 load_persistent_data();
3641 if (persist_data == NULL) {
3642 SLOGE("Setfield error, cannot load persistent data");
3643 goto out;
3644 }
3645 }
3646
3647 property_get("ro.crypto.state", encrypted_state, "");
3648 if (!strcmp(encrypted_state, "encrypted") ) {
3649 encrypted = 1;
3650 }
3651
Rubin Xu85c01f92014-10-13 12:49:54 +01003652 // Compute the number of entries required to store value, each entry can store up to
3653 // (PROPERTY_VALUE_MAX - 1) chars
3654 if (strlen(value) == 0) {
3655 // Empty value also needs one entry to store.
3656 num_entries = 1;
3657 } else {
3658 num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1);
3659 }
3660
3661 max_keylen = strlen(fieldname);
3662 if (num_entries > 1) {
3663 // Need an extra "_%d" suffix.
3664 max_keylen += 1 + log10(num_entries);
3665 }
3666 if (max_keylen > PROPERTY_KEY_MAX - 1) {
3667 rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003668 goto out;
3669 }
3670
Rubin Xu85c01f92014-10-13 12:49:54 +01003671 // Make sure we have enough space to write the new value
3672 if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) >
3673 persist_get_max_entries(encrypted)) {
3674 rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG;
3675 goto out;
3676 }
3677
3678 // Now that we know persist_data has enough space for value, let's delete the old field first
3679 // to make up space.
3680 persist_del_keys(fieldname, 0);
3681
3682 if (persist_set_key(fieldname, value, encrypted)) {
3683 // fail to set key, should not happen as we have already checked the available space
3684 SLOGE("persist_set_key() error during setfield()");
3685 goto out;
3686 }
3687
3688 for (field_id = 1; field_id < num_entries; field_id++) {
3689 snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, field_id);
3690
3691 if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), encrypted)) {
3692 // fail to set key, should not happen as we have already checked the available space.
3693 SLOGE("persist_set_key() error during setfield()");
3694 goto out;
3695 }
3696 }
3697
Ken Sumrall160b4d62013-04-22 12:15:39 -07003698 /* If we are running encrypted, save the persistent data now */
3699 if (encrypted) {
3700 if (save_persistent_data()) {
3701 SLOGE("Setfield error, cannot save persistent data");
3702 goto out;
3703 }
3704 }
3705
Rubin Xu85c01f92014-10-13 12:49:54 +01003706 rc = CRYPTO_SETFIELD_OK;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003707
3708out:
3709 return rc;
3710}
Paul Lawrencef4faa572014-01-29 13:31:03 -08003711
3712/* Checks userdata. Attempt to mount the volume if default-
3713 * encrypted.
3714 * On success trigger next init phase and return 0.
3715 * Currently do not handle failure - see TODO below.
3716 */
3717int cryptfs_mount_default_encrypted(void)
3718{
3719 char decrypt_state[PROPERTY_VALUE_MAX];
3720 property_get("vold.decrypt", decrypt_state, "0");
3721 if (!strcmp(decrypt_state, "0")) {
3722 SLOGE("Not encrypted - should not call here");
3723 } else {
3724 int crypt_type = cryptfs_get_password_type();
3725 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
3726 SLOGE("Bad crypt type - error");
3727 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
3728 SLOGD("Password is not default - "
3729 "starting min framework to prompt");
3730 property_set("vold.decrypt", "trigger_restart_min_framework");
3731 return 0;
3732 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
3733 SLOGD("Password is default - restarting filesystem");
3734 cryptfs_restart_internal(0);
3735 return 0;
3736 } else {
3737 SLOGE("Encrypted, default crypt type but can't decrypt");
3738 }
3739 }
3740
Paul Lawrence6bfed202014-07-28 12:47:22 -07003741 /** Corrupt. Allow us to boot into framework, which will detect bad
3742 crypto when it calls do_crypto_complete, then do a factory reset
Paul Lawrencef4faa572014-01-29 13:31:03 -08003743 */
Paul Lawrence6bfed202014-07-28 12:47:22 -07003744 property_set("vold.decrypt", "trigger_restart_min_framework");
Paul Lawrencef4faa572014-01-29 13:31:03 -08003745 return 0;
3746}
3747
3748/* Returns type of the password, default, pattern, pin or password.
3749 */
3750int cryptfs_get_password_type(void)
3751{
Paul Lawrence8175a0b2015-03-05 09:46:23 -08003752 if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
3753 return e4crypt_get_password_type(DATA_MNT_POINT);
3754 }
3755
Paul Lawrencef4faa572014-01-29 13:31:03 -08003756 struct crypt_mnt_ftr crypt_ftr;
3757
3758 if (get_crypt_ftr_and_key(&crypt_ftr)) {
3759 SLOGE("Error getting crypt footer and key\n");
3760 return -1;
3761 }
3762
Paul Lawrence6bfed202014-07-28 12:47:22 -07003763 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) {
3764 return -1;
3765 }
3766
Paul Lawrencef4faa572014-01-29 13:31:03 -08003767 return crypt_ftr.crypt_type;
3768}
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003769
Paul Lawrence8175a0b2015-03-05 09:46:23 -08003770const char* cryptfs_get_password()
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003771{
Paul Lawrence8175a0b2015-03-05 09:46:23 -08003772 if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
3773 return e4crypt_get_password(DATA_MNT_POINT);
3774 }
3775
Paul Lawrence399317e2014-03-10 13:20:50 -07003776 struct timespec now;
Paul Lawrenceef2b5be2014-11-11 12:47:03 -08003777 clock_gettime(CLOCK_BOOTTIME, &now);
Paul Lawrence399317e2014-03-10 13:20:50 -07003778 if (now.tv_sec < password_expiry_time) {
3779 return password;
3780 } else {
3781 cryptfs_clear_password();
3782 return 0;
3783 }
3784}
3785
3786void cryptfs_clear_password()
3787{
3788 if (password) {
3789 size_t len = strlen(password);
3790 memset(password, 0, len);
3791 free(password);
3792 password = 0;
3793 password_expiry_time = 0;
3794 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003795}