blob: 5d1453f72f8af5054fcecb152d66f5a8159b77c4 [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 Lawrence87999172014-02-20 12:21:31 -080061#include "CheckBattery.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
Jason parks70a4b3f2011-01-28 10:10:47 -060099static unsigned char saved_master_key[KEY_LEN_BYTES];
Ken Sumrall3ad90722011-10-04 20:38:29 -0700100static char *saved_mount_point;
Jason parks70a4b3f2011-01-28 10:10:47 -0600101static int master_key_saved = 0;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700102static struct crypt_persist_data *persist_data = NULL;
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800103
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700104/* Should we use keymaster? */
105static int keymaster_check_compatibility()
106{
Janis Danisevskis015ec302017-01-31 11:31:08 +0000107 return keymaster_compatibility_cryptfs_scrypt();
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700108}
109
110/* Create a new keymaster key and store it in this footer */
111static int keymaster_create_key(struct crypt_mnt_ftr *ftr)
112{
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800113 if (ftr->keymaster_blob_size) {
114 SLOGI("Already have key");
115 return 0;
116 }
117
Janis Danisevskis015ec302017-01-31 11:31:08 +0000118 int rc = keymaster_create_key_for_cryptfs_scrypt(RSA_KEY_SIZE, RSA_EXPONENT,
119 KEYMASTER_CRYPTFS_RATE_LIMIT, ftr->keymaster_blob, KEYMASTER_BLOB_SIZE,
120 &ftr->keymaster_blob_size);
121 if (rc) {
122 if (ftr->keymaster_blob_size > KEYMASTER_BLOB_SIZE) {
123 SLOGE("Keymaster key blob to large)");
124 ftr->keymaster_blob_size = 0;
125 }
126 SLOGE("Failed to generate keypair");
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700127 return -1;
128 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000129 return 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700130}
131
Shawn Willdene17a9c42014-09-08 13:04:08 -0600132/* This signs the given object using the keymaster key. */
133static int keymaster_sign_object(struct crypt_mnt_ftr *ftr,
Shawn Willden47ba10d2014-09-03 17:07:06 -0600134 const unsigned char *object,
135 const size_t object_size,
136 unsigned char **signature,
137 size_t *signature_size)
138{
Shawn Willden47ba10d2014-09-03 17:07:06 -0600139 unsigned char to_sign[RSA_KEY_SIZE_BYTES];
Shawn Willdene17a9c42014-09-08 13:04:08 -0600140 size_t to_sign_size = sizeof(to_sign);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600141 memset(to_sign, 0, RSA_KEY_SIZE_BYTES);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600142
Shawn Willdene17a9c42014-09-08 13:04:08 -0600143 // To sign a message with RSA, the message must satisfy two
144 // constraints:
145 //
146 // 1. The message, when interpreted as a big-endian numeric value, must
147 // be strictly less than the public modulus of the RSA key. Note
148 // that because the most significant bit of the public modulus is
149 // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit
150 // key), an n-bit message with most significant bit 0 always
151 // satisfies this requirement.
152 //
153 // 2. The message must have the same length in bits as the public
154 // modulus of the RSA key. This requirement isn't mathematically
155 // necessary, but is necessary to ensure consistency in
156 // implementations.
157 switch (ftr->kdf_type) {
Shawn Willdene17a9c42014-09-08 13:04:08 -0600158 case KDF_SCRYPT_KEYMASTER:
159 // This ensures the most significant byte of the signed message
160 // is zero. We could have zero-padded to the left instead, but
161 // this approach is slightly more robust against changes in
162 // object size. However, it's still broken (but not unusably
Shawn Willdenda6e8992015-06-03 09:40:45 -0600163 // so) because we really should be using a proper deterministic
164 // RSA padding function, such as PKCS1.
Wei Wang4375f1b2017-02-24 17:43:01 -0800165 memcpy(to_sign + 1, object, std::min((size_t)RSA_KEY_SIZE_BYTES - 1, object_size));
Shawn Willdene17a9c42014-09-08 13:04:08 -0600166 SLOGI("Signing safely-padded object");
167 break;
168 default:
169 SLOGE("Unknown KDF type %d", ftr->kdf_type);
Janis Danisevskis015ec302017-01-31 11:31:08 +0000170 return -1;
Shawn Willdene17a9c42014-09-08 13:04:08 -0600171 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000172 return keymaster_sign_object_for_cryptfs_scrypt(ftr->keymaster_blob, ftr->keymaster_blob_size,
173 KEYMASTER_CRYPTFS_RATE_LIMIT, to_sign, to_sign_size, signature, signature_size);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600174}
175
Paul Lawrence399317e2014-03-10 13:20:50 -0700176/* Store password when userdata is successfully decrypted and mounted.
177 * Cleared by cryptfs_clear_password
178 *
179 * To avoid a double prompt at boot, we need to store the CryptKeeper
180 * password and pass it to KeyGuard, which uses it to unlock KeyStore.
181 * Since the entire framework is torn down and rebuilt after encryption,
182 * we have to use a daemon or similar to store the password. Since vold
183 * is secured against IPC except from system processes, it seems a reasonable
184 * place to store this.
185 *
186 * password should be cleared once it has been used.
187 *
188 * password is aged out after password_max_age_seconds seconds.
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800189 */
Paul Lawrence399317e2014-03-10 13:20:50 -0700190static char* password = 0;
191static int password_expiry_time = 0;
192static const int password_max_age_seconds = 60;
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800193
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800194extern struct fstab *fstab;
Ken Sumrall8ddbe402011-01-17 15:26:29 -0800195
Paul Lawrence87999172014-02-20 12:21:31 -0800196enum RebootType {reboot, recovery, shutdown};
197static void cryptfs_reboot(enum RebootType rt)
Ken Sumralladfba362013-06-04 16:37:52 -0700198{
Paul Lawrence87999172014-02-20 12:21:31 -0800199 switch(rt) {
200 case reboot:
201 property_set(ANDROID_RB_PROPERTY, "reboot");
202 break;
203
204 case recovery:
205 property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
206 break;
207
208 case shutdown:
209 property_set(ANDROID_RB_PROPERTY, "shutdown");
210 break;
Ken Sumralladfba362013-06-04 16:37:52 -0700211 }
Paul Lawrence87999172014-02-20 12:21:31 -0800212
Ken Sumralladfba362013-06-04 16:37:52 -0700213 sleep(20);
214
215 /* Shouldn't get here, reboot should happen before sleep times out */
216 return;
217}
218
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800219static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
220{
221 memset(io, 0, dataSize);
222 io->data_size = dataSize;
223 io->data_start = sizeof(struct dm_ioctl);
224 io->version[0] = 4;
225 io->version[1] = 0;
226 io->version[2] = 0;
227 io->flags = flags;
228 if (name) {
Marek Pola5e6b9142015-02-05 14:22:34 +0100229 strlcpy(io->name, name, sizeof(io->name));
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800230 }
231}
232
Kenny Rootc4c70f12013-06-14 12:11:38 -0700233/**
234 * Gets the default device scrypt parameters for key derivation time tuning.
235 * The parameters should lead to about one second derivation time for the
236 * given device.
237 */
238static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700239 char paramstr[PROPERTY_VALUE_MAX];
Paul Crowley63c18d32016-02-10 14:02:47 +0000240 int Nf, rf, pf;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700241
Paul Crowley63c18d32016-02-10 14:02:47 +0000242 property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS);
243 if (!parse_scrypt_parameters(paramstr, &Nf, &rf, &pf)) {
244 SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
245 parse_scrypt_parameters(SCRYPT_DEFAULTS, &Nf, &rf, &pf);
Kenny Rootc4c70f12013-06-14 12:11:38 -0700246 }
Paul Crowley63c18d32016-02-10 14:02:47 +0000247 ftr->N_factor = Nf;
248 ftr->r_factor = rf;
249 ftr->p_factor = pf;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700250}
251
Ken Sumrall3ed82362011-01-28 23:31:16 -0800252static unsigned int get_fs_size(char *dev)
253{
254 int fd, block_size;
255 struct ext4_super_block sb;
256 off64_t len;
257
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700258 if ((fd = open(dev, O_RDONLY|O_CLOEXEC)) < 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -0800259 SLOGE("Cannot open device to get filesystem size ");
260 return 0;
261 }
262
263 if (lseek64(fd, 1024, SEEK_SET) < 0) {
264 SLOGE("Cannot seek to superblock");
265 return 0;
266 }
267
268 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
269 SLOGE("Cannot read superblock");
270 return 0;
271 }
272
273 close(fd);
274
Daniel Rosenberge82df162014-08-15 22:19:23 +0000275 if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) {
276 SLOGE("Not a valid ext4 superblock");
277 return 0;
278 }
Ken Sumrall3ed82362011-01-28 23:31:16 -0800279 block_size = 1024 << sb.s_log_block_size;
280 /* compute length in bytes */
281 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
282
283 /* return length in sectors */
284 return (unsigned int) (len / 512);
285}
286
Ken Sumrall160b4d62013-04-22 12:15:39 -0700287static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
288{
289 static int cached_data = 0;
290 static off64_t cached_off = 0;
291 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
292 int fd;
293 char key_loc[PROPERTY_VALUE_MAX];
294 char real_blkdev[PROPERTY_VALUE_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -0700295 int rc = -1;
296
297 if (!cached_data) {
298 fs_mgr_get_crypt_info(fstab, key_loc, real_blkdev, sizeof(key_loc));
299
300 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700301 if ( (fd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700302 SLOGE("Cannot open real block device %s\n", real_blkdev);
303 return -1;
304 }
305
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900306 unsigned long nr_sec = 0;
307 get_blkdev_size(fd, &nr_sec);
308 if (nr_sec != 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700309 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
310 * encryption info footer and key, and plenty of bytes to spare for future
311 * growth.
312 */
313 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
314 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
315 cached_data = 1;
316 } else {
317 SLOGE("Cannot get size of block device %s\n", real_blkdev);
318 }
319 close(fd);
320 } else {
321 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
322 cached_off = 0;
323 cached_data = 1;
324 }
325 }
326
327 if (cached_data) {
328 if (metadata_fname) {
329 *metadata_fname = cached_metadata_fname;
330 }
331 if (off) {
332 *off = cached_off;
333 }
334 rc = 0;
335 }
336
337 return rc;
338}
339
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800340/* Set sha256 checksum in structure */
341static void set_ftr_sha(struct crypt_mnt_ftr *crypt_ftr)
342{
343 SHA256_CTX c;
344 SHA256_Init(&c);
345 memset(crypt_ftr->sha256, 0, sizeof(crypt_ftr->sha256));
346 SHA256_Update(&c, crypt_ftr, sizeof(*crypt_ftr));
347 SHA256_Final(crypt_ftr->sha256, &c);
348}
349
Ken Sumralle8744072011-01-18 22:01:55 -0800350/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800351 * update the failed mount count but not change the key.
352 */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700353static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800354{
355 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800356 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700357 /* starting_off is set to the SEEK_SET offset
358 * where the crypto structure starts
359 */
360 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800361 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700362 char *fname = NULL;
Ken Sumrall3be890f2011-09-14 16:53:46 -0700363 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800364
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800365 set_ftr_sha(crypt_ftr);
366
Ken Sumrall160b4d62013-04-22 12:15:39 -0700367 if (get_crypt_ftr_info(&fname, &starting_off)) {
368 SLOGE("Unable to get crypt_ftr_info\n");
369 return -1;
370 }
371 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700372 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700373 return -1;
374 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700375 if ( (fd = open(fname, O_RDWR | O_CREAT|O_CLOEXEC, 0600)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700376 SLOGE("Cannot open footer file %s for put\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700377 return -1;
378 }
379
380 /* Seek to the start of the crypt footer */
381 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
382 SLOGE("Cannot seek to real block device footer\n");
383 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800384 }
385
386 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
387 SLOGE("Cannot write real block device footer\n");
388 goto errout;
389 }
390
Ken Sumrall3be890f2011-09-14 16:53:46 -0700391 fstat(fd, &statbuf);
392 /* If the keys are kept on a raw block device, do not try to truncate it. */
Ken Sumralle550f782013-08-20 13:48:23 -0700393 if (S_ISREG(statbuf.st_mode)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700394 if (ftruncate(fd, 0x4000)) {
Colin Cross59846b62014-02-06 20:34:29 -0800395 SLOGE("Cannot set footer file size\n");
Ken Sumralle8744072011-01-18 22:01:55 -0800396 goto errout;
397 }
398 }
399
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800400 /* Success! */
401 rc = 0;
402
403errout:
404 close(fd);
405 return rc;
406
407}
408
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800409static bool check_ftr_sha(const struct crypt_mnt_ftr *crypt_ftr)
410{
411 struct crypt_mnt_ftr copy;
412 memcpy(&copy, crypt_ftr, sizeof(copy));
413 set_ftr_sha(&copy);
414 return memcmp(copy.sha256, crypt_ftr->sha256, sizeof(copy.sha256)) == 0;
415}
416
Ken Sumrall160b4d62013-04-22 12:15:39 -0700417static inline int unix_read(int fd, void* buff, int len)
418{
419 return TEMP_FAILURE_RETRY(read(fd, buff, len));
420}
421
422static inline int unix_write(int fd, const void* buff, int len)
423{
424 return TEMP_FAILURE_RETRY(write(fd, buff, len));
425}
426
427static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
428{
429 memset(pdata, 0, len);
430 pdata->persist_magic = PERSIST_DATA_MAGIC;
431 pdata->persist_valid_entries = 0;
432}
433
434/* A routine to update the passed in crypt_ftr to the lastest version.
435 * fd is open read/write on the device that holds the crypto footer and persistent
436 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
437 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
438 */
439static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
440{
Kenny Root7434b312013-06-14 11:29:53 -0700441 int orig_major = crypt_ftr->major_version;
442 int orig_minor = crypt_ftr->minor_version;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700443
Kenny Root7434b312013-06-14 11:29:53 -0700444 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
445 struct crypt_persist_data *pdata;
446 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700447
Kenny Rootc4c70f12013-06-14 12:11:38 -0700448 SLOGW("upgrading crypto footer to 1.1");
449
Wei Wang4375f1b2017-02-24 17:43:01 -0800450 pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
Kenny Root7434b312013-06-14 11:29:53 -0700451 if (pdata == NULL) {
452 SLOGE("Cannot allocate persisent data\n");
453 return;
454 }
455 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
456
457 /* Need to initialize the persistent data area */
458 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
459 SLOGE("Cannot seek to persisent data offset\n");
Henrik Baard91064632015-02-05 15:09:17 +0100460 free(pdata);
Kenny Root7434b312013-06-14 11:29:53 -0700461 return;
462 }
463 /* Write all zeros to the first copy, making it invalid */
464 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
465
466 /* Write a valid but empty structure to the second copy */
467 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
468 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
469
470 /* Update the footer */
471 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
472 crypt_ftr->persist_data_offset[0] = pdata_offset;
473 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
474 crypt_ftr->minor_version = 1;
Henrik Baard91064632015-02-05 15:09:17 +0100475 free(pdata);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700476 }
477
Paul Lawrencef4faa572014-01-29 13:31:03 -0800478 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700479 SLOGW("upgrading crypto footer to 1.2");
JP Abgrall7bdfa522013-11-15 13:42:56 -0800480 /* But keep the old kdf_type.
481 * It will get updated later to KDF_SCRYPT after the password has been verified.
482 */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700483 crypt_ftr->kdf_type = KDF_PBKDF2;
484 get_device_scrypt_params(crypt_ftr);
485 crypt_ftr->minor_version = 2;
486 }
487
Paul Lawrencef4faa572014-01-29 13:31:03 -0800488 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
489 SLOGW("upgrading crypto footer to 1.3");
490 crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
491 crypt_ftr->minor_version = 3;
492 }
493
Kenny Root7434b312013-06-14 11:29:53 -0700494 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
495 if (lseek64(fd, offset, SEEK_SET) == -1) {
496 SLOGE("Cannot seek to crypt footer\n");
497 return;
498 }
499 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700500 }
Ken Sumrall160b4d62013-04-22 12:15:39 -0700501}
502
503
504static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800505{
506 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800507 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700508 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800509 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700510 char *fname = NULL;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700511 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800512
Ken Sumrall160b4d62013-04-22 12:15:39 -0700513 if (get_crypt_ftr_info(&fname, &starting_off)) {
514 SLOGE("Unable to get crypt_ftr_info\n");
515 return -1;
516 }
517 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700518 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700519 return -1;
520 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700521 if ( (fd = open(fname, O_RDWR|O_CLOEXEC)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700522 SLOGE("Cannot open footer file %s for get\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700523 return -1;
524 }
525
526 /* Make sure it's 16 Kbytes in length */
527 fstat(fd, &statbuf);
528 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
529 SLOGE("footer file %s is not the expected size!\n", fname);
530 goto errout;
531 }
532
533 /* Seek to the start of the crypt footer */
534 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
535 SLOGE("Cannot seek to real block device footer\n");
536 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800537 }
538
539 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
540 SLOGE("Cannot read real block device footer\n");
541 goto errout;
542 }
543
544 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700545 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800546 goto errout;
547 }
548
Kenny Rootc96a5f82013-06-14 12:08:28 -0700549 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
550 SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
551 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800552 goto errout;
553 }
554
Kenny Rootc96a5f82013-06-14 12:08:28 -0700555 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
556 SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
557 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800558 }
559
Ken Sumrall160b4d62013-04-22 12:15:39 -0700560 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
561 * copy on disk before returning.
562 */
Kenny Rootc96a5f82013-06-14 12:08:28 -0700563 if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700564 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
Ken Sumralle8744072011-01-18 22:01:55 -0800565 }
566
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800567 /* Success! */
568 rc = 0;
569
570errout:
571 close(fd);
572 return rc;
573}
574
Ken Sumrall160b4d62013-04-22 12:15:39 -0700575static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
576{
577 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
578 crypt_ftr->persist_data_offset[1]) {
579 SLOGE("Crypt_ftr persist data regions overlap");
580 return -1;
581 }
582
583 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
584 SLOGE("Crypt_ftr persist data region 0 starts after region 1");
585 return -1;
586 }
587
588 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
589 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
590 CRYPT_FOOTER_OFFSET) {
591 SLOGE("Persistent data extends past crypto footer");
592 return -1;
593 }
594
595 return 0;
596}
597
598static int load_persistent_data(void)
599{
600 struct crypt_mnt_ftr crypt_ftr;
601 struct crypt_persist_data *pdata = NULL;
602 char encrypted_state[PROPERTY_VALUE_MAX];
603 char *fname;
604 int found = 0;
605 int fd;
606 int ret;
607 int i;
608
609 if (persist_data) {
610 /* Nothing to do, we've already loaded or initialized it */
611 return 0;
612 }
613
614
615 /* If not encrypted, just allocate an empty table and initialize it */
616 property_get("ro.crypto.state", encrypted_state, "");
617 if (strcmp(encrypted_state, "encrypted") ) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800618 pdata = (crypt_persist_data*)malloc(CRYPT_PERSIST_DATA_SIZE);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700619 if (pdata) {
620 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
621 persist_data = pdata;
622 return 0;
623 }
624 return -1;
625 }
626
627 if(get_crypt_ftr_and_key(&crypt_ftr)) {
628 return -1;
629 }
630
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700631 if ((crypt_ftr.major_version < 1)
632 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700633 SLOGE("Crypt_ftr version doesn't support persistent data");
634 return -1;
635 }
636
637 if (get_crypt_ftr_info(&fname, NULL)) {
638 return -1;
639 }
640
641 ret = validate_persistent_data_storage(&crypt_ftr);
642 if (ret) {
643 return -1;
644 }
645
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700646 fd = open(fname, O_RDONLY|O_CLOEXEC);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700647 if (fd < 0) {
648 SLOGE("Cannot open %s metadata file", fname);
649 return -1;
650 }
651
Wei Wang4375f1b2017-02-24 17:43:01 -0800652 pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
Paul Lawrence300dae72016-03-11 11:02:52 -0800653 if (pdata == NULL) {
654 SLOGE("Cannot allocate memory for persistent data");
655 goto err;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700656 }
657
658 for (i = 0; i < 2; i++) {
659 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
660 SLOGE("Cannot seek to read persistent data on %s", fname);
661 goto err2;
662 }
663 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
664 SLOGE("Error reading persistent data on iteration %d", i);
665 goto err2;
666 }
667 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
668 found = 1;
669 break;
670 }
671 }
672
673 if (!found) {
674 SLOGI("Could not find valid persistent data, creating");
675 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
676 }
677
678 /* Success */
679 persist_data = pdata;
680 close(fd);
681 return 0;
682
683err2:
684 free(pdata);
685
686err:
687 close(fd);
688 return -1;
689}
690
691static int save_persistent_data(void)
692{
693 struct crypt_mnt_ftr crypt_ftr;
694 struct crypt_persist_data *pdata;
695 char *fname;
696 off64_t write_offset;
697 off64_t erase_offset;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700698 int fd;
699 int ret;
700
701 if (persist_data == NULL) {
702 SLOGE("No persistent data to save");
703 return -1;
704 }
705
706 if(get_crypt_ftr_and_key(&crypt_ftr)) {
707 return -1;
708 }
709
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700710 if ((crypt_ftr.major_version < 1)
711 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700712 SLOGE("Crypt_ftr version doesn't support persistent data");
713 return -1;
714 }
715
716 ret = validate_persistent_data_storage(&crypt_ftr);
717 if (ret) {
718 return -1;
719 }
720
721 if (get_crypt_ftr_info(&fname, NULL)) {
722 return -1;
723 }
724
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700725 fd = open(fname, O_RDWR|O_CLOEXEC);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700726 if (fd < 0) {
727 SLOGE("Cannot open %s metadata file", fname);
728 return -1;
729 }
730
Wei Wang4375f1b2017-02-24 17:43:01 -0800731 pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700732 if (pdata == NULL) {
733 SLOGE("Cannot allocate persistant data");
734 goto err;
735 }
736
737 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
738 SLOGE("Cannot seek to read persistent data on %s", fname);
739 goto err2;
740 }
741
742 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
743 SLOGE("Error reading persistent data before save");
744 goto err2;
745 }
746
747 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
748 /* The first copy is the curent valid copy, so write to
749 * the second copy and erase this one */
750 write_offset = crypt_ftr.persist_data_offset[1];
751 erase_offset = crypt_ftr.persist_data_offset[0];
752 } else {
753 /* The second copy must be the valid copy, so write to
754 * the first copy, and erase the second */
755 write_offset = crypt_ftr.persist_data_offset[0];
756 erase_offset = crypt_ftr.persist_data_offset[1];
757 }
758
759 /* Write the new copy first, if successful, then erase the old copy */
Björn Landström96dbee72015-01-20 12:43:56 +0100760 if (lseek64(fd, write_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700761 SLOGE("Cannot seek to write persistent data");
762 goto err2;
763 }
764 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
765 (int) crypt_ftr.persist_data_size) {
Björn Landström96dbee72015-01-20 12:43:56 +0100766 if (lseek64(fd, erase_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700767 SLOGE("Cannot seek to erase previous persistent data");
768 goto err2;
769 }
770 fsync(fd);
771 memset(pdata, 0, crypt_ftr.persist_data_size);
772 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
773 (int) crypt_ftr.persist_data_size) {
774 SLOGE("Cannot write to erase previous persistent data");
775 goto err2;
776 }
777 fsync(fd);
778 } else {
779 SLOGE("Cannot write to save persistent data");
780 goto err2;
781 }
782
783 /* Success */
784 free(pdata);
785 close(fd);
786 return 0;
787
788err2:
789 free(pdata);
790err:
791 close(fd);
792 return -1;
793}
794
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800795/* Convert a binary key of specified length into an ascii hex string equivalent,
796 * without the leading 0x and with null termination
797 */
Jeff Sharkey9c484982015-03-31 10:35:33 -0700798static void convert_key_to_hex_ascii(const unsigned char *master_key,
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700799 unsigned int keysize, char *master_key_ascii) {
800 unsigned int i, a;
801 unsigned char nibble;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800802
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700803 for (i=0, a=0; i<keysize; i++, a+=2) {
804 /* For each byte, write out two ascii hex digits */
805 nibble = (master_key[i] >> 4) & 0xf;
806 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800807
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700808 nibble = master_key[i] & 0xf;
809 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
810 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800811
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700812 /* Add the null termination */
813 master_key_ascii[a] = '\0';
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800814
815}
816
Jeff Sharkey9c484982015-03-31 10:35:33 -0700817static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr,
818 const unsigned char *master_key, const char *real_blk_name,
819 const char *name, int fd, const char *extra_params) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800820 alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumralldb5e0262013-02-05 17:39:48 -0800821 struct dm_ioctl *io;
822 struct dm_target_spec *tgt;
823 char *crypt_params;
824 char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
George Burgess IV605d7ae2016-02-29 13:39:17 -0800825 size_t buff_offset;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800826 int i;
827
828 io = (struct dm_ioctl *) buffer;
829
830 /* Load the mapping table for this device */
831 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
832
833 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
834 io->target_count = 1;
835 tgt->status = 0;
836 tgt->sector_start = 0;
837 tgt->length = crypt_ftr->fs_size;
Ajay Dudani87701e22014-09-17 21:02:52 -0700838 strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME);
Ken Sumralldb5e0262013-02-05 17:39:48 -0800839
840 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
841 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
George Burgess IV605d7ae2016-02-29 13:39:17 -0800842
843 buff_offset = crypt_params - buffer;
844 snprintf(crypt_params, sizeof(buffer) - buff_offset, "%s %s 0 %s 0 %s",
845 crypt_ftr->crypto_type_name, master_key_ascii, real_blk_name,
846 extra_params);
Ken Sumralldb5e0262013-02-05 17:39:48 -0800847 crypt_params += strlen(crypt_params) + 1;
848 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
849 tgt->next = crypt_params - buffer;
850
851 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
852 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
853 break;
854 }
855 usleep(500000);
856 }
857
858 if (i == TABLE_LOAD_RETRIES) {
859 /* We failed to load the table, return an error */
860 return -1;
861 } else {
862 return i + 1;
863 }
864}
865
866
867static int get_dm_crypt_version(int fd, const char *name, int *version)
868{
869 char buffer[DM_CRYPT_BUF_SIZE];
870 struct dm_ioctl *io;
871 struct dm_target_versions *v;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800872
873 io = (struct dm_ioctl *) buffer;
874
875 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
876
877 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
878 return -1;
879 }
880
881 /* Iterate over the returned versions, looking for name of "crypt".
882 * When found, get and return the version.
883 */
884 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
885 while (v->next) {
886 if (! strcmp(v->name, "crypt")) {
887 /* We found the crypt driver, return the version, and get out */
888 version[0] = v->version[0];
889 version[1] = v->version[1];
890 version[2] = v->version[2];
891 return 0;
892 }
893 v = (struct dm_target_versions *)(((char *)v) + v->next);
894 }
895
896 return -1;
897}
898
Jeff Sharkey9c484982015-03-31 10:35:33 -0700899static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr,
900 const unsigned char *master_key, const char *real_blk_name,
901 char *crypto_blk_name, const char *name) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800902 char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800903 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800904 unsigned int minor;
Ajay Dudani87701e22014-09-17 21:02:52 -0700905 int fd=0;
Daniel Rosenberg25a52132016-02-26 16:44:36 -0800906 int err;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800907 int retval = -1;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800908 int version[3];
Wei Wang4375f1b2017-02-24 17:43:01 -0800909 const char *extra_params;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800910 int load_count;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800911
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700912 if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800913 SLOGE("Cannot open device-mapper\n");
914 goto errout;
915 }
916
917 io = (struct dm_ioctl *) buffer;
918
919 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Daniel Rosenberg25a52132016-02-26 16:44:36 -0800920 err = ioctl(fd, DM_DEV_CREATE, io);
921 if (err) {
922 SLOGE("Cannot create dm-crypt device %s: %s\n", name, strerror(errno));
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800923 goto errout;
924 }
925
926 /* Get the device status, in particular, the name of it's device file */
927 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
928 if (ioctl(fd, DM_DEV_STATUS, io)) {
929 SLOGE("Cannot retrieve dm-crypt device status\n");
930 goto errout;
931 }
932 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
933 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
934
Ken Sumralldb5e0262013-02-05 17:39:48 -0800935 extra_params = "";
936 if (! get_dm_crypt_version(fd, name, version)) {
937 /* Support for allow_discards was added in version 1.11.0 */
938 if ((version[0] >= 2) ||
939 ((version[0] == 1) && (version[1] >= 11))) {
940 extra_params = "1 allow_discards";
941 SLOGI("Enabling support for allow_discards in dmcrypt.\n");
942 }
Ken Sumralle919efe2012-09-29 17:07:41 -0700943 }
944
Ken Sumralldb5e0262013-02-05 17:39:48 -0800945 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name,
946 fd, extra_params);
947 if (load_count < 0) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800948 SLOGE("Cannot load dm-crypt mapping table.\n");
949 goto errout;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800950 } else if (load_count > 1) {
951 SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800952 }
953
954 /* Resume this device to activate it */
Ken Sumralldb5e0262013-02-05 17:39:48 -0800955 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800956
957 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
958 SLOGE("Cannot resume the dm-crypt device\n");
959 goto errout;
960 }
961
962 /* We made it here with no errors. Woot! */
963 retval = 0;
964
965errout:
966 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
967
968 return retval;
969}
970
Wei Wang4375f1b2017-02-24 17:43:01 -0800971static int delete_crypto_blk_dev(const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800972{
973 int fd;
974 char buffer[DM_CRYPT_BUF_SIZE];
975 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800976 int retval = -1;
977
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700978 if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800979 SLOGE("Cannot open device-mapper\n");
980 goto errout;
981 }
982
983 io = (struct dm_ioctl *) buffer;
984
985 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
986 if (ioctl(fd, DM_DEV_REMOVE, io)) {
987 SLOGE("Cannot remove dm-crypt device\n");
988 goto errout;
989 }
990
991 /* We made it here with no errors. Woot! */
992 retval = 0;
993
994errout:
995 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
996
997 return retval;
998
999}
1000
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001001static int pbkdf2(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001002 unsigned char *ikey, void *params UNUSED)
1003{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001004 SLOGI("Using pbkdf2 for cryptfs KDF");
1005
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001006 /* Turn the password into a key and IV that can decrypt the master key */
Adam Langleybf0d9722015-11-04 14:51:39 -08001007 return PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
1008 HASH_COUNT, KEY_LEN_BYTES + IV_LEN_BYTES,
1009 ikey) != 1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001010}
1011
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001012static int scrypt(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001013 unsigned char *ikey, void *params)
1014{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001015 SLOGI("Using scrypt for cryptfs KDF");
1016
Kenny Rootc4c70f12013-06-14 12:11:38 -07001017 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1018
1019 int N = 1 << ftr->N_factor;
1020 int r = 1 << ftr->r_factor;
1021 int p = 1 << ftr->p_factor;
1022
1023 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001024 unsigned int keysize;
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001025 crypto_scrypt((const uint8_t*)passwd, strlen(passwd),
1026 salt, SALT_LEN, N, r, p, ikey,
1027 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001028
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001029 return 0;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001030}
1031
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001032static int scrypt_keymaster(const char *passwd, const unsigned char *salt,
1033 unsigned char *ikey, void *params)
1034{
1035 SLOGI("Using scrypt with keymaster for cryptfs KDF");
1036
1037 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001038 size_t signature_size;
1039 unsigned char* signature;
1040 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1041
1042 int N = 1 << ftr->N_factor;
1043 int r = 1 << ftr->r_factor;
1044 int p = 1 << ftr->p_factor;
1045
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001046 rc = crypto_scrypt((const uint8_t*)passwd, strlen(passwd),
1047 salt, SALT_LEN, N, r, p, ikey,
1048 KEY_LEN_BYTES + IV_LEN_BYTES);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001049
1050 if (rc) {
1051 SLOGE("scrypt failed");
1052 return -1;
1053 }
1054
Shawn Willdene17a9c42014-09-08 13:04:08 -06001055 if (keymaster_sign_object(ftr, ikey, KEY_LEN_BYTES + IV_LEN_BYTES,
1056 &signature, &signature_size)) {
1057 SLOGE("Signing failed");
1058 return -1;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001059 }
1060
1061 rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN,
1062 N, r, p, ikey, KEY_LEN_BYTES + IV_LEN_BYTES);
1063 free(signature);
1064
1065 if (rc) {
1066 SLOGE("scrypt failed");
1067 return -1;
1068 }
1069
1070 return 0;
1071}
1072
1073static int encrypt_master_key(const char *passwd, const unsigned char *salt,
1074 const unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001075 unsigned char *encrypted_master_key,
1076 struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001077{
1078 unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
1079 EVP_CIPHER_CTX e_ctx;
1080 int encrypted_len, final_len;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001081 int rc = 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001082
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001083 /* Turn the password into an intermediate key and IV that can decrypt the master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001084 get_device_scrypt_params(crypt_ftr);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001085
1086 switch (crypt_ftr->kdf_type) {
1087 case KDF_SCRYPT_KEYMASTER:
1088 if (keymaster_create_key(crypt_ftr)) {
1089 SLOGE("keymaster_create_key failed");
1090 return -1;
1091 }
1092
1093 if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) {
1094 SLOGE("scrypt failed");
1095 return -1;
1096 }
1097 break;
1098
1099 case KDF_SCRYPT:
1100 if (scrypt(passwd, salt, ikey, crypt_ftr)) {
1101 SLOGE("scrypt failed");
1102 return -1;
1103 }
1104 break;
1105
1106 default:
1107 SLOGE("Invalid kdf_type");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001108 return -1;
1109 }
Kenny Rootc4c70f12013-06-14 12:11:38 -07001110
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001111 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001112 EVP_CIPHER_CTX_init(&e_ctx);
1113 if (! EVP_EncryptInit_ex(&e_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001114 SLOGE("EVP_EncryptInit failed\n");
1115 return -1;
1116 }
1117 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001118
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001119 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001120 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
Paul Lawrence731a7a22015-04-28 22:14:15 +00001121 decrypted_master_key, KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001122 SLOGE("EVP_EncryptUpdate failed\n");
1123 return -1;
1124 }
Adam Langley889c4f12014-09-03 14:23:13 -07001125 if (! EVP_EncryptFinal_ex(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001126 SLOGE("EVP_EncryptFinal failed\n");
1127 return -1;
1128 }
1129
1130 if (encrypted_len + final_len != KEY_LEN_BYTES) {
1131 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
1132 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001133 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001134
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001135 /* Store the scrypt of the intermediate key, so we can validate if it's a
1136 password error or mount error when things go wrong.
1137 Note there's no need to check for errors, since if this is incorrect, we
1138 simply won't wipe userdata, which is the correct default behavior
1139 */
1140 int N = 1 << crypt_ftr->N_factor;
1141 int r = 1 << crypt_ftr->r_factor;
1142 int p = 1 << crypt_ftr->p_factor;
1143
1144 rc = crypto_scrypt(ikey, KEY_LEN_BYTES,
1145 crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p,
1146 crypt_ftr->scrypted_intermediate_key,
1147 sizeof(crypt_ftr->scrypted_intermediate_key));
1148
1149 if (rc) {
1150 SLOGE("encrypt_master_key: crypto_scrypt failed");
1151 }
1152
Thurston Hou Yeen Dang06dc3112016-07-18 14:16:37 -07001153 EVP_CIPHER_CTX_cleanup(&e_ctx);
1154
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001155 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001156}
1157
Paul Lawrence731a7a22015-04-28 22:14:15 +00001158static int decrypt_master_key_aux(const char *passwd, unsigned char *salt,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001159 unsigned char *encrypted_master_key,
1160 unsigned char *decrypted_master_key,
1161 kdf_func kdf, void *kdf_params,
1162 unsigned char** intermediate_key,
1163 size_t* intermediate_key_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001164{
1165 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 -08001166 EVP_CIPHER_CTX d_ctx;
1167 int decrypted_len, final_len;
1168
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001169 /* Turn the password into an intermediate key and IV that can decrypt the
1170 master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001171 if (kdf(passwd, salt, ikey, kdf_params)) {
1172 SLOGE("kdf failed");
1173 return -1;
1174 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001175
1176 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001177 EVP_CIPHER_CTX_init(&d_ctx);
1178 if (! EVP_DecryptInit_ex(&d_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001179 return -1;
1180 }
1181 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1182 /* Decrypt the master key */
1183 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
1184 encrypted_master_key, KEY_LEN_BYTES)) {
1185 return -1;
1186 }
Adam Langley889c4f12014-09-03 14:23:13 -07001187 if (! EVP_DecryptFinal_ex(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001188 return -1;
1189 }
1190
1191 if (decrypted_len + final_len != KEY_LEN_BYTES) {
1192 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001193 }
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001194
1195 /* Copy intermediate key if needed by params */
1196 if (intermediate_key && intermediate_key_size) {
1197 *intermediate_key = (unsigned char*) malloc(KEY_LEN_BYTES);
Greg Kaisere8167af2016-04-20 10:50:15 -07001198 if (*intermediate_key) {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001199 memcpy(*intermediate_key, ikey, KEY_LEN_BYTES);
1200 *intermediate_key_size = KEY_LEN_BYTES;
1201 }
1202 }
1203
Thurston Hou Yeen Dang06dc3112016-07-18 14:16:37 -07001204 EVP_CIPHER_CTX_cleanup(&d_ctx);
1205
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001206 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001207}
1208
Kenny Rootc4c70f12013-06-14 12:11:38 -07001209static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001210{
Paul Lawrencedb3730c2015-02-03 13:08:10 -08001211 if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001212 *kdf = scrypt_keymaster;
1213 *kdf_params = ftr;
1214 } else if (ftr->kdf_type == KDF_SCRYPT) {
Kenny Rootc4c70f12013-06-14 12:11:38 -07001215 *kdf = scrypt;
1216 *kdf_params = ftr;
1217 } else {
1218 *kdf = pbkdf2;
1219 *kdf_params = NULL;
1220 }
1221}
1222
Paul Lawrence731a7a22015-04-28 22:14:15 +00001223static int decrypt_master_key(const char *passwd, unsigned char *decrypted_master_key,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001224 struct crypt_mnt_ftr *crypt_ftr,
1225 unsigned char** intermediate_key,
1226 size_t* intermediate_key_size)
Kenny Rootc4c70f12013-06-14 12:11:38 -07001227{
1228 kdf_func kdf;
1229 void *kdf_params;
1230 int ret;
1231
1232 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001233 ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key,
1234 decrypted_master_key, kdf, kdf_params,
1235 intermediate_key, intermediate_key_size);
Kenny Rootc4c70f12013-06-14 12:11:38 -07001236 if (ret != 0) {
1237 SLOGW("failure decrypting master key");
Kenny Rootc4c70f12013-06-14 12:11:38 -07001238 }
1239
1240 return ret;
1241}
1242
Wei Wang4375f1b2017-02-24 17:43:01 -08001243static int create_encrypted_random_key(const char *passwd, unsigned char *master_key, unsigned char *salt,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001244 struct crypt_mnt_ftr *crypt_ftr) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001245 int fd;
Ken Sumralle8744072011-01-18 22:01:55 -08001246 unsigned char key_buf[KEY_LEN_BYTES];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001247
1248 /* Get some random bits for a key */
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001249 fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC);
Ken Sumralle8744072011-01-18 22:01:55 -08001250 read(fd, key_buf, sizeof(key_buf));
1251 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001252 close(fd);
1253
1254 /* Now encrypt it with the password */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001255 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001256}
1257
Paul Lawrence2f32cda2015-05-05 14:28:25 -07001258int wait_and_unmount(const char *mountpoint, bool kill)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001259{
Greg Hackmann955653e2014-09-24 14:55:20 -07001260 int i, err, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001261#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001262
1263 /* Now umount the tmpfs filesystem */
1264 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001265 if (umount(mountpoint) == 0) {
1266 break;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001267 }
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001268
1269 if (errno == EINVAL) {
1270 /* EINVAL is returned if the directory is not a mountpoint,
1271 * i.e. there is no filesystem mounted there. So just get out.
1272 */
1273 break;
1274 }
1275
1276 err = errno;
1277
1278 /* If allowed, be increasingly aggressive before the last two retries */
1279 if (kill) {
1280 if (i == (WAIT_UNMOUNT_COUNT - 3)) {
1281 SLOGW("sending SIGHUP to processes with open files\n");
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001282 vold_killProcessesWithOpenFiles(mountpoint, SIGTERM);
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001283 } else if (i == (WAIT_UNMOUNT_COUNT - 2)) {
1284 SLOGW("sending SIGKILL to processes with open files\n");
Jeff Sharkey36801cc2015-03-13 16:09:20 -07001285 vold_killProcessesWithOpenFiles(mountpoint, SIGKILL);
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001286 }
1287 }
1288
1289 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001290 }
1291
1292 if (i < WAIT_UNMOUNT_COUNT) {
1293 SLOGD("unmounting %s succeeded\n", mountpoint);
1294 rc = 0;
1295 } else {
jessica_yu3f14fe42014-09-22 15:57:40 +08001296 vold_killProcessesWithOpenFiles(mountpoint, 0);
Greg Hackmann955653e2014-09-24 14:55:20 -07001297 SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001298 rc = -1;
1299 }
1300
1301 return rc;
1302}
1303
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001304static int prep_data_fs(void)
1305{
1306 int i;
1307
Jeff Sharkey47695b22016-02-01 17:02:29 -07001308 // NOTE: post_fs_data results in init calling back around to vold, so all
1309 // callers to this method must be async
1310
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001311 /* Do the prep of the /data filesystem */
1312 property_set("vold.post_fs_data_done", "0");
1313 property_set("vold.decrypt", "trigger_post_fs_data");
1314 SLOGD("Just triggered post_fs_data\n");
1315
Ken Sumrallc5872692013-05-14 15:26:31 -07001316 /* Wait a max of 50 seconds, hopefully it takes much less */
Wei Wang4375f1b2017-02-24 17:43:01 -08001317 if (!android::base::WaitForProperty("vold.post_fs_data_done",
1318 "1",
1319 std::chrono::seconds(50))) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001320 /* Ugh, we failed to prep /data in time. Bail. */
Ken Sumrallc5872692013-05-14 15:26:31 -07001321 SLOGE("post_fs_data timed out!\n");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001322 return -1;
1323 } else {
1324 SLOGD("post_fs_data done\n");
1325 return 0;
1326 }
1327}
1328
Paul Lawrence74f29f12014-08-28 15:54:10 -07001329static void cryptfs_set_corrupt()
1330{
1331 // Mark the footer as bad
1332 struct crypt_mnt_ftr crypt_ftr;
1333 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1334 SLOGE("Failed to get crypto footer - panic");
1335 return;
1336 }
1337
1338 crypt_ftr.flags |= CRYPT_DATA_CORRUPT;
1339 if (put_crypt_ftr_and_key(&crypt_ftr)) {
1340 SLOGE("Failed to set crypto footer - panic");
1341 return;
1342 }
1343}
1344
1345static void cryptfs_trigger_restart_min_framework()
1346{
1347 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
1348 SLOGE("Failed to mount tmpfs on data - panic");
1349 return;
1350 }
1351
1352 if (property_set("vold.decrypt", "trigger_post_fs_data")) {
1353 SLOGE("Failed to trigger post fs data - panic");
1354 return;
1355 }
1356
1357 if (property_set("vold.decrypt", "trigger_restart_min_framework")) {
1358 SLOGE("Failed to trigger restart min framework - panic");
1359 return;
1360 }
1361}
1362
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001363/* returns < 0 on failure */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001364static int cryptfs_restart_internal(int restart_main)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001365{
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001366 char crypto_blkdev[MAXPATHLEN];
Tim Murray8439dc92014-12-15 11:56:11 -08001367 int rc = -1;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001368 static int restart_successful = 0;
1369
1370 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -06001371 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001372 SLOGE("Encrypted filesystem not validated, aborting");
1373 return -1;
1374 }
1375
1376 if (restart_successful) {
1377 SLOGE("System already restarted with encrypted disk, aborting");
1378 return -1;
1379 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001380
Paul Lawrencef4faa572014-01-29 13:31:03 -08001381 if (restart_main) {
1382 /* Here is where we shut down the framework. The init scripts
1383 * start all services in one of three classes: core, main or late_start.
1384 * On boot, we start core and main. Now, we stop main, but not core,
1385 * as core includes vold and a few other really important things that
1386 * we need to keep running. Once main has stopped, we should be able
1387 * to umount the tmpfs /data, then mount the encrypted /data.
1388 * We then restart the class main, and also the class late_start.
1389 * At the moment, I've only put a few things in late_start that I know
1390 * are not needed to bring up the framework, and that also cause problems
1391 * with unmounting the tmpfs /data, but I hope to add add more services
1392 * to the late_start class as we optimize this to decrease the delay
1393 * till the user is asked for the password to the filesystem.
1394 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001395
Paul Lawrencef4faa572014-01-29 13:31:03 -08001396 /* The init files are setup to stop the class main when vold.decrypt is
1397 * set to trigger_reset_main.
1398 */
1399 property_set("vold.decrypt", "trigger_reset_main");
1400 SLOGD("Just asked init to shut down class main\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001401
Paul Lawrencef4faa572014-01-29 13:31:03 -08001402 /* Ugh, shutting down the framework is not synchronous, so until it
1403 * can be fixed, this horrible hack will wait a moment for it all to
1404 * shut down before proceeding. Without it, some devices cannot
1405 * restart the graphics services.
1406 */
1407 sleep(2);
1408 }
Ken Sumrall9dedfd42012-10-09 14:16:59 -07001409
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001410 /* Now that the framework is shutdown, we should be able to umount()
1411 * the tmpfs filesystem, and mount the real one.
1412 */
1413
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001414 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1415 if (strlen(crypto_blkdev) == 0) {
1416 SLOGE("fs_crypto_blkdev not set\n");
1417 return -1;
1418 }
1419
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001420 if (! (rc = wait_and_unmount(DATA_MNT_POINT, true)) ) {
Doug Zongker6fd57712013-12-17 09:43:23 -08001421 /* If ro.crypto.readonly is set to 1, mount the decrypted
1422 * filesystem readonly. This is used when /data is mounted by
1423 * recovery mode.
1424 */
1425 char ro_prop[PROPERTY_VALUE_MAX];
1426 property_get("ro.crypto.readonly", ro_prop, "");
1427 if (strlen(ro_prop) > 0 && atoi(ro_prop)) {
1428 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
1429 rec->flags |= MS_RDONLY;
1430 }
JP Abgrall62c7af32014-06-16 13:01:23 -07001431
Ken Sumralle5032c42012-04-01 23:58:44 -07001432 /* If that succeeded, then mount the decrypted filesystem */
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001433 int retries = RETRY_MOUNT_ATTEMPTS;
1434 int mount_rc;
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001435
1436 /*
1437 * fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
1438 * partitions in the fsck domain.
1439 */
1440 if (setexeccon(secontextFsck())){
1441 SLOGE("Failed to setexeccon");
1442 return -1;
1443 }
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001444 while ((mount_rc = fs_mgr_do_mount(fstab, DATA_MNT_POINT,
1445 crypto_blkdev, 0))
1446 != 0) {
1447 if (mount_rc == FS_MGR_DOMNT_BUSY) {
1448 /* TODO: invoke something similar to
1449 Process::killProcessWithOpenFiles(DATA_MNT_POINT,
1450 retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */
1451 SLOGI("Failed to mount %s because it is busy - waiting",
1452 crypto_blkdev);
1453 if (--retries) {
1454 sleep(RETRY_MOUNT_DELAY_SECONDS);
1455 } else {
1456 /* Let's hope that a reboot clears away whatever is keeping
1457 the mount busy */
1458 cryptfs_reboot(reboot);
1459 }
1460 } else {
1461 SLOGE("Failed to mount decrypted data");
1462 cryptfs_set_corrupt();
1463 cryptfs_trigger_restart_min_framework();
1464 SLOGI("Started framework to offer wipe");
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001465 if (setexeccon(NULL)) {
1466 SLOGE("Failed to setexeccon");
1467 }
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001468 return -1;
1469 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001470 }
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001471 if (setexeccon(NULL)) {
1472 SLOGE("Failed to setexeccon");
1473 return -1;
1474 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001475
Ken Sumralle5032c42012-04-01 23:58:44 -07001476 /* Create necessary paths on /data */
1477 if (prep_data_fs()) {
1478 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001479 }
Seigo Nonakae2ef0c02016-06-20 17:05:40 +09001480 property_set("vold.decrypt", "trigger_load_persist_props");
Ken Sumralle5032c42012-04-01 23:58:44 -07001481
1482 /* startup service classes main and late_start */
1483 property_set("vold.decrypt", "trigger_restart_framework");
1484 SLOGD("Just triggered restart_framework\n");
1485
1486 /* Give it a few moments to get started */
1487 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001488 }
1489
Ken Sumrall0cc16632011-01-18 20:32:26 -08001490 if (rc == 0) {
1491 restart_successful = 1;
1492 }
1493
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001494 return rc;
1495}
1496
Paul Lawrencef4faa572014-01-29 13:31:03 -08001497int cryptfs_restart(void)
1498{
Paul Lawrence05335c32015-03-05 09:46:23 -08001499 SLOGI("cryptfs_restart");
Paul Crowley38132a12016-02-09 09:50:32 +00001500 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001501 SLOGE("cryptfs_restart not valid for file encryption:");
1502 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08001503 }
1504
Paul Lawrencef4faa572014-01-29 13:31:03 -08001505 /* Call internal implementation forcing a restart of main service group */
1506 return cryptfs_restart_internal(1);
1507}
1508
Wei Wang4375f1b2017-02-24 17:43:01 -08001509static int do_crypto_complete(const char *mount_point)
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001510{
1511 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001512 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -08001513 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001514
1515 property_get("ro.crypto.state", encrypted_state, "");
1516 if (strcmp(encrypted_state, "encrypted") ) {
1517 SLOGE("not running with encryption, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001518 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001519 }
1520
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001521 // crypto_complete is full disk encrypted status
Paul Crowley38132a12016-02-09 09:50:32 +00001522 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001523 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Paul Lawrence05335c32015-03-05 09:46:23 -08001524 }
1525
Ken Sumrall160b4d62013-04-22 12:15:39 -07001526 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall56ad03c2013-02-13 13:00:19 -08001527 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumralle5032c42012-04-01 23:58:44 -07001528
Ken Sumralle1a45852011-12-14 21:24:27 -08001529 /*
1530 * Only report this error if key_loc is a file and it exists.
1531 * If the device was never encrypted, and /data is not mountable for
1532 * some reason, returning 1 should prevent the UI from presenting the
1533 * a "enter password" screen, or worse, a "press button to wipe the
1534 * device" screen.
1535 */
1536 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
1537 SLOGE("master key file does not exist, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001538 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumralle1a45852011-12-14 21:24:27 -08001539 } else {
1540 SLOGE("Error getting crypt footer and key\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001541 return CRYPTO_COMPLETE_BAD_METADATA;
Ken Sumralle1a45852011-12-14 21:24:27 -08001542 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001543 }
1544
Paul Lawrence74f29f12014-08-28 15:54:10 -07001545 // Test for possible error flags
1546 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS){
1547 SLOGE("Encryption process is partway completed\n");
1548 return CRYPTO_COMPLETE_PARTIAL;
1549 }
1550
1551 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE){
1552 SLOGE("Encryption process was interrupted but cannot continue\n");
1553 return CRYPTO_COMPLETE_INCONSISTENT;
1554 }
1555
1556 if (crypt_ftr.flags & CRYPT_DATA_CORRUPT){
1557 SLOGE("Encryption is successful but data is corrupt\n");
1558 return CRYPTO_COMPLETE_CORRUPT;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001559 }
1560
1561 /* We passed the test! We shall diminish, and return to the west */
Paul Lawrence74f29f12014-08-28 15:54:10 -07001562 return CRYPTO_COMPLETE_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001563}
1564
Paul Lawrencef4faa572014-01-29 13:31:03 -08001565static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
Wei Wang4375f1b2017-02-24 17:43:01 -08001566 const char *passwd, const char *mount_point, const char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001567{
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001568 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001569 unsigned char decrypted_master_key[32];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001570 char crypto_blkdev[MAXPATHLEN];
1571 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001572 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001573 unsigned int orig_failed_decrypt_count;
1574 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001575 int use_keymaster = 0;
1576 int upgrade = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001577 unsigned char* intermediate_key = 0;
1578 size_t intermediate_key_size = 0;
Wei Wang4375f1b2017-02-24 17:43:01 -08001579 int N = 1 << crypt_ftr->N_factor;
1580 int r = 1 << crypt_ftr->r_factor;
1581 int p = 1 << crypt_ftr->p_factor;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001582
Paul Lawrencef4faa572014-01-29 13:31:03 -08001583 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1584 orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001585
Paul Lawrencef4faa572014-01-29 13:31:03 -08001586 if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001587 if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr,
1588 &intermediate_key, &intermediate_key_size)) {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001589 SLOGE("Failed to decrypt master key\n");
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001590 rc = -1;
1591 goto errout;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001592 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001593 }
1594
Paul Lawrencef4faa572014-01-29 13:31:03 -08001595 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
1596
Paul Lawrence74f29f12014-08-28 15:54:10 -07001597 // Create crypto block device - all (non fatal) code paths
1598 // need it
Paul Lawrencef4faa572014-01-29 13:31:03 -08001599 if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key,
1600 real_blkdev, crypto_blkdev, label)) {
Paul Lawrence74f29f12014-08-28 15:54:10 -07001601 SLOGE("Error creating decrypted block device\n");
1602 rc = -1;
1603 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001604 }
1605
Paul Lawrence74f29f12014-08-28 15:54:10 -07001606 /* Work out if the problem is the password or the data */
1607 unsigned char scrypted_intermediate_key[sizeof(crypt_ftr->
1608 scrypted_intermediate_key)];
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001609
Paul Lawrence74f29f12014-08-28 15:54:10 -07001610 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
1611 crypt_ftr->salt, sizeof(crypt_ftr->salt),
1612 N, r, p, scrypted_intermediate_key,
1613 sizeof(scrypted_intermediate_key));
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001614
Paul Lawrence74f29f12014-08-28 15:54:10 -07001615 // Does the key match the crypto footer?
1616 if (rc == 0 && memcmp(scrypted_intermediate_key,
1617 crypt_ftr->scrypted_intermediate_key,
1618 sizeof(scrypted_intermediate_key)) == 0) {
1619 SLOGI("Password matches");
1620 rc = 0;
1621 } else {
1622 /* Try mounting the file system anyway, just in case the problem's with
1623 * the footer, not the key. */
George Burgess IV605d7ae2016-02-29 13:39:17 -08001624 snprintf(tmp_mount_point, sizeof(tmp_mount_point), "%s/tmp_mnt",
1625 mount_point);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001626 mkdir(tmp_mount_point, 0755);
1627 if (fs_mgr_do_mount(fstab, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
1628 SLOGE("Error temp mounting decrypted block device\n");
1629 delete_crypto_blk_dev(label);
1630
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001631 rc = ++crypt_ftr->failed_decrypt_count;
1632 put_crypt_ftr_and_key(crypt_ftr);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001633 } else {
1634 /* Success! */
1635 SLOGI("Password did not match but decrypted drive mounted - continue");
1636 umount(tmp_mount_point);
1637 rc = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001638 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001639 }
1640
1641 if (rc == 0) {
1642 crypt_ftr->failed_decrypt_count = 0;
Paul Lawrence72b8b822014-10-05 12:57:37 -07001643 if (orig_failed_decrypt_count != 0) {
1644 put_crypt_ftr_and_key(crypt_ftr);
1645 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001646
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001647 /* Save the name of the crypto block device
Paul Lawrence74f29f12014-08-28 15:54:10 -07001648 * so we can mount it when restarting the framework. */
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001649 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -06001650
1651 /* Also save a the master key so we can reencrypted the key
Paul Lawrence74f29f12014-08-28 15:54:10 -07001652 * the key when we want to change the password on it. */
Jason parks70a4b3f2011-01-28 10:10:47 -06001653 memcpy(saved_master_key, decrypted_master_key, KEY_LEN_BYTES);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001654 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -06001655 master_key_saved = 1;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001656 SLOGD("%s(): Master key saved\n", __FUNCTION__);
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001657 rc = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001658
Paul Lawrence74f29f12014-08-28 15:54:10 -07001659 // Upgrade if we're not using the latest KDF.
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001660 use_keymaster = keymaster_check_compatibility();
1661 if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Shawn Willden47ba10d2014-09-03 17:07:06 -06001662 // Don't allow downgrade
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001663 } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) {
1664 crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1665 upgrade = 1;
1666 } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001667 crypt_ftr->kdf_type = KDF_SCRYPT;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001668 upgrade = 1;
1669 }
1670
1671 if (upgrade) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001672 rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1673 crypt_ftr->master_key, crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001674 if (!rc) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001675 rc = put_crypt_ftr_and_key(crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001676 }
1677 SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
Paul Lawrenceb2f682b2014-09-08 11:28:19 -07001678
1679 // Do not fail even if upgrade failed - machine is bootable
1680 // Note that if this code is ever hit, there is a *serious* problem
1681 // since KDFs should never fail. You *must* fix the kdf before
1682 // proceeding!
1683 if (rc) {
1684 SLOGW("Upgrade failed with error %d,"
1685 " but continuing with previous state",
1686 rc);
1687 rc = 0;
1688 }
JP Abgrall7bdfa522013-11-15 13:42:56 -08001689 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001690 }
1691
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001692 errout:
1693 if (intermediate_key) {
1694 memset(intermediate_key, 0, intermediate_key_size);
1695 free(intermediate_key);
1696 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001697 return rc;
1698}
1699
Ken Sumrall29d8da82011-05-18 17:20:07 -07001700/*
Jeff Sharkey9c484982015-03-31 10:35:33 -07001701 * Called by vold when it's asked to mount an encrypted external
1702 * storage volume. The incoming partition has no crypto header/footer,
1703 * as any metadata is been stored in a separate, small partition.
1704 *
1705 * out_crypto_blkdev must be MAXPATHLEN.
Ken Sumrall29d8da82011-05-18 17:20:07 -07001706 */
Jeff Sharkey9c484982015-03-31 10:35:33 -07001707int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev,
1708 const unsigned char* key, int keysize, char* out_crypto_blkdev) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001709 int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001710 if (fd == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001711 SLOGE("Failed to open %s: %s", real_blkdev, strerror(errno));
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001712 return -1;
1713 }
1714
1715 unsigned long nr_sec = 0;
1716 get_blkdev_size(fd, &nr_sec);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001717 close(fd);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001718
Ken Sumrall29d8da82011-05-18 17:20:07 -07001719 if (nr_sec == 0) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001720 SLOGE("Failed to get size of %s: %s", real_blkdev, strerror(errno));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001721 return -1;
1722 }
1723
Jeff Sharkey9c484982015-03-31 10:35:33 -07001724 struct crypt_mnt_ftr ext_crypt_ftr;
1725 memset(&ext_crypt_ftr, 0, sizeof(ext_crypt_ftr));
1726 ext_crypt_ftr.fs_size = nr_sec;
1727 ext_crypt_ftr.keysize = keysize;
Jeff Sharkey32ebb732017-03-27 16:18:50 -06001728 strlcpy((char*) ext_crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256",
1729 MAX_CRYPTO_TYPE_NAME_LEN);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001730
Jeff Sharkey9c484982015-03-31 10:35:33 -07001731 return create_crypto_blk_dev(&ext_crypt_ftr, key, real_blkdev,
1732 out_crypto_blkdev, label);
1733}
Ken Sumrall29d8da82011-05-18 17:20:07 -07001734
Jeff Sharkey9c484982015-03-31 10:35:33 -07001735/*
1736 * Called by vold when it's asked to unmount an encrypted external
1737 * storage volume.
1738 */
1739int cryptfs_revert_ext_volume(const char* label) {
1740 return delete_crypto_blk_dev((char*) label);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001741}
1742
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001743int cryptfs_crypto_complete(void)
1744{
1745 return do_crypto_complete("/data");
1746}
1747
Paul Lawrencef4faa572014-01-29 13:31:03 -08001748int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr)
1749{
1750 char encrypted_state[PROPERTY_VALUE_MAX];
1751 property_get("ro.crypto.state", encrypted_state, "");
1752 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
1753 SLOGE("encrypted fs already validated or not running with encryption,"
1754 " aborting");
1755 return -1;
1756 }
1757
1758 if (get_crypt_ftr_and_key(crypt_ftr)) {
1759 SLOGE("Error getting crypt footer and key");
1760 return -1;
1761 }
1762
1763 return 0;
1764}
1765
Wei Wang4375f1b2017-02-24 17:43:01 -08001766int cryptfs_check_passwd(const char *passwd)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001767{
Paul Lawrence05335c32015-03-05 09:46:23 -08001768 SLOGI("cryptfs_check_passwd");
Paul Crowley38132a12016-02-09 09:50:32 +00001769 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001770 SLOGE("cryptfs_check_passwd not valid for file encryption");
1771 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08001772 }
1773
Paul Lawrencef4faa572014-01-29 13:31:03 -08001774 struct crypt_mnt_ftr crypt_ftr;
1775 int rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001776
Paul Lawrencef4faa572014-01-29 13:31:03 -08001777 rc = check_unmounted_and_get_ftr(&crypt_ftr);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001778 if (rc) {
1779 SLOGE("Could not get footer");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001780 return rc;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001781 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001782
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001783 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001784 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
1785 if (rc) {
1786 SLOGE("Password did not match");
1787 return rc;
1788 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08001789
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001790 if (crypt_ftr.flags & CRYPT_FORCE_COMPLETE) {
1791 // Here we have a default actual password but a real password
1792 // we must test against the scrypted value
1793 // First, we must delete the crypto block device that
1794 // test_mount_encrypted_fs leaves behind as a side effect
1795 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
1796 rc = test_mount_encrypted_fs(&crypt_ftr, DEFAULT_PASSWORD,
1797 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
1798 if (rc) {
1799 SLOGE("Default password did not match on reboot encryption");
1800 return rc;
1801 }
1802
1803 crypt_ftr.flags &= ~CRYPT_FORCE_COMPLETE;
1804 put_crypt_ftr_and_key(&crypt_ftr);
1805 rc = cryptfs_changepw(crypt_ftr.crypt_type, passwd);
1806 if (rc) {
1807 SLOGE("Could not change password on reboot encryption");
1808 return rc;
1809 }
1810 }
1811
1812 if (crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
Paul Lawrence399317e2014-03-10 13:20:50 -07001813 cryptfs_clear_password();
1814 password = strdup(passwd);
1815 struct timespec now;
1816 clock_gettime(CLOCK_BOOTTIME, &now);
1817 password_expiry_time = now.tv_sec + password_max_age_seconds;
Paul Lawrence684dbdf2014-02-07 12:07:22 -08001818 }
1819
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001820 return rc;
1821}
1822
Ken Sumrall3ad90722011-10-04 20:38:29 -07001823int cryptfs_verify_passwd(char *passwd)
1824{
1825 struct crypt_mnt_ftr crypt_ftr;
1826 /* Allocate enough space for a 256 bit key, but we may use less */
Ken Sumrall160b4d62013-04-22 12:15:39 -07001827 unsigned char decrypted_master_key[32];
Ken Sumrall3ad90722011-10-04 20:38:29 -07001828 char encrypted_state[PROPERTY_VALUE_MAX];
1829 int rc;
1830
1831 property_get("ro.crypto.state", encrypted_state, "");
1832 if (strcmp(encrypted_state, "encrypted") ) {
1833 SLOGE("device not encrypted, aborting");
1834 return -2;
1835 }
1836
1837 if (!master_key_saved) {
1838 SLOGE("encrypted fs not yet mounted, aborting");
1839 return -1;
1840 }
1841
1842 if (!saved_mount_point) {
1843 SLOGE("encrypted fs failed to save mount point, aborting");
1844 return -1;
1845 }
1846
Ken Sumrall160b4d62013-04-22 12:15:39 -07001847 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall3ad90722011-10-04 20:38:29 -07001848 SLOGE("Error getting crypt footer and key\n");
1849 return -1;
1850 }
1851
1852 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
1853 /* If the device has no password, then just say the password is valid */
1854 rc = 0;
1855 } else {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001856 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001857 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
1858 /* They match, the password is correct */
1859 rc = 0;
1860 } else {
1861 /* If incorrect, sleep for a bit to prevent dictionary attacks */
1862 sleep(1);
1863 rc = 1;
1864 }
1865 }
1866
1867 return rc;
1868}
1869
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001870/* Initialize a crypt_mnt_ftr structure. The keysize is
1871 * defaulted to 16 bytes, and the filesystem size to 0.
1872 * Presumably, at a minimum, the caller will update the
1873 * filesystem size and crypto_type_name after calling this function.
1874 */
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001875static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001876{
Ken Sumrall160b4d62013-04-22 12:15:39 -07001877 off64_t off;
1878
1879 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001880 ftr->magic = CRYPT_MNT_MAGIC;
Kenny Rootc96a5f82013-06-14 12:08:28 -07001881 ftr->major_version = CURRENT_MAJOR_VERSION;
1882 ftr->minor_version = CURRENT_MINOR_VERSION;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001883 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Jason parks70a4b3f2011-01-28 10:10:47 -06001884 ftr->keysize = KEY_LEN_BYTES;
Ken Sumrall160b4d62013-04-22 12:15:39 -07001885
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001886 switch (keymaster_check_compatibility()) {
1887 case 1:
1888 ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1889 break;
1890
1891 case 0:
1892 ftr->kdf_type = KDF_SCRYPT;
1893 break;
1894
1895 default:
1896 SLOGE("keymaster_check_compatibility failed");
1897 return -1;
1898 }
1899
Kenny Rootc4c70f12013-06-14 12:11:38 -07001900 get_device_scrypt_params(ftr);
1901
Ken Sumrall160b4d62013-04-22 12:15:39 -07001902 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
1903 if (get_crypt_ftr_info(NULL, &off) == 0) {
1904 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
1905 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
1906 ftr->persist_data_size;
1907 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001908
1909 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001910}
1911
Ken Sumrall29d8da82011-05-18 17:20:07 -07001912static int cryptfs_enable_wipe(char *crypto_blkdev, off64_t size, int type)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001913{
Ken Sumralle550f782013-08-20 13:48:23 -07001914 const char *args[10];
1915 char size_str[32]; /* Must be large enough to hold a %lld and null byte */
1916 int num_args;
1917 int status;
1918 int tmp;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001919 int rc = -1;
1920
Ken Sumrall29d8da82011-05-18 17:20:07 -07001921 if (type == EXT4_FS) {
Adrien Schildknechte0f409c2016-11-21 15:14:37 -08001922#ifdef TARGET_USES_MKE2FS
1923 args[0] = "/system/bin/mke2fs";
1924 args[1] = "-M";
1925 args[2] = "/data";
1926 args[3] = "-b";
1927 args[4] = "4096";
1928 args[5] = "-t";
1929 args[6] = "ext4";
1930 args[7] = crypto_blkdev;
1931 snprintf(size_str, sizeof(size_str), "%" PRId64, size / (4096 / 512));
1932 args[8] = size_str;
1933 num_args = 9;
1934#else
Ken Sumralle550f782013-08-20 13:48:23 -07001935 args[0] = "/system/bin/make_ext4fs";
1936 args[1] = "-a";
1937 args[2] = "/data";
1938 args[3] = "-l";
Elliott Hughes73737162014-06-25 17:27:42 -07001939 snprintf(size_str, sizeof(size_str), "%" PRId64, size * 512);
Ken Sumralle550f782013-08-20 13:48:23 -07001940 args[4] = size_str;
1941 args[5] = crypto_blkdev;
1942 num_args = 6;
Adrien Schildknechte0f409c2016-11-21 15:14:37 -08001943#endif
Ken Sumralle550f782013-08-20 13:48:23 -07001944 SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n",
1945 args[0], args[1], args[2], args[3], args[4], args[5]);
JP Abgrall62c7af32014-06-16 13:01:23 -07001946 } else if (type == F2FS_FS) {
1947 args[0] = "/system/bin/mkfs.f2fs";
1948 args[1] = "-t";
1949 args[2] = "-d1";
1950 args[3] = crypto_blkdev;
Elliott Hughes73737162014-06-25 17:27:42 -07001951 snprintf(size_str, sizeof(size_str), "%" PRId64, size);
JP Abgrall62c7af32014-06-16 13:01:23 -07001952 args[4] = size_str;
1953 num_args = 5;
1954 SLOGI("Making empty filesystem with command %s %s %s %s %s\n",
1955 args[0], args[1], args[2], args[3], args[4]);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001956 } else {
1957 SLOGE("cryptfs_enable_wipe(): unknown filesystem type %d\n", type);
1958 return -1;
1959 }
1960
Ken Sumralle550f782013-08-20 13:48:23 -07001961 tmp = android_fork_execvp(num_args, (char **)args, &status, false, true);
1962
1963 if (tmp != 0) {
1964 SLOGE("Error creating empty filesystem on %s due to logwrap error\n", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001965 } else {
Ken Sumralle550f782013-08-20 13:48:23 -07001966 if (WIFEXITED(status)) {
1967 if (WEXITSTATUS(status)) {
1968 SLOGE("Error creating filesystem on %s, exit status %d ",
1969 crypto_blkdev, WEXITSTATUS(status));
1970 } else {
1971 SLOGD("Successfully created filesystem on %s\n", crypto_blkdev);
1972 rc = 0;
1973 }
1974 } else {
1975 SLOGE("Error creating filesystem on %s, did not exit normally\n", crypto_blkdev);
1976 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001977 }
1978
1979 return rc;
1980}
1981
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001982#define CRYPT_INPLACE_BUFSIZE 4096
Paul Lawrence87999172014-02-20 12:21:31 -08001983#define CRYPT_SECTORS_PER_BUFSIZE (CRYPT_INPLACE_BUFSIZE / CRYPT_SECTOR_SIZE)
1984#define CRYPT_SECTOR_SIZE 512
Paul Lawrenceae59fe62014-01-21 08:23:27 -08001985
1986/* aligned 32K writes tends to make flash happy.
1987 * SD card association recommends it.
1988 */
1989#define BLOCKS_AT_A_TIME 8
1990
1991struct encryptGroupsData
1992{
1993 int realfd;
1994 int cryptofd;
1995 off64_t numblocks;
1996 off64_t one_pct, cur_pct, new_pct;
1997 off64_t blocks_already_done, tot_numblocks;
Paul Lawrence58c58cf2014-06-04 13:12:21 -07001998 off64_t used_blocks_already_done, tot_used_blocks;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08001999 char* real_blkdev, * crypto_blkdev;
2000 int count;
2001 off64_t offset;
2002 char* buffer;
Paul Lawrence87999172014-02-20 12:21:31 -08002003 off64_t last_written_sector;
2004 int completed;
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002005 time_t time_started;
2006 int remaining_time;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002007};
2008
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002009static void update_progress(struct encryptGroupsData* data, int is_used)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002010{
2011 data->blocks_already_done++;
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002012
2013 if (is_used) {
2014 data->used_blocks_already_done++;
2015 }
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002016 if (data->tot_used_blocks) {
2017 data->new_pct = data->used_blocks_already_done / data->one_pct;
2018 } else {
2019 data->new_pct = data->blocks_already_done / data->one_pct;
2020 }
2021
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002022 if (data->new_pct > data->cur_pct) {
2023 char buf[8];
2024 data->cur_pct = data->new_pct;
Elliott Hughescb33f572014-06-25 18:25:11 -07002025 snprintf(buf, sizeof(buf), "%" PRId64, data->cur_pct);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002026 property_set("vold.encrypt_progress", buf);
2027 }
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002028
2029 if (data->cur_pct >= 5) {
Paul Lawrence9c58a872014-09-30 09:12:51 -07002030 struct timespec time_now;
2031 if (clock_gettime(CLOCK_MONOTONIC, &time_now)) {
2032 SLOGW("Error getting time");
2033 } else {
2034 double elapsed_time = difftime(time_now.tv_sec, data->time_started);
2035 off64_t remaining_blocks = data->tot_used_blocks
2036 - data->used_blocks_already_done;
2037 int remaining_time = (int)(elapsed_time * remaining_blocks
2038 / data->used_blocks_already_done);
Paul Lawrence71577502014-08-13 14:55:55 -07002039
Paul Lawrence9c58a872014-09-30 09:12:51 -07002040 // Change time only if not yet set, lower, or a lot higher for
2041 // best user experience
2042 if (data->remaining_time == -1
2043 || remaining_time < data->remaining_time
2044 || remaining_time > data->remaining_time + 60) {
2045 char buf[8];
2046 snprintf(buf, sizeof(buf), "%d", remaining_time);
2047 property_set("vold.encrypt_time_remaining", buf);
2048 data->remaining_time = remaining_time;
2049 }
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002050 }
2051 }
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002052}
2053
Paul Lawrence3846be12014-09-22 11:33:54 -07002054static void log_progress(struct encryptGroupsData const* data, bool completed)
2055{
2056 // Precondition - if completed data = 0 else data != 0
2057
2058 // Track progress so we can skip logging blocks
2059 static off64_t offset = -1;
2060
2061 // Need to close existing 'Encrypting from' log?
2062 if (completed || (offset != -1 && data->offset != offset)) {
2063 SLOGI("Encrypted to sector %" PRId64,
2064 offset / info.block_size * CRYPT_SECTOR_SIZE);
2065 offset = -1;
2066 }
2067
2068 // Need to start new 'Encrypting from' log?
2069 if (!completed && offset != data->offset) {
2070 SLOGI("Encrypting from sector %" PRId64,
2071 data->offset / info.block_size * CRYPT_SECTOR_SIZE);
2072 }
2073
2074 // Update offset
2075 if (!completed) {
2076 offset = data->offset + (off64_t)data->count * info.block_size;
2077 }
2078}
2079
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002080static int flush_outstanding_data(struct encryptGroupsData* data)
2081{
2082 if (data->count == 0) {
2083 return 0;
2084 }
2085
Elliott Hughes231bdba2014-06-25 18:36:19 -07002086 SLOGV("Copying %d blocks at offset %" PRIx64, data->count, data->offset);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002087
2088 if (pread64(data->realfd, data->buffer,
2089 info.block_size * data->count, data->offset)
2090 <= 0) {
2091 SLOGE("Error reading real_blkdev %s for inplace encrypt",
2092 data->real_blkdev);
2093 return -1;
2094 }
2095
2096 if (pwrite64(data->cryptofd, data->buffer,
2097 info.block_size * data->count, data->offset)
2098 <= 0) {
2099 SLOGE("Error writing crypto_blkdev %s for inplace encrypt",
2100 data->crypto_blkdev);
2101 return -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002102 } else {
Paul Lawrence3846be12014-09-22 11:33:54 -07002103 log_progress(data, false);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002104 }
2105
2106 data->count = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002107 data->last_written_sector = (data->offset + data->count)
2108 / info.block_size * CRYPT_SECTOR_SIZE - 1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002109 return 0;
2110}
2111
2112static int encrypt_groups(struct encryptGroupsData* data)
2113{
2114 unsigned int i;
2115 u8 *block_bitmap = 0;
2116 unsigned int block;
2117 off64_t ret;
2118 int rc = -1;
2119
Wei Wang4375f1b2017-02-24 17:43:01 -08002120 data->buffer = (char *)malloc(info.block_size * BLOCKS_AT_A_TIME);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002121 if (!data->buffer) {
2122 SLOGE("Failed to allocate crypto buffer");
2123 goto errout;
2124 }
2125
Wei Wang4375f1b2017-02-24 17:43:01 -08002126 block_bitmap = (u8 *)malloc(info.block_size);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002127 if (!block_bitmap) {
2128 SLOGE("failed to allocate block bitmap");
2129 goto errout;
2130 }
2131
2132 for (i = 0; i < aux_info.groups; ++i) {
2133 SLOGI("Encrypting group %d", i);
2134
2135 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
Wei Wang4375f1b2017-02-24 17:43:01 -08002136 u32 block_count = std::min(info.blocks_per_group,
2137 (u32)(aux_info.len_blocks - first_block));
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002138
2139 off64_t offset = (u64)info.block_size
2140 * aux_info.bg_desc[i].bg_block_bitmap;
2141
2142 ret = pread64(data->realfd, block_bitmap, info.block_size, offset);
2143 if (ret != (int)info.block_size) {
2144 SLOGE("failed to read all of block group bitmap %d", i);
2145 goto errout;
2146 }
2147
2148 offset = (u64)info.block_size * first_block;
2149
2150 data->count = 0;
2151
2152 for (block = 0; block < block_count; block++) {
liminghaoaa08e582016-01-06 10:30:49 +08002153 int used = (aux_info.bg_desc[i].bg_flags & EXT4_BG_BLOCK_UNINIT) ?
2154 0 : bitmap_get_bit(block_bitmap, block);
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002155 update_progress(data, used);
2156 if (used) {
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002157 if (data->count == 0) {
2158 data->offset = offset;
2159 }
2160 data->count++;
2161 } else {
2162 if (flush_outstanding_data(data)) {
2163 goto errout;
2164 }
2165 }
2166
2167 offset += info.block_size;
2168
2169 /* Write data if we are aligned or buffer size reached */
2170 if (offset % (info.block_size * BLOCKS_AT_A_TIME) == 0
2171 || data->count == BLOCKS_AT_A_TIME) {
2172 if (flush_outstanding_data(data)) {
2173 goto errout;
2174 }
2175 }
Paul Lawrence87999172014-02-20 12:21:31 -08002176
Paul Lawrence73d7a022014-06-09 14:10:09 -07002177 if (!is_battery_ok_to_continue()) {
Paul Lawrence87999172014-02-20 12:21:31 -08002178 SLOGE("Stopping encryption due to low battery");
2179 rc = 0;
2180 goto errout;
2181 }
2182
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002183 }
2184 if (flush_outstanding_data(data)) {
2185 goto errout;
2186 }
2187 }
2188
Paul Lawrence87999172014-02-20 12:21:31 -08002189 data->completed = 1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002190 rc = 0;
2191
2192errout:
Paul Lawrence3846be12014-09-22 11:33:54 -07002193 log_progress(0, true);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002194 free(data->buffer);
2195 free(block_bitmap);
2196 return rc;
2197}
2198
2199static int cryptfs_enable_inplace_ext4(char *crypto_blkdev,
2200 char *real_blkdev,
2201 off64_t size,
2202 off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002203 off64_t tot_size,
2204 off64_t previously_encrypted_upto)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002205{
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002206 u32 i;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002207 struct encryptGroupsData data;
Paul Lawrence74f29f12014-08-28 15:54:10 -07002208 int rc; // Can't initialize without causing warning -Wclobbered
Wei Wang4375f1b2017-02-24 17:43:01 -08002209 struct timespec time_started = {0};
2210 int retries = RETRY_MOUNT_ATTEMPTS;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002211
Paul Lawrence87999172014-02-20 12:21:31 -08002212 if (previously_encrypted_upto > *size_already_done) {
2213 SLOGD("Not fast encrypting since resuming part way through");
2214 return -1;
2215 }
2216
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002217 memset(&data, 0, sizeof(data));
2218 data.real_blkdev = real_blkdev;
2219 data.crypto_blkdev = crypto_blkdev;
2220
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002221 if ( (data.realfd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
JP Abgrall3334c6a2014-10-10 15:52:11 -07002222 SLOGE("Error opening real_blkdev %s for inplace encrypt. err=%d(%s)\n",
2223 real_blkdev, errno, strerror(errno));
Paul Lawrence74f29f12014-08-28 15:54:10 -07002224 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002225 goto errout;
2226 }
2227
David Ng82fd8042015-01-21 13:55:21 -08002228 // Wait until the block device appears. Re-use the mount retry values since it is reasonable.
David Ng82fd8042015-01-21 13:55:21 -08002229 while ((data.cryptofd = open(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
2230 if (--retries) {
2231 SLOGE("Error opening crypto_blkdev %s for ext4 inplace encrypt. err=%d(%s), retrying\n",
2232 crypto_blkdev, errno, strerror(errno));
2233 sleep(RETRY_MOUNT_DELAY_SECONDS);
2234 } else {
2235 SLOGE("Error opening crypto_blkdev %s for ext4 inplace encrypt. err=%d(%s)\n",
2236 crypto_blkdev, errno, strerror(errno));
2237 rc = ENABLE_INPLACE_ERR_DEV;
2238 goto errout;
2239 }
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002240 }
2241
Jeff Sharkey32ebb732017-03-27 16:18:50 -06002242 if (setjmp(setjmp_env)) { // NOLINT
JP Abgrall7fc1de82014-10-10 18:43:41 -07002243 SLOGE("Reading ext4 extent caused an exception\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07002244 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002245 goto errout;
2246 }
2247
2248 if (read_ext(data.realfd, 0) != 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002249 SLOGE("Failed to read ext4 extent\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07002250 rc = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002251 goto errout;
2252 }
2253
2254 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2255 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2256 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
2257
JP Abgrall7fc1de82014-10-10 18:43:41 -07002258 SLOGI("Encrypting ext4 filesystem in place...");
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002259
Paul Lawrence58c58cf2014-06-04 13:12:21 -07002260 data.tot_used_blocks = data.numblocks;
2261 for (i = 0; i < aux_info.groups; ++i) {
2262 data.tot_used_blocks -= aux_info.bg_desc[i].bg_free_blocks_count;
2263 }
2264
2265 data.one_pct = data.tot_used_blocks / 100;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002266 data.cur_pct = 0;
Paul Lawrence9c58a872014-09-30 09:12:51 -07002267
Paul Lawrence9c58a872014-09-30 09:12:51 -07002268 if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
2269 SLOGW("Error getting time at start");
2270 // Note - continue anyway - we'll run with 0
2271 }
2272 data.time_started = time_started.tv_sec;
Paul Lawrencea96d9c92014-06-04 14:05:01 -07002273 data.remaining_time = -1;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002274
2275 rc = encrypt_groups(&data);
2276 if (rc) {
2277 SLOGE("Error encrypting groups");
2278 goto errout;
2279 }
2280
Paul Lawrence87999172014-02-20 12:21:31 -08002281 *size_already_done += data.completed ? size : data.last_written_sector;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002282 rc = 0;
2283
2284errout:
2285 close(data.realfd);
2286 close(data.cryptofd);
2287
2288 return rc;
2289}
2290
Paul Lawrence3846be12014-09-22 11:33:54 -07002291static void log_progress_f2fs(u64 block, bool completed)
2292{
2293 // Precondition - if completed data = 0 else data != 0
2294
2295 // Track progress so we can skip logging blocks
2296 static u64 last_block = (u64)-1;
2297
2298 // Need to close existing 'Encrypting from' log?
2299 if (completed || (last_block != (u64)-1 && block != last_block + 1)) {
2300 SLOGI("Encrypted to block %" PRId64, last_block);
2301 last_block = -1;
2302 }
2303
2304 // Need to start new 'Encrypting from' log?
2305 if (!completed && (last_block == (u64)-1 || block != last_block + 1)) {
2306 SLOGI("Encrypting from block %" PRId64, block);
2307 }
2308
2309 // Update offset
2310 if (!completed) {
2311 last_block = block;
2312 }
2313}
2314
Daniel Rosenberge82df162014-08-15 22:19:23 +00002315static int encrypt_one_block_f2fs(u64 pos, void *data)
2316{
2317 struct encryptGroupsData *priv_dat = (struct encryptGroupsData *)data;
2318
2319 priv_dat->blocks_already_done = pos - 1;
2320 update_progress(priv_dat, 1);
2321
2322 off64_t offset = pos * CRYPT_INPLACE_BUFSIZE;
2323
2324 if (pread64(priv_dat->realfd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002325 SLOGE("Error reading real_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002326 return -1;
2327 }
2328
2329 if (pwrite64(priv_dat->cryptofd, priv_dat->buffer, CRYPT_INPLACE_BUFSIZE, offset) <= 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002330 SLOGE("Error writing crypto_blkdev %s for f2fs inplace encrypt", priv_dat->crypto_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002331 return -1;
2332 } else {
Paul Lawrence3846be12014-09-22 11:33:54 -07002333 log_progress_f2fs(pos, false);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002334 }
2335
2336 return 0;
2337}
2338
2339static int cryptfs_enable_inplace_f2fs(char *crypto_blkdev,
2340 char *real_blkdev,
2341 off64_t size,
2342 off64_t *size_already_done,
2343 off64_t tot_size,
2344 off64_t previously_encrypted_upto)
2345{
Daniel Rosenberge82df162014-08-15 22:19:23 +00002346 struct encryptGroupsData data;
2347 struct f2fs_info *f2fs_info = NULL;
JP Abgrall7fc1de82014-10-10 18:43:41 -07002348 int rc = ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002349 if (previously_encrypted_upto > *size_already_done) {
2350 SLOGD("Not fast encrypting since resuming part way through");
JP Abgrall7fc1de82014-10-10 18:43:41 -07002351 return ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002352 }
2353 memset(&data, 0, sizeof(data));
2354 data.real_blkdev = real_blkdev;
2355 data.crypto_blkdev = crypto_blkdev;
2356 data.realfd = -1;
2357 data.cryptofd = -1;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002358 if ( (data.realfd = open64(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002359 SLOGE("Error opening real_blkdev %s for f2fs inplace encrypt\n",
Daniel Rosenberge82df162014-08-15 22:19:23 +00002360 real_blkdev);
2361 goto errout;
2362 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002363 if ( (data.cryptofd = open64(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002364 SLOGE("Error opening crypto_blkdev %s for f2fs inplace encrypt. err=%d(%s)\n",
JP Abgrall3334c6a2014-10-10 15:52:11 -07002365 crypto_blkdev, errno, strerror(errno));
JP Abgrall7fc1de82014-10-10 18:43:41 -07002366 rc = ENABLE_INPLACE_ERR_DEV;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002367 goto errout;
2368 }
2369
2370 f2fs_info = generate_f2fs_info(data.realfd);
2371 if (!f2fs_info)
2372 goto errout;
2373
2374 data.numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2375 data.tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2376 data.blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
2377
2378 data.tot_used_blocks = get_num_blocks_used(f2fs_info);
2379
2380 data.one_pct = data.tot_used_blocks / 100;
2381 data.cur_pct = 0;
2382 data.time_started = time(NULL);
2383 data.remaining_time = -1;
2384
Wei Wang4375f1b2017-02-24 17:43:01 -08002385 data.buffer = (char *)malloc(f2fs_info->block_size);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002386 if (!data.buffer) {
2387 SLOGE("Failed to allocate crypto buffer");
2388 goto errout;
2389 }
2390
2391 data.count = 0;
2392
2393 /* Currently, this either runs to completion, or hits a nonrecoverable error */
2394 rc = run_on_used_blocks(data.blocks_already_done, f2fs_info, &encrypt_one_block_f2fs, &data);
2395
2396 if (rc) {
JP Abgrall7fc1de82014-10-10 18:43:41 -07002397 SLOGE("Error in running over f2fs blocks");
2398 rc = ENABLE_INPLACE_ERR_OTHER;
Daniel Rosenberge82df162014-08-15 22:19:23 +00002399 goto errout;
2400 }
2401
2402 *size_already_done += size;
2403 rc = 0;
2404
2405errout:
2406 if (rc)
2407 SLOGE("Failed to encrypt f2fs filesystem on %s", real_blkdev);
2408
Paul Lawrence3846be12014-09-22 11:33:54 -07002409 log_progress_f2fs(0, true);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002410 free(f2fs_info);
2411 free(data.buffer);
2412 close(data.realfd);
2413 close(data.cryptofd);
2414
2415 return rc;
2416}
2417
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002418static int cryptfs_enable_inplace_full(char *crypto_blkdev, char *real_blkdev,
2419 off64_t size, off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002420 off64_t tot_size,
2421 off64_t previously_encrypted_upto)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002422{
2423 int realfd, cryptofd;
2424 char *buf[CRYPT_INPLACE_BUFSIZE];
JP Abgrall7fc1de82014-10-10 18:43:41 -07002425 int rc = ENABLE_INPLACE_ERR_OTHER;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002426 off64_t numblocks, i, remainder;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002427 off64_t one_pct, cur_pct, new_pct;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002428 off64_t blocks_already_done, tot_numblocks;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002429
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002430 if ( (realfd = open(real_blkdev, O_RDONLY|O_CLOEXEC)) < 0) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002431 SLOGE("Error opening real_blkdev %s for inplace encrypt\n", real_blkdev);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002432 return ENABLE_INPLACE_ERR_OTHER;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002433 }
2434
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002435 if ( (cryptofd = open(crypto_blkdev, O_WRONLY|O_CLOEXEC)) < 0) {
JP Abgrall3334c6a2014-10-10 15:52:11 -07002436 SLOGE("Error opening crypto_blkdev %s for inplace encrypt. err=%d(%s)\n",
2437 crypto_blkdev, errno, strerror(errno));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002438 close(realfd);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002439 return ENABLE_INPLACE_ERR_DEV;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002440 }
2441
2442 /* This is pretty much a simple loop of reading 4K, and writing 4K.
2443 * The size passed in is the number of 512 byte sectors in the filesystem.
2444 * So compute the number of whole 4K blocks we should read/write,
2445 * and the remainder.
2446 */
2447 numblocks = size / CRYPT_SECTORS_PER_BUFSIZE;
2448 remainder = size % CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002449 tot_numblocks = tot_size / CRYPT_SECTORS_PER_BUFSIZE;
2450 blocks_already_done = *size_already_done / CRYPT_SECTORS_PER_BUFSIZE;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002451
2452 SLOGE("Encrypting filesystem in place...");
2453
Paul Lawrence87999172014-02-20 12:21:31 -08002454 i = previously_encrypted_upto + 1 - *size_already_done;
2455
2456 if (lseek64(realfd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
2457 SLOGE("Cannot seek to previously encrypted point on %s", real_blkdev);
2458 goto errout;
2459 }
2460
2461 if (lseek64(cryptofd, i * CRYPT_SECTOR_SIZE, SEEK_SET) < 0) {
2462 SLOGE("Cannot seek to previously encrypted point on %s", crypto_blkdev);
2463 goto errout;
2464 }
2465
2466 for (;i < size && i % CRYPT_SECTORS_PER_BUFSIZE != 0; ++i) {
2467 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2468 SLOGE("Error reading initial sectors from real_blkdev %s for "
2469 "inplace encrypt\n", crypto_blkdev);
2470 goto errout;
2471 }
2472 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2473 SLOGE("Error writing initial sectors to crypto_blkdev %s for "
2474 "inplace encrypt\n", crypto_blkdev);
2475 goto errout;
2476 } else {
Elliott Hughescb33f572014-06-25 18:25:11 -07002477 SLOGI("Encrypted 1 block at %" PRId64, i);
Paul Lawrence87999172014-02-20 12:21:31 -08002478 }
2479 }
2480
Ken Sumrall29d8da82011-05-18 17:20:07 -07002481 one_pct = tot_numblocks / 100;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002482 cur_pct = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002483 /* process the majority of the filesystem in blocks */
Paul Lawrence87999172014-02-20 12:21:31 -08002484 for (i/=CRYPT_SECTORS_PER_BUFSIZE; i<numblocks; i++) {
Ken Sumrall29d8da82011-05-18 17:20:07 -07002485 new_pct = (i + blocks_already_done) / one_pct;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002486 if (new_pct > cur_pct) {
2487 char buf[8];
2488
2489 cur_pct = new_pct;
Elliott Hughes73737162014-06-25 17:27:42 -07002490 snprintf(buf, sizeof(buf), "%" PRId64, cur_pct);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002491 property_set("vold.encrypt_progress", buf);
2492 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002493 if (unix_read(realfd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Lawrence87999172014-02-20 12:21:31 -08002494 SLOGE("Error reading real_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002495 goto errout;
2496 }
2497 if (unix_write(cryptofd, buf, CRYPT_INPLACE_BUFSIZE) <= 0) {
Paul Lawrence87999172014-02-20 12:21:31 -08002498 SLOGE("Error writing crypto_blkdev %s for inplace encrypt", crypto_blkdev);
2499 goto errout;
2500 } else {
Elliott Hughescb33f572014-06-25 18:25:11 -07002501 SLOGD("Encrypted %d block at %" PRId64,
Paul Lawrence87999172014-02-20 12:21:31 -08002502 CRYPT_SECTORS_PER_BUFSIZE,
2503 i * CRYPT_SECTORS_PER_BUFSIZE);
2504 }
2505
Paul Lawrence73d7a022014-06-09 14:10:09 -07002506 if (!is_battery_ok_to_continue()) {
Paul Lawrence87999172014-02-20 12:21:31 -08002507 SLOGE("Stopping encryption due to low battery");
2508 *size_already_done += (i + 1) * CRYPT_SECTORS_PER_BUFSIZE - 1;
2509 rc = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002510 goto errout;
2511 }
2512 }
2513
2514 /* Do any remaining sectors */
2515 for (i=0; i<remainder; i++) {
Paul Lawrence87999172014-02-20 12:21:31 -08002516 if (unix_read(realfd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2517 SLOGE("Error reading final sectors from real_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002518 goto errout;
2519 }
Paul Lawrence87999172014-02-20 12:21:31 -08002520 if (unix_write(cryptofd, buf, CRYPT_SECTOR_SIZE) <= 0) {
2521 SLOGE("Error writing final sectors to crypto_blkdev %s for inplace encrypt", crypto_blkdev);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002522 goto errout;
Paul Lawrence87999172014-02-20 12:21:31 -08002523 } else {
2524 SLOGI("Encrypted 1 block at next location");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002525 }
2526 }
2527
Ken Sumrall29d8da82011-05-18 17:20:07 -07002528 *size_already_done += size;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002529 rc = 0;
2530
2531errout:
2532 close(realfd);
2533 close(cryptofd);
2534
2535 return rc;
2536}
2537
JP Abgrall7fc1de82014-10-10 18:43:41 -07002538/* returns on of the ENABLE_INPLACE_* return codes */
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002539static int cryptfs_enable_inplace(char *crypto_blkdev, char *real_blkdev,
2540 off64_t size, off64_t *size_already_done,
Paul Lawrence87999172014-02-20 12:21:31 -08002541 off64_t tot_size,
2542 off64_t previously_encrypted_upto)
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002543{
JP Abgrall7fc1de82014-10-10 18:43:41 -07002544 int rc_ext4, rc_f2fs, rc_full;
Paul Lawrence87999172014-02-20 12:21:31 -08002545 if (previously_encrypted_upto) {
Elliott Hughescb33f572014-06-25 18:25:11 -07002546 SLOGD("Continuing encryption from %" PRId64, previously_encrypted_upto);
Paul Lawrence87999172014-02-20 12:21:31 -08002547 }
2548
2549 if (*size_already_done + size < previously_encrypted_upto) {
2550 *size_already_done += size;
2551 return 0;
2552 }
2553
Daniel Rosenberge82df162014-08-15 22:19:23 +00002554 /* TODO: identify filesystem type.
2555 * As is, cryptfs_enable_inplace_ext4 will fail on an f2fs partition, and
2556 * then we will drop down to cryptfs_enable_inplace_f2fs.
2557 * */
JP Abgrall7fc1de82014-10-10 18:43:41 -07002558 if ((rc_ext4 = cryptfs_enable_inplace_ext4(crypto_blkdev, real_blkdev,
Daniel Rosenberge82df162014-08-15 22:19:23 +00002559 size, size_already_done,
JP Abgrall7fc1de82014-10-10 18:43:41 -07002560 tot_size, previously_encrypted_upto)) == 0) {
Daniel Rosenberge82df162014-08-15 22:19:23 +00002561 return 0;
2562 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002563 SLOGD("cryptfs_enable_inplace_ext4()=%d\n", rc_ext4);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002564
JP Abgrall7fc1de82014-10-10 18:43:41 -07002565 if ((rc_f2fs = cryptfs_enable_inplace_f2fs(crypto_blkdev, real_blkdev,
Daniel Rosenberge82df162014-08-15 22:19:23 +00002566 size, size_already_done,
JP Abgrall7fc1de82014-10-10 18:43:41 -07002567 tot_size, previously_encrypted_upto)) == 0) {
Daniel Rosenberge82df162014-08-15 22:19:23 +00002568 return 0;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002569 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002570 SLOGD("cryptfs_enable_inplace_f2fs()=%d\n", rc_f2fs);
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002571
JP Abgrall7fc1de82014-10-10 18:43:41 -07002572 rc_full = cryptfs_enable_inplace_full(crypto_blkdev, real_blkdev,
Paul Lawrence87999172014-02-20 12:21:31 -08002573 size, size_already_done, tot_size,
2574 previously_encrypted_upto);
JP Abgrall7fc1de82014-10-10 18:43:41 -07002575 SLOGD("cryptfs_enable_inplace_full()=%d\n", rc_full);
2576
2577 /* Hack for b/17898962, the following is the symptom... */
2578 if (rc_ext4 == ENABLE_INPLACE_ERR_DEV
2579 && rc_f2fs == ENABLE_INPLACE_ERR_DEV
2580 && rc_full == ENABLE_INPLACE_ERR_DEV) {
2581 return ENABLE_INPLACE_ERR_DEV;
2582 }
2583 return rc_full;
Paul Lawrenceae59fe62014-01-21 08:23:27 -08002584}
2585
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002586#define CRYPTO_ENABLE_WIPE 1
2587#define CRYPTO_ENABLE_INPLACE 2
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002588
2589#define FRAMEWORK_BOOT_WAIT 60
2590
Paul Lawrence87999172014-02-20 12:21:31 -08002591static int cryptfs_SHA256_fileblock(const char* filename, __le8* buf)
2592{
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002593 int fd = open(filename, O_RDONLY|O_CLOEXEC);
Paul Lawrence87999172014-02-20 12:21:31 -08002594 if (fd == -1) {
2595 SLOGE("Error opening file %s", filename);
2596 return -1;
2597 }
2598
2599 char block[CRYPT_INPLACE_BUFSIZE];
2600 memset(block, 0, sizeof(block));
2601 if (unix_read(fd, block, sizeof(block)) < 0) {
2602 SLOGE("Error reading file %s", filename);
2603 close(fd);
2604 return -1;
2605 }
2606
2607 close(fd);
2608
2609 SHA256_CTX c;
2610 SHA256_Init(&c);
2611 SHA256_Update(&c, block, sizeof(block));
2612 SHA256_Final(buf, &c);
2613
2614 return 0;
2615}
2616
JP Abgrall62c7af32014-06-16 13:01:23 -07002617static int get_fs_type(struct fstab_rec *rec)
2618{
2619 if (!strcmp(rec->fs_type, "ext4")) {
2620 return EXT4_FS;
2621 } else if (!strcmp(rec->fs_type, "f2fs")) {
2622 return F2FS_FS;
2623 } else {
2624 return -1;
2625 }
2626}
2627
Paul Lawrence87999172014-02-20 12:21:31 -08002628static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr *crypt_ftr, int how,
2629 char *crypto_blkdev, char *real_blkdev,
2630 int previously_encrypted_upto)
2631{
2632 off64_t cur_encryption_done=0, tot_encryption_size=0;
Tim Murray8439dc92014-12-15 11:56:11 -08002633 int rc = -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002634
Paul Lawrence73d7a022014-06-09 14:10:09 -07002635 if (!is_battery_ok_to_start()) {
2636 SLOGW("Not starting encryption due to low battery");
Paul Lawrence87999172014-02-20 12:21:31 -08002637 return 0;
2638 }
2639
2640 /* The size of the userdata partition, and add in the vold volumes below */
2641 tot_encryption_size = crypt_ftr->fs_size;
2642
2643 if (how == CRYPTO_ENABLE_WIPE) {
JP Abgrall62c7af32014-06-16 13:01:23 -07002644 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
2645 int fs_type = get_fs_type(rec);
2646 if (fs_type < 0) {
2647 SLOGE("cryptfs_enable: unsupported fs type %s\n", rec->fs_type);
2648 return -1;
2649 }
2650 rc = cryptfs_enable_wipe(crypto_blkdev, crypt_ftr->fs_size, fs_type);
Paul Lawrence87999172014-02-20 12:21:31 -08002651 } else if (how == CRYPTO_ENABLE_INPLACE) {
2652 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev,
2653 crypt_ftr->fs_size, &cur_encryption_done,
2654 tot_encryption_size,
2655 previously_encrypted_upto);
2656
JP Abgrall7fc1de82014-10-10 18:43:41 -07002657 if (rc == ENABLE_INPLACE_ERR_DEV) {
2658 /* Hack for b/17898962 */
2659 SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n");
2660 cryptfs_reboot(reboot);
2661 }
2662
Paul Lawrence73d7a022014-06-09 14:10:09 -07002663 if (!rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002664 crypt_ftr->encrypted_upto = cur_encryption_done;
2665 }
2666
Paul Lawrence73d7a022014-06-09 14:10:09 -07002667 if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002668 /* The inplace routine never actually sets the progress to 100% due
2669 * to the round down nature of integer division, so set it here */
2670 property_set("vold.encrypt_progress", "100");
2671 }
2672 } else {
2673 /* Shouldn't happen */
2674 SLOGE("cryptfs_enable: internal error, unknown option\n");
2675 rc = -1;
2676 }
2677
2678 return rc;
2679}
2680
Wei Wang4375f1b2017-02-24 17:43:01 -08002681int cryptfs_enable_internal(char *howarg, int crypt_type, const char *passwd,
Paul Lawrence569649f2015-09-09 12:13:00 -07002682 int no_ui)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002683{
2684 int how = 0;
Paul Lawrence87999172014-02-20 12:21:31 -08002685 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN];
Ken Sumrall160b4d62013-04-22 12:15:39 -07002686 unsigned char decrypted_master_key[KEY_LEN_BYTES];
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002687 int rc=-1, i;
Paul Lawrence87999172014-02-20 12:21:31 -08002688 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002689 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002690 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002691 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07002692 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall29d8da82011-05-18 17:20:07 -07002693 int num_vols;
Paul Lawrence87999172014-02-20 12:21:31 -08002694 off64_t previously_encrypted_upto = 0;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002695 bool rebootEncryption = false;
Wei Wang4375f1b2017-02-24 17:43:01 -08002696 bool onlyCreateHeader = false;
2697 int fd = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002698
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002699 if (!strcmp(howarg, "wipe")) {
2700 how = CRYPTO_ENABLE_WIPE;
2701 } else if (! strcmp(howarg, "inplace")) {
2702 how = CRYPTO_ENABLE_INPLACE;
2703 } else {
2704 /* Shouldn't happen, as CommandListener vets the args */
Ken Sumrall3ed82362011-01-28 23:31:16 -08002705 goto error_unencrypted;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002706 }
2707
Paul Lawrence87999172014-02-20 12:21:31 -08002708 if (how == CRYPTO_ENABLE_INPLACE
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002709 && get_crypt_ftr_and_key(&crypt_ftr) == 0) {
2710 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
2711 /* An encryption was underway and was interrupted */
2712 previously_encrypted_upto = crypt_ftr.encrypted_upto;
2713 crypt_ftr.encrypted_upto = 0;
2714 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002715
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002716 /* At this point, we are in an inconsistent state. Until we successfully
2717 complete encryption, a reboot will leave us broken. So mark the
2718 encryption failed in case that happens.
2719 On successfully completing encryption, remove this flag */
2720 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002721
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002722 put_crypt_ftr_and_key(&crypt_ftr);
2723 } else if (crypt_ftr.flags & CRYPT_FORCE_ENCRYPTION) {
2724 if (!check_ftr_sha(&crypt_ftr)) {
2725 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
2726 put_crypt_ftr_and_key(&crypt_ftr);
2727 goto error_unencrypted;
2728 }
2729
2730 /* Doing a reboot-encryption*/
2731 crypt_ftr.flags &= ~CRYPT_FORCE_ENCRYPTION;
2732 crypt_ftr.flags |= CRYPT_FORCE_COMPLETE;
2733 rebootEncryption = true;
2734 }
Paul Lawrence87999172014-02-20 12:21:31 -08002735 }
2736
2737 property_get("ro.crypto.state", encrypted_state, "");
2738 if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) {
2739 SLOGE("Device is already running encrypted, aborting");
2740 goto error_unencrypted;
2741 }
2742
2743 // TODO refactor fs_mgr_get_crypt_info to get both in one call
2744 fs_mgr_get_crypt_info(fstab, key_loc, 0, sizeof(key_loc));
Ken Sumrall56ad03c2013-02-13 13:00:19 -08002745 fs_mgr_get_crypt_info(fstab, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002746
Ken Sumrall3ed82362011-01-28 23:31:16 -08002747 /* Get the size of the real block device */
Wei Wang4375f1b2017-02-24 17:43:01 -08002748 fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002749 if (fd == -1) {
2750 SLOGE("Cannot open block device %s\n", real_blkdev);
2751 goto error_unencrypted;
2752 }
2753 unsigned long nr_sec;
2754 get_blkdev_size(fd, &nr_sec);
2755 if (nr_sec == 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002756 SLOGE("Cannot get size of block device %s\n", real_blkdev);
2757 goto error_unencrypted;
2758 }
2759 close(fd);
2760
2761 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002762 if ((how == CRYPTO_ENABLE_INPLACE) && (!strcmp(key_loc, KEY_IN_FOOTER))) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002763 unsigned int fs_size_sec, max_fs_size_sec;
Jim Millera70abc62014-08-15 02:00:45 +00002764 fs_size_sec = get_fs_size(real_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002765 if (fs_size_sec == 0)
2766 fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev);
2767
Paul Lawrence87999172014-02-20 12:21:31 -08002768 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002769
2770 if (fs_size_sec > max_fs_size_sec) {
2771 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
2772 goto error_unencrypted;
2773 }
2774 }
2775
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002776 /* Get a wakelock as this may take a while, and we don't want the
2777 * device to sleep on us. We'll grab a partial wakelock, and if the UI
2778 * wants to keep the screen on, it can grab a full wakelock.
2779 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002780 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002781 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
2782
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002783 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002784 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002785 */
2786 property_set("vold.decrypt", "trigger_shutdown_framework");
2787 SLOGD("Just asked init to shut down class main\n");
2788
Jeff Sharkey9c484982015-03-31 10:35:33 -07002789 /* Ask vold to unmount all devices that it manages */
2790 if (vold_unmountAll()) {
2791 SLOGE("Failed to unmount all vold managed devices");
Ken Sumrall2eaf7132011-01-14 12:45:48 -08002792 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002793
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002794 /* no_ui means we are being called from init, not settings.
2795 Now we always reboot from settings, so !no_ui means reboot
2796 */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002797 if (!no_ui) {
2798 /* Try fallback, which is to reboot and try there */
2799 onlyCreateHeader = true;
2800 FILE* breadcrumb = fopen(BREADCRUMB_FILE, "we");
2801 if (breadcrumb == 0) {
2802 SLOGE("Failed to create breadcrumb file");
2803 goto error_shutting_down;
2804 }
2805 fclose(breadcrumb);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002806 }
2807
2808 /* Do extra work for a better UX when doing the long inplace encryption */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002809 if (how == CRYPTO_ENABLE_INPLACE && !onlyCreateHeader) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002810 /* Now that /data is unmounted, we need to mount a tmpfs
2811 * /data, set a property saying we're doing inplace encryption,
2812 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002813 */
Ken Sumralle5032c42012-04-01 23:58:44 -07002814 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002815 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002816 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002817 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08002818 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002819
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002820 /* restart the framework. */
2821 /* Create necessary paths on /data */
2822 if (prep_data_fs()) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002823 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002824 }
2825
Ken Sumrall92736ef2012-10-17 20:57:14 -07002826 /* Ugh, shutting down the framework is not synchronous, so until it
2827 * can be fixed, this horrible hack will wait a moment for it all to
2828 * shut down before proceeding. Without it, some devices cannot
2829 * restart the graphics services.
2830 */
2831 sleep(2);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002832 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002833
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002834 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002835 /* Initialize a crypt_mnt_ftr for the partition */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002836 if (previously_encrypted_upto == 0 && !rebootEncryption) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002837 if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) {
2838 goto error_shutting_down;
2839 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002840
Paul Lawrence87999172014-02-20 12:21:31 -08002841 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
2842 crypt_ftr.fs_size = nr_sec
2843 - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
2844 } else {
2845 crypt_ftr.fs_size = nr_sec;
2846 }
Paul Lawrence6bfed202014-07-28 12:47:22 -07002847 /* At this point, we are in an inconsistent state. Until we successfully
2848 complete encryption, a reboot will leave us broken. So mark the
2849 encryption failed in case that happens.
2850 On successfully completing encryption, remove this flag */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002851 if (onlyCreateHeader) {
2852 crypt_ftr.flags |= CRYPT_FORCE_ENCRYPTION;
2853 } else {
2854 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
2855 }
Paul Lawrence87999172014-02-20 12:21:31 -08002856 crypt_ftr.crypt_type = crypt_type;
Ajay Dudani87701e22014-09-17 21:02:52 -07002857 strlcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256", MAX_CRYPTO_TYPE_NAME_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002858
Paul Lawrence87999172014-02-20 12:21:31 -08002859 /* Make an encrypted master key */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002860 if (create_encrypted_random_key(onlyCreateHeader ? DEFAULT_PASSWORD : passwd,
2861 crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
Paul Lawrence87999172014-02-20 12:21:31 -08002862 SLOGE("Cannot create encrypted master key\n");
2863 goto error_shutting_down;
2864 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002865
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002866 /* Replace scrypted intermediate key if we are preparing for a reboot */
2867 if (onlyCreateHeader) {
2868 unsigned char fake_master_key[KEY_LEN_BYTES];
2869 unsigned char encrypted_fake_master_key[KEY_LEN_BYTES];
2870 memset(fake_master_key, 0, sizeof(fake_master_key));
2871 encrypt_master_key(passwd, crypt_ftr.salt, fake_master_key,
2872 encrypted_fake_master_key, &crypt_ftr);
2873 }
2874
Paul Lawrence87999172014-02-20 12:21:31 -08002875 /* Write the key to the end of the partition */
2876 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002877
Paul Lawrence87999172014-02-20 12:21:31 -08002878 /* If any persistent data has been remembered, save it.
2879 * If none, create a valid empty table and save that.
2880 */
2881 if (!persist_data) {
Wei Wang4375f1b2017-02-24 17:43:01 -08002882 pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
Paul Lawrence87999172014-02-20 12:21:31 -08002883 if (pdata) {
2884 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
2885 persist_data = pdata;
2886 }
2887 }
2888 if (persist_data) {
2889 save_persistent_data();
2890 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002891 }
2892
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002893 if (onlyCreateHeader) {
2894 sleep(2);
2895 cryptfs_reboot(reboot);
2896 }
2897
2898 if (how == CRYPTO_ENABLE_INPLACE && (!no_ui || rebootEncryption)) {
Ajay Dudani87701e22014-09-17 21:02:52 -07002899 /* startup service classes main and late_start */
2900 property_set("vold.decrypt", "trigger_restart_min_framework");
2901 SLOGD("Just triggered restart_min_framework\n");
2902
2903 /* OK, the framework is restarted and will soon be showing a
2904 * progress bar. Time to setup an encrypted mapping, and
2905 * either write a new filesystem, or encrypt in place updating
2906 * the progress bar as we work.
2907 */
2908 }
2909
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002910 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002911 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002912 CRYPTO_BLOCK_DEVICE);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002913
Paul Lawrence87999172014-02-20 12:21:31 -08002914 /* If we are continuing, check checksums match */
2915 rc = 0;
2916 if (previously_encrypted_upto) {
2917 __le8 hash_first_block[SHA256_DIGEST_LENGTH];
2918 rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block);
Ken Sumrall128626f2011-06-28 18:45:14 -07002919
Paul Lawrence87999172014-02-20 12:21:31 -08002920 if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block,
2921 sizeof(hash_first_block)) != 0) {
2922 SLOGE("Checksums do not match - trigger wipe");
2923 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002924 }
2925 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002926
Paul Lawrence87999172014-02-20 12:21:31 -08002927 if (!rc) {
2928 rc = cryptfs_enable_all_volumes(&crypt_ftr, how,
2929 crypto_blkdev, real_blkdev,
2930 previously_encrypted_upto);
2931 }
2932
2933 /* Calculate checksum if we are not finished */
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08002934 if (!rc && how == CRYPTO_ENABLE_INPLACE
2935 && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002936 rc = cryptfs_SHA256_fileblock(crypto_blkdev,
2937 crypt_ftr.hash_first_block);
Paul Lawrence73d7a022014-06-09 14:10:09 -07002938 if (rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002939 SLOGE("Error calculating checksum for continuing encryption");
2940 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002941 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002942 }
2943
2944 /* Undo the dm-crypt mapping whether we succeed or not */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002945 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002946
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002947 if (! rc) {
2948 /* Success */
Paul Lawrence6bfed202014-07-28 12:47:22 -07002949 crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08002950
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08002951 if (how == CRYPTO_ENABLE_INPLACE
2952 && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002953 SLOGD("Encrypted up to sector %lld - will continue after reboot",
2954 crypt_ftr.encrypted_upto);
Paul Lawrence6bfed202014-07-28 12:47:22 -07002955 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence87999172014-02-20 12:21:31 -08002956 }
Paul Lawrence73d7a022014-06-09 14:10:09 -07002957
Paul Lawrence6bfed202014-07-28 12:47:22 -07002958 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08002959
Paul Lawrenceb1eb7a02014-11-25 14:57:32 -08002960 if (how == CRYPTO_ENABLE_WIPE
2961 || crypt_ftr.encrypted_upto == crypt_ftr.fs_size) {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002962 char value[PROPERTY_VALUE_MAX];
2963 property_get("ro.crypto.state", value, "");
2964 if (!strcmp(value, "")) {
2965 /* default encryption - continue first boot sequence */
2966 property_set("ro.crypto.state", "encrypted");
Paul Lawrence4ed45262016-03-10 15:44:21 -08002967 property_set("ro.crypto.type", "block");
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002968 release_wake_lock(lockid);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002969 if (rebootEncryption && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
2970 // Bring up cryptkeeper that will check the password and set it
2971 property_set("vold.decrypt", "trigger_shutdown_framework");
2972 sleep(2);
2973 property_set("vold.encrypt_progress", "");
2974 cryptfs_trigger_restart_min_framework();
2975 } else {
2976 cryptfs_check_passwd(DEFAULT_PASSWORD);
2977 cryptfs_restart_internal(1);
2978 }
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002979 return 0;
2980 } else {
2981 sleep(2); /* Give the UI a chance to show 100% progress */
Paul Lawrence87999172014-02-20 12:21:31 -08002982 cryptfs_reboot(reboot);
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002983 }
Paul Lawrence87999172014-02-20 12:21:31 -08002984 } else {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002985 sleep(2); /* Partially encrypted, ensure writes flushed to ssd */
Paul Lawrence87999172014-02-20 12:21:31 -08002986 cryptfs_reboot(shutdown);
2987 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002988 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002989 char value[PROPERTY_VALUE_MAX];
2990
Ken Sumrall319369a2012-06-27 16:30:18 -07002991 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002992 if (!strcmp(value, "1")) {
2993 /* wipe data if encryption failed */
2994 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
Wei Wang4375f1b2017-02-24 17:43:01 -08002995 std::string err;
2996 const std::vector<std::string> options = {
2997 "--wipe_data\n--reason=cryptfs_enable_internal\n"
2998 };
2999 if (!write_bootloader_message(options, &err)) {
3000 SLOGE("could not write bootloader message: %s", err.c_str());
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003001 }
Paul Lawrence87999172014-02-20 12:21:31 -08003002 cryptfs_reboot(recovery);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08003003 } else {
3004 /* set property to trigger dialog */
3005 property_set("vold.encrypt_progress", "error_partially_encrypted");
3006 release_wake_lock(lockid);
3007 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003008 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003009 }
3010
Ken Sumrall3ed82362011-01-28 23:31:16 -08003011 /* hrm, the encrypt step claims success, but the reboot failed.
3012 * This should not happen.
3013 * Set the property and return. Hope the framework can deal with it.
3014 */
3015 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003016 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003017 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08003018
3019error_unencrypted:
3020 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003021 if (lockid[0]) {
3022 release_wake_lock(lockid);
3023 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003024 return -1;
3025
3026error_shutting_down:
3027 /* we failed, and have not encrypted anthing, so the users's data is still intact,
3028 * but the framework is stopped and not restarted to show the error, so it's up to
3029 * vold to restart the system.
3030 */
3031 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Paul Lawrence87999172014-02-20 12:21:31 -08003032 cryptfs_reboot(reboot);
Ken Sumrall3ed82362011-01-28 23:31:16 -08003033
3034 /* shouldn't get here */
3035 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08003036 if (lockid[0]) {
3037 release_wake_lock(lockid);
3038 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08003039 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003040}
3041
Paul Lawrence569649f2015-09-09 12:13:00 -07003042int cryptfs_enable(char *howarg, int type, char *passwd, int no_ui)
Paul Lawrence13486032014-02-03 13:28:11 -08003043{
Paul Lawrence569649f2015-09-09 12:13:00 -07003044 return cryptfs_enable_internal(howarg, type, passwd, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08003045}
3046
Paul Lawrence569649f2015-09-09 12:13:00 -07003047int cryptfs_enable_default(char *howarg, int no_ui)
Paul Lawrence13486032014-02-03 13:28:11 -08003048{
3049 return cryptfs_enable_internal(howarg, CRYPT_TYPE_DEFAULT,
Paul Lawrence569649f2015-09-09 12:13:00 -07003050 DEFAULT_PASSWORD, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08003051}
3052
3053int cryptfs_changepw(int crypt_type, const char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003054{
Paul Crowley38132a12016-02-09 09:50:32 +00003055 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08003056 SLOGE("cryptfs_changepw not valid for file encryption");
3057 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08003058 }
3059
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003060 struct crypt_mnt_ftr crypt_ftr;
JP Abgrall933216c2015-02-11 13:44:32 -08003061 int rc;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003062
3063 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08003064 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08003065 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003066 return -1;
3067 }
3068
Paul Lawrencef4faa572014-01-29 13:31:03 -08003069 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
3070 SLOGE("Invalid crypt_type %d", crypt_type);
3071 return -1;
3072 }
3073
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003074 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07003075 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08003076 SLOGE("Error getting crypt footer and key");
3077 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08003078 }
3079
Paul Lawrencef4faa572014-01-29 13:31:03 -08003080 crypt_ftr.crypt_type = crypt_type;
3081
JP Abgrall933216c2015-02-11 13:44:32 -08003082 rc = encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
Paul Lawrencef4faa572014-01-29 13:31:03 -08003083 : newpw,
3084 crypt_ftr.salt,
3085 saved_master_key,
3086 crypt_ftr.master_key,
3087 &crypt_ftr);
JP Abgrall933216c2015-02-11 13:44:32 -08003088 if (rc) {
3089 SLOGE("Encrypt master key failed: %d", rc);
3090 return -1;
3091 }
Jason parks70a4b3f2011-01-28 10:10:47 -06003092 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07003093 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08003094
3095 return 0;
3096}
Ken Sumrall160b4d62013-04-22 12:15:39 -07003097
Rubin Xu85c01f92014-10-13 12:49:54 +01003098static unsigned int persist_get_max_entries(int encrypted) {
3099 struct crypt_mnt_ftr crypt_ftr;
3100 unsigned int dsize;
3101 unsigned int max_persistent_entries;
3102
3103 /* If encrypted, use the values from the crypt_ftr, otherwise
3104 * use the values for the current spec.
3105 */
3106 if (encrypted) {
3107 if (get_crypt_ftr_and_key(&crypt_ftr)) {
3108 return -1;
3109 }
3110 dsize = crypt_ftr.persist_data_size;
3111 } else {
3112 dsize = CRYPT_PERSIST_DATA_SIZE;
3113 }
3114
3115 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
3116 sizeof(struct crypt_persist_entry);
3117
3118 return max_persistent_entries;
3119}
3120
3121static int persist_get_key(const char *fieldname, char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003122{
3123 unsigned int i;
3124
3125 if (persist_data == NULL) {
3126 return -1;
3127 }
3128 for (i = 0; i < persist_data->persist_valid_entries; i++) {
3129 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
3130 /* We found it! */
3131 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
3132 return 0;
3133 }
3134 }
3135
3136 return -1;
3137}
3138
Rubin Xu85c01f92014-10-13 12:49:54 +01003139static int persist_set_key(const char *fieldname, const char *value, int encrypted)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003140{
3141 unsigned int i;
3142 unsigned int num;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003143 unsigned int max_persistent_entries;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003144
3145 if (persist_data == NULL) {
3146 return -1;
3147 }
3148
Rubin Xu85c01f92014-10-13 12:49:54 +01003149 max_persistent_entries = persist_get_max_entries(encrypted);
Ken Sumrall160b4d62013-04-22 12:15:39 -07003150
3151 num = persist_data->persist_valid_entries;
3152
3153 for (i = 0; i < num; i++) {
3154 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
3155 /* We found an existing entry, update it! */
3156 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
3157 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
3158 return 0;
3159 }
3160 }
3161
3162 /* We didn't find it, add it to the end, if there is room */
3163 if (persist_data->persist_valid_entries < max_persistent_entries) {
3164 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
3165 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
3166 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
3167 persist_data->persist_valid_entries++;
3168 return 0;
3169 }
3170
3171 return -1;
3172}
3173
Rubin Xu85c01f92014-10-13 12:49:54 +01003174/**
3175 * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the
3176 * sequence and its index is greater than or equal to index. Return 0 otherwise.
3177 */
3178static int match_multi_entry(const char *key, const char *field, unsigned index) {
Rubin Xu85c01f92014-10-13 12:49:54 +01003179 unsigned int field_len;
3180 unsigned int key_index;
3181 field_len = strlen(field);
3182
3183 if (index == 0) {
3184 // The first key in a multi-entry field is just the filedname itself.
3185 if (!strcmp(key, field)) {
3186 return 1;
3187 }
3188 }
3189 // Match key against "%s_%d" % (field, index)
3190 if (strlen(key) < field_len + 1 + 1) {
3191 // Need at least a '_' and a digit.
3192 return 0;
3193 }
3194 if (strncmp(key, field, field_len)) {
3195 // If the key does not begin with field, it's not a match.
3196 return 0;
3197 }
3198 if (1 != sscanf(&key[field_len],"_%d", &key_index)) {
3199 return 0;
3200 }
3201 return key_index >= index;
3202}
3203
3204/*
3205 * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all
3206 * remaining entries starting from index will be deleted.
3207 * returns PERSIST_DEL_KEY_OK if deletion succeeds,
3208 * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist,
3209 * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs.
3210 *
3211 */
3212static int persist_del_keys(const char *fieldname, unsigned index)
3213{
3214 unsigned int i;
3215 unsigned int j;
3216 unsigned int num;
3217
3218 if (persist_data == NULL) {
3219 return PERSIST_DEL_KEY_ERROR_OTHER;
3220 }
3221
3222 num = persist_data->persist_valid_entries;
3223
3224 j = 0; // points to the end of non-deleted entries.
3225 // Filter out to-be-deleted entries in place.
3226 for (i = 0; i < num; i++) {
3227 if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) {
3228 persist_data->persist_entry[j] = persist_data->persist_entry[i];
3229 j++;
3230 }
3231 }
3232
3233 if (j < num) {
3234 persist_data->persist_valid_entries = j;
3235 // Zeroise the remaining entries
3236 memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry));
3237 return PERSIST_DEL_KEY_OK;
3238 } else {
3239 // Did not find an entry matching the given fieldname
3240 return PERSIST_DEL_KEY_ERROR_NO_FIELD;
3241 }
3242}
3243
3244static int persist_count_keys(const char *fieldname)
3245{
3246 unsigned int i;
3247 unsigned int count;
3248
3249 if (persist_data == NULL) {
3250 return -1;
3251 }
3252
3253 count = 0;
3254 for (i = 0; i < persist_data->persist_valid_entries; i++) {
3255 if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) {
3256 count++;
3257 }
3258 }
3259
3260 return count;
3261}
3262
Ken Sumrall160b4d62013-04-22 12:15:39 -07003263/* Return the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01003264int cryptfs_getfield(const char *fieldname, char *value, int len)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003265{
Paul Crowley38132a12016-02-09 09:50:32 +00003266 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08003267 SLOGE("Cannot get field when file encrypted");
3268 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07003269 }
3270
Ken Sumrall160b4d62013-04-22 12:15:39 -07003271 char temp_value[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01003272 /* CRYPTO_GETFIELD_OK is success,
3273 * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set,
3274 * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small,
3275 * CRYPTO_GETFIELD_ERROR_OTHER is any other error
Ken Sumrall160b4d62013-04-22 12:15:39 -07003276 */
Rubin Xu85c01f92014-10-13 12:49:54 +01003277 int rc = CRYPTO_GETFIELD_ERROR_OTHER;
3278 int i;
3279 char temp_field[PROPERTY_KEY_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -07003280
3281 if (persist_data == NULL) {
3282 load_persistent_data();
3283 if (persist_data == NULL) {
3284 SLOGE("Getfield error, cannot load persistent data");
3285 goto out;
3286 }
3287 }
3288
Rubin Xu85c01f92014-10-13 12:49:54 +01003289 // Read value from persistent entries. If the original value is split into multiple entries,
3290 // stitch them back together.
Ken Sumrall160b4d62013-04-22 12:15:39 -07003291 if (!persist_get_key(fieldname, temp_value)) {
Rubin Xu85c01f92014-10-13 12:49:54 +01003292 // We found it, copy it to the caller's buffer and keep going until all entries are read.
3293 if (strlcpy(value, temp_value, len) >= (unsigned) len) {
3294 // value too small
3295 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
3296 goto out;
3297 }
3298 rc = CRYPTO_GETFIELD_OK;
3299
3300 for (i = 1; /* break explicitly */; i++) {
3301 if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >=
3302 (int) sizeof(temp_field)) {
3303 // If the fieldname is very long, we stop as soon as it begins to overflow the
3304 // maximum field length. At this point we have in fact fully read out the original
3305 // value because cryptfs_setfield would not allow fields with longer names to be
3306 // written in the first place.
3307 break;
3308 }
3309 if (!persist_get_key(temp_field, temp_value)) {
3310 if (strlcat(value, temp_value, len) >= (unsigned)len) {
3311 // value too small.
3312 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
3313 goto out;
3314 }
3315 } else {
3316 // Exhaust all entries.
3317 break;
3318 }
3319 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07003320 } else {
3321 /* Sadness, it's not there. Return the error */
Rubin Xu85c01f92014-10-13 12:49:54 +01003322 rc = CRYPTO_GETFIELD_ERROR_NO_FIELD;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003323 }
3324
3325out:
3326 return rc;
3327}
3328
3329/* Set the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01003330int cryptfs_setfield(const char *fieldname, const char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07003331{
Paul Crowley38132a12016-02-09 09:50:32 +00003332 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08003333 SLOGE("Cannot set field when file encrypted");
3334 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07003335 }
3336
Ken Sumrall160b4d62013-04-22 12:15:39 -07003337 char encrypted_state[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01003338 /* 0 is success, negative values are error */
3339 int rc = CRYPTO_SETFIELD_ERROR_OTHER;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003340 int encrypted = 0;
Rubin Xu85c01f92014-10-13 12:49:54 +01003341 unsigned int field_id;
3342 char temp_field[PROPERTY_KEY_MAX];
3343 unsigned int num_entries;
3344 unsigned int max_keylen;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003345
3346 if (persist_data == NULL) {
3347 load_persistent_data();
3348 if (persist_data == NULL) {
3349 SLOGE("Setfield error, cannot load persistent data");
3350 goto out;
3351 }
3352 }
3353
3354 property_get("ro.crypto.state", encrypted_state, "");
3355 if (!strcmp(encrypted_state, "encrypted") ) {
3356 encrypted = 1;
3357 }
3358
Rubin Xu85c01f92014-10-13 12:49:54 +01003359 // Compute the number of entries required to store value, each entry can store up to
3360 // (PROPERTY_VALUE_MAX - 1) chars
3361 if (strlen(value) == 0) {
3362 // Empty value also needs one entry to store.
3363 num_entries = 1;
3364 } else {
3365 num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1);
3366 }
3367
3368 max_keylen = strlen(fieldname);
3369 if (num_entries > 1) {
3370 // Need an extra "_%d" suffix.
3371 max_keylen += 1 + log10(num_entries);
3372 }
3373 if (max_keylen > PROPERTY_KEY_MAX - 1) {
3374 rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003375 goto out;
3376 }
3377
Rubin Xu85c01f92014-10-13 12:49:54 +01003378 // Make sure we have enough space to write the new value
3379 if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) >
3380 persist_get_max_entries(encrypted)) {
3381 rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG;
3382 goto out;
3383 }
3384
3385 // Now that we know persist_data has enough space for value, let's delete the old field first
3386 // to make up space.
3387 persist_del_keys(fieldname, 0);
3388
3389 if (persist_set_key(fieldname, value, encrypted)) {
3390 // fail to set key, should not happen as we have already checked the available space
3391 SLOGE("persist_set_key() error during setfield()");
3392 goto out;
3393 }
3394
3395 for (field_id = 1; field_id < num_entries; field_id++) {
3396 snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, field_id);
3397
3398 if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), encrypted)) {
3399 // fail to set key, should not happen as we have already checked the available space.
3400 SLOGE("persist_set_key() error during setfield()");
3401 goto out;
3402 }
3403 }
3404
Ken Sumrall160b4d62013-04-22 12:15:39 -07003405 /* If we are running encrypted, save the persistent data now */
3406 if (encrypted) {
3407 if (save_persistent_data()) {
3408 SLOGE("Setfield error, cannot save persistent data");
3409 goto out;
3410 }
3411 }
3412
Rubin Xu85c01f92014-10-13 12:49:54 +01003413 rc = CRYPTO_SETFIELD_OK;
Ken Sumrall160b4d62013-04-22 12:15:39 -07003414
3415out:
3416 return rc;
3417}
Paul Lawrencef4faa572014-01-29 13:31:03 -08003418
3419/* Checks userdata. Attempt to mount the volume if default-
3420 * encrypted.
3421 * On success trigger next init phase and return 0.
3422 * Currently do not handle failure - see TODO below.
3423 */
3424int cryptfs_mount_default_encrypted(void)
3425{
Paul Lawrence84274cc2016-04-15 15:41:33 -07003426 int crypt_type = cryptfs_get_password_type();
3427 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
3428 SLOGE("Bad crypt type - error");
3429 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
3430 SLOGD("Password is not default - "
3431 "starting min framework to prompt");
3432 property_set("vold.decrypt", "trigger_restart_min_framework");
3433 return 0;
3434 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
3435 SLOGD("Password is default - restarting filesystem");
3436 cryptfs_restart_internal(0);
3437 return 0;
Paul Lawrencef4faa572014-01-29 13:31:03 -08003438 } else {
Paul Lawrence84274cc2016-04-15 15:41:33 -07003439 SLOGE("Encrypted, default crypt type but can't decrypt");
Paul Lawrencef4faa572014-01-29 13:31:03 -08003440 }
3441
Paul Lawrence6bfed202014-07-28 12:47:22 -07003442 /** Corrupt. Allow us to boot into framework, which will detect bad
3443 crypto when it calls do_crypto_complete, then do a factory reset
Paul Lawrencef4faa572014-01-29 13:31:03 -08003444 */
Paul Lawrence6bfed202014-07-28 12:47:22 -07003445 property_set("vold.decrypt", "trigger_restart_min_framework");
Paul Lawrencef4faa572014-01-29 13:31:03 -08003446 return 0;
3447}
3448
3449/* Returns type of the password, default, pattern, pin or password.
3450 */
3451int cryptfs_get_password_type(void)
3452{
Paul Crowley38132a12016-02-09 09:50:32 +00003453 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08003454 SLOGE("cryptfs_get_password_type not valid for file encryption");
3455 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08003456 }
3457
Paul Lawrencef4faa572014-01-29 13:31:03 -08003458 struct crypt_mnt_ftr crypt_ftr;
3459
3460 if (get_crypt_ftr_and_key(&crypt_ftr)) {
3461 SLOGE("Error getting crypt footer and key\n");
3462 return -1;
3463 }
3464
Paul Lawrence6bfed202014-07-28 12:47:22 -07003465 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) {
3466 return -1;
3467 }
3468
Paul Lawrencef4faa572014-01-29 13:31:03 -08003469 return crypt_ftr.crypt_type;
3470}
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003471
Paul Lawrence05335c32015-03-05 09:46:23 -08003472const char* cryptfs_get_password()
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003473{
Paul Crowley38132a12016-02-09 09:50:32 +00003474 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08003475 SLOGE("cryptfs_get_password not valid for file encryption");
3476 return 0;
Paul Lawrence05335c32015-03-05 09:46:23 -08003477 }
3478
Paul Lawrence399317e2014-03-10 13:20:50 -07003479 struct timespec now;
Paul Lawrenceef2b5be2014-11-11 12:47:03 -08003480 clock_gettime(CLOCK_BOOTTIME, &now);
Paul Lawrence399317e2014-03-10 13:20:50 -07003481 if (now.tv_sec < password_expiry_time) {
3482 return password;
3483 } else {
3484 cryptfs_clear_password();
3485 return 0;
3486 }
3487}
3488
3489void cryptfs_clear_password()
3490{
3491 if (password) {
3492 size_t len = strlen(password);
3493 memset(password, 0, len);
3494 free(password);
3495 password = 0;
3496 password_expiry_time = 0;
3497 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08003498}
Paul Lawrence731a7a22015-04-28 22:14:15 +00003499
3500int cryptfs_enable_file()
3501{
Paul Crowley38132a12016-02-09 09:50:32 +00003502 return e4crypt_initialize_global_de();
Paul Lawrence731a7a22015-04-28 22:14:15 +00003503}
3504
Paul Lawrence0c247462015-10-29 10:30:57 -07003505int cryptfs_isConvertibleToFBE()
3506{
3507 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
3508 return fs_mgr_is_convertible_to_fbe(rec) ? 1 : 0;
3509}
3510
Paul Lawrence731a7a22015-04-28 22:14:15 +00003511int cryptfs_create_default_ftr(struct crypt_mnt_ftr* crypt_ftr, __attribute__((unused))int key_length)
3512{
3513 if (cryptfs_init_crypt_mnt_ftr(crypt_ftr)) {
3514 SLOGE("Failed to initialize crypt_ftr");
3515 return -1;
3516 }
3517
3518 if (create_encrypted_random_key(DEFAULT_PASSWORD, crypt_ftr->master_key,
3519 crypt_ftr->salt, crypt_ftr)) {
3520 SLOGE("Cannot create encrypted master key\n");
3521 return -1;
3522 }
3523
3524 //crypt_ftr->keysize = key_length / 8;
3525 return 0;
3526}
3527
3528int cryptfs_get_master_key(struct crypt_mnt_ftr* ftr, const char* password,
3529 unsigned char* master_key)
3530{
3531 int rc;
3532
Paul Lawrence731a7a22015-04-28 22:14:15 +00003533 unsigned char* intermediate_key = 0;
3534 size_t intermediate_key_size = 0;
Paul Lawrencec78c71b2015-04-14 15:26:29 -07003535
3536 if (password == 0 || *password == 0) {
3537 password = DEFAULT_PASSWORD;
3538 }
3539
Paul Lawrence731a7a22015-04-28 22:14:15 +00003540 rc = decrypt_master_key(password, master_key, ftr, &intermediate_key,
3541 &intermediate_key_size);
3542
Paul Lawrence300dae72016-03-11 11:02:52 -08003543 if (rc) {
3544 SLOGE("Can't calculate intermediate key");
3545 return rc;
3546 }
3547
Paul Lawrencec78c71b2015-04-14 15:26:29 -07003548 int N = 1 << ftr->N_factor;
3549 int r = 1 << ftr->r_factor;
3550 int p = 1 << ftr->p_factor;
3551
3552 unsigned char scrypted_intermediate_key[sizeof(ftr->scrypted_intermediate_key)];
3553
3554 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
3555 ftr->salt, sizeof(ftr->salt), N, r, p,
3556 scrypted_intermediate_key,
3557 sizeof(scrypted_intermediate_key));
3558
3559 free(intermediate_key);
3560
3561 if (rc) {
Paul Lawrence300dae72016-03-11 11:02:52 -08003562 SLOGE("Can't scrypt intermediate key");
Paul Lawrencec78c71b2015-04-14 15:26:29 -07003563 return rc;
3564 }
3565
3566 return memcmp(scrypted_intermediate_key, ftr->scrypted_intermediate_key,
3567 intermediate_key_size);
Paul Lawrence731a7a22015-04-28 22:14:15 +00003568}
3569
3570int cryptfs_set_password(struct crypt_mnt_ftr* ftr, const char* password,
3571 const unsigned char* master_key)
3572{
3573 return encrypt_master_key(password, ftr->salt, master_key, ftr->master_key,
3574 ftr);
3575}
Paul Lawrence6e410592016-05-24 14:20:38 -07003576
Eric Biggersb45caaf2017-02-02 14:52:12 -08003577void cryptfs_get_file_encryption_modes(const char **contents_mode_ret,
3578 const char **filenames_mode_ret)
Paul Lawrence6e410592016-05-24 14:20:38 -07003579{
3580 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
Eric Biggersb45caaf2017-02-02 14:52:12 -08003581 fs_mgr_get_file_encryption_modes(rec, contents_mode_ret, filenames_mode_ret);
Paul Lawrence6e410592016-05-24 14:20:38 -07003582}