blob: 9c2b4bc81cc552f5f8c396e32fb0c05eaac24c3d [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>
28#include <unistd.h>
29#include <stdio.h>
30#include <sys/ioctl.h>
31#include <linux/dm-ioctl.h>
32#include <libgen.h>
33#include <stdlib.h>
34#include <sys/param.h>
35#include <string.h>
36#include <sys/mount.h>
37#include <openssl/evp.h>
Ken Sumrall8ddbe402011-01-17 15:26:29 -080038#include <openssl/sha.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080039#include <errno.h>
Ken Sumrall3ed82362011-01-28 23:31:16 -080040#include <ext4.h>
Ken Sumrall29d8da82011-05-18 17:20:07 -070041#include <linux/kdev_t.h>
Ken Sumralle5032c42012-04-01 23:58:44 -070042#include <fs_mgr.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080043#include "cryptfs.h"
44#define LOG_TAG "Cryptfs"
45#include "cutils/log.h"
46#include "cutils/properties.h"
Ken Sumralladfba362013-06-04 16:37:52 -070047#include "cutils/android_reboot.h"
Ken Sumrall5d4c68e2011-01-30 19:06:03 -080048#include "hardware_legacy/power.h"
Ken Sumralle550f782013-08-20 13:48:23 -070049#include <logwrap/logwrap.h>
Ken Sumrall29d8da82011-05-18 17:20:07 -070050#include "VolumeManager.h"
Ken Sumrall9caab762013-06-11 19:10:20 -070051#include "VoldUtil.h"
Kenny Rootc4c70f12013-06-14 12:11:38 -070052#include "crypto_scrypt.h"
Paul Lawrenceae59fe62014-01-21 08:23:27 -080053#include "ext4_utils.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080054
Mark Salyzyn3e971272014-01-21 13:27:04 -080055#define UNUSED __attribute__((unused))
56
Mark Salyzyn5eecc442014-02-12 14:16:14 -080057#define UNUSED __attribute__((unused))
58
Ken Sumrall8f869aa2010-12-03 03:47:09 -080059#define DM_CRYPT_BUF_SIZE 4096
60
Jason parks70a4b3f2011-01-28 10:10:47 -060061#define HASH_COUNT 2000
62#define KEY_LEN_BYTES 16
63#define IV_LEN_BYTES 16
64
Ken Sumrall29d8da82011-05-18 17:20:07 -070065#define KEY_IN_FOOTER "footer"
66
Paul Lawrencef4faa572014-01-29 13:31:03 -080067// "default_password" encoded into hex (d=0x64 etc)
68#define DEFAULT_PASSWORD "64656661756c745f70617373776f7264"
69
Ken Sumrall29d8da82011-05-18 17:20:07 -070070#define EXT4_FS 1
71#define FAT_FS 2
72
Ken Sumralle919efe2012-09-29 17:07:41 -070073#define TABLE_LOAD_RETRIES 10
74
Ken Sumrall8f869aa2010-12-03 03:47:09 -080075char *me = "cryptfs";
76
Jason parks70a4b3f2011-01-28 10:10:47 -060077static unsigned char saved_master_key[KEY_LEN_BYTES];
Ken Sumrall3ad90722011-10-04 20:38:29 -070078static char *saved_mount_point;
Jason parks70a4b3f2011-01-28 10:10:47 -060079static int master_key_saved = 0;
Ken Sumrall160b4d62013-04-22 12:15:39 -070080static struct crypt_persist_data *persist_data = NULL;
Ken Sumrall56ad03c2013-02-13 13:00:19 -080081
82extern struct fstab *fstab;
Ken Sumrall8ddbe402011-01-17 15:26:29 -080083
Ken Sumralladfba362013-06-04 16:37:52 -070084static void cryptfs_reboot(int recovery)
85{
86 if (recovery) {
87 property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
88 } else {
89 property_set(ANDROID_RB_PROPERTY, "reboot");
90 }
91 sleep(20);
92
93 /* Shouldn't get here, reboot should happen before sleep times out */
94 return;
95}
96
Ken Sumrall8f869aa2010-12-03 03:47:09 -080097static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
98{
99 memset(io, 0, dataSize);
100 io->data_size = dataSize;
101 io->data_start = sizeof(struct dm_ioctl);
102 io->version[0] = 4;
103 io->version[1] = 0;
104 io->version[2] = 0;
105 io->flags = flags;
106 if (name) {
107 strncpy(io->name, name, sizeof(io->name));
108 }
109}
110
Kenny Rootc4c70f12013-06-14 12:11:38 -0700111/**
112 * Gets the default device scrypt parameters for key derivation time tuning.
113 * The parameters should lead to about one second derivation time for the
114 * given device.
115 */
116static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
117 const int default_params[] = SCRYPT_DEFAULTS;
118 int params[] = SCRYPT_DEFAULTS;
119 char paramstr[PROPERTY_VALUE_MAX];
120 char *token;
121 char *saveptr;
122 int i;
123
124 property_get(SCRYPT_PROP, paramstr, "");
125 if (paramstr[0] != '\0') {
126 /*
127 * The token we're looking for should be three integers separated by
128 * colons (e.g., "12:8:1"). Scan the property to make sure it matches.
129 */
Kenny Root2947e342013-08-14 15:54:49 -0700130 for (i = 0, token = strtok_r(paramstr, ":", &saveptr);
131 token != NULL && i < 3;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700132 i++, token = strtok_r(NULL, ":", &saveptr)) {
133 char *endptr;
134 params[i] = strtol(token, &endptr, 10);
135
136 /*
137 * Check that there was a valid number and it's 8-bit. If not,
138 * break out and the end check will take the default values.
139 */
140 if ((*token == '\0') || (*endptr != '\0') || params[i] < 0 || params[i] > 255) {
141 break;
142 }
143 }
144
145 /*
146 * If there were not enough tokens or a token was malformed (not an
147 * integer), it will end up here and the default parameters can be
148 * taken.
149 */
150 if ((i != 3) || (token != NULL)) {
151 SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
152 memcpy(params, default_params, sizeof(params));
153 }
154 }
155
156 ftr->N_factor = params[0];
157 ftr->r_factor = params[1];
158 ftr->p_factor = params[2];
159}
160
Ken Sumrall3ed82362011-01-28 23:31:16 -0800161static unsigned int get_fs_size(char *dev)
162{
163 int fd, block_size;
164 struct ext4_super_block sb;
165 off64_t len;
166
167 if ((fd = open(dev, O_RDONLY)) < 0) {
168 SLOGE("Cannot open device to get filesystem size ");
169 return 0;
170 }
171
172 if (lseek64(fd, 1024, SEEK_SET) < 0) {
173 SLOGE("Cannot seek to superblock");
174 return 0;
175 }
176
177 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
178 SLOGE("Cannot read superblock");
179 return 0;
180 }
181
182 close(fd);
183
184 block_size = 1024 << sb.s_log_block_size;
185 /* compute length in bytes */
186 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
187
188 /* return length in sectors */
189 return (unsigned int) (len / 512);
190}
191
Ken Sumrall160b4d62013-04-22 12:15:39 -0700192static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
193{
194 static int cached_data = 0;
195 static off64_t cached_off = 0;
196 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
197 int fd;
198 char key_loc[PROPERTY_VALUE_MAX];
199 char real_blkdev[PROPERTY_VALUE_MAX];
200 unsigned int nr_sec;
201 int rc = -1;
202
203 if (!cached_data) {
204 fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
205
206 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
207 if ( (fd = open(real_blkdev, O_RDWR)) < 0) {
208 SLOGE("Cannot open real block device %s\n", real_blkdev);
209 return -1;
210 }
211
212 if ((nr_sec = get_blkdev_size(fd))) {
213 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
214 * encryption info footer and key, and plenty of bytes to spare for future
215 * growth.
216 */
217 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
218 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
219 cached_data = 1;
220 } else {
221 SLOGE("Cannot get size of block device %s\n", real_blkdev);
222 }
223 close(fd);
224 } else {
225 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
226 cached_off = 0;
227 cached_data = 1;
228 }
229 }
230
231 if (cached_data) {
232 if (metadata_fname) {
233 *metadata_fname = cached_metadata_fname;
234 }
235 if (off) {
236 *off = cached_off;
237 }
238 rc = 0;
239 }
240
241 return rc;
242}
243
Ken Sumralle8744072011-01-18 22:01:55 -0800244/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800245 * update the failed mount count but not change the key.
246 */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700247static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800248{
249 int fd;
250 unsigned int nr_sec, cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700251 /* starting_off is set to the SEEK_SET offset
252 * where the crypto structure starts
253 */
254 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800255 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700256 char *fname = NULL;
Ken Sumrall3be890f2011-09-14 16:53:46 -0700257 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800258
Ken Sumrall160b4d62013-04-22 12:15:39 -0700259 if (get_crypt_ftr_info(&fname, &starting_off)) {
260 SLOGE("Unable to get crypt_ftr_info\n");
261 return -1;
262 }
263 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700264 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700265 return -1;
266 }
Ken Sumralle550f782013-08-20 13:48:23 -0700267 if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) {
268 SLOGE("Cannot open footer file %s for put\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700269 return -1;
270 }
271
272 /* Seek to the start of the crypt footer */
273 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
274 SLOGE("Cannot seek to real block device footer\n");
275 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800276 }
277
278 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
279 SLOGE("Cannot write real block device footer\n");
280 goto errout;
281 }
282
Ken Sumrall3be890f2011-09-14 16:53:46 -0700283 fstat(fd, &statbuf);
284 /* If the keys are kept on a raw block device, do not try to truncate it. */
Ken Sumralle550f782013-08-20 13:48:23 -0700285 if (S_ISREG(statbuf.st_mode)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700286 if (ftruncate(fd, 0x4000)) {
Colin Cross59846b62014-02-06 20:34:29 -0800287 SLOGE("Cannot set footer file size\n");
Ken Sumralle8744072011-01-18 22:01:55 -0800288 goto errout;
289 }
290 }
291
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800292 /* Success! */
293 rc = 0;
294
295errout:
296 close(fd);
297 return rc;
298
299}
300
Ken Sumrall160b4d62013-04-22 12:15:39 -0700301static inline int unix_read(int fd, void* buff, int len)
302{
303 return TEMP_FAILURE_RETRY(read(fd, buff, len));
304}
305
306static inline int unix_write(int fd, const void* buff, int len)
307{
308 return TEMP_FAILURE_RETRY(write(fd, buff, len));
309}
310
311static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
312{
313 memset(pdata, 0, len);
314 pdata->persist_magic = PERSIST_DATA_MAGIC;
315 pdata->persist_valid_entries = 0;
316}
317
318/* A routine to update the passed in crypt_ftr to the lastest version.
319 * fd is open read/write on the device that holds the crypto footer and persistent
320 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
321 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
322 */
323static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
324{
Kenny Root7434b312013-06-14 11:29:53 -0700325 int orig_major = crypt_ftr->major_version;
326 int orig_minor = crypt_ftr->minor_version;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700327
Kenny Root7434b312013-06-14 11:29:53 -0700328 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
329 struct crypt_persist_data *pdata;
330 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700331
Kenny Rootc4c70f12013-06-14 12:11:38 -0700332 SLOGW("upgrading crypto footer to 1.1");
333
Kenny Root7434b312013-06-14 11:29:53 -0700334 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
335 if (pdata == NULL) {
336 SLOGE("Cannot allocate persisent data\n");
337 return;
338 }
339 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
340
341 /* Need to initialize the persistent data area */
342 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
343 SLOGE("Cannot seek to persisent data offset\n");
344 return;
345 }
346 /* Write all zeros to the first copy, making it invalid */
347 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
348
349 /* Write a valid but empty structure to the second copy */
350 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
351 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
352
353 /* Update the footer */
354 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
355 crypt_ftr->persist_data_offset[0] = pdata_offset;
356 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
357 crypt_ftr->minor_version = 1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700358 }
359
Paul Lawrencef4faa572014-01-29 13:31:03 -0800360 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700361 SLOGW("upgrading crypto footer to 1.2");
JP Abgrall7bdfa522013-11-15 13:42:56 -0800362 /* But keep the old kdf_type.
363 * It will get updated later to KDF_SCRYPT after the password has been verified.
364 */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700365 crypt_ftr->kdf_type = KDF_PBKDF2;
366 get_device_scrypt_params(crypt_ftr);
367 crypt_ftr->minor_version = 2;
368 }
369
Paul Lawrencef4faa572014-01-29 13:31:03 -0800370 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
371 SLOGW("upgrading crypto footer to 1.3");
372 crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
373 crypt_ftr->minor_version = 3;
374 }
375
Kenny Root7434b312013-06-14 11:29:53 -0700376 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
377 if (lseek64(fd, offset, SEEK_SET) == -1) {
378 SLOGE("Cannot seek to crypt footer\n");
379 return;
380 }
381 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700382 }
Ken Sumrall160b4d62013-04-22 12:15:39 -0700383}
384
385
386static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800387{
388 int fd;
389 unsigned int nr_sec, cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700390 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800391 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700392 char *fname = NULL;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700393 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800394
Ken Sumrall160b4d62013-04-22 12:15:39 -0700395 if (get_crypt_ftr_info(&fname, &starting_off)) {
396 SLOGE("Unable to get crypt_ftr_info\n");
397 return -1;
398 }
399 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700400 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700401 return -1;
402 }
403 if ( (fd = open(fname, O_RDWR)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700404 SLOGE("Cannot open footer file %s for get\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700405 return -1;
406 }
407
408 /* Make sure it's 16 Kbytes in length */
409 fstat(fd, &statbuf);
410 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
411 SLOGE("footer file %s is not the expected size!\n", fname);
412 goto errout;
413 }
414
415 /* Seek to the start of the crypt footer */
416 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
417 SLOGE("Cannot seek to real block device footer\n");
418 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800419 }
420
421 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
422 SLOGE("Cannot read real block device footer\n");
423 goto errout;
424 }
425
426 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700427 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800428 goto errout;
429 }
430
Kenny Rootc96a5f82013-06-14 12:08:28 -0700431 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
432 SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
433 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800434 goto errout;
435 }
436
Kenny Rootc96a5f82013-06-14 12:08:28 -0700437 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
438 SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
439 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800440 }
441
Ken Sumrall160b4d62013-04-22 12:15:39 -0700442 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
443 * copy on disk before returning.
444 */
Kenny Rootc96a5f82013-06-14 12:08:28 -0700445 if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700446 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
Ken Sumralle8744072011-01-18 22:01:55 -0800447 }
448
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800449 /* Success! */
450 rc = 0;
451
452errout:
453 close(fd);
454 return rc;
455}
456
Ken Sumrall160b4d62013-04-22 12:15:39 -0700457static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
458{
459 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
460 crypt_ftr->persist_data_offset[1]) {
461 SLOGE("Crypt_ftr persist data regions overlap");
462 return -1;
463 }
464
465 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
466 SLOGE("Crypt_ftr persist data region 0 starts after region 1");
467 return -1;
468 }
469
470 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
471 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
472 CRYPT_FOOTER_OFFSET) {
473 SLOGE("Persistent data extends past crypto footer");
474 return -1;
475 }
476
477 return 0;
478}
479
480static int load_persistent_data(void)
481{
482 struct crypt_mnt_ftr crypt_ftr;
483 struct crypt_persist_data *pdata = NULL;
484 char encrypted_state[PROPERTY_VALUE_MAX];
485 char *fname;
486 int found = 0;
487 int fd;
488 int ret;
489 int i;
490
491 if (persist_data) {
492 /* Nothing to do, we've already loaded or initialized it */
493 return 0;
494 }
495
496
497 /* If not encrypted, just allocate an empty table and initialize it */
498 property_get("ro.crypto.state", encrypted_state, "");
499 if (strcmp(encrypted_state, "encrypted") ) {
500 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
501 if (pdata) {
502 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
503 persist_data = pdata;
504 return 0;
505 }
506 return -1;
507 }
508
509 if(get_crypt_ftr_and_key(&crypt_ftr)) {
510 return -1;
511 }
512
513 if ((crypt_ftr.major_version != 1) || (crypt_ftr.minor_version != 1)) {
514 SLOGE("Crypt_ftr version doesn't support persistent data");
515 return -1;
516 }
517
518 if (get_crypt_ftr_info(&fname, NULL)) {
519 return -1;
520 }
521
522 ret = validate_persistent_data_storage(&crypt_ftr);
523 if (ret) {
524 return -1;
525 }
526
527 fd = open(fname, O_RDONLY);
528 if (fd < 0) {
529 SLOGE("Cannot open %s metadata file", fname);
530 return -1;
531 }
532
533 if (persist_data == NULL) {
534 pdata = malloc(crypt_ftr.persist_data_size);
535 if (pdata == NULL) {
536 SLOGE("Cannot allocate memory for persistent data");
537 goto err;
538 }
539 }
540
541 for (i = 0; i < 2; i++) {
542 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
543 SLOGE("Cannot seek to read persistent data on %s", fname);
544 goto err2;
545 }
546 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
547 SLOGE("Error reading persistent data on iteration %d", i);
548 goto err2;
549 }
550 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
551 found = 1;
552 break;
553 }
554 }
555
556 if (!found) {
557 SLOGI("Could not find valid persistent data, creating");
558 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
559 }
560
561 /* Success */
562 persist_data = pdata;
563 close(fd);
564 return 0;
565
566err2:
567 free(pdata);
568
569err:
570 close(fd);
571 return -1;
572}
573
574static int save_persistent_data(void)
575{
576 struct crypt_mnt_ftr crypt_ftr;
577 struct crypt_persist_data *pdata;
578 char *fname;
579 off64_t write_offset;
580 off64_t erase_offset;
581 int found = 0;
582 int fd;
583 int ret;
584
585 if (persist_data == NULL) {
586 SLOGE("No persistent data to save");
587 return -1;
588 }
589
590 if(get_crypt_ftr_and_key(&crypt_ftr)) {
591 return -1;
592 }
593
594 if ((crypt_ftr.major_version != 1) || (crypt_ftr.minor_version != 1)) {
595 SLOGE("Crypt_ftr version doesn't support persistent data");
596 return -1;
597 }
598
599 ret = validate_persistent_data_storage(&crypt_ftr);
600 if (ret) {
601 return -1;
602 }
603
604 if (get_crypt_ftr_info(&fname, NULL)) {
605 return -1;
606 }
607
608 fd = open(fname, O_RDWR);
609 if (fd < 0) {
610 SLOGE("Cannot open %s metadata file", fname);
611 return -1;
612 }
613
614 pdata = malloc(crypt_ftr.persist_data_size);
615 if (pdata == NULL) {
616 SLOGE("Cannot allocate persistant data");
617 goto err;
618 }
619
620 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
621 SLOGE("Cannot seek to read persistent data on %s", fname);
622 goto err2;
623 }
624
625 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
626 SLOGE("Error reading persistent data before save");
627 goto err2;
628 }
629
630 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
631 /* The first copy is the curent valid copy, so write to
632 * the second copy and erase this one */
633 write_offset = crypt_ftr.persist_data_offset[1];
634 erase_offset = crypt_ftr.persist_data_offset[0];
635 } else {
636 /* The second copy must be the valid copy, so write to
637 * the first copy, and erase the second */
638 write_offset = crypt_ftr.persist_data_offset[0];
639 erase_offset = crypt_ftr.persist_data_offset[1];
640 }
641
642 /* Write the new copy first, if successful, then erase the old copy */
643 if (lseek(fd, write_offset, SEEK_SET) < 0) {
644 SLOGE("Cannot seek to write persistent data");
645 goto err2;
646 }
647 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
648 (int) crypt_ftr.persist_data_size) {
649 if (lseek(fd, erase_offset, SEEK_SET) < 0) {
650 SLOGE("Cannot seek to erase previous persistent data");
651 goto err2;
652 }
653 fsync(fd);
654 memset(pdata, 0, crypt_ftr.persist_data_size);
655 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
656 (int) crypt_ftr.persist_data_size) {
657 SLOGE("Cannot write to erase previous persistent data");
658 goto err2;
659 }
660 fsync(fd);
661 } else {
662 SLOGE("Cannot write to save persistent data");
663 goto err2;
664 }
665
666 /* Success */
667 free(pdata);
668 close(fd);
669 return 0;
670
671err2:
672 free(pdata);
673err:
674 close(fd);
675 return -1;
676}
677
Paul Lawrencef4faa572014-01-29 13:31:03 -0800678static int hexdigit (char c)
679{
680 if (c >= '0' && c <= '9') return c - '0';
681 c = tolower(c);
682 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
683 return -1;
684}
685
686static unsigned char* convert_hex_ascii_to_key(const char* master_key_ascii,
687 unsigned int* out_keysize)
688{
689 unsigned int i;
690 *out_keysize = 0;
691
692 size_t size = strlen (master_key_ascii);
693 if (size % 2) {
694 SLOGE("Trying to convert ascii string of odd length");
695 return NULL;
696 }
697
698 unsigned char* master_key = (unsigned char*) malloc(size / 2);
699 if (master_key == 0) {
700 SLOGE("Cannot allocate");
701 return NULL;
702 }
703
704 for (i = 0; i < size; i += 2) {
705 int high_nibble = hexdigit (master_key_ascii[i]);
706 int low_nibble = hexdigit (master_key_ascii[i + 1]);
707
708 if(high_nibble < 0 || low_nibble < 0) {
709 SLOGE("Invalid hex string");
710 free (master_key);
711 return NULL;
712 }
713
714 master_key[*out_keysize] = high_nibble * 16 + low_nibble;
715 (*out_keysize)++;
716 }
717
718 return master_key;
719}
720
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800721/* Convert a binary key of specified length into an ascii hex string equivalent,
722 * without the leading 0x and with null termination
723 */
Paul Lawrencef4faa572014-01-29 13:31:03 -0800724static void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800725 char *master_key_ascii)
726{
727 unsigned int i, a;
728 unsigned char nibble;
729
730 for (i=0, a=0; i<keysize; i++, a+=2) {
731 /* For each byte, write out two ascii hex digits */
732 nibble = (master_key[i] >> 4) & 0xf;
733 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
734
735 nibble = master_key[i] & 0xf;
736 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
737 }
738
739 /* Add the null termination */
740 master_key_ascii[a] = '\0';
741
742}
743
Ken Sumralldb5e0262013-02-05 17:39:48 -0800744static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
745 char *real_blk_name, const char *name, int fd,
746 char *extra_params)
747{
748 char buffer[DM_CRYPT_BUF_SIZE];
749 struct dm_ioctl *io;
750 struct dm_target_spec *tgt;
751 char *crypt_params;
752 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
753 int i;
754
755 io = (struct dm_ioctl *) buffer;
756
757 /* Load the mapping table for this device */
758 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
759
760 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
761 io->target_count = 1;
762 tgt->status = 0;
763 tgt->sector_start = 0;
764 tgt->length = crypt_ftr->fs_size;
765 strcpy(tgt->target_type, "crypt");
766
767 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
768 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
769 sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name,
770 master_key_ascii, real_blk_name, extra_params);
771 crypt_params += strlen(crypt_params) + 1;
772 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
773 tgt->next = crypt_params - buffer;
774
775 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
776 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
777 break;
778 }
779 usleep(500000);
780 }
781
782 if (i == TABLE_LOAD_RETRIES) {
783 /* We failed to load the table, return an error */
784 return -1;
785 } else {
786 return i + 1;
787 }
788}
789
790
791static int get_dm_crypt_version(int fd, const char *name, int *version)
792{
793 char buffer[DM_CRYPT_BUF_SIZE];
794 struct dm_ioctl *io;
795 struct dm_target_versions *v;
796 int i;
797
798 io = (struct dm_ioctl *) buffer;
799
800 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
801
802 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
803 return -1;
804 }
805
806 /* Iterate over the returned versions, looking for name of "crypt".
807 * When found, get and return the version.
808 */
809 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
810 while (v->next) {
811 if (! strcmp(v->name, "crypt")) {
812 /* We found the crypt driver, return the version, and get out */
813 version[0] = v->version[0];
814 version[1] = v->version[1];
815 version[2] = v->version[2];
816 return 0;
817 }
818 v = (struct dm_target_versions *)(((char *)v) + v->next);
819 }
820
821 return -1;
822}
823
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800824static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
Ken Sumrall29d8da82011-05-18 17:20:07 -0700825 char *real_blk_name, char *crypto_blk_name, const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800826{
827 char buffer[DM_CRYPT_BUF_SIZE];
828 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
829 char *crypt_params;
830 struct dm_ioctl *io;
831 struct dm_target_spec *tgt;
832 unsigned int minor;
833 int fd;
Ken Sumralle919efe2012-09-29 17:07:41 -0700834 int i;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800835 int retval = -1;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800836 int version[3];
837 char *extra_params;
838 int load_count;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800839
840 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
841 SLOGE("Cannot open device-mapper\n");
842 goto errout;
843 }
844
845 io = (struct dm_ioctl *) buffer;
846
847 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
848 if (ioctl(fd, DM_DEV_CREATE, io)) {
849 SLOGE("Cannot create dm-crypt device\n");
850 goto errout;
851 }
852
853 /* Get the device status, in particular, the name of it's device file */
854 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
855 if (ioctl(fd, DM_DEV_STATUS, io)) {
856 SLOGE("Cannot retrieve dm-crypt device status\n");
857 goto errout;
858 }
859 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
860 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
861
Ken Sumralldb5e0262013-02-05 17:39:48 -0800862 extra_params = "";
863 if (! get_dm_crypt_version(fd, name, version)) {
864 /* Support for allow_discards was added in version 1.11.0 */
865 if ((version[0] >= 2) ||
866 ((version[0] == 1) && (version[1] >= 11))) {
867 extra_params = "1 allow_discards";
868 SLOGI("Enabling support for allow_discards in dmcrypt.\n");
869 }
Ken Sumralle919efe2012-09-29 17:07:41 -0700870 }
871
Ken Sumralldb5e0262013-02-05 17:39:48 -0800872 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name,
873 fd, extra_params);
874 if (load_count < 0) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800875 SLOGE("Cannot load dm-crypt mapping table.\n");
876 goto errout;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800877 } else if (load_count > 1) {
878 SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800879 }
880
881 /* Resume this device to activate it */
Ken Sumralldb5e0262013-02-05 17:39:48 -0800882 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800883
884 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
885 SLOGE("Cannot resume the dm-crypt device\n");
886 goto errout;
887 }
888
889 /* We made it here with no errors. Woot! */
890 retval = 0;
891
892errout:
893 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
894
895 return retval;
896}
897
Ken Sumrall29d8da82011-05-18 17:20:07 -0700898static int delete_crypto_blk_dev(char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800899{
900 int fd;
901 char buffer[DM_CRYPT_BUF_SIZE];
902 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800903 int retval = -1;
904
905 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
906 SLOGE("Cannot open device-mapper\n");
907 goto errout;
908 }
909
910 io = (struct dm_ioctl *) buffer;
911
912 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
913 if (ioctl(fd, DM_DEV_REMOVE, io)) {
914 SLOGE("Cannot remove dm-crypt device\n");
915 goto errout;
916 }
917
918 /* We made it here with no errors. Woot! */
919 retval = 0;
920
921errout:
922 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
923
924 return retval;
925
926}
927
Paul Lawrence13486032014-02-03 13:28:11 -0800928static int pbkdf2(const char *passwd, unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -0800929 unsigned char *ikey, void *params UNUSED)
930{
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800931 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -0800932 unsigned int keysize;
933 char* master_key = (char*)convert_hex_ascii_to_key(passwd, &keysize);
934 if (!master_key) return -1;
935 PKCS5_PBKDF2_HMAC_SHA1(master_key, keysize, salt, SALT_LEN,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800936 HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
Paul Lawrencef4faa572014-01-29 13:31:03 -0800937
938 free (master_key);
939 return 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800940}
941
Paul Lawrence13486032014-02-03 13:28:11 -0800942static int scrypt(const char *passwd, unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -0800943 unsigned char *ikey, void *params)
944{
Kenny Rootc4c70f12013-06-14 12:11:38 -0700945 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
946
947 int N = 1 << ftr->N_factor;
948 int r = 1 << ftr->r_factor;
949 int p = 1 << ftr->p_factor;
950
951 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -0800952 unsigned int keysize;
953 unsigned char* master_key = convert_hex_ascii_to_key(passwd, &keysize);
954 if (!master_key) return -1;
955 crypto_scrypt(master_key, keysize, salt, SALT_LEN, N, r, p, ikey,
Kenny Rootc4c70f12013-06-14 12:11:38 -0700956 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrencef4faa572014-01-29 13:31:03 -0800957
958 free (master_key);
959 return 0;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700960}
961
Paul Lawrence13486032014-02-03 13:28:11 -0800962static int encrypt_master_key(const char *passwd, unsigned char *salt,
Ken Sumralle8744072011-01-18 22:01:55 -0800963 unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -0700964 unsigned char *encrypted_master_key,
965 struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800966{
967 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
968 EVP_CIPHER_CTX e_ctx;
969 int encrypted_len, final_len;
970
971 /* Turn the password into a key and IV that can decrypt the master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700972 get_device_scrypt_params(crypt_ftr);
Paul Lawrencef4faa572014-01-29 13:31:03 -0800973 if (scrypt(passwd, salt, ikey, crypt_ftr)) {
974 SLOGE("scrypt failed");
975 return -1;
976 }
Kenny Rootc4c70f12013-06-14 12:11:38 -0700977
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800978 /* Initialize the decryption engine */
979 if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
980 SLOGE("EVP_EncryptInit failed\n");
981 return -1;
982 }
983 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800984
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800985 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800986 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
987 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800988 SLOGE("EVP_EncryptUpdate failed\n");
989 return -1;
990 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800991 if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800992 SLOGE("EVP_EncryptFinal failed\n");
993 return -1;
994 }
995
996 if (encrypted_len + final_len != KEY_LEN_BYTES) {
997 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
998 return -1;
999 } else {
1000 return 0;
1001 }
1002}
1003
JP Abgrall7bdfa522013-11-15 13:42:56 -08001004static int decrypt_master_key_aux(char *passwd, unsigned char *salt,
Ken Sumralle8744072011-01-18 22:01:55 -08001005 unsigned char *encrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001006 unsigned char *decrypted_master_key,
1007 kdf_func kdf, void *kdf_params)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001008{
1009 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 -08001010 EVP_CIPHER_CTX d_ctx;
1011 int decrypted_len, final_len;
1012
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001013 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001014 if (kdf(passwd, salt, ikey, kdf_params)) {
1015 SLOGE("kdf failed");
1016 return -1;
1017 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001018
1019 /* Initialize the decryption engine */
1020 if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
1021 return -1;
1022 }
1023 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1024 /* Decrypt the master key */
1025 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
1026 encrypted_master_key, KEY_LEN_BYTES)) {
1027 return -1;
1028 }
1029 if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
1030 return -1;
1031 }
1032
1033 if (decrypted_len + final_len != KEY_LEN_BYTES) {
1034 return -1;
1035 } else {
1036 return 0;
1037 }
1038}
1039
Kenny Rootc4c70f12013-06-14 12:11:38 -07001040static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001041{
Kenny Rootc4c70f12013-06-14 12:11:38 -07001042 if (ftr->kdf_type == KDF_SCRYPT) {
1043 *kdf = scrypt;
1044 *kdf_params = ftr;
1045 } else {
1046 *kdf = pbkdf2;
1047 *kdf_params = NULL;
1048 }
1049}
1050
JP Abgrall7bdfa522013-11-15 13:42:56 -08001051static int decrypt_master_key(char *passwd, unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001052 struct crypt_mnt_ftr *crypt_ftr)
1053{
1054 kdf_func kdf;
1055 void *kdf_params;
1056 int ret;
1057
1058 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001059 ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key, decrypted_master_key, kdf,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001060 kdf_params);
1061 if (ret != 0) {
1062 SLOGW("failure decrypting master key");
Kenny Rootc4c70f12013-06-14 12:11:38 -07001063 }
1064
1065 return ret;
1066}
1067
1068static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt,
1069 struct crypt_mnt_ftr *crypt_ftr) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001070 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -08001071 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001072 EVP_CIPHER_CTX e_ctx;
1073 int encrypted_len, final_len;
1074
1075 /* Get some random bits for a key */
1076 fd = open("/dev/urandom", O_RDONLY);
Ken Sumralle8744072011-01-18 22:01:55 -08001077 read(fd, key_buf, sizeof(key_buf));
1078 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001079 close(fd);
1080
1081 /* Now encrypt it with the password */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001082 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001083}
1084
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001085static int wait_and_unmount(char *mountpoint)
1086{
1087 int i, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001088#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001089
1090 /* Now umount the tmpfs filesystem */
1091 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
1092 if (umount(mountpoint)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001093 if (errno == EINVAL) {
1094 /* EINVAL is returned if the directory is not a mountpoint,
1095 * i.e. there is no filesystem mounted there. So just get out.
1096 */
1097 break;
1098 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001099 sleep(1);
1100 i++;
1101 } else {
1102 break;
1103 }
1104 }
1105
1106 if (i < WAIT_UNMOUNT_COUNT) {
1107 SLOGD("unmounting %s succeeded\n", mountpoint);
1108 rc = 0;
1109 } else {
1110 SLOGE("unmounting %s failed\n", mountpoint);
1111 rc = -1;
1112 }
1113
1114 return rc;
1115}
1116
Ken Sumrallc5872692013-05-14 15:26:31 -07001117#define DATA_PREP_TIMEOUT 200
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001118static int prep_data_fs(void)
1119{
1120 int i;
1121
1122 /* Do the prep of the /data filesystem */
1123 property_set("vold.post_fs_data_done", "0");
1124 property_set("vold.decrypt", "trigger_post_fs_data");
1125 SLOGD("Just triggered post_fs_data\n");
1126
Ken Sumrallc5872692013-05-14 15:26:31 -07001127 /* Wait a max of 50 seconds, hopefully it takes much less */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001128 for (i=0; i<DATA_PREP_TIMEOUT; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001129 char p[PROPERTY_VALUE_MAX];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001130
1131 property_get("vold.post_fs_data_done", p, "0");
1132 if (*p == '1') {
1133 break;
1134 } else {
1135 usleep(250000);
1136 }
1137 }
1138 if (i == DATA_PREP_TIMEOUT) {
1139 /* Ugh, we failed to prep /data in time. Bail. */
Ken Sumrallc5872692013-05-14 15:26:31 -07001140 SLOGE("post_fs_data timed out!\n");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001141 return -1;
1142 } else {
1143 SLOGD("post_fs_data done\n");
1144 return 0;
1145 }
1146}
1147
Paul Lawrencef4faa572014-01-29 13:31:03 -08001148static int cryptfs_restart_internal(int restart_main)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001149{
1150 char fs_type[32];
1151 char real_blkdev[MAXPATHLEN];
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001152 char crypto_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001153 char fs_options[256];
1154 unsigned long mnt_flags;
1155 struct stat statbuf;
1156 int rc = -1, i;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001157 static int restart_successful = 0;
1158
1159 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -06001160 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001161 SLOGE("Encrypted filesystem not validated, aborting");
1162 return -1;
1163 }
1164
1165 if (restart_successful) {
1166 SLOGE("System already restarted with encrypted disk, aborting");
1167 return -1;
1168 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001169
Paul Lawrencef4faa572014-01-29 13:31:03 -08001170 if (restart_main) {
1171 /* Here is where we shut down the framework. The init scripts
1172 * start all services in one of three classes: core, main or late_start.
1173 * On boot, we start core and main. Now, we stop main, but not core,
1174 * as core includes vold and a few other really important things that
1175 * we need to keep running. Once main has stopped, we should be able
1176 * to umount the tmpfs /data, then mount the encrypted /data.
1177 * We then restart the class main, and also the class late_start.
1178 * At the moment, I've only put a few things in late_start that I know
1179 * are not needed to bring up the framework, and that also cause problems
1180 * with unmounting the tmpfs /data, but I hope to add add more services
1181 * to the late_start class as we optimize this to decrease the delay
1182 * till the user is asked for the password to the filesystem.
1183 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001184
Paul Lawrencef4faa572014-01-29 13:31:03 -08001185 /* The init files are setup to stop the class main when vold.decrypt is
1186 * set to trigger_reset_main.
1187 */
1188 property_set("vold.decrypt", "trigger_reset_main");
1189 SLOGD("Just asked init to shut down class main\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001190
Paul Lawrencef4faa572014-01-29 13:31:03 -08001191 /* Ugh, shutting down the framework is not synchronous, so until it
1192 * can be fixed, this horrible hack will wait a moment for it all to
1193 * shut down before proceeding. Without it, some devices cannot
1194 * restart the graphics services.
1195 */
1196 sleep(2);
1197 }
Ken Sumrall9dedfd42012-10-09 14:16:59 -07001198
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001199 /* Now that the framework is shutdown, we should be able to umount()
1200 * the tmpfs filesystem, and mount the real one.
1201 */
1202
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001203 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1204 if (strlen(crypto_blkdev) == 0) {
1205 SLOGE("fs_crypto_blkdev not set\n");
1206 return -1;
1207 }
1208
Ken Sumralle5032c42012-04-01 23:58:44 -07001209 if (! (rc = wait_and_unmount(DATA_MNT_POINT)) ) {
Doug Zongker6fd57712013-12-17 09:43:23 -08001210 /* If ro.crypto.readonly is set to 1, mount the decrypted
1211 * filesystem readonly. This is used when /data is mounted by
1212 * recovery mode.
1213 */
1214 char ro_prop[PROPERTY_VALUE_MAX];
1215 property_get("ro.crypto.readonly", ro_prop, "");
1216 if (strlen(ro_prop) > 0 && atoi(ro_prop)) {
1217 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
1218 rec->flags |= MS_RDONLY;
1219 }
1220
Ken Sumralle5032c42012-04-01 23:58:44 -07001221 /* If that succeeded, then mount the decrypted filesystem */
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001222 fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001223
Ken Sumralle5032c42012-04-01 23:58:44 -07001224 property_set("vold.decrypt", "trigger_load_persist_props");
1225 /* Create necessary paths on /data */
1226 if (prep_data_fs()) {
1227 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001228 }
Ken Sumralle5032c42012-04-01 23:58:44 -07001229
1230 /* startup service classes main and late_start */
1231 property_set("vold.decrypt", "trigger_restart_framework");
1232 SLOGD("Just triggered restart_framework\n");
1233
1234 /* Give it a few moments to get started */
1235 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001236 }
1237
Ken Sumrall0cc16632011-01-18 20:32:26 -08001238 if (rc == 0) {
1239 restart_successful = 1;
1240 }
1241
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001242 return rc;
1243}
1244
Paul Lawrencef4faa572014-01-29 13:31:03 -08001245int cryptfs_restart(void)
1246{
1247 /* Call internal implementation forcing a restart of main service group */
1248 return cryptfs_restart_internal(1);
1249}
1250
Mark Salyzyn3e971272014-01-21 13:27:04 -08001251static int do_crypto_complete(char *mount_point UNUSED)
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001252{
1253 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001254 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -08001255 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001256
1257 property_get("ro.crypto.state", encrypted_state, "");
1258 if (strcmp(encrypted_state, "encrypted") ) {
1259 SLOGE("not running with encryption, aborting");
1260 return 1;
1261 }
1262
Ken Sumrall160b4d62013-04-22 12:15:39 -07001263 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001264 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumralle5032c42012-04-01 23:58:44 -07001265
Ken Sumralle1a45852011-12-14 21:24:27 -08001266 /*
1267 * Only report this error if key_loc is a file and it exists.
1268 * If the device was never encrypted, and /data is not mountable for
1269 * some reason, returning 1 should prevent the UI from presenting the
1270 * a "enter password" screen, or worse, a "press button to wipe the
1271 * device" screen.
1272 */
1273 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
1274 SLOGE("master key file does not exist, aborting");
1275 return 1;
1276 } else {
1277 SLOGE("Error getting crypt footer and key\n");
1278 return -1;
1279 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001280 }
1281
1282 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
1283 SLOGE("Encryption process didn't finish successfully\n");
1284 return -2; /* -2 is the clue to the UI that there is no usable data on the disk,
1285 * and give the user an option to wipe the disk */
1286 }
1287
1288 /* We passed the test! We shall diminish, and return to the west */
1289 return 0;
1290}
1291
Paul Lawrencef4faa572014-01-29 13:31:03 -08001292static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
1293 char *passwd, char *mount_point, char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001294{
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001295 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001296 unsigned char decrypted_master_key[32];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001297 char crypto_blkdev[MAXPATHLEN];
1298 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001299 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001300 unsigned int orig_failed_decrypt_count;
1301 int rc;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001302 kdf_func kdf;
1303 void *kdf_params;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001304
Paul Lawrencef4faa572014-01-29 13:31:03 -08001305 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1306 orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001307
Paul Lawrencef4faa572014-01-29 13:31:03 -08001308 if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
1309 if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr)) {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001310 SLOGE("Failed to decrypt master key\n");
1311 return -1;
1312 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001313 }
1314
Paul Lawrencef4faa572014-01-29 13:31:03 -08001315 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
1316
1317 if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key,
1318 real_blkdev, crypto_blkdev, label)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001319 SLOGE("Error creating decrypted block device\n");
1320 return -1;
1321 }
1322
Alex Klyubin707795a2013-05-10 15:17:07 -07001323 /* If init detects an encrypted filesystem, it writes a file for each such
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001324 * encrypted fs into the tmpfs /data filesystem, and then the framework finds those
1325 * files and passes that data to me */
1326 /* Create a tmp mount point to try mounting the decryptd fs
1327 * Since we're here, the mount_point should be a tmpfs filesystem, so make
1328 * a directory in it to test mount the decrypted filesystem.
1329 */
1330 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
1331 mkdir(tmp_mount_point, 0755);
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001332 if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001333 SLOGE("Error temp mounting decrypted block device\n");
Ken Sumrall29d8da82011-05-18 17:20:07 -07001334 delete_crypto_blk_dev(label);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001335 crypt_ftr->failed_decrypt_count++;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001336 } else {
1337 /* Success, so just umount and we'll mount it properly when we restart
1338 * the framework.
1339 */
1340 umount(tmp_mount_point);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001341 crypt_ftr->failed_decrypt_count = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001342 }
1343
Paul Lawrencef4faa572014-01-29 13:31:03 -08001344 if (orig_failed_decrypt_count != crypt_ftr->failed_decrypt_count) {
1345 put_crypt_ftr_and_key(crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001346 }
1347
Paul Lawrencef4faa572014-01-29 13:31:03 -08001348 if (crypt_ftr->failed_decrypt_count) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001349 /* We failed to mount the device, so return an error */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001350 rc = crypt_ftr->failed_decrypt_count;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001351
1352 } else {
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001353 /* Woot! Success! Save the name of the crypto block device
1354 * so we can mount it when restarting the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001355 */
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001356 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -06001357
1358 /* Also save a the master key so we can reencrypted the key
1359 * the key when we want to change the password on it.
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001360 */
Jason parks70a4b3f2011-01-28 10:10:47 -06001361 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001362 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -06001363 master_key_saved = 1;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001364 SLOGD("%s(): Master key saved\n", __FUNCTION__);
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001365 rc = 0;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001366 /*
1367 * Upgrade if we're not using the latest KDF.
1368 */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001369 if (crypt_ftr->kdf_type != KDF_SCRYPT) {
1370 crypt_ftr->kdf_type = KDF_SCRYPT;
1371 rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1372 crypt_ftr->master_key, crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001373 if (!rc) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001374 rc = put_crypt_ftr_and_key(crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001375 }
1376 SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
1377 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001378 }
1379
1380 return rc;
1381}
1382
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001383/* Called by vold when it wants to undo the crypto mapping of a volume it
1384 * manages. This is usually in response to a factory reset, when we want
1385 * to undo the crypto mapping so the volume is formatted in the clear.
1386 */
1387int cryptfs_revert_volume(const char *label)
1388{
1389 return delete_crypto_blk_dev((char *)label);
1390}
1391
Ken Sumrall29d8da82011-05-18 17:20:07 -07001392/*
1393 * Called by vold when it's asked to mount an encrypted, nonremovable volume.
1394 * Setup a dm-crypt mapping, use the saved master key from
1395 * setting up the /data mapping, and return the new device path.
1396 */
1397int cryptfs_setup_volume(const char *label, int major, int minor,
1398 char *crypto_sys_path, unsigned int max_path,
1399 int *new_major, int *new_minor)
1400{
1401 char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN];
1402 struct crypt_mnt_ftr sd_crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001403 struct stat statbuf;
1404 int nr_sec, fd;
1405
1406 sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor);
1407
Ken Sumrall160b4d62013-04-22 12:15:39 -07001408 get_crypt_ftr_and_key(&sd_crypt_ftr);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001409
1410 /* Update the fs_size field to be the size of the volume */
1411 fd = open(real_blkdev, O_RDONLY);
1412 nr_sec = get_blkdev_size(fd);
1413 close(fd);
1414 if (nr_sec == 0) {
1415 SLOGE("Cannot get size of volume %s\n", real_blkdev);
1416 return -1;
1417 }
1418
1419 sd_crypt_ftr.fs_size = nr_sec;
1420 create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev,
1421 crypto_blkdev, label);
1422
1423 stat(crypto_blkdev, &statbuf);
1424 *new_major = MAJOR(statbuf.st_rdev);
1425 *new_minor = MINOR(statbuf.st_rdev);
1426
1427 /* Create path to sys entry for this block device */
1428 snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1);
1429
1430 return 0;
1431}
1432
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001433int cryptfs_crypto_complete(void)
1434{
1435 return do_crypto_complete("/data");
1436}
1437
Paul Lawrencef4faa572014-01-29 13:31:03 -08001438int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr)
1439{
1440 char encrypted_state[PROPERTY_VALUE_MAX];
1441 property_get("ro.crypto.state", encrypted_state, "");
1442 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
1443 SLOGE("encrypted fs already validated or not running with encryption,"
1444 " aborting");
1445 return -1;
1446 }
1447
1448 if (get_crypt_ftr_and_key(crypt_ftr)) {
1449 SLOGE("Error getting crypt footer and key");
1450 return -1;
1451 }
1452
1453 return 0;
1454}
1455
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001456int cryptfs_check_passwd(char *passwd)
1457{
Paul Lawrencef4faa572014-01-29 13:31:03 -08001458 struct crypt_mnt_ftr crypt_ftr;
1459 int rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001460
Paul Lawrencef4faa572014-01-29 13:31:03 -08001461 rc = check_unmounted_and_get_ftr(&crypt_ftr);
1462 if (rc)
1463 return rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001464
Paul Lawrencef4faa572014-01-29 13:31:03 -08001465 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
1466 DATA_MNT_POINT, "userdata");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001467 return rc;
1468}
1469
Ken Sumrall3ad90722011-10-04 20:38:29 -07001470int cryptfs_verify_passwd(char *passwd)
1471{
1472 struct crypt_mnt_ftr crypt_ftr;
1473 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001474 unsigned char decrypted_master_key[32];
Ken Sumrall3ad90722011-10-04 20:38:29 -07001475 char encrypted_state[PROPERTY_VALUE_MAX];
1476 int rc;
1477
1478 property_get("ro.crypto.state", encrypted_state, "");
1479 if (strcmp(encrypted_state, "encrypted") ) {
1480 SLOGE("device not encrypted, aborting");
1481 return -2;
1482 }
1483
1484 if (!master_key_saved) {
1485 SLOGE("encrypted fs not yet mounted, aborting");
1486 return -1;
1487 }
1488
1489 if (!saved_mount_point) {
1490 SLOGE("encrypted fs failed to save mount point, aborting");
1491 return -1;
1492 }
1493
Ken Sumrall160b4d62013-04-22 12:15:39 -07001494 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall3ad90722011-10-04 20:38:29 -07001495 SLOGE("Error getting crypt footer and key\n");
1496 return -1;
1497 }
1498
1499 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
1500 /* If the device has no password, then just say the password is valid */
1501 rc = 0;
1502 } else {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001503 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001504 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
1505 /* They match, the password is correct */
1506 rc = 0;
1507 } else {
1508 /* If incorrect, sleep for a bit to prevent dictionary attacks */
1509 sleep(1);
1510 rc = 1;
1511 }
1512 }
1513
1514 return rc;
1515}
1516
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001517/* Initialize a crypt_mnt_ftr structure. The keysize is
1518 * defaulted to 16 bytes, and the filesystem size to 0.
1519 * Presumably, at a minimum, the caller will update the
1520 * filesystem size and crypto_type_name after calling this function.
1521 */
1522static void cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
1523{
Ken Sumrall160b4d62013-04-22 12:15:39 -07001524 off64_t off;
1525
1526 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001527 ftr->magic = CRYPT_MNT_MAGIC;
Kenny Rootc96a5f82013-06-14 12:08:28 -07001528 ftr->major_version = CURRENT_MAJOR_VERSION;
1529 ftr->minor_version = CURRENT_MINOR_VERSION;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001530 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Jason parks70a4b3f2011-01-28 10:10:47 -06001531 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall160b4d62013-04-22 12:15:39 -07001532
Kenny Rootc4c70f12013-06-14 12:11:38 -07001533 ftr->kdf_type = KDF_SCRYPT;
1534 get_device_scrypt_params(ftr);
1535
Ken Sumrall160b4d62013-04-22 12:15:39 -07001536 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
1537 if (get_crypt_ftr_info(NULL, &off) == 0) {
1538 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
1539 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
1540 ftr->persist_data_size;
1541 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001542}
1543
Ken Sumrall29d8da82011-05-18 17:20:07 -07001544static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001545{
Ken Sumralle550f782013-08-20 13:48:23 -07001546 const char *args[10];
1547 char size_str[32]; /* Must be large enough to hold a %lld and null byte */
1548 int num_args;
1549 int status;
1550 int tmp;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001551 int rc = -1;
1552
Ken Sumrall29d8da82011-05-18 17:20:07 -07001553 if (type == EXT4_FS) {
Ken Sumralle550f782013-08-20 13:48:23 -07001554 args[0] = "/system/bin/make_ext4fs";
1555 args[1] = "-a";
1556 args[2] = "/data";
1557 args[3] = "-l";
1558 snprintf(size_str, sizeof(size_str), "%lld", size * 512);
1559 args[4] = size_str;
1560 args[5] = crypto_blkdev;
1561 num_args = 6;
1562 SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n",
1563 args[0], args[1], args[2], args[3], args[4], args[5]);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001564 } else if (type== FAT_FS) {
Ken Sumralle550f782013-08-20 13:48:23 -07001565 args[0] = "/system/bin/newfs_msdos";
1566 args[1] = "-F";
1567 args[2] = "32";
1568 args[3] = "-O";
1569 args[4] = "android";
1570 args[5] = "-c";
1571 args[6] = "8";
1572 args[7] = "-s";
1573 snprintf(size_str, sizeof(size_str), "%lld", size);
1574 args[8] = size_str;
1575 args[9] = crypto_blkdev;
1576 num_args = 10;
1577 SLOGI("Making empty filesystem with command %s %s %s %s %s %s %s %s %s %s\n",
1578 args[0], args[1], args[2], args[3], args[4], args[5],
1579 args[6], args[7], args[8], args[9]);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001580 } else {
1581 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
1582 return -1;
1583 }
1584
Ken Sumralle550f782013-08-20 13:48:23 -07001585 tmp = android_fork_execvp(num_args, (char **)args, &status, false, true);
1586
1587 if (tmp != 0) {
1588 SLOGE("Error creating empty filesystem on %s due to logwrap error\n", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001589 } else {
Ken Sumralle550f782013-08-20 13:48:23 -07001590 if (WIFEXITED(status)) {
1591 if (WEXITSTATUS(status)) {
1592 SLOGE("Error creating filesystem on %s, exit status %d ",
1593 crypto_blkdev, WEXITSTATUS(status));
1594 } else {
1595 SLOGD("Successfully created filesystem on %s\n", crypto_blkdev);
1596 rc = 0;
1597 }
1598 } else {
1599 SLOGE("Error creating filesystem on %s, did not exit normally\n", crypto_blkdev);
1600 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001601 }
1602
1603 return rc;
1604}
1605
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001606#define CRYPT_INPLACE_BUFSIZE 4096
1607#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / 512)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08001608
1609/* aligned 32K writes tends to make flash happy.
1610 * SD card association recommends it.
1611 */
1612#define BLOCKS_AT_A_TIME 8
1613
1614struct encryptGroupsData
1615{
1616 int realfd;
1617 int cryptofd;
1618 off64_t numblocks;
1619 off64_t one_pct, cur_pct, new_pct;
1620 off64_t blocks_already_done, tot_numblocks;
1621 char* real_blkdev, * crypto_blkdev;
1622 int count;
1623 off64_t offset;
1624 char* buffer;
1625};
1626
1627static void update_progress(struct encryptGroupsData* data)
1628{
1629 data->blocks_already_done++;
1630 data->new_pct = data->blocks_already_done / data->one_pct;
1631 if (data->new_pct > data->cur_pct) {
1632 char buf[8];
1633 data->cur_pct = data->new_pct;
1634 snprintf(buf, sizeof(buf), "%lld", data->cur_pct);
1635 property_set("vold.encrypt_progress", buf);
1636 }
1637}
1638
1639static int flush_outstanding_data(struct encryptGroupsData* data)
1640{
1641 if (data->count == 0) {
1642 return 0;
1643 }
1644
1645 SLOGV("Copying %d blocks at offset %llx", data->count, data->offset);
1646
1647 if (pread64(data->realfd, data->buffer,
1648 info.block_size * data->count, data->offset)
1649 <= 0) {
1650 SLOGE("Error reading real_blkdev %s for inplace encrypt",
1651 data->real_blkdev);
1652 return -1;
1653 }
1654
1655 if (pwrite64(data->cryptofd, data->buffer,
1656 info.block_size * data->count, data->offset)
1657 <= 0) {
1658 SLOGE("Error writing crypto_blkdev %s for inplace encrypt",
1659 data->crypto_blkdev);
1660 return -1;
1661 }
1662
1663 data->count = 0;
1664 return 0;
1665}
1666
1667static int encrypt_groups(struct encryptGroupsData* data)
1668{
1669 unsigned int i;
1670 u8 *block_bitmap = 0;
1671 unsigned int block;
1672 off64_t ret;
1673 int rc = -1;
1674
1675 data->buffer = malloc(info.block_size * BLOCKS_AT_A_TIME);
1676 if (!data->buffer) {
1677 SLOGE("Failed to allocate crypto buffer");
1678 goto errout;
1679 }
1680
1681 block_bitmap = malloc(info.block_size);
1682 if (!block_bitmap) {
1683 SLOGE("failed to allocate block bitmap");
1684 goto errout;
1685 }
1686
1687 for (i = 0; i < aux_info.groups; ++i) {
1688 SLOGI("Encrypting group %d", i);
1689
1690 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
1691 u32 block_count = min(info.blocks_per_group,
1692 aux_info.len_blocks - first_block);
1693
1694 off64_t offset = (u64)info.block_size
1695 * aux_info.bg_desc[i].bg_block_bitmap;
1696
1697 ret = pread64(data->realfd, block_bitmap, info.block_size, offset);
1698 if (ret != (int)info.block_size) {
1699 SLOGE("failed to read all of block group bitmap %d", i);
1700 goto errout;
1701 }
1702
1703 offset = (u64)info.block_size * first_block;
1704
1705 data->count = 0;
1706
1707 for (block = 0; block < block_count; block++) {
1708 update_progress(data);
1709 if (bitmap_get_bit(block_bitmap, block)) {
1710 if (data->count == 0) {
1711 data->offset = offset;
1712 }
1713 data->count++;
1714 } else {
1715 if (flush_outstanding_data(data)) {
1716 goto errout;
1717 }
1718 }
1719
1720 offset += info.block_size;
1721
1722 /* Write data if we are aligned or buffer size reached */
1723 if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0
1724 || data->count == BLOCKS_AT_A_TIME) {
1725 if (flush_outstanding_data(data)) {
1726 goto errout;
1727 }
1728 }
1729 }
1730 if (flush_outstanding_data(data)) {
1731 goto errout;
1732 }
1733 }
1734
1735 rc = 0;
1736
1737errout:
1738 free(data->buffer);
1739 free(block_bitmap);
1740 return rc;
1741}
1742
1743static int cryptfs_enable_inplace_ext4(char *crypto_blkdev,
1744 char *real_blkdev,
1745 off64_t size,
1746 off64_t *size_already_done,
1747 off64_t tot_size)
1748{
1749 int i;
1750 struct encryptGroupsData data;
1751 int rc = -1;
1752
1753 memset(&data, 0, sizeof(data));
1754 data.real_blkdev = real_blkdev;
1755 data.crypto_blkdev = crypto_blkdev;
1756
1757 if ( (data.realfd = open(real_blkdev, O_RDWR)) < 0) {
1758 SLOGE("Error opening real_blkdev %s for inplace encrypt\n",
1759 real_blkdev);
1760 goto errout;
1761 }
1762
1763 if ( (data.cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
1764 SLOGE("Error opening crypto_blkdev %s for inplace encrypt\n",
1765 crypto_blkdev);
1766 goto errout;
1767 }
1768
1769 if (setjmp(setjmp_env)) {
1770 SLOGE("Reading extent caused an exception");
1771 goto errout;
1772 }
1773
1774 if (read_ext(data.realfd, 0) != 0) {
1775 SLOGE("Failed to read extent");
1776 goto errout;
1777 }
1778
1779 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
1780 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
1781 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
1782
1783 SLOGI("Encrypting filesystem in place...");
1784
1785 data.one_pct = data.tot_numblocks / 100;
1786 data.cur_pct = 0;
1787
1788 rc = encrypt_groups(&data);
1789 if (rc) {
1790 SLOGE("Error encrypting groups");
1791 goto errout;
1792 }
1793
1794 *size_already_done += size;
1795 rc = 0;
1796
1797errout:
1798 close(data.realfd);
1799 close(data.cryptofd);
1800
1801 return rc;
1802}
1803
1804static int cryptfs_enable_inplace_full(char *crypto_blkdev, char *real_blkdev,
1805 off64_t size, off64_t *size_already_done,
1806 off64_t tot_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001807{
1808 int realfd, cryptofd;
1809 char *buf[CRYPT_INPLACE_BUFSIZE];
1810 int rc = -1;
1811 off64_t numblocks, i, remainder;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001812 off64_t one_pct, cur_pct, new_pct;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001813 off64_t blocks_already_done, tot_numblocks;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001814
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001815 if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) {
1816 SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
1817 return -1;
1818 }
1819
1820 if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
1821 SLOGE("Error opening crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1822 close(realfd);
1823 return -1;
1824 }
1825
1826 /* This is pretty much a simple loop of reading 4K, and writing 4K.
1827 * The size passed in is the number of 512 byte sectors in the filesystem.
1828 * So compute the number of whole 4K blocks we should read/write,
1829 * and the remainder.
1830 */
1831 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
1832 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001833 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
1834 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001835
1836 SLOGE("Encrypting filesystem in place...");
1837
Ken Sumrall29d8da82011-05-18 17:20:07 -07001838 one_pct = tot_numblocks / 100;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001839 cur_pct = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001840 /* process the majority of the filesystem in blocks */
1841 for (i=0; i<numblocks; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001842 new_pct = (i + blocks_already_done) / one_pct;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001843 if (new_pct > cur_pct) {
1844 char buf[8];
1845
1846 cur_pct = new_pct;
1847 snprintf(buf, sizeof(buf), "%lld", cur_pct);
1848 property_set("vold.encrypt_progress", buf);
1849 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001850 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
1851 SLOGE("Error reading real_blkdev %s for inplace encrypt\n", crypto_blkdev);
1852 goto errout;
1853 }
1854 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
1855 SLOGE("Error writing crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1856 goto errout;
1857 }
1858 }
1859
1860 /* Do any remaining sectors */
1861 for (i=0; i<remainder; i++) {
1862 if (unix_read(realfd, buf, 512) <= 0) {
1863 SLOGE("Error reading rival sectors from real_blkdev %s for inplace encrypt\n", crypto_blkdev);
1864 goto errout;
1865 }
1866 if (unix_write(cryptofd, buf, 512) <= 0) {
1867 SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1868 goto errout;
1869 }
1870 }
1871
Ken Sumrall29d8da82011-05-18 17:20:07 -07001872 *size_already_done += size;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001873 rc = 0;
1874
1875errout:
1876 close(realfd);
1877 close(cryptofd);
1878
1879 return rc;
1880}
1881
Paul Lawrenceae59fe62014-01-21 08:23:27 -08001882static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev,
1883 off64_t size, off64_t *size_already_done,
1884 off64_t tot_size)
1885{
1886 if (cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev,
1887 size, size_already_done, tot_size) == 0) {
1888 return 0;
1889 }
1890
1891 return cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev,
1892 size, size_already_done, tot_size);
1893}
1894
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001895#define CRYPTO_ENABLE_WIPE 1
1896#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001897
1898#define FRAMEWORK_BOOT_WAIT 60
1899
Ken Sumrall29d8da82011-05-18 17:20:07 -07001900static inline int should_encrypt(struct volume_info *volume)
1901{
Paul Lawrenceae59fe62014-01-21 08:23:27 -08001902 return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) ==
Ken Sumrall29d8da82011-05-18 17:20:07 -07001903 (VOL_ENCRYPTABLE | VOL_NONREMOVABLE);
1904}
1905
Paul Lawrence13486032014-02-03 13:28:11 -08001906int cryptfs_enable_internal(char *howarg, int crypt_type, char *passwd,
1907 int allow_reboot)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001908{
1909 int how = 0;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001910 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN], sd_crypto_blkdev[MAXPATHLEN];
Ken Sumralle5032c42012-04-01 23:58:44 -07001911 unsigned long nr_sec;
Ken Sumrall160b4d62013-04-22 12:15:39 -07001912 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Ken Sumrall319b1042011-06-14 14:01:55 -07001913 int rc=-1, fd, i, ret;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001914 struct crypt_mnt_ftr crypt_ftr, sd_crypt_ftr;;
Ken Sumrall160b4d62013-04-22 12:15:39 -07001915 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001916 char tmpfs_options[PROPERTY_VALUE_MAX];
1917 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001918 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07001919 char key_loc[PROPERTY_VALUE_MAX];
1920 char fuse_sdcard[PROPERTY_VALUE_MAX];
1921 char *sd_mnt_point;
1922 char sd_blk_dev[256] = { 0 };
1923 int num_vols;
1924 struct volume_info *vol_list = 0;
1925 off64_t cur_encryption_done=0, tot_encryption_size=0;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001926
1927 property_get("ro.crypto.state", encrypted_state, "");
JP Abgrall502dc742013-11-01 13:06:20 -07001928 if (!strcmp(encrypted_state, "encrypted")) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001929 SLOGE("Device is already running encrypted, aborting");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001930 goto error_unencrypted;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001931 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001932
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001933 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001934
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001935 if (!strcmp(howarg, "wipe")) {
1936 how = CRYPTO_ENABLE_WIPE;
1937 } else if (! strcmp(howarg, "inplace")) {
1938 how = CRYPTO_ENABLE_INPLACE;
1939 } else {
1940 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08001941 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001942 }
1943
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001944 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001945
Ken Sumrall3ed82362011-01-28 23:31:16 -08001946 /* Get the size of the real block device */
1947 fd = open(real_blkdev, O_RDONLY);
1948 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
1949 SLOGE("Cannot get size of block device %s\n", real_blkdev);
1950 goto error_unencrypted;
1951 }
1952 close(fd);
1953
1954 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001955 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001956 unsigned int fs_size_sec, max_fs_size_sec;
1957
1958 fs_size_sec = get_fs_size(real_blkdev);
1959 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
1960
1961 if (fs_size_sec > max_fs_size_sec) {
1962 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
1963 goto error_unencrypted;
1964 }
1965 }
1966
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001967 /* Get a wakelock as this may take a while, and we don't want the
1968 * device to sleep on us. We'll grab a partial wakelock, and if the UI
1969 * wants to keep the screen on, it can grab a full wakelock.
1970 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001971 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001972 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
1973
Jeff Sharkey7382f812012-08-23 14:08:59 -07001974 /* Get the sdcard mount point */
Jeff Sharkeyb77bc462012-10-01 14:36:26 -07001975 sd_mnt_point = getenv("EMULATED_STORAGE_SOURCE");
Jeff Sharkey7382f812012-08-23 14:08:59 -07001976 if (!sd_mnt_point) {
1977 sd_mnt_point = getenv("EXTERNAL_STORAGE");
1978 }
1979 if (!sd_mnt_point) {
1980 sd_mnt_point = "/mnt/sdcard";
1981 }
Ken Sumrall29d8da82011-05-18 17:20:07 -07001982
1983 num_vols=vold_getNumDirectVolumes();
1984 vol_list = malloc(sizeof(struct volume_info) * num_vols);
1985 vold_getDirectVolumeList(vol_list);
1986
1987 for (i=0; i<num_vols; i++) {
1988 if (should_encrypt(&vol_list[i])) {
1989 fd = open(vol_list[i].blk_dev, O_RDONLY);
1990 if ( (vol_list[i].size = get_blkdev_size(fd)) == 0) {
1991 SLOGE("Cannot get size of block device %s\n", vol_list[i].blk_dev);
1992 goto error_unencrypted;
1993 }
1994 close(fd);
1995
Ken Sumrall3b170052011-07-11 15:38:57 -07001996 ret=vold_disableVol(vol_list[i].label);
Ken Sumrall319b1042011-06-14 14:01:55 -07001997 if ((ret < 0) && (ret != UNMOUNT_NOT_MOUNTED_ERR)) {
1998 /* -2 is returned when the device exists but is not currently mounted.
1999 * ignore the error and continue. */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002000 SLOGE("Failed to unmount volume %s\n", vol_list[i].label);
2001 goto error_unencrypted;
2002 }
2003 }
2004 }
2005
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002006 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002007 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002008 */
2009 property_set("vold.decrypt", "trigger_shutdown_framework");
2010 SLOGD("Just asked init to shut down class main\n");
2011
Ken Sumrall425524d2012-06-14 20:55:28 -07002012 if (vold_unmountAllAsecs()) {
2013 /* Just report the error. If any are left mounted,
2014 * umounting /data below will fail and handle the error.
2015 */
2016 SLOGE("Error unmounting internal asecs");
2017 }
2018
Ken Sumrall29d8da82011-05-18 17:20:07 -07002019 property_get("ro.crypto.fuse_sdcard", fuse_sdcard, "");
2020 if (!strcmp(fuse_sdcard, "true")) {
2021 /* This is a device using the fuse layer to emulate the sdcard semantics
2022 * on top of the userdata partition. vold does not manage it, it is managed
2023 * by the sdcard service. The sdcard service was killed by the property trigger
2024 * above, so just unmount it now. We must do this _AFTER_ killing the framework,
2025 * unlike the case for vold managed devices above.
2026 */
2027 if (wait_and_unmount(sd_mnt_point)) {
2028 goto error_shutting_down;
2029 }
Ken Sumrall2eaf7132011-01-14 12:45:48 -08002030 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002031
2032 /* Now unmount the /data partition. */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002033 if (wait_and_unmount(DATA_MNT_POINT)) {
JP Abgrall502dc742013-11-01 13:06:20 -07002034 if (allow_reboot) {
2035 goto error_shutting_down;
2036 } else {
2037 goto error_unencrypted;
2038 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002039 }
2040
2041 /* Do extra work for a better UX when doing the long inplace encryption */
2042 if (how == CRYPTO_ENABLE_INPLACE) {
2043 /* Now that /data is unmounted, we need to mount a tmpfs
2044 * /data, set a property saying we're doing inplace encryption,
2045 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002046 */
Ken Sumralle5032c42012-04-01 23:58:44 -07002047 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002048 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002049 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002050 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08002051 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002052
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002053 /* restart the framework. */
2054 /* Create necessary paths on /data */
2055 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002056 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002057 }
2058
Ken Sumrall92736ef2012-10-17 20:57:14 -07002059 /* Ugh, shutting down the framework is not synchronous, so until it
2060 * can be fixed, this horrible hack will wait a moment for it all to
2061 * shut down before proceeding. Without it, some devices cannot
2062 * restart the graphics services.
2063 */
2064 sleep(2);
2065
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002066 /* startup service classes main and late_start */
2067 property_set("vold.decrypt", "trigger_restart_min_framework");
2068 SLOGD("Just triggered restart_min_framework\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002069
Ken Sumrall7df84122011-01-18 14:04:08 -08002070 /* OK, the framework is restarted and will soon be showing a
2071 * progress bar. Time to setup an encrypted mapping, and
2072 * either write a new filesystem, or encrypt in place updating
2073 * the progress bar as we work.
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002074 */
2075 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002076
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002077 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002078 /* Initialize a crypt_mnt_ftr for the partition */
2079 cryptfs_init_crypt_mnt_ftr(&crypt_ftr);
Ken Sumrall160b4d62013-04-22 12:15:39 -07002080
Ken Sumrall29d8da82011-05-18 17:20:07 -07002081 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
2082 crypt_ftr.fs_size = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
2083 } else {
2084 crypt_ftr.fs_size = nr_sec;
2085 }
Ken Sumralld33d4172011-02-01 00:49:13 -08002086 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence13486032014-02-03 13:28:11 -08002087 crypt_ftr.crypt_type = crypt_type;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002088 strcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");
2089
2090 /* Make an encrypted master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -07002091 if (create_encrypted_random_key(passwd, crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002092 SLOGE("Cannot create encrypted master key\n");
JP Abgrall502dc742013-11-01 13:06:20 -07002093 goto error_shutting_down;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002094 }
2095
2096 /* Write the key to the end of the partition */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002097 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002098
Ken Sumrall160b4d62013-04-22 12:15:39 -07002099 /* If any persistent data has been remembered, save it.
2100 * If none, create a valid empty table and save that.
2101 */
2102 if (!persist_data) {
2103 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
2104 if (pdata) {
2105 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
2106 persist_data = pdata;
2107 }
2108 }
2109 if (persist_data) {
2110 save_persistent_data();
2111 }
2112
JP Abgrall7bdfa522013-11-15 13:42:56 -08002113 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002114 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
2115 "userdata");
2116
Ken Sumrall128626f2011-06-28 18:45:14 -07002117 /* The size of the userdata partition, and add in the vold volumes below */
2118 tot_encryption_size = crypt_ftr.fs_size;
2119
Ken Sumrall29d8da82011-05-18 17:20:07 -07002120 /* setup crypto mapping for all encryptable volumes handled by vold */
2121 for (i=0; i<num_vols; i++) {
2122 if (should_encrypt(&vol_list[i])) {
2123 vol_list[i].crypt_ftr = crypt_ftr; /* gotta love struct assign */
2124 vol_list[i].crypt_ftr.fs_size = vol_list[i].size;
2125 create_crypto_blk_dev(&vol_list[i].crypt_ftr, decrypted_master_key,
2126 vol_list[i].blk_dev, vol_list[i].crypto_blkdev,
2127 vol_list[i].label);
Ken Sumrall128626f2011-06-28 18:45:14 -07002128 tot_encryption_size += vol_list[i].size;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002129 }
2130 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002131
2132 if (how == CRYPTO_ENABLE_WIPE) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07002133 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr.fs_size, EXT4_FS);
2134 /* Encrypt all encryptable volumes handled by vold */
2135 if (!rc) {
2136 for (i=0; i<num_vols; i++) {
2137 if (should_encrypt(&vol_list[i])) {
2138 rc = cryptfs_enable_wipe(vol_list[i].crypto_blkdev,
2139 vol_list[i].crypt_ftr.fs_size, FAT_FS);
2140 }
2141 }
2142 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002143 } else if (how == CRYPTO_ENABLE_INPLACE) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07002144 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, crypt_ftr.fs_size,
2145 &cur_encryption_done, tot_encryption_size);
2146 /* Encrypt all encryptable volumes handled by vold */
2147 if (!rc) {
2148 for (i=0; i<num_vols; i++) {
2149 if (should_encrypt(&vol_list[i])) {
2150 rc = cryptfs_enable_inplace(vol_list[i].crypto_blkdev,
2151 vol_list[i].blk_dev,
2152 vol_list[i].crypt_ftr.fs_size,
2153 &cur_encryption_done, tot_encryption_size);
2154 }
2155 }
2156 }
2157 if (!rc) {
2158 /* The inplace routine never actually sets the progress to 100%
2159 * due to the round down nature of integer division, so set it here */
2160 property_set("vold.encrypt_progress", "100");
2161 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002162 } else {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002163 /* Shouldn't happen */
2164 SLOGE("cryptfs_enable: internal error, unknown option\n");
JP Abgrall502dc742013-11-01 13:06:20 -07002165 goto error_shutting_down;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002166 }
2167
2168 /* Undo the dm-crypt mapping whether we succeed or not */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002169 delete_crypto_blk_dev("userdata");
2170 for (i=0; i<num_vols; i++) {
2171 if (should_encrypt(&vol_list[i])) {
2172 delete_crypto_blk_dev(vol_list[i].label);
2173 }
2174 }
2175
2176 free(vol_list);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002177
2178 if (! rc) {
2179 /* Success */
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08002180
Ken Sumralld33d4172011-02-01 00:49:13 -08002181 /* Clear the encryption in progres flag in the footer */
2182 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002183 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08002184
Ken Sumrall29d8da82011-05-18 17:20:07 -07002185 sleep(2); /* Give the UI a chance to show 100% progress */
Ken Sumralladfba362013-06-04 16:37:52 -07002186 cryptfs_reboot(0);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002187 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002188 char value[PROPERTY_VALUE_MAX];
2189
Ken Sumrall319369a2012-06-27 16:30:18 -07002190 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002191 if (!strcmp(value, "1")) {
2192 /* wipe data if encryption failed */
2193 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
2194 mkdir("/cache/recovery", 0700);
Nick Kralevich4684e582012-06-26 15:07:03 -07002195 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC, 0600);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002196 if (fd >= 0) {
2197 write(fd, "--wipe_data", strlen("--wipe_data") + 1);
2198 close(fd);
2199 } else {
2200 SLOGE("could not open /cache/recovery/command\n");
2201 }
Ken Sumralladfba362013-06-04 16:37:52 -07002202 cryptfs_reboot(1);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002203 } else {
2204 /* set property to trigger dialog */
2205 property_set("vold.encrypt_progress", "error_partially_encrypted");
2206 release_wake_lock(lockid);
2207 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002208 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002209 }
2210
Ken Sumrall3ed82362011-01-28 23:31:16 -08002211 /* hrm, the encrypt step claims success, but the reboot failed.
2212 * This should not happen.
2213 * Set the property and return. Hope the framework can deal with it.
2214 */
2215 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002216 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002217 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08002218
2219error_unencrypted:
Ken Sumrall29d8da82011-05-18 17:20:07 -07002220 free(vol_list);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002221 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002222 if (lockid[0]) {
2223 release_wake_lock(lockid);
2224 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002225 return -1;
2226
2227error_shutting_down:
2228 /* we failed, and have not encrypted anthing, so the users's data is still intact,
2229 * but the framework is stopped and not restarted to show the error, so it's up to
2230 * vold to restart the system.
2231 */
2232 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Ken Sumralladfba362013-06-04 16:37:52 -07002233 cryptfs_reboot(0);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002234
2235 /* shouldn't get here */
2236 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall29d8da82011-05-18 17:20:07 -07002237 free(vol_list);
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002238 if (lockid[0]) {
2239 release_wake_lock(lockid);
2240 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002241 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002242}
2243
Paul Lawrence13486032014-02-03 13:28:11 -08002244int cryptfs_enable(char *howarg, char *passwd, int allow_reboot)
2245{
2246 /** @todo If we keep this route (user selected encryption)
2247 * need to take a type in and pass it to here.
2248 */
2249 return cryptfs_enable_internal(howarg, CRYPT_TYPE_PASSWORD,
2250 passwd, allow_reboot);
2251}
2252
2253int cryptfs_enable_default(char *howarg, int allow_reboot)
2254{
2255 return cryptfs_enable_internal(howarg, CRYPT_TYPE_DEFAULT,
2256 DEFAULT_PASSWORD, allow_reboot);
2257}
2258
2259int cryptfs_changepw(int crypt_type, const char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002260{
2261 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002262 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002263
2264 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08002265 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08002266 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002267 return -1;
2268 }
2269
Paul Lawrencef4faa572014-01-29 13:31:03 -08002270 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2271 SLOGE("Invalid crypt_type %d", crypt_type);
2272 return -1;
2273 }
2274
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002275 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002276 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08002277 SLOGE("Error getting crypt footer and key");
2278 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002279 }
2280
Paul Lawrencef4faa572014-01-29 13:31:03 -08002281 crypt_ftr.crypt_type = crypt_type;
2282
2283 encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
2284 : newpw,
2285 crypt_ftr.salt,
2286 saved_master_key,
2287 crypt_ftr.master_key,
2288 &crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002289
Jason parks70a4b3f2011-01-28 10:10:47 -06002290 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002291 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002292
2293 return 0;
2294}
Ken Sumrall160b4d62013-04-22 12:15:39 -07002295
2296static int persist_get_key(char *fieldname, char *value)
2297{
2298 unsigned int i;
2299
2300 if (persist_data == NULL) {
2301 return -1;
2302 }
2303 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2304 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2305 /* We found it! */
2306 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
2307 return 0;
2308 }
2309 }
2310
2311 return -1;
2312}
2313
2314static int persist_set_key(char *fieldname, char *value, int encrypted)
2315{
2316 unsigned int i;
2317 unsigned int num;
2318 struct crypt_mnt_ftr crypt_ftr;
2319 unsigned int max_persistent_entries;
2320 unsigned int dsize;
2321
2322 if (persist_data == NULL) {
2323 return -1;
2324 }
2325
2326 /* If encrypted, use the values from the crypt_ftr, otherwise
2327 * use the values for the current spec.
2328 */
2329 if (encrypted) {
2330 if(get_crypt_ftr_and_key(&crypt_ftr)) {
2331 return -1;
2332 }
2333 dsize = crypt_ftr.persist_data_size;
2334 } else {
2335 dsize = CRYPT_PERSIST_DATA_SIZE;
2336 }
2337 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
2338 sizeof(struct crypt_persist_entry);
2339
2340 num = persist_data->persist_valid_entries;
2341
2342 for (i = 0; i < num; i++) {
2343 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2344 /* We found an existing entry, update it! */
2345 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
2346 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
2347 return 0;
2348 }
2349 }
2350
2351 /* We didn't find it, add it to the end, if there is room */
2352 if (persist_data->persist_valid_entries < max_persistent_entries) {
2353 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
2354 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
2355 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
2356 persist_data->persist_valid_entries++;
2357 return 0;
2358 }
2359
2360 return -1;
2361}
2362
2363/* Return the value of the specified field. */
2364int cryptfs_getfield(char *fieldname, char *value, int len)
2365{
2366 char temp_value[PROPERTY_VALUE_MAX];
2367 char real_blkdev[MAXPATHLEN];
2368 /* 0 is success, 1 is not encrypted,
2369 * -1 is value not set, -2 is any other error
2370 */
2371 int rc = -2;
2372
2373 if (persist_data == NULL) {
2374 load_persistent_data();
2375 if (persist_data == NULL) {
2376 SLOGE("Getfield error, cannot load persistent data");
2377 goto out;
2378 }
2379 }
2380
2381 if (!persist_get_key(fieldname, temp_value)) {
2382 /* We found it, copy it to the caller's buffer and return */
2383 strlcpy(value, temp_value, len);
2384 rc = 0;
2385 } else {
2386 /* Sadness, it's not there. Return the error */
2387 rc = -1;
2388 }
2389
2390out:
2391 return rc;
2392}
2393
2394/* Set the value of the specified field. */
2395int cryptfs_setfield(char *fieldname, char *value)
2396{
2397 struct crypt_persist_data stored_pdata;
2398 struct crypt_persist_data *pdata_p;
2399 struct crypt_mnt_ftr crypt_ftr;
2400 char encrypted_state[PROPERTY_VALUE_MAX];
2401 /* 0 is success, -1 is an error */
2402 int rc = -1;
2403 int encrypted = 0;
2404
2405 if (persist_data == NULL) {
2406 load_persistent_data();
2407 if (persist_data == NULL) {
2408 SLOGE("Setfield error, cannot load persistent data");
2409 goto out;
2410 }
2411 }
2412
2413 property_get("ro.crypto.state", encrypted_state, "");
2414 if (!strcmp(encrypted_state, "encrypted") ) {
2415 encrypted = 1;
2416 }
2417
2418 if (persist_set_key(fieldname, value, encrypted)) {
2419 goto out;
2420 }
2421
2422 /* If we are running encrypted, save the persistent data now */
2423 if (encrypted) {
2424 if (save_persistent_data()) {
2425 SLOGE("Setfield error, cannot save persistent data");
2426 goto out;
2427 }
2428 }
2429
2430 rc = 0;
2431
2432out:
2433 return rc;
2434}
Paul Lawrencef4faa572014-01-29 13:31:03 -08002435
2436/* Checks userdata. Attempt to mount the volume if default-
2437 * encrypted.
2438 * On success trigger next init phase and return 0.
2439 * Currently do not handle failure - see TODO below.
2440 */
2441int cryptfs_mount_default_encrypted(void)
2442{
2443 char decrypt_state[PROPERTY_VALUE_MAX];
2444 property_get("vold.decrypt", decrypt_state, "0");
2445 if (!strcmp(decrypt_state, "0")) {
2446 SLOGE("Not encrypted - should not call here");
2447 } else {
2448 int crypt_type = cryptfs_get_password_type();
2449 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2450 SLOGE("Bad crypt type - error");
2451 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
2452 SLOGD("Password is not default - "
2453 "starting min framework to prompt");
2454 property_set("vold.decrypt", "trigger_restart_min_framework");
2455 return 0;
2456 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
2457 SLOGD("Password is default - restarting filesystem");
2458 cryptfs_restart_internal(0);
2459 return 0;
2460 } else {
2461 SLOGE("Encrypted, default crypt type but can't decrypt");
2462 }
2463 }
2464
2465 /** @TODO make sure we factory wipe in this situation
2466 * In general if we got here there is no recovery
2467 */
2468 return 0;
2469}
2470
2471/* Returns type of the password, default, pattern, pin or password.
2472 */
2473int cryptfs_get_password_type(void)
2474{
2475 struct crypt_mnt_ftr crypt_ftr;
2476
2477 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2478 SLOGE("Error getting crypt footer and key\n");
2479 return -1;
2480 }
2481
2482 return crypt_ftr.crypt_type;
2483}