blob: 1e08c8f2abf217ee1b9e7a0cd57bdb180b0e14c0 [file] [log] [blame]
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* TO DO:
18 * 1. Perhaps keep several copies of the encrypted key, in case something
19 * goes horribly wrong?
20 *
21 */
22
23#include <sys/types.h>
Ken Sumralle550f782013-08-20 13:48:23 -070024#include <sys/wait.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080025#include <sys/stat.h>
Paul Lawrencef4faa572014-01-29 13:31:03 -080026#include <ctype.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080027#include <fcntl.h>
Elliott Hughes73737162014-06-25 17:27:42 -070028#include <inttypes.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080029#include <unistd.h>
30#include <stdio.h>
31#include <sys/ioctl.h>
32#include <linux/dm-ioctl.h>
33#include <libgen.h>
34#include <stdlib.h>
35#include <sys/param.h>
36#include <string.h>
37#include <sys/mount.h>
38#include <openssl/evp.h>
Adam Langley41405bb2015-01-22 16:45:28 -080039#include <openssl/sha.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080040#include <errno.h>
Tao Bao5a95ddb2016-10-05 18:01:19 -070041#include <ext4_utils/ext4.h>
42#include <ext4_utils/ext4_utils.h>
Ken Sumrall29d8da82011-05-18 17:20:07 -070043#include <linux/kdev_t.h>
Ken Sumralle5032c42012-04-01 23:58:44 -070044#include <fs_mgr.h>
Paul Lawrence9c58a872014-09-30 09:12:51 -070045#include <time.h>
Rubin Xu85c01f92014-10-13 12:49:54 +010046#include <math.h>
Jeff Vander Stoepdf725752016-01-29 15:34:43 -080047#include <selinux/selinux.h>
Ken Sumrall8f869aa2010-12-03 03:47:09 -080048#include "cryptfs.h"
Jeff Vander Stoepdf725752016-01-29 15:34:43 -080049#include "secontext.h"
Ken Sumrall8f869aa2010-12-03 03:47:09 -080050#define LOG_TAG "Cryptfs"
51#include "cutils/log.h"
52#include "cutils/properties.h"
Ken Sumralladfba362013-06-04 16:37:52 -070053#include "cutils/android_reboot.h"
Ken Sumrall5d4c68e2011-01-30 19:06:03 -080054#include "hardware_legacy/power.h"
Ken Sumralle550f782013-08-20 13:48:23 -070055#include <logwrap/logwrap.h>
Paul Crowley63c18d32016-02-10 14:02:47 +000056#include "ScryptParameters.h"
Ken Sumrall29d8da82011-05-18 17:20:07 -070057#include "VolumeManager.h"
Ken Sumrall9caab762013-06-11 19:10:20 -070058#include "VoldUtil.h"
Paul Lawrence731a7a22015-04-28 22:14:15 +000059#include "Ext4Crypt.h"
Daniel Rosenberge82df162014-08-15 22:19:23 +000060#include "f2fs_sparseblock.h"
Paul Crowleyf71ace32016-06-02 11:01:19 -070061#include "EncryptInplace.h"
jessica_yu3f14fe42014-09-22 15:57:40 +080062#include "Process.h"
Janis Danisevskis015ec302017-01-31 11:31:08 +000063#include "Keymaster.h"
Wei Wang4375f1b2017-02-24 17:43:01 -080064#include "android-base/properties.h"
Yabin Cui1fb59662016-06-24 14:48:49 -070065#include <bootloader_message/bootloader_message.h>
Wei Wang4375f1b2017-02-24 17:43:01 -080066extern "C" {
67#include <crypto_scrypt.h>
68}
Mark Salyzyn3e971272014-01-21 13:27:04 -080069
Mark Salyzyn5eecc442014-02-12 14:16:14 -080070#define UNUSED __attribute__((unused))
71
Ken Sumrall8f869aa2010-12-03 03:47:09 -080072#define DM_CRYPT_BUF_SIZE 4096
73
Jason parks70a4b3f2011-01-28 10:10:47 -060074#define HASH_COUNT 2000
75#define KEY_LEN_BYTES 16
76#define IV_LEN_BYTES 16
77
Ken Sumrall29d8da82011-05-18 17:20:07 -070078#define KEY_IN_FOOTER "footer"
79
Paul Lawrence3bd36d52015-06-09 13:37:44 -070080#define DEFAULT_PASSWORD "default_password"
Paul Lawrencef4faa572014-01-29 13:31:03 -080081
Paul Lawrence3d99eba2015-11-20 07:07:19 -080082#define CRYPTO_BLOCK_DEVICE "userdata"
83
84#define BREADCRUMB_FILE "/data/misc/vold/convert_fde"
85
Ken Sumrall29d8da82011-05-18 17:20:07 -070086#define EXT4_FS 1
JP Abgrall62c7af32014-06-16 13:01:23 -070087#define F2FS_FS 2
Ken Sumrall29d8da82011-05-18 17:20:07 -070088
Ken Sumralle919efe2012-09-29 17:07:41 -070089#define TABLE_LOAD_RETRIES 10
90
Shawn Willden47ba10d2014-09-03 17:07:06 -060091#define RSA_KEY_SIZE 2048
92#define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8)
93#define RSA_EXPONENT 0x10001
Shawn Willdenda6e8992015-06-03 09:40:45 -060094#define KEYMASTER_CRYPTFS_RATE_LIMIT 1 // Maximum one try per second
Paul Lawrence69f4ebd2014-04-14 12:17:14 -070095
Paul Lawrence8e3f4512014-09-08 10:11:17 -070096#define RETRY_MOUNT_ATTEMPTS 10
97#define RETRY_MOUNT_DELAY_SECONDS 1
98
Paul Crowley73473332017-11-21 15:43:51 -080099static int put_crypt_ftr_and_key(struct crypt_mnt_ftr* crypt_ftr);
100
Jason parks70a4b3f2011-01-28 10:10:47 -0600101static unsigned char saved_master_key[KEY_LEN_BYTES];
Ken Sumrall3ad90722011-10-04 20:38:29 -0700102static char *saved_mount_point;
Jason parks70a4b3f2011-01-28 10:10:47 -0600103static int master_key_saved = 0;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700104static struct crypt_persist_data *persist_data = NULL;
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800105
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700106/* Should we use keymaster? */
107static int keymaster_check_compatibility()
108{
Janis Danisevskis015ec302017-01-31 11:31:08 +0000109 return keymaster_compatibility_cryptfs_scrypt();
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700110}
111
112/* Create a new keymaster key and store it in this footer */
113static int keymaster_create_key(struct crypt_mnt_ftr *ftr)
114{
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800115 if (ftr->keymaster_blob_size) {
116 SLOGI("Already have key");
117 return 0;
118 }
119
Janis Danisevskis015ec302017-01-31 11:31:08 +0000120 int rc = keymaster_create_key_for_cryptfs_scrypt(RSA_KEY_SIZE, RSA_EXPONENT,
121 KEYMASTER_CRYPTFS_RATE_LIMIT, ftr->keymaster_blob, KEYMASTER_BLOB_SIZE,
122 &ftr->keymaster_blob_size);
123 if (rc) {
124 if (ftr->keymaster_blob_size > KEYMASTER_BLOB_SIZE) {
Paul Crowley73473332017-11-21 15:43:51 -0800125 SLOGE("Keymaster key blob too large");
Janis Danisevskis015ec302017-01-31 11:31:08 +0000126 ftr->keymaster_blob_size = 0;
127 }
128 SLOGE("Failed to generate keypair");
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700129 return -1;
130 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000131 return 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700132}
133
Shawn Willdene17a9c42014-09-08 13:04:08 -0600134/* This signs the given object using the keymaster key. */
135static int keymaster_sign_object(struct crypt_mnt_ftr *ftr,
Shawn Willden47ba10d2014-09-03 17:07:06 -0600136 const unsigned char *object,
137 const size_t object_size,
138 unsigned char **signature,
139 size_t *signature_size)
140{
Shawn Willden47ba10d2014-09-03 17:07:06 -0600141 unsigned char to_sign[RSA_KEY_SIZE_BYTES];
Shawn Willdene17a9c42014-09-08 13:04:08 -0600142 size_t to_sign_size = sizeof(to_sign);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600143 memset(to_sign, 0, RSA_KEY_SIZE_BYTES);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600144
Shawn Willdene17a9c42014-09-08 13:04:08 -0600145 // To sign a message with RSA, the message must satisfy two
146 // constraints:
147 //
148 // 1. The message, when interpreted as a big-endian numeric value, must
149 // be strictly less than the public modulus of the RSA key. Note
150 // that because the most significant bit of the public modulus is
151 // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit
152 // key), an n-bit message with most significant bit 0 always
153 // satisfies this requirement.
154 //
155 // 2. The message must have the same length in bits as the public
156 // modulus of the RSA key. This requirement isn't mathematically
157 // necessary, but is necessary to ensure consistency in
158 // implementations.
159 switch (ftr->kdf_type) {
Shawn Willdene17a9c42014-09-08 13:04:08 -0600160 case KDF_SCRYPT_KEYMASTER:
161 // This ensures the most significant byte of the signed message
162 // is zero. We could have zero-padded to the left instead, but
163 // this approach is slightly more robust against changes in
164 // object size. However, it's still broken (but not unusably
Shawn Willdenda6e8992015-06-03 09:40:45 -0600165 // so) because we really should be using a proper deterministic
166 // RSA padding function, such as PKCS1.
Wei Wang4375f1b2017-02-24 17:43:01 -0800167 memcpy(to_sign + 1, object, std::min((size_t)RSA_KEY_SIZE_BYTES - 1, object_size));
Shawn Willdene17a9c42014-09-08 13:04:08 -0600168 SLOGI("Signing safely-padded object");
169 break;
170 default:
171 SLOGE("Unknown KDF type %d", ftr->kdf_type);
Janis Danisevskis015ec302017-01-31 11:31:08 +0000172 return -1;
Shawn Willdene17a9c42014-09-08 13:04:08 -0600173 }
Paul Crowley73473332017-11-21 15:43:51 -0800174 for (;;) {
175 auto result = keymaster_sign_object_for_cryptfs_scrypt(
176 ftr->keymaster_blob, ftr->keymaster_blob_size, KEYMASTER_CRYPTFS_RATE_LIMIT, to_sign,
177 to_sign_size, signature, signature_size);
178 switch (result) {
179 case KeymasterSignResult::ok:
180 return 0;
181 case KeymasterSignResult::upgrade:
182 break;
183 default:
184 return -1;
185 }
186 SLOGD("Upgrading key");
187 if (keymaster_upgrade_key_for_cryptfs_scrypt(
188 RSA_KEY_SIZE, RSA_EXPONENT, KEYMASTER_CRYPTFS_RATE_LIMIT, ftr->keymaster_blob,
189 ftr->keymaster_blob_size, ftr->keymaster_blob, KEYMASTER_BLOB_SIZE,
190 &ftr->keymaster_blob_size) != 0) {
191 SLOGE("Failed to upgrade key");
192 return -1;
193 }
194 if (put_crypt_ftr_and_key(ftr) != 0) {
195 SLOGE("Failed to write upgraded key to disk");
196 }
197 SLOGD("Key upgraded successfully");
198 }
Shawn Willden47ba10d2014-09-03 17:07:06 -0600199}
200
Paul Lawrence399317e2014-03-10 13:20:50 -0700201/* Store password when userdata is successfully decrypted and mounted.
202 * Cleared by cryptfs_clear_password
203 *
204 * To avoid a double prompt at boot, we need to store the CryptKeeper
205 * password and pass it to KeyGuard, which uses it to unlock KeyStore.
206 * Since the entire framework is torn down and rebuilt after encryption,
207 * we have to use a daemon or similar to store the password. Since vold
208 * is secured against IPC except from system processes, it seems a reasonable
209 * place to store this.
210 *
211 * password should be cleared once it has been used.
212 *
213 * password is aged out after password_max_age_seconds seconds.
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800214 */
Paul Lawrence399317e2014-03-10 13:20:50 -0700215static char* password = 0;
216static int password_expiry_time = 0;
217static const int password_max_age_seconds = 60;
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800218
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800219extern struct fstab *fstab;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800220
Josh Gaofec44372017-08-28 13:22:55 -0700221enum class RebootType {reboot, recovery, shutdown};
222static void cryptfs_reboot(RebootType rt)
Ken Sumralladfba362013-06-04 16:37:52 -0700223{
Josh Gaofec44372017-08-28 13:22:55 -0700224 switch (rt) {
225 case RebootType::reboot:
Paul Lawrence87999172014-02-20 12:21:31 -0800226 property_set(ANDROID_RB_PROPERTY, "reboot");
227 break;
228
Josh Gaofec44372017-08-28 13:22:55 -0700229 case RebootType::recovery:
Paul Lawrence87999172014-02-20 12:21:31 -0800230 property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
231 break;
232
Josh Gaofec44372017-08-28 13:22:55 -0700233 case RebootType::shutdown:
Paul Lawrence87999172014-02-20 12:21:31 -0800234 property_set(ANDROID_RB_PROPERTY, "shutdown");
235 break;
Ken Sumralladfba362013-06-04 16:37:52 -0700236 }
Paul Lawrence87999172014-02-20 12:21:31 -0800237
Ken Sumralladfba362013-06-04 16:37:52 -0700238 sleep(20);
239
240 /* Shouldn't get here, reboot should happen before sleep times out */
241 return;
242}
243
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800244static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
245{
246 memset(io, 0, dataSize);
247 io->data_size = dataSize;
248 io->data_start = sizeof(struct dm_ioctl);
249 io->version[0] = 4;
250 io->version[1] = 0;
251 io->version[2] = 0;
252 io->flags = flags;
253 if (name) {
Marek Pola5e6b9142015-02-05 14:22:34 +0100254 strlcpy(io->name, name, sizeof(io->name));
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800255 }
256}
257
Kenny Rootc4c70f12013-06-14 12:11:38 -0700258/**
259 * Gets the default device scrypt parameters for key derivation time tuning.
260 * The parameters should lead to about one second derivation time for the
261 * given device.
262 */
263static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700264 char paramstr[PROPERTY_VALUE_MAX];
Paul Crowley63c18d32016-02-10 14:02:47 +0000265 int Nf, rf, pf;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700266
Paul Crowley63c18d32016-02-10 14:02:47 +0000267 property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS);
268 if (!parse_scrypt_parameters(paramstr, &Nf, &rf, &pf)) {
269 SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
270 parse_scrypt_parameters(SCRYPT_DEFAULTS, &Nf, &rf, &pf);
Kenny Rootc4c70f12013-06-14 12:11:38 -0700271 }
Paul Crowley63c18d32016-02-10 14:02:47 +0000272 ftr->N_factor = Nf;
273 ftr->r_factor = rf;
274 ftr->p_factor = pf;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700275}
276
Ken Sumrall3ed82362011-01-28 23:31:16 -0800277static unsigned int get_fs_size(char *dev)
278{
279 int fd, block_size;
280 struct ext4_super_block sb;
281 off64_t len;
282
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700283 if ((fd = open(dev, O_RDONLY|O_CLOEXEC)) < 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -0800284 SLOGE("Cannot open device to get filesystem size ");
285 return 0;
286 }
287
288 if (lseek64(fd, 1024, SEEK_SET) < 0) {
289 SLOGE("Cannot seek to superblock");
290 return 0;
291 }
292
293 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
294 SLOGE("Cannot read superblock");
295 return 0;
296 }
297
298 close(fd);
299
Daniel Rosenberge82df162014-08-15 22:19:23 +0000300 if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) {
301 SLOGE("Not a valid ext4 superblock");
302 return 0;
303 }
Ken Sumrall3ed82362011-01-28 23:31:16 -0800304 block_size = 1024 << sb.s_log_block_size;
305 /* compute length in bytes */
306 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
307
308 /* return length in sectors */
309 return (unsigned int) (len / 512);
310}
311
Ken Sumrall160b4d62013-04-22 12:15:39 -0700312static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
313{
314 static int cached_data = 0;
315 static off64_t cached_off = 0;
316 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
317 int fd;
318 char key_loc[PROPERTY_VALUE_MAX];
319 char real_blkdev[PROPERTY_VALUE_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -0700320 int rc = -1;
321
322 if (!cached_data) {
323 fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
324
325 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700326 if ( (fd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700327 SLOGE("Cannot open real block device %s\n", real_blkdev);
328 return -1;
329 }
330
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900331 unsigned long nr_sec = 0;
332 get_blkdev_size(fd, &nr_sec);
333 if (nr_sec != 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700334 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
335 * encryption info footer and key, and plenty of bytes to spare for future
336 * growth.
337 */
338 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
339 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
340 cached_data = 1;
341 } else {
342 SLOGE("Cannot get size of block device %s\n", real_blkdev);
343 }
344 close(fd);
345 } else {
346 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
347 cached_off = 0;
348 cached_data = 1;
349 }
350 }
351
352 if (cached_data) {
353 if (metadata_fname) {
354 *metadata_fname = cached_metadata_fname;
355 }
356 if (off) {
357 *off = cached_off;
358 }
359 rc = 0;
360 }
361
362 return rc;
363}
364
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800365/* Set sha256 checksum in structure */
366static void set_ftr_sha(struct crypt_mnt_ftr *crypt_ftr)
367{
368 SHA256_CTX c;
369 SHA256_Init(&c);
370 memset(crypt_ftr->sha256, 0, sizeof(crypt_ftr->sha256));
371 SHA256_Update(&c, crypt_ftr, sizeof(*crypt_ftr));
372 SHA256_Final(crypt_ftr->sha256, &c);
373}
374
Ken Sumralle8744072011-01-18 22:01:55 -0800375/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800376 * update the failed mount count but not change the key.
377 */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700378static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800379{
380 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800381 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700382 /* starting_off is set to the SEEK_SET offset
383 * where the crypto structure starts
384 */
385 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800386 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700387 char *fname = NULL;
Ken Sumrall3be890f2011-09-14 16:53:46 -0700388 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800389
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800390 set_ftr_sha(crypt_ftr);
391
Ken Sumrall160b4d62013-04-22 12:15:39 -0700392 if (get_crypt_ftr_info(&fname, &starting_off)) {
393 SLOGE("Unable to get crypt_ftr_info\n");
394 return -1;
395 }
396 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700397 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700398 return -1;
399 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700400 if ( (fd = open(fname, O_RDWR | O_CREAT|O_CLOEXEC, 0600)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700401 SLOGE("Cannot open footer file %s for put\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700402 return -1;
403 }
404
405 /* Seek to the start of the crypt footer */
406 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
407 SLOGE("Cannot seek to real block device footer\n");
408 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800409 }
410
411 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
412 SLOGE("Cannot write real block device footer\n");
413 goto errout;
414 }
415
Ken Sumrall3be890f2011-09-14 16:53:46 -0700416 fstat(fd, &statbuf);
417 /* If the keys are kept on a raw block device, do not try to truncate it. */
Ken Sumralle550f782013-08-20 13:48:23 -0700418 if (S_ISREG(statbuf.st_mode)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700419 if (ftruncate(fd, 0x4000)) {
Colin Cross59846b62014-02-06 20:34:29 -0800420 SLOGE("Cannot set footer file size\n");
Ken Sumralle8744072011-01-18 22:01:55 -0800421 goto errout;
422 }
423 }
424
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800425 /* Success! */
426 rc = 0;
427
428errout:
429 close(fd);
430 return rc;
431
432}
433
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800434static bool check_ftr_sha(const struct crypt_mnt_ftr *crypt_ftr)
435{
436 struct crypt_mnt_ftr copy;
437 memcpy(&copy, crypt_ftr, sizeof(copy));
438 set_ftr_sha(&copy);
439 return memcmp(copy.sha256, crypt_ftr->sha256, sizeof(copy.sha256)) == 0;
440}
441
Ken Sumrall160b4d62013-04-22 12:15:39 -0700442static inline int unix_read(int fd, void* buff, int len)
443{
444 return TEMP_FAILURE_RETRY(read(fd, buff, len));
445}
446
447static inline int unix_write(int fd, const void* buff, int len)
448{
449 return TEMP_FAILURE_RETRY(write(fd, buff, len));
450}
451
452static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
453{
454 memset(pdata, 0, len);
455 pdata->persist_magic = PERSIST_DATA_MAGIC;
456 pdata->persist_valid_entries = 0;
457}
458
459/* A routine to update the passed in crypt_ftr to the lastest version.
460 * fd is open read/write on the device that holds the crypto footer and persistent
461 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
462 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
463 */
464static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
465{
Kenny Root7434b312013-06-14 11:29:53 -0700466 int orig_major = crypt_ftr->major_version;
467 int orig_minor = crypt_ftr->minor_version;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700468
Kenny Root7434b312013-06-14 11:29:53 -0700469 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
470 struct crypt_persist_data *pdata;
471 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700472
Kenny Rootc4c70f12013-06-14 12:11:38 -0700473 SLOGW("upgrading crypto footer to 1.1");
474
Wei Wang4375f1b2017-02-24 17:43:01 -0800475 pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
Kenny Root7434b312013-06-14 11:29:53 -0700476 if (pdata == NULL) {
477 SLOGE("Cannot allocate persisent data\n");
478 return;
479 }
480 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
481
482 /* Need to initialize the persistent data area */
483 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
484 SLOGE("Cannot seek to persisent data offset\n");
Henrik Baard91064632015-02-05 15:09:17 +0100485 free(pdata);
Kenny Root7434b312013-06-14 11:29:53 -0700486 return;
487 }
488 /* Write all zeros to the first copy, making it invalid */
489 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
490
491 /* Write a valid but empty structure to the second copy */
492 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
493 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
494
495 /* Update the footer */
496 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
497 crypt_ftr->persist_data_offset[0] = pdata_offset;
498 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
499 crypt_ftr->minor_version = 1;
Henrik Baard91064632015-02-05 15:09:17 +0100500 free(pdata);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700501 }
502
Paul Lawrencef4faa572014-01-29 13:31:03 -0800503 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700504 SLOGW("upgrading crypto footer to 1.2");
JP Abgrall7bdfa522013-11-15 13:42:56 -0800505 /* But keep the old kdf_type.
506 * It will get updated later to KDF_SCRYPT after the password has been verified.
507 */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700508 crypt_ftr->kdf_type = KDF_PBKDF2;
509 get_device_scrypt_params(crypt_ftr);
510 crypt_ftr->minor_version = 2;
511 }
512
Paul Lawrencef4faa572014-01-29 13:31:03 -0800513 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
514 SLOGW("upgrading crypto footer to 1.3");
515 crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
516 crypt_ftr->minor_version = 3;
517 }
518
Kenny Root7434b312013-06-14 11:29:53 -0700519 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
520 if (lseek64(fd, offset, SEEK_SET) == -1) {
521 SLOGE("Cannot seek to crypt footer\n");
522 return;
523 }
524 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700525 }
Ken Sumrall160b4d62013-04-22 12:15:39 -0700526}
527
528
529static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800530{
531 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800532 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700533 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800534 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700535 char *fname = NULL;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700536 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800537
Ken Sumrall160b4d62013-04-22 12:15:39 -0700538 if (get_crypt_ftr_info(&fname, &starting_off)) {
539 SLOGE("Unable to get crypt_ftr_info\n");
540 return -1;
541 }
542 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700543 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700544 return -1;
545 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700546 if ( (fd = open(fname, O_RDWR|O_CLOEXEC)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700547 SLOGE("Cannot open footer file %s for get\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700548 return -1;
549 }
550
551 /* Make sure it's 16 Kbytes in length */
552 fstat(fd, &statbuf);
553 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
554 SLOGE("footer file %s is not the expected size!\n", fname);
555 goto errout;
556 }
557
558 /* Seek to the start of the crypt footer */
559 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
560 SLOGE("Cannot seek to real block device footer\n");
561 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800562 }
563
564 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
565 SLOGE("Cannot read real block device footer\n");
566 goto errout;
567 }
568
569 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700570 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800571 goto errout;
572 }
573
Kenny Rootc96a5f82013-06-14 12:08:28 -0700574 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
575 SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
576 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800577 goto errout;
578 }
579
Kenny Rootc96a5f82013-06-14 12:08:28 -0700580 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
581 SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
582 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800583 }
584
Ken Sumrall160b4d62013-04-22 12:15:39 -0700585 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
586 * copy on disk before returning.
587 */
Kenny Rootc96a5f82013-06-14 12:08:28 -0700588 if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700589 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
Ken Sumralle8744072011-01-18 22:01:55 -0800590 }
591
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800592 /* Success! */
593 rc = 0;
594
595errout:
596 close(fd);
597 return rc;
598}
599
Ken Sumrall160b4d62013-04-22 12:15:39 -0700600static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
601{
602 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
603 crypt_ftr->persist_data_offset[1]) {
604 SLOGE("Crypt_ftr persist data regions overlap");
605 return -1;
606 }
607
608 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
609 SLOGE("Crypt_ftr persist data region 0 starts after region 1");
610 return -1;
611 }
612
613 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
614 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
615 CRYPT_FOOTER_OFFSET) {
616 SLOGE("Persistent data extends past crypto footer");
617 return -1;
618 }
619
620 return 0;
621}
622
623static int load_persistent_data(void)
624{
625 struct crypt_mnt_ftr crypt_ftr;
626 struct crypt_persist_data *pdata = NULL;
627 char encrypted_state[PROPERTY_VALUE_MAX];
628 char *fname;
629 int found = 0;
630 int fd;
631 int ret;
632 int i;
633
634 if (persist_data) {
635 /* Nothing to do, we've already loaded or initialized it */
636 return 0;
637 }
638
639
640 /* If not encrypted, just allocate an empty table and initialize it */
641 property_get("ro.crypto.state", encrypted_state, "");
642 if (strcmp(encrypted_state, "encrypted") ) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800643 pdata = (crypt_persist_data*)malloc(CRYPT_PERSIST_DATA_SIZE);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700644 if (pdata) {
645 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
646 persist_data = pdata;
647 return 0;
648 }
649 return -1;
650 }
651
652 if(get_crypt_ftr_and_key(&crypt_ftr)) {
653 return -1;
654 }
655
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700656 if ((crypt_ftr.major_version < 1)
657 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700658 SLOGE("Crypt_ftr version doesn't support persistent data");
659 return -1;
660 }
661
662 if (get_crypt_ftr_info(&fname, NULL)) {
663 return -1;
664 }
665
666 ret = validate_persistent_data_storage(&crypt_ftr);
667 if (ret) {
668 return -1;
669 }
670
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700671 fd = open(fname, O_RDONLY|O_CLOEXEC);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700672 if (fd < 0) {
673 SLOGE("Cannot open %s metadata file", fname);
674 return -1;
675 }
676
Wei Wang4375f1b2017-02-24 17:43:01 -0800677 pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
Paul Lawrence300dae72016-03-11 11:02:52 -0800678 if (pdata == NULL) {
679 SLOGE("Cannot allocate memory for persistent data");
680 goto err;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700681 }
682
683 for (i = 0; i < 2; i++) {
684 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
685 SLOGE("Cannot seek to read persistent data on %s", fname);
686 goto err2;
687 }
688 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
689 SLOGE("Error reading persistent data on iteration %d", i);
690 goto err2;
691 }
692 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
693 found = 1;
694 break;
695 }
696 }
697
698 if (!found) {
699 SLOGI("Could not find valid persistent data, creating");
700 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
701 }
702
703 /* Success */
704 persist_data = pdata;
705 close(fd);
706 return 0;
707
708err2:
709 free(pdata);
710
711err:
712 close(fd);
713 return -1;
714}
715
716static int save_persistent_data(void)
717{
718 struct crypt_mnt_ftr crypt_ftr;
719 struct crypt_persist_data *pdata;
720 char *fname;
721 off64_t write_offset;
722 off64_t erase_offset;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700723 int fd;
724 int ret;
725
726 if (persist_data == NULL) {
727 SLOGE("No persistent data to save");
728 return -1;
729 }
730
731 if(get_crypt_ftr_and_key(&crypt_ftr)) {
732 return -1;
733 }
734
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700735 if ((crypt_ftr.major_version < 1)
736 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700737 SLOGE("Crypt_ftr version doesn't support persistent data");
738 return -1;
739 }
740
741 ret = validate_persistent_data_storage(&crypt_ftr);
742 if (ret) {
743 return -1;
744 }
745
746 if (get_crypt_ftr_info(&fname, NULL)) {
747 return -1;
748 }
749
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700750 fd = open(fname, O_RDWR|O_CLOEXEC);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700751 if (fd < 0) {
752 SLOGE("Cannot open %s metadata file", fname);
753 return -1;
754 }
755
Wei Wang4375f1b2017-02-24 17:43:01 -0800756 pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700757 if (pdata == NULL) {
758 SLOGE("Cannot allocate persistant data");
759 goto err;
760 }
761
762 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
763 SLOGE("Cannot seek to read persistent data on %s", fname);
764 goto err2;
765 }
766
767 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
768 SLOGE("Error reading persistent data before save");
769 goto err2;
770 }
771
772 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
773 /* The first copy is the curent valid copy, so write to
774 * the second copy and erase this one */
775 write_offset = crypt_ftr.persist_data_offset[1];
776 erase_offset = crypt_ftr.persist_data_offset[0];
777 } else {
778 /* The second copy must be the valid copy, so write to
779 * the first copy, and erase the second */
780 write_offset = crypt_ftr.persist_data_offset[0];
781 erase_offset = crypt_ftr.persist_data_offset[1];
782 }
783
784 /* Write the new copy first, if successful, then erase the old copy */
Björn Landström96dbee72015-01-20 12:43:56 +0100785 if (lseek64(fd, write_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700786 SLOGE("Cannot seek to write persistent data");
787 goto err2;
788 }
789 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
790 (int) crypt_ftr.persist_data_size) {
Björn Landström96dbee72015-01-20 12:43:56 +0100791 if (lseek64(fd, erase_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700792 SLOGE("Cannot seek to erase previous persistent data");
793 goto err2;
794 }
795 fsync(fd);
796 memset(pdata, 0, crypt_ftr.persist_data_size);
797 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
798 (int) crypt_ftr.persist_data_size) {
799 SLOGE("Cannot write to erase previous persistent data");
800 goto err2;
801 }
802 fsync(fd);
803 } else {
804 SLOGE("Cannot write to save persistent data");
805 goto err2;
806 }
807
808 /* Success */
809 free(pdata);
810 close(fd);
811 return 0;
812
813err2:
814 free(pdata);
815err:
816 close(fd);
817 return -1;
818}
819
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800820/* Convert a binary key of specified length into an ascii hex string equivalent,
821 * without the leading 0x and with null termination
822 */
Jeff Sharkey9c484982015-03-31 10:35:33 -0700823static void convert_key_to_hex_ascii(const unsigned char *master_key,
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700824 unsigned int keysize, char *master_key_ascii) {
825 unsigned int i, a;
826 unsigned char nibble;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800827
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700828 for (i=0, a=0; i<keysize; i++, a+=2) {
829 /* For each byte, write out two ascii hex digits */
830 nibble = (master_key[i] >> 4) & 0xf;
831 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800832
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700833 nibble = master_key[i] & 0xf;
834 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
835 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800836
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700837 /* Add the null termination */
838 master_key_ascii[a] = '\0';
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800839
840}
841
Jeff Sharkey9c484982015-03-31 10:35:33 -0700842static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr,
843 const unsigned char *master_key, const char *real_blk_name,
844 const char *name, int fd, const char *extra_params) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800845 alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumralldb5e0262013-02-05 17:39:48 -0800846 struct dm_ioctl *io;
847 struct dm_target_spec *tgt;
848 char *crypt_params;
849 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
George Burgess IV605d7ae2016-02-29 13:39:17 -0800850 size_t buff_offset;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800851 int i;
852
853 io = (struct dm_ioctl *) buffer;
854
855 /* Load the mapping table for this device */
856 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
857
858 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
859 io->target_count = 1;
860 tgt->status = 0;
861 tgt->sector_start = 0;
862 tgt->length = crypt_ftr->fs_size;
Ajay Dudani87701e22014-09-17 21:02:52 -0700863 strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME);
Ken Sumralldb5e0262013-02-05 17:39:48 -0800864
865 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
866 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
George Burgess IV605d7ae2016-02-29 13:39:17 -0800867
868 buff_offset = crypt_params - buffer;
869 snprintf(crypt_params, sizeof(buffer) - buff_offset, "%s %s 0 %s 0 %s",
870 crypt_ftr->crypto_type_name, master_key_ascii, real_blk_name,
871 extra_params);
Ken Sumralldb5e0262013-02-05 17:39:48 -0800872 crypt_params += strlen(crypt_params) + 1;
873 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
874 tgt->next = crypt_params - buffer;
875
876 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
877 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
878 break;
879 }
880 usleep(500000);
881 }
882
883 if (i == TABLE_LOAD_RETRIES) {
884 /* We failed to load the table, return an error */
885 return -1;
886 } else {
887 return i + 1;
888 }
889}
890
891
892static int get_dm_crypt_version(int fd, const char *name, int *version)
893{
894 char buffer[DM_CRYPT_BUF_SIZE];
895 struct dm_ioctl *io;
896 struct dm_target_versions *v;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800897
898 io = (struct dm_ioctl *) buffer;
899
900 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
901
902 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
903 return -1;
904 }
905
906 /* Iterate over the returned versions, looking for name of "crypt".
907 * When found, get and return the version.
908 */
909 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
910 while (v->next) {
911 if (! strcmp(v->name, "crypt")) {
912 /* We found the crypt driver, return the version, and get out */
913 version[0] = v->version[0];
914 version[1] = v->version[1];
915 version[2] = v->version[2];
916 return 0;
917 }
918 v = (struct dm_target_versions *)(((char *)v) + v->next);
919 }
920
921 return -1;
922}
923
Jeff Sharkey9c484982015-03-31 10:35:33 -0700924static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr,
925 const unsigned char *master_key, const char *real_blk_name,
926 char *crypto_blk_name, const char *name) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800927 char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800928 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800929 unsigned int minor;
Ajay Dudani87701e22014-09-17 21:02:52 -0700930 int fd=0;
Daniel Rosenberg25a52132016-02-26 16:44:36 -0800931 int err;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800932 int retval = -1;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800933 int version[3];
Wei Wang4375f1b2017-02-24 17:43:01 -0800934 const char *extra_params;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800935 int load_count;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800936
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700937 if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800938 SLOGE("Cannot open device-mapper\n");
939 goto errout;
940 }
941
942 io = (struct dm_ioctl *) buffer;
943
944 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Daniel Rosenberg25a52132016-02-26 16:44:36 -0800945 err = ioctl(fd, DM_DEV_CREATE, io);
946 if (err) {
947 SLOGE("Cannot create dm-crypt device %s: %s\n", name, strerror(errno));
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800948 goto errout;
949 }
950
951 /* Get the device status, in particular, the name of it's device file */
952 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
953 if (ioctl(fd, DM_DEV_STATUS, io)) {
954 SLOGE("Cannot retrieve dm-crypt device status\n");
955 goto errout;
956 }
957 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
958 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
959
Ken Sumralldb5e0262013-02-05 17:39:48 -0800960 extra_params = "";
961 if (! get_dm_crypt_version(fd, name, version)) {
962 /* Support for allow_discards was added in version 1.11.0 */
963 if ((version[0] >= 2) ||
964 ((version[0] == 1) && (version[1] >= 11))) {
965 extra_params = "1 allow_discards";
966 SLOGI("Enabling support for allow_discards in dmcrypt.\n");
967 }
Ken Sumralle919efe2012-09-29 17:07:41 -0700968 }
969
Ken Sumralldb5e0262013-02-05 17:39:48 -0800970 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name,
971 fd, extra_params);
972 if (load_count < 0) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800973 SLOGE("Cannot load dm-crypt mapping table.\n");
974 goto errout;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800975 } else if (load_count > 1) {
976 SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800977 }
978
979 /* Resume this device to activate it */
Ken Sumralldb5e0262013-02-05 17:39:48 -0800980 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800981
982 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
983 SLOGE("Cannot resume the dm-crypt device\n");
984 goto errout;
985 }
986
987 /* We made it here with no errors. Woot! */
988 retval = 0;
989
990errout:
991 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
992
993 return retval;
994}
995
Wei Wang4375f1b2017-02-24 17:43:01 -0800996static int delete_crypto_blk_dev(const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800997{
998 int fd;
999 char buffer[DM_CRYPT_BUF_SIZE];
1000 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001001 int retval = -1;
1002
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001003 if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001004 SLOGE("Cannot open device-mapper\n");
1005 goto errout;
1006 }
1007
1008 io = (struct dm_ioctl *) buffer;
1009
1010 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1011 if (ioctl(fd, DM_DEV_REMOVE, io)) {
1012 SLOGE("Cannot remove dm-crypt device\n");
1013 goto errout;
1014 }
1015
1016 /* We made it here with no errors. Woot! */
1017 retval = 0;
1018
1019errout:
1020 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1021
1022 return retval;
1023
1024}
1025
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001026static int pbkdf2(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001027 unsigned char *ikey, void *params UNUSED)
1028{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001029 SLOGI("Using pbkdf2 for cryptfs KDF");
1030
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001031 /* Turn the password into a key and IV that can decrypt the master key */
Adam Langleybf0d9722015-11-04 14:51:39 -08001032 return PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
1033 HASH_COUNT, KEY_LEN_BYTES + IV_LEN_BYTES,
1034 ikey) != 1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001035}
1036
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001037static int scrypt(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001038 unsigned char *ikey, void *params)
1039{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001040 SLOGI("Using scrypt for cryptfs KDF");
1041
Kenny Rootc4c70f12013-06-14 12:11:38 -07001042 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1043
1044 int N = 1 << ftr->N_factor;
1045 int r = 1 << ftr->r_factor;
1046 int p = 1 << ftr->p_factor;
1047
1048 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001049 unsigned int keysize;
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001050 crypto_scrypt((const uint8_t*)passwd, strlen(passwd),
1051 salt, SALT_LEN, N, r, p, ikey,
1052 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001053
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001054 return 0;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001055}
1056
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001057static int scrypt_keymaster(const char *passwd, const unsigned char *salt,
1058 unsigned char *ikey, void *params)
1059{
1060 SLOGI("Using scrypt with keymaster for cryptfs KDF");
1061
1062 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001063 size_t signature_size;
1064 unsigned char* signature;
1065 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1066
1067 int N = 1 << ftr->N_factor;
1068 int r = 1 << ftr->r_factor;
1069 int p = 1 << ftr->p_factor;
1070
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001071 rc = crypto_scrypt((const uint8_t*)passwd, strlen(passwd),
1072 salt, SALT_LEN, N, r, p, ikey,
1073 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001074
1075 if (rc) {
1076 SLOGE("scrypt failed");
1077 return -1;
1078 }
1079
Shawn Willdene17a9c42014-09-08 13:04:08 -06001080 if (keymaster_sign_object(ftr, ikey, KEY_LEN_BYTES + IV_LEN_BYTES,
1081 &signature, &signature_size)) {
1082 SLOGE("Signing failed");
1083 return -1;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001084 }
1085
1086 rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN,
1087 N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES);
1088 free(signature);
1089
1090 if (rc) {
1091 SLOGE("scrypt failed");
1092 return -1;
1093 }
1094
1095 return 0;
1096}
1097
1098static int encrypt_master_key(const char *passwd, const unsigned char *salt,
1099 const unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001100 unsigned char *encrypted_master_key,
1101 struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001102{
1103 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
1104 EVP_CIPHER_CTX e_ctx;
1105 int encrypted_len, final_len;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001106 int rc = 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001107
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001108 /* Turn the password into an intermediate key and IV that can decrypt the master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001109 get_device_scrypt_params(crypt_ftr);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001110
1111 switch (crypt_ftr->kdf_type) {
1112 case KDF_SCRYPT_KEYMASTER:
1113 if (keymaster_create_key(crypt_ftr)) {
1114 SLOGE("keymaster_create_key failed");
1115 return -1;
1116 }
1117
1118 if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) {
1119 SLOGE("scrypt failed");
1120 return -1;
1121 }
1122 break;
1123
1124 case KDF_SCRYPT:
1125 if (scrypt(passwd, salt, ikey, crypt_ftr)) {
1126 SLOGE("scrypt failed");
1127 return -1;
1128 }
1129 break;
1130
1131 default:
1132 SLOGE("Invalid kdf_type");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001133 return -1;
1134 }
Kenny Rootc4c70f12013-06-14 12:11:38 -07001135
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001136 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001137 EVP_CIPHER_CTX_init(&e_ctx);
1138 if (! EVP_EncryptInit_ex(&e_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001139 SLOGE("EVP_EncryptInit failed\n");
1140 return -1;
1141 }
1142 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001143
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001144 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001145 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
Paul Lawrence731a7a22015-04-28 22:14:15 +00001146 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001147 SLOGE("EVP_EncryptUpdate failed\n");
1148 return -1;
1149 }
Adam Langley889c4f12014-09-03 14:23:13 -07001150 if (! EVP_EncryptFinal_ex(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001151 SLOGE("EVP_EncryptFinal failed\n");
1152 return -1;
1153 }
1154
1155 if (encrypted_len + final_len != KEY_LEN_BYTES) {
1156 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
1157 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001158 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001159
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001160 /* Store the scrypt of the intermediate key, so we can validate if it's a
1161 password error or mount error when things go wrong.
1162 Note there's no need to check for errors, since if this is incorrect, we
1163 simply won't wipe userdata, which is the correct default behavior
1164 */
1165 int N = 1 << crypt_ftr->N_factor;
1166 int r = 1 << crypt_ftr->r_factor;
1167 int p = 1 << crypt_ftr->p_factor;
1168
1169 rc = crypto_scrypt(ikey, KEY_LEN_BYTES,
1170 crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p,
1171 crypt_ftr->scrypted_intermediate_key,
1172 sizeof(crypt_ftr->scrypted_intermediate_key));
1173
1174 if (rc) {
1175 SLOGE("encrypt_master_key: crypto_scrypt failed");
1176 }
1177
Thurston Hou Yeen Dang06dc3112016-07-18 14:16:37 -07001178 EVP_CIPHER_CTX_cleanup(&e_ctx);
1179
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001180 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001181}
1182
Paul Lawrence731a7a22015-04-28 22:14:15 +00001183static int decrypt_master_key_aux(const char *passwd, unsigned char *salt,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001184 unsigned char *encrypted_master_key,
1185 unsigned char *decrypted_master_key,
1186 kdf_func kdf, void *kdf_params,
1187 unsigned char** intermediate_key,
1188 size_t* intermediate_key_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001189{
1190 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 -08001191 EVP_CIPHER_CTX d_ctx;
1192 int decrypted_len, final_len;
1193
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001194 /* Turn the password into an intermediate key and IV that can decrypt the
1195 master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001196 if (kdf(passwd, salt, ikey, kdf_params)) {
1197 SLOGE("kdf failed");
1198 return -1;
1199 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001200
1201 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001202 EVP_CIPHER_CTX_init(&d_ctx);
1203 if (! EVP_DecryptInit_ex(&d_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001204 return -1;
1205 }
1206 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1207 /* Decrypt the master key */
1208 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
1209 encrypted_master_key, KEY_LEN_BYTES)) {
1210 return -1;
1211 }
Adam Langley889c4f12014-09-03 14:23:13 -07001212 if (! EVP_DecryptFinal_ex(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001213 return -1;
1214 }
1215
1216 if (decrypted_len + final_len != KEY_LEN_BYTES) {
1217 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001218 }
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001219
1220 /* Copy intermediate key if needed by params */
1221 if (intermediate_key && intermediate_key_size) {
1222 *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES);
Greg Kaisere8167af2016-04-20 10:50:15 -07001223 if (*intermediate_key) {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001224 memcpy(*intermediate_key, ikey, KEY_LEN_BYTES);
1225 *intermediate_key_size = KEY_LEN_BYTES;
1226 }
1227 }
1228
Thurston Hou Yeen Dang06dc3112016-07-18 14:16:37 -07001229 EVP_CIPHER_CTX_cleanup(&d_ctx);
1230
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001231 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001232}
1233
Kenny Rootc4c70f12013-06-14 12:11:38 -07001234static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001235{
Paul Lawrencedb3730c2015-02-03 13:08:10 -08001236 if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001237 *kdf = scrypt_keymaster;
1238 *kdf_params = ftr;
1239 } else if (ftr->kdf_type == KDF_SCRYPT) {
Kenny Rootc4c70f12013-06-14 12:11:38 -07001240 *kdf = scrypt;
1241 *kdf_params = ftr;
1242 } else {
1243 *kdf = pbkdf2;
1244 *kdf_params = NULL;
1245 }
1246}
1247
Paul Lawrence731a7a22015-04-28 22:14:15 +00001248static int decrypt_master_key(const char *passwd, unsigned char *decrypted_master_key,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001249 struct crypt_mnt_ftr *crypt_ftr,
1250 unsigned char** intermediate_key,
1251 size_t* intermediate_key_size)
Kenny Rootc4c70f12013-06-14 12:11:38 -07001252{
1253 kdf_func kdf;
1254 void *kdf_params;
1255 int ret;
1256
1257 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001258 ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key,
1259 decrypted_master_key, kdf, kdf_params,
1260 intermediate_key, intermediate_key_size);
Kenny Rootc4c70f12013-06-14 12:11:38 -07001261 if (ret != 0) {
1262 SLOGW("failure decrypting master key");
Kenny Rootc4c70f12013-06-14 12:11:38 -07001263 }
1264
1265 return ret;
1266}
1267
Wei Wang4375f1b2017-02-24 17:43:01 -08001268static int create_encrypted_random_key(const char *passwd, unsigned char *master_key, unsigned char *salt,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001269 struct crypt_mnt_ftr *crypt_ftr) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001270 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -08001271 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001272
1273 /* Get some random bits for a key */
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001274 fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC);
Ken Sumralle8744072011-01-18 22:01:55 -08001275 read(fd, key_buf, sizeof(key_buf));
1276 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001277 close(fd);
1278
1279 /* Now encrypt it with the password */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001280 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001281}
1282
Paul Lawrence2f32cda2015-05-05 14:28:25 -07001283int wait_and_unmount(const char *mountpoint, bool kill)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001284{
Greg Hackmann955653e2014-09-24 14:55:20 -07001285 int i, err, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001286#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001287
1288 /* Now umount the tmpfs filesystem */
1289 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001290 if (umount(mountpoint) == 0) {
1291 break;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001292 }
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001293
1294 if (errno == EINVAL) {
1295 /* EINVAL is returned if the directory is not a mountpoint,
1296 * i.e. there is no filesystem mounted there. So just get out.
1297 */
1298 break;
1299 }
1300
1301 err = errno;
1302
1303 /* If allowed, be increasingly aggressive before the last two retries */
1304 if (kill) {
1305 if (i == (WAIT_UNMOUNT_COUNT - 3)) {
1306 SLOGW("sending SIGHUP to processes with open files\n");
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001307 vold_killProcessesWithOpenFiles(mountpoint, SIGTERM);
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001308 } else if (i == (WAIT_UNMOUNT_COUNT - 2)) {
1309 SLOGW("sending SIGKILL to processes with open files\n");
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001310 vold_killProcessesWithOpenFiles(mountpoint, SIGKILL);
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001311 }
1312 }
1313
1314 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001315 }
1316
1317 if (i < WAIT_UNMOUNT_COUNT) {
1318 SLOGD("unmounting %s succeeded\n", mountpoint);
1319 rc = 0;
1320 } else {
jessica_yu3f14fe42014-09-22 15:57:40 +08001321 vold_killProcessesWithOpenFiles(mountpoint, 0);
Greg Hackmann955653e2014-09-24 14:55:20 -07001322 SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001323 rc = -1;
1324 }
1325
1326 return rc;
1327}
1328
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001329static int prep_data_fs(void)
1330{
1331 int i;
1332
Jeff Sharkey47695b22016-02-01 17:02:29 -07001333 // NOTE: post_fs_data results in init calling back around to vold, so all
1334 // callers to this method must be async
1335
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001336 /* Do the prep of the /data filesystem */
1337 property_set("vold.post_fs_data_done", "0");
1338 property_set("vold.decrypt", "trigger_post_fs_data");
1339 SLOGD("Just triggered post_fs_data\n");
1340
Ken Sumrallc5872692013-05-14 15:26:31 -07001341 /* Wait a max of 50 seconds, hopefully it takes much less */
Wei Wang4375f1b2017-02-24 17:43:01 -08001342 if (!android::base::WaitForProperty("vold.post_fs_data_done",
1343 "1",
1344 std::chrono::seconds(50))) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001345 /* Ugh, we failed to prep /data in time. Bail. */
Ken Sumrallc5872692013-05-14 15:26:31 -07001346 SLOGE("post_fs_data timed out!\n");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001347 return -1;
1348 } else {
1349 SLOGD("post_fs_data done\n");
1350 return 0;
1351 }
1352}
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);
1454 rec->flags |= MS_RDONLY;
1455 }
JP Abgrall62c7af32014-06-16 13:01:23 -07001456
Ken Sumralle5032c42012-04-01 23:58:44 -07001457 /* If that succeeded, then mount the decrypted filesystem */
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001458 int retries = RETRY_MOUNT_ATTEMPTS;
1459 int mount_rc;
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001460
1461 /*
1462 * fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
1463 * partitions in the fsck domain.
1464 */
1465 if (setexeccon(secontextFsck())){
1466 SLOGE("Failed to setexeccon");
1467 return -1;
1468 }
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001469 while ((mount_rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT,
1470 crypto_blkdev, 0))
1471 != 0) {
1472 if (mount_rc == FS_MGR_DOMNT_BUSY) {
1473 /* TODO: invoke something similar to
1474 Process::killProcessWithOpenFiles(DATA_MNT_POINT,
1475 retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */
1476 SLOGI("Failed to mount %s because it is busy - waiting",
1477 crypto_blkdev);
1478 if (--retries) {
1479 sleep(RETRY_MOUNT_DELAY_SECONDS);
1480 } else {
1481 /* Let's hope that a reboot clears away whatever is keeping
1482 the mount busy */
Josh Gaofec44372017-08-28 13:22:55 -07001483 cryptfs_reboot(RebootType::reboot);
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001484 }
1485 } else {
1486 SLOGE("Failed to mount decrypted data");
1487 cryptfs_set_corrupt();
1488 cryptfs_trigger_restart_min_framework();
1489 SLOGI("Started framework to offer wipe");
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001490 if (setexeccon(NULL)) {
1491 SLOGE("Failed to setexeccon");
1492 }
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001493 return -1;
1494 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001495 }
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001496 if (setexeccon(NULL)) {
1497 SLOGE("Failed to setexeccon");
1498 return -1;
1499 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001500
Ken Sumralle5032c42012-04-01 23:58:44 -07001501 /* Create necessary paths on /data */
1502 if (prep_data_fs()) {
1503 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001504 }
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);
2066 int fs_type = get_fs_type(rec);
2067 if (fs_type < 0) {
2068 SLOGE("cryptfs_enable: unsupported fs type %s\n", rec->fs_type);
2069 return -1;
2070 }
2071 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr->fs_size, fs_type);
Paul Lawrence87999172014-02-20 12:21:31 -08002072 } else if (how == CRYPTO_ENABLE_INPLACE) {
2073 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev,
2074 crypt_ftr->fs_size, &cur_encryption_done,
2075 tot_encryption_size,
2076 previously_encrypted_upto);
2077
JP Abgrall7fc1de82014-10-10 18:43:41 -07002078 if (rc == ENABLE_INPLACE_ERR_DEV) {
2079 /* Hack for b/17898962 */
2080 SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n");
Josh Gaofec44372017-08-28 13:22:55 -07002081 cryptfs_reboot(RebootType::reboot);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002082 }
2083
Paul Lawrence73d7a022014-06-09 14:10:09 -07002084 if (!rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002085 crypt_ftr->encrypted_upto = cur_encryption_done;
2086 }
2087
Paul Lawrence73d7a022014-06-09 14:10:09 -07002088 if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002089 /* The inplace routine never actually sets the progress to 100% due
2090 * to the round down nature of integer division, so set it here */
2091 property_set("vold.encrypt_progress", "100");
2092 }
2093 } else {
2094 /* Shouldn't happen */
2095 SLOGE("cryptfs_enable: internal error, unknown option\n");
2096 rc = -1;
2097 }
2098
2099 return rc;
2100}
2101
Wei Wang4375f1b2017-02-24 17:43:01 -08002102int cryptfs_enable_internal(char *howarg, int crypt_type, const char *passwd,
Paul Lawrence569649f2015-09-09 12:13:00 -07002103 int no_ui)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002104{
2105 int how = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002106 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN];
Ken Sumrall160b4d62013-04-22 12:15:39 -07002107 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002108 int rc=-1, i;
Paul Lawrence87999172014-02-20 12:21:31 -08002109 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002110 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002111 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002112 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07002113 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall29d8da82011-05-18 17:20:07 -07002114 int num_vols;
Paul Lawrence87999172014-02-20 12:21:31 -08002115 off64_t previously_encrypted_upto = 0;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002116 bool rebootEncryption = false;
Wei Wang4375f1b2017-02-24 17:43:01 -08002117 bool onlyCreateHeader = false;
2118 int fd = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002119
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002120 if (!strcmp(howarg, "wipe")) {
2121 how = CRYPTO_ENABLE_WIPE;
2122 } else if (! strcmp(howarg, "inplace")) {
2123 how = CRYPTO_ENABLE_INPLACE;
2124 } else {
2125 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08002126 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002127 }
2128
Paul Lawrence87999172014-02-20 12:21:31 -08002129 if (how == CRYPTO_ENABLE_INPLACE
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002130 && get_crypt_ftr_and_key(&crypt_ftr) == 0) {
2131 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
2132 /* An encryption was underway and was interrupted */
2133 previously_encrypted_upto = crypt_ftr.encrypted_upto;
2134 crypt_ftr.encrypted_upto = 0;
2135 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002136
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002137 /* At this point, we are in an inconsistent state. Until we successfully
2138 complete encryption, a reboot will leave us broken. So mark the
2139 encryption failed in case that happens.
2140 On successfully completing encryption, remove this flag */
2141 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002142
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002143 put_crypt_ftr_and_key(&crypt_ftr);
2144 } else if (crypt_ftr.flags & CRYPT_FORCE_ENCRYPTION) {
2145 if (!check_ftr_sha(&crypt_ftr)) {
2146 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
2147 put_crypt_ftr_and_key(&crypt_ftr);
2148 goto error_unencrypted;
2149 }
2150
2151 /* Doing a reboot-encryption*/
2152 crypt_ftr.flags &= ~CRYPT_FORCE_ENCRYPTION;
2153 crypt_ftr.flags |= CRYPT_FORCE_COMPLETE;
2154 rebootEncryption = true;
2155 }
Paul Lawrence87999172014-02-20 12:21:31 -08002156 }
2157
2158 property_get("ro.crypto.state", encrypted_state, "");
2159 if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) {
2160 SLOGE("Device is already running encrypted, aborting");
2161 goto error_unencrypted;
2162 }
2163
2164 // TODO refactor fs_mgr_get_crypt_info to get both in one call
2165 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumrall56ad03c2013-02-13 13:00:19 -08002166 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002167
Ken Sumrall3ed82362011-01-28 23:31:16 -08002168 /* Get the size of the real block device */
Wei Wang4375f1b2017-02-24 17:43:01 -08002169 fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002170 if (fd == -1) {
2171 SLOGE("Cannot open block device %s\n", real_blkdev);
2172 goto error_unencrypted;
2173 }
2174 unsigned long nr_sec;
2175 get_blkdev_size(fd, &nr_sec);
2176 if (nr_sec == 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002177 SLOGE("Cannot get size of block device %s\n", real_blkdev);
2178 goto error_unencrypted;
2179 }
2180 close(fd);
2181
2182 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002183 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002184 unsigned int fs_size_sec, max_fs_size_sec;
Jim Millera70abc62014-08-15 02:00:45 +00002185 fs_size_sec = get_fs_size(real_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002186 if (fs_size_sec == 0)
2187 fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev);
2188
Paul Lawrence87999172014-02-20 12:21:31 -08002189 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002190
2191 if (fs_size_sec > max_fs_size_sec) {
2192 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
2193 goto error_unencrypted;
2194 }
2195 }
2196
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002197 /* Get a wakelock as this may take a while, and we don't want the
2198 * device to sleep on us. We'll grab a partial wakelock, and if the UI
2199 * wants to keep the screen on, it can grab a full wakelock.
2200 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002201 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002202 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
2203
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002204 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002205 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002206 */
2207 property_set("vold.decrypt", "trigger_shutdown_framework");
2208 SLOGD("Just asked init to shut down class main\n");
2209
Jeff Sharkey9c484982015-03-31 10:35:33 -07002210 /* Ask vold to unmount all devices that it manages */
2211 if (vold_unmountAll()) {
2212 SLOGE("Failed to unmount all vold managed devices");
Ken Sumrall2eaf7132011-01-14 12:45:48 -08002213 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002214
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002215 /* no_ui means we are being called from init, not settings.
2216 Now we always reboot from settings, so !no_ui means reboot
2217 */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002218 if (!no_ui) {
2219 /* Try fallback, which is to reboot and try there */
2220 onlyCreateHeader = true;
2221 FILE* breadcrumb = fopen(BREADCRUMB_FILE, "we");
2222 if (breadcrumb == 0) {
2223 SLOGE("Failed to create breadcrumb file");
2224 goto error_shutting_down;
2225 }
2226 fclose(breadcrumb);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002227 }
2228
2229 /* Do extra work for a better UX when doing the long inplace encryption */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002230 if (how == CRYPTO_ENABLE_INPLACE && !onlyCreateHeader) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002231 /* Now that /data is unmounted, we need to mount a tmpfs
2232 * /data, set a property saying we're doing inplace encryption,
2233 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002234 */
Ken Sumralle5032c42012-04-01 23:58:44 -07002235 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002236 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002237 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002238 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08002239 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002240
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002241 /* restart the framework. */
2242 /* Create necessary paths on /data */
2243 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002244 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002245 }
2246
Ken Sumrall92736ef2012-10-17 20:57:14 -07002247 /* Ugh, shutting down the framework is not synchronous, so until it
2248 * can be fixed, this horrible hack will wait a moment for it all to
2249 * shut down before proceeding. Without it, some devices cannot
2250 * restart the graphics services.
2251 */
2252 sleep(2);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002253 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002254
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002255 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002256 /* Initialize a crypt_mnt_ftr for the partition */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002257 if (previously_encrypted_upto == 0 && !rebootEncryption) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002258 if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) {
2259 goto error_shutting_down;
2260 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002261
Paul Lawrence87999172014-02-20 12:21:31 -08002262 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
2263 crypt_ftr.fs_size = nr_sec
2264 - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
2265 } else {
2266 crypt_ftr.fs_size = nr_sec;
2267 }
Paul Lawrence6bfed202014-07-28 12:47:22 -07002268 /* At this point, we are in an inconsistent state. Until we successfully
2269 complete encryption, a reboot will leave us broken. So mark the
2270 encryption failed in case that happens.
2271 On successfully completing encryption, remove this flag */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002272 if (onlyCreateHeader) {
2273 crypt_ftr.flags |= CRYPT_FORCE_ENCRYPTION;
2274 } else {
2275 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
2276 }
Paul Lawrence87999172014-02-20 12:21:31 -08002277 crypt_ftr.crypt_type = crypt_type;
Ajay Dudani87701e22014-09-17 21:02:52 -07002278 strlcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256", MAX_CRYPTO_TYPE_NAME_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002279
Paul Lawrence87999172014-02-20 12:21:31 -08002280 /* Make an encrypted master key */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002281 if (create_encrypted_random_key(onlyCreateHeader ? DEFAULT_PASSWORD : passwd,
2282 crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
Paul Lawrence87999172014-02-20 12:21:31 -08002283 SLOGE("Cannot create encrypted master key\n");
2284 goto error_shutting_down;
2285 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002286
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002287 /* Replace scrypted intermediate key if we are preparing for a reboot */
2288 if (onlyCreateHeader) {
2289 unsigned char fake_master_key[KEY_LEN_BYTES];
2290 unsigned char encrypted_fake_master_key[KEY_LEN_BYTES];
2291 memset(fake_master_key, 0, sizeof(fake_master_key));
2292 encrypt_master_key(passwd, crypt_ftr.salt, fake_master_key,
2293 encrypted_fake_master_key, &crypt_ftr);
2294 }
2295
Paul Lawrence87999172014-02-20 12:21:31 -08002296 /* Write the key to the end of the partition */
2297 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002298
Paul Lawrence87999172014-02-20 12:21:31 -08002299 /* If any persistent data has been remembered, save it.
2300 * If none, create a valid empty table and save that.
2301 */
2302 if (!persist_data) {
Wei Wang4375f1b2017-02-24 17:43:01 -08002303 pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
Paul Lawrence87999172014-02-20 12:21:31 -08002304 if (pdata) {
2305 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
2306 persist_data = pdata;
2307 }
2308 }
2309 if (persist_data) {
2310 save_persistent_data();
2311 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002312 }
2313
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002314 if (onlyCreateHeader) {
2315 sleep(2);
Josh Gaofec44372017-08-28 13:22:55 -07002316 cryptfs_reboot(RebootType::reboot);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002317 }
2318
2319 if (how == CRYPTO_ENABLE_INPLACE && (!no_ui || rebootEncryption)) {
Ajay Dudani87701e22014-09-17 21:02:52 -07002320 /* startup service classes main and late_start */
2321 property_set("vold.decrypt", "trigger_restart_min_framework");
2322 SLOGD("Just triggered restart_min_framework\n");
2323
2324 /* OK, the framework is restarted and will soon be showing a
2325 * progress bar. Time to setup an encrypted mapping, and
2326 * either write a new filesystem, or encrypt in place updating
2327 * the progress bar as we work.
2328 */
2329 }
2330
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002331 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002332 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002333 CRYPTO_BLOCK_DEVICE);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002334
Paul Lawrence87999172014-02-20 12:21:31 -08002335 /* If we are continuing, check checksums match */
2336 rc = 0;
2337 if (previously_encrypted_upto) {
2338 __le8 hash_first_block[SHA256_DIGEST_LENGTH];
2339 rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block);
Ken Sumrall128626f2011-06-28 18:45:14 -07002340
Paul Lawrence87999172014-02-20 12:21:31 -08002341 if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block,
2342 sizeof(hash_first_block)) != 0) {
2343 SLOGE("Checksums do not match - trigger wipe");
2344 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002345 }
2346 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002347
Paul Lawrence87999172014-02-20 12:21:31 -08002348 if (!rc) {
2349 rc = cryptfs_enable_all_volumes(&crypt_ftr, how,
2350 crypto_blkdev, real_blkdev,
2351 previously_encrypted_upto);
2352 }
2353
2354 /* Calculate checksum if we are not finished */
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08002355 if (!rc && how == CRYPTO_ENABLE_INPLACE
2356 && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002357 rc = cryptfs_SHA256_fileblock(crypto_blkdev,
2358 crypt_ftr.hash_first_block);
Paul Lawrence73d7a022014-06-09 14:10:09 -07002359 if (rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002360 SLOGE("Error calculating checksum for continuing encryption");
2361 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002362 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002363 }
2364
2365 /* Undo the dm-crypt mapping whether we succeed or not */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002366 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002367
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002368 if (! rc) {
2369 /* Success */
Paul Lawrence6bfed202014-07-28 12:47:22 -07002370 crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08002371
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08002372 if (how == CRYPTO_ENABLE_INPLACE
2373 && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002374 SLOGD("Encrypted up to sector %lld - will continue after reboot",
2375 crypt_ftr.encrypted_upto);
Paul Lawrence6bfed202014-07-28 12:47:22 -07002376 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence87999172014-02-20 12:21:31 -08002377 }
Paul Lawrence73d7a022014-06-09 14:10:09 -07002378
Paul Lawrence6bfed202014-07-28 12:47:22 -07002379 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08002380
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08002381 if (how == CRYPTO_ENABLE_WIPE
2382 || crypt_ftr.encrypted_upto == crypt_ftr.fs_size) {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002383 char value[PROPERTY_VALUE_MAX];
2384 property_get("ro.crypto.state", value, "");
2385 if (!strcmp(value, "")) {
2386 /* default encryption - continue first boot sequence */
2387 property_set("ro.crypto.state", "encrypted");
Paul Lawrence4ed45262016-03-10 15:44:21 -08002388 property_set("ro.crypto.type", "block");
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002389 release_wake_lock(lockid);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002390 if (rebootEncryption && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
2391 // Bring up cryptkeeper that will check the password and set it
2392 property_set("vold.decrypt", "trigger_shutdown_framework");
2393 sleep(2);
2394 property_set("vold.encrypt_progress", "");
2395 cryptfs_trigger_restart_min_framework();
2396 } else {
2397 cryptfs_check_passwd(DEFAULT_PASSWORD);
2398 cryptfs_restart_internal(1);
2399 }
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002400 return 0;
2401 } else {
2402 sleep(2); /* Give the UI a chance to show 100% progress */
Josh Gaofec44372017-08-28 13:22:55 -07002403 cryptfs_reboot(RebootType::reboot);
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002404 }
Paul Lawrence87999172014-02-20 12:21:31 -08002405 } else {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002406 sleep(2); /* Partially encrypted, ensure writes flushed to ssd */
Josh Gaofec44372017-08-28 13:22:55 -07002407 cryptfs_reboot(RebootType::shutdown);
Paul Lawrence87999172014-02-20 12:21:31 -08002408 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002409 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002410 char value[PROPERTY_VALUE_MAX];
2411
Ken Sumrall319369a2012-06-27 16:30:18 -07002412 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002413 if (!strcmp(value, "1")) {
2414 /* wipe data if encryption failed */
2415 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
Wei Wang4375f1b2017-02-24 17:43:01 -08002416 std::string err;
2417 const std::vector<std::string> options = {
2418 "--wipe_data\n--reason=cryptfs_enable_internal\n"
2419 };
2420 if (!write_bootloader_message(options, &err)) {
2421 SLOGE("could not write bootloader message: %s", err.c_str());
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002422 }
Josh Gaofec44372017-08-28 13:22:55 -07002423 cryptfs_reboot(RebootType::recovery);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002424 } else {
2425 /* set property to trigger dialog */
2426 property_set("vold.encrypt_progress", "error_partially_encrypted");
2427 release_wake_lock(lockid);
2428 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002429 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002430 }
2431
Ken Sumrall3ed82362011-01-28 23:31:16 -08002432 /* hrm, the encrypt step claims success, but the reboot failed.
2433 * This should not happen.
2434 * Set the property and return. Hope the framework can deal with it.
2435 */
2436 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002437 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002438 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08002439
2440error_unencrypted:
2441 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002442 if (lockid[0]) {
2443 release_wake_lock(lockid);
2444 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002445 return -1;
2446
2447error_shutting_down:
2448 /* we failed, and have not encrypted anthing, so the users's data is still intact,
2449 * but the framework is stopped and not restarted to show the error, so it's up to
2450 * vold to restart the system.
2451 */
2452 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Josh Gaofec44372017-08-28 13:22:55 -07002453 cryptfs_reboot(RebootType::reboot);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002454
2455 /* shouldn't get here */
2456 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002457 if (lockid[0]) {
2458 release_wake_lock(lockid);
2459 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002460 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002461}
2462
Paul Lawrence569649f2015-09-09 12:13:00 -07002463int cryptfs_enable(char *howarg, int type, char *passwd, int no_ui)
Paul Lawrence13486032014-02-03 13:28:11 -08002464{
Paul Lawrence569649f2015-09-09 12:13:00 -07002465 return cryptfs_enable_internal(howarg, type, passwd, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08002466}
2467
Paul Lawrence569649f2015-09-09 12:13:00 -07002468int cryptfs_enable_default(char *howarg, int no_ui)
Paul Lawrence13486032014-02-03 13:28:11 -08002469{
2470 return cryptfs_enable_internal(howarg, CRYPT_TYPE_DEFAULT,
Paul Lawrence569649f2015-09-09 12:13:00 -07002471 DEFAULT_PASSWORD, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08002472}
2473
2474int cryptfs_changepw(int crypt_type, const char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002475{
Paul Crowley38132a12016-02-09 09:50:32 +00002476 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002477 SLOGE("cryptfs_changepw not valid for file encryption");
2478 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08002479 }
2480
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002481 struct crypt_mnt_ftr crypt_ftr;
JP Abgrall933216c2015-02-11 13:44:32 -08002482 int rc;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002483
2484 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08002485 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08002486 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002487 return -1;
2488 }
2489
Paul Lawrencef4faa572014-01-29 13:31:03 -08002490 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2491 SLOGE("Invalid crypt_type %d", crypt_type);
2492 return -1;
2493 }
2494
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002495 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002496 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08002497 SLOGE("Error getting crypt footer and key");
2498 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002499 }
2500
Paul Lawrencef4faa572014-01-29 13:31:03 -08002501 crypt_ftr.crypt_type = crypt_type;
2502
JP Abgrall933216c2015-02-11 13:44:32 -08002503 rc = encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
Paul Lawrencef4faa572014-01-29 13:31:03 -08002504 : newpw,
2505 crypt_ftr.salt,
2506 saved_master_key,
2507 crypt_ftr.master_key,
2508 &crypt_ftr);
JP Abgrall933216c2015-02-11 13:44:32 -08002509 if (rc) {
2510 SLOGE("Encrypt master key failed: %d", rc);
2511 return -1;
2512 }
Jason parks70a4b3f2011-01-28 10:10:47 -06002513 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002514 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002515
2516 return 0;
2517}
Ken Sumrall160b4d62013-04-22 12:15:39 -07002518
Rubin Xu85c01f92014-10-13 12:49:54 +01002519static unsigned int persist_get_max_entries(int encrypted) {
2520 struct crypt_mnt_ftr crypt_ftr;
2521 unsigned int dsize;
2522 unsigned int max_persistent_entries;
2523
2524 /* If encrypted, use the values from the crypt_ftr, otherwise
2525 * use the values for the current spec.
2526 */
2527 if (encrypted) {
2528 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2529 return -1;
2530 }
2531 dsize = crypt_ftr.persist_data_size;
2532 } else {
2533 dsize = CRYPT_PERSIST_DATA_SIZE;
2534 }
2535
2536 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
2537 sizeof(struct crypt_persist_entry);
2538
2539 return max_persistent_entries;
2540}
2541
2542static int persist_get_key(const char *fieldname, char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002543{
2544 unsigned int i;
2545
2546 if (persist_data == NULL) {
2547 return -1;
2548 }
2549 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2550 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2551 /* We found it! */
2552 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
2553 return 0;
2554 }
2555 }
2556
2557 return -1;
2558}
2559
Rubin Xu85c01f92014-10-13 12:49:54 +01002560static int persist_set_key(const char *fieldname, const char *value, int encrypted)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002561{
2562 unsigned int i;
2563 unsigned int num;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002564 unsigned int max_persistent_entries;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002565
2566 if (persist_data == NULL) {
2567 return -1;
2568 }
2569
Rubin Xu85c01f92014-10-13 12:49:54 +01002570 max_persistent_entries = persist_get_max_entries(encrypted);
Ken Sumrall160b4d62013-04-22 12:15:39 -07002571
2572 num = persist_data->persist_valid_entries;
2573
2574 for (i = 0; i < num; i++) {
2575 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2576 /* We found an existing entry, update it! */
2577 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
2578 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
2579 return 0;
2580 }
2581 }
2582
2583 /* We didn't find it, add it to the end, if there is room */
2584 if (persist_data->persist_valid_entries < max_persistent_entries) {
2585 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
2586 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
2587 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
2588 persist_data->persist_valid_entries++;
2589 return 0;
2590 }
2591
2592 return -1;
2593}
2594
Rubin Xu85c01f92014-10-13 12:49:54 +01002595/**
2596 * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the
2597 * sequence and its index is greater than or equal to index. Return 0 otherwise.
2598 */
2599static int match_multi_entry(const char *key, const char *field, unsigned index) {
Rubin Xu85c01f92014-10-13 12:49:54 +01002600 unsigned int field_len;
2601 unsigned int key_index;
2602 field_len = strlen(field);
2603
2604 if (index == 0) {
2605 // The first key in a multi-entry field is just the filedname itself.
2606 if (!strcmp(key, field)) {
2607 return 1;
2608 }
2609 }
2610 // Match key against "%s_%d" % (field, index)
2611 if (strlen(key) < field_len + 1 + 1) {
2612 // Need at least a '_' and a digit.
2613 return 0;
2614 }
2615 if (strncmp(key, field, field_len)) {
2616 // If the key does not begin with field, it's not a match.
2617 return 0;
2618 }
2619 if (1 != sscanf(&key[field_len],"_%d", &key_index)) {
2620 return 0;
2621 }
2622 return key_index >= index;
2623}
2624
2625/*
2626 * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all
2627 * remaining entries starting from index will be deleted.
2628 * returns PERSIST_DEL_KEY_OK if deletion succeeds,
2629 * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist,
2630 * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs.
2631 *
2632 */
2633static int persist_del_keys(const char *fieldname, unsigned index)
2634{
2635 unsigned int i;
2636 unsigned int j;
2637 unsigned int num;
2638
2639 if (persist_data == NULL) {
2640 return PERSIST_DEL_KEY_ERROR_OTHER;
2641 }
2642
2643 num = persist_data->persist_valid_entries;
2644
2645 j = 0; // points to the end of non-deleted entries.
2646 // Filter out to-be-deleted entries in place.
2647 for (i = 0; i < num; i++) {
2648 if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) {
2649 persist_data->persist_entry[j] = persist_data->persist_entry[i];
2650 j++;
2651 }
2652 }
2653
2654 if (j < num) {
2655 persist_data->persist_valid_entries = j;
2656 // Zeroise the remaining entries
2657 memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry));
2658 return PERSIST_DEL_KEY_OK;
2659 } else {
2660 // Did not find an entry matching the given fieldname
2661 return PERSIST_DEL_KEY_ERROR_NO_FIELD;
2662 }
2663}
2664
2665static int persist_count_keys(const char *fieldname)
2666{
2667 unsigned int i;
2668 unsigned int count;
2669
2670 if (persist_data == NULL) {
2671 return -1;
2672 }
2673
2674 count = 0;
2675 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2676 if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) {
2677 count++;
2678 }
2679 }
2680
2681 return count;
2682}
2683
Ken Sumrall160b4d62013-04-22 12:15:39 -07002684/* Return the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01002685int cryptfs_getfield(const char *fieldname, char *value, int len)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002686{
Paul Crowley38132a12016-02-09 09:50:32 +00002687 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08002688 SLOGE("Cannot get field when file encrypted");
2689 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07002690 }
2691
Ken Sumrall160b4d62013-04-22 12:15:39 -07002692 char temp_value[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01002693 /* CRYPTO_GETFIELD_OK is success,
2694 * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set,
2695 * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small,
2696 * CRYPTO_GETFIELD_ERROR_OTHER is any other error
Ken Sumrall160b4d62013-04-22 12:15:39 -07002697 */
Rubin Xu85c01f92014-10-13 12:49:54 +01002698 int rc = CRYPTO_GETFIELD_ERROR_OTHER;
2699 int i;
2700 char temp_field[PROPERTY_KEY_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -07002701
2702 if (persist_data == NULL) {
2703 load_persistent_data();
2704 if (persist_data == NULL) {
2705 SLOGE("Getfield error, cannot load persistent data");
2706 goto out;
2707 }
2708 }
2709
Rubin Xu85c01f92014-10-13 12:49:54 +01002710 // Read value from persistent entries. If the original value is split into multiple entries,
2711 // stitch them back together.
Ken Sumrall160b4d62013-04-22 12:15:39 -07002712 if (!persist_get_key(fieldname, temp_value)) {
Rubin Xu85c01f92014-10-13 12:49:54 +01002713 // We found it, copy it to the caller's buffer and keep going until all entries are read.
2714 if (strlcpy(value, temp_value, len) >= (unsigned) len) {
2715 // value too small
2716 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
2717 goto out;
2718 }
2719 rc = CRYPTO_GETFIELD_OK;
2720
2721 for (i = 1; /* break explicitly */; i++) {
2722 if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >=
2723 (int) sizeof(temp_field)) {
2724 // If the fieldname is very long, we stop as soon as it begins to overflow the
2725 // maximum field length. At this point we have in fact fully read out the original
2726 // value because cryptfs_setfield would not allow fields with longer names to be
2727 // written in the first place.
2728 break;
2729 }
2730 if (!persist_get_key(temp_field, temp_value)) {
2731 if (strlcat(value, temp_value, len) >= (unsigned)len) {
2732 // value too small.
2733 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
2734 goto out;
2735 }
2736 } else {
2737 // Exhaust all entries.
2738 break;
2739 }
2740 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002741 } else {
2742 /* Sadness, it's not there. Return the error */
Rubin Xu85c01f92014-10-13 12:49:54 +01002743 rc = CRYPTO_GETFIELD_ERROR_NO_FIELD;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002744 }
2745
2746out:
2747 return rc;
2748}
2749
2750/* Set the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01002751int cryptfs_setfield(const char *fieldname, const char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002752{
Paul Crowley38132a12016-02-09 09:50:32 +00002753 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08002754 SLOGE("Cannot set field when file encrypted");
2755 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07002756 }
2757
Ken Sumrall160b4d62013-04-22 12:15:39 -07002758 char encrypted_state[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01002759 /* 0 is success, negative values are error */
2760 int rc = CRYPTO_SETFIELD_ERROR_OTHER;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002761 int encrypted = 0;
Rubin Xu85c01f92014-10-13 12:49:54 +01002762 unsigned int field_id;
2763 char temp_field[PROPERTY_KEY_MAX];
2764 unsigned int num_entries;
2765 unsigned int max_keylen;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002766
2767 if (persist_data == NULL) {
2768 load_persistent_data();
2769 if (persist_data == NULL) {
2770 SLOGE("Setfield error, cannot load persistent data");
2771 goto out;
2772 }
2773 }
2774
2775 property_get("ro.crypto.state", encrypted_state, "");
2776 if (!strcmp(encrypted_state, "encrypted") ) {
2777 encrypted = 1;
2778 }
2779
Rubin Xu85c01f92014-10-13 12:49:54 +01002780 // Compute the number of entries required to store value, each entry can store up to
2781 // (PROPERTY_VALUE_MAX - 1) chars
2782 if (strlen(value) == 0) {
2783 // Empty value also needs one entry to store.
2784 num_entries = 1;
2785 } else {
2786 num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1);
2787 }
2788
2789 max_keylen = strlen(fieldname);
2790 if (num_entries > 1) {
2791 // Need an extra "_%d" suffix.
2792 max_keylen += 1 + log10(num_entries);
2793 }
2794 if (max_keylen > PROPERTY_KEY_MAX - 1) {
2795 rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002796 goto out;
2797 }
2798
Rubin Xu85c01f92014-10-13 12:49:54 +01002799 // Make sure we have enough space to write the new value
2800 if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) >
2801 persist_get_max_entries(encrypted)) {
2802 rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG;
2803 goto out;
2804 }
2805
2806 // Now that we know persist_data has enough space for value, let's delete the old field first
2807 // to make up space.
2808 persist_del_keys(fieldname, 0);
2809
2810 if (persist_set_key(fieldname, value, encrypted)) {
2811 // fail to set key, should not happen as we have already checked the available space
2812 SLOGE("persist_set_key() error during setfield()");
2813 goto out;
2814 }
2815
2816 for (field_id = 1; field_id < num_entries; field_id++) {
2817 snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, field_id);
2818
2819 if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), encrypted)) {
2820 // fail to set key, should not happen as we have already checked the available space.
2821 SLOGE("persist_set_key() error during setfield()");
2822 goto out;
2823 }
2824 }
2825
Ken Sumrall160b4d62013-04-22 12:15:39 -07002826 /* If we are running encrypted, save the persistent data now */
2827 if (encrypted) {
2828 if (save_persistent_data()) {
2829 SLOGE("Setfield error, cannot save persistent data");
2830 goto out;
2831 }
2832 }
2833
Rubin Xu85c01f92014-10-13 12:49:54 +01002834 rc = CRYPTO_SETFIELD_OK;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002835
2836out:
2837 return rc;
2838}
Paul Lawrencef4faa572014-01-29 13:31:03 -08002839
2840/* Checks userdata. Attempt to mount the volume if default-
2841 * encrypted.
2842 * On success trigger next init phase and return 0.
2843 * Currently do not handle failure - see TODO below.
2844 */
2845int cryptfs_mount_default_encrypted(void)
2846{
Paul Lawrence84274cc2016-04-15 15:41:33 -07002847 int crypt_type = cryptfs_get_password_type();
2848 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2849 SLOGE("Bad crypt type - error");
2850 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
2851 SLOGD("Password is not default - "
2852 "starting min framework to prompt");
2853 property_set("vold.decrypt", "trigger_restart_min_framework");
2854 return 0;
2855 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
2856 SLOGD("Password is default - restarting filesystem");
2857 cryptfs_restart_internal(0);
2858 return 0;
Paul Lawrencef4faa572014-01-29 13:31:03 -08002859 } else {
Paul Lawrence84274cc2016-04-15 15:41:33 -07002860 SLOGE("Encrypted, default crypt type but can't decrypt");
Paul Lawrencef4faa572014-01-29 13:31:03 -08002861 }
2862
Paul Lawrence6bfed202014-07-28 12:47:22 -07002863 /** Corrupt. Allow us to boot into framework, which will detect bad
2864 crypto when it calls do_crypto_complete, then do a factory reset
Paul Lawrencef4faa572014-01-29 13:31:03 -08002865 */
Paul Lawrence6bfed202014-07-28 12:47:22 -07002866 property_set("vold.decrypt", "trigger_restart_min_framework");
Paul Lawrencef4faa572014-01-29 13:31:03 -08002867 return 0;
2868}
2869
2870/* Returns type of the password, default, pattern, pin or password.
2871 */
2872int cryptfs_get_password_type(void)
2873{
Paul Crowley38132a12016-02-09 09:50:32 +00002874 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002875 SLOGE("cryptfs_get_password_type not valid for file encryption");
2876 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08002877 }
2878
Paul Lawrencef4faa572014-01-29 13:31:03 -08002879 struct crypt_mnt_ftr crypt_ftr;
2880
2881 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2882 SLOGE("Error getting crypt footer and key\n");
2883 return -1;
2884 }
2885
Paul Lawrence6bfed202014-07-28 12:47:22 -07002886 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) {
2887 return -1;
2888 }
2889
Paul Lawrencef4faa572014-01-29 13:31:03 -08002890 return crypt_ftr.crypt_type;
2891}
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002892
Paul Lawrence05335c32015-03-05 09:46:23 -08002893const char* cryptfs_get_password()
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002894{
Paul Crowley38132a12016-02-09 09:50:32 +00002895 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002896 SLOGE("cryptfs_get_password not valid for file encryption");
2897 return 0;
Paul Lawrence05335c32015-03-05 09:46:23 -08002898 }
2899
Paul Lawrence399317e2014-03-10 13:20:50 -07002900 struct timespec now;
Paul Lawrenceef2b5be2014-11-11 12:47:03 -08002901 clock_gettime(CLOCK_BOOTTIME, &now);
Paul Lawrence399317e2014-03-10 13:20:50 -07002902 if (now.tv_sec < password_expiry_time) {
2903 return password;
2904 } else {
2905 cryptfs_clear_password();
2906 return 0;
2907 }
2908}
2909
2910void cryptfs_clear_password()
2911{
2912 if (password) {
2913 size_t len = strlen(password);
2914 memset(password, 0, len);
2915 free(password);
2916 password = 0;
2917 password_expiry_time = 0;
2918 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002919}
Paul Lawrence731a7a22015-04-28 22:14:15 +00002920
Paul Lawrence0c247462015-10-29 10:30:57 -07002921int cryptfs_isConvertibleToFBE()
2922{
2923 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
2924 return fs_mgr_is_convertible_to_fbe(rec) ? 1 : 0;
2925}
2926
Paul Lawrence731a7a22015-04-28 22:14:15 +00002927int cryptfs_create_default_ftr(struct crypt_mnt_ftr* crypt_ftr, __attribute__((unused))int key_length)
2928{
2929 if (cryptfs_init_crypt_mnt_ftr(crypt_ftr)) {
2930 SLOGE("Failed to initialize crypt_ftr");
2931 return -1;
2932 }
2933
2934 if (create_encrypted_random_key(DEFAULT_PASSWORD, crypt_ftr->master_key,
2935 crypt_ftr->salt, crypt_ftr)) {
2936 SLOGE("Cannot create encrypted master key\n");
2937 return -1;
2938 }
2939
2940 //crypt_ftr->keysize = key_length / 8;
2941 return 0;
2942}
2943
2944int cryptfs_get_master_key(struct crypt_mnt_ftr* ftr, const char* password,
2945 unsigned char* master_key)
2946{
2947 int rc;
2948
Paul Lawrence731a7a22015-04-28 22:14:15 +00002949 unsigned char* intermediate_key = 0;
2950 size_t intermediate_key_size = 0;
Paul Lawrencec78c71b2015-04-14 15:26:29 -07002951
2952 if (password == 0 || *password == 0) {
2953 password = DEFAULT_PASSWORD;
2954 }
2955
Paul Lawrence731a7a22015-04-28 22:14:15 +00002956 rc = decrypt_master_key(password, master_key, ftr, &intermediate_key,
2957 &intermediate_key_size);
2958
Paul Lawrence300dae72016-03-11 11:02:52 -08002959 if (rc) {
2960 SLOGE("Can't calculate intermediate key");
2961 return rc;
2962 }
2963
Paul Lawrencec78c71b2015-04-14 15:26:29 -07002964 int N = 1 << ftr->N_factor;
2965 int r = 1 << ftr->r_factor;
2966 int p = 1 << ftr->p_factor;
2967
2968 unsigned char scrypted_intermediate_key[sizeof(ftr->scrypted_intermediate_key)];
2969
2970 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
2971 ftr->salt, sizeof(ftr->salt), N, r, p,
2972 scrypted_intermediate_key,
2973 sizeof(scrypted_intermediate_key));
2974
2975 free(intermediate_key);
2976
2977 if (rc) {
Paul Lawrence300dae72016-03-11 11:02:52 -08002978 SLOGE("Can't scrypt intermediate key");
Paul Lawrencec78c71b2015-04-14 15:26:29 -07002979 return rc;
2980 }
2981
2982 return memcmp(scrypted_intermediate_key, ftr->scrypted_intermediate_key,
2983 intermediate_key_size);
Paul Lawrence731a7a22015-04-28 22:14:15 +00002984}
2985
2986int cryptfs_set_password(struct crypt_mnt_ftr* ftr, const char* password,
2987 const unsigned char* master_key)
2988{
2989 return encrypt_master_key(password, ftr->salt, master_key, ftr->master_key,
2990 ftr);
2991}
Paul Lawrence6e410592016-05-24 14:20:38 -07002992
Eric Biggersb45caaf2017-02-02 14:52:12 -08002993void cryptfs_get_file_encryption_modes(const char **contents_mode_ret,
2994 const char **filenames_mode_ret)
Paul Lawrence6e410592016-05-24 14:20:38 -07002995{
2996 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
Eric Biggersb45caaf2017-02-02 14:52:12 -08002997 fs_mgr_get_file_encryption_modes(rec, contents_mode_ret, filenames_mode_ret);
Paul Lawrence6e410592016-05-24 14:20:38 -07002998}