blob: 4e325e68a85a321113d81377996334a6b72a1b8e [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
Logan Chien862eef72018-05-02 11:36:45 +080023#define LOG_TAG "Cryptfs"
24
25#include "cryptfs.h"
26
27#include "EncryptInplace.h"
28#include "Ext4Crypt.h"
29#include "Keymaster.h"
30#include "Process.h"
31#include "ScryptParameters.h"
32#include "VoldUtil.h"
33#include "VolumeManager.h"
34#include "secontext.h"
35
Logan Chien196d5852018-05-02 11:39:03 +080036#include <android-base/properties.h>
Logan Chien862eef72018-05-02 11:36:45 +080037#include <bootloader_message/bootloader_message.h>
Logan Chien196d5852018-05-02 11:39:03 +080038#include <cutils/android_reboot.h>
39#include <cutils/log.h>
40#include <cutils/properties.h>
Tao Bao5a95ddb2016-10-05 18:01:19 -070041#include <ext4_utils/ext4.h>
42#include <ext4_utils/ext4_utils.h>
Logan Chien196d5852018-05-02 11:39:03 +080043#include <f2fs_sparseblock.h>
Ken Sumralle5032c42012-04-01 23:58:44 -070044#include <fs_mgr.h>
Logan Chien196d5852018-05-02 11:39:03 +080045#include <hardware_legacy/power.h>
Logan Chien862eef72018-05-02 11:36:45 +080046#include <logwrap/logwrap.h>
47#include <openssl/evp.h>
48#include <openssl/sha.h>
Jeff Vander Stoepdf725752016-01-29 15:34:43 -080049#include <selinux/selinux.h>
Logan Chien862eef72018-05-02 11:36:45 +080050
51#include <ctype.h>
52#include <errno.h>
53#include <fcntl.h>
54#include <inttypes.h>
55#include <libgen.h>
56#include <linux/dm-ioctl.h>
57#include <linux/kdev_t.h>
58#include <math.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <sys/ioctl.h>
63#include <sys/mount.h>
64#include <sys/param.h>
65#include <sys/stat.h>
66#include <sys/types.h>
67#include <sys/wait.h>
68#include <time.h>
69#include <unistd.h>
70
Wei Wang4375f1b2017-02-24 17:43:01 -080071extern "C" {
72#include <crypto_scrypt.h>
73}
Mark Salyzyn3e971272014-01-21 13:27:04 -080074
Mark Salyzyn5eecc442014-02-12 14:16:14 -080075#define UNUSED __attribute__((unused))
76
Ken Sumrall8f869aa2010-12-03 03:47:09 -080077#define DM_CRYPT_BUF_SIZE 4096
78
Jason parks70a4b3f2011-01-28 10:10:47 -060079#define HASH_COUNT 2000
80#define KEY_LEN_BYTES 16
81#define IV_LEN_BYTES 16
82
Ken Sumrall29d8da82011-05-18 17:20:07 -070083#define KEY_IN_FOOTER "footer"
84
Paul Lawrence3bd36d52015-06-09 13:37:44 -070085#define DEFAULT_PASSWORD "default_password"
Paul Lawrencef4faa572014-01-29 13:31:03 -080086
Paul Lawrence3d99eba2015-11-20 07:07:19 -080087#define CRYPTO_BLOCK_DEVICE "userdata"
88
89#define BREADCRUMB_FILE "/data/misc/vold/convert_fde"
90
Ken Sumrall29d8da82011-05-18 17:20:07 -070091#define EXT4_FS 1
JP Abgrall62c7af32014-06-16 13:01:23 -070092#define F2FS_FS 2
Ken Sumrall29d8da82011-05-18 17:20:07 -070093
Ken Sumralle919efe2012-09-29 17:07:41 -070094#define TABLE_LOAD_RETRIES 10
95
Shawn Willden47ba10d2014-09-03 17:07:06 -060096#define RSA_KEY_SIZE 2048
97#define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8)
98#define RSA_EXPONENT 0x10001
Shawn Willdenda6e8992015-06-03 09:40:45 -060099#define KEYMASTER_CRYPTFS_RATE_LIMIT 1 // Maximum one try per second
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700100
Paul Lawrence8e3f4512014-09-08 10:11:17 -0700101#define RETRY_MOUNT_ATTEMPTS 10
102#define RETRY_MOUNT_DELAY_SECONDS 1
103
Paul Crowley73473332017-11-21 15:43:51 -0800104static int put_crypt_ftr_and_key(struct crypt_mnt_ftr* crypt_ftr);
105
Jason parks70a4b3f2011-01-28 10:10:47 -0600106static unsigned char saved_master_key[KEY_LEN_BYTES];
Ken Sumrall3ad90722011-10-04 20:38:29 -0700107static char *saved_mount_point;
Jason parks70a4b3f2011-01-28 10:10:47 -0600108static int master_key_saved = 0;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700109static struct crypt_persist_data *persist_data = NULL;
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800110
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700111/* Should we use keymaster? */
112static int keymaster_check_compatibility()
113{
Janis Danisevskis015ec302017-01-31 11:31:08 +0000114 return keymaster_compatibility_cryptfs_scrypt();
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700115}
116
117/* Create a new keymaster key and store it in this footer */
118static int keymaster_create_key(struct crypt_mnt_ftr *ftr)
119{
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800120 if (ftr->keymaster_blob_size) {
121 SLOGI("Already have key");
122 return 0;
123 }
124
Janis Danisevskis015ec302017-01-31 11:31:08 +0000125 int rc = keymaster_create_key_for_cryptfs_scrypt(RSA_KEY_SIZE, RSA_EXPONENT,
126 KEYMASTER_CRYPTFS_RATE_LIMIT, ftr->keymaster_blob, KEYMASTER_BLOB_SIZE,
127 &ftr->keymaster_blob_size);
128 if (rc) {
129 if (ftr->keymaster_blob_size > KEYMASTER_BLOB_SIZE) {
Paul Crowley73473332017-11-21 15:43:51 -0800130 SLOGE("Keymaster key blob too large");
Janis Danisevskis015ec302017-01-31 11:31:08 +0000131 ftr->keymaster_blob_size = 0;
132 }
133 SLOGE("Failed to generate keypair");
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700134 return -1;
135 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000136 return 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700137}
138
Shawn Willdene17a9c42014-09-08 13:04:08 -0600139/* This signs the given object using the keymaster key. */
140static int keymaster_sign_object(struct crypt_mnt_ftr *ftr,
Shawn Willden47ba10d2014-09-03 17:07:06 -0600141 const unsigned char *object,
142 const size_t object_size,
143 unsigned char **signature,
144 size_t *signature_size)
145{
Shawn Willden47ba10d2014-09-03 17:07:06 -0600146 unsigned char to_sign[RSA_KEY_SIZE_BYTES];
Shawn Willdene17a9c42014-09-08 13:04:08 -0600147 size_t to_sign_size = sizeof(to_sign);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600148 memset(to_sign, 0, RSA_KEY_SIZE_BYTES);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600149
Shawn Willdene17a9c42014-09-08 13:04:08 -0600150 // To sign a message with RSA, the message must satisfy two
151 // constraints:
152 //
153 // 1. The message, when interpreted as a big-endian numeric value, must
154 // be strictly less than the public modulus of the RSA key. Note
155 // that because the most significant bit of the public modulus is
156 // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit
157 // key), an n-bit message with most significant bit 0 always
158 // satisfies this requirement.
159 //
160 // 2. The message must have the same length in bits as the public
161 // modulus of the RSA key. This requirement isn't mathematically
162 // necessary, but is necessary to ensure consistency in
163 // implementations.
164 switch (ftr->kdf_type) {
Shawn Willdene17a9c42014-09-08 13:04:08 -0600165 case KDF_SCRYPT_KEYMASTER:
166 // This ensures the most significant byte of the signed message
167 // is zero. We could have zero-padded to the left instead, but
168 // this approach is slightly more robust against changes in
169 // object size. However, it's still broken (but not unusably
Shawn Willdenda6e8992015-06-03 09:40:45 -0600170 // so) because we really should be using a proper deterministic
171 // RSA padding function, such as PKCS1.
Wei Wang4375f1b2017-02-24 17:43:01 -0800172 memcpy(to_sign + 1, object, std::min((size_t)RSA_KEY_SIZE_BYTES - 1, object_size));
Shawn Willdene17a9c42014-09-08 13:04:08 -0600173 SLOGI("Signing safely-padded object");
174 break;
175 default:
176 SLOGE("Unknown KDF type %d", ftr->kdf_type);
Janis Danisevskis015ec302017-01-31 11:31:08 +0000177 return -1;
Shawn Willdene17a9c42014-09-08 13:04:08 -0600178 }
Paul Crowley73473332017-11-21 15:43:51 -0800179 for (;;) {
180 auto result = keymaster_sign_object_for_cryptfs_scrypt(
181 ftr->keymaster_blob, ftr->keymaster_blob_size, KEYMASTER_CRYPTFS_RATE_LIMIT, to_sign,
182 to_sign_size, signature, signature_size);
183 switch (result) {
184 case KeymasterSignResult::ok:
185 return 0;
186 case KeymasterSignResult::upgrade:
187 break;
188 default:
189 return -1;
190 }
191 SLOGD("Upgrading key");
192 if (keymaster_upgrade_key_for_cryptfs_scrypt(
193 RSA_KEY_SIZE, RSA_EXPONENT, KEYMASTER_CRYPTFS_RATE_LIMIT, ftr->keymaster_blob,
194 ftr->keymaster_blob_size, ftr->keymaster_blob, KEYMASTER_BLOB_SIZE,
195 &ftr->keymaster_blob_size) != 0) {
196 SLOGE("Failed to upgrade key");
197 return -1;
198 }
199 if (put_crypt_ftr_and_key(ftr) != 0) {
200 SLOGE("Failed to write upgraded key to disk");
201 }
202 SLOGD("Key upgraded successfully");
203 }
Shawn Willden47ba10d2014-09-03 17:07:06 -0600204}
205
Paul Lawrence399317e2014-03-10 13:20:50 -0700206/* Store password when userdata is successfully decrypted and mounted.
207 * Cleared by cryptfs_clear_password
208 *
209 * To avoid a double prompt at boot, we need to store the CryptKeeper
210 * password and pass it to KeyGuard, which uses it to unlock KeyStore.
211 * Since the entire framework is torn down and rebuilt after encryption,
212 * we have to use a daemon or similar to store the password. Since vold
213 * is secured against IPC except from system processes, it seems a reasonable
214 * place to store this.
215 *
216 * password should be cleared once it has been used.
217 *
218 * password is aged out after password_max_age_seconds seconds.
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800219 */
Paul Lawrence399317e2014-03-10 13:20:50 -0700220static char* password = 0;
221static int password_expiry_time = 0;
222static const int password_max_age_seconds = 60;
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800223
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800224extern struct fstab *fstab;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800225
Josh Gaofec44372017-08-28 13:22:55 -0700226enum class RebootType {reboot, recovery, shutdown};
227static void cryptfs_reboot(RebootType rt)
Ken Sumralladfba362013-06-04 16:37:52 -0700228{
Josh Gaofec44372017-08-28 13:22:55 -0700229 switch (rt) {
230 case RebootType::reboot:
Paul Lawrence87999172014-02-20 12:21:31 -0800231 property_set(ANDROID_RB_PROPERTY, "reboot");
232 break;
233
Josh Gaofec44372017-08-28 13:22:55 -0700234 case RebootType::recovery:
Paul Lawrence87999172014-02-20 12:21:31 -0800235 property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
236 break;
237
Josh Gaofec44372017-08-28 13:22:55 -0700238 case RebootType::shutdown:
Paul Lawrence87999172014-02-20 12:21:31 -0800239 property_set(ANDROID_RB_PROPERTY, "shutdown");
240 break;
Ken Sumralladfba362013-06-04 16:37:52 -0700241 }
Paul Lawrence87999172014-02-20 12:21:31 -0800242
Ken Sumralladfba362013-06-04 16:37:52 -0700243 sleep(20);
244
245 /* Shouldn't get here, reboot should happen before sleep times out */
246 return;
247}
248
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800249static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
250{
251 memset(io, 0, dataSize);
252 io->data_size = dataSize;
253 io->data_start = sizeof(struct dm_ioctl);
254 io->version[0] = 4;
255 io->version[1] = 0;
256 io->version[2] = 0;
257 io->flags = flags;
258 if (name) {
Marek Pola5e6b9142015-02-05 14:22:34 +0100259 strlcpy(io->name, name, sizeof(io->name));
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800260 }
261}
262
Kenny Rootc4c70f12013-06-14 12:11:38 -0700263/**
264 * Gets the default device scrypt parameters for key derivation time tuning.
265 * The parameters should lead to about one second derivation time for the
266 * given device.
267 */
268static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700269 char paramstr[PROPERTY_VALUE_MAX];
Paul Crowley63c18d32016-02-10 14:02:47 +0000270 int Nf, rf, pf;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700271
Paul Crowley63c18d32016-02-10 14:02:47 +0000272 property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS);
273 if (!parse_scrypt_parameters(paramstr, &Nf, &rf, &pf)) {
274 SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
275 parse_scrypt_parameters(SCRYPT_DEFAULTS, &Nf, &rf, &pf);
Kenny Rootc4c70f12013-06-14 12:11:38 -0700276 }
Paul Crowley63c18d32016-02-10 14:02:47 +0000277 ftr->N_factor = Nf;
278 ftr->r_factor = rf;
279 ftr->p_factor = pf;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700280}
281
Ken Sumrall3ed82362011-01-28 23:31:16 -0800282static unsigned int get_fs_size(char *dev)
283{
284 int fd, block_size;
285 struct ext4_super_block sb;
286 off64_t len;
287
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700288 if ((fd = open(dev, O_RDONLY|O_CLOEXEC)) < 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -0800289 SLOGE("Cannot open device to get filesystem size ");
290 return 0;
291 }
292
293 if (lseek64(fd, 1024, SEEK_SET) < 0) {
294 SLOGE("Cannot seek to superblock");
295 return 0;
296 }
297
298 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
299 SLOGE("Cannot read superblock");
300 return 0;
301 }
302
303 close(fd);
304
Daniel Rosenberge82df162014-08-15 22:19:23 +0000305 if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) {
306 SLOGE("Not a valid ext4 superblock");
307 return 0;
308 }
Ken Sumrall3ed82362011-01-28 23:31:16 -0800309 block_size = 1024 << sb.s_log_block_size;
310 /* compute length in bytes */
311 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
312
313 /* return length in sectors */
314 return (unsigned int) (len / 512);
315}
316
Ken Sumrall160b4d62013-04-22 12:15:39 -0700317static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
318{
319 static int cached_data = 0;
320 static off64_t cached_off = 0;
321 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
322 int fd;
323 char key_loc[PROPERTY_VALUE_MAX];
324 char real_blkdev[PROPERTY_VALUE_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -0700325 int rc = -1;
326
327 if (!cached_data) {
328 fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
329
330 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700331 if ( (fd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700332 SLOGE("Cannot open real block device %s\n", real_blkdev);
333 return -1;
334 }
335
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900336 unsigned long nr_sec = 0;
337 get_blkdev_size(fd, &nr_sec);
338 if (nr_sec != 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700339 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
340 * encryption info footer and key, and plenty of bytes to spare for future
341 * growth.
342 */
343 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
344 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
345 cached_data = 1;
346 } else {
347 SLOGE("Cannot get size of block device %s\n", real_blkdev);
348 }
349 close(fd);
350 } else {
351 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
352 cached_off = 0;
353 cached_data = 1;
354 }
355 }
356
357 if (cached_data) {
358 if (metadata_fname) {
359 *metadata_fname = cached_metadata_fname;
360 }
361 if (off) {
362 *off = cached_off;
363 }
364 rc = 0;
365 }
366
367 return rc;
368}
369
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800370/* Set sha256 checksum in structure */
371static void set_ftr_sha(struct crypt_mnt_ftr *crypt_ftr)
372{
373 SHA256_CTX c;
374 SHA256_Init(&c);
375 memset(crypt_ftr->sha256, 0, sizeof(crypt_ftr->sha256));
376 SHA256_Update(&c, crypt_ftr, sizeof(*crypt_ftr));
377 SHA256_Final(crypt_ftr->sha256, &c);
378}
379
Ken Sumralle8744072011-01-18 22:01:55 -0800380/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800381 * update the failed mount count but not change the key.
382 */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700383static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800384{
385 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800386 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700387 /* starting_off is set to the SEEK_SET offset
388 * where the crypto structure starts
389 */
390 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800391 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700392 char *fname = NULL;
Ken Sumrall3be890f2011-09-14 16:53:46 -0700393 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800394
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800395 set_ftr_sha(crypt_ftr);
396
Ken Sumrall160b4d62013-04-22 12:15:39 -0700397 if (get_crypt_ftr_info(&fname, &starting_off)) {
398 SLOGE("Unable to get crypt_ftr_info\n");
399 return -1;
400 }
401 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700402 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700403 return -1;
404 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700405 if ( (fd = open(fname, O_RDWR | O_CREAT|O_CLOEXEC, 0600)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700406 SLOGE("Cannot open footer file %s for put\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700407 return -1;
408 }
409
410 /* Seek to the start of the crypt footer */
411 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
412 SLOGE("Cannot seek to real block device footer\n");
413 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800414 }
415
416 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
417 SLOGE("Cannot write real block device footer\n");
418 goto errout;
419 }
420
Ken Sumrall3be890f2011-09-14 16:53:46 -0700421 fstat(fd, &statbuf);
422 /* If the keys are kept on a raw block device, do not try to truncate it. */
Ken Sumralle550f782013-08-20 13:48:23 -0700423 if (S_ISREG(statbuf.st_mode)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700424 if (ftruncate(fd, 0x4000)) {
Colin Cross59846b62014-02-06 20:34:29 -0800425 SLOGE("Cannot set footer file size\n");
Ken Sumralle8744072011-01-18 22:01:55 -0800426 goto errout;
427 }
428 }
429
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800430 /* Success! */
431 rc = 0;
432
433errout:
434 close(fd);
435 return rc;
436
437}
438
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800439static bool check_ftr_sha(const struct crypt_mnt_ftr *crypt_ftr)
440{
441 struct crypt_mnt_ftr copy;
442 memcpy(&copy, crypt_ftr, sizeof(copy));
443 set_ftr_sha(&copy);
444 return memcmp(copy.sha256, crypt_ftr->sha256, sizeof(copy.sha256)) == 0;
445}
446
Ken Sumrall160b4d62013-04-22 12:15:39 -0700447static inline int unix_read(int fd, void* buff, int len)
448{
449 return TEMP_FAILURE_RETRY(read(fd, buff, len));
450}
451
452static inline int unix_write(int fd, const void* buff, int len)
453{
454 return TEMP_FAILURE_RETRY(write(fd, buff, len));
455}
456
457static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
458{
459 memset(pdata, 0, len);
460 pdata->persist_magic = PERSIST_DATA_MAGIC;
461 pdata->persist_valid_entries = 0;
462}
463
464/* A routine to update the passed in crypt_ftr to the lastest version.
465 * fd is open read/write on the device that holds the crypto footer and persistent
466 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
467 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
468 */
469static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
470{
Kenny Root7434b312013-06-14 11:29:53 -0700471 int orig_major = crypt_ftr->major_version;
472 int orig_minor = crypt_ftr->minor_version;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700473
Kenny Root7434b312013-06-14 11:29:53 -0700474 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
475 struct crypt_persist_data *pdata;
476 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700477
Kenny Rootc4c70f12013-06-14 12:11:38 -0700478 SLOGW("upgrading crypto footer to 1.1");
479
Wei Wang4375f1b2017-02-24 17:43:01 -0800480 pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
Kenny Root7434b312013-06-14 11:29:53 -0700481 if (pdata == NULL) {
482 SLOGE("Cannot allocate persisent data\n");
483 return;
484 }
485 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
486
487 /* Need to initialize the persistent data area */
488 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
489 SLOGE("Cannot seek to persisent data offset\n");
Henrik Baard91064632015-02-05 15:09:17 +0100490 free(pdata);
Kenny Root7434b312013-06-14 11:29:53 -0700491 return;
492 }
493 /* Write all zeros to the first copy, making it invalid */
494 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
495
496 /* Write a valid but empty structure to the second copy */
497 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
498 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
499
500 /* Update the footer */
501 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
502 crypt_ftr->persist_data_offset[0] = pdata_offset;
503 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
504 crypt_ftr->minor_version = 1;
Henrik Baard91064632015-02-05 15:09:17 +0100505 free(pdata);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700506 }
507
Paul Lawrencef4faa572014-01-29 13:31:03 -0800508 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700509 SLOGW("upgrading crypto footer to 1.2");
JP Abgrall7bdfa522013-11-15 13:42:56 -0800510 /* But keep the old kdf_type.
511 * It will get updated later to KDF_SCRYPT after the password has been verified.
512 */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700513 crypt_ftr->kdf_type = KDF_PBKDF2;
514 get_device_scrypt_params(crypt_ftr);
515 crypt_ftr->minor_version = 2;
516 }
517
Paul Lawrencef4faa572014-01-29 13:31:03 -0800518 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
519 SLOGW("upgrading crypto footer to 1.3");
520 crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
521 crypt_ftr->minor_version = 3;
522 }
523
Kenny Root7434b312013-06-14 11:29:53 -0700524 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
525 if (lseek64(fd, offset, SEEK_SET) == -1) {
526 SLOGE("Cannot seek to crypt footer\n");
527 return;
528 }
529 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700530 }
Ken Sumrall160b4d62013-04-22 12:15:39 -0700531}
532
533
534static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800535{
536 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800537 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700538 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800539 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700540 char *fname = NULL;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700541 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800542
Ken Sumrall160b4d62013-04-22 12:15:39 -0700543 if (get_crypt_ftr_info(&fname, &starting_off)) {
544 SLOGE("Unable to get crypt_ftr_info\n");
545 return -1;
546 }
547 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700548 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700549 return -1;
550 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700551 if ( (fd = open(fname, O_RDWR|O_CLOEXEC)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700552 SLOGE("Cannot open footer file %s for get\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700553 return -1;
554 }
555
556 /* Make sure it's 16 Kbytes in length */
557 fstat(fd, &statbuf);
558 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
559 SLOGE("footer file %s is not the expected size!\n", fname);
560 goto errout;
561 }
562
563 /* Seek to the start of the crypt footer */
564 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
565 SLOGE("Cannot seek to real block device footer\n");
566 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800567 }
568
569 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
570 SLOGE("Cannot read real block device footer\n");
571 goto errout;
572 }
573
574 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700575 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800576 goto errout;
577 }
578
Kenny Rootc96a5f82013-06-14 12:08:28 -0700579 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
580 SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
581 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800582 goto errout;
583 }
584
Kenny Rootc96a5f82013-06-14 12:08:28 -0700585 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
586 SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
587 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800588 }
589
Ken Sumrall160b4d62013-04-22 12:15:39 -0700590 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
591 * copy on disk before returning.
592 */
Kenny Rootc96a5f82013-06-14 12:08:28 -0700593 if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700594 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
Ken Sumralle8744072011-01-18 22:01:55 -0800595 }
596
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800597 /* Success! */
598 rc = 0;
599
600errout:
601 close(fd);
602 return rc;
603}
604
Ken Sumrall160b4d62013-04-22 12:15:39 -0700605static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
606{
607 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
608 crypt_ftr->persist_data_offset[1]) {
609 SLOGE("Crypt_ftr persist data regions overlap");
610 return -1;
611 }
612
613 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
614 SLOGE("Crypt_ftr persist data region 0 starts after region 1");
615 return -1;
616 }
617
618 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
619 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
620 CRYPT_FOOTER_OFFSET) {
621 SLOGE("Persistent data extends past crypto footer");
622 return -1;
623 }
624
625 return 0;
626}
627
628static int load_persistent_data(void)
629{
630 struct crypt_mnt_ftr crypt_ftr;
631 struct crypt_persist_data *pdata = NULL;
632 char encrypted_state[PROPERTY_VALUE_MAX];
633 char *fname;
634 int found = 0;
635 int fd;
636 int ret;
637 int i;
638
639 if (persist_data) {
640 /* Nothing to do, we've already loaded or initialized it */
641 return 0;
642 }
643
644
645 /* If not encrypted, just allocate an empty table and initialize it */
646 property_get("ro.crypto.state", encrypted_state, "");
647 if (strcmp(encrypted_state, "encrypted") ) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800648 pdata = (crypt_persist_data*)malloc(CRYPT_PERSIST_DATA_SIZE);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700649 if (pdata) {
650 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
651 persist_data = pdata;
652 return 0;
653 }
654 return -1;
655 }
656
657 if(get_crypt_ftr_and_key(&crypt_ftr)) {
658 return -1;
659 }
660
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700661 if ((crypt_ftr.major_version < 1)
662 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700663 SLOGE("Crypt_ftr version doesn't support persistent data");
664 return -1;
665 }
666
667 if (get_crypt_ftr_info(&fname, NULL)) {
668 return -1;
669 }
670
671 ret = validate_persistent_data_storage(&crypt_ftr);
672 if (ret) {
673 return -1;
674 }
675
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700676 fd = open(fname, O_RDONLY|O_CLOEXEC);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700677 if (fd < 0) {
678 SLOGE("Cannot open %s metadata file", fname);
679 return -1;
680 }
681
Wei Wang4375f1b2017-02-24 17:43:01 -0800682 pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
Paul Lawrence300dae72016-03-11 11:02:52 -0800683 if (pdata == NULL) {
684 SLOGE("Cannot allocate memory for persistent data");
685 goto err;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700686 }
687
688 for (i = 0; i < 2; i++) {
689 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
690 SLOGE("Cannot seek to read persistent data on %s", fname);
691 goto err2;
692 }
693 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
694 SLOGE("Error reading persistent data on iteration %d", i);
695 goto err2;
696 }
697 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
698 found = 1;
699 break;
700 }
701 }
702
703 if (!found) {
704 SLOGI("Could not find valid persistent data, creating");
705 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
706 }
707
708 /* Success */
709 persist_data = pdata;
710 close(fd);
711 return 0;
712
713err2:
714 free(pdata);
715
716err:
717 close(fd);
718 return -1;
719}
720
721static int save_persistent_data(void)
722{
723 struct crypt_mnt_ftr crypt_ftr;
724 struct crypt_persist_data *pdata;
725 char *fname;
726 off64_t write_offset;
727 off64_t erase_offset;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700728 int fd;
729 int ret;
730
731 if (persist_data == NULL) {
732 SLOGE("No persistent data to save");
733 return -1;
734 }
735
736 if(get_crypt_ftr_and_key(&crypt_ftr)) {
737 return -1;
738 }
739
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700740 if ((crypt_ftr.major_version < 1)
741 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700742 SLOGE("Crypt_ftr version doesn't support persistent data");
743 return -1;
744 }
745
746 ret = validate_persistent_data_storage(&crypt_ftr);
747 if (ret) {
748 return -1;
749 }
750
751 if (get_crypt_ftr_info(&fname, NULL)) {
752 return -1;
753 }
754
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700755 fd = open(fname, O_RDWR|O_CLOEXEC);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700756 if (fd < 0) {
757 SLOGE("Cannot open %s metadata file", fname);
758 return -1;
759 }
760
Wei Wang4375f1b2017-02-24 17:43:01 -0800761 pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700762 if (pdata == NULL) {
763 SLOGE("Cannot allocate persistant data");
764 goto err;
765 }
766
767 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
768 SLOGE("Cannot seek to read persistent data on %s", fname);
769 goto err2;
770 }
771
772 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
773 SLOGE("Error reading persistent data before save");
774 goto err2;
775 }
776
777 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
778 /* The first copy is the curent valid copy, so write to
779 * the second copy and erase this one */
780 write_offset = crypt_ftr.persist_data_offset[1];
781 erase_offset = crypt_ftr.persist_data_offset[0];
782 } else {
783 /* The second copy must be the valid copy, so write to
784 * the first copy, and erase the second */
785 write_offset = crypt_ftr.persist_data_offset[0];
786 erase_offset = crypt_ftr.persist_data_offset[1];
787 }
788
789 /* Write the new copy first, if successful, then erase the old copy */
Björn Landström96dbee72015-01-20 12:43:56 +0100790 if (lseek64(fd, write_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700791 SLOGE("Cannot seek to write persistent data");
792 goto err2;
793 }
794 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
795 (int) crypt_ftr.persist_data_size) {
Björn Landström96dbee72015-01-20 12:43:56 +0100796 if (lseek64(fd, erase_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700797 SLOGE("Cannot seek to erase previous persistent data");
798 goto err2;
799 }
800 fsync(fd);
801 memset(pdata, 0, crypt_ftr.persist_data_size);
802 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
803 (int) crypt_ftr.persist_data_size) {
804 SLOGE("Cannot write to erase previous persistent data");
805 goto err2;
806 }
807 fsync(fd);
808 } else {
809 SLOGE("Cannot write to save persistent data");
810 goto err2;
811 }
812
813 /* Success */
814 free(pdata);
815 close(fd);
816 return 0;
817
818err2:
819 free(pdata);
820err:
821 close(fd);
822 return -1;
823}
824
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800825/* Convert a binary key of specified length into an ascii hex string equivalent,
826 * without the leading 0x and with null termination
827 */
Jeff Sharkey9c484982015-03-31 10:35:33 -0700828static void convert_key_to_hex_ascii(const unsigned char *master_key,
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700829 unsigned int keysize, char *master_key_ascii) {
830 unsigned int i, a;
831 unsigned char nibble;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800832
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700833 for (i=0, a=0; i<keysize; i++, a+=2) {
834 /* For each byte, write out two ascii hex digits */
835 nibble = (master_key[i] >> 4) & 0xf;
836 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800837
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700838 nibble = master_key[i] & 0xf;
839 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
840 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800841
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700842 /* Add the null termination */
843 master_key_ascii[a] = '\0';
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800844
845}
846
Jeff Sharkey9c484982015-03-31 10:35:33 -0700847static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr,
848 const unsigned char *master_key, const char *real_blk_name,
849 const char *name, int fd, const char *extra_params) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800850 alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumralldb5e0262013-02-05 17:39:48 -0800851 struct dm_ioctl *io;
852 struct dm_target_spec *tgt;
853 char *crypt_params;
854 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
George Burgess IV605d7ae2016-02-29 13:39:17 -0800855 size_t buff_offset;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800856 int i;
857
858 io = (struct dm_ioctl *) buffer;
859
860 /* Load the mapping table for this device */
861 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
862
863 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
864 io->target_count = 1;
865 tgt->status = 0;
866 tgt->sector_start = 0;
867 tgt->length = crypt_ftr->fs_size;
Ajay Dudani87701e22014-09-17 21:02:52 -0700868 strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME);
Ken Sumralldb5e0262013-02-05 17:39:48 -0800869
870 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
871 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
George Burgess IV605d7ae2016-02-29 13:39:17 -0800872
873 buff_offset = crypt_params - buffer;
874 snprintf(crypt_params, sizeof(buffer) - buff_offset, "%s %s 0 %s 0 %s",
875 crypt_ftr->crypto_type_name, master_key_ascii, real_blk_name,
876 extra_params);
Ken Sumralldb5e0262013-02-05 17:39:48 -0800877 crypt_params += strlen(crypt_params) + 1;
878 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
879 tgt->next = crypt_params - buffer;
880
881 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
882 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
883 break;
884 }
885 usleep(500000);
886 }
887
888 if (i == TABLE_LOAD_RETRIES) {
889 /* We failed to load the table, return an error */
890 return -1;
891 } else {
892 return i + 1;
893 }
894}
895
896
897static int get_dm_crypt_version(int fd, const char *name, int *version)
898{
899 char buffer[DM_CRYPT_BUF_SIZE];
900 struct dm_ioctl *io;
901 struct dm_target_versions *v;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800902
903 io = (struct dm_ioctl *) buffer;
904
905 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
906
907 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
908 return -1;
909 }
910
911 /* Iterate over the returned versions, looking for name of "crypt".
912 * When found, get and return the version.
913 */
914 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
915 while (v->next) {
916 if (! strcmp(v->name, "crypt")) {
917 /* We found the crypt driver, return the version, and get out */
918 version[0] = v->version[0];
919 version[1] = v->version[1];
920 version[2] = v->version[2];
921 return 0;
922 }
923 v = (struct dm_target_versions *)(((char *)v) + v->next);
924 }
925
926 return -1;
927}
928
Jeff Sharkey9c484982015-03-31 10:35:33 -0700929static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr,
930 const unsigned char *master_key, const char *real_blk_name,
931 char *crypto_blk_name, const char *name) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800932 char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800933 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800934 unsigned int minor;
Ajay Dudani87701e22014-09-17 21:02:52 -0700935 int fd=0;
Daniel Rosenberg25a52132016-02-26 16:44:36 -0800936 int err;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800937 int retval = -1;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800938 int version[3];
Wei Wang4375f1b2017-02-24 17:43:01 -0800939 const char *extra_params;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800940 int load_count;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800941
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700942 if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800943 SLOGE("Cannot open device-mapper\n");
944 goto errout;
945 }
946
947 io = (struct dm_ioctl *) buffer;
948
949 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Daniel Rosenberg25a52132016-02-26 16:44:36 -0800950 err = ioctl(fd, DM_DEV_CREATE, io);
951 if (err) {
952 SLOGE("Cannot create dm-crypt device %s: %s\n", name, strerror(errno));
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800953 goto errout;
954 }
955
956 /* Get the device status, in particular, the name of it's device file */
957 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
958 if (ioctl(fd, DM_DEV_STATUS, io)) {
959 SLOGE("Cannot retrieve dm-crypt device status\n");
960 goto errout;
961 }
962 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
963 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
964
Ken Sumralldb5e0262013-02-05 17:39:48 -0800965 extra_params = "";
966 if (! get_dm_crypt_version(fd, name, version)) {
967 /* Support for allow_discards was added in version 1.11.0 */
968 if ((version[0] >= 2) ||
969 ((version[0] == 1) && (version[1] >= 11))) {
970 extra_params = "1 allow_discards";
971 SLOGI("Enabling support for allow_discards in dmcrypt.\n");
972 }
Ken Sumralle919efe2012-09-29 17:07:41 -0700973 }
974
Ken Sumralldb5e0262013-02-05 17:39:48 -0800975 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name,
976 fd, extra_params);
977 if (load_count < 0) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800978 SLOGE("Cannot load dm-crypt mapping table.\n");
979 goto errout;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800980 } else if (load_count > 1) {
981 SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800982 }
983
984 /* Resume this device to activate it */
Ken Sumralldb5e0262013-02-05 17:39:48 -0800985 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800986
987 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
988 SLOGE("Cannot resume the dm-crypt device\n");
989 goto errout;
990 }
991
992 /* We made it here with no errors. Woot! */
993 retval = 0;
994
995errout:
996 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
997
998 return retval;
999}
1000
Wei Wang4375f1b2017-02-24 17:43:01 -08001001static int delete_crypto_blk_dev(const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001002{
1003 int fd;
1004 char buffer[DM_CRYPT_BUF_SIZE];
1005 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001006 int retval = -1;
1007
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001008 if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001009 SLOGE("Cannot open device-mapper\n");
1010 goto errout;
1011 }
1012
1013 io = (struct dm_ioctl *) buffer;
1014
1015 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1016 if (ioctl(fd, DM_DEV_REMOVE, io)) {
1017 SLOGE("Cannot remove dm-crypt device\n");
1018 goto errout;
1019 }
1020
1021 /* We made it here with no errors. Woot! */
1022 retval = 0;
1023
1024errout:
1025 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1026
1027 return retval;
1028
1029}
1030
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001031static int pbkdf2(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001032 unsigned char *ikey, void *params UNUSED)
1033{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001034 SLOGI("Using pbkdf2 for cryptfs KDF");
1035
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001036 /* Turn the password into a key and IV that can decrypt the master key */
Adam Langleybf0d9722015-11-04 14:51:39 -08001037 return PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
1038 HASH_COUNT, KEY_LEN_BYTES + IV_LEN_BYTES,
1039 ikey) != 1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001040}
1041
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001042static int scrypt(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001043 unsigned char *ikey, void *params)
1044{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001045 SLOGI("Using scrypt for cryptfs KDF");
1046
Kenny Rootc4c70f12013-06-14 12:11:38 -07001047 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1048
1049 int N = 1 << ftr->N_factor;
1050 int r = 1 << ftr->r_factor;
1051 int p = 1 << ftr->p_factor;
1052
1053 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001054 unsigned int keysize;
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001055 crypto_scrypt((const uint8_t*)passwd, strlen(passwd),
1056 salt, SALT_LEN, N, r, p, ikey,
1057 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001058
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001059 return 0;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001060}
1061
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001062static int scrypt_keymaster(const char *passwd, const unsigned char *salt,
1063 unsigned char *ikey, void *params)
1064{
1065 SLOGI("Using scrypt with keymaster for cryptfs KDF");
1066
1067 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001068 size_t signature_size;
1069 unsigned char* signature;
1070 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1071
1072 int N = 1 << ftr->N_factor;
1073 int r = 1 << ftr->r_factor;
1074 int p = 1 << ftr->p_factor;
1075
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001076 rc = crypto_scrypt((const uint8_t*)passwd, strlen(passwd),
1077 salt, SALT_LEN, N, r, p, ikey,
1078 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001079
1080 if (rc) {
1081 SLOGE("scrypt failed");
1082 return -1;
1083 }
1084
Shawn Willdene17a9c42014-09-08 13:04:08 -06001085 if (keymaster_sign_object(ftr, ikey, KEY_LEN_BYTES + IV_LEN_BYTES,
1086 &signature, &signature_size)) {
1087 SLOGE("Signing failed");
1088 return -1;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001089 }
1090
1091 rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN,
1092 N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES);
1093 free(signature);
1094
1095 if (rc) {
1096 SLOGE("scrypt failed");
1097 return -1;
1098 }
1099
1100 return 0;
1101}
1102
1103static int encrypt_master_key(const char *passwd, const unsigned char *salt,
1104 const unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001105 unsigned char *encrypted_master_key,
1106 struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001107{
1108 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
1109 EVP_CIPHER_CTX e_ctx;
1110 int encrypted_len, final_len;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001111 int rc = 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001112
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001113 /* Turn the password into an intermediate key and IV that can decrypt the master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001114 get_device_scrypt_params(crypt_ftr);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001115
1116 switch (crypt_ftr->kdf_type) {
1117 case KDF_SCRYPT_KEYMASTER:
1118 if (keymaster_create_key(crypt_ftr)) {
1119 SLOGE("keymaster_create_key failed");
1120 return -1;
1121 }
1122
1123 if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) {
1124 SLOGE("scrypt failed");
1125 return -1;
1126 }
1127 break;
1128
1129 case KDF_SCRYPT:
1130 if (scrypt(passwd, salt, ikey, crypt_ftr)) {
1131 SLOGE("scrypt failed");
1132 return -1;
1133 }
1134 break;
1135
1136 default:
1137 SLOGE("Invalid kdf_type");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001138 return -1;
1139 }
Kenny Rootc4c70f12013-06-14 12:11:38 -07001140
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001141 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001142 EVP_CIPHER_CTX_init(&e_ctx);
1143 if (! EVP_EncryptInit_ex(&e_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001144 SLOGE("EVP_EncryptInit failed\n");
1145 return -1;
1146 }
1147 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001148
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001149 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001150 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
Paul Lawrence731a7a22015-04-28 22:14:15 +00001151 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001152 SLOGE("EVP_EncryptUpdate failed\n");
1153 return -1;
1154 }
Adam Langley889c4f12014-09-03 14:23:13 -07001155 if (! EVP_EncryptFinal_ex(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001156 SLOGE("EVP_EncryptFinal failed\n");
1157 return -1;
1158 }
1159
1160 if (encrypted_len + final_len != KEY_LEN_BYTES) {
1161 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
1162 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001163 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001164
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001165 /* Store the scrypt of the intermediate key, so we can validate if it's a
1166 password error or mount error when things go wrong.
1167 Note there's no need to check for errors, since if this is incorrect, we
1168 simply won't wipe userdata, which is the correct default behavior
1169 */
1170 int N = 1 << crypt_ftr->N_factor;
1171 int r = 1 << crypt_ftr->r_factor;
1172 int p = 1 << crypt_ftr->p_factor;
1173
1174 rc = crypto_scrypt(ikey, KEY_LEN_BYTES,
1175 crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p,
1176 crypt_ftr->scrypted_intermediate_key,
1177 sizeof(crypt_ftr->scrypted_intermediate_key));
1178
1179 if (rc) {
1180 SLOGE("encrypt_master_key: crypto_scrypt failed");
1181 }
1182
Thurston Hou Yeen Dang06dc3112016-07-18 14:16:37 -07001183 EVP_CIPHER_CTX_cleanup(&e_ctx);
1184
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001185 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001186}
1187
Paul Lawrence731a7a22015-04-28 22:14:15 +00001188static int decrypt_master_key_aux(const char *passwd, unsigned char *salt,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001189 unsigned char *encrypted_master_key,
1190 unsigned char *decrypted_master_key,
1191 kdf_func kdf, void *kdf_params,
1192 unsigned char** intermediate_key,
1193 size_t* intermediate_key_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001194{
1195 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 -08001196 EVP_CIPHER_CTX d_ctx;
1197 int decrypted_len, final_len;
1198
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001199 /* Turn the password into an intermediate key and IV that can decrypt the
1200 master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001201 if (kdf(passwd, salt, ikey, kdf_params)) {
1202 SLOGE("kdf failed");
1203 return -1;
1204 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001205
1206 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001207 EVP_CIPHER_CTX_init(&d_ctx);
1208 if (! EVP_DecryptInit_ex(&d_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001209 return -1;
1210 }
1211 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1212 /* Decrypt the master key */
1213 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
1214 encrypted_master_key, KEY_LEN_BYTES)) {
1215 return -1;
1216 }
Adam Langley889c4f12014-09-03 14:23:13 -07001217 if (! EVP_DecryptFinal_ex(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001218 return -1;
1219 }
1220
1221 if (decrypted_len + final_len != KEY_LEN_BYTES) {
1222 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001223 }
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001224
1225 /* Copy intermediate key if needed by params */
1226 if (intermediate_key && intermediate_key_size) {
1227 *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES);
Greg Kaisere8167af2016-04-20 10:50:15 -07001228 if (*intermediate_key) {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001229 memcpy(*intermediate_key, ikey, KEY_LEN_BYTES);
1230 *intermediate_key_size = KEY_LEN_BYTES;
1231 }
1232 }
1233
Thurston Hou Yeen Dang06dc3112016-07-18 14:16:37 -07001234 EVP_CIPHER_CTX_cleanup(&d_ctx);
1235
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001236 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001237}
1238
Kenny Rootc4c70f12013-06-14 12:11:38 -07001239static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001240{
Paul Lawrencedb3730c2015-02-03 13:08:10 -08001241 if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001242 *kdf = scrypt_keymaster;
1243 *kdf_params = ftr;
1244 } else if (ftr->kdf_type == KDF_SCRYPT) {
Kenny Rootc4c70f12013-06-14 12:11:38 -07001245 *kdf = scrypt;
1246 *kdf_params = ftr;
1247 } else {
1248 *kdf = pbkdf2;
1249 *kdf_params = NULL;
1250 }
1251}
1252
Paul Lawrence731a7a22015-04-28 22:14:15 +00001253static int decrypt_master_key(const char *passwd, unsigned char *decrypted_master_key,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001254 struct crypt_mnt_ftr *crypt_ftr,
1255 unsigned char** intermediate_key,
1256 size_t* intermediate_key_size)
Kenny Rootc4c70f12013-06-14 12:11:38 -07001257{
1258 kdf_func kdf;
1259 void *kdf_params;
1260 int ret;
1261
1262 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001263 ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key,
1264 decrypted_master_key, kdf, kdf_params,
1265 intermediate_key, intermediate_key_size);
Kenny Rootc4c70f12013-06-14 12:11:38 -07001266 if (ret != 0) {
1267 SLOGW("failure decrypting master key");
Kenny Rootc4c70f12013-06-14 12:11:38 -07001268 }
1269
1270 return ret;
1271}
1272
Wei Wang4375f1b2017-02-24 17:43:01 -08001273static int create_encrypted_random_key(const char *passwd, unsigned char *master_key, unsigned char *salt,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001274 struct crypt_mnt_ftr *crypt_ftr) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001275 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -08001276 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001277
1278 /* Get some random bits for a key */
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001279 fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC);
Ken Sumralle8744072011-01-18 22:01:55 -08001280 read(fd, key_buf, sizeof(key_buf));
1281 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001282 close(fd);
1283
1284 /* Now encrypt it with the password */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001285 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001286}
1287
Paul Lawrence2f32cda2015-05-05 14:28:25 -07001288int wait_and_unmount(const char *mountpoint, bool kill)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001289{
Greg Hackmann955653e2014-09-24 14:55:20 -07001290 int i, err, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001291#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001292
1293 /* Now umount the tmpfs filesystem */
1294 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001295 if (umount(mountpoint) == 0) {
1296 break;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001297 }
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001298
1299 if (errno == EINVAL) {
1300 /* EINVAL is returned if the directory is not a mountpoint,
1301 * i.e. there is no filesystem mounted there. So just get out.
1302 */
1303 break;
1304 }
1305
1306 err = errno;
1307
1308 /* If allowed, be increasingly aggressive before the last two retries */
1309 if (kill) {
1310 if (i == (WAIT_UNMOUNT_COUNT - 3)) {
1311 SLOGW("sending SIGHUP to processes with open files\n");
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001312 vold_killProcessesWithOpenFiles(mountpoint, SIGTERM);
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001313 } else if (i == (WAIT_UNMOUNT_COUNT - 2)) {
1314 SLOGW("sending SIGKILL to processes with open files\n");
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001315 vold_killProcessesWithOpenFiles(mountpoint, SIGKILL);
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001316 }
1317 }
1318
1319 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001320 }
1321
1322 if (i < WAIT_UNMOUNT_COUNT) {
1323 SLOGD("unmounting %s succeeded\n", mountpoint);
1324 rc = 0;
1325 } else {
jessica_yu3f14fe42014-09-22 15:57:40 +08001326 vold_killProcessesWithOpenFiles(mountpoint, 0);
Greg Hackmann955653e2014-09-24 14:55:20 -07001327 SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001328 rc = -1;
1329 }
1330
1331 return rc;
1332}
1333
Wei Wang42e38102017-06-07 10:46:12 -07001334static void prep_data_fs(void)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001335{
Jeff Sharkey47695b22016-02-01 17:02:29 -07001336 // NOTE: post_fs_data results in init calling back around to vold, so all
1337 // callers to this method must be async
1338
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001339 /* Do the prep of the /data filesystem */
1340 property_set("vold.post_fs_data_done", "0");
1341 property_set("vold.decrypt", "trigger_post_fs_data");
Wei Wang42e38102017-06-07 10:46:12 -07001342 SLOGD("Just triggered post_fs_data");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001343
Ken Sumrallc5872692013-05-14 15:26:31 -07001344 /* Wait a max of 50 seconds, hopefully it takes much less */
Wei Wang42e38102017-06-07 10:46:12 -07001345 while (!android::base::WaitForProperty("vold.post_fs_data_done",
Wei Wang4375f1b2017-02-24 17:43:01 -08001346 "1",
Wei Wang42e38102017-06-07 10:46:12 -07001347 std::chrono::seconds(15))) {
1348 /* We timed out to prep /data in time. Continue wait. */
1349 SLOGE("waited 15s for vold.post_fs_data_done, still waiting...");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001350 }
Wei Wang42e38102017-06-07 10:46:12 -07001351 SLOGD("post_fs_data done");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001352}
1353
Paul Lawrence74f29f12014-08-28 15:54:10 -07001354static void cryptfs_set_corrupt()
1355{
1356 // Mark the footer as bad
1357 struct crypt_mnt_ftr crypt_ftr;
1358 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1359 SLOGE("Failed to get crypto footer - panic");
1360 return;
1361 }
1362
1363 crypt_ftr.flags |= CRYPT_DATA_CORRUPT;
1364 if (put_crypt_ftr_and_key(&crypt_ftr)) {
1365 SLOGE("Failed to set crypto footer - panic");
1366 return;
1367 }
1368}
1369
1370static void cryptfs_trigger_restart_min_framework()
1371{
1372 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
1373 SLOGE("Failed to mount tmpfs on data - panic");
1374 return;
1375 }
1376
1377 if (property_set("vold.decrypt", "trigger_post_fs_data")) {
1378 SLOGE("Failed to trigger post fs data - panic");
1379 return;
1380 }
1381
1382 if (property_set("vold.decrypt", "trigger_restart_min_framework")) {
1383 SLOGE("Failed to trigger restart min framework - panic");
1384 return;
1385 }
1386}
1387
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001388/* returns < 0 on failure */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001389static int cryptfs_restart_internal(int restart_main)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001390{
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001391 char crypto_blkdev[MAXPATHLEN];
Tim Murray8439dc92014-12-15 11:56:11 -08001392 int rc = -1;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001393 static int restart_successful = 0;
1394
1395 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -06001396 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001397 SLOGE("Encrypted filesystem not validated, aborting");
1398 return -1;
1399 }
1400
1401 if (restart_successful) {
1402 SLOGE("System already restarted with encrypted disk, aborting");
1403 return -1;
1404 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001405
Paul Lawrencef4faa572014-01-29 13:31:03 -08001406 if (restart_main) {
1407 /* Here is where we shut down the framework. The init scripts
1408 * start all services in one of three classes: core, main or late_start.
1409 * On boot, we start core and main. Now, we stop main, but not core,
1410 * as core includes vold and a few other really important things that
1411 * we need to keep running. Once main has stopped, we should be able
1412 * to umount the tmpfs /data, then mount the encrypted /data.
1413 * We then restart the class main, and also the class late_start.
1414 * At the moment, I've only put a few things in late_start that I know
1415 * are not needed to bring up the framework, and that also cause problems
1416 * with unmounting the tmpfs /data, but I hope to add add more services
1417 * to the late_start class as we optimize this to decrease the delay
1418 * till the user is asked for the password to the filesystem.
1419 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001420
Paul Lawrencef4faa572014-01-29 13:31:03 -08001421 /* The init files are setup to stop the class main when vold.decrypt is
1422 * set to trigger_reset_main.
1423 */
1424 property_set("vold.decrypt", "trigger_reset_main");
1425 SLOGD("Just asked init to shut down class main\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001426
Paul Lawrencef4faa572014-01-29 13:31:03 -08001427 /* Ugh, shutting down the framework is not synchronous, so until it
1428 * can be fixed, this horrible hack will wait a moment for it all to
1429 * shut down before proceeding. Without it, some devices cannot
1430 * restart the graphics services.
1431 */
1432 sleep(2);
1433 }
Ken Sumrall9dedfd42012-10-09 14:16:59 -07001434
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001435 /* Now that the framework is shutdown, we should be able to umount()
1436 * the tmpfs filesystem, and mount the real one.
1437 */
1438
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001439 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1440 if (strlen(crypto_blkdev) == 0) {
1441 SLOGE("fs_crypto_blkdev not set\n");
1442 return -1;
1443 }
1444
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001445 if (! (rc = wait_and_unmount(DATA_MNT_POINT, true)) ) {
Doug Zongker6fd57712013-12-17 09:43:23 -08001446 /* If ro.crypto.readonly is set to 1, mount the decrypted
1447 * filesystem readonly. This is used when /data is mounted by
1448 * recovery mode.
1449 */
1450 char ro_prop[PROPERTY_VALUE_MAX];
1451 property_get("ro.crypto.readonly", ro_prop, "");
1452 if (strlen(ro_prop) > 0 && atoi(ro_prop)) {
1453 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
Luis Hector Chavezbbb512d2018-05-30 15:47:50 -07001454 if (rec) {
1455 rec->flags |= MS_RDONLY;
1456 }
Doug Zongker6fd57712013-12-17 09:43:23 -08001457 }
JP Abgrall62c7af32014-06-16 13:01:23 -07001458
Ken Sumralle5032c42012-04-01 23:58:44 -07001459 /* If that succeeded, then mount the decrypted filesystem */
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001460 int retries = RETRY_MOUNT_ATTEMPTS;
1461 int mount_rc;
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001462
1463 /*
1464 * fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
1465 * partitions in the fsck domain.
1466 */
1467 if (setexeccon(secontextFsck())){
1468 SLOGE("Failed to setexeccon");
1469 return -1;
1470 }
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001471 while ((mount_rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT,
1472 crypto_blkdev, 0))
1473 != 0) {
1474 if (mount_rc == FS_MGR_DOMNT_BUSY) {
1475 /* TODO: invoke something similar to
1476 Process::killProcessWithOpenFiles(DATA_MNT_POINT,
1477 retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */
1478 SLOGI("Failed to mount %s because it is busy - waiting",
1479 crypto_blkdev);
1480 if (--retries) {
1481 sleep(RETRY_MOUNT_DELAY_SECONDS);
1482 } else {
1483 /* Let's hope that a reboot clears away whatever is keeping
1484 the mount busy */
Josh Gaofec44372017-08-28 13:22:55 -07001485 cryptfs_reboot(RebootType::reboot);
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001486 }
1487 } else {
1488 SLOGE("Failed to mount decrypted data");
1489 cryptfs_set_corrupt();
1490 cryptfs_trigger_restart_min_framework();
1491 SLOGI("Started framework to offer wipe");
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001492 if (setexeccon(NULL)) {
1493 SLOGE("Failed to setexeccon");
1494 }
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001495 return -1;
1496 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001497 }
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001498 if (setexeccon(NULL)) {
1499 SLOGE("Failed to setexeccon");
1500 return -1;
1501 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001502
Ken Sumralle5032c42012-04-01 23:58:44 -07001503 /* Create necessary paths on /data */
Wei Wang42e38102017-06-07 10:46:12 -07001504 prep_data_fs();
Seigo Nonakae2ef0c02016-06-20 17:05:40 +09001505 property_set("vold.decrypt", "trigger_load_persist_props");
Ken Sumralle5032c42012-04-01 23:58:44 -07001506
1507 /* startup service classes main and late_start */
1508 property_set("vold.decrypt", "trigger_restart_framework");
1509 SLOGD("Just triggered restart_framework\n");
1510
1511 /* Give it a few moments to get started */
1512 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001513 }
1514
Ken Sumrall0cc16632011-01-18 20:32:26 -08001515 if (rc == 0) {
1516 restart_successful = 1;
1517 }
1518
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001519 return rc;
1520}
1521
Paul Lawrencef4faa572014-01-29 13:31:03 -08001522int cryptfs_restart(void)
1523{
Paul Lawrence05335c32015-03-05 09:46:23 -08001524 SLOGI("cryptfs_restart");
Paul Crowley38132a12016-02-09 09:50:32 +00001525 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001526 SLOGE("cryptfs_restart not valid for file encryption:");
1527 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08001528 }
1529
Paul Lawrencef4faa572014-01-29 13:31:03 -08001530 /* Call internal implementation forcing a restart of main service group */
1531 return cryptfs_restart_internal(1);
1532}
1533
Wei Wang4375f1b2017-02-24 17:43:01 -08001534static int do_crypto_complete(const char *mount_point)
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001535{
1536 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001537 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -08001538 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001539
1540 property_get("ro.crypto.state", encrypted_state, "");
1541 if (strcmp(encrypted_state, "encrypted") ) {
1542 SLOGE("not running with encryption, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001543 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001544 }
1545
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001546 // crypto_complete is full disk encrypted status
Paul Crowley38132a12016-02-09 09:50:32 +00001547 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001548 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Paul Lawrence05335c32015-03-05 09:46:23 -08001549 }
1550
Ken Sumrall160b4d62013-04-22 12:15:39 -07001551 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001552 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumralle5032c42012-04-01 23:58:44 -07001553
Ken Sumralle1a45852011-12-14 21:24:27 -08001554 /*
1555 * Only report this error if key_loc is a file and it exists.
1556 * If the device was never encrypted, and /data is not mountable for
1557 * some reason, returning 1 should prevent the UI from presenting the
1558 * a "enter password" screen, or worse, a "press button to wipe the
1559 * device" screen.
1560 */
1561 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
1562 SLOGE("master key file does not exist, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001563 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumralle1a45852011-12-14 21:24:27 -08001564 } else {
1565 SLOGE("Error getting crypt footer and key\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001566 return CRYPTO_COMPLETE_BAD_METADATA;
Ken Sumralle1a45852011-12-14 21:24:27 -08001567 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001568 }
1569
Paul Lawrence74f29f12014-08-28 15:54:10 -07001570 // Test for possible error flags
1571 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS){
1572 SLOGE("Encryption process is partway completed\n");
1573 return CRYPTO_COMPLETE_PARTIAL;
1574 }
1575
1576 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE){
1577 SLOGE("Encryption process was interrupted but cannot continue\n");
1578 return CRYPTO_COMPLETE_INCONSISTENT;
1579 }
1580
1581 if (crypt_ftr.flags & CRYPT_DATA_CORRUPT){
1582 SLOGE("Encryption is successful but data is corrupt\n");
1583 return CRYPTO_COMPLETE_CORRUPT;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001584 }
1585
1586 /* We passed the test! We shall diminish, and return to the west */
Paul Lawrence74f29f12014-08-28 15:54:10 -07001587 return CRYPTO_COMPLETE_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001588}
1589
Paul Lawrencef4faa572014-01-29 13:31:03 -08001590static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
Wei Wang4375f1b2017-02-24 17:43:01 -08001591 const char *passwd, const char *mount_point, const char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001592{
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001593 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001594 unsigned char decrypted_master_key[32];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001595 char crypto_blkdev[MAXPATHLEN];
1596 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001597 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001598 unsigned int orig_failed_decrypt_count;
1599 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001600 int use_keymaster = 0;
1601 int upgrade = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001602 unsigned char* intermediate_key = 0;
1603 size_t intermediate_key_size = 0;
Wei Wang4375f1b2017-02-24 17:43:01 -08001604 int N = 1 << crypt_ftr->N_factor;
1605 int r = 1 << crypt_ftr->r_factor;
1606 int p = 1 << crypt_ftr->p_factor;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001607
Paul Lawrencef4faa572014-01-29 13:31:03 -08001608 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1609 orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001610
Paul Lawrencef4faa572014-01-29 13:31:03 -08001611 if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001612 if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr,
1613 &intermediate_key, &intermediate_key_size)) {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001614 SLOGE("Failed to decrypt master key\n");
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001615 rc = -1;
1616 goto errout;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001617 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001618 }
1619
Paul Lawrencef4faa572014-01-29 13:31:03 -08001620 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
1621
Paul Lawrence74f29f12014-08-28 15:54:10 -07001622 // Create crypto block device - all (non fatal) code paths
1623 // need it
Paul Lawrencef4faa572014-01-29 13:31:03 -08001624 if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key,
1625 real_blkdev, crypto_blkdev, label)) {
Paul Lawrence74f29f12014-08-28 15:54:10 -07001626 SLOGE("Error creating decrypted block device\n");
1627 rc = -1;
1628 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001629 }
1630
Paul Lawrence74f29f12014-08-28 15:54:10 -07001631 /* Work out if the problem is the password or the data */
1632 unsigned char scrypted_intermediate_key[sizeof(crypt_ftr->
1633 scrypted_intermediate_key)];
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001634
Paul Lawrence74f29f12014-08-28 15:54:10 -07001635 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
1636 crypt_ftr->salt, sizeof(crypt_ftr->salt),
1637 N, r, p, scrypted_intermediate_key,
1638 sizeof(scrypted_intermediate_key));
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001639
Paul Lawrence74f29f12014-08-28 15:54:10 -07001640 // Does the key match the crypto footer?
1641 if (rc == 0 && memcmp(scrypted_intermediate_key,
1642 crypt_ftr->scrypted_intermediate_key,
1643 sizeof(scrypted_intermediate_key)) == 0) {
1644 SLOGI("Password matches");
1645 rc = 0;
1646 } else {
1647 /* Try mounting the file system anyway, just in case the problem's with
1648 * the footer, not the key. */
George Burgess IV605d7ae2016-02-29 13:39:17 -08001649 snprintf(tmp_mount_point, sizeof(tmp_mount_point), "%s/tmp_mnt",
1650 mount_point);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001651 mkdir(tmp_mount_point, 0755);
1652 if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
1653 SLOGE("Error temp mounting decrypted block device\n");
1654 delete_crypto_blk_dev(label);
1655
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001656 rc = ++crypt_ftr->failed_decrypt_count;
1657 put_crypt_ftr_and_key(crypt_ftr);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001658 } else {
1659 /* Success! */
1660 SLOGI("Password did not match but decrypted drive mounted - continue");
1661 umount(tmp_mount_point);
1662 rc = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001663 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001664 }
1665
1666 if (rc == 0) {
1667 crypt_ftr->failed_decrypt_count = 0;
Paul Lawrence72b8b822014-10-05 12:57:37 -07001668 if (orig_failed_decrypt_count != 0) {
1669 put_crypt_ftr_and_key(crypt_ftr);
1670 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001671
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001672 /* Save the name of the crypto block device
Paul Lawrence74f29f12014-08-28 15:54:10 -07001673 * so we can mount it when restarting the framework. */
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001674 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -06001675
1676 /* Also save a the master key so we can reencrypted the key
Paul Lawrence74f29f12014-08-28 15:54:10 -07001677 * the key when we want to change the password on it. */
Jason parks70a4b3f2011-01-28 10:10:47 -06001678 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001679 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -06001680 master_key_saved = 1;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001681 SLOGD("%s(): Master key saved\n", __FUNCTION__);
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001682 rc = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001683
Paul Lawrence74f29f12014-08-28 15:54:10 -07001684 // Upgrade if we're not using the latest KDF.
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001685 use_keymaster = keymaster_check_compatibility();
1686 if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Shawn Willden47ba10d2014-09-03 17:07:06 -06001687 // Don't allow downgrade
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001688 } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) {
1689 crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1690 upgrade = 1;
1691 } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001692 crypt_ftr->kdf_type = KDF_SCRYPT;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001693 upgrade = 1;
1694 }
1695
1696 if (upgrade) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001697 rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1698 crypt_ftr->master_key, crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001699 if (!rc) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001700 rc = put_crypt_ftr_and_key(crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001701 }
1702 SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
Paul Lawrenceb2f682b2014-09-08 11:28:19 -07001703
1704 // Do not fail even if upgrade failed - machine is bootable
1705 // Note that if this code is ever hit, there is a *serious* problem
1706 // since KDFs should never fail. You *must* fix the kdf before
1707 // proceeding!
1708 if (rc) {
1709 SLOGW("Upgrade failed with error %d,"
1710 " but continuing with previous state",
1711 rc);
1712 rc = 0;
1713 }
JP Abgrall7bdfa522013-11-15 13:42:56 -08001714 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001715 }
1716
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001717 errout:
1718 if (intermediate_key) {
1719 memset(intermediate_key, 0, intermediate_key_size);
1720 free(intermediate_key);
1721 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001722 return rc;
1723}
1724
Ken Sumrall29d8da82011-05-18 17:20:07 -07001725/*
Jeff Sharkey9c484982015-03-31 10:35:33 -07001726 * Called by vold when it's asked to mount an encrypted external
1727 * storage volume. The incoming partition has no crypto header/footer,
1728 * as any metadata is been stored in a separate, small partition.
1729 *
1730 * out_crypto_blkdev must be MAXPATHLEN.
Ken Sumrall29d8da82011-05-18 17:20:07 -07001731 */
Jeff Sharkey9c484982015-03-31 10:35:33 -07001732int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev,
1733 const unsigned char* key, int keysize, char* out_crypto_blkdev) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001734 int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001735 if (fd == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001736 SLOGE("Failed to open %s: %s", real_blkdev, strerror(errno));
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001737 return -1;
1738 }
1739
1740 unsigned long nr_sec = 0;
1741 get_blkdev_size(fd, &nr_sec);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001742 close(fd);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001743
Ken Sumrall29d8da82011-05-18 17:20:07 -07001744 if (nr_sec == 0) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001745 SLOGE("Failed to get size of %s: %s", real_blkdev, strerror(errno));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001746 return -1;
1747 }
1748
Jeff Sharkey9c484982015-03-31 10:35:33 -07001749 struct crypt_mnt_ftr ext_crypt_ftr;
1750 memset(&ext_crypt_ftr, 0, sizeof(ext_crypt_ftr));
1751 ext_crypt_ftr.fs_size = nr_sec;
1752 ext_crypt_ftr.keysize = keysize;
Jeff Sharkey32ebb732017-03-27 16:18:50 -06001753 strlcpy((char*) ext_crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256",
1754 MAX_CRYPTO_TYPE_NAME_LEN);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001755
Jeff Sharkey9c484982015-03-31 10:35:33 -07001756 return create_crypto_blk_dev(&ext_crypt_ftr, key, real_blkdev,
1757 out_crypto_blkdev, label);
1758}
Ken Sumrall29d8da82011-05-18 17:20:07 -07001759
Jeff Sharkey9c484982015-03-31 10:35:33 -07001760/*
1761 * Called by vold when it's asked to unmount an encrypted external
1762 * storage volume.
1763 */
1764int cryptfs_revert_ext_volume(const char* label) {
1765 return delete_crypto_blk_dev((char*) label);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001766}
1767
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001768int cryptfs_crypto_complete(void)
1769{
1770 return do_crypto_complete("/data");
1771}
1772
Paul Lawrencef4faa572014-01-29 13:31:03 -08001773int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr)
1774{
1775 char encrypted_state[PROPERTY_VALUE_MAX];
1776 property_get("ro.crypto.state", encrypted_state, "");
1777 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
1778 SLOGE("encrypted fs already validated or not running with encryption,"
1779 " aborting");
1780 return -1;
1781 }
1782
1783 if (get_crypt_ftr_and_key(crypt_ftr)) {
1784 SLOGE("Error getting crypt footer and key");
1785 return -1;
1786 }
1787
1788 return 0;
1789}
1790
Wei Wang4375f1b2017-02-24 17:43:01 -08001791int cryptfs_check_passwd(const char *passwd)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001792{
Paul Lawrence05335c32015-03-05 09:46:23 -08001793 SLOGI("cryptfs_check_passwd");
Paul Crowley38132a12016-02-09 09:50:32 +00001794 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001795 SLOGE("cryptfs_check_passwd not valid for file encryption");
1796 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08001797 }
1798
Paul Lawrencef4faa572014-01-29 13:31:03 -08001799 struct crypt_mnt_ftr crypt_ftr;
1800 int rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001801
Paul Lawrencef4faa572014-01-29 13:31:03 -08001802 rc = check_unmounted_and_get_ftr(&crypt_ftr);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001803 if (rc) {
1804 SLOGE("Could not get footer");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001805 return rc;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001806 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001807
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001808 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001809 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
1810 if (rc) {
1811 SLOGE("Password did not match");
1812 return rc;
1813 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08001814
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001815 if (crypt_ftr.flags & CRYPT_FORCE_COMPLETE) {
1816 // Here we have a default actual password but a real password
1817 // we must test against the scrypted value
1818 // First, we must delete the crypto block device that
1819 // test_mount_encrypted_fs leaves behind as a side effect
1820 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
1821 rc = test_mount_encrypted_fs(&crypt_ftr, DEFAULT_PASSWORD,
1822 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
1823 if (rc) {
1824 SLOGE("Default password did not match on reboot encryption");
1825 return rc;
1826 }
1827
1828 crypt_ftr.flags &= ~CRYPT_FORCE_COMPLETE;
1829 put_crypt_ftr_and_key(&crypt_ftr);
1830 rc = cryptfs_changepw(crypt_ftr.crypt_type, passwd);
1831 if (rc) {
1832 SLOGE("Could not change password on reboot encryption");
1833 return rc;
1834 }
1835 }
1836
1837 if (crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
Paul Lawrence399317e2014-03-10 13:20:50 -07001838 cryptfs_clear_password();
1839 password = strdup(passwd);
1840 struct timespec now;
1841 clock_gettime(CLOCK_BOOTTIME, &now);
1842 password_expiry_time = now.tv_sec + password_max_age_seconds;
Paul Lawrence684dbdf2014-02-07 12:07:22 -08001843 }
1844
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001845 return rc;
1846}
1847
Ken Sumrall3ad90722011-10-04 20:38:29 -07001848int cryptfs_verify_passwd(char *passwd)
1849{
1850 struct crypt_mnt_ftr crypt_ftr;
1851 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001852 unsigned char decrypted_master_key[32];
Ken Sumrall3ad90722011-10-04 20:38:29 -07001853 char encrypted_state[PROPERTY_VALUE_MAX];
1854 int rc;
1855
1856 property_get("ro.crypto.state", encrypted_state, "");
1857 if (strcmp(encrypted_state, "encrypted") ) {
1858 SLOGE("device not encrypted, aborting");
1859 return -2;
1860 }
1861
1862 if (!master_key_saved) {
1863 SLOGE("encrypted fs not yet mounted, aborting");
1864 return -1;
1865 }
1866
1867 if (!saved_mount_point) {
1868 SLOGE("encrypted fs failed to save mount point, aborting");
1869 return -1;
1870 }
1871
Ken Sumrall160b4d62013-04-22 12:15:39 -07001872 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall3ad90722011-10-04 20:38:29 -07001873 SLOGE("Error getting crypt footer and key\n");
1874 return -1;
1875 }
1876
1877 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
1878 /* If the device has no password, then just say the password is valid */
1879 rc = 0;
1880 } else {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001881 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001882 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
1883 /* They match, the password is correct */
1884 rc = 0;
1885 } else {
1886 /* If incorrect, sleep for a bit to prevent dictionary attacks */
1887 sleep(1);
1888 rc = 1;
1889 }
1890 }
1891
1892 return rc;
1893}
1894
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001895/* Initialize a crypt_mnt_ftr structure. The keysize is
1896 * defaulted to 16 bytes, and the filesystem size to 0.
1897 * Presumably, at a minimum, the caller will update the
1898 * filesystem size and crypto_type_name after calling this function.
1899 */
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001900static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001901{
Ken Sumrall160b4d62013-04-22 12:15:39 -07001902 off64_t off;
1903
1904 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001905 ftr->magic = CRYPT_MNT_MAGIC;
Kenny Rootc96a5f82013-06-14 12:08:28 -07001906 ftr->major_version = CURRENT_MAJOR_VERSION;
1907 ftr->minor_version = CURRENT_MINOR_VERSION;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001908 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Jason parks70a4b3f2011-01-28 10:10:47 -06001909 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall160b4d62013-04-22 12:15:39 -07001910
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001911 switch (keymaster_check_compatibility()) {
1912 case 1:
1913 ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1914 break;
1915
1916 case 0:
1917 ftr->kdf_type = KDF_SCRYPT;
1918 break;
1919
1920 default:
1921 SLOGE("keymaster_check_compatibility failed");
1922 return -1;
1923 }
1924
Kenny Rootc4c70f12013-06-14 12:11:38 -07001925 get_device_scrypt_params(ftr);
1926
Ken Sumrall160b4d62013-04-22 12:15:39 -07001927 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
1928 if (get_crypt_ftr_info(NULL, &off) == 0) {
1929 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
1930 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
1931 ftr->persist_data_size;
1932 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001933
1934 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001935}
1936
Ken Sumrall29d8da82011-05-18 17:20:07 -07001937static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001938{
Ken Sumralle550f782013-08-20 13:48:23 -07001939 const char *args[10];
1940 char size_str[32]; /* Must be large enough to hold a %lld and null byte */
1941 int num_args;
1942 int status;
1943 int tmp;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001944 int rc = -1;
1945
Ken Sumrall29d8da82011-05-18 17:20:07 -07001946 if (type == EXT4_FS) {
Adrien Schildknechte0f409c2016-11-21 15:14:37 -08001947#ifdef TARGET_USES_MKE2FS
1948 args[0] = "/system/bin/mke2fs";
1949 args[1] = "-M";
1950 args[2] = "/data";
1951 args[3] = "-b";
1952 args[4] = "4096";
1953 args[5] = "-t";
1954 args[6] = "ext4";
1955 args[7] = crypto_blkdev;
1956 snprintf(size_str, sizeof(size_str), "%" PRId64, size / (4096 / 512));
1957 args[8] = size_str;
1958 num_args = 9;
1959#else
Ken Sumralle550f782013-08-20 13:48:23 -07001960 args[0] = "/system/bin/make_ext4fs";
1961 args[1] = "-a";
1962 args[2] = "/data";
1963 args[3] = "-l";
Elliott Hughes73737162014-06-25 17:27:42 -07001964 snprintf(size_str, sizeof(size_str), "%" PRId64, size * 512);
Ken Sumralle550f782013-08-20 13:48:23 -07001965 args[4] = size_str;
1966 args[5] = crypto_blkdev;
1967 num_args = 6;
Adrien Schildknechte0f409c2016-11-21 15:14:37 -08001968#endif
Ken Sumralle550f782013-08-20 13:48:23 -07001969 SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n",
1970 args[0], args[1], args[2], args[3], args[4], args[5]);
JP Abgrall62c7af32014-06-16 13:01:23 -07001971 } else if (type == F2FS_FS) {
Jaegeuk Kim98651a22017-06-05 10:22:04 -07001972 args[0] = "/system/bin/make_f2fs";
Jaegeuk Kim8de9f062017-11-01 10:35:30 -07001973 args[1] = "-f";
JP Abgrall62c7af32014-06-16 13:01:23 -07001974 args[2] = "-d1";
Jaegeuk Kim8de9f062017-11-01 10:35:30 -07001975 args[3] = "-O";
1976 args[4] = "encrypt";
1977 args[5] = "-O";
1978 args[6] = "quota";
1979 args[7] = crypto_blkdev;
Elliott Hughes73737162014-06-25 17:27:42 -07001980 snprintf(size_str, sizeof(size_str), "%" PRId64, size);
Jaegeuk Kim8de9f062017-11-01 10:35:30 -07001981 args[8] = size_str;
1982 num_args = 9;
1983 SLOGI("Making empty filesystem with command %s %s %s %s %s %s %s %s %s\n",
1984 args[0], args[1], args[2], args[3], args[4], args[5],
1985 args[6], args[7], args[8]);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001986 } else {
1987 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
1988 return -1;
1989 }
1990
Ken Sumralle550f782013-08-20 13:48:23 -07001991 tmp = android_fork_execvp(num_args, (char **)args, &status, false, true);
1992
1993 if (tmp != 0) {
1994 SLOGE("Error creating empty filesystem on %s due to logwrap error\n", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001995 } else {
Ken Sumralle550f782013-08-20 13:48:23 -07001996 if (WIFEXITED(status)) {
1997 if (WEXITSTATUS(status)) {
1998 SLOGE("Error creating filesystem on %s, exit status %d ",
1999 crypto_blkdev, WEXITSTATUS(status));
2000 } else {
2001 SLOGD("Successfully created filesystem on %s\n", crypto_blkdev);
2002 rc = 0;
2003 }
2004 } else {
2005 SLOGE("Error creating filesystem on %s, did not exit normally\n", crypto_blkdev);
2006 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002007 }
2008
2009 return rc;
2010}
2011
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002012#define CRYPTO_ENABLE_WIPE 1
2013#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002014
2015#define FRAMEWORK_BOOT_WAIT 60
2016
Paul Lawrence87999172014-02-20 12:21:31 -08002017static int cryptfs_SHA256_fileblock(const char* filename, __le8* buf)
2018{
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002019 int fd = open(filename, O_RDONLY|O_CLOEXEC);
Paul Lawrence87999172014-02-20 12:21:31 -08002020 if (fd == -1) {
2021 SLOGE("Error opening file %s", filename);
2022 return -1;
2023 }
2024
2025 char block[CRYPT_INPLACE_BUFSIZE];
2026 memset(block, 0, sizeof(block));
2027 if (unix_read(fd, block, sizeof(block)) < 0) {
2028 SLOGE("Error reading file %s", filename);
2029 close(fd);
2030 return -1;
2031 }
2032
2033 close(fd);
2034
2035 SHA256_CTX c;
2036 SHA256_Init(&c);
2037 SHA256_Update(&c, block, sizeof(block));
2038 SHA256_Final(buf, &c);
2039
2040 return 0;
2041}
2042
JP Abgrall62c7af32014-06-16 13:01:23 -07002043static int get_fs_type(struct fstab_rec *rec)
2044{
2045 if (!strcmp(rec->fs_type, "ext4")) {
2046 return EXT4_FS;
2047 } else if (!strcmp(rec->fs_type, "f2fs")) {
2048 return F2FS_FS;
2049 } else {
2050 return -1;
2051 }
2052}
2053
Paul Lawrence87999172014-02-20 12:21:31 -08002054static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr *crypt_ftr, int how,
2055 char *crypto_blkdev, char *real_blkdev,
2056 int previously_encrypted_upto)
2057{
2058 off64_t cur_encryption_done=0, tot_encryption_size=0;
Tim Murray8439dc92014-12-15 11:56:11 -08002059 int rc = -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002060
Paul Lawrence87999172014-02-20 12:21:31 -08002061 /* The size of the userdata partition, and add in the vold volumes below */
2062 tot_encryption_size = crypt_ftr->fs_size;
2063
2064 if (how == CRYPTO_ENABLE_WIPE) {
JP Abgrall62c7af32014-06-16 13:01:23 -07002065 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
Luis Hector Chavezbbb512d2018-05-30 15:47:50 -07002066 if (!rec) {
2067 SLOGE("cryptfs_enable: missing %s entry in fstab\n", DATA_MNT_POINT);
2068 return -1;
2069 }
JP Abgrall62c7af32014-06-16 13:01:23 -07002070 int fs_type = get_fs_type(rec);
2071 if (fs_type < 0) {
2072 SLOGE("cryptfs_enable: unsupported fs type %s\n", rec->fs_type);
2073 return -1;
2074 }
2075 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr->fs_size, fs_type);
Paul Lawrence87999172014-02-20 12:21:31 -08002076 } else if (how == CRYPTO_ENABLE_INPLACE) {
2077 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev,
2078 crypt_ftr->fs_size, &cur_encryption_done,
2079 tot_encryption_size,
2080 previously_encrypted_upto);
2081
JP Abgrall7fc1de82014-10-10 18:43:41 -07002082 if (rc == ENABLE_INPLACE_ERR_DEV) {
2083 /* Hack for b/17898962 */
2084 SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n");
Josh Gaofec44372017-08-28 13:22:55 -07002085 cryptfs_reboot(RebootType::reboot);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002086 }
2087
Paul Lawrence73d7a022014-06-09 14:10:09 -07002088 if (!rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002089 crypt_ftr->encrypted_upto = cur_encryption_done;
2090 }
2091
Paul Lawrence73d7a022014-06-09 14:10:09 -07002092 if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002093 /* The inplace routine never actually sets the progress to 100% due
2094 * to the round down nature of integer division, so set it here */
2095 property_set("vold.encrypt_progress", "100");
2096 }
2097 } else {
2098 /* Shouldn't happen */
2099 SLOGE("cryptfs_enable: internal error, unknown option\n");
2100 rc = -1;
2101 }
2102
2103 return rc;
2104}
2105
Wei Wang4375f1b2017-02-24 17:43:01 -08002106int cryptfs_enable_internal(char *howarg, int crypt_type, const char *passwd,
Paul Lawrence569649f2015-09-09 12:13:00 -07002107 int no_ui)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002108{
2109 int how = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002110 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN];
Ken Sumrall160b4d62013-04-22 12:15:39 -07002111 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002112 int rc=-1, i;
Paul Lawrence87999172014-02-20 12:21:31 -08002113 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002114 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002115 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002116 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07002117 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall29d8da82011-05-18 17:20:07 -07002118 int num_vols;
Paul Lawrence87999172014-02-20 12:21:31 -08002119 off64_t previously_encrypted_upto = 0;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002120 bool rebootEncryption = false;
Wei Wang4375f1b2017-02-24 17:43:01 -08002121 bool onlyCreateHeader = false;
2122 int fd = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002123
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002124 if (!strcmp(howarg, "wipe")) {
2125 how = CRYPTO_ENABLE_WIPE;
2126 } else if (! strcmp(howarg, "inplace")) {
2127 how = CRYPTO_ENABLE_INPLACE;
2128 } else {
2129 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08002130 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002131 }
2132
Paul Lawrence87999172014-02-20 12:21:31 -08002133 if (how == CRYPTO_ENABLE_INPLACE
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002134 && get_crypt_ftr_and_key(&crypt_ftr) == 0) {
2135 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
2136 /* An encryption was underway and was interrupted */
2137 previously_encrypted_upto = crypt_ftr.encrypted_upto;
2138 crypt_ftr.encrypted_upto = 0;
2139 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002140
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002141 /* At this point, we are in an inconsistent state. Until we successfully
2142 complete encryption, a reboot will leave us broken. So mark the
2143 encryption failed in case that happens.
2144 On successfully completing encryption, remove this flag */
2145 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002146
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002147 put_crypt_ftr_and_key(&crypt_ftr);
2148 } else if (crypt_ftr.flags & CRYPT_FORCE_ENCRYPTION) {
2149 if (!check_ftr_sha(&crypt_ftr)) {
2150 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
2151 put_crypt_ftr_and_key(&crypt_ftr);
2152 goto error_unencrypted;
2153 }
2154
2155 /* Doing a reboot-encryption*/
2156 crypt_ftr.flags &= ~CRYPT_FORCE_ENCRYPTION;
2157 crypt_ftr.flags |= CRYPT_FORCE_COMPLETE;
2158 rebootEncryption = true;
2159 }
Paul Lawrence87999172014-02-20 12:21:31 -08002160 }
2161
2162 property_get("ro.crypto.state", encrypted_state, "");
2163 if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) {
2164 SLOGE("Device is already running encrypted, aborting");
2165 goto error_unencrypted;
2166 }
2167
2168 // TODO refactor fs_mgr_get_crypt_info to get both in one call
2169 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumrall56ad03c2013-02-13 13:00:19 -08002170 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002171
Ken Sumrall3ed82362011-01-28 23:31:16 -08002172 /* Get the size of the real block device */
Wei Wang4375f1b2017-02-24 17:43:01 -08002173 fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002174 if (fd == -1) {
2175 SLOGE("Cannot open block device %s\n", real_blkdev);
2176 goto error_unencrypted;
2177 }
2178 unsigned long nr_sec;
2179 get_blkdev_size(fd, &nr_sec);
2180 if (nr_sec == 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002181 SLOGE("Cannot get size of block device %s\n", real_blkdev);
2182 goto error_unencrypted;
2183 }
2184 close(fd);
2185
2186 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002187 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002188 unsigned int fs_size_sec, max_fs_size_sec;
Jim Millera70abc62014-08-15 02:00:45 +00002189 fs_size_sec = get_fs_size(real_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002190 if (fs_size_sec == 0)
2191 fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev);
2192
Paul Lawrence87999172014-02-20 12:21:31 -08002193 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002194
2195 if (fs_size_sec > max_fs_size_sec) {
2196 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
2197 goto error_unencrypted;
2198 }
2199 }
2200
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002201 /* Get a wakelock as this may take a while, and we don't want the
2202 * device to sleep on us. We'll grab a partial wakelock, and if the UI
2203 * wants to keep the screen on, it can grab a full wakelock.
2204 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002205 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002206 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
2207
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002208 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002209 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002210 */
2211 property_set("vold.decrypt", "trigger_shutdown_framework");
2212 SLOGD("Just asked init to shut down class main\n");
2213
Jeff Sharkey9c484982015-03-31 10:35:33 -07002214 /* Ask vold to unmount all devices that it manages */
2215 if (vold_unmountAll()) {
2216 SLOGE("Failed to unmount all vold managed devices");
Ken Sumrall2eaf7132011-01-14 12:45:48 -08002217 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002218
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002219 /* no_ui means we are being called from init, not settings.
2220 Now we always reboot from settings, so !no_ui means reboot
2221 */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002222 if (!no_ui) {
2223 /* Try fallback, which is to reboot and try there */
2224 onlyCreateHeader = true;
2225 FILE* breadcrumb = fopen(BREADCRUMB_FILE, "we");
2226 if (breadcrumb == 0) {
2227 SLOGE("Failed to create breadcrumb file");
2228 goto error_shutting_down;
2229 }
2230 fclose(breadcrumb);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002231 }
2232
2233 /* Do extra work for a better UX when doing the long inplace encryption */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002234 if (how == CRYPTO_ENABLE_INPLACE && !onlyCreateHeader) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002235 /* Now that /data is unmounted, we need to mount a tmpfs
2236 * /data, set a property saying we're doing inplace encryption,
2237 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002238 */
Ken Sumralle5032c42012-04-01 23:58:44 -07002239 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002240 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002241 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002242 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08002243 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002244
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002245 /* restart the framework. */
2246 /* Create necessary paths on /data */
Wei Wang42e38102017-06-07 10:46:12 -07002247 prep_data_fs();
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002248
Ken Sumrall92736ef2012-10-17 20:57:14 -07002249 /* Ugh, shutting down the framework is not synchronous, so until it
2250 * can be fixed, this horrible hack will wait a moment for it all to
2251 * shut down before proceeding. Without it, some devices cannot
2252 * restart the graphics services.
2253 */
2254 sleep(2);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002255 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002256
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002257 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002258 /* Initialize a crypt_mnt_ftr for the partition */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002259 if (previously_encrypted_upto == 0 && !rebootEncryption) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002260 if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) {
2261 goto error_shutting_down;
2262 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002263
Paul Lawrence87999172014-02-20 12:21:31 -08002264 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
2265 crypt_ftr.fs_size = nr_sec
2266 - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
2267 } else {
2268 crypt_ftr.fs_size = nr_sec;
2269 }
Paul Lawrence6bfed202014-07-28 12:47:22 -07002270 /* At this point, we are in an inconsistent state. Until we successfully
2271 complete encryption, a reboot will leave us broken. So mark the
2272 encryption failed in case that happens.
2273 On successfully completing encryption, remove this flag */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002274 if (onlyCreateHeader) {
2275 crypt_ftr.flags |= CRYPT_FORCE_ENCRYPTION;
2276 } else {
2277 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
2278 }
Paul Lawrence87999172014-02-20 12:21:31 -08002279 crypt_ftr.crypt_type = crypt_type;
Ajay Dudani87701e22014-09-17 21:02:52 -07002280 strlcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256", MAX_CRYPTO_TYPE_NAME_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002281
Paul Lawrence87999172014-02-20 12:21:31 -08002282 /* Make an encrypted master key */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002283 if (create_encrypted_random_key(onlyCreateHeader ? DEFAULT_PASSWORD : passwd,
2284 crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
Paul Lawrence87999172014-02-20 12:21:31 -08002285 SLOGE("Cannot create encrypted master key\n");
2286 goto error_shutting_down;
2287 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002288
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002289 /* Replace scrypted intermediate key if we are preparing for a reboot */
2290 if (onlyCreateHeader) {
2291 unsigned char fake_master_key[KEY_LEN_BYTES];
2292 unsigned char encrypted_fake_master_key[KEY_LEN_BYTES];
2293 memset(fake_master_key, 0, sizeof(fake_master_key));
2294 encrypt_master_key(passwd, crypt_ftr.salt, fake_master_key,
2295 encrypted_fake_master_key, &crypt_ftr);
2296 }
2297
Paul Lawrence87999172014-02-20 12:21:31 -08002298 /* Write the key to the end of the partition */
2299 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002300
Paul Lawrence87999172014-02-20 12:21:31 -08002301 /* If any persistent data has been remembered, save it.
2302 * If none, create a valid empty table and save that.
2303 */
2304 if (!persist_data) {
Wei Wang4375f1b2017-02-24 17:43:01 -08002305 pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
Paul Lawrence87999172014-02-20 12:21:31 -08002306 if (pdata) {
2307 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
2308 persist_data = pdata;
2309 }
2310 }
2311 if (persist_data) {
2312 save_persistent_data();
2313 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002314 }
2315
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002316 if (onlyCreateHeader) {
2317 sleep(2);
Josh Gaofec44372017-08-28 13:22:55 -07002318 cryptfs_reboot(RebootType::reboot);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002319 }
2320
2321 if (how == CRYPTO_ENABLE_INPLACE && (!no_ui || rebootEncryption)) {
Ajay Dudani87701e22014-09-17 21:02:52 -07002322 /* startup service classes main and late_start */
2323 property_set("vold.decrypt", "trigger_restart_min_framework");
2324 SLOGD("Just triggered restart_min_framework\n");
2325
2326 /* OK, the framework is restarted and will soon be showing a
2327 * progress bar. Time to setup an encrypted mapping, and
2328 * either write a new filesystem, or encrypt in place updating
2329 * the progress bar as we work.
2330 */
2331 }
2332
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002333 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002334 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002335 CRYPTO_BLOCK_DEVICE);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002336
Paul Lawrence87999172014-02-20 12:21:31 -08002337 /* If we are continuing, check checksums match */
2338 rc = 0;
2339 if (previously_encrypted_upto) {
2340 __le8 hash_first_block[SHA256_DIGEST_LENGTH];
2341 rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block);
Ken Sumrall128626f2011-06-28 18:45:14 -07002342
Paul Lawrence87999172014-02-20 12:21:31 -08002343 if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block,
2344 sizeof(hash_first_block)) != 0) {
2345 SLOGE("Checksums do not match - trigger wipe");
2346 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002347 }
2348 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002349
Paul Lawrence87999172014-02-20 12:21:31 -08002350 if (!rc) {
2351 rc = cryptfs_enable_all_volumes(&crypt_ftr, how,
2352 crypto_blkdev, real_blkdev,
2353 previously_encrypted_upto);
2354 }
2355
2356 /* Calculate checksum if we are not finished */
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08002357 if (!rc && how == CRYPTO_ENABLE_INPLACE
2358 && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002359 rc = cryptfs_SHA256_fileblock(crypto_blkdev,
2360 crypt_ftr.hash_first_block);
Paul Lawrence73d7a022014-06-09 14:10:09 -07002361 if (rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002362 SLOGE("Error calculating checksum for continuing encryption");
2363 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002364 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002365 }
2366
2367 /* Undo the dm-crypt mapping whether we succeed or not */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002368 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002369
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002370 if (! rc) {
2371 /* Success */
Paul Lawrence6bfed202014-07-28 12:47:22 -07002372 crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08002373
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08002374 if (how == CRYPTO_ENABLE_INPLACE
2375 && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002376 SLOGD("Encrypted up to sector %lld - will continue after reboot",
2377 crypt_ftr.encrypted_upto);
Paul Lawrence6bfed202014-07-28 12:47:22 -07002378 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence87999172014-02-20 12:21:31 -08002379 }
Paul Lawrence73d7a022014-06-09 14:10:09 -07002380
Paul Lawrence6bfed202014-07-28 12:47:22 -07002381 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08002382
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08002383 if (how == CRYPTO_ENABLE_WIPE
2384 || crypt_ftr.encrypted_upto == crypt_ftr.fs_size) {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002385 char value[PROPERTY_VALUE_MAX];
2386 property_get("ro.crypto.state", value, "");
2387 if (!strcmp(value, "")) {
2388 /* default encryption - continue first boot sequence */
2389 property_set("ro.crypto.state", "encrypted");
Paul Lawrence4ed45262016-03-10 15:44:21 -08002390 property_set("ro.crypto.type", "block");
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002391 release_wake_lock(lockid);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002392 if (rebootEncryption && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
2393 // Bring up cryptkeeper that will check the password and set it
2394 property_set("vold.decrypt", "trigger_shutdown_framework");
2395 sleep(2);
2396 property_set("vold.encrypt_progress", "");
2397 cryptfs_trigger_restart_min_framework();
2398 } else {
2399 cryptfs_check_passwd(DEFAULT_PASSWORD);
2400 cryptfs_restart_internal(1);
2401 }
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002402 return 0;
2403 } else {
2404 sleep(2); /* Give the UI a chance to show 100% progress */
Josh Gaofec44372017-08-28 13:22:55 -07002405 cryptfs_reboot(RebootType::reboot);
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002406 }
Paul Lawrence87999172014-02-20 12:21:31 -08002407 } else {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002408 sleep(2); /* Partially encrypted, ensure writes flushed to ssd */
Josh Gaofec44372017-08-28 13:22:55 -07002409 cryptfs_reboot(RebootType::shutdown);
Paul Lawrence87999172014-02-20 12:21:31 -08002410 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002411 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002412 char value[PROPERTY_VALUE_MAX];
2413
Ken Sumrall319369a2012-06-27 16:30:18 -07002414 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002415 if (!strcmp(value, "1")) {
2416 /* wipe data if encryption failed */
2417 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
Wei Wang4375f1b2017-02-24 17:43:01 -08002418 std::string err;
2419 const std::vector<std::string> options = {
2420 "--wipe_data\n--reason=cryptfs_enable_internal\n"
2421 };
2422 if (!write_bootloader_message(options, &err)) {
2423 SLOGE("could not write bootloader message: %s", err.c_str());
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002424 }
Josh Gaofec44372017-08-28 13:22:55 -07002425 cryptfs_reboot(RebootType::recovery);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002426 } else {
2427 /* set property to trigger dialog */
2428 property_set("vold.encrypt_progress", "error_partially_encrypted");
2429 release_wake_lock(lockid);
2430 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002431 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002432 }
2433
Ken Sumrall3ed82362011-01-28 23:31:16 -08002434 /* hrm, the encrypt step claims success, but the reboot failed.
2435 * This should not happen.
2436 * Set the property and return. Hope the framework can deal with it.
2437 */
2438 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002439 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002440 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08002441
2442error_unencrypted:
2443 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002444 if (lockid[0]) {
2445 release_wake_lock(lockid);
2446 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002447 return -1;
2448
2449error_shutting_down:
2450 /* we failed, and have not encrypted anthing, so the users's data is still intact,
2451 * but the framework is stopped and not restarted to show the error, so it's up to
2452 * vold to restart the system.
2453 */
2454 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Josh Gaofec44372017-08-28 13:22:55 -07002455 cryptfs_reboot(RebootType::reboot);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002456
2457 /* shouldn't get here */
2458 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002459 if (lockid[0]) {
2460 release_wake_lock(lockid);
2461 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002462 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002463}
2464
Paul Lawrence569649f2015-09-09 12:13:00 -07002465int cryptfs_enable(char *howarg, int type, char *passwd, int no_ui)
Paul Lawrence13486032014-02-03 13:28:11 -08002466{
Paul Lawrence569649f2015-09-09 12:13:00 -07002467 return cryptfs_enable_internal(howarg, type, passwd, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08002468}
2469
Paul Lawrence569649f2015-09-09 12:13:00 -07002470int cryptfs_enable_default(char *howarg, int no_ui)
Paul Lawrence13486032014-02-03 13:28:11 -08002471{
2472 return cryptfs_enable_internal(howarg, CRYPT_TYPE_DEFAULT,
Paul Lawrence569649f2015-09-09 12:13:00 -07002473 DEFAULT_PASSWORD, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08002474}
2475
2476int cryptfs_changepw(int crypt_type, const char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002477{
Paul Crowley38132a12016-02-09 09:50:32 +00002478 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002479 SLOGE("cryptfs_changepw not valid for file encryption");
2480 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08002481 }
2482
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002483 struct crypt_mnt_ftr crypt_ftr;
JP Abgrall933216c2015-02-11 13:44:32 -08002484 int rc;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002485
2486 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08002487 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08002488 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002489 return -1;
2490 }
2491
Paul Lawrencef4faa572014-01-29 13:31:03 -08002492 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2493 SLOGE("Invalid crypt_type %d", crypt_type);
2494 return -1;
2495 }
2496
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002497 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002498 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08002499 SLOGE("Error getting crypt footer and key");
2500 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002501 }
2502
Paul Lawrencef4faa572014-01-29 13:31:03 -08002503 crypt_ftr.crypt_type = crypt_type;
2504
JP Abgrall933216c2015-02-11 13:44:32 -08002505 rc = encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
Paul Lawrencef4faa572014-01-29 13:31:03 -08002506 : newpw,
2507 crypt_ftr.salt,
2508 saved_master_key,
2509 crypt_ftr.master_key,
2510 &crypt_ftr);
JP Abgrall933216c2015-02-11 13:44:32 -08002511 if (rc) {
2512 SLOGE("Encrypt master key failed: %d", rc);
2513 return -1;
2514 }
Jason parks70a4b3f2011-01-28 10:10:47 -06002515 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002516 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002517
2518 return 0;
2519}
Ken Sumrall160b4d62013-04-22 12:15:39 -07002520
Rubin Xu85c01f92014-10-13 12:49:54 +01002521static unsigned int persist_get_max_entries(int encrypted) {
2522 struct crypt_mnt_ftr crypt_ftr;
2523 unsigned int dsize;
2524 unsigned int max_persistent_entries;
2525
2526 /* If encrypted, use the values from the crypt_ftr, otherwise
2527 * use the values for the current spec.
2528 */
2529 if (encrypted) {
2530 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2531 return -1;
2532 }
2533 dsize = crypt_ftr.persist_data_size;
2534 } else {
2535 dsize = CRYPT_PERSIST_DATA_SIZE;
2536 }
2537
2538 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
2539 sizeof(struct crypt_persist_entry);
2540
2541 return max_persistent_entries;
2542}
2543
2544static int persist_get_key(const char *fieldname, char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002545{
2546 unsigned int i;
2547
2548 if (persist_data == NULL) {
2549 return -1;
2550 }
2551 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2552 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2553 /* We found it! */
2554 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
2555 return 0;
2556 }
2557 }
2558
2559 return -1;
2560}
2561
Rubin Xu85c01f92014-10-13 12:49:54 +01002562static int persist_set_key(const char *fieldname, const char *value, int encrypted)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002563{
2564 unsigned int i;
2565 unsigned int num;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002566 unsigned int max_persistent_entries;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002567
2568 if (persist_data == NULL) {
2569 return -1;
2570 }
2571
Rubin Xu85c01f92014-10-13 12:49:54 +01002572 max_persistent_entries = persist_get_max_entries(encrypted);
Ken Sumrall160b4d62013-04-22 12:15:39 -07002573
2574 num = persist_data->persist_valid_entries;
2575
2576 for (i = 0; i < num; i++) {
2577 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2578 /* We found an existing entry, update it! */
2579 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
2580 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
2581 return 0;
2582 }
2583 }
2584
2585 /* We didn't find it, add it to the end, if there is room */
2586 if (persist_data->persist_valid_entries < max_persistent_entries) {
2587 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
2588 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
2589 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
2590 persist_data->persist_valid_entries++;
2591 return 0;
2592 }
2593
2594 return -1;
2595}
2596
Rubin Xu85c01f92014-10-13 12:49:54 +01002597/**
2598 * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the
2599 * sequence and its index is greater than or equal to index. Return 0 otherwise.
2600 */
2601static int match_multi_entry(const char *key, const char *field, unsigned index) {
Rubin Xu85c01f92014-10-13 12:49:54 +01002602 unsigned int field_len;
2603 unsigned int key_index;
2604 field_len = strlen(field);
2605
2606 if (index == 0) {
2607 // The first key in a multi-entry field is just the filedname itself.
2608 if (!strcmp(key, field)) {
2609 return 1;
2610 }
2611 }
2612 // Match key against "%s_%d" % (field, index)
2613 if (strlen(key) < field_len + 1 + 1) {
2614 // Need at least a '_' and a digit.
2615 return 0;
2616 }
2617 if (strncmp(key, field, field_len)) {
2618 // If the key does not begin with field, it's not a match.
2619 return 0;
2620 }
2621 if (1 != sscanf(&key[field_len],"_%d", &key_index)) {
2622 return 0;
2623 }
2624 return key_index >= index;
2625}
2626
2627/*
2628 * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all
2629 * remaining entries starting from index will be deleted.
2630 * returns PERSIST_DEL_KEY_OK if deletion succeeds,
2631 * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist,
2632 * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs.
2633 *
2634 */
2635static int persist_del_keys(const char *fieldname, unsigned index)
2636{
2637 unsigned int i;
2638 unsigned int j;
2639 unsigned int num;
2640
2641 if (persist_data == NULL) {
2642 return PERSIST_DEL_KEY_ERROR_OTHER;
2643 }
2644
2645 num = persist_data->persist_valid_entries;
2646
2647 j = 0; // points to the end of non-deleted entries.
2648 // Filter out to-be-deleted entries in place.
2649 for (i = 0; i < num; i++) {
2650 if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) {
2651 persist_data->persist_entry[j] = persist_data->persist_entry[i];
2652 j++;
2653 }
2654 }
2655
2656 if (j < num) {
2657 persist_data->persist_valid_entries = j;
2658 // Zeroise the remaining entries
2659 memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry));
2660 return PERSIST_DEL_KEY_OK;
2661 } else {
2662 // Did not find an entry matching the given fieldname
2663 return PERSIST_DEL_KEY_ERROR_NO_FIELD;
2664 }
2665}
2666
2667static int persist_count_keys(const char *fieldname)
2668{
2669 unsigned int i;
2670 unsigned int count;
2671
2672 if (persist_data == NULL) {
2673 return -1;
2674 }
2675
2676 count = 0;
2677 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2678 if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) {
2679 count++;
2680 }
2681 }
2682
2683 return count;
2684}
2685
Ken Sumrall160b4d62013-04-22 12:15:39 -07002686/* Return the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01002687int cryptfs_getfield(const char *fieldname, char *value, int len)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002688{
Paul Crowley38132a12016-02-09 09:50:32 +00002689 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08002690 SLOGE("Cannot get field when file encrypted");
2691 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07002692 }
2693
Ken Sumrall160b4d62013-04-22 12:15:39 -07002694 char temp_value[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01002695 /* CRYPTO_GETFIELD_OK is success,
2696 * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set,
2697 * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small,
2698 * CRYPTO_GETFIELD_ERROR_OTHER is any other error
Ken Sumrall160b4d62013-04-22 12:15:39 -07002699 */
Rubin Xu85c01f92014-10-13 12:49:54 +01002700 int rc = CRYPTO_GETFIELD_ERROR_OTHER;
2701 int i;
2702 char temp_field[PROPERTY_KEY_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -07002703
2704 if (persist_data == NULL) {
2705 load_persistent_data();
2706 if (persist_data == NULL) {
2707 SLOGE("Getfield error, cannot load persistent data");
2708 goto out;
2709 }
2710 }
2711
Rubin Xu85c01f92014-10-13 12:49:54 +01002712 // Read value from persistent entries. If the original value is split into multiple entries,
2713 // stitch them back together.
Ken Sumrall160b4d62013-04-22 12:15:39 -07002714 if (!persist_get_key(fieldname, temp_value)) {
Rubin Xu85c01f92014-10-13 12:49:54 +01002715 // We found it, copy it to the caller's buffer and keep going until all entries are read.
2716 if (strlcpy(value, temp_value, len) >= (unsigned) len) {
2717 // value too small
2718 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
2719 goto out;
2720 }
2721 rc = CRYPTO_GETFIELD_OK;
2722
2723 for (i = 1; /* break explicitly */; i++) {
2724 if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >=
2725 (int) sizeof(temp_field)) {
2726 // If the fieldname is very long, we stop as soon as it begins to overflow the
2727 // maximum field length. At this point we have in fact fully read out the original
2728 // value because cryptfs_setfield would not allow fields with longer names to be
2729 // written in the first place.
2730 break;
2731 }
2732 if (!persist_get_key(temp_field, temp_value)) {
2733 if (strlcat(value, temp_value, len) >= (unsigned)len) {
2734 // value too small.
2735 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
2736 goto out;
2737 }
2738 } else {
2739 // Exhaust all entries.
2740 break;
2741 }
2742 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002743 } else {
2744 /* Sadness, it's not there. Return the error */
Rubin Xu85c01f92014-10-13 12:49:54 +01002745 rc = CRYPTO_GETFIELD_ERROR_NO_FIELD;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002746 }
2747
2748out:
2749 return rc;
2750}
2751
2752/* Set the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01002753int cryptfs_setfield(const char *fieldname, const char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002754{
Paul Crowley38132a12016-02-09 09:50:32 +00002755 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08002756 SLOGE("Cannot set field when file encrypted");
2757 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07002758 }
2759
Ken Sumrall160b4d62013-04-22 12:15:39 -07002760 char encrypted_state[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01002761 /* 0 is success, negative values are error */
2762 int rc = CRYPTO_SETFIELD_ERROR_OTHER;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002763 int encrypted = 0;
Rubin Xu85c01f92014-10-13 12:49:54 +01002764 unsigned int field_id;
2765 char temp_field[PROPERTY_KEY_MAX];
2766 unsigned int num_entries;
2767 unsigned int max_keylen;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002768
2769 if (persist_data == NULL) {
2770 load_persistent_data();
2771 if (persist_data == NULL) {
2772 SLOGE("Setfield error, cannot load persistent data");
2773 goto out;
2774 }
2775 }
2776
2777 property_get("ro.crypto.state", encrypted_state, "");
2778 if (!strcmp(encrypted_state, "encrypted") ) {
2779 encrypted = 1;
2780 }
2781
Rubin Xu85c01f92014-10-13 12:49:54 +01002782 // Compute the number of entries required to store value, each entry can store up to
2783 // (PROPERTY_VALUE_MAX - 1) chars
2784 if (strlen(value) == 0) {
2785 // Empty value also needs one entry to store.
2786 num_entries = 1;
2787 } else {
2788 num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1);
2789 }
2790
2791 max_keylen = strlen(fieldname);
2792 if (num_entries > 1) {
2793 // Need an extra "_%d" suffix.
2794 max_keylen += 1 + log10(num_entries);
2795 }
2796 if (max_keylen > PROPERTY_KEY_MAX - 1) {
2797 rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002798 goto out;
2799 }
2800
Rubin Xu85c01f92014-10-13 12:49:54 +01002801 // Make sure we have enough space to write the new value
2802 if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) >
2803 persist_get_max_entries(encrypted)) {
2804 rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG;
2805 goto out;
2806 }
2807
2808 // Now that we know persist_data has enough space for value, let's delete the old field first
2809 // to make up space.
2810 persist_del_keys(fieldname, 0);
2811
2812 if (persist_set_key(fieldname, value, encrypted)) {
2813 // fail to set key, should not happen as we have already checked the available space
2814 SLOGE("persist_set_key() error during setfield()");
2815 goto out;
2816 }
2817
2818 for (field_id = 1; field_id < num_entries; field_id++) {
2819 snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, field_id);
2820
2821 if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), encrypted)) {
2822 // fail to set key, should not happen as we have already checked the available space.
2823 SLOGE("persist_set_key() error during setfield()");
2824 goto out;
2825 }
2826 }
2827
Ken Sumrall160b4d62013-04-22 12:15:39 -07002828 /* If we are running encrypted, save the persistent data now */
2829 if (encrypted) {
2830 if (save_persistent_data()) {
2831 SLOGE("Setfield error, cannot save persistent data");
2832 goto out;
2833 }
2834 }
2835
Rubin Xu85c01f92014-10-13 12:49:54 +01002836 rc = CRYPTO_SETFIELD_OK;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002837
2838out:
2839 return rc;
2840}
Paul Lawrencef4faa572014-01-29 13:31:03 -08002841
2842/* Checks userdata. Attempt to mount the volume if default-
2843 * encrypted.
2844 * On success trigger next init phase and return 0.
2845 * Currently do not handle failure - see TODO below.
2846 */
2847int cryptfs_mount_default_encrypted(void)
2848{
Paul Lawrence84274cc2016-04-15 15:41:33 -07002849 int crypt_type = cryptfs_get_password_type();
2850 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2851 SLOGE("Bad crypt type - error");
2852 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
2853 SLOGD("Password is not default - "
2854 "starting min framework to prompt");
2855 property_set("vold.decrypt", "trigger_restart_min_framework");
2856 return 0;
2857 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
2858 SLOGD("Password is default - restarting filesystem");
2859 cryptfs_restart_internal(0);
2860 return 0;
Paul Lawrencef4faa572014-01-29 13:31:03 -08002861 } else {
Paul Lawrence84274cc2016-04-15 15:41:33 -07002862 SLOGE("Encrypted, default crypt type but can't decrypt");
Paul Lawrencef4faa572014-01-29 13:31:03 -08002863 }
2864
Paul Lawrence6bfed202014-07-28 12:47:22 -07002865 /** Corrupt. Allow us to boot into framework, which will detect bad
2866 crypto when it calls do_crypto_complete, then do a factory reset
Paul Lawrencef4faa572014-01-29 13:31:03 -08002867 */
Paul Lawrence6bfed202014-07-28 12:47:22 -07002868 property_set("vold.decrypt", "trigger_restart_min_framework");
Paul Lawrencef4faa572014-01-29 13:31:03 -08002869 return 0;
2870}
2871
2872/* Returns type of the password, default, pattern, pin or password.
2873 */
2874int cryptfs_get_password_type(void)
2875{
Paul Crowley38132a12016-02-09 09:50:32 +00002876 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002877 SLOGE("cryptfs_get_password_type not valid for file encryption");
2878 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08002879 }
2880
Paul Lawrencef4faa572014-01-29 13:31:03 -08002881 struct crypt_mnt_ftr crypt_ftr;
2882
2883 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2884 SLOGE("Error getting crypt footer and key\n");
2885 return -1;
2886 }
2887
Paul Lawrence6bfed202014-07-28 12:47:22 -07002888 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) {
2889 return -1;
2890 }
2891
Paul Lawrencef4faa572014-01-29 13:31:03 -08002892 return crypt_ftr.crypt_type;
2893}
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002894
Paul Lawrence05335c32015-03-05 09:46:23 -08002895const char* cryptfs_get_password()
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002896{
Paul Crowley38132a12016-02-09 09:50:32 +00002897 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002898 SLOGE("cryptfs_get_password not valid for file encryption");
2899 return 0;
Paul Lawrence05335c32015-03-05 09:46:23 -08002900 }
2901
Paul Lawrence399317e2014-03-10 13:20:50 -07002902 struct timespec now;
Paul Lawrenceef2b5be2014-11-11 12:47:03 -08002903 clock_gettime(CLOCK_BOOTTIME, &now);
Paul Lawrence399317e2014-03-10 13:20:50 -07002904 if (now.tv_sec < password_expiry_time) {
2905 return password;
2906 } else {
2907 cryptfs_clear_password();
2908 return 0;
2909 }
2910}
2911
2912void cryptfs_clear_password()
2913{
2914 if (password) {
2915 size_t len = strlen(password);
2916 memset(password, 0, len);
2917 free(password);
2918 password = 0;
2919 password_expiry_time = 0;
2920 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002921}
Paul Lawrence731a7a22015-04-28 22:14:15 +00002922
Paul Lawrence0c247462015-10-29 10:30:57 -07002923int cryptfs_isConvertibleToFBE()
2924{
2925 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
Luis Hector Chavezbbb512d2018-05-30 15:47:50 -07002926 return (rec && fs_mgr_is_convertible_to_fbe(rec)) ? 1 : 0;
Paul Lawrence0c247462015-10-29 10:30:57 -07002927}
2928
Paul Lawrence731a7a22015-04-28 22:14:15 +00002929int cryptfs_create_default_ftr(struct crypt_mnt_ftr* crypt_ftr, __attribute__((unused))int key_length)
2930{
2931 if (cryptfs_init_crypt_mnt_ftr(crypt_ftr)) {
2932 SLOGE("Failed to initialize crypt_ftr");
2933 return -1;
2934 }
2935
2936 if (create_encrypted_random_key(DEFAULT_PASSWORD, crypt_ftr->master_key,
2937 crypt_ftr->salt, crypt_ftr)) {
2938 SLOGE("Cannot create encrypted master key\n");
2939 return -1;
2940 }
2941
2942 //crypt_ftr->keysize = key_length / 8;
2943 return 0;
2944}
2945
2946int cryptfs_get_master_key(struct crypt_mnt_ftr* ftr, const char* password,
2947 unsigned char* master_key)
2948{
2949 int rc;
2950
Paul Lawrence731a7a22015-04-28 22:14:15 +00002951 unsigned char* intermediate_key = 0;
2952 size_t intermediate_key_size = 0;
Paul Lawrencec78c71b2015-04-14 15:26:29 -07002953
2954 if (password == 0 || *password == 0) {
2955 password = DEFAULT_PASSWORD;
2956 }
2957
Paul Lawrence731a7a22015-04-28 22:14:15 +00002958 rc = decrypt_master_key(password, master_key, ftr, &intermediate_key,
2959 &intermediate_key_size);
2960
Paul Lawrence300dae72016-03-11 11:02:52 -08002961 if (rc) {
2962 SLOGE("Can't calculate intermediate key");
2963 return rc;
2964 }
2965
Paul Lawrencec78c71b2015-04-14 15:26:29 -07002966 int N = 1 << ftr->N_factor;
2967 int r = 1 << ftr->r_factor;
2968 int p = 1 << ftr->p_factor;
2969
2970 unsigned char scrypted_intermediate_key[sizeof(ftr->scrypted_intermediate_key)];
2971
2972 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
2973 ftr->salt, sizeof(ftr->salt), N, r, p,
2974 scrypted_intermediate_key,
2975 sizeof(scrypted_intermediate_key));
2976
2977 free(intermediate_key);
2978
2979 if (rc) {
Paul Lawrence300dae72016-03-11 11:02:52 -08002980 SLOGE("Can't scrypt intermediate key");
Paul Lawrencec78c71b2015-04-14 15:26:29 -07002981 return rc;
2982 }
2983
2984 return memcmp(scrypted_intermediate_key, ftr->scrypted_intermediate_key,
2985 intermediate_key_size);
Paul Lawrence731a7a22015-04-28 22:14:15 +00002986}
2987
2988int cryptfs_set_password(struct crypt_mnt_ftr* ftr, const char* password,
2989 const unsigned char* master_key)
2990{
2991 return encrypt_master_key(password, ftr->salt, master_key, ftr->master_key,
2992 ftr);
2993}
Paul Lawrence6e410592016-05-24 14:20:38 -07002994
Eric Biggersb45caaf2017-02-02 14:52:12 -08002995void cryptfs_get_file_encryption_modes(const char **contents_mode_ret,
2996 const char **filenames_mode_ret)
Paul Lawrence6e410592016-05-24 14:20:38 -07002997{
2998 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
Luis Hector Chavezbbb512d2018-05-30 15:47:50 -07002999 if (!rec) {
3000 *contents_mode_ret = nullptr;
3001 *filenames_mode_ret = nullptr;
3002 return;
3003 }
Eric Biggersb45caaf2017-02-02 14:52:12 -08003004 fs_mgr_get_file_encryption_modes(rec, contents_mode_ret, filenames_mode_ret);
Paul Lawrence6e410592016-05-24 14:20:38 -07003005}