blob: f8d14cf0a4e005030d1091e1d979c6147d9aeee2 [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
Paul Lawrence684dbdf2014-02-07 12:07:22 -080082/* Set when userdata is successfully decrypted and mounted.
83 * Reset whenever read (via cryptfs_just_decrypted)
84 * (Read by keyguard to avoid a double prompt.)
85 */
86static int just_decrypted = 0;
87
Ken Sumrall56ad03c2013-02-13 13:00:19 -080088extern struct fstab *fstab;
Ken Sumrall8ddbe402011-01-17 15:26:29 -080089
Ken Sumralladfba362013-06-04 16:37:52 -070090static void cryptfs_reboot(int recovery)
91{
92 if (recovery) {
93 property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
94 } else {
95 property_set(ANDROID_RB_PROPERTY, "reboot");
96 }
97 sleep(20);
98
99 /* Shouldn't get here, reboot should happen before sleep times out */
100 return;
101}
102
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800103static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
104{
105 memset(io, 0, dataSize);
106 io->data_size = dataSize;
107 io->data_start = sizeof(struct dm_ioctl);
108 io->version[0] = 4;
109 io->version[1] = 0;
110 io->version[2] = 0;
111 io->flags = flags;
112 if (name) {
113 strncpy(io->name, name, sizeof(io->name));
114 }
115}
116
Kenny Rootc4c70f12013-06-14 12:11:38 -0700117/**
118 * Gets the default device scrypt parameters for key derivation time tuning.
119 * The parameters should lead to about one second derivation time for the
120 * given device.
121 */
122static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
123 const int default_params[] = SCRYPT_DEFAULTS;
124 int params[] = SCRYPT_DEFAULTS;
125 char paramstr[PROPERTY_VALUE_MAX];
126 char *token;
127 char *saveptr;
128 int i;
129
130 property_get(SCRYPT_PROP, paramstr, "");
131 if (paramstr[0] != '\0') {
132 /*
133 * The token we're looking for should be three integers separated by
134 * colons (e.g., "12:8:1"). Scan the property to make sure it matches.
135 */
Kenny Root2947e342013-08-14 15:54:49 -0700136 for (i = 0, token = strtok_r(paramstr, ":", &saveptr);
137 token != NULL && i < 3;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700138 i++, token = strtok_r(NULL, ":", &saveptr)) {
139 char *endptr;
140 params[i] = strtol(token, &endptr, 10);
141
142 /*
143 * Check that there was a valid number and it's 8-bit. If not,
144 * break out and the end check will take the default values.
145 */
146 if ((*token == '\0') || (*endptr != '\0') || params[i] < 0 || params[i] > 255) {
147 break;
148 }
149 }
150
151 /*
152 * If there were not enough tokens or a token was malformed (not an
153 * integer), it will end up here and the default parameters can be
154 * taken.
155 */
156 if ((i != 3) || (token != NULL)) {
157 SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
158 memcpy(params, default_params, sizeof(params));
159 }
160 }
161
162 ftr->N_factor = params[0];
163 ftr->r_factor = params[1];
164 ftr->p_factor = params[2];
165}
166
Ken Sumrall3ed82362011-01-28 23:31:16 -0800167static unsigned int get_fs_size(char *dev)
168{
169 int fd, block_size;
170 struct ext4_super_block sb;
171 off64_t len;
172
173 if ((fd = open(dev, O_RDONLY)) < 0) {
174 SLOGE("Cannot open device to get filesystem size ");
175 return 0;
176 }
177
178 if (lseek64(fd, 1024, SEEK_SET) < 0) {
179 SLOGE("Cannot seek to superblock");
180 return 0;
181 }
182
183 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
184 SLOGE("Cannot read superblock");
185 return 0;
186 }
187
188 close(fd);
189
190 block_size = 1024 << sb.s_log_block_size;
191 /* compute length in bytes */
192 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
193
194 /* return length in sectors */
195 return (unsigned int) (len / 512);
196}
197
Ken Sumrall160b4d62013-04-22 12:15:39 -0700198static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
199{
200 static int cached_data = 0;
201 static off64_t cached_off = 0;
202 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
203 int fd;
204 char key_loc[PROPERTY_VALUE_MAX];
205 char real_blkdev[PROPERTY_VALUE_MAX];
206 unsigned int nr_sec;
207 int rc = -1;
208
209 if (!cached_data) {
210 fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
211
212 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
213 if ( (fd = open(real_blkdev, O_RDWR)) < 0) {
214 SLOGE("Cannot open real block device %s\n", real_blkdev);
215 return -1;
216 }
217
218 if ((nr_sec = get_blkdev_size(fd))) {
219 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
220 * encryption info footer and key, and plenty of bytes to spare for future
221 * growth.
222 */
223 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
224 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
225 cached_data = 1;
226 } else {
227 SLOGE("Cannot get size of block device %s\n", real_blkdev);
228 }
229 close(fd);
230 } else {
231 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
232 cached_off = 0;
233 cached_data = 1;
234 }
235 }
236
237 if (cached_data) {
238 if (metadata_fname) {
239 *metadata_fname = cached_metadata_fname;
240 }
241 if (off) {
242 *off = cached_off;
243 }
244 rc = 0;
245 }
246
247 return rc;
248}
249
Ken Sumralle8744072011-01-18 22:01:55 -0800250/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800251 * update the failed mount count but not change the key.
252 */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700253static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800254{
255 int fd;
256 unsigned int nr_sec, cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700257 /* starting_off is set to the SEEK_SET offset
258 * where the crypto structure starts
259 */
260 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800261 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700262 char *fname = NULL;
Ken Sumrall3be890f2011-09-14 16:53:46 -0700263 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800264
Ken Sumrall160b4d62013-04-22 12:15:39 -0700265 if (get_crypt_ftr_info(&fname, &starting_off)) {
266 SLOGE("Unable to get crypt_ftr_info\n");
267 return -1;
268 }
269 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700270 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700271 return -1;
272 }
Ken Sumralle550f782013-08-20 13:48:23 -0700273 if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) {
274 SLOGE("Cannot open footer file %s for put\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700275 return -1;
276 }
277
278 /* Seek to the start of the crypt footer */
279 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
280 SLOGE("Cannot seek to real block device footer\n");
281 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800282 }
283
284 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
285 SLOGE("Cannot write real block device footer\n");
286 goto errout;
287 }
288
Ken Sumrall3be890f2011-09-14 16:53:46 -0700289 fstat(fd, &statbuf);
290 /* If the keys are kept on a raw block device, do not try to truncate it. */
Ken Sumralle550f782013-08-20 13:48:23 -0700291 if (S_ISREG(statbuf.st_mode)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700292 if (ftruncate(fd, 0x4000)) {
Colin Cross59846b62014-02-06 20:34:29 -0800293 SLOGE("Cannot set footer file size\n");
Ken Sumralle8744072011-01-18 22:01:55 -0800294 goto errout;
295 }
296 }
297
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800298 /* Success! */
299 rc = 0;
300
301errout:
302 close(fd);
303 return rc;
304
305}
306
Ken Sumrall160b4d62013-04-22 12:15:39 -0700307static inline int unix_read(int fd, void* buff, int len)
308{
309 return TEMP_FAILURE_RETRY(read(fd, buff, len));
310}
311
312static inline int unix_write(int fd, const void* buff, int len)
313{
314 return TEMP_FAILURE_RETRY(write(fd, buff, len));
315}
316
317static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
318{
319 memset(pdata, 0, len);
320 pdata->persist_magic = PERSIST_DATA_MAGIC;
321 pdata->persist_valid_entries = 0;
322}
323
324/* A routine to update the passed in crypt_ftr to the lastest version.
325 * fd is open read/write on the device that holds the crypto footer and persistent
326 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
327 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
328 */
329static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
330{
Kenny Root7434b312013-06-14 11:29:53 -0700331 int orig_major = crypt_ftr->major_version;
332 int orig_minor = crypt_ftr->minor_version;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700333
Kenny Root7434b312013-06-14 11:29:53 -0700334 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
335 struct crypt_persist_data *pdata;
336 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700337
Kenny Rootc4c70f12013-06-14 12:11:38 -0700338 SLOGW("upgrading crypto footer to 1.1");
339
Kenny Root7434b312013-06-14 11:29:53 -0700340 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
341 if (pdata == NULL) {
342 SLOGE("Cannot allocate persisent data\n");
343 return;
344 }
345 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
346
347 /* Need to initialize the persistent data area */
348 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
349 SLOGE("Cannot seek to persisent data offset\n");
350 return;
351 }
352 /* Write all zeros to the first copy, making it invalid */
353 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
354
355 /* Write a valid but empty structure to the second copy */
356 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
357 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
358
359 /* Update the footer */
360 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
361 crypt_ftr->persist_data_offset[0] = pdata_offset;
362 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
363 crypt_ftr->minor_version = 1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700364 }
365
Paul Lawrencef4faa572014-01-29 13:31:03 -0800366 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700367 SLOGW("upgrading crypto footer to 1.2");
JP Abgrall7bdfa522013-11-15 13:42:56 -0800368 /* But keep the old kdf_type.
369 * It will get updated later to KDF_SCRYPT after the password has been verified.
370 */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700371 crypt_ftr->kdf_type = KDF_PBKDF2;
372 get_device_scrypt_params(crypt_ftr);
373 crypt_ftr->minor_version = 2;
374 }
375
Paul Lawrencef4faa572014-01-29 13:31:03 -0800376 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
377 SLOGW("upgrading crypto footer to 1.3");
378 crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
379 crypt_ftr->minor_version = 3;
380 }
381
Kenny Root7434b312013-06-14 11:29:53 -0700382 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
383 if (lseek64(fd, offset, SEEK_SET) == -1) {
384 SLOGE("Cannot seek to crypt footer\n");
385 return;
386 }
387 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700388 }
Ken Sumrall160b4d62013-04-22 12:15:39 -0700389}
390
391
392static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800393{
394 int fd;
395 unsigned int nr_sec, cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700396 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800397 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700398 char *fname = NULL;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700399 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800400
Ken Sumrall160b4d62013-04-22 12:15:39 -0700401 if (get_crypt_ftr_info(&fname, &starting_off)) {
402 SLOGE("Unable to get crypt_ftr_info\n");
403 return -1;
404 }
405 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700406 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700407 return -1;
408 }
409 if ( (fd = open(fname, O_RDWR)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700410 SLOGE("Cannot open footer file %s for get\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700411 return -1;
412 }
413
414 /* Make sure it's 16 Kbytes in length */
415 fstat(fd, &statbuf);
416 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
417 SLOGE("footer file %s is not the expected size!\n", fname);
418 goto errout;
419 }
420
421 /* Seek to the start of the crypt footer */
422 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
423 SLOGE("Cannot seek to real block device footer\n");
424 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800425 }
426
427 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
428 SLOGE("Cannot read real block device footer\n");
429 goto errout;
430 }
431
432 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700433 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800434 goto errout;
435 }
436
Kenny Rootc96a5f82013-06-14 12:08:28 -0700437 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
438 SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
439 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800440 goto errout;
441 }
442
Kenny Rootc96a5f82013-06-14 12:08:28 -0700443 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
444 SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
445 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800446 }
447
Ken Sumrall160b4d62013-04-22 12:15:39 -0700448 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
449 * copy on disk before returning.
450 */
Kenny Rootc96a5f82013-06-14 12:08:28 -0700451 if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700452 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
Ken Sumralle8744072011-01-18 22:01:55 -0800453 }
454
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800455 /* Success! */
456 rc = 0;
457
458errout:
459 close(fd);
460 return rc;
461}
462
Ken Sumrall160b4d62013-04-22 12:15:39 -0700463static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
464{
465 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
466 crypt_ftr->persist_data_offset[1]) {
467 SLOGE("Crypt_ftr persist data regions overlap");
468 return -1;
469 }
470
471 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
472 SLOGE("Crypt_ftr persist data region 0 starts after region 1");
473 return -1;
474 }
475
476 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
477 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
478 CRYPT_FOOTER_OFFSET) {
479 SLOGE("Persistent data extends past crypto footer");
480 return -1;
481 }
482
483 return 0;
484}
485
486static int load_persistent_data(void)
487{
488 struct crypt_mnt_ftr crypt_ftr;
489 struct crypt_persist_data *pdata = NULL;
490 char encrypted_state[PROPERTY_VALUE_MAX];
491 char *fname;
492 int found = 0;
493 int fd;
494 int ret;
495 int i;
496
497 if (persist_data) {
498 /* Nothing to do, we've already loaded or initialized it */
499 return 0;
500 }
501
502
503 /* If not encrypted, just allocate an empty table and initialize it */
504 property_get("ro.crypto.state", encrypted_state, "");
505 if (strcmp(encrypted_state, "encrypted") ) {
506 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
507 if (pdata) {
508 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
509 persist_data = pdata;
510 return 0;
511 }
512 return -1;
513 }
514
515 if(get_crypt_ftr_and_key(&crypt_ftr)) {
516 return -1;
517 }
518
519 if ((crypt_ftr.major_version != 1) || (crypt_ftr.minor_version != 1)) {
520 SLOGE("Crypt_ftr version doesn't support persistent data");
521 return -1;
522 }
523
524 if (get_crypt_ftr_info(&fname, NULL)) {
525 return -1;
526 }
527
528 ret = validate_persistent_data_storage(&crypt_ftr);
529 if (ret) {
530 return -1;
531 }
532
533 fd = open(fname, O_RDONLY);
534 if (fd < 0) {
535 SLOGE("Cannot open %s metadata file", fname);
536 return -1;
537 }
538
539 if (persist_data == NULL) {
540 pdata = malloc(crypt_ftr.persist_data_size);
541 if (pdata == NULL) {
542 SLOGE("Cannot allocate memory for persistent data");
543 goto err;
544 }
545 }
546
547 for (i = 0; i < 2; i++) {
548 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
549 SLOGE("Cannot seek to read persistent data on %s", fname);
550 goto err2;
551 }
552 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
553 SLOGE("Error reading persistent data on iteration %d", i);
554 goto err2;
555 }
556 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
557 found = 1;
558 break;
559 }
560 }
561
562 if (!found) {
563 SLOGI("Could not find valid persistent data, creating");
564 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
565 }
566
567 /* Success */
568 persist_data = pdata;
569 close(fd);
570 return 0;
571
572err2:
573 free(pdata);
574
575err:
576 close(fd);
577 return -1;
578}
579
580static int save_persistent_data(void)
581{
582 struct crypt_mnt_ftr crypt_ftr;
583 struct crypt_persist_data *pdata;
584 char *fname;
585 off64_t write_offset;
586 off64_t erase_offset;
587 int found = 0;
588 int fd;
589 int ret;
590
591 if (persist_data == NULL) {
592 SLOGE("No persistent data to save");
593 return -1;
594 }
595
596 if(get_crypt_ftr_and_key(&crypt_ftr)) {
597 return -1;
598 }
599
600 if ((crypt_ftr.major_version != 1) || (crypt_ftr.minor_version != 1)) {
601 SLOGE("Crypt_ftr version doesn't support persistent data");
602 return -1;
603 }
604
605 ret = validate_persistent_data_storage(&crypt_ftr);
606 if (ret) {
607 return -1;
608 }
609
610 if (get_crypt_ftr_info(&fname, NULL)) {
611 return -1;
612 }
613
614 fd = open(fname, O_RDWR);
615 if (fd < 0) {
616 SLOGE("Cannot open %s metadata file", fname);
617 return -1;
618 }
619
620 pdata = malloc(crypt_ftr.persist_data_size);
621 if (pdata == NULL) {
622 SLOGE("Cannot allocate persistant data");
623 goto err;
624 }
625
626 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
627 SLOGE("Cannot seek to read persistent data on %s", fname);
628 goto err2;
629 }
630
631 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
632 SLOGE("Error reading persistent data before save");
633 goto err2;
634 }
635
636 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
637 /* The first copy is the curent valid copy, so write to
638 * the second copy and erase this one */
639 write_offset = crypt_ftr.persist_data_offset[1];
640 erase_offset = crypt_ftr.persist_data_offset[0];
641 } else {
642 /* The second copy must be the valid copy, so write to
643 * the first copy, and erase the second */
644 write_offset = crypt_ftr.persist_data_offset[0];
645 erase_offset = crypt_ftr.persist_data_offset[1];
646 }
647
648 /* Write the new copy first, if successful, then erase the old copy */
649 if (lseek(fd, write_offset, SEEK_SET) < 0) {
650 SLOGE("Cannot seek to write persistent data");
651 goto err2;
652 }
653 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
654 (int) crypt_ftr.persist_data_size) {
655 if (lseek(fd, erase_offset, SEEK_SET) < 0) {
656 SLOGE("Cannot seek to erase previous persistent data");
657 goto err2;
658 }
659 fsync(fd);
660 memset(pdata, 0, crypt_ftr.persist_data_size);
661 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
662 (int) crypt_ftr.persist_data_size) {
663 SLOGE("Cannot write to erase previous persistent data");
664 goto err2;
665 }
666 fsync(fd);
667 } else {
668 SLOGE("Cannot write to save persistent data");
669 goto err2;
670 }
671
672 /* Success */
673 free(pdata);
674 close(fd);
675 return 0;
676
677err2:
678 free(pdata);
679err:
680 close(fd);
681 return -1;
682}
683
Paul Lawrencef4faa572014-01-29 13:31:03 -0800684static int hexdigit (char c)
685{
686 if (c >= '0' && c <= '9') return c - '0';
687 c = tolower(c);
688 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
689 return -1;
690}
691
692static unsigned char* convert_hex_ascii_to_key(const char* master_key_ascii,
693 unsigned int* out_keysize)
694{
695 unsigned int i;
696 *out_keysize = 0;
697
698 size_t size = strlen (master_key_ascii);
699 if (size % 2) {
700 SLOGE("Trying to convert ascii string of odd length");
701 return NULL;
702 }
703
704 unsigned char* master_key = (unsigned char*) malloc(size / 2);
705 if (master_key == 0) {
706 SLOGE("Cannot allocate");
707 return NULL;
708 }
709
710 for (i = 0; i < size; i += 2) {
711 int high_nibble = hexdigit (master_key_ascii[i]);
712 int low_nibble = hexdigit (master_key_ascii[i + 1]);
713
714 if(high_nibble < 0 || low_nibble < 0) {
715 SLOGE("Invalid hex string");
716 free (master_key);
717 return NULL;
718 }
719
720 master_key[*out_keysize] = high_nibble * 16 + low_nibble;
721 (*out_keysize)++;
722 }
723
724 return master_key;
725}
726
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800727/* Convert a binary key of specified length into an ascii hex string equivalent,
728 * without the leading 0x and with null termination
729 */
Paul Lawrencef4faa572014-01-29 13:31:03 -0800730static void convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800731 char *master_key_ascii)
732{
733 unsigned int i, a;
734 unsigned char nibble;
735
736 for (i=0, a=0; i<keysize; i++, a+=2) {
737 /* For each byte, write out two ascii hex digits */
738 nibble = (master_key[i] >> 4) & 0xf;
739 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
740
741 nibble = master_key[i] & 0xf;
742 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
743 }
744
745 /* Add the null termination */
746 master_key_ascii[a] = '\0';
747
748}
749
Ken Sumralldb5e0262013-02-05 17:39:48 -0800750static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
751 char *real_blk_name, const char *name, int fd,
752 char *extra_params)
753{
754 char buffer[DM_CRYPT_BUF_SIZE];
755 struct dm_ioctl *io;
756 struct dm_target_spec *tgt;
757 char *crypt_params;
758 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
759 int i;
760
761 io = (struct dm_ioctl *) buffer;
762
763 /* Load the mapping table for this device */
764 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
765
766 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
767 io->target_count = 1;
768 tgt->status = 0;
769 tgt->sector_start = 0;
770 tgt->length = crypt_ftr->fs_size;
771 strcpy(tgt->target_type, "crypt");
772
773 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
774 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
775 sprintf(crypt_params, "%s %s 0 %s 0 %s", crypt_ftr->crypto_type_name,
776 master_key_ascii, real_blk_name, extra_params);
777 crypt_params += strlen(crypt_params) + 1;
778 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
779 tgt->next = crypt_params - buffer;
780
781 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
782 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
783 break;
784 }
785 usleep(500000);
786 }
787
788 if (i == TABLE_LOAD_RETRIES) {
789 /* We failed to load the table, return an error */
790 return -1;
791 } else {
792 return i + 1;
793 }
794}
795
796
797static int get_dm_crypt_version(int fd, const char *name, int *version)
798{
799 char buffer[DM_CRYPT_BUF_SIZE];
800 struct dm_ioctl *io;
801 struct dm_target_versions *v;
802 int i;
803
804 io = (struct dm_ioctl *) buffer;
805
806 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
807
808 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
809 return -1;
810 }
811
812 /* Iterate over the returned versions, looking for name of "crypt".
813 * When found, get and return the version.
814 */
815 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
816 while (v->next) {
817 if (! strcmp(v->name, "crypt")) {
818 /* We found the crypt driver, return the version, and get out */
819 version[0] = v->version[0];
820 version[1] = v->version[1];
821 version[2] = v->version[2];
822 return 0;
823 }
824 v = (struct dm_target_versions *)(((char *)v) + v->next);
825 }
826
827 return -1;
828}
829
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800830static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
Ken Sumrall29d8da82011-05-18 17:20:07 -0700831 char *real_blk_name, char *crypto_blk_name, const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800832{
833 char buffer[DM_CRYPT_BUF_SIZE];
834 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
835 char *crypt_params;
836 struct dm_ioctl *io;
837 struct dm_target_spec *tgt;
838 unsigned int minor;
839 int fd;
Ken Sumralle919efe2012-09-29 17:07:41 -0700840 int i;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800841 int retval = -1;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800842 int version[3];
843 char *extra_params;
844 int load_count;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800845
846 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
847 SLOGE("Cannot open device-mapper\n");
848 goto errout;
849 }
850
851 io = (struct dm_ioctl *) buffer;
852
853 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
854 if (ioctl(fd, DM_DEV_CREATE, io)) {
855 SLOGE("Cannot create dm-crypt device\n");
856 goto errout;
857 }
858
859 /* Get the device status, in particular, the name of it's device file */
860 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
861 if (ioctl(fd, DM_DEV_STATUS, io)) {
862 SLOGE("Cannot retrieve dm-crypt device status\n");
863 goto errout;
864 }
865 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
866 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
867
Ken Sumralldb5e0262013-02-05 17:39:48 -0800868 extra_params = "";
869 if (! get_dm_crypt_version(fd, name, version)) {
870 /* Support for allow_discards was added in version 1.11.0 */
871 if ((version[0] >= 2) ||
872 ((version[0] == 1) && (version[1] >= 11))) {
873 extra_params = "1 allow_discards";
874 SLOGI("Enabling support for allow_discards in dmcrypt.\n");
875 }
Ken Sumralle919efe2012-09-29 17:07:41 -0700876 }
877
Ken Sumralldb5e0262013-02-05 17:39:48 -0800878 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name,
879 fd, extra_params);
880 if (load_count < 0) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800881 SLOGE("Cannot load dm-crypt mapping table.\n");
882 goto errout;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800883 } else if (load_count > 1) {
884 SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800885 }
886
887 /* Resume this device to activate it */
Ken Sumralldb5e0262013-02-05 17:39:48 -0800888 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800889
890 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
891 SLOGE("Cannot resume the dm-crypt device\n");
892 goto errout;
893 }
894
895 /* We made it here with no errors. Woot! */
896 retval = 0;
897
898errout:
899 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
900
901 return retval;
902}
903
Ken Sumrall29d8da82011-05-18 17:20:07 -0700904static int delete_crypto_blk_dev(char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800905{
906 int fd;
907 char buffer[DM_CRYPT_BUF_SIZE];
908 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800909 int retval = -1;
910
911 if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
912 SLOGE("Cannot open device-mapper\n");
913 goto errout;
914 }
915
916 io = (struct dm_ioctl *) buffer;
917
918 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
919 if (ioctl(fd, DM_DEV_REMOVE, io)) {
920 SLOGE("Cannot remove dm-crypt device\n");
921 goto errout;
922 }
923
924 /* We made it here with no errors. Woot! */
925 retval = 0;
926
927errout:
928 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
929
930 return retval;
931
932}
933
Paul Lawrence13486032014-02-03 13:28:11 -0800934static int pbkdf2(const char *passwd, unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -0800935 unsigned char *ikey, void *params UNUSED)
936{
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800937 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -0800938 unsigned int keysize;
939 char* master_key = (char*)convert_hex_ascii_to_key(passwd, &keysize);
940 if (!master_key) return -1;
941 PKCS5_PBKDF2_HMAC_SHA1(master_key, keysize, salt, SALT_LEN,
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800942 HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
Paul Lawrencef4faa572014-01-29 13:31:03 -0800943
944 free (master_key);
945 return 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800946}
947
Paul Lawrence13486032014-02-03 13:28:11 -0800948static int scrypt(const char *passwd, unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -0800949 unsigned char *ikey, void *params)
950{
Kenny Rootc4c70f12013-06-14 12:11:38 -0700951 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
952
953 int N = 1 << ftr->N_factor;
954 int r = 1 << ftr->r_factor;
955 int p = 1 << ftr->p_factor;
956
957 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -0800958 unsigned int keysize;
959 unsigned char* master_key = convert_hex_ascii_to_key(passwd, &keysize);
960 if (!master_key) return -1;
961 crypto_scrypt(master_key, keysize, salt, SALT_LEN, N, r, p, ikey,
Kenny Rootc4c70f12013-06-14 12:11:38 -0700962 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrencef4faa572014-01-29 13:31:03 -0800963
964 free (master_key);
965 return 0;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700966}
967
Paul Lawrence13486032014-02-03 13:28:11 -0800968static int encrypt_master_key(const char *passwd, unsigned char *salt,
Ken Sumralle8744072011-01-18 22:01:55 -0800969 unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -0700970 unsigned char *encrypted_master_key,
971 struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800972{
973 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
974 EVP_CIPHER_CTX e_ctx;
975 int encrypted_len, final_len;
976
977 /* Turn the password into a key and IV that can decrypt the master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700978 get_device_scrypt_params(crypt_ftr);
Paul Lawrencef4faa572014-01-29 13:31:03 -0800979 if (scrypt(passwd, salt, ikey, crypt_ftr)) {
980 SLOGE("scrypt failed");
981 return -1;
982 }
Kenny Rootc4c70f12013-06-14 12:11:38 -0700983
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800984 /* Initialize the decryption engine */
985 if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
986 SLOGE("EVP_EncryptInit failed\n");
987 return -1;
988 }
989 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800990
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800991 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800992 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
993 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800994 SLOGE("EVP_EncryptUpdate failed\n");
995 return -1;
996 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800997 if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800998 SLOGE("EVP_EncryptFinal failed\n");
999 return -1;
1000 }
1001
1002 if (encrypted_len + final_len != KEY_LEN_BYTES) {
1003 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
1004 return -1;
1005 } else {
1006 return 0;
1007 }
1008}
1009
JP Abgrall7bdfa522013-11-15 13:42:56 -08001010static int decrypt_master_key_aux(char *passwd, unsigned char *salt,
Ken Sumralle8744072011-01-18 22:01:55 -08001011 unsigned char *encrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001012 unsigned char *decrypted_master_key,
1013 kdf_func kdf, void *kdf_params)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001014{
1015 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 -08001016 EVP_CIPHER_CTX d_ctx;
1017 int decrypted_len, final_len;
1018
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001019 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001020 if (kdf(passwd, salt, ikey, kdf_params)) {
1021 SLOGE("kdf failed");
1022 return -1;
1023 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001024
1025 /* Initialize the decryption engine */
1026 if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
1027 return -1;
1028 }
1029 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1030 /* Decrypt the master key */
1031 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
1032 encrypted_master_key, KEY_LEN_BYTES)) {
1033 return -1;
1034 }
1035 if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
1036 return -1;
1037 }
1038
1039 if (decrypted_len + final_len != KEY_LEN_BYTES) {
1040 return -1;
1041 } else {
1042 return 0;
1043 }
1044}
1045
Kenny Rootc4c70f12013-06-14 12:11:38 -07001046static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001047{
Kenny Rootc4c70f12013-06-14 12:11:38 -07001048 if (ftr->kdf_type == KDF_SCRYPT) {
1049 *kdf = scrypt;
1050 *kdf_params = ftr;
1051 } else {
1052 *kdf = pbkdf2;
1053 *kdf_params = NULL;
1054 }
1055}
1056
JP Abgrall7bdfa522013-11-15 13:42:56 -08001057static int decrypt_master_key(char *passwd, unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001058 struct crypt_mnt_ftr *crypt_ftr)
1059{
1060 kdf_func kdf;
1061 void *kdf_params;
1062 int ret;
1063
1064 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001065 ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key, decrypted_master_key, kdf,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001066 kdf_params);
1067 if (ret != 0) {
1068 SLOGW("failure decrypting master key");
Kenny Rootc4c70f12013-06-14 12:11:38 -07001069 }
1070
1071 return ret;
1072}
1073
1074static int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt,
1075 struct crypt_mnt_ftr *crypt_ftr) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001076 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -08001077 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001078 EVP_CIPHER_CTX e_ctx;
1079 int encrypted_len, final_len;
1080
1081 /* Get some random bits for a key */
1082 fd = open("/dev/urandom", O_RDONLY);
Ken Sumralle8744072011-01-18 22:01:55 -08001083 read(fd, key_buf, sizeof(key_buf));
1084 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001085 close(fd);
1086
1087 /* Now encrypt it with the password */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001088 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001089}
1090
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001091static int wait_and_unmount(char *mountpoint)
1092{
1093 int i, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001094#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001095
1096 /* Now umount the tmpfs filesystem */
1097 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
1098 if (umount(mountpoint)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001099 if (errno == EINVAL) {
1100 /* EINVAL is returned if the directory is not a mountpoint,
1101 * i.e. there is no filesystem mounted there. So just get out.
1102 */
1103 break;
1104 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001105 sleep(1);
1106 i++;
1107 } else {
1108 break;
1109 }
1110 }
1111
1112 if (i < WAIT_UNMOUNT_COUNT) {
1113 SLOGD("unmounting %s succeeded\n", mountpoint);
1114 rc = 0;
1115 } else {
1116 SLOGE("unmounting %s failed\n", mountpoint);
1117 rc = -1;
1118 }
1119
1120 return rc;
1121}
1122
Ken Sumrallc5872692013-05-14 15:26:31 -07001123#define DATA_PREP_TIMEOUT 200
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001124static int prep_data_fs(void)
1125{
1126 int i;
1127
1128 /* Do the prep of the /data filesystem */
1129 property_set("vold.post_fs_data_done", "0");
1130 property_set("vold.decrypt", "trigger_post_fs_data");
1131 SLOGD("Just triggered post_fs_data\n");
1132
Ken Sumrallc5872692013-05-14 15:26:31 -07001133 /* Wait a max of 50 seconds, hopefully it takes much less */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001134 for (i=0; i<DATA_PREP_TIMEOUT; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001135 char p[PROPERTY_VALUE_MAX];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001136
1137 property_get("vold.post_fs_data_done", p, "0");
1138 if (*p == '1') {
1139 break;
1140 } else {
1141 usleep(250000);
1142 }
1143 }
1144 if (i == DATA_PREP_TIMEOUT) {
1145 /* Ugh, we failed to prep /data in time. Bail. */
Ken Sumrallc5872692013-05-14 15:26:31 -07001146 SLOGE("post_fs_data timed out!\n");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001147 return -1;
1148 } else {
1149 SLOGD("post_fs_data done\n");
1150 return 0;
1151 }
1152}
1153
Paul Lawrencef4faa572014-01-29 13:31:03 -08001154static int cryptfs_restart_internal(int restart_main)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001155{
1156 char fs_type[32];
1157 char real_blkdev[MAXPATHLEN];
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001158 char crypto_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001159 char fs_options[256];
1160 unsigned long mnt_flags;
1161 struct stat statbuf;
1162 int rc = -1, i;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001163 static int restart_successful = 0;
1164
1165 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -06001166 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001167 SLOGE("Encrypted filesystem not validated, aborting");
1168 return -1;
1169 }
1170
1171 if (restart_successful) {
1172 SLOGE("System already restarted with encrypted disk, aborting");
1173 return -1;
1174 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001175
Paul Lawrencef4faa572014-01-29 13:31:03 -08001176 if (restart_main) {
1177 /* Here is where we shut down the framework. The init scripts
1178 * start all services in one of three classes: core, main or late_start.
1179 * On boot, we start core and main. Now, we stop main, but not core,
1180 * as core includes vold and a few other really important things that
1181 * we need to keep running. Once main has stopped, we should be able
1182 * to umount the tmpfs /data, then mount the encrypted /data.
1183 * We then restart the class main, and also the class late_start.
1184 * At the moment, I've only put a few things in late_start that I know
1185 * are not needed to bring up the framework, and that also cause problems
1186 * with unmounting the tmpfs /data, but I hope to add add more services
1187 * to the late_start class as we optimize this to decrease the delay
1188 * till the user is asked for the password to the filesystem.
1189 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001190
Paul Lawrencef4faa572014-01-29 13:31:03 -08001191 /* The init files are setup to stop the class main when vold.decrypt is
1192 * set to trigger_reset_main.
1193 */
1194 property_set("vold.decrypt", "trigger_reset_main");
1195 SLOGD("Just asked init to shut down class main\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001196
Paul Lawrencef4faa572014-01-29 13:31:03 -08001197 /* Ugh, shutting down the framework is not synchronous, so until it
1198 * can be fixed, this horrible hack will wait a moment for it all to
1199 * shut down before proceeding. Without it, some devices cannot
1200 * restart the graphics services.
1201 */
1202 sleep(2);
1203 }
Ken Sumrall9dedfd42012-10-09 14:16:59 -07001204
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001205 /* Now that the framework is shutdown, we should be able to umount()
1206 * the tmpfs filesystem, and mount the real one.
1207 */
1208
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001209 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1210 if (strlen(crypto_blkdev) == 0) {
1211 SLOGE("fs_crypto_blkdev not set\n");
1212 return -1;
1213 }
1214
Ken Sumralle5032c42012-04-01 23:58:44 -07001215 if (! (rc = wait_and_unmount(DATA_MNT_POINT)) ) {
Doug Zongker6fd57712013-12-17 09:43:23 -08001216 /* If ro.crypto.readonly is set to 1, mount the decrypted
1217 * filesystem readonly. This is used when /data is mounted by
1218 * recovery mode.
1219 */
1220 char ro_prop[PROPERTY_VALUE_MAX];
1221 property_get("ro.crypto.readonly", ro_prop, "");
1222 if (strlen(ro_prop) > 0 && atoi(ro_prop)) {
1223 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
1224 rec->flags |= MS_RDONLY;
1225 }
1226
Ken Sumralle5032c42012-04-01 23:58:44 -07001227 /* If that succeeded, then mount the decrypted filesystem */
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001228 fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001229
Ken Sumralle5032c42012-04-01 23:58:44 -07001230 property_set("vold.decrypt", "trigger_load_persist_props");
1231 /* Create necessary paths on /data */
1232 if (prep_data_fs()) {
1233 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001234 }
Ken Sumralle5032c42012-04-01 23:58:44 -07001235
1236 /* startup service classes main and late_start */
1237 property_set("vold.decrypt", "trigger_restart_framework");
1238 SLOGD("Just triggered restart_framework\n");
1239
1240 /* Give it a few moments to get started */
1241 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001242 }
1243
Ken Sumrall0cc16632011-01-18 20:32:26 -08001244 if (rc == 0) {
1245 restart_successful = 1;
1246 }
1247
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001248 return rc;
1249}
1250
Paul Lawrencef4faa572014-01-29 13:31:03 -08001251int cryptfs_restart(void)
1252{
1253 /* Call internal implementation forcing a restart of main service group */
1254 return cryptfs_restart_internal(1);
1255}
1256
Mark Salyzyn3e971272014-01-21 13:27:04 -08001257static int do_crypto_complete(char *mount_point UNUSED)
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001258{
1259 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001260 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -08001261 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001262
1263 property_get("ro.crypto.state", encrypted_state, "");
1264 if (strcmp(encrypted_state, "encrypted") ) {
1265 SLOGE("not running with encryption, aborting");
1266 return 1;
1267 }
1268
Ken Sumrall160b4d62013-04-22 12:15:39 -07001269 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001270 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumralle5032c42012-04-01 23:58:44 -07001271
Ken Sumralle1a45852011-12-14 21:24:27 -08001272 /*
1273 * Only report this error if key_loc is a file and it exists.
1274 * If the device was never encrypted, and /data is not mountable for
1275 * some reason, returning 1 should prevent the UI from presenting the
1276 * a "enter password" screen, or worse, a "press button to wipe the
1277 * device" screen.
1278 */
1279 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
1280 SLOGE("master key file does not exist, aborting");
1281 return 1;
1282 } else {
1283 SLOGE("Error getting crypt footer and key\n");
1284 return -1;
1285 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001286 }
1287
1288 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
1289 SLOGE("Encryption process didn't finish successfully\n");
1290 return -2; /* -2 is the clue to the UI that there is no usable data on the disk,
1291 * and give the user an option to wipe the disk */
1292 }
1293
1294 /* We passed the test! We shall diminish, and return to the west */
1295 return 0;
1296}
1297
Paul Lawrencef4faa572014-01-29 13:31:03 -08001298static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
1299 char *passwd, char *mount_point, char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001300{
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001301 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001302 unsigned char decrypted_master_key[32];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001303 char crypto_blkdev[MAXPATHLEN];
1304 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001305 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001306 unsigned int orig_failed_decrypt_count;
1307 int rc;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001308 kdf_func kdf;
1309 void *kdf_params;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001310
Paul Lawrencef4faa572014-01-29 13:31:03 -08001311 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1312 orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001313
Paul Lawrencef4faa572014-01-29 13:31:03 -08001314 if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
1315 if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr)) {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001316 SLOGE("Failed to decrypt master key\n");
1317 return -1;
1318 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001319 }
1320
Paul Lawrencef4faa572014-01-29 13:31:03 -08001321 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
1322
1323 if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key,
1324 real_blkdev, crypto_blkdev, label)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001325 SLOGE("Error creating decrypted block device\n");
1326 return -1;
1327 }
1328
Alex Klyubin707795a2013-05-10 15:17:07 -07001329 /* If init detects an encrypted filesystem, it writes a file for each such
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001330 * encrypted fs into the tmpfs /data filesystem, and then the framework finds those
1331 * files and passes that data to me */
1332 /* Create a tmp mount point to try mounting the decryptd fs
1333 * Since we're here, the mount_point should be a tmpfs filesystem, so make
1334 * a directory in it to test mount the decrypted filesystem.
1335 */
1336 sprintf(tmp_mount_point, "%s/tmp_mnt", mount_point);
1337 mkdir(tmp_mount_point, 0755);
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001338 if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001339 SLOGE("Error temp mounting decrypted block device\n");
Ken Sumrall29d8da82011-05-18 17:20:07 -07001340 delete_crypto_blk_dev(label);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001341 crypt_ftr->failed_decrypt_count++;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001342 } else {
1343 /* Success, so just umount and we'll mount it properly when we restart
1344 * the framework.
1345 */
1346 umount(tmp_mount_point);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001347 crypt_ftr->failed_decrypt_count = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001348 }
1349
Paul Lawrencef4faa572014-01-29 13:31:03 -08001350 if (orig_failed_decrypt_count != crypt_ftr->failed_decrypt_count) {
1351 put_crypt_ftr_and_key(crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001352 }
1353
Paul Lawrencef4faa572014-01-29 13:31:03 -08001354 if (crypt_ftr->failed_decrypt_count) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001355 /* We failed to mount the device, so return an error */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001356 rc = crypt_ftr->failed_decrypt_count;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001357
1358 } else {
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001359 /* Woot! Success! Save the name of the crypto block device
1360 * so we can mount it when restarting the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001361 */
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001362 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -06001363
1364 /* Also save a the master key so we can reencrypted the key
1365 * the key when we want to change the password on it.
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001366 */
Jason parks70a4b3f2011-01-28 10:10:47 -06001367 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001368 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -06001369 master_key_saved = 1;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001370 SLOGD("%s(): Master key saved\n", __FUNCTION__);
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001371 rc = 0;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001372 /*
1373 * Upgrade if we're not using the latest KDF.
1374 */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001375 if (crypt_ftr->kdf_type != KDF_SCRYPT) {
1376 crypt_ftr->kdf_type = KDF_SCRYPT;
1377 rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1378 crypt_ftr->master_key, crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001379 if (!rc) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001380 rc = put_crypt_ftr_and_key(crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001381 }
1382 SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
1383 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001384 }
1385
1386 return rc;
1387}
1388
Ken Sumrall0b8b5972011-08-31 16:14:23 -07001389/* Called by vold when it wants to undo the crypto mapping of a volume it
1390 * manages. This is usually in response to a factory reset, when we want
1391 * to undo the crypto mapping so the volume is formatted in the clear.
1392 */
1393int cryptfs_revert_volume(const char *label)
1394{
1395 return delete_crypto_blk_dev((char *)label);
1396}
1397
Ken Sumrall29d8da82011-05-18 17:20:07 -07001398/*
1399 * Called by vold when it's asked to mount an encrypted, nonremovable volume.
1400 * Setup a dm-crypt mapping, use the saved master key from
1401 * setting up the /data mapping, and return the new device path.
1402 */
1403int cryptfs_setup_volume(const char *label, int major, int minor,
1404 char *crypto_sys_path, unsigned int max_path,
1405 int *new_major, int *new_minor)
1406{
1407 char real_blkdev[MAXPATHLEN], crypto_blkdev[MAXPATHLEN];
1408 struct crypt_mnt_ftr sd_crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001409 struct stat statbuf;
1410 int nr_sec, fd;
1411
1412 sprintf(real_blkdev, "/dev/block/vold/%d:%d", major, minor);
1413
Ken Sumrall160b4d62013-04-22 12:15:39 -07001414 get_crypt_ftr_and_key(&sd_crypt_ftr);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001415
1416 /* Update the fs_size field to be the size of the volume */
1417 fd = open(real_blkdev, O_RDONLY);
1418 nr_sec = get_blkdev_size(fd);
1419 close(fd);
1420 if (nr_sec == 0) {
1421 SLOGE("Cannot get size of volume %s\n", real_blkdev);
1422 return -1;
1423 }
1424
1425 sd_crypt_ftr.fs_size = nr_sec;
1426 create_crypto_blk_dev(&sd_crypt_ftr, saved_master_key, real_blkdev,
1427 crypto_blkdev, label);
1428
1429 stat(crypto_blkdev, &statbuf);
1430 *new_major = MAJOR(statbuf.st_rdev);
1431 *new_minor = MINOR(statbuf.st_rdev);
1432
1433 /* Create path to sys entry for this block device */
1434 snprintf(crypto_sys_path, max_path, "/devices/virtual/block/%s", strrchr(crypto_blkdev, '/')+1);
1435
1436 return 0;
1437}
1438
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001439int cryptfs_crypto_complete(void)
1440{
1441 return do_crypto_complete("/data");
1442}
1443
Paul Lawrencef4faa572014-01-29 13:31:03 -08001444int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr)
1445{
1446 char encrypted_state[PROPERTY_VALUE_MAX];
1447 property_get("ro.crypto.state", encrypted_state, "");
1448 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
1449 SLOGE("encrypted fs already validated or not running with encryption,"
1450 " aborting");
1451 return -1;
1452 }
1453
1454 if (get_crypt_ftr_and_key(crypt_ftr)) {
1455 SLOGE("Error getting crypt footer and key");
1456 return -1;
1457 }
1458
1459 return 0;
1460}
1461
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001462int cryptfs_check_passwd(char *passwd)
1463{
Paul Lawrencef4faa572014-01-29 13:31:03 -08001464 struct crypt_mnt_ftr crypt_ftr;
1465 int rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001466
Paul Lawrencef4faa572014-01-29 13:31:03 -08001467 rc = check_unmounted_and_get_ftr(&crypt_ftr);
1468 if (rc)
1469 return rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001470
Paul Lawrencef4faa572014-01-29 13:31:03 -08001471 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
1472 DATA_MNT_POINT, "userdata");
Paul Lawrence684dbdf2014-02-07 12:07:22 -08001473
1474 if (rc == 0 && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
1475 just_decrypted = 1;
1476 }
1477
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001478 return rc;
1479}
1480
Ken Sumrall3ad90722011-10-04 20:38:29 -07001481int cryptfs_verify_passwd(char *passwd)
1482{
1483 struct crypt_mnt_ftr crypt_ftr;
1484 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001485 unsigned char decrypted_master_key[32];
Ken Sumrall3ad90722011-10-04 20:38:29 -07001486 char encrypted_state[PROPERTY_VALUE_MAX];
1487 int rc;
1488
1489 property_get("ro.crypto.state", encrypted_state, "");
1490 if (strcmp(encrypted_state, "encrypted") ) {
1491 SLOGE("device not encrypted, aborting");
1492 return -2;
1493 }
1494
1495 if (!master_key_saved) {
1496 SLOGE("encrypted fs not yet mounted, aborting");
1497 return -1;
1498 }
1499
1500 if (!saved_mount_point) {
1501 SLOGE("encrypted fs failed to save mount point, aborting");
1502 return -1;
1503 }
1504
Ken Sumrall160b4d62013-04-22 12:15:39 -07001505 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall3ad90722011-10-04 20:38:29 -07001506 SLOGE("Error getting crypt footer and key\n");
1507 return -1;
1508 }
1509
1510 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
1511 /* If the device has no password, then just say the password is valid */
1512 rc = 0;
1513 } else {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001514 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001515 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
1516 /* They match, the password is correct */
1517 rc = 0;
1518 } else {
1519 /* If incorrect, sleep for a bit to prevent dictionary attacks */
1520 sleep(1);
1521 rc = 1;
1522 }
1523 }
1524
1525 return rc;
1526}
1527
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001528/* Initialize a crypt_mnt_ftr structure. The keysize is
1529 * defaulted to 16 bytes, and the filesystem size to 0.
1530 * Presumably, at a minimum, the caller will update the
1531 * filesystem size and crypto_type_name after calling this function.
1532 */
1533static void cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
1534{
Ken Sumrall160b4d62013-04-22 12:15:39 -07001535 off64_t off;
1536
1537 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001538 ftr->magic = CRYPT_MNT_MAGIC;
Kenny Rootc96a5f82013-06-14 12:08:28 -07001539 ftr->major_version = CURRENT_MAJOR_VERSION;
1540 ftr->minor_version = CURRENT_MINOR_VERSION;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001541 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Jason parks70a4b3f2011-01-28 10:10:47 -06001542 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall160b4d62013-04-22 12:15:39 -07001543
Kenny Rootc4c70f12013-06-14 12:11:38 -07001544 ftr->kdf_type = KDF_SCRYPT;
1545 get_device_scrypt_params(ftr);
1546
Ken Sumrall160b4d62013-04-22 12:15:39 -07001547 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
1548 if (get_crypt_ftr_info(NULL, &off) == 0) {
1549 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
1550 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
1551 ftr->persist_data_size;
1552 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001553}
1554
Ken Sumrall29d8da82011-05-18 17:20:07 -07001555static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001556{
Ken Sumralle550f782013-08-20 13:48:23 -07001557 const char *args[10];
1558 char size_str[32]; /* Must be large enough to hold a %lld and null byte */
1559 int num_args;
1560 int status;
1561 int tmp;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001562 int rc = -1;
1563
Ken Sumrall29d8da82011-05-18 17:20:07 -07001564 if (type == EXT4_FS) {
Ken Sumralle550f782013-08-20 13:48:23 -07001565 args[0] = "/system/bin/make_ext4fs";
1566 args[1] = "-a";
1567 args[2] = "/data";
1568 args[3] = "-l";
1569 snprintf(size_str, sizeof(size_str), "%lld", size * 512);
1570 args[4] = size_str;
1571 args[5] = crypto_blkdev;
1572 num_args = 6;
1573 SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n",
1574 args[0], args[1], args[2], args[3], args[4], args[5]);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001575 } else if (type== FAT_FS) {
Ken Sumralle550f782013-08-20 13:48:23 -07001576 args[0] = "/system/bin/newfs_msdos";
1577 args[1] = "-F";
1578 args[2] = "32";
1579 args[3] = "-O";
1580 args[4] = "android";
1581 args[5] = "-c";
1582 args[6] = "8";
1583 args[7] = "-s";
1584 snprintf(size_str, sizeof(size_str), "%lld", size);
1585 args[8] = size_str;
1586 args[9] = crypto_blkdev;
1587 num_args = 10;
1588 SLOGI("Making empty filesystem with command %s %s %s %s %s %s %s %s %s %s\n",
1589 args[0], args[1], args[2], args[3], args[4], args[5],
1590 args[6], args[7], args[8], args[9]);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001591 } else {
1592 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
1593 return -1;
1594 }
1595
Ken Sumralle550f782013-08-20 13:48:23 -07001596 tmp = android_fork_execvp(num_args, (char **)args, &status, false, true);
1597
1598 if (tmp != 0) {
1599 SLOGE("Error creating empty filesystem on %s due to logwrap error\n", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001600 } else {
Ken Sumralle550f782013-08-20 13:48:23 -07001601 if (WIFEXITED(status)) {
1602 if (WEXITSTATUS(status)) {
1603 SLOGE("Error creating filesystem on %s, exit status %d ",
1604 crypto_blkdev, WEXITSTATUS(status));
1605 } else {
1606 SLOGD("Successfully created filesystem on %s\n", crypto_blkdev);
1607 rc = 0;
1608 }
1609 } else {
1610 SLOGE("Error creating filesystem on %s, did not exit normally\n", crypto_blkdev);
1611 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001612 }
1613
1614 return rc;
1615}
1616
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001617#define CRYPT_INPLACE_BUFSIZE 4096
1618#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / 512)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08001619
1620/* aligned 32K writes tends to make flash happy.
1621 * SD card association recommends it.
1622 */
1623#define BLOCKS_AT_A_TIME 8
1624
1625struct encryptGroupsData
1626{
1627 int realfd;
1628 int cryptofd;
1629 off64_t numblocks;
1630 off64_t one_pct, cur_pct, new_pct;
1631 off64_t blocks_already_done, tot_numblocks;
1632 char* real_blkdev, * crypto_blkdev;
1633 int count;
1634 off64_t offset;
1635 char* buffer;
1636};
1637
1638static void update_progress(struct encryptGroupsData* data)
1639{
1640 data->blocks_already_done++;
1641 data->new_pct = data->blocks_already_done / data->one_pct;
1642 if (data->new_pct > data->cur_pct) {
1643 char buf[8];
1644 data->cur_pct = data->new_pct;
1645 snprintf(buf, sizeof(buf), "%lld", data->cur_pct);
1646 property_set("vold.encrypt_progress", buf);
1647 }
1648}
1649
1650static int flush_outstanding_data(struct encryptGroupsData* data)
1651{
1652 if (data->count == 0) {
1653 return 0;
1654 }
1655
1656 SLOGV("Copying %d blocks at offset %llx", data->count, data->offset);
1657
1658 if (pread64(data->realfd, data->buffer,
1659 info.block_size * data->count, data->offset)
1660 <= 0) {
1661 SLOGE("Error reading real_blkdev %s for inplace encrypt",
1662 data->real_blkdev);
1663 return -1;
1664 }
1665
1666 if (pwrite64(data->cryptofd, data->buffer,
1667 info.block_size * data->count, data->offset)
1668 <= 0) {
1669 SLOGE("Error writing crypto_blkdev %s for inplace encrypt",
1670 data->crypto_blkdev);
1671 return -1;
1672 }
1673
1674 data->count = 0;
1675 return 0;
1676}
1677
1678static int encrypt_groups(struct encryptGroupsData* data)
1679{
1680 unsigned int i;
1681 u8 *block_bitmap = 0;
1682 unsigned int block;
1683 off64_t ret;
1684 int rc = -1;
1685
1686 data->buffer = malloc(info.block_size * BLOCKS_AT_A_TIME);
1687 if (!data->buffer) {
1688 SLOGE("Failed to allocate crypto buffer");
1689 goto errout;
1690 }
1691
1692 block_bitmap = malloc(info.block_size);
1693 if (!block_bitmap) {
1694 SLOGE("failed to allocate block bitmap");
1695 goto errout;
1696 }
1697
1698 for (i = 0; i < aux_info.groups; ++i) {
1699 SLOGI("Encrypting group %d", i);
1700
1701 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
1702 u32 block_count = min(info.blocks_per_group,
1703 aux_info.len_blocks - first_block);
1704
1705 off64_t offset = (u64)info.block_size
1706 * aux_info.bg_desc[i].bg_block_bitmap;
1707
1708 ret = pread64(data->realfd, block_bitmap, info.block_size, offset);
1709 if (ret != (int)info.block_size) {
1710 SLOGE("failed to read all of block group bitmap %d", i);
1711 goto errout;
1712 }
1713
1714 offset = (u64)info.block_size * first_block;
1715
1716 data->count = 0;
1717
1718 for (block = 0; block < block_count; block++) {
1719 update_progress(data);
1720 if (bitmap_get_bit(block_bitmap, block)) {
1721 if (data->count == 0) {
1722 data->offset = offset;
1723 }
1724 data->count++;
1725 } else {
1726 if (flush_outstanding_data(data)) {
1727 goto errout;
1728 }
1729 }
1730
1731 offset += info.block_size;
1732
1733 /* Write data if we are aligned or buffer size reached */
1734 if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0
1735 || data->count == BLOCKS_AT_A_TIME) {
1736 if (flush_outstanding_data(data)) {
1737 goto errout;
1738 }
1739 }
1740 }
1741 if (flush_outstanding_data(data)) {
1742 goto errout;
1743 }
1744 }
1745
1746 rc = 0;
1747
1748errout:
1749 free(data->buffer);
1750 free(block_bitmap);
1751 return rc;
1752}
1753
1754static int cryptfs_enable_inplace_ext4(char *crypto_blkdev,
1755 char *real_blkdev,
1756 off64_t size,
1757 off64_t *size_already_done,
1758 off64_t tot_size)
1759{
1760 int i;
1761 struct encryptGroupsData data;
1762 int rc = -1;
1763
1764 memset(&data, 0, sizeof(data));
1765 data.real_blkdev = real_blkdev;
1766 data.crypto_blkdev = crypto_blkdev;
1767
1768 if ( (data.realfd = open(real_blkdev, O_RDWR)) < 0) {
1769 SLOGE("Error opening real_blkdev %s for inplace encrypt\n",
1770 real_blkdev);
1771 goto errout;
1772 }
1773
1774 if ( (data.cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
1775 SLOGE("Error opening crypto_blkdev %s for inplace encrypt\n",
1776 crypto_blkdev);
1777 goto errout;
1778 }
1779
1780 if (setjmp(setjmp_env)) {
1781 SLOGE("Reading extent caused an exception");
1782 goto errout;
1783 }
1784
1785 if (read_ext(data.realfd, 0) != 0) {
1786 SLOGE("Failed to read extent");
1787 goto errout;
1788 }
1789
1790 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
1791 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
1792 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
1793
1794 SLOGI("Encrypting filesystem in place...");
1795
1796 data.one_pct = data.tot_numblocks / 100;
1797 data.cur_pct = 0;
1798
1799 rc = encrypt_groups(&data);
1800 if (rc) {
1801 SLOGE("Error encrypting groups");
1802 goto errout;
1803 }
1804
1805 *size_already_done += size;
1806 rc = 0;
1807
1808errout:
1809 close(data.realfd);
1810 close(data.cryptofd);
1811
1812 return rc;
1813}
1814
1815static int cryptfs_enable_inplace_full(char *crypto_blkdev, char *real_blkdev,
1816 off64_t size, off64_t *size_already_done,
1817 off64_t tot_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001818{
1819 int realfd, cryptofd;
1820 char *buf[CRYPT_INPLACE_BUFSIZE];
1821 int rc = -1;
1822 off64_t numblocks, i, remainder;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001823 off64_t one_pct, cur_pct, new_pct;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001824 off64_t blocks_already_done, tot_numblocks;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001825
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001826 if ( (realfd = open(real_blkdev, O_RDONLY)) < 0) {
1827 SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
1828 return -1;
1829 }
1830
1831 if ( (cryptofd = open(crypto_blkdev, O_WRONLY)) < 0) {
1832 SLOGE("Error opening crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1833 close(realfd);
1834 return -1;
1835 }
1836
1837 /* This is pretty much a simple loop of reading 4K, and writing 4K.
1838 * The size passed in is the number of 512 byte sectors in the filesystem.
1839 * So compute the number of whole 4K blocks we should read/write,
1840 * and the remainder.
1841 */
1842 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
1843 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001844 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
1845 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001846
1847 SLOGE("Encrypting filesystem in place...");
1848
Ken Sumrall29d8da82011-05-18 17:20:07 -07001849 one_pct = tot_numblocks / 100;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001850 cur_pct = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001851 /* process the majority of the filesystem in blocks */
1852 for (i=0; i<numblocks; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07001853 new_pct = (i + blocks_already_done) / one_pct;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001854 if (new_pct > cur_pct) {
1855 char buf[8];
1856
1857 cur_pct = new_pct;
1858 snprintf(buf, sizeof(buf), "%lld", cur_pct);
1859 property_set("vold.encrypt_progress", buf);
1860 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001861 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
1862 SLOGE("Error reading real_blkdev %s for inplace encrypt\n", crypto_blkdev);
1863 goto errout;
1864 }
1865 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
1866 SLOGE("Error writing crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1867 goto errout;
1868 }
1869 }
1870
1871 /* Do any remaining sectors */
1872 for (i=0; i<remainder; i++) {
1873 if (unix_read(realfd, buf, 512) <= 0) {
1874 SLOGE("Error reading rival sectors from real_blkdev %s for inplace encrypt\n", crypto_blkdev);
1875 goto errout;
1876 }
1877 if (unix_write(cryptofd, buf, 512) <= 0) {
1878 SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt\n", crypto_blkdev);
1879 goto errout;
1880 }
1881 }
1882
Ken Sumrall29d8da82011-05-18 17:20:07 -07001883 *size_already_done += size;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001884 rc = 0;
1885
1886errout:
1887 close(realfd);
1888 close(cryptofd);
1889
1890 return rc;
1891}
1892
Paul Lawrenceae59fe62014-01-21 08:23:27 -08001893static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev,
1894 off64_t size, off64_t *size_already_done,
1895 off64_t tot_size)
1896{
1897 if (cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev,
1898 size, size_already_done, tot_size) == 0) {
1899 return 0;
1900 }
1901
1902 return cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev,
1903 size, size_already_done, tot_size);
1904}
1905
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001906#define CRYPTO_ENABLE_WIPE 1
1907#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001908
1909#define FRAMEWORK_BOOT_WAIT 60
1910
Ken Sumrall29d8da82011-05-18 17:20:07 -07001911static inline int should_encrypt(struct volume_info *volume)
1912{
Paul Lawrenceae59fe62014-01-21 08:23:27 -08001913 return (volume->flags & (VOL_ENCRYPTABLE | VOL_NONREMOVABLE)) ==
Ken Sumrall29d8da82011-05-18 17:20:07 -07001914 (VOL_ENCRYPTABLE | VOL_NONREMOVABLE);
1915}
1916
Paul Lawrence13486032014-02-03 13:28:11 -08001917int cryptfs_enable_internal(char *howarg, int crypt_type, char *passwd,
1918 int allow_reboot)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001919{
1920 int how = 0;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001921 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN], sd_crypto_blkdev[MAXPATHLEN];
Ken Sumralle5032c42012-04-01 23:58:44 -07001922 unsigned long nr_sec;
Ken Sumrall160b4d62013-04-22 12:15:39 -07001923 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Ken Sumrall319b1042011-06-14 14:01:55 -07001924 int rc=-1, fd, i, ret;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001925 struct crypt_mnt_ftr crypt_ftr, sd_crypt_ftr;;
Ken Sumrall160b4d62013-04-22 12:15:39 -07001926 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001927 char tmpfs_options[PROPERTY_VALUE_MAX];
1928 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001929 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07001930 char key_loc[PROPERTY_VALUE_MAX];
1931 char fuse_sdcard[PROPERTY_VALUE_MAX];
1932 char *sd_mnt_point;
1933 char sd_blk_dev[256] = { 0 };
1934 int num_vols;
1935 struct volume_info *vol_list = 0;
1936 off64_t cur_encryption_done=0, tot_encryption_size=0;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001937
1938 property_get("ro.crypto.state", encrypted_state, "");
JP Abgrall502dc742013-11-01 13:06:20 -07001939 if (!strcmp(encrypted_state, "encrypted")) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001940 SLOGE("Device is already running encrypted, aborting");
Ken Sumrall3ed82362011-01-28 23:31:16 -08001941 goto error_unencrypted;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001942 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001943
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001944 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001945
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001946 if (!strcmp(howarg, "wipe")) {
1947 how = CRYPTO_ENABLE_WIPE;
1948 } else if (! strcmp(howarg, "inplace")) {
1949 how = CRYPTO_ENABLE_INPLACE;
1950 } else {
1951 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08001952 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001953 }
1954
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001955 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001956
Ken Sumrall3ed82362011-01-28 23:31:16 -08001957 /* Get the size of the real block device */
1958 fd = open(real_blkdev, O_RDONLY);
1959 if ( (nr_sec = get_blkdev_size(fd)) == 0) {
1960 SLOGE("Cannot get size of block device %s\n", real_blkdev);
1961 goto error_unencrypted;
1962 }
1963 close(fd);
1964
1965 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001966 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08001967 unsigned int fs_size_sec, max_fs_size_sec;
1968
1969 fs_size_sec = get_fs_size(real_blkdev);
1970 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
1971
1972 if (fs_size_sec > max_fs_size_sec) {
1973 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
1974 goto error_unencrypted;
1975 }
1976 }
1977
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001978 /* Get a wakelock as this may take a while, and we don't want the
1979 * device to sleep on us. We'll grab a partial wakelock, and if the UI
1980 * wants to keep the screen on, it can grab a full wakelock.
1981 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07001982 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08001983 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
1984
Jeff Sharkey7382f812012-08-23 14:08:59 -07001985 /* Get the sdcard mount point */
Jeff Sharkeyb77bc462012-10-01 14:36:26 -07001986 sd_mnt_point = getenv("EMULATED_STORAGE_SOURCE");
Jeff Sharkey7382f812012-08-23 14:08:59 -07001987 if (!sd_mnt_point) {
1988 sd_mnt_point = getenv("EXTERNAL_STORAGE");
1989 }
1990 if (!sd_mnt_point) {
1991 sd_mnt_point = "/mnt/sdcard";
1992 }
Ken Sumrall29d8da82011-05-18 17:20:07 -07001993
1994 num_vols=vold_getNumDirectVolumes();
1995 vol_list = malloc(sizeof(struct volume_info) * num_vols);
1996 vold_getDirectVolumeList(vol_list);
1997
1998 for (i=0; i<num_vols; i++) {
1999 if (should_encrypt(&vol_list[i])) {
2000 fd = open(vol_list[i].blk_dev, O_RDONLY);
2001 if ( (vol_list[i].size = get_blkdev_size(fd)) == 0) {
2002 SLOGE("Cannot get size of block device %s\n", vol_list[i].blk_dev);
2003 goto error_unencrypted;
2004 }
2005 close(fd);
2006
Ken Sumrall3b170052011-07-11 15:38:57 -07002007 ret=vold_disableVol(vol_list[i].label);
Ken Sumrall319b1042011-06-14 14:01:55 -07002008 if ((ret < 0) && (ret != UNMOUNT_NOT_MOUNTED_ERR)) {
2009 /* -2 is returned when the device exists but is not currently mounted.
2010 * ignore the error and continue. */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002011 SLOGE("Failed to unmount volume %s\n", vol_list[i].label);
2012 goto error_unencrypted;
2013 }
2014 }
2015 }
2016
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002017 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002018 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002019 */
2020 property_set("vold.decrypt", "trigger_shutdown_framework");
2021 SLOGD("Just asked init to shut down class main\n");
2022
Ken Sumrall425524d2012-06-14 20:55:28 -07002023 if (vold_unmountAllAsecs()) {
2024 /* Just report the error. If any are left mounted,
2025 * umounting /data below will fail and handle the error.
2026 */
2027 SLOGE("Error unmounting internal asecs");
2028 }
2029
Ken Sumrall29d8da82011-05-18 17:20:07 -07002030 property_get("ro.crypto.fuse_sdcard", fuse_sdcard, "");
2031 if (!strcmp(fuse_sdcard, "true")) {
2032 /* This is a device using the fuse layer to emulate the sdcard semantics
2033 * on top of the userdata partition. vold does not manage it, it is managed
2034 * by the sdcard service. The sdcard service was killed by the property trigger
2035 * above, so just unmount it now. We must do this _AFTER_ killing the framework,
2036 * unlike the case for vold managed devices above.
2037 */
2038 if (wait_and_unmount(sd_mnt_point)) {
2039 goto error_shutting_down;
2040 }
Ken Sumrall2eaf7132011-01-14 12:45:48 -08002041 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002042
2043 /* Now unmount the /data partition. */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002044 if (wait_and_unmount(DATA_MNT_POINT)) {
JP Abgrall502dc742013-11-01 13:06:20 -07002045 if (allow_reboot) {
2046 goto error_shutting_down;
2047 } else {
2048 goto error_unencrypted;
2049 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002050 }
2051
2052 /* Do extra work for a better UX when doing the long inplace encryption */
2053 if (how == CRYPTO_ENABLE_INPLACE) {
2054 /* Now that /data is unmounted, we need to mount a tmpfs
2055 * /data, set a property saying we're doing inplace encryption,
2056 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002057 */
Ken Sumralle5032c42012-04-01 23:58:44 -07002058 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002059 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002060 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002061 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08002062 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002063
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002064 /* restart the framework. */
2065 /* Create necessary paths on /data */
2066 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002067 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002068 }
2069
Ken Sumrall92736ef2012-10-17 20:57:14 -07002070 /* Ugh, shutting down the framework is not synchronous, so until it
2071 * can be fixed, this horrible hack will wait a moment for it all to
2072 * shut down before proceeding. Without it, some devices cannot
2073 * restart the graphics services.
2074 */
2075 sleep(2);
2076
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002077 /* startup service classes main and late_start */
2078 property_set("vold.decrypt", "trigger_restart_min_framework");
2079 SLOGD("Just triggered restart_min_framework\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002080
Ken Sumrall7df84122011-01-18 14:04:08 -08002081 /* OK, the framework is restarted and will soon be showing a
2082 * progress bar. Time to setup an encrypted mapping, and
2083 * either write a new filesystem, or encrypt in place updating
2084 * the progress bar as we work.
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002085 */
2086 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002087
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002088 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002089 /* Initialize a crypt_mnt_ftr for the partition */
2090 cryptfs_init_crypt_mnt_ftr(&crypt_ftr);
Ken Sumrall160b4d62013-04-22 12:15:39 -07002091
Ken Sumrall29d8da82011-05-18 17:20:07 -07002092 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
2093 crypt_ftr.fs_size = nr_sec - (CRYPT_FOOTER_OFFSET / 512);
2094 } else {
2095 crypt_ftr.fs_size = nr_sec;
2096 }
Ken Sumralld33d4172011-02-01 00:49:13 -08002097 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence13486032014-02-03 13:28:11 -08002098 crypt_ftr.crypt_type = crypt_type;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002099 strcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");
2100
2101 /* Make an encrypted master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -07002102 if (create_encrypted_random_key(passwd, crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002103 SLOGE("Cannot create encrypted master key\n");
JP Abgrall502dc742013-11-01 13:06:20 -07002104 goto error_shutting_down;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002105 }
2106
2107 /* Write the key to the end of the partition */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002108 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002109
Ken Sumrall160b4d62013-04-22 12:15:39 -07002110 /* If any persistent data has been remembered, save it.
2111 * If none, create a valid empty table and save that.
2112 */
2113 if (!persist_data) {
2114 pdata = malloc(CRYPT_PERSIST_DATA_SIZE);
2115 if (pdata) {
2116 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
2117 persist_data = pdata;
2118 }
2119 }
2120 if (persist_data) {
2121 save_persistent_data();
2122 }
2123
JP Abgrall7bdfa522013-11-15 13:42:56 -08002124 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002125 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
2126 "userdata");
2127
Ken Sumrall128626f2011-06-28 18:45:14 -07002128 /* The size of the userdata partition, and add in the vold volumes below */
2129 tot_encryption_size = crypt_ftr.fs_size;
2130
Ken Sumrall29d8da82011-05-18 17:20:07 -07002131 /* setup crypto mapping for all encryptable volumes handled by vold */
2132 for (i=0; i<num_vols; i++) {
2133 if (should_encrypt(&vol_list[i])) {
2134 vol_list[i].crypt_ftr = crypt_ftr; /* gotta love struct assign */
2135 vol_list[i].crypt_ftr.fs_size = vol_list[i].size;
2136 create_crypto_blk_dev(&vol_list[i].crypt_ftr, decrypted_master_key,
2137 vol_list[i].blk_dev, vol_list[i].crypto_blkdev,
2138 vol_list[i].label);
Ken Sumrall128626f2011-06-28 18:45:14 -07002139 tot_encryption_size += vol_list[i].size;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002140 }
2141 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002142
2143 if (how == CRYPTO_ENABLE_WIPE) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07002144 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr.fs_size, EXT4_FS);
2145 /* Encrypt all encryptable volumes handled by vold */
2146 if (!rc) {
2147 for (i=0; i<num_vols; i++) {
2148 if (should_encrypt(&vol_list[i])) {
2149 rc = cryptfs_enable_wipe(vol_list[i].crypto_blkdev,
2150 vol_list[i].crypt_ftr.fs_size, FAT_FS);
2151 }
2152 }
2153 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002154 } else if (how == CRYPTO_ENABLE_INPLACE) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07002155 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, crypt_ftr.fs_size,
2156 &cur_encryption_done, tot_encryption_size);
2157 /* Encrypt all encryptable volumes handled by vold */
2158 if (!rc) {
2159 for (i=0; i<num_vols; i++) {
2160 if (should_encrypt(&vol_list[i])) {
2161 rc = cryptfs_enable_inplace(vol_list[i].crypto_blkdev,
2162 vol_list[i].blk_dev,
2163 vol_list[i].crypt_ftr.fs_size,
2164 &cur_encryption_done, tot_encryption_size);
2165 }
2166 }
2167 }
2168 if (!rc) {
2169 /* The inplace routine never actually sets the progress to 100%
2170 * due to the round down nature of integer division, so set it here */
2171 property_set("vold.encrypt_progress", "100");
2172 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002173 } else {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002174 /* Shouldn't happen */
2175 SLOGE("cryptfs_enable: internal error, unknown option\n");
JP Abgrall502dc742013-11-01 13:06:20 -07002176 goto error_shutting_down;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002177 }
2178
2179 /* Undo the dm-crypt mapping whether we succeed or not */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002180 delete_crypto_blk_dev("userdata");
2181 for (i=0; i<num_vols; i++) {
2182 if (should_encrypt(&vol_list[i])) {
2183 delete_crypto_blk_dev(vol_list[i].label);
2184 }
2185 }
2186
2187 free(vol_list);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002188
2189 if (! rc) {
2190 /* Success */
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08002191
Ken Sumralld33d4172011-02-01 00:49:13 -08002192 /* Clear the encryption in progres flag in the footer */
2193 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002194 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08002195
Ken Sumrall29d8da82011-05-18 17:20:07 -07002196 sleep(2); /* Give the UI a chance to show 100% progress */
Ken Sumralladfba362013-06-04 16:37:52 -07002197 cryptfs_reboot(0);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002198 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002199 char value[PROPERTY_VALUE_MAX];
2200
Ken Sumrall319369a2012-06-27 16:30:18 -07002201 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002202 if (!strcmp(value, "1")) {
2203 /* wipe data if encryption failed */
2204 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
2205 mkdir("/cache/recovery", 0700);
Nick Kralevich4684e582012-06-26 15:07:03 -07002206 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC, 0600);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002207 if (fd >= 0) {
2208 write(fd, "--wipe_data", strlen("--wipe_data") + 1);
2209 close(fd);
2210 } else {
2211 SLOGE("could not open /cache/recovery/command\n");
2212 }
Ken Sumralladfba362013-06-04 16:37:52 -07002213 cryptfs_reboot(1);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002214 } else {
2215 /* set property to trigger dialog */
2216 property_set("vold.encrypt_progress", "error_partially_encrypted");
2217 release_wake_lock(lockid);
2218 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002219 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002220 }
2221
Ken Sumrall3ed82362011-01-28 23:31:16 -08002222 /* hrm, the encrypt step claims success, but the reboot failed.
2223 * This should not happen.
2224 * Set the property and return. Hope the framework can deal with it.
2225 */
2226 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002227 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002228 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08002229
2230error_unencrypted:
Ken Sumrall29d8da82011-05-18 17:20:07 -07002231 free(vol_list);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002232 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002233 if (lockid[0]) {
2234 release_wake_lock(lockid);
2235 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002236 return -1;
2237
2238error_shutting_down:
2239 /* we failed, and have not encrypted anthing, so the users's data is still intact,
2240 * but the framework is stopped and not restarted to show the error, so it's up to
2241 * vold to restart the system.
2242 */
2243 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Ken Sumralladfba362013-06-04 16:37:52 -07002244 cryptfs_reboot(0);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002245
2246 /* shouldn't get here */
2247 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall29d8da82011-05-18 17:20:07 -07002248 free(vol_list);
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002249 if (lockid[0]) {
2250 release_wake_lock(lockid);
2251 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002252 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002253}
2254
Paul Lawrence13486032014-02-03 13:28:11 -08002255int cryptfs_enable(char *howarg, char *passwd, int allow_reboot)
2256{
2257 /** @todo If we keep this route (user selected encryption)
2258 * need to take a type in and pass it to here.
2259 */
2260 return cryptfs_enable_internal(howarg, CRYPT_TYPE_PASSWORD,
2261 passwd, allow_reboot);
2262}
2263
2264int cryptfs_enable_default(char *howarg, int allow_reboot)
2265{
2266 return cryptfs_enable_internal(howarg, CRYPT_TYPE_DEFAULT,
2267 DEFAULT_PASSWORD, allow_reboot);
2268}
2269
2270int cryptfs_changepw(int crypt_type, const char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002271{
2272 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002273 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002274
2275 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08002276 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08002277 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002278 return -1;
2279 }
2280
Paul Lawrencef4faa572014-01-29 13:31:03 -08002281 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2282 SLOGE("Invalid crypt_type %d", crypt_type);
2283 return -1;
2284 }
2285
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002286 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002287 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08002288 SLOGE("Error getting crypt footer and key");
2289 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002290 }
2291
Paul Lawrencef4faa572014-01-29 13:31:03 -08002292 crypt_ftr.crypt_type = crypt_type;
2293
2294 encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
2295 : newpw,
2296 crypt_ftr.salt,
2297 saved_master_key,
2298 crypt_ftr.master_key,
2299 &crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002300
Jason parks70a4b3f2011-01-28 10:10:47 -06002301 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002302 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002303
2304 return 0;
2305}
Ken Sumrall160b4d62013-04-22 12:15:39 -07002306
2307static int persist_get_key(char *fieldname, char *value)
2308{
2309 unsigned int i;
2310
2311 if (persist_data == NULL) {
2312 return -1;
2313 }
2314 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2315 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2316 /* We found it! */
2317 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
2318 return 0;
2319 }
2320 }
2321
2322 return -1;
2323}
2324
2325static int persist_set_key(char *fieldname, char *value, int encrypted)
2326{
2327 unsigned int i;
2328 unsigned int num;
2329 struct crypt_mnt_ftr crypt_ftr;
2330 unsigned int max_persistent_entries;
2331 unsigned int dsize;
2332
2333 if (persist_data == NULL) {
2334 return -1;
2335 }
2336
2337 /* If encrypted, use the values from the crypt_ftr, otherwise
2338 * use the values for the current spec.
2339 */
2340 if (encrypted) {
2341 if(get_crypt_ftr_and_key(&crypt_ftr)) {
2342 return -1;
2343 }
2344 dsize = crypt_ftr.persist_data_size;
2345 } else {
2346 dsize = CRYPT_PERSIST_DATA_SIZE;
2347 }
2348 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
2349 sizeof(struct crypt_persist_entry);
2350
2351 num = persist_data->persist_valid_entries;
2352
2353 for (i = 0; i < num; i++) {
2354 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2355 /* We found an existing entry, update it! */
2356 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
2357 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
2358 return 0;
2359 }
2360 }
2361
2362 /* We didn't find it, add it to the end, if there is room */
2363 if (persist_data->persist_valid_entries < max_persistent_entries) {
2364 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
2365 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
2366 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
2367 persist_data->persist_valid_entries++;
2368 return 0;
2369 }
2370
2371 return -1;
2372}
2373
2374/* Return the value of the specified field. */
2375int cryptfs_getfield(char *fieldname, char *value, int len)
2376{
2377 char temp_value[PROPERTY_VALUE_MAX];
2378 char real_blkdev[MAXPATHLEN];
2379 /* 0 is success, 1 is not encrypted,
2380 * -1 is value not set, -2 is any other error
2381 */
2382 int rc = -2;
2383
2384 if (persist_data == NULL) {
2385 load_persistent_data();
2386 if (persist_data == NULL) {
2387 SLOGE("Getfield error, cannot load persistent data");
2388 goto out;
2389 }
2390 }
2391
2392 if (!persist_get_key(fieldname, temp_value)) {
2393 /* We found it, copy it to the caller's buffer and return */
2394 strlcpy(value, temp_value, len);
2395 rc = 0;
2396 } else {
2397 /* Sadness, it's not there. Return the error */
2398 rc = -1;
2399 }
2400
2401out:
2402 return rc;
2403}
2404
2405/* Set the value of the specified field. */
2406int cryptfs_setfield(char *fieldname, char *value)
2407{
2408 struct crypt_persist_data stored_pdata;
2409 struct crypt_persist_data *pdata_p;
2410 struct crypt_mnt_ftr crypt_ftr;
2411 char encrypted_state[PROPERTY_VALUE_MAX];
2412 /* 0 is success, -1 is an error */
2413 int rc = -1;
2414 int encrypted = 0;
2415
2416 if (persist_data == NULL) {
2417 load_persistent_data();
2418 if (persist_data == NULL) {
2419 SLOGE("Setfield error, cannot load persistent data");
2420 goto out;
2421 }
2422 }
2423
2424 property_get("ro.crypto.state", encrypted_state, "");
2425 if (!strcmp(encrypted_state, "encrypted") ) {
2426 encrypted = 1;
2427 }
2428
2429 if (persist_set_key(fieldname, value, encrypted)) {
2430 goto out;
2431 }
2432
2433 /* If we are running encrypted, save the persistent data now */
2434 if (encrypted) {
2435 if (save_persistent_data()) {
2436 SLOGE("Setfield error, cannot save persistent data");
2437 goto out;
2438 }
2439 }
2440
2441 rc = 0;
2442
2443out:
2444 return rc;
2445}
Paul Lawrencef4faa572014-01-29 13:31:03 -08002446
2447/* Checks userdata. Attempt to mount the volume if default-
2448 * encrypted.
2449 * On success trigger next init phase and return 0.
2450 * Currently do not handle failure - see TODO below.
2451 */
2452int cryptfs_mount_default_encrypted(void)
2453{
2454 char decrypt_state[PROPERTY_VALUE_MAX];
2455 property_get("vold.decrypt", decrypt_state, "0");
2456 if (!strcmp(decrypt_state, "0")) {
2457 SLOGE("Not encrypted - should not call here");
2458 } else {
2459 int crypt_type = cryptfs_get_password_type();
2460 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2461 SLOGE("Bad crypt type - error");
2462 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
2463 SLOGD("Password is not default - "
2464 "starting min framework to prompt");
2465 property_set("vold.decrypt", "trigger_restart_min_framework");
2466 return 0;
2467 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
2468 SLOGD("Password is default - restarting filesystem");
2469 cryptfs_restart_internal(0);
2470 return 0;
2471 } else {
2472 SLOGE("Encrypted, default crypt type but can't decrypt");
2473 }
2474 }
2475
2476 /** @TODO make sure we factory wipe in this situation
2477 * In general if we got here there is no recovery
2478 */
2479 return 0;
2480}
2481
2482/* Returns type of the password, default, pattern, pin or password.
2483 */
2484int cryptfs_get_password_type(void)
2485{
2486 struct crypt_mnt_ftr crypt_ftr;
2487
2488 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2489 SLOGE("Error getting crypt footer and key\n");
2490 return -1;
2491 }
2492
2493 return crypt_ftr.crypt_type;
2494}
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002495
2496int cryptfs_just_decrypted(void)
2497{
2498 int rc = just_decrypted;
2499 just_decrypted = 0;
2500 return rc;
2501}