blob: 26069598eba691192919837d712e47309cb65577 [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
Wei Wang42e38102017-06-07 10:46:12 -07001329static void prep_data_fs(void)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001330{
Jeff Sharkey47695b22016-02-01 17:02:29 -07001331 // NOTE: post_fs_data results in init calling back around to vold, so all
1332 // callers to this method must be async
1333
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001334 /* Do the prep of the /data filesystem */
1335 property_set("vold.post_fs_data_done", "0");
1336 property_set("vold.decrypt", "trigger_post_fs_data");
Wei Wang42e38102017-06-07 10:46:12 -07001337 SLOGD("Just triggered post_fs_data");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001338
Ken Sumrallc5872692013-05-14 15:26:31 -07001339 /* Wait a max of 50 seconds, hopefully it takes much less */
Wei Wang42e38102017-06-07 10:46:12 -07001340 while (!android::base::WaitForProperty("vold.post_fs_data_done",
Wei Wang4375f1b2017-02-24 17:43:01 -08001341 "1",
Wei Wang42e38102017-06-07 10:46:12 -07001342 std::chrono::seconds(15))) {
1343 /* We timed out to prep /data in time. Continue wait. */
1344 SLOGE("waited 15s for vold.post_fs_data_done, still waiting...");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001345 }
Wei Wang42e38102017-06-07 10:46:12 -07001346 SLOGD("post_fs_data done");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001347}
1348
Paul Lawrence74f29f12014-08-28 15:54:10 -07001349static void cryptfs_set_corrupt()
1350{
1351 // Mark the footer as bad
1352 struct crypt_mnt_ftr crypt_ftr;
1353 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1354 SLOGE("Failed to get crypto footer - panic");
1355 return;
1356 }
1357
1358 crypt_ftr.flags |= CRYPT_DATA_CORRUPT;
1359 if (put_crypt_ftr_and_key(&crypt_ftr)) {
1360 SLOGE("Failed to set crypto footer - panic");
1361 return;
1362 }
1363}
1364
1365static void cryptfs_trigger_restart_min_framework()
1366{
1367 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
1368 SLOGE("Failed to mount tmpfs on data - panic");
1369 return;
1370 }
1371
1372 if (property_set("vold.decrypt", "trigger_post_fs_data")) {
1373 SLOGE("Failed to trigger post fs data - panic");
1374 return;
1375 }
1376
1377 if (property_set("vold.decrypt", "trigger_restart_min_framework")) {
1378 SLOGE("Failed to trigger restart min framework - panic");
1379 return;
1380 }
1381}
1382
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001383/* returns < 0 on failure */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001384static int cryptfs_restart_internal(int restart_main)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001385{
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001386 char crypto_blkdev[MAXPATHLEN];
Tim Murray8439dc92014-12-15 11:56:11 -08001387 int rc = -1;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001388 static int restart_successful = 0;
1389
1390 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -06001391 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001392 SLOGE("Encrypted filesystem not validated, aborting");
1393 return -1;
1394 }
1395
1396 if (restart_successful) {
1397 SLOGE("System already restarted with encrypted disk, aborting");
1398 return -1;
1399 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001400
Paul Lawrencef4faa572014-01-29 13:31:03 -08001401 if (restart_main) {
1402 /* Here is where we shut down the framework. The init scripts
1403 * start all services in one of three classes: core, main or late_start.
1404 * On boot, we start core and main. Now, we stop main, but not core,
1405 * as core includes vold and a few other really important things that
1406 * we need to keep running. Once main has stopped, we should be able
1407 * to umount the tmpfs /data, then mount the encrypted /data.
1408 * We then restart the class main, and also the class late_start.
1409 * At the moment, I've only put a few things in late_start that I know
1410 * are not needed to bring up the framework, and that also cause problems
1411 * with unmounting the tmpfs /data, but I hope to add add more services
1412 * to the late_start class as we optimize this to decrease the delay
1413 * till the user is asked for the password to the filesystem.
1414 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001415
Paul Lawrencef4faa572014-01-29 13:31:03 -08001416 /* The init files are setup to stop the class main when vold.decrypt is
1417 * set to trigger_reset_main.
1418 */
1419 property_set("vold.decrypt", "trigger_reset_main");
1420 SLOGD("Just asked init to shut down class main\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001421
Paul Lawrencef4faa572014-01-29 13:31:03 -08001422 /* Ugh, shutting down the framework is not synchronous, so until it
1423 * can be fixed, this horrible hack will wait a moment for it all to
1424 * shut down before proceeding. Without it, some devices cannot
1425 * restart the graphics services.
1426 */
1427 sleep(2);
1428 }
Ken Sumrall9dedfd42012-10-09 14:16:59 -07001429
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001430 /* Now that the framework is shutdown, we should be able to umount()
1431 * the tmpfs filesystem, and mount the real one.
1432 */
1433
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001434 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1435 if (strlen(crypto_blkdev) == 0) {
1436 SLOGE("fs_crypto_blkdev not set\n");
1437 return -1;
1438 }
1439
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001440 if (! (rc = wait_and_unmount(DATA_MNT_POINT, true)) ) {
Doug Zongker6fd57712013-12-17 09:43:23 -08001441 /* If ro.crypto.readonly is set to 1, mount the decrypted
1442 * filesystem readonly. This is used when /data is mounted by
1443 * recovery mode.
1444 */
1445 char ro_prop[PROPERTY_VALUE_MAX];
1446 property_get("ro.crypto.readonly", ro_prop, "");
1447 if (strlen(ro_prop) > 0 && atoi(ro_prop)) {
1448 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
1449 rec->flags |= MS_RDONLY;
1450 }
JP Abgrall62c7af32014-06-16 13:01:23 -07001451
Ken Sumralle5032c42012-04-01 23:58:44 -07001452 /* If that succeeded, then mount the decrypted filesystem */
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001453 int retries = RETRY_MOUNT_ATTEMPTS;
1454 int mount_rc;
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001455
1456 /*
1457 * fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
1458 * partitions in the fsck domain.
1459 */
1460 if (setexeccon(secontextFsck())){
1461 SLOGE("Failed to setexeccon");
1462 return -1;
1463 }
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001464 while ((mount_rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT,
1465 crypto_blkdev, 0))
1466 != 0) {
1467 if (mount_rc == FS_MGR_DOMNT_BUSY) {
1468 /* TODO: invoke something similar to
1469 Process::killProcessWithOpenFiles(DATA_MNT_POINT,
1470 retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */
1471 SLOGI("Failed to mount %s because it is busy - waiting",
1472 crypto_blkdev);
1473 if (--retries) {
1474 sleep(RETRY_MOUNT_DELAY_SECONDS);
1475 } else {
1476 /* Let's hope that a reboot clears away whatever is keeping
1477 the mount busy */
Josh Gaofec44372017-08-28 13:22:55 -07001478 cryptfs_reboot(RebootType::reboot);
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001479 }
1480 } else {
1481 SLOGE("Failed to mount decrypted data");
1482 cryptfs_set_corrupt();
1483 cryptfs_trigger_restart_min_framework();
1484 SLOGI("Started framework to offer wipe");
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001485 if (setexeccon(NULL)) {
1486 SLOGE("Failed to setexeccon");
1487 }
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001488 return -1;
1489 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001490 }
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001491 if (setexeccon(NULL)) {
1492 SLOGE("Failed to setexeccon");
1493 return -1;
1494 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001495
Ken Sumralle5032c42012-04-01 23:58:44 -07001496 /* Create necessary paths on /data */
Wei Wang42e38102017-06-07 10:46:12 -07001497 prep_data_fs();
Seigo Nonakae2ef0c02016-06-20 17:05:40 +09001498 property_set("vold.decrypt", "trigger_load_persist_props");
Ken Sumralle5032c42012-04-01 23:58:44 -07001499
1500 /* startup service classes main and late_start */
1501 property_set("vold.decrypt", "trigger_restart_framework");
1502 SLOGD("Just triggered restart_framework\n");
1503
1504 /* Give it a few moments to get started */
1505 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001506 }
1507
Ken Sumrall0cc16632011-01-18 20:32:26 -08001508 if (rc == 0) {
1509 restart_successful = 1;
1510 }
1511
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001512 return rc;
1513}
1514
Paul Lawrencef4faa572014-01-29 13:31:03 -08001515int cryptfs_restart(void)
1516{
Paul Lawrence05335c32015-03-05 09:46:23 -08001517 SLOGI("cryptfs_restart");
Paul Crowley38132a12016-02-09 09:50:32 +00001518 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001519 SLOGE("cryptfs_restart not valid for file encryption:");
1520 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08001521 }
1522
Paul Lawrencef4faa572014-01-29 13:31:03 -08001523 /* Call internal implementation forcing a restart of main service group */
1524 return cryptfs_restart_internal(1);
1525}
1526
Wei Wang4375f1b2017-02-24 17:43:01 -08001527static int do_crypto_complete(const char *mount_point)
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001528{
1529 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001530 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -08001531 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001532
1533 property_get("ro.crypto.state", encrypted_state, "");
1534 if (strcmp(encrypted_state, "encrypted") ) {
1535 SLOGE("not running with encryption, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001536 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001537 }
1538
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001539 // crypto_complete is full disk encrypted status
Paul Crowley38132a12016-02-09 09:50:32 +00001540 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001541 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Paul Lawrence05335c32015-03-05 09:46:23 -08001542 }
1543
Ken Sumrall160b4d62013-04-22 12:15:39 -07001544 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001545 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumralle5032c42012-04-01 23:58:44 -07001546
Ken Sumralle1a45852011-12-14 21:24:27 -08001547 /*
1548 * Only report this error if key_loc is a file and it exists.
1549 * If the device was never encrypted, and /data is not mountable for
1550 * some reason, returning 1 should prevent the UI from presenting the
1551 * a "enter password" screen, or worse, a "press button to wipe the
1552 * device" screen.
1553 */
1554 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
1555 SLOGE("master key file does not exist, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001556 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumralle1a45852011-12-14 21:24:27 -08001557 } else {
1558 SLOGE("Error getting crypt footer and key\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001559 return CRYPTO_COMPLETE_BAD_METADATA;
Ken Sumralle1a45852011-12-14 21:24:27 -08001560 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001561 }
1562
Paul Lawrence74f29f12014-08-28 15:54:10 -07001563 // Test for possible error flags
1564 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS){
1565 SLOGE("Encryption process is partway completed\n");
1566 return CRYPTO_COMPLETE_PARTIAL;
1567 }
1568
1569 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE){
1570 SLOGE("Encryption process was interrupted but cannot continue\n");
1571 return CRYPTO_COMPLETE_INCONSISTENT;
1572 }
1573
1574 if (crypt_ftr.flags & CRYPT_DATA_CORRUPT){
1575 SLOGE("Encryption is successful but data is corrupt\n");
1576 return CRYPTO_COMPLETE_CORRUPT;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001577 }
1578
1579 /* We passed the test! We shall diminish, and return to the west */
Paul Lawrence74f29f12014-08-28 15:54:10 -07001580 return CRYPTO_COMPLETE_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001581}
1582
Paul Lawrencef4faa572014-01-29 13:31:03 -08001583static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
Wei Wang4375f1b2017-02-24 17:43:01 -08001584 const char *passwd, const char *mount_point, const char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001585{
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001586 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001587 unsigned char decrypted_master_key[32];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001588 char crypto_blkdev[MAXPATHLEN];
1589 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001590 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001591 unsigned int orig_failed_decrypt_count;
1592 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001593 int use_keymaster = 0;
1594 int upgrade = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001595 unsigned char* intermediate_key = 0;
1596 size_t intermediate_key_size = 0;
Wei Wang4375f1b2017-02-24 17:43:01 -08001597 int N = 1 << crypt_ftr->N_factor;
1598 int r = 1 << crypt_ftr->r_factor;
1599 int p = 1 << crypt_ftr->p_factor;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001600
Paul Lawrencef4faa572014-01-29 13:31:03 -08001601 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1602 orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001603
Paul Lawrencef4faa572014-01-29 13:31:03 -08001604 if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001605 if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr,
1606 &intermediate_key, &intermediate_key_size)) {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001607 SLOGE("Failed to decrypt master key\n");
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001608 rc = -1;
1609 goto errout;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001610 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001611 }
1612
Paul Lawrencef4faa572014-01-29 13:31:03 -08001613 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
1614
Paul Lawrence74f29f12014-08-28 15:54:10 -07001615 // Create crypto block device - all (non fatal) code paths
1616 // need it
Paul Lawrencef4faa572014-01-29 13:31:03 -08001617 if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key,
1618 real_blkdev, crypto_blkdev, label)) {
Paul Lawrence74f29f12014-08-28 15:54:10 -07001619 SLOGE("Error creating decrypted block device\n");
1620 rc = -1;
1621 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001622 }
1623
Paul Lawrence74f29f12014-08-28 15:54:10 -07001624 /* Work out if the problem is the password or the data */
1625 unsigned char scrypted_intermediate_key[sizeof(crypt_ftr->
1626 scrypted_intermediate_key)];
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001627
Paul Lawrence74f29f12014-08-28 15:54:10 -07001628 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
1629 crypt_ftr->salt, sizeof(crypt_ftr->salt),
1630 N, r, p, scrypted_intermediate_key,
1631 sizeof(scrypted_intermediate_key));
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001632
Paul Lawrence74f29f12014-08-28 15:54:10 -07001633 // Does the key match the crypto footer?
1634 if (rc == 0 && memcmp(scrypted_intermediate_key,
1635 crypt_ftr->scrypted_intermediate_key,
1636 sizeof(scrypted_intermediate_key)) == 0) {
1637 SLOGI("Password matches");
1638 rc = 0;
1639 } else {
1640 /* Try mounting the file system anyway, just in case the problem's with
1641 * the footer, not the key. */
George Burgess IV605d7ae2016-02-29 13:39:17 -08001642 snprintf(tmp_mount_point, sizeof(tmp_mount_point), "%s/tmp_mnt",
1643 mount_point);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001644 mkdir(tmp_mount_point, 0755);
1645 if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
1646 SLOGE("Error temp mounting decrypted block device\n");
1647 delete_crypto_blk_dev(label);
1648
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001649 rc = ++crypt_ftr->failed_decrypt_count;
1650 put_crypt_ftr_and_key(crypt_ftr);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001651 } else {
1652 /* Success! */
1653 SLOGI("Password did not match but decrypted drive mounted - continue");
1654 umount(tmp_mount_point);
1655 rc = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001656 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001657 }
1658
1659 if (rc == 0) {
1660 crypt_ftr->failed_decrypt_count = 0;
Paul Lawrence72b8b822014-10-05 12:57:37 -07001661 if (orig_failed_decrypt_count != 0) {
1662 put_crypt_ftr_and_key(crypt_ftr);
1663 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001664
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001665 /* Save the name of the crypto block device
Paul Lawrence74f29f12014-08-28 15:54:10 -07001666 * so we can mount it when restarting the framework. */
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001667 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -06001668
1669 /* Also save a the master key so we can reencrypted the key
Paul Lawrence74f29f12014-08-28 15:54:10 -07001670 * the key when we want to change the password on it. */
Jason parks70a4b3f2011-01-28 10:10:47 -06001671 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001672 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -06001673 master_key_saved = 1;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001674 SLOGD("%s(): Master key saved\n", __FUNCTION__);
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001675 rc = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001676
Paul Lawrence74f29f12014-08-28 15:54:10 -07001677 // Upgrade if we're not using the latest KDF.
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001678 use_keymaster = keymaster_check_compatibility();
1679 if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Shawn Willden47ba10d2014-09-03 17:07:06 -06001680 // Don't allow downgrade
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001681 } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) {
1682 crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1683 upgrade = 1;
1684 } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001685 crypt_ftr->kdf_type = KDF_SCRYPT;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001686 upgrade = 1;
1687 }
1688
1689 if (upgrade) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001690 rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1691 crypt_ftr->master_key, crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001692 if (!rc) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001693 rc = put_crypt_ftr_and_key(crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001694 }
1695 SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
Paul Lawrenceb2f682b2014-09-08 11:28:19 -07001696
1697 // Do not fail even if upgrade failed - machine is bootable
1698 // Note that if this code is ever hit, there is a *serious* problem
1699 // since KDFs should never fail. You *must* fix the kdf before
1700 // proceeding!
1701 if (rc) {
1702 SLOGW("Upgrade failed with error %d,"
1703 " but continuing with previous state",
1704 rc);
1705 rc = 0;
1706 }
JP Abgrall7bdfa522013-11-15 13:42:56 -08001707 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001708 }
1709
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001710 errout:
1711 if (intermediate_key) {
1712 memset(intermediate_key, 0, intermediate_key_size);
1713 free(intermediate_key);
1714 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001715 return rc;
1716}
1717
Ken Sumrall29d8da82011-05-18 17:20:07 -07001718/*
Jeff Sharkey9c484982015-03-31 10:35:33 -07001719 * Called by vold when it's asked to mount an encrypted external
1720 * storage volume. The incoming partition has no crypto header/footer,
1721 * as any metadata is been stored in a separate, small partition.
1722 *
1723 * out_crypto_blkdev must be MAXPATHLEN.
Ken Sumrall29d8da82011-05-18 17:20:07 -07001724 */
Jeff Sharkey9c484982015-03-31 10:35:33 -07001725int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev,
1726 const unsigned char* key, int keysize, char* out_crypto_blkdev) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001727 int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001728 if (fd == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001729 SLOGE("Failed to open %s: %s", real_blkdev, strerror(errno));
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001730 return -1;
1731 }
1732
1733 unsigned long nr_sec = 0;
1734 get_blkdev_size(fd, &nr_sec);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001735 close(fd);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001736
Ken Sumrall29d8da82011-05-18 17:20:07 -07001737 if (nr_sec == 0) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001738 SLOGE("Failed to get size of %s: %s", real_blkdev, strerror(errno));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001739 return -1;
1740 }
1741
Jeff Sharkey9c484982015-03-31 10:35:33 -07001742 struct crypt_mnt_ftr ext_crypt_ftr;
1743 memset(&ext_crypt_ftr, 0, sizeof(ext_crypt_ftr));
1744 ext_crypt_ftr.fs_size = nr_sec;
1745 ext_crypt_ftr.keysize = keysize;
Jeff Sharkey32ebb732017-03-27 16:18:50 -06001746 strlcpy((char*) ext_crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256",
1747 MAX_CRYPTO_TYPE_NAME_LEN);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001748
Jeff Sharkey9c484982015-03-31 10:35:33 -07001749 return create_crypto_blk_dev(&ext_crypt_ftr, key, real_blkdev,
1750 out_crypto_blkdev, label);
1751}
Ken Sumrall29d8da82011-05-18 17:20:07 -07001752
Jeff Sharkey9c484982015-03-31 10:35:33 -07001753/*
1754 * Called by vold when it's asked to unmount an encrypted external
1755 * storage volume.
1756 */
1757int cryptfs_revert_ext_volume(const char* label) {
1758 return delete_crypto_blk_dev((char*) label);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001759}
1760
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001761int cryptfs_crypto_complete(void)
1762{
1763 return do_crypto_complete("/data");
1764}
1765
Paul Lawrencef4faa572014-01-29 13:31:03 -08001766int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr)
1767{
1768 char encrypted_state[PROPERTY_VALUE_MAX];
1769 property_get("ro.crypto.state", encrypted_state, "");
1770 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
1771 SLOGE("encrypted fs already validated or not running with encryption,"
1772 " aborting");
1773 return -1;
1774 }
1775
1776 if (get_crypt_ftr_and_key(crypt_ftr)) {
1777 SLOGE("Error getting crypt footer and key");
1778 return -1;
1779 }
1780
1781 return 0;
1782}
1783
Wei Wang4375f1b2017-02-24 17:43:01 -08001784int cryptfs_check_passwd(const char *passwd)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001785{
Paul Lawrence05335c32015-03-05 09:46:23 -08001786 SLOGI("cryptfs_check_passwd");
Paul Crowley38132a12016-02-09 09:50:32 +00001787 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001788 SLOGE("cryptfs_check_passwd not valid for file encryption");
1789 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08001790 }
1791
Paul Lawrencef4faa572014-01-29 13:31:03 -08001792 struct crypt_mnt_ftr crypt_ftr;
1793 int rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001794
Paul Lawrencef4faa572014-01-29 13:31:03 -08001795 rc = check_unmounted_and_get_ftr(&crypt_ftr);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001796 if (rc) {
1797 SLOGE("Could not get footer");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001798 return rc;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001799 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001800
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001801 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001802 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
1803 if (rc) {
1804 SLOGE("Password did not match");
1805 return rc;
1806 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08001807
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001808 if (crypt_ftr.flags & CRYPT_FORCE_COMPLETE) {
1809 // Here we have a default actual password but a real password
1810 // we must test against the scrypted value
1811 // First, we must delete the crypto block device that
1812 // test_mount_encrypted_fs leaves behind as a side effect
1813 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
1814 rc = test_mount_encrypted_fs(&crypt_ftr, DEFAULT_PASSWORD,
1815 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
1816 if (rc) {
1817 SLOGE("Default password did not match on reboot encryption");
1818 return rc;
1819 }
1820
1821 crypt_ftr.flags &= ~CRYPT_FORCE_COMPLETE;
1822 put_crypt_ftr_and_key(&crypt_ftr);
1823 rc = cryptfs_changepw(crypt_ftr.crypt_type, passwd);
1824 if (rc) {
1825 SLOGE("Could not change password on reboot encryption");
1826 return rc;
1827 }
1828 }
1829
1830 if (crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
Paul Lawrence399317e2014-03-10 13:20:50 -07001831 cryptfs_clear_password();
1832 password = strdup(passwd);
1833 struct timespec now;
1834 clock_gettime(CLOCK_BOOTTIME, &now);
1835 password_expiry_time = now.tv_sec + password_max_age_seconds;
Paul Lawrence684dbdf2014-02-07 12:07:22 -08001836 }
1837
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001838 return rc;
1839}
1840
Ken Sumrall3ad90722011-10-04 20:38:29 -07001841int cryptfs_verify_passwd(char *passwd)
1842{
1843 struct crypt_mnt_ftr crypt_ftr;
1844 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001845 unsigned char decrypted_master_key[32];
Ken Sumrall3ad90722011-10-04 20:38:29 -07001846 char encrypted_state[PROPERTY_VALUE_MAX];
1847 int rc;
1848
1849 property_get("ro.crypto.state", encrypted_state, "");
1850 if (strcmp(encrypted_state, "encrypted") ) {
1851 SLOGE("device not encrypted, aborting");
1852 return -2;
1853 }
1854
1855 if (!master_key_saved) {
1856 SLOGE("encrypted fs not yet mounted, aborting");
1857 return -1;
1858 }
1859
1860 if (!saved_mount_point) {
1861 SLOGE("encrypted fs failed to save mount point, aborting");
1862 return -1;
1863 }
1864
Ken Sumrall160b4d62013-04-22 12:15:39 -07001865 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall3ad90722011-10-04 20:38:29 -07001866 SLOGE("Error getting crypt footer and key\n");
1867 return -1;
1868 }
1869
1870 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
1871 /* If the device has no password, then just say the password is valid */
1872 rc = 0;
1873 } else {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001874 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001875 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
1876 /* They match, the password is correct */
1877 rc = 0;
1878 } else {
1879 /* If incorrect, sleep for a bit to prevent dictionary attacks */
1880 sleep(1);
1881 rc = 1;
1882 }
1883 }
1884
1885 return rc;
1886}
1887
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001888/* Initialize a crypt_mnt_ftr structure. The keysize is
1889 * defaulted to 16 bytes, and the filesystem size to 0.
1890 * Presumably, at a minimum, the caller will update the
1891 * filesystem size and crypto_type_name after calling this function.
1892 */
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001893static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001894{
Ken Sumrall160b4d62013-04-22 12:15:39 -07001895 off64_t off;
1896
1897 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001898 ftr->magic = CRYPT_MNT_MAGIC;
Kenny Rootc96a5f82013-06-14 12:08:28 -07001899 ftr->major_version = CURRENT_MAJOR_VERSION;
1900 ftr->minor_version = CURRENT_MINOR_VERSION;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001901 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Jason parks70a4b3f2011-01-28 10:10:47 -06001902 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall160b4d62013-04-22 12:15:39 -07001903
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001904 switch (keymaster_check_compatibility()) {
1905 case 1:
1906 ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1907 break;
1908
1909 case 0:
1910 ftr->kdf_type = KDF_SCRYPT;
1911 break;
1912
1913 default:
1914 SLOGE("keymaster_check_compatibility failed");
1915 return -1;
1916 }
1917
Kenny Rootc4c70f12013-06-14 12:11:38 -07001918 get_device_scrypt_params(ftr);
1919
Ken Sumrall160b4d62013-04-22 12:15:39 -07001920 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
1921 if (get_crypt_ftr_info(NULL, &off) == 0) {
1922 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
1923 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
1924 ftr->persist_data_size;
1925 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001926
1927 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001928}
1929
Ken Sumrall29d8da82011-05-18 17:20:07 -07001930static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001931{
Ken Sumralle550f782013-08-20 13:48:23 -07001932 const char *args[10];
1933 char size_str[32]; /* Must be large enough to hold a %lld and null byte */
1934 int num_args;
1935 int status;
1936 int tmp;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001937 int rc = -1;
1938
Ken Sumrall29d8da82011-05-18 17:20:07 -07001939 if (type == EXT4_FS) {
Adrien Schildknechte0f409c2016-11-21 15:14:37 -08001940#ifdef TARGET_USES_MKE2FS
1941 args[0] = "/system/bin/mke2fs";
1942 args[1] = "-M";
1943 args[2] = "/data";
1944 args[3] = "-b";
1945 args[4] = "4096";
1946 args[5] = "-t";
1947 args[6] = "ext4";
1948 args[7] = crypto_blkdev;
1949 snprintf(size_str, sizeof(size_str), "%" PRId64, size / (4096 / 512));
1950 args[8] = size_str;
1951 num_args = 9;
1952#else
Ken Sumralle550f782013-08-20 13:48:23 -07001953 args[0] = "/system/bin/make_ext4fs";
1954 args[1] = "-a";
1955 args[2] = "/data";
1956 args[3] = "-l";
Elliott Hughes73737162014-06-25 17:27:42 -07001957 snprintf(size_str, sizeof(size_str), "%" PRId64, size * 512);
Ken Sumralle550f782013-08-20 13:48:23 -07001958 args[4] = size_str;
1959 args[5] = crypto_blkdev;
1960 num_args = 6;
Adrien Schildknechte0f409c2016-11-21 15:14:37 -08001961#endif
Ken Sumralle550f782013-08-20 13:48:23 -07001962 SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n",
1963 args[0], args[1], args[2], args[3], args[4], args[5]);
JP Abgrall62c7af32014-06-16 13:01:23 -07001964 } else if (type == F2FS_FS) {
Jaegeuk Kim98651a22017-06-05 10:22:04 -07001965 args[0] = "/system/bin/make_f2fs";
Jaegeuk Kim8de9f062017-11-01 10:35:30 -07001966 args[1] = "-f";
JP Abgrall62c7af32014-06-16 13:01:23 -07001967 args[2] = "-d1";
Jaegeuk Kim8de9f062017-11-01 10:35:30 -07001968 args[3] = "-O";
1969 args[4] = "encrypt";
1970 args[5] = "-O";
1971 args[6] = "quota";
1972 args[7] = crypto_blkdev;
Elliott Hughes73737162014-06-25 17:27:42 -07001973 snprintf(size_str, sizeof(size_str), "%" PRId64, size);
Jaegeuk Kim8de9f062017-11-01 10:35:30 -07001974 args[8] = size_str;
1975 num_args = 9;
1976 SLOGI("Making empty filesystem with command %s %s %s %s %s %s %s %s %s\n",
1977 args[0], args[1], args[2], args[3], args[4], args[5],
1978 args[6], args[7], args[8]);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001979 } else {
1980 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
1981 return -1;
1982 }
1983
Ken Sumralle550f782013-08-20 13:48:23 -07001984 tmp = android_fork_execvp(num_args, (char **)args, &status, false, true);
1985
1986 if (tmp != 0) {
1987 SLOGE("Error creating empty filesystem on %s due to logwrap error\n", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001988 } else {
Ken Sumralle550f782013-08-20 13:48:23 -07001989 if (WIFEXITED(status)) {
1990 if (WEXITSTATUS(status)) {
1991 SLOGE("Error creating filesystem on %s, exit status %d ",
1992 crypto_blkdev, WEXITSTATUS(status));
1993 } else {
1994 SLOGD("Successfully created filesystem on %s\n", crypto_blkdev);
1995 rc = 0;
1996 }
1997 } else {
1998 SLOGE("Error creating filesystem on %s, did not exit normally\n", crypto_blkdev);
1999 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002000 }
2001
2002 return rc;
2003}
2004
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002005#define CRYPTO_ENABLE_WIPE 1
2006#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002007
2008#define FRAMEWORK_BOOT_WAIT 60
2009
Paul Lawrence87999172014-02-20 12:21:31 -08002010static int cryptfs_SHA256_fileblock(const char* filename, __le8* buf)
2011{
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002012 int fd = open(filename, O_RDONLY|O_CLOEXEC);
Paul Lawrence87999172014-02-20 12:21:31 -08002013 if (fd == -1) {
2014 SLOGE("Error opening file %s", filename);
2015 return -1;
2016 }
2017
2018 char block[CRYPT_INPLACE_BUFSIZE];
2019 memset(block, 0, sizeof(block));
2020 if (unix_read(fd, block, sizeof(block)) < 0) {
2021 SLOGE("Error reading file %s", filename);
2022 close(fd);
2023 return -1;
2024 }
2025
2026 close(fd);
2027
2028 SHA256_CTX c;
2029 SHA256_Init(&c);
2030 SHA256_Update(&c, block, sizeof(block));
2031 SHA256_Final(buf, &c);
2032
2033 return 0;
2034}
2035
JP Abgrall62c7af32014-06-16 13:01:23 -07002036static int get_fs_type(struct fstab_rec *rec)
2037{
2038 if (!strcmp(rec->fs_type, "ext4")) {
2039 return EXT4_FS;
2040 } else if (!strcmp(rec->fs_type, "f2fs")) {
2041 return F2FS_FS;
2042 } else {
2043 return -1;
2044 }
2045}
2046
Paul Lawrence87999172014-02-20 12:21:31 -08002047static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr *crypt_ftr, int how,
2048 char *crypto_blkdev, char *real_blkdev,
2049 int previously_encrypted_upto)
2050{
2051 off64_t cur_encryption_done=0, tot_encryption_size=0;
Tim Murray8439dc92014-12-15 11:56:11 -08002052 int rc = -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002053
Paul Lawrence87999172014-02-20 12:21:31 -08002054 /* The size of the userdata partition, and add in the vold volumes below */
2055 tot_encryption_size = crypt_ftr->fs_size;
2056
2057 if (how == CRYPTO_ENABLE_WIPE) {
JP Abgrall62c7af32014-06-16 13:01:23 -07002058 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
2059 int fs_type = get_fs_type(rec);
2060 if (fs_type < 0) {
2061 SLOGE("cryptfs_enable: unsupported fs type %s\n", rec->fs_type);
2062 return -1;
2063 }
2064 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr->fs_size, fs_type);
Paul Lawrence87999172014-02-20 12:21:31 -08002065 } else if (how == CRYPTO_ENABLE_INPLACE) {
2066 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev,
2067 crypt_ftr->fs_size, &cur_encryption_done,
2068 tot_encryption_size,
2069 previously_encrypted_upto);
2070
JP Abgrall7fc1de82014-10-10 18:43:41 -07002071 if (rc == ENABLE_INPLACE_ERR_DEV) {
2072 /* Hack for b/17898962 */
2073 SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n");
Josh Gaofec44372017-08-28 13:22:55 -07002074 cryptfs_reboot(RebootType::reboot);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002075 }
2076
Paul Lawrence73d7a022014-06-09 14:10:09 -07002077 if (!rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002078 crypt_ftr->encrypted_upto = cur_encryption_done;
2079 }
2080
Paul Lawrence73d7a022014-06-09 14:10:09 -07002081 if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002082 /* The inplace routine never actually sets the progress to 100% due
2083 * to the round down nature of integer division, so set it here */
2084 property_set("vold.encrypt_progress", "100");
2085 }
2086 } else {
2087 /* Shouldn't happen */
2088 SLOGE("cryptfs_enable: internal error, unknown option\n");
2089 rc = -1;
2090 }
2091
2092 return rc;
2093}
2094
Wei Wang4375f1b2017-02-24 17:43:01 -08002095int cryptfs_enable_internal(char *howarg, int crypt_type, const char *passwd,
Paul Lawrence569649f2015-09-09 12:13:00 -07002096 int no_ui)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002097{
2098 int how = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002099 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN];
Ken Sumrall160b4d62013-04-22 12:15:39 -07002100 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002101 int rc=-1, i;
Paul Lawrence87999172014-02-20 12:21:31 -08002102 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002103 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002104 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002105 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07002106 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall29d8da82011-05-18 17:20:07 -07002107 int num_vols;
Paul Lawrence87999172014-02-20 12:21:31 -08002108 off64_t previously_encrypted_upto = 0;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002109 bool rebootEncryption = false;
Wei Wang4375f1b2017-02-24 17:43:01 -08002110 bool onlyCreateHeader = false;
2111 int fd = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002112
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002113 if (!strcmp(howarg, "wipe")) {
2114 how = CRYPTO_ENABLE_WIPE;
2115 } else if (! strcmp(howarg, "inplace")) {
2116 how = CRYPTO_ENABLE_INPLACE;
2117 } else {
2118 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08002119 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002120 }
2121
Paul Lawrence87999172014-02-20 12:21:31 -08002122 if (how == CRYPTO_ENABLE_INPLACE
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002123 && get_crypt_ftr_and_key(&crypt_ftr) == 0) {
2124 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
2125 /* An encryption was underway and was interrupted */
2126 previously_encrypted_upto = crypt_ftr.encrypted_upto;
2127 crypt_ftr.encrypted_upto = 0;
2128 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002129
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002130 /* At this point, we are in an inconsistent state. Until we successfully
2131 complete encryption, a reboot will leave us broken. So mark the
2132 encryption failed in case that happens.
2133 On successfully completing encryption, remove this flag */
2134 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002135
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002136 put_crypt_ftr_and_key(&crypt_ftr);
2137 } else if (crypt_ftr.flags & CRYPT_FORCE_ENCRYPTION) {
2138 if (!check_ftr_sha(&crypt_ftr)) {
2139 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
2140 put_crypt_ftr_and_key(&crypt_ftr);
2141 goto error_unencrypted;
2142 }
2143
2144 /* Doing a reboot-encryption*/
2145 crypt_ftr.flags &= ~CRYPT_FORCE_ENCRYPTION;
2146 crypt_ftr.flags |= CRYPT_FORCE_COMPLETE;
2147 rebootEncryption = true;
2148 }
Paul Lawrence87999172014-02-20 12:21:31 -08002149 }
2150
2151 property_get("ro.crypto.state", encrypted_state, "");
2152 if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) {
2153 SLOGE("Device is already running encrypted, aborting");
2154 goto error_unencrypted;
2155 }
2156
2157 // TODO refactor fs_mgr_get_crypt_info to get both in one call
2158 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumrall56ad03c2013-02-13 13:00:19 -08002159 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002160
Ken Sumrall3ed82362011-01-28 23:31:16 -08002161 /* Get the size of the real block device */
Wei Wang4375f1b2017-02-24 17:43:01 -08002162 fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002163 if (fd == -1) {
2164 SLOGE("Cannot open block device %s\n", real_blkdev);
2165 goto error_unencrypted;
2166 }
2167 unsigned long nr_sec;
2168 get_blkdev_size(fd, &nr_sec);
2169 if (nr_sec == 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002170 SLOGE("Cannot get size of block device %s\n", real_blkdev);
2171 goto error_unencrypted;
2172 }
2173 close(fd);
2174
2175 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002176 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002177 unsigned int fs_size_sec, max_fs_size_sec;
Jim Millera70abc62014-08-15 02:00:45 +00002178 fs_size_sec = get_fs_size(real_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002179 if (fs_size_sec == 0)
2180 fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev);
2181
Paul Lawrence87999172014-02-20 12:21:31 -08002182 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002183
2184 if (fs_size_sec > max_fs_size_sec) {
2185 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
2186 goto error_unencrypted;
2187 }
2188 }
2189
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002190 /* Get a wakelock as this may take a while, and we don't want the
2191 * device to sleep on us. We'll grab a partial wakelock, and if the UI
2192 * wants to keep the screen on, it can grab a full wakelock.
2193 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002194 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002195 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
2196
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002197 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002198 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002199 */
2200 property_set("vold.decrypt", "trigger_shutdown_framework");
2201 SLOGD("Just asked init to shut down class main\n");
2202
Jeff Sharkey9c484982015-03-31 10:35:33 -07002203 /* Ask vold to unmount all devices that it manages */
2204 if (vold_unmountAll()) {
2205 SLOGE("Failed to unmount all vold managed devices");
Ken Sumrall2eaf7132011-01-14 12:45:48 -08002206 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002207
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002208 /* no_ui means we are being called from init, not settings.
2209 Now we always reboot from settings, so !no_ui means reboot
2210 */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002211 if (!no_ui) {
2212 /* Try fallback, which is to reboot and try there */
2213 onlyCreateHeader = true;
2214 FILE* breadcrumb = fopen(BREADCRUMB_FILE, "we");
2215 if (breadcrumb == 0) {
2216 SLOGE("Failed to create breadcrumb file");
2217 goto error_shutting_down;
2218 }
2219 fclose(breadcrumb);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002220 }
2221
2222 /* Do extra work for a better UX when doing the long inplace encryption */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002223 if (how == CRYPTO_ENABLE_INPLACE && !onlyCreateHeader) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002224 /* Now that /data is unmounted, we need to mount a tmpfs
2225 * /data, set a property saying we're doing inplace encryption,
2226 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002227 */
Ken Sumralle5032c42012-04-01 23:58:44 -07002228 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002229 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002230 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002231 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08002232 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002233
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002234 /* restart the framework. */
2235 /* Create necessary paths on /data */
Wei Wang42e38102017-06-07 10:46:12 -07002236 prep_data_fs();
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002237
Ken Sumrall92736ef2012-10-17 20:57:14 -07002238 /* Ugh, shutting down the framework is not synchronous, so until it
2239 * can be fixed, this horrible hack will wait a moment for it all to
2240 * shut down before proceeding. Without it, some devices cannot
2241 * restart the graphics services.
2242 */
2243 sleep(2);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002244 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002245
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002246 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002247 /* Initialize a crypt_mnt_ftr for the partition */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002248 if (previously_encrypted_upto == 0 && !rebootEncryption) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002249 if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) {
2250 goto error_shutting_down;
2251 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002252
Paul Lawrence87999172014-02-20 12:21:31 -08002253 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
2254 crypt_ftr.fs_size = nr_sec
2255 - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
2256 } else {
2257 crypt_ftr.fs_size = nr_sec;
2258 }
Paul Lawrence6bfed202014-07-28 12:47:22 -07002259 /* At this point, we are in an inconsistent state. Until we successfully
2260 complete encryption, a reboot will leave us broken. So mark the
2261 encryption failed in case that happens.
2262 On successfully completing encryption, remove this flag */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002263 if (onlyCreateHeader) {
2264 crypt_ftr.flags |= CRYPT_FORCE_ENCRYPTION;
2265 } else {
2266 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
2267 }
Paul Lawrence87999172014-02-20 12:21:31 -08002268 crypt_ftr.crypt_type = crypt_type;
Ajay Dudani87701e22014-09-17 21:02:52 -07002269 strlcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256", MAX_CRYPTO_TYPE_NAME_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002270
Paul Lawrence87999172014-02-20 12:21:31 -08002271 /* Make an encrypted master key */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002272 if (create_encrypted_random_key(onlyCreateHeader ? DEFAULT_PASSWORD : passwd,
2273 crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
Paul Lawrence87999172014-02-20 12:21:31 -08002274 SLOGE("Cannot create encrypted master key\n");
2275 goto error_shutting_down;
2276 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002277
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002278 /* Replace scrypted intermediate key if we are preparing for a reboot */
2279 if (onlyCreateHeader) {
2280 unsigned char fake_master_key[KEY_LEN_BYTES];
2281 unsigned char encrypted_fake_master_key[KEY_LEN_BYTES];
2282 memset(fake_master_key, 0, sizeof(fake_master_key));
2283 encrypt_master_key(passwd, crypt_ftr.salt, fake_master_key,
2284 encrypted_fake_master_key, &crypt_ftr);
2285 }
2286
Paul Lawrence87999172014-02-20 12:21:31 -08002287 /* Write the key to the end of the partition */
2288 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002289
Paul Lawrence87999172014-02-20 12:21:31 -08002290 /* If any persistent data has been remembered, save it.
2291 * If none, create a valid empty table and save that.
2292 */
2293 if (!persist_data) {
Wei Wang4375f1b2017-02-24 17:43:01 -08002294 pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
Paul Lawrence87999172014-02-20 12:21:31 -08002295 if (pdata) {
2296 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
2297 persist_data = pdata;
2298 }
2299 }
2300 if (persist_data) {
2301 save_persistent_data();
2302 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002303 }
2304
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002305 if (onlyCreateHeader) {
2306 sleep(2);
Josh Gaofec44372017-08-28 13:22:55 -07002307 cryptfs_reboot(RebootType::reboot);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002308 }
2309
2310 if (how == CRYPTO_ENABLE_INPLACE && (!no_ui || rebootEncryption)) {
Ajay Dudani87701e22014-09-17 21:02:52 -07002311 /* startup service classes main and late_start */
2312 property_set("vold.decrypt", "trigger_restart_min_framework");
2313 SLOGD("Just triggered restart_min_framework\n");
2314
2315 /* OK, the framework is restarted and will soon be showing a
2316 * progress bar. Time to setup an encrypted mapping, and
2317 * either write a new filesystem, or encrypt in place updating
2318 * the progress bar as we work.
2319 */
2320 }
2321
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002322 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002323 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002324 CRYPTO_BLOCK_DEVICE);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002325
Paul Lawrence87999172014-02-20 12:21:31 -08002326 /* If we are continuing, check checksums match */
2327 rc = 0;
2328 if (previously_encrypted_upto) {
2329 __le8 hash_first_block[SHA256_DIGEST_LENGTH];
2330 rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block);
Ken Sumrall128626f2011-06-28 18:45:14 -07002331
Paul Lawrence87999172014-02-20 12:21:31 -08002332 if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block,
2333 sizeof(hash_first_block)) != 0) {
2334 SLOGE("Checksums do not match - trigger wipe");
2335 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002336 }
2337 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002338
Paul Lawrence87999172014-02-20 12:21:31 -08002339 if (!rc) {
2340 rc = cryptfs_enable_all_volumes(&crypt_ftr, how,
2341 crypto_blkdev, real_blkdev,
2342 previously_encrypted_upto);
2343 }
2344
2345 /* Calculate checksum if we are not finished */
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08002346 if (!rc && how == CRYPTO_ENABLE_INPLACE
2347 && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002348 rc = cryptfs_SHA256_fileblock(crypto_blkdev,
2349 crypt_ftr.hash_first_block);
Paul Lawrence73d7a022014-06-09 14:10:09 -07002350 if (rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002351 SLOGE("Error calculating checksum for continuing encryption");
2352 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002353 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002354 }
2355
2356 /* Undo the dm-crypt mapping whether we succeed or not */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002357 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002358
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002359 if (! rc) {
2360 /* Success */
Paul Lawrence6bfed202014-07-28 12:47:22 -07002361 crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08002362
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08002363 if (how == CRYPTO_ENABLE_INPLACE
2364 && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002365 SLOGD("Encrypted up to sector %lld - will continue after reboot",
2366 crypt_ftr.encrypted_upto);
Paul Lawrence6bfed202014-07-28 12:47:22 -07002367 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence87999172014-02-20 12:21:31 -08002368 }
Paul Lawrence73d7a022014-06-09 14:10:09 -07002369
Paul Lawrence6bfed202014-07-28 12:47:22 -07002370 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08002371
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08002372 if (how == CRYPTO_ENABLE_WIPE
2373 || crypt_ftr.encrypted_upto == crypt_ftr.fs_size) {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002374 char value[PROPERTY_VALUE_MAX];
2375 property_get("ro.crypto.state", value, "");
2376 if (!strcmp(value, "")) {
2377 /* default encryption - continue first boot sequence */
2378 property_set("ro.crypto.state", "encrypted");
Paul Lawrence4ed45262016-03-10 15:44:21 -08002379 property_set("ro.crypto.type", "block");
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002380 release_wake_lock(lockid);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002381 if (rebootEncryption && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
2382 // Bring up cryptkeeper that will check the password and set it
2383 property_set("vold.decrypt", "trigger_shutdown_framework");
2384 sleep(2);
2385 property_set("vold.encrypt_progress", "");
2386 cryptfs_trigger_restart_min_framework();
2387 } else {
2388 cryptfs_check_passwd(DEFAULT_PASSWORD);
2389 cryptfs_restart_internal(1);
2390 }
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002391 return 0;
2392 } else {
2393 sleep(2); /* Give the UI a chance to show 100% progress */
Josh Gaofec44372017-08-28 13:22:55 -07002394 cryptfs_reboot(RebootType::reboot);
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002395 }
Paul Lawrence87999172014-02-20 12:21:31 -08002396 } else {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002397 sleep(2); /* Partially encrypted, ensure writes flushed to ssd */
Josh Gaofec44372017-08-28 13:22:55 -07002398 cryptfs_reboot(RebootType::shutdown);
Paul Lawrence87999172014-02-20 12:21:31 -08002399 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002400 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002401 char value[PROPERTY_VALUE_MAX];
2402
Ken Sumrall319369a2012-06-27 16:30:18 -07002403 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002404 if (!strcmp(value, "1")) {
2405 /* wipe data if encryption failed */
2406 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
Wei Wang4375f1b2017-02-24 17:43:01 -08002407 std::string err;
2408 const std::vector<std::string> options = {
2409 "--wipe_data\n--reason=cryptfs_enable_internal\n"
2410 };
2411 if (!write_bootloader_message(options, &err)) {
2412 SLOGE("could not write bootloader message: %s", err.c_str());
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002413 }
Josh Gaofec44372017-08-28 13:22:55 -07002414 cryptfs_reboot(RebootType::recovery);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002415 } else {
2416 /* set property to trigger dialog */
2417 property_set("vold.encrypt_progress", "error_partially_encrypted");
2418 release_wake_lock(lockid);
2419 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002420 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002421 }
2422
Ken Sumrall3ed82362011-01-28 23:31:16 -08002423 /* hrm, the encrypt step claims success, but the reboot failed.
2424 * This should not happen.
2425 * Set the property and return. Hope the framework can deal with it.
2426 */
2427 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002428 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002429 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08002430
2431error_unencrypted:
2432 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002433 if (lockid[0]) {
2434 release_wake_lock(lockid);
2435 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002436 return -1;
2437
2438error_shutting_down:
2439 /* we failed, and have not encrypted anthing, so the users's data is still intact,
2440 * but the framework is stopped and not restarted to show the error, so it's up to
2441 * vold to restart the system.
2442 */
2443 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Josh Gaofec44372017-08-28 13:22:55 -07002444 cryptfs_reboot(RebootType::reboot);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002445
2446 /* shouldn't get here */
2447 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002448 if (lockid[0]) {
2449 release_wake_lock(lockid);
2450 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002451 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002452}
2453
Paul Lawrence569649f2015-09-09 12:13:00 -07002454int cryptfs_enable(char *howarg, int type, char *passwd, int no_ui)
Paul Lawrence13486032014-02-03 13:28:11 -08002455{
Paul Lawrence569649f2015-09-09 12:13:00 -07002456 return cryptfs_enable_internal(howarg, type, passwd, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08002457}
2458
Paul Lawrence569649f2015-09-09 12:13:00 -07002459int cryptfs_enable_default(char *howarg, int no_ui)
Paul Lawrence13486032014-02-03 13:28:11 -08002460{
2461 return cryptfs_enable_internal(howarg, CRYPT_TYPE_DEFAULT,
Paul Lawrence569649f2015-09-09 12:13:00 -07002462 DEFAULT_PASSWORD, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08002463}
2464
2465int cryptfs_changepw(int crypt_type, const char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002466{
Paul Crowley38132a12016-02-09 09:50:32 +00002467 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002468 SLOGE("cryptfs_changepw not valid for file encryption");
2469 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08002470 }
2471
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002472 struct crypt_mnt_ftr crypt_ftr;
JP Abgrall933216c2015-02-11 13:44:32 -08002473 int rc;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002474
2475 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08002476 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08002477 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002478 return -1;
2479 }
2480
Paul Lawrencef4faa572014-01-29 13:31:03 -08002481 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2482 SLOGE("Invalid crypt_type %d", crypt_type);
2483 return -1;
2484 }
2485
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002486 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002487 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08002488 SLOGE("Error getting crypt footer and key");
2489 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002490 }
2491
Paul Lawrencef4faa572014-01-29 13:31:03 -08002492 crypt_ftr.crypt_type = crypt_type;
2493
JP Abgrall933216c2015-02-11 13:44:32 -08002494 rc = encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
Paul Lawrencef4faa572014-01-29 13:31:03 -08002495 : newpw,
2496 crypt_ftr.salt,
2497 saved_master_key,
2498 crypt_ftr.master_key,
2499 &crypt_ftr);
JP Abgrall933216c2015-02-11 13:44:32 -08002500 if (rc) {
2501 SLOGE("Encrypt master key failed: %d", rc);
2502 return -1;
2503 }
Jason parks70a4b3f2011-01-28 10:10:47 -06002504 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002505 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002506
2507 return 0;
2508}
Ken Sumrall160b4d62013-04-22 12:15:39 -07002509
Rubin Xu85c01f92014-10-13 12:49:54 +01002510static unsigned int persist_get_max_entries(int encrypted) {
2511 struct crypt_mnt_ftr crypt_ftr;
2512 unsigned int dsize;
2513 unsigned int max_persistent_entries;
2514
2515 /* If encrypted, use the values from the crypt_ftr, otherwise
2516 * use the values for the current spec.
2517 */
2518 if (encrypted) {
2519 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2520 return -1;
2521 }
2522 dsize = crypt_ftr.persist_data_size;
2523 } else {
2524 dsize = CRYPT_PERSIST_DATA_SIZE;
2525 }
2526
2527 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
2528 sizeof(struct crypt_persist_entry);
2529
2530 return max_persistent_entries;
2531}
2532
2533static int persist_get_key(const char *fieldname, char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002534{
2535 unsigned int i;
2536
2537 if (persist_data == NULL) {
2538 return -1;
2539 }
2540 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2541 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2542 /* We found it! */
2543 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
2544 return 0;
2545 }
2546 }
2547
2548 return -1;
2549}
2550
Rubin Xu85c01f92014-10-13 12:49:54 +01002551static int persist_set_key(const char *fieldname, const char *value, int encrypted)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002552{
2553 unsigned int i;
2554 unsigned int num;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002555 unsigned int max_persistent_entries;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002556
2557 if (persist_data == NULL) {
2558 return -1;
2559 }
2560
Rubin Xu85c01f92014-10-13 12:49:54 +01002561 max_persistent_entries = persist_get_max_entries(encrypted);
Ken Sumrall160b4d62013-04-22 12:15:39 -07002562
2563 num = persist_data->persist_valid_entries;
2564
2565 for (i = 0; i < num; i++) {
2566 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2567 /* We found an existing entry, update it! */
2568 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
2569 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
2570 return 0;
2571 }
2572 }
2573
2574 /* We didn't find it, add it to the end, if there is room */
2575 if (persist_data->persist_valid_entries < max_persistent_entries) {
2576 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
2577 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
2578 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
2579 persist_data->persist_valid_entries++;
2580 return 0;
2581 }
2582
2583 return -1;
2584}
2585
Rubin Xu85c01f92014-10-13 12:49:54 +01002586/**
2587 * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the
2588 * sequence and its index is greater than or equal to index. Return 0 otherwise.
2589 */
2590static int match_multi_entry(const char *key, const char *field, unsigned index) {
Rubin Xu85c01f92014-10-13 12:49:54 +01002591 unsigned int field_len;
2592 unsigned int key_index;
2593 field_len = strlen(field);
2594
2595 if (index == 0) {
2596 // The first key in a multi-entry field is just the filedname itself.
2597 if (!strcmp(key, field)) {
2598 return 1;
2599 }
2600 }
2601 // Match key against "%s_%d" % (field, index)
2602 if (strlen(key) < field_len + 1 + 1) {
2603 // Need at least a '_' and a digit.
2604 return 0;
2605 }
2606 if (strncmp(key, field, field_len)) {
2607 // If the key does not begin with field, it's not a match.
2608 return 0;
2609 }
2610 if (1 != sscanf(&key[field_len],"_%d", &key_index)) {
2611 return 0;
2612 }
2613 return key_index >= index;
2614}
2615
2616/*
2617 * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all
2618 * remaining entries starting from index will be deleted.
2619 * returns PERSIST_DEL_KEY_OK if deletion succeeds,
2620 * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist,
2621 * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs.
2622 *
2623 */
2624static int persist_del_keys(const char *fieldname, unsigned index)
2625{
2626 unsigned int i;
2627 unsigned int j;
2628 unsigned int num;
2629
2630 if (persist_data == NULL) {
2631 return PERSIST_DEL_KEY_ERROR_OTHER;
2632 }
2633
2634 num = persist_data->persist_valid_entries;
2635
2636 j = 0; // points to the end of non-deleted entries.
2637 // Filter out to-be-deleted entries in place.
2638 for (i = 0; i < num; i++) {
2639 if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) {
2640 persist_data->persist_entry[j] = persist_data->persist_entry[i];
2641 j++;
2642 }
2643 }
2644
2645 if (j < num) {
2646 persist_data->persist_valid_entries = j;
2647 // Zeroise the remaining entries
2648 memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry));
2649 return PERSIST_DEL_KEY_OK;
2650 } else {
2651 // Did not find an entry matching the given fieldname
2652 return PERSIST_DEL_KEY_ERROR_NO_FIELD;
2653 }
2654}
2655
2656static int persist_count_keys(const char *fieldname)
2657{
2658 unsigned int i;
2659 unsigned int count;
2660
2661 if (persist_data == NULL) {
2662 return -1;
2663 }
2664
2665 count = 0;
2666 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2667 if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) {
2668 count++;
2669 }
2670 }
2671
2672 return count;
2673}
2674
Ken Sumrall160b4d62013-04-22 12:15:39 -07002675/* Return the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01002676int cryptfs_getfield(const char *fieldname, char *value, int len)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002677{
Paul Crowley38132a12016-02-09 09:50:32 +00002678 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08002679 SLOGE("Cannot get field when file encrypted");
2680 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07002681 }
2682
Ken Sumrall160b4d62013-04-22 12:15:39 -07002683 char temp_value[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01002684 /* CRYPTO_GETFIELD_OK is success,
2685 * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set,
2686 * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small,
2687 * CRYPTO_GETFIELD_ERROR_OTHER is any other error
Ken Sumrall160b4d62013-04-22 12:15:39 -07002688 */
Rubin Xu85c01f92014-10-13 12:49:54 +01002689 int rc = CRYPTO_GETFIELD_ERROR_OTHER;
2690 int i;
2691 char temp_field[PROPERTY_KEY_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -07002692
2693 if (persist_data == NULL) {
2694 load_persistent_data();
2695 if (persist_data == NULL) {
2696 SLOGE("Getfield error, cannot load persistent data");
2697 goto out;
2698 }
2699 }
2700
Rubin Xu85c01f92014-10-13 12:49:54 +01002701 // Read value from persistent entries. If the original value is split into multiple entries,
2702 // stitch them back together.
Ken Sumrall160b4d62013-04-22 12:15:39 -07002703 if (!persist_get_key(fieldname, temp_value)) {
Rubin Xu85c01f92014-10-13 12:49:54 +01002704 // We found it, copy it to the caller's buffer and keep going until all entries are read.
2705 if (strlcpy(value, temp_value, len) >= (unsigned) len) {
2706 // value too small
2707 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
2708 goto out;
2709 }
2710 rc = CRYPTO_GETFIELD_OK;
2711
2712 for (i = 1; /* break explicitly */; i++) {
2713 if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >=
2714 (int) sizeof(temp_field)) {
2715 // If the fieldname is very long, we stop as soon as it begins to overflow the
2716 // maximum field length. At this point we have in fact fully read out the original
2717 // value because cryptfs_setfield would not allow fields with longer names to be
2718 // written in the first place.
2719 break;
2720 }
2721 if (!persist_get_key(temp_field, temp_value)) {
2722 if (strlcat(value, temp_value, len) >= (unsigned)len) {
2723 // value too small.
2724 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
2725 goto out;
2726 }
2727 } else {
2728 // Exhaust all entries.
2729 break;
2730 }
2731 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002732 } else {
2733 /* Sadness, it's not there. Return the error */
Rubin Xu85c01f92014-10-13 12:49:54 +01002734 rc = CRYPTO_GETFIELD_ERROR_NO_FIELD;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002735 }
2736
2737out:
2738 return rc;
2739}
2740
2741/* Set the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01002742int cryptfs_setfield(const char *fieldname, const char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002743{
Paul Crowley38132a12016-02-09 09:50:32 +00002744 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08002745 SLOGE("Cannot set field when file encrypted");
2746 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07002747 }
2748
Ken Sumrall160b4d62013-04-22 12:15:39 -07002749 char encrypted_state[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01002750 /* 0 is success, negative values are error */
2751 int rc = CRYPTO_SETFIELD_ERROR_OTHER;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002752 int encrypted = 0;
Rubin Xu85c01f92014-10-13 12:49:54 +01002753 unsigned int field_id;
2754 char temp_field[PROPERTY_KEY_MAX];
2755 unsigned int num_entries;
2756 unsigned int max_keylen;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002757
2758 if (persist_data == NULL) {
2759 load_persistent_data();
2760 if (persist_data == NULL) {
2761 SLOGE("Setfield error, cannot load persistent data");
2762 goto out;
2763 }
2764 }
2765
2766 property_get("ro.crypto.state", encrypted_state, "");
2767 if (!strcmp(encrypted_state, "encrypted") ) {
2768 encrypted = 1;
2769 }
2770
Rubin Xu85c01f92014-10-13 12:49:54 +01002771 // Compute the number of entries required to store value, each entry can store up to
2772 // (PROPERTY_VALUE_MAX - 1) chars
2773 if (strlen(value) == 0) {
2774 // Empty value also needs one entry to store.
2775 num_entries = 1;
2776 } else {
2777 num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1);
2778 }
2779
2780 max_keylen = strlen(fieldname);
2781 if (num_entries > 1) {
2782 // Need an extra "_%d" suffix.
2783 max_keylen += 1 + log10(num_entries);
2784 }
2785 if (max_keylen > PROPERTY_KEY_MAX - 1) {
2786 rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002787 goto out;
2788 }
2789
Rubin Xu85c01f92014-10-13 12:49:54 +01002790 // Make sure we have enough space to write the new value
2791 if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) >
2792 persist_get_max_entries(encrypted)) {
2793 rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG;
2794 goto out;
2795 }
2796
2797 // Now that we know persist_data has enough space for value, let's delete the old field first
2798 // to make up space.
2799 persist_del_keys(fieldname, 0);
2800
2801 if (persist_set_key(fieldname, value, encrypted)) {
2802 // fail to set key, should not happen as we have already checked the available space
2803 SLOGE("persist_set_key() error during setfield()");
2804 goto out;
2805 }
2806
2807 for (field_id = 1; field_id < num_entries; field_id++) {
2808 snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, field_id);
2809
2810 if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), 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
Ken Sumrall160b4d62013-04-22 12:15:39 -07002817 /* If we are running encrypted, save the persistent data now */
2818 if (encrypted) {
2819 if (save_persistent_data()) {
2820 SLOGE("Setfield error, cannot save persistent data");
2821 goto out;
2822 }
2823 }
2824
Rubin Xu85c01f92014-10-13 12:49:54 +01002825 rc = CRYPTO_SETFIELD_OK;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002826
2827out:
2828 return rc;
2829}
Paul Lawrencef4faa572014-01-29 13:31:03 -08002830
2831/* Checks userdata. Attempt to mount the volume if default-
2832 * encrypted.
2833 * On success trigger next init phase and return 0.
2834 * Currently do not handle failure - see TODO below.
2835 */
2836int cryptfs_mount_default_encrypted(void)
2837{
Paul Lawrence84274cc2016-04-15 15:41:33 -07002838 int crypt_type = cryptfs_get_password_type();
2839 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2840 SLOGE("Bad crypt type - error");
2841 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
2842 SLOGD("Password is not default - "
2843 "starting min framework to prompt");
2844 property_set("vold.decrypt", "trigger_restart_min_framework");
2845 return 0;
2846 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
2847 SLOGD("Password is default - restarting filesystem");
2848 cryptfs_restart_internal(0);
2849 return 0;
Paul Lawrencef4faa572014-01-29 13:31:03 -08002850 } else {
Paul Lawrence84274cc2016-04-15 15:41:33 -07002851 SLOGE("Encrypted, default crypt type but can't decrypt");
Paul Lawrencef4faa572014-01-29 13:31:03 -08002852 }
2853
Paul Lawrence6bfed202014-07-28 12:47:22 -07002854 /** Corrupt. Allow us to boot into framework, which will detect bad
2855 crypto when it calls do_crypto_complete, then do a factory reset
Paul Lawrencef4faa572014-01-29 13:31:03 -08002856 */
Paul Lawrence6bfed202014-07-28 12:47:22 -07002857 property_set("vold.decrypt", "trigger_restart_min_framework");
Paul Lawrencef4faa572014-01-29 13:31:03 -08002858 return 0;
2859}
2860
2861/* Returns type of the password, default, pattern, pin or password.
2862 */
2863int cryptfs_get_password_type(void)
2864{
Paul Crowley38132a12016-02-09 09:50:32 +00002865 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002866 SLOGE("cryptfs_get_password_type not valid for file encryption");
2867 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08002868 }
2869
Paul Lawrencef4faa572014-01-29 13:31:03 -08002870 struct crypt_mnt_ftr crypt_ftr;
2871
2872 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2873 SLOGE("Error getting crypt footer and key\n");
2874 return -1;
2875 }
2876
Paul Lawrence6bfed202014-07-28 12:47:22 -07002877 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) {
2878 return -1;
2879 }
2880
Paul Lawrencef4faa572014-01-29 13:31:03 -08002881 return crypt_ftr.crypt_type;
2882}
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002883
Paul Lawrence05335c32015-03-05 09:46:23 -08002884const char* cryptfs_get_password()
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002885{
Paul Crowley38132a12016-02-09 09:50:32 +00002886 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002887 SLOGE("cryptfs_get_password not valid for file encryption");
2888 return 0;
Paul Lawrence05335c32015-03-05 09:46:23 -08002889 }
2890
Paul Lawrence399317e2014-03-10 13:20:50 -07002891 struct timespec now;
Paul Lawrenceef2b5be2014-11-11 12:47:03 -08002892 clock_gettime(CLOCK_BOOTTIME, &now);
Paul Lawrence399317e2014-03-10 13:20:50 -07002893 if (now.tv_sec < password_expiry_time) {
2894 return password;
2895 } else {
2896 cryptfs_clear_password();
2897 return 0;
2898 }
2899}
2900
2901void cryptfs_clear_password()
2902{
2903 if (password) {
2904 size_t len = strlen(password);
2905 memset(password, 0, len);
2906 free(password);
2907 password = 0;
2908 password_expiry_time = 0;
2909 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002910}
Paul Lawrence731a7a22015-04-28 22:14:15 +00002911
Paul Lawrence0c247462015-10-29 10:30:57 -07002912int cryptfs_isConvertibleToFBE()
2913{
2914 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
2915 return fs_mgr_is_convertible_to_fbe(rec) ? 1 : 0;
2916}
2917
Paul Lawrence731a7a22015-04-28 22:14:15 +00002918int cryptfs_create_default_ftr(struct crypt_mnt_ftr* crypt_ftr, __attribute__((unused))int key_length)
2919{
2920 if (cryptfs_init_crypt_mnt_ftr(crypt_ftr)) {
2921 SLOGE("Failed to initialize crypt_ftr");
2922 return -1;
2923 }
2924
2925 if (create_encrypted_random_key(DEFAULT_PASSWORD, crypt_ftr->master_key,
2926 crypt_ftr->salt, crypt_ftr)) {
2927 SLOGE("Cannot create encrypted master key\n");
2928 return -1;
2929 }
2930
2931 //crypt_ftr->keysize = key_length / 8;
2932 return 0;
2933}
2934
2935int cryptfs_get_master_key(struct crypt_mnt_ftr* ftr, const char* password,
2936 unsigned char* master_key)
2937{
2938 int rc;
2939
Paul Lawrence731a7a22015-04-28 22:14:15 +00002940 unsigned char* intermediate_key = 0;
2941 size_t intermediate_key_size = 0;
Paul Lawrencec78c71b2015-04-14 15:26:29 -07002942
2943 if (password == 0 || *password == 0) {
2944 password = DEFAULT_PASSWORD;
2945 }
2946
Paul Lawrence731a7a22015-04-28 22:14:15 +00002947 rc = decrypt_master_key(password, master_key, ftr, &intermediate_key,
2948 &intermediate_key_size);
2949
Paul Lawrence300dae72016-03-11 11:02:52 -08002950 if (rc) {
2951 SLOGE("Can't calculate intermediate key");
2952 return rc;
2953 }
2954
Paul Lawrencec78c71b2015-04-14 15:26:29 -07002955 int N = 1 << ftr->N_factor;
2956 int r = 1 << ftr->r_factor;
2957 int p = 1 << ftr->p_factor;
2958
2959 unsigned char scrypted_intermediate_key[sizeof(ftr->scrypted_intermediate_key)];
2960
2961 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
2962 ftr->salt, sizeof(ftr->salt), N, r, p,
2963 scrypted_intermediate_key,
2964 sizeof(scrypted_intermediate_key));
2965
2966 free(intermediate_key);
2967
2968 if (rc) {
Paul Lawrence300dae72016-03-11 11:02:52 -08002969 SLOGE("Can't scrypt intermediate key");
Paul Lawrencec78c71b2015-04-14 15:26:29 -07002970 return rc;
2971 }
2972
2973 return memcmp(scrypted_intermediate_key, ftr->scrypted_intermediate_key,
2974 intermediate_key_size);
Paul Lawrence731a7a22015-04-28 22:14:15 +00002975}
2976
2977int cryptfs_set_password(struct crypt_mnt_ftr* ftr, const char* password,
2978 const unsigned char* master_key)
2979{
2980 return encrypt_master_key(password, ftr->salt, master_key, ftr->master_key,
2981 ftr);
2982}
Paul Lawrence6e410592016-05-24 14:20:38 -07002983
Eric Biggersb45caaf2017-02-02 14:52:12 -08002984void cryptfs_get_file_encryption_modes(const char **contents_mode_ret,
2985 const char **filenames_mode_ret)
Paul Lawrence6e410592016-05-24 14:20:38 -07002986{
2987 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
Eric Biggersb45caaf2017-02-02 14:52:12 -08002988 fs_mgr_get_file_encryption_modes(rec, contents_mode_ret, filenames_mode_ret);
Paul Lawrence6e410592016-05-24 14:20:38 -07002989}