blob: c4274ed9a5a3b5b5b47591e81fb32d549ce8e1eb [file] [log] [blame]
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* TO DO:
18 * 1. Perhaps keep several copies of the encrypted key, in case something
19 * goes horribly wrong?
20 *
21 */
22
Logan Chiend557d762018-05-02 11:36:45 +080023#define LOG_TAG "Cryptfs"
24
25#include "cryptfs.h"
26
27#include "EncryptInplace.h"
28#include "Ext4Crypt.h"
29#include "Keymaster.h"
30#include "Process.h"
31#include "ScryptParameters.h"
32#include "VoldUtil.h"
33#include "VolumeManager.h"
34#include "secontext.h"
35
Logan Chien3f2b1222018-05-02 11:39:03 +080036#include <android-base/properties.h>
Logan Chiend557d762018-05-02 11:36:45 +080037#include <bootloader_message/bootloader_message.h>
Logan Chien3f2b1222018-05-02 11:39:03 +080038#include <cutils/android_reboot.h>
Logan Chien3f2b1222018-05-02 11:39:03 +080039#include <cutils/properties.h>
Paul Crowley3b71fc52017-10-09 10:55:21 -070040#include <ext4_utils/ext4_crypt.h>
Tao Bao5a95ddb2016-10-05 18:01:19 -070041#include <ext4_utils/ext4_utils.h>
Logan Chien3f2b1222018-05-02 11:39:03 +080042#include <f2fs_sparseblock.h>
Ken Sumralle5032c42012-04-01 23:58:44 -070043#include <fs_mgr.h>
Logan Chien3f2b1222018-05-02 11:39:03 +080044#include <hardware_legacy/power.h>
Logan Chien188b0ab2018-04-23 13:37:39 +080045#include <log/log.h>
Logan Chiend557d762018-05-02 11:36:45 +080046#include <logwrap/logwrap.h>
47#include <openssl/evp.h>
48#include <openssl/sha.h>
Jeff Vander Stoepdf725752016-01-29 15:34:43 -080049#include <selinux/selinux.h>
Logan Chiend557d762018-05-02 11:36:45 +080050
51#include <ctype.h>
52#include <errno.h>
53#include <fcntl.h>
54#include <inttypes.h>
55#include <libgen.h>
56#include <linux/dm-ioctl.h>
57#include <linux/kdev_t.h>
58#include <math.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <sys/ioctl.h>
63#include <sys/mount.h>
64#include <sys/param.h>
65#include <sys/stat.h>
66#include <sys/types.h>
67#include <sys/wait.h>
68#include <time.h>
69#include <unistd.h>
70
Wei Wang4375f1b2017-02-24 17:43:01 -080071extern "C" {
72#include <crypto_scrypt.h>
73}
Mark Salyzyn3e971272014-01-21 13:27:04 -080074
Mark Salyzyn5eecc442014-02-12 14:16:14 -080075#define UNUSED __attribute__((unused))
76
Ken Sumrall8f869aa2010-12-03 03:47:09 -080077#define DM_CRYPT_BUF_SIZE 4096
78
Jason parks70a4b3f2011-01-28 10:10:47 -060079#define HASH_COUNT 2000
Greg Kaiserc0de9c72018-02-14 20:05:54 -080080
81constexpr size_t INTERMEDIATE_KEY_LEN_BYTES = 16;
82constexpr size_t INTERMEDIATE_IV_LEN_BYTES = 16;
83constexpr size_t INTERMEDIATE_BUF_SIZE =
84 (INTERMEDIATE_KEY_LEN_BYTES + INTERMEDIATE_IV_LEN_BYTES);
85
86// SCRYPT_LEN is used by struct crypt_mnt_ftr for its intermediate key.
87static_assert(INTERMEDIATE_BUF_SIZE == SCRYPT_LEN,
88 "Mismatch of intermediate key sizes");
Jason parks70a4b3f2011-01-28 10:10:47 -060089
Ken Sumrall29d8da82011-05-18 17:20:07 -070090#define KEY_IN_FOOTER "footer"
91
Paul Lawrence3bd36d52015-06-09 13:37:44 -070092#define DEFAULT_PASSWORD "default_password"
Paul Lawrencef4faa572014-01-29 13:31:03 -080093
Paul Lawrence3d99eba2015-11-20 07:07:19 -080094#define CRYPTO_BLOCK_DEVICE "userdata"
95
96#define BREADCRUMB_FILE "/data/misc/vold/convert_fde"
97
Ken Sumrall29d8da82011-05-18 17:20:07 -070098#define EXT4_FS 1
JP Abgrall62c7af32014-06-16 13:01:23 -070099#define F2FS_FS 2
Ken Sumrall29d8da82011-05-18 17:20:07 -0700100
Ken Sumralle919efe2012-09-29 17:07:41 -0700101#define TABLE_LOAD_RETRIES 10
102
Shawn Willden47ba10d2014-09-03 17:07:06 -0600103#define RSA_KEY_SIZE 2048
104#define RSA_KEY_SIZE_BYTES (RSA_KEY_SIZE / 8)
105#define RSA_EXPONENT 0x10001
Shawn Willdenda6e8992015-06-03 09:40:45 -0600106#define KEYMASTER_CRYPTFS_RATE_LIMIT 1 // Maximum one try per second
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700107
Paul Lawrence8e3f4512014-09-08 10:11:17 -0700108#define RETRY_MOUNT_ATTEMPTS 10
109#define RETRY_MOUNT_DELAY_SECONDS 1
110
Paul Crowley5afbc622017-11-27 09:42:17 -0800111#define CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE (1)
112
Paul Crowley73473332017-11-21 15:43:51 -0800113static int put_crypt_ftr_and_key(struct crypt_mnt_ftr* crypt_ftr);
114
Greg Kaiser59ad0182018-02-16 13:01:36 -0800115static unsigned char saved_master_key[MAX_KEY_LEN];
Ken Sumrall3ad90722011-10-04 20:38:29 -0700116static char *saved_mount_point;
Jason parks70a4b3f2011-01-28 10:10:47 -0600117static int master_key_saved = 0;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700118static struct crypt_persist_data *persist_data = NULL;
Ken Sumrall56ad03c2013-02-13 13:00:19 -0800119
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700120/* Should we use keymaster? */
121static int keymaster_check_compatibility()
122{
Janis Danisevskis015ec302017-01-31 11:31:08 +0000123 return keymaster_compatibility_cryptfs_scrypt();
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700124}
125
126/* Create a new keymaster key and store it in this footer */
127static int keymaster_create_key(struct crypt_mnt_ftr *ftr)
128{
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800129 if (ftr->keymaster_blob_size) {
130 SLOGI("Already have key");
131 return 0;
132 }
133
Janis Danisevskis015ec302017-01-31 11:31:08 +0000134 int rc = keymaster_create_key_for_cryptfs_scrypt(RSA_KEY_SIZE, RSA_EXPONENT,
135 KEYMASTER_CRYPTFS_RATE_LIMIT, ftr->keymaster_blob, KEYMASTER_BLOB_SIZE,
136 &ftr->keymaster_blob_size);
137 if (rc) {
138 if (ftr->keymaster_blob_size > KEYMASTER_BLOB_SIZE) {
Paul Crowley73473332017-11-21 15:43:51 -0800139 SLOGE("Keymaster key blob too large");
Janis Danisevskis015ec302017-01-31 11:31:08 +0000140 ftr->keymaster_blob_size = 0;
141 }
142 SLOGE("Failed to generate keypair");
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700143 return -1;
144 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000145 return 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -0700146}
147
Shawn Willdene17a9c42014-09-08 13:04:08 -0600148/* This signs the given object using the keymaster key. */
149static int keymaster_sign_object(struct crypt_mnt_ftr *ftr,
Shawn Willden47ba10d2014-09-03 17:07:06 -0600150 const unsigned char *object,
151 const size_t object_size,
152 unsigned char **signature,
153 size_t *signature_size)
154{
Shawn Willden47ba10d2014-09-03 17:07:06 -0600155 unsigned char to_sign[RSA_KEY_SIZE_BYTES];
Shawn Willdene17a9c42014-09-08 13:04:08 -0600156 size_t to_sign_size = sizeof(to_sign);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600157 memset(to_sign, 0, RSA_KEY_SIZE_BYTES);
Shawn Willden47ba10d2014-09-03 17:07:06 -0600158
Shawn Willdene17a9c42014-09-08 13:04:08 -0600159 // To sign a message with RSA, the message must satisfy two
160 // constraints:
161 //
162 // 1. The message, when interpreted as a big-endian numeric value, must
163 // be strictly less than the public modulus of the RSA key. Note
164 // that because the most significant bit of the public modulus is
165 // guaranteed to be 1 (else it's an (n-1)-bit key, not an n-bit
166 // key), an n-bit message with most significant bit 0 always
167 // satisfies this requirement.
168 //
169 // 2. The message must have the same length in bits as the public
170 // modulus of the RSA key. This requirement isn't mathematically
171 // necessary, but is necessary to ensure consistency in
172 // implementations.
173 switch (ftr->kdf_type) {
Shawn Willdene17a9c42014-09-08 13:04:08 -0600174 case KDF_SCRYPT_KEYMASTER:
175 // This ensures the most significant byte of the signed message
176 // is zero. We could have zero-padded to the left instead, but
177 // this approach is slightly more robust against changes in
178 // object size. However, it's still broken (but not unusably
Shawn Willdenda6e8992015-06-03 09:40:45 -0600179 // so) because we really should be using a proper deterministic
180 // RSA padding function, such as PKCS1.
Wei Wang4375f1b2017-02-24 17:43:01 -0800181 memcpy(to_sign + 1, object, std::min((size_t)RSA_KEY_SIZE_BYTES - 1, object_size));
Shawn Willdene17a9c42014-09-08 13:04:08 -0600182 SLOGI("Signing safely-padded object");
183 break;
184 default:
185 SLOGE("Unknown KDF type %d", ftr->kdf_type);
Janis Danisevskis015ec302017-01-31 11:31:08 +0000186 return -1;
Shawn Willdene17a9c42014-09-08 13:04:08 -0600187 }
Paul Crowley73473332017-11-21 15:43:51 -0800188 for (;;) {
189 auto result = keymaster_sign_object_for_cryptfs_scrypt(
190 ftr->keymaster_blob, ftr->keymaster_blob_size, KEYMASTER_CRYPTFS_RATE_LIMIT, to_sign,
191 to_sign_size, signature, signature_size);
192 switch (result) {
193 case KeymasterSignResult::ok:
194 return 0;
195 case KeymasterSignResult::upgrade:
196 break;
197 default:
198 return -1;
199 }
200 SLOGD("Upgrading key");
201 if (keymaster_upgrade_key_for_cryptfs_scrypt(
202 RSA_KEY_SIZE, RSA_EXPONENT, KEYMASTER_CRYPTFS_RATE_LIMIT, ftr->keymaster_blob,
203 ftr->keymaster_blob_size, ftr->keymaster_blob, KEYMASTER_BLOB_SIZE,
204 &ftr->keymaster_blob_size) != 0) {
205 SLOGE("Failed to upgrade key");
206 return -1;
207 }
208 if (put_crypt_ftr_and_key(ftr) != 0) {
209 SLOGE("Failed to write upgraded key to disk");
210 }
211 SLOGD("Key upgraded successfully");
212 }
Shawn Willden47ba10d2014-09-03 17:07:06 -0600213}
214
Paul Lawrence399317e2014-03-10 13:20:50 -0700215/* Store password when userdata is successfully decrypted and mounted.
216 * Cleared by cryptfs_clear_password
217 *
218 * To avoid a double prompt at boot, we need to store the CryptKeeper
219 * password and pass it to KeyGuard, which uses it to unlock KeyStore.
220 * Since the entire framework is torn down and rebuilt after encryption,
221 * we have to use a daemon or similar to store the password. Since vold
222 * is secured against IPC except from system processes, it seems a reasonable
223 * place to store this.
224 *
225 * password should be cleared once it has been used.
226 *
227 * password is aged out after password_max_age_seconds seconds.
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800228 */
Paul Lawrence399317e2014-03-10 13:20:50 -0700229static char* password = 0;
230static int password_expiry_time = 0;
231static const int password_max_age_seconds = 60;
Paul Lawrence684dbdf2014-02-07 12:07:22 -0800232
Josh Gaofec44372017-08-28 13:22:55 -0700233enum class RebootType {reboot, recovery, shutdown};
234static void cryptfs_reboot(RebootType rt)
Ken Sumralladfba362013-06-04 16:37:52 -0700235{
Josh Gaofec44372017-08-28 13:22:55 -0700236 switch (rt) {
237 case RebootType::reboot:
Paul Lawrence87999172014-02-20 12:21:31 -0800238 property_set(ANDROID_RB_PROPERTY, "reboot");
239 break;
240
Josh Gaofec44372017-08-28 13:22:55 -0700241 case RebootType::recovery:
Paul Lawrence87999172014-02-20 12:21:31 -0800242 property_set(ANDROID_RB_PROPERTY, "reboot,recovery");
243 break;
244
Josh Gaofec44372017-08-28 13:22:55 -0700245 case RebootType::shutdown:
Paul Lawrence87999172014-02-20 12:21:31 -0800246 property_set(ANDROID_RB_PROPERTY, "shutdown");
247 break;
Ken Sumralladfba362013-06-04 16:37:52 -0700248 }
Paul Lawrence87999172014-02-20 12:21:31 -0800249
Ken Sumralladfba362013-06-04 16:37:52 -0700250 sleep(20);
251
252 /* Shouldn't get here, reboot should happen before sleep times out */
253 return;
254}
255
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800256static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
257{
258 memset(io, 0, dataSize);
259 io->data_size = dataSize;
260 io->data_start = sizeof(struct dm_ioctl);
261 io->version[0] = 4;
262 io->version[1] = 0;
263 io->version[2] = 0;
264 io->flags = flags;
265 if (name) {
Marek Pola5e6b9142015-02-05 14:22:34 +0100266 strlcpy(io->name, name, sizeof(io->name));
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800267 }
268}
269
Greg Kaiser38723f22018-02-16 13:35:35 -0800270namespace {
271
272struct CryptoType;
273
274// Use to get the CryptoType in use on this device.
275const CryptoType &get_crypto_type();
276
277struct CryptoType {
278 // We should only be constructing CryptoTypes as part of
279 // supported_crypto_types[]. We do it via this pseudo-builder pattern,
280 // which isn't pure or fully protected as a concession to being able to
281 // do it all at compile time. Add new CryptoTypes in
282 // supported_crypto_types[] below.
283 constexpr CryptoType() : CryptoType(nullptr, nullptr, 0xFFFFFFFF) {}
284 constexpr CryptoType set_keysize(uint32_t size) const {
285 return CryptoType(this->property_name, this->crypto_name, size);
286 }
287 constexpr CryptoType set_property_name(const char *property) const {
288 return CryptoType(property, this->crypto_name, this->keysize);
289 }
290 constexpr CryptoType set_crypto_name(const char *crypto) const {
291 return CryptoType(this->property_name, crypto, this->keysize);
292 }
293
294 constexpr const char *get_property_name() const { return property_name; }
295 constexpr const char *get_crypto_name() const { return crypto_name; }
296 constexpr uint32_t get_keysize() const { return keysize; }
297
298 private:
299 const char *property_name;
300 const char *crypto_name;
301 uint32_t keysize;
302
303 constexpr CryptoType(const char *property, const char *crypto,
304 uint32_t ksize)
305 : property_name(property), crypto_name(crypto), keysize(ksize) {}
306 friend const CryptoType &get_crypto_type();
307 static const CryptoType &get_device_crypto_algorithm();
308};
309
310// We only want to parse this read-only property once. But we need to wait
311// until the system is initialized before we can read it. So we use a static
312// scoped within this function to get it only once.
313const CryptoType &get_crypto_type() {
314 static CryptoType crypto_type = CryptoType::get_device_crypto_algorithm();
315 return crypto_type;
316}
317
318constexpr CryptoType default_crypto_type = CryptoType()
319 .set_property_name("AES-128-CBC")
320 .set_crypto_name("aes-cbc-essiv:sha256")
321 .set_keysize(16);
322
323constexpr CryptoType supported_crypto_types[] = {
324 default_crypto_type,
Greg Kaiser38723f22018-02-16 13:35:35 -0800325 // Add new CryptoTypes here. Order is not important.
326};
327
328
329// ---------- START COMPILE-TIME SANITY CHECK BLOCK -------------------------
330// We confirm all supported_crypto_types have a small enough keysize and
331// had both set_property_name() and set_crypto_name() called.
332
333template <typename T, size_t N>
334constexpr size_t array_length(T (&)[N]) { return N; }
335
336constexpr bool indexOutOfBoundsForCryptoTypes(size_t index) {
337 return (index >= array_length(supported_crypto_types));
338}
339
340constexpr bool isValidCryptoType(const CryptoType &crypto_type) {
341 return ((crypto_type.get_property_name() != nullptr) &&
342 (crypto_type.get_crypto_name() != nullptr) &&
343 (crypto_type.get_keysize() <= MAX_KEY_LEN));
344}
345
346// Note in C++11 that constexpr functions can only have a single line.
347// So our code is a bit convoluted (using recursion instead of a loop),
348// but it's asserting at compile time that all of our key lengths are valid.
349constexpr bool validateSupportedCryptoTypes(size_t index) {
350 return indexOutOfBoundsForCryptoTypes(index) ||
351 (isValidCryptoType(supported_crypto_types[index]) &&
352 validateSupportedCryptoTypes(index + 1));
353}
354
355static_assert(validateSupportedCryptoTypes(0),
356 "We have a CryptoType with keysize > MAX_KEY_LEN or which was "
357 "incompletely constructed.");
358// ---------- END COMPILE-TIME SANITY CHECK BLOCK -------------------------
359
360
361// Don't call this directly, use get_crypto_type(), which caches this result.
362const CryptoType &CryptoType::get_device_crypto_algorithm() {
363 constexpr char CRYPT_ALGO_PROP[] = "ro.crypto.fde_algorithm";
364 char paramstr[PROPERTY_VALUE_MAX];
365
366 property_get(CRYPT_ALGO_PROP, paramstr,
367 default_crypto_type.get_property_name());
368 for (auto const &ctype : supported_crypto_types) {
369 if (strcmp(paramstr, ctype.get_property_name()) == 0) {
370 return ctype;
371 }
372 }
373 ALOGE("Invalid name (%s) for %s. Defaulting to %s\n", paramstr,
374 CRYPT_ALGO_PROP, default_crypto_type.get_property_name());
375 return default_crypto_type;
376}
377
378} // namespace
379
380
381
Kenny Rootc4c70f12013-06-14 12:11:38 -0700382/**
383 * Gets the default device scrypt parameters for key derivation time tuning.
384 * The parameters should lead to about one second derivation time for the
385 * given device.
386 */
387static void get_device_scrypt_params(struct crypt_mnt_ftr *ftr) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700388 char paramstr[PROPERTY_VALUE_MAX];
Paul Crowley63c18d32016-02-10 14:02:47 +0000389 int Nf, rf, pf;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700390
Paul Crowley63c18d32016-02-10 14:02:47 +0000391 property_get(SCRYPT_PROP, paramstr, SCRYPT_DEFAULTS);
392 if (!parse_scrypt_parameters(paramstr, &Nf, &rf, &pf)) {
393 SLOGW("bad scrypt parameters '%s' should be like '12:8:1'; using defaults", paramstr);
394 parse_scrypt_parameters(SCRYPT_DEFAULTS, &Nf, &rf, &pf);
Kenny Rootc4c70f12013-06-14 12:11:38 -0700395 }
Paul Crowley63c18d32016-02-10 14:02:47 +0000396 ftr->N_factor = Nf;
397 ftr->r_factor = rf;
398 ftr->p_factor = pf;
Kenny Rootc4c70f12013-06-14 12:11:38 -0700399}
400
Greg Kaiser57f9af62018-02-16 13:13:58 -0800401uint32_t cryptfs_get_keysize() {
Greg Kaiser38723f22018-02-16 13:35:35 -0800402 return get_crypto_type().get_keysize();
Greg Kaiser57f9af62018-02-16 13:13:58 -0800403}
404
405const char *cryptfs_get_crypto_name() {
Greg Kaiser38723f22018-02-16 13:35:35 -0800406 return get_crypto_type().get_crypto_name();
Greg Kaiser57f9af62018-02-16 13:13:58 -0800407}
408
Ken Sumrall3ed82362011-01-28 23:31:16 -0800409static unsigned int get_fs_size(char *dev)
410{
411 int fd, block_size;
412 struct ext4_super_block sb;
413 off64_t len;
414
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700415 if ((fd = open(dev, O_RDONLY|O_CLOEXEC)) < 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -0800416 SLOGE("Cannot open device to get filesystem size ");
417 return 0;
418 }
419
420 if (lseek64(fd, 1024, SEEK_SET) < 0) {
421 SLOGE("Cannot seek to superblock");
422 return 0;
423 }
424
425 if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
426 SLOGE("Cannot read superblock");
427 return 0;
428 }
429
430 close(fd);
431
Daniel Rosenberge82df162014-08-15 22:19:23 +0000432 if (le32_to_cpu(sb.s_magic) != EXT4_SUPER_MAGIC) {
433 SLOGE("Not a valid ext4 superblock");
434 return 0;
435 }
Ken Sumrall3ed82362011-01-28 23:31:16 -0800436 block_size = 1024 << sb.s_log_block_size;
437 /* compute length in bytes */
438 len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
439
440 /* return length in sectors */
441 return (unsigned int) (len / 512);
442}
443
Ken Sumrall160b4d62013-04-22 12:15:39 -0700444static int get_crypt_ftr_info(char **metadata_fname, off64_t *off)
445{
446 static int cached_data = 0;
447 static off64_t cached_off = 0;
448 static char cached_metadata_fname[PROPERTY_VALUE_MAX] = "";
449 int fd;
450 char key_loc[PROPERTY_VALUE_MAX];
451 char real_blkdev[PROPERTY_VALUE_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -0700452 int rc = -1;
453
454 if (!cached_data) {
Paul Crowleye2ee1522017-09-26 14:05:26 -0700455 fs_mgr_get_crypt_info(fstab_default, key_loc, real_blkdev, sizeof(key_loc));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700456
457 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700458 if ( (fd = open(real_blkdev, O_RDWR|O_CLOEXEC)) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700459 SLOGE("Cannot open real block device %s\n", real_blkdev);
460 return -1;
461 }
462
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +0900463 unsigned long nr_sec = 0;
464 get_blkdev_size(fd, &nr_sec);
465 if (nr_sec != 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700466 /* If it's an encrypted Android partition, the last 16 Kbytes contain the
467 * encryption info footer and key, and plenty of bytes to spare for future
468 * growth.
469 */
470 strlcpy(cached_metadata_fname, real_blkdev, sizeof(cached_metadata_fname));
471 cached_off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
472 cached_data = 1;
473 } else {
474 SLOGE("Cannot get size of block device %s\n", real_blkdev);
475 }
476 close(fd);
477 } else {
478 strlcpy(cached_metadata_fname, key_loc, sizeof(cached_metadata_fname));
479 cached_off = 0;
480 cached_data = 1;
481 }
482 }
483
484 if (cached_data) {
485 if (metadata_fname) {
486 *metadata_fname = cached_metadata_fname;
487 }
488 if (off) {
489 *off = cached_off;
490 }
491 rc = 0;
492 }
493
494 return rc;
495}
496
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800497/* Set sha256 checksum in structure */
498static void set_ftr_sha(struct crypt_mnt_ftr *crypt_ftr)
499{
500 SHA256_CTX c;
501 SHA256_Init(&c);
502 memset(crypt_ftr->sha256, 0, sizeof(crypt_ftr->sha256));
503 SHA256_Update(&c, crypt_ftr, sizeof(*crypt_ftr));
504 SHA256_Final(crypt_ftr->sha256, &c);
505}
506
Ken Sumralle8744072011-01-18 22:01:55 -0800507/* key or salt can be NULL, in which case just skip writing that value. Useful to
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800508 * update the failed mount count but not change the key.
509 */
Ken Sumrall160b4d62013-04-22 12:15:39 -0700510static int put_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800511{
512 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800513 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700514 /* starting_off is set to the SEEK_SET offset
515 * where the crypto structure starts
516 */
517 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800518 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700519 char *fname = NULL;
Ken Sumrall3be890f2011-09-14 16:53:46 -0700520 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800521
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800522 set_ftr_sha(crypt_ftr);
523
Ken Sumrall160b4d62013-04-22 12:15:39 -0700524 if (get_crypt_ftr_info(&fname, &starting_off)) {
525 SLOGE("Unable to get crypt_ftr_info\n");
526 return -1;
527 }
528 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700529 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700530 return -1;
531 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700532 if ( (fd = open(fname, O_RDWR | O_CREAT|O_CLOEXEC, 0600)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700533 SLOGE("Cannot open footer file %s for put\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700534 return -1;
535 }
536
537 /* Seek to the start of the crypt footer */
538 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
539 SLOGE("Cannot seek to real block device footer\n");
540 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800541 }
542
543 if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
544 SLOGE("Cannot write real block device footer\n");
545 goto errout;
546 }
547
Ken Sumrall3be890f2011-09-14 16:53:46 -0700548 fstat(fd, &statbuf);
549 /* If the keys are kept on a raw block device, do not try to truncate it. */
Ken Sumralle550f782013-08-20 13:48:23 -0700550 if (S_ISREG(statbuf.st_mode)) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700551 if (ftruncate(fd, 0x4000)) {
Colin Cross59846b62014-02-06 20:34:29 -0800552 SLOGE("Cannot set footer file size\n");
Ken Sumralle8744072011-01-18 22:01:55 -0800553 goto errout;
554 }
555 }
556
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800557 /* Success! */
558 rc = 0;
559
560errout:
561 close(fd);
562 return rc;
563
564}
565
Paul Lawrence3d99eba2015-11-20 07:07:19 -0800566static bool check_ftr_sha(const struct crypt_mnt_ftr *crypt_ftr)
567{
568 struct crypt_mnt_ftr copy;
569 memcpy(&copy, crypt_ftr, sizeof(copy));
570 set_ftr_sha(&copy);
571 return memcmp(copy.sha256, crypt_ftr->sha256, sizeof(copy.sha256)) == 0;
572}
573
Ken Sumrall160b4d62013-04-22 12:15:39 -0700574static inline int unix_read(int fd, void* buff, int len)
575{
576 return TEMP_FAILURE_RETRY(read(fd, buff, len));
577}
578
579static inline int unix_write(int fd, const void* buff, int len)
580{
581 return TEMP_FAILURE_RETRY(write(fd, buff, len));
582}
583
584static void init_empty_persist_data(struct crypt_persist_data *pdata, int len)
585{
586 memset(pdata, 0, len);
587 pdata->persist_magic = PERSIST_DATA_MAGIC;
588 pdata->persist_valid_entries = 0;
589}
590
591/* A routine to update the passed in crypt_ftr to the lastest version.
592 * fd is open read/write on the device that holds the crypto footer and persistent
593 * data, crypt_ftr is a pointer to the struct to be updated, and offset is the
594 * absolute offset to the start of the crypt_mnt_ftr on the passed in fd.
595 */
596static void upgrade_crypt_ftr(int fd, struct crypt_mnt_ftr *crypt_ftr, off64_t offset)
597{
Kenny Root7434b312013-06-14 11:29:53 -0700598 int orig_major = crypt_ftr->major_version;
599 int orig_minor = crypt_ftr->minor_version;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700600
Kenny Root7434b312013-06-14 11:29:53 -0700601 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 0)) {
602 struct crypt_persist_data *pdata;
603 off64_t pdata_offset = offset + CRYPT_FOOTER_TO_PERSIST_OFFSET;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700604
Kenny Rootc4c70f12013-06-14 12:11:38 -0700605 SLOGW("upgrading crypto footer to 1.1");
606
Wei Wang4375f1b2017-02-24 17:43:01 -0800607 pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
Kenny Root7434b312013-06-14 11:29:53 -0700608 if (pdata == NULL) {
609 SLOGE("Cannot allocate persisent data\n");
610 return;
611 }
612 memset(pdata, 0, CRYPT_PERSIST_DATA_SIZE);
613
614 /* Need to initialize the persistent data area */
615 if (lseek64(fd, pdata_offset, SEEK_SET) == -1) {
616 SLOGE("Cannot seek to persisent data offset\n");
Henrik Baard91064632015-02-05 15:09:17 +0100617 free(pdata);
Kenny Root7434b312013-06-14 11:29:53 -0700618 return;
619 }
620 /* Write all zeros to the first copy, making it invalid */
621 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
622
623 /* Write a valid but empty structure to the second copy */
624 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
625 unix_write(fd, pdata, CRYPT_PERSIST_DATA_SIZE);
626
627 /* Update the footer */
628 crypt_ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
629 crypt_ftr->persist_data_offset[0] = pdata_offset;
630 crypt_ftr->persist_data_offset[1] = pdata_offset + CRYPT_PERSIST_DATA_SIZE;
631 crypt_ftr->minor_version = 1;
Henrik Baard91064632015-02-05 15:09:17 +0100632 free(pdata);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700633 }
634
Paul Lawrencef4faa572014-01-29 13:31:03 -0800635 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 1)) {
Kenny Rootc4c70f12013-06-14 12:11:38 -0700636 SLOGW("upgrading crypto footer to 1.2");
JP Abgrall7bdfa522013-11-15 13:42:56 -0800637 /* But keep the old kdf_type.
638 * It will get updated later to KDF_SCRYPT after the password has been verified.
639 */
Kenny Rootc4c70f12013-06-14 12:11:38 -0700640 crypt_ftr->kdf_type = KDF_PBKDF2;
641 get_device_scrypt_params(crypt_ftr);
642 crypt_ftr->minor_version = 2;
643 }
644
Paul Lawrencef4faa572014-01-29 13:31:03 -0800645 if ((crypt_ftr->major_version == 1) && (crypt_ftr->minor_version == 2)) {
646 SLOGW("upgrading crypto footer to 1.3");
647 crypt_ftr->crypt_type = CRYPT_TYPE_PASSWORD;
648 crypt_ftr->minor_version = 3;
649 }
650
Kenny Root7434b312013-06-14 11:29:53 -0700651 if ((orig_major != crypt_ftr->major_version) || (orig_minor != crypt_ftr->minor_version)) {
652 if (lseek64(fd, offset, SEEK_SET) == -1) {
653 SLOGE("Cannot seek to crypt footer\n");
654 return;
655 }
656 unix_write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr));
Ken Sumrall160b4d62013-04-22 12:15:39 -0700657 }
Ken Sumrall160b4d62013-04-22 12:15:39 -0700658}
659
660
661static int get_crypt_ftr_and_key(struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800662{
663 int fd;
Tim Murray8439dc92014-12-15 11:56:11 -0800664 unsigned int cnt;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700665 off64_t starting_off;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800666 int rc = -1;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700667 char *fname = NULL;
Ken Sumrall29d8da82011-05-18 17:20:07 -0700668 struct stat statbuf;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800669
Ken Sumrall160b4d62013-04-22 12:15:39 -0700670 if (get_crypt_ftr_info(&fname, &starting_off)) {
671 SLOGE("Unable to get crypt_ftr_info\n");
672 return -1;
673 }
674 if (fname[0] != '/') {
Ken Sumralle5032c42012-04-01 23:58:44 -0700675 SLOGE("Unexpected value for crypto key location\n");
Ken Sumrall160b4d62013-04-22 12:15:39 -0700676 return -1;
677 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700678 if ( (fd = open(fname, O_RDWR|O_CLOEXEC)) < 0) {
Ken Sumralle550f782013-08-20 13:48:23 -0700679 SLOGE("Cannot open footer file %s for get\n", fname);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700680 return -1;
681 }
682
683 /* Make sure it's 16 Kbytes in length */
684 fstat(fd, &statbuf);
685 if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
686 SLOGE("footer file %s is not the expected size!\n", fname);
687 goto errout;
688 }
689
690 /* Seek to the start of the crypt footer */
691 if (lseek64(fd, starting_off, SEEK_SET) == -1) {
692 SLOGE("Cannot seek to real block device footer\n");
693 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800694 }
695
696 if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
697 SLOGE("Cannot read real block device footer\n");
698 goto errout;
699 }
700
701 if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
Ken Sumrall29d8da82011-05-18 17:20:07 -0700702 SLOGE("Bad magic for real block device %s\n", fname);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800703 goto errout;
704 }
705
Kenny Rootc96a5f82013-06-14 12:08:28 -0700706 if (crypt_ftr->major_version != CURRENT_MAJOR_VERSION) {
707 SLOGE("Cannot understand major version %d real block device footer; expected %d\n",
708 crypt_ftr->major_version, CURRENT_MAJOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800709 goto errout;
710 }
711
Greg Kaiser59ad0182018-02-16 13:01:36 -0800712 // We risk buffer overflows with oversized keys, so we just reject them.
713 // 0-sized keys are problematic (essentially by-passing encryption), and
714 // AES-CBC key wrapping only works for multiples of 16 bytes.
715 if ((crypt_ftr->keysize == 0) || ((crypt_ftr->keysize % 16) != 0) ||
716 (crypt_ftr->keysize > MAX_KEY_LEN)) {
717 SLOGE("Invalid keysize (%u) for block device %s; Must be non-zero, "
718 "divisible by 16, and <= %d\n", crypt_ftr->keysize, fname,
719 MAX_KEY_LEN);
720 goto errout;
721 }
722
Kenny Rootc96a5f82013-06-14 12:08:28 -0700723 if (crypt_ftr->minor_version > CURRENT_MINOR_VERSION) {
724 SLOGW("Warning: crypto footer minor version %d, expected <= %d, continuing...\n",
725 crypt_ftr->minor_version, CURRENT_MINOR_VERSION);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800726 }
727
Ken Sumrall160b4d62013-04-22 12:15:39 -0700728 /* If this is a verion 1.0 crypt_ftr, make it a 1.1 crypt footer, and update the
729 * copy on disk before returning.
730 */
Kenny Rootc96a5f82013-06-14 12:08:28 -0700731 if (crypt_ftr->minor_version < CURRENT_MINOR_VERSION) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700732 upgrade_crypt_ftr(fd, crypt_ftr, starting_off);
Ken Sumralle8744072011-01-18 22:01:55 -0800733 }
734
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800735 /* Success! */
736 rc = 0;
737
738errout:
739 close(fd);
740 return rc;
741}
742
Ken Sumrall160b4d62013-04-22 12:15:39 -0700743static int validate_persistent_data_storage(struct crypt_mnt_ftr *crypt_ftr)
744{
745 if (crypt_ftr->persist_data_offset[0] + crypt_ftr->persist_data_size >
746 crypt_ftr->persist_data_offset[1]) {
747 SLOGE("Crypt_ftr persist data regions overlap");
748 return -1;
749 }
750
751 if (crypt_ftr->persist_data_offset[0] >= crypt_ftr->persist_data_offset[1]) {
752 SLOGE("Crypt_ftr persist data region 0 starts after region 1");
753 return -1;
754 }
755
756 if (((crypt_ftr->persist_data_offset[1] + crypt_ftr->persist_data_size) -
757 (crypt_ftr->persist_data_offset[0] - CRYPT_FOOTER_TO_PERSIST_OFFSET)) >
758 CRYPT_FOOTER_OFFSET) {
759 SLOGE("Persistent data extends past crypto footer");
760 return -1;
761 }
762
763 return 0;
764}
765
766static int load_persistent_data(void)
767{
768 struct crypt_mnt_ftr crypt_ftr;
769 struct crypt_persist_data *pdata = NULL;
770 char encrypted_state[PROPERTY_VALUE_MAX];
771 char *fname;
772 int found = 0;
773 int fd;
774 int ret;
775 int i;
776
777 if (persist_data) {
778 /* Nothing to do, we've already loaded or initialized it */
779 return 0;
780 }
781
782
783 /* If not encrypted, just allocate an empty table and initialize it */
784 property_get("ro.crypto.state", encrypted_state, "");
785 if (strcmp(encrypted_state, "encrypted") ) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800786 pdata = (crypt_persist_data*)malloc(CRYPT_PERSIST_DATA_SIZE);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700787 if (pdata) {
788 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
789 persist_data = pdata;
790 return 0;
791 }
792 return -1;
793 }
794
795 if(get_crypt_ftr_and_key(&crypt_ftr)) {
796 return -1;
797 }
798
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700799 if ((crypt_ftr.major_version < 1)
800 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700801 SLOGE("Crypt_ftr version doesn't support persistent data");
802 return -1;
803 }
804
805 if (get_crypt_ftr_info(&fname, NULL)) {
806 return -1;
807 }
808
809 ret = validate_persistent_data_storage(&crypt_ftr);
810 if (ret) {
811 return -1;
812 }
813
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700814 fd = open(fname, O_RDONLY|O_CLOEXEC);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700815 if (fd < 0) {
816 SLOGE("Cannot open %s metadata file", fname);
817 return -1;
818 }
819
Wei Wang4375f1b2017-02-24 17:43:01 -0800820 pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
Paul Lawrence300dae72016-03-11 11:02:52 -0800821 if (pdata == NULL) {
822 SLOGE("Cannot allocate memory for persistent data");
823 goto err;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700824 }
825
826 for (i = 0; i < 2; i++) {
827 if (lseek64(fd, crypt_ftr.persist_data_offset[i], SEEK_SET) < 0) {
828 SLOGE("Cannot seek to read persistent data on %s", fname);
829 goto err2;
830 }
831 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0){
832 SLOGE("Error reading persistent data on iteration %d", i);
833 goto err2;
834 }
835 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
836 found = 1;
837 break;
838 }
839 }
840
841 if (!found) {
842 SLOGI("Could not find valid persistent data, creating");
843 init_empty_persist_data(pdata, crypt_ftr.persist_data_size);
844 }
845
846 /* Success */
847 persist_data = pdata;
848 close(fd);
849 return 0;
850
851err2:
852 free(pdata);
853
854err:
855 close(fd);
856 return -1;
857}
858
859static int save_persistent_data(void)
860{
861 struct crypt_mnt_ftr crypt_ftr;
862 struct crypt_persist_data *pdata;
863 char *fname;
864 off64_t write_offset;
865 off64_t erase_offset;
Ken Sumrall160b4d62013-04-22 12:15:39 -0700866 int fd;
867 int ret;
868
869 if (persist_data == NULL) {
870 SLOGE("No persistent data to save");
871 return -1;
872 }
873
874 if(get_crypt_ftr_and_key(&crypt_ftr)) {
875 return -1;
876 }
877
Paul Lawrence8561b5c2014-03-17 14:10:51 -0700878 if ((crypt_ftr.major_version < 1)
879 || (crypt_ftr.major_version == 1 && crypt_ftr.minor_version < 1)) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700880 SLOGE("Crypt_ftr version doesn't support persistent data");
881 return -1;
882 }
883
884 ret = validate_persistent_data_storage(&crypt_ftr);
885 if (ret) {
886 return -1;
887 }
888
889 if (get_crypt_ftr_info(&fname, NULL)) {
890 return -1;
891 }
892
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700893 fd = open(fname, O_RDWR|O_CLOEXEC);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700894 if (fd < 0) {
895 SLOGE("Cannot open %s metadata file", fname);
896 return -1;
897 }
898
Wei Wang4375f1b2017-02-24 17:43:01 -0800899 pdata = (crypt_persist_data*)malloc(crypt_ftr.persist_data_size);
Ken Sumrall160b4d62013-04-22 12:15:39 -0700900 if (pdata == NULL) {
901 SLOGE("Cannot allocate persistant data");
902 goto err;
903 }
904
905 if (lseek64(fd, crypt_ftr.persist_data_offset[0], SEEK_SET) < 0) {
906 SLOGE("Cannot seek to read persistent data on %s", fname);
907 goto err2;
908 }
909
910 if (unix_read(fd, pdata, crypt_ftr.persist_data_size) < 0) {
911 SLOGE("Error reading persistent data before save");
912 goto err2;
913 }
914
915 if (pdata->persist_magic == PERSIST_DATA_MAGIC) {
916 /* The first copy is the curent valid copy, so write to
917 * the second copy and erase this one */
918 write_offset = crypt_ftr.persist_data_offset[1];
919 erase_offset = crypt_ftr.persist_data_offset[0];
920 } else {
921 /* The second copy must be the valid copy, so write to
922 * the first copy, and erase the second */
923 write_offset = crypt_ftr.persist_data_offset[0];
924 erase_offset = crypt_ftr.persist_data_offset[1];
925 }
926
927 /* Write the new copy first, if successful, then erase the old copy */
Björn Landström96dbee72015-01-20 12:43:56 +0100928 if (lseek64(fd, write_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700929 SLOGE("Cannot seek to write persistent data");
930 goto err2;
931 }
932 if (unix_write(fd, persist_data, crypt_ftr.persist_data_size) ==
933 (int) crypt_ftr.persist_data_size) {
Björn Landström96dbee72015-01-20 12:43:56 +0100934 if (lseek64(fd, erase_offset, SEEK_SET) < 0) {
Ken Sumrall160b4d62013-04-22 12:15:39 -0700935 SLOGE("Cannot seek to erase previous persistent data");
936 goto err2;
937 }
938 fsync(fd);
939 memset(pdata, 0, crypt_ftr.persist_data_size);
940 if (unix_write(fd, pdata, crypt_ftr.persist_data_size) !=
941 (int) crypt_ftr.persist_data_size) {
942 SLOGE("Cannot write to erase previous persistent data");
943 goto err2;
944 }
945 fsync(fd);
946 } else {
947 SLOGE("Cannot write to save persistent data");
948 goto err2;
949 }
950
951 /* Success */
952 free(pdata);
953 close(fd);
954 return 0;
955
956err2:
957 free(pdata);
958err:
959 close(fd);
960 return -1;
961}
962
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800963/* Convert a binary key of specified length into an ascii hex string equivalent,
964 * without the leading 0x and with null termination
965 */
Jeff Sharkey9c484982015-03-31 10:35:33 -0700966static void convert_key_to_hex_ascii(const unsigned char *master_key,
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700967 unsigned int keysize, char *master_key_ascii) {
968 unsigned int i, a;
969 unsigned char nibble;
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800970
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700971 for (i=0, a=0; i<keysize; i++, a+=2) {
972 /* For each byte, write out two ascii hex digits */
973 nibble = (master_key[i] >> 4) & 0xf;
974 master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800975
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700976 nibble = master_key[i] & 0xf;
977 master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
978 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800979
Paul Lawrence3bd36d52015-06-09 13:37:44 -0700980 /* Add the null termination */
981 master_key_ascii[a] = '\0';
Ken Sumrall8f869aa2010-12-03 03:47:09 -0800982
983}
984
Jeff Sharkey9c484982015-03-31 10:35:33 -0700985static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr,
986 const unsigned char *master_key, const char *real_blk_name,
987 const char *name, int fd, const char *extra_params) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800988 alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
Ken Sumralldb5e0262013-02-05 17:39:48 -0800989 struct dm_ioctl *io;
990 struct dm_target_spec *tgt;
991 char *crypt_params;
Greg Kaiser59ad0182018-02-16 13:01:36 -0800992 // We need two ASCII characters to represent each byte, and need space for
993 // the '\0' terminator.
994 char master_key_ascii[MAX_KEY_LEN * 2 + 1];
George Burgess IV605d7ae2016-02-29 13:39:17 -0800995 size_t buff_offset;
Ken Sumralldb5e0262013-02-05 17:39:48 -0800996 int i;
997
998 io = (struct dm_ioctl *) buffer;
999
1000 /* Load the mapping table for this device */
1001 tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
1002
1003 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1004 io->target_count = 1;
1005 tgt->status = 0;
1006 tgt->sector_start = 0;
1007 tgt->length = crypt_ftr->fs_size;
Ajay Dudani87701e22014-09-17 21:02:52 -07001008 strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME);
Ken Sumralldb5e0262013-02-05 17:39:48 -08001009
1010 crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
1011 convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
George Burgess IV605d7ae2016-02-29 13:39:17 -08001012
1013 buff_offset = crypt_params - buffer;
Paul Crowley5afbc622017-11-27 09:42:17 -08001014 SLOGI("Extra parameters for dm_crypt: %s\n", extra_params);
George Burgess IV605d7ae2016-02-29 13:39:17 -08001015 snprintf(crypt_params, sizeof(buffer) - buff_offset, "%s %s 0 %s 0 %s",
1016 crypt_ftr->crypto_type_name, master_key_ascii, real_blk_name,
1017 extra_params);
Ken Sumralldb5e0262013-02-05 17:39:48 -08001018 crypt_params += strlen(crypt_params) + 1;
1019 crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
1020 tgt->next = crypt_params - buffer;
1021
1022 for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
1023 if (! ioctl(fd, DM_TABLE_LOAD, io)) {
1024 break;
1025 }
1026 usleep(500000);
1027 }
1028
1029 if (i == TABLE_LOAD_RETRIES) {
1030 /* We failed to load the table, return an error */
1031 return -1;
1032 } else {
1033 return i + 1;
1034 }
1035}
1036
1037
1038static int get_dm_crypt_version(int fd, const char *name, int *version)
1039{
1040 char buffer[DM_CRYPT_BUF_SIZE];
1041 struct dm_ioctl *io;
1042 struct dm_target_versions *v;
Ken Sumralldb5e0262013-02-05 17:39:48 -08001043
1044 io = (struct dm_ioctl *) buffer;
1045
1046 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1047
1048 if (ioctl(fd, DM_LIST_VERSIONS, io)) {
1049 return -1;
1050 }
1051
1052 /* Iterate over the returned versions, looking for name of "crypt".
1053 * When found, get and return the version.
1054 */
1055 v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
1056 while (v->next) {
1057 if (! strcmp(v->name, "crypt")) {
1058 /* We found the crypt driver, return the version, and get out */
1059 version[0] = v->version[0];
1060 version[1] = v->version[1];
1061 version[2] = v->version[2];
1062 return 0;
1063 }
1064 v = (struct dm_target_versions *)(((char *)v) + v->next);
1065 }
1066
1067 return -1;
1068}
1069
Paul Crowley5afbc622017-11-27 09:42:17 -08001070static std::string extra_params_as_string(const std::vector<std::string>& extra_params_vec) {
1071 if (extra_params_vec.empty()) return "";
1072 std::string extra_params = std::to_string(extra_params_vec.size());
1073 for (const auto& p : extra_params_vec) {
1074 extra_params.append(" ");
1075 extra_params.append(p);
1076 }
1077 return extra_params;
1078}
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001079
Paul Crowley5afbc622017-11-27 09:42:17 -08001080static int create_crypto_blk_dev(struct crypt_mnt_ftr* crypt_ftr, const unsigned char* master_key,
1081 const char* real_blk_name, char* crypto_blk_name, const char* name,
1082 uint32_t flags) {
1083 char buffer[DM_CRYPT_BUF_SIZE];
1084 struct dm_ioctl* io;
1085 unsigned int minor;
1086 int fd = 0;
1087 int err;
1088 int retval = -1;
1089 int version[3];
1090 int load_count;
1091 std::vector<std::string> extra_params_vec;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001092
Paul Crowley5afbc622017-11-27 09:42:17 -08001093 if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
1094 SLOGE("Cannot open device-mapper\n");
1095 goto errout;
1096 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001097
Paul Crowley5afbc622017-11-27 09:42:17 -08001098 io = (struct dm_ioctl*)buffer;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001099
Paul Crowley5afbc622017-11-27 09:42:17 -08001100 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1101 err = ioctl(fd, DM_DEV_CREATE, io);
1102 if (err) {
1103 SLOGE("Cannot create dm-crypt device %s: %s\n", name, strerror(errno));
1104 goto errout;
1105 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001106
Paul Crowley5afbc622017-11-27 09:42:17 -08001107 /* Get the device status, in particular, the name of it's device file */
1108 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1109 if (ioctl(fd, DM_DEV_STATUS, io)) {
1110 SLOGE("Cannot retrieve dm-crypt device status\n");
1111 goto errout;
1112 }
1113 minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
1114 snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
Ken Sumralle919efe2012-09-29 17:07:41 -07001115
Paul Crowley5afbc622017-11-27 09:42:17 -08001116 if (!get_dm_crypt_version(fd, name, version)) {
1117 /* Support for allow_discards was added in version 1.11.0 */
1118 if ((version[0] >= 2) || ((version[0] == 1) && (version[1] >= 11))) {
1119 extra_params_vec.emplace_back("allow_discards");
1120 }
1121 }
1122 if (flags & CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE) {
1123 extra_params_vec.emplace_back("allow_encrypt_override");
1124 }
1125 load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name, fd,
1126 extra_params_as_string(extra_params_vec).c_str());
1127 if (load_count < 0) {
1128 SLOGE("Cannot load dm-crypt mapping table.\n");
1129 goto errout;
1130 } else if (load_count > 1) {
1131 SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
1132 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001133
Paul Crowley5afbc622017-11-27 09:42:17 -08001134 /* Resume this device to activate it */
1135 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001136
Paul Crowley5afbc622017-11-27 09:42:17 -08001137 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
1138 SLOGE("Cannot resume the dm-crypt device\n");
1139 goto errout;
1140 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001141
Paul Crowley5afbc622017-11-27 09:42:17 -08001142 /* We made it here with no errors. Woot! */
1143 retval = 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001144
1145errout:
1146 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1147
1148 return retval;
1149}
1150
Wei Wang4375f1b2017-02-24 17:43:01 -08001151static int delete_crypto_blk_dev(const char *name)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001152{
1153 int fd;
1154 char buffer[DM_CRYPT_BUF_SIZE];
1155 struct dm_ioctl *io;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001156 int retval = -1;
1157
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001158 if ((fd = open("/dev/device-mapper", O_RDWR|O_CLOEXEC)) < 0 ) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001159 SLOGE("Cannot open device-mapper\n");
1160 goto errout;
1161 }
1162
1163 io = (struct dm_ioctl *) buffer;
1164
1165 ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
1166 if (ioctl(fd, DM_DEV_REMOVE, io)) {
1167 SLOGE("Cannot remove dm-crypt device\n");
1168 goto errout;
1169 }
1170
1171 /* We made it here with no errors. Woot! */
1172 retval = 0;
1173
1174errout:
1175 close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
1176
1177 return retval;
1178
1179}
1180
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001181static int pbkdf2(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001182 unsigned char *ikey, void *params UNUSED)
1183{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001184 SLOGI("Using pbkdf2 for cryptfs KDF");
1185
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001186 /* Turn the password into a key and IV that can decrypt the master key */
Adam Langleybf0d9722015-11-04 14:51:39 -08001187 return PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001188 HASH_COUNT, INTERMEDIATE_BUF_SIZE,
Adam Langleybf0d9722015-11-04 14:51:39 -08001189 ikey) != 1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001190}
1191
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001192static int scrypt(const char *passwd, const unsigned char *salt,
Paul Lawrencef4faa572014-01-29 13:31:03 -08001193 unsigned char *ikey, void *params)
1194{
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001195 SLOGI("Using scrypt for cryptfs KDF");
1196
Kenny Rootc4c70f12013-06-14 12:11:38 -07001197 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1198
1199 int N = 1 << ftr->N_factor;
1200 int r = 1 << ftr->r_factor;
1201 int p = 1 << ftr->p_factor;
1202
1203 /* Turn the password into a key and IV that can decrypt the master key */
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001204 crypto_scrypt((const uint8_t*)passwd, strlen(passwd),
1205 salt, SALT_LEN, N, r, p, ikey,
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001206 INTERMEDIATE_BUF_SIZE);
Paul Lawrencef4faa572014-01-29 13:31:03 -08001207
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001208 return 0;
Kenny Rootc4c70f12013-06-14 12:11:38 -07001209}
1210
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001211static int scrypt_keymaster(const char *passwd, const unsigned char *salt,
1212 unsigned char *ikey, void *params)
1213{
1214 SLOGI("Using scrypt with keymaster for cryptfs KDF");
1215
1216 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001217 size_t signature_size;
1218 unsigned char* signature;
1219 struct crypt_mnt_ftr *ftr = (struct crypt_mnt_ftr *) params;
1220
1221 int N = 1 << ftr->N_factor;
1222 int r = 1 << ftr->r_factor;
1223 int p = 1 << ftr->p_factor;
1224
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001225 rc = crypto_scrypt((const uint8_t*)passwd, strlen(passwd),
1226 salt, SALT_LEN, N, r, p, ikey,
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001227 INTERMEDIATE_BUF_SIZE);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001228
1229 if (rc) {
1230 SLOGE("scrypt failed");
1231 return -1;
1232 }
1233
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001234 if (keymaster_sign_object(ftr, ikey, INTERMEDIATE_BUF_SIZE,
Shawn Willdene17a9c42014-09-08 13:04:08 -06001235 &signature, &signature_size)) {
1236 SLOGE("Signing failed");
1237 return -1;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001238 }
1239
1240 rc = crypto_scrypt(signature, signature_size, salt, SALT_LEN,
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001241 N, r, p, ikey, INTERMEDIATE_BUF_SIZE);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001242 free(signature);
1243
1244 if (rc) {
1245 SLOGE("scrypt failed");
1246 return -1;
1247 }
1248
1249 return 0;
1250}
1251
1252static int encrypt_master_key(const char *passwd, const unsigned char *salt,
1253 const unsigned char *decrypted_master_key,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001254 unsigned char *encrypted_master_key,
1255 struct crypt_mnt_ftr *crypt_ftr)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001256{
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001257 unsigned char ikey[INTERMEDIATE_BUF_SIZE] = { 0 };
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001258 EVP_CIPHER_CTX e_ctx;
1259 int encrypted_len, final_len;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001260 int rc = 0;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001261
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001262 /* Turn the password into an intermediate key and IV that can decrypt the master key */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001263 get_device_scrypt_params(crypt_ftr);
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001264
1265 switch (crypt_ftr->kdf_type) {
1266 case KDF_SCRYPT_KEYMASTER:
1267 if (keymaster_create_key(crypt_ftr)) {
1268 SLOGE("keymaster_create_key failed");
1269 return -1;
1270 }
1271
1272 if (scrypt_keymaster(passwd, salt, ikey, crypt_ftr)) {
1273 SLOGE("scrypt failed");
1274 return -1;
1275 }
1276 break;
1277
1278 case KDF_SCRYPT:
1279 if (scrypt(passwd, salt, ikey, crypt_ftr)) {
1280 SLOGE("scrypt failed");
1281 return -1;
1282 }
1283 break;
1284
1285 default:
1286 SLOGE("Invalid kdf_type");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001287 return -1;
1288 }
Kenny Rootc4c70f12013-06-14 12:11:38 -07001289
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001290 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001291 EVP_CIPHER_CTX_init(&e_ctx);
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001292 if (! EVP_EncryptInit_ex(&e_ctx, EVP_aes_128_cbc(), NULL, ikey,
1293 ikey+INTERMEDIATE_KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001294 SLOGE("EVP_EncryptInit failed\n");
1295 return -1;
1296 }
1297 EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001298
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001299 /* Encrypt the master key */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001300 if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
Greg Kaiser59ad0182018-02-16 13:01:36 -08001301 decrypted_master_key, crypt_ftr->keysize)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001302 SLOGE("EVP_EncryptUpdate failed\n");
1303 return -1;
1304 }
Adam Langley889c4f12014-09-03 14:23:13 -07001305 if (! EVP_EncryptFinal_ex(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001306 SLOGE("EVP_EncryptFinal failed\n");
1307 return -1;
1308 }
1309
Greg Kaiser59ad0182018-02-16 13:01:36 -08001310 if (encrypted_len + final_len != static_cast<int>(crypt_ftr->keysize)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001311 SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
1312 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001313 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001314
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001315 /* Store the scrypt of the intermediate key, so we can validate if it's a
1316 password error or mount error when things go wrong.
1317 Note there's no need to check for errors, since if this is incorrect, we
1318 simply won't wipe userdata, which is the correct default behavior
1319 */
1320 int N = 1 << crypt_ftr->N_factor;
1321 int r = 1 << crypt_ftr->r_factor;
1322 int p = 1 << crypt_ftr->p_factor;
1323
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001324 rc = crypto_scrypt(ikey, INTERMEDIATE_KEY_LEN_BYTES,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001325 crypt_ftr->salt, sizeof(crypt_ftr->salt), N, r, p,
1326 crypt_ftr->scrypted_intermediate_key,
1327 sizeof(crypt_ftr->scrypted_intermediate_key));
1328
1329 if (rc) {
1330 SLOGE("encrypt_master_key: crypto_scrypt failed");
1331 }
1332
Thurston Hou Yeen Dang06dc3112016-07-18 14:16:37 -07001333 EVP_CIPHER_CTX_cleanup(&e_ctx);
1334
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001335 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001336}
1337
Paul Lawrence731a7a22015-04-28 22:14:15 +00001338static int decrypt_master_key_aux(const char *passwd, unsigned char *salt,
Greg Kaiser59ad0182018-02-16 13:01:36 -08001339 const unsigned char *encrypted_master_key,
1340 size_t keysize,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001341 unsigned char *decrypted_master_key,
1342 kdf_func kdf, void *kdf_params,
1343 unsigned char** intermediate_key,
1344 size_t* intermediate_key_size)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001345{
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001346 unsigned char ikey[INTERMEDIATE_BUF_SIZE] = { 0 };
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001347 EVP_CIPHER_CTX d_ctx;
1348 int decrypted_len, final_len;
1349
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001350 /* Turn the password into an intermediate key and IV that can decrypt the
1351 master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001352 if (kdf(passwd, salt, ikey, kdf_params)) {
1353 SLOGE("kdf failed");
1354 return -1;
1355 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001356
1357 /* Initialize the decryption engine */
Adam Langley889c4f12014-09-03 14:23:13 -07001358 EVP_CIPHER_CTX_init(&d_ctx);
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001359 if (! EVP_DecryptInit_ex(&d_ctx, EVP_aes_128_cbc(), NULL, ikey, ikey+INTERMEDIATE_KEY_LEN_BYTES)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001360 return -1;
1361 }
1362 EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
1363 /* Decrypt the master key */
1364 if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
Greg Kaiser59ad0182018-02-16 13:01:36 -08001365 encrypted_master_key, keysize)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001366 return -1;
1367 }
Adam Langley889c4f12014-09-03 14:23:13 -07001368 if (! EVP_DecryptFinal_ex(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001369 return -1;
1370 }
1371
Greg Kaiser59ad0182018-02-16 13:01:36 -08001372 if (decrypted_len + final_len != static_cast<int>(keysize)) {
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001373 return -1;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001374 }
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001375
1376 /* Copy intermediate key if needed by params */
1377 if (intermediate_key && intermediate_key_size) {
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001378 *intermediate_key = (unsigned char*) malloc(INTERMEDIATE_KEY_LEN_BYTES);
Greg Kaisere8167af2016-04-20 10:50:15 -07001379 if (*intermediate_key) {
Greg Kaiserc0de9c72018-02-14 20:05:54 -08001380 memcpy(*intermediate_key, ikey, INTERMEDIATE_KEY_LEN_BYTES);
1381 *intermediate_key_size = INTERMEDIATE_KEY_LEN_BYTES;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001382 }
1383 }
1384
Thurston Hou Yeen Dang06dc3112016-07-18 14:16:37 -07001385 EVP_CIPHER_CTX_cleanup(&d_ctx);
1386
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001387 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001388}
1389
Kenny Rootc4c70f12013-06-14 12:11:38 -07001390static void get_kdf_func(struct crypt_mnt_ftr *ftr, kdf_func *kdf, void** kdf_params)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001391{
Paul Lawrencedb3730c2015-02-03 13:08:10 -08001392 if (ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001393 *kdf = scrypt_keymaster;
1394 *kdf_params = ftr;
1395 } else if (ftr->kdf_type == KDF_SCRYPT) {
Kenny Rootc4c70f12013-06-14 12:11:38 -07001396 *kdf = scrypt;
1397 *kdf_params = ftr;
1398 } else {
1399 *kdf = pbkdf2;
1400 *kdf_params = NULL;
1401 }
1402}
1403
Paul Lawrence731a7a22015-04-28 22:14:15 +00001404static int decrypt_master_key(const char *passwd, unsigned char *decrypted_master_key,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001405 struct crypt_mnt_ftr *crypt_ftr,
1406 unsigned char** intermediate_key,
1407 size_t* intermediate_key_size)
Kenny Rootc4c70f12013-06-14 12:11:38 -07001408{
1409 kdf_func kdf;
1410 void *kdf_params;
1411 int ret;
1412
1413 get_kdf_func(crypt_ftr, &kdf, &kdf_params);
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001414 ret = decrypt_master_key_aux(passwd, crypt_ftr->salt, crypt_ftr->master_key,
Greg Kaiser59ad0182018-02-16 13:01:36 -08001415 crypt_ftr->keysize,
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001416 decrypted_master_key, kdf, kdf_params,
1417 intermediate_key, intermediate_key_size);
Kenny Rootc4c70f12013-06-14 12:11:38 -07001418 if (ret != 0) {
1419 SLOGW("failure decrypting master key");
Kenny Rootc4c70f12013-06-14 12:11:38 -07001420 }
1421
1422 return ret;
1423}
1424
Wei Wang4375f1b2017-02-24 17:43:01 -08001425static int create_encrypted_random_key(const char *passwd, unsigned char *master_key, unsigned char *salt,
Kenny Rootc4c70f12013-06-14 12:11:38 -07001426 struct crypt_mnt_ftr *crypt_ftr) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001427 int fd;
Greg Kaiser59ad0182018-02-16 13:01:36 -08001428 unsigned char key_buf[MAX_KEY_LEN];
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001429
1430 /* Get some random bits for a key */
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001431 fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC);
Ken Sumralle8744072011-01-18 22:01:55 -08001432 read(fd, key_buf, sizeof(key_buf));
1433 read(fd, salt, SALT_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001434 close(fd);
1435
1436 /* Now encrypt it with the password */
Kenny Rootc4c70f12013-06-14 12:11:38 -07001437 return encrypt_master_key(passwd, salt, key_buf, master_key, crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001438}
1439
Paul Lawrence2f32cda2015-05-05 14:28:25 -07001440int wait_and_unmount(const char *mountpoint, bool kill)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001441{
Greg Hackmann955653e2014-09-24 14:55:20 -07001442 int i, err, rc;
Ken Sumrall2eaf7132011-01-14 12:45:48 -08001443#define WAIT_UNMOUNT_COUNT 20
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001444
1445 /* Now umount the tmpfs filesystem */
1446 for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001447 if (umount(mountpoint) == 0) {
1448 break;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001449 }
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001450
1451 if (errno == EINVAL) {
1452 /* EINVAL is returned if the directory is not a mountpoint,
1453 * i.e. there is no filesystem mounted there. So just get out.
1454 */
1455 break;
1456 }
1457
1458 err = errno;
1459
1460 /* If allowed, be increasingly aggressive before the last two retries */
1461 if (kill) {
1462 if (i == (WAIT_UNMOUNT_COUNT - 3)) {
1463 SLOGW("sending SIGHUP to processes with open files\n");
Jeff Sharkey3472e522017-10-06 18:02:53 -06001464 android::vold::KillProcessesWithOpenFiles(mountpoint, SIGTERM);
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001465 } else if (i == (WAIT_UNMOUNT_COUNT - 2)) {
1466 SLOGW("sending SIGKILL to processes with open files\n");
Jeff Sharkey3472e522017-10-06 18:02:53 -06001467 android::vold::KillProcessesWithOpenFiles(mountpoint, SIGKILL);
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001468 }
1469 }
1470
1471 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001472 }
1473
1474 if (i < WAIT_UNMOUNT_COUNT) {
1475 SLOGD("unmounting %s succeeded\n", mountpoint);
1476 rc = 0;
1477 } else {
Jeff Sharkey3472e522017-10-06 18:02:53 -06001478 android::vold::KillProcessesWithOpenFiles(mountpoint, 0);
Greg Hackmann955653e2014-09-24 14:55:20 -07001479 SLOGE("unmounting %s failed: %s\n", mountpoint, strerror(err));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001480 rc = -1;
1481 }
1482
1483 return rc;
1484}
1485
Wei Wang42e38102017-06-07 10:46:12 -07001486static void prep_data_fs(void)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001487{
Jeff Sharkey47695b22016-02-01 17:02:29 -07001488 // NOTE: post_fs_data results in init calling back around to vold, so all
1489 // callers to this method must be async
1490
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001491 /* Do the prep of the /data filesystem */
1492 property_set("vold.post_fs_data_done", "0");
1493 property_set("vold.decrypt", "trigger_post_fs_data");
Wei Wang42e38102017-06-07 10:46:12 -07001494 SLOGD("Just triggered post_fs_data");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001495
Ken Sumrallc5872692013-05-14 15:26:31 -07001496 /* Wait a max of 50 seconds, hopefully it takes much less */
Wei Wang42e38102017-06-07 10:46:12 -07001497 while (!android::base::WaitForProperty("vold.post_fs_data_done",
Wei Wang4375f1b2017-02-24 17:43:01 -08001498 "1",
Wei Wang42e38102017-06-07 10:46:12 -07001499 std::chrono::seconds(15))) {
1500 /* We timed out to prep /data in time. Continue wait. */
1501 SLOGE("waited 15s for vold.post_fs_data_done, still waiting...");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001502 }
Wei Wang42e38102017-06-07 10:46:12 -07001503 SLOGD("post_fs_data done");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08001504}
1505
Paul Lawrence74f29f12014-08-28 15:54:10 -07001506static void cryptfs_set_corrupt()
1507{
1508 // Mark the footer as bad
1509 struct crypt_mnt_ftr crypt_ftr;
1510 if (get_crypt_ftr_and_key(&crypt_ftr)) {
1511 SLOGE("Failed to get crypto footer - panic");
1512 return;
1513 }
1514
1515 crypt_ftr.flags |= CRYPT_DATA_CORRUPT;
1516 if (put_crypt_ftr_and_key(&crypt_ftr)) {
1517 SLOGE("Failed to set crypto footer - panic");
1518 return;
1519 }
1520}
1521
1522static void cryptfs_trigger_restart_min_framework()
1523{
1524 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
1525 SLOGE("Failed to mount tmpfs on data - panic");
1526 return;
1527 }
1528
1529 if (property_set("vold.decrypt", "trigger_post_fs_data")) {
1530 SLOGE("Failed to trigger post fs data - panic");
1531 return;
1532 }
1533
1534 if (property_set("vold.decrypt", "trigger_restart_min_framework")) {
1535 SLOGE("Failed to trigger restart min framework - panic");
1536 return;
1537 }
1538}
1539
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001540/* returns < 0 on failure */
Paul Lawrencef4faa572014-01-29 13:31:03 -08001541static int cryptfs_restart_internal(int restart_main)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001542{
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001543 char crypto_blkdev[MAXPATHLEN];
Tim Murray8439dc92014-12-15 11:56:11 -08001544 int rc = -1;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001545 static int restart_successful = 0;
1546
1547 /* Validate that it's OK to call this routine */
Jason parks70a4b3f2011-01-28 10:10:47 -06001548 if (! master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08001549 SLOGE("Encrypted filesystem not validated, aborting");
1550 return -1;
1551 }
1552
1553 if (restart_successful) {
1554 SLOGE("System already restarted with encrypted disk, aborting");
1555 return -1;
1556 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001557
Paul Lawrencef4faa572014-01-29 13:31:03 -08001558 if (restart_main) {
1559 /* Here is where we shut down the framework. The init scripts
1560 * start all services in one of three classes: core, main or late_start.
1561 * On boot, we start core and main. Now, we stop main, but not core,
1562 * as core includes vold and a few other really important things that
1563 * we need to keep running. Once main has stopped, we should be able
1564 * to umount the tmpfs /data, then mount the encrypted /data.
1565 * We then restart the class main, and also the class late_start.
1566 * At the moment, I've only put a few things in late_start that I know
1567 * are not needed to bring up the framework, and that also cause problems
1568 * with unmounting the tmpfs /data, but I hope to add add more services
1569 * to the late_start class as we optimize this to decrease the delay
1570 * till the user is asked for the password to the filesystem.
1571 */
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001572
Paul Lawrencef4faa572014-01-29 13:31:03 -08001573 /* The init files are setup to stop the class main when vold.decrypt is
1574 * set to trigger_reset_main.
1575 */
1576 property_set("vold.decrypt", "trigger_reset_main");
1577 SLOGD("Just asked init to shut down class main\n");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001578
Paul Lawrencef4faa572014-01-29 13:31:03 -08001579 /* Ugh, shutting down the framework is not synchronous, so until it
1580 * can be fixed, this horrible hack will wait a moment for it all to
1581 * shut down before proceeding. Without it, some devices cannot
1582 * restart the graphics services.
1583 */
1584 sleep(2);
1585 }
Ken Sumrall9dedfd42012-10-09 14:16:59 -07001586
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001587 /* Now that the framework is shutdown, we should be able to umount()
1588 * the tmpfs filesystem, and mount the real one.
1589 */
1590
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001591 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "");
1592 if (strlen(crypto_blkdev) == 0) {
1593 SLOGE("fs_crypto_blkdev not set\n");
1594 return -1;
1595 }
1596
Greg Hackmann6e8440f2014-10-02 17:18:20 -07001597 if (! (rc = wait_and_unmount(DATA_MNT_POINT, true)) ) {
Doug Zongker6fd57712013-12-17 09:43:23 -08001598 /* If ro.crypto.readonly is set to 1, mount the decrypted
1599 * filesystem readonly. This is used when /data is mounted by
1600 * recovery mode.
1601 */
1602 char ro_prop[PROPERTY_VALUE_MAX];
1603 property_get("ro.crypto.readonly", ro_prop, "");
Jeff Sharkey95440eb2017-09-18 18:19:28 -06001604 if (strlen(ro_prop) > 0 && std::stoi(ro_prop)) {
Paul Crowleye2ee1522017-09-26 14:05:26 -07001605 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
Luis Hector Chavezbbb512d2018-05-30 15:47:50 -07001606 if (rec) {
1607 rec->flags |= MS_RDONLY;
1608 }
Doug Zongker6fd57712013-12-17 09:43:23 -08001609 }
JP Abgrall62c7af32014-06-16 13:01:23 -07001610
Ken Sumralle5032c42012-04-01 23:58:44 -07001611 /* If that succeeded, then mount the decrypted filesystem */
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001612 int retries = RETRY_MOUNT_ATTEMPTS;
1613 int mount_rc;
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001614
1615 /*
1616 * fs_mgr_do_mount runs fsck. Use setexeccon to run trusted
1617 * partitions in the fsck domain.
1618 */
1619 if (setexeccon(secontextFsck())){
1620 SLOGE("Failed to setexeccon");
1621 return -1;
1622 }
Paul Crowleye2ee1522017-09-26 14:05:26 -07001623 while ((mount_rc = fs_mgr_do_mount(fstab_default, DATA_MNT_POINT,
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001624 crypto_blkdev, 0))
1625 != 0) {
1626 if (mount_rc == FS_MGR_DOMNT_BUSY) {
1627 /* TODO: invoke something similar to
1628 Process::killProcessWithOpenFiles(DATA_MNT_POINT,
1629 retries > RETRY_MOUNT_ATTEMPT/2 ? 1 : 2 ) */
1630 SLOGI("Failed to mount %s because it is busy - waiting",
1631 crypto_blkdev);
1632 if (--retries) {
1633 sleep(RETRY_MOUNT_DELAY_SECONDS);
1634 } else {
1635 /* Let's hope that a reboot clears away whatever is keeping
1636 the mount busy */
Josh Gaofec44372017-08-28 13:22:55 -07001637 cryptfs_reboot(RebootType::reboot);
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001638 }
1639 } else {
1640 SLOGE("Failed to mount decrypted data");
1641 cryptfs_set_corrupt();
1642 cryptfs_trigger_restart_min_framework();
1643 SLOGI("Started framework to offer wipe");
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001644 if (setexeccon(NULL)) {
1645 SLOGE("Failed to setexeccon");
1646 }
Paul Lawrence8e3f4512014-09-08 10:11:17 -07001647 return -1;
1648 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001649 }
Jeff Vander Stoepdf725752016-01-29 15:34:43 -08001650 if (setexeccon(NULL)) {
1651 SLOGE("Failed to setexeccon");
1652 return -1;
1653 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001654
Ken Sumralle5032c42012-04-01 23:58:44 -07001655 /* Create necessary paths on /data */
Wei Wang42e38102017-06-07 10:46:12 -07001656 prep_data_fs();
Seigo Nonakae2ef0c02016-06-20 17:05:40 +09001657 property_set("vold.decrypt", "trigger_load_persist_props");
Ken Sumralle5032c42012-04-01 23:58:44 -07001658
1659 /* startup service classes main and late_start */
1660 property_set("vold.decrypt", "trigger_restart_framework");
1661 SLOGD("Just triggered restart_framework\n");
1662
1663 /* Give it a few moments to get started */
1664 sleep(1);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001665 }
1666
Ken Sumrall0cc16632011-01-18 20:32:26 -08001667 if (rc == 0) {
1668 restart_successful = 1;
1669 }
1670
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001671 return rc;
1672}
1673
Paul Lawrencef4faa572014-01-29 13:31:03 -08001674int cryptfs_restart(void)
1675{
Paul Lawrence05335c32015-03-05 09:46:23 -08001676 SLOGI("cryptfs_restart");
Paul Crowley38132a12016-02-09 09:50:32 +00001677 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001678 SLOGE("cryptfs_restart not valid for file encryption:");
1679 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08001680 }
1681
Paul Lawrencef4faa572014-01-29 13:31:03 -08001682 /* Call internal implementation forcing a restart of main service group */
1683 return cryptfs_restart_internal(1);
1684}
1685
Wei Wang4375f1b2017-02-24 17:43:01 -08001686static int do_crypto_complete(const char *mount_point)
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001687{
1688 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001689 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumralle1a45852011-12-14 21:24:27 -08001690 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001691
1692 property_get("ro.crypto.state", encrypted_state, "");
1693 if (strcmp(encrypted_state, "encrypted") ) {
1694 SLOGE("not running with encryption, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001695 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001696 }
1697
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001698 // crypto_complete is full disk encrypted status
Paul Crowley38132a12016-02-09 09:50:32 +00001699 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001700 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Paul Lawrence05335c32015-03-05 09:46:23 -08001701 }
1702
Ken Sumrall160b4d62013-04-22 12:15:39 -07001703 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Crowleye2ee1522017-09-26 14:05:26 -07001704 fs_mgr_get_crypt_info(fstab_default, key_loc, 0, sizeof(key_loc));
Ken Sumralle5032c42012-04-01 23:58:44 -07001705
Ken Sumralle1a45852011-12-14 21:24:27 -08001706 /*
1707 * Only report this error if key_loc is a file and it exists.
1708 * If the device was never encrypted, and /data is not mountable for
1709 * some reason, returning 1 should prevent the UI from presenting the
1710 * a "enter password" screen, or worse, a "press button to wipe the
1711 * device" screen.
1712 */
1713 if ((key_loc[0] == '/') && (access("key_loc", F_OK) == -1)) {
1714 SLOGE("master key file does not exist, aborting");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001715 return CRYPTO_COMPLETE_NOT_ENCRYPTED;
Ken Sumralle1a45852011-12-14 21:24:27 -08001716 } else {
1717 SLOGE("Error getting crypt footer and key\n");
Paul Lawrence74f29f12014-08-28 15:54:10 -07001718 return CRYPTO_COMPLETE_BAD_METADATA;
Ken Sumralle1a45852011-12-14 21:24:27 -08001719 }
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001720 }
1721
Paul Lawrence74f29f12014-08-28 15:54:10 -07001722 // Test for possible error flags
1723 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS){
1724 SLOGE("Encryption process is partway completed\n");
1725 return CRYPTO_COMPLETE_PARTIAL;
1726 }
1727
1728 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE){
1729 SLOGE("Encryption process was interrupted but cannot continue\n");
1730 return CRYPTO_COMPLETE_INCONSISTENT;
1731 }
1732
1733 if (crypt_ftr.flags & CRYPT_DATA_CORRUPT){
1734 SLOGE("Encryption is successful but data is corrupt\n");
1735 return CRYPTO_COMPLETE_CORRUPT;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001736 }
1737
1738 /* We passed the test! We shall diminish, and return to the west */
Paul Lawrence74f29f12014-08-28 15:54:10 -07001739 return CRYPTO_COMPLETE_ENCRYPTED;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001740}
1741
Paul Lawrencef4faa572014-01-29 13:31:03 -08001742static int test_mount_encrypted_fs(struct crypt_mnt_ftr* crypt_ftr,
Wei Wang4375f1b2017-02-24 17:43:01 -08001743 const char *passwd, const char *mount_point, const char *label)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001744{
Greg Kaiser59ad0182018-02-16 13:01:36 -08001745 unsigned char decrypted_master_key[MAX_KEY_LEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001746 char crypto_blkdev[MAXPATHLEN];
1747 char real_blkdev[MAXPATHLEN];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001748 char tmp_mount_point[64];
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001749 unsigned int orig_failed_decrypt_count;
1750 int rc;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001751 int use_keymaster = 0;
1752 int upgrade = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001753 unsigned char* intermediate_key = 0;
1754 size_t intermediate_key_size = 0;
Wei Wang4375f1b2017-02-24 17:43:01 -08001755 int N = 1 << crypt_ftr->N_factor;
1756 int r = 1 << crypt_ftr->r_factor;
1757 int p = 1 << crypt_ftr->p_factor;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001758
Paul Lawrencef4faa572014-01-29 13:31:03 -08001759 SLOGD("crypt_ftr->fs_size = %lld\n", crypt_ftr->fs_size);
1760 orig_failed_decrypt_count = crypt_ftr->failed_decrypt_count;
Ken Sumrall0cc16632011-01-18 20:32:26 -08001761
Paul Lawrencef4faa572014-01-29 13:31:03 -08001762 if (! (crypt_ftr->flags & CRYPT_MNT_KEY_UNENCRYPTED) ) {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001763 if (decrypt_master_key(passwd, decrypted_master_key, crypt_ftr,
1764 &intermediate_key, &intermediate_key_size)) {
JP Abgrall7bdfa522013-11-15 13:42:56 -08001765 SLOGE("Failed to decrypt master key\n");
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001766 rc = -1;
1767 goto errout;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001768 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001769 }
1770
Paul Crowleye2ee1522017-09-26 14:05:26 -07001771 fs_mgr_get_crypt_info(fstab_default, 0, real_blkdev, sizeof(real_blkdev));
Paul Lawrencef4faa572014-01-29 13:31:03 -08001772
Paul Lawrence74f29f12014-08-28 15:54:10 -07001773 // Create crypto block device - all (non fatal) code paths
1774 // need it
Paul Crowley5afbc622017-11-27 09:42:17 -08001775 if (create_crypto_blk_dev(crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev, label, 0)) {
1776 SLOGE("Error creating decrypted block device\n");
1777 rc = -1;
1778 goto errout;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001779 }
1780
Paul Lawrence74f29f12014-08-28 15:54:10 -07001781 /* Work out if the problem is the password or the data */
1782 unsigned char scrypted_intermediate_key[sizeof(crypt_ftr->
1783 scrypted_intermediate_key)];
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001784
Paul Lawrence74f29f12014-08-28 15:54:10 -07001785 rc = crypto_scrypt(intermediate_key, intermediate_key_size,
1786 crypt_ftr->salt, sizeof(crypt_ftr->salt),
1787 N, r, p, scrypted_intermediate_key,
1788 sizeof(scrypted_intermediate_key));
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001789
Paul Lawrence74f29f12014-08-28 15:54:10 -07001790 // Does the key match the crypto footer?
1791 if (rc == 0 && memcmp(scrypted_intermediate_key,
1792 crypt_ftr->scrypted_intermediate_key,
1793 sizeof(scrypted_intermediate_key)) == 0) {
1794 SLOGI("Password matches");
1795 rc = 0;
1796 } else {
1797 /* Try mounting the file system anyway, just in case the problem's with
1798 * the footer, not the key. */
George Burgess IV605d7ae2016-02-29 13:39:17 -08001799 snprintf(tmp_mount_point, sizeof(tmp_mount_point), "%s/tmp_mnt",
1800 mount_point);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001801 mkdir(tmp_mount_point, 0755);
Paul Crowleye2ee1522017-09-26 14:05:26 -07001802 if (fs_mgr_do_mount(fstab_default, DATA_MNT_POINT, crypto_blkdev, tmp_mount_point)) {
Paul Lawrence74f29f12014-08-28 15:54:10 -07001803 SLOGE("Error temp mounting decrypted block device\n");
1804 delete_crypto_blk_dev(label);
1805
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001806 rc = ++crypt_ftr->failed_decrypt_count;
1807 put_crypt_ftr_and_key(crypt_ftr);
Paul Lawrence74f29f12014-08-28 15:54:10 -07001808 } else {
1809 /* Success! */
1810 SLOGI("Password did not match but decrypted drive mounted - continue");
1811 umount(tmp_mount_point);
1812 rc = 0;
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001813 }
Paul Lawrence74f29f12014-08-28 15:54:10 -07001814 }
1815
1816 if (rc == 0) {
1817 crypt_ftr->failed_decrypt_count = 0;
Paul Lawrence72b8b822014-10-05 12:57:37 -07001818 if (orig_failed_decrypt_count != 0) {
1819 put_crypt_ftr_and_key(crypt_ftr);
1820 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001821
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001822 /* Save the name of the crypto block device
Paul Lawrence74f29f12014-08-28 15:54:10 -07001823 * so we can mount it when restarting the framework. */
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001824 property_set("ro.crypto.fs_crypto_blkdev", crypto_blkdev);
Jason parks70a4b3f2011-01-28 10:10:47 -06001825
1826 /* Also save a the master key so we can reencrypted the key
Paul Lawrence74f29f12014-08-28 15:54:10 -07001827 * the key when we want to change the password on it. */
Greg Kaiser59ad0182018-02-16 13:01:36 -08001828 memcpy(saved_master_key, decrypted_master_key, crypt_ftr->keysize);
Ken Sumrall3ad90722011-10-04 20:38:29 -07001829 saved_mount_point = strdup(mount_point);
Jason parks70a4b3f2011-01-28 10:10:47 -06001830 master_key_saved = 1;
JP Abgrall7bdfa522013-11-15 13:42:56 -08001831 SLOGD("%s(): Master key saved\n", __FUNCTION__);
Ken Sumrall6864b7e2011-01-14 15:20:02 -08001832 rc = 0;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001833
Paul Lawrence74f29f12014-08-28 15:54:10 -07001834 // Upgrade if we're not using the latest KDF.
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001835 use_keymaster = keymaster_check_compatibility();
1836 if (crypt_ftr->kdf_type == KDF_SCRYPT_KEYMASTER) {
Shawn Willden47ba10d2014-09-03 17:07:06 -06001837 // Don't allow downgrade
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001838 } else if (use_keymaster == 1 && crypt_ftr->kdf_type != KDF_SCRYPT_KEYMASTER) {
1839 crypt_ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
1840 upgrade = 1;
1841 } else if (use_keymaster == 0 && crypt_ftr->kdf_type != KDF_SCRYPT) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001842 crypt_ftr->kdf_type = KDF_SCRYPT;
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07001843 upgrade = 1;
1844 }
1845
1846 if (upgrade) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001847 rc = encrypt_master_key(passwd, crypt_ftr->salt, saved_master_key,
1848 crypt_ftr->master_key, crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001849 if (!rc) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08001850 rc = put_crypt_ftr_and_key(crypt_ftr);
JP Abgrall7bdfa522013-11-15 13:42:56 -08001851 }
1852 SLOGD("Key Derivation Function upgrade: rc=%d\n", rc);
Paul Lawrenceb2f682b2014-09-08 11:28:19 -07001853
1854 // Do not fail even if upgrade failed - machine is bootable
1855 // Note that if this code is ever hit, there is a *serious* problem
1856 // since KDFs should never fail. You *must* fix the kdf before
1857 // proceeding!
1858 if (rc) {
1859 SLOGW("Upgrade failed with error %d,"
1860 " but continuing with previous state",
1861 rc);
1862 rc = 0;
1863 }
JP Abgrall7bdfa522013-11-15 13:42:56 -08001864 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001865 }
1866
Paul Lawrenced0c7b172014-08-08 14:28:10 -07001867 errout:
1868 if (intermediate_key) {
1869 memset(intermediate_key, 0, intermediate_key_size);
1870 free(intermediate_key);
1871 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001872 return rc;
1873}
1874
Ken Sumrall29d8da82011-05-18 17:20:07 -07001875/*
Jeff Sharkey9c484982015-03-31 10:35:33 -07001876 * Called by vold when it's asked to mount an encrypted external
1877 * storage volume. The incoming partition has no crypto header/footer,
Greg Kaiser57f9af62018-02-16 13:13:58 -08001878 * as any metadata is been stored in a separate, small partition. We
1879 * assume it must be using our same crypt type and keysize.
Jeff Sharkey9c484982015-03-31 10:35:33 -07001880 *
1881 * out_crypto_blkdev must be MAXPATHLEN.
Ken Sumrall29d8da82011-05-18 17:20:07 -07001882 */
Jeff Sharkey9c484982015-03-31 10:35:33 -07001883int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev,
Greg Kaiser57f9af62018-02-16 13:13:58 -08001884 const unsigned char* key, char* out_crypto_blkdev) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07001885 int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001886 if (fd == -1) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001887 SLOGE("Failed to open %s: %s", real_blkdev, strerror(errno));
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001888 return -1;
1889 }
1890
1891 unsigned long nr_sec = 0;
1892 get_blkdev_size(fd, &nr_sec);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001893 close(fd);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09001894
Ken Sumrall29d8da82011-05-18 17:20:07 -07001895 if (nr_sec == 0) {
Jeff Sharkey9c484982015-03-31 10:35:33 -07001896 SLOGE("Failed to get size of %s: %s", real_blkdev, strerror(errno));
Ken Sumrall29d8da82011-05-18 17:20:07 -07001897 return -1;
1898 }
1899
Jeff Sharkey9c484982015-03-31 10:35:33 -07001900 struct crypt_mnt_ftr ext_crypt_ftr;
1901 memset(&ext_crypt_ftr, 0, sizeof(ext_crypt_ftr));
1902 ext_crypt_ftr.fs_size = nr_sec;
Greg Kaiser57f9af62018-02-16 13:13:58 -08001903 ext_crypt_ftr.keysize = cryptfs_get_keysize();
1904 strlcpy((char*) ext_crypt_ftr.crypto_type_name, cryptfs_get_crypto_name(),
Jeff Sharkey32ebb732017-03-27 16:18:50 -06001905 MAX_CRYPTO_TYPE_NAME_LEN);
Paul Crowley385cb8c2018-03-29 13:27:23 -07001906 uint32_t flags = 0;
1907 if (e4crypt_is_native() &&
1908 android::base::GetBoolProperty("ro.crypto.allow_encrypt_override", false))
1909 flags |= CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE;
Ken Sumrall29d8da82011-05-18 17:20:07 -07001910
Paul Crowley385cb8c2018-03-29 13:27:23 -07001911 return create_crypto_blk_dev(&ext_crypt_ftr, key, real_blkdev, out_crypto_blkdev, label, flags);
Jeff Sharkey9c484982015-03-31 10:35:33 -07001912}
Ken Sumrall29d8da82011-05-18 17:20:07 -07001913
Jeff Sharkey9c484982015-03-31 10:35:33 -07001914/*
1915 * Called by vold when it's asked to unmount an encrypted external
1916 * storage volume.
1917 */
1918int cryptfs_revert_ext_volume(const char* label) {
1919 return delete_crypto_blk_dev((char*) label);
Ken Sumrall29d8da82011-05-18 17:20:07 -07001920}
1921
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08001922int cryptfs_crypto_complete(void)
1923{
1924 return do_crypto_complete("/data");
1925}
1926
Paul Lawrencef4faa572014-01-29 13:31:03 -08001927int check_unmounted_and_get_ftr(struct crypt_mnt_ftr* crypt_ftr)
1928{
1929 char encrypted_state[PROPERTY_VALUE_MAX];
1930 property_get("ro.crypto.state", encrypted_state, "");
1931 if ( master_key_saved || strcmp(encrypted_state, "encrypted") ) {
1932 SLOGE("encrypted fs already validated or not running with encryption,"
1933 " aborting");
1934 return -1;
1935 }
1936
1937 if (get_crypt_ftr_and_key(crypt_ftr)) {
1938 SLOGE("Error getting crypt footer and key");
1939 return -1;
1940 }
1941
1942 return 0;
1943}
1944
Wei Wang4375f1b2017-02-24 17:43:01 -08001945int cryptfs_check_passwd(const char *passwd)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001946{
Paul Lawrence05335c32015-03-05 09:46:23 -08001947 SLOGI("cryptfs_check_passwd");
Paul Crowley38132a12016-02-09 09:50:32 +00001948 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08001949 SLOGE("cryptfs_check_passwd not valid for file encryption");
1950 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08001951 }
1952
Paul Lawrencef4faa572014-01-29 13:31:03 -08001953 struct crypt_mnt_ftr crypt_ftr;
1954 int rc;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001955
Paul Lawrencef4faa572014-01-29 13:31:03 -08001956 rc = check_unmounted_and_get_ftr(&crypt_ftr);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001957 if (rc) {
1958 SLOGE("Could not get footer");
Paul Lawrencef4faa572014-01-29 13:31:03 -08001959 return rc;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001960 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001961
Paul Lawrence3bd36d52015-06-09 13:37:44 -07001962 rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001963 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
1964 if (rc) {
1965 SLOGE("Password did not match");
1966 return rc;
1967 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08001968
Paul Lawrence3d99eba2015-11-20 07:07:19 -08001969 if (crypt_ftr.flags & CRYPT_FORCE_COMPLETE) {
1970 // Here we have a default actual password but a real password
1971 // we must test against the scrypted value
1972 // First, we must delete the crypto block device that
1973 // test_mount_encrypted_fs leaves behind as a side effect
1974 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
1975 rc = test_mount_encrypted_fs(&crypt_ftr, DEFAULT_PASSWORD,
1976 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
1977 if (rc) {
1978 SLOGE("Default password did not match on reboot encryption");
1979 return rc;
1980 }
1981
1982 crypt_ftr.flags &= ~CRYPT_FORCE_COMPLETE;
1983 put_crypt_ftr_and_key(&crypt_ftr);
1984 rc = cryptfs_changepw(crypt_ftr.crypt_type, passwd);
1985 if (rc) {
1986 SLOGE("Could not change password on reboot encryption");
1987 return rc;
1988 }
1989 }
1990
1991 if (crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
Paul Lawrence399317e2014-03-10 13:20:50 -07001992 cryptfs_clear_password();
1993 password = strdup(passwd);
1994 struct timespec now;
1995 clock_gettime(CLOCK_BOOTTIME, &now);
1996 password_expiry_time = now.tv_sec + password_max_age_seconds;
Paul Lawrence684dbdf2014-02-07 12:07:22 -08001997 }
1998
Ken Sumrall8f869aa2010-12-03 03:47:09 -08001999 return rc;
2000}
2001
Jeff Sharkey83b559c2017-09-12 16:30:52 -06002002int cryptfs_verify_passwd(const char *passwd)
Ken Sumrall3ad90722011-10-04 20:38:29 -07002003{
2004 struct crypt_mnt_ftr crypt_ftr;
Greg Kaiser59ad0182018-02-16 13:01:36 -08002005 unsigned char decrypted_master_key[MAX_KEY_LEN];
Ken Sumrall3ad90722011-10-04 20:38:29 -07002006 char encrypted_state[PROPERTY_VALUE_MAX];
2007 int rc;
2008
2009 property_get("ro.crypto.state", encrypted_state, "");
2010 if (strcmp(encrypted_state, "encrypted") ) {
2011 SLOGE("device not encrypted, aborting");
2012 return -2;
2013 }
2014
2015 if (!master_key_saved) {
2016 SLOGE("encrypted fs not yet mounted, aborting");
2017 return -1;
2018 }
2019
2020 if (!saved_mount_point) {
2021 SLOGE("encrypted fs failed to save mount point, aborting");
2022 return -1;
2023 }
2024
Ken Sumrall160b4d62013-04-22 12:15:39 -07002025 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Ken Sumrall3ad90722011-10-04 20:38:29 -07002026 SLOGE("Error getting crypt footer and key\n");
2027 return -1;
2028 }
2029
2030 if (crypt_ftr.flags & CRYPT_MNT_KEY_UNENCRYPTED) {
2031 /* If the device has no password, then just say the password is valid */
2032 rc = 0;
2033 } else {
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002034 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall3ad90722011-10-04 20:38:29 -07002035 if (!memcmp(decrypted_master_key, saved_master_key, crypt_ftr.keysize)) {
2036 /* They match, the password is correct */
2037 rc = 0;
2038 } else {
2039 /* If incorrect, sleep for a bit to prevent dictionary attacks */
2040 sleep(1);
2041 rc = 1;
2042 }
2043 }
2044
2045 return rc;
2046}
2047
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002048/* Initialize a crypt_mnt_ftr structure. The keysize is
Greg Kaiser57f9af62018-02-16 13:13:58 -08002049 * defaulted to cryptfs_get_keysize() bytes, and the filesystem size to 0.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002050 * Presumably, at a minimum, the caller will update the
2051 * filesystem size and crypto_type_name after calling this function.
2052 */
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002053static int cryptfs_init_crypt_mnt_ftr(struct crypt_mnt_ftr *ftr)
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002054{
Ken Sumrall160b4d62013-04-22 12:15:39 -07002055 off64_t off;
2056
2057 memset(ftr, 0, sizeof(struct crypt_mnt_ftr));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002058 ftr->magic = CRYPT_MNT_MAGIC;
Kenny Rootc96a5f82013-06-14 12:08:28 -07002059 ftr->major_version = CURRENT_MAJOR_VERSION;
2060 ftr->minor_version = CURRENT_MINOR_VERSION;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002061 ftr->ftr_size = sizeof(struct crypt_mnt_ftr);
Greg Kaiser57f9af62018-02-16 13:13:58 -08002062 ftr->keysize = cryptfs_get_keysize();
Ken Sumrall160b4d62013-04-22 12:15:39 -07002063
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002064 switch (keymaster_check_compatibility()) {
2065 case 1:
2066 ftr->kdf_type = KDF_SCRYPT_KEYMASTER;
2067 break;
2068
2069 case 0:
2070 ftr->kdf_type = KDF_SCRYPT;
2071 break;
2072
2073 default:
2074 SLOGE("keymaster_check_compatibility failed");
2075 return -1;
2076 }
2077
Kenny Rootc4c70f12013-06-14 12:11:38 -07002078 get_device_scrypt_params(ftr);
2079
Ken Sumrall160b4d62013-04-22 12:15:39 -07002080 ftr->persist_data_size = CRYPT_PERSIST_DATA_SIZE;
2081 if (get_crypt_ftr_info(NULL, &off) == 0) {
2082 ftr->persist_data_offset[0] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET;
2083 ftr->persist_data_offset[1] = off + CRYPT_FOOTER_TO_PERSIST_OFFSET +
2084 ftr->persist_data_size;
2085 }
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002086
2087 return 0;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002088}
2089
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002090#define FRAMEWORK_BOOT_WAIT 60
2091
Paul Lawrence87999172014-02-20 12:21:31 -08002092static int cryptfs_SHA256_fileblock(const char* filename, __le8* buf)
2093{
Jeff Sharkeyce6a9132015-04-08 21:07:21 -07002094 int fd = open(filename, O_RDONLY|O_CLOEXEC);
Paul Lawrence87999172014-02-20 12:21:31 -08002095 if (fd == -1) {
2096 SLOGE("Error opening file %s", filename);
2097 return -1;
2098 }
2099
2100 char block[CRYPT_INPLACE_BUFSIZE];
2101 memset(block, 0, sizeof(block));
2102 if (unix_read(fd, block, sizeof(block)) < 0) {
2103 SLOGE("Error reading file %s", filename);
2104 close(fd);
2105 return -1;
2106 }
2107
2108 close(fd);
2109
2110 SHA256_CTX c;
2111 SHA256_Init(&c);
2112 SHA256_Update(&c, block, sizeof(block));
2113 SHA256_Final(buf, &c);
2114
2115 return 0;
2116}
2117
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002118static int cryptfs_enable_all_volumes(struct crypt_mnt_ftr* crypt_ftr, char* crypto_blkdev,
2119 char* real_blkdev, int previously_encrypted_upto) {
Paul Lawrence87999172014-02-20 12:21:31 -08002120 off64_t cur_encryption_done=0, tot_encryption_size=0;
Tim Murray8439dc92014-12-15 11:56:11 -08002121 int rc = -1;
Paul Lawrence87999172014-02-20 12:21:31 -08002122
Paul Lawrence87999172014-02-20 12:21:31 -08002123 /* The size of the userdata partition, and add in the vold volumes below */
2124 tot_encryption_size = crypt_ftr->fs_size;
2125
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002126 rc = cryptfs_enable_inplace(crypto_blkdev, real_blkdev, crypt_ftr->fs_size, &cur_encryption_done,
Paul Crowley0fd26262018-01-30 09:48:19 -08002127 tot_encryption_size, previously_encrypted_upto, true);
Paul Lawrence87999172014-02-20 12:21:31 -08002128
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002129 if (rc == ENABLE_INPLACE_ERR_DEV) {
2130 /* Hack for b/17898962 */
2131 SLOGE("cryptfs_enable: crypto block dev failure. Must reboot...\n");
2132 cryptfs_reboot(RebootType::reboot);
2133 }
JP Abgrall7fc1de82014-10-10 18:43:41 -07002134
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002135 if (!rc) {
2136 crypt_ftr->encrypted_upto = cur_encryption_done;
2137 }
Paul Lawrence87999172014-02-20 12:21:31 -08002138
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002139 if (!rc && crypt_ftr->encrypted_upto == crypt_ftr->fs_size) {
2140 /* The inplace routine never actually sets the progress to 100% due
2141 * to the round down nature of integer division, so set it here */
2142 property_set("vold.encrypt_progress", "100");
Paul Lawrence87999172014-02-20 12:21:31 -08002143 }
2144
2145 return rc;
2146}
2147
Paul Crowleyb64933a2017-10-31 08:25:55 -07002148static int vold_unmountAll(void) {
2149 VolumeManager* vm = VolumeManager::Instance();
2150 return vm->unmountAll();
2151}
2152
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002153int cryptfs_enable_internal(int crypt_type, const char* passwd, int no_ui) {
Paul Lawrence87999172014-02-20 12:21:31 -08002154 char crypto_blkdev[MAXPATHLEN], real_blkdev[MAXPATHLEN];
Greg Kaiser59ad0182018-02-16 13:01:36 -08002155 unsigned char decrypted_master_key[MAX_KEY_LEN];
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002156 int rc=-1, i;
Paul Lawrence87999172014-02-20 12:21:31 -08002157 struct crypt_mnt_ftr crypt_ftr;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002158 struct crypt_persist_data *pdata;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002159 char encrypted_state[PROPERTY_VALUE_MAX];
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002160 char lockid[32] = { 0 };
Ken Sumrall29d8da82011-05-18 17:20:07 -07002161 char key_loc[PROPERTY_VALUE_MAX];
Ken Sumrall29d8da82011-05-18 17:20:07 -07002162 int num_vols;
Paul Lawrence87999172014-02-20 12:21:31 -08002163 off64_t previously_encrypted_upto = 0;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002164 bool rebootEncryption = false;
Wei Wang4375f1b2017-02-24 17:43:01 -08002165 bool onlyCreateHeader = false;
2166 int fd = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002167
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002168 if (get_crypt_ftr_and_key(&crypt_ftr) == 0) {
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002169 if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
2170 /* An encryption was underway and was interrupted */
2171 previously_encrypted_upto = crypt_ftr.encrypted_upto;
2172 crypt_ftr.encrypted_upto = 0;
2173 crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002174
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002175 /* At this point, we are in an inconsistent state. Until we successfully
2176 complete encryption, a reboot will leave us broken. So mark the
2177 encryption failed in case that happens.
2178 On successfully completing encryption, remove this flag */
2179 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
Paul Lawrence6bfed202014-07-28 12:47:22 -07002180
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002181 put_crypt_ftr_and_key(&crypt_ftr);
2182 } else if (crypt_ftr.flags & CRYPT_FORCE_ENCRYPTION) {
2183 if (!check_ftr_sha(&crypt_ftr)) {
2184 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
2185 put_crypt_ftr_and_key(&crypt_ftr);
2186 goto error_unencrypted;
2187 }
2188
2189 /* Doing a reboot-encryption*/
2190 crypt_ftr.flags &= ~CRYPT_FORCE_ENCRYPTION;
2191 crypt_ftr.flags |= CRYPT_FORCE_COMPLETE;
2192 rebootEncryption = true;
2193 }
Greg Kaiser59ad0182018-02-16 13:01:36 -08002194 } else {
2195 // We don't want to accidentally reference invalid data.
2196 memset(&crypt_ftr, 0, sizeof(crypt_ftr));
Paul Lawrence87999172014-02-20 12:21:31 -08002197 }
2198
2199 property_get("ro.crypto.state", encrypted_state, "");
2200 if (!strcmp(encrypted_state, "encrypted") && !previously_encrypted_upto) {
2201 SLOGE("Device is already running encrypted, aborting");
2202 goto error_unencrypted;
2203 }
2204
2205 // TODO refactor fs_mgr_get_crypt_info to get both in one call
Paul Crowleye2ee1522017-09-26 14:05:26 -07002206 fs_mgr_get_crypt_info(fstab_default, key_loc, 0, sizeof(key_loc));
2207 fs_mgr_get_crypt_info(fstab_default, 0, real_blkdev, sizeof(real_blkdev));
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002208
Ken Sumrall3ed82362011-01-28 23:31:16 -08002209 /* Get the size of the real block device */
Wei Wang4375f1b2017-02-24 17:43:01 -08002210 fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
Hiroaki Miyazawa14eab552015-02-04 13:29:15 +09002211 if (fd == -1) {
2212 SLOGE("Cannot open block device %s\n", real_blkdev);
2213 goto error_unencrypted;
2214 }
2215 unsigned long nr_sec;
2216 get_blkdev_size(fd, &nr_sec);
2217 if (nr_sec == 0) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002218 SLOGE("Cannot get size of block device %s\n", real_blkdev);
2219 goto error_unencrypted;
2220 }
2221 close(fd);
2222
2223 /* If doing inplace encryption, make sure the orig fs doesn't include the crypto footer */
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002224 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002225 unsigned int fs_size_sec, max_fs_size_sec;
Jim Millera70abc62014-08-15 02:00:45 +00002226 fs_size_sec = get_fs_size(real_blkdev);
Daniel Rosenberge82df162014-08-15 22:19:23 +00002227 if (fs_size_sec == 0)
2228 fs_size_sec = get_f2fs_filesystem_size_sec(real_blkdev);
2229
Paul Lawrence87999172014-02-20 12:21:31 -08002230 max_fs_size_sec = nr_sec - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002231
2232 if (fs_size_sec > max_fs_size_sec) {
2233 SLOGE("Orig filesystem overlaps crypto footer region. Cannot encrypt in place.");
2234 goto error_unencrypted;
2235 }
2236 }
2237
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002238 /* Get a wakelock as this may take a while, and we don't want the
2239 * device to sleep on us. We'll grab a partial wakelock, and if the UI
2240 * wants to keep the screen on, it can grab a full wakelock.
2241 */
Ken Sumrall29d8da82011-05-18 17:20:07 -07002242 snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int) getpid());
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002243 acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
2244
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002245 /* The init files are setup to stop the class main and late start when
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002246 * vold sets trigger_shutdown_framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002247 */
2248 property_set("vold.decrypt", "trigger_shutdown_framework");
2249 SLOGD("Just asked init to shut down class main\n");
2250
Jeff Sharkey9c484982015-03-31 10:35:33 -07002251 /* Ask vold to unmount all devices that it manages */
2252 if (vold_unmountAll()) {
2253 SLOGE("Failed to unmount all vold managed devices");
Ken Sumrall2eaf7132011-01-14 12:45:48 -08002254 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002255
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002256 /* no_ui means we are being called from init, not settings.
2257 Now we always reboot from settings, so !no_ui means reboot
2258 */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002259 if (!no_ui) {
2260 /* Try fallback, which is to reboot and try there */
2261 onlyCreateHeader = true;
2262 FILE* breadcrumb = fopen(BREADCRUMB_FILE, "we");
2263 if (breadcrumb == 0) {
2264 SLOGE("Failed to create breadcrumb file");
2265 goto error_shutting_down;
2266 }
2267 fclose(breadcrumb);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002268 }
2269
2270 /* Do extra work for a better UX when doing the long inplace encryption */
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002271 if (!onlyCreateHeader) {
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002272 /* Now that /data is unmounted, we need to mount a tmpfs
2273 * /data, set a property saying we're doing inplace encryption,
2274 * and restart the framework.
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002275 */
Ken Sumralle5032c42012-04-01 23:58:44 -07002276 if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
Ken Sumrall3ed82362011-01-28 23:31:16 -08002277 goto error_shutting_down;
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002278 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002279 /* Tells the framework that inplace encryption is starting */
Ken Sumrall7df84122011-01-18 14:04:08 -08002280 property_set("vold.encrypt_progress", "0");
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002281
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002282 /* restart the framework. */
2283 /* Create necessary paths on /data */
Wei Wang42e38102017-06-07 10:46:12 -07002284 prep_data_fs();
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002285
Ken Sumrall92736ef2012-10-17 20:57:14 -07002286 /* Ugh, shutting down the framework is not synchronous, so until it
2287 * can be fixed, this horrible hack will wait a moment for it all to
2288 * shut down before proceeding. Without it, some devices cannot
2289 * restart the graphics services.
2290 */
2291 sleep(2);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002292 }
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002293
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002294 /* Start the actual work of making an encrypted filesystem */
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002295 /* Initialize a crypt_mnt_ftr for the partition */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002296 if (previously_encrypted_upto == 0 && !rebootEncryption) {
Paul Lawrence69f4ebd2014-04-14 12:17:14 -07002297 if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) {
2298 goto error_shutting_down;
2299 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002300
Paul Lawrence87999172014-02-20 12:21:31 -08002301 if (!strcmp(key_loc, KEY_IN_FOOTER)) {
2302 crypt_ftr.fs_size = nr_sec
2303 - (CRYPT_FOOTER_OFFSET / CRYPT_SECTOR_SIZE);
2304 } else {
2305 crypt_ftr.fs_size = nr_sec;
2306 }
Paul Lawrence6bfed202014-07-28 12:47:22 -07002307 /* At this point, we are in an inconsistent state. Until we successfully
2308 complete encryption, a reboot will leave us broken. So mark the
2309 encryption failed in case that happens.
2310 On successfully completing encryption, remove this flag */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002311 if (onlyCreateHeader) {
2312 crypt_ftr.flags |= CRYPT_FORCE_ENCRYPTION;
2313 } else {
2314 crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
2315 }
Paul Lawrence87999172014-02-20 12:21:31 -08002316 crypt_ftr.crypt_type = crypt_type;
Greg Kaiser57f9af62018-02-16 13:13:58 -08002317 strlcpy((char *)crypt_ftr.crypto_type_name, cryptfs_get_crypto_name(), MAX_CRYPTO_TYPE_NAME_LEN);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002318
Paul Lawrence87999172014-02-20 12:21:31 -08002319 /* Make an encrypted master key */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002320 if (create_encrypted_random_key(onlyCreateHeader ? DEFAULT_PASSWORD : passwd,
2321 crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
Paul Lawrence87999172014-02-20 12:21:31 -08002322 SLOGE("Cannot create encrypted master key\n");
2323 goto error_shutting_down;
2324 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002325
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002326 /* Replace scrypted intermediate key if we are preparing for a reboot */
2327 if (onlyCreateHeader) {
Greg Kaiser59ad0182018-02-16 13:01:36 -08002328 unsigned char fake_master_key[MAX_KEY_LEN];
2329 unsigned char encrypted_fake_master_key[MAX_KEY_LEN];
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002330 memset(fake_master_key, 0, sizeof(fake_master_key));
2331 encrypt_master_key(passwd, crypt_ftr.salt, fake_master_key,
2332 encrypted_fake_master_key, &crypt_ftr);
2333 }
2334
Paul Lawrence87999172014-02-20 12:21:31 -08002335 /* Write the key to the end of the partition */
2336 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002337
Paul Lawrence87999172014-02-20 12:21:31 -08002338 /* If any persistent data has been remembered, save it.
2339 * If none, create a valid empty table and save that.
2340 */
2341 if (!persist_data) {
Wei Wang4375f1b2017-02-24 17:43:01 -08002342 pdata = (crypt_persist_data *)malloc(CRYPT_PERSIST_DATA_SIZE);
Paul Lawrence87999172014-02-20 12:21:31 -08002343 if (pdata) {
2344 init_empty_persist_data(pdata, CRYPT_PERSIST_DATA_SIZE);
2345 persist_data = pdata;
2346 }
2347 }
2348 if (persist_data) {
2349 save_persistent_data();
2350 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002351 }
2352
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002353 if (onlyCreateHeader) {
2354 sleep(2);
Josh Gaofec44372017-08-28 13:22:55 -07002355 cryptfs_reboot(RebootType::reboot);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002356 }
2357
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002358 if (!no_ui || rebootEncryption) {
Ajay Dudani87701e22014-09-17 21:02:52 -07002359 /* startup service classes main and late_start */
2360 property_set("vold.decrypt", "trigger_restart_min_framework");
2361 SLOGD("Just triggered restart_min_framework\n");
2362
2363 /* OK, the framework is restarted and will soon be showing a
2364 * progress bar. Time to setup an encrypted mapping, and
2365 * either write a new filesystem, or encrypt in place updating
2366 * the progress bar as we work.
2367 */
2368 }
2369
Paul Lawrenced0c7b172014-08-08 14:28:10 -07002370 decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002371 create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
Paul Crowley5afbc622017-11-27 09:42:17 -08002372 CRYPTO_BLOCK_DEVICE, 0);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002373
Paul Lawrence87999172014-02-20 12:21:31 -08002374 /* If we are continuing, check checksums match */
2375 rc = 0;
2376 if (previously_encrypted_upto) {
2377 __le8 hash_first_block[SHA256_DIGEST_LENGTH];
2378 rc = cryptfs_SHA256_fileblock(crypto_blkdev, hash_first_block);
Ken Sumrall128626f2011-06-28 18:45:14 -07002379
Paul Lawrence87999172014-02-20 12:21:31 -08002380 if (!rc && memcmp(hash_first_block, crypt_ftr.hash_first_block,
2381 sizeof(hash_first_block)) != 0) {
2382 SLOGE("Checksums do not match - trigger wipe");
2383 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002384 }
2385 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002386
Paul Lawrence87999172014-02-20 12:21:31 -08002387 if (!rc) {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002388 rc = cryptfs_enable_all_volumes(&crypt_ftr, crypto_blkdev, real_blkdev,
Paul Lawrence87999172014-02-20 12:21:31 -08002389 previously_encrypted_upto);
2390 }
2391
2392 /* Calculate checksum if we are not finished */
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002393 if (!rc && crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002394 rc = cryptfs_SHA256_fileblock(crypto_blkdev,
2395 crypt_ftr.hash_first_block);
Paul Lawrence73d7a022014-06-09 14:10:09 -07002396 if (rc) {
Paul Lawrence87999172014-02-20 12:21:31 -08002397 SLOGE("Error calculating checksum for continuing encryption");
2398 rc = -1;
Ken Sumrall29d8da82011-05-18 17:20:07 -07002399 }
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002400 }
2401
2402 /* Undo the dm-crypt mapping whether we succeed or not */
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002403 delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
Ken Sumrall29d8da82011-05-18 17:20:07 -07002404
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002405 if (! rc) {
2406 /* Success */
Paul Lawrence6bfed202014-07-28 12:47:22 -07002407 crypt_ftr.flags &= ~CRYPT_INCONSISTENT_STATE;
Ken Sumrall7f7dbaa2011-02-01 15:46:41 -08002408
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002409 if (crypt_ftr.encrypted_upto != crypt_ftr.fs_size) {
Paul Lawrence87999172014-02-20 12:21:31 -08002410 SLOGD("Encrypted up to sector %lld - will continue after reboot",
2411 crypt_ftr.encrypted_upto);
Paul Lawrence6bfed202014-07-28 12:47:22 -07002412 crypt_ftr.flags |= CRYPT_ENCRYPTION_IN_PROGRESS;
Paul Lawrence87999172014-02-20 12:21:31 -08002413 }
Paul Lawrence73d7a022014-06-09 14:10:09 -07002414
Paul Lawrence6bfed202014-07-28 12:47:22 -07002415 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumralld33d4172011-02-01 00:49:13 -08002416
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002417 if (crypt_ftr.encrypted_upto == crypt_ftr.fs_size) {
2418 char value[PROPERTY_VALUE_MAX];
2419 property_get("ro.crypto.state", value, "");
2420 if (!strcmp(value, "")) {
2421 /* default encryption - continue first boot sequence */
2422 property_set("ro.crypto.state", "encrypted");
2423 property_set("ro.crypto.type", "block");
2424 release_wake_lock(lockid);
2425 if (rebootEncryption && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
2426 // Bring up cryptkeeper that will check the password and set it
2427 property_set("vold.decrypt", "trigger_shutdown_framework");
2428 sleep(2);
2429 property_set("vold.encrypt_progress", "");
2430 cryptfs_trigger_restart_min_framework();
2431 } else {
2432 cryptfs_check_passwd(DEFAULT_PASSWORD);
2433 cryptfs_restart_internal(1);
2434 }
2435 return 0;
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002436 } else {
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002437 sleep(2); /* Give the UI a chance to show 100% progress */
2438 cryptfs_reboot(RebootType::reboot);
Paul Lawrence3d99eba2015-11-20 07:07:19 -08002439 }
Paul Lawrence87999172014-02-20 12:21:31 -08002440 } else {
Paul Lawrenceb6672e12014-08-15 07:37:28 -07002441 sleep(2); /* Partially encrypted, ensure writes flushed to ssd */
Josh Gaofec44372017-08-28 13:22:55 -07002442 cryptfs_reboot(RebootType::shutdown);
Paul Lawrence87999172014-02-20 12:21:31 -08002443 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002444 } else {
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002445 char value[PROPERTY_VALUE_MAX];
2446
Ken Sumrall319369a2012-06-27 16:30:18 -07002447 property_get("ro.vold.wipe_on_crypt_fail", value, "0");
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002448 if (!strcmp(value, "1")) {
2449 /* wipe data if encryption failed */
2450 SLOGE("encryption failed - rebooting into recovery to wipe data\n");
Wei Wang4375f1b2017-02-24 17:43:01 -08002451 std::string err;
2452 const std::vector<std::string> options = {
2453 "--wipe_data\n--reason=cryptfs_enable_internal\n"
2454 };
2455 if (!write_bootloader_message(options, &err)) {
2456 SLOGE("could not write bootloader message: %s", err.c_str());
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002457 }
Josh Gaofec44372017-08-28 13:22:55 -07002458 cryptfs_reboot(RebootType::recovery);
Mike Lockwoodee6d8c42012-02-15 13:43:28 -08002459 } else {
2460 /* set property to trigger dialog */
2461 property_set("vold.encrypt_progress", "error_partially_encrypted");
2462 release_wake_lock(lockid);
2463 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002464 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002465 }
2466
Ken Sumrall3ed82362011-01-28 23:31:16 -08002467 /* hrm, the encrypt step claims success, but the reboot failed.
2468 * This should not happen.
2469 * Set the property and return. Hope the framework can deal with it.
2470 */
2471 property_set("vold.encrypt_progress", "error_reboot_failed");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002472 release_wake_lock(lockid);
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002473 return rc;
Ken Sumrall3ed82362011-01-28 23:31:16 -08002474
2475error_unencrypted:
2476 property_set("vold.encrypt_progress", "error_not_encrypted");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002477 if (lockid[0]) {
2478 release_wake_lock(lockid);
2479 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002480 return -1;
2481
2482error_shutting_down:
2483 /* we failed, and have not encrypted anthing, so the users's data is still intact,
2484 * but the framework is stopped and not restarted to show the error, so it's up to
2485 * vold to restart the system.
2486 */
2487 SLOGE("Error enabling encryption after framework is shutdown, no data changed, restarting system");
Josh Gaofec44372017-08-28 13:22:55 -07002488 cryptfs_reboot(RebootType::reboot);
Ken Sumrall3ed82362011-01-28 23:31:16 -08002489
2490 /* shouldn't get here */
2491 property_set("vold.encrypt_progress", "error_shutting_down");
Ken Sumrall5d4c68e2011-01-30 19:06:03 -08002492 if (lockid[0]) {
2493 release_wake_lock(lockid);
2494 }
Ken Sumrall3ed82362011-01-28 23:31:16 -08002495 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002496}
2497
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002498int cryptfs_enable(int type, const char* passwd, int no_ui) {
2499 return cryptfs_enable_internal(type, passwd, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08002500}
2501
Paul Lawrence7ee87cf2017-12-22 10:12:06 -08002502int cryptfs_enable_default(int no_ui) {
2503 return cryptfs_enable_internal(CRYPT_TYPE_DEFAULT, DEFAULT_PASSWORD, no_ui);
Paul Lawrence13486032014-02-03 13:28:11 -08002504}
2505
2506int cryptfs_changepw(int crypt_type, const char *newpw)
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002507{
Paul Crowley38132a12016-02-09 09:50:32 +00002508 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002509 SLOGE("cryptfs_changepw not valid for file encryption");
2510 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08002511 }
2512
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002513 struct crypt_mnt_ftr crypt_ftr;
JP Abgrall933216c2015-02-11 13:44:32 -08002514 int rc;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002515
2516 /* This is only allowed after we've successfully decrypted the master key */
Paul Lawrencef4faa572014-01-29 13:31:03 -08002517 if (!master_key_saved) {
Ken Sumrall0cc16632011-01-18 20:32:26 -08002518 SLOGE("Key not saved, aborting");
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002519 return -1;
2520 }
2521
Paul Lawrencef4faa572014-01-29 13:31:03 -08002522 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2523 SLOGE("Invalid crypt_type %d", crypt_type);
2524 return -1;
2525 }
2526
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002527 /* get key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002528 if (get_crypt_ftr_and_key(&crypt_ftr)) {
Paul Lawrencef4faa572014-01-29 13:31:03 -08002529 SLOGE("Error getting crypt footer and key");
2530 return -1;
Ken Sumrall8ddbe402011-01-17 15:26:29 -08002531 }
2532
Paul Lawrencef4faa572014-01-29 13:31:03 -08002533 crypt_ftr.crypt_type = crypt_type;
2534
JP Abgrall933216c2015-02-11 13:44:32 -08002535 rc = encrypt_master_key(crypt_type == CRYPT_TYPE_DEFAULT ? DEFAULT_PASSWORD
Paul Lawrencef4faa572014-01-29 13:31:03 -08002536 : newpw,
2537 crypt_ftr.salt,
2538 saved_master_key,
2539 crypt_ftr.master_key,
2540 &crypt_ftr);
JP Abgrall933216c2015-02-11 13:44:32 -08002541 if (rc) {
2542 SLOGE("Encrypt master key failed: %d", rc);
2543 return -1;
2544 }
Jason parks70a4b3f2011-01-28 10:10:47 -06002545 /* save the key */
Ken Sumrall160b4d62013-04-22 12:15:39 -07002546 put_crypt_ftr_and_key(&crypt_ftr);
Ken Sumrall8f869aa2010-12-03 03:47:09 -08002547
2548 return 0;
2549}
Ken Sumrall160b4d62013-04-22 12:15:39 -07002550
Rubin Xu85c01f92014-10-13 12:49:54 +01002551static unsigned int persist_get_max_entries(int encrypted) {
2552 struct crypt_mnt_ftr crypt_ftr;
2553 unsigned int dsize;
2554 unsigned int max_persistent_entries;
2555
2556 /* If encrypted, use the values from the crypt_ftr, otherwise
2557 * use the values for the current spec.
2558 */
2559 if (encrypted) {
2560 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2561 return -1;
2562 }
2563 dsize = crypt_ftr.persist_data_size;
2564 } else {
2565 dsize = CRYPT_PERSIST_DATA_SIZE;
2566 }
2567
2568 max_persistent_entries = (dsize - sizeof(struct crypt_persist_data)) /
2569 sizeof(struct crypt_persist_entry);
2570
2571 return max_persistent_entries;
2572}
2573
2574static int persist_get_key(const char *fieldname, char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002575{
2576 unsigned int i;
2577
2578 if (persist_data == NULL) {
2579 return -1;
2580 }
2581 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2582 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2583 /* We found it! */
2584 strlcpy(value, persist_data->persist_entry[i].val, PROPERTY_VALUE_MAX);
2585 return 0;
2586 }
2587 }
2588
2589 return -1;
2590}
2591
Rubin Xu85c01f92014-10-13 12:49:54 +01002592static int persist_set_key(const char *fieldname, const char *value, int encrypted)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002593{
2594 unsigned int i;
2595 unsigned int num;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002596 unsigned int max_persistent_entries;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002597
2598 if (persist_data == NULL) {
2599 return -1;
2600 }
2601
Rubin Xu85c01f92014-10-13 12:49:54 +01002602 max_persistent_entries = persist_get_max_entries(encrypted);
Ken Sumrall160b4d62013-04-22 12:15:39 -07002603
2604 num = persist_data->persist_valid_entries;
2605
2606 for (i = 0; i < num; i++) {
2607 if (!strncmp(persist_data->persist_entry[i].key, fieldname, PROPERTY_KEY_MAX)) {
2608 /* We found an existing entry, update it! */
2609 memset(persist_data->persist_entry[i].val, 0, PROPERTY_VALUE_MAX);
2610 strlcpy(persist_data->persist_entry[i].val, value, PROPERTY_VALUE_MAX);
2611 return 0;
2612 }
2613 }
2614
2615 /* We didn't find it, add it to the end, if there is room */
2616 if (persist_data->persist_valid_entries < max_persistent_entries) {
2617 memset(&persist_data->persist_entry[num], 0, sizeof(struct crypt_persist_entry));
2618 strlcpy(persist_data->persist_entry[num].key, fieldname, PROPERTY_KEY_MAX);
2619 strlcpy(persist_data->persist_entry[num].val, value, PROPERTY_VALUE_MAX);
2620 persist_data->persist_valid_entries++;
2621 return 0;
2622 }
2623
2624 return -1;
2625}
2626
Rubin Xu85c01f92014-10-13 12:49:54 +01002627/**
2628 * Test if key is part of the multi-entry (field, index) sequence. Return non-zero if key is in the
2629 * sequence and its index is greater than or equal to index. Return 0 otherwise.
2630 */
Jeff Sharkey95440eb2017-09-18 18:19:28 -06002631int match_multi_entry(const char *key, const char *field, unsigned index) {
2632 std::string key_ = key;
2633 std::string field_ = field;
Rubin Xu85c01f92014-10-13 12:49:54 +01002634
Jeff Sharkey95440eb2017-09-18 18:19:28 -06002635 std::string parsed_field;
2636 unsigned parsed_index;
2637
2638 std::string::size_type split = key_.find_last_of('_');
2639 if (split == std::string::npos) {
2640 parsed_field = key_;
2641 parsed_index = 0;
2642 } else {
2643 parsed_field = key_.substr(0, split);
2644 parsed_index = std::stoi(key_.substr(split + 1));
Rubin Xu85c01f92014-10-13 12:49:54 +01002645 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -06002646
2647 return parsed_field == field_ && parsed_index >= index;
Rubin Xu85c01f92014-10-13 12:49:54 +01002648}
2649
2650/*
2651 * Delete entry/entries from persist_data. If the entries are part of a multi-segment field, all
2652 * remaining entries starting from index will be deleted.
2653 * returns PERSIST_DEL_KEY_OK if deletion succeeds,
2654 * PERSIST_DEL_KEY_ERROR_NO_FIELD if the field does not exist,
2655 * and PERSIST_DEL_KEY_ERROR_OTHER if error occurs.
2656 *
2657 */
2658static int persist_del_keys(const char *fieldname, unsigned index)
2659{
2660 unsigned int i;
2661 unsigned int j;
2662 unsigned int num;
2663
2664 if (persist_data == NULL) {
2665 return PERSIST_DEL_KEY_ERROR_OTHER;
2666 }
2667
2668 num = persist_data->persist_valid_entries;
2669
2670 j = 0; // points to the end of non-deleted entries.
2671 // Filter out to-be-deleted entries in place.
2672 for (i = 0; i < num; i++) {
2673 if (!match_multi_entry(persist_data->persist_entry[i].key, fieldname, index)) {
2674 persist_data->persist_entry[j] = persist_data->persist_entry[i];
2675 j++;
2676 }
2677 }
2678
2679 if (j < num) {
2680 persist_data->persist_valid_entries = j;
2681 // Zeroise the remaining entries
2682 memset(&persist_data->persist_entry[j], 0, (num - j) * sizeof(struct crypt_persist_entry));
2683 return PERSIST_DEL_KEY_OK;
2684 } else {
2685 // Did not find an entry matching the given fieldname
2686 return PERSIST_DEL_KEY_ERROR_NO_FIELD;
2687 }
2688}
2689
2690static int persist_count_keys(const char *fieldname)
2691{
2692 unsigned int i;
2693 unsigned int count;
2694
2695 if (persist_data == NULL) {
2696 return -1;
2697 }
2698
2699 count = 0;
2700 for (i = 0; i < persist_data->persist_valid_entries; i++) {
2701 if (match_multi_entry(persist_data->persist_entry[i].key, fieldname, 0)) {
2702 count++;
2703 }
2704 }
2705
2706 return count;
2707}
2708
Ken Sumrall160b4d62013-04-22 12:15:39 -07002709/* Return the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01002710int cryptfs_getfield(const char *fieldname, char *value, int len)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002711{
Paul Crowley38132a12016-02-09 09:50:32 +00002712 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08002713 SLOGE("Cannot get field when file encrypted");
2714 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07002715 }
2716
Ken Sumrall160b4d62013-04-22 12:15:39 -07002717 char temp_value[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01002718 /* CRYPTO_GETFIELD_OK is success,
2719 * CRYPTO_GETFIELD_ERROR_NO_FIELD is value not set,
2720 * CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL is buffer (as given by len) too small,
2721 * CRYPTO_GETFIELD_ERROR_OTHER is any other error
Ken Sumrall160b4d62013-04-22 12:15:39 -07002722 */
Rubin Xu85c01f92014-10-13 12:49:54 +01002723 int rc = CRYPTO_GETFIELD_ERROR_OTHER;
2724 int i;
2725 char temp_field[PROPERTY_KEY_MAX];
Ken Sumrall160b4d62013-04-22 12:15:39 -07002726
2727 if (persist_data == NULL) {
2728 load_persistent_data();
2729 if (persist_data == NULL) {
2730 SLOGE("Getfield error, cannot load persistent data");
2731 goto out;
2732 }
2733 }
2734
Rubin Xu85c01f92014-10-13 12:49:54 +01002735 // Read value from persistent entries. If the original value is split into multiple entries,
2736 // stitch them back together.
Ken Sumrall160b4d62013-04-22 12:15:39 -07002737 if (!persist_get_key(fieldname, temp_value)) {
Rubin Xu85c01f92014-10-13 12:49:54 +01002738 // We found it, copy it to the caller's buffer and keep going until all entries are read.
2739 if (strlcpy(value, temp_value, len) >= (unsigned) len) {
2740 // value too small
2741 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
2742 goto out;
2743 }
2744 rc = CRYPTO_GETFIELD_OK;
2745
2746 for (i = 1; /* break explicitly */; i++) {
2747 if (snprintf(temp_field, sizeof(temp_field), "%s_%d", fieldname, i) >=
2748 (int) sizeof(temp_field)) {
2749 // If the fieldname is very long, we stop as soon as it begins to overflow the
2750 // maximum field length. At this point we have in fact fully read out the original
2751 // value because cryptfs_setfield would not allow fields with longer names to be
2752 // written in the first place.
2753 break;
2754 }
2755 if (!persist_get_key(temp_field, temp_value)) {
2756 if (strlcat(value, temp_value, len) >= (unsigned)len) {
2757 // value too small.
2758 rc = CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL;
2759 goto out;
2760 }
2761 } else {
2762 // Exhaust all entries.
2763 break;
2764 }
2765 }
Ken Sumrall160b4d62013-04-22 12:15:39 -07002766 } else {
2767 /* Sadness, it's not there. Return the error */
Rubin Xu85c01f92014-10-13 12:49:54 +01002768 rc = CRYPTO_GETFIELD_ERROR_NO_FIELD;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002769 }
2770
2771out:
2772 return rc;
2773}
2774
2775/* Set the value of the specified field. */
Rubin Xu85c01f92014-10-13 12:49:54 +01002776int cryptfs_setfield(const char *fieldname, const char *value)
Ken Sumrall160b4d62013-04-22 12:15:39 -07002777{
Paul Crowley38132a12016-02-09 09:50:32 +00002778 if (e4crypt_is_native()) {
Paul Lawrence5a06a642016-02-03 13:39:13 -08002779 SLOGE("Cannot set field when file encrypted");
2780 return -1;
Paul Lawrence368d7942015-04-15 14:12:00 -07002781 }
2782
Ken Sumrall160b4d62013-04-22 12:15:39 -07002783 char encrypted_state[PROPERTY_VALUE_MAX];
Rubin Xu85c01f92014-10-13 12:49:54 +01002784 /* 0 is success, negative values are error */
2785 int rc = CRYPTO_SETFIELD_ERROR_OTHER;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002786 int encrypted = 0;
Rubin Xu85c01f92014-10-13 12:49:54 +01002787 unsigned int field_id;
2788 char temp_field[PROPERTY_KEY_MAX];
2789 unsigned int num_entries;
2790 unsigned int max_keylen;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002791
2792 if (persist_data == NULL) {
2793 load_persistent_data();
2794 if (persist_data == NULL) {
2795 SLOGE("Setfield error, cannot load persistent data");
2796 goto out;
2797 }
2798 }
2799
2800 property_get("ro.crypto.state", encrypted_state, "");
2801 if (!strcmp(encrypted_state, "encrypted") ) {
2802 encrypted = 1;
2803 }
2804
Rubin Xu85c01f92014-10-13 12:49:54 +01002805 // Compute the number of entries required to store value, each entry can store up to
2806 // (PROPERTY_VALUE_MAX - 1) chars
2807 if (strlen(value) == 0) {
2808 // Empty value also needs one entry to store.
2809 num_entries = 1;
2810 } else {
2811 num_entries = (strlen(value) + (PROPERTY_VALUE_MAX - 1) - 1) / (PROPERTY_VALUE_MAX - 1);
2812 }
2813
2814 max_keylen = strlen(fieldname);
2815 if (num_entries > 1) {
2816 // Need an extra "_%d" suffix.
2817 max_keylen += 1 + log10(num_entries);
2818 }
2819 if (max_keylen > PROPERTY_KEY_MAX - 1) {
2820 rc = CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002821 goto out;
2822 }
2823
Rubin Xu85c01f92014-10-13 12:49:54 +01002824 // Make sure we have enough space to write the new value
2825 if (persist_data->persist_valid_entries + num_entries - persist_count_keys(fieldname) >
2826 persist_get_max_entries(encrypted)) {
2827 rc = CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG;
2828 goto out;
2829 }
2830
2831 // Now that we know persist_data has enough space for value, let's delete the old field first
2832 // to make up space.
2833 persist_del_keys(fieldname, 0);
2834
2835 if (persist_set_key(fieldname, value, encrypted)) {
2836 // fail to set key, should not happen as we have already checked the available space
2837 SLOGE("persist_set_key() error during setfield()");
2838 goto out;
2839 }
2840
2841 for (field_id = 1; field_id < num_entries; field_id++) {
Greg Kaiserb610e772018-02-09 09:19:54 -08002842 snprintf(temp_field, sizeof(temp_field), "%s_%u", fieldname, field_id);
Rubin Xu85c01f92014-10-13 12:49:54 +01002843
2844 if (persist_set_key(temp_field, value + field_id * (PROPERTY_VALUE_MAX - 1), encrypted)) {
2845 // fail to set key, should not happen as we have already checked the available space.
2846 SLOGE("persist_set_key() error during setfield()");
2847 goto out;
2848 }
2849 }
2850
Ken Sumrall160b4d62013-04-22 12:15:39 -07002851 /* If we are running encrypted, save the persistent data now */
2852 if (encrypted) {
2853 if (save_persistent_data()) {
2854 SLOGE("Setfield error, cannot save persistent data");
2855 goto out;
2856 }
2857 }
2858
Rubin Xu85c01f92014-10-13 12:49:54 +01002859 rc = CRYPTO_SETFIELD_OK;
Ken Sumrall160b4d62013-04-22 12:15:39 -07002860
2861out:
2862 return rc;
2863}
Paul Lawrencef4faa572014-01-29 13:31:03 -08002864
2865/* Checks userdata. Attempt to mount the volume if default-
2866 * encrypted.
2867 * On success trigger next init phase and return 0.
2868 * Currently do not handle failure - see TODO below.
2869 */
2870int cryptfs_mount_default_encrypted(void)
2871{
Paul Lawrence84274cc2016-04-15 15:41:33 -07002872 int crypt_type = cryptfs_get_password_type();
2873 if (crypt_type < 0 || crypt_type > CRYPT_TYPE_MAX_TYPE) {
2874 SLOGE("Bad crypt type - error");
2875 } else if (crypt_type != CRYPT_TYPE_DEFAULT) {
2876 SLOGD("Password is not default - "
2877 "starting min framework to prompt");
2878 property_set("vold.decrypt", "trigger_restart_min_framework");
2879 return 0;
2880 } else if (cryptfs_check_passwd(DEFAULT_PASSWORD) == 0) {
2881 SLOGD("Password is default - restarting filesystem");
2882 cryptfs_restart_internal(0);
2883 return 0;
Paul Lawrencef4faa572014-01-29 13:31:03 -08002884 } else {
Paul Lawrence84274cc2016-04-15 15:41:33 -07002885 SLOGE("Encrypted, default crypt type but can't decrypt");
Paul Lawrencef4faa572014-01-29 13:31:03 -08002886 }
2887
Paul Lawrence6bfed202014-07-28 12:47:22 -07002888 /** Corrupt. Allow us to boot into framework, which will detect bad
2889 crypto when it calls do_crypto_complete, then do a factory reset
Paul Lawrencef4faa572014-01-29 13:31:03 -08002890 */
Paul Lawrence6bfed202014-07-28 12:47:22 -07002891 property_set("vold.decrypt", "trigger_restart_min_framework");
Paul Lawrencef4faa572014-01-29 13:31:03 -08002892 return 0;
2893}
2894
2895/* Returns type of the password, default, pattern, pin or password.
2896 */
2897int cryptfs_get_password_type(void)
2898{
Paul Crowley38132a12016-02-09 09:50:32 +00002899 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002900 SLOGE("cryptfs_get_password_type not valid for file encryption");
2901 return -1;
Paul Lawrence05335c32015-03-05 09:46:23 -08002902 }
2903
Paul Lawrencef4faa572014-01-29 13:31:03 -08002904 struct crypt_mnt_ftr crypt_ftr;
2905
2906 if (get_crypt_ftr_and_key(&crypt_ftr)) {
2907 SLOGE("Error getting crypt footer and key\n");
2908 return -1;
2909 }
2910
Paul Lawrence6bfed202014-07-28 12:47:22 -07002911 if (crypt_ftr.flags & CRYPT_INCONSISTENT_STATE) {
2912 return -1;
2913 }
2914
Paul Lawrencef4faa572014-01-29 13:31:03 -08002915 return crypt_ftr.crypt_type;
2916}
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002917
Paul Lawrence05335c32015-03-05 09:46:23 -08002918const char* cryptfs_get_password()
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002919{
Paul Crowley38132a12016-02-09 09:50:32 +00002920 if (e4crypt_is_native()) {
Paul Lawrence7b6b5652016-02-02 11:14:59 -08002921 SLOGE("cryptfs_get_password not valid for file encryption");
2922 return 0;
Paul Lawrence05335c32015-03-05 09:46:23 -08002923 }
2924
Paul Lawrence399317e2014-03-10 13:20:50 -07002925 struct timespec now;
Paul Lawrenceef2b5be2014-11-11 12:47:03 -08002926 clock_gettime(CLOCK_BOOTTIME, &now);
Paul Lawrence399317e2014-03-10 13:20:50 -07002927 if (now.tv_sec < password_expiry_time) {
2928 return password;
2929 } else {
2930 cryptfs_clear_password();
2931 return 0;
2932 }
2933}
2934
2935void cryptfs_clear_password()
2936{
2937 if (password) {
2938 size_t len = strlen(password);
2939 memset(password, 0, len);
2940 free(password);
2941 password = 0;
2942 password_expiry_time = 0;
2943 }
Paul Lawrence684dbdf2014-02-07 12:07:22 -08002944}
Paul Lawrence731a7a22015-04-28 22:14:15 +00002945
Paul Lawrence0c247462015-10-29 10:30:57 -07002946int cryptfs_isConvertibleToFBE()
2947{
Paul Crowleye2ee1522017-09-26 14:05:26 -07002948 struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab_default, DATA_MNT_POINT);
Luis Hector Chavezbbb512d2018-05-30 15:47:50 -07002949 return (rec && fs_mgr_is_convertible_to_fbe(rec)) ? 1 : 0;
Paul Lawrence0c247462015-10-29 10:30:57 -07002950}